/*
  ==============================================================================

   This file is part of the JUCE library - "Jules' Utility Class Extensions"
   Copyright 2004-9 by Raw Material Software Ltd.

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

   JUCE can be redistributed and/or modified under the terms of the GNU General
   Public License (Version 2), as published by the Free Software Foundation.
   A copy of the license is included in the JUCE distribution, or can be found
   online at www.gnu.org/licenses.

   JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

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

   To release a closed-source product which uses JUCE, commercial licenses are
   available: visit www.rawmaterialsoftware.com/juce for more information.

  ==============================================================================
*/

#ifdef __JUCE_JUCEHEADER__
 #error
#endif


/*** Start of inlined file: juce_TargetPlatform.h ***/
#ifndef __JUCE_TARGETPLATFORM_JUCEHEADER__
#define __JUCE_TARGETPLATFORM_JUCEHEADER__

#if (defined (_WIN32) || defined (_WIN64))
  #define	   JUCE_WIN32 1
  #define	   JUCE_WINDOWS 1
#elif defined (LINUX) || defined (__linux__)
  #define	 JUCE_LINUX 1
#elif defined(__APPLE_CPP__) || defined(__APPLE_CC__)
  #include <CoreFoundation/CoreFoundation.h> // (needed to find out what platform we're using)

  #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
	#define	 JUCE_IPHONE 1
  #else
	#define	 JUCE_MAC 1
  #endif
#else
  #error "Unknown platform!"
#endif

#if JUCE_WINDOWS
  #ifdef _MSC_VER
	#ifdef _WIN64
	  #define JUCE_64BIT 1
	#else
	  #define JUCE_32BIT 1
	#endif
  #endif

  #ifdef _DEBUG
	#define JUCE_DEBUG 1
  #endif

  #ifdef __MINGW32__
	#define JUCE_MINGW 1
  #endif

  #define JUCE_LITTLE_ENDIAN 1

  #define JUCE_INTEL 1
#endif

#if JUCE_MAC

  #ifndef NDEBUG
	#define JUCE_DEBUG 1
  #endif

  #ifdef __LITTLE_ENDIAN__
	#define JUCE_LITTLE_ENDIAN 1
  #else
	#define JUCE_BIG_ENDIAN 1
  #endif

  #if defined (__ppc__) || defined (__ppc64__)
	#define JUCE_PPC 1
	#undef MAC_OS_X_VERSION_MAX_ALLOWED
	#define MAC_OS_X_VERSION_MAX_ALLOWED	MAC_OS_X_VERSION_10_4
  #else
	#define JUCE_INTEL 1
  #endif

  #ifdef __LP64__
	#define JUCE_64BIT 1
  #else
	#define JUCE_32BIT 1
  #endif

  #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
	#error "Building for OSX 10.3 is no longer supported!"
  #endif

  #ifndef MAC_OS_X_VERSION_10_5
	#error "To build with 10.4 compatibility, use a 10.5 or 10.6 SDK and set the deployment target to 10.4"
  #endif

#endif

#if JUCE_IPHONE

  #ifndef NDEBUG
	#define JUCE_DEBUG 1
  #endif

  #ifdef __LITTLE_ENDIAN__
	#define JUCE_LITTLE_ENDIAN 1
  #else
	#define JUCE_BIG_ENDIAN 1
  #endif
#endif

#if JUCE_LINUX

  #ifdef _DEBUG
	#define JUCE_DEBUG 1
  #endif

  // Allow override for big-endian Linux platforms
  #ifndef JUCE_BIG_ENDIAN
	#define JUCE_LITTLE_ENDIAN 1
  #endif

  #if defined (__LP64__) || defined (_LP64)
	#define JUCE_64BIT 1
  #else
	#define JUCE_32BIT 1
  #endif

  #define JUCE_INTEL 1
#endif

// Compiler type macros.

#ifdef __GNUC__
  #define JUCE_GCC 1
#elif defined (_MSC_VER)
  #define JUCE_MSVC 1

  #if _MSC_VER >= 1400
	#define JUCE_USE_INTRINSICS 1
  #endif
#else
  #error unknown compiler
#endif

#endif   // __JUCE_TARGETPLATFORM_JUCEHEADER__
/*** End of inlined file: juce_TargetPlatform.h ***/

 // FORCE_AMALGAMATOR_INCLUDE

/*** Start of inlined file: juce_Config.h ***/
#ifndef __JUCE_CONFIG_JUCEHEADER__
#define __JUCE_CONFIG_JUCEHEADER__

#ifndef JUCE_NAMESPACE
  #define JUCE_NAMESPACE juce
#endif

#ifndef JUCE_FORCE_DEBUG
  #define JUCE_FORCE_DEBUG 0
#endif

#ifndef JUCE_LOG_ASSERTIONS
  #define JUCE_LOG_ASSERTIONS 0
#endif

#ifndef JUCE_ASIO
  #define JUCE_ASIO 0
#endif

#ifndef JUCE_WASAPI
  #define JUCE_WASAPI 0
#endif

#ifndef JUCE_DIRECTSOUND
  #define JUCE_DIRECTSOUND 1
#endif

#ifndef JUCE_ALSA
  #define JUCE_ALSA 1
#endif

#ifndef JUCE_JACK
  #define JUCE_JACK 0
#endif

#if ! (defined (JUCE_QUICKTIME) || JUCE_LINUX || JUCE_IPHONE || (JUCE_WINDOWS && ! JUCE_MSVC))
  #define JUCE_QUICKTIME 0
#endif

#if (JUCE_IPHONE || JUCE_LINUX) && JUCE_QUICKTIME
  #undef JUCE_QUICKTIME
#endif

#ifndef JUCE_OPENGL
  #define JUCE_OPENGL 1
#endif

#ifndef JUCE_USE_FLAC
  #define JUCE_USE_FLAC 1
#endif

#ifndef JUCE_USE_OGGVORBIS
  #define JUCE_USE_OGGVORBIS 1
#endif

#if (! defined (JUCE_USE_CDBURNER)) && ! (JUCE_WINDOWS && ! JUCE_MSVC)
  #define JUCE_USE_CDBURNER 0
#endif

#ifndef JUCE_USE_CDREADER
  #define JUCE_USE_CDREADER 0
#endif

#if (JUCE_QUICKTIME || JUCE_WINDOWS) && ! defined (JUCE_USE_CAMERA)
  #define JUCE_USE_CAMERA 0
#endif

#ifndef JUCE_ENABLE_REPAINT_DEBUGGING
  #define JUCE_ENABLE_REPAINT_DEBUGGING 0
#endif

#ifndef JUCE_USE_XINERAMA
  #define JUCE_USE_XINERAMA 1
#endif

#ifndef JUCE_USE_XSHM
  #define JUCE_USE_XSHM 1
#endif

#ifndef JUCE_USE_XRENDER
  #define JUCE_USE_XRENDER 0
#endif

#ifndef JUCE_USE_XCURSOR
  #define JUCE_USE_XCURSOR 1
#endif

#ifndef JUCE_PLUGINHOST_VST
  #define JUCE_PLUGINHOST_VST 0
#endif

#ifndef JUCE_PLUGINHOST_AU
  #define JUCE_PLUGINHOST_AU 0
#endif

#ifndef JUCE_ONLY_BUILD_CORE_LIBRARY
  #define JUCE_ONLY_BUILD_CORE_LIBRARY  0
#endif

#ifndef JUCE_WEB_BROWSER
  #define JUCE_WEB_BROWSER 0
#endif

#ifndef JUCE_SUPPORT_CARBON
  #define JUCE_SUPPORT_CARBON 1
#endif

#ifndef JUCE_INCLUDE_ZLIB_CODE
  #define JUCE_INCLUDE_ZLIB_CODE	1
#endif

#ifndef JUCE_INCLUDE_FLAC_CODE
  #define JUCE_INCLUDE_FLAC_CODE	1
#endif

#ifndef JUCE_INCLUDE_OGGVORBIS_CODE
  #define JUCE_INCLUDE_OGGVORBIS_CODE   1
#endif

#ifndef JUCE_INCLUDE_PNGLIB_CODE
  #define JUCE_INCLUDE_PNGLIB_CODE	  1
#endif

#ifndef JUCE_INCLUDE_JPEGLIB_CODE
  #define JUCE_INCLUDE_JPEGLIB_CODE	 1
#endif

#ifndef JUCE_CHECK_MEMORY_LEAKS
  #define JUCE_CHECK_MEMORY_LEAKS 1
#endif

#ifndef JUCE_CATCH_UNHANDLED_EXCEPTIONS
  #define JUCE_CATCH_UNHANDLED_EXCEPTIONS 1
#endif

// If only building the core classes, we can explicitly turn off some features to avoid including them:
#if JUCE_ONLY_BUILD_CORE_LIBRARY
  #undef  JUCE_QUICKTIME
  #define JUCE_QUICKTIME 0
  #undef  JUCE_OPENGL
  #define JUCE_OPENGL 0
  #undef JUCE_USE_CDBURNER
  #define JUCE_USE_CDBURNER 0
  #undef JUCE_USE_CDREADER
  #define JUCE_USE_CDREADER 0
  #undef JUCE_WEB_BROWSER
  #define JUCE_WEB_BROWSER 0
  #undef JUCE_PLUGINHOST_AU
  #define JUCE_PLUGINHOST_AU 0
  #undef JUCE_PLUGINHOST_VST
  #define JUCE_PLUGINHOST_VST 0
#endif

#endif
/*** End of inlined file: juce_Config.h ***/

  // FORCE_AMALGAMATOR_INCLUDE

#ifndef JUCE_BUILD_CORE
 #define JUCE_BUILD_CORE 1
#endif
#ifndef JUCE_BUILD_MISC
 #define JUCE_BUILD_MISC 1
#endif
#ifndef JUCE_BUILD_GUI
 #define JUCE_BUILD_GUI 1
#endif
#ifndef JUCE_BUILD_NATIVE
 #define JUCE_BUILD_NATIVE 1
#endif

#if JUCE_ONLY_BUILD_CORE_LIBRARY
 #undef JUCE_BUILD_MISC
 #undef JUCE_BUILD_GUI
#endif

//==============================================================================
#if JUCE_BUILD_NATIVE || JUCE_BUILD_CORE || (JUCE_BUILD_MISC && (JUCE_PLUGINHOST_VST || JUCE_PLUGINHOST_AU))
 #if JUCE_WINDOWS

/*** Start of inlined file: juce_win32_NativeIncludes.h ***/
#ifndef __JUCE_WIN32_NATIVEINCLUDES_JUCEHEADER__
#define __JUCE_WIN32_NATIVEINCLUDES_JUCEHEADER__

#ifndef STRICT
 #define STRICT 1
#endif

#undef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1

#ifdef _MSC_VER
  #pragma warning (push)
  #pragma warning (disable : 4100 4201 4514 4312 4995)
#endif

#define _WIN32_WINNT 0x0500
#define _UNICODE 1
#define UNICODE 1

#ifndef _WIN32_IE
 #define _WIN32_IE 0x0400
#endif

#include <windows.h>
#include <windowsx.h>
#include <commdlg.h>
#include <shellapi.h>
#include <mmsystem.h>
#include <vfw.h>
#include <tchar.h>
#include <stddef.h>
#include <ctime>
#include <wininet.h>
#include <nb30.h>
#include <iphlpapi.h>
#include <mapi.h>
#include <float.h>
#include <process.h>
#include <Exdisp.h>
#include <exdispid.h>
#include <shlobj.h>

#if ! JUCE_MINGW
 #include <crtdbg.h>
 #include <comutil.h>
#endif

#if JUCE_OPENGL
 #include <gl/gl.h>
#endif

#undef PACKED

#if JUCE_ASIO
 #include "iasiodrv.h"
#endif

#if JUCE_USE_CDBURNER

 #include <imapi.h>
 #include <imapierror.h>
#endif

#if JUCE_USE_CAMERA

 #include <dshow.h>
 #include <qedit.h>
 #include <dshowasf.h>
#endif

#if JUCE_WASAPI
 #include <MMReg.h>
 #include <mmdeviceapi.h>
 #include <Audioclient.h>
 #include <Avrt.h>
 #include <functiondiscoverykeys.h>
#endif

#if JUCE_QUICKTIME

 #include <Movies.h>
 #include <QTML.h>
 #include <QuickTimeComponents.h>
 #include <MediaHandlers.h>
 #include <ImageCodec.h>

 #import <QTOLibrary.dll>
 #import <QTOControl.dll>
#endif

#ifdef _MSC_VER
 #pragma warning (pop)
#endif

template <class T>
class ComSmartPtr
{
public:
	ComSmartPtr() throw() : p (0)			   {}
	ComSmartPtr (T* const p_) : p (p_)		  { if (p_ != 0) p_->AddRef(); }
	ComSmartPtr (const ComSmartPtr<T>& p_) : p (p_.p)   { if (p != 0) p->AddRef(); }
	~ComSmartPtr()					  { if (p != 0) p->Release(); }

	operator T*() const throw()	 { return p; }
	T& operator*() const throw()	{ return *p; }
	T** operator&() throw()	 { return &p; }
	T* operator->() const throw()   { return p; }

	T* operator= (T* const newP)
	{
		if (newP != 0)
			newP->AddRef();

		if (p != 0)
			p->Release();

		p = newP;
		return newP;
	}

	T* operator= (const ComSmartPtr<T>& newP)  { return operator= (newP.p); }

	HRESULT CoCreateInstance (REFCLSID rclsid, DWORD dwClsContext)
	{
#ifndef __MINGW32__
		operator= (0);
		return ::CoCreateInstance (rclsid, 0, dwClsContext, __uuidof(T), (void**) &p);
#else
		return S_FALSE;
#endif
	}

	T* p;
};

#endif   // __JUCE_WIN32_NATIVEINCLUDES_JUCEHEADER__
/*** End of inlined file: juce_win32_NativeIncludes.h ***/


 #elif JUCE_LINUX

/*** Start of inlined file: juce_linux_NativeIncludes.h ***/
#ifndef __JUCE_LINUX_NATIVEINCLUDES_JUCEHEADER__
#define __JUCE_LINUX_NATIVEINCLUDES_JUCEHEADER__

#include <sched.h>
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <sys/ptrace.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <fnmatch.h>
#include <utime.h>
#include <pwd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <sys/sysinfo.h>
#include <sys/file.h>
#include <signal.h>

#include <ft2build.h>
#include FT_FREETYPE_H

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#include <X11/Xmd.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>

#if JUCE_USE_XINERAMA
 #include <X11/extensions/Xinerama.h>
#endif

#if JUCE_USE_XSHM
 #include <X11/extensions/XShm.h>
 #include <sys/shm.h>
 #include <sys/ipc.h>
#endif

#if JUCE_USE_XRENDER
 // If you're missing these headers, try installing the libxrender-dev and libxcomposite-dev
 #include <X11/extensions/Xrender.h>
 #include <X11/extensions/Xcomposite.h>
#endif

#if JUCE_USE_XCURSOR
 // If you're missing this header, try installing the libxcursor-dev package
 #include <X11/Xcursor/Xcursor.h>
#endif

#if JUCE_OPENGL
 #include <GL/glx.h>
#endif

#undef KeyPress

#if JUCE_ALSA
 #include <alsa/asoundlib.h>
#endif

#if JUCE_JACK
 #include <jack/jack.h>
 //#include <jack/transport.h>
#endif

#undef SIZEOF

#endif   // __JUCE_LINUX_NATIVEINCLUDES_JUCEHEADER__
/*** End of inlined file: juce_linux_NativeIncludes.h ***/


 #elif JUCE_MAC || JUCE_IPHONE

/*** Start of inlined file: juce_mac_NativeIncludes.h ***/
#ifndef __JUCE_MAC_NATIVEINCLUDES_JUCEHEADER__
#define __JUCE_MAC_NATIVEINCLUDES_JUCEHEADER__

#define USE_COREGRAPHICS_RENDERING 1

#if JUCE_IPHONE
 #import <Foundation/Foundation.h>
 #import <UIKit/UIKit.h>
 #import <AudioToolbox/AudioToolbox.h>
 #import <AVFoundation/AVFoundation.h>
 #import <CoreData/CoreData.h>
 #import <MobileCoreServices/MobileCoreServices.h>
 #import <QuartzCore/QuartzCore.h>
 #include <sys/fcntl.h>
 #if JUCE_OPENGL
  #include <OpenGLES/ES1/gl.h>
  #include <OpenGLES/ES1/glext.h>
 #endif
#else
 #import <Cocoa/Cocoa.h>
 #import <CoreAudio/HostTime.h>
 #import <CoreAudio/AudioHardware.h>
 #import <CoreMIDI/MIDIServices.h>
 #import <QTKit/QTKit.h>
 #import <WebKit/WebKit.h>
 #import <DiscRecording/DiscRecording.h>
 #import <IOKit/IOKitLib.h>
 #import <IOKit/IOCFPlugIn.h>
 #import <IOKit/hid/IOHIDLib.h>
 #import <IOKit/hid/IOHIDKeys.h>
 #import <IOKit/pwr_mgt/IOPMLib.h>
 #include <Carbon/Carbon.h>
 #include <sys/dir.h>
#endif

#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <fnmatch.h>
#include <utime.h>
#include <dlfcn.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#include <mach/mach_time.h>

#if MACOS_10_4_OR_EARLIER
 #include <GLUT/glut.h>
#endif

#if ! CGFLOAT_DEFINED
  #define CGFloat float
#endif

#endif   // __JUCE_MAC_NATIVEINCLUDES_JUCEHEADER__
/*** End of inlined file: juce_mac_NativeIncludes.h ***/


 #else
  #error "Unknown platform!"
 #endif
#endif

//==============================================================================
#define DONT_SET_USING_JUCE_NAMESPACE 1
#undef max
#undef min
#define NO_DUMMY_DECL

#if JUCE_BUILD_NATIVE
 #include "juce_amalgamated.h"  // FORCE_AMALGAMATOR_INCLUDE
#endif

#if (defined(_MSC_VER) && (_MSC_VER <= 1200))
 #pragma warning (disable: 4309 4305)
#endif

#if JUCE_MAC && JUCE_32BIT && JUCE_SUPPORT_CARBON && JUCE_BUILD_NATIVE && ! JUCE_ONLY_BUILD_CORE_LIBRARY
 BEGIN_JUCE_NAMESPACE

/*** Start of inlined file: juce_mac_CarbonViewWrapperComponent.h ***/
#ifndef __JUCE_MAC_CARBONVIEWWRAPPERCOMPONENT_JUCEHEADER__
#define __JUCE_MAC_CARBONVIEWWRAPPERCOMPONENT_JUCEHEADER__

class CarbonViewWrapperComponent  : public Component,
									public ComponentMovementWatcher,
									public Timer
{
public:
	CarbonViewWrapperComponent()
		: ComponentMovementWatcher (this),
		  wrapperWindow (0),
		  embeddedView (0),
		  recursiveResize (false)
	{
	}

	virtual ~CarbonViewWrapperComponent()
	{
		jassert (embeddedView == 0); // must call deleteWindow() in the subclass's destructor!
	}

	virtual HIViewRef attachView (WindowRef windowRef, HIViewRef rootView) = 0;
	virtual void removeView (HIViewRef embeddedView) = 0;
	virtual void mouseDown (int x, int y) {}
	virtual void paint() {}

	virtual bool getEmbeddedViewSize (int& w, int& h)
	{
		if (embeddedView == 0)
			return false;

		HIRect bounds;
		HIViewGetBounds (embeddedView, &bounds);
		w = jmax (1, roundToInt (bounds.size.width));
		h = jmax (1, roundToInt (bounds.size.height));
		return true;
	}

	void createWindow()
	{
		if (wrapperWindow == 0)
		{
			Rect r;
			r.left = getScreenX();
			r.top = getScreenY();
			r.right = r.left + getWidth();
			r.bottom = r.top + getHeight();

			CreateNewWindow (kDocumentWindowClass,
							 (WindowAttributes) (kWindowStandardHandlerAttribute | kWindowCompositingAttribute
												  | kWindowNoShadowAttribute | kWindowNoTitleBarAttribute),
							 &r, &wrapperWindow);

			jassert (wrapperWindow != 0);
			if (wrapperWindow == 0)
				return;

			NSWindow* carbonWindow = [[NSWindow alloc] initWithWindowRef: wrapperWindow];
			NSWindow* ownerWindow = [((NSView*) getWindowHandle()) window];

			[ownerWindow addChildWindow: carbonWindow
								ordered: NSWindowAbove];

			embeddedView = attachView (wrapperWindow, HIViewGetRoot (wrapperWindow));

			EventTypeSpec  windowEventTypes[] = { { kEventClassWindow, kEventWindowGetClickActivation },
												  { kEventClassWindow, kEventWindowHandleDeactivate } };

			EventHandlerUPP upp = NewEventHandlerUPP (carbonEventCallback);
			InstallWindowEventHandler (wrapperWindow, upp,
									   sizeof (windowEventTypes) / sizeof (EventTypeSpec),
									   windowEventTypes, this, &eventHandlerRef);

			setOurSizeToEmbeddedViewSize();
			setEmbeddedWindowToOurSize();

			creationTime = Time::getCurrentTime();
		}
	}

	void deleteWindow()
	{
		removeView (embeddedView);
		embeddedView = 0;

		if (wrapperWindow != 0)
		{
			RemoveEventHandler (eventHandlerRef);
			DisposeWindow (wrapperWindow);
			wrapperWindow = 0;
		}
	}

	void setOurSizeToEmbeddedViewSize()
	{
		int w, h;
		if (getEmbeddedViewSize (w, h))
		{
			if (w != getWidth() || h != getHeight())
			{
				startTimer (50);

				setSize (w, h);
				if (getParentComponent() != 0)
					getParentComponent()->setSize (w, h);
			}
			else
			{
				startTimer (jlimit (50, 500, getTimerInterval() + 20));
			}
		}
		else
		{
			stopTimer();
		}
	}

	void setEmbeddedWindowToOurSize()
	{
		if (! recursiveResize)
		{
			recursiveResize = true;

			if (embeddedView != 0)
			{
				HIRect r;
				r.origin.x = 0;
				r.origin.y = 0;
				r.size.width = (float) getWidth();
				r.size.height = (float) getHeight();
				HIViewSetFrame (embeddedView, &r);
			}

			if (wrapperWindow != 0)
			{
				Rect wr;
				wr.left = getScreenX();
				wr.top = getScreenY();
				wr.right = wr.left + getWidth();
				wr.bottom = wr.top + getHeight();

				SetWindowBounds (wrapperWindow, kWindowContentRgn, &wr);
				ShowWindow (wrapperWindow);
			}

			recursiveResize = false;
		}
	}

	void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
	{
		setEmbeddedWindowToOurSize();
	}

	void componentPeerChanged()
	{
		deleteWindow();
		createWindow();
	}

	void componentVisibilityChanged (Component&)
	{
		if (isShowing())
			createWindow();
		else
			deleteWindow();

		setEmbeddedWindowToOurSize();
	}

	static void recursiveHIViewRepaint (HIViewRef view)
	{
		HIViewSetNeedsDisplay (view, true);
		HIViewRef child = HIViewGetFirstSubview (view);

		while (child != 0)
		{
			recursiveHIViewRepaint (child);
			child = HIViewGetNextView (child);
		}
	}

	void timerCallback()
	{
		setOurSizeToEmbeddedViewSize();

		// To avoid strange overpainting problems when the UI is first opened, we'll
		// repaint it a few times during the first second that it's on-screen..
		if ((Time::getCurrentTime() - creationTime).inMilliseconds() < 1000)
			recursiveHIViewRepaint (HIViewGetRoot (wrapperWindow));
	}

	OSStatus carbonEventHandler (EventHandlerCallRef nextHandlerRef,
								 EventRef event)
	{
		switch (GetEventKind (event))
		{
			case kEventWindowHandleDeactivate:
				ActivateWindow (wrapperWindow, TRUE);
				break;

			case kEventWindowGetClickActivation:
			{
				getTopLevelComponent()->toFront (false);

				ClickActivationResult howToHandleClick = kActivateAndHandleClick;

				SetEventParameter (event, kEventParamClickActivation, typeClickActivationResult,
								   sizeof (ClickActivationResult), &howToHandleClick);

				HIViewSetNeedsDisplay (embeddedView, true);
			}
			break;
		}

		return noErr;
	}

	static pascal OSStatus carbonEventCallback (EventHandlerCallRef nextHandlerRef,
												EventRef event, void* userData)
	{
		return ((CarbonViewWrapperComponent*) userData)->carbonEventHandler (nextHandlerRef, event);
	}

protected:
	WindowRef wrapperWindow;
	HIViewRef embeddedView;
	bool recursiveResize;
	Time creationTime;

	EventHandlerRef eventHandlerRef;
};

#endif   // __JUCE_MAC_CARBONVIEWWRAPPERCOMPONENT_JUCEHEADER__
/*** End of inlined file: juce_mac_CarbonViewWrapperComponent.h ***/


 END_JUCE_NAMESPACE
#endif

#define JUCE_AMALGAMATED_TEMPLATE 1

//==============================================================================
#if JUCE_BUILD_CORE

/*** Start of inlined file: juce_FileLogger.cpp ***/
BEGIN_JUCE_NAMESPACE

FileLogger::FileLogger (const File& logFile_,
						const String& welcomeMessage,
						const int maxInitialFileSizeBytes)
	: logFile (logFile_)
{
	if (maxInitialFileSizeBytes >= 0)
		trimFileSize (maxInitialFileSizeBytes);

	if (! logFile_.exists())
	{
		// do this so that the parent directories get created..
		logFile_.create();
	}

	logStream = logFile_.createOutputStream (256);
	jassert (logStream != 0);

	String welcome;
	welcome << "\r\n**********************************************************\r\n"
			<< welcomeMessage
			<< "\r\nLog started: " << Time::getCurrentTime().toString (true, true)
			<< "\r\n";

	logMessage (welcome);
}

FileLogger::~FileLogger()
{
}

void FileLogger::logMessage (const String& message)
{
	if (logStream != 0)
	{
		DBG (message);

		const ScopedLock sl (logLock);
		(*logStream) << message << "\r\n";
		logStream->flush();
	}
}

void FileLogger::trimFileSize (int maxFileSizeBytes) const
{
	if (maxFileSizeBytes <= 0)
	{
		logFile.deleteFile();
	}
	else
	{
		const int64 fileSize = logFile.getSize();

		if (fileSize > maxFileSizeBytes)
		{
			ScopedPointer <FileInputStream> in (logFile.createInputStream());
			jassert (in != 0);

			if (in != 0)
			{
				in->setPosition (fileSize - maxFileSizeBytes);
				String content;

				{
					MemoryBlock contentToSave;
					contentToSave.setSize (maxFileSizeBytes + 4);
					contentToSave.fillWith (0);

					in->read (contentToSave.getData(), maxFileSizeBytes);
					in = 0;

					content = contentToSave.toString();
				}

				int newStart = 0;

				while (newStart < fileSize
						&& content[newStart] != '\n'
						&& content[newStart] != '\r')
					++newStart;

				logFile.deleteFile();
				logFile.appendText (content.substring (newStart), false, false);
			}
		}
	}
}

FileLogger* FileLogger::createDefaultAppLogger (const String& logFileSubDirectoryName,
												const String& logFileName,
												const String& welcomeMessage,
												const int maxInitialFileSizeBytes)
{
#if JUCE_MAC
	File logFile ("~/Library/Logs");
	logFile = logFile.getChildFile (logFileSubDirectoryName)
					 .getChildFile (logFileName);

#else
	File logFile (File::getSpecialLocation (File::userApplicationDataDirectory));

	if (logFile.isDirectory())
	{
		logFile = logFile.getChildFile (logFileSubDirectoryName)
						 .getChildFile (logFileName);
	}
#endif

	return new FileLogger (logFile, welcomeMessage, maxInitialFileSizeBytes);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileLogger.cpp ***/



/*** Start of inlined file: juce_Logger.cpp ***/
BEGIN_JUCE_NAMESPACE

Logger::Logger()
{
}

Logger::~Logger()
{
}

static Logger* currentLogger = 0;

void Logger::setCurrentLogger (Logger* const newLogger,
							   const bool deleteOldLogger)
{
	Logger* const oldLogger = currentLogger;
	currentLogger = newLogger;

	if (deleteOldLogger)
		delete oldLogger;
}

void Logger::writeToLog (const String& message)
{
	if (currentLogger != 0)
		currentLogger->logMessage (message);
	else
		outputDebugString (message);
}

#if JUCE_LOG_ASSERTIONS
void JUCE_API juce_LogAssertion (const char* filename, const int lineNum) throw()
{
	String m ("JUCE Assertion failure in ");
	m << filename << ", line " << lineNum;

	Logger::writeToLog (m);
}
#endif

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Logger.cpp ***/


/*** Start of inlined file: juce_Random.cpp ***/
BEGIN_JUCE_NAMESPACE

Random::Random (const int64 seedValue) throw()
	: seed (seedValue)
{
}

Random::~Random() throw()
{
}

void Random::setSeed (const int64 newSeed) throw()
{
	seed = newSeed;
}

void Random::combineSeed (const int64 seedValue) throw()
{
	seed ^= nextInt64() ^ seedValue;
}

void Random::setSeedRandomly()
{
	combineSeed ((int64) (pointer_sized_int) this);
	combineSeed (Time::getMillisecondCounter());
	combineSeed (Time::getHighResolutionTicks());
	combineSeed (Time::getHighResolutionTicksPerSecond());
	combineSeed (Time::currentTimeMillis());
}

int Random::nextInt() throw()
{
	seed = (seed * literal64bit (0x5deece66d) + 11) & literal64bit (0xffffffffffff);

	return (int) (seed >> 16);
}

int Random::nextInt (const int maxValue) throw()
{
	jassert (maxValue > 0);
	return (nextInt() & 0x7fffffff) % maxValue;
}

int64 Random::nextInt64() throw()
{
	return (((int64) nextInt()) << 32) | (int64) (uint64) (uint32) nextInt();
}

bool Random::nextBool() throw()
{
	return (nextInt() & 0x80000000) != 0;
}

float Random::nextFloat() throw()
{
	return static_cast <uint32> (nextInt()) / (float) 0xffffffff;
}

double Random::nextDouble() throw()
{
	return static_cast <uint32> (nextInt()) / (double) 0xffffffff;
}

const BigInteger Random::nextLargeNumber (const BigInteger& maximumValue)
{
	BigInteger n;

	do
	{
		fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1);
	}
	while (n >= maximumValue);

	return n;
}

void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits)
{
	arrayToChange.setBit (startBit + numBits - 1, true);  // to force the array to pre-allocate space

	while ((startBit & 31) != 0 && numBits > 0)
	{
		arrayToChange.setBit (startBit++, nextBool());
		--numBits;
	}

	while (numBits >= 32)
	{
		arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt());
		startBit += 32;
		numBits -= 32;
	}

	while (--numBits >= 0)
		arrayToChange.setBit (startBit + numBits, nextBool());
}

Random& Random::getSystemRandom() throw()
{
	static Random sysRand (1);
	return sysRand;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Random.cpp ***/


/*** Start of inlined file: juce_RelativeTime.cpp ***/
BEGIN_JUCE_NAMESPACE

RelativeTime::RelativeTime (const double seconds_) throw()
	: seconds (seconds_)
{
}

RelativeTime::RelativeTime (const RelativeTime& other) throw()
	: seconds (other.seconds)
{
}

RelativeTime::~RelativeTime() throw()
{
}

const RelativeTime RelativeTime::milliseconds (const int milliseconds) throw()
{
	return RelativeTime (milliseconds * 0.001);
}

const RelativeTime RelativeTime::milliseconds (const int64 milliseconds) throw()
{
	return RelativeTime (milliseconds * 0.001);
}

const RelativeTime RelativeTime::minutes (const double numberOfMinutes) throw()
{
	return RelativeTime (numberOfMinutes * 60.0);
}

const RelativeTime RelativeTime::hours (const double numberOfHours) throw()
{
	return RelativeTime (numberOfHours * (60.0 * 60.0));
}

const RelativeTime RelativeTime::days (const double numberOfDays) throw()
{
	return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0));
}

const RelativeTime RelativeTime::weeks (const double numberOfWeeks) throw()
{
	return RelativeTime (numberOfWeeks * (60.0 * 60.0 * 24.0 * 7.0));
}

int64 RelativeTime::inMilliseconds() const throw()
{
	return (int64)(seconds * 1000.0);
}

double RelativeTime::inMinutes() const throw()
{
	return seconds / 60.0;
}

double RelativeTime::inHours() const throw()
{
	return seconds / (60.0 * 60.0);
}

double RelativeTime::inDays() const throw()
{
	return seconds / (60.0 * 60.0 * 24.0);
}

double RelativeTime::inWeeks() const throw()
{
	return seconds / (60.0 * 60.0 * 24.0 * 7.0);
}

const String RelativeTime::getDescription (const String& returnValueForZeroTime) const throw()
{
	if (seconds < 0.001 && seconds > -0.001)
		return returnValueForZeroTime;

	String result;

	if (seconds < 0)
		result = "-";

	int fieldsShown = 0;
	int n = abs ((int) inWeeks());
	if (n > 0)
	{
		result << n << ((n == 1) ? TRANS(" week ")
								 : TRANS(" weeks "));
		++fieldsShown;
	}

	n = abs ((int) inDays()) % 7;
	if (n > 0)
	{
		result << n << ((n == 1) ? TRANS(" day ")
								 : TRANS(" days "));
		++fieldsShown;
	}

	if (fieldsShown < 2)
	{
		n = abs ((int) inHours()) % 24;
		if (n > 0)
		{
			result << n << ((n == 1) ? TRANS(" hr ")
									 : TRANS(" hrs "));
			++fieldsShown;
		}

		if (fieldsShown < 2)
		{
			n = abs ((int) inMinutes()) % 60;
			if (n > 0)
			{
				result << n << ((n == 1) ? TRANS(" min ")
										 : TRANS(" mins "));
				++fieldsShown;
			}

			if (fieldsShown < 2)
			{
				n = abs ((int) inSeconds()) % 60;
				if (n > 0)
				{
					result << n << ((n == 1) ? TRANS(" sec ")
											 : TRANS(" secs "));
					++fieldsShown;
				}

				if (fieldsShown < 1)
				{
					n = abs ((int) inMilliseconds()) % 1000;
					if (n > 0)
					{
						result << n << TRANS(" ms");
						++fieldsShown;
					}
				}
			}
		}
	}

	return result.trimEnd();
}

RelativeTime& RelativeTime::operator= (const RelativeTime& other) throw()
{
	seconds = other.seconds;
	return *this;
}

bool RelativeTime::operator== (const RelativeTime& other) const throw()
{
	return seconds == other.seconds;
}

bool RelativeTime::operator!= (const RelativeTime& other) const throw()
{
	return seconds != other.seconds;
}

bool RelativeTime::operator>  (const RelativeTime& other) const throw()
{
	return seconds > other.seconds;
}

bool RelativeTime::operator<  (const RelativeTime& other) const throw()
{
	return seconds < other.seconds;
}

bool RelativeTime::operator>= (const RelativeTime& other) const throw()
{
	return seconds >= other.seconds;
}

bool RelativeTime::operator<= (const RelativeTime& other) const throw()
{
	return seconds <= other.seconds;
}

const RelativeTime RelativeTime::operator+ (const RelativeTime& timeToAdd) const throw()
{
	return RelativeTime (seconds + timeToAdd.seconds);
}

const RelativeTime RelativeTime::operator- (const RelativeTime& timeToSubtract) const throw()
{
	return RelativeTime (seconds - timeToSubtract.seconds);
}

const RelativeTime RelativeTime::operator+ (const double secondsToAdd) const throw()
{
	return RelativeTime (seconds + secondsToAdd);
}

const RelativeTime RelativeTime::operator- (const double secondsToSubtract) const throw()
{
	return RelativeTime (seconds - secondsToSubtract);
}

const RelativeTime& RelativeTime::operator+= (const RelativeTime& timeToAdd) throw()
{
	seconds += timeToAdd.seconds;
	return *this;
}

const RelativeTime& RelativeTime::operator-= (const RelativeTime& timeToSubtract) throw()
{
	seconds -= timeToSubtract.seconds;
	return *this;
}

const RelativeTime& RelativeTime::operator+= (const double secondsToAdd) throw()
{
	seconds += secondsToAdd;
	return *this;
}

const RelativeTime& RelativeTime::operator-= (const double secondsToSubtract) throw()
{
	seconds -= secondsToSubtract;
	return *this;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_RelativeTime.cpp ***/


/*** Start of inlined file: juce_SystemStats.cpp ***/
BEGIN_JUCE_NAMESPACE

const String SystemStats::getJUCEVersion() throw()
{
	return "JUCE v" + String (JUCE_MAJOR_VERSION)
			  + "." + String (JUCE_MINOR_VERSION)
			  + "." + String (JUCE_BUILDNUMBER);
}

const StringArray SystemStats::getMACAddressStrings()
{
	int64 macAddresses [16];
	const int numAddresses = getMACAddresses (macAddresses, numElementsInArray (macAddresses), false);

	StringArray s;

	for (int i = 0; i < numAddresses; ++i)
	{
		s.add (String::toHexString (0xff & (int) (macAddresses [i] >> 40)).paddedLeft ('0', 2)
				+ "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 32)).paddedLeft ('0', 2)
				+ "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 24)).paddedLeft ('0', 2)
				+ "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 16)).paddedLeft ('0', 2)
				+ "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 8)).paddedLeft ('0', 2)
				+ "-" + String::toHexString (0xff & (int) (macAddresses [i] >> 0)).paddedLeft ('0', 2));
	}

	return s;
}

static bool juceInitialisedNonGUI = false;

void JUCE_PUBLIC_FUNCTION initialiseJuce_NonGUI()
{
	if (! juceInitialisedNonGUI)
	{
#if JUCE_MAC || JUCE_IPHONE
		const ScopedAutoReleasePool pool;
#endif

#ifdef JUCE_DEBUG
		{
			// Some simple test code to keep an eye on things and make sure these functions
			// work ok on all platforms. Let me know if any of these assertions fail!

			static_jassert (sizeof (pointer_sized_int) == sizeof (void*));

			static_jassert (sizeof (int8) == 1);
			static_jassert (sizeof (uint8) == 1);
			static_jassert (sizeof (int16) == 2);
			static_jassert (sizeof (uint16) == 2);
			static_jassert (sizeof (int32) == 4);
			static_jassert (sizeof (uint32) == 4);
			static_jassert (sizeof (int64) == 8);
			static_jassert (sizeof (uint64) == 8);

			char a1[7];
			jassert (numElementsInArray(a1) == 7);
			int a2[3];
			jassert (numElementsInArray(a2) == 3);

			int n = 1;
			Atomic::increment (n);
			jassert (Atomic::incrementAndReturn (n) == 3);
			Atomic::decrement (n);
			jassert (Atomic::decrementAndReturn (n) == 1);

			jassert (ByteOrder::swap ((uint16) 0x1122) == 0x2211);
			jassert (ByteOrder::swap ((uint32) 0x11223344) == 0x44332211);

			// Some quick stream tests..
			int randomInt = Random::getSystemRandom().nextInt();
			int64 randomInt64 = Random::getSystemRandom().nextInt64();
			double randomDouble = Random::getSystemRandom().nextDouble();
			String randomString;
			for (int i = 50; --i >= 0;)
				randomString << (juce_wchar) (Random::getSystemRandom().nextInt() & 0xffff);

			MemoryOutputStream mo;
			mo.writeInt (randomInt);
			mo.writeIntBigEndian (randomInt);
			mo.writeCompressedInt (randomInt);
			mo.writeString (randomString);
			mo.writeInt64 (randomInt64);
			mo.writeInt64BigEndian (randomInt64);
			mo.writeDouble (randomDouble);
			mo.writeDoubleBigEndian (randomDouble);

			MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
			jassert (mi.readInt() == randomInt);
			jassert (mi.readIntBigEndian() == randomInt);
			jassert (mi.readCompressedInt() == randomInt);
			jassert (mi.readString() == randomString);
			jassert (mi.readInt64() == randomInt64);
			jassert (mi.readInt64BigEndian() == randomInt64);
			jassert (mi.readDouble() == randomDouble);
			jassert (mi.readDoubleBigEndian() == randomDouble);
		}
#endif
		// Now the real initialisation..

		juceInitialisedNonGUI = true;

		DBG (SystemStats::getJUCEVersion());
		SystemStats::initialiseStats();
		Random::getSystemRandom().setSeedRandomly(); // (mustn't call this before initialiseStats() because it relies on the time being set up)
	}
}

#if JUCE_WINDOWS
 // This is imported from the sockets code..
 typedef int (__stdcall juce_CloseWin32SocketLibCall) (void);
 extern juce_CloseWin32SocketLibCall* juce_CloseWin32SocketLib;
#endif

#if JUCE_DEBUG
  extern void juce_CheckForDanglingStreams();
#endif

void JUCE_PUBLIC_FUNCTION shutdownJuce_NonGUI()
{
	if (juceInitialisedNonGUI)
	{
#if JUCE_MAC || JUCE_IPHONE
		const ScopedAutoReleasePool pool;
#endif

#if JUCE_WINDOWS
		// need to shut down sockets if they were used..
		if (juce_CloseWin32SocketLib != 0)
			(*juce_CloseWin32SocketLib)();
#endif

		LocalisedStrings::setCurrentMappings (0);
		Thread::stopAllThreads (3000);

#if JUCE_DEBUG
		juce_CheckForDanglingStreams();
#endif

		juceInitialisedNonGUI = false;
	}
}

#ifdef JUCE_DLL

void* juce_Malloc (const int size)
{
	return malloc (size);
}

void* juce_Calloc (const int size)
{
	return calloc (1, size);
}

void* juce_Realloc (void* const block, const int size)
{
	return realloc (block, size);
}

void juce_Free (void* const block)
{
	free (block);
}

#if defined (JUCE_DEBUG) && JUCE_MSVC && JUCE_CHECK_MEMORY_LEAKS

void* juce_DebugMalloc (const int size, const char* file, const int line)
{
	return _malloc_dbg  (size, _NORMAL_BLOCK, file, line);
}

void* juce_DebugCalloc (const int size, const char* file, const int line)
{
	return _calloc_dbg  (1, size, _NORMAL_BLOCK, file, line);
}

void* juce_DebugRealloc (void* const block, const int size, const char* file, const int line)
{
	return _realloc_dbg  (block, size, _NORMAL_BLOCK, file, line);
}

void juce_DebugFree (void* const block)
{
	_free_dbg (block, _NORMAL_BLOCK);
}

#endif
#endif

END_JUCE_NAMESPACE
/*** End of inlined file: juce_SystemStats.cpp ***/


/*** Start of inlined file: juce_Time.cpp ***/
#ifdef _MSC_VER
  #pragma warning (disable: 4514)
  #pragma warning (push)
#endif

#ifndef JUCE_WINDOWS
  #include <sys/time.h>
#else
  #include <ctime>
#endif

#include <sys/timeb.h>

BEGIN_JUCE_NAMESPACE

#ifdef _MSC_VER
  #pragma warning (pop)

  #ifdef _INC_TIME_INL
	#define USE_NEW_SECURE_TIME_FNS
  #endif
#endif

namespace TimeHelpers
{
	static struct tm  millisToLocal (const int64 millis) throw()
	{
		struct tm result;
		const int64 seconds = millis / 1000;

		if (seconds < literal64bit (86400) || seconds >= literal64bit (2145916800))
		{
			// use extended maths for dates beyond 1970 to 2037..
			const int timeZoneAdjustment = 31536000 - (int) (Time (1971, 0, 1, 0, 0).toMilliseconds() / 1000);
			const int64 jdm = seconds + timeZoneAdjustment + literal64bit (210866803200);

			const int days = (int) (jdm / literal64bit (86400));
			const int a = 32044 + days;
			const int b = (4 * a + 3) / 146097;
			const int c = a - (b * 146097) / 4;
			const int d = (4 * c + 3) / 1461;
			const int e = c - (d * 1461) / 4;
			const int m = (5 * e + 2) / 153;

			result.tm_mday  = e - (153 * m + 2) / 5 + 1;
			result.tm_mon   = m + 2 - 12 * (m / 10);
			result.tm_year  = b * 100 + d - 6700 + (m / 10);
			result.tm_wday  = (days + 1) % 7;
			result.tm_yday  = -1;

			int t = (int) (jdm % literal64bit (86400));
			result.tm_hour  = t / 3600;
			t %= 3600;
			result.tm_min   = t / 60;
			result.tm_sec   = t % 60;
			result.tm_isdst = -1;
		}
		else
		{
			time_t now = static_cast <time_t> (seconds);

	#if JUCE_WINDOWS
	  #ifdef USE_NEW_SECURE_TIME_FNS
			if (now >= 0 && now <= 0x793406fff)
				localtime_s (&result, &now);
			else
				zeromem (&result, sizeof (result));
	  #else
			result = *localtime (&now);
	  #endif
	#else
			// more thread-safe
			localtime_r (&now, &result);
	#endif
		}

		return result;
	}

	static int extendedModulo (const int64 value, const int modulo) throw()
	{
		return (int) (value >= 0 ? (value % modulo)
								 : (value - ((value / modulo) + 1) * modulo));
	}

	static uint32 lastMSCounterValue = 0;
}

Time::Time() throw()
	: millisSinceEpoch (0)
{
}

Time::Time (const Time& other) throw()
	: millisSinceEpoch (other.millisSinceEpoch)
{
}

Time::Time (const int64 ms) throw()
	: millisSinceEpoch (ms)
{
}

Time::Time (const int year,
			const int month,
			const int day,
			const int hours,
			const int minutes,
			const int seconds,
			const int milliseconds,
			const bool useLocalTime) throw()
{
	jassert (year > 100); // year must be a 4-digit version

	if (year < 1971 || year >= 2038 || ! useLocalTime)
	{
		// use extended maths for dates beyond 1970 to 2037..
		const int timeZoneAdjustment = useLocalTime ? (31536000 - (int) (Time (1971, 0, 1, 0, 0).toMilliseconds() / 1000))
													: 0;
		const int a = (13 - month) / 12;
		const int y = year + 4800 - a;
		const int jd = day + (153 * (month + 12 * a - 2) + 2) / 5
						   + (y * 365) + (y /  4) - (y / 100) + (y / 400)
						   - 32045;

		const int64 s = ((int64) jd) * literal64bit (86400) - literal64bit (210866803200);

		millisSinceEpoch = 1000 * (s + (hours * 3600 + minutes * 60 + seconds - timeZoneAdjustment))
							 + milliseconds;
	}
	else
	{
		struct tm t;
		t.tm_year   = year - 1900;
		t.tm_mon	= month;
		t.tm_mday   = day;
		t.tm_hour   = hours;
		t.tm_min	= minutes;
		t.tm_sec	= seconds;
		t.tm_isdst  = -1;

		millisSinceEpoch = 1000 * (int64) mktime (&t);

		if (millisSinceEpoch < 0)
			millisSinceEpoch = 0;
		else
			millisSinceEpoch += milliseconds;
	}
}

Time::~Time() throw()
{
}

Time& Time::operator= (const Time& other) throw()
{
	millisSinceEpoch = other.millisSinceEpoch;
	return *this;
}

int64 Time::currentTimeMillis() throw()
{
	static uint32 lastCounterResult = 0xffffffff;
	static int64 correction = 0;

	const uint32 now = getMillisecondCounter();

	// check the counter hasn't wrapped (also triggered the first time this function is called)
	if (now < lastCounterResult)
	{
		// double-check it's actually wrapped, in case multi-cpu machines have timers that drift a bit.
		if (lastCounterResult == 0xffffffff || now < lastCounterResult - 10)
		{
			// get the time once using normal library calls, and store the difference needed to
			// turn the millisecond counter into a real time.
#if JUCE_WINDOWS
			struct _timeb t;
  #ifdef USE_NEW_SECURE_TIME_FNS
			_ftime_s (&t);
  #else
			_ftime (&t);
  #endif
			correction = (((int64) t.time) * 1000 + t.millitm) - now;
#else
			struct timeval tv;
			struct timezone tz;
			gettimeofday (&tv, &tz);
			correction = (((int64) tv.tv_sec) * 1000 + tv.tv_usec / 1000) - now;
#endif
		}
	}

	lastCounterResult = now;

	return correction + now;
}

uint32 juce_millisecondsSinceStartup() throw();

uint32 Time::getMillisecondCounter() throw()
{
	const uint32 now = juce_millisecondsSinceStartup();

	if (now < TimeHelpers::lastMSCounterValue)
	{
		// in multi-threaded apps this might be called concurrently, so
		// make sure that our last counter value only increases and doesn't
		// go backwards..
		if (now < TimeHelpers::lastMSCounterValue - 1000)
			TimeHelpers::lastMSCounterValue = now;
	}
	else
	{
		TimeHelpers::lastMSCounterValue = now;
	}

	return now;
}

uint32 Time::getApproximateMillisecondCounter() throw()
{
	jassert (TimeHelpers::lastMSCounterValue != 0);
	return TimeHelpers::lastMSCounterValue;
}

void Time::waitForMillisecondCounter (const uint32 targetTime) throw()
{
	for (;;)
	{
		const uint32 now = getMillisecondCounter();

		if (now >= targetTime)
			break;

		const int toWait = targetTime - now;

		if (toWait > 2)
		{
			Thread::sleep (jmin (20, toWait >> 1));
		}
		else
		{
			// xxx should consider using mutex_pause on the mac as it apparently
			// makes it seem less like a spinlock and avoids lowering the thread pri.
			for (int i = 10; --i >= 0;)
				Thread::yield();
		}
	}
}

double Time::highResolutionTicksToSeconds (const int64 ticks) throw()
{
	return ticks / (double) getHighResolutionTicksPerSecond();
}

int64 Time::secondsToHighResolutionTicks (const double seconds) throw()
{
	return (int64) (seconds * (double) getHighResolutionTicksPerSecond());
}

const Time JUCE_CALLTYPE Time::getCurrentTime() throw()
{
	return Time (currentTimeMillis());
}

const String Time::toString (const bool includeDate,
							 const bool includeTime,
							 const bool includeSeconds,
							 const bool use24HourClock) const throw()
{
	String result;

	if (includeDate)
	{
		result << getDayOfMonth() << ' '
			   << getMonthName (true) << ' '
			   << getYear();

		if (includeTime)
			result << ' ';
	}

	if (includeTime)
	{
		const int mins = getMinutes();

		result << (use24HourClock ? getHours() : getHoursInAmPmFormat())
			   << (mins < 10 ? ":0" : ":") << mins;

		if (includeSeconds)
		{
			const int secs = getSeconds();
			result << (secs < 10 ? ":0" : ":") << secs;
		}

		if (! use24HourClock)
			result << (isAfternoon() ? "pm" : "am");
	}

	return result.trimEnd();
}

const String Time::formatted (const juce_wchar* const format) const throw()
{
	String buffer;
	int bufferSize = 128;
	buffer.preallocateStorage (bufferSize);

	struct tm t (TimeHelpers::millisToLocal (millisSinceEpoch));

	while (CharacterFunctions::ftime (static_cast <juce_wchar*> (buffer), bufferSize, format, &t) <= 0)
	{
		bufferSize += 128;
		buffer.preallocateStorage (bufferSize);
	}

	return buffer;
}

int Time::getYear() const throw()
{
	return TimeHelpers::millisToLocal (millisSinceEpoch).tm_year + 1900;
}

int Time::getMonth() const throw()
{
	return TimeHelpers::millisToLocal (millisSinceEpoch).tm_mon;
}

int Time::getDayOfMonth() const throw()
{
	return TimeHelpers::millisToLocal (millisSinceEpoch).tm_mday;
}

int Time::getDayOfWeek() const throw()
{
	return TimeHelpers::millisToLocal (millisSinceEpoch).tm_wday;
}

int Time::getHours() const throw()
{
	return TimeHelpers::millisToLocal (millisSinceEpoch).tm_hour;
}

int Time::getHoursInAmPmFormat() const throw()
{
	const int hours = getHours();

	if (hours == 0)
		return 12;
	else if (hours <= 12)
		return hours;
	else
		return hours - 12;
}

bool Time::isAfternoon() const throw()
{
	return getHours() >= 12;
}

int Time::getMinutes() const throw()
{
	return TimeHelpers::millisToLocal (millisSinceEpoch).tm_min;
}

int Time::getSeconds() const throw()
{
	return TimeHelpers::extendedModulo (millisSinceEpoch / 1000, 60);
}

int Time::getMilliseconds() const throw()
{
	return TimeHelpers::extendedModulo (millisSinceEpoch, 1000);
}

bool Time::isDaylightSavingTime() const throw()
{
	return TimeHelpers::millisToLocal (millisSinceEpoch).tm_isdst != 0;
}

const String Time::getTimeZone() const throw()
{
	String zone[2];

#if JUCE_WINDOWS
	_tzset();

  #ifdef USE_NEW_SECURE_TIME_FNS
	{
		char name [128];
		size_t length;

		for (int i = 0; i < 2; ++i)
		{
			zeromem (name, sizeof (name));
			_get_tzname (&length, name, 127, i);
			zone[i] = name;
		}
	}
  #else
	const char** const zonePtr = (const char**) _tzname;
	zone[0] = zonePtr[0];
	zone[1] = zonePtr[1];
  #endif
#else
	tzset();
	const char** const zonePtr = (const char**) tzname;
	zone[0] = zonePtr[0];
	zone[1] = zonePtr[1];
#endif

	if (isDaylightSavingTime())
	{
		zone[0] = zone[1];

		if (zone[0].length() > 3
			 && zone[0].containsIgnoreCase ("daylight")
			 && zone[0].contains ("GMT"))
			zone[0] = "BST";
	}

	return zone[0].substring (0, 3);
}

const String Time::getMonthName (const bool threeLetterVersion) const throw()
{
	return getMonthName (getMonth(), threeLetterVersion);
}

const String Time::getWeekdayName (const bool threeLetterVersion) const throw()
{
	return getWeekdayName (getDayOfWeek(), threeLetterVersion);
}

const String Time::getMonthName (int monthNumber, const bool threeLetterVersion) throw()
{
	const char* const shortMonthNames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
	const char* const longMonthNames[]  = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

	monthNumber %= 12;

	return TRANS (threeLetterVersion ? shortMonthNames [monthNumber]
									 : longMonthNames [monthNumber]);
}

const String Time::getWeekdayName (int day, const bool threeLetterVersion) throw()
{
	const char* const shortDayNames[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
	const char* const longDayNames[]  = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

	day %= 7;

	return TRANS (threeLetterVersion ? shortDayNames [day]
									 : longDayNames [day]);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Time.cpp ***/


/*** Start of inlined file: juce_BitArray.cpp ***/
BEGIN_JUCE_NAMESPACE

BigInteger::BigInteger()
	: numValues (4),
	  highestBit (-1),
	  negative (false)
{
	values.calloc (numValues + 1);
}

BigInteger::BigInteger (const int value)
	: numValues (4),
	  highestBit (31),
	  negative (value < 0)
{
	values.calloc (numValues + 1);
	values[0] = abs (value);
	highestBit = getHighestBit();
}

BigInteger::BigInteger (int64 value)
	: numValues (4),
	  highestBit (63),
	  negative (value < 0)
{
	values.calloc (numValues + 1);

	if (value < 0)
		value = -value;

	values[0] = (unsigned int) value;
	values[1] = (unsigned int) (value >> 32);
	highestBit = getHighestBit();
}

BigInteger::BigInteger (const unsigned int value)
	: numValues (4),
	  highestBit (31),
	  negative (false)
{
	values.calloc (numValues + 1);
	values[0] = value;
	highestBit = getHighestBit();
}

BigInteger::BigInteger (const BigInteger& other)
	: numValues (jmax (4, (other.highestBit >> 5) + 1)),
	  highestBit (other.getHighestBit()),
	  negative (other.negative)
{
	values.malloc (numValues + 1);
	memcpy (values, other.values, sizeof (unsigned int) * (numValues + 1));
}

BigInteger::~BigInteger()
{
}

void BigInteger::swapWith (BigInteger& other) throw()
{
	values.swapWith (other.values);
	swapVariables (numValues, other.numValues);
	swapVariables (highestBit, other.highestBit);
	swapVariables (negative, other.negative);
}

BigInteger& BigInteger::operator= (const BigInteger& other)
{
	if (this != &other)
	{
		highestBit = other.getHighestBit();
		numValues = jmax (4, (highestBit >> 5) + 1);
		negative = other.negative;
		values.malloc (numValues + 1);
		memcpy (values, other.values, sizeof (unsigned int) * (numValues + 1));
	}

	return *this;
}

void BigInteger::ensureSize (const int numVals)
{
	if (numVals + 2 >= numValues)
	{
		int oldSize = numValues;
		numValues = ((numVals + 2) * 3) / 2;
		values.realloc (numValues + 1);

		while (oldSize < numValues)
			values [oldSize++] = 0;
	}
}

bool BigInteger::operator[] (const int bit) const throw()
{
	return bit <= highestBit && bit >= 0
			 && ((values [bit >> 5] & (1 << (bit & 31))) != 0);
}

int BigInteger::toInteger() const throw()
{
	const int n = (int) (values[0] & 0x7fffffff);
	return negative ? -n : n;
}

const BigInteger BigInteger::getBitRange (int startBit, int numBits) const
{
	BigInteger r;
	numBits = jmin (numBits, getHighestBit() + 1 - startBit);
	r.ensureSize (numBits >> 5);
	r.highestBit = numBits;

	int i = 0;
	while (numBits > 0)
	{
		r.values[i++] = getBitRangeAsInt (startBit, jmin (32, numBits));
		numBits -= 32;
		startBit += 32;
	}

	r.highestBit = r.getHighestBit();
	return r;
}

int BigInteger::getBitRangeAsInt (const int startBit, int numBits) const throw()
{
	if (numBits > 32)
	{
		jassertfalse  // use getBitRange() if you need more than 32 bits..
		numBits = 32;
	}

	numBits = jmin (numBits, highestBit + 1 - startBit);

	if (numBits <= 0)
		return 0;

	const int pos = startBit >> 5;
	const int offset = startBit & 31;
	const int endSpace = 32 - numBits;

	uint32 n = ((uint32) values [pos]) >> offset;

	if (offset > endSpace)
		n |= ((uint32) values [pos + 1]) << (32 - offset);

	return (int) (n & (((uint32) 0xffffffff) >> endSpace));
}

void BigInteger::setBitRangeAsInt (const int startBit, int numBits, unsigned int valueToSet)
{
	if (numBits > 32)
	{
		jassertfalse
		numBits = 32;
	}

	for (int i = 0; i < numBits; ++i)
	{
		setBit (startBit + i, (valueToSet & 1) != 0);
		valueToSet >>= 1;
	}
}

void BigInteger::clear()
{
	if (numValues > 16)
	{
		numValues = 4;
		values.calloc (numValues + 1);
	}
	else
	{
		zeromem (values, sizeof (unsigned int) * (numValues + 1));
	}

	highestBit = -1;
	negative = false;
}

void BigInteger::setBit (const int bit)
{
	if (bit >= 0)
	{
		if (bit > highestBit)
		{
			ensureSize (bit >> 5);
			highestBit = bit;
		}

		values [bit >> 5] |= (1 << (bit & 31));
	}
}

void BigInteger::setBit (const int bit, const bool shouldBeSet)
{
	if (shouldBeSet)
		setBit (bit);
	else
		clearBit (bit);
}

void BigInteger::clearBit (const int bit) throw()
{
	if (bit >= 0 && bit <= highestBit)
		values [bit >> 5] &= ~(1 << (bit & 31));
}

void BigInteger::setRange (int startBit, int numBits, const bool shouldBeSet)
{
	while (--numBits >= 0)
		setBit (startBit++, shouldBeSet);
}

void BigInteger::insertBit (const int bit, const bool shouldBeSet)
{
	if (bit >= 0)
		shiftBits (1, bit);

	setBit (bit, shouldBeSet);
}

bool BigInteger::isZero() const throw()
{
	return getHighestBit() < 0;
}

bool BigInteger::isOne() const throw()
{
	return getHighestBit() == 0 && ! negative;
}

bool BigInteger::isNegative() const throw()
{
	return negative && ! isZero();
}

void BigInteger::setNegative (const bool neg) throw()
{
	negative = neg;
}

void BigInteger::negate() throw()
{
	negative = (! negative) && ! isZero();
}

int BigInteger::countNumberOfSetBits() const throw()
{
	int total = 0;

	for (int i = (highestBit >> 5) + 1; --i >= 0;)
	{
		unsigned int n = values[i];

		if (n == 0xffffffff)
		{
			total += 32;
		}
		else
		{
			while (n != 0)
			{
				total += (n & 1);
				n >>= 1;
			}
		}
	}

	return total;
}

int BigInteger::getHighestBit() const throw()
{
	for (int i = highestBit + 1; --i >= 0;)
		if ((values [i >> 5] & (1 << (i & 31))) != 0)
			return i;

	return -1;
}

int BigInteger::findNextSetBit (int i) const throw()
{
	for (; i <= highestBit; ++i)
		if ((values [i >> 5] & (1 << (i & 31))) != 0)
			return i;

	return -1;
}

int BigInteger::findNextClearBit (int i) const throw()
{
	for (; i <= highestBit; ++i)
		if ((values [i >> 5] & (1 << (i & 31))) == 0)
			break;

	return i;
}

BigInteger& BigInteger::operator+= (const BigInteger& other)
{
	if (other.isNegative())
		return operator-= (-other);

	if (isNegative())
	{
		if (compareAbsolute (other) < 0)
		{
			BigInteger temp (*this);
			temp.negate();
			*this = other;
			operator-= (temp);
		}
		else
		{
			negate();
			operator-= (other);
			negate();
		}
	}
	else
	{
		if (other.highestBit > highestBit)
			highestBit = other.highestBit;

		++highestBit;

		const int numInts = (highestBit >> 5) + 1;
		ensureSize (numInts);

		int64 remainder = 0;

		for (int i = 0; i <= numInts; ++i)
		{
			if (i < numValues)
				remainder += values[i];

			if (i < other.numValues)
				remainder += other.values[i];

			values[i] = (unsigned int) remainder;
			remainder >>= 32;
		}

		jassert (remainder == 0);
		highestBit = getHighestBit();
	}

	return *this;
}

BigInteger& BigInteger::operator-= (const BigInteger& other)
{
	if (other.isNegative())
		return operator+= (-other);

	if (! isNegative())
	{
		if (compareAbsolute (other) < 0)
		{
			BigInteger temp (other);
			swapWith (temp);
			operator-= (temp);
			negate();
			return *this;
		}
	}
	else
	{
		negate();
		operator+= (other);
		negate();
		return *this;
	}

	const int numInts = (highestBit >> 5) + 1;
	const int maxOtherInts = (other.highestBit >> 5) + 1;
	int64 amountToSubtract = 0;

	for (int i = 0; i <= numInts; ++i)
	{
		if (i <= maxOtherInts)
			amountToSubtract += (int64) other.values[i];

		if (values[i] >= amountToSubtract)
		{
			values[i] = (unsigned int) (values[i] - amountToSubtract);
			amountToSubtract = 0;
		}
		else
		{
			const int64 n = ((int64) values[i] + (((int64) 1) << 32)) - amountToSubtract;
			values[i] = (unsigned int) n;
			amountToSubtract = 1;
		}
	}

	return *this;
}

BigInteger& BigInteger::operator*= (const BigInteger& other)
{
	BigInteger total;
	highestBit = getHighestBit();
	const bool wasNegative = isNegative();
	setNegative (false);

	for (int i = 0; i <= highestBit; ++i)
	{
		if (operator[](i))
		{
			BigInteger n (other);
			n.setNegative (false);
			n <<= i;
			total += n;
		}
	}

	total.setNegative (wasNegative ^ other.isNegative());
	swapWith (total);
	return *this;
}

void BigInteger::divideBy (const BigInteger& divisor, BigInteger& remainder)
{
	jassert (this != &remainder); // (can't handle passing itself in to get the remainder)

	const int divHB = divisor.getHighestBit();
	const int ourHB = getHighestBit();

	if (divHB < 0 || ourHB < 0)
	{
		// division by zero
		remainder.clear();
		clear();
	}
	else
	{
		const bool wasNegative = isNegative();

		swapWith (remainder);
		remainder.setNegative (false);
		clear();

		BigInteger temp (divisor);
		temp.setNegative (false);

		int leftShift = ourHB - divHB;
		temp <<= leftShift;

		while (leftShift >= 0)
		{
			if (remainder.compareAbsolute (temp) >= 0)
			{
				remainder -= temp;
				setBit (leftShift);
			}

			if (--leftShift >= 0)
				temp >>= 1;
		}

		negative = wasNegative ^ divisor.isNegative();
		remainder.setNegative (wasNegative);
	}
}

BigInteger& BigInteger::operator/= (const BigInteger& other)
{
	BigInteger remainder;
	divideBy (other, remainder);
	return *this;
}

BigInteger& BigInteger::operator|= (const BigInteger& other)
{
	// this operation doesn't take into account negative values..
	jassert (isNegative() == other.isNegative());

	if (other.highestBit >= 0)
	{
		ensureSize (other.highestBit >> 5);

		int n = (other.highestBit >> 5) + 1;

		while (--n >= 0)
			values[n] |= other.values[n];

		if (other.highestBit > highestBit)
			highestBit = other.highestBit;

		highestBit = getHighestBit();
	}

	return *this;
}

BigInteger& BigInteger::operator&= (const BigInteger& other)
{
	// this operation doesn't take into account negative values..
	jassert (isNegative() == other.isNegative());

	int n = numValues;

	while (n > other.numValues)
		values[--n] = 0;

	while (--n >= 0)
		values[n] &= other.values[n];

	if (other.highestBit < highestBit)
		highestBit = other.highestBit;

	highestBit = getHighestBit();
	return *this;
}

BigInteger& BigInteger::operator^= (const BigInteger& other)
{
	// this operation will only work with the absolute values
	jassert (isNegative() == other.isNegative());

	if (other.highestBit >= 0)
	{
		ensureSize (other.highestBit >> 5);

		int n = (other.highestBit >> 5) + 1;

		while (--n >= 0)
			values[n] ^= other.values[n];

		if (other.highestBit > highestBit)
			highestBit = other.highestBit;

		highestBit = getHighestBit();
	}

	return *this;
}

BigInteger& BigInteger::operator%= (const BigInteger& divisor)
{
	BigInteger remainder;
	divideBy (divisor, remainder);
	swapWith (remainder);
	return *this;
}

BigInteger& BigInteger::operator<<= (int numBitsToShift)
{
	shiftBits (numBitsToShift, 0);
	return *this;
}

BigInteger& BigInteger::operator>>= (int numBitsToShift)
{
	return operator<<= (-numBitsToShift);
}

BigInteger& BigInteger::operator++()		{ return operator+= (1); }
BigInteger& BigInteger::operator--()		{ return operator-= (1); }
const BigInteger BigInteger::operator++ (int)   { const BigInteger old (*this); operator+= (1); return old; }
const BigInteger BigInteger::operator-- (int)   { const BigInteger old (*this); operator-= (1); return old; }

const BigInteger BigInteger::operator+ (const BigInteger& other) const  { BigInteger b (*this); return b += other; }
const BigInteger BigInteger::operator- (const BigInteger& other) const  { BigInteger b (*this); return b -= other; }
const BigInteger BigInteger::operator* (const BigInteger& other) const  { BigInteger b (*this); return b *= other; }
const BigInteger BigInteger::operator/ (const BigInteger& other) const  { BigInteger b (*this); return b /= other; }
const BigInteger BigInteger::operator| (const BigInteger& other) const  { BigInteger b (*this); return b |= other; }
const BigInteger BigInteger::operator& (const BigInteger& other) const  { BigInteger b (*this); return b &= other; }
const BigInteger BigInteger::operator^ (const BigInteger& other) const  { BigInteger b (*this); return b ^= other; }
const BigInteger BigInteger::operator% (const BigInteger& other) const  { BigInteger b (*this); return b %= other; }
const BigInteger BigInteger::operator<< (const int numBits) const	   { BigInteger b (*this); return b <<= numBits; }
const BigInteger BigInteger::operator>> (const int numBits) const	   { BigInteger b (*this); return b >>= numBits; }
const BigInteger BigInteger::operator-() const			  { BigInteger b (*this); b.negate(); return b; }

int BigInteger::compare (const BigInteger& other) const throw()
{
	if (isNegative() == other.isNegative())
	{
		const int absComp = compareAbsolute (other);
		return isNegative() ? -absComp : absComp;
	}
	else
	{
		return isNegative() ? -1 : 1;
	}
}

int BigInteger::compareAbsolute (const BigInteger& other) const throw()
{
	const int h1 = getHighestBit();
	const int h2 = other.getHighestBit();

	if (h1 > h2)
		return 1;
	else if (h1 < h2)
		return -1;

	for (int i = (h1 >> 5) + 1; --i >= 0;)
		if (values[i] != other.values[i])
			return (values[i] > other.values[i]) ? 1 : -1;

	return 0;
}

bool BigInteger::operator== (const BigInteger& other) const throw()	 { return compare (other) == 0; }
bool BigInteger::operator!= (const BigInteger& other) const throw()	 { return compare (other) != 0; }
bool BigInteger::operator<  (const BigInteger& other) const throw()	 { return compare (other) < 0; }
bool BigInteger::operator<= (const BigInteger& other) const throw()	 { return compare (other) <= 0; }
bool BigInteger::operator>  (const BigInteger& other) const throw()	 { return compare (other) > 0; }
bool BigInteger::operator>= (const BigInteger& other) const throw()	 { return compare (other) >= 0; }

void BigInteger::shiftBits (int bits, const int startBit)
{
	if (highestBit < 0)
		return;

	if (startBit > 0)
	{
		if (bits < 0)
		{
			// right shift
			for (int i = startBit; i <= highestBit; ++i)
				setBit (i, operator[] (i - bits));

			highestBit = getHighestBit();
		}
		else if (bits > 0)
		{
			// left shift
			for (int i = highestBit + 1; --i >= startBit;)
				setBit (i + bits, operator[] (i));

			while (--bits >= 0)
				clearBit (bits + startBit);
		}
	}
	else
	{
		if (bits < 0)
		{
			// right shift
			bits = -bits;

			if (bits > highestBit)
			{
				clear();
			}
			else
			{
				const int wordsToMove = bits >> 5;
				int top = 1 + (highestBit >> 5) - wordsToMove;
				highestBit -= bits;

				if (wordsToMove > 0)
				{
					int i;
					for (i = 0; i < top; ++i)
						values [i] = values [i + wordsToMove];

					for (i = 0; i < wordsToMove; ++i)
						values [top + i] = 0;

					bits &= 31;
				}

				if (bits != 0)
				{
					const int invBits = 32 - bits;

					--top;
					for (int i = 0; i < top; ++i)
						values[i] = (values[i] >> bits) | (values [i + 1] << invBits);

					values[top] = (values[top] >> bits);
				}

				highestBit = getHighestBit();
			}
		}
		else if (bits > 0)
		{
			// left shift
			ensureSize (((highestBit + bits) >> 5) + 1);

			const int wordsToMove = bits >> 5;
			int top = 1 + (highestBit >> 5);
			highestBit += bits;

			if (wordsToMove > 0)
			{
				int i;
				for (i = top; --i >= 0;)
					values [i + wordsToMove] = values [i];

				for (i = 0; i < wordsToMove; ++i)
					values [i] = 0;

				bits &= 31;
			}

			if (bits != 0)
			{
				const int invBits = 32 - bits;

				for (int i = top + 1 + wordsToMove; --i > wordsToMove;)
					values[i] = (values[i] << bits) | (values [i - 1] >> invBits);

				values [wordsToMove] = values [wordsToMove] << bits;
			}

			highestBit = getHighestBit();
		}
	}
}

const BigInteger BigInteger::simpleGCD (BigInteger* m, BigInteger* n)
{
	while (! m->isZero())
	{
		if (n->compareAbsolute (*m) > 0)
			swapVariables (m, n);

		*m -= *n;
	}

	return *n;
}

const BigInteger BigInteger::findGreatestCommonDivisor (BigInteger n) const
{
	BigInteger m (*this);

	while (! n.isZero())
	{
		if (abs (m.getHighestBit() - n.getHighestBit()) <= 16)
			return simpleGCD (&m, &n);

		BigInteger temp1 (m), temp2;
		temp1.divideBy (n, temp2);

		m = n;
		n = temp2;
	}

	return m;
}

void BigInteger::exponentModulo (const BigInteger& exponent, const BigInteger& modulus)
{
	BigInteger exp (exponent);
	exp %= modulus;

	BigInteger value (1);
	swapWith (value);
	value %= modulus;

	while (! exp.isZero())
	{
		if (exp [0])
		{
			operator*= (value);
			operator%= (modulus);
		}

		value *= value;
		value %= modulus;
		exp >>= 1;
	}
}

void BigInteger::inverseModulo (const BigInteger& modulus)
{
	if (modulus.isOne() || modulus.isNegative())
	{
		clear();
		return;
	}

	if (isNegative() || compareAbsolute (modulus) >= 0)
		operator%= (modulus);

	if (isOne())
		return;

	if (! (*this)[0])
	{
		// not invertible
		clear();
		return;
	}

	BigInteger a1 (modulus);
	BigInteger a2 (*this);
	BigInteger b1 (modulus);
	BigInteger b2 (1);

	while (! a2.isOne())
	{
		BigInteger temp1, temp2, multiplier (a1);
		multiplier.divideBy (a2, temp1);

		temp1 = a2;
		temp1 *= multiplier;
		temp2 = a1;
		temp2 -= temp1;
		a1 = a2;
		a2 = temp2;

		temp1 = b2;
		temp1 *= multiplier;
		temp2 = b1;
		temp2 -= temp1;
		b1 = b2;
		b2 = temp2;
	}

	while (b2.isNegative())
		b2 += modulus;

	b2 %= modulus;
	swapWith (b2);
}

OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const BigInteger& value)
{
	return stream << value.toString (10);
}

const String BigInteger::toString (const int base, const int minimumNumCharacters) const
{
	String s;
	BigInteger v (*this);

	if (base == 2 || base == 8 || base == 16)
	{
		const int bits = (base == 2) ? 1 : (base == 8 ? 3 : 4);
		static const char* const hexDigits = "0123456789abcdef";

		for (;;)
		{
			const int remainder = v.getBitRangeAsInt (0, bits);

			v >>= bits;

			if (remainder == 0 && v.isZero())
				break;

			s = String::charToString (hexDigits [remainder]) + s;
		}
	}
	else if (base == 10)
	{
		const BigInteger ten (10);
		BigInteger remainder;

		for (;;)
		{
			v.divideBy (ten, remainder);

			if (remainder.isZero() && v.isZero())
				break;

			s = String (remainder.getBitRangeAsInt (0, 8)) + s;
		}
	}
	else
	{
		jassertfalse // can't do the specified base!
		return String::empty;
	}

	s = s.paddedLeft ('0', minimumNumCharacters);

	return isNegative() ? "-" + s : s;
}

void BigInteger::parseString (const String& text, const int base)
{
	clear();
	const juce_wchar* t = text;

	if (base == 2 || base == 8 || base == 16)
	{
		const int bits = (base == 2) ? 1 : (base == 8 ? 3 : 4);

		for (;;)
		{
			const juce_wchar c = *t++;
			const int digit = CharacterFunctions::getHexDigitValue (c);

			if (((unsigned int) digit) < (unsigned int) base)
			{
				operator<<= (bits);
				operator+= (digit);
			}
			else if (c == 0)
			{
				break;
			}
		}
	}
	else if (base == 10)
	{
		const BigInteger ten ((unsigned int) 10);

		for (;;)
		{
			const juce_wchar c = *t++;

			if (c >= '0' && c <= '9')
			{
				operator*= (ten);
				operator+= ((int) (c - '0'));
			}
			else if (c == 0)
			{
				break;
			}
		}
	}

	setNegative (text.trimStart().startsWithChar ('-'));
}

const MemoryBlock BigInteger::toMemoryBlock() const
{
	const int numBytes = (getHighestBit() + 8) >> 3;
	MemoryBlock mb ((size_t) numBytes);

	for (int i = 0; i < numBytes; ++i)
		mb[i] = (uint8) getBitRangeAsInt (i << 3, 8);

	return mb;
}

void BigInteger::loadFromMemoryBlock (const MemoryBlock& data)
{
	clear();

	for (int i = (int) data.getSize(); --i >= 0;)
		this->setBitRangeAsInt ((int) (i << 3), 8, data [i]);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_BitArray.cpp ***/


/*** Start of inlined file: juce_MemoryBlock.cpp ***/
BEGIN_JUCE_NAMESPACE

MemoryBlock::MemoryBlock() throw()
	: size (0)
{
}

MemoryBlock::MemoryBlock (const size_t initialSize,
						  const bool initialiseToZero) throw()
{
	if (initialSize > 0)
	{
		size = initialSize;
		data.allocate (initialSize, initialiseToZero);
	}
	else
	{
		size = 0;
	}
}

MemoryBlock::MemoryBlock (const MemoryBlock& other) throw()
	: size (other.size)
{
	if (size > 0)
	{
		jassert (other.data != 0);
		data.malloc (size);
		memcpy (data, other.data, size);
	}
}

MemoryBlock::MemoryBlock (const void* const dataToInitialiseFrom,
						  const size_t sizeInBytes) throw()
	: size (jmax ((size_t) 0, sizeInBytes))
{
	jassert (sizeInBytes >= 0);

	if (size > 0)
	{
		jassert (dataToInitialiseFrom != 0); // non-zero size, but a zero pointer passed-in?

		data.malloc (size);

		if (dataToInitialiseFrom != 0)
			memcpy (data, dataToInitialiseFrom, size);
	}
}

MemoryBlock::~MemoryBlock() throw()
{
	jassert (size >= 0);	// should never happen
	jassert (size == 0 || data != 0); // non-zero size but no data allocated?
}

MemoryBlock& MemoryBlock::operator= (const MemoryBlock& other) throw()
{
	if (this != &other)
	{
		setSize (other.size, false);
		memcpy (data, other.data, size);
	}

	return *this;
}

bool MemoryBlock::operator== (const MemoryBlock& other) const throw()
{
	return matches (other.data, other.size);
}

bool MemoryBlock::operator!= (const MemoryBlock& other) const throw()
{
	return ! operator== (other);
}

bool MemoryBlock::matches (const void* dataToCompare, size_t dataSize) const throw()
{
	return size == dataSize
			&& memcmp (data, dataToCompare, size) == 0;
}

// this will resize the block to this size
void MemoryBlock::setSize (const size_t newSize,
						   const bool initialiseToZero) throw()
{
	if (size != newSize)
	{
		if (newSize <= 0)
		{
			data.free();
			size = 0;
		}
		else
		{
			if (data != 0)
			{
				data.realloc (newSize);

				if (initialiseToZero && (newSize > size))
					zeromem (data + size, newSize - size);
			}
			else
			{
				data.allocate (newSize, initialiseToZero);
			}

			size = newSize;
		}
	}
}

void MemoryBlock::ensureSize (const size_t minimumSize,
							  const bool initialiseToZero) throw()
{
	if (size < minimumSize)
		setSize (minimumSize, initialiseToZero);
}

void MemoryBlock::swapWith (MemoryBlock& other) throw()
{
	swapVariables (size, other.size);
	data.swapWith (other.data);
}

void MemoryBlock::fillWith (const uint8 value) throw()
{
	memset (data, (int) value, size);
}

void MemoryBlock::append (const void* const srcData,
						  const size_t numBytes) throw()
{
	if (numBytes > 0)
	{
		const size_t oldSize = size;
		setSize (size + numBytes);
		memcpy (data + oldSize, srcData, numBytes);
	}
}

void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) throw()
{
	const char* d = static_cast<const char*> (src);

	if (offset < 0)
	{
		d -= offset;
		num -= offset;
		offset = 0;
	}

	if (offset + num > size)
		num = size - offset;

	if (num > 0)
		memcpy (data + offset, d, num);
}

void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const throw()
{
	char* d = static_cast<char*> (dst);

	if (offset < 0)
	{
		zeromem (d, -offset);
		d -= offset;

		num += offset;
		offset = 0;
	}

	if (offset + num > size)
	{
		const size_t newNum = size - offset;
		zeromem (d + newNum, num - newNum);
		num = newNum;
	}

	if (num > 0)
		memcpy (d, data + offset, num);
}

void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove) throw()
{
	if (startByte < 0)
	{
		numBytesToRemove += startByte;
		startByte = 0;
	}

	if (startByte + numBytesToRemove >= size)
	{
		setSize (startByte);
	}
	else if (numBytesToRemove > 0)
	{
		memmove (data + startByte,
				 data + startByte + numBytesToRemove,
				 size - (startByte + numBytesToRemove));

		setSize (size - numBytesToRemove);
	}
}

const String MemoryBlock::toString() const throw()
{
	return String (static_cast <const char*> (getData()), size);
}

int MemoryBlock::getBitRange (const size_t bitRangeStart, size_t numBits) const throw()
{
	int res = 0;

	size_t byte = bitRangeStart >> 3;
	int offsetInByte = (int) bitRangeStart & 7;
	size_t bitsSoFar = 0;

	while (numBits > 0 && (size_t) byte < size)
	{
		const int bitsThisTime = jmin ((int) numBits, 8 - offsetInByte);
		const int mask = (0xff >> (8 - bitsThisTime)) << offsetInByte;

		res |= (((data[byte] & mask) >> offsetInByte) << bitsSoFar);

		bitsSoFar += bitsThisTime;
		numBits -= bitsThisTime;
		++byte;
		offsetInByte = 0;
	}

	return res;
}

void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int bitsToSet) throw()
{
	size_t byte = bitRangeStart >> 3;
	int offsetInByte = (int) bitRangeStart & 7;
	unsigned int mask = ~((((unsigned int) 0xffffffff) << (32 - numBits)) >> (32 - numBits));

	while (numBits > 0 && (size_t) byte < size)
	{
		const int bitsThisTime = jmin ((int) numBits, 8 - offsetInByte);

		const unsigned int tempMask = (mask << offsetInByte) | ~((((unsigned int) 0xffffffff) >> offsetInByte) << offsetInByte);
		const unsigned int tempBits = bitsToSet << offsetInByte;

		data[byte] = (char) ((data[byte] & tempMask) | tempBits);

		++byte;
		numBits -= bitsThisTime;
		bitsToSet >>= bitsThisTime;
		mask >>= bitsThisTime;
		offsetInByte = 0;
	}
}

void MemoryBlock::loadFromHexString (const String& hex) throw()
{
	ensureSize (hex.length() >> 1);
	char* dest = data;
	int i = 0;

	for (;;)
	{
		int byte = 0;

		for (int loop = 2; --loop >= 0;)
		{
			byte <<= 4;

			for (;;)
			{
				const juce_wchar c = hex [i++];

				if (c >= '0' && c <= '9')
				{
					byte |= c - '0';
					break;
				}
				else if (c >= 'a' && c <= 'z')
				{
					byte |= c - ('a' - 10);
					break;
				}
				else if (c >= 'A' && c <= 'Z')
				{
					byte |= c - ('A' - 10);
					break;
				}
				else if (c == 0)
				{
					setSize (static_cast <size_t> (dest - data));
					return;
				}
			}
		}

		*dest++ = (char) byte;
	}
}

static const char* const encodingTable
	= ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+";

const String MemoryBlock::toBase64Encoding() const throw()
{
	const size_t numChars = ((size << 3) + 5) / 6;

	String destString ((unsigned int) size); // store the length, followed by a '.', and then the data.
	const int initialLen = destString.length();
	destString.preallocateStorage (initialLen + 2 + numChars);

	juce_wchar* d = destString;
	d += initialLen;
	*d++ = '.';

	for (size_t i = 0; i < numChars; ++i)
		*d++ = encodingTable [getBitRange (i * 6, 6)];

	*d++ = 0;

	return destString;
}

bool MemoryBlock::fromBase64Encoding (const String& s) throw()
{
	const int startPos = s.indexOfChar ('.') + 1;

	if (startPos <= 0)
		return false;

	const int numBytesNeeded = s.substring (0, startPos - 1).getIntValue();

	setSize (numBytesNeeded, true);

	const int numChars = s.length() - startPos;
	const juce_wchar* srcChars = s;
	srcChars += startPos;
	int pos = 0;

	for (int i = 0; i < numChars; ++i)
	{
		const char c = (char) srcChars[i];

		for (int j = 0; j < 64; ++j)
		{
			if (encodingTable[j] == c)
			{
				setBitRange (pos, 6, j);
				pos += 6;
				break;
			}
		}
	}

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MemoryBlock.cpp ***/


/*** Start of inlined file: juce_PropertySet.cpp ***/
BEGIN_JUCE_NAMESPACE

PropertySet::PropertySet (const bool ignoreCaseOfKeyNames) throw()
	: properties (ignoreCaseOfKeyNames),
	  fallbackProperties (0),
	  ignoreCaseOfKeys (ignoreCaseOfKeyNames)
{
}

PropertySet::PropertySet (const PropertySet& other) throw()
	: properties (other.properties),
	  fallbackProperties (other.fallbackProperties),
	  ignoreCaseOfKeys (other.ignoreCaseOfKeys)
{
}

PropertySet& PropertySet::operator= (const PropertySet& other) throw()
{
	properties = other.properties;
	fallbackProperties = other.fallbackProperties;
	ignoreCaseOfKeys = other.ignoreCaseOfKeys;

	propertyChanged();
	return *this;
}

PropertySet::~PropertySet()
{
}

void PropertySet::clear()
{
	const ScopedLock sl (lock);

	if (properties.size() > 0)
	{
		properties.clear();
		propertyChanged();
	}
}

const String PropertySet::getValue (const String& keyName,
									const String& defaultValue) const throw()
{
	const ScopedLock sl (lock);

	const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);

	if (index >= 0)
		return properties.getAllValues() [index];

	return fallbackProperties != 0 ? fallbackProperties->getValue (keyName, defaultValue)
								   : defaultValue;
}

int PropertySet::getIntValue (const String& keyName,
							  const int defaultValue) const throw()
{
	const ScopedLock sl (lock);
	const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);

	if (index >= 0)
		return properties.getAllValues() [index].getIntValue();

	return fallbackProperties != 0 ? fallbackProperties->getIntValue (keyName, defaultValue)
								   : defaultValue;
}

double PropertySet::getDoubleValue (const String& keyName,
									const double defaultValue) const throw()
{
	const ScopedLock sl (lock);
	const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);

	if (index >= 0)
		return properties.getAllValues()[index].getDoubleValue();

	return fallbackProperties != 0 ? fallbackProperties->getDoubleValue (keyName, defaultValue)
								   : defaultValue;
}

bool PropertySet::getBoolValue (const String& keyName,
								const bool defaultValue) const throw()
{
	const ScopedLock sl (lock);
	const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);

	if (index >= 0)
		return properties.getAllValues() [index].getIntValue() != 0;

	return fallbackProperties != 0 ? fallbackProperties->getBoolValue (keyName, defaultValue)
								   : defaultValue;
}

XmlElement* PropertySet::getXmlValue (const String& keyName) const
{
	XmlDocument doc (getValue (keyName));

	return doc.getDocumentElement();
}

void PropertySet::setValue (const String& keyName,
							const String& value) throw()
{
	jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!

	if (keyName.isNotEmpty())
	{
		const ScopedLock sl (lock);

		const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);

		if (index < 0 || properties.getAllValues() [index] != value)
		{
			properties.set (keyName, value);
			propertyChanged();
		}
	}
}

void PropertySet::removeValue (const String& keyName) throw()
{
	if (keyName.isNotEmpty())
	{
		const ScopedLock sl (lock);
		const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);

		if (index >= 0)
		{
			properties.remove (keyName);
			propertyChanged();
		}
	}
}

void PropertySet::setValue (const String& keyName, const int value) throw()
{
	setValue (keyName, String (value));
}

void PropertySet::setValue (const String& keyName, const double value) throw()
{
	setValue (keyName, String (value));
}

void PropertySet::setValue (const String& keyName, const bool value) throw()
{
	setValue (keyName, String (value ? "1" : "0"));
}

void PropertySet::setValue (const String& keyName, const XmlElement* const xml)
{
	setValue (keyName, (xml == 0) ? String::empty
								  : xml->createDocument (String::empty, true));
}

bool PropertySet::containsKey (const String& keyName) const throw()
{
	const ScopedLock sl (lock);
	return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
}

void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) throw()
{
	const ScopedLock sl (lock);
	fallbackProperties = fallbackProperties_;
}

XmlElement* PropertySet::createXml (const String& nodeName) const throw()
{
	const ScopedLock sl (lock);
	XmlElement* const xml = new XmlElement (nodeName);

	for (int i = 0; i < properties.getAllKeys().size(); ++i)
	{
		XmlElement* const e = xml->createNewChildElement ("VALUE");
		e->setAttribute ("name", properties.getAllKeys()[i]);
		e->setAttribute ("val", properties.getAllValues()[i]);
	}

	return xml;
}

void PropertySet::restoreFromXml (const XmlElement& xml) throw()
{
	const ScopedLock sl (lock);
	clear();

	forEachXmlChildElementWithTagName (xml, e, "VALUE")
	{
		if (e->hasAttribute ("name")
			 && e->hasAttribute ("val"))
		{
			properties.set (e->getStringAttribute ("name"),
							e->getStringAttribute ("val"));
		}
	}

	if (properties.size() > 0)
		propertyChanged();
}

void PropertySet::propertyChanged()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PropertySet.cpp ***/


/*** Start of inlined file: juce_Variant.cpp ***/
BEGIN_JUCE_NAMESPACE

var::var() throw()
   : type (voidType)
{
	value.doubleValue = 0;
}

var::~var() throw()
{
	if (type == stringType)
		delete value.stringValue;
	else if (type == objectType && value.objectValue != 0)
		value.objectValue->decReferenceCount();
}

const var var::null;

var::var (const var& valueToCopy)
   : type (valueToCopy.type),
	 value (valueToCopy.value)
{
	if (type == stringType)
		value.stringValue = new String (*(value.stringValue));
	else if (type == objectType && value.objectValue != 0)
		value.objectValue->incReferenceCount();
}

var::var (const int value_) throw()
   : type (intType)
{
	value.intValue = value_;
}

var::var (const bool value_) throw()
   : type (boolType)
{
	value.boolValue = value_;
}

var::var (const double value_) throw()
   : type (doubleType)
{
	value.doubleValue = value_;
}

var::var (const String& value_)
   : type (stringType)
{
	value.stringValue = new String (value_);
}

var::var (const char* const value_)
   : type (stringType)
{
	value.stringValue = new String (value_);
}

var::var (const juce_wchar* const value_)
   : type (stringType)
{
	value.stringValue = new String (value_);
}

var::var (DynamicObject* const object)
   : type (objectType)
{
	value.objectValue = object;

	if (object != 0)
		object->incReferenceCount();
}

var::var (MethodFunction method_) throw()
   : type (methodType)
{
	value.methodValue = method_;
}

void var::swapWith (var& other) throw()
{
	swapVariables (type, other.type);
	swapVariables (value, other.value);
}

var& var::operator= (const var& value_)	 { var newValue (value_); swapWith (newValue); return *this; }
var& var::operator= (int value_)		{ var newValue (value_); swapWith (newValue); return *this; }
var& var::operator= (bool value_)		   { var newValue (value_); swapWith (newValue); return *this; }
var& var::operator= (double value_)		 { var newValue (value_); swapWith (newValue); return *this; }
var& var::operator= (const char* value_)	{ var newValue (value_); swapWith (newValue); return *this; }
var& var::operator= (const juce_wchar* value_)  { var newValue (value_); swapWith (newValue); return *this; }
var& var::operator= (const String& value_)	  { var newValue (value_); swapWith (newValue); return *this; }
var& var::operator= (DynamicObject* value_)	 { var newValue (value_); swapWith (newValue); return *this; }
var& var::operator= (MethodFunction value_)	 { var newValue (value_); swapWith (newValue); return *this; }

var::operator int() const
{
	switch (type)
	{
		case voidType:	  break;
		case intType:	   return value.intValue;
		case boolType:	  return value.boolValue ? 1 : 0;
		case doubleType:	return static_cast <int> (value.doubleValue);
		case stringType:	return value.stringValue->getIntValue();
		case objectType:	break;
		default:		jassertfalse; break;
	}

	return 0;
}

var::operator bool() const
{
	switch (type)
	{
		case voidType:	  break;
		case intType:	   return value.intValue != 0;
		case boolType:	  return value.boolValue;
		case doubleType:	return value.doubleValue != 0;
		case stringType:	return value.stringValue->getIntValue() != 0
									|| value.stringValue->trim().equalsIgnoreCase ("true")
									|| value.stringValue->trim().equalsIgnoreCase ("yes");
		case objectType:	return value.objectValue != 0;
		default:		jassertfalse; break;
	}

	return false;
}

var::operator float() const
{
	return (float) operator double();
}

var::operator double() const
{
	switch (type)
	{
		case voidType:	  break;
		case intType:	   return value.intValue;
		case boolType:	  return value.boolValue ? 1.0 : 0.0;
		case doubleType:	return value.doubleValue;
		case stringType:	return value.stringValue->getDoubleValue();
		case objectType:	break;
		default:		jassertfalse; break;
	}

	return 0.0;
}

const String var::toString() const
{
	switch (type)
	{
		case voidType:	  return String::empty;
		case intType:	   return String (value.intValue);
		case boolType:	  return String::charToString (value.boolValue ? '1' : '0');
		case doubleType:	return String (value.doubleValue);
		case stringType:	return *(value.stringValue);
		case objectType:    return "Object 0x" + String::toHexString ((int) (pointer_sized_int) value.objectValue);
		case methodType:    return "Method";
		default:		jassertfalse; break;
	}

	return String::empty;
}

var::operator const String() const
{
	return toString();
}

DynamicObject* var::getObject() const
{
	return type == objectType ? value.objectValue : 0;
}

bool var::equals (const var& other) const throw()
{
	switch (type)
	{
		case voidType:	  return other.isVoid();
		case intType:	   return value.intValue == static_cast <int> (other);
		case boolType:	  return value.boolValue == static_cast <bool> (other);
		case doubleType:	return value.doubleValue == static_cast <double> (other);
		case stringType:	return (*(value.stringValue)) == other.toString();
		case objectType:	return value.objectValue == other.getObject();
		case methodType:	return value.methodValue == other.value.methodValue && other.isMethod();
		default:		jassertfalse; break;
	}

	return false;
}

bool operator== (const var& v1, const var& v2) throw()	  { return v1.equals (v2); }
bool operator!= (const var& v1, const var& v2) throw()	  { return ! v1.equals (v2); }
bool operator== (const var& v1, const String& v2) throw()   { return v1.toString() == v2; }
bool operator!= (const var& v1, const String& v2) throw()   { return v1.toString() != v2; }

void var::writeToStream (OutputStream& output) const
{
	switch (type)
	{
		case voidType:	  output.writeCompressedInt (0); break;
		case intType:	   output.writeCompressedInt (5); output.writeByte (1); output.writeInt (value.intValue); break;
		case boolType:	  output.writeCompressedInt (1); output.writeByte (value.boolValue ? 2 : 3); break;
		case doubleType:	output.writeCompressedInt (9); output.writeByte (4); output.writeDouble (value.doubleValue); break;
		case stringType:
		{
			const int len = value.stringValue->getNumBytesAsUTF8() + 1;
			output.writeCompressedInt (len + 1);
			output.writeByte (5);
			HeapBlock<char> temp (len);
			value.stringValue->copyToUTF8 (temp, len);
			output.write (temp, len);
			break;
		}
		case objectType:
		case methodType:	output.writeCompressedInt (0); jassertfalse; break; // Can't write an object to a stream!
		default:		jassertfalse; break; // Is this a corrupted object?
	}
}

const var var::readFromStream (InputStream& input)
{
	const int numBytes = input.readCompressedInt();

	if (numBytes > 0)
	{
		switch (input.readByte())
		{
			case 1:	 return var (input.readInt());
			case 2:	 return var (true);
			case 3:	 return var (false);
			case 4:	 return var (input.readDouble());
			case 5:
			{
				MemoryBlock mb;
				input.readIntoMemoryBlock (mb, numBytes - 1);
				return var (String::fromUTF8 (static_cast <const char*> (mb.getData()), (int) mb.getSize()));
			}

			default:	input.skipNextBytes (numBytes - 1); break;
		}
	}

	return var::null;
}

const var var::operator[] (const var::identifier& propertyName) const
{
	if (type == objectType && value.objectValue != 0)
		return value.objectValue->getProperty (propertyName);

	return var::null;
}

const var var::invoke (const var::identifier& method, const var* arguments, int numArguments) const
{
	if (type == objectType && value.objectValue != 0)
		return value.objectValue->invokeMethod (method, arguments, numArguments);

	return var::null;
}

const var var::invoke (const var& targetObject, const var* arguments, int numArguments) const
{
	if (isMethod())
	{
		DynamicObject* const target = targetObject.getObject();

		if (target != 0)
			return (target->*(value.methodValue)) (arguments, numArguments);
	}

	return var::null;
}

const var var::call (const var::identifier& method) const
{
	return invoke (method, 0, 0);
}

const var var::call (const var::identifier& method, const var& arg1) const
{
	return invoke (method, &arg1, 1);
}

const var var::call (const var::identifier& method, const var& arg1, const var& arg2) const
{
	var args[] = { arg1, arg2 };
	return invoke (method, args, 2);
}

const var var::call (const var::identifier& method, const var& arg1, const var& arg2, const var& arg3)
{
	var args[] = { arg1, arg2, arg3 };
	return invoke (method, args, 3);
}

const var var::call (const var::identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4) const
{
	var args[] = { arg1, arg2, arg3, arg4 };
	return invoke (method, args, 4);
}

const var var::call (const var::identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4, const var& arg5) const
{
	var args[] = { arg1, arg2, arg3, arg4, arg5 };
	return invoke (method, args, 5);
}

var::identifier::identifier() throw()
	: hashCode (0)
{
}

var::identifier::identifier (const String& name_)
	: name (name_),
	  hashCode (name_.hashCode())
{
	/* An identifier string must be suitable for use as a script variable or XML
	   attribute, so it can only contain this limited set of characters.. */
	jassert (name.containsOnly ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_") && name.isNotEmpty());
}

var::identifier::identifier (const char* const name_)
	: name (name_),
	  hashCode (name.hashCode())
{
	/* An identifier string must be suitable for use as a script variable or XML
	   attribute, so it can only contain this limited set of characters.. */
	jassert (name.containsOnly ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_") && name.isNotEmpty());
}

var::identifier::~identifier()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Variant.cpp ***/


/*** Start of inlined file: juce_NamedValueSet.cpp ***/
BEGIN_JUCE_NAMESPACE

NamedValueSet::NamedValue::NamedValue() throw()
{
}

inline NamedValueSet::NamedValue::NamedValue (const var::identifier& name_, const var& value_)
	: name (name_), value (value_)
{
}

NamedValueSet::NamedValueSet() throw()
{
}

NamedValueSet::NamedValueSet (const NamedValueSet& other)
	: values (other.values)
{
}

NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
{
	values = other.values;
	return *this;
}

NamedValueSet::~NamedValueSet()
{
}

int NamedValueSet::size() const throw()
{
	return values.size();
}

const var& NamedValueSet::operator[] (const var::identifier& name) const
{
	for (int i = values.size(); --i >= 0;)
	{
		const NamedValue& v = values.getReference(i);

		if (v.name == name)
			return v.value;
	}

	return var::null;
}

const var NamedValueSet::getWithDefault (const var::identifier& name, const var& defaultReturnValue) const
{
	const var* v = getItem (name);
	return v != 0 ? *v : defaultReturnValue;
}

var* NamedValueSet::getItem (const var::identifier& name) const
{
	for (int i = values.size(); --i >= 0;)
	{
		NamedValue& v = values.getReference(i);

		if (v.name == name)
			return &(v.value);
	}

	return 0;
}

bool NamedValueSet::set (const var::identifier& name, const var& newValue)
{
	for (int i = values.size(); --i >= 0;)
	{
		NamedValue& v = values.getReference(i);

		if (v.name == name)
		{
			if (v.value == newValue)
				return false;

			v.value = newValue;
			return true;
		}
	}

	values.add (NamedValue (name, newValue));
	return true;
}

bool NamedValueSet::contains (const var::identifier& name) const
{
	return getItem (name) != 0;
}

bool NamedValueSet::remove (const var::identifier& name)
{
	for (int i = values.size(); --i >= 0;)
	{
		if (values.getReference(i).name == name)
		{
			values.remove (i);
			return true;
		}
	}

	return false;
}

const var::identifier NamedValueSet::getName (int index) const
{
	jassert (((unsigned int) index) < (unsigned int) values.size());
	return values [index].name;
}

void NamedValueSet::clear()
{
	values.clear();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_NamedValueSet.cpp ***/


/*** Start of inlined file: juce_DynamicObject.cpp ***/
BEGIN_JUCE_NAMESPACE

DynamicObject::DynamicObject()
{
}

DynamicObject::~DynamicObject()
{
}

bool DynamicObject::hasProperty (const var::identifier& propertyName) const
{
	var* const v = properties.getItem (propertyName);
	return v != 0 && ! v->isMethod();
}

const var DynamicObject::getProperty (const var::identifier& propertyName) const
{
	return properties [propertyName];
}

void DynamicObject::setProperty (const var::identifier& propertyName, const var& newValue)
{
	properties.set (propertyName, newValue);
}

void DynamicObject::removeProperty (const var::identifier& propertyName)
{
	properties.remove (propertyName);
}

bool DynamicObject::hasMethod (const var::identifier& methodName) const
{
	return getProperty (methodName).isMethod();
}

const var DynamicObject::invokeMethod (const var::identifier& methodName,
									   const var* parameters,
									   int numParameters)
{
	return properties [methodName].invoke (var (this), parameters, numParameters);
}

void DynamicObject::setMethod (const var::identifier& name,
							   var::MethodFunction methodFunction)
{
	properties.set (name, var (methodFunction));
}

void DynamicObject::clear()
{
	properties.clear();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DynamicObject.cpp ***/


/*** Start of inlined file: juce_BlowFish.cpp ***/
BEGIN_JUCE_NAMESPACE

BlowFish::BlowFish (const void* const keyData, const int keyBytes)
{
	jassert (keyData != 0);
	jassert (keyBytes > 0);

	static const uint32 initialPValues [18] =
	{
		0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
		0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
		0x9216d5d9, 0x8979fb1b
	};

	static const uint32 initialSValues [4 * 256] =
	{
		0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
		0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e,
		0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
		0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e,
		0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440,
		0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
		0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677,
		0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032,
		0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
		0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0,
		0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98,
		0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
		0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d,
		0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7,
		0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
		0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09,
		0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb,
		0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
		0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82,
		0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573,
		0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
		0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8,
		0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0,
		0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
		0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1,
		0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9,
		0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
		0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af,
		0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5,
		0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
		0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915,
		0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a,
		0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266,
		0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e,
		0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
		0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1,
		0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8,
		0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
		0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7,
		0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331,
		0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
		0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87,
		0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2,
		0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
		0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509,
		0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3,
		0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
		0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960,
		0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28,
		0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
		0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf,
		0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e,
		0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
		0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281,
		0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696,
		0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
		0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0,
		0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250,
		0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
		0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061,
		0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e,
		0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
		0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340,
		0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7,
		0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068,
		0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840,
		0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
		0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb,
		0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6,
		0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
		0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb,
		0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b,
		0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
		0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc,
		0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564,
		0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
		0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728,
		0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e,
		0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
		0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b,
		0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb,
		0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
		0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9,
		0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe,
		0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
		0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61,
		0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9,
		0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
		0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633,
		0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169,
		0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
		0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62,
		0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76,
		0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
		0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c,
		0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0,
		0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe,
		0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4,
		0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
		0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22,
		0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6,
		0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
		0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51,
		0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c,
		0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
		0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd,
		0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319,
		0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
		0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32,
		0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166,
		0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
		0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47,
		0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d,
		0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
		0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd,
		0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7,
		0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
		0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525,
		0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442,
		0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
		0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d,
		0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299,
		0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
		0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a,
		0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b,
		0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
		0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9,
		0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6
	};

	memcpy (p, initialPValues, sizeof (p));

	int i, j = 0;
	for (i = 4; --i >= 0;)
	{
		s[i].malloc (256);
		memcpy (s[i], initialSValues + i * 256, 256 * sizeof (uint32));
	}

	for (i = 0; i < 18; ++i)
	{
		uint32 d = 0;

		for (int k = 0; k < 4; ++k)
		{
			d = (d << 8) | static_cast <const uint8*> (keyData)[j];

			if (++j >= keyBytes)
				j = 0;
		}

		p[i] = initialPValues[i] ^ d;
	}

	uint32 l = 0, r = 0;

	for (i = 0; i < 18; i += 2)
	{
		encrypt (l, r);

		p[i] = l;
		p[i + 1] = r;
	}

	for (i = 0; i < 4; ++i)
	{
		for (j = 0; j < 256; j += 2)
		{
			encrypt (l, r);

			s[i][j] = l;
			s[i][j + 1] = r;
		}
	}
}

BlowFish::BlowFish (const BlowFish& other)
{
	for (int i = 4; --i >= 0;)
		s[i].malloc (256);

	operator= (other);
}

BlowFish& BlowFish::operator= (const BlowFish& other)
{
	memcpy (p, other.p, sizeof (p));

	for (int i = 4; --i >= 0;)
		memcpy (s[i], other.s[i], 256 * sizeof (uint32));

	return *this;
}

BlowFish::~BlowFish()
{
}

uint32 BlowFish::F (const uint32 x) const throw()
{
	return ((s[0][(x >> 24) & 0xff] + s[1][(x >> 16) & 0xff])
				^ s[2][(x >> 8) & 0xff]) + s[3][x & 0xff];
}

void BlowFish::encrypt (uint32& data1, uint32& data2) const throw()
{
	uint32 l = data1;
	uint32 r = data2;

	for (int i = 0; i < 16; ++i)
	{
		l ^= p[i];
		r ^= F(l);
		swapVariables (l, r);
	}

	data1 = r ^ p[17];
	data2 = l ^ p[16];
}

void BlowFish::decrypt (uint32& data1, uint32& data2) const throw()
{
	uint32 l = data1;
	uint32 r = data2;

	for (int i = 17; i > 1; --i)
	{
		l ^= p[i];
		r ^= F(l);
		swapVariables (l, r);
	}

	data1 = r ^ p[0];
	data2 = l ^ p[1];
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_BlowFish.cpp ***/


/*** Start of inlined file: juce_MD5.cpp ***/
BEGIN_JUCE_NAMESPACE

MD5::MD5()
{
	zerostruct (result);
}

MD5::MD5 (const MD5& other)
{
	memcpy (result, other.result, sizeof (result));
}

MD5& MD5::operator= (const MD5& other)
{
	memcpy (result, other.result, sizeof (result));
	return *this;
}

MD5::MD5 (const MemoryBlock& data)
{
	ProcessContext context;
	context.processBlock (data.getData(), data.getSize());
	context.finish (result);
}

MD5::MD5 (const void* data, const size_t numBytes)
{
	ProcessContext context;
	context.processBlock (data, numBytes);
	context.finish (result);
}

MD5::MD5 (const String& text)
{
	ProcessContext context;

	const int len = text.length();
	const juce_wchar* const t = text;

	for (int i = 0; i < len; ++i)
	{
		// force the string into integer-sized unicode characters, to try to make it
		// get the same results on all platforms + compilers.
		uint32 unicodeChar = ByteOrder::swapIfBigEndian ((uint32) t[i]);

		context.processBlock (&unicodeChar, sizeof (unicodeChar));
	}

	context.finish (result);
}

void MD5::processStream (InputStream& input, int64 numBytesToRead)
{
	ProcessContext context;

	if (numBytesToRead < 0)
		numBytesToRead = std::numeric_limits<int64>::max();

	while (numBytesToRead > 0)
	{
		uint8 tempBuffer [512];
		const int bytesRead = input.read (tempBuffer, (int) jmin (numBytesToRead, (int64) sizeof (tempBuffer)));

		if (bytesRead <= 0)
			break;

		numBytesToRead -= bytesRead;

		context.processBlock (tempBuffer, bytesRead);
	}

	context.finish (result);
}

MD5::MD5 (InputStream& input, int64 numBytesToRead)
{
	processStream (input, numBytesToRead);
}

MD5::MD5 (const File& file)
{
	const ScopedPointer <FileInputStream> fin (file.createInputStream());

	if (fin != 0)
		processStream (*fin, -1);
	else
		zerostruct (result);
}

MD5::~MD5()
{
}

namespace MD5Functions
{
	static void encode (void* const output, const void* const input, const int numBytes) throw()
	{
		for (int i = 0; i < (numBytes >> 2); ++i)
			static_cast<uint32*> (output)[i] = ByteOrder::swapIfBigEndian (static_cast<const uint32*> (input) [i]);
	}

	static inline uint32 F (const uint32 x, const uint32 y, const uint32 z) throw()   { return (x & y) | (~x & z); }
	static inline uint32 G (const uint32 x, const uint32 y, const uint32 z) throw()   { return (x & z) | (y & ~z); }
	static inline uint32 H (const uint32 x, const uint32 y, const uint32 z) throw()   { return x ^ y ^ z; }
	static inline uint32 I (const uint32 x, const uint32 y, const uint32 z) throw()   { return y ^ (x | ~z); }

	static inline uint32 rotateLeft (const uint32 x, const uint32 n) throw()  { return (x << n) | (x >> (32 - n)); }

	static void FF (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
	{
		a += F (b, c, d) + x + ac;
		a = rotateLeft (a, s) + b;
	}

	static void GG (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
	{
		a += G (b, c, d) + x + ac;
		a = rotateLeft (a, s) + b;
	}

	static void HH (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
	{
		a += H (b, c, d) + x + ac;
		a = rotateLeft (a, s) + b;
	}

	static void II (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
	{
		a += I (b, c, d) + x + ac;
		a = rotateLeft (a, s) + b;
	}
}

MD5::ProcessContext::ProcessContext()
{
	state[0] = 0x67452301;
	state[1] = 0xefcdab89;
	state[2] = 0x98badcfe;
	state[3] = 0x10325476;

	count[0] = 0;
	count[1] = 0;
}

void MD5::ProcessContext::processBlock (const void* const data, const size_t dataSize)
{
	int bufferPos = ((count[0] >> 3) & 0x3F);

	count[0] += (uint32) (dataSize << 3);

	if (count[0] < ((uint32) dataSize << 3))
		count[1]++;

	count[1] += (uint32) (dataSize >> 29);

	const size_t spaceLeft = 64 - bufferPos;
	size_t i = 0;

	if (dataSize >= spaceLeft)
	{
		memcpy (buffer + bufferPos, data, spaceLeft);
		transform (buffer);

		for (i = spaceLeft; i + 64 <= dataSize; i += 64)
			transform (static_cast <const char*> (data) + i);

		bufferPos = 0;
	}

	memcpy (buffer + bufferPos, static_cast <const char*> (data) + i, dataSize - i);
}

void MD5::ProcessContext::finish (void* const result)
{
	unsigned char encodedLength[8];
	MD5Functions::encode (encodedLength, count, 8);

	// Pad out to 56 mod 64.
	const int index = (uint32) ((count[0] >> 3) & 0x3f);

	const int paddingLength = (index < 56) ? (56 - index)
										   : (120 - index);

	uint8 paddingBuffer [64];
	zeromem (paddingBuffer, paddingLength);
	paddingBuffer [0] = 0x80;
	processBlock (paddingBuffer, paddingLength);

	processBlock (encodedLength, 8);

	MD5Functions::encode (result, state, 16);
	zerostruct (buffer);
}

void MD5::ProcessContext::transform (const void* const bufferToTransform)
{
	using namespace MD5Functions;

	uint32 a = state[0];
	uint32 b = state[1];
	uint32 c = state[2];
	uint32 d = state[3];
	uint32 x[16];

	encode (x, bufferToTransform, 64);

	enum Constants
	{
		S11 = 7, S12 = 12, S13 = 17, S14 = 22, S21 = 5, S22 = 9, S23 = 14, S24 = 20,
		S31 = 4, S32 = 11, S33 = 16, S34 = 23, S41 = 6, S42 = 10, S43 = 15, S44 = 21
	};

	FF (a, b, c, d, x[ 0], S11, 0xd76aa478);	 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
	FF (c, d, a, b, x[ 2], S13, 0x242070db);	 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
	FF (a, b, c, d, x[ 4], S11, 0xf57c0faf);	 FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
	FF (c, d, a, b, x[ 6], S13, 0xa8304613);	 FF (b, c, d, a, x[ 7], S14, 0xfd469501);
	FF (a, b, c, d, x[ 8], S11, 0x698098d8);	 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
	FF (c, d, a, b, x[10], S13, 0xffff5bb1);	 FF (b, c, d, a, x[11], S14, 0x895cd7be);
	FF (a, b, c, d, x[12], S11, 0x6b901122);	 FF (d, a, b, c, x[13], S12, 0xfd987193);
	FF (c, d, a, b, x[14], S13, 0xa679438e);	 FF (b, c, d, a, x[15], S14, 0x49b40821);

	GG (a, b, c, d, x[ 1], S21, 0xf61e2562);	 GG (d, a, b, c, x[ 6], S22, 0xc040b340);
	GG (c, d, a, b, x[11], S23, 0x265e5a51);	 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
	GG (a, b, c, d, x[ 5], S21, 0xd62f105d);	 GG (d, a, b, c, x[10], S22, 0x02441453);
	GG (c, d, a, b, x[15], S23, 0xd8a1e681);	 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
	GG (a, b, c, d, x[ 9], S21, 0x21e1cde6);	 GG (d, a, b, c, x[14], S22, 0xc33707d6);
	GG (c, d, a, b, x[ 3], S23, 0xf4d50d87);	 GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
	GG (a, b, c, d, x[13], S21, 0xa9e3e905);	 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
	GG (c, d, a, b, x[ 7], S23, 0x676f02d9);	 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);

	HH (a, b, c, d, x[ 5], S31, 0xfffa3942);	 HH (d, a, b, c, x[ 8], S32, 0x8771f681);
	HH (c, d, a, b, x[11], S33, 0x6d9d6122);	 HH (b, c, d, a, x[14], S34, 0xfde5380c);
	HH (a, b, c, d, x[ 1], S31, 0xa4beea44);	 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
	HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60);	 HH (b, c, d, a, x[10], S34, 0xbebfbc70);
	HH (a, b, c, d, x[13], S31, 0x289b7ec6);	 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
	HH (c, d, a, b, x[ 3], S33, 0xd4ef3085);	 HH (b, c, d, a, x[ 6], S34, 0x04881d05);
	HH (a, b, c, d, x[ 9], S31, 0xd9d4d039);	 HH (d, a, b, c, x[12], S32, 0xe6db99e5);
	HH (c, d, a, b, x[15], S33, 0x1fa27cf8);	 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);

	II (a, b, c, d, x[ 0], S41, 0xf4292244);	 II (d, a, b, c, x[ 7], S42, 0x432aff97);
	II (c, d, a, b, x[14], S43, 0xab9423a7);	 II (b, c, d, a, x[ 5], S44, 0xfc93a039);
	II (a, b, c, d, x[12], S41, 0x655b59c3);	 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
	II (c, d, a, b, x[10], S43, 0xffeff47d);	 II (b, c, d, a, x[ 1], S44, 0x85845dd1);
	II (a, b, c, d, x[ 8], S41, 0x6fa87e4f);	 II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
	II (c, d, a, b, x[ 6], S43, 0xa3014314);	 II (b, c, d, a, x[13], S44, 0x4e0811a1);
	II (a, b, c, d, x[ 4], S41, 0xf7537e82);	 II (d, a, b, c, x[11], S42, 0xbd3af235);
	II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);	 II (b, c, d, a, x[ 9], S44, 0xeb86d391);

	state[0] += a;
	state[1] += b;
	state[2] += c;
	state[3] += d;

	zerostruct (x);
}

const MemoryBlock MD5::getRawChecksumData() const
{
	return MemoryBlock (result, sizeof (result));
}

const String MD5::toHexString() const
{
	return String::toHexString (result, sizeof (result), 0);
}

bool MD5::operator== (const MD5& other) const
{
	return memcmp (result, other.result, sizeof (result)) == 0;
}

bool MD5::operator!= (const MD5& other) const
{
	return ! operator== (other);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MD5.cpp ***/


/*** Start of inlined file: juce_Primes.cpp ***/
BEGIN_JUCE_NAMESPACE

namespace PrimesHelpers
{
	static void createSmallSieve (const int numBits, BigInteger& result)
	{
		result.setBit (numBits);
		result.clearBit (numBits); // to enlarge the array

		result.setBit (0);
		int n = 2;

		do
		{
			for (int i = n + n; i < numBits; i += n)
				result.setBit (i);

			n = result.findNextClearBit (n + 1);
		}
		while (n <= (numBits >> 1));
	}

	static void bigSieve (const BigInteger& base, const int numBits, BigInteger& result,
						  const BigInteger& smallSieve, const int smallSieveSize)
	{
		jassert (! base[0]); // must be even!

		result.setBit (numBits);
		result.clearBit (numBits);  // to enlarge the array

		int index = smallSieve.findNextClearBit (0);

		do
		{
			const int prime = (index << 1) + 1;

			BigInteger r (base), remainder;
			r.divideBy (prime, remainder);

			int i = prime - remainder.getBitRangeAsInt (0, 32);

			if (r.isZero())
				i += prime;

			if ((i & 1) == 0)
				i += prime;

			i = (i - 1) >> 1;

			while (i < numBits)
			{
				result.setBit (i);
				i += prime;
			}

			index = smallSieve.findNextClearBit (index + 1);
		}
		while (index < smallSieveSize);
	}

	static bool findCandidate (const BigInteger& base, const BigInteger& sieve,
							   const int numBits, BigInteger& result, const int certainty)
	{
		for (int i = 0; i < numBits; ++i)
		{
			if (! sieve[i])
			{
				result = base + (unsigned int) ((i << 1) + 1);

				if (Primes::isProbablyPrime (result, certainty))
					return true;
			}
		}

		return false;
	}

	static bool passesMillerRabin (const BigInteger& n, int iterations)
	{
		const BigInteger one (1), two (2);
		const BigInteger nMinusOne (n - one);

		BigInteger d (nMinusOne);
		const int s = d.findNextSetBit (0);
		d >>= s;

		BigInteger smallPrimes;
		int numBitsInSmallPrimes = 0;

		for (;;)
		{
			numBitsInSmallPrimes += 256;
			createSmallSieve (numBitsInSmallPrimes, smallPrimes);

			const int numPrimesFound = numBitsInSmallPrimes - smallPrimes.countNumberOfSetBits();

			if (numPrimesFound > iterations + 1)
				break;
		}

		int smallPrime = 2;

		while (--iterations >= 0)
		{
			smallPrime = smallPrimes.findNextClearBit (smallPrime + 1);

			BigInteger r (smallPrime);
			r.exponentModulo (d, n);

			if (r != one && r != nMinusOne)
			{
				for (int j = 0; j < s; ++j)
				{
					r.exponentModulo (two, n);

					if (r == nMinusOne)
						break;
				}

				if (r != nMinusOne)
					return false;
			}
		}

		return true;
	}
}

const BigInteger Primes::createProbablePrime (const int bitLength,
											  const int certainty,
											  const int* randomSeeds,
											  int numRandomSeeds)
{
	using namespace PrimesHelpers;
	int defaultSeeds [16];

	if (numRandomSeeds <= 0)
	{
		randomSeeds = defaultSeeds;
		numRandomSeeds = numElementsInArray (defaultSeeds);
		Random r (0);

		for (int j = 10; --j >= 0;)
		{
			r.setSeedRandomly();

			for (int i = numRandomSeeds; --i >= 0;)
				defaultSeeds[i] ^= r.nextInt() ^ Random::getSystemRandom().nextInt();
		}
	}

	BigInteger smallSieve;
	const int smallSieveSize = 15000;
	createSmallSieve (smallSieveSize, smallSieve);

	BigInteger p;

	for (int i = numRandomSeeds; --i >= 0;)
	{
		BigInteger p2;

		Random r (randomSeeds[i]);
		r.fillBitsRandomly (p2, 0, bitLength);

		p ^= p2;
	}

	p.setBit (bitLength - 1);
	p.clearBit (0);

	const int searchLen = jmax (1024, (bitLength / 20) * 64);

	while (p.getHighestBit() < bitLength)
	{
		p += 2 * searchLen;

		BigInteger sieve;
		bigSieve (p, searchLen, sieve,
				  smallSieve, smallSieveSize);

		BigInteger candidate;

		if (findCandidate (p, sieve, searchLen, candidate, certainty))
			return candidate;
	}

	jassertfalse
	return BigInteger();
}

bool Primes::isProbablyPrime (const BigInteger& number, const int certainty)
{
	using namespace PrimesHelpers;

	if (! number[0])
		return false;

	if (number.getHighestBit() <= 10)
	{
		const int num = number.getBitRangeAsInt (0, 10);

		for (int i = num / 2; --i > 1;)
			if (num % i == 0)
				return false;

		return true;
	}
	else
	{
		if (number.findGreatestCommonDivisor (2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23) != 1)
			return false;

		return passesMillerRabin (number, certainty);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Primes.cpp ***/


/*** Start of inlined file: juce_RSAKey.cpp ***/
BEGIN_JUCE_NAMESPACE

RSAKey::RSAKey()
{
}

RSAKey::RSAKey (const String& s)
{
	if (s.containsChar (','))
	{
		part1.parseString (s.upToFirstOccurrenceOf (",", false, false), 16);
		part2.parseString (s.fromFirstOccurrenceOf (",", false, false), 16);
	}
	else
	{
		// the string needs to be two hex numbers, comma-separated..
		jassertfalse;
	}
}

RSAKey::~RSAKey()
{
}

const String RSAKey::toString() const
{
	return part1.toString (16) + "," + part2.toString (16);
}

bool RSAKey::applyToValue (BigInteger& value) const
{
	if (part1.isZero() || part2.isZero() || value <= 0)
	{
		jassertfalse   // using an uninitialised key
		value.clear();
		return false;
	}

	BigInteger result;

	while (! value.isZero())
	{
		result *= part2;

		BigInteger remainder;
		value.divideBy (part2, remainder);

		remainder.exponentModulo (part1, part2);

		result += remainder;
	}

	value.swapWith (result);
	return true;
}

static const BigInteger findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
{
	// try 3, 5, 9, 17, etc first because these only contain 2 bits and so
	// are fast to divide + multiply
	for (int i = 2; i <= 65536; i *= 2)
	{
		const BigInteger e (1 + i);

		if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
			return e;
	}

	BigInteger e (4);

	while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
		++e;

	return e;
}

void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
							const int numBits, const int* randomSeeds, const int numRandomSeeds)
{
	jassert (numBits > 16); // not much point using less than this..

	BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds));
	BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds, numRandomSeeds));

	const BigInteger n (p * q);
	const BigInteger m (--p * --q);
	const BigInteger e (findBestCommonDivisor (p, q));

	BigInteger d (e);
	d.inverseModulo (m);

	publicKey.part1 = e;
	publicKey.part2 = n;

	privateKey.part1 = d;
	privateKey.part2 = n;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_RSAKey.cpp ***/


/*** Start of inlined file: juce_InputStream.cpp ***/
BEGIN_JUCE_NAMESPACE

char InputStream::readByte()
{
	char temp = 0;
	read (&temp, 1);
	return temp;
}

bool InputStream::readBool()
{
	return readByte() != 0;
}

short InputStream::readShort()
{
	char temp[2];

	if (read (temp, 2) == 2)
		return (short) ByteOrder::littleEndianShort (temp);

	return 0;
}

short InputStream::readShortBigEndian()
{
	char temp[2];

	if (read (temp, 2) == 2)
		return (short) ByteOrder::bigEndianShort (temp);

	return 0;
}

int InputStream::readInt()
{
	char temp[4];

	if (read (temp, 4) == 4)
		return (int) ByteOrder::littleEndianInt (temp);

	return 0;
}

int InputStream::readIntBigEndian()
{
	char temp[4];

	if (read (temp, 4) == 4)
		return (int) ByteOrder::bigEndianInt (temp);

	return 0;
}

int InputStream::readCompressedInt()
{
	const unsigned char sizeByte = readByte();
	if (sizeByte == 0)
		return 0;

	const int numBytes = (sizeByte & 0x7f);
	if (numBytes > 4)
	{
		jassertfalse	// trying to read corrupt data - this method must only be used
						// to read data that was written by OutputStream::writeCompressedInt()
		return 0;
	}

	char bytes[4] = { 0, 0, 0, 0 };
	if (read (bytes, numBytes) != numBytes)
		return 0;

	const int num = (int) ByteOrder::littleEndianInt (bytes);
	return (sizeByte >> 7) ? -num : num;
}

int64 InputStream::readInt64()
{
	union { uint8 asBytes[8]; uint64 asInt64; } n;

	if (read (n.asBytes, 8) == 8)
		return (int64) ByteOrder::swapIfBigEndian (n.asInt64);

	return 0;
}

int64 InputStream::readInt64BigEndian()
{
	union { uint8 asBytes[8]; uint64 asInt64; } n;

	if (read (n.asBytes, 8) == 8)
		return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);

	return 0;
}

float InputStream::readFloat()
{
	// the union below relies on these types being the same size...
	static_jassert (sizeof (int32) == sizeof (float));
	union { int32 asInt; float asFloat; } n;
	n.asInt = (int32) readInt();
	return n.asFloat;
}

float InputStream::readFloatBigEndian()
{
	union { int32 asInt; float asFloat; } n;
	n.asInt = (int32) readIntBigEndian();
	return n.asFloat;
}

double InputStream::readDouble()
{
	union { int64 asInt; double asDouble; } n;
	n.asInt = readInt64();
	return n.asDouble;
}

double InputStream::readDoubleBigEndian()
{
	union { int64 asInt; double asDouble; } n;
	n.asInt = readInt64BigEndian();
	return n.asDouble;
}

const String InputStream::readString()
{
	MemoryBlock buffer (256);
	char* data = static_cast<char*> (buffer.getData());
	size_t i = 0;

	while ((data[i] = readByte()) != 0)
	{
		if (++i >= buffer.getSize())
		{
			buffer.setSize (buffer.getSize() + 512);
			data = static_cast<char*> (buffer.getData());
		}
	}

	return String::fromUTF8 (data, (int) i);
}

const String InputStream::readNextLine()
{
	MemoryBlock buffer (256);
	char* data = static_cast<char*> (buffer.getData());
	size_t i = 0;

	while ((data[i] = readByte()) != 0)
	{
		if (data[i] == '\n')
			break;

		if (data[i] == '\r')
		{
			const int64 lastPos = getPosition();

			if (readByte() != '\n')
				setPosition (lastPos);

			break;
		}

		if (++i >= buffer.getSize())
		{
			buffer.setSize (buffer.getSize() + 512);
			data = static_cast<char*> (buffer.getData());
		}
	}

	return String::fromUTF8 (data, (int) i);
}

int InputStream::readIntoMemoryBlock (MemoryBlock& block, int numBytes)
{
	const int64 totalLength = getTotalLength();

	if (totalLength >= 0)
	{
		const int totalBytesRemaining = (int) jmin ((int64) 0x7fffffff,
													totalLength - getPosition());

		if (numBytes < 0)
			numBytes = totalBytesRemaining;
		else if (numBytes > 0)
			numBytes = jmin (numBytes, totalBytesRemaining);
		else
			return 0;
	}

	const size_t originalBlockSize = block.getSize();
	int totalBytesRead = 0;

	if (numBytes > 0)
	{
		// know how many bytes we want, so we can resize the block first..
		block.setSize (originalBlockSize + numBytes, false);
		totalBytesRead = read (static_cast<char*> (block.getData()) + originalBlockSize, numBytes);
	}
	else
	{
		// read until end of stram..
		const int chunkSize = 32768;

		for (;;)
		{
			block.ensureSize (originalBlockSize + totalBytesRead + chunkSize, false);

			const int bytesJustIn = read (static_cast<char*> (block.getData())
											+ originalBlockSize
											+ totalBytesRead,
										  chunkSize);

			if (bytesJustIn == 0)
				break;

			totalBytesRead += bytesJustIn;
		}
	}

	// trim off any excess left at the end
	block.setSize (originalBlockSize +  totalBytesRead, false);
	return totalBytesRead;
}

const String InputStream::readEntireStreamAsString()
{
	MemoryBlock mb;
	const int size = readIntoMemoryBlock (mb);

	return String::createStringFromData (static_cast<const char*> (mb.getData()), size);
}

void InputStream::skipNextBytes (int64 numBytesToSkip)
{
	if (numBytesToSkip > 0)
	{
		const int skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
		HeapBlock<char> temp (skipBufferSize);

		while (numBytesToSkip > 0 && ! isExhausted())
			numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_InputStream.cpp ***/


/*** Start of inlined file: juce_OutputStream.cpp ***/
BEGIN_JUCE_NAMESPACE

#if JUCE_DEBUG
static CriticalSection activeStreamLock;
static VoidArray activeStreams;

void juce_CheckForDanglingStreams()
{
	/*
		It's always a bad idea to leak any object, but if you're leaking output
		streams, then there's a good chance that you're failing to flush a file
		to disk properly, which could result in corrupted data and other similar
		nastiness..
	*/
	jassert (activeStreams.size() == 0);
};
#endif

OutputStream::OutputStream()
{
#if JUCE_DEBUG
	const ScopedLock sl (activeStreamLock);
	activeStreams.add (this);
#endif
}

OutputStream::~OutputStream()
{
#if JUCE_DEBUG
	const ScopedLock sl (activeStreamLock);
	activeStreams.removeValue (this);
#endif
}

void OutputStream::writeBool (const bool b)
{
	writeByte (b ? (char) 1
				 : (char) 0);
}

void OutputStream::writeByte (char byte)
{
	write (&byte, 1);
}

void OutputStream::writeShort (short value)
{
	const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value);
	write (&v, 2);
}

void OutputStream::writeShortBigEndian (short value)
{
	const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value);
	write (&v, 2);
}

void OutputStream::writeInt (int value)
{
	const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value);
	write (&v, 4);
}

void OutputStream::writeIntBigEndian (int value)
{
	const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value);
	write (&v, 4);
}

void OutputStream::writeCompressedInt (int value)
{
	unsigned int un = (value < 0) ? (unsigned int) -value
								  : (unsigned int) value;

	uint8 data[5];
	int num = 0;

	while (un > 0)
	{
		data[++num] = (uint8) un;
		un >>= 8;
	}

	data[0] = (uint8) num;

	if (value < 0)
		data[0] |= 0x80;

	write (data, num + 1);
}

void OutputStream::writeInt64 (int64 value)
{
	const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value);
	write (&v, 8);
}

void OutputStream::writeInt64BigEndian (int64 value)
{
	const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value);
	write (&v, 8);
}

void OutputStream::writeFloat (float value)
{
	union { int asInt; float asFloat; } n;
	n.asFloat = value;
	writeInt (n.asInt);
}

void OutputStream::writeFloatBigEndian (float value)
{
	union { int asInt; float asFloat; } n;
	n.asFloat = value;
	writeIntBigEndian (n.asInt);
}

void OutputStream::writeDouble (double value)
{
	union { int64 asInt; double asDouble; } n;
	n.asDouble = value;
	writeInt64 (n.asInt);
}

void OutputStream::writeDoubleBigEndian (double value)
{
	union { int64 asInt; double asDouble; } n;
	n.asDouble = value;
	writeInt64BigEndian (n.asInt);
}

void OutputStream::writeString (const String& text)
{
	// (This avoids using toUTF8() to prevent the memory bloat that it would leave behind
	// if lots of large, persistent strings were to be written to streams).
	const int numBytes = text.getNumBytesAsUTF8() + 1;
	HeapBlock<char> temp (numBytes);
	text.copyToUTF8 (temp, numBytes);
	write (temp, numBytes);
}

void OutputStream::writeText (const String& text, const bool asUnicode,
							  const bool writeUnicodeHeaderBytes)
{
	if (asUnicode)
	{
		if (writeUnicodeHeaderBytes)
			write ("\x0ff\x0fe", 2);

		const juce_wchar* src = text;
		bool lastCharWasReturn = false;

		while (*src != 0)
		{
			if (*src == L'\n' && ! lastCharWasReturn)
				writeShort ((short) L'\r');

			lastCharWasReturn = (*src == L'\r');
			writeShort ((short) *src++);
		}
	}
	else
	{
		const char* src = text.toUTF8();
		const char* t = src;

		for (;;)
		{
			if (*t == '\n')
			{
				if (t > src)
					write (src, (int) (t - src));

				write ("\r\n", 2);
				src = t + 1;
			}
			else if (*t == '\r')
			{
				if (t[1] == '\n')
					++t;
			}
			else if (*t == 0)
			{
				if (t > src)
					write (src, (int) (t - src));

				break;
			}

			++t;
		}
	}
}

int OutputStream::writeFromInputStream (InputStream& source, int numBytesToWrite)
{
	if (numBytesToWrite < 0)
		numBytesToWrite = 0x7fffffff;

	int numWritten = 0;

	while (numBytesToWrite > 0 && ! source.isExhausted())
	{
		char buffer [8192];

		const int num = (int) source.read (buffer, (int) jmin ((size_t) numBytesToWrite, sizeof (buffer)));

		if (num == 0)
			break;

		write (buffer, num);

		numBytesToWrite -= num;
		numWritten += num;
	}

	return numWritten;
}

OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const int number)
{
	return stream << String (number);
}

OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const double number)
{
	return stream << String (number);
}

OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char character)
{
	stream.writeByte (character);
	return stream;
}

OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char* const text)
{
	stream.write (text, (int) strlen (text));
	return stream;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_OutputStream.cpp ***/


/*** Start of inlined file: juce_DirectoryIterator.cpp ***/
BEGIN_JUCE_NAMESPACE

void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
						  bool* isDirectory, bool* isHidden, int64* fileSize,
						  Time* modTime, Time* creationTime, bool* isReadOnly);
bool juce_findFileNext (void* handle, String& resultFile,
						bool* isDirectory, bool* isHidden, int64* fileSize,
						Time* modTime, Time* creationTime, bool* isReadOnly);
void juce_findFileClose (void* handle);

DirectoryIterator::DirectoryIterator (const File& directory,
									  bool isRecursive,
									  const String& wc,
									  const int whatToLookFor_)
  : wildCard (wc),
	index (-1),
	whatToLookFor (whatToLookFor_)
{
	// you have to specify the type of files you're looking for!
	jassert ((whatToLookFor_ & (File::findFiles | File::findDirectories)) != 0);
	jassert (whatToLookFor_ > 0 && whatToLookFor_ <= 7);

	String path (directory.getFullPathName());
	if (! path.endsWithChar (File::separator))
		path += File::separator;

	String filename;
	bool isDirectory, isHidden;

	void* const handle = juce_findFileStart (path,
											 isRecursive ? "*" : wc,
											 filename, &isDirectory, &isHidden, 0, 0, 0, 0);

	if (handle != 0)
	{
		do
		{
			if (! filename.containsOnly ("."))
			{
				bool addToList = false;

				if (isDirectory)
				{
					if (isRecursive
						 && ((whatToLookFor_ & File::ignoreHiddenFiles) == 0
							  || ! isHidden))
					{
						dirsFound.add (File (path + filename, 0));
					}

					addToList = (whatToLookFor_ & File::findDirectories) != 0;
				}
				else
				{
					addToList = (whatToLookFor_ & File::findFiles) != 0;
				}

				// if it's recursive, we're not relying on the OS iterator
				// to do the wildcard match, so do it now..
				if (isRecursive && addToList)
					addToList = filename.matchesWildcard (wc, true);

				if (addToList && (whatToLookFor_ & File::ignoreHiddenFiles) != 0)
					addToList = ! isHidden;

				if (addToList)
					filesFound.add (File (path + filename, 0));
			}

		} while (juce_findFileNext (handle, filename, &isDirectory, &isHidden, 0, 0, 0, 0));

		juce_findFileClose (handle);
	}
}

DirectoryIterator::~DirectoryIterator()
{
}

bool DirectoryIterator::next()
{
	if (subIterator != 0)
	{
		if (subIterator->next())
			return true;

		subIterator = 0;
	}

	if (index >= filesFound.size() + dirsFound.size() - 1)
		return false;

	++index;

	if (index >= filesFound.size())
	{
		subIterator = new DirectoryIterator (dirsFound.getReference (index - filesFound.size()),
											 true, wildCard, whatToLookFor);
		return next();
	}

	return true;
}

const File DirectoryIterator::getFile() const
{
	if (subIterator != 0)
		return subIterator->getFile();

	return filesFound [index];
}

float DirectoryIterator::getEstimatedProgress() const
{
	if (filesFound.size() + dirsFound.size() == 0)
	{
		return 0.0f;
	}
	else
	{
		const float detailedIndex = (subIterator != 0) ? index + subIterator->getEstimatedProgress()
													   : (float) index;

		return detailedIndex / (filesFound.size() + dirsFound.size());
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DirectoryIterator.cpp ***/


/*** Start of inlined file: juce_File.cpp ***/
#ifdef _MSC_VER
  #pragma warning (disable: 4514)
  #pragma warning (push)
#endif

#if ! JUCE_WINDOWS
  #include <pwd.h>
#endif

BEGIN_JUCE_NAMESPACE

#ifdef _MSC_VER
  #pragma warning (pop)
#endif

void* juce_fileOpen (const String& path, bool forWriting);
void juce_fileClose (void* handle);
int juce_fileWrite (void* handle, const void* buffer, int size);
int64 juce_fileGetPosition (void* handle);
int64 juce_fileSetPosition (void* handle, int64 pos);
void juce_fileFlush (void* handle);

bool juce_fileExists (const String& fileName, const bool dontCountDirectories);
bool juce_isDirectory (const String& fileName);
int64 juce_getFileSize (const String& fileName);
bool juce_canWriteToFile (const String& fileName);
bool juce_setFileReadOnly (const String& fileName, bool isReadOnly);

void juce_getFileTimes (const String& fileName, int64& modificationTime, int64& accessTime, int64& creationTime);
bool juce_setFileTimes (const String& fileName, int64 modificationTime, int64 accessTime, int64 creationTime);

bool juce_deleteFile (const String& fileName);
bool juce_copyFile (const String& source, const String& dest);
bool juce_moveFile (const String& source, const String& dest);

// this must also create all paths involved in the directory.
void juce_createDirectory (const String& fileName);

bool juce_launchFile (const String& fileName, const String& parameters);

const StringArray juce_getFileSystemRoots();
const String juce_getVolumeLabel (const String& filenameOnVolume, int& volumeSerialNumber);

// starts a directory search operation with a wildcard, returning a handle for
// use in calls to juce_findFileNext.
// juce_firstResultFile gets the name of the file (not the whole pathname) and
// the other pointers, if non-null, are set based on the properties of the file.
void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
						  bool* isDirectory, bool* isHidden, int64* fileSize, Time* modTime,
						  Time* creationTime, bool* isReadOnly);

// returns false when no more files are found
bool juce_findFileNext (void* handle, String& resultFile,
						bool* isDirectory, bool* isHidden, int64* fileSize,
						Time* modTime, Time* creationTime, bool* isReadOnly);

void juce_findFileClose (void* handle);

static const String juce_addTrailingSeparator (const String& path)
{
	return path.endsWithChar (File::separator) ? path
											   : path + File::separator;
}

static const String parseAbsolutePath (String path)
{
	if (path.isEmpty())
		return String::empty;

#if JUCE_WINDOWS
	// Windows..
	path = path.replaceCharacter ('/', '\\');

	if (path.startsWithChar (File::separator))
	{
		if (path[1] != File::separator)
		{
			/*  When you supply a raw string to the File object constructor, it must be an absolute path.
				If you're trying to parse a string that may be either a relative path or an absolute path,
				you MUST provide a context against which the partial path can be evaluated - you can do
				this by simply using File::getChildFile() instead of the File constructor. E.g. saying
				"File::getCurrentWorkingDirectory().getChildFile (myUnknownPath)" would return an absolute
				path if that's what was supplied, or would evaluate a partial path relative to the CWD.
			*/
			jassertfalse

			path = File::getCurrentWorkingDirectory().getFullPathName().substring (0, 2) + path;
		}
	}
	else if (path.indexOfChar (':') < 0)
	{
		if (path.isEmpty())
			return String::empty;

		/*  When you supply a raw string to the File object constructor, it must be an absolute path.
			If you're trying to parse a string that may be either a relative path or an absolute path,
			you MUST provide a context against which the partial path can be evaluated - you can do
			this by simply using File::getChildFile() instead of the File constructor. E.g. saying
			"File::getCurrentWorkingDirectory().getChildFile (myUnknownPath)" would return an absolute
			path if that's what was supplied, or would evaluate a partial path relative to the CWD.
		*/
		jassertfalse

		return File::getCurrentWorkingDirectory().getChildFile (path).getFullPathName();
	}
#else
	// Mac or Linux..
	path = path.replaceCharacter ('\\', '/');

	if (path.startsWithChar ('~'))
	{
		const char* homeDir = 0;

		if (path[1] == File::separator || path[1] == 0)
		{
			// expand a name of the form "~/abc"
			path = File::getSpecialLocation (File::userHomeDirectory).getFullPathName()
					+ path.substring (1);
		}
		else
		{
			// expand a name of type "~dave/abc"
			const String userName (path.substring (1).upToFirstOccurrenceOf ("/", false, false));

			struct passwd* const pw = getpwnam (userName.toUTF8());
			if (pw != 0)
			{
				String home (homeDir);

				if (home.endsWithChar (File::separator))
					home [home.length() - 1] = 0;

				path = String (pw->pw_dir)
					  + path.substring (userName.length());
			}
		}
	}
	else if (! path.startsWithChar (File::separator))
	{
		/*  When you supply a raw string to the File object constructor, it must be an absolute path.
			If you're trying to parse a string that may be either a relative path or an absolute path,
			you MUST provide a context against which the partial path can be evaluated - you can do
			this by simply using File::getChildFile() instead of the File constructor. E.g. saying
			"File::getCurrentWorkingDirectory().getChildFile (myUnknownPath)" would return an absolute
			path if that's what was supplied, or would evaluate a partial path relative to the CWD.
		*/
		jassert (path.startsWith ("./") || path.startsWith ("../")); // (assume that a path "./xyz" is deliberately intended to be relative to the CWD)

		return File::getCurrentWorkingDirectory().getChildFile (path).getFullPathName();
	}
#endif

	int len = path.length();
	while (--len > 0 && path [len] == File::separator)
		path [len] = 0;

	return path;
}

const File File::nonexistent;

File::File (const String& fullPathName)
	: fullPath (parseAbsolutePath (fullPathName))
{
}

File::File (const String& path, int)
	: fullPath (path)
{
}

const File File::createFileWithoutCheckingPath (const String& path)
{
	return File (path, 0);
}

File::File (const File& other)
	: fullPath (other.fullPath)
{
}

File& File::operator= (const String& newPath)
{
	fullPath = parseAbsolutePath (newPath);
	return *this;
}

File& File::operator= (const File& other)
{
	fullPath = other.fullPath;
	return *this;
}

#if JUCE_LINUX
  #define NAMES_ARE_CASE_SENSITIVE 1
#endif

bool File::areFileNamesCaseSensitive()
{
#if NAMES_ARE_CASE_SENSITIVE
	return true;
#else
	return false;
#endif
}

bool File::operator== (const File& other) const
{
	// case-insensitive on Windows, but not on linux.
#if NAMES_ARE_CASE_SENSITIVE
	return fullPath == other.fullPath;
#else
	return fullPath.equalsIgnoreCase (other.fullPath);
#endif
}

bool File::operator!= (const File& other) const
{
	return ! operator== (other);
}

bool File::exists() const
{
	return juce_fileExists (fullPath, false);
}

bool File::existsAsFile() const
{
	return juce_fileExists (fullPath, true);
}

bool File::isDirectory() const
{
	return juce_isDirectory (fullPath);
}

bool File::hasWriteAccess() const
{
	if (exists())
		return juce_canWriteToFile (fullPath);

#if ! JUCE_WINDOWS
	else if ((! isDirectory()) && fullPath.containsChar (separator))
		return getParentDirectory().hasWriteAccess();
	else
		return false;
#else
	// on windows, it seems that even read-only directories can still be written into,
	// so checking the parent directory's permissions would return the wrong result..
	else
		return true;
#endif
}

bool File::setReadOnly (const bool shouldBeReadOnly,
						const bool applyRecursively) const
{
	bool worked = true;

	if (applyRecursively && isDirectory())
	{
		Array <File> subFiles;
		findChildFiles (subFiles, File::findFilesAndDirectories, false);

		for (int i = subFiles.size(); --i >= 0;)
			worked = subFiles.getReference(i).setReadOnly (shouldBeReadOnly, true) && worked;
	}

	return juce_setFileReadOnly (fullPath, shouldBeReadOnly) && worked;
}

bool File::deleteFile() const
{
	return (! exists())
			|| juce_deleteFile (fullPath);
}

bool File::deleteRecursively() const
{
	bool worked = true;

	if (isDirectory())
	{
		Array<File> subFiles;
		findChildFiles (subFiles, File::findFilesAndDirectories, false);

		for (int i = subFiles.size(); --i >= 0;)
			worked = subFiles.getReference(i).deleteRecursively() && worked;
	}

	return deleteFile() && worked;
}

bool File::moveFileTo (const File& newFile) const
{
	if (newFile.fullPath == fullPath)
		return true;

#if ! NAMES_ARE_CASE_SENSITIVE
	if (*this != newFile)
#endif
		if (! newFile.deleteFile())
			return false;

	return juce_moveFile (fullPath, newFile.fullPath);
}

bool File::copyFileTo (const File& newFile) const
{
	if (*this == newFile)
		return true;

	if (! newFile.deleteFile())
		return false;

	return juce_copyFile (fullPath, newFile.fullPath);
}

bool File::copyDirectoryTo (const File& newDirectory) const
{
	if (isDirectory() && newDirectory.createDirectory())
	{
		Array<File> subFiles;
		findChildFiles (subFiles, File::findFiles, false);

		int i;
		for (i = 0; i < subFiles.size(); ++i)
			if (! subFiles.getReference(i).copyFileTo (newDirectory.getChildFile (subFiles.getReference(i).getFileName())))
				return false;

		subFiles.clear();
		findChildFiles (subFiles, File::findDirectories, false);

		for (i = 0; i < subFiles.size(); ++i)
			if (! subFiles.getReference(i).copyDirectoryTo (newDirectory.getChildFile (subFiles.getReference(i).getFileName())))
				return false;

		return true;
	}

	return false;
}

const String File::getPathUpToLastSlash() const
{
	const int lastSlash = fullPath.lastIndexOfChar (separator);

	if (lastSlash > 0)
		return fullPath.substring (0, lastSlash);
	else if (lastSlash == 0)
		return separatorString;
	else
		return fullPath;
}

const File File::getParentDirectory() const
{
	return File (getPathUpToLastSlash());
}

const String File::getFileName() const
{
	return fullPath.substring (fullPath.lastIndexOfChar (separator) + 1);
}

int File::hashCode() const
{
	return fullPath.hashCode();
}

int64 File::hashCode64() const
{
	return fullPath.hashCode64();
}

const String File::getFileNameWithoutExtension() const
{
	const int lastSlash = fullPath.lastIndexOfChar (separator) + 1;
	const int lastDot = fullPath.lastIndexOfChar ('.');

	if (lastDot > lastSlash)
		return fullPath.substring (lastSlash, lastDot);
	else
		return fullPath.substring (lastSlash);
}

bool File::isAChildOf (const File& potentialParent) const
{
	if (potentialParent == File::nonexistent)
		return false;

	const String ourPath (getPathUpToLastSlash());

#if NAMES_ARE_CASE_SENSITIVE
	if (potentialParent.fullPath == ourPath)
#else
	if (potentialParent.fullPath.equalsIgnoreCase (ourPath))
#endif
	{
		return true;
	}
	else if (potentialParent.fullPath.length() >= ourPath.length())
	{
		return false;
	}
	else
	{
		return getParentDirectory().isAChildOf (potentialParent);
	}
}

bool File::isAbsolutePath (const String& path)
{
	return path.startsWithChar ('/') || path.startsWithChar ('\\')
#if JUCE_WINDOWS
			|| (path.isNotEmpty() && path[1] == ':');
#else
			|| path.startsWithChar ('~');
#endif
}

const File File::getChildFile (String relativePath) const
{
	if (isAbsolutePath (relativePath))
	{
		// the path is really absolute..
		return File (relativePath);
	}
	else
	{
		// it's relative, so remove any ../ or ./ bits at the start.
		String path (fullPath);

		if (relativePath[0] == '.')
		{
#if JUCE_WINDOWS
			relativePath = relativePath.replaceCharacter ('/', '\\').trimStart();
#else
			relativePath = relativePath.replaceCharacter ('\\', '/').trimStart();
#endif
			while (relativePath[0] == '.')
			{
				if (relativePath[1] == '.')
				{
					if (relativePath [2] == 0 || relativePath[2] == separator)
					{
						const int lastSlash = path.lastIndexOfChar (separator);
						if (lastSlash >= 0)
							path = path.substring (0, lastSlash);

						relativePath = relativePath.substring (3);
					}
					else
					{
						break;
					}
				}
				else if (relativePath[1] == separator)
				{
					relativePath = relativePath.substring (2);
				}
				else
				{
					break;
				}
			}
		}

		return File (juce_addTrailingSeparator (path) + relativePath);
	}
}

const File File::getSiblingFile (const String& fileName) const
{
	return getParentDirectory().getChildFile (fileName);
}

int64 File::getSize() const
{
	return juce_getFileSize (fullPath);
}

const String File::descriptionOfSizeInBytes (const int64 bytes)
{
	if (bytes == 1)
	{
		return "1 byte";
	}
	else if (bytes < 1024)
	{
		return String ((int) bytes) + " bytes";
	}
	else if (bytes < 1024 * 1024)
	{
		return String (bytes / 1024.0, 1) + " KB";
	}
	else if (bytes < 1024 * 1024 * 1024)
	{
		return String (bytes / (1024.0 * 1024.0), 1) + " MB";
	}
	else
	{
		return String (bytes / (1024.0 * 1024.0 * 1024.0), 1) + " GB";
	}
}

bool File::create() const
{
	if (! exists())
	{
		const File parentDir (getParentDirectory());

		if (parentDir == *this || ! parentDir.createDirectory())
			return false;

		void* const fh = juce_fileOpen (fullPath, true);

		if (fh == 0)
			return false;

		juce_fileClose (fh);
	}

	return true;
}

bool File::createDirectory() const
{
	if (! isDirectory())
	{
		const File parentDir (getParentDirectory());

		if (parentDir == *this || ! parentDir.createDirectory())
			return false;

		String dir (fullPath);

		while (dir.endsWithChar (separator))
			dir [dir.length() - 1] = 0;

		juce_createDirectory (dir);

		return isDirectory();
	}

	return true;
}

const Time File::getCreationTime() const
{
	int64 m, a, c;
	juce_getFileTimes (fullPath, m, a, c);
	return Time (c);
}

bool File::setCreationTime (const Time& t) const
{
	return juce_setFileTimes (fullPath, 0, 0, t.toMilliseconds());
}

const Time File::getLastModificationTime() const
{
	int64 m, a, c;
	juce_getFileTimes (fullPath, m, a, c);
	return Time (m);
}

bool File::setLastModificationTime (const Time& t) const
{
	return juce_setFileTimes (fullPath, t.toMilliseconds(), 0, 0);
}

const Time File::getLastAccessTime() const
{
	int64 m, a, c;
	juce_getFileTimes (fullPath, m, a, c);
	return Time (a);
}

bool File::setLastAccessTime (const Time& t) const
{
	return juce_setFileTimes (fullPath, 0, t.toMilliseconds(), 0);
}

bool File::loadFileAsData (MemoryBlock& destBlock) const
{
	if (! existsAsFile())
		return false;

	FileInputStream in (*this);
	return getSize() == in.readIntoMemoryBlock (destBlock);
}

const String File::loadFileAsString() const
{
	if (! existsAsFile())
		return String::empty;

	FileInputStream in (*this);
	return in.readEntireStreamAsString();
}

static inline bool fileTypeMatches (const int whatToLookFor,
									const bool isDir,
									const bool isHidden)
{
	return (whatToLookFor & (isDir ? File::findDirectories
								   : File::findFiles)) != 0
			 && ((! isHidden)
				  || (whatToLookFor & File::ignoreHiddenFiles) == 0);
}

int File::findChildFiles (Array<File>& results,
						  const int whatToLookFor,
						  const bool searchRecursively,
						  const String& wildCardPattern) const
{
	// you have to specify the type of files you're looking for!
	jassert ((whatToLookFor & (findFiles | findDirectories)) != 0);

	int total = 0;

	// find child files or directories in this directory first..
	if (isDirectory())
	{
		const String path (juce_addTrailingSeparator (fullPath));

		String filename;
		bool itemIsDirectory, itemIsHidden;

		void* const handle = juce_findFileStart (path, wildCardPattern, filename,
												 &itemIsDirectory, &itemIsHidden,
												 0, 0, 0, 0);

		if (handle != 0)
		{
			do
			{
				if (fileTypeMatches (whatToLookFor, itemIsDirectory, itemIsHidden)
					 && ! filename.containsOnly ("."))
				{
					results.add (File (path + filename, 0));
					++total;
				}

			} while (juce_findFileNext (handle, filename, &itemIsDirectory, &itemIsHidden, 0, 0, 0, 0));

			juce_findFileClose (handle);
		}
	}
	else
	{
		// trying to search for files inside a non-directory?
		//jassertfalse
	}

	// and recurse down if required.
	if (searchRecursively)
	{
		Array<File> subDirectories;
		findChildFiles (subDirectories, File::findDirectories, false);

		for (int i = 0; i < subDirectories.size(); ++i)
		{
			total += subDirectories.getReference(i).findChildFiles (results, whatToLookFor,
																	true, wildCardPattern);
		}
	}

	return total;
}

int File::getNumberOfChildFiles (const int whatToLookFor,
								 const String& wildCardPattern) const
{
	// you have to specify the type of files you're looking for!
	jassert (whatToLookFor > 0 && whatToLookFor <= 3);

	int count = 0;

	if (isDirectory())
	{
		String filename;
		bool itemIsDirectory, itemIsHidden;

		void* const handle = juce_findFileStart (fullPath, wildCardPattern, filename,
												 &itemIsDirectory, &itemIsHidden,
												 0, 0, 0, 0);

		if (handle != 0)
		{
			do
			{
				if (fileTypeMatches (whatToLookFor, itemIsDirectory, itemIsHidden)
					 && ! filename.containsOnly ("."))
				{
					++count;
				}

			} while (juce_findFileNext (handle, filename, &itemIsDirectory, &itemIsHidden, 0, 0, 0, 0));

			juce_findFileClose (handle);
		}
	}
	else
	{
		// trying to search for files inside a non-directory?
		jassertfalse
	}

	return count;
}

bool File::containsSubDirectories() const
{
	bool result = false;

	if (isDirectory())
	{
		String filename;
		bool itemIsDirectory, itemIsHidden;
		void* const handle = juce_findFileStart (juce_addTrailingSeparator (fullPath),
												 "*", filename,
												 &itemIsDirectory, &itemIsHidden, 0, 0, 0, 0);

		if (handle != 0)
		{
			do
			{
				if (itemIsDirectory)
				{
					result = true;
					break;
				}

			} while (juce_findFileNext (handle, filename, &itemIsDirectory, &itemIsHidden, 0, 0, 0, 0));

			juce_findFileClose (handle);
		}
	}

	return result;
}

const File File::getNonexistentChildFile (const String& prefix_,
										  const String& suffix,
										  bool putNumbersInBrackets) const
{
	File f (getChildFile (prefix_ + suffix));

	if (f.exists())
	{
		int num = 2;
		String prefix (prefix_);

		// remove any bracketed numbers that may already be on the end..
		if (prefix.trim().endsWithChar (')'))
		{
			putNumbersInBrackets = true;

			const int openBracks = prefix.lastIndexOfChar ('(');
			const int closeBracks = prefix.lastIndexOfChar (')');

			if (openBracks > 0
				 && closeBracks > openBracks
				 && prefix.substring (openBracks + 1, closeBracks).containsOnly ("0123456789"))
			{
				num = prefix.substring (openBracks + 1, closeBracks).getIntValue() + 1;
				prefix = prefix.substring (0, openBracks);
			}
		}

		// also use brackets if it ends in a digit.
		putNumbersInBrackets = putNumbersInBrackets
								|| CharacterFunctions::isDigit (prefix.getLastCharacter());

		do
		{
			if (putNumbersInBrackets)
				f = getChildFile (prefix + '(' + String (num++) + ')' + suffix);
			else
				f = getChildFile (prefix + String (num++) + suffix);

		} while (f.exists());
	}

	return f;
}

const File File::getNonexistentSibling (const bool putNumbersInBrackets) const
{
	if (exists())
	{
		return getParentDirectory()
				.getNonexistentChildFile (getFileNameWithoutExtension(),
										  getFileExtension(),
										  putNumbersInBrackets);
	}
	else
	{
		return *this;
	}
}

const String File::getFileExtension() const
{
	String ext;

	if (! isDirectory())
	{
		const int indexOfDot = fullPath.lastIndexOfChar ('.');

		if (indexOfDot > fullPath.lastIndexOfChar (separator))
			ext = fullPath.substring (indexOfDot);
	}

	return ext;
}

bool File::hasFileExtension (const String& possibleSuffix) const
{
	if (possibleSuffix.isEmpty())
		return fullPath.lastIndexOfChar ('.') <= fullPath.lastIndexOfChar (separator);

	const int semicolon = possibleSuffix.indexOfChar (0, ';');

	if (semicolon >= 0)
	{
		return hasFileExtension (possibleSuffix.substring (0, semicolon).trimEnd())
				|| hasFileExtension (possibleSuffix.substring (semicolon + 1).trimStart());
	}
	else
	{
		if (fullPath.endsWithIgnoreCase (possibleSuffix))
		{
			if (possibleSuffix.startsWithChar ('.'))
				return true;

			const int dotPos = fullPath.length() - possibleSuffix.length() - 1;

			if (dotPos >= 0)
				return fullPath [dotPos] == '.';
		}
	}

	return false;
}

const File File::withFileExtension (const String& newExtension) const
{
	if (fullPath.isEmpty())
		return File::nonexistent;

	String filePart (getFileName());

	int i = filePart.lastIndexOfChar ('.');
	if (i >= 0)
		filePart = filePart.substring (0, i);

	if (newExtension.isNotEmpty() && ! newExtension.startsWithChar ('.'))
		filePart << '.';

	return getSiblingFile (filePart + newExtension);
}

bool File::startAsProcess (const String& parameters) const
{
	return exists() && juce_launchFile (fullPath, parameters);
}

FileInputStream* File::createInputStream() const
{
	if (existsAsFile())
		return new FileInputStream (*this);

	return 0;
}

FileOutputStream* File::createOutputStream (const int bufferSize) const
{
	ScopedPointer <FileOutputStream> out (new FileOutputStream (*this, bufferSize));

	if (out->failedToOpen())
		return 0;

	return out.release();
}

bool File::appendData (const void* const dataToAppend,
					   const int numberOfBytes) const
{
	if (numberOfBytes > 0)
	{
		const ScopedPointer <FileOutputStream> out (createOutputStream());

		if (out == 0)
			return false;

		out->write (dataToAppend, numberOfBytes);
	}

	return true;
}

bool File::replaceWithData (const void* const dataToWrite,
							const int numberOfBytes) const
{
	jassert (numberOfBytes >= 0); // a negative number of bytes??

	if (numberOfBytes <= 0)
		return deleteFile();

	TemporaryFile tempFile (*this, TemporaryFile::useHiddenFile);
	tempFile.getFile().appendData (dataToWrite, numberOfBytes);
	return tempFile.overwriteTargetFileWithTemporary();
}

bool File::appendText (const String& text,
					   const bool asUnicode,
					   const bool writeUnicodeHeaderBytes) const
{
	const ScopedPointer <FileOutputStream> out (createOutputStream());

	if (out != 0)
	{
		out->writeText (text, asUnicode, writeUnicodeHeaderBytes);
		return true;
	}

	return false;
}

bool File::replaceWithText (const String& textToWrite,
							const bool asUnicode,
							const bool writeUnicodeHeaderBytes) const
{
	TemporaryFile tempFile (*this, TemporaryFile::useHiddenFile);
	tempFile.getFile().appendText (textToWrite, asUnicode, writeUnicodeHeaderBytes);
	return tempFile.overwriteTargetFileWithTemporary();
}

const String File::createLegalPathName (const String& original)
{
	String s (original);
	String start;

	if (s[1] == ':')
	{
		start = s.substring (0, 2);
		s = s.substring (2);
	}

	return start + s.removeCharacters ("\"#@,;:<>*^|?")
					.substring (0, 1024);
}

const String File::createLegalFileName (const String& original)
{
	String s (original.removeCharacters ("\"#@,;:<>*^|?\\/"));

	const int maxLength = 128; // only the length of the filename, not the whole path
	const int len = s.length();

	if (len > maxLength)
	{
		const int lastDot = s.lastIndexOfChar ('.');

		if (lastDot > jmax (0, len - 12))
		{
			s = s.substring (0, maxLength - (len - lastDot))
				 + s.substring (lastDot);
		}
		else
		{
			s = s.substring (0, maxLength);
		}
	}

	return s;
}

const String File::getRelativePathFrom (const File& dir)  const
{
	String thisPath (fullPath);

	{
		int len = thisPath.length();
		while (--len >= 0 && thisPath [len] == File::separator)
			thisPath [len] = 0;
	}

	String dirPath (juce_addTrailingSeparator ((dir.existsAsFile()) ? dir.getParentDirectory().getFullPathName()
																	: dir.fullPath));

	const int len = jmin (thisPath.length(), dirPath.length());
	int commonBitLength = 0;

	for (int i = 0; i < len; ++i)
	{
#if NAMES_ARE_CASE_SENSITIVE
		if (thisPath[i] != dirPath[i])
#else
		if (CharacterFunctions::toLowerCase (thisPath[i])
			 != CharacterFunctions::toLowerCase (dirPath[i]))
#endif
		{
			break;
		}

		++commonBitLength;
	}

	while (commonBitLength > 0 && thisPath [commonBitLength - 1] != File::separator)
		--commonBitLength;

	// if the only common bit is the root, then just return the full path..
	if (commonBitLength <= 0
		 || (commonBitLength == 1 && thisPath [1] == File::separator))
		return fullPath;

	thisPath = thisPath.substring (commonBitLength);
	dirPath  = dirPath.substring (commonBitLength);

	while (dirPath.isNotEmpty())
	{
#if JUCE_WINDOWS
		thisPath = "..\\" + thisPath;
#else
		thisPath = "../" + thisPath;
#endif

		const int sep = dirPath.indexOfChar (separator);

		if (sep >= 0)
			dirPath = dirPath.substring (sep + 1);
		else
			dirPath = String::empty;
	}

	return thisPath;
}

void File::findFileSystemRoots (Array<File>& destArray)
{
	const StringArray roots (juce_getFileSystemRoots());

	for (int i = 0; i < roots.size(); ++i)
		destArray.add (File (roots[i]));
}

const String File::getVolumeLabel() const
{
	int serialNum;
	return juce_getVolumeLabel (fullPath, serialNum);
}

int File::getVolumeSerialNumber() const
{
	int serialNum;
	juce_getVolumeLabel (fullPath, serialNum);

	return serialNum;
}

const File File::createTempFile (const String& fileNameEnding)
{
	const File tempFile (getSpecialLocation (tempDirectory)
							.getChildFile ("temp_" + String (Random::getSystemRandom().nextInt()))
							.withFileExtension (fileNameEnding));

	if (tempFile.exists())
		return createTempFile (fileNameEnding);
	else
		return tempFile;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_File.cpp ***/


/*** Start of inlined file: juce_FileInputStream.cpp ***/
BEGIN_JUCE_NAMESPACE

void* juce_fileOpen (const String& path, bool forWriting);
void juce_fileClose (void* handle);
int juce_fileRead (void* handle, void* buffer, int size);
int64 juce_fileSetPosition (void* handle, int64 pos);

FileInputStream::FileInputStream (const File& f)
	: file (f),
	  currentPosition (0),
	  needToSeek (true)
{
	totalSize = f.getSize();

	fileHandle = juce_fileOpen (f.getFullPathName(), false);
}

FileInputStream::~FileInputStream()
{
	juce_fileClose (fileHandle);
}

int64 FileInputStream::getTotalLength()
{
	return totalSize;
}

int FileInputStream::read (void* buffer, int bytesToRead)
{
	int num = 0;

	if (needToSeek)
	{
		if (juce_fileSetPosition (fileHandle, currentPosition) < 0)
			return 0;

		needToSeek = false;
	}

	num = juce_fileRead (fileHandle, buffer, bytesToRead);
	currentPosition += num;

	return num;
}

bool FileInputStream::isExhausted()
{
	return currentPosition >= totalSize;
}

int64 FileInputStream::getPosition()
{
	return currentPosition;
}

bool FileInputStream::setPosition (int64 pos)
{
	pos = jlimit ((int64) 0, totalSize, pos);

	needToSeek |= (currentPosition != pos);
	currentPosition = pos;

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileInputStream.cpp ***/


/*** Start of inlined file: juce_FileOutputStream.cpp ***/
BEGIN_JUCE_NAMESPACE

void* juce_fileOpen (const String& path, bool forWriting);
void juce_fileClose (void* handle);
int juce_fileWrite (void* handle, const void* buffer, int size);
void juce_fileFlush (void* handle);
int64 juce_fileGetPosition (void* handle);
int64 juce_fileSetPosition (void* handle, int64 pos);

FileOutputStream::FileOutputStream (const File& f,
									const int bufferSize_)
	: file (f),
	  bufferSize (bufferSize_),
	  bytesInBuffer (0)
{
	fileHandle = juce_fileOpen (f.getFullPathName(), true);

	if (fileHandle != 0)
	{
		currentPosition = juce_fileGetPosition (fileHandle);

		if (currentPosition < 0)
		{
			jassertfalse
			juce_fileClose (fileHandle);
			fileHandle = 0;
		}
	}

	buffer.malloc (jmax (bufferSize_, 16));
}

FileOutputStream::~FileOutputStream()
{
	flush();

	juce_fileClose (fileHandle);
}

int64 FileOutputStream::getPosition()
{
	return currentPosition;
}

bool FileOutputStream::setPosition (int64 newPosition)
{
	if (newPosition != currentPosition)
	{
		flush();
		currentPosition = juce_fileSetPosition (fileHandle, newPosition);
	}

	return newPosition == currentPosition;
}

void FileOutputStream::flush()
{
	if (bytesInBuffer > 0)
	{
		juce_fileWrite (fileHandle, buffer, bytesInBuffer);
		bytesInBuffer = 0;
	}

	juce_fileFlush (fileHandle);
}

bool FileOutputStream::write (const void* const src, const int numBytes)
{
	if (bytesInBuffer + numBytes < bufferSize)
	{
		memcpy (buffer + bytesInBuffer, src, numBytes);
		bytesInBuffer += numBytes;
		currentPosition += numBytes;
	}
	else
	{
		if (bytesInBuffer > 0)
		{
			// flush the reservoir
			const bool wroteOk = (juce_fileWrite (fileHandle, buffer, bytesInBuffer) == bytesInBuffer);
			bytesInBuffer = 0;

			if (! wroteOk)
				return false;
		}

		if (numBytes < bufferSize)
		{
			memcpy (buffer + bytesInBuffer, src, numBytes);
			bytesInBuffer += numBytes;
			currentPosition += numBytes;
		}
		else
		{
			const int bytesWritten = juce_fileWrite (fileHandle, src, numBytes);
			currentPosition += bytesWritten;

			return bytesWritten == numBytes;
		}
	}

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileOutputStream.cpp ***/


/*** Start of inlined file: juce_FileSearchPath.cpp ***/
BEGIN_JUCE_NAMESPACE

FileSearchPath::FileSearchPath()
{
}

FileSearchPath::FileSearchPath (const String& path)
{
	init (path);
}

FileSearchPath::FileSearchPath (const FileSearchPath& other)
  : directories (other.directories)
{
}

FileSearchPath::~FileSearchPath()
{
}

FileSearchPath& FileSearchPath::operator= (const String& path)
{
	init (path);
	return *this;
}

void FileSearchPath::init (const String& path)
{
	directories.clear();
	directories.addTokens (path, ";", "\"");
	directories.trim();
	directories.removeEmptyStrings();

	for (int i = directories.size(); --i >= 0;)
		directories.set (i, directories[i].unquoted());
}

int FileSearchPath::getNumPaths() const
{
	return directories.size();
}

const File FileSearchPath::operator[] (const int index) const
{
	return File (directories [index]);
}

const String FileSearchPath::toString() const
{
	StringArray directories2 (directories);
	for (int i = directories2.size(); --i >= 0;)
		if (directories2[i].containsChar (';'))
			directories2.set (i, directories2[i].quoted());

	return directories2.joinIntoString (";");
}

void FileSearchPath::add (const File& dir, const int insertIndex)
{
	directories.insert (insertIndex, dir.getFullPathName());
}

void FileSearchPath::addIfNotAlreadyThere (const File& dir)
{
	for (int i = 0; i < directories.size(); ++i)
		if (File (directories[i]) == dir)
			return;

	add (dir);
}

void FileSearchPath::remove (const int index)
{
	directories.remove (index);
}

void FileSearchPath::addPath (const FileSearchPath& other)
{
	for (int i = 0; i < other.getNumPaths(); ++i)
		addIfNotAlreadyThere (other[i]);
}

void FileSearchPath::removeRedundantPaths()
{
	for (int i = directories.size(); --i >= 0;)
	{
		const File d1 (directories[i]);

		for (int j = directories.size(); --j >= 0;)
		{
			const File d2 (directories[j]);

			if ((i != j) && (d1.isAChildOf (d2) || d1 == d2))
			{
				directories.remove (i);
				break;
			}
		}
	}
}

void FileSearchPath::removeNonExistentPaths()
{
	for (int i = directories.size(); --i >= 0;)
		if (! File (directories[i]).isDirectory())
			directories.remove (i);
}

int FileSearchPath::findChildFiles (Array<File>& results,
									const int whatToLookFor,
									const bool searchRecursively,
									const String& wildCardPattern) const
{
	int total = 0;

	for (int i = 0; i < directories.size(); ++i)
		total += operator[] (i).findChildFiles (results,
												whatToLookFor,
												searchRecursively,
												wildCardPattern);

	return total;
}

bool FileSearchPath::isFileInPath (const File& fileToCheck,
								   const bool checkRecursively) const
{
	for (int i = directories.size(); --i >= 0;)
	{
		const File d (directories[i]);

		if (checkRecursively)
		{
			if (fileToCheck.isAChildOf (d))
				return true;
		}
		else
		{
			if (fileToCheck.getParentDirectory() == d)
				return true;
		}
	}

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileSearchPath.cpp ***/


/*** Start of inlined file: juce_NamedPipe.cpp ***/
BEGIN_JUCE_NAMESPACE

NamedPipe::NamedPipe()
	: internal (0)
{
}

NamedPipe::~NamedPipe()
{
	close();
}

bool NamedPipe::openExisting (const String& pipeName)
{
	currentPipeName = pipeName;
	return openInternal (pipeName, false);
}

bool NamedPipe::createNewPipe (const String& pipeName)
{
	currentPipeName = pipeName;
	return openInternal (pipeName, true);
}

bool NamedPipe::isOpen() const
{
	return internal != 0;
}

const String NamedPipe::getName() const
{
	return currentPipeName;
}

// other methods for this class are implemented in the platform-specific files

END_JUCE_NAMESPACE
/*** End of inlined file: juce_NamedPipe.cpp ***/


/*** Start of inlined file: juce_TemporaryFile.cpp ***/
BEGIN_JUCE_NAMESPACE

TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags)
{
	createTempFile (File::getSpecialLocation (File::tempDirectory),
					"temp_" + String (Random::getSystemRandom().nextInt()),
					suffix,
					optionFlags);
}

TemporaryFile::TemporaryFile (const File& targetFile_, const int optionFlags)
	: targetFile (targetFile_)
{
	// If you use this constructor, you need to give it a valid target file!
	jassert (targetFile != File::nonexistent);

	createTempFile (targetFile.getParentDirectory(),
					targetFile.getFileNameWithoutExtension() + "_temp" + String (Random::getSystemRandom().nextInt()),
					targetFile.getFileExtension(),
					optionFlags);
}

void TemporaryFile::createTempFile (const File& parentDirectory, String name,
									const String& suffix, const int optionFlags)
{
	if ((optionFlags & useHiddenFile) != 0)
		name = "." + name;

	temporaryFile = parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & putNumbersInBrackets) != 0);
}

TemporaryFile::~TemporaryFile()
{
	// Have a few attempts at deleting the file before giving up..
	for (int i = 5; --i >= 0;)
	{
		if (temporaryFile.deleteFile())
			return;

		Thread::sleep (50);
	}

	// Failed to delete our temporary file! Check that you've deleted all the
	// file output streams that were using it!
	jassertfalse;
}

bool TemporaryFile::overwriteTargetFileWithTemporary() const
{
	// This method only works if you created this object with the constructor
	// that takes a target file!
	jassert (targetFile != File::nonexistent);

	if (temporaryFile.exists())
	{
		// Have a few attempts at overwriting the file before giving up..
		for (int i = 5; --i >= 0;)
		{
			if (temporaryFile.moveFileTo (targetFile))
				return true;

			Thread::sleep (100);
		}
	}
	else
	{
		// There's no temporary file to use. If your write failed, you should
		// probably check, and not bother calling this method.
		jassertfalse
	}

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TemporaryFile.cpp ***/


/*** Start of inlined file: juce_Socket.cpp ***/
#if JUCE_WINDOWS
  #include <winsock2.h>

  #ifdef _MSC_VER
	#pragma warning (disable : 4127 4389 4018)
  #endif

#else
  #if JUCE_LINUX
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <sys/errno.h>
	#include <unistd.h>
	#include <netinet/in.h>
  #elif (MACOSX_DEPLOYMENT_TARGET <= MAC_OS_X_VERSION_10_4) && ! JUCE_IPHONE
	#include <CoreServices/CoreServices.h>
  #endif

  #include <fcntl.h>
  #include <netdb.h>
  #include <arpa/inet.h>
  #include <netinet/tcp.h>
#endif

BEGIN_JUCE_NAMESPACE

#if defined (JUCE_LINUX) || defined (JUCE_MAC) || defined (JUCE_IPHONE)
 typedef socklen_t juce_socklen_t;
#else
 typedef int juce_socklen_t;
#endif

#if JUCE_WINDOWS

typedef int (__stdcall juce_CloseWin32SocketLibCall) (void);
juce_CloseWin32SocketLibCall* juce_CloseWin32SocketLib = 0;

static void initWin32Sockets()
{
	static CriticalSection lock;
	const ScopedLock sl (lock);

	if (juce_CloseWin32SocketLib == 0)
	{
		WSADATA wsaData;
		const WORD wVersionRequested = MAKEWORD (1, 1);
		WSAStartup (wVersionRequested, &wsaData);

		juce_CloseWin32SocketLib = &WSACleanup;
	}
}

#endif

static bool resetSocketOptions (const int handle, const bool isDatagram, const bool allowBroadcast) throw()
{
	const int sndBufSize = 65536;
	const int rcvBufSize = 65536;
	const int one = 1;

	return handle > 0
			&& setsockopt (handle, SOL_SOCKET, SO_RCVBUF, (const char*) &rcvBufSize, sizeof (rcvBufSize)) == 0
			&& setsockopt (handle, SOL_SOCKET, SO_SNDBUF, (const char*) &sndBufSize, sizeof (sndBufSize)) == 0
			&& (isDatagram ? ((! allowBroadcast) || setsockopt (handle, SOL_SOCKET, SO_BROADCAST, (const char*) &one, sizeof (one)) == 0)
						   : (setsockopt (handle, IPPROTO_TCP, TCP_NODELAY, (const char*) &one, sizeof (one)) == 0));
}

static bool bindSocketToPort (const int handle, const int port) throw()
{
	if (handle <= 0 || port <= 0)
		return false;

	struct sockaddr_in servTmpAddr;
	zerostruct (servTmpAddr);
	servTmpAddr.sin_family = PF_INET;
	servTmpAddr.sin_addr.s_addr = htonl (INADDR_ANY);
	servTmpAddr.sin_port = htons ((uint16) port);

	return bind (handle, (struct sockaddr*) &servTmpAddr, sizeof (struct sockaddr_in)) >= 0;
}

static int readSocket (const int handle,
					   void* const destBuffer, const int maxBytesToRead,
					   bool volatile& connected,
					   const bool blockUntilSpecifiedAmountHasArrived) throw()
{
	int bytesRead = 0;

	while (bytesRead < maxBytesToRead)
	{
		int bytesThisTime;

#if JUCE_WINDOWS
		bytesThisTime = recv (handle, ((char*) destBuffer) + bytesRead, maxBytesToRead - bytesRead, 0);
#else
		while ((bytesThisTime = (int) ::read (handle, ((char*) destBuffer) + bytesRead, maxBytesToRead - bytesRead)) < 0
				 && errno == EINTR
				 && connected)
		{
		}
#endif

		if (bytesThisTime <= 0 || ! connected)
		{
			if (bytesRead == 0)
				bytesRead = -1;

			break;
		}

		bytesRead += bytesThisTime;

		if (! blockUntilSpecifiedAmountHasArrived)
			break;
	}

	return bytesRead;
}

static int waitForReadiness (const int handle, const bool forReading,
							 const int timeoutMsecs) throw()
{
	struct timeval timeout;
	struct timeval* timeoutp;

	if (timeoutMsecs >= 0)
	{
		timeout.tv_sec = timeoutMsecs / 1000;
		timeout.tv_usec = (timeoutMsecs % 1000) * 1000;
		timeoutp = &timeout;
	}
	else
	{
		timeoutp = 0;
	}

	fd_set rset, wset;
	FD_ZERO (&rset);
	FD_SET (handle, &rset);
	FD_ZERO (&wset);
	FD_SET (handle, &wset);

	fd_set* const prset = forReading ? &rset : 0;
	fd_set* const pwset = forReading ? 0 : &wset;

#if JUCE_WINDOWS
	if (select (handle + 1, prset, pwset, 0, timeoutp) < 0)
		return -1;
#else
	{
		int result;
		while ((result = select (handle + 1, prset, pwset, 0, timeoutp)) < 0
				&& errno == EINTR)
		{
		}

		if (result < 0)
			return -1;
	}
#endif

	{
		int opt;
		juce_socklen_t len = sizeof (opt);

		if (getsockopt (handle, SOL_SOCKET, SO_ERROR, (char*) &opt, &len) < 0
			 || opt != 0)
			return -1;
	}

	if ((forReading && FD_ISSET (handle, &rset))
		 || ((! forReading) && FD_ISSET (handle, &wset)))
		return 1;

	return 0;
}

static bool setSocketBlockingState (const int handle, const bool shouldBlock) throw()
{
#if JUCE_WINDOWS
	u_long nonBlocking = shouldBlock ? 0 : 1;

	if (ioctlsocket (handle, FIONBIO, &nonBlocking) != 0)
		return false;
#else
	int socketFlags = fcntl (handle, F_GETFL, 0);

	if (socketFlags == -1)
		return false;

	if (shouldBlock)
		socketFlags &= ~O_NONBLOCK;
	else
		socketFlags |= O_NONBLOCK;

	if (fcntl (handle, F_SETFL, socketFlags) != 0)
		return false;
#endif

	return true;
}

static bool connectSocket (int volatile& handle,
						   const bool isDatagram,
						   void** serverAddress,
						   const String& hostName,
						   const int portNumber,
						   const int timeOutMillisecs) throw()
{
	struct hostent* const hostEnt = gethostbyname (hostName.toUTF8());

	if (hostEnt == 0)
		return false;

	struct in_addr targetAddress;
	memcpy (&targetAddress.s_addr,
			*(hostEnt->h_addr_list),
			sizeof (targetAddress.s_addr));

	struct sockaddr_in servTmpAddr;
	zerostruct (servTmpAddr);
	servTmpAddr.sin_family = PF_INET;
	servTmpAddr.sin_addr = targetAddress;
	servTmpAddr.sin_port = htons ((uint16) portNumber);

	if (handle < 0)
		handle = (int) socket (AF_INET, isDatagram ? SOCK_DGRAM : SOCK_STREAM, 0);

	if (handle < 0)
		return false;

	if (isDatagram)
	{
		*serverAddress = new struct sockaddr_in();
		*((struct sockaddr_in*) *serverAddress) = servTmpAddr;

		return true;
	}

	setSocketBlockingState (handle, false);

	const int result = ::connect (handle, (struct sockaddr*) &servTmpAddr, sizeof (struct sockaddr_in));

	if (result < 0)
	{
#if JUCE_WINDOWS
		if (result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
#else
		if (errno == EINPROGRESS)
#endif
		{
			if (waitForReadiness (handle, false, timeOutMillisecs) != 1)
			{
				setSocketBlockingState (handle, true);
				return false;
			}
		}
	}

	setSocketBlockingState (handle, true);
	resetSocketOptions (handle, false, false);

	return true;
}

StreamingSocket::StreamingSocket()
	: portNumber (0),
	  handle (-1),
	  connected (false),
	  isListener (false)
{
#if JUCE_WINDOWS
	initWin32Sockets();
#endif
}

StreamingSocket::StreamingSocket (const String& hostName_,
								  const int portNumber_,
								  const int handle_)
	: hostName (hostName_),
	  portNumber (portNumber_),
	  handle (handle_),
	  connected (true),
	  isListener (false)
{
#if JUCE_WINDOWS
	initWin32Sockets();
#endif

	resetSocketOptions (handle_, false, false);
}

StreamingSocket::~StreamingSocket()
{
	close();
}

int StreamingSocket::read (void* destBuffer, const int maxBytesToRead, const bool blockUntilSpecifiedAmountHasArrived)
{
	return (connected && ! isListener) ? readSocket (handle, destBuffer, maxBytesToRead, connected, blockUntilSpecifiedAmountHasArrived)
									   : -1;
}

int StreamingSocket::write (const void* sourceBuffer, const int numBytesToWrite)
{
	if (isListener || ! connected)
		return -1;

#if JUCE_WINDOWS
	return send (handle, (const char*) sourceBuffer, numBytesToWrite, 0);
#else
	int result;

	while ((result = (int) ::write (handle, sourceBuffer, numBytesToWrite)) < 0
			&& errno == EINTR)
	{
	}

	return result;
#endif
}

int StreamingSocket::waitUntilReady (const bool readyForReading,
									 const int timeoutMsecs) const
{
	return connected ? waitForReadiness (handle, readyForReading, timeoutMsecs)
					 : -1;
}

bool StreamingSocket::bindToPort (const int port)
{
	return bindSocketToPort (handle, port);
}

bool StreamingSocket::connect (const String& remoteHostName,
							   const int remotePortNumber,
							   const int timeOutMillisecs)
{
	if (isListener)
	{
		jassertfalse	// a listener socket can't connect to another one!
		return false;
	}

	if (connected)
		close();

	hostName = remoteHostName;
	portNumber = remotePortNumber;
	isListener = false;

	connected = connectSocket (handle, false, 0, remoteHostName,
							   remotePortNumber, timeOutMillisecs);

	if (! (connected && resetSocketOptions (handle, false, false)))
	{
		close();
		return false;
	}

	return true;
}

void StreamingSocket::close()
{
#if JUCE_WINDOWS
	if (handle != SOCKET_ERROR || connected)
		closesocket (handle);

	connected = false;
#else
	if (connected)
	{
		connected = false;

		if (isListener)
		{
			// need to do this to interrupt the accept() function..
			StreamingSocket temp;
			temp.connect ("localhost", portNumber, 1000);
		}
	}

	if (handle != -1)
		::close (handle);
#endif

	hostName = String::empty;
	portNumber = 0;
	handle = -1;
	isListener = false;
}

bool StreamingSocket::createListener (const int newPortNumber, const String& localHostName)
{
	if (connected)
		close();

	hostName = "listener";
	portNumber = newPortNumber;
	isListener = true;

	struct sockaddr_in servTmpAddr;
	zerostruct (servTmpAddr);
	servTmpAddr.sin_family = PF_INET;
	servTmpAddr.sin_addr.s_addr = htonl (INADDR_ANY);

	if (localHostName.isNotEmpty())
		servTmpAddr.sin_addr.s_addr = ::inet_addr (localHostName.toUTF8());

	servTmpAddr.sin_port = htons ((uint16) portNumber);

	handle = (int) socket (AF_INET, SOCK_STREAM, 0);

	if (handle < 0)
		return false;

	const int reuse = 1;
	setsockopt (handle, SOL_SOCKET, SO_REUSEADDR, (const char*) &reuse, sizeof (reuse));

	if (bind (handle, (struct sockaddr*) &servTmpAddr, sizeof (struct sockaddr_in)) < 0
		 || listen (handle, SOMAXCONN) < 0)
	{
		close();
		return false;
	}

	connected = true;
	return true;
}

StreamingSocket* StreamingSocket::waitForNextConnection() const
{
	jassert (isListener || ! connected); // to call this method, you first have to use createListener() to
										 // prepare this socket as a listener.

	if (connected && isListener)
	{
		struct sockaddr address;
		juce_socklen_t len = sizeof (sockaddr);
		const int newSocket = (int) accept (handle, &address, &len);

		if (newSocket >= 0 && connected)
			return new StreamingSocket (inet_ntoa (((struct sockaddr_in*) &address)->sin_addr),
										portNumber, newSocket);
	}

	return 0;
}

bool StreamingSocket::isLocal() const throw()
{
	return hostName == "127.0.0.1";
}

DatagramSocket::DatagramSocket (const int localPortNumber, const bool allowBroadcast_)
	: portNumber (0),
	  handle (-1),
	  connected (true),
	  allowBroadcast (allowBroadcast_),
	  serverAddress (0)
{
#if JUCE_WINDOWS
	initWin32Sockets();
#endif

	handle = (int) socket (AF_INET, SOCK_DGRAM, 0);
	bindToPort (localPortNumber);
}

DatagramSocket::DatagramSocket (const String& hostName_, const int portNumber_,
								const int handle_, const int localPortNumber)
	: hostName (hostName_),
	  portNumber (portNumber_),
	  handle (handle_),
	  connected (true),
	  allowBroadcast (false),
	  serverAddress (0)
{
#if JUCE_WINDOWS
	initWin32Sockets();
#endif

	resetSocketOptions (handle_, true, allowBroadcast);
	bindToPort (localPortNumber);
}

DatagramSocket::~DatagramSocket()
{
	close();

	delete ((struct sockaddr_in*) serverAddress);
	serverAddress = 0;
}

void DatagramSocket::close()
{
#if JUCE_WINDOWS
	closesocket (handle);
	connected = false;
#else
	connected = false;
	::close (handle);
#endif

	hostName = String::empty;
	portNumber = 0;
	handle = -1;
}

bool DatagramSocket::bindToPort (const int port)
{
	return bindSocketToPort (handle, port);
}

bool DatagramSocket::connect (const String& remoteHostName,
							  const int remotePortNumber,
							  const int timeOutMillisecs)
{
	if (connected)
		close();

	hostName = remoteHostName;
	portNumber = remotePortNumber;

	connected = connectSocket (handle, true, &serverAddress,
							   remoteHostName, remotePortNumber,
							   timeOutMillisecs);

	if (! (connected && resetSocketOptions (handle, true, allowBroadcast)))
	{
		close();
		return false;
	}

	return true;
}

DatagramSocket* DatagramSocket::waitForNextConnection() const
{
	struct sockaddr address;
	juce_socklen_t len = sizeof (sockaddr);

	while (waitUntilReady (true, -1) == 1)
	{
		char buf[1];

		if (recvfrom (handle, buf, 0, 0, &address, &len) > 0)
		{
			return new DatagramSocket (inet_ntoa (((struct sockaddr_in*) &address)->sin_addr),
									   ntohs (((struct sockaddr_in*) &address)->sin_port),
									   -1, -1);
		}
	}

	return 0;
}

int DatagramSocket::waitUntilReady (const bool readyForReading,
									const int timeoutMsecs) const
{
	return connected ? waitForReadiness (handle, readyForReading, timeoutMsecs)
					 : -1;
}

int DatagramSocket::read (void* destBuffer, const int maxBytesToRead, const bool blockUntilSpecifiedAmountHasArrived)
{
	return connected ? readSocket (handle, destBuffer, maxBytesToRead, connected, blockUntilSpecifiedAmountHasArrived)
					 : -1;
}

int DatagramSocket::write (const void* sourceBuffer, const int numBytesToWrite)
{
	// You need to call connect() first to set the server address..
	jassert (serverAddress != 0 && connected);

	return connected ? (int) sendto (handle, (const char*) sourceBuffer,
									 numBytesToWrite, 0,
									 (const struct sockaddr*) serverAddress,
									 sizeof (struct sockaddr_in))
					 : -1;
}

bool DatagramSocket::isLocal() const throw()
{
	return hostName == "127.0.0.1";
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Socket.cpp ***/


/*** Start of inlined file: juce_URL.cpp ***/
BEGIN_JUCE_NAMESPACE

URL::URL()
{
}

URL::URL (const String& url_)
	: url (url_)
{
	int i = url.indexOfChar ('?');

	if (i >= 0)
	{
		do
		{
			const int nextAmp   = url.indexOfChar (i + 1, '&');
			const int equalsPos = url.indexOfChar (i + 1, '=');

			if (equalsPos > i + 1)
			{
				if (nextAmp < 0)
				{
					parameters.set (removeEscapeChars (url.substring (i + 1, equalsPos)),
									removeEscapeChars (url.substring (equalsPos + 1)));
				}
				else if (nextAmp > 0 && equalsPos < nextAmp)
				{
					parameters.set (removeEscapeChars (url.substring (i + 1, equalsPos)),
									removeEscapeChars (url.substring (equalsPos + 1, nextAmp)));
				}
			}

			i = nextAmp;
		}
		while (i >= 0);

		url = url.upToFirstOccurrenceOf ("?", false, false);
	}
}

URL::URL (const URL& other)
	: url (other.url),
	  postData (other.postData),
	  parameters (other.parameters),
	  filesToUpload (other.filesToUpload),
	  mimeTypes (other.mimeTypes)
{
}

URL& URL::operator= (const URL& other)
{
	url = other.url;
	postData = other.postData;
	parameters = other.parameters;
	filesToUpload = other.filesToUpload;
	mimeTypes = other.mimeTypes;

	return *this;
}

URL::~URL()
{
}

static const String getMangledParameters (const StringPairArray& parameters)
{
	String p;

	for (int i = 0; i < parameters.size(); ++i)
	{
		if (i > 0)
			p += '&';

		p << URL::addEscapeChars (parameters.getAllKeys() [i], true)
		  << '='
		  << URL::addEscapeChars (parameters.getAllValues() [i], true);
	}

	return p;
}

const String URL::toString (const bool includeGetParameters) const
{
	if (includeGetParameters && parameters.size() > 0)
		return url + "?" + getMangledParameters (parameters);
	else
		return url;
}

bool URL::isWellFormed() const
{
	//xxx TODO
	return url.isNotEmpty();
}

static int findStartOfDomain (const String& url)
{
	int i = 0;

	while (CharacterFunctions::isLetterOrDigit (url[i])
		   || CharacterFunctions::indexOfChar (L"+-.", url[i], false) >= 0)
		++i;

	return url[i] == ':' ? i + 1 : 0;
}

const String URL::getDomain() const
{
	int start = findStartOfDomain (url);
	while (url[start] == '/')
		++start;

	const int end1 = url.indexOfChar (start, '/');
	const int end2 = url.indexOfChar (start, ':');

	const int end = (end1 < 0 || end2 < 0) ? jmax (end1, end2)
										   : jmin (end1, end2);

	return url.substring (start, end);
}

const String URL::getSubPath() const
{
	int start = findStartOfDomain (url);
	while (url[start] == '/')
		++start;

	const int startOfPath = url.indexOfChar (start, '/') + 1;

	return startOfPath <= 0 ? String::empty
							: url.substring (startOfPath);
}

const String URL::getScheme() const
{
	return url.substring (0, findStartOfDomain (url) - 1);
}

const URL URL::withNewSubPath (const String& newPath) const
{
	int start = findStartOfDomain (url);
	while (url[start] == '/')
		++start;

	const int startOfPath = url.indexOfChar (start, '/') + 1;

	URL u (*this);

	if (startOfPath > 0)
		u.url = url.substring (0, startOfPath);

	if (! u.url.endsWithChar ('/'))
		u.url << '/';

	if (newPath.startsWithChar ('/'))
		u.url << newPath.substring (1);
	else
		u.url << newPath;

	return u;
}

bool URL::isProbablyAWebsiteURL (const String& possibleURL)
{
	if (possibleURL.startsWithIgnoreCase ("http:")
		 || possibleURL.startsWithIgnoreCase ("ftp:"))
		return true;

	if (possibleURL.startsWithIgnoreCase ("file:")
		 || possibleURL.containsChar ('@')
		 || possibleURL.endsWithChar ('.')
		 || (! possibleURL.containsChar ('.')))
		return false;

	if (possibleURL.startsWithIgnoreCase ("www.")
		 && possibleURL.substring (5).containsChar ('.'))
		return true;

	const char* commonTLDs[] = { "com", "net", "org", "uk", "de", "fr", "jp" };

	for (int i = 0; i < numElementsInArray (commonTLDs); ++i)
		if ((possibleURL + "/").containsIgnoreCase ("." + String (commonTLDs[i]) + "/"))
			return true;

	return false;
}

bool URL::isProbablyAnEmailAddress (const String& possibleEmailAddress)
{
	const int atSign = possibleEmailAddress.indexOfChar ('@');

	return atSign > 0
			&& possibleEmailAddress.lastIndexOfChar ('.') > (atSign + 1)
			&& (! possibleEmailAddress.endsWithChar ('.'));
}

void* juce_openInternetFile (const String& url,
							 const String& headers,
							 const MemoryBlock& optionalPostData,
							 const bool isPost,
							 URL::OpenStreamProgressCallback* callback,
							 void* callbackContext,
							 int timeOutMs);

void juce_closeInternetFile (void* handle);
int juce_readFromInternetFile (void* handle, void* dest, int bytesToRead);
int juce_seekInInternetFile (void* handle, int newPosition);
int64 juce_getInternetFileContentLength (void* handle);

class WebInputStream  : public InputStream
{
public:

	WebInputStream (const URL& url,
					const bool isPost_,
					URL::OpenStreamProgressCallback* const progressCallback_,
					void* const progressCallbackContext_,
					const String& extraHeaders,
					int timeOutMs_)
	  : position (0),
		finished (false),
		isPost (isPost_),
		progressCallback (progressCallback_),
		progressCallbackContext (progressCallbackContext_),
		timeOutMs (timeOutMs_)
	{
		server = url.toString (! isPost);

		if (isPost_)
			createHeadersAndPostData (url);

		headers += extraHeaders;

		if (! headers.endsWithChar ('\n'))
			headers << "\r\n";

		handle = juce_openInternetFile (server, headers, postData, isPost,
										progressCallback_, progressCallbackContext_,
										timeOutMs);
	}

	~WebInputStream()
	{
		juce_closeInternetFile (handle);
	}

	bool isError() const
	{
		return handle == 0;
	}

	int64 getTotalLength()
	{
		return juce_getInternetFileContentLength (handle);
	}

	bool isExhausted()
	{
		return finished;
	}

	int read (void* dest, int bytes)
	{
		if (finished || isError())
		{
			return 0;
		}
		else
		{
			const int bytesRead = juce_readFromInternetFile (handle, dest, bytes);
			position += bytesRead;

			if (bytesRead == 0)
				finished = true;

			return bytesRead;
		}
	}

	int64 getPosition()
	{
		return position;
	}

	bool setPosition (int64 wantedPos)
	{
		if (wantedPos != position)
		{
			finished = false;

			const int actualPos = juce_seekInInternetFile (handle, (int) wantedPos);

			if (actualPos == wantedPos)
			{
				position = wantedPos;
			}
			else
			{
				if (wantedPos < position)
				{
					juce_closeInternetFile (handle);

					position = 0;
					finished = false;

					handle = juce_openInternetFile (server, headers, postData, isPost,
													progressCallback, progressCallbackContext,
													timeOutMs);
				}

				skipNextBytes (wantedPos - position);
			}
		}

		return true;
	}

	juce_UseDebuggingNewOperator

private:
	String server, headers;
	MemoryBlock postData;
	int64 position;
	bool finished;
	const bool isPost;
	void* handle;
	URL::OpenStreamProgressCallback* const progressCallback;
	void* const progressCallbackContext;
	const int timeOutMs;

	void createHeadersAndPostData (const URL& url)
	{
		if (url.getFilesToUpload().size() > 0)
		{
			// need to upload some files, so do it as multi-part...
			String boundary (String::toHexString (Random::getSystemRandom().nextInt64()));

			headers << "Content-Type: multipart/form-data; boundary=" << boundary << "\r\n";

			appendUTF8ToPostData ("--" + boundary);

			int i;
			for (i = 0; i < url.getParameters().size(); ++i)
			{
				String s;
				s << "\r\nContent-Disposition: form-data; name=\""
				  << url.getParameters().getAllKeys() [i]
				  << "\"\r\n\r\n"
				  << url.getParameters().getAllValues() [i]
				  << "\r\n--"
				  << boundary;

				appendUTF8ToPostData (s);
			}

			for (i = 0; i < url.getFilesToUpload().size(); ++i)
			{
				const File f (url.getFilesToUpload().getAllValues() [i]);
				const String paramName (url.getFilesToUpload().getAllKeys() [i]);

				String s;
				s << "\r\nContent-Disposition: form-data; name=\""
				  << paramName
				  << "\"; filename=\""
				  << f.getFileName()
				  << "\"\r\n";

				const String mimeType (url.getMimeTypesOfUploadFiles()
										  .getValue (paramName, String::empty));

				if (mimeType.isNotEmpty())
					s << "Content-Type: " << mimeType << "\r\n";

				s << "Content-Transfer-Encoding: binary\r\n\r\n";

				appendUTF8ToPostData (s);

				f.loadFileAsData (postData);

				s = "\r\n--" + boundary;

				appendUTF8ToPostData (s);
			}

			appendUTF8ToPostData ("--\r\n");
		}
		else
		{
			appendUTF8ToPostData (getMangledParameters (url.getParameters()));
			appendUTF8ToPostData (url.getPostData());

			// just a short text attachment, so use simple url encoding..
			headers = "Content-Type: application/x-www-form-urlencoded\r\nContent-length: "
						+ String ((unsigned int) postData.getSize())
						+ "\r\n";
		}
	}

	void appendUTF8ToPostData (const String& text)
	{
		postData.append (text.toUTF8(),
						 (int) strlen (text.toUTF8()));
	}

	WebInputStream (const WebInputStream&);
	WebInputStream& operator= (const WebInputStream&);
};

InputStream* URL::createInputStream (const bool usePostCommand,
									 OpenStreamProgressCallback* const progressCallback,
									 void* const progressCallbackContext,
									 const String& extraHeaders,
									 const int timeOutMs) const
{
	ScopedPointer <WebInputStream> wi (new WebInputStream (*this, usePostCommand,
														   progressCallback, progressCallbackContext,
														   extraHeaders,
														   timeOutMs));

	return wi->isError() ? 0 : wi.release();
}

bool URL::readEntireBinaryStream (MemoryBlock& destData,
								  const bool usePostCommand) const
{
	const ScopedPointer <InputStream> in (createInputStream (usePostCommand));

	if (in != 0)
	{
		in->readIntoMemoryBlock (destData, -1);
		return true;
	}

	return false;
}

const String URL::readEntireTextStream (const bool usePostCommand) const
{
	const ScopedPointer <InputStream> in (createInputStream (usePostCommand));

	if (in != 0)
		return in->readEntireStreamAsString();

	return String::empty;
}

XmlElement* URL::readEntireXmlStream (const bool usePostCommand) const
{
	XmlDocument doc (readEntireTextStream (usePostCommand));
	return doc.getDocumentElement();
}

const URL URL::withParameter (const String& parameterName,
							  const String& parameterValue) const
{
	URL u (*this);
	u.parameters.set (parameterName, parameterValue);
	return u;
}

const URL URL::withFileToUpload (const String& parameterName,
								 const File& fileToUpload,
								 const String& mimeType) const
{
	jassert (mimeType.isNotEmpty()); // You need to supply a mime type!

	URL u (*this);
	u.filesToUpload.set (parameterName, fileToUpload.getFullPathName());
	u.mimeTypes.set (parameterName, mimeType);
	return u;
}

const URL URL::withPOSTData (const String& postData_) const
{
	URL u (*this);
	u.postData = postData_;
	return u;
}

const StringPairArray& URL::getParameters() const
{
	return parameters;
}

const StringPairArray& URL::getFilesToUpload() const
{
	return filesToUpload;
}

const StringPairArray& URL::getMimeTypesOfUploadFiles() const
{
	return mimeTypes;
}

const String URL::removeEscapeChars (const String& s)
{
	String result (s.replaceCharacter ('+', ' '));
	int nextPercent = 0;

	for (;;)
	{
		nextPercent = result.indexOfChar (nextPercent, '%');

		if (nextPercent < 0)
			break;

		juce_wchar replacementChar = (juce_wchar) result.substring (nextPercent + 1, nextPercent + 3).getHexValue32();
		result = result.replaceSection (nextPercent, 3, String::charToString (replacementChar));
		++nextPercent;
	}

	return result;
}

const String URL::addEscapeChars (const String& s, const bool isParameter)
{
	String result;
	result.preallocateStorage (s.length() + 8);
	const char* utf8 = s.toUTF8();
	const char* legalChars = isParameter ? "_-.*!'()"
										 : "_-$.*!'(),";

	while (*utf8 != 0)
	{
		const char c = *utf8++;

		if (CharacterFunctions::isLetterOrDigit (c)
			 || CharacterFunctions::indexOfChar (legalChars, c, false) >= 0)
		{
			result << c;
		}
		else
		{
			const int v = (int) (uint8) c;
			result << (v < 0x10 ? "%0" : "%") << String::toHexString (v);
		}
	}

	return result;
}

extern bool juce_launchFile (const String& fileName, const String& parameters);

bool URL::launchInDefaultBrowser() const
{
	String u (toString (true));

	if (u.containsChar ('@') && ! u.containsChar (':'))
		u = "mailto:" + u;

	return juce_launchFile (u, String::empty);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_URL.cpp ***/


/*** Start of inlined file: juce_BufferedInputStream.cpp ***/
BEGIN_JUCE_NAMESPACE

BufferedInputStream::BufferedInputStream (InputStream* const source_,
										  const int bufferSize_,
										  const bool deleteSourceWhenDestroyed)
   : source (source_),
	 sourceToDelete (deleteSourceWhenDestroyed ? source_ : 0),
	 bufferSize (jmax (256, bufferSize_)),
	 position (source_->getPosition()),
	 lastReadPos (0),
	 bufferOverlap (128)
{
	const int sourceSize = (int) source_->getTotalLength();
	if (sourceSize >= 0)
		bufferSize = jmin (jmax (32, sourceSize), bufferSize);

	bufferStart = position;
	buffer.malloc (bufferSize);
}

BufferedInputStream::~BufferedInputStream()
{
}

int64 BufferedInputStream::getTotalLength()
{
	return source->getTotalLength();
}

int64 BufferedInputStream::getPosition()
{
	return position;
}

bool BufferedInputStream::setPosition (int64 newPosition)
{
	position = jmax ((int64) 0, newPosition);
	return true;
}

bool BufferedInputStream::isExhausted()
{
	return (position >= lastReadPos)
			 && source->isExhausted();
}

void BufferedInputStream::ensureBuffered()
{
	const int64 bufferEndOverlap = lastReadPos - bufferOverlap;

	if (position < bufferStart || position >= bufferEndOverlap)
	{
		int bytesRead;

		if (position < lastReadPos
			 && position >= bufferEndOverlap
			 && position >= bufferStart)
		{
			const int bytesToKeep = (int) (lastReadPos - position);
			memmove (buffer, buffer + (int) (position - bufferStart), bytesToKeep);

			bufferStart = position;

			bytesRead = source->read (buffer + bytesToKeep,
									  bufferSize - bytesToKeep);

			lastReadPos += bytesRead;
			bytesRead += bytesToKeep;
		}
		else
		{
			bufferStart = position;
			source->setPosition (bufferStart);
			bytesRead = source->read (buffer, bufferSize);
			lastReadPos = bufferStart + bytesRead;
		}

		while (bytesRead < bufferSize)
			buffer [bytesRead++] = 0;
	}
}

int BufferedInputStream::read (void* destBuffer, int maxBytesToRead)
{
	if (position >= bufferStart
		 && position + maxBytesToRead <= lastReadPos)
	{
		memcpy (destBuffer, buffer + (int) (position - bufferStart), maxBytesToRead);
		position += maxBytesToRead;

		return maxBytesToRead;
	}
	else
	{
		if (position < bufferStart || position >= lastReadPos)
			ensureBuffered();

		int bytesRead = 0;

		while (maxBytesToRead > 0)
		{
			const int bytesAvailable = jmin (maxBytesToRead, (int) (lastReadPos - position));

			if (bytesAvailable > 0)
			{
				memcpy (destBuffer, buffer + (int) (position - bufferStart), bytesAvailable);
				maxBytesToRead -= bytesAvailable;
				bytesRead += bytesAvailable;
				position += bytesAvailable;
				destBuffer = static_cast <char*> (destBuffer) + bytesAvailable;
			}

			const int64 oldLastReadPos = lastReadPos;
			ensureBuffered();

			if (oldLastReadPos == lastReadPos)
				break; // if ensureBuffered() failed to read any more data, bail out

			if (isExhausted())
				break;
		}

		return bytesRead;
	}
}

const String BufferedInputStream::readString()
{
	if (position >= bufferStart
		 && position < lastReadPos)
	{
		const int maxChars = (int) (lastReadPos - position);

		const char* const src = buffer + (int) (position - bufferStart);

		for (int i = 0; i < maxChars; ++i)
		{
			if (src[i] == 0)
			{
				position += i + 1;
				return String::fromUTF8 (src, i);
			}
		}
	}

	return InputStream::readString();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_BufferedInputStream.cpp ***/


/*** Start of inlined file: juce_FileInputSource.cpp ***/
BEGIN_JUCE_NAMESPACE

FileInputSource::FileInputSource (const File& file_)
	: file (file_)
{
}

FileInputSource::~FileInputSource()
{
}

InputStream* FileInputSource::createInputStream()
{
	return file.createInputStream();
}

InputStream* FileInputSource::createInputStreamFor (const String& relatedItemPath)
{
	return file.getSiblingFile (relatedItemPath).createInputStream();
}

int64 FileInputSource::hashCode() const
{
	return file.hashCode();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileInputSource.cpp ***/


/*** Start of inlined file: juce_MemoryInputStream.cpp ***/
BEGIN_JUCE_NAMESPACE

MemoryInputStream::MemoryInputStream (const void* const sourceData,
									  const size_t sourceDataSize,
									  const bool keepInternalCopy)
	: data (static_cast <const char*> (sourceData)),
	  dataSize (sourceDataSize),
	  position (0)
{
	if (keepInternalCopy)
	{
		internalCopy.append (data, sourceDataSize);
		data = static_cast <const char*> (internalCopy.getData());
	}
}

MemoryInputStream::MemoryInputStream (const MemoryBlock& sourceData,
									  const bool keepInternalCopy)
	: data (static_cast <const char*> (sourceData.getData())),
	  dataSize (sourceData.getSize()),
	  position (0)
{
	if (keepInternalCopy)
	{
		internalCopy = sourceData;
		data = static_cast <const char*> (internalCopy.getData());
	}
}

MemoryInputStream::~MemoryInputStream()
{
}

int64 MemoryInputStream::getTotalLength()
{
	return dataSize;
}

int MemoryInputStream::read (void* const buffer, const int howMany)
{
	jassert (howMany >= 0);
	const int num = jmin (howMany, (int) (dataSize - position));
	memcpy (buffer, data + position, num);
	position += num;
	return (int) num;
}

bool MemoryInputStream::isExhausted()
{
	return (position >= dataSize);
}

bool MemoryInputStream::setPosition (const int64 pos)
{
	position = (int) jlimit ((int64) 0, (int64) dataSize, pos);
	return true;
}

int64 MemoryInputStream::getPosition()
{
	return position;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MemoryInputStream.cpp ***/


/*** Start of inlined file: juce_MemoryOutputStream.cpp ***/
BEGIN_JUCE_NAMESPACE

MemoryOutputStream::MemoryOutputStream (const size_t initialSize,
										const size_t blockSizeToIncreaseBy,
										MemoryBlock* const memoryBlockToWriteTo)
  : data (memoryBlockToWriteTo),
	position (0),
	size (0),
	blockSize (jmax ((size_t) 16, blockSizeToIncreaseBy))
{
	if (data == 0)
		dataToDelete = data = new MemoryBlock (initialSize);
	else
		data->setSize (initialSize, false);
}

MemoryOutputStream::~MemoryOutputStream()
{
	flush();
}

void MemoryOutputStream::flush()
{
	if (dataToDelete == 0)
		data->setSize (size, false);
}

void MemoryOutputStream::reset() throw()
{
	position = 0;
	size = 0;
}

bool MemoryOutputStream::write (const void* const buffer, int howMany)
{
	if (howMany > 0)
	{
		size_t storageNeeded = position + howMany;

		if (storageNeeded >= data->getSize())
		{
			// if we need more space, increase the block by at least 10%..
			storageNeeded += jmax (blockSize, storageNeeded / 10);
			storageNeeded = storageNeeded - (storageNeeded % blockSize) + blockSize;

			data->ensureSize (storageNeeded);
		}

		data->copyFrom (buffer, (int) position, howMany);
		position += howMany;
		size = jmax (size, position);
	}

	return true;
}

const char* MemoryOutputStream::getData() const throw()
{
	char* const d = static_cast <char*> (data->getData());

	if (data->getSize() > size)
		d [size] = 0;

	return d;
}

bool MemoryOutputStream::setPosition (int64 newPosition)
{
	if (newPosition <= (int64) size)
	{
		// ok to seek backwards
		position = jlimit ((size_t) 0, size, (size_t) newPosition);
		return true;
	}
	else
	{
		// trying to make it bigger isn't a good thing to do..
		return false;
	}
}

const String MemoryOutputStream::toUTF8() const
{
	return String (getData(), getDataSize());
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MemoryOutputStream.cpp ***/


/*** Start of inlined file: juce_SubregionStream.cpp ***/
BEGIN_JUCE_NAMESPACE

SubregionStream::SubregionStream (InputStream* const sourceStream,
								  const int64 startPositionInSourceStream_,
								  const int64 lengthOfSourceStream_,
								  const bool deleteSourceWhenDestroyed) throw()
  : source (sourceStream),
	startPositionInSourceStream (startPositionInSourceStream_),
	lengthOfSourceStream (lengthOfSourceStream_)
{
	if (deleteSourceWhenDestroyed)
		sourceToDelete = source;

	setPosition (0);
}

SubregionStream::~SubregionStream() throw()
{
}

int64 SubregionStream::getTotalLength()
{
	const int64 srcLen = source->getTotalLength() - startPositionInSourceStream;

	return (lengthOfSourceStream >= 0) ? jmin (lengthOfSourceStream, srcLen)
									   : srcLen;
}

int64 SubregionStream::getPosition()
{
	return source->getPosition() - startPositionInSourceStream;
}

bool SubregionStream::setPosition (int64 newPosition)
{
	return source->setPosition (jmax ((int64) 0, newPosition + startPositionInSourceStream));
}

int SubregionStream::read (void* destBuffer, int maxBytesToRead)
{
	if (lengthOfSourceStream < 0)
	{
		return source->read (destBuffer, maxBytesToRead);
	}
	else
	{
		maxBytesToRead = (int) jmin ((int64) maxBytesToRead, lengthOfSourceStream - getPosition());

		if (maxBytesToRead <= 0)
			return 0;

		return source->read (destBuffer, maxBytesToRead);
	}
}

bool SubregionStream::isExhausted()
{
	if (lengthOfSourceStream >= 0)
		return (getPosition() >= lengthOfSourceStream) || source->isExhausted();
	else
		return source->isExhausted();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_SubregionStream.cpp ***/


/*** Start of inlined file: juce_PerformanceCounter.cpp ***/
BEGIN_JUCE_NAMESPACE

PerformanceCounter::PerformanceCounter (const String& name_,
										int runsPerPrintout,
										const File& loggingFile)
	: name (name_),
	  numRuns (0),
	  runsPerPrint (runsPerPrintout),
	  totalTime (0),
	  outputFile (loggingFile)
{
	if (outputFile != File::nonexistent)
	{
		String s ("**** Counter for \"");
		s << name_ << "\" started at: "
		  << Time::getCurrentTime().toString (true, true)
		  << "\r\n";

		outputFile.appendText (s, false, false);
	}
}

PerformanceCounter::~PerformanceCounter()
{
	printStatistics();
}

void PerformanceCounter::start()
{
	started = Time::getHighResolutionTicks();
}

void PerformanceCounter::stop()
{
	const int64 now = Time::getHighResolutionTicks();

	totalTime += 1000.0 * Time::highResolutionTicksToSeconds (now - started);

	if (++numRuns == runsPerPrint)
		printStatistics();
}

void PerformanceCounter::printStatistics()
{
	if (numRuns > 0)
	{
		String s ("Performance count for \"");
		s << name << "\" - average over " << numRuns << " run(s) = ";

		const int micros = (int) (totalTime * (1000.0 / numRuns));

		if (micros > 10000)
			s << (micros/1000) << " millisecs";
		else
			s << micros << " microsecs";

		s << ", total = " << String (totalTime / 1000, 5) << " seconds";

		Logger::outputDebugString (s);

		s << "\r\n";

		if (outputFile != File::nonexistent)
			outputFile.appendText (s, false, false);

		numRuns = 0;
		totalTime = 0;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PerformanceCounter.cpp ***/


/*** Start of inlined file: juce_Uuid.cpp ***/
BEGIN_JUCE_NAMESPACE

Uuid::Uuid()
{
	// Mix up any available MAC addresses with some time-based pseudo-random numbers
	// to make it very very unlikely that two UUIDs will ever be the same..

	static int64 macAddresses[2];
	static bool hasCheckedMacAddresses = false;

	if (! hasCheckedMacAddresses)
	{
		hasCheckedMacAddresses = true;
		SystemStats::getMACAddresses (macAddresses, 2);
	}

	value.asInt64[0] = macAddresses[0];
	value.asInt64[1] = macAddresses[1];

	// We'll use both a local RNG that is re-seeded, plus the shared RNG,
	// whose seed will carry over between calls to this method.

	Random r (macAddresses[0] ^ macAddresses[1]
				^ Random::getSystemRandom().nextInt64());

	for (int i = 4; --i >= 0;)
	{
		r.setSeedRandomly(); // calling this repeatedly improves randomness
		value.asInt[i] ^= r.nextInt();
		value.asInt[i] ^= Random::getSystemRandom().nextInt();
	}
}

Uuid::~Uuid() throw()
{
}

Uuid::Uuid (const Uuid& other)
	: value (other.value)
{
}

Uuid& Uuid::operator= (const Uuid& other)
{
	value = other.value;
	return *this;
}

bool Uuid::operator== (const Uuid& other) const
{
	return value.asInt64[0] == other.value.asInt64[0]
		&& value.asInt64[1] == other.value.asInt64[1];
}

bool Uuid::operator!= (const Uuid& other) const
{
	return ! operator== (other);
}

bool Uuid::isNull() const throw()
{
	return (value.asInt64 [0] == 0) && (value.asInt64 [1] == 0);
}

const String Uuid::toString() const
{
	return String::toHexString (value.asBytes, sizeof (value.asBytes), 0);
}

Uuid::Uuid (const String& uuidString)
{
	operator= (uuidString);
}

Uuid& Uuid::operator= (const String& uuidString)
{
	MemoryBlock mb;
	mb.loadFromHexString (uuidString);
	mb.ensureSize (sizeof (value.asBytes), true);
	mb.copyTo (value.asBytes, 0, sizeof (value.asBytes));
	return *this;
}

Uuid::Uuid (const uint8* const rawData)
{
	operator= (rawData);
}

Uuid& Uuid::operator= (const uint8* const rawData)
{
	if (rawData != 0)
		memcpy (value.asBytes, rawData, sizeof (value.asBytes));
	else
		zeromem (value.asBytes, sizeof (value.asBytes));

	return *this;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Uuid.cpp ***/


/*** Start of inlined file: juce_ZipFile.cpp ***/
BEGIN_JUCE_NAMESPACE

class ZipFile::ZipEntryInfo
{
public:
	ZipFile::ZipEntry entry;
	int streamOffset;
	int compressedSize;
	bool compressed;
};

class ZipFile::ZipInputStream  : public InputStream
{
public:

	ZipInputStream (ZipFile& file_, ZipFile::ZipEntryInfo& zei)
		: file (file_),
		  zipEntryInfo (zei),
		  pos (0),
		  headerSize (0),
		  inputStream (0)
	{
		inputStream = file_.inputStream;

		if (file_.inputSource != 0)
		{
			inputStream = file.inputSource->createInputStream();
		}
		else
		{
#ifdef JUCE_DEBUG
			file_.numOpenStreams++;
#endif
		}

		char buffer [30];

		if (inputStream != 0
			 && inputStream->setPosition (zei.streamOffset)
			 && inputStream->read (buffer, 30) == 30
			 && ByteOrder::littleEndianInt (buffer) == 0x04034b50)
		{
			headerSize = 30 + ByteOrder::littleEndianShort (buffer + 26)
							+ ByteOrder::littleEndianShort (buffer + 28);
		}
	}

	~ZipInputStream() throw()
	{
#ifdef JUCE_DEBUG
		if (inputStream != 0 && inputStream == file.inputStream)
			file.numOpenStreams--;
#endif

		if (inputStream != file.inputStream)
			delete inputStream;
	}

	int64 getTotalLength() throw()
	{
		return zipEntryInfo.compressedSize;
	}

	int read (void* buffer, int howMany) throw()
	{
		if (headerSize <= 0)
			return 0;

		howMany = (int) jmin ((int64) howMany, zipEntryInfo.compressedSize - pos);

		if (inputStream == 0)
			return 0;

		int num;

		if (inputStream == file.inputStream)
		{
			const ScopedLock sl (file.lock);
			inputStream->setPosition (pos + zipEntryInfo.streamOffset + headerSize);
			num = inputStream->read (buffer, howMany);
		}
		else
		{
			inputStream->setPosition (pos + zipEntryInfo.streamOffset + headerSize);
			num = inputStream->read (buffer, howMany);
		}

		pos += num;
		return num;
	}

	bool isExhausted() throw()
	{
		return headerSize <= 0 || pos >= zipEntryInfo.compressedSize;
	}

	int64 getPosition() throw()
	{
		return pos;
	}

	bool setPosition (int64 newPos) throw()
	{
		pos = jlimit ((int64) 0, (int64) zipEntryInfo.compressedSize, newPos);
		return true;
	}

private:

	ZipFile& file;
	ZipEntryInfo zipEntryInfo;
	int64 pos;
	int headerSize;
	InputStream* inputStream;

	ZipInputStream (const ZipInputStream&);
	ZipInputStream& operator= (const ZipInputStream&);
};

ZipFile::ZipFile (InputStream* const source_,
				  const bool deleteStreamWhenDestroyed) throw()
   : inputStream (source_)
#ifdef JUCE_DEBUG
	 , numOpenStreams (0)
#endif
{
	if (deleteStreamWhenDestroyed)
		streamToDelete = inputStream;

	init();
}

ZipFile::ZipFile (const File& file)
	: inputStream (0)
#ifdef JUCE_DEBUG
	  , numOpenStreams (0)
#endif
{
	inputSource = new FileInputSource (file);
	init();
}

ZipFile::ZipFile (InputSource* const inputSource_)
	: inputStream (0),
	  inputSource (inputSource_)
#ifdef JUCE_DEBUG
	  , numOpenStreams (0)
#endif
{
	init();
}

ZipFile::~ZipFile() throw()
{
#ifdef JUCE_DEBUG
	entries.clear();

	// If you hit this assertion, it means you've created a stream to read
	// one of the items in the zipfile, but you've forgotten to delete that
	// stream object before deleting the file.. Streams can't be kept open
	// after the file is deleted because they need to share the input
	// stream that the file uses to read itself.
	jassert (numOpenStreams == 0);
#endif
}

int ZipFile::getNumEntries() const throw()
{
	return entries.size();
}

const ZipFile::ZipEntry* ZipFile::getEntry (const int index) const throw()
{
	ZipEntryInfo* const zei = entries [index];

	return zei != 0 ? &(zei->entry) : 0;
}

int ZipFile::getIndexOfFileName (const String& fileName) const throw()
{
	for (int i = 0; i < entries.size(); ++i)
		if (entries.getUnchecked (i)->entry.filename == fileName)
			return i;

	return -1;
}

const ZipFile::ZipEntry* ZipFile::getEntry (const String& fileName) const throw()
{
	return getEntry (getIndexOfFileName (fileName));
}

InputStream* ZipFile::createStreamForEntry (const int index)
{
	ZipEntryInfo* const zei = entries[index];
	InputStream* stream = 0;

	if (zei != 0)
	{
		stream = new ZipInputStream (*this, *zei);

		if (zei->compressed)
		{
			stream = new GZIPDecompressorInputStream (stream, true, true,
													  zei->entry.uncompressedSize);

			// (much faster to unzip in big blocks using a buffer..)
			stream = new BufferedInputStream (stream, 32768, true);
		}
	}

	return stream;
}

class ZipFile::ZipFilenameComparator
{
public:
	int compareElements (const ZipFile::ZipEntryInfo* first, const ZipFile::ZipEntryInfo* second)
	{
		return first->entry.filename.compare (second->entry.filename);
	}
};

void ZipFile::sortEntriesByFilename()
{
	ZipFilenameComparator sorter;
	entries.sort (sorter);
}

void ZipFile::init()
{
	ScopedPointer <InputStream> toDelete;
	InputStream* in = inputStream;

	if (inputSource != 0)
	{
		in = inputSource->createInputStream();
		toDelete = in;
	}

	if (in != 0)
	{
		int numEntries = 0;
		int pos = findEndOfZipEntryTable (in, numEntries);

		if (pos >= 0 && pos < in->getTotalLength())
		{
			const int size = (int) (in->getTotalLength() - pos);

			in->setPosition (pos);
			MemoryBlock headerData;

			if (in->readIntoMemoryBlock (headerData, size) == size)
			{
				pos = 0;

				for (int i = 0; i < numEntries; ++i)
				{
					if (pos + 46 > size)
						break;

					const char* const buffer = static_cast <const char*> (headerData.getData()) + pos;

					const int fileNameLen = ByteOrder::littleEndianShort (buffer + 28);

					if (pos + 46 + fileNameLen > size)
						break;

					ZipEntryInfo* const zei = new ZipEntryInfo();
					zei->entry.filename = String::fromUTF8 (buffer + 46, fileNameLen);

					const int time = ByteOrder::littleEndianShort (buffer + 12);
					const int date = ByteOrder::littleEndianShort (buffer + 14);

					const int year	  = 1980 + (date >> 9);
					const int month	 = ((date >> 5) & 15) - 1;
					const int day	   = date & 31;
					const int hours	 = time >> 11;
					const int minutes   = (time >> 5) & 63;
					const int seconds   = (time & 31) << 1;

					zei->entry.fileTime = Time (year, month, day, hours, minutes, seconds);

					zei->compressed = ByteOrder::littleEndianShort (buffer + 10) != 0;
					zei->compressedSize = ByteOrder::littleEndianInt (buffer + 20);
					zei->entry.uncompressedSize = ByteOrder::littleEndianInt (buffer + 24);

					zei->streamOffset = ByteOrder::littleEndianInt (buffer + 42);
					entries.add (zei);

					pos += 46 + fileNameLen
							+ ByteOrder::littleEndianShort (buffer + 30)
							+ ByteOrder::littleEndianShort (buffer + 32);
				}
			}
		}
	}
}

int ZipFile::findEndOfZipEntryTable (InputStream* input, int& numEntries)
{
	BufferedInputStream in (input, 8192, false);

	in.setPosition (in.getTotalLength());
	int64 pos = in.getPosition();
	const int64 lowestPos = jmax ((int64) 0, pos - 1024);

	char buffer [32];
	zeromem (buffer, sizeof (buffer));

	while (pos > lowestPos)
	{
		in.setPosition (pos - 22);
		pos = in.getPosition();
		memcpy (buffer + 22, buffer, 4);

		if (in.read (buffer, 22) != 22)
			return 0;

		for (int i = 0; i < 22; ++i)
		{
			if (ByteOrder::littleEndianInt (buffer + i) == 0x06054b50)
			{
				in.setPosition (pos + i);
				in.read (buffer, 22);
				numEntries = ByteOrder::littleEndianShort (buffer + 10);

				return ByteOrder::littleEndianInt (buffer + 16);
			}
		}
	}

	return 0;
}

void ZipFile::uncompressTo (const File& targetDirectory,
							const bool shouldOverwriteFiles)
{
	for (int i = 0; i < entries.size(); ++i)
	{
		const ZipEntry& zei = entries.getUnchecked(i)->entry;

		const File targetFile (targetDirectory.getChildFile (zei.filename));

		if (zei.filename.endsWithChar ('/'))
		{
			targetFile.createDirectory(); // (entry is a directory, not a file)
		}
		else
		{
			ScopedPointer <InputStream> in (createStreamForEntry (i));

			if (in != 0)
			{
				if (shouldOverwriteFiles)
					targetFile.deleteFile();

				if ((! targetFile.exists())
					 && targetFile.getParentDirectory().createDirectory())
				{
					ScopedPointer <FileOutputStream> out (targetFile.createOutputStream());

					if (out != 0)
					{
						out->writeFromInputStream (*in, -1);
						out = 0;

						targetFile.setCreationTime (zei.fileTime);
						targetFile.setLastModificationTime (zei.fileTime);
						targetFile.setLastAccessTime (zei.fileTime);
					}
				}
			}
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ZipFile.cpp ***/


/*** Start of inlined file: juce_CharacterFunctions.cpp ***/
#ifdef _MSC_VER
  #pragma warning (disable: 4514 4996)
  #pragma warning (push)
#endif

#include <cwctype>
#include <cctype>
#include <ctime>

#ifdef _MSC_VER
  #pragma warning (pop)
#endif

BEGIN_JUCE_NAMESPACE

int CharacterFunctions::length (const char* const s) throw()
{
	return (int) strlen (s);
}

int CharacterFunctions::length (const juce_wchar* const s) throw()
{
	return (int) wcslen (s);
}

void CharacterFunctions::copy (char* dest, const char* src, const int maxChars) throw()
{
	strncpy (dest, src, maxChars);
}

void CharacterFunctions::copy (juce_wchar* dest, const juce_wchar* src, int maxChars) throw()
{
	wcsncpy (dest, src, maxChars);
}

void CharacterFunctions::copy (juce_wchar* dest, const char* src, const int maxChars) throw()
{
	mbstowcs (dest, src, maxChars);
}

void CharacterFunctions::copy (char* dest, const juce_wchar* src, const int maxChars) throw()
{
	wcstombs (dest, src, maxChars);
}

int CharacterFunctions::bytesRequiredForCopy (const juce_wchar* src) throw()
{
	return (int) wcstombs (0, src, 0);
}

void CharacterFunctions::append (char* dest, const char* src) throw()
{
	strcat (dest, src);
}

void CharacterFunctions::append (juce_wchar* dest, const juce_wchar* src) throw()
{
	wcscat (dest, src);
}

int CharacterFunctions::compare (const char* const s1, const char* const s2) throw()
{
	return strcmp (s1, s2);
}

int CharacterFunctions::compare (const juce_wchar* s1, const juce_wchar* s2) throw()
{
	jassert (s1 != 0 && s2 != 0);
	return wcscmp (s1, s2);
}

int CharacterFunctions::compare (const char* const s1, const char* const s2, const int maxChars) throw()
{
	jassert (s1 != 0 && s2 != 0);
	return strncmp (s1, s2, maxChars);
}

int CharacterFunctions::compare (const juce_wchar* s1, const juce_wchar* s2, int maxChars) throw()
{
	jassert (s1 != 0 && s2 != 0);
	return wcsncmp (s1, s2, maxChars);
}

int CharacterFunctions::compare (const juce_wchar* s1, const char* s2) throw()
{
	jassert (s1 != 0 && s2 != 0);

	for (;;)
	{
		const int diff = (int) (*s1 - (juce_wchar) (unsigned char) *s2);

		if (diff != 0)
			return diff;
		else if (*s1 == 0)
			break;

		++s1;
		++s2;
	}

	return 0;
}

int CharacterFunctions::compare (const char* s1, const juce_wchar* s2) throw()
{
	return -compare (s2, s1);
}

int CharacterFunctions::compareIgnoreCase (const char* const s1, const char* const s2) throw()
{
	jassert (s1 != 0 && s2 != 0);

#if JUCE_WIN32
	return stricmp (s1, s2);
#else
	return strcasecmp (s1, s2);
#endif
}

int CharacterFunctions::compareIgnoreCase (const juce_wchar* s1, const juce_wchar* s2) throw()
{
	jassert (s1 != 0 && s2 != 0);

#if JUCE_WIN32
	return _wcsicmp (s1, s2);
#else
	for (;;)
	{
		if (*s1 != *s2)
		{
			const int diff = toUpperCase (*s1) - toUpperCase (*s2);

			if (diff != 0)
				return diff < 0 ? -1 : 1;
		}
		else if (*s1 == 0)
			break;

		++s1;
		++s2;
	}

	return 0;
#endif
}

int CharacterFunctions::compareIgnoreCase (const juce_wchar* s1, const char* s2) throw()
{
	jassert (s1 != 0 && s2 != 0);

	for (;;)
	{
		if (*s1 != *s2)
		{
			const int diff = toUpperCase (*s1) - toUpperCase (*s2);

			if (diff != 0)
				return diff < 0 ? -1 : 1;
		}
		else if (*s1 == 0)
			break;

		++s1;
		++s2;
	}

	return 0;
}

int CharacterFunctions::compareIgnoreCase (const char* const s1, const char* const s2, const int maxChars) throw()
{
	jassert (s1 != 0 && s2 != 0);

#if JUCE_WIN32
	return strnicmp (s1, s2, maxChars);
#else
	return strncasecmp (s1, s2, maxChars);
#endif
}

int CharacterFunctions::compareIgnoreCase (const juce_wchar* s1, const juce_wchar* s2, int maxChars) throw()
{
	jassert (s1 != 0 && s2 != 0);

#if JUCE_WIN32
	return _wcsnicmp (s1, s2, maxChars);
#else
	while (--maxChars >= 0)
	{
		if (*s1 != *s2)
		{
			const int diff = toUpperCase (*s1) - toUpperCase (*s2);

			if (diff != 0)
				return diff < 0 ? -1 : 1;
		}
		else if (*s1 == 0)
			break;

		++s1;
		++s2;
	}

	return 0;
#endif
}

const char* CharacterFunctions::find (const char* const haystack, const char* const needle) throw()
{
	return strstr (haystack, needle);
}

const juce_wchar* CharacterFunctions::find (const juce_wchar* haystack, const juce_wchar* const needle) throw()
{
	return wcsstr (haystack, needle);
}

int CharacterFunctions::indexOfChar (const char* const haystack, const char needle, const bool ignoreCase) throw()
{
	if (haystack != 0)
	{
		int i = 0;

		if (ignoreCase)
		{
			const char n1 = toLowerCase (needle);
			const char n2 = toUpperCase (needle);

			if (n1 != n2) // if the char is the same in upper/lower case, fall through to the normal search
			{
				while (haystack[i] != 0)
				{
					if (haystack[i] == n1 || haystack[i] == n2)
						return i;

					++i;
				}

				return -1;
			}

			jassert (n1 == needle);
		}

		while (haystack[i] != 0)
		{
			if (haystack[i] == needle)
				return i;

			++i;
		}
	}

	return -1;
}

int CharacterFunctions::indexOfChar (const juce_wchar* const haystack, const juce_wchar needle, const bool ignoreCase) throw()
{
	if (haystack != 0)
	{
		int i = 0;

		if (ignoreCase)
		{
			const juce_wchar n1 = toLowerCase (needle);
			const juce_wchar n2 = toUpperCase (needle);

			if (n1 != n2) // if the char is the same in upper/lower case, fall through to the normal search
			{
				while (haystack[i] != 0)
				{
					if (haystack[i] == n1 || haystack[i] == n2)
						return i;

					++i;
				}

				return -1;
			}

			jassert (n1 == needle);
		}

		while (haystack[i] != 0)
		{
			if (haystack[i] == needle)
				return i;

			++i;
		}
	}

	return -1;
}

int CharacterFunctions::indexOfCharFast (const char* const haystack, const char needle) throw()
{
	jassert (haystack != 0);

	int i = 0;
	while (haystack[i] != 0)
	{
		if (haystack[i] == needle)
			return i;

		++i;
	}

	return -1;
}

int CharacterFunctions::indexOfCharFast (const juce_wchar* const haystack, const juce_wchar needle) throw()
{
	jassert (haystack != 0);

	int i = 0;
	while (haystack[i] != 0)
	{
		if (haystack[i] == needle)
			return i;

		++i;
	}

	return -1;
}

int CharacterFunctions::getIntialSectionContainingOnly (const char* const text, const char* const allowedChars) throw()
{
	return allowedChars == 0 ? 0 : (int) strspn (text, allowedChars);
}

int CharacterFunctions::getIntialSectionContainingOnly (const juce_wchar* const text, const juce_wchar* const allowedChars) throw()
{
	if (allowedChars == 0)
		return 0;

	int i = 0;

	for (;;)
	{
		if (indexOfCharFast (allowedChars, text[i]) < 0)
			break;

		++i;
	}

	return i;
}

int CharacterFunctions::ftime (char* const dest, const int maxChars, const char* const format, const struct tm* const tm) throw()
{
	return (int) strftime (dest, maxChars, format, tm);
}

int CharacterFunctions::ftime (juce_wchar* const dest, const int maxChars, const juce_wchar* const format, const struct tm* const tm) throw()
{
	return (int) wcsftime (dest, maxChars, format, tm);
}

int CharacterFunctions::getIntValue (const char* const s) throw()
{
	return atoi (s);
}

int CharacterFunctions::getIntValue (const juce_wchar* s) throw()
{
#if JUCE_WIN32
	return _wtoi (s);
#else
	int v = 0;

	while (isWhitespace (*s))
		++s;

	const bool isNeg = *s == '-';
	if (isNeg)
		++s;

	for (;;)
	{
		const wchar_t c = *s++;

		if (c >= '0' && c <= '9')
			v = v * 10 + (int) (c - '0');
		else
			break;
	}

	return isNeg ? -v : v;
#endif
}

int64 CharacterFunctions::getInt64Value (const char* s) throw()
{
#if JUCE_LINUX
	return atoll (s);
#elif defined (JUCE_WIN32)
	return _atoi64 (s);
#else
	int64 v = 0;

	while (isWhitespace (*s))
		++s;

	const bool isNeg = *s == '-';
	if (isNeg)
		++s;

	for (;;)
	{
		const char c = *s++;

		if (c >= '0' && c <= '9')
			v = v * 10 + (int64) (c - '0');
		else
			break;
	}

	return isNeg ? -v : v;
#endif
}

int64 CharacterFunctions::getInt64Value (const juce_wchar* s) throw()
{
#if JUCE_WIN32
	return _wtoi64 (s);
#else
	int64 v = 0;

	while (isWhitespace (*s))
		++s;

	const bool isNeg = *s == '-';
	if (isNeg)
		++s;

	for (;;)
	{
		const juce_wchar c = *s++;

		if (c >= '0' && c <= '9')
			v = v * 10 + (int64) (c - '0');
		else
			break;
	}

	return isNeg ? -v : v;
#endif
}

static double juce_mulexp10 (const double value, int exponent) throw()
{
	if (exponent == 0)
		return value;

	if (value == 0)
		return 0;

	const bool negative = (exponent < 0);
	if (negative)
		exponent = -exponent;

	double result = 1.0, power = 10.0;
	for (int bit = 1; exponent != 0; bit <<= 1)
	{
		if ((exponent & bit) != 0)
		{
			exponent ^= bit;
			result *= power;
			if (exponent == 0)
				break;
		}
		power *= power;
	}

	return negative ? (value / result) : (value * result);
}

template <class CharType>
double juce_atof (const CharType* const original) throw()
{
	double result[3] = { 0, 0, 0 }, accumulator[2] = { 0, 0 };
	int exponentAdjustment[2] = { 0, 0 }, exponentAccumulator[2] = { -1, -1 };
	int exponent = 0, decPointIndex = 0, digit = 0;
	int lastDigit = 0, numSignificantDigits = 0;
	bool isNegative = false, digitsFound = false;
	const int maxSignificantDigits = 15 + 2;

	const CharType* s = original;
	while (CharacterFunctions::isWhitespace (*s))
		++s;

	switch (*s)
	{
		case '-':   isNegative = true; // fall-through..
		case '+':   ++s;
	}

	if (*s == 'n' || *s == 'N' || *s == 'i' || *s == 'I')
		return atof (String (original).toUTF8()); // Let the c library deal with NAN and INF

	for (;;)
	{
		if (CharacterFunctions::isDigit (*s))
		{
			lastDigit = digit;
			digit = *s++ - '0';
			digitsFound = true;

			if (decPointIndex != 0)
				exponentAdjustment[1]++;

			if (numSignificantDigits == 0 && digit == 0)
				continue;

			if (++numSignificantDigits > maxSignificantDigits)
			{
				if (digit > 5)
					++accumulator [decPointIndex];
				else if (digit == 5 && (lastDigit & 1) != 0)
					++accumulator [decPointIndex];

				if (decPointIndex > 0)
					exponentAdjustment[1]--;
				else
					exponentAdjustment[0]++;

				while (CharacterFunctions::isDigit (*s))
				{
					++s;
					if (decPointIndex == 0)
						exponentAdjustment[0]++;
				}
			}
			else
			{
				const double maxAccumulatorValue = (double) ((std::numeric_limits<unsigned int>::max() - 9) / 10);
				if (accumulator [decPointIndex] > maxAccumulatorValue)
				{
					result [decPointIndex] = juce_mulexp10 (result [decPointIndex], exponentAccumulator [decPointIndex])
												+ accumulator [decPointIndex];
					accumulator [decPointIndex] = 0;
					exponentAccumulator [decPointIndex] = 0;
				}

				accumulator [decPointIndex] = accumulator[decPointIndex] * 10 + digit;
				exponentAccumulator [decPointIndex]++;
			}
		}
		else if (decPointIndex == 0 && *s == '.')
		{
			++s;
			decPointIndex = 1;

			if (numSignificantDigits > maxSignificantDigits)
			{
				while (CharacterFunctions::isDigit (*s))
					++s;
				break;
			}
		}
		else
		{
			break;
		}
	}

	result[0] = juce_mulexp10 (result[0], exponentAccumulator[0]) + accumulator[0];

	if (decPointIndex != 0)
		result[1] = juce_mulexp10 (result[1], exponentAccumulator[1]) + accumulator[1];

	if ((*s == 'e' || *s == 'E') && digitsFound)
	{
		bool negativeExponent = false;

		switch (*++s)
		{
			case '-':   negativeExponent = true; // fall-through..
			case '+':   ++s;
		}

		while (CharacterFunctions::isDigit (*s))
			exponent = (exponent * 10) + (*s++ - '0');

		if (negativeExponent)
			exponent = -exponent;
	}

	double r = juce_mulexp10 (result[0], exponent + exponentAdjustment[0]);
	if (decPointIndex != 0)
		r += juce_mulexp10 (result[1], exponent - exponentAdjustment[1]);

	return isNegative ? -r : r;
}

double CharacterFunctions::getDoubleValue (const char* const s) throw()
{
	return juce_atof <char> (s);
}

double CharacterFunctions::getDoubleValue (const juce_wchar* const s) throw()
{
	return juce_atof <juce_wchar> (s);
}

char CharacterFunctions::toUpperCase (const char character) throw()
{
	return (char) toupper (character);
}

juce_wchar CharacterFunctions::toUpperCase (const juce_wchar character) throw()
{
	return towupper (character);
}

void CharacterFunctions::toUpperCase (char* s) throw()
{
#if JUCE_WIN32
	strupr (s);
#else
	while (*s != 0)
	{
		*s = toUpperCase (*s);
		++s;
	}
#endif
}

void CharacterFunctions::toUpperCase (juce_wchar* s) throw()
{
#if JUCE_WIN32
	_wcsupr (s);
#else
	while (*s != 0)
	{
		*s = toUpperCase (*s);
		++s;
	}
#endif
}

bool CharacterFunctions::isUpperCase (const char character) throw()
{
	return isupper (character) != 0;
}

bool CharacterFunctions::isUpperCase (const juce_wchar character) throw()
{
#if JUCE_WIN32
	return iswupper (character) != 0;
#else
	return toLowerCase (character) != character;
#endif
}

char CharacterFunctions::toLowerCase (const char character) throw()
{
	return (char) tolower (character);
}

juce_wchar CharacterFunctions::toLowerCase (const juce_wchar character) throw()
{
	return towlower (character);
}

void CharacterFunctions::toLowerCase (char* s) throw()
{
#if JUCE_WIN32
	strlwr (s);
#else
	while (*s != 0)
	{
		*s = toLowerCase (*s);
		++s;
	}
#endif
}

void CharacterFunctions::toLowerCase (juce_wchar* s) throw()
{
#if JUCE_WIN32
	_wcslwr (s);
#else
	while (*s != 0)
	{
		*s = toLowerCase (*s);
		++s;
	}
#endif
}

bool CharacterFunctions::isLowerCase (const char character) throw()
{
	return islower (character) != 0;
}

bool CharacterFunctions::isLowerCase (const juce_wchar character) throw()
{
#if JUCE_WIN32
	return iswlower (character) != 0;
#else
	return toUpperCase (character) != character;
#endif
}

bool CharacterFunctions::isWhitespace (const char character) throw()
{
	return character == ' ' || (character <= 13 && character >= 9);
}

bool CharacterFunctions::isWhitespace (const juce_wchar character) throw()
{
	return iswspace (character) != 0;
}

bool CharacterFunctions::isDigit (const char character) throw()
{
	return (character >= '0' && character <= '9');
}

bool CharacterFunctions::isDigit (const juce_wchar character) throw()
{
	return iswdigit (character) != 0;
}

bool CharacterFunctions::isLetter (const char character) throw()
{
	return (character >= 'a' && character <= 'z')
		|| (character >= 'A' && character <= 'Z');
}

bool CharacterFunctions::isLetter (const juce_wchar character) throw()
{
	return iswalpha (character) != 0;
}

bool CharacterFunctions::isLetterOrDigit (const char character) throw()
{
	return (character >= 'a' && character <= 'z')
		|| (character >= 'A' && character <= 'Z')
		|| (character >= '0' && character <= '9');
}

bool CharacterFunctions::isLetterOrDigit (const juce_wchar character) throw()
{
	return iswalnum (character) != 0;
}

int CharacterFunctions::getHexDigitValue (const juce_wchar digit) throw()
{
	if (digit >= '0' && digit <= '9')
		return digit - '0';
	else if (digit >= 'a' && digit <= 'f')
		return digit - ('a' - 10);
	else if (digit >= 'A' && digit <= 'F')
		return digit - ('A' - 10);

	return -1;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_CharacterFunctions.cpp ***/


/*** Start of inlined file: juce_LocalisedStrings.cpp ***/
BEGIN_JUCE_NAMESPACE

LocalisedStrings::LocalisedStrings (const String& fileContents)
{
	loadFromText (fileContents);
}

LocalisedStrings::LocalisedStrings (const File& fileToLoad)
{
	loadFromText (fileToLoad.loadFileAsString());
}

LocalisedStrings::~LocalisedStrings()
{
}

const String LocalisedStrings::translate (const String& text) const
{
	return translations.getValue (text, text);
}

static int findCloseQuote (const String& text, int startPos)
{
	juce_wchar lastChar = 0;

	for (;;)
	{
		const juce_wchar c = text [startPos];

		if (c == 0 || (c == '"' && lastChar != '\\'))
			break;

		lastChar = c;
		++startPos;
	}

	return startPos;
}

static const String unescapeString (const String& s)
{
	return s.replace ("\\\"", "\"")
			.replace ("\\\'", "\'")
			.replace ("\\t", "\t")
			.replace ("\\r", "\r")
			.replace ("\\n", "\n");
}

void LocalisedStrings::loadFromText (const String& fileContents)
{
	StringArray lines;
	lines.addLines (fileContents);

	for (int i = 0; i < lines.size(); ++i)
	{
		String line (lines[i].trim());

		if (line.startsWithChar ('"'))
		{
			int closeQuote = findCloseQuote (line, 1);

			const String originalText (unescapeString (line.substring (1, closeQuote)));

			if (originalText.isNotEmpty())
			{
				const int openingQuote = findCloseQuote (line, closeQuote + 1);
				closeQuote = findCloseQuote (line, openingQuote + 1);

				const String newText (unescapeString (line.substring (openingQuote + 1, closeQuote)));

				if (newText.isNotEmpty())
					translations.set (originalText, newText);
			}
		}
		else if (line.startsWithIgnoreCase ("language:"))
		{
			languageName = line.substring (9).trim();
		}
		else if (line.startsWithIgnoreCase ("countries:"))
		{
			countryCodes.addTokens (line.substring (10).trim(), true);
			countryCodes.trim();
			countryCodes.removeEmptyStrings();
		}
	}
}

void LocalisedStrings::setIgnoresCase (const bool shouldIgnoreCase)
{
	translations.setIgnoresCase (shouldIgnoreCase);
}

static CriticalSection currentMappingsLock;
static LocalisedStrings* currentMappings = 0;

void LocalisedStrings::setCurrentMappings (LocalisedStrings* newTranslations)
{
	const ScopedLock sl (currentMappingsLock);

	delete currentMappings;
	currentMappings = newTranslations;
}

LocalisedStrings* LocalisedStrings::getCurrentMappings()
{
	return currentMappings;
}

const String LocalisedStrings::translateWithCurrentMappings (const String& text)
{
	const ScopedLock sl (currentMappingsLock);

	if (currentMappings != 0)
		return currentMappings->translate (text);

	return text;
}

const String LocalisedStrings::translateWithCurrentMappings (const char* text)
{
	return translateWithCurrentMappings (String (text));
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_LocalisedStrings.cpp ***/


/*** Start of inlined file: juce_String.cpp ***/
#ifdef _MSC_VER
  #pragma warning (push)
  #pragma warning (disable: 4514)
#endif
#include <locale>

#if JUCE_MSVC
  #include <float.h>
#endif

BEGIN_JUCE_NAMESPACE

#ifdef _MSC_VER
  #pragma warning (pop)
#endif

#if defined (JUCE_STRINGS_ARE_UNICODE) && ! JUCE_STRINGS_ARE_UNICODE
 #error "JUCE_STRINGS_ARE_UNICODE is deprecated! All strings are now unicode by default."
#endif

class StringHolder
{
public:
	StringHolder()
		: refCount (0x3fffffff), allocatedNumChars (0)
	{
		text[0] = 0;
	}

	static juce_wchar* createUninitialised (const size_t numChars)
	{
		StringHolder* const s = reinterpret_cast <StringHolder*> (new char [sizeof (StringHolder) + numChars * sizeof (juce_wchar)]);
		s->refCount = 0;
		s->allocatedNumChars = numChars;
		return &(s->text[0]);
	}

	static juce_wchar* createCopy (const juce_wchar* const src, const size_t numChars)
	{
		juce_wchar* const dest = createUninitialised (numChars);
		copyChars (dest, src, numChars);
		return dest;
	}

	static juce_wchar* createCopy (const char* const src, const size_t numChars)
	{
		juce_wchar* const dest = createUninitialised (numChars);
		CharacterFunctions::copy (dest, src, (int) numChars);
		dest [numChars] = 0;
		return dest;
	}

	static inline juce_wchar* getEmpty() throw()
	{
		return &(empty.text[0]);
	}

	static void retain (juce_wchar* const text) throw()
	{
		Atomic::increment (bufferFromText (text)->refCount);
	}

	static inline void release (StringHolder* const b) throw()
	{
		if (Atomic::decrementAndReturn (b->refCount) == -1 && b != &empty)
			delete[] reinterpret_cast <char*> (b);
	}

	static void release (juce_wchar* const text) throw()
	{
		release (bufferFromText (text));
	}

	static juce_wchar* makeUnique (juce_wchar* const text)
	{
		StringHolder* const b = bufferFromText (text);

		if (b->refCount <= 0)
			return text;

		juce_wchar* const newText = createCopy (text, b->allocatedNumChars);
		release (b);

		return newText;
	}

	static juce_wchar* makeUniqueWithSize (juce_wchar* const text, size_t numChars)
	{
		StringHolder* const b = bufferFromText (text);

		if (b->refCount <= 0 && b->allocatedNumChars >= numChars)
			return text;

		juce_wchar* const newText = createUninitialised (jmax (b->allocatedNumChars, numChars));
		copyChars (newText, text, b->allocatedNumChars);
		release (b);

		return newText;
	}

	static size_t getAllocatedNumChars (juce_wchar* const text) throw()
	{
		return bufferFromText (text)->allocatedNumChars;
	}

	static void copyChars (juce_wchar* const dest, const juce_wchar* const src, const size_t numChars) throw()
	{
		memcpy (dest, src, numChars * sizeof (juce_wchar));
		dest [numChars] = 0;
	}

	int refCount;
	size_t allocatedNumChars;
	juce_wchar text[1];

	static StringHolder empty;

private:
	static inline StringHolder* bufferFromText (void* const text) throw()
	{
		// (Can't use offsetof() here because of warnings about this not being a POD)
		return reinterpret_cast <StringHolder*> (static_cast <char*> (text)
					- (reinterpret_cast <size_t> (reinterpret_cast <StringHolder*> (1)->text) - 1));
	}
};

StringHolder StringHolder::empty;
const String String::empty;

void String::createInternal (const juce_wchar* const t, const size_t numChars)
{
	jassert (t[numChars] == 0); // must have a null terminator

	text = StringHolder::createCopy (t, numChars);
}

void String::appendInternal (const juce_wchar* const newText, const int numExtraChars)
{
	if (numExtraChars > 0)
	{
		const int oldLen = length();
		const int newTotalLen = oldLen + numExtraChars;

		text = StringHolder::makeUniqueWithSize (text, newTotalLen);
		StringHolder::copyChars (text + oldLen, newText, numExtraChars);
	}
}

void String::preallocateStorage (const size_t numChars)
{
	text = StringHolder::makeUniqueWithSize (text, numChars);
}

String::String() throw()
	: text (StringHolder::getEmpty())
{
}

String::~String() throw()
{
	StringHolder::release (text);
}

String::String (const String& other) throw()
	: text (other.text)
{
	StringHolder::retain (text);
}

void String::swapWith (String& other) throw()
{
	swapVariables (text, other.text);
}

String& String::operator= (const String& other) throw()
{
	juce_wchar* const newText = other.text;
	StringHolder::retain (newText);
	StringHolder::release (static_cast <juce_wchar*> (Atomic::swapPointers ((void* volatile*) &text, newText)));
	return *this;
}

String::String (const size_t numChars, const int /*dummyVariable*/)
	: text (StringHolder::createUninitialised (numChars))
{
}

String::String (const String& stringToCopy, const size_t charsToAllocate)
{
	const size_t otherSize = StringHolder::getAllocatedNumChars (stringToCopy.text);
	text = StringHolder::createUninitialised (jmax (charsToAllocate, otherSize));
	StringHolder::copyChars (text, stringToCopy.text, otherSize);
}

String::String (const char* const t)
{
	if (t != 0 && *t != 0)
		text = StringHolder::createCopy (t, CharacterFunctions::length (t));
	else
		text = StringHolder::getEmpty();
}

String::String (const juce_wchar* const t)
{
	if (t != 0 && *t != 0)
		text = StringHolder::createCopy (t, CharacterFunctions::length (t));
	else
		text = StringHolder::getEmpty();
}

String::String (const char* const t, const size_t maxChars)
{
	int i;
	for (i = 0; (size_t) i < maxChars; ++i)
		if (t[i] == 0)
			break;

	if (i > 0)
		text = StringHolder::createCopy (t, i);
	else
		text = StringHolder::getEmpty();
}

String::String (const juce_wchar* const t, const size_t maxChars)
{
	int i;
	for (i = 0; (size_t) i < maxChars; ++i)
		if (t[i] == 0)
			break;

	if (i > 0)
		text = StringHolder::createCopy (t, i);
	else
		text = StringHolder::getEmpty();
}

const String String::charToString (const juce_wchar character)
{
	String result ((size_t) 1, (int) 0);
	result.text[0] = character;
	result.text[1] = 0;
	return result;
}

namespace NumberToStringConverters
{
	// pass in a pointer to the END of a buffer..
	static juce_wchar* int64ToString (juce_wchar* t, const int64 n) throw()
	{
		*--t = 0;
		int64 v = (n >= 0) ? n : -n;

		do
		{
			*--t = (juce_wchar) ('0' + (int) (v % 10));
			v /= 10;

		} while (v > 0);

		if (n < 0)
			*--t = '-';

		return t;
	}

	static juce_wchar* uint64ToString (juce_wchar* t, int64 v) throw()
	{
		*--t = 0;

		do
		{
			*--t = (juce_wchar) ('0' + (int) (v % 10));
			v /= 10;

		} while (v > 0);

		return t;
	}

	static juce_wchar* intToString (juce_wchar* t, const int n) throw()
	{
		if (n == (int) 0x80000000) // (would cause an overflow)
			return int64ToString (t, n);

		*--t = 0;
		int v = abs (n);

		do
		{
			*--t = (juce_wchar) ('0' + (v % 10));
			v /= 10;

		} while (v > 0);

		if (n < 0)
			*--t = '-';

		return t;
	}

	static juce_wchar* uintToString (juce_wchar* t, unsigned int v) throw()
	{
		*--t = 0;

		do
		{
			*--t = (juce_wchar) ('0' + (v % 10));
			v /= 10;

		} while (v > 0);

		return t;
	}

	static juce_wchar getDecimalPoint()
	{
#if JUCE_WINDOWS && _MSC_VER < 1400
		static juce_wchar dp = std::_USE (std::locale(), std::numpunct <wchar_t>).decimal_point();
#else
		static juce_wchar dp = std::use_facet <std::numpunct <wchar_t> > (std::locale()).decimal_point();
#endif
		return dp;
	}

	static juce_wchar* doubleToString (juce_wchar* buffer, int numChars, double n, int numDecPlaces, size_t& len) throw()
	{
		if (numDecPlaces > 0 && n > -1.0e20 && n < 1.0e20)
		{
			juce_wchar* const end = buffer + numChars;
			juce_wchar* t = end;
			int64 v = (int64) (pow (10.0, numDecPlaces) * fabs (n) + 0.5);
			*--t = (juce_wchar) 0;

			while (numDecPlaces >= 0 || v > 0)
			{
				if (numDecPlaces == 0)
					*--t = getDecimalPoint();

				*--t = (juce_wchar) ('0' + (v % 10));

				v /= 10;
				--numDecPlaces;
			}

			if (n < 0)
				*--t = '-';

			len = end - t - 1;
			return t;
		}
		else
		{
#if JUCE_WIN32
  #if _MSC_VER <= 1400
			len = _snwprintf (buffer, numChars, L"%.9g", n);
  #else
			len = _snwprintf_s (buffer, numChars, _TRUNCATE, L"%.9g", n);
  #endif
#else
			len = swprintf (buffer, numChars, L"%.9g", n);
#endif
			return buffer;
		}
	}
}

String::String (const int number)
{
	juce_wchar buffer [16];
	juce_wchar* const end = buffer + numElementsInArray (buffer);
	juce_wchar* const start = NumberToStringConverters::intToString (end, number);
	createInternal (start, end - start - 1);
}

String::String (const unsigned int number)
{
	juce_wchar buffer [16];
	juce_wchar* const end = buffer + numElementsInArray (buffer);
	juce_wchar* const start = NumberToStringConverters::uintToString (end, number);
	createInternal (start, end - start - 1);
}

String::String (const short number)
{
	juce_wchar buffer [16];
	juce_wchar* const end = buffer + numElementsInArray (buffer);
	juce_wchar* const start = NumberToStringConverters::intToString (end, (int) number);
	createInternal (start, end - start - 1);
}

String::String (const unsigned short number)
{
	juce_wchar buffer [16];
	juce_wchar* const end = buffer + numElementsInArray (buffer);
	juce_wchar* const start = NumberToStringConverters::uintToString (end, (unsigned int) number);
	createInternal (start, end - start - 1);
}

String::String (const int64 number)
{
	juce_wchar buffer [32];
	juce_wchar* const end = buffer + numElementsInArray (buffer);
	juce_wchar* const start = NumberToStringConverters::int64ToString (end, number);
	createInternal (start, end - start - 1);
}

String::String (const uint64 number)
{
	juce_wchar buffer [32];
	juce_wchar* const end = buffer + numElementsInArray (buffer);
	juce_wchar* const start = NumberToStringConverters::uint64ToString (end, number);
	createInternal (start, end - start - 1);
}

String::String (const float number, const int numberOfDecimalPlaces)
{
	juce_wchar buffer [48];
	size_t len;
	juce_wchar* start = NumberToStringConverters::doubleToString (buffer, numElementsInArray (buffer), (double) number, numberOfDecimalPlaces, len);
	createInternal (start, len);
}

String::String (const double number, const int numberOfDecimalPlaces)
{
	juce_wchar buffer [48];
	size_t len;
	juce_wchar* start = NumberToStringConverters::doubleToString (buffer, numElementsInArray (buffer), number, numberOfDecimalPlaces, len);
	createInternal (start, len);
}

int String::length() const throw()
{
	return CharacterFunctions::length (text);
}

int String::hashCode() const throw()
{
	const juce_wchar* t = text;
	int result = 0;

	while (*t != (juce_wchar) 0)
		result = 31 * result + *t++;

	return result;
}

int64 String::hashCode64() const throw()
{
	const juce_wchar* t = text;
	int64 result = 0;

	while (*t != (juce_wchar) 0)
		result = 101 * result + *t++;

	return result;
}

bool JUCE_CALLTYPE operator== (const String& string1, const String& string2) throw()
{
	return string1.compare (string2) == 0;
}

bool JUCE_CALLTYPE operator== (const String& string1, const char* string2) throw()
{
	return string1.compare (string2) == 0;
}

bool JUCE_CALLTYPE operator== (const String& string1, const juce_wchar* string2) throw()
{
	return string1.compare (string2) == 0;
}

bool JUCE_CALLTYPE operator!= (const String& string1, const String& string2) throw()
{
	return string1.compare (string2) != 0;
}

bool JUCE_CALLTYPE operator!= (const String& string1, const char* string2) throw()
{
	return string1.compare (string2) != 0;
}

bool JUCE_CALLTYPE operator!= (const String& string1, const juce_wchar* string2) throw()
{
	return string1.compare (string2) != 0;
}

bool JUCE_CALLTYPE operator>  (const String& string1, const String& string2) throw()
{
	return string1.compare (string2) > 0;
}

bool JUCE_CALLTYPE operator<  (const String& string1, const String& string2) throw()
{
	return string1.compare (string2) < 0;
}

bool JUCE_CALLTYPE operator>= (const String& string1, const String& string2) throw()
{
	return string1.compare (string2) >= 0;
}

bool JUCE_CALLTYPE operator<= (const String& string1, const String& string2) throw()
{
	return string1.compare (string2) <= 0;
}

bool String::equalsIgnoreCase (const juce_wchar* t) const throw()
{
	return t != 0 ? CharacterFunctions::compareIgnoreCase (text, t) == 0
				  : isEmpty();
}

bool String::equalsIgnoreCase (const char* t) const throw()
{
	return t != 0 ? CharacterFunctions::compareIgnoreCase (text, t) == 0
				  : isEmpty();
}

bool String::equalsIgnoreCase (const String& other) const throw()
{
	return text == other.text
			|| CharacterFunctions::compareIgnoreCase (text, other.text) == 0;
}

int String::compare (const String& other) const throw()
{
	return (text == other.text) ? 0 : CharacterFunctions::compare (text, other.text);
}

int String::compare (const char* other) const throw()
{
	return other == 0 ? isEmpty() : CharacterFunctions::compare (text, other);
}

int String::compare (const juce_wchar* other) const throw()
{
	return other == 0 ? isEmpty() : CharacterFunctions::compare (text, other);
}

int String::compareIgnoreCase (const String& other) const throw()
{
	return (text == other.text) ? 0 : CharacterFunctions::compareIgnoreCase (text, other.text);
}

int String::compareLexicographically (const String& other) const throw()
{
	const juce_wchar* s1 = text;
	while (*s1 != 0 && ! CharacterFunctions::isLetterOrDigit (*s1))
		++s1;

	const juce_wchar* s2 = other.text;
	while (*s2 != 0 && ! CharacterFunctions::isLetterOrDigit (*s2))
		++s2;

	return CharacterFunctions::compareIgnoreCase (s1, s2);
}

String& String::operator+= (const juce_wchar* const t)
{
	if (t != 0)
		appendInternal (t, CharacterFunctions::length (t));

	return *this;
}

String& String::operator+= (const String& other)
{
	if (isEmpty())
		operator= (other);
	else
		appendInternal (other.text, other.length());

	return *this;
}

String& String::operator+= (const char ch)
{
	const juce_wchar asString[] = { (juce_wchar) ch, 0 };
	return operator+= (static_cast <const juce_wchar*> (asString));
}

String& String::operator+= (const juce_wchar ch)
{
	const juce_wchar asString[] = { (juce_wchar) ch, 0 };
	return operator+= (static_cast <const juce_wchar*> (asString));
}

String& String::operator+= (const int number)
{
	juce_wchar buffer [16];
	juce_wchar* const end = buffer + numElementsInArray (buffer);
	juce_wchar* const start = NumberToStringConverters::intToString (end, number);
	appendInternal (start, (int) (end - start));
	return *this;
}

String& String::operator+= (const unsigned int number)
{
	juce_wchar buffer [16];
	juce_wchar* const end = buffer + numElementsInArray (buffer);
	juce_wchar* const start = NumberToStringConverters::uintToString (end, number);
	appendInternal (start, (int) (end - start));
	return *this;
}

void String::append (const juce_wchar* const other, const int howMany)
{
	if (howMany > 0)
	{
		int i;
		for (i = 0; i < howMany; ++i)
			if (other[i] == 0)
				break;

		appendInternal (other, i);
	}
}

const String JUCE_CALLTYPE operator+ (const char* const string1, const String& string2)
{
	String s (string1);
	return s += string2;
}

const String JUCE_CALLTYPE operator+ (const juce_wchar* const string1, const String& string2)
{
	String s (string1);
	return s += string2;
}

const String JUCE_CALLTYPE operator+ (const char string1, const String& string2)
{
	return String::charToString (string1) + string2;
}

const String JUCE_CALLTYPE operator+ (const juce_wchar string1, const String& string2)
{
	return String::charToString (string1) + string2;
}

const String JUCE_CALLTYPE operator+ (String string1, const String& string2)
{
	return string1 += string2;
}

const String JUCE_CALLTYPE operator+ (String string1, const char* const string2)
{
	return string1 += string2;
}

const String JUCE_CALLTYPE operator+ (String string1, const juce_wchar* const string2)
{
	return string1 += string2;
}

const String JUCE_CALLTYPE operator+ (String string1, const char string2)
{
	return string1 += string2;
}

const String JUCE_CALLTYPE operator+ (String string1, const juce_wchar string2)
{
	return string1 += string2;
}

String& JUCE_CALLTYPE operator<< (String& string1, const char characterToAppend)
{
	return string1 += characterToAppend;
}

String& JUCE_CALLTYPE operator<< (String& string1, const juce_wchar characterToAppend)
{
	return string1 += characterToAppend;
}

String& JUCE_CALLTYPE operator<< (String& string1, const char* const string2)
{
	return string1 += string2;
}

String& JUCE_CALLTYPE operator<< (String& string1, const juce_wchar* const string2)
{
	return string1 += string2;
}

String& JUCE_CALLTYPE operator<< (String& string1, const String& string2)
{
	return string1 += string2;
}

String& JUCE_CALLTYPE operator<< (String& string1, const short number)
{
	return string1 += (int) number;
}

String& JUCE_CALLTYPE operator<< (String& string1, const int number)
{
	return string1 += number;
}

String& JUCE_CALLTYPE operator<< (String& string1, const unsigned int number)
{
	return string1 += number;
}

String& JUCE_CALLTYPE operator<< (String& string1, const long number)
{
	return string1 += (int) number;
}

String& JUCE_CALLTYPE operator<< (String& string1, const unsigned long number)
{
	return string1 += (unsigned int) number;
}

String& JUCE_CALLTYPE operator<< (String& string1, const float number)
{
	return string1 += String (number);
}

String& JUCE_CALLTYPE operator<< (String& string1, const double number)
{
	return string1 += String (number);
}

OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& text)
{
	// (This avoids using toUTF8() to prevent the memory bloat that it would leave behind
	// if lots of large, persistent strings were to be written to streams).
	const int numBytes = text.getNumBytesAsUTF8();
	HeapBlock<char> temp (numBytes + 1);
	text.copyToUTF8 (temp, numBytes + 1);
	stream.write (temp, numBytes);
	return stream;
}

int String::indexOfChar (const juce_wchar character) const throw()
{
	const juce_wchar* t = text;

	for (;;)
	{
		if (*t == character)
			return (int) (t - text);

		if (*t++ == 0)
			return -1;
	}
}

int String::lastIndexOfChar (const juce_wchar character) const throw()
{
	for (int i = length(); --i >= 0;)
		if (text[i] == character)
			return i;

	return -1;
}

int String::indexOf (const String& t) const throw()
{
	const juce_wchar* const r = CharacterFunctions::find (text, t.text);
	return r == 0 ? -1 : (int) (r - text);
}

int String::indexOfChar (const int startIndex,
						 const juce_wchar character) const throw()
{
	if (startIndex > 0 && startIndex >= length())
		return -1;

	const juce_wchar* t = text + jmax (0, startIndex);

	for (;;)
	{
		if (*t == character)
			return (int) (t - text);

		if (*t == 0)
			return -1;

		++t;
	}
}

int String::indexOfAnyOf (const String& charactersToLookFor,
						  const int startIndex,
						  const bool ignoreCase) const throw()
{
	if (startIndex > 0 && startIndex >= length())
		return -1;

	const juce_wchar* t = text + jmax (0, startIndex);

	while (*t != 0)
	{
		if (CharacterFunctions::indexOfChar (charactersToLookFor.text, *t, ignoreCase) >= 0)
			return (int) (t - text);

		++t;
	}

	return -1;
}

int String::indexOf (const int startIndex, const String& other) const throw()
{
	if (startIndex > 0 && startIndex >= length())
		return -1;

	const juce_wchar* const found = CharacterFunctions::find (text + jmax (0, startIndex), other.text);

	return found == 0 ? -1 : (int) (found - text);
}

int String::indexOfIgnoreCase (const String& other) const throw()
{
	if (other.isNotEmpty())
	{
		const int len = other.length();
		const int end = length() - len;

		for (int i = 0; i <= end; ++i)
			if (CharacterFunctions::compareIgnoreCase (text + i, other.text, len) == 0)
				return i;
	}

	return -1;
}

int String::indexOfIgnoreCase (const int startIndex, const String& other) const throw()
{
	if (other.isNotEmpty())
	{
		const int len = other.length();
		const int end = length() - len;

		for (int i = jmax (0, startIndex); i <= end; ++i)
			if (CharacterFunctions::compareIgnoreCase (text + i, other.text, len) == 0)
				return i;
	}

	return -1;
}

int String::lastIndexOf (const String& other) const throw()
{
	if (other.isNotEmpty())
	{
		const int len = other.length();
		int i = length() - len;

		if (i >= 0)
		{
			const juce_wchar* n = text + i;

			while (i >= 0)
			{
				if (CharacterFunctions::compare (n--, other.text, len) == 0)
					return i;

				--i;
			}
		}
	}

	return -1;
}

int String::lastIndexOfIgnoreCase (const String& other) const throw()
{
	if (other.isNotEmpty())
	{
		const int len = other.length();
		int i = length() - len;

		if (i >= 0)
		{
			const juce_wchar* n = text + i;

			while (i >= 0)
			{
				if (CharacterFunctions::compareIgnoreCase (n--, other.text, len) == 0)
					return i;

				--i;
			}
		}
	}

	return -1;
}

int String::lastIndexOfAnyOf (const String& charactersToLookFor, const bool ignoreCase) const throw()
{
	for (int i = length(); --i >= 0;)
		if (CharacterFunctions::indexOfChar (charactersToLookFor.text, text[i], ignoreCase) >= 0)
			return i;

	return -1;
}

bool String::contains (const String& other) const throw()
{
	return indexOf (other) >= 0;
}

bool String::containsChar (const juce_wchar character) const throw()
{
	const juce_wchar* t = text;

	for (;;)
	{
		if (*t == 0)
			return false;

		if (*t == character)
			return true;

		++t;
	}
}

bool String::containsIgnoreCase (const String& t) const throw()
{
	return indexOfIgnoreCase (t) >= 0;
}

int String::indexOfWholeWord (const String& word) const throw()
{
	if (word.isNotEmpty())
	{
		const int wordLen = word.length();
		const int end = length() - wordLen;
		const juce_wchar* t = text;

		for (int i = 0; i <= end; ++i)
		{
			if (CharacterFunctions::compare (t, word.text, wordLen) == 0
				  && (i == 0 || ! CharacterFunctions::isLetterOrDigit (* (t - 1)))
				  && ! CharacterFunctions::isLetterOrDigit (t [wordLen]))
			{
				return i;
			}

			++t;
		}
	}

	return -1;
}

int String::indexOfWholeWordIgnoreCase (const String& word) const throw()
{
	if (word.isNotEmpty())
	{
		const int wordLen = word.length();
		const int end = length() - wordLen;
		const juce_wchar* t = text;

		for (int i = 0; i <= end; ++i)
		{
			if (CharacterFunctions::compareIgnoreCase (t, word.text, wordLen) == 0
				  && (i == 0 || ! CharacterFunctions::isLetterOrDigit (* (t - 1)))
				  && ! CharacterFunctions::isLetterOrDigit (t [wordLen]))
			{
				return i;
			}

			++t;
		}
	}

	return -1;
}

bool String::containsWholeWord (const String& wordToLookFor) const throw()
{
	return indexOfWholeWord (wordToLookFor) >= 0;
}

bool String::containsWholeWordIgnoreCase (const String& wordToLookFor) const throw()
{
	return indexOfWholeWordIgnoreCase (wordToLookFor) >= 0;
}

static int indexOfMatch (const juce_wchar* const wildcard,
						 const juce_wchar* const test,
						 const bool ignoreCase) throw()
{
	int start = 0;

	while (test [start] != 0)
	{
		int i = 0;

		for (;;)
		{
			const juce_wchar wc = wildcard [i];
			const juce_wchar c = test [i + start];

			if (wc == c
				 || (ignoreCase && CharacterFunctions::toLowerCase (wc) == CharacterFunctions::toLowerCase (c))
				 || (wc == '?' && c != 0))
			{
				if (wc == 0)
					return start;

				++i;
			}
			else
			{
				if (wc == '*' && (wildcard [i + 1] == 0
								   || indexOfMatch (wildcard + i + 1,
													test + start + i,
													ignoreCase) >= 0))
				{
					return start;
				}

				break;
			}
		}

		++start;
	}

	return -1;
}

bool String::matchesWildcard (const String& wildcard, const bool ignoreCase) const throw()
{
	int i = 0;

	for (;;)
	{
		const juce_wchar wc = wildcard.text [i];
		const juce_wchar c = text [i];

		if (wc == c
			 || (ignoreCase && CharacterFunctions::toLowerCase (wc) == CharacterFunctions::toLowerCase (c))
			 || (wc == '?' && c != 0))
		{
			if (wc == 0)
				return true;

			++i;
		}
		else
		{
			return wc == '*' && (wildcard [i + 1] == 0
								  || indexOfMatch (wildcard.text + i + 1,
												   text + i,
												   ignoreCase) >= 0);
		}
	}
}

const String String::repeatedString (const String& stringToRepeat, int numberOfTimesToRepeat)
{
	const int len = stringToRepeat.length();
	String result ((size_t) (len * numberOfTimesToRepeat + 1), (int) 0);
	juce_wchar* n = result.text;
	*n = 0;

	while (--numberOfTimesToRepeat >= 0)
	{
		StringHolder::copyChars (n, stringToRepeat.text, len);
		n += len;
	}

	return result;
}

const String String::paddedLeft (const juce_wchar padCharacter, int minimumLength) const
{
	jassert (padCharacter != 0);

	const int len = length();
	if (len >= minimumLength || padCharacter == 0)
		return *this;

	String result ((size_t) minimumLength + 1, (int) 0);
	juce_wchar* n = result.text;

	minimumLength -= len;
	while (--minimumLength >= 0)
		*n++ = padCharacter;

	StringHolder::copyChars (n, text, len);
	return result;
}

const String String::paddedRight (const juce_wchar padCharacter, int minimumLength) const
{
	jassert (padCharacter != 0);

	const int len = length();
	if (len >= minimumLength || padCharacter == 0)
		return *this;

	String result (*this, (size_t) minimumLength);
	juce_wchar* n = result.text + len;

	minimumLength -= len;
	while (--minimumLength >= 0)
		*n++ = padCharacter;

	*n = 0;
	return result;
}

const String String::replaceSection (int index, int numCharsToReplace, const String& stringToInsert) const
{
	if (index < 0)
	{
		// a negative index to replace from?
		jassertfalse
		index = 0;
	}

	if (numCharsToReplace < 0)
	{
		// replacing a negative number of characters?
		numCharsToReplace = 0;
		jassertfalse;
	}

	const int len = length();

	if (index + numCharsToReplace > len)
	{
		if (index > len)
		{
			// replacing beyond the end of the string?
			index = len;
			jassertfalse
		}

		numCharsToReplace = len - index;
	}

	const int newStringLen = stringToInsert.length();
	const int newTotalLen = len + newStringLen - numCharsToReplace;

	if (newTotalLen <= 0)
		return String::empty;

	String result ((size_t) newTotalLen, (int) 0);

	StringHolder::copyChars (result.text, text, index);

	if (newStringLen > 0)
		StringHolder::copyChars (result.text + index, stringToInsert.text, newStringLen);

	const int endStringLen = newTotalLen - (index + newStringLen);

	if (endStringLen > 0)
		StringHolder::copyChars (result.text + (index + newStringLen),
								 text + (index + numCharsToReplace),
								 endStringLen);

	return result;
}

const String String::replace (const String& stringToReplace, const String& stringToInsert, const bool ignoreCase) const
{
	const int stringToReplaceLen = stringToReplace.length();
	const int stringToInsertLen = stringToInsert.length();

	int i = 0;
	String result (*this);

	while ((i = (ignoreCase ? result.indexOfIgnoreCase (i, stringToReplace)
							: result.indexOf (i, stringToReplace))) >= 0)
	{
		result = result.replaceSection (i, stringToReplaceLen, stringToInsert);
		i += stringToInsertLen;
	}

	return result;
}

const String String::replaceCharacter (const juce_wchar charToReplace, const juce_wchar charToInsert) const
{
	const int index = indexOfChar (charToReplace);

	if (index < 0)
		return *this;

	String result (*this, size_t());
	juce_wchar* t = result.text + index;

	while (*t != 0)
	{
		if (*t == charToReplace)
			*t = charToInsert;

		++t;
	}

	return result;
}

const String String::replaceCharacters (const String& charactersToReplace,
										const String& charactersToInsertInstead) const
{
	String result (*this, size_t());
	juce_wchar* t = result.text;
	const int len2 = charactersToInsertInstead.length();

	// the two strings passed in are supposed to be the same length!
	jassert (len2 == charactersToReplace.length());

	while (*t != 0)
	{
		const int index = charactersToReplace.indexOfChar (*t);

		if (((unsigned int) index) < (unsigned int) len2)
			*t = charactersToInsertInstead [index];

		++t;
	}

	return result;
}

bool String::startsWith (const String& other) const throw()
{
	return CharacterFunctions::compare (text, other.text, other.length()) == 0;
}

bool String::startsWithIgnoreCase (const String& other) const throw()
{
	return CharacterFunctions::compareIgnoreCase (text, other.text, other.length()) == 0;
}

bool String::startsWithChar (const juce_wchar character) const throw()
{
	jassert (character != 0); // strings can't contain a null character!

	return text[0] == character;
}

bool String::endsWithChar (const juce_wchar character) const throw()
{
	jassert (character != 0); // strings can't contain a null character!

	return text[0] != 0
			&& text [length() - 1] == character;
}

bool String::endsWith (const String& other) const throw()
{
	const int thisLen = length();
	const int otherLen = other.length();

	return thisLen >= otherLen
			&& CharacterFunctions::compare (text + thisLen - otherLen, other.text) == 0;
}

bool String::endsWithIgnoreCase (const String& other) const throw()
{
	const int thisLen = length();
	const int otherLen = other.length();

	return thisLen >= otherLen
			&& CharacterFunctions::compareIgnoreCase (text + thisLen - otherLen, other.text) == 0;
}

const String String::toUpperCase() const
{
	String result (*this, size_t());
	CharacterFunctions::toUpperCase (result.text);
	return result;
}

const String String::toLowerCase() const
{
	String result (*this, size_t());
	CharacterFunctions::toLowerCase (result.text);
	return result;
}

juce_wchar& String::operator[] (const int index)
{
	jassert (((unsigned int) index) <= (unsigned int) length());
	text = StringHolder::makeUnique (text);
	return text [index];
}

juce_wchar String::getLastCharacter() const throw()
{
	return isEmpty() ? juce_wchar() : text [length() - 1];
}

const String String::substring (int start, int end) const
{
	if (start < 0)
		start = 0;
	else if (end <= start)
		return empty;

	int len = 0;
	while (len <= end && text [len] != 0)
		++len;

	if (end >= len)
	{
		if (start == 0)
			return *this;

		end = len;
	}

	return String (text + start, end - start);
}

const String String::substring (const int start) const
{
	if (start <= 0)
		return *this;

	const int len = length();

	if (start >= len)
		return empty;

	return String (text + start, len - start);
}

const String String::dropLastCharacters (const int numberToDrop) const
{
	return String (text, jmax (0, length() - numberToDrop));
}

const String String::getLastCharacters (const int numCharacters) const
{
	return String (text + jmax (0, length() - jmax (0, numCharacters)));
}

const String String::fromFirstOccurrenceOf (const String& sub,
											const bool includeSubString,
											const bool ignoreCase) const
{
	const int i = ignoreCase ? indexOfIgnoreCase (sub)
							 : indexOf (sub);
	if (i < 0)
		return empty;

	return substring (includeSubString ? i : i + sub.length());
}

const String String::fromLastOccurrenceOf (const String& sub,
										   const bool includeSubString,
										   const bool ignoreCase) const
{
	const int i = ignoreCase ? lastIndexOfIgnoreCase (sub)
							 : lastIndexOf (sub);
	if (i < 0)
		return *this;

	return substring (includeSubString ? i : i + sub.length());
}

const String String::upToFirstOccurrenceOf (const String& sub,
											const bool includeSubString,
											const bool ignoreCase) const
{
	const int i = ignoreCase ? indexOfIgnoreCase (sub)
							 : indexOf (sub);
	if (i < 0)
		return *this;

	return substring (0, includeSubString ? i + sub.length() : i);
}

const String String::upToLastOccurrenceOf (const String& sub,
										   const bool includeSubString,
										   const bool ignoreCase) const
{
	const int i = ignoreCase ? lastIndexOfIgnoreCase (sub)
							 : lastIndexOf (sub);
	if (i < 0)
		return *this;

	return substring (0, includeSubString ? i + sub.length() : i);
}

bool String::isQuotedString() const
{
	const String trimmed (trimStart());

	return trimmed[0] == '"'
		|| trimmed[0] == '\'';
}

const String String::unquoted() const
{
	String s (*this);

	if (s.text[0] == '"' || s.text[0] == '\'')
		s = s.substring (1);

	const int lastCharIndex = s.length() - 1;

	if (lastCharIndex >= 0
		 && (s [lastCharIndex] == '"' || s[lastCharIndex] == '\''))
		s [lastCharIndex] = 0;

	return s;
}

const String String::quoted (const juce_wchar quoteCharacter) const
{
	if (isEmpty())
		return charToString (quoteCharacter) + quoteCharacter;

	String t (*this);

	if (! t.startsWithChar (quoteCharacter))
		t = charToString (quoteCharacter) + t;

	if (! t.endsWithChar (quoteCharacter))
		t += quoteCharacter;

	return t;
}

const String String::trim() const
{
	if (isEmpty())
		return empty;

	int start = 0;

	while (CharacterFunctions::isWhitespace (text [start]))
		++start;

	const int len = length();
	int end = len - 1;

	while ((end >= start) && CharacterFunctions::isWhitespace (text [end]))
		--end;

	++end;

	if (end <= start)
		return empty;
	else if (start > 0 || end < len)
		return String (text + start, end - start);

	return *this;
}

const String String::trimStart() const
{
	if (isEmpty())
		return empty;

	const juce_wchar* t = text;

	while (CharacterFunctions::isWhitespace (*t))
		++t;

	if (t == text)
		return *this;

	return String (t);
}

const String String::trimEnd() const
{
	if (isEmpty())
		return empty;

	const juce_wchar* endT = text + (length() - 1);

	while ((endT >= text) && CharacterFunctions::isWhitespace (*endT))
		--endT;

	return String (text, (int) (++endT - text));
}

const String String::trimCharactersAtStart (const String& charactersToTrim) const
{
	const juce_wchar* t = text;

	while (charactersToTrim.containsChar (*t))
		++t;

	if (t == text)
		return *this;

	return String (t);
}

const String String::trimCharactersAtEnd (const String& charactersToTrim) const
{
	if (isEmpty())
		return empty;

	const juce_wchar* endT = text + (length() - 1);

	while (endT >= text && charactersToTrim.containsChar (*endT))
		--endT;

	return String (text, (int) (++endT - text));
}

const String String::retainCharacters (const String& charactersToRetain) const
{
	if (isEmpty())
		return empty;

	String result (StringHolder::getAllocatedNumChars (text), (int) 0);
	juce_wchar* dst = result.text;
	const juce_wchar* src = text;

	while (*src != 0)
	{
		if (charactersToRetain.containsChar (*src))
			*dst++ = *src;

		++src;
	}

	*dst = 0;
	return result;
}

const String String::removeCharacters (const String& charactersToRemove) const
{
	if (isEmpty())
		return empty;

	String result (StringHolder::getAllocatedNumChars (text), (int) 0);
	juce_wchar* dst = result.text;
	const juce_wchar* src = text;

	while (*src != 0)
	{
		if (! charactersToRemove.containsChar (*src))
			*dst++ = *src;

		++src;
	}

	*dst = 0;
	return result;
}

const String String::initialSectionContainingOnly (const String& permittedCharacters) const
{
	int i = 0;

	for (;;)
	{
		if (! permittedCharacters.containsChar (text[i]))
			break;

		++i;
	}

	return substring (0, i);
}

const String String::initialSectionNotContaining (const String& charactersToStopAt) const
{
	const juce_wchar* const t = text;
	int i = 0;

	while (t[i] != 0)
	{
		if (charactersToStopAt.containsChar (t[i]))
			return String (text, i);

		++i;
	}

	return empty;
}

bool String::containsOnly (const String& chars) const throw()
{
	const juce_wchar* t = text;

	while (*t != 0)
		if (! chars.containsChar (*t++))
			return false;

	return true;
}

bool String::containsAnyOf (const String& chars) const throw()
{
	const juce_wchar* t = text;

	while (*t != 0)
		if (chars.containsChar (*t++))
			return true;

	return false;
}

bool String::containsNonWhitespaceChars() const throw()
{
	const juce_wchar* t = text;

	while (*t != 0)
		if (! CharacterFunctions::isWhitespace (*t++))
			return true;

	return false;
}

const String String::formatted (const juce_wchar* const pf, ... )
{
	jassert (pf != 0);

	va_list args;
	va_start (args, pf);

	size_t bufferSize = 256;
	String result (bufferSize, (int) 0);
	result.text[0] = 0;

	for (;;)
	{
#if JUCE_LINUX && JUCE_64BIT
		va_list tempArgs;
		va_copy (tempArgs, args);
		const int num = (int) vswprintf (result.text, bufferSize - 1, pf, tempArgs);
		va_end (tempArgs);
#elif JUCE_WINDOWS
		#ifdef _MSC_VER
		  #pragma warning (push)
		  #pragma warning (disable: 4996)
		#endif
		const int num = (int) _vsnwprintf (result.text, bufferSize - 1, pf, args);
		#ifdef _MSC_VER
		  #pragma warning (pop)
		#endif
#else
		const int num = (int) vswprintf (result.text, bufferSize - 1, pf, args);
#endif

		if (num > 0)
			return result;

		bufferSize += 256;

		if (num == 0 || bufferSize > 65536) // the upper limit is a sanity check to avoid situations where vprintf repeatedly
			break;			  // returns -1 because of an error rather than because it needs more space.

		result.preallocateStorage (bufferSize);
	}

	return empty;
}

int String::getIntValue() const throw()
{
	return CharacterFunctions::getIntValue (text);
}

int String::getTrailingIntValue() const throw()
{
	int n = 0;
	int mult = 1;
	const juce_wchar* t = text + length();

	while (--t >= text)
	{
		const juce_wchar c = *t;

		if (! CharacterFunctions::isDigit (c))
		{
			if (c == '-')
				n = -n;

			break;
		}

		n += mult * (c - '0');
		mult *= 10;
	}

	return n;
}

int64 String::getLargeIntValue() const throw()
{
	return CharacterFunctions::getInt64Value (text);
}

float String::getFloatValue() const throw()
{
	return (float) CharacterFunctions::getDoubleValue (text);
}

double String::getDoubleValue() const throw()
{
	return CharacterFunctions::getDoubleValue (text);
}

static const juce_wchar* const hexDigits = T("0123456789abcdef");

const String String::toHexString (const int number)
{
	juce_wchar buffer[32];
	juce_wchar* const end = buffer + 32;
	juce_wchar* t = end;
	*--t = 0;
	unsigned int v = (unsigned int) number;

	do
	{
		*--t = hexDigits [v & 15];
		v >>= 4;

	} while (v != 0);

	return String (t, (int) (((char*) end) - (char*) t) - 1);
}

const String String::toHexString (const int64 number)
{
	juce_wchar buffer[32];
	juce_wchar* const end = buffer + 32;
	juce_wchar* t = end;
	*--t = 0;
	uint64 v = (uint64) number;

	do
	{
		*--t = hexDigits [(int) (v & 15)];
		v >>= 4;

	} while (v != 0);

	return String (t, (int) (((char*) end) - (char*) t));
}

const String String::toHexString (const short number)
{
	return toHexString ((int) (unsigned short) number);
}

const String String::toHexString (const unsigned char* data,
								  const int size,
								  const int groupSize)
{
	if (size <= 0)
		return empty;

	int numChars = (size * 2) + 2;
	if (groupSize > 0)
		numChars += size / groupSize;

	String s ((size_t) numChars, (int) 0);

	juce_wchar* d = s.text;

	for (int i = 0; i < size; ++i)
	{
		*d++ = hexDigits [(*data) >> 4];
		*d++ = hexDigits [(*data) & 0xf];
		++data;

		if (groupSize > 0 && (i % groupSize) == (groupSize - 1) && i < (size - 1))
			*d++ = ' ';
	}

	*d = 0;
	return s;
}

int String::getHexValue32() const throw()
{
	int result = 0;
	const juce_wchar* c = text;

	for (;;)
	{
		const int hexValue = CharacterFunctions::getHexDigitValue (*c);

		if (hexValue >= 0)
			result = (result << 4) | hexValue;
		else if (*c == 0)
			break;

		++c;
	}

	return result;
}

int64 String::getHexValue64() const throw()
{
	int64 result = 0;
	const juce_wchar* c = text;

	for (;;)
	{
		const int hexValue = CharacterFunctions::getHexDigitValue (*c);

		if (hexValue >= 0)
			result = (result << 4) | hexValue;
		else if (*c == 0)
			break;

		++c;
	}

	return result;
}

const String String::createStringFromData (const void* const data_, const int size)
{
	const char* const data = static_cast <const char*> (data_);

	if (size <= 0 || data == 0)
	{
		return empty;
	}
	else if (size < 2)
	{
		return charToString (data[0]);
	}
	else if ((data[0] == (char)-2 && data[1] == (char)-1)
			 || (data[0] == (char)-1 && data[1] == (char)-2))
	{
		// assume it's 16-bit unicode
		const bool bigEndian = (data[0] == (char)-2);
		const int numChars = size / 2 - 1;

		String result;
		result.preallocateStorage (numChars + 2);

		const uint16* const src = (const uint16*) (data + 2);
		juce_wchar* const dst = const_cast <juce_wchar*> (static_cast <const juce_wchar*> (result));

		if (bigEndian)
		{
			for (int i = 0; i < numChars; ++i)
				dst[i] = (juce_wchar) ByteOrder::swapIfLittleEndian (src[i]);
		}
		else
		{
			for (int i = 0; i < numChars; ++i)
				dst[i] = (juce_wchar) ByteOrder::swapIfBigEndian (src[i]);
		}

		dst [numChars] = 0;
		return result;
	}
	else
	{
		return String::fromUTF8 (data, size);
	}
}

const char* String::toUTF8() const
{
	if (isEmpty())
	{
		return reinterpret_cast <const char*> (text);
	}
	else
	{
		const int currentLen = length() + 1;
		const int utf8BytesNeeded = getNumBytesAsUTF8();

		String* const mutableThis = const_cast <String*> (this);
		mutableThis->text = StringHolder::makeUniqueWithSize (mutableThis->text, currentLen + 1 + utf8BytesNeeded / sizeof (juce_wchar));

		char* const otherCopy = reinterpret_cast <char*> (mutableThis->text + currentLen);
		copyToUTF8 (otherCopy, std::numeric_limits<int>::max());

		return otherCopy;
	}
}

int String::copyToUTF8 (char* const buffer, const int maxBufferSizeBytes) const throw()
{
	jassert (maxBufferSizeBytes >= 0); // keep this value positive, or no characters will be copied!

	int num = 0, index = 0;

	for (;;)
	{
		const uint32 c = (uint32) text [index++];

		if (c >= 0x80)
		{
			int numExtraBytes = 1;

			if (c >= 0x800)
			{
				++numExtraBytes;

				if (c >= 0x10000)
				{
					++numExtraBytes;

					if (c >= 0x200000)
					{
						++numExtraBytes;

						if (c >= 0x4000000)
							++numExtraBytes;
					}
				}
			}

			if (buffer != 0)
			{
				if (num + numExtraBytes >= maxBufferSizeBytes)
				{
					buffer [num++] = 0;
					break;
				}
				else
				{
					buffer [num++] = (uint8) ((0xff << (7 - numExtraBytes)) | (c >> (numExtraBytes * 6)));

					while (--numExtraBytes >= 0)
						buffer [num++] = (uint8) (0x80 | (0x3f & (c >> (numExtraBytes * 6))));
				}
			}
			else
			{
				num += numExtraBytes + 1;
			}
		}
		else
		{
			if (buffer != 0)
			{
				if (num + 1 >= maxBufferSizeBytes)
				{
					buffer [num++] = 0;
					break;
				}

				buffer [num] = (uint8) c;
			}

			++num;
		}

		if (c == 0)
			break;
	}

	return num;
}

int String::getNumBytesAsUTF8() const throw()
{
	int num = 0;
	const juce_wchar* t = text;

	for (;;)
	{
		const uint32 c = (uint32) *t;

		if (c >= 0x80)
		{
			++num;
			if (c >= 0x800)
			{
				++num;
				if (c >= 0x10000)
				{
					++num;
					if (c >= 0x200000)
					{
						++num;
						if (c >= 0x4000000)
							++num;
					}
				}
			}
		}
		else if (c == 0)
			break;

		++num;
		++t;
	}

	return num;
}

const String String::fromUTF8 (const char* const buffer, int bufferSizeBytes)
{
	if (buffer == 0)
		return empty;

	if (bufferSizeBytes < 0)
		bufferSizeBytes = std::numeric_limits<int>::max();

	size_t numBytes;
	for (numBytes = 0; numBytes < (size_t) bufferSizeBytes; ++numBytes)
		if (buffer [numBytes] == 0)
			break;

	String result ((size_t) numBytes + 1, (int) 0);
	juce_wchar* dest = result.text;

	size_t i = 0;
	while (i < numBytes)
	{
		const char c = buffer [i++];

		if (c < 0)
		{
			unsigned int mask = 0x7f;
			int bit = 0x40;
			int numExtraValues = 0;

			while (bit != 0 && (c & bit) != 0)
			{
				bit >>= 1;
				mask >>= 1;
				++numExtraValues;
			}

			int n = (mask & (unsigned char) c);

			while (--numExtraValues >= 0 && i < (size_t) bufferSizeBytes)
			{
				const char nextByte = buffer[i];

				if ((nextByte & 0xc0) != 0x80)
					break;

				n <<= 6;
				n |= (nextByte & 0x3f);
				++i;
			}

			*dest++ = (juce_wchar) n;
		}
		else
		{
			*dest++ = (juce_wchar) c;
		}
	}

	*dest = 0;
	return result;
}

const char* String::toCString() const
{
	if (isEmpty())
	{
		return reinterpret_cast <const char*> (text);
	}
	else
	{
		const int len = length();
		String* const mutableThis = const_cast <String*> (this);
		mutableThis->text = StringHolder::makeUniqueWithSize (mutableThis->text, (len + 1) * 2);

		char* otherCopy = reinterpret_cast <char*> (mutableThis->text + len + 1);
		CharacterFunctions::copy (otherCopy, text, len);
		otherCopy [len] = 0;
		return otherCopy;
	}
}

#ifdef _MSC_VER
  #pragma warning (disable: 4514 4996)
  #pragma warning (push)
#endif

int String::getNumBytesAsCString() const throw()
{
	return (int) wcstombs (0, text, 0);
}

int String::copyToCString (char* destBuffer, const int maxBufferSizeBytes) const throw()
{
	const int numBytes = (int) wcstombs (destBuffer, text, maxBufferSizeBytes);

	if (destBuffer != 0 && numBytes >= 0)
		destBuffer [numBytes] = 0;

	return numBytes;
}

#ifdef _MSC_VER
  #pragma warning (pop)
#endif

void String::copyToUnicode (juce_wchar* const destBuffer, const int maxCharsToCopy) const throw()
{
	StringHolder::copyChars (destBuffer, text, jmin (maxCharsToCopy, length()));
}

String::Concatenator::Concatenator (String& stringToAppendTo)
	: result (stringToAppendTo),
	  nextIndex (stringToAppendTo.length())
{
}

String::Concatenator::~Concatenator()
{
}

void String::Concatenator::append (const String& s)
{
	const int len = s.length();

	if (len > 0)
	{
		result.preallocateStorage (nextIndex + len);
		s.copyToUnicode (static_cast <juce_wchar*> (result) + nextIndex, len);
		nextIndex += len;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_String.cpp ***/


/*** Start of inlined file: juce_StringArray.cpp ***/
BEGIN_JUCE_NAMESPACE

StringArray::StringArray() throw()
{
}

StringArray::StringArray (const StringArray& other)
	: strings (other.strings)
{
}

StringArray::StringArray (const String& firstValue)
{
	strings.add (firstValue);
}

StringArray::StringArray (const juce_wchar** const initialStrings,
						  const int numberOfStrings)
{
	for (int i = 0; i < numberOfStrings; ++i)
		strings.add (initialStrings [i]);
}

StringArray::StringArray (const char** const initialStrings,
						  const int numberOfStrings)
{
	for (int i = 0; i < numberOfStrings; ++i)
		strings.add (initialStrings [i]);
}

StringArray::StringArray (const juce_wchar** const initialStrings)
{
	int i = 0;

	while (initialStrings[i] != 0)
		strings.add (initialStrings [i++]);
}

StringArray::StringArray (const char** const initialStrings)
{
	int i = 0;

	while (initialStrings[i] != 0)
		strings.add (initialStrings [i++]);
}

StringArray& StringArray::operator= (const StringArray& other)
{
	strings = other.strings;
	return *this;
}

StringArray::~StringArray()
{
}

bool StringArray::operator== (const StringArray& other) const throw()
{
	if (other.size() != size())
		return false;

	for (int i = size(); --i >= 0;)
		if (other.strings.getReference(i) != strings.getReference(i))
			return false;

	return true;
}

bool StringArray::operator!= (const StringArray& other) const throw()
{
	return ! operator== (other);
}

void StringArray::clear()
{
	strings.clear();
}

const String& StringArray::operator[] (const int index) const throw()
{
	if (((unsigned int) index) < (unsigned int) strings.size())
		return strings.getReference (index);

	return String::empty;
}

void StringArray::add (const String& newString)
{
	strings.add (newString);
}

void StringArray::insert (const int index, const String& newString)
{
	strings.insert (index, newString);
}

void StringArray::addIfNotAlreadyThere (const String& newString, const bool ignoreCase)
{
	if (! contains (newString, ignoreCase))
		add (newString);
}

void StringArray::addArray (const StringArray& otherArray, int startIndex, int numElementsToAdd)
{
	if (startIndex < 0)
	{
		jassertfalse
		startIndex = 0;
	}

	if (numElementsToAdd < 0 || startIndex + numElementsToAdd > otherArray.size())
		numElementsToAdd = otherArray.size() - startIndex;

	while (--numElementsToAdd >= 0)
		strings.add (otherArray.strings.getReference (startIndex++));
}

void StringArray::set (const int index, const String& newString)
{
	strings.set (index, newString);
}

bool StringArray::contains (const String& stringToLookFor, const bool ignoreCase) const
{
	if (ignoreCase)
	{
		for (int i = size(); --i >= 0;)
			if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
				return true;
	}
	else
	{
		for (int i = size(); --i >= 0;)
			if (stringToLookFor == strings.getReference(i))
				return true;
	}

	return false;
}

int StringArray::indexOf (const String& stringToLookFor, const bool ignoreCase, int i) const
{
	if (i < 0)
		i = 0;

	const int numElements = size();

	if (ignoreCase)
	{
		while (i < numElements)
		{
			if (strings.getReference(i).equalsIgnoreCase (stringToLookFor))
				return i;

			++i;
		}
	}
	else
	{
		while (i < numElements)
		{
			if (stringToLookFor == strings.getReference (i))
				return i;

			++i;
		}
	}

	return -1;
}

void StringArray::remove (const int index)
{
	strings.remove (index);
}

void StringArray::removeString (const String& stringToRemove,
								const bool ignoreCase)
{
	if (ignoreCase)
	{
		for (int i = size(); --i >= 0;)
			if (strings.getReference(i).equalsIgnoreCase (stringToRemove))
				strings.remove (i);
	}
	else
	{
		for (int i = size(); --i >= 0;)
			if (stringToRemove == strings.getReference (i))
				strings.remove (i);
	}
}

void StringArray::removeEmptyStrings (const bool removeWhitespaceStrings)
{
	if (removeWhitespaceStrings)
	{
		for (int i = size(); --i >= 0;)
			if (! strings.getReference(i).containsNonWhitespaceChars())
				strings.remove (i);
	}
	else
	{
		for (int i = size(); --i >= 0;)
			if (strings.getReference(i).isEmpty())
				strings.remove (i);
	}
}

void StringArray::trim()
{
	for (int i = size(); --i >= 0;)
	{
		String& s = strings.getReference(i);
		s = s.trim();
	}
}

class InternalStringArrayComparator_CaseSensitive
{
public:
	static int compareElements (String& first, String& second)	  { return first.compare (second); }
};

class InternalStringArrayComparator_CaseInsensitive
{
public:
	static int compareElements (String& first, String& second)	  { return first.compareIgnoreCase (second); }
};

void StringArray::sort (const bool ignoreCase)
{
	if (ignoreCase)
	{
		InternalStringArrayComparator_CaseInsensitive comp;
		strings.sort (comp);
	}
	else
	{
		InternalStringArrayComparator_CaseSensitive comp;
		strings.sort (comp);
	}
}

void StringArray::move (const int currentIndex, int newIndex) throw()
{
	strings.move (currentIndex, newIndex);
}

const String StringArray::joinIntoString (const String& separator, int start, int numberToJoin) const
{
	const int last = (numberToJoin < 0) ? size()
										: jmin (size(), start + numberToJoin);

	if (start < 0)
		start = 0;

	if (start >= last)
		return String::empty;

	if (start == last - 1)
		return strings.getReference (start);

	const int separatorLen = separator.length();
	int charsNeeded = separatorLen * (last - start - 1);

	for (int i = start; i < last; ++i)
		charsNeeded += strings.getReference(i).length();

	String result;
	result.preallocateStorage (charsNeeded);

	juce_wchar* dest = result;

	while (start < last)
	{
		const String& s = strings.getReference (start);
		const int len = s.length();

		if (len > 0)
		{
			s.copyToUnicode (dest, len);
			dest += len;
		}

		if (++start < last && separatorLen > 0)
		{
			separator.copyToUnicode (dest, separatorLen);
			dest += separatorLen;
		}
	}

	*dest = 0;

	return result;
}

int StringArray::addTokens (const String& text, const bool preserveQuotedStrings)
{
	return addTokens (text, " \n\r\t", preserveQuotedStrings ? "\"" : "");
}

int StringArray::addTokens (const String& text, const String& breakCharacters, const String& quoteCharacters)
{
	int num = 0;

	if (text.isNotEmpty())
	{
		bool insideQuotes = false;
		juce_wchar currentQuoteChar = 0;
		int i = 0;
		int tokenStart = 0;

		for (;;)
		{
			const juce_wchar c = text[i];

			const bool isBreak = (c == 0) || ((! insideQuotes) && breakCharacters.containsChar (c));

			if (! isBreak)
			{
				if (quoteCharacters.containsChar (c))
				{
					if (insideQuotes)
					{
						// only break out of quotes-mode if we find a matching quote to the
						// one that we opened with..
						if (currentQuoteChar == c)
							insideQuotes = false;
					}
					else
					{
						insideQuotes = true;
						currentQuoteChar = c;
					}
				}
			}
			else
			{
				add (String (static_cast <const juce_wchar*> (text) + tokenStart, i - tokenStart));

				++num;
				tokenStart = i + 1;
			}

			if (c == 0)
				break;

			++i;
		}
	}

	return num;
}

int StringArray::addLines (const String& sourceText)
{
	int numLines = 0;
	const juce_wchar* text = sourceText;

	while (*text != 0)
	{
		const juce_wchar* const startOfLine = text;

		while (*text != 0)
		{
			if (*text == '\r')
			{
				++text;
				if (*text == '\n')
					++text;

				break;
			}

			if (*text == '\n')
			{
				++text;
				break;
			}

			++text;
		}

		const juce_wchar* endOfLine = text;
		if (endOfLine > startOfLine && (*(endOfLine - 1) == '\r' || *(endOfLine - 1) == '\n'))
			--endOfLine;

		if (endOfLine > startOfLine && (*(endOfLine - 1) == '\r' || *(endOfLine - 1) == '\n'))
			--endOfLine;

		add (String (startOfLine, jmax (0, (int) (endOfLine - startOfLine))));

		++numLines;
	}

	return numLines;
}

void StringArray::removeDuplicates (const bool ignoreCase)
{
	for (int i = 0; i < size() - 1; ++i)
	{
		const String s (strings.getReference(i));

		int nextIndex = i + 1;

		for (;;)
		{
			nextIndex = indexOf (s, ignoreCase, nextIndex);

			if (nextIndex < 0)
				break;

			strings.remove (nextIndex);
		}
	}
}

void StringArray::appendNumbersToDuplicates (const bool ignoreCase,
											 const bool appendNumberToFirstInstance,
											 const juce_wchar* preNumberString,
											 const juce_wchar* postNumberString)
{
	if (preNumberString == 0)
		preNumberString = T(" (");

	if (postNumberString == 0)
		postNumberString = T(")");

	for (int i = 0; i < size() - 1; ++i)
	{
		String& s = strings.getReference(i);

		int nextIndex = indexOf (s, ignoreCase, i + 1);

		if (nextIndex >= 0)
		{
			const String original (s);

			int number = 0;

			if (appendNumberToFirstInstance)
				s = original + preNumberString + String (++number) + postNumberString;
			else
				++number;

			while (nextIndex >= 0)
			{
				set (nextIndex, (*this)[nextIndex] + preNumberString + String (++number) + postNumberString);
				nextIndex = indexOf (original, ignoreCase, nextIndex + 1);
			}
		}
	}
}

void StringArray::minimiseStorageOverheads()
{
	strings.minimiseStorageOverheads();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_StringArray.cpp ***/


/*** Start of inlined file: juce_StringPairArray.cpp ***/
BEGIN_JUCE_NAMESPACE

StringPairArray::StringPairArray (const bool ignoreCase_)
	: ignoreCase (ignoreCase_)
{
}

StringPairArray::StringPairArray (const StringPairArray& other)
	: keys (other.keys),
	  values (other.values),
	  ignoreCase (other.ignoreCase)
{
}

StringPairArray::~StringPairArray()
{
}

StringPairArray& StringPairArray::operator= (const StringPairArray& other)
{
	keys = other.keys;
	values = other.values;
	return *this;
}

bool StringPairArray::operator== (const StringPairArray& other) const
{
	for (int i = keys.size(); --i >= 0;)
		if (other [keys[i]] != values[i])
			return false;

	return true;
}

bool StringPairArray::operator!= (const StringPairArray& other) const
{
	return ! operator== (other);
}

const String& StringPairArray::operator[] (const String& key) const
{
	return values [keys.indexOf (key, ignoreCase)];
}

const String StringPairArray::getValue (const String& key, const String& defaultReturnValue) const
{
	const int i = keys.indexOf (key, ignoreCase);

	if (i >= 0)
		return values[i];

	return defaultReturnValue;
}

void StringPairArray::set (const String& key, const String& value)
{
	const int i = keys.indexOf (key, ignoreCase);

	if (i >= 0)
	{
		values.set (i, value);
	}
	else
	{
		keys.add (key);
		values.add (value);
	}
}

void StringPairArray::addArray (const StringPairArray& other)
{
	for (int i = 0; i < other.size(); ++i)
		set (other.keys[i], other.values[i]);
}

void StringPairArray::clear()
{
	keys.clear();
	values.clear();
}

void StringPairArray::remove (const String& key)
{
	remove (keys.indexOf (key, ignoreCase));
}

void StringPairArray::remove (const int index)
{
	keys.remove (index);
	values.remove (index);
}

void StringPairArray::setIgnoresCase (const bool shouldIgnoreCase)
{
	ignoreCase = shouldIgnoreCase;
}

const String StringPairArray::getDescription() const
{
	String s;

	for (int i = 0; i < keys.size(); ++i)
	{
		s << keys[i] << " = " << values[i];
		if (i < keys.size())
			s << ", ";
	}

	return s;
}

void StringPairArray::minimiseStorageOverheads()
{
	keys.minimiseStorageOverheads();
	values.minimiseStorageOverheads();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_StringPairArray.cpp ***/


/*** Start of inlined file: juce_XmlDocument.cpp ***/
BEGIN_JUCE_NAMESPACE

XmlDocument::XmlDocument (const String& documentText)
	: originalText (documentText),
	  ignoreEmptyTextElements (true)
{
}

XmlDocument::XmlDocument (const File& file)
	: ignoreEmptyTextElements (true)
{
	inputSource = new FileInputSource (file);
}

XmlDocument::~XmlDocument()
{
}

void XmlDocument::setInputSource (InputSource* const newSource) throw()
{
	inputSource = newSource;
}

void XmlDocument::setEmptyTextElementsIgnored (const bool shouldBeIgnored) throw()
{
	ignoreEmptyTextElements = shouldBeIgnored;
}

bool XmlDocument::isXmlIdentifierCharSlow (const juce_wchar c) throw()
{
	return CharacterFunctions::isLetterOrDigit (c)
			|| c == '_' || c == '-' || c == ':' || c == '.';
}

inline bool XmlDocument::isXmlIdentifierChar (const juce_wchar c) const throw()
{
	return (c > 0 && c <= 127) ? identifierLookupTable [(int) c]
							   : isXmlIdentifierCharSlow (c);
}

XmlElement* XmlDocument::getDocumentElement (const bool onlyReadOuterDocumentElement)
{
	String textToParse (originalText);

	if (textToParse.isEmpty() && inputSource != 0)
	{
		ScopedPointer <InputStream> in (inputSource->createInputStream());

		if (in != 0)
		{
			MemoryBlock data;

			in->readIntoMemoryBlock (data, onlyReadOuterDocumentElement ? 8192 : -1);

			if (data.getSize() >= 2
				 && ((data[0] == (char)-2 && data[1] == (char)-1)
					  || (data[0] == (char)-1 && data[1] == (char)-2)))
			{
				textToParse = String::createStringFromData (static_cast <const char*> (data.getData()), (int) data.getSize());
			}
			else
			{
				textToParse = String::fromUTF8 (static_cast <const char*> (data.getData()), (int) data.getSize());
			}

			if (! onlyReadOuterDocumentElement)
				originalText = textToParse;
		}
	}

	input = textToParse;
	lastError = String::empty;
	errorOccurred = false;
	outOfData = false;
	needToLoadDTD = true;

	for (int i = 0; i < 128; ++i)
		identifierLookupTable[i] = isXmlIdentifierCharSlow ((juce_wchar) i);

	if (textToParse.isEmpty())
	{
		lastError = "not enough input";
	}
	else
	{
		skipHeader();

		if (input != 0)
		{
			ScopedPointer <XmlElement> result (readNextElement (! onlyReadOuterDocumentElement));

			if (! errorOccurred)
				return result.release();
		}
		else
		{
			lastError = "incorrect xml header";
		}
	}

	return 0;
}

const String& XmlDocument::getLastParseError() const throw()
{
	return lastError;
}

void XmlDocument::setLastError (const String& desc, const bool carryOn)
{
	lastError = desc;
	errorOccurred = ! carryOn;
}

const String XmlDocument::getFileContents (const String& filename) const
{
	if (inputSource != 0)
	{
		const ScopedPointer <InputStream> in (inputSource->createInputStreamFor (filename.trim().unquoted()));

		if (in != 0)
			return in->readEntireStreamAsString();
	}

	return String::empty;
}

juce_wchar XmlDocument::readNextChar() throw()
{
	if (*input != 0)
	{
		return *input++;
	}
	else
	{
		outOfData = true;
		return 0;
	}
}

int XmlDocument::findNextTokenLength() throw()
{
	int len = 0;
	juce_wchar c = *input;

	while (isXmlIdentifierChar (c))
		c = input [++len];

	return len;
}

void XmlDocument::skipHeader()
{
	const juce_wchar* const found = CharacterFunctions::find (input, T("<?xml"));

	if (found != 0)
	{
		input = found;
		input = CharacterFunctions::find (input, T("?>"));

		if (input == 0)
			return;

		input += 2;
	}

	skipNextWhiteSpace();
	const juce_wchar* docType = CharacterFunctions::find (input, T("<!DOCTYPE"));

	if (docType == 0)
		return;

	input = docType + 9;

	int n = 1;

	while (n > 0)
	{
		const juce_wchar c = readNextChar();

		if (outOfData)
			return;

		if (c == '<')
			++n;
		else if (c == '>')
			--n;
	}

	docType += 9;
	dtdText = String (docType, (int) (input - (docType + 1))).trim();
}

void XmlDocument::skipNextWhiteSpace()
{
	for (;;)
	{
		juce_wchar c = *input;

		while (CharacterFunctions::isWhitespace (c))
			c = *++input;

		if (c == 0)
		{
			outOfData = true;
			break;
		}
		else if (c == '<')
		{
			if (input[1] == '!'
				 && input[2] == '-'
				 && input[3] == '-')
			{
				const juce_wchar* const closeComment = CharacterFunctions::find (input, T("-->"));

				if (closeComment == 0)
				{
					outOfData = true;
					break;
				}

				input = closeComment + 3;
				continue;
			}
			else if (input[1] == '?')
			{
				const juce_wchar* const closeBracket = CharacterFunctions::find (input, T("?>"));

				if (closeBracket == 0)
				{
					outOfData = true;
					break;
				}

				input = closeBracket + 2;
				continue;
			}
		}

		break;
	}
}

void XmlDocument::readQuotedString (String& result)
{
	const juce_wchar quote = readNextChar();

	while (! outOfData)
	{
		const juce_wchar c = readNextChar();

		if (c == quote)
			break;

		if (c == '&')
		{
			--input;
			readEntity (result);
		}
		else
		{
			--input;
			const juce_wchar* const start = input;

			for (;;)
			{
				const juce_wchar character = *input;

				if (character == quote)
				{
					result.append (start, (int) (input - start));
					++input;

					return;
				}
				else if (character == '&')
				{
					result.append (start, (int) (input - start));
					break;
				}
				else if (character == 0)
				{
					outOfData = true;
					setLastError ("unmatched quotes", false);
					break;
				}

				++input;
			}
		}
	}
}

XmlElement* XmlDocument::readNextElement (const bool alsoParseSubElements)
{
	XmlElement* node = 0;

	skipNextWhiteSpace();
	if (outOfData)
		return 0;

	input = CharacterFunctions::find (input, T("<"));

	if (input != 0)
	{
		++input;
		int tagLen = findNextTokenLength();

		if (tagLen == 0)
		{
			// no tag name - but allow for a gap after the '<' before giving an error
			skipNextWhiteSpace();
			tagLen = findNextTokenLength();

			if (tagLen == 0)
			{
				setLastError ("tag name missing", false);
				return node;
			}
		}

		node = new XmlElement (String (input, tagLen));
		input += tagLen;
		XmlElement::XmlAttributeNode* lastAttribute = 0;

		// look for attributes
		for (;;)
		{
			skipNextWhiteSpace();

			const juce_wchar c = *input;

			// empty tag..
			if (c == '/' && input[1] == '>')
			{
				input += 2;
				break;
			}

			// parse the guts of the element..
			if (c == '>')
			{
				++input;
				skipNextWhiteSpace();

				if (alsoParseSubElements)
					readChildElements (node);

				break;
			}

			// get an attribute..
			if (isXmlIdentifierChar (c))
			{
				const int attNameLen = findNextTokenLength();

				if (attNameLen > 0)
				{
					const juce_wchar* attNameStart = input;
					input += attNameLen;

					skipNextWhiteSpace();

					if (readNextChar() == '=')
					{
						skipNextWhiteSpace();

						const juce_wchar nextChar = *input;

						if (nextChar == '"' || nextChar == '\'')
						{
							XmlElement::XmlAttributeNode* const newAtt
								= new XmlElement::XmlAttributeNode (String (attNameStart, attNameLen),
																	String::empty);

							readQuotedString (newAtt->value);

							if (lastAttribute == 0)
								node->attributes = newAtt;
							else
								lastAttribute->next = newAtt;

							lastAttribute = newAtt;

							continue;
						}
					}
				}
			}
			else
			{
				if (! outOfData)
					setLastError ("illegal character found in " + node->getTagName() + ": '" + c + "'", false);
			}

			break;
		}
	}

	return node;
}

void XmlDocument::readChildElements (XmlElement* parent)
{
	XmlElement* lastChildNode = 0;

	for (;;)
	{
		skipNextWhiteSpace();

		if (outOfData)
		{
			setLastError ("unmatched tags", false);
			break;
		}

		if (*input == '<')
		{
			if (input[1] == '/')
			{
				// our close tag..
				input = CharacterFunctions::find (input, T(">"));
				++input;
				break;
			}
			else if (input[1] == '!'
				  && input[2] == '['
				  && input[3] == 'C'
				  && input[4] == 'D'
				  && input[5] == 'A'
				  && input[6] == 'T'
				  && input[7] == 'A'
				  && input[8] == '[')
			{
				input += 9;
				const juce_wchar* const inputStart = input;

				int len = 0;

				for (;;)
				{
					if (*input == 0)
					{
						setLastError ("unterminated CDATA section", false);
						outOfData = true;
						break;
					}
					else if (input[0] == ']'
							  && input[1] == ']'
							  && input[2] == '>')
					{
						input += 3;
						break;
					}

					++input;
					++len;
				}

				XmlElement* const e = new XmlElement ((int) 0);
				e->setText (String (inputStart, len));

				if (lastChildNode != 0)
					lastChildNode->nextElement = e;
				else
					parent->addChildElement (e);

				lastChildNode = e;
			}
			else
			{
				// this is some other element, so parse and add it..
				XmlElement* const n = readNextElement (true);

				if (n != 0)
				{
					if (lastChildNode == 0)
						parent->addChildElement (n);
					else
						lastChildNode->nextElement = n;

					lastChildNode = n;
				}
				else
				{
					return;
				}
			}
		}
		else
		{
			// read character block..
			XmlElement* const e = new XmlElement ((int)0);

			if (lastChildNode != 0)
				lastChildNode->nextElement = e;
			else
				parent->addChildElement (e);

			lastChildNode = e;

			String textElementContent;

			for (;;)
			{
				const juce_wchar c = *input;

				if (c == '<')
					break;

				if (c == 0)
				{
					setLastError ("unmatched tags", false);
					outOfData = true;
					return;
				}

				if (c == '&')
				{
					String entity;
					readEntity (entity);

					if (entity.startsWithChar ('<') && entity [1] != 0)
					{
						const juce_wchar* const oldInput = input;
						const bool oldOutOfData = outOfData;

						input = entity;
						outOfData = false;

						for (;;)
						{
							XmlElement* const n = readNextElement (true);

							if (n == 0)
								break;

							if (lastChildNode == 0)
								parent->addChildElement (n);
							else
								lastChildNode->nextElement = n;

							lastChildNode = n;
						}

						input = oldInput;
						outOfData = oldOutOfData;
					}
					else
					{
						textElementContent += entity;
					}
				}
				else
				{
					const juce_wchar* start = input;
					int len = 0;

					for (;;)
					{
						const juce_wchar nextChar = *input;

						if (nextChar == '<' || nextChar == '&')
						{
							break;
						}
						else if (nextChar == 0)
						{
							setLastError ("unmatched tags", false);
							outOfData = true;
							return;
						}

						++input;
						++len;
					}

					textElementContent.append (start, len);
				}
			}

			if (ignoreEmptyTextElements ? textElementContent.containsNonWhitespaceChars()
										: textElementContent.isNotEmpty())
				e->setText (textElementContent);
		}
	}
}

void XmlDocument::readEntity (String& result)
{
	// skip over the ampersand
	++input;

	if (CharacterFunctions::compareIgnoreCase (input, T("amp;"), 4) == 0)
	{
		input += 4;
		result += '&';
	}
	else if (CharacterFunctions::compareIgnoreCase (input, T("quot;"), 5) == 0)
	{
		input += 5;
		result += '"';
	}
	else if (CharacterFunctions::compareIgnoreCase (input, T("apos;"), 5) == 0)
	{
		input += 5;
		result += '\'';
	}
	else if (CharacterFunctions::compareIgnoreCase (input, T("lt;"), 3) == 0)
	{
		input += 3;
		result += '<';
	}
	else if (CharacterFunctions::compareIgnoreCase (input, T("gt;"), 3) == 0)
	{
		input += 3;
		result += '>';
	}
	else if (*input == '#')
	{
		int charCode = 0;
		++input;

		if (*input == 'x' || *input == 'X')
		{
			++input;
			int numChars = 0;

			while (input[0] != ';')
			{
				const int hexValue = CharacterFunctions::getHexDigitValue (input[0]);

				if (hexValue < 0 || ++numChars > 8)
				{
					setLastError ("illegal escape sequence", true);
					break;
				}

				charCode = (charCode << 4) | hexValue;
				++input;
			}

			++input;
		}
		else if (input[0] >= '0' && input[0] <= '9')
		{
			int numChars = 0;

			while (input[0] != ';')
			{
				if (++numChars > 12)
				{
					setLastError ("illegal escape sequence", true);
					break;
				}

				charCode = charCode * 10 + (input[0] - '0');
				++input;
			}

			++input;
		}
		else
		{
			setLastError ("illegal escape sequence", true);
			result += '&';
			return;
		}

		result << (juce_wchar) charCode;
	}
	else
	{
		const juce_wchar* const entityNameStart = input;
		const juce_wchar* const closingSemiColon = CharacterFunctions::find (input, T(";"));

		if (closingSemiColon == 0)
		{
			outOfData = true;
			result += '&';
		}
		else
		{
			input = closingSemiColon + 1;

			result += expandExternalEntity (String (entityNameStart,
													(int) (closingSemiColon - entityNameStart)));
		}
	}
}

const String XmlDocument::expandEntity (const String& ent)
{
	if (ent.equalsIgnoreCase ("amp"))
		return String::charToString ('&');

	if (ent.equalsIgnoreCase ("quot"))
		return String::charToString ('"');

	if (ent.equalsIgnoreCase ("apos"))
		return String::charToString ('\'');

	if (ent.equalsIgnoreCase ("lt"))
		return String::charToString ('<');

	if (ent.equalsIgnoreCase ("gt"))
		return String::charToString ('>');

	if (ent[0] == '#')
	{
		if (ent[1] == 'x' || ent[1] == 'X')
			return String::charToString (static_cast <juce_wchar> (ent.substring (2).getHexValue32()));

		if (ent[1] >= '0' && ent[1] <= '9')
			return String::charToString (static_cast <juce_wchar> (ent.substring (1).getIntValue()));

		setLastError ("illegal escape sequence", false);
		return String::charToString ('&');
	}

	return expandExternalEntity (ent);
}

const String XmlDocument::expandExternalEntity (const String& entity)
{
	if (needToLoadDTD)
	{
		if (dtdText.isNotEmpty())
		{
			while (dtdText.endsWithChar ('>'))
				dtdText = dtdText.dropLastCharacters (1);

			tokenisedDTD.addTokens (dtdText, true);

			if (tokenisedDTD [tokenisedDTD.size() - 2].equalsIgnoreCase ("system")
				 && tokenisedDTD [tokenisedDTD.size() - 1].isQuotedString())
			{
				const String fn (tokenisedDTD [tokenisedDTD.size() - 1]);

				tokenisedDTD.clear();
				tokenisedDTD.addTokens (getFileContents (fn), true);
			}
			else
			{
				tokenisedDTD.clear();
				const int openBracket = dtdText.indexOfChar ('[');

				if (openBracket > 0)
				{
					const int closeBracket = dtdText.lastIndexOfChar (']');

					if (closeBracket > openBracket)
						tokenisedDTD.addTokens (dtdText.substring (openBracket + 1,
																   closeBracket), true);
				}
			}

			for (int i = tokenisedDTD.size(); --i >= 0;)
			{
				if (tokenisedDTD[i].startsWithChar ('%')
					 && tokenisedDTD[i].endsWithChar (';'))
				{
					const String parsed (getParameterEntity (tokenisedDTD[i].substring (1, tokenisedDTD[i].length() - 1)));
					StringArray newToks;
					newToks.addTokens (parsed, true);

					tokenisedDTD.remove (i);

					for (int j = newToks.size(); --j >= 0;)
						tokenisedDTD.insert (i, newToks[j]);
				}
			}
		}

		needToLoadDTD = false;
	}

	for (int i = 0; i < tokenisedDTD.size(); ++i)
	{
		if (tokenisedDTD[i] == entity)
		{
			if (tokenisedDTD[i - 1].equalsIgnoreCase ("<!entity"))
			{
				String ent (tokenisedDTD [i + 1]);

				while (ent.endsWithChar ('>'))
					ent = ent.dropLastCharacters (1);

				ent = ent.trim().unquoted();

				// check for sub-entities..
				int ampersand = ent.indexOfChar ('&');

				while (ampersand >= 0)
				{
					const int semiColon = ent.indexOf (i + 1, ";");

					if (semiColon < 0)
					{
						setLastError ("entity without terminating semi-colon", false);
						break;
					}

					const String resolved (expandEntity (ent.substring (i + 1, semiColon)));

					ent = ent.substring (0, ampersand)
						   + resolved
						   + ent.substring (semiColon + 1);

					ampersand = ent.indexOfChar (semiColon + 1, '&');
				}

				return ent;
			}
		}
	}

	setLastError ("unknown entity", true);

	return entity;
}

const String XmlDocument::getParameterEntity (const String& entity)
{
	for (int i = 0; i < tokenisedDTD.size(); ++i)
	{
		if (tokenisedDTD[i] == entity)
		{
			if (tokenisedDTD [i - 1] == "%"
				&& tokenisedDTD [i - 2].equalsIgnoreCase ("<!entity"))
			{
				String ent (tokenisedDTD [i + 1]);

				while (ent.endsWithChar ('>'))
					ent = ent.dropLastCharacters (1);

				if (ent.equalsIgnoreCase ("system"))
				{
					String filename (tokenisedDTD [i + 2]);

					while (filename.endsWithChar ('>'))
						filename = filename.dropLastCharacters (1);

					return getFileContents (filename);
				}
				else
				{
					return ent.trim().unquoted();
				}
			}
		}
	}

	return entity;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_XmlDocument.cpp ***/


/*** Start of inlined file: juce_XmlElement.cpp ***/
BEGIN_JUCE_NAMESPACE

XmlElement::XmlAttributeNode::XmlAttributeNode (const XmlAttributeNode& other) throw()
	: name (other.name),
	  value (other.value),
	  next (0)
{
}

XmlElement::XmlAttributeNode::XmlAttributeNode (const String& name_, const String& value_) throw()
	: name (name_),
	  value (value_),
	  next (0)
{
}

XmlElement::XmlElement (const String& tagName_) throw()
	: tagName (tagName_),
	  firstChildElement (0),
	  nextElement (0),
	  attributes (0)
{
	// the tag name mustn't be empty, or it'll look like a text element!
	jassert (tagName_.containsNonWhitespaceChars())

	// The tag can't contain spaces or other characters that would create invalid XML!
	jassert (! tagName_.containsAnyOf (" <>/&"));
}

XmlElement::XmlElement (int /*dummy*/) throw()
	: firstChildElement (0),
	  nextElement (0),
	  attributes (0)
{
}

XmlElement::XmlElement (const XmlElement& other) throw()
	: tagName (other.tagName),
	  firstChildElement (0),
	  nextElement (0),
	  attributes (0)
{
	copyChildrenAndAttributesFrom (other);
}

XmlElement& XmlElement::operator= (const XmlElement& other) throw()
{
	if (this != &other)
	{
		removeAllAttributes();
		deleteAllChildElements();

		tagName = other.tagName;

		copyChildrenAndAttributesFrom (other);
	}

	return *this;
}

void XmlElement::copyChildrenAndAttributesFrom (const XmlElement& other) throw()
{
	XmlElement* child = other.firstChildElement;
	XmlElement* lastChild = 0;

	while (child != 0)
	{
		XmlElement* const copiedChild = new XmlElement (*child);

		if (lastChild != 0)
			lastChild->nextElement = copiedChild;
		else
			firstChildElement = copiedChild;

		lastChild = copiedChild;
		child = child->nextElement;
	}

	const XmlAttributeNode* att = other.attributes;
	XmlAttributeNode* lastAtt = 0;

	while (att != 0)
	{
		XmlAttributeNode* const newAtt = new XmlAttributeNode (*att);

		if (lastAtt != 0)
			lastAtt->next = newAtt;
		else
			attributes = newAtt;

		lastAtt = newAtt;
		att = att->next;
	}
}

XmlElement::~XmlElement() throw()
{
	XmlElement* child = firstChildElement;

	while (child != 0)
	{
		XmlElement* const nextChild = child->nextElement;
		delete child;
		child = nextChild;
	}

	XmlAttributeNode* att = attributes;

	while (att != 0)
	{
		XmlAttributeNode* const nextAtt = att->next;
		delete att;
		att = nextAtt;
	}
}

namespace XmlOutputFunctions
{

	static bool isLegalXmlChar (const uint32 c) throw()
	{
		static const unsigned char legalChars[] = { 0, 0, 0, 0, 171, 255, 255, 175, 255, 255, 255, 191, 254, 255, 255, 111 };

		return c < sizeof (legalChars) * 8
				 && (legalChars [c >> 3] & (1 << (c & 7))) != 0;
	}

	static void escapeIllegalXmlChars (OutputStream& outputStream, const String& text, const bool changeNewLines)
	{
		const juce_wchar* t = text;

		for (;;)
		{
			const juce_wchar character = *t++;

			if (character == 0)
			{
				break;
			}
			else if (isLegalXmlChar ((uint32) character))
			{
				outputStream << (char) character;
			}
			else
			{
				switch (character)
				{
				case '&':   outputStream << "&amp;"; break;
				case '"':   outputStream << "&quot;"; break;
				case '>':   outputStream << "&gt;"; break;
				case '<':   outputStream << "&lt;"; break;

				case '\n':
					if (changeNewLines)
						outputStream << "&#10;";
					else
						outputStream << (char) character;

					break;

				case '\r':
					if (changeNewLines)
						outputStream << "&#13;";
					else
						outputStream << (char) character;

					break;

				default:
					outputStream << "&#" << ((int) (unsigned int) character) << ';';
					break;
				}
			}
		}
	}

	static void writeSpaces (OutputStream& out, int numSpaces)
	{
		if (numSpaces > 0)
		{
			const char* const blanks = "                        ";
			const int blankSize = (int) sizeof (blanks) - 1;

			while (numSpaces > blankSize)
			{
				out.write (blanks, blankSize);
				numSpaces -= blankSize;
			}

			out.write (blanks, numSpaces);
		}
	}
}

void XmlElement::writeElementAsText (OutputStream& outputStream,
									 const int indentationLevel,
									 const int lineWrapLength) const
{
	using namespace XmlOutputFunctions;
	writeSpaces (outputStream, indentationLevel);

	if (! isTextElement())
	{
		outputStream.writeByte ('<');
		outputStream << tagName;

		const int attIndent = indentationLevel + tagName.length() + 1;
		int lineLen = 0;

		const XmlAttributeNode* att = attributes;
		while (att != 0)
		{
			if (lineLen > lineWrapLength && indentationLevel >= 0)
			{
				outputStream.write ("\r\n", 2);
				writeSpaces (outputStream, attIndent);
				lineLen = 0;
			}

			const int64 startPos = outputStream.getPosition();
			outputStream.writeByte (' ');
			outputStream << att->name;
			outputStream.write ("=\"", 2);
			escapeIllegalXmlChars (outputStream, att->value, true);
			outputStream.writeByte ('"');
			lineLen += (int) (outputStream.getPosition() - startPos);

			att = att->next;
		}

		if (firstChildElement != 0)
		{
			XmlElement* child = firstChildElement;

			if (child->nextElement == 0 && child->isTextElement())
			{
				outputStream.writeByte ('>');
				escapeIllegalXmlChars (outputStream, child->getText(), false);
			}
			else
			{
				if (indentationLevel >= 0)
					outputStream.write (">\r\n", 3);
				else
					outputStream.writeByte ('>');

				bool lastWasTextNode = false;

				while (child != 0)
				{
					if (child->isTextElement())
					{
						if ((! lastWasTextNode) && (indentationLevel >= 0))
							writeSpaces (outputStream, indentationLevel + 2);

						escapeIllegalXmlChars (outputStream, child->getText(), false);
						lastWasTextNode = true;
					}
					else
					{
						if (indentationLevel >= 0)
						{
							if (lastWasTextNode)
								outputStream.write ("\r\n", 2);

							child->writeElementAsText (outputStream, indentationLevel + 2, lineWrapLength);
						}
						else
						{
							child->writeElementAsText (outputStream, indentationLevel, lineWrapLength);
						}

						lastWasTextNode = false;
					}

					child = child->nextElement;
				}

				if (indentationLevel >= 0)
				{
					if (lastWasTextNode)
						outputStream.write ("\r\n", 2);

					writeSpaces (outputStream, indentationLevel);
				}
			}

			outputStream.write ("</", 2);
			outputStream << tagName;

			if (indentationLevel >= 0)
				outputStream.write (">\r\n", 3);
			else
				outputStream.writeByte ('>');
		}
		else
		{
			if (indentationLevel >= 0)
				outputStream.write ("/>\r\n", 4);
			else
				outputStream.write ("/>", 2);
		}
	}
	else
	{
		if (indentationLevel >= 0)
			writeSpaces (outputStream, indentationLevel + 2);

		escapeIllegalXmlChars (outputStream, getText(), false);
	}
}

const String XmlElement::createDocument (const String& dtdToUse,
										 const bool allOnOneLine,
										 const bool includeXmlHeader,
										 const String& encodingType,
										 const int lineWrapLength) const
{
	MemoryOutputStream mem (2048, 4096);
	writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength);

	return mem.toUTF8();
}

void XmlElement::writeToStream (OutputStream& output,
								const String& dtdToUse,
								const bool allOnOneLine,
								const bool includeXmlHeader,
								const String& encodingType,
								const int lineWrapLength) const
{
	if (includeXmlHeader)
		output << "<?xml version=\"1.0\" encoding=\"" << encodingType
			   << (allOnOneLine ? "\"?> " : "\"?>\r\n\r\n");

	if (dtdToUse.isNotEmpty())
		output << dtdToUse << (allOnOneLine ? " " : "\r\n");

	writeElementAsText (output, allOnOneLine ? -1 : 0, lineWrapLength);
}

bool XmlElement::writeToFile (const File& file,
							  const String& dtdToUse,
							  const String& encodingType,
							  const int lineWrapLength) const
{
	if (file.hasWriteAccess())
	{
		TemporaryFile tempFile (file);
		ScopedPointer <FileOutputStream> out (tempFile.getFile().createOutputStream());

		if (out != 0)
		{
			writeToStream (*out, dtdToUse, false, true, encodingType, lineWrapLength);
			out = 0;

			return tempFile.overwriteTargetFileWithTemporary();
		}
	}

	return false;
}

bool XmlElement::hasTagName (const String& tagNameWanted) const throw()
{
#ifdef JUCE_DEBUG
	// if debugging, check that the case is actually the same, because
	// valid xml is case-sensitive, and although this lets it pass, it's
	// better not to..
	if (tagName.equalsIgnoreCase (tagNameWanted))
	{
		jassert (tagName == tagNameWanted);
		return true;
	}
	else
	{
		return false;
	}
#else
	return tagName.equalsIgnoreCase (tagNameWanted);
#endif
}

XmlElement* XmlElement::getNextElementWithTagName (const String& requiredTagName) const
{
	XmlElement* e = nextElement;

	while (e != 0 && ! e->hasTagName (requiredTagName))
		e = e->nextElement;

	return e;
}

int XmlElement::getNumAttributes() const throw()
{
	const XmlAttributeNode* att = attributes;
	int count = 0;

	while (att != 0)
	{
		att = att->next;
		++count;
	}

	return count;
}

const String& XmlElement::getAttributeName (const int index) const throw()
{
	const XmlAttributeNode* att = attributes;
	int count = 0;

	while (att != 0)
	{
		if (count == index)
			return att->name;

		att = att->next;
		++count;
	}

	return String::empty;
}

const String& XmlElement::getAttributeValue (const int index) const throw()
{
	const XmlAttributeNode* att = attributes;
	int count = 0;

	while (att != 0)
	{
		if (count == index)
			return att->value;

		att = att->next;
		++count;
	}

	return String::empty;
}

bool XmlElement::hasAttribute (const String& attributeName) const throw()
{
	const XmlAttributeNode* att = attributes;

	while (att != 0)
	{
		if (att->name.equalsIgnoreCase (attributeName))
			return true;

		att = att->next;
	}

	return false;
}

const String& XmlElement::getStringAttribute (const String& attributeName) const throw()
{
	const XmlAttributeNode* att = attributes;

	while (att != 0)
	{
		if (att->name.equalsIgnoreCase (attributeName))
			return att->value;

		att = att->next;
	}

	return String::empty;
}

const String XmlElement::getStringAttribute (const String& attributeName, const String& defaultReturnValue) const
{
	const XmlAttributeNode* att = attributes;

	while (att != 0)
	{
		if (att->name.equalsIgnoreCase (attributeName))
			return att->value;

		att = att->next;
	}

	return defaultReturnValue;
}

int XmlElement::getIntAttribute (const String& attributeName, const int defaultReturnValue) const
{
	const XmlAttributeNode* att = attributes;

	while (att != 0)
	{
		if (att->name.equalsIgnoreCase (attributeName))
			return att->value.getIntValue();

		att = att->next;
	}

	return defaultReturnValue;
}

double XmlElement::getDoubleAttribute (const String& attributeName, const double defaultReturnValue) const
{
	const XmlAttributeNode* att = attributes;

	while (att != 0)
	{
		if (att->name.equalsIgnoreCase (attributeName))
			return att->value.getDoubleValue();

		att = att->next;
	}

	return defaultReturnValue;
}

bool XmlElement::getBoolAttribute (const String& attributeName, const bool defaultReturnValue) const
{
	const XmlAttributeNode* att = attributes;

	while (att != 0)
	{
		if (att->name.equalsIgnoreCase (attributeName))
		{
			juce_wchar firstChar = att->value[0];

			if (CharacterFunctions::isWhitespace (firstChar))
				firstChar = att->value.trimStart() [0];

			return firstChar == '1'
				|| firstChar == 't'
				|| firstChar == 'y'
				|| firstChar == 'T'
				|| firstChar == 'Y';
		}

		att = att->next;
	}

	return defaultReturnValue;
}

bool XmlElement::compareAttribute (const String& attributeName,
								   const String& stringToCompareAgainst,
								   const bool ignoreCase) const throw()
{
	const XmlAttributeNode* att = attributes;

	while (att != 0)
	{
		if (att->name.equalsIgnoreCase (attributeName))
		{
			if (ignoreCase)
				return att->value.equalsIgnoreCase (stringToCompareAgainst);
			else
				return att->value == stringToCompareAgainst;
		}

		att = att->next;
	}

	return false;
}

void XmlElement::setAttribute (const String& attributeName, const String& value)
{
#ifdef JUCE_DEBUG
	// check the identifier being passed in is legal..
	const juce_wchar* t = attributeName;
	while (*t != 0)
	{
		jassert (CharacterFunctions::isLetterOrDigit (*t)
				  || *t == '_'
				  || *t == '-'
				  || *t == ':');
		++t;
	}
#endif

	if (attributes == 0)
	{
		attributes = new XmlAttributeNode (attributeName, value);
	}
	else
	{
		XmlAttributeNode* att = attributes;

		for (;;)
		{
			if (att->name.equalsIgnoreCase (attributeName))
			{
				att->value = value;
				break;
			}
			else if (att->next == 0)
			{
				att->next = new XmlAttributeNode (attributeName, value);
				break;
			}

			att = att->next;
		}
	}
}

void XmlElement::setAttribute (const String& attributeName, const int number)
{
	setAttribute (attributeName, String (number));
}

void XmlElement::setAttribute (const String& attributeName, const double number)
{
	setAttribute (attributeName, String (number));
}

void XmlElement::removeAttribute (const String& attributeName) throw()
{
	XmlAttributeNode* att = attributes;
	XmlAttributeNode* lastAtt = 0;

	while (att != 0)
	{
		if (att->name.equalsIgnoreCase (attributeName))
		{
			if (lastAtt == 0)
				attributes = att->next;
			else
				lastAtt->next = att->next;

			delete att;
			break;
		}

		lastAtt = att;
		att = att->next;
	}
}

void XmlElement::removeAllAttributes() throw()
{
	while (attributes != 0)
	{
		XmlAttributeNode* const nextAtt = attributes->next;
		delete attributes;
		attributes = nextAtt;
	}
}

int XmlElement::getNumChildElements() const throw()
{
	int count = 0;
	const XmlElement* child = firstChildElement;

	while (child != 0)
	{
		++count;
		child = child->nextElement;
	}

	return count;
}

XmlElement* XmlElement::getChildElement (const int index) const throw()
{
	int count = 0;
	XmlElement* child = firstChildElement;

	while (child != 0 && count < index)
	{
		child = child->nextElement;
		++count;
	}

	return child;
}

XmlElement* XmlElement::getChildByName (const String& childName) const throw()
{
	XmlElement* child = firstChildElement;

	while (child != 0)
	{
		if (child->hasTagName (childName))
			break;

		child = child->nextElement;
	}

	return child;
}

void XmlElement::addChildElement (XmlElement* const newNode) throw()
{
	if (newNode != 0)
	{
		if (firstChildElement == 0)
		{
			firstChildElement = newNode;
		}
		else
		{
			XmlElement* child = firstChildElement;

			while (child->nextElement != 0)
				child = child->nextElement;

			child->nextElement = newNode;

			// if this is non-zero, then something's probably
			// gone wrong..
			jassert (newNode->nextElement == 0);
		}
	}
}

void XmlElement::insertChildElement (XmlElement* const newNode,
									 int indexToInsertAt) throw()
{
	if (newNode != 0)
	{
		removeChildElement (newNode, false);

		if (indexToInsertAt == 0)
		{
			newNode->nextElement = firstChildElement;
			firstChildElement = newNode;
		}
		else
		{
			if (firstChildElement == 0)
			{
				firstChildElement = newNode;
			}
			else
			{
				if (indexToInsertAt < 0)
					indexToInsertAt = std::numeric_limits<int>::max();

				XmlElement* child = firstChildElement;

				while (child->nextElement != 0 && --indexToInsertAt > 0)
					child = child->nextElement;

				newNode->nextElement = child->nextElement;
				child->nextElement = newNode;
			}
		}
	}
}

XmlElement* XmlElement::createNewChildElement (const String& tagName)
{
	XmlElement* const newElement = new XmlElement (tagName);
	addChildElement (newElement);
	return newElement;
}

bool XmlElement::replaceChildElement (XmlElement* const currentChildElement,
									  XmlElement* const newNode) throw()
{
	if (newNode != 0)
	{
		XmlElement* child = firstChildElement;
		XmlElement* previousNode = 0;

		while (child != 0)
		{
			if (child == currentChildElement)
			{
				if (child != newNode)
				{
					if (previousNode == 0)
						firstChildElement = newNode;
					else
						previousNode->nextElement = newNode;

					newNode->nextElement = child->nextElement;

					delete child;
				}

				return true;
			}

			previousNode = child;
			child = child->nextElement;
		}
	}

	return false;
}

void XmlElement::removeChildElement (XmlElement* const childToRemove,
									 const bool shouldDeleteTheChild) throw()
{
	if (childToRemove != 0)
	{
		if (firstChildElement == childToRemove)
		{
			firstChildElement = childToRemove->nextElement;
			childToRemove->nextElement = 0;
		}
		else
		{
			XmlElement* child = firstChildElement;
			XmlElement* last = 0;

			while (child != 0)
			{
				if (child == childToRemove)
				{
					if (last == 0)
						firstChildElement = child->nextElement;
					else
						last->nextElement = child->nextElement;

					childToRemove->nextElement = 0;
					break;
				}

				last = child;
				child = child->nextElement;
			}
		}

		if (shouldDeleteTheChild)
			delete childToRemove;
	}
}

bool XmlElement::isEquivalentTo (const XmlElement* const other,
								 const bool ignoreOrderOfAttributes) const throw()
{
	if (this != other)
	{
		if (other == 0 || tagName != other->tagName)
		{
			return false;
		}

		if (ignoreOrderOfAttributes)
		{
			int totalAtts = 0;
			const XmlAttributeNode* att = attributes;

			while (att != 0)
			{
				if (! other->compareAttribute (att->name, att->value))
					return false;

				att = att->next;
				++totalAtts;
			}

			if (totalAtts != other->getNumAttributes())
				return false;
		}
		else
		{
			const XmlAttributeNode* thisAtt = attributes;
			const XmlAttributeNode* otherAtt = other->attributes;

			for (;;)
			{
				if (thisAtt == 0 || otherAtt == 0)
				{
					if (thisAtt == otherAtt) // both 0, so it's a match
						break;

					return false;
				}

				if (thisAtt->name != otherAtt->name
					 || thisAtt->value != otherAtt->value)
				{
					return false;
				}

				thisAtt = thisAtt->next;
				otherAtt = otherAtt->next;
			}
		}

		const XmlElement* thisChild = firstChildElement;
		const XmlElement* otherChild = other->firstChildElement;

		for (;;)
		{
			if (thisChild == 0 || otherChild == 0)
			{
				if (thisChild == otherChild) // both 0, so it's a match
					break;

				return false;
			}

			if (! thisChild->isEquivalentTo (otherChild, ignoreOrderOfAttributes))
				return false;

			thisChild = thisChild->nextElement;
			otherChild = otherChild->nextElement;
		}
	}

	return true;
}

void XmlElement::deleteAllChildElements() throw()
{
	while (firstChildElement != 0)
	{
		XmlElement* const nextChild = firstChildElement->nextElement;
		delete firstChildElement;
		firstChildElement = nextChild;
	}
}

void XmlElement::deleteAllChildElementsWithTagName (const String& name) throw()
{
	XmlElement* child = firstChildElement;

	while (child != 0)
	{
		if (child->hasTagName (name))
		{
			XmlElement* const nextChild = child->nextElement;
			removeChildElement (child, true);
			child = nextChild;
		}
		else
		{
			child = child->nextElement;
		}
	}
}

bool XmlElement::containsChildElement (const XmlElement* const possibleChild) const throw()
{
	const XmlElement* child = firstChildElement;

	while (child != 0)
	{
		if (child == possibleChild)
			return true;

		child = child->nextElement;
	}

	return false;
}

XmlElement* XmlElement::findParentElementOf (const XmlElement* const elementToLookFor) throw()
{
	if (this == elementToLookFor || elementToLookFor == 0)
		return 0;

	XmlElement* child = firstChildElement;

	while (child != 0)
	{
		if (elementToLookFor == child)
			return this;

		XmlElement* const found = child->findParentElementOf (elementToLookFor);

		if (found != 0)
			return found;

		child = child->nextElement;
	}

	return 0;
}

void XmlElement::getChildElementsAsArray (XmlElement** elems) const throw()
{
	XmlElement* e = firstChildElement;

	while (e != 0)
	{
		*elems++ = e;
		e = e->nextElement;
	}
}

void XmlElement::reorderChildElements (XmlElement** const elems, const int num) throw()
{
	XmlElement* e = firstChildElement = elems[0];

	for (int i = 1; i < num; ++i)
	{
		e->nextElement = elems[i];
		e = e->nextElement;
	}

	e->nextElement = 0;
}

bool XmlElement::isTextElement() const throw()
{
	return tagName.isEmpty();
}

static const juce_wchar* const juce_xmltextContentAttributeName = T("text");

const String XmlElement::getText() const throw()
{
	jassert (isTextElement());  // you're trying to get the text from an element that
								// isn't actually a text element.. If this contains text sub-nodes, you
								// probably want to use getAllSubText instead.

	return getStringAttribute (juce_xmltextContentAttributeName);
}

void XmlElement::setText (const String& newText) throw()
{
	if (isTextElement())
	{
		setAttribute (juce_xmltextContentAttributeName, newText);
	}
	else
	{
		jassertfalse // you can only change the text in a text element, not a normal one.
	}
}

const String XmlElement::getAllSubText() const throw()
{
	String result;
	String::Concatenator concatenator (result);
	const XmlElement* child = firstChildElement;

	while (child != 0)
	{
		if (child->isTextElement())
			concatenator.append (child->getText());

		child = child->nextElement;
	}

	return result;
}

const String XmlElement::getChildElementAllSubText (const String& childTagName,
													const String& defaultReturnValue) const throw()
{
	const XmlElement* const child = getChildByName (childTagName);

	if (child != 0)
		return child->getAllSubText();

	return defaultReturnValue;
}

XmlElement* XmlElement::createTextElement (const String& text) throw()
{
	XmlElement* const e = new XmlElement ((int) 0);
	e->setAttribute (juce_xmltextContentAttributeName, text);
	return e;
}

void XmlElement::addTextElement (const String& text) throw()
{
	addChildElement (createTextElement (text));
}

void XmlElement::deleteAllTextElements() throw()
{
	XmlElement* child = firstChildElement;

	while (child != 0)
	{
		XmlElement* const next = child->nextElement;

		if (child->isTextElement())
			removeChildElement (child, true);

		child = next;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_XmlElement.cpp ***/


/*** Start of inlined file: juce_ReadWriteLock.cpp ***/
BEGIN_JUCE_NAMESPACE

ReadWriteLock::ReadWriteLock() throw()
	: numWaitingWriters (0),
	  numWriters (0),
	  writerThreadId (0)
{
}

ReadWriteLock::~ReadWriteLock() throw()
{
	jassert (readerThreads.size() == 0);
	jassert (numWriters == 0);
}

void ReadWriteLock::enterRead() const throw()
{
	const Thread::ThreadID threadId = Thread::getCurrentThreadId();
	const ScopedLock sl (accessLock);

	for (;;)
	{
		jassert (readerThreads.size() % 2 == 0);

		int i;
		for (i = 0; i < readerThreads.size(); i += 2)
			if (readerThreads.getUnchecked(i) == threadId)
				break;

		if (i < readerThreads.size()
			  || numWriters + numWaitingWriters == 0
			  || (threadId == writerThreadId && numWriters > 0))
		{
			if (i < readerThreads.size())
			{
				readerThreads.set (i + 1, (Thread::ThreadID) (1 + (pointer_sized_int) readerThreads.getUnchecked (i + 1)));
			}
			else
			{
				readerThreads.add (threadId);
				readerThreads.add ((Thread::ThreadID) 1);
			}

			return;
		}

		const ScopedUnlock ul (accessLock);
		waitEvent.wait (100);
	}
}

void ReadWriteLock::exitRead() const throw()
{
	const Thread::ThreadID threadId = Thread::getCurrentThreadId();
	const ScopedLock sl (accessLock);

	for (int i = 0; i < readerThreads.size(); i += 2)
	{
		if (readerThreads.getUnchecked(i) == threadId)
		{
			const pointer_sized_int newCount = ((pointer_sized_int) readerThreads.getUnchecked (i + 1)) - 1;

			if (newCount == 0)
			{
				readerThreads.removeRange (i, 2);
				waitEvent.signal();
			}
			else
			{
				readerThreads.set (i + 1, (Thread::ThreadID) newCount);
			}

			return;
		}
	}

	jassertfalse // unlocking a lock that wasn't locked..
}

void ReadWriteLock::enterWrite() const throw()
{
	const Thread::ThreadID threadId = Thread::getCurrentThreadId();
	const ScopedLock sl (accessLock);

	for (;;)
	{
		if (readerThreads.size() + numWriters == 0
			 || threadId == writerThreadId
			 || (readerThreads.size() == 2
				  && readerThreads.getUnchecked(0) == threadId))
		{
			writerThreadId = threadId;
			++numWriters;
			break;
		}

		++numWaitingWriters;
		accessLock.exit();
		waitEvent.wait (100);
		accessLock.enter();
		--numWaitingWriters;
	}
}

bool ReadWriteLock::tryEnterWrite() const throw()
{
	const Thread::ThreadID threadId = Thread::getCurrentThreadId();
	const ScopedLock sl (accessLock);

	if (readerThreads.size() + numWriters == 0
		 || threadId == writerThreadId
		 || (readerThreads.size() == 2
			  && readerThreads.getUnchecked(0) == threadId))
	{
		writerThreadId = threadId;
		++numWriters;
		return true;
	}

	return false;
}

void ReadWriteLock::exitWrite() const throw()
{
	const ScopedLock sl (accessLock);

	// check this thread actually had the lock..
	jassert (numWriters > 0 && writerThreadId == Thread::getCurrentThreadId());

	if (--numWriters == 0)
	{
		writerThreadId = 0;
		waitEvent.signal();
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ReadWriteLock.cpp ***/


/*** Start of inlined file: juce_Thread.cpp ***/
BEGIN_JUCE_NAMESPACE

// these functions are implemented in the platform-specific code.
void* juce_createThread (void* userData);
void juce_killThread (void* handle);
bool juce_setThreadPriority (void* handle, int priority);
void juce_setCurrentThreadName (const String& name);
#if JUCE_WIN32
void juce_CloseThreadHandle (void* handle);
#endif

void Thread::threadEntryPoint (Thread* const thread)
{
	{
		const ScopedLock sl (runningThreadsLock);
		runningThreads.add (thread);
	}

	JUCE_TRY
	{
		thread->threadId_ = Thread::getCurrentThreadId();

		if (thread->threadName_.isNotEmpty())
			juce_setCurrentThreadName (thread->threadName_);

		if (thread->startSuspensionEvent_.wait (10000))
		{
			if (thread->affinityMask_ != 0)
				setCurrentThreadAffinityMask (thread->affinityMask_);

			thread->run();
		}
	}
	JUCE_CATCH_ALL_ASSERT

	{
		const ScopedLock sl (runningThreadsLock);

		jassert (runningThreads.contains (thread));
		runningThreads.removeValue (thread);
	}

#if JUCE_WIN32
	juce_CloseThreadHandle (thread->threadHandle_);
#endif

	thread->threadHandle_ = 0;
	thread->threadId_ = 0;
}

// used to wrap the incoming call from the platform-specific code
void JUCE_API juce_threadEntryPoint (void* userData)
{
	Thread::threadEntryPoint (static_cast <Thread*> (userData));
}

Thread::Thread (const String& threadName)
	: threadName_ (threadName),
	  threadHandle_ (0),
	  threadPriority_ (5),
	  threadId_ (0),
	  affinityMask_ (0),
	  threadShouldExit_ (false)
{
}

Thread::~Thread()
{
	stopThread (100);
}

void Thread::startThread()
{
	const ScopedLock sl (startStopLock);

	threadShouldExit_ = false;

	if (threadHandle_ == 0)
	{
		threadHandle_ = juce_createThread (this);
		juce_setThreadPriority (threadHandle_, threadPriority_);
		startSuspensionEvent_.signal();
	}
}

void Thread::startThread (const int priority)
{
	const ScopedLock sl (startStopLock);

	if (threadHandle_ == 0)
	{
		threadPriority_ = priority;
		startThread();
	}
	else
	{
		setPriority (priority);
	}
}

bool Thread::isThreadRunning() const
{
	return threadHandle_ != 0;
}

void Thread::signalThreadShouldExit()
{
	threadShouldExit_ = true;
}

bool Thread::waitForThreadToExit (const int timeOutMilliseconds) const
{
	// Doh! So how exactly do you expect this thread to wait for itself to stop??
	jassert (getThreadId() != getCurrentThreadId());

	const int sleepMsPerIteration = 5;
	int count = timeOutMilliseconds / sleepMsPerIteration;

	while (isThreadRunning())
	{
		if (timeOutMilliseconds > 0 && --count < 0)
			return false;

		sleep (sleepMsPerIteration);
	}

	return true;
}

void Thread::stopThread (const int timeOutMilliseconds)
{
	// agh! You can't stop the thread that's calling this method! How on earth
	// would that work??
	jassert (getCurrentThreadId() != getThreadId());

	const ScopedLock sl (startStopLock);

	if (isThreadRunning())
	{
		signalThreadShouldExit();
		notify();

		if (timeOutMilliseconds != 0)
			waitForThreadToExit (timeOutMilliseconds);

		if (isThreadRunning())
		{
			// very bad karma if this point is reached, as
			// there are bound to be locks and events left in
			// silly states when a thread is killed by force..
			jassertfalse
			Logger::writeToLog ("!! killing thread by force !!");

			juce_killThread (threadHandle_);
			threadHandle_ = 0;
			threadId_ = 0;

			const ScopedLock sl2 (runningThreadsLock);
			runningThreads.removeValue (this);
		}
	}
}

bool Thread::setPriority (const int priority)
{
	const ScopedLock sl (startStopLock);

	const bool worked = juce_setThreadPriority (threadHandle_, priority);

	if (worked)
		threadPriority_ = priority;

	return worked;
}

bool Thread::setCurrentThreadPriority (const int priority)
{
	return juce_setThreadPriority (0, priority);
}

void Thread::setAffinityMask (const uint32 affinityMask)
{
	affinityMask_ = affinityMask;
}

bool Thread::wait (const int timeOutMilliseconds) const
{
	return defaultEvent_.wait (timeOutMilliseconds);
}

void Thread::notify() const
{
	defaultEvent_.signal();
}

int Thread::getNumRunningThreads()
{
	return runningThreads.size();
}

Thread* Thread::getCurrentThread()
{
	const ThreadID thisId = getCurrentThreadId();

	const ScopedLock sl (runningThreadsLock);

	for (int i = runningThreads.size(); --i >= 0;)
	{
		Thread* const t = runningThreads.getUnchecked(i);

		if (t->threadId_ == thisId)
			return t;
	}

	return 0;
}

void Thread::stopAllThreads (const int timeOutMilliseconds)
{
	{
		const ScopedLock sl (runningThreadsLock);

		for (int i = runningThreads.size(); --i >= 0;)
			runningThreads.getUnchecked(i)->signalThreadShouldExit();
	}

	for (;;)
	{
		Thread* firstThread;

		{
			const ScopedLock sl (runningThreadsLock);
			firstThread = runningThreads.getFirst();
		}

		if (firstThread == 0)
			break;

		firstThread->stopThread (timeOutMilliseconds);
	}
}

Array<Thread*> Thread::runningThreads;
CriticalSection Thread::runningThreadsLock;

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Thread.cpp ***/


/*** Start of inlined file: juce_ThreadPool.cpp ***/
BEGIN_JUCE_NAMESPACE

ThreadPoolJob::ThreadPoolJob (const String& name)
	: jobName (name),
	  pool (0),
	  shouldStop (false),
	  isActive (false),
	  shouldBeDeleted (false)
{
}

ThreadPoolJob::~ThreadPoolJob()
{
	// you mustn't delete a job while it's still in a pool! Use ThreadPool::removeJob()
	// to remove it first!
	jassert (pool == 0 || ! pool->contains (this));
}

const String ThreadPoolJob::getJobName() const
{
	return jobName;
}

void ThreadPoolJob::setJobName (const String& newName)
{
	jobName = newName;
}

void ThreadPoolJob::signalJobShouldExit()
{
	shouldStop = true;
}

class ThreadPool::ThreadPoolThread  : public Thread
{
public:
	ThreadPoolThread (ThreadPool& pool_)
		: Thread ("Pool"),
		  pool (pool_),
		  busy (false)
	{
	}

	~ThreadPoolThread()
	{
	}

	void run()
	{
		while (! threadShouldExit())
		{
			if (! pool.runNextJob())
				wait (500);
		}
	}

private:
	ThreadPool& pool;
	bool volatile busy;

	ThreadPoolThread (const ThreadPoolThread&);
	ThreadPoolThread& operator= (const ThreadPoolThread&);
};

ThreadPool::ThreadPool (const int numThreads,
						const bool startThreadsOnlyWhenNeeded,
						const int stopThreadsWhenNotUsedTimeoutMs)
	: threadStopTimeout (stopThreadsWhenNotUsedTimeoutMs),
	  priority (5)
{
	jassert (numThreads > 0); // not much point having one of these with no threads in it.

	for (int i = jmax (1, numThreads); --i >= 0;)
		threads.add (new ThreadPoolThread (*this));

	if (! startThreadsOnlyWhenNeeded)
		for (int i = threads.size(); --i >= 0;)
			threads.getUnchecked(i)->startThread (priority);
}

ThreadPool::~ThreadPool()
{
	removeAllJobs (true, 4000);

	int i;
	for (i = threads.size(); --i >= 0;)
		threads.getUnchecked(i)->signalThreadShouldExit();

	for (i = threads.size(); --i >= 0;)
		threads.getUnchecked(i)->stopThread (500);
}

void ThreadPool::addJob (ThreadPoolJob* const job)
{
	jassert (job != 0);
	jassert (job->pool == 0);

	if (job->pool == 0)
	{
		job->pool = this;
		job->shouldStop = false;
		job->isActive = false;

		{
			const ScopedLock sl (lock);
			jobs.add (job);

			int numRunning = 0;

			for (int i = threads.size(); --i >= 0;)
				if (threads.getUnchecked(i)->isThreadRunning() && ! threads.getUnchecked(i)->threadShouldExit())
					++numRunning;

			if (numRunning < threads.size())
			{
				bool startedOne = false;
				int n = 1000;

				while (--n >= 0 && ! startedOne)
				{
					for (int i = threads.size(); --i >= 0;)
					{
						if (! threads.getUnchecked(i)->isThreadRunning())
						{
							threads.getUnchecked(i)->startThread (priority);
							startedOne = true;
							break;
						}
					}

					if (! startedOne)
						Thread::sleep (2);
				}
			}
		}

		for (int i = threads.size(); --i >= 0;)
			threads.getUnchecked(i)->notify();
	}
}

int ThreadPool::getNumJobs() const
{
	return jobs.size();
}

ThreadPoolJob* ThreadPool::getJob (const int index) const
{
	const ScopedLock sl (lock);
	return jobs [index];
}

bool ThreadPool::contains (const ThreadPoolJob* const job) const
{
	const ScopedLock sl (lock);
	return jobs.contains (const_cast <ThreadPoolJob*> (job));
}

bool ThreadPool::isJobRunning (const ThreadPoolJob* const job) const
{
	const ScopedLock sl (lock);
	return jobs.contains (const_cast <ThreadPoolJob*> (job)) && job->isActive;
}

bool ThreadPool::waitForJobToFinish (const ThreadPoolJob* const job,
									 const int timeOutMs) const
{
	if (job != 0)
	{
		const uint32 start = Time::getMillisecondCounter();

		while (contains (job))
		{
			if (timeOutMs >= 0 && Time::getMillisecondCounter() >= start + timeOutMs)
				return false;

			jobFinishedSignal.wait (2);
		}
	}

	return true;
}

bool ThreadPool::removeJob (ThreadPoolJob* const job,
							const bool interruptIfRunning,
							const int timeOutMs)
{
	bool dontWait = true;

	if (job != 0)
	{
		const ScopedLock sl (lock);

		if (jobs.contains (job))
		{
			if (job->isActive)
			{
				if (interruptIfRunning)
					job->signalJobShouldExit();

				dontWait = false;
			}
			else
			{
				jobs.removeValue (job);
			}
		}
	}

	return dontWait || waitForJobToFinish (job, timeOutMs);
}

bool ThreadPool::removeAllJobs (const bool interruptRunningJobs,
								const int timeOutMs,
								const bool deleteInactiveJobs,
								ThreadPool::JobSelector* selectedJobsToRemove)
{
	Array <ThreadPoolJob*> jobsToWaitFor;

	{
		const ScopedLock sl (lock);

		for (int i = jobs.size(); --i >= 0;)
		{
			ThreadPoolJob* const job = jobs.getUnchecked(i);

			if (selectedJobsToRemove == 0 || selectedJobsToRemove->isJobSuitable (job))
			{
				if (job->isActive)
				{
					jobsToWaitFor.add (job);

					if (interruptRunningJobs)
						job->signalJobShouldExit();
				}
				else
				{
					jobs.remove (i);

					if (deleteInactiveJobs)
						delete job;
				}
			}
		}
	}

	const uint32 start = Time::getMillisecondCounter();

	for (;;)
	{
		for (int i = jobsToWaitFor.size(); --i >= 0;)
			if (! isJobRunning (jobsToWaitFor.getUnchecked (i)))
				jobsToWaitFor.remove (i);

		if (jobsToWaitFor.size() == 0)
			break;

		if (timeOutMs >= 0 && Time::getMillisecondCounter() >= start + timeOutMs)
			return false;

		jobFinishedSignal.wait (20);
	}

	return true;
}

const StringArray ThreadPool::getNamesOfAllJobs (const bool onlyReturnActiveJobs) const
{
	StringArray s;
	const ScopedLock sl (lock);

	for (int i = 0; i < jobs.size(); ++i)
	{
		const ThreadPoolJob* const job = jobs.getUnchecked(i);
		if (job->isActive || ! onlyReturnActiveJobs)
			s.add (job->getJobName());
	}

	return s;
}

bool ThreadPool::setThreadPriorities (const int newPriority)
{
	bool ok = true;

	if (priority != newPriority)
	{
		priority = newPriority;

		for (int i = threads.size(); --i >= 0;)
			if (! threads.getUnchecked(i)->setPriority (newPriority))
				ok = false;
	}

	return ok;
}

bool ThreadPool::runNextJob()
{
	ThreadPoolJob* job = 0;

	{
		const ScopedLock sl (lock);

		for (int i = 0; i < jobs.size(); ++i)
		{
			job = jobs[i];

			if (job != 0 && ! (job->isActive || job->shouldStop))
				break;

			job = 0;
		}

		if (job != 0)
			job->isActive = true;

	}

	if (job != 0)
	{
		JUCE_TRY
		{
			ThreadPoolJob::JobStatus result = job->runJob();

			lastJobEndTime = Time::getApproximateMillisecondCounter();

			const ScopedLock sl (lock);

			if (jobs.contains (job))
			{
				job->isActive = false;

				if (result != ThreadPoolJob::jobNeedsRunningAgain || job->shouldStop)
				{
					job->pool = 0;
					job->shouldStop = true;
					jobs.removeValue (job);

					if (result == ThreadPoolJob::jobHasFinishedAndShouldBeDeleted)
						delete job;

					jobFinishedSignal.signal();
				}
				else
				{
					// move the job to the end of the queue if it wants another go
					jobs.move (jobs.indexOf (job), -1);
				}
			}
		}
#if JUCE_CATCH_UNHANDLED_EXCEPTIONS
		catch (...)
		{
			const ScopedLock sl (lock);
			jobs.removeValue (job);
		}
#endif
	}
	else
	{
		if (threadStopTimeout > 0
			 && Time::getApproximateMillisecondCounter() > lastJobEndTime + threadStopTimeout)
		{
			const ScopedLock sl (lock);

			if (jobs.size() == 0)
				for (int i = threads.size(); --i >= 0;)
					threads.getUnchecked(i)->signalThreadShouldExit();
		}
		else
		{
			return false;
		}
	}

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ThreadPool.cpp ***/


/*** Start of inlined file: juce_TimeSliceThread.cpp ***/
BEGIN_JUCE_NAMESPACE

TimeSliceThread::TimeSliceThread (const String& threadName)
	: Thread (threadName),
	  index (0),
	  clientBeingCalled (0),
	  clientsChanged (false)
{
}

TimeSliceThread::~TimeSliceThread()
{
	stopThread (2000);
}

void TimeSliceThread::addTimeSliceClient (TimeSliceClient* const client)
{
	const ScopedLock sl (listLock);
	clients.addIfNotAlreadyThere (client);
	clientsChanged = true;
	notify();
}

void TimeSliceThread::removeTimeSliceClient (TimeSliceClient* const client)
{
	const ScopedLock sl1 (listLock);
	clientsChanged = true;

	// if there's a chance we're in the middle of calling this client, we need to
	// also lock the outer lock..
	if (clientBeingCalled == client)
	{
		const ScopedUnlock ul (listLock); // unlock first to get the order right..

		const ScopedLock sl2 (callbackLock);
		const ScopedLock sl3 (listLock);

		clients.removeValue (client);
	}
	else
	{
		clients.removeValue (client);
	}
}

int TimeSliceThread::getNumClients() const
{
	return clients.size();
}

TimeSliceClient* TimeSliceThread::getClient (const int i) const
{
	const ScopedLock sl (listLock);
	return clients [i];
}

void TimeSliceThread::run()
{
	int numCallsSinceBusy = 0;

	while (! threadShouldExit())
	{
		int timeToWait = 500;

		{
			const ScopedLock sl (callbackLock);

			{
				const ScopedLock sl2 (listLock);

				if (clients.size() > 0)
				{
					index = (index + 1) % clients.size();

					clientBeingCalled = clients [index];
				}
				else
				{
					index = 0;
					clientBeingCalled = 0;
				}

				if (clientsChanged)
				{
					clientsChanged = false;
					numCallsSinceBusy = 0;
				}
			}

			if (clientBeingCalled != 0)
			{
				if (clientBeingCalled->useTimeSlice())
					numCallsSinceBusy = 0;
				else
					++numCallsSinceBusy;

				if (numCallsSinceBusy >= clients.size())
					timeToWait = 500;
				else if (index == 0)
					timeToWait = 1;   // throw in an occasional pause, to stop everything locking up
				else
					timeToWait = 0;
			}
		}

		if (timeToWait > 0)
			wait (timeToWait);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TimeSliceThread.cpp ***/

#endif

#if JUCE_BUILD_MISC

/*** Start of inlined file: juce_ValueTree.cpp ***/
BEGIN_JUCE_NAMESPACE

class ValueTree::SetPropertyAction  : public UndoableAction
{
public:
	SetPropertyAction (const SharedObjectPtr& target_, const var::identifier& name_,
					   const var& newValue_, const bool isAddingNewProperty_, const bool isDeletingProperty_)
		: target (target_), name (name_), newValue (newValue_),
		  isAddingNewProperty (isAddingNewProperty_),
		  isDeletingProperty (isDeletingProperty_)
	{
		if (! isAddingNewProperty)
			oldValue = target_->getProperty (name_);
	}

	~SetPropertyAction() {}

	bool perform()
	{
		jassert (! (isAddingNewProperty && target->hasProperty (name)));

		if (isDeletingProperty)
			target->removeProperty (name, 0);
		else
			target->setProperty (name, newValue, 0);

		return true;
	}

	bool undo()
	{
		if (isAddingNewProperty)
			target->removeProperty (name, 0);
		else
			target->setProperty (name, oldValue, 0);

		return true;
	}

	int getSizeInUnits()
	{
		return (int) sizeof (*this); //xxx should be more accurate
	}

private:
	const SharedObjectPtr target;
	const var::identifier name;
	const var newValue;
	var oldValue;
	const bool isAddingNewProperty, isDeletingProperty;

	SetPropertyAction (const SetPropertyAction&);
	SetPropertyAction& operator= (const SetPropertyAction&);
};

class ValueTree::AddOrRemoveChildAction  : public UndoableAction
{
public:
	AddOrRemoveChildAction (const SharedObjectPtr& target_, const int childIndex_,
							const SharedObjectPtr& newChild_)
		: target (target_),
		  child (newChild_ != 0 ? newChild_ : target_->children [childIndex_]),
		  childIndex (childIndex_),
		  isDeleting (newChild_ == 0)
	{
		jassert (child != 0);
	}

	~AddOrRemoveChildAction() {}

	bool perform()
	{
		if (isDeleting)
			target->removeChild (childIndex, 0);
		else
			target->addChild (child, childIndex, 0);

		return true;
	}

	bool undo()
	{
		if (isDeleting)
		{
			target->addChild (child, childIndex, 0);
		}
		else
		{
			// If you hit this, it seems that your object's state is getting confused - probably
			// because you've interleaved some undoable and non-undoable operations?
			jassert (childIndex < target->children.size());
			target->removeChild (childIndex, 0);
		}

		return true;
	}

	int getSizeInUnits()
	{
		return (int) sizeof (*this); //xxx should be more accurate
	}

private:
	const SharedObjectPtr target, child;
	const int childIndex;
	const bool isDeleting;

	AddOrRemoveChildAction (const AddOrRemoveChildAction&);
	AddOrRemoveChildAction& operator= (const AddOrRemoveChildAction&);
};

class ValueTree::MoveChildAction  : public UndoableAction
{
public:
	MoveChildAction (const SharedObjectPtr& target_,
					 const int startIndex_, const int endIndex_)
		: target (target_),
		  startIndex (startIndex_),
		  endIndex (endIndex_)
	{
	}

	~MoveChildAction() {}

	bool perform()
	{
		target->moveChild (startIndex, endIndex, 0);
		return true;
	}

	bool undo()
	{
		target->moveChild (endIndex, startIndex, 0);
		return true;
	}

	int getSizeInUnits()
	{
		return (int) sizeof (*this); //xxx should be more accurate
	}

private:
	const SharedObjectPtr target, child;
	const int startIndex, endIndex;

	MoveChildAction (const MoveChildAction&);
	MoveChildAction& operator= (const MoveChildAction&);
};

ValueTree::SharedObject::SharedObject (const String& type_)
	: type (type_), parent (0)
{
}

ValueTree::SharedObject::SharedObject (const SharedObject& other)
	: type (other.type), properties (other.properties), parent (0)
{
	for (int i = 0; i < other.children.size(); ++i)
		children.add (new SharedObject (*other.children.getUnchecked(i)));
}

ValueTree::SharedObject::~SharedObject()
{
	jassert (parent == 0); // this should never happen unless something isn't obeying the ref-counting!

	for (int i = children.size(); --i >= 0;)
	{
		const SharedObjectPtr c (children.getUnchecked(i));
		c->parent = 0;
		children.remove (i);
		c->sendParentChangeMessage();
	}
}

void ValueTree::SharedObject::sendPropertyChangeMessage (ValueTree& tree, const var::identifier& property)
{
	for (int i = valueTreesWithListeners.size(); --i >= 0;)
	{
		ValueTree* const v = valueTreesWithListeners[i];
		if (v != 0)
			v->listeners.call (&ValueTree::Listener::valueTreePropertyChanged, tree, property);
	}
}

void ValueTree::SharedObject::sendPropertyChangeMessage (const var::identifier& property)
{
	ValueTree tree (this);
	ValueTree::SharedObject* t = this;

	while (t != 0)
	{
		t->sendPropertyChangeMessage (tree, property);
		t = t->parent;
	}
}

void ValueTree::SharedObject::sendChildChangeMessage (ValueTree& tree)
{
	for (int i = valueTreesWithListeners.size(); --i >= 0;)
	{
		ValueTree* const v = valueTreesWithListeners[i];
		if (v != 0)
			v->listeners.call (&ValueTree::Listener::valueTreeChildrenChanged, tree);
	}
}

void ValueTree::SharedObject::sendChildChangeMessage()
{
	ValueTree tree (this);
	ValueTree::SharedObject* t = this;

	while (t != 0)
	{
		t->sendChildChangeMessage (tree);
		t = t->parent;
	}
}

void ValueTree::SharedObject::sendParentChangeMessage()
{
	ValueTree tree (this);

	int i;
	for (i = children.size(); --i >= 0;)
	{
		SharedObject* const t = children[i];
		if (t != 0)
			t->sendParentChangeMessage();
	}

	for (i = valueTreesWithListeners.size(); --i >= 0;)
	{
		ValueTree* const v = valueTreesWithListeners[i];
		if (v != 0)
			v->listeners.call (&ValueTree::Listener::valueTreeParentChanged, tree);
	}
}

const var& ValueTree::SharedObject::getProperty (const var::identifier& name) const
{
	return properties [name];
}

const var ValueTree::SharedObject::getProperty (const var::identifier& name, const var& defaultReturnValue) const
{
	return properties.getWithDefault (name, defaultReturnValue);
}

void ValueTree::SharedObject::setProperty (const var::identifier& name, const var& newValue, UndoManager* const undoManager)
{
	if (undoManager == 0)
	{
		if (properties.set (name, newValue))
			sendPropertyChangeMessage (name);
	}
	else
	{
		var* const existingValue = properties.getItem (name);

		if (existingValue != 0)
		{
			if (*existingValue != newValue)
				undoManager->perform (new SetPropertyAction (this, name, newValue, false, false));
		}
		else
		{
			undoManager->perform (new SetPropertyAction (this, name, newValue, true, false));
		}
	}
}

bool ValueTree::SharedObject::hasProperty (const var::identifier& name) const
{
	return properties.contains (name);
}

void ValueTree::SharedObject::removeProperty (const var::identifier& name, UndoManager* const undoManager)
{
	if (undoManager == 0)
	{
		if (properties.remove (name))
			sendPropertyChangeMessage (name);
	}
	else
	{
		if (properties.contains (name))
			undoManager->perform (new SetPropertyAction (this, name, var::null, false, true));
	}
}

void ValueTree::SharedObject::removeAllProperties (UndoManager* const undoManager)
{
	if (undoManager == 0)
	{
		while (properties.size() > 0)
		{
			const var::identifier name (properties.getName (properties.size() - 1));
			properties.remove (name);
			sendPropertyChangeMessage (name);
		}
	}
	else
	{
		for (int i = properties.size(); --i >= 0;)
			undoManager->perform (new SetPropertyAction (this, properties.getName(i), var::null, false, true));
	}
}

ValueTree ValueTree::SharedObject::getChildWithName (const String& typeToMatch) const
{
	for (int i = 0; i < children.size(); ++i)
		if (children.getUnchecked(i)->type == typeToMatch)
			return ValueTree (static_cast <SharedObject*> (children.getUnchecked(i)));

	return ValueTree::invalid;
}

ValueTree ValueTree::SharedObject::getChildWithProperty (const var::identifier& propertyName, const var& propertyValue) const
{
	for (int i = 0; i < children.size(); ++i)
		if (children.getUnchecked(i)->getProperty (propertyName) == propertyValue)
			return ValueTree (static_cast <SharedObject*> (children.getUnchecked(i)));

	return ValueTree::invalid;
}

bool ValueTree::SharedObject::isAChildOf (const SharedObject* const possibleParent) const
{
	const SharedObject* p = parent;

	while (p != 0)
	{
		if (p == possibleParent)
			return true;

		p = p->parent;
	}

	return false;
}

int ValueTree::SharedObject::indexOf (const ValueTree& child) const
{
	return children.indexOf (child.object);
}

void ValueTree::SharedObject::addChild (SharedObject* child, int index, UndoManager* const undoManager)
{
	if (child != 0 && child->parent != this)
	{
		if (child != this && ! isAChildOf (child))
		{
			// You should always make sure that a child is removed from its previous parent before
			// adding it somewhere else - otherwise, it's ambiguous as to whether a different
			// undomanager should be used when removing it from its current parent..
			jassert (child->parent == 0);

			if (child->parent != 0)
			{
				jassert (child->parent->children.indexOf (child) >= 0);
				child->parent->removeChild (child->parent->children.indexOf (child), undoManager);
			}

			if (undoManager == 0)
			{
				children.insert (index, child);
				child->parent = this;
				sendChildChangeMessage();
				child->sendParentChangeMessage();
			}
			else
			{
				if (index < 0)
					index = children.size();

				undoManager->perform (new AddOrRemoveChildAction (this, index, child));
			}
		}
		else
		{
			// You're attempting to create a recursive loop! A node
			// can't be a child of one of its own children!
			jassertfalse
		}
	}
}

void ValueTree::SharedObject::removeChild (const int childIndex, UndoManager* const undoManager)
{
	const SharedObjectPtr child (children [childIndex]);

	if (child != 0)
	{
		if (undoManager == 0)
		{
			children.remove (childIndex);
			child->parent = 0;
			sendChildChangeMessage();
			child->sendParentChangeMessage();
		}
		else
		{
			undoManager->perform (new AddOrRemoveChildAction (this, childIndex, 0));
		}
	}
}

void ValueTree::SharedObject::removeAllChildren (UndoManager* const undoManager)
{
	while (children.size() > 0)
		removeChild (children.size() - 1, undoManager);
}

void ValueTree::SharedObject::moveChild (int currentIndex, int newIndex, UndoManager* undoManager)
{
	// The source index must be a valid index!
	jassert (((unsigned int) currentIndex) < (unsigned int) children.size());

	if (currentIndex != newIndex
		 && ((unsigned int) currentIndex) < (unsigned int) children.size())
	{
		if (undoManager == 0)
		{
			children.move (currentIndex, newIndex);
			sendChildChangeMessage();
		}
		else
		{
			if (((unsigned int) newIndex) >= (unsigned int) children.size())
				newIndex = children.size() - 1;

			undoManager->perform (new MoveChildAction (this, currentIndex, newIndex));
		}
	}
}

ValueTree::ValueTree() throw()
	: object (0)
{
}

const ValueTree ValueTree::invalid;

ValueTree::ValueTree (const String& type_)
	: object (new ValueTree::SharedObject (type_))
{
	jassert (type_.isNotEmpty()); // All objects should be given a sensible type name!
}

ValueTree::ValueTree (SharedObject* const object_)
	: object (object_)
{
}

ValueTree::ValueTree (const ValueTree& other)
	: object (other.object)
{
}

ValueTree& ValueTree::operator= (const ValueTree& other)
{
	if (listeners.size() > 0)
	{
		if (object != 0)
			object->valueTreesWithListeners.removeValue (this);

		if (other.object != 0)
			other.object->valueTreesWithListeners.add (this);
	}

	object = other.object;

	return *this;
}

ValueTree::~ValueTree()
{
	if (listeners.size() > 0 && object != 0)
		object->valueTreesWithListeners.removeValue (this);
}

bool ValueTree::operator== (const ValueTree& other) const
{
	return object == other.object;
}

bool ValueTree::operator!= (const ValueTree& other) const
{
	return object != other.object;
}

ValueTree ValueTree::createCopy() const
{
	return ValueTree (object != 0 ? new SharedObject (*object) : 0);
}

bool ValueTree::hasType (const String& typeName) const
{
	return object != 0 && object->type == typeName;
}

const String ValueTree::getType() const
{
	return object != 0 ? object->type : String::empty;
}

ValueTree ValueTree::getParent() const
{
	return ValueTree (object != 0 ? object->parent : (SharedObject*) 0);
}

const var& ValueTree::operator[] (const var::identifier& name) const
{
	return object == 0 ? var::null : object->getProperty (name);
}

const var& ValueTree::getProperty (const var::identifier& name) const
{
	return object == 0 ? var::null : object->getProperty (name);
}

const var ValueTree::getProperty (const var::identifier& name, const var& defaultReturnValue) const
{
	return object == 0 ? defaultReturnValue : object->getProperty (name, defaultReturnValue);
}

void ValueTree::setProperty (const var::identifier& name, const var& newValue, UndoManager* const undoManager)
{
	jassert (name.name.isNotEmpty());

	if (object != 0 && name.name.isNotEmpty())
		object->setProperty (name, newValue, undoManager);
}

bool ValueTree::hasProperty (const var::identifier& name) const
{
	return object != 0 && object->hasProperty (name);
}

void ValueTree::removeProperty (const var::identifier& name, UndoManager* const undoManager)
{
	if (object != 0)
		object->removeProperty (name, undoManager);
}

void ValueTree::removeAllProperties (UndoManager* const undoManager)
{
	if (object != 0)
		object->removeAllProperties (undoManager);
}

int ValueTree::getNumProperties() const
{
	return object == 0 ? 0 : object->properties.size();
}

const var::identifier ValueTree::getPropertyName (const int index) const
{
	return object == 0 ? var::identifier()
					   : object->properties.getName (index);
}

class ValueTreePropertyValueSource  : public Value::ValueSource,
									  public ValueTree::Listener
{
public:
	ValueTreePropertyValueSource (const ValueTree& tree_,
								  const var::identifier& property_,
								  UndoManager* const undoManager_)
		: tree (tree_),
		  property (property_),
		  undoManager (undoManager_)
	{
		tree.addListener (this);
	}

	~ValueTreePropertyValueSource()
	{
		tree.removeListener (this);
	}

	const var getValue() const
	{
		return tree [property];
	}

	void setValue (const var& newValue)
	{
		tree.setProperty (property, newValue, undoManager);
	}

	void valueTreePropertyChanged (ValueTree& treeWhosePropertyHasChanged, const var::identifier& changedProperty)
	{
		if (tree == treeWhosePropertyHasChanged && property == changedProperty)
			sendChangeMessage (false);
	}

	void valueTreeChildrenChanged (ValueTree&) {}
	void valueTreeParentChanged (ValueTree&)   {}

private:
	ValueTree tree;
	const var::identifier property;
	UndoManager* const undoManager;

	ValueTreePropertyValueSource& operator= (const ValueTreePropertyValueSource&);
};

Value ValueTree::getPropertyAsValue (const var::identifier& name, UndoManager* const undoManager) const
{
	return Value (new ValueTreePropertyValueSource (*this, name, undoManager));
}

int ValueTree::getNumChildren() const
{
	return object == 0 ? 0 : object->children.size();
}

ValueTree ValueTree::getChild (int index) const
{
	return ValueTree (object != 0 ? (SharedObject*) object->children [index] : (SharedObject*) 0);
}

ValueTree ValueTree::getChildWithName (const String& type) const
{
	return object != 0 ? object->getChildWithName (type) : ValueTree::invalid;
}

ValueTree ValueTree::getChildWithProperty (const var::identifier& propertyName, const var& propertyValue) const
{
	return object != 0 ? object->getChildWithProperty (propertyName, propertyValue) : ValueTree::invalid;
}

bool ValueTree::isAChildOf (const ValueTree& possibleParent) const
{
	return object != 0 && object->isAChildOf (possibleParent.object);
}

int ValueTree::indexOf (const ValueTree& child) const
{
	return object != 0 ? object->indexOf (child) : -1;
}

void ValueTree::addChild (ValueTree child, int index, UndoManager* const undoManager)
{
	if (object != 0)
		object->addChild (child.object, index, undoManager);
}

void ValueTree::removeChild (const int childIndex, UndoManager* const undoManager)
{
	if (object != 0)
		object->removeChild (childIndex, undoManager);
}

void ValueTree::removeChild (const ValueTree& child, UndoManager* const undoManager)
{
	if (object != 0)
		object->removeChild (object->children.indexOf (child.object), undoManager);
}

void ValueTree::removeAllChildren (UndoManager* const undoManager)
{
	if (object != 0)
		object->removeAllChildren (undoManager);
}

void ValueTree::moveChild (int currentIndex, int newIndex, UndoManager* undoManager)
{
	if (object != 0)
		object->moveChild (currentIndex, newIndex, undoManager);
}

void ValueTree::addListener (Listener* listener)
{
	if (listener != 0)
	{
		if (listeners.size() == 0 && object != 0)
			object->valueTreesWithListeners.add (this);

		listeners.add (listener);
	}
}

void ValueTree::removeListener (Listener* listener)
{
	listeners.remove (listener);

	if (listeners.size() == 0 && object != 0)
		object->valueTreesWithListeners.removeValue (this);
}

XmlElement* ValueTree::SharedObject::createXml() const
{
	XmlElement* xml = new XmlElement (type);

	int i;
	for (i = 0; i < properties.size(); ++i)
	{
		var::identifier name (properties.getName(i));
		const var& v = properties [name];

		jassert (! v.isObject()); // DynamicObjects can't be stored as XML!

		xml->setAttribute (name.name, v.toString());
	}

	for (i = 0; i < children.size(); ++i)
		xml->addChildElement (children.getUnchecked(i)->createXml());

	return xml;
}

XmlElement* ValueTree::createXml() const
{
	return object != 0 ? object->createXml() : 0;
}

ValueTree ValueTree::fromXml (const XmlElement& xml)
{
	ValueTree v (xml.getTagName());

	const int numAtts = xml.getNumAttributes(); // xxx inefficient - should write an att iterator..

	for (int i = 0; i < numAtts; ++i)
		v.setProperty (xml.getAttributeName (i), var (xml.getAttributeValue (i)), 0);

	forEachXmlChildElement (xml, e)
	{
		v.addChild (fromXml (*e), -1, 0);
	}

	return v;
}

void ValueTree::writeToStream (OutputStream& output)
{
	output.writeString (getType());

	const int numProps = getNumProperties();
	output.writeCompressedInt (numProps);

	int i;
	for (i = 0; i < numProps; ++i)
	{
		const var::identifier name (getPropertyName(i));
		output.writeString (name.name);
		getProperty(name).writeToStream (output);
	}

	const int numChildren = getNumChildren();
	output.writeCompressedInt (numChildren);

	for (i = 0; i < numChildren; ++i)
		getChild (i).writeToStream (output);
}

ValueTree ValueTree::readFromStream (InputStream& input)
{
	String type (input.readString());

	if (type.isEmpty())
		return ValueTree::invalid;

	ValueTree v (type);

	const int numProps = input.readCompressedInt();

	if (numProps < 0)
	{
		jassertfalse  // trying to read corrupted data!
		return v;
	}

	int i;
	for (i = 0; i < numProps; ++i)
	{
		const String name (input.readString());
		jassert (name.isNotEmpty());
		const var value (var::readFromStream (input));
		v.setProperty (name, value, 0);
	}

	const int numChildren = input.readCompressedInt();

	for (i = 0; i < numChildren; ++i)
		v.addChild (readFromStream (input), -1, 0);

	return v;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ValueTree.cpp ***/



/*** Start of inlined file: juce_Value.cpp ***/
BEGIN_JUCE_NAMESPACE

Value::ValueSource::ValueSource()
{
}

Value::ValueSource::~ValueSource()
{
}

void Value::ValueSource::sendChangeMessage (const bool synchronous)
{
	if (synchronous)
	{
		for (int i = valuesWithListeners.size(); --i >= 0;)
		{
			Value* const v = valuesWithListeners[i];

			if (v != 0)
				v->callListeners();
		}
	}
	else
	{
		triggerAsyncUpdate();
	}
}

void Value::ValueSource::handleAsyncUpdate()
{
	sendChangeMessage (true);
}

class SimpleValueSource  : public Value::ValueSource
{
public:
	SimpleValueSource()
	{
	}

	SimpleValueSource (const var& initialValue)
		: value (initialValue)
	{
	}

	~SimpleValueSource()
	{
	}

	const var getValue() const
	{
		return value;
	}

	void setValue (const var& newValue)
	{
		if (newValue != value)
		{
			value = newValue;
			sendChangeMessage (false);
		}
	}

private:
	var value;

	SimpleValueSource (const SimpleValueSource&);
	SimpleValueSource& operator= (const SimpleValueSource&);
};

Value::Value()
	: value (new SimpleValueSource())
{
}

Value::Value (ValueSource* const value_)
	: value (value_)
{
	jassert (value_ != 0);
}

Value::Value (const var& initialValue)
	: value (new SimpleValueSource (initialValue))
{
}

Value::Value (const Value& other)
	: value (other.value)
{
}

Value& Value::operator= (const Value& other)
{
	value = other.value;
	return *this;
}

Value::~Value()
{
	if (listeners.size() > 0)
		value->valuesWithListeners.removeValue (this);
}

const var Value::getValue() const
{
	return value->getValue();
}

Value::operator const var() const
{
	return getValue();
}

void Value::setValue (const var& newValue)
{
	value->setValue (newValue);
}

const String Value::toString() const
{
	return value->getValue().toString();
}

Value& Value::operator= (const var& newValue)
{
	value->setValue (newValue);
	return *this;
}

void Value::referTo (const Value& valueToReferTo)
{
	if (valueToReferTo.value != value)
	{
		if (listeners.size() > 0)
		{
			value->valuesWithListeners.removeValue (this);
			valueToReferTo.value->valuesWithListeners.add (this);
		}

		value = valueToReferTo.value;
		callListeners();
	}
}

bool Value::refersToSameSourceAs (const Value& other) const
{
	return value == other.value;
}

bool Value::operator== (const Value& other) const
{
	return value == other.value || value->getValue() == other.getValue();
}

bool Value::operator!= (const Value& other) const
{
	return value != other.value && value->getValue() != other.getValue();
}

void Value::addListener (Listener* const listener)
{
	if (listener != 0)
	{
		if (listeners.size() == 0)
			value->valuesWithListeners.add (this);

		listeners.add (listener);
	}
}

void Value::removeListener (Listener* const listener)
{
	listeners.remove (listener);

	if (listeners.size() == 0)
		value->valuesWithListeners.removeValue (this);
}

void Value::callListeners()
{
	Value v (*this); // (create a copy in case this gets deleted by a callback)
	listeners.call (&Listener::valueChanged, v);
}

OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
{
	return stream << value.toString();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Value.cpp ***/


/*** Start of inlined file: juce_Application.cpp ***/
#if JUCE_MSVC
  #pragma warning (push)
  #pragma warning (disable: 4245 4514 4100)
  #include <crtdbg.h>
  #pragma warning (pop)
#endif

BEGIN_JUCE_NAMESPACE

void juce_setCurrentThreadName (const String& name);

static JUCEApplication* appInstance = 0;

JUCEApplication::JUCEApplication()
	: appReturnValue (0),
	  stillInitialising (true)
{
}

JUCEApplication::~JUCEApplication()
{
	if (appLock != 0)
	{
		appLock->exit();
		appLock = 0;
	}
}

JUCEApplication* JUCEApplication::getInstance() throw()
{
	return appInstance;
}

bool JUCEApplication::isInitialising() const throw()
{
	return stillInitialising;
}

const String JUCEApplication::getApplicationVersion()
{
	return String::empty;
}

bool JUCEApplication::moreThanOneInstanceAllowed()
{
	return true;
}

void JUCEApplication::anotherInstanceStarted (const String&)
{
}

void JUCEApplication::systemRequestedQuit()
{
	quit();
}

void JUCEApplication::quit()
{
	MessageManager::getInstance()->stopDispatchLoop();
}

void JUCEApplication::setApplicationReturnValue (const int newReturnValue) throw()
{
	appReturnValue = newReturnValue;
}

void JUCEApplication::unhandledException (const std::exception*,
										  const String&,
										  const int)
{
	jassertfalse
}

void JUCEApplication::sendUnhandledException (const std::exception* const e,
											  const char* const sourceFile,
											  const int lineNumber)
{
	if (appInstance != 0)
		appInstance->unhandledException (e, sourceFile, lineNumber);
}

ApplicationCommandTarget* JUCEApplication::getNextCommandTarget()
{
	return 0;
}

void JUCEApplication::getAllCommands (Array <CommandID>& commands)
{
	commands.add (StandardApplicationCommandIDs::quit);
}

void JUCEApplication::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
{
	if (commandID == StandardApplicationCommandIDs::quit)
	{
		result.setInfo (TRANS("Quit"),
						TRANS("Quits the application"),
						"Application",
						0);

		result.defaultKeypresses.add (KeyPress ('q', ModifierKeys::commandModifier, 0));
	}
}

bool JUCEApplication::perform (const InvocationInfo& info)
{
	if (info.commandID == StandardApplicationCommandIDs::quit)
	{
		systemRequestedQuit();
		return true;
	}

	return false;
}

int JUCEApplication::main (String& commandLine, JUCEApplication* const app)
{
	if (! app->initialiseApp (commandLine))
		return 0;

	// now loop until a quit message is received..
	JUCE_TRY
	{
		MessageManager::getInstance()->runDispatchLoop();
	}
#if JUCE_CATCH_UNHANDLED_EXCEPTIONS
	catch (const std::exception& e)
	{
		app->unhandledException (&e, __FILE__, __LINE__);
	}
	catch (...)
	{
		app->unhandledException (0, __FILE__, __LINE__);
	}
#endif

	return shutdownAppAndClearUp();
}

bool JUCEApplication::initialiseApp (String& commandLine)
{
	jassert (appInstance == 0);
	appInstance = this;

	commandLineParameters = commandLine.trim();
	commandLine = String::empty;

	initialiseJuce_GUI();

#if ! JUCE_IPHONE
	jassert (appLock == 0); // initialiseApp must only be called once!

	if (! moreThanOneInstanceAllowed())
	{
		appLock = new InterProcessLock ("juceAppLock_" + getApplicationName());

		if (! appLock->enter(0))
		{
			MessageManager::broadcastMessage (getApplicationName() + "/" + commandLineParameters);

			delete appInstance;
			appInstance = 0;

			DBG ("Another instance is running - quitting...");
			return false;
		}
	}
#endif

	// let the app do its setting-up..
	initialise (commandLineParameters);

	// register for broadcast new app messages
	MessageManager::getInstance()->registerBroadcastListener (this);

	stillInitialising = false;
	return true;
}

int JUCEApplication::shutdownAppAndClearUp()
{
	jassert (appInstance != 0);
	ScopedPointer<JUCEApplication> app (appInstance);
	int returnValue = 0;

	MessageManager::getInstance()->deregisterBroadcastListener (static_cast <JUCEApplication*> (app));

	static bool reentrancyCheck = false;

	if (! reentrancyCheck)
	{
		reentrancyCheck = true;

		JUCE_TRY
		{
			// give the app a chance to clean up..
			app->shutdown();
		}
#if JUCE_CATCH_UNHANDLED_EXCEPTIONS
		catch (const std::exception& e)
		{
			app->unhandledException (&e, __FILE__, __LINE__);
		}
		catch (...)
		{
			app->unhandledException (0, __FILE__, __LINE__);
		}
#endif

		JUCE_TRY
		{
			shutdownJuce_GUI();

			returnValue = app->getApplicationReturnValue();

			appInstance = 0;
			app = 0;
		}
		JUCE_CATCH_ALL_ASSERT

		reentrancyCheck = false;
	}

	return returnValue;
}

#if JUCE_IPHONE
 extern int juce_IPhoneMain (int argc, const char* argv[], JUCEApplication* app);
#endif

#if ! JUCE_WINDOWS
extern const char* juce_Argv0;
#endif

int JUCEApplication::main (int argc, const char* argv[], JUCEApplication* const newApp)
{
#if ! JUCE_WINDOWS
	juce_Argv0 = argv[0];
#endif

#if JUCE_IPHONE
	const ScopedAutoReleasePool pool;
	return juce_IPhoneMain (argc, argv, newApp);
#else

  #if JUCE_MAC
	const ScopedAutoReleasePool pool;
  #endif

	String cmd;
	for (int i = 1; i < argc; ++i)
		cmd << argv[i] << ' ';

	return JUCEApplication::main (cmd, newApp);
#endif
}

void JUCEApplication::actionListenerCallback (const String& message)
{
	if (message.startsWith (getApplicationName() + "/"))
		anotherInstanceStarted (message.substring (getApplicationName().length() + 1));
}

static bool juceInitialisedGUI = false;

void JUCE_PUBLIC_FUNCTION initialiseJuce_GUI()
{
	if (! juceInitialisedGUI)
	{
#if JUCE_MAC || JUCE_IPHONE
		const ScopedAutoReleasePool pool;
#endif
		juceInitialisedGUI = true;

		initialiseJuce_NonGUI();
		MessageManager::getInstance();
		LookAndFeel::setDefaultLookAndFeel (0);
		juce_setCurrentThreadName ("Juce Message Thread");

#if JUCE_WINDOWS && JUCE_DEBUG
		// This section is just for catching people who mess up their project settings and
		// turn RTTI off..
		try
		{
			TextButton tb (String::empty);
			Component* c = &tb;

			// Got an exception here? Then TURN ON RTTI in your compiler settings!!
			c = dynamic_cast <Button*> (c);
		}
		catch (...)
		{
			// Ended up here? If so, TURN ON RTTI in your compiler settings!! And if you
			// got as far as this catch statement, then why haven't you got exception catching
			// turned on in the debugger???
			jassertfalse
		}
#endif
	}
}

void JUCE_PUBLIC_FUNCTION shutdownJuce_GUI()
{
	if (juceInitialisedGUI)
	{
#if JUCE_MAC
		const ScopedAutoReleasePool pool;
#endif
		{
			DeletedAtShutdown::deleteAll();

			LookAndFeel::clearDefaultLookAndFeel();
		}

		delete MessageManager::getInstance();

		shutdownJuce_NonGUI();

		juceInitialisedGUI = false;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Application.cpp ***/


/*** Start of inlined file: juce_ApplicationCommandInfo.cpp ***/
BEGIN_JUCE_NAMESPACE

ApplicationCommandInfo::ApplicationCommandInfo (const CommandID commandID_) throw()
	: commandID (commandID_),
	  flags (0)
{
}

void ApplicationCommandInfo::setInfo (const String& shortName_,
									  const String& description_,
									  const String& categoryName_,
									  const int flags_) throw()
{
	shortName = shortName_;
	description = description_;
	categoryName = categoryName_;
	flags = flags_;
}

void ApplicationCommandInfo::setActive (const bool b) throw()
{
	if (b)
		flags &= ~isDisabled;
	else
		flags |= isDisabled;
}

void ApplicationCommandInfo::setTicked (const bool b) throw()
{
	if (b)
		flags |= isTicked;
	else
		flags &= ~isTicked;
}

void ApplicationCommandInfo::addDefaultKeypress (const int keyCode, const ModifierKeys& modifiers) throw()
{
	defaultKeypresses.add (KeyPress (keyCode, modifiers, 0));
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ApplicationCommandInfo.cpp ***/


/*** Start of inlined file: juce_ApplicationCommandManager.cpp ***/
BEGIN_JUCE_NAMESPACE

ApplicationCommandManager::ApplicationCommandManager()
	: firstTarget (0)
{
	keyMappings = new KeyPressMappingSet (this);

	Desktop::getInstance().addFocusChangeListener (this);
}

ApplicationCommandManager::~ApplicationCommandManager()
{
	Desktop::getInstance().removeFocusChangeListener (this);
	keyMappings = 0;
}

void ApplicationCommandManager::clearCommands()
{
	commands.clear();
	keyMappings->clearAllKeyPresses();
	triggerAsyncUpdate();
}

void ApplicationCommandManager::registerCommand (const ApplicationCommandInfo& newCommand)
{
	// zero isn't a valid command ID!
	jassert (newCommand.commandID != 0);

	// the name isn't optional!
	jassert (newCommand.shortName.isNotEmpty());

	if (getCommandForID (newCommand.commandID) == 0)
	{
		ApplicationCommandInfo* const newInfo = new ApplicationCommandInfo (newCommand);
		newInfo->flags &= ~ApplicationCommandInfo::isTicked;
		commands.add (newInfo);

		keyMappings->resetToDefaultMapping (newCommand.commandID);

		triggerAsyncUpdate();
	}
	else
	{
		// trying to re-register the same command with different parameters?
		jassert (newCommand.shortName == getCommandForID (newCommand.commandID)->shortName
				  && (newCommand.description == getCommandForID (newCommand.commandID)->description || newCommand.description.isEmpty())
				  && newCommand.categoryName == getCommandForID (newCommand.commandID)->categoryName
				  && newCommand.defaultKeypresses == getCommandForID (newCommand.commandID)->defaultKeypresses
				  && (newCommand.flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor))
					   == (getCommandForID (newCommand.commandID)->flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor)));
	}
}

void ApplicationCommandManager::registerAllCommandsForTarget (ApplicationCommandTarget* target)
{
	if (target != 0)
	{
		Array <CommandID> commandIDs;
		target->getAllCommands (commandIDs);

		for (int i = 0; i < commandIDs.size(); ++i)
		{
			ApplicationCommandInfo info (commandIDs.getUnchecked(i));
			target->getCommandInfo (info.commandID, info);

			registerCommand (info);
		}
	}
}

void ApplicationCommandManager::removeCommand (const CommandID commandID)
{
	for (int i = commands.size(); --i >= 0;)
	{
		if (commands.getUnchecked (i)->commandID == commandID)
		{
			commands.remove (i);
			triggerAsyncUpdate();

			const Array <KeyPress> keys (keyMappings->getKeyPressesAssignedToCommand (commandID));

			for (int j = keys.size(); --j >= 0;)
				keyMappings->removeKeyPress (keys.getReference (j));
		}
	}
}

void ApplicationCommandManager::commandStatusChanged()
{
	triggerAsyncUpdate();
}

const ApplicationCommandInfo* ApplicationCommandManager::getCommandForID (const CommandID commandID) const throw()
{
	for (int i = commands.size(); --i >= 0;)
		if (commands.getUnchecked(i)->commandID == commandID)
			return commands.getUnchecked(i);

	return 0;
}

const String ApplicationCommandManager::getNameOfCommand (const CommandID commandID) const throw()
{
	const ApplicationCommandInfo* const ci = getCommandForID (commandID);

	return (ci != 0) ? ci->shortName : String::empty;
}

const String ApplicationCommandManager::getDescriptionOfCommand (const CommandID commandID) const throw()
{
	const ApplicationCommandInfo* const ci = getCommandForID (commandID);

	return (ci != 0) ? (ci->description.isNotEmpty() ? ci->description : ci->shortName)
					 : String::empty;
}

const StringArray ApplicationCommandManager::getCommandCategories() const throw()
{
	StringArray s;

	for (int i = 0; i < commands.size(); ++i)
		s.addIfNotAlreadyThere (commands.getUnchecked(i)->categoryName, false);

	return s;
}

const Array <CommandID> ApplicationCommandManager::getCommandsInCategory (const String& categoryName) const throw()
{
	Array <CommandID> results;

	for (int i = 0; i < commands.size(); ++i)
		if (commands.getUnchecked(i)->categoryName == categoryName)
			results.add (commands.getUnchecked(i)->commandID);

	return results;
}

bool ApplicationCommandManager::invokeDirectly (const CommandID commandID, const bool asynchronously)
{
	ApplicationCommandTarget::InvocationInfo info (commandID);
	info.invocationMethod = ApplicationCommandTarget::InvocationInfo::direct;

	return invoke (info, asynchronously);
}

bool ApplicationCommandManager::invoke (const ApplicationCommandTarget::InvocationInfo& info_, const bool asynchronously)
{
	// This call isn't thread-safe for use from a non-UI thread without locking the message
	// manager first..
	jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());

	ApplicationCommandTarget* const target = getFirstCommandTarget (info_.commandID);

	if (target == 0)
		return false;

	ApplicationCommandInfo commandInfo (0);
	target->getCommandInfo (info_.commandID, commandInfo);

	ApplicationCommandTarget::InvocationInfo info (info_);
	info.commandFlags = commandInfo.flags;

	sendListenerInvokeCallback (info);

	const bool ok = target->invoke (info, asynchronously);

	commandStatusChanged();

	return ok;
}

ApplicationCommandTarget* ApplicationCommandManager::getFirstCommandTarget (const CommandID)
{
	return firstTarget != 0 ? firstTarget
							: findDefaultComponentTarget();
}

void ApplicationCommandManager::setFirstCommandTarget (ApplicationCommandTarget* const newTarget) throw()
{
	firstTarget = newTarget;
}

ApplicationCommandTarget* ApplicationCommandManager::getTargetForCommand (const CommandID commandID,
																		  ApplicationCommandInfo& upToDateInfo)
{
	ApplicationCommandTarget* target = getFirstCommandTarget (commandID);

	if (target == 0)
		target = JUCEApplication::getInstance();

	if (target != 0)
		target = target->getTargetForCommand (commandID);

	if (target != 0)
		target->getCommandInfo (commandID, upToDateInfo);

	return target;
}

ApplicationCommandTarget* ApplicationCommandManager::findTargetForComponent (Component* c)
{
	ApplicationCommandTarget* target = dynamic_cast <ApplicationCommandTarget*> (c);

	if (target == 0 && c != 0)
		// (unable to use the syntax findParentComponentOfClass <ApplicationCommandTarget> () because of a VC6 compiler bug)
		target = c->findParentComponentOfClass ((ApplicationCommandTarget*) 0);

	return target;
}

ApplicationCommandTarget* ApplicationCommandManager::findDefaultComponentTarget()
{
	Component* c = Component::getCurrentlyFocusedComponent();

	if (c == 0)
	{
		TopLevelWindow* const activeWindow = TopLevelWindow::getActiveTopLevelWindow();

		if (activeWindow != 0)
		{
			c = activeWindow->getPeer()->getLastFocusedSubcomponent();

			if (c == 0)
				c = activeWindow;
		}
	}

	if (c == 0 && Process::isForegroundProcess())
	{
		// getting a bit desperate now - try all desktop comps..
		for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
		{
			ApplicationCommandTarget* const target
				= findTargetForComponent (Desktop::getInstance().getComponent (i)
											  ->getPeer()->getLastFocusedSubcomponent());

			if (target != 0)
				return target;
		}
	}

	if (c != 0)
	{
		ResizableWindow* const resizableWindow = dynamic_cast <ResizableWindow*> (c);

		// if we're focused on a ResizableWindow, chances are that it's the content
		// component that really should get the event. And if not, the event will
		// still be passed up to the top level window anyway, so let's send it to the
		// content comp.
		if (resizableWindow != 0 && resizableWindow->getContentComponent() != 0)
			c = resizableWindow->getContentComponent();

		ApplicationCommandTarget* const target = findTargetForComponent (c);

		if (target != 0)
			return target;
	}

	return JUCEApplication::getInstance();
}

void ApplicationCommandManager::addListener (ApplicationCommandManagerListener* const listener) throw()
{
	listeners.add (listener);
}

void ApplicationCommandManager::removeListener (ApplicationCommandManagerListener* const listener) throw()
{
	listeners.remove (listener);
}

void ApplicationCommandManager::sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo& info)
{
	listeners.call (&ApplicationCommandManagerListener::applicationCommandInvoked, info);
}

void ApplicationCommandManager::handleAsyncUpdate()
{
	listeners.call (&ApplicationCommandManagerListener::applicationCommandListChanged);
}

void ApplicationCommandManager::globalFocusChanged (Component*)
{
	commandStatusChanged();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ApplicationCommandManager.cpp ***/


/*** Start of inlined file: juce_ApplicationCommandTarget.cpp ***/
BEGIN_JUCE_NAMESPACE

ApplicationCommandTarget::ApplicationCommandTarget()
{
}

ApplicationCommandTarget::~ApplicationCommandTarget()
{
	messageInvoker = 0;
}

bool ApplicationCommandTarget::tryToInvoke (const InvocationInfo& info, const bool async)
{
	if (isCommandActive (info.commandID))
	{
		if (async)
		{
			if (messageInvoker == 0)
				messageInvoker = new CommandTargetMessageInvoker (this);

			messageInvoker->postMessage (new Message (0, 0, 0, new ApplicationCommandTarget::InvocationInfo (info)));
			return true;
		}
		else
		{
			const bool success = perform (info);

			jassert (success);  // hmm - your target should have been able to perform this command. If it can't
								// do it at the moment for some reason, it should clear the 'isActive' flag when it
								// returns the command's info.
			return success;
		}
	}

	return false;
}

ApplicationCommandTarget* ApplicationCommandTarget::findFirstTargetParentComponent()
{
	Component* c = dynamic_cast <Component*> (this);

	if (c != 0)
		// (unable to use the syntax findParentComponentOfClass <ApplicationCommandTarget> () because of a VC6 compiler bug)
		return c->findParentComponentOfClass ((ApplicationCommandTarget*) 0);

	return 0;
}

ApplicationCommandTarget* ApplicationCommandTarget::getTargetForCommand (const CommandID commandID)
{
	ApplicationCommandTarget* target = this;
	int depth = 0;

	while (target != 0)
	{
		Array <CommandID> commandIDs;
		target->getAllCommands (commandIDs);

		if (commandIDs.contains (commandID))
			return target;

		target = target->getNextCommandTarget();

		++depth;
		jassert (depth < 100); // could be a recursive command chain??
		jassert (target != this); // definitely a recursive command chain!

		if (depth > 100 || target == this)
			break;
	}

	if (target == 0)
	{
		target = JUCEApplication::getInstance();

		if (target != 0)
		{
			Array <CommandID> commandIDs;
			target->getAllCommands (commandIDs);

			if (commandIDs.contains (commandID))
				return target;
		}
	}

	return 0;
}

bool ApplicationCommandTarget::isCommandActive (const CommandID commandID)
{
	ApplicationCommandInfo info (commandID);
	info.flags = ApplicationCommandInfo::isDisabled;

	getCommandInfo (commandID, info);

	return (info.flags & ApplicationCommandInfo::isDisabled) == 0;
}

bool ApplicationCommandTarget::invoke (const InvocationInfo& info, const bool async)
{
	ApplicationCommandTarget* target = this;
	int depth = 0;

	while (target != 0)
	{
		if (target->tryToInvoke (info, async))
			return true;

		target = target->getNextCommandTarget();

		++depth;
		jassert (depth < 100); // could be a recursive command chain??
		jassert (target != this); // definitely a recursive command chain!

		if (depth > 100 || target == this)
			break;
	}

	if (target == 0)
	{
		target = JUCEApplication::getInstance();

		if (target != 0)
			return target->tryToInvoke (info, async);
	}

	return false;
}

bool ApplicationCommandTarget::invokeDirectly (const CommandID commandID, const bool asynchronously)
{
	ApplicationCommandTarget::InvocationInfo info (commandID);
	info.invocationMethod = ApplicationCommandTarget::InvocationInfo::direct;

	return invoke (info, asynchronously);
}

ApplicationCommandTarget::InvocationInfo::InvocationInfo (const CommandID commandID_) throw()
	: commandID (commandID_),
	  commandFlags (0),
	  invocationMethod (direct),
	  originatingComponent (0),
	  isKeyDown (false),
	  millisecsSinceKeyPressed (0)
{
}

ApplicationCommandTarget::CommandTargetMessageInvoker::CommandTargetMessageInvoker (ApplicationCommandTarget* const owner_)
	: owner (owner_)
{
}

ApplicationCommandTarget::CommandTargetMessageInvoker::~CommandTargetMessageInvoker()
{
}

void ApplicationCommandTarget::CommandTargetMessageInvoker::handleMessage (const Message& message)
{
	const ScopedPointer <InvocationInfo> info (static_cast <InvocationInfo*> (message.pointerParameter));
	owner->tryToInvoke (*info, false);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ApplicationCommandTarget.cpp ***/


/*** Start of inlined file: juce_ApplicationProperties.cpp ***/
BEGIN_JUCE_NAMESPACE

juce_ImplementSingleton (ApplicationProperties)

ApplicationProperties::ApplicationProperties() throw()
	: msBeforeSaving (3000),
	  options (PropertiesFile::storeAsBinary),
	  commonSettingsAreReadOnly (0)
{
}

ApplicationProperties::~ApplicationProperties()
{
	closeFiles();
	clearSingletonInstance();
}

void ApplicationProperties::setStorageParameters (const String& applicationName,
												  const String& fileNameSuffix,
												  const String& folderName_,
												  const int millisecondsBeforeSaving,
												  const int propertiesFileOptions) throw()
{
	appName = applicationName;
	fileSuffix = fileNameSuffix;
	folderName = folderName_;
	msBeforeSaving = millisecondsBeforeSaving;
	options = propertiesFileOptions;
}

bool ApplicationProperties::testWriteAccess (const bool testUserSettings,
											 const bool testCommonSettings,
											 const bool showWarningDialogOnFailure)
{
	const bool userOk	= (! testUserSettings)   || getUserSettings()->save();
	const bool commonOk  = (! testCommonSettings) || getCommonSettings (false)->save();

	if (! (userOk && commonOk))
	{
		if (showWarningDialogOnFailure)
		{
			String filenames;

			if (userProps != 0 && ! userOk)
				filenames << '\n' << userProps->getFile().getFullPathName();

			if (commonProps != 0 && ! commonOk)
				filenames << '\n' << commonProps->getFile().getFullPathName();

			AlertWindow::showMessageBox (AlertWindow::WarningIcon,
										 appName + TRANS(" - Unable to save settings"),
										 TRANS("An error occurred when trying to save the application's settings file...\n\nIn order to save and restore its settings, ")
										  + appName + TRANS(" needs to be able to write to the following files:\n")
										  + filenames
										  + TRANS("\n\nMake sure that these files aren't read-only, and that the disk isn't full."));
		}

		return false;
	}

	return true;
}

void ApplicationProperties::openFiles() throw()
{
	// You need to call setStorageParameters() before trying to get hold of the
	// properties!
	jassert (appName.isNotEmpty());

	if (appName.isNotEmpty())
	{
		if (userProps == 0)
			userProps = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
																		false, msBeforeSaving, options);

		if (commonProps == 0)
			commonProps = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
																		  true, msBeforeSaving, options);

		userProps->setFallbackPropertySet (commonProps);
	}
}

PropertiesFile* ApplicationProperties::getUserSettings() throw()
{
	if (userProps == 0)
		openFiles();

	return userProps;
}

PropertiesFile* ApplicationProperties::getCommonSettings (const bool returnUserPropsIfReadOnly) throw()
{
	if (commonProps == 0)
		openFiles();

	if (returnUserPropsIfReadOnly)
	{
		if (commonSettingsAreReadOnly == 0)
			commonSettingsAreReadOnly = commonProps->save() ? -1 : 1;

		if (commonSettingsAreReadOnly > 0)
			return userProps;
	}

	return commonProps;
}

bool ApplicationProperties::saveIfNeeded()
{
	return (userProps == 0 || userProps->saveIfNeeded())
		 && (commonProps == 0 || commonProps->saveIfNeeded());
}

void ApplicationProperties::closeFiles()
{
	userProps = 0;
	commonProps = 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ApplicationProperties.cpp ***/


/*** Start of inlined file: juce_DeletedAtShutdown.cpp ***/
BEGIN_JUCE_NAMESPACE

static VoidArray objectsToDelete;
static CriticalSection lock;

DeletedAtShutdown::DeletedAtShutdown()
{
	const ScopedLock sl (lock);
	objectsToDelete.add (this);
}

DeletedAtShutdown::~DeletedAtShutdown()
{
	const ScopedLock sl (lock);
	objectsToDelete.removeValue (this);
}

void DeletedAtShutdown::deleteAll()
{
	// make a local copy of the array, so it can't get into a loop if something
	// creates another DeletedAtShutdown object during its destructor.
	VoidArray localCopy;

	{
		const ScopedLock sl (lock);
		localCopy = objectsToDelete;
	}

	for (int i = localCopy.size(); --i >= 0;)
	{
		JUCE_TRY
		{
			DeletedAtShutdown* deletee = static_cast <DeletedAtShutdown*> (localCopy.getUnchecked(i));

			// double-check that it's not already been deleted during another object's destructor.
			{
				const ScopedLock sl (lock);
				if (! objectsToDelete.contains (deletee))
					deletee = 0;
			}

			delete deletee;
		}
		JUCE_CATCH_EXCEPTION
	}

	// if no objects got re-created during shutdown, this should have been emptied by their
	// destructors
	jassert (objectsToDelete.size() == 0);

	objectsToDelete.clear(); // just to make sure the array doesn't have any memory still allocated
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DeletedAtShutdown.cpp ***/


/*** Start of inlined file: juce_PropertiesFile.cpp ***/
BEGIN_JUCE_NAMESPACE

namespace PropertyFileConstants
{
	static const int magicNumber            = (int) ByteOrder::littleEndianInt ("PROP");
	static const int magicNumberCompressed  = (int) ByteOrder::littleEndianInt ("CPRP");

	static const char* const fileTag        = "PROPERTIES";
	static const char* const valueTag       = "VALUE";
	static const char* const nameAttribute  = "name";
	static const char* const valueAttribute = "val";
}

PropertiesFile::PropertiesFile (const File& f, const int millisecondsBeforeSaving,
								const int options_, InterProcessLock* const processLock_)
	: PropertySet (ignoreCaseOfKeyNames),
	  file (f),
	  timerInterval (millisecondsBeforeSaving),
	  options (options_),
	  loadedOk (false),
	  needsWriting (false),
	  processLock (processLock_)
{
	// You need to correctly specify just one storage format for the file
	jassert ((options_ & (storeAsBinary | storeAsCompressedBinary | storeAsXML)) == storeAsBinary
			 || (options_ & (storeAsBinary | storeAsCompressedBinary | storeAsXML)) == storeAsCompressedBinary
			 || (options_ & (storeAsBinary | storeAsCompressedBinary | storeAsXML)) == storeAsXML);

	ProcessScopedLock pl (createProcessLock());

	if (pl != 0 && ! pl->isLocked())
		return; // locking failure..

	ScopedPointer<InputStream> fileStream (f.createInputStream());

	if (fileStream != 0)
	{
		int magicNumber = fileStream->readInt();

		if (magicNumber == PropertyFileConstants::magicNumberCompressed)
		{
			fileStream = new GZIPDecompressorInputStream (new SubregionStream (fileStream.release(), 4, -1, true), true);
			magicNumber = PropertyFileConstants::magicNumber;
		}

		if (magicNumber == PropertyFileConstants::magicNumber)
		{
			loadedOk = true;
			BufferedInputStream in (fileStream.release(), 2048, true);

			int numValues = in.readInt();

			while (--numValues >= 0 && ! in.isExhausted())
			{
				const String key (in.readString());
				const String value (in.readString());

				jassert (key.isNotEmpty());
				if (key.isNotEmpty())
					getAllProperties().set (key, value);
			}
		}
		else
		{
			// Not a binary props file - let's see if it's XML..
			fileStream = 0;

			XmlDocument parser (f);
			ScopedPointer<XmlElement> doc (parser.getDocumentElement (true));

			if (doc != 0 && doc->hasTagName (PropertyFileConstants::fileTag))
			{
				doc = parser.getDocumentElement();

				if (doc != 0)
				{
					loadedOk = true;

					forEachXmlChildElementWithTagName (*doc, e, PropertyFileConstants::valueTag)
					{
						const String name (e->getStringAttribute (PropertyFileConstants::nameAttribute));

						if (name.isNotEmpty())
						{
							getAllProperties().set (name,
													e->getFirstChildElement() != 0
														? e->getFirstChildElement()->createDocument (String::empty, true)
														: e->getStringAttribute (PropertyFileConstants::valueAttribute));
						}
					}
				}
				else
				{
					// must be a pretty broken XML file we're trying to parse here,
					// or a sign that this object needs an InterProcessLock,
					// or just a failure reading the file.  This last reason is why
					// we don't jassertfalse here.
				}
			}
		}
	}
	else
	{
		loadedOk = ! f.exists();
	}
}

PropertiesFile::~PropertiesFile()
{
	if (! saveIfNeeded())
		jassertfalse;
}

InterProcessLock::ScopedLockType* PropertiesFile::createProcessLock() const
{
	return processLock != 0 ? new InterProcessLock::ScopedLockType (*processLock) : 0;
}

bool PropertiesFile::saveIfNeeded()
{
	const ScopedLock sl (getLock());
	return (! needsWriting) || save();
}

bool PropertiesFile::needsToBeSaved() const
{
	const ScopedLock sl (getLock());
	return needsWriting;
}

void PropertiesFile::setNeedsToBeSaved (const bool needsToBeSaved)
{
	const ScopedLock sl (getLock());
	needsWriting = needsToBeSaved;
}

bool PropertiesFile::save()
{
	const ScopedLock sl (getLock());

	stopTimer();

	if (file == File::nonexistent
		 || file.isDirectory()
		 || ! file.getParentDirectory().createDirectory())
		return false;

	if ((options & storeAsXML) != 0)
	{
		XmlElement doc (PropertyFileConstants::fileTag);

		for (int i = 0; i < getAllProperties().size(); ++i)
		{
			XmlElement* const e = doc.createNewChildElement (PropertyFileConstants::valueTag);
			e->setAttribute (PropertyFileConstants::nameAttribute, getAllProperties().getAllKeys() [i]);

			// if the value seems to contain xml, store it as such..
			XmlDocument xmlContent (getAllProperties().getAllValues() [i]);
			XmlElement* const childElement = xmlContent.getDocumentElement();

			if (childElement != 0)
				e->addChildElement (childElement);
			else
				e->setAttribute (PropertyFileConstants::valueAttribute,
								 getAllProperties().getAllValues() [i]);
		}

		ProcessScopedLock pl (createProcessLock());

		if (pl != 0 && ! pl->isLocked())
			return false; // locking failure..

		if (doc.writeToFile (file, String::empty))
		{
			needsWriting = false;
			return true;
		}
	}
	else
	{
		ProcessScopedLock pl (createProcessLock());

		if (pl != 0 && ! pl->isLocked())
			return false; // locking failure..

		TemporaryFile tempFile (file);
		ScopedPointer <OutputStream> out (tempFile.getFile().createOutputStream());

		if (out != 0)
		{
			if ((options & storeAsCompressedBinary) != 0)
			{
				out->writeInt (PropertyFileConstants::magicNumberCompressed);
				out->flush();

				out = new GZIPCompressorOutputStream (out.release(), 9, true);
			}
			else
			{
				// have you set up the storage option flags correctly?
				jassert ((options & storeAsBinary) != 0);

				out->writeInt (PropertyFileConstants::magicNumber);
			}

			const int numProperties = getAllProperties().size();

			out->writeInt (numProperties);

			for (int i = 0; i < numProperties; ++i)
			{
				out->writeString (getAllProperties().getAllKeys() [i]);
				out->writeString (getAllProperties().getAllValues() [i]);
			}

			out = 0;

			if (tempFile.overwriteTargetFileWithTemporary())
			{
				needsWriting = false;
				return true;
			}
		}
	}

	return false;
}

void PropertiesFile::timerCallback()
{
	saveIfNeeded();
}

void PropertiesFile::propertyChanged()
{
	sendChangeMessage (this);

	needsWriting = true;

	if (timerInterval > 0)
		startTimer (timerInterval);
	else if (timerInterval == 0)
		saveIfNeeded();
}

const File PropertiesFile::getDefaultAppSettingsFile (const String& applicationName,
													  const String& fileNameSuffix,
													  const String& folderName,
													  const bool commonToAllUsers)
{
	// mustn't have illegal characters in this name..
	jassert (applicationName == File::createLegalFileName (applicationName));

#if JUCE_MAC || JUCE_IPHONE
	File dir (commonToAllUsers ? "/Library/Preferences"
							   : "~/Library/Preferences");

	if (folderName.isNotEmpty())
		dir = dir.getChildFile (folderName);
#endif

#ifdef JUCE_LINUX
	const File dir ((commonToAllUsers ? "/var/" : "~/")
						+ (folderName.isNotEmpty() ? folderName
												   : ("." + applicationName)));
#endif

#if JUCE_WIN32
	File dir (File::getSpecialLocation (commonToAllUsers ? File::commonApplicationDataDirectory
														 : File::userApplicationDataDirectory));

	if (dir == File::nonexistent)
		return File::nonexistent;

	dir = dir.getChildFile (folderName.isNotEmpty() ? folderName
													: applicationName);

#endif

	return dir.getChildFile (applicationName)
			  .withFileExtension (fileNameSuffix);
}

PropertiesFile* PropertiesFile::createDefaultAppPropertiesFile (const String& applicationName,
																const String& fileNameSuffix,
																const String& folderName,
																const bool commonToAllUsers,
																const int millisecondsBeforeSaving,
																const int propertiesFileOptions,
																InterProcessLock* processLock_)
{
	const File file (getDefaultAppSettingsFile (applicationName,
												fileNameSuffix,
												folderName,
												commonToAllUsers));

	jassert (file != File::nonexistent);

	if (file == File::nonexistent)
		return 0;

	return new PropertiesFile (file, millisecondsBeforeSaving, propertiesFileOptions,processLock_);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PropertiesFile.cpp ***/


/*** Start of inlined file: juce_FileBasedDocument.cpp ***/
BEGIN_JUCE_NAMESPACE

FileBasedDocument::FileBasedDocument (const String& fileExtension_,
									  const String& fileWildcard_,
									  const String& openFileDialogTitle_,
									  const String& saveFileDialogTitle_)
	: changedSinceSave (false),
	  fileExtension (fileExtension_),
	  fileWildcard (fileWildcard_),
	  openFileDialogTitle (openFileDialogTitle_),
	  saveFileDialogTitle (saveFileDialogTitle_)
{
}

FileBasedDocument::~FileBasedDocument()
{
}

void FileBasedDocument::setChangedFlag (const bool hasChanged)
{
	if (changedSinceSave != hasChanged)
	{
		changedSinceSave = hasChanged;
		sendChangeMessage (this);
	}
}

void FileBasedDocument::changed()
{
	changedSinceSave = true;
	sendChangeMessage (this);
}

void FileBasedDocument::setFile (const File& newFile)
{
	if (documentFile != newFile)
	{
		documentFile = newFile;
		changed();
	}
}

bool FileBasedDocument::loadFrom (const File& newFile,
								  const bool showMessageOnFailure)
{
	MouseCursor::showWaitCursor();

	const File oldFile (documentFile);
	documentFile = newFile;

	String error;

	if (newFile.existsAsFile())
	{
		error = loadDocument (newFile);

		if (error.isEmpty())
		{
			setChangedFlag (false);
			MouseCursor::hideWaitCursor();

			setLastDocumentOpened (newFile);
			return true;
		}
	}
	else
	{
		error = "The file doesn't exist";
	}

	documentFile = oldFile;
	MouseCursor::hideWaitCursor();

	if (showMessageOnFailure)
	{
		AlertWindow::showMessageBox (AlertWindow::WarningIcon,
									 TRANS("Failed to open file..."),
									 TRANS("There was an error while trying to load the file:\n\n")
									   + newFile.getFullPathName()
									   + "\n\n"
									   + error);
	}

	return false;
}

bool FileBasedDocument::loadFromUserSpecifiedFile (const bool showMessageOnFailure)
{
	FileChooser fc (openFileDialogTitle,
					getLastDocumentOpened(),
					fileWildcard);

	if (fc.browseForFileToOpen())
		return loadFrom (fc.getResult(), showMessageOnFailure);

	return false;
}

FileBasedDocument::SaveResult FileBasedDocument::save (const bool askUserForFileIfNotSpecified,
													   const bool showMessageOnFailure)
{
	return saveAs (documentFile,
				   false,
				   askUserForFileIfNotSpecified,
				   showMessageOnFailure);
}

FileBasedDocument::SaveResult FileBasedDocument::saveAs (const File& newFile,
														 const bool warnAboutOverwritingExistingFiles,
														 const bool askUserForFileIfNotSpecified,
														 const bool showMessageOnFailure)
{
	if (newFile == File::nonexistent)
	{
		if (askUserForFileIfNotSpecified)
		{
			return saveAsInteractive (true);
		}
		else
		{
			// can't save to an unspecified file
			jassertfalse
			return failedToWriteToFile;
		}
	}

	if (warnAboutOverwritingExistingFiles && newFile.exists())
	{
		if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
											TRANS("File already exists"),
											TRANS("There's already a file called:\n\n")
											  + newFile.getFullPathName()
											  + TRANS("\n\nAre you sure you want to overwrite it?"),
											TRANS("overwrite"),
											TRANS("cancel")))
		{
			return userCancelledSave;
		}
	}

	MouseCursor::showWaitCursor();

	const File oldFile (documentFile);
	documentFile = newFile;

	String error (saveDocument (newFile));

	if (error.isEmpty())
	{
		setChangedFlag (false);
		MouseCursor::hideWaitCursor();

		return savedOk;
	}

	documentFile = oldFile;
	MouseCursor::hideWaitCursor();

	if (showMessageOnFailure)
	{
		AlertWindow::showMessageBox (AlertWindow::WarningIcon,
									 TRANS("Error writing to file..."),
									 TRANS("An error occurred while trying to save \"")
										+ getDocumentTitle()
										+ TRANS("\" to the file:\n\n")
										+ newFile.getFullPathName()
										+ "\n\n"
										+ error);
	}

	return failedToWriteToFile;
}

FileBasedDocument::SaveResult FileBasedDocument::saveIfNeededAndUserAgrees()
{
	if (! hasChangedSinceSaved())
		return savedOk;

	const int r = AlertWindow::showYesNoCancelBox (AlertWindow::QuestionIcon,
												   TRANS("Closing document..."),
												   TRANS("Do you want to save the changes to \"")
													   + getDocumentTitle() + "\"?",
												   TRANS("save"),
												   TRANS("discard changes"),
												   TRANS("cancel"));

	if (r == 1)
	{
		// save changes
		return save (true, true);
	}
	else if (r == 2)
	{
		// discard changes
		return savedOk;
	}

	return userCancelledSave;
}

FileBasedDocument::SaveResult FileBasedDocument::saveAsInteractive (const bool warnAboutOverwritingExistingFiles)
{
	File f;

	if (documentFile.existsAsFile())
		f = documentFile;
	else
		f = getLastDocumentOpened();

	String legalFilename (File::createLegalFileName (getDocumentTitle()));

	if (legalFilename.isEmpty())
		legalFilename = "unnamed";

	if (f.existsAsFile() || f.getParentDirectory().isDirectory())
		f = f.getSiblingFile (legalFilename);
	else
		f = File::getSpecialLocation (File::userDocumentsDirectory).getChildFile (legalFilename);

	f = f.withFileExtension (fileExtension)
		 .getNonexistentSibling (true);

	FileChooser fc (saveFileDialogTitle, f, fileWildcard);

	if (fc.browseForFileToSave (warnAboutOverwritingExistingFiles))
	{
		setLastDocumentOpened (fc.getResult());

		File chosen (fc.getResult());
		if (chosen.getFileExtension().isEmpty())
		{
			chosen = chosen.withFileExtension (fileExtension);

			if (chosen.exists())
			{
				if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
													TRANS("File already exists"),
													TRANS("There's already a file called:")
													  + "\n\n" + chosen.getFullPathName()
													  + "\n\n" + TRANS("Are you sure you want to overwrite it?"),
													TRANS("overwrite"),
													TRANS("cancel")))
				{
					return userCancelledSave;
				}
			}
		}

		return saveAs (chosen, false, false, true);
	}

	return userCancelledSave;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileBasedDocument.cpp ***/


/*** Start of inlined file: juce_RecentlyOpenedFilesList.cpp ***/
BEGIN_JUCE_NAMESPACE

RecentlyOpenedFilesList::RecentlyOpenedFilesList()
	: maxNumberOfItems (10)
{
}

RecentlyOpenedFilesList::~RecentlyOpenedFilesList()
{
}

void RecentlyOpenedFilesList::setMaxNumberOfItems (const int newMaxNumber)
{
	maxNumberOfItems = jmax (1, newMaxNumber);

	while (getNumFiles() > maxNumberOfItems)
		files.remove (getNumFiles() - 1);
}

int RecentlyOpenedFilesList::getNumFiles() const
{
	return files.size();
}

const File RecentlyOpenedFilesList::getFile (const int index) const
{
	return File (files [index]);
}

void RecentlyOpenedFilesList::clear()
{
	files.clear();
}

void RecentlyOpenedFilesList::addFile (const File& file)
{
	const String path (file.getFullPathName());

	files.removeString (path, true);
	files.insert (0, path);

	setMaxNumberOfItems (maxNumberOfItems);
}

void RecentlyOpenedFilesList::removeNonExistentFiles()
{
	for (int i = getNumFiles(); --i >= 0;)
		if (! getFile(i).exists())
			files.remove (i);
}

int RecentlyOpenedFilesList::createPopupMenuItems (PopupMenu& menuToAddTo,
													const int baseItemId,
													const bool showFullPaths,
													const bool dontAddNonExistentFiles,
													const File** filesToAvoid)
{
	int num = 0;

	for (int i = 0; i < getNumFiles(); ++i)
	{
		const File f (getFile(i));

		if ((! dontAddNonExistentFiles) || f.exists())
		{
			bool needsAvoiding = false;

			if (filesToAvoid != 0)
			{
				const File** avoid = filesToAvoid;

				while (*avoid != 0)
				{
					if (f == **avoid)
					{
						needsAvoiding = true;
						break;
					}

					++avoid;
				}
			}

			if (! needsAvoiding)
			{
				menuToAddTo.addItem (baseItemId + i,
									 showFullPaths ? f.getFullPathName()
												   : f.getFileName());
				++num;
			}
		}
	}

	return num;
}

const String RecentlyOpenedFilesList::toString() const
{
	return files.joinIntoString ("\n");
}

void RecentlyOpenedFilesList::restoreFromString (const String& stringifiedVersion)
{
	clear();
	files.addLines (stringifiedVersion);

	setMaxNumberOfItems (maxNumberOfItems);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_RecentlyOpenedFilesList.cpp ***/


/*** Start of inlined file: juce_UndoManager.cpp ***/
BEGIN_JUCE_NAMESPACE

UndoManager::UndoManager (const int maxNumberOfUnitsToKeep,
						  const int minimumTransactions)
   : totalUnitsStored (0),
	 nextIndex (0),
	 newTransaction (true),
	 reentrancyCheck (false)
{
	setMaxNumberOfStoredUnits (maxNumberOfUnitsToKeep,
							   minimumTransactions);
}

UndoManager::~UndoManager()
{
	clearUndoHistory();
}

void UndoManager::clearUndoHistory()
{
	transactions.clear();
	transactionNames.clear();
	totalUnitsStored = 0;
	nextIndex = 0;
	sendChangeMessage (this);
}

int UndoManager::getNumberOfUnitsTakenUpByStoredCommands() const
{
	return totalUnitsStored;
}

void UndoManager::setMaxNumberOfStoredUnits (const int maxNumberOfUnitsToKeep,
											 const int minimumTransactions)
{
	maxNumUnitsToKeep	  = jmax (1, maxNumberOfUnitsToKeep);
	minimumTransactionsToKeep  = jmax (1, minimumTransactions);
}

bool UndoManager::perform (UndoableAction* const command, const String& actionName)
{
	if (command != 0)
	{
		if (actionName.isNotEmpty())
			currentTransactionName = actionName;

		if (reentrancyCheck)
		{
			jassertfalse	// don't call perform() recursively from the UndoableAction::perform() or
							// undo() methods, or else these actions won't actually get done.

			return false;
		}
		else
		{
			bool success = false;

			JUCE_TRY
			{
				success = command->perform();
			}
			JUCE_CATCH_EXCEPTION

			jassert (success);
			if (success)
			{
				if (nextIndex > 0 && ! newTransaction)
				{
					OwnedArray<UndoableAction>* commandSet = transactions [nextIndex - 1];

					jassert (commandSet != 0);
					if (commandSet == 0)
						return false;

					commandSet->add (command);
				}
				else
				{
					OwnedArray<UndoableAction>* commandSet = new OwnedArray<UndoableAction>();
					commandSet->add (command);
					transactions.insert (nextIndex, commandSet);
					transactionNames.insert (nextIndex, currentTransactionName);
					++nextIndex;
				}

				totalUnitsStored += command->getSizeInUnits();
				newTransaction = false;
			}

			while (nextIndex < transactions.size())
			{
				const OwnedArray <UndoableAction>* const lastSet = transactions.getLast();

				for (int i = lastSet->size(); --i >= 0;)
					totalUnitsStored -= lastSet->getUnchecked (i)->getSizeInUnits();

				transactions.removeLast();
				transactionNames.remove (transactionNames.size() - 1);
			}

			while (nextIndex > 0
				   && totalUnitsStored > maxNumUnitsToKeep
				   && transactions.size() > minimumTransactionsToKeep)
			{
				const OwnedArray <UndoableAction>* const firstSet = transactions.getFirst();

				for (int i = firstSet->size(); --i >= 0;)
					totalUnitsStored -= firstSet->getUnchecked (i)->getSizeInUnits();

				jassert (totalUnitsStored >= 0); // something fishy going on if this fails!

				transactions.remove (0);
				transactionNames.remove (0);
				--nextIndex;
			}

			sendChangeMessage (this);

			return success;
		}
	}

	return false;
}

void UndoManager::beginNewTransaction (const String& actionName)
{
	newTransaction = true;
	currentTransactionName = actionName;
}

void UndoManager::setCurrentTransactionName (const String& newName)
{
	currentTransactionName = newName;
}

bool UndoManager::canUndo() const
{
	return nextIndex > 0;
}

bool UndoManager::canRedo() const
{
	return nextIndex < transactions.size();
}

const String UndoManager::getUndoDescription() const
{
	return transactionNames [nextIndex - 1];
}

const String UndoManager::getRedoDescription() const
{
	return transactionNames [nextIndex];
}

bool UndoManager::undo()
{
	const OwnedArray<UndoableAction>* const commandSet = transactions [nextIndex - 1];

	if (commandSet == 0)
		return false;

	reentrancyCheck = true;
	bool failed = false;

	for (int i = commandSet->size(); --i >= 0;)
	{
		if (! commandSet->getUnchecked(i)->undo())
		{
			jassertfalse
			failed = true;
			break;
		}
	}

	reentrancyCheck = false;

	if (failed)
	{
		clearUndoHistory();
	}
	else
	{
		--nextIndex;
	}

	beginNewTransaction();

	sendChangeMessage (this);
	return true;
}

bool UndoManager::redo()
{
	const OwnedArray<UndoableAction>* const commandSet = transactions [nextIndex];

	if (commandSet == 0)
		return false;

	reentrancyCheck = true;
	bool failed = false;

	for (int i = 0; i < commandSet->size(); ++i)
	{
		if (! commandSet->getUnchecked(i)->perform())
		{
			jassertfalse
			failed = true;
			break;
		}
	}

	reentrancyCheck = false;

	if (failed)
	{
		clearUndoHistory();
	}
	else
	{
		++nextIndex;
	}

	beginNewTransaction();

	sendChangeMessage (this);
	return true;
}

bool UndoManager::undoCurrentTransactionOnly()
{
	return newTransaction ? false
						  : undo();
}

void UndoManager::getActionsInCurrentTransaction (Array <const UndoableAction*>& actionsFound) const
{
	const OwnedArray <UndoableAction>* const commandSet = transactions [nextIndex - 1];

	if (commandSet != 0 && ! newTransaction)
	{
		for (int i = 0; i < commandSet->size(); ++i)
			actionsFound.add (commandSet->getUnchecked(i));
	}
}

int UndoManager::getNumActionsInCurrentTransaction() const
{
	const OwnedArray <UndoableAction>* const commandSet = transactions [nextIndex - 1];

	if (commandSet != 0 && ! newTransaction)
		return commandSet->size();

	return 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_UndoManager.cpp ***/


/*** Start of inlined file: juce_AiffAudioFormat.cpp ***/
BEGIN_JUCE_NAMESPACE

static const char* const aiffFormatName = "AIFF file";
static const juce_wchar* const aiffExtensions[] =    { T(".aiff"), T(".aif"), 0 };

class AiffAudioFormatReader  : public AudioFormatReader
{
public:
	int bytesPerFrame;
	int64 dataChunkStart;
	bool littleEndian;

	AiffAudioFormatReader (InputStream* in)
		: AudioFormatReader (in, TRANS (aiffFormatName))
	{
		if (input->readInt() == chunkName ("FORM"))
		{
			const int len = input->readIntBigEndian();
			const int64 end = input->getPosition() + len;

			const int nextType = input->readInt();
			if (nextType == chunkName ("AIFF") || nextType == chunkName ("AIFC"))
			{
				bool hasGotVer = false;
				bool hasGotData = false;
				bool hasGotType = false;

				while (input->getPosition() < end)
				{
					const int type = input->readInt();
					const uint32 length = (uint32) input->readIntBigEndian();
					const int64 chunkEnd = input->getPosition() + length;

					if (type == chunkName ("FVER"))
					{
						hasGotVer = true;

						const int ver = input->readIntBigEndian();
						if (ver != 0 && ver != (int)0xa2805140)
							break;
					}
					else if (type == chunkName ("COMM"))
					{
						hasGotType = true;

						numChannels = (unsigned int)input->readShortBigEndian();
						lengthInSamples = input->readIntBigEndian();
						bitsPerSample = input->readShortBigEndian();
						bytesPerFrame = (numChannels * bitsPerSample) >> 3;

						unsigned char sampleRateBytes[10];
						input->read (sampleRateBytes, 10);
						const int byte0 = sampleRateBytes[0];

						if ((byte0 & 0x80) != 0
							 || byte0 <= 0x3F || byte0 > 0x40
							 || (byte0 == 0x40 && sampleRateBytes[1] > 0x1C))
							break;

						unsigned int sampRate = ByteOrder::bigEndianInt (sampleRateBytes + 2);
						sampRate >>= (16414 - ByteOrder::bigEndianShort (sampleRateBytes));
						sampleRate = (int) sampRate;

						if (length <= 18)
						{
							// some types don't have a chunk large enough to include a compression
							// type, so assume it's just big-endian pcm
							littleEndian = false;
						}
						else
						{
							const int compType = input->readInt();

							if (compType == chunkName ("NONE") || compType == chunkName ("twos"))
							{
								littleEndian = false;
							}
							else if (compType == chunkName ("sowt"))
							{
								littleEndian = true;
							}
							else
							{
								sampleRate = 0;
								break;
							}
						}
					}
					else if (type == chunkName ("SSND"))
					{
						hasGotData = true;

						const int offset = input->readIntBigEndian();
						dataChunkStart = input->getPosition() + 4 + offset;
						lengthInSamples = (bytesPerFrame > 0) ? jmin (lengthInSamples, (int64) (length / bytesPerFrame)) : 0;
					}
					else if ((hasGotVer && hasGotData && hasGotType)
							  || chunkEnd < input->getPosition()
							  || input->isExhausted())
					{
						break;
					}

					input->setPosition (chunkEnd);
				}
			}
		}
	}

	~AiffAudioFormatReader()
	{
	}

	bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
					  int64 startSampleInFile, int numSamples)
	{
		const int64 samplesAvailable = lengthInSamples - startSampleInFile;

		if (samplesAvailable < numSamples)
		{
			for (int i = numDestChannels; --i >= 0;)
				if (destSamples[i] != 0)
					zeromem (destSamples[i] + startOffsetInDestBuffer, sizeof (int) * numSamples);

			numSamples = (int) samplesAvailable;
		}

		if (numSamples <= 0)
			return true;

		input->setPosition (dataChunkStart + startSampleInFile * bytesPerFrame);

		const int tempBufSize = 480 * 3 * 4; // (keep this a multiple of 3)
		char tempBuffer [tempBufSize];

		while (numSamples > 0)
		{
			int* left = destSamples[0];
			if (left != 0)
				left += startOffsetInDestBuffer;

			int* right = numDestChannels > 1 ? destSamples[1] : 0;
			if (right != 0)
				right += startOffsetInDestBuffer;

			const int numThisTime = jmin (tempBufSize / bytesPerFrame, numSamples);
			const int bytesRead = input->read (tempBuffer, numThisTime * bytesPerFrame);

			if (bytesRead < numThisTime * bytesPerFrame)
				zeromem (tempBuffer + bytesRead, numThisTime * bytesPerFrame - bytesRead);

			if (bitsPerSample == 16)
			{
				if (littleEndian)
				{
					const short* src = reinterpret_cast <const short*> (tempBuffer);

					if (numChannels > 1)
					{
						if (left == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*right++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
								++src;
							}
						}
						else if (right == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								++src;
								*left++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
							}
						}
						else
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*left++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
								*right++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
							}
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
						}
					}
				}
				else
				{
					const char* src = tempBuffer;

					if (numChannels > 1)
					{
						if (left == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*right++ = ByteOrder::bigEndianShort (src) << 16;
								src += 4;
							}
						}
						else if (right == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								src += 2;
								*left++ = ByteOrder::bigEndianShort (src) << 16;
								src += 2;
							}
						}
						else
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*left++ = ByteOrder::bigEndianShort (src) << 16;
								src += 2;
								*right++ = ByteOrder::bigEndianShort (src) << 16;
								src += 2;
							}
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = ByteOrder::bigEndianShort (src) << 16;
							src += 2;
						}
					}
				}
			}
			else if (bitsPerSample == 24)
			{
				const char* src = (const char*)tempBuffer;

				if (littleEndian)
				{
					if (numChannels > 1)
					{
						if (left == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*right++ = ByteOrder::littleEndian24Bit (src) << 8;
								src += 6;
							}
						}
						else if (right == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								src += 3;
								*left++ = ByteOrder::littleEndian24Bit (src) << 8;
								src += 3;
							}
						}
						else
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*left++ = ByteOrder::littleEndian24Bit (src) << 8;
								src += 3;
								*right++ = ByteOrder::littleEndian24Bit (src) << 8;
								src += 3;
							}
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = ByteOrder::littleEndian24Bit (src) << 8;
							src += 3;
						}
					}
				}
				else
				{
					if (numChannels > 1)
					{
						if (left == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*right++ = ByteOrder::bigEndian24Bit (src) << 8;
								src += 6;
							}
						}
						else if (right == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								src += 3;
								*left++ = ByteOrder::bigEndian24Bit (src) << 8;
								src += 3;
							}
						}
						else
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*left++ = ByteOrder::bigEndian24Bit (src) << 8;
								src += 3;
								*right++ = ByteOrder::bigEndian24Bit (src) << 8;
								src += 3;
							}
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = ByteOrder::bigEndian24Bit (src) << 8;
							src += 3;
						}
					}
				}
			}
			else if (bitsPerSample == 32)
			{
				const unsigned int* src = reinterpret_cast <const unsigned int*> (tempBuffer);
				unsigned int* l = reinterpret_cast <unsigned int*> (left);
				unsigned int* r = reinterpret_cast <unsigned int*> (right);

				if (littleEndian)
				{
					if (numChannels > 1)
					{
						if (l == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								++src;
								*r++ = ByteOrder::swapIfBigEndian (*src++);
							}
						}
						else if (r == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*l++ = ByteOrder::swapIfBigEndian (*src++);
								++src;
							}
						}
						else
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*l++ = ByteOrder::swapIfBigEndian (*src++);
								*r++ = ByteOrder::swapIfBigEndian (*src++);
							}
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*l++ = ByteOrder::swapIfBigEndian (*src++);
						}
					}
				}
				else
				{
					if (numChannels > 1)
					{
						if (l == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								++src;
								*r++ = ByteOrder::swapIfLittleEndian (*src++);
							}
						}
						else if (r == 0)
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*l++ = ByteOrder::swapIfLittleEndian (*src++);
								++src;
							}
						}
						else
						{
							for (int i = numThisTime; --i >= 0;)
							{
								*l++ = ByteOrder::swapIfLittleEndian (*src++);
								*r++ = ByteOrder::swapIfLittleEndian (*src++);
							}
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*l++ = ByteOrder::swapIfLittleEndian (*src++);
						}
					}
				}

				left = reinterpret_cast <int*> (l);
				right = reinterpret_cast <int*> (r);
			}
			else if (bitsPerSample == 8)
			{
				const char* src = tempBuffer;

				if (numChannels > 1)
				{
					if (left == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*right++ = ((int) *src++) << 24;
							++src;
						}
					}
					else if (right == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							++src;
							*left++ = ((int) *src++) << 24;
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = ((int) *src++) << 24;
							*right++ = ((int) *src++) << 24;
						}
					}
				}
				else
				{
					for (int i = numThisTime; --i >= 0;)
					{
						*left++ = ((int) *src++) << 24;
					}
				}
			}

			startOffsetInDestBuffer += numThisTime;
			numSamples -= numThisTime;
		}

		if (numSamples > 0)
		{
			for (int i = numDestChannels; --i >= 0;)
				if (destSamples[i] != 0)
					zeromem (destSamples[i] + startOffsetInDestBuffer,
							 sizeof (int) * numSamples);
		}

		return true;
	}

	juce_UseDebuggingNewOperator

private:
	AiffAudioFormatReader (const AiffAudioFormatReader&);
	AiffAudioFormatReader& operator= (const AiffAudioFormatReader&);

	static inline int chunkName (const char* const name)   { return (int) ByteOrder::littleEndianInt (name); }
};

class AiffAudioFormatWriter  : public AudioFormatWriter
{
	MemoryBlock tempBlock;
	uint32 lengthInSamples, bytesWritten;
	int64 headerPosition;
	bool writeFailed;

	static inline int chunkName (const char* const name)   { return (int) ByteOrder::littleEndianInt (name); }

	AiffAudioFormatWriter (const AiffAudioFormatWriter&);
	AiffAudioFormatWriter& operator= (const AiffAudioFormatWriter&);

	void writeHeader()
	{
		const bool couldSeekOk = output->setPosition (headerPosition);
		(void) couldSeekOk;

		// if this fails, you've given it an output stream that can't seek! It needs
		// to be able to seek back to write the header
		jassert (couldSeekOk);

		const int headerLen = 54;
		int audioBytes = lengthInSamples * ((bitsPerSample * numChannels) / 8);
		audioBytes += (audioBytes & 1);

		output->writeInt (chunkName ("FORM"));
		output->writeIntBigEndian (headerLen + audioBytes - 8);
		output->writeInt (chunkName ("AIFF"));
		output->writeInt (chunkName ("COMM"));
		output->writeIntBigEndian (18);
		output->writeShortBigEndian ((short) numChannels);
		output->writeIntBigEndian (lengthInSamples);
		output->writeShortBigEndian ((short) bitsPerSample);

		uint8 sampleRateBytes[10];
		zeromem (sampleRateBytes, 10);

		if (sampleRate <= 1)
		{
			sampleRateBytes[0] = 0x3f;
			sampleRateBytes[1] = 0xff;
			sampleRateBytes[2] = 0x80;
		}
		else
		{
			int mask = 0x40000000;
			sampleRateBytes[0] = 0x40;

			if (sampleRate >= mask)
			{
				jassertfalse
				sampleRateBytes[1] = 0x1d;
			}
			else
			{
				int n = (int) sampleRate;

				int i;
				for (i = 0; i <= 32 ; ++i)
				{
					if ((n & mask) != 0)
						break;

					mask >>= 1;
				}

				n = n << (i + 1);

				sampleRateBytes[1] = (uint8) (29 - i);
				sampleRateBytes[2] = (uint8) ((n >> 24) & 0xff);
				sampleRateBytes[3] = (uint8) ((n >> 16) & 0xff);
				sampleRateBytes[4] = (uint8) ((n >>  8) & 0xff);
				sampleRateBytes[5] = (uint8) (n & 0xff);
			}
		}

		output->write (sampleRateBytes, 10);

		output->writeInt (chunkName ("SSND"));
		output->writeIntBigEndian (audioBytes + 8);
		output->writeInt (0);
		output->writeInt (0);

		jassert (output->getPosition() == headerLen);
	}

public:

	AiffAudioFormatWriter (OutputStream* out,
						   const double sampleRate_,
						   const unsigned int chans,
						   const int bits)
		: AudioFormatWriter (out,
							 TRANS (aiffFormatName),
							 sampleRate_,
							 chans,
							 bits),
		  lengthInSamples (0),
		  bytesWritten (0),
		  writeFailed (false)
	{
		headerPosition = out->getPosition();
		writeHeader();
	}

	~AiffAudioFormatWriter()
	{
		if ((bytesWritten & 1) != 0)
			output->writeByte (0);

		writeHeader();
	}

	bool write (const int** data, int numSamples)
	{
		if (writeFailed)
			return false;

		const int bytes = numChannels * numSamples * bitsPerSample / 8;
		tempBlock.ensureSize (bytes, false);
		char* buffer = static_cast <char*> (tempBlock.getData());

		const int* left = data[0];
		const int* right = data[1];
		if (right == 0)
			right = left;

		if (bitsPerSample == 16)
		{
			short* b = reinterpret_cast <short*> (buffer);

			if (numChannels > 1)
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = (short) ByteOrder::swapIfLittleEndian ((uint16) (*left++ >> 16));
					*b++ = (short) ByteOrder::swapIfLittleEndian ((uint16) (*right++ >> 16));
				}
			}
			else
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = (short) ByteOrder::swapIfLittleEndian ((uint16) (*left++ >> 16));
				}
			}
		}
		else if (bitsPerSample == 24)
		{
			char* b = buffer;

			if (numChannels > 1)
			{
				for (int i = numSamples; --i >= 0;)
				{
					ByteOrder::bigEndian24BitToChars (*left++ >> 8, b);
					b += 3;
					ByteOrder::bigEndian24BitToChars (*right++ >> 8, b);
					b += 3;
				}
			}
			else
			{
				for (int i = numSamples; --i >= 0;)
				{
					ByteOrder::bigEndian24BitToChars (*left++ >> 8, b);
					b += 3;
				}
			}
		}
		else if (bitsPerSample == 32)
		{
			uint32* b = reinterpret_cast <uint32*> (buffer);

			if (numChannels > 1)
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = ByteOrder::swapIfLittleEndian ((uint32) *left++);
					*b++ = ByteOrder::swapIfLittleEndian ((uint32) *right++);
				}
			}
			else
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = ByteOrder::swapIfLittleEndian ((uint32) *left++);
				}
			}
		}
		else if (bitsPerSample == 8)
		{
			char* b = buffer;

			if (numChannels > 1)
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = (char) (*left++ >> 24);
					*b++ = (char) (*right++ >> 24);
				}
			}
			else
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = (char) (*left++ >> 24);
				}
			}
		}

		if (bytesWritten + bytes >= (uint32) 0xfff00000
			 || ! output->write (buffer, bytes))
		{
			// failed to write to disk, so let's try writing the header.
			// If it's just run out of disk space, then if it does manage
			// to write the header, we'll still have a useable file..
			writeHeader();
			writeFailed = true;
			return false;
		}
		else
		{
			bytesWritten += bytes;
			lengthInSamples += numSamples;

			return true;
		}
	}

	juce_UseDebuggingNewOperator
};

AiffAudioFormat::AiffAudioFormat()
	: AudioFormat (TRANS (aiffFormatName), (const juce_wchar**) aiffExtensions)
{
}

AiffAudioFormat::~AiffAudioFormat()
{
}

const Array <int> AiffAudioFormat::getPossibleSampleRates()
{
	const int rates[] = { 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 0 };
	return Array <int> (rates);
}

const Array <int> AiffAudioFormat::getPossibleBitDepths()
{
	const int depths[] = { 8, 16, 24, 0 };
	return Array <int> (depths);
}

bool AiffAudioFormat::canDoStereo()
{
	return true;
}

bool AiffAudioFormat::canDoMono()
{
	return true;
}

#if JUCE_MAC
bool AiffAudioFormat::canHandleFile (const File& f)
{
	if (AudioFormat::canHandleFile (f))
		return true;

	const OSType type = PlatformUtilities::getTypeOfFile (f.getFullPathName());
	return type == 'AIFF' || type == 'AIFC'
		|| type == 'aiff' || type == 'aifc';
}
#endif

AudioFormatReader* AiffAudioFormat::createReaderFor (InputStream* sourceStream,
													 const bool deleteStreamIfOpeningFails)
{
	ScopedPointer <AiffAudioFormatReader> w (new AiffAudioFormatReader (sourceStream));

	if (w->sampleRate != 0)
		return w.release();

	if (! deleteStreamIfOpeningFails)
		w->input = 0;

	return 0;
}

AudioFormatWriter* AiffAudioFormat::createWriterFor (OutputStream* out,
													 double sampleRate,
													 unsigned int chans,
													 int bitsPerSample,
													 const StringPairArray& /*metadataValues*/,
													 int /*qualityOptionIndex*/)
{
	if (getPossibleBitDepths().contains (bitsPerSample))
	{
		return new AiffAudioFormatWriter (out,
										  sampleRate,
										  chans,
										  bitsPerSample);
	}

	return 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AiffAudioFormat.cpp ***/


/*** Start of inlined file: juce_AudioCDReader.cpp ***/
BEGIN_JUCE_NAMESPACE

#if JUCE_MAC && JUCE_USE_CDREADER

// Mac version doesn't need any native code because it's all done with files..
// Windows + Linux versions are in the platform-dependent code sections.

static void findCDs (Array<File>& cds)
{
	File volumes ("/Volumes");
	volumes.findChildFiles (cds, File::findDirectories, false);

	for (int i = cds.size(); --i >= 0;)
		if (! cds.getReference(i).getChildFile (".TOC.plist").exists())
			cds.remove (i);
}

const StringArray AudioCDReader::getAvailableCDNames()
{
	Array<File> cds;
	findCDs (cds);

	StringArray names;

	for (int i = 0; i < cds.size(); ++i)
		names.add (cds.getReference(i).getFileName());

	return names;
}

AudioCDReader* AudioCDReader::createReaderForCD (const int index)
{
	Array<File> cds;
	findCDs (cds);

	if (cds[index] != File::nonexistent)
		return new AudioCDReader (cds[index]);
	else
		return 0;
}

AudioCDReader::AudioCDReader (const File& volume)
   : AudioFormatReader (0, "CD Audio"),
	 volumeDir (volume),
	 currentReaderTrack (-1),
	 reader (0)
{
	 sampleRate = 44100.0;
	 bitsPerSample = 16;
	 numChannels = 2;
	 usesFloatingPointData = false;

	 refreshTrackLengths();
}

AudioCDReader::~AudioCDReader()
{
}

static int getTrackNumber (const File& file)
{
	return file.getFileName()
			   .initialSectionContainingOnly ("0123456789")
			   .getIntValue();
}

int AudioCDReader::compareElements (const File& first, const File& second)
{
	const int firstTrack  = getTrackNumber (first);
	const int secondTrack = getTrackNumber (second);

	jassert (firstTrack > 0 && secondTrack > 0);

	return firstTrack - secondTrack;
}

void AudioCDReader::refreshTrackLengths()
{
	tracks.clear();
	trackStartSamples.clear();
	volumeDir.findChildFiles (tracks, File::findFiles | File::ignoreHiddenFiles, false, "*.aiff");

	tracks.sort (*this);

	AiffAudioFormat format;
	int sample = 0;

	for (int i = 0; i < tracks.size(); ++i)
	{
		trackStartSamples.add (sample);

		FileInputStream* const in = tracks.getReference(i).createInputStream();

		if (in != 0)
		{
			ScopedPointer <AudioFormatReader> r (format.createReaderFor (in, true));

			if (r != 0)
				sample += (int) r->lengthInSamples;
		}
	}

	trackStartSamples.add (sample);
	lengthInSamples = sample;
}

bool AudioCDReader::readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
								 int64 startSampleInFile, int numSamples)
{
	while (numSamples > 0)
	{
		int track = -1;

		for (int i = 0; i < trackStartSamples.size() - 1; ++i)
		{
			if (startSampleInFile < trackStartSamples.getUnchecked (i + 1))
			{
				track = i;
				break;
			}
		}

		if (track < 0)
			return false;

		if (track != currentReaderTrack)
		{
			reader = 0;

			FileInputStream* const in = tracks [track].createInputStream();

			if (in != 0)
			{
				BufferedInputStream* const bin = new BufferedInputStream (in, 65536, true);

				AiffAudioFormat format;
				reader = format.createReaderFor (bin, true);

				if (reader == 0)
					currentReaderTrack = -1;
				else
					currentReaderTrack = track;
			}
		}

		if (reader == 0)
			return false;

		const int startPos = (int) (startSampleInFile - trackStartSamples.getUnchecked (track));
		const int numAvailable = (int) jmin ((int64) numSamples, reader->lengthInSamples - startPos);

		reader->readSamples (destSamples, numDestChannels, startOffsetInDestBuffer, startPos, numAvailable);

		numSamples -= numAvailable;
		startSampleInFile += numAvailable;
	}

	return true;
}

bool AudioCDReader::isCDStillPresent() const
{
	return volumeDir.exists();
}

int AudioCDReader::getNumTracks() const
{
	return tracks.size();
}

int AudioCDReader::getPositionOfTrackStart (int trackNum) const
{
	return trackStartSamples [trackNum];
}

bool AudioCDReader::isTrackAudio (int trackNum) const
{
	return tracks [trackNum] != File::nonexistent;
}

void AudioCDReader::enableIndexScanning (bool b)
{
	// any way to do this on a Mac??
}

int AudioCDReader::getLastIndex() const
{
	return 0;
}

const Array <int> AudioCDReader::findIndexesInTrack (const int trackNumber)
{
	return Array <int>();
}

int AudioCDReader::getCDDBId()
{
	return 0; //xxx
}

#endif

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioCDReader.cpp ***/


/*** Start of inlined file: juce_AudioFormat.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioFormatReader::AudioFormatReader (InputStream* const in,
									  const String& formatName_)
	: sampleRate (0),
	  bitsPerSample (0),
	  lengthInSamples (0),
	  numChannels (0),
	  usesFloatingPointData (false),
	  input (in),
	  formatName (formatName_)
{
}

AudioFormatReader::~AudioFormatReader()
{
	delete input;
}

bool AudioFormatReader::read (int** destSamples,
							  int numDestChannels,
							  int64 startSampleInSource,
							  int numSamplesToRead,
							  const bool fillLeftoverChannelsWithCopies)
{
	jassert (numDestChannels > 0); // you have to actually give this some channels to work with!

	int startOffsetInDestBuffer = 0;

	if (startSampleInSource < 0)
	{
		const int silence = (int) jmin (-startSampleInSource, (int64) numSamplesToRead);

		for (int i = numDestChannels; --i >= 0;)
			if (destSamples[i] != 0)
				zeromem (destSamples[i], sizeof (int) * silence);

		startOffsetInDestBuffer += silence;
		numSamplesToRead -= silence;
		startSampleInSource = 0;
	}

	if (numSamplesToRead <= 0)
		return true;

	if (! readSamples (destSamples, jmin ((int) numChannels, numDestChannels), startOffsetInDestBuffer,
					   startSampleInSource, numSamplesToRead))
		return false;

	if (numDestChannels > (int) numChannels)
	{
		if (fillLeftoverChannelsWithCopies)
		{
			int* lastFullChannel = destSamples[0];

			for (int i = numDestChannels; --i > 0;)
			{
				if (destSamples[i] != 0)
				{
					lastFullChannel = destSamples[i];
					break;
				}
			}

			if (lastFullChannel != 0)
				for (int i = numChannels; i < numDestChannels; ++i)
					if (destSamples[i] != 0)
						memcpy (destSamples[i], lastFullChannel, sizeof (int) * numSamplesToRead);
		}
		else
		{
			for (int i = numChannels; i < numDestChannels; ++i)
				if (destSamples[i] != 0)
					zeromem (destSamples[i], sizeof (int) * numSamplesToRead);
		}
	}

	return true;
}

static void findAudioBufferMaxMin (const float* const buffer, const int num, float& maxVal, float& minVal) throw()
{
	float mn = buffer[0];
	float mx = mn;

	for (int i = 1; i < num; ++i)
	{
		const float s = buffer[i];
		if (s > mx)  mx = s;
		if (s < mn)  mn = s;
	}

	maxVal = mx;
	minVal = mn;
}

void AudioFormatReader::readMaxLevels (int64 startSampleInFile,
									   int64 numSamples,
									   float& lowestLeft, float& highestLeft,
									   float& lowestRight, float& highestRight)
{
	if (numSamples <= 0)
	{
		lowestLeft = 0;
		lowestRight = 0;
		highestLeft = 0;
		highestRight = 0;
		return;
	}

	const int bufferSize = (int) jmin (numSamples, (int64) 4096);
	MemoryBlock tempSpace (bufferSize * sizeof (int) * 2 + 64);

	int* tempBuffer[3];
	tempBuffer[0] = (int*) tempSpace.getData();
	tempBuffer[1] = ((int*) tempSpace.getData()) + bufferSize;
	tempBuffer[2] = 0;

	if (usesFloatingPointData)
	{
		float lmin = 1.0e6f;
		float lmax = -lmin;
		float rmin = lmin;
		float rmax = lmax;

		while (numSamples > 0)
		{
			const int numToDo = (int) jmin (numSamples, (int64) bufferSize);
			read ((int**) tempBuffer, 2, startSampleInFile, numToDo, false);

			numSamples -= numToDo;
			startSampleInFile += numToDo;

			float bufmin, bufmax;
			findAudioBufferMaxMin ((float*) tempBuffer[0], numToDo, bufmax, bufmin);
			lmin = jmin (lmin, bufmin);
			lmax = jmax (lmax, bufmax);

			if (numChannels > 1)
			{
				findAudioBufferMaxMin ((float*) tempBuffer[1], numToDo, bufmax, bufmin);
				rmin = jmin (rmin, bufmin);
				rmax = jmax (rmax, bufmax);
			}
		}

		if (numChannels <= 1)
		{
			rmax = lmax;
			rmin = lmin;
		}

		lowestLeft = lmin;
		highestLeft = lmax;
		lowestRight = rmin;
		highestRight = rmax;
	}
	else
	{
		int lmax = std::numeric_limits<int>::min();
		int lmin = std::numeric_limits<int>::max();
		int rmax = std::numeric_limits<int>::min();
		int rmin = std::numeric_limits<int>::max();

		while (numSamples > 0)
		{
			const int numToDo = (int) jmin (numSamples, (int64) bufferSize);
			read ((int**) tempBuffer, 2, startSampleInFile, numToDo, false);

			numSamples -= numToDo;
			startSampleInFile += numToDo;

			for (int j = numChannels; --j >= 0;)
			{
				int bufMax = std::numeric_limits<int>::min();
				int bufMin = std::numeric_limits<int>::max();

				const int* const b = tempBuffer[j];

				for (int i = 0; i < numToDo; ++i)
				{
					const int samp = b[i];

					if (samp < bufMin)
						bufMin = samp;

					if (samp > bufMax)
						bufMax = samp;
				}

				if (j == 0)
				{
					lmax = jmax (lmax, bufMax);
					lmin = jmin (lmin, bufMin);
				}
				else
				{
					rmax = jmax (rmax, bufMax);
					rmin = jmin (rmin, bufMin);
				}
			}
		}

		if (numChannels <= 1)
		{
			rmax = lmax;
			rmin = lmin;
		}

		lowestLeft = lmin / (float) std::numeric_limits<int>::max();
		highestLeft = lmax / (float) std::numeric_limits<int>::max();
		lowestRight = rmin / (float) std::numeric_limits<int>::max();
		highestRight = rmax / (float) std::numeric_limits<int>::max();
	}
}

int64 AudioFormatReader::searchForLevel (int64 startSample,
										 int64 numSamplesToSearch,
										 const double magnitudeRangeMinimum,
										 const double magnitudeRangeMaximum,
										 const int minimumConsecutiveSamples)
{
	if (numSamplesToSearch == 0)
		return -1;

	const int bufferSize = 4096;
	MemoryBlock tempSpace (bufferSize * sizeof (int) * 2 + 64);

	int* tempBuffer[3];
	tempBuffer[0] = (int*) tempSpace.getData();
	tempBuffer[1] = ((int*) tempSpace.getData()) + bufferSize;
	tempBuffer[2] = 0;

	int consecutive = 0;
	int64 firstMatchPos = -1;

	jassert (magnitudeRangeMaximum > magnitudeRangeMinimum);

	const double doubleMin = jlimit (0.0, (double) std::numeric_limits<int>::max(), magnitudeRangeMinimum * std::numeric_limits<int>::max());
	const double doubleMax = jlimit (doubleMin, (double) std::numeric_limits<int>::max(), magnitudeRangeMaximum * std::numeric_limits<int>::max());
	const int intMagnitudeRangeMinimum = roundToInt (doubleMin);
	const int intMagnitudeRangeMaximum = roundToInt (doubleMax);

	while (numSamplesToSearch != 0)
	{
		const int numThisTime = (int) jmin (abs64 (numSamplesToSearch), (int64) bufferSize);
		int64 bufferStart = startSample;

		if (numSamplesToSearch < 0)
			bufferStart -= numThisTime;

		if (bufferStart >= (int) lengthInSamples)
			break;

		read ((int**) tempBuffer, 2, bufferStart, numThisTime, false);

		int num = numThisTime;
		while (--num >= 0)
		{
			if (numSamplesToSearch < 0)
				--startSample;

			bool matches = false;
			const int index = (int) (startSample - bufferStart);

			if (usesFloatingPointData)
			{
				const float sample1 = fabsf (((float*) tempBuffer[0]) [index]);

				if (sample1 >= magnitudeRangeMinimum
					 && sample1 <= magnitudeRangeMaximum)
				{
					matches = true;
				}
				else if (numChannels > 1)
				{
					const float sample2 = fabsf (((float*) tempBuffer[1]) [index]);

					matches = (sample2 >= magnitudeRangeMinimum
								 && sample2 <= magnitudeRangeMaximum);
				}
			}
			else
			{
				const int sample1 = abs (tempBuffer[0] [index]);

				if (sample1 >= intMagnitudeRangeMinimum
					 && sample1 <= intMagnitudeRangeMaximum)
				{
					matches = true;
				}
				else if (numChannels > 1)
				{
					const int sample2 = abs (tempBuffer[1][index]);

					matches = (sample2 >= intMagnitudeRangeMinimum
								 && sample2 <= intMagnitudeRangeMaximum);
				}
			}

			if (matches)
			{
				if (firstMatchPos < 0)
					firstMatchPos = startSample;

				if (++consecutive >= minimumConsecutiveSamples)
				{
					if (firstMatchPos < 0 || firstMatchPos >= lengthInSamples)
						return -1;

					return firstMatchPos;
				}
			}
			else
			{
				consecutive = 0;
				firstMatchPos = -1;
			}

			if (numSamplesToSearch > 0)
				++startSample;
		}

		if (numSamplesToSearch > 0)
			numSamplesToSearch -= numThisTime;
		else
			numSamplesToSearch += numThisTime;
	}

	return -1;
}

AudioFormatWriter::AudioFormatWriter (OutputStream* const out,
									  const String& formatName_,
									  const double rate,
									  const unsigned int numChannels_,
									  const unsigned int bitsPerSample_)
  : sampleRate (rate),
	numChannels (numChannels_),
	bitsPerSample (bitsPerSample_),
	usesFloatingPointData (false),
	output (out),
	formatName (formatName_)
{
}

AudioFormatWriter::~AudioFormatWriter()
{
	delete output;
}

bool AudioFormatWriter::writeFromAudioReader (AudioFormatReader& reader,
											  int64 startSample,
											  int64 numSamplesToRead)
{
	const int bufferSize = 16384;
	const int maxChans = 128;
	AudioSampleBuffer tempBuffer (reader.numChannels, bufferSize);
	int* buffers [maxChans];

	for (int i = maxChans; --i >= 0;)
		buffers[i] = 0;

	if (numSamplesToRead < 0)
		numSamplesToRead = reader.lengthInSamples;

	while (numSamplesToRead > 0)
	{
		const int numToDo = (int) jmin (numSamplesToRead, (int64) bufferSize);

		for (int i = tempBuffer.getNumChannels(); --i >= 0;)
			buffers[i] = (int*) tempBuffer.getSampleData (i, 0);

		if (! reader.read (buffers, maxChans, startSample, numToDo, false))
			return false;

		if (reader.usesFloatingPointData != isFloatingPoint())
		{
			int** bufferChan = buffers;

			while (*bufferChan != 0)
			{
				int* b = *bufferChan++;

				if (isFloatingPoint())
				{
					// int -> float
					const double factor = 1.0 / std::numeric_limits<int>::max();

					for (int i = 0; i < numToDo; ++i)
						((float*) b)[i] = (float) (factor * b[i]);
				}
				else
				{
					// float -> int
					for (int i = 0; i < numToDo; ++i)
					{
						const double samp = *(const float*) b;

						if (samp <= -1.0)
							*b++ = std::numeric_limits<int>::min();
						else if (samp >= 1.0)
							*b++ = std::numeric_limits<int>::max();
						else
							*b++ = roundToInt (std::numeric_limits<int>::max() * samp);
					}
				}
			}
		}

		if (! write ((const int**) buffers, numToDo))
			return false;

		numSamplesToRead -= numToDo;
		startSample += numToDo;
	}

	return true;
}

bool AudioFormatWriter::writeFromAudioSource (AudioSource& source,
											  int numSamplesToRead,
											  const int samplesPerBlock)
{
	const int maxChans = 128;
	AudioSampleBuffer tempBuffer (getNumChannels(), samplesPerBlock);
	int* buffers [maxChans];

	while (numSamplesToRead > 0)
	{
		const int numToDo = jmin (numSamplesToRead, samplesPerBlock);

		AudioSourceChannelInfo info;
		info.buffer = &tempBuffer;
		info.startSample = 0;
		info.numSamples = numToDo;
		info.clearActiveBufferRegion();

		source.getNextAudioBlock (info);

		int i;
		for (i = maxChans; --i >= 0;)
			buffers[i] = 0;

		for (i = tempBuffer.getNumChannels(); --i >= 0;)
			buffers[i] = (int*) tempBuffer.getSampleData (i, 0);

		if (! isFloatingPoint())
		{
			int** bufferChan = buffers;

			while (*bufferChan != 0)
			{
				int* b = *bufferChan++;

				// float -> int
				for (int j = numToDo; --j >= 0;)
				{
					const double samp = *(const float*) b;

					if (samp <= -1.0)
						*b++ = std::numeric_limits<int>::min();
					else if (samp >= 1.0)
						*b++ = std::numeric_limits<int>::max();
					else
						*b++ = roundToInt (std::numeric_limits<int>::max() * samp);
				}
			}
		}

		if (! write ((const int**) buffers, numToDo))
			return false;

		numSamplesToRead -= numToDo;
	}

	return true;
}

AudioFormat::AudioFormat (const String& name,
						  const juce_wchar** const extensions)
  : formatName (name),
	fileExtensions (extensions)
{
}

AudioFormat::~AudioFormat()
{
}

const String& AudioFormat::getFormatName() const
{
	return formatName;
}

const StringArray& AudioFormat::getFileExtensions() const
{
	return fileExtensions;
}

bool AudioFormat::canHandleFile (const File& f)
{
	for (int i = 0; i < fileExtensions.size(); ++i)
		if (f.hasFileExtension (fileExtensions[i]))
			return true;

	return false;
}

bool AudioFormat::isCompressed()
{
	return false;
}

const StringArray AudioFormat::getQualityOptions()
{
	return StringArray();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioFormat.cpp ***/


/*** Start of inlined file: juce_AudioFormatManager.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioFormatManager::AudioFormatManager()
	: defaultFormatIndex (0)
{
}

AudioFormatManager::~AudioFormatManager()
{
	clearFormats();
	clearSingletonInstance();
}

juce_ImplementSingleton (AudioFormatManager);

void AudioFormatManager::registerFormat (AudioFormat* newFormat,
										 const bool makeThisTheDefaultFormat)
{
	jassert (newFormat != 0);

	if (newFormat != 0)
	{
#ifdef JUCE_DEBUG
		for (int i = getNumKnownFormats(); --i >= 0;)
		{
			if (getKnownFormat (i)->getFormatName() == newFormat->getFormatName())
			{
				jassertfalse // trying to add the same format twice!
			}
		}
#endif

		if (makeThisTheDefaultFormat)
			defaultFormatIndex = knownFormats.size();

		knownFormats.add (newFormat);
	}
}

void AudioFormatManager::registerBasicFormats()
{
#if JUCE_MAC
	registerFormat (new AiffAudioFormat(), true);
	registerFormat (new WavAudioFormat(), false);
#else
	registerFormat (new WavAudioFormat(), true);
	registerFormat (new AiffAudioFormat(), false);
#endif

#if JUCE_USE_FLAC
	registerFormat (new FlacAudioFormat(), false);
#endif

#if JUCE_USE_OGGVORBIS
	registerFormat (new OggVorbisAudioFormat(), false);
#endif
}

void AudioFormatManager::clearFormats()
{
	for (int i = getNumKnownFormats(); --i >= 0;)
		delete getKnownFormat(i);

	knownFormats.clear();
	defaultFormatIndex = 0;
}

int AudioFormatManager::getNumKnownFormats() const
{
	return knownFormats.size();
}

AudioFormat* AudioFormatManager::getKnownFormat (const int index) const
{
	return (AudioFormat*) knownFormats [index];
}

AudioFormat* AudioFormatManager::getDefaultFormat() const
{
	return getKnownFormat (defaultFormatIndex);
}

AudioFormat* AudioFormatManager::findFormatForFileExtension (const String& fileExtension) const
{
	String e (fileExtension);
	if (! e.startsWithChar ('.'))
		e = "." + e;

	for (int i = 0; i < getNumKnownFormats(); ++i)
		if (getKnownFormat(i)->getFileExtensions().contains (e, true))
			return getKnownFormat(i);

	return 0;
}

const String AudioFormatManager::getWildcardForAllFormats() const
{
	StringArray allExtensions;

	int i;
	for (i = 0; i < getNumKnownFormats(); ++i)
		allExtensions.addArray (getKnownFormat (i)->getFileExtensions());

	allExtensions.trim();
	allExtensions.removeEmptyStrings();

	String s;
	for (i = 0; i < allExtensions.size(); ++i)
	{
		s << '*';

		if (! allExtensions[i].startsWithChar ('.'))
			s << '.';

		s << allExtensions[i];

		if (i < allExtensions.size() - 1)
			s << ';';
	}

	return s;
}

AudioFormatReader* AudioFormatManager::createReaderFor (const File& file)
{
	// you need to actually register some formats before the manager can
	// use them to open a file!
	jassert (knownFormats.size() > 0);

	for (int i = 0; i < getNumKnownFormats(); ++i)
	{
		AudioFormat* const af = getKnownFormat(i);

		if (af->canHandleFile (file))
		{
			InputStream* const in = file.createInputStream();

			if (in != 0)
			{
				AudioFormatReader* const r = af->createReaderFor (in, true);

				if (r != 0)
					return r;
			}
		}
	}

	return 0;
}

AudioFormatReader* AudioFormatManager::createReaderFor (InputStream* audioFileStream)
{
	// you need to actually register some formats before the manager can
	// use them to open a file!
	jassert (knownFormats.size() > 0);

	ScopedPointer <InputStream> in (audioFileStream);

	if (in != 0)
	{
		const int64 originalStreamPos = in->getPosition();

		for (int i = 0; i < getNumKnownFormats(); ++i)
		{
			AudioFormatReader* const r = getKnownFormat(i)->createReaderFor (in, false);

			if (r != 0)
			{
				in.release();
				return r;
			}

			in->setPosition (originalStreamPos);

			// the stream that is passed-in must be capable of being repositioned so
			// that all the formats can have a go at opening it.
			jassert (in->getPosition() == originalStreamPos);
		}
	}

	return 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioFormatManager.cpp ***/


/*** Start of inlined file: juce_AudioSubsectionReader.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioSubsectionReader::AudioSubsectionReader (AudioFormatReader* const source_,
											  const int64 startSample_,
											  const int64 length_,
											  const bool deleteSourceWhenDeleted_)
   : AudioFormatReader (0, source_->getFormatName()),
	 source (source_),
	 startSample (startSample_),
	 deleteSourceWhenDeleted (deleteSourceWhenDeleted_)
{
	length = jmin (jmax ((int64) 0, source->lengthInSamples - startSample), length_);

	sampleRate = source->sampleRate;
	bitsPerSample = source->bitsPerSample;
	lengthInSamples = length;
	numChannels = source->numChannels;
	usesFloatingPointData = source->usesFloatingPointData;
}

AudioSubsectionReader::~AudioSubsectionReader()
{
	if (deleteSourceWhenDeleted)
		delete source;
}

bool AudioSubsectionReader::readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
										 int64 startSampleInFile, int numSamples)
{
	if (startSampleInFile + numSamples > length)
	{
		for (int i = numDestChannels; --i >= 0;)
			if (destSamples[i] != 0)
				zeromem (destSamples[i], sizeof (int) * numSamples);

		numSamples = jmin (numSamples, (int) (length - startSampleInFile));

		if (numSamples <= 0)
			return true;
	}

	return source->readSamples (destSamples, numDestChannels, startOffsetInDestBuffer,
								startSampleInFile + startSample, numSamples);
}

void AudioSubsectionReader::readMaxLevels (int64 startSampleInFile,
										   int64 numSamples,
										   float& lowestLeft,
										   float& highestLeft,
										   float& lowestRight,
										   float& highestRight)
{
	startSampleInFile = jmax ((int64) 0, startSampleInFile);
	numSamples = jmax ((int64) 0, jmin (numSamples, length - startSampleInFile));

	source->readMaxLevels (startSampleInFile + startSample,
						   numSamples,
						   lowestLeft,
						   highestLeft,
						   lowestRight,
						   highestRight);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioSubsectionReader.cpp ***/


/*** Start of inlined file: juce_AudioThumbnail.cpp ***/
BEGIN_JUCE_NAMESPACE

const int timeBeforeDeletingReader = 2000;

struct AudioThumbnailDataFormat
{
	char thumbnailMagic[4];
	int samplesPerThumbSample;
	int64 totalSamples;	 // source samples
	int64 numFinishedSamples;   // source samples
	int numThumbnailSamples;
	int numChannels;
	int sampleRate;
	char future[16];
	char data[1];

	void swapEndiannessIfNeeded() throw()
	{
	  #if JUCE_BIG_ENDIAN
		flip (samplesPerThumbSample);
		flip (totalSamples);
		flip (numFinishedSamples);
		flip (numThumbnailSamples);
		flip (numChannels);
		flip (sampleRate);
	  #endif
	}

private:
  #if JUCE_BIG_ENDIAN
	static void flip (int& n)   { n = (int) ByteOrder::swap ((uint32) n); }
	static void flip (int64& n) { n = (int64) ByteOrder::swap ((uint64) n); }
  #endif
};

AudioThumbnail::AudioThumbnail (const int orginalSamplesPerThumbnailSample_,
								AudioFormatManager& formatManagerToUse_,
								AudioThumbnailCache& cacheToUse)
	: formatManagerToUse (formatManagerToUse_),
	  cache (cacheToUse),
	  orginalSamplesPerThumbnailSample (orginalSamplesPerThumbnailSample_)
{
	clear();
}

AudioThumbnail::~AudioThumbnail()
{
	cache.removeThumbnail (this);

	const ScopedLock sl (readerLock);
	reader = 0;
}

void AudioThumbnail::setSource (InputSource* const newSource)
{
	cache.removeThumbnail (this);
	timerCallback(); // stops the timer and deletes the reader

	source = newSource;
	clear();

	if (newSource != 0
		  && ! (cache.loadThumb (*this, newSource->hashCode())
				 && isFullyLoaded()))
	{
		{
			const ScopedLock sl (readerLock);
			reader = createReader();
		}

		if (reader != 0)
		{
			initialiseFromAudioFile (*reader);
			cache.addThumbnail (this);
		}
	}

	sendChangeMessage (this);
}

bool AudioThumbnail::useTimeSlice()
{
	const ScopedLock sl (readerLock);

	if (isFullyLoaded())
	{
		if (reader != 0)
			startTimer (timeBeforeDeletingReader);

		cache.removeThumbnail (this);
		return false;
	}

	if (reader == 0)
		reader = createReader();

	if (reader != 0)
	{
		readNextBlockFromAudioFile (*reader);
		stopTimer();

		sendChangeMessage (this);

		const bool justFinished = isFullyLoaded();

		if (justFinished)
			cache.storeThumb (*this, source->hashCode());

		return ! justFinished;
	}

	return false;
}

AudioFormatReader* AudioThumbnail::createReader() const
{
	if (source != 0)
	{
		InputStream* const audioFileStream = source->createInputStream();

		if (audioFileStream != 0)
			return formatManagerToUse.createReaderFor (audioFileStream);
	}

	return 0;
}

void AudioThumbnail::timerCallback()
{
	stopTimer();

	const ScopedLock sl (readerLock);
	reader = 0;
}

void AudioThumbnail::clear()
{
	data.setSize (sizeof (AudioThumbnailDataFormat) + 3);

	AudioThumbnailDataFormat* const d = (AudioThumbnailDataFormat*) data.getData();

	d->thumbnailMagic[0] = 'j';
	d->thumbnailMagic[1] = 'a';
	d->thumbnailMagic[2] = 't';
	d->thumbnailMagic[3] = 'm';

	d->samplesPerThumbSample = orginalSamplesPerThumbnailSample;
	d->totalSamples = 0;
	d->numFinishedSamples = 0;
	d->numThumbnailSamples = 0;
	d->numChannels = 0;
	d->sampleRate = 0;

	numSamplesCached = 0;
	cacheNeedsRefilling = true;
}

void AudioThumbnail::loadFrom (InputStream& input)
{
	const ScopedLock sl (readerLock);

	data.setSize (0);
	input.readIntoMemoryBlock (data);

	AudioThumbnailDataFormat* const d = (AudioThumbnailDataFormat*) data.getData();
	d->swapEndiannessIfNeeded();

	if (! (d->thumbnailMagic[0] == 'j'
			 && d->thumbnailMagic[1] == 'a'
			 && d->thumbnailMagic[2] == 't'
			 && d->thumbnailMagic[3] == 'm'))
	{
		clear();
	}

	numSamplesCached = 0;
	cacheNeedsRefilling = true;
}

void AudioThumbnail::saveTo (OutputStream& output) const
{
	AudioThumbnailDataFormat* const d = (AudioThumbnailDataFormat*) data.getData();
	d->swapEndiannessIfNeeded();
	output.write (data.getData(), (int) data.getSize());
	d->swapEndiannessIfNeeded();
}

bool AudioThumbnail::initialiseFromAudioFile (AudioFormatReader& fileReader)
{
	AudioThumbnailDataFormat* d = (AudioThumbnailDataFormat*) data.getData();

	d->totalSamples = fileReader.lengthInSamples;
	d->numChannels = jmin ((uint32) 2, fileReader.numChannels);
	d->numFinishedSamples = 0;
	d->sampleRate = roundToInt (fileReader.sampleRate);
	d->numThumbnailSamples = (int) (d->totalSamples / d->samplesPerThumbSample) + 1;

	data.setSize (sizeof (AudioThumbnailDataFormat) + 3 + d->numThumbnailSamples * d->numChannels * 2);

	d = (AudioThumbnailDataFormat*) data.getData();
	zeromem (&(d->data[0]), d->numThumbnailSamples * d->numChannels * 2);

	return d->totalSamples > 0;
}

bool AudioThumbnail::readNextBlockFromAudioFile (AudioFormatReader& fileReader)
{
	AudioThumbnailDataFormat* const d = (AudioThumbnailDataFormat*) data.getData();

	if (d->numFinishedSamples < d->totalSamples)
	{
		const int numToDo = (int) jmin ((int64) 65536, d->totalSamples - d->numFinishedSamples);

		generateSection (fileReader,
						 d->numFinishedSamples,
						 numToDo);

		d->numFinishedSamples += numToDo;
	}

	cacheNeedsRefilling = true;
	return (d->numFinishedSamples < d->totalSamples);
}

int AudioThumbnail::getNumChannels() const throw()
{
	const AudioThumbnailDataFormat* const d = (const AudioThumbnailDataFormat*) data.getData();
	jassert (d != 0);

	return d->numChannels;
}

double AudioThumbnail::getTotalLength() const throw()
{
	const AudioThumbnailDataFormat* const d = (const AudioThumbnailDataFormat*) data.getData();
	jassert (d != 0);

	if (d->sampleRate > 0)
		return d->totalSamples / (double)d->sampleRate;
	else
		return 0.0;
}

void AudioThumbnail::generateSection (AudioFormatReader& fileReader,
									  int64 startSample,
									  int numSamples)
{
	AudioThumbnailDataFormat* const d = (AudioThumbnailDataFormat*) data.getData();
	jassert (d != 0);

	int firstDataPos = (int) (startSample / d->samplesPerThumbSample);
	int lastDataPos = (int) ((startSample + numSamples) / d->samplesPerThumbSample);

	char* l = getChannelData (0);
	char* r = getChannelData (1);

	for (int i = firstDataPos; i < lastDataPos; ++i)
	{
		const int sourceStart = i * d->samplesPerThumbSample;
		const int sourceEnd = sourceStart + d->samplesPerThumbSample;

		float lowestLeft, highestLeft, lowestRight, highestRight;

		fileReader.readMaxLevels (sourceStart,
								  sourceEnd - sourceStart,
								  lowestLeft,
								  highestLeft,
								  lowestRight,
								  highestRight);

		int n = i * 2;

		if (r != 0)
		{
			l [n]   = (char) jlimit (-128.0f, 127.0f, lowestLeft * 127.0f);
			r [n++] = (char) jlimit (-128.0f, 127.0f, lowestRight * 127.0f);
			l [n]   = (char) jlimit (-128.0f, 127.0f, highestLeft * 127.0f);
			r [n++] = (char) jlimit (-128.0f, 127.0f, highestRight * 127.0f);
		}
		else
		{
			l [n++] = (char) jlimit (-128.0f, 127.0f, lowestLeft * 127.0f);
			l [n++] = (char) jlimit (-128.0f, 127.0f, highestLeft * 127.0f);
		}
	}
}

char* AudioThumbnail::getChannelData (int channel) const
{
	AudioThumbnailDataFormat* const d = (AudioThumbnailDataFormat*) data.getData();
	jassert (d != 0);

	if (channel >= 0 && channel < d->numChannels)
		return d->data + (channel * 2 * d->numThumbnailSamples);

	return 0;
}

bool AudioThumbnail::isFullyLoaded() const throw()
{
	const AudioThumbnailDataFormat* const d = (const AudioThumbnailDataFormat*) data.getData();
	jassert (d != 0);

	return d->numFinishedSamples >= d->totalSamples;
}

void AudioThumbnail::refillCache (const int numSamples,
								  double startTime,
								  const double timePerPixel)
{
	const AudioThumbnailDataFormat* const d = (const AudioThumbnailDataFormat*) data.getData();
	jassert (d != 0);

	if (numSamples <= 0
		 || timePerPixel <= 0.0
		 || d->sampleRate <= 0)
	{
		numSamplesCached = 0;
		cacheNeedsRefilling = true;
		return;
	}

	if (numSamples == numSamplesCached
		&& numChannelsCached == d->numChannels
		&& startTime == cachedStart
		&& timePerPixel == cachedTimePerPixel
		&& ! cacheNeedsRefilling)
	{
		return;
	}

	numSamplesCached = numSamples;
	numChannelsCached = d->numChannels;
	cachedStart = startTime;
	cachedTimePerPixel = timePerPixel;

	cachedLevels.ensureSize (2 * numChannelsCached * numSamples);

	const bool needExtraDetail = (timePerPixel * d->sampleRate <= d->samplesPerThumbSample);

	const ScopedLock sl (readerLock);

	cacheNeedsRefilling = false;

	if (needExtraDetail && reader == 0)
		reader = createReader();

	if (reader != 0 && timePerPixel * d->sampleRate <= d->samplesPerThumbSample)
	{
		startTimer (timeBeforeDeletingReader);

		char* cacheData = static_cast <char*> (cachedLevels.getData());
		int sample = roundToInt (startTime * d->sampleRate);

		for (int i = numSamples; --i >= 0;)
		{
			const int nextSample = roundToInt ((startTime + timePerPixel) * d->sampleRate);

			if (sample >= 0)
			{
				if (sample >= reader->lengthInSamples)
					break;

				float lmin, lmax, rmin, rmax;

				reader->readMaxLevels (sample,
									   jmax (1, nextSample - sample),
									   lmin, lmax, rmin, rmax);

				cacheData[0] = (char) jlimit (-128, 127, roundFloatToInt (lmin * 127.0f));
				cacheData[1] = (char) jlimit (-128, 127, roundFloatToInt (lmax * 127.0f));

				if (numChannelsCached > 1)
				{
					cacheData[2] = (char) jlimit (-128, 127, roundFloatToInt (rmin * 127.0f));
					cacheData[3] = (char) jlimit (-128, 127, roundFloatToInt (rmax * 127.0f));
				}

				cacheData += 2 * numChannelsCached;
			}

			startTime += timePerPixel;
			sample = nextSample;
		}
	}
	else
	{
		for (int channelNum = 0; channelNum < numChannelsCached; ++channelNum)
		{
			char* const channelData = getChannelData (channelNum);
			char* cacheData = static_cast <char*> (cachedLevels.getData()) + channelNum * 2;

			const double timeToThumbSampleFactor = d->sampleRate / (double) d->samplesPerThumbSample;

			startTime = cachedStart;
			int sample = roundToInt (startTime * timeToThumbSampleFactor);
			const int numFinished = (int) (d->numFinishedSamples / d->samplesPerThumbSample);

			for (int i = numSamples; --i >= 0;)
			{
				const int nextSample = roundToInt ((startTime + timePerPixel) * timeToThumbSampleFactor);

				if (sample >= 0 && channelData != 0)
				{
					char mx = -128;
					char mn = 127;

					while (sample <= nextSample)
					{
						if (sample >= numFinished)
							break;

						const int n = sample << 1;
						const char sampMin = channelData [n];
						const char sampMax = channelData [n + 1];

						if (sampMin < mn)
							mn = sampMin;

						if (sampMax > mx)
							mx = sampMax;

						++sample;
					}

					if (mn <= mx)
					{
						cacheData[0] = mn;
						cacheData[1] = mx;
					}
					else
					{
						cacheData[0] = 1;
						cacheData[1] = 0;
					}
				}
				else
				{
					cacheData[0] = 1;
					cacheData[1] = 0;
				}

				cacheData += numChannelsCached * 2;
				startTime += timePerPixel;
				sample = nextSample;
			}
		}
	}
}

void AudioThumbnail::drawChannel (Graphics& g,
								  int x, int y, int w, int h,
								  double startTime,
								  double endTime,
								  int channelNum,
								  const float verticalZoomFactor)
{
	refillCache (w, startTime, (endTime - startTime) / w);

	if (numSamplesCached >= w
		&& channelNum >= 0
		&& channelNum < numChannelsCached)
	{
		const float topY = (float) y;
		const float bottomY = topY + h;
		const float midY = topY + h * 0.5f;
		const float vscale = verticalZoomFactor * h / 256.0f;

		const Rectangle<int> clip (g.getClipBounds());
		const int skipLeft = jlimit (0, w, clip.getX() - x);
		w -= skipLeft;
		x += skipLeft;

		const char* cacheData = static_cast <const char*> (cachedLevels.getData())
								   + (channelNum << 1)
								   + skipLeft * (numChannelsCached << 1);

		while (--w >= 0)
		{
			const char mn = cacheData[0];
			const char mx = cacheData[1];
			cacheData += numChannelsCached << 1;

			if (mn <= mx) // if the wrong way round, signifies that the sample's not yet known
				g.drawLine ((float) x, jmax (midY - mx * vscale - 0.3f, topY),
							(float) x, jmin (midY - mn * vscale + 0.3f, bottomY));

			++x;

			if (x >= clip.getRight())
				break;
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioThumbnail.cpp ***/


/*** Start of inlined file: juce_AudioThumbnailCache.cpp ***/
BEGIN_JUCE_NAMESPACE

struct ThumbnailCacheEntry
{
	int64 hash;
	uint32 lastUsed;
	MemoryBlock data;

	juce_UseDebuggingNewOperator
};

AudioThumbnailCache::AudioThumbnailCache (const int maxNumThumbsToStore_)
	: TimeSliceThread ("thumb cache"),
	  maxNumThumbsToStore (maxNumThumbsToStore_)
{
	startThread (2);
}

AudioThumbnailCache::~AudioThumbnailCache()
{
}

bool AudioThumbnailCache::loadThumb (AudioThumbnail& thumb, const int64 hashCode)
{
	for (int i = thumbs.size(); --i >= 0;)
	{
		if (thumbs[i]->hash == hashCode)
		{
			MemoryInputStream in (thumbs[i]->data, false);
			thumb.loadFrom (in);

			thumbs[i]->lastUsed = Time::getMillisecondCounter();
			return true;
		}
	}

	return false;
}

void AudioThumbnailCache::storeThumb (const AudioThumbnail& thumb,
									  const int64 hashCode)
{
	MemoryOutputStream out;
	thumb.saveTo (out);

	ThumbnailCacheEntry* te = 0;

	for (int i = thumbs.size(); --i >= 0;)
	{
		if (thumbs[i]->hash == hashCode)
		{
			te = thumbs[i];
			break;
		}
	}

	if (te == 0)
	{
		te = new ThumbnailCacheEntry();
		te->hash = hashCode;

		if (thumbs.size() < maxNumThumbsToStore)
		{
			thumbs.add (te);
		}
		else
		{
			int oldest = 0;
			unsigned int oldestTime = Time::getMillisecondCounter() + 1;

			int i;
			for (i = thumbs.size(); --i >= 0;)
				if (thumbs[i]->lastUsed < oldestTime)
					oldest = i;

			thumbs.set (i, te);
		}
	}

	te->lastUsed = Time::getMillisecondCounter();
	te->data.setSize (0);
	te->data.append (out.getData(), out.getDataSize());
}

void AudioThumbnailCache::clear()
{
	thumbs.clear();
}

void AudioThumbnailCache::addThumbnail (AudioThumbnail* const thumb)
{
	addTimeSliceClient (thumb);
}

void AudioThumbnailCache::removeThumbnail (AudioThumbnail* const thumb)
{
	removeTimeSliceClient (thumb);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioThumbnailCache.cpp ***/


/*** Start of inlined file: juce_QuickTimeAudioFormat.cpp ***/
#if JUCE_QUICKTIME && ! (JUCE_64BIT || JUCE_IPHONE)

#if ! JUCE_WINDOWS
 #include <QuickTime/Movies.h>
 #include <QuickTime/QTML.h>
 #include <QuickTime/QuickTimeComponents.h>
 #include <QuickTime/MediaHandlers.h>
 #include <QuickTime/ImageCodec.h>
#else
 #if JUCE_MSVC
  #pragma warning (push)
  #pragma warning (disable : 4100)
 #endif

 #include <Movies.h>
 #include <QTML.h>
 #include <QuickTimeComponents.h>
 #include <MediaHandlers.h>
 #include <ImageCodec.h>

 #if JUCE_MSVC
   #pragma warning (pop)
 #endif
#endif

BEGIN_JUCE_NAMESPACE

bool juce_OpenQuickTimeMovieFromStream (InputStream* input, Movie& movie, Handle& dataHandle);

static const char* const quickTimeFormatName = "QuickTime file";
static const juce_wchar* const quickTimeExtensions[] =    { T(".mov"), T(".mp3"), T(".mp4"), 0 };

class QTAudioReader	 : public AudioFormatReader
{
public:
	QTAudioReader (InputStream* const input_, const int trackNum_)
		: AudioFormatReader (input_, TRANS (quickTimeFormatName)),
		  ok (false),
		  movie (0),
		  trackNum (trackNum_),
		  lastSampleRead (0),
		  lastThreadId (0),
		  extractor (0),
		  dataHandle (0)
	{
		bufferList.calloc (256, 1);

#ifdef WIN32
		if (InitializeQTML (0) != noErr)
			return;
#endif
		if (EnterMovies() != noErr)
			return;

		bool opened = juce_OpenQuickTimeMovieFromStream (input_, movie, dataHandle);

		if (! opened)
			return;

		{
			const int numTracks = GetMovieTrackCount (movie);
			int trackCount = 0;

			for (int i = 1; i <= numTracks; ++i)
			{
				track = GetMovieIndTrack (movie, i);
				media = GetTrackMedia (track);

				OSType mediaType;
				GetMediaHandlerDescription (media, &mediaType, 0, 0);

				if (mediaType == SoundMediaType
					 && trackCount++ == trackNum_)
				{
					ok = true;
					break;
				}
			}
		}

		if (! ok)
			return;

		ok = false;

		lengthInSamples = GetMediaDecodeDuration (media);
		usesFloatingPointData = false;

		samplesPerFrame = (int) (GetMediaDecodeDuration (media) / GetMediaSampleCount (media));

		trackUnitsPerFrame = GetMovieTimeScale (movie) * samplesPerFrame
								/ GetMediaTimeScale (media);

		OSStatus err = MovieAudioExtractionBegin (movie, 0, &extractor);

		unsigned long output_layout_size;
		err = MovieAudioExtractionGetPropertyInfo (extractor,
												   kQTPropertyClass_MovieAudioExtraction_Audio,
												   kQTMovieAudioExtractionAudioPropertyID_AudioChannelLayout,
												   0, &output_layout_size, 0);
		if (err != noErr)
			return;

		HeapBlock <AudioChannelLayout> qt_audio_channel_layout;
		qt_audio_channel_layout.calloc (output_layout_size, 1);

		err = MovieAudioExtractionGetProperty (extractor,
											   kQTPropertyClass_MovieAudioExtraction_Audio,
											   kQTMovieAudioExtractionAudioPropertyID_AudioChannelLayout,
											   output_layout_size, qt_audio_channel_layout, 0);

		qt_audio_channel_layout[0].mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;

		err = MovieAudioExtractionSetProperty (extractor,
											   kQTPropertyClass_MovieAudioExtraction_Audio,
											   kQTMovieAudioExtractionAudioPropertyID_AudioChannelLayout,
											   output_layout_size,
											   qt_audio_channel_layout);

		err = MovieAudioExtractionGetProperty (extractor,
											   kQTPropertyClass_MovieAudioExtraction_Audio,
											   kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
											   sizeof (inputStreamDesc),
											   &inputStreamDesc, 0);
		if (err != noErr)
			return;

		inputStreamDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger
										| kAudioFormatFlagIsPacked
										| kAudioFormatFlagsNativeEndian;
		inputStreamDesc.mBitsPerChannel = sizeof (SInt16) * 8;
		inputStreamDesc.mChannelsPerFrame = jmin ((UInt32) 2, inputStreamDesc.mChannelsPerFrame);
		inputStreamDesc.mBytesPerFrame = sizeof (SInt16) * inputStreamDesc.mChannelsPerFrame;
		inputStreamDesc.mBytesPerPacket = inputStreamDesc.mBytesPerFrame;

		err = MovieAudioExtractionSetProperty (extractor,
											   kQTPropertyClass_MovieAudioExtraction_Audio,
											   kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
											   sizeof (inputStreamDesc),
											   &inputStreamDesc);
		if (err != noErr)
			return;

		Boolean allChannelsDiscrete = false;
		err = MovieAudioExtractionSetProperty (extractor,
											   kQTPropertyClass_MovieAudioExtraction_Movie,
											   kQTMovieAudioExtractionMoviePropertyID_AllChannelsDiscrete,
											   sizeof (allChannelsDiscrete),
											   &allChannelsDiscrete);

		if (err != noErr)
			return;

		bufferList->mNumberBuffers = 1;
		bufferList->mBuffers[0].mNumberChannels = inputStreamDesc.mChannelsPerFrame;
		bufferList->mBuffers[0].mDataByteSize = (UInt32) (samplesPerFrame * inputStreamDesc.mBytesPerFrame) + 16;
		bufferList->mBuffers[0].mData = malloc (bufferList->mBuffers[0].mDataByteSize);

		sampleRate = inputStreamDesc.mSampleRate;
		bitsPerSample = 16;
		numChannels = inputStreamDesc.mChannelsPerFrame;

		detachThread();
		ok = true;
	}

	~QTAudioReader()
	{
		if (dataHandle != 0)
			DisposeHandle (dataHandle);

		if (extractor != 0)
		{
			MovieAudioExtractionEnd (extractor);
			extractor = 0;
		}

		checkThreadIsAttached();
		DisposeMovie (movie);

		juce_free (bufferList->mBuffers[0].mData);

#if JUCE_MAC
		ExitMoviesOnThread ();
#endif
	}

	bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
					  int64 startSampleInFile, int numSamples)
	{
		checkThreadIsAttached();

		while (numSamples > 0)
		{
			if (! loadFrame ((int) startSampleInFile))
				return false;

			const int numToDo = jmin (numSamples, samplesPerFrame);

			for (int j = numDestChannels; --j >= 0;)
			{
				if (destSamples[j] != 0)
				{
					const short* const src = ((const short*) bufferList->mBuffers[0].mData) + j;

					for (int i = 0; i < numToDo; ++i)
						destSamples[j][startOffsetInDestBuffer + i] = src [i << 1] << 16;
				}
			}

			startOffsetInDestBuffer += numToDo;
			startSampleInFile += numToDo;
			numSamples -= numToDo;
		}

		detachThread();
		return true;
	}

	bool loadFrame (const int sampleNum)
	{
		if (lastSampleRead != sampleNum)
		{
			TimeRecord time;
			time.scale = (TimeScale) inputStreamDesc.mSampleRate;
			time.base = 0;
			time.value.hi = 0;
			time.value.lo = (UInt32) sampleNum;

			OSStatus err = MovieAudioExtractionSetProperty (extractor,
															kQTPropertyClass_MovieAudioExtraction_Movie,
															kQTMovieAudioExtractionMoviePropertyID_CurrentTime,
															sizeof (time), &time);

			if (err != noErr)
				return false;
		}

		bufferList->mBuffers[0].mDataByteSize = inputStreamDesc.mBytesPerFrame * samplesPerFrame;

		UInt32 outFlags = 0;
		UInt32 actualNumSamples = samplesPerFrame;
		OSStatus err = MovieAudioExtractionFillBuffer (extractor, &actualNumSamples,
													   bufferList, &outFlags);

		lastSampleRead = sampleNum + samplesPerFrame;

		return err == noErr;
	}

	juce_UseDebuggingNewOperator

	bool ok;

private:
	Movie movie;
	Media media;
	Track track;
	const int trackNum;
	double trackUnitsPerFrame;
	int samplesPerFrame;
	int lastSampleRead;
	Thread::ThreadID lastThreadId;
	MovieAudioExtractionRef extractor;
	AudioStreamBasicDescription inputStreamDesc;
	HeapBlock <AudioBufferList> bufferList;
	Handle dataHandle;

	void checkThreadIsAttached()
	{
#if JUCE_MAC
		if (Thread::getCurrentThreadId() != lastThreadId)
			EnterMoviesOnThread (0);
		AttachMovieToCurrentThread (movie);
#endif
	}

	void detachThread()
	{
#if JUCE_MAC
		DetachMovieFromCurrentThread (movie);
#endif
	}

	QTAudioReader (const QTAudioReader&);
	QTAudioReader& operator= (const QTAudioReader&);
};

QuickTimeAudioFormat::QuickTimeAudioFormat()
	: AudioFormat (TRANS (quickTimeFormatName), (const juce_wchar**) quickTimeExtensions)
{
}

QuickTimeAudioFormat::~QuickTimeAudioFormat()
{
}

const Array <int> QuickTimeAudioFormat::getPossibleSampleRates()
{
	return Array<int>();
}

const Array <int> QuickTimeAudioFormat::getPossibleBitDepths()
{
	return Array<int>();
}

bool QuickTimeAudioFormat::canDoStereo()
{
	return true;
}

bool QuickTimeAudioFormat::canDoMono()
{
	return true;
}

AudioFormatReader* QuickTimeAudioFormat::createReaderFor (InputStream* sourceStream,
														  const bool deleteStreamIfOpeningFails)
{
	ScopedPointer <QTAudioReader> r (new QTAudioReader (sourceStream, 0));

	if (r->ok)
		return r.release();

	if (! deleteStreamIfOpeningFails)
		r->input = 0;

	return 0;
}

AudioFormatWriter* QuickTimeAudioFormat::createWriterFor (OutputStream* /*streamToWriteTo*/,
														  double /*sampleRateToUse*/,
														  unsigned int /*numberOfChannels*/,
														  int /*bitsPerSample*/,
														  const StringPairArray& /*metadataValues*/,
														  int /*qualityOptionIndex*/)
{
	jassertfalse // not yet implemented!
	return 0;
}

END_JUCE_NAMESPACE

#endif
/*** End of inlined file: juce_QuickTimeAudioFormat.cpp ***/


/*** Start of inlined file: juce_WavAudioFormat.cpp ***/
BEGIN_JUCE_NAMESPACE

static const char* const wavFormatName = "WAV file";
static const juce_wchar* const wavExtensions[] =    { T(".wav"), T(".bwf"), 0 };

const char* const WavAudioFormat::bwavDescription      = "bwav description";
const char* const WavAudioFormat::bwavOriginator       = "bwav originator";
const char* const WavAudioFormat::bwavOriginatorRef    = "bwav originator ref";
const char* const WavAudioFormat::bwavOriginationDate  = "bwav origination date";
const char* const WavAudioFormat::bwavOriginationTime  = "bwav origination time";
const char* const WavAudioFormat::bwavTimeReference    = "bwav time reference";
const char* const WavAudioFormat::bwavCodingHistory    = "bwav coding history";

const StringPairArray WavAudioFormat::createBWAVMetadata (const String& description,
														  const String& originator,
														  const String& originatorRef,
														  const Time& date,
														  const int64 timeReferenceSamples,
														  const String& codingHistory)
{
	StringPairArray m;

	m.set (bwavDescription, description);
	m.set (bwavOriginator, originator);
	m.set (bwavOriginatorRef, originatorRef);
	m.set (bwavOriginationDate, date.formatted (T("%Y-%m-%d")));
	m.set (bwavOriginationTime, date.formatted (T("%H:%M:%S")));
	m.set (bwavTimeReference, String (timeReferenceSamples));
	m.set (bwavCodingHistory, codingHistory);

	return m;
}

#if JUCE_MSVC
  #pragma pack (push, 1)
  #define PACKED
#elif JUCE_GCC
  #define PACKED __attribute__((packed))
#else
  #define PACKED
#endif

struct BWAVChunk
{
	char description [256];
	char originator [32];
	char originatorRef [32];
	char originationDate [10];
	char originationTime [8];
	uint32 timeRefLow;
	uint32 timeRefHigh;
	uint16 version;
	uint8 umid[64];
	uint8 reserved[190];
	char codingHistory[1];

	void copyTo (StringPairArray& values) const
	{
		values.set (WavAudioFormat::bwavDescription, String::fromUTF8 (description, 256));
		values.set (WavAudioFormat::bwavOriginator, String::fromUTF8 (originator, 32));
		values.set (WavAudioFormat::bwavOriginatorRef, String::fromUTF8 (originatorRef, 32));
		values.set (WavAudioFormat::bwavOriginationDate, String::fromUTF8 (originationDate, 10));
		values.set (WavAudioFormat::bwavOriginationTime, String::fromUTF8 (originationTime, 8));

		const uint32 timeLow = ByteOrder::swapIfBigEndian (timeRefLow);
		const uint32 timeHigh = ByteOrder::swapIfBigEndian (timeRefHigh);
		const int64 time = (((int64)timeHigh) << 32) + timeLow;

		values.set (WavAudioFormat::bwavTimeReference, String (time));
		values.set (WavAudioFormat::bwavCodingHistory, String::fromUTF8 (codingHistory));
	}

	static MemoryBlock createFrom (const StringPairArray& values)
	{
		const size_t sizeNeeded = sizeof (BWAVChunk) + values [WavAudioFormat::bwavCodingHistory].getNumBytesAsUTF8();
		MemoryBlock data ((sizeNeeded + 3) & ~3);
		data.fillWith (0);

		BWAVChunk* b = (BWAVChunk*) data.getData();

		// Allow these calls to overwrite an extra byte at the end, which is fine as long
		// as they get called in the right order..
		values [WavAudioFormat::bwavDescription].copyToUTF8 (b->description, 257);
		values [WavAudioFormat::bwavOriginator].copyToUTF8 (b->originator, 33);
		values [WavAudioFormat::bwavOriginatorRef].copyToUTF8 (b->originatorRef, 33);
		values [WavAudioFormat::bwavOriginationDate].copyToUTF8 (b->originationDate, 11);
		values [WavAudioFormat::bwavOriginationTime].copyToUTF8 (b->originationTime, 9);

		const int64 time = values [WavAudioFormat::bwavTimeReference].getLargeIntValue();
		b->timeRefLow = ByteOrder::swapIfBigEndian ((uint32) (time & 0xffffffff));
		b->timeRefHigh = ByteOrder::swapIfBigEndian ((uint32) (time >> 32));

		values [WavAudioFormat::bwavCodingHistory].copyToUTF8 (b->codingHistory, 0x7fffffff);

		if (b->description[0] != 0
			|| b->originator[0] != 0
			|| b->originationDate[0] != 0
			|| b->originationTime[0] != 0
			|| b->codingHistory[0] != 0
			|| time != 0)
		{
			return data;
		}

		return MemoryBlock();
	}

} PACKED;

struct SMPLChunk
{
	struct SampleLoop
	{
		uint32 identifier;
		uint32 type;
		uint32 start;
		uint32 end;
		uint32 fraction;
		uint32 playCount;
	} PACKED;

	uint32 manufacturer;
	uint32 product;
	uint32 samplePeriod;
	uint32 midiUnityNote;
	uint32 midiPitchFraction;
	uint32 smpteFormat;
	uint32 smpteOffset;
	uint32 numSampleLoops;
	uint32 samplerData;
	SampleLoop loops[1];

	void copyTo (StringPairArray& values, const int totalSize) const
	{
		values.set ("Manufacturer",      String (ByteOrder::swapIfBigEndian (manufacturer)));
		values.set ("Product",           String (ByteOrder::swapIfBigEndian (product)));
		values.set ("SamplePeriod",      String (ByteOrder::swapIfBigEndian (samplePeriod)));
		values.set ("MidiUnityNote",     String (ByteOrder::swapIfBigEndian (midiUnityNote)));
		values.set ("MidiPitchFraction", String (ByteOrder::swapIfBigEndian (midiPitchFraction)));
		values.set ("SmpteFormat",       String (ByteOrder::swapIfBigEndian (smpteFormat)));
		values.set ("SmpteOffset",       String (ByteOrder::swapIfBigEndian (smpteOffset)));
		values.set ("NumSampleLoops",    String (ByteOrder::swapIfBigEndian (numSampleLoops)));
		values.set ("SamplerData",       String (ByteOrder::swapIfBigEndian (samplerData)));

		for (uint32 i = 0; i < numSampleLoops; ++i)
		{
			if ((uint8*) (loops + (i + 1)) > ((uint8*) this) + totalSize)
				break;

			const String prefix ("Loop" + String(i));
			values.set (prefix + "Identifier", String (ByteOrder::swapIfBigEndian (loops[i].identifier)));
			values.set (prefix + "Type",       String (ByteOrder::swapIfBigEndian (loops[i].type)));
			values.set (prefix + "Start",      String (ByteOrder::swapIfBigEndian (loops[i].start)));
			values.set (prefix + "End",        String (ByteOrder::swapIfBigEndian (loops[i].end)));
			values.set (prefix + "Fraction",   String (ByteOrder::swapIfBigEndian (loops[i].fraction)));
			values.set (prefix + "PlayCount",  String (ByteOrder::swapIfBigEndian (loops[i].playCount)));
		}
	}

	static MemoryBlock createFrom (const StringPairArray& values)
	{
		const int numLoops = jmin (64, values.getValue ("NumSampleLoops", "0").getIntValue());

		if (numLoops <= 0)
			return MemoryBlock();

		const size_t sizeNeeded = sizeof (SMPLChunk) + (numLoops - 1) * sizeof (SampleLoop);
		MemoryBlock data ((sizeNeeded + 3) & ~3);
		data.fillWith (0);

		SMPLChunk* s = (SMPLChunk*) data.getData();

		// Allow these calls to overwrite an extra byte at the end, which is fine as long
		// as they get called in the right order..
		s->manufacturer      = ByteOrder::swapIfBigEndian ((uint32) values.getValue ("Manufacturer", "0").getIntValue());
		s->product           = ByteOrder::swapIfBigEndian ((uint32) values.getValue ("Product", "0").getIntValue());
		s->samplePeriod      = ByteOrder::swapIfBigEndian ((uint32) values.getValue ("SamplePeriod", "0").getIntValue());
		s->midiUnityNote     = ByteOrder::swapIfBigEndian ((uint32) values.getValue ("MidiUnityNote", "60").getIntValue());
		s->midiPitchFraction = ByteOrder::swapIfBigEndian ((uint32) values.getValue ("MidiPitchFraction", "0").getIntValue());
		s->smpteFormat       = ByteOrder::swapIfBigEndian ((uint32) values.getValue ("SmpteFormat", "0").getIntValue());
		s->smpteOffset       = ByteOrder::swapIfBigEndian ((uint32) values.getValue ("SmpteOffset", "0").getIntValue());
		s->numSampleLoops	= ByteOrder::swapIfBigEndian ((uint32) numLoops);
		s->samplerData       = ByteOrder::swapIfBigEndian ((uint32) values.getValue ("SamplerData", "0").getIntValue());

		for (int i = 0; i < numLoops; ++i)
		{
			const String prefix ("Loop" + String(i));
			s->loops[i].identifier = ByteOrder::swapIfBigEndian ((uint32) values.getValue (prefix + "Identifier", "0").getIntValue());
			s->loops[i].type       = ByteOrder::swapIfBigEndian ((uint32) values.getValue (prefix + "Type", "0").getIntValue());
			s->loops[i].start      = ByteOrder::swapIfBigEndian ((uint32) values.getValue (prefix + "Start", "0").getIntValue());
			s->loops[i].end        = ByteOrder::swapIfBigEndian ((uint32) values.getValue (prefix + "End", "0").getIntValue());
			s->loops[i].fraction   = ByteOrder::swapIfBigEndian ((uint32) values.getValue (prefix + "Fraction", "0").getIntValue());
			s->loops[i].playCount  = ByteOrder::swapIfBigEndian ((uint32) values.getValue (prefix + "PlayCount", "0").getIntValue());
		}

		return data;
	}
} PACKED;

struct ExtensibleWavSubFormat
{
	uint32 data1;
	uint16 data2;
	uint16 data3;
	uint8  data4[8];
} PACKED;

#if JUCE_MSVC
  #pragma pack (pop)
#endif

#undef PACKED

class WavAudioFormatReader  : public AudioFormatReader
{
	int bytesPerFrame;
	int64 dataChunkStart, dataLength;

	static inline int chunkName (const char* const name)   { return (int) ByteOrder::littleEndianInt (name); }

	WavAudioFormatReader (const WavAudioFormatReader&);
	WavAudioFormatReader& operator= (const WavAudioFormatReader&);

public:
	int64 bwavChunkStart, bwavSize;

	WavAudioFormatReader (InputStream* const in)
		: AudioFormatReader (in, TRANS (wavFormatName)),
		  dataLength (0),
		  bwavChunkStart (0),
		  bwavSize (0)
	{
		if (input->readInt() == chunkName ("RIFF"))
		{
			const uint32 len = (uint32) input->readInt();
			const int64 end = input->getPosition() + len;
			bool hasGotType = false;
			bool hasGotData = false;

			if (input->readInt() == chunkName ("WAVE"))
			{
				while (input->getPosition() < end
						&& ! input->isExhausted())
				{
					const int chunkType = input->readInt();
					uint32 length = (uint32) input->readInt();
					const int64 chunkEnd = input->getPosition() + length + (length & 1);

					if (chunkType == chunkName ("fmt "))
					{
						// read the format chunk
						const unsigned short format = input->readShort();
						const short numChans = input->readShort();
						sampleRate = input->readInt();
						const int bytesPerSec = input->readInt();

						numChannels = numChans;
						bytesPerFrame = bytesPerSec / (int)sampleRate;
						bitsPerSample = 8 * bytesPerFrame / numChans;

						if (format == 3)
						{
							usesFloatingPointData = true;
						}
						else if (format == 0xfffe /*WAVE_FORMAT_EXTENSIBLE*/)
						{
							if (length < 40) // too short
							{
								bytesPerFrame = 0;
							}
							else
							{
								input->skipNextBytes (12); // skip over blockAlign, bitsPerSample and speakerPosition mask
								ExtensibleWavSubFormat subFormat;
								subFormat.data1 = input->readInt();
								subFormat.data2 = input->readShort();
								subFormat.data3 = input->readShort();
								input->read (subFormat.data4, sizeof (subFormat.data4));

								const ExtensibleWavSubFormat pcmFormat
									= { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };

								if (memcmp (&subFormat, &pcmFormat, sizeof (subFormat)) != 0)
								{
									const ExtensibleWavSubFormat ambisonicFormat
										= { 0x00000001, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 } };

									if (memcmp (&subFormat, &ambisonicFormat, sizeof (subFormat)) != 0)
										bytesPerFrame = 0;
								}
							}
						}
						else if (format != 1)
						{
							bytesPerFrame = 0;
						}

						hasGotType = true;
					}
					else if (chunkType == chunkName ("data"))
					{
						// get the data chunk's position
						dataLength = length;
						dataChunkStart = input->getPosition();
						lengthInSamples = (bytesPerFrame > 0) ? (dataLength / bytesPerFrame) : 0;

						hasGotData = true;
					}
					else if (chunkType == chunkName ("bext"))
					{
						bwavChunkStart = input->getPosition();
						bwavSize = length;

						// Broadcast-wav extension chunk..
						HeapBlock <BWAVChunk> bwav;
						bwav.calloc (jmax ((size_t) length + 1, sizeof (BWAVChunk)), 1);
						input->read (bwav, length);
						bwav->copyTo (metadataValues);
					}
					else if (chunkType == chunkName ("smpl"))
					{
						HeapBlock <SMPLChunk> smpl;
						smpl.calloc (jmax ((size_t) length + 1, sizeof (SMPLChunk)), 1);
						input->read (smpl, length);
						smpl->copyTo (metadataValues, length);
					}
					else if (chunkEnd <= input->getPosition())
					{
						break;
					}

					input->setPosition (chunkEnd);
				}
			}
		}
	}

	~WavAudioFormatReader()
	{
	}

	bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
					  int64 startSampleInFile, int numSamples)
	{
		const int64 samplesAvailable = lengthInSamples - startSampleInFile;

		if (samplesAvailable < numSamples)
		{
			for (int i = numDestChannels; --i >= 0;)
				if (destSamples[i] != 0)
					zeromem (destSamples[i] + startOffsetInDestBuffer, sizeof (int) * numSamples);

			numSamples = (int) samplesAvailable;
		}

		if (numSamples <= 0)
			return true;

		input->setPosition (dataChunkStart + startSampleInFile * bytesPerFrame);

		const int tempBufSize = 480 * 3 * 4; // (keep this a multiple of 3)
		char tempBuffer [tempBufSize];

		while (numSamples > 0)
		{
			int* left = destSamples[0];
			if (left != 0)
				left += startOffsetInDestBuffer;

			int* right = numDestChannels > 1 ? destSamples[1] : 0;
			if (right != 0)
				right += startOffsetInDestBuffer;

			const int numThisTime = jmin (tempBufSize / bytesPerFrame, numSamples);
			const int bytesRead = input->read (tempBuffer, numThisTime * bytesPerFrame);

			if (bytesRead < numThisTime * bytesPerFrame)
				zeromem (tempBuffer + bytesRead, numThisTime * bytesPerFrame - bytesRead);

			if (bitsPerSample == 16)
			{
				const short* src = reinterpret_cast <const short*> (tempBuffer);

				if (numChannels > 1)
				{
					if (left == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							++src;
							*right++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
						}
					}
					else if (right == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
							++src;
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
							*right++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
						}
					}
				}
				else
				{
					for (int i = numThisTime; --i >= 0;)
					{
						*left++ = (int) ByteOrder::swapIfBigEndian ((unsigned short) *src++) << 16;
					}
				}
			}
			else if (bitsPerSample == 24)
			{
				const char* src = tempBuffer;

				if (numChannels > 1)
				{
					if (left == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							src += 3;
							*right++ = ByteOrder::littleEndian24Bit (src) << 8;
							src += 3;
						}
					}
					else if (right == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = ByteOrder::littleEndian24Bit (src) << 8;
							src += 6;
						}
					}
					else
					{
						for (int i = 0; i < numThisTime; ++i)
						{
							*left++ = ByteOrder::littleEndian24Bit (src) << 8;
							src += 3;
							*right++ = ByteOrder::littleEndian24Bit (src) << 8;
							src += 3;
						}
					}
				}
				else
				{
					for (int i = 0; i < numThisTime; ++i)
					{
						*left++ = ByteOrder::littleEndian24Bit (src) << 8;
						src += 3;
					}
				}
			}
			else if (bitsPerSample == 32)
			{
				const unsigned int* src = (const unsigned int*) tempBuffer;
				unsigned int* l = (unsigned int*) left;
				unsigned int* r = (unsigned int*) right;

				if (numChannels > 1)
				{
					if (l == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							++src;
							*r++ = ByteOrder::swapIfBigEndian (*src++);
						}
					}
					else if (r == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*l++ = ByteOrder::swapIfBigEndian (*src++);
							++src;
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*l++ = ByteOrder::swapIfBigEndian (*src++);
							*r++ = ByteOrder::swapIfBigEndian (*src++);
						}
					}
				}
				else
				{
					for (int i = numThisTime; --i >= 0;)
					{
						*l++ = ByteOrder::swapIfBigEndian (*src++);
					}
				}

				left = (int*)l;
				right = (int*)r;
			}
			else if (bitsPerSample == 8)
			{
				const unsigned char* src = (const unsigned char*) tempBuffer;

				if (numChannels > 1)
				{
					if (left == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							++src;
							*right++ = ((int) *src++ - 128) << 24;
						}
					}
					else if (right == 0)
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = ((int) *src++ - 128) << 24;
							++src;
						}
					}
					else
					{
						for (int i = numThisTime; --i >= 0;)
						{
							*left++ = ((int) *src++ - 128) << 24;
							*right++ = ((int) *src++ - 128) << 24;
						}
					}
				}
				else
				{
					for (int i = numThisTime; --i >= 0;)
					{
						*left++ = ((int)*src++ - 128) << 24;
					}
				}
			}

			startOffsetInDestBuffer += numThisTime;
			numSamples -= numThisTime;
		}

		if (numSamples > 0)
		{
			for (int i = numDestChannels; --i >= 0;)
				if (destSamples[i] != 0)
					zeromem (destSamples[i] + startOffsetInDestBuffer,
							 sizeof (int) * numSamples);
		}

		return true;
	}

	juce_UseDebuggingNewOperator
};

class WavAudioFormatWriter  : public AudioFormatWriter
{
	MemoryBlock tempBlock, bwavChunk, smplChunk;
	uint32 lengthInSamples, bytesWritten;
	int64 headerPosition;
	bool writeFailed;

	static inline int chunkName (const char* const name)   { return (int) ByteOrder::littleEndianInt (name); }

	WavAudioFormatWriter (const WavAudioFormatWriter&);
	WavAudioFormatWriter& operator= (const WavAudioFormatWriter&);

	void writeHeader()
	{
		const bool seekedOk = output->setPosition (headerPosition);
		(void) seekedOk;

		// if this fails, you've given it an output stream that can't seek! It needs
		// to be able to seek back to write the header
		jassert (seekedOk);

		const int bytesPerFrame = numChannels * bitsPerSample / 8;
		output->writeInt (chunkName ("RIFF"));
		output->writeInt ((int) (lengthInSamples * bytesPerFrame
								   + ((bwavChunk.getSize() > 0) ? (44 + bwavChunk.getSize()) : 36)));

		output->writeInt (chunkName ("WAVE"));
		output->writeInt (chunkName ("fmt "));
		output->writeInt (16);
		output->writeShort ((bitsPerSample < 32) ? (short) 1 /*WAVE_FORMAT_PCM*/
												 : (short) 3 /*WAVE_FORMAT_IEEE_FLOAT*/);
		output->writeShort ((short) numChannels);
		output->writeInt ((int) sampleRate);
		output->writeInt (bytesPerFrame * (int) sampleRate);
		output->writeShort ((short) bytesPerFrame);
		output->writeShort ((short) bitsPerSample);

		if (bwavChunk.getSize() > 0)
		{
			output->writeInt (chunkName ("bext"));
			output->writeInt ((int) bwavChunk.getSize());
			output->write (bwavChunk.getData(), (int) bwavChunk.getSize());
		}

		if (smplChunk.getSize() > 0)
		{
			output->writeInt (chunkName ("smpl"));
			output->writeInt ((int) smplChunk.getSize());
			output->write (smplChunk.getData(), (int) smplChunk.getSize());
		}

		output->writeInt (chunkName ("data"));
		output->writeInt (lengthInSamples * bytesPerFrame);

		usesFloatingPointData = (bitsPerSample == 32);
	}

public:

	WavAudioFormatWriter (OutputStream* const out,
						  const double sampleRate_,
						  const unsigned int numChannels_,
						  const int bits,
						  const StringPairArray& metadataValues)
		: AudioFormatWriter (out,
							 TRANS (wavFormatName),
							 sampleRate_,
							 numChannels_,
							 bits),
		  lengthInSamples (0),
		  bytesWritten (0),
		  writeFailed (false)
	{
		if (metadataValues.size() > 0)
		{
			bwavChunk = BWAVChunk::createFrom (metadataValues);
			smplChunk = SMPLChunk::createFrom (metadataValues);
		}

		headerPosition = out->getPosition();
		writeHeader();
	}

	~WavAudioFormatWriter()
	{
		writeHeader();
	}

	bool write (const int** data, int numSamples)
	{
		if (writeFailed)
			return false;

		const int bytes = numChannels * numSamples * bitsPerSample / 8;
		tempBlock.ensureSize (bytes, false);
		char* buffer = static_cast <char*> (tempBlock.getData());

		const int* left = data[0];
		const int* right = data[1];
		if (right == 0)
			right = left;

		if (bitsPerSample == 16)
		{
			short* b = (short*) buffer;

			if (numChannels > 1)
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = (short) ByteOrder::swapIfBigEndian ((unsigned short) (*left++ >> 16));
					*b++ = (short) ByteOrder::swapIfBigEndian ((unsigned short) (*right++ >> 16));
				}
			}
			else
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = (short) ByteOrder::swapIfBigEndian ((unsigned short) (*left++ >> 16));
				}
			}
		}
		else if (bitsPerSample == 24)
		{
			char* b = buffer;

			if (numChannels > 1)
			{
				for (int i = numSamples; --i >= 0;)
				{
					ByteOrder::littleEndian24BitToChars ((*left++) >> 8, b);
					b += 3;
					ByteOrder::littleEndian24BitToChars ((*right++) >> 8, b);
					b += 3;
				}
			}
			else
			{
				for (int i = numSamples; --i >= 0;)
				{
					ByteOrder::littleEndian24BitToChars ((*left++) >> 8, b);
					b += 3;
				}
			}
		}
		else if (bitsPerSample == 32)
		{
			unsigned int* b = (unsigned int*) buffer;

			if (numChannels > 1)
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = ByteOrder::swapIfBigEndian ((unsigned int) *left++);
					*b++ = ByteOrder::swapIfBigEndian ((unsigned int) *right++);
				}
			}
			else
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = ByteOrder::swapIfBigEndian ((unsigned int) *left++);
				}
			}
		}
		else if (bitsPerSample == 8)
		{
			unsigned char* b = (unsigned char*) buffer;

			if (numChannels > 1)
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = (unsigned char) (128 + (*left++ >> 24));
					*b++ = (unsigned char) (128 + (*right++ >> 24));
				}
			}
			else
			{
				for (int i = numSamples; --i >= 0;)
				{
					*b++ = (unsigned char) (128 + (*left++ >> 24));
				}
			}
		}

		if (bytesWritten + bytes >= (uint32) 0xfff00000
			 || ! output->write (buffer, bytes))
		{
			// failed to write to disk, so let's try writing the header.
			// If it's just run out of disk space, then if it does manage
			// to write the header, we'll still have a useable file..
			writeHeader();
			writeFailed = true;
			return false;
		}
		else
		{
			bytesWritten += bytes;
			lengthInSamples += numSamples;

			return true;
		}
	}

	juce_UseDebuggingNewOperator
};

WavAudioFormat::WavAudioFormat()
	: AudioFormat (TRANS (wavFormatName), (const juce_wchar**) wavExtensions)
{
}

WavAudioFormat::~WavAudioFormat()
{
}

const Array <int> WavAudioFormat::getPossibleSampleRates()
{
	const int rates[] = { 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 0 };
	return Array <int> (rates);
}

const Array <int> WavAudioFormat::getPossibleBitDepths()
{
	const int depths[] = { 8, 16, 24, 32, 0 };
	return Array <int> (depths);
}

bool WavAudioFormat::canDoStereo()
{
	return true;
}

bool WavAudioFormat::canDoMono()
{
	return true;
}

AudioFormatReader* WavAudioFormat::createReaderFor (InputStream* sourceStream,
													const bool deleteStreamIfOpeningFails)
{
	ScopedPointer <WavAudioFormatReader> r (new WavAudioFormatReader (sourceStream));

	if (r->sampleRate != 0)
		return r.release();

	if (! deleteStreamIfOpeningFails)
		r->input = 0;

	return 0;
}

AudioFormatWriter* WavAudioFormat::createWriterFor (OutputStream* out,
													double sampleRate,
													unsigned int numChannels,
													int bitsPerSample,
													const StringPairArray& metadataValues,
													int /*qualityOptionIndex*/)
{
	if (getPossibleBitDepths().contains (bitsPerSample))
	{
		return new WavAudioFormatWriter (out,
										 sampleRate,
										 numChannels,
										 bitsPerSample,
										 metadataValues);
	}

	return 0;
}

static bool juce_slowCopyOfWavFileWithNewMetadata (const File& file, const StringPairArray& metadata)
{
	TemporaryFile tempFile (file);

	WavAudioFormat wav;
	ScopedPointer <AudioFormatReader> reader (wav.createReaderFor (file.createInputStream(), true));

	if (reader != 0)
	{
		ScopedPointer <OutputStream> outStream (tempFile.getFile().createOutputStream());

		if (outStream != 0)
		{
			ScopedPointer <AudioFormatWriter> writer (wav.createWriterFor (outStream, reader->sampleRate,
																		   reader->numChannels, reader->bitsPerSample,
																		   metadata, 0));

			if (writer != 0)
			{
				outStream.release();

				bool ok = writer->writeFromAudioReader (*reader, 0, -1);
				writer = 0;
				reader = 0;

				return ok && tempFile.overwriteTargetFileWithTemporary();
			}
		}
	}

	return false;
}

bool WavAudioFormat::replaceMetadataInFile (const File& wavFile, const StringPairArray& newMetadata)
{
	ScopedPointer <WavAudioFormatReader> reader ((WavAudioFormatReader*) createReaderFor (wavFile.createInputStream(), true));

	if (reader != 0)
	{
		const int64 bwavPos = reader->bwavChunkStart;
		const int64 bwavSize = reader->bwavSize;
		reader = 0;

		if (bwavSize > 0)
		{
			MemoryBlock chunk = BWAVChunk::createFrom (newMetadata);

			if (chunk.getSize() <= (size_t) bwavSize)
			{
				// the new one will fit in the space available, so write it directly..
				const int64 oldSize = wavFile.getSize();

				{
					ScopedPointer <FileOutputStream> out (wavFile.createOutputStream());
					out->setPosition (bwavPos);
					out->write (chunk.getData(), (int) chunk.getSize());
					out->setPosition (oldSize);
				}

				jassert (wavFile.getSize() == oldSize);

				return true;
			}
		}
	}

	return juce_slowCopyOfWavFileWithNewMetadata (wavFile, newMetadata);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_WavAudioFormat.cpp ***/


/*** Start of inlined file: juce_AudioFormatReaderSource.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioFormatReaderSource::AudioFormatReaderSource (AudioFormatReader* const reader_,
												  const bool deleteReaderWhenThisIsDeleted)
	: reader (reader_),
	  deleteReader (deleteReaderWhenThisIsDeleted),
	  nextPlayPos (0),
	  looping (false)
{
	jassert (reader != 0);
}

AudioFormatReaderSource::~AudioFormatReaderSource()
{
	releaseResources();

	if (deleteReader)
		delete reader;
}

void AudioFormatReaderSource::setNextReadPosition (int newPosition)
{
	nextPlayPos = newPosition;
}

void AudioFormatReaderSource::setLooping (const bool shouldLoop) throw()
{
	looping = shouldLoop;
}

int AudioFormatReaderSource::getNextReadPosition() const
{
	return (looping) ? (nextPlayPos % (int) reader->lengthInSamples)
					 : nextPlayPos;
}

int AudioFormatReaderSource::getTotalLength() const
{
	return (int) reader->lengthInSamples;
}

void AudioFormatReaderSource::prepareToPlay (int /*samplesPerBlockExpected*/,
											 double /*sampleRate*/)
{
}

void AudioFormatReaderSource::releaseResources()
{
}

void AudioFormatReaderSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
	if (info.numSamples > 0)
	{
		const int start = nextPlayPos;

		if (looping)
		{
			const int newStart = start % (int) reader->lengthInSamples;
			const int newEnd = (start + info.numSamples) % (int) reader->lengthInSamples;

			if (newEnd > newStart)
			{
				info.buffer->readFromAudioReader (reader,
												  info.startSample,
												  newEnd - newStart,
												  newStart,
												  true, true);
			}
			else
			{
				const int endSamps = (int) reader->lengthInSamples - newStart;

				info.buffer->readFromAudioReader (reader,
												  info.startSample,
												  endSamps,
												  newStart,
												  true, true);

				info.buffer->readFromAudioReader (reader,
												  info.startSample + endSamps,
												  newEnd,
												  0,
												  true, true);
			}

			nextPlayPos = newEnd;
		}
		else
		{
			info.buffer->readFromAudioReader (reader,
											  info.startSample,
											  info.numSamples,
											  start,
											  true, true);

			nextPlayPos += info.numSamples;
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioFormatReaderSource.cpp ***/


/*** Start of inlined file: juce_AudioSourcePlayer.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioSourcePlayer::AudioSourcePlayer()
	: source (0),
	  sampleRate (0),
	  bufferSize (0),
	  tempBuffer (2, 8),
	  lastGain (1.0f),
	  gain (1.0f)
{
}

AudioSourcePlayer::~AudioSourcePlayer()
{
	setSource (0);
}

void AudioSourcePlayer::setSource (AudioSource* newSource)
{
	if (source != newSource)
	{
		AudioSource* const oldSource = source;

		if (newSource != 0 && bufferSize > 0 && sampleRate > 0)
			newSource->prepareToPlay (bufferSize, sampleRate);

		{
			const ScopedLock sl (readLock);
			source = newSource;
		}

		if (oldSource != 0)
			oldSource->releaseResources();
	}
}

void AudioSourcePlayer::setGain (const float newGain) throw()
{
	gain = newGain;
}

void AudioSourcePlayer::audioDeviceIOCallback (const float** inputChannelData,
											   int totalNumInputChannels,
											   float** outputChannelData,
											   int totalNumOutputChannels,
											   int numSamples)
{
	// these should have been prepared by audioDeviceAboutToStart()...
	jassert (sampleRate > 0 && bufferSize > 0);

	const ScopedLock sl (readLock);

	if (source != 0)
	{
		AudioSourceChannelInfo info;
		int i, numActiveChans = 0, numInputs = 0, numOutputs = 0;

		// messy stuff needed to compact the channels down into an array
		// of non-zero pointers..
		for (i = 0; i < totalNumInputChannels; ++i)
		{
			if (inputChannelData[i] != 0)
			{
				inputChans [numInputs++] = inputChannelData[i];
				if (numInputs >= numElementsInArray (inputChans))
					break;
			}
		}

		for (i = 0; i < totalNumOutputChannels; ++i)
		{
			if (outputChannelData[i] != 0)
			{
				outputChans [numOutputs++] = outputChannelData[i];
				if (numOutputs >= numElementsInArray (outputChans))
					break;
			}
		}

		if (numInputs > numOutputs)
		{
			// if there aren't enough output channels for the number of
			// inputs, we need to create some temporary extra ones (can't
			// use the input data in case it gets written to)
			tempBuffer.setSize (numInputs - numOutputs, numSamples,
								false, false, true);

			for (i = 0; i < numOutputs; ++i)
			{
				channels[numActiveChans] = outputChans[i];
				memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * numSamples);
				++numActiveChans;
			}

			for (i = numOutputs; i < numInputs; ++i)
			{
				channels[numActiveChans] = tempBuffer.getSampleData (i - numOutputs, 0);
				memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * numSamples);
				++numActiveChans;
			}
		}
		else
		{
			for (i = 0; i < numInputs; ++i)
			{
				channels[numActiveChans] = outputChans[i];
				memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * numSamples);
				++numActiveChans;
			}

			for (i = numInputs; i < numOutputs; ++i)
			{
				channels[numActiveChans] = outputChans[i];
				zeromem (channels[numActiveChans], sizeof (float) * numSamples);
				++numActiveChans;
			}
		}

		AudioSampleBuffer buffer (channels, numActiveChans, numSamples);

		info.buffer = &buffer;
		info.startSample = 0;
		info.numSamples = numSamples;

		source->getNextAudioBlock (info);

		for (i = info.buffer->getNumChannels(); --i >= 0;)
			info.buffer->applyGainRamp (i, info.startSample, info.numSamples, lastGain, gain);

		lastGain = gain;
	}
	else
	{
		for (int i = 0; i < totalNumOutputChannels; ++i)
			if (outputChannelData[i] != 0)
				zeromem (outputChannelData[i], sizeof (float) * numSamples);
	}
}

void AudioSourcePlayer::audioDeviceAboutToStart (AudioIODevice* device)
{
	sampleRate = device->getCurrentSampleRate();
	bufferSize = device->getCurrentBufferSizeSamples();
	zeromem (channels, sizeof (channels));

	if (source != 0)
		source->prepareToPlay (bufferSize, sampleRate);
}

void AudioSourcePlayer::audioDeviceStopped()
{
	if (source != 0)
		source->releaseResources();

	sampleRate = 0.0;
	bufferSize = 0;

	tempBuffer.setSize (2, 8);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioSourcePlayer.cpp ***/


/*** Start of inlined file: juce_AudioTransportSource.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioTransportSource::AudioTransportSource()
	: source (0),
	  resamplerSource (0),
	  bufferingSource (0),
	  positionableSource (0),
	  masterSource (0),
	  gain (1.0f),
	  lastGain (1.0f),
	  playing (false),
	  stopped (true),
	  sampleRate (44100.0),
	  sourceSampleRate (0.0),
	  blockSize (128),
	  readAheadBufferSize (0),
	  isPrepared (false),
	  inputStreamEOF (false)
{
}

AudioTransportSource::~AudioTransportSource()
{
	setSource (0);

	releaseResources();
}

void AudioTransportSource::setSource (PositionableAudioSource* const newSource,
									  int readAheadBufferSize_,
									  double sourceSampleRateToCorrectFor)
{
	if (source == newSource)
	{
		if (source == 0)
			return;

		setSource (0, 0, 0); // deselect and reselect to avoid releasing resources wrongly
	}

	readAheadBufferSize = readAheadBufferSize_;
	sourceSampleRate = sourceSampleRateToCorrectFor;

	ResamplingAudioSource* newResamplerSource = 0;
	BufferingAudioSource* newBufferingSource = 0;
	PositionableAudioSource* newPositionableSource = 0;
	AudioSource* newMasterSource = 0;

	ScopedPointer <ResamplingAudioSource> oldResamplerSource (resamplerSource);
	ScopedPointer <BufferingAudioSource> oldBufferingSource (bufferingSource);
	AudioSource* oldMasterSource = masterSource;

	if (newSource != 0)
	{
		newPositionableSource = newSource;

		if (readAheadBufferSize_ > 0)
			newPositionableSource = newBufferingSource
				= new BufferingAudioSource (newPositionableSource, false, readAheadBufferSize_);

		newPositionableSource->setNextReadPosition (0);

		if (sourceSampleRateToCorrectFor != 0)
			newMasterSource = newResamplerSource
				= new ResamplingAudioSource (newPositionableSource, false);
		else
			newMasterSource = newPositionableSource;

		if (isPrepared)
		{
			if (newResamplerSource != 0 && sourceSampleRate > 0 && sampleRate > 0)
				newResamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);

			newMasterSource->prepareToPlay (blockSize, sampleRate);
		}
	}

	{
		const ScopedLock sl (callbackLock);

		source = newSource;
		resamplerSource = newResamplerSource;
		bufferingSource = newBufferingSource;
		masterSource = newMasterSource;
		positionableSource = newPositionableSource;

		playing = false;
	}

	if (oldMasterSource != 0)
		oldMasterSource->releaseResources();
}

void AudioTransportSource::start()
{
	if ((! playing) && masterSource != 0)
	{
		{
			const ScopedLock sl (callbackLock);
			playing = true;
			stopped = false;
			inputStreamEOF = false;
		}

		sendChangeMessage (this);
	}
}

void AudioTransportSource::stop()
{
	if (playing)
	{
		{
			const ScopedLock sl (callbackLock);
			playing = false;
		}

		int n = 500;
		while (--n >= 0 && ! stopped)
			Thread::sleep (2);

		sendChangeMessage (this);
	}
}

void AudioTransportSource::setPosition (double newPosition)
{
	if (sampleRate > 0.0)
		setNextReadPosition (roundToInt (newPosition * sampleRate));
}

double AudioTransportSource::getCurrentPosition() const
{
	if (sampleRate > 0.0)
		return getNextReadPosition() / sampleRate;
	else
		return 0.0;
}

void AudioTransportSource::setNextReadPosition (int newPosition)
{
	if (positionableSource != 0)
	{
		if (sampleRate > 0 && sourceSampleRate > 0)
			newPosition = roundToInt (newPosition * sourceSampleRate / sampleRate);

		positionableSource->setNextReadPosition (newPosition);
	}
}

int AudioTransportSource::getNextReadPosition() const
{
	if (positionableSource != 0)
	{
		const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;

		return roundToInt (positionableSource->getNextReadPosition() * ratio);
	}

	return 0;
}

int AudioTransportSource::getTotalLength() const
{
	const ScopedLock sl (callbackLock);

	if (positionableSource != 0)
	{
		const double ratio = (sampleRate > 0 && sourceSampleRate > 0) ? sampleRate / sourceSampleRate : 1.0;

		return roundToInt (positionableSource->getTotalLength() * ratio);
	}

	return 0;
}

bool AudioTransportSource::isLooping() const
{
	const ScopedLock sl (callbackLock);

	return positionableSource != 0
			&& positionableSource->isLooping();
}

void AudioTransportSource::setGain (const float newGain) throw()
{
	gain = newGain;
}

void AudioTransportSource::prepareToPlay (int samplesPerBlockExpected,
										  double sampleRate_)
{
	const ScopedLock sl (callbackLock);

	sampleRate = sampleRate_;
	blockSize = samplesPerBlockExpected;

	if (masterSource != 0)
		masterSource->prepareToPlay (samplesPerBlockExpected, sampleRate);

	if (resamplerSource != 0 && sourceSampleRate != 0)
		resamplerSource->setResamplingRatio (sourceSampleRate / sampleRate);

	isPrepared = true;
}

void AudioTransportSource::releaseResources()
{
	const ScopedLock sl (callbackLock);

	if (masterSource != 0)
		masterSource->releaseResources();

	isPrepared = false;
}

void AudioTransportSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
	const ScopedLock sl (callbackLock);

	inputStreamEOF = false;

	if (masterSource != 0 && ! stopped)
	{
		masterSource->getNextAudioBlock (info);

		if (! playing)
		{
			// just stopped playing, so fade out the last block..
			for (int i = info.buffer->getNumChannels(); --i >= 0;)
				info.buffer->applyGainRamp (i, info.startSample, jmin (256, info.numSamples), 1.0f, 0.0f);

			if (info.numSamples > 256)
				info.buffer->clear (info.startSample + 256, info.numSamples - 256);
		}

		if (positionableSource->getNextReadPosition() > positionableSource->getTotalLength() + 1
			 && ! positionableSource->isLooping())
		{
			playing = false;
			inputStreamEOF = true;
			sendChangeMessage (this);
		}

		stopped = ! playing;

		for (int i = info.buffer->getNumChannels(); --i >= 0;)
		{
			info.buffer->applyGainRamp (i, info.startSample, info.numSamples,
										lastGain, gain);
		}
	}
	else
	{
		info.clearActiveBufferRegion();
		stopped = true;
	}

	lastGain = gain;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioTransportSource.cpp ***/


/*** Start of inlined file: juce_BufferingAudioSource.cpp ***/
BEGIN_JUCE_NAMESPACE

class SharedBufferingAudioSourceThread  : public DeletedAtShutdown,
										  public Thread,
										  private Timer
{
public:
	SharedBufferingAudioSourceThread()
		: Thread ("Audio Buffer")
	{
	}

	~SharedBufferingAudioSourceThread()
	{
		stopThread (10000);
		clearSingletonInstance();
	}

	juce_DeclareSingleton (SharedBufferingAudioSourceThread, false)

	void addSource (BufferingAudioSource* source)
	{
		const ScopedLock sl (lock);

		if (! sources.contains (source))
		{
			sources.add (source);
			startThread();

			stopTimer();
		}

		notify();
	}

	void removeSource (BufferingAudioSource* source)
	{
		const ScopedLock sl (lock);
		sources.removeValue (source);

		if (sources.size() == 0)
			startTimer (5000);
	}

private:
	Array <BufferingAudioSource*> sources;
	CriticalSection lock;

	void run()
	{
		while (! threadShouldExit())
		{
			bool busy = false;

			for (int i = sources.size(); --i >= 0;)
			{
				if (threadShouldExit())
					return;

				const ScopedLock sl (lock);

				BufferingAudioSource* const b = sources[i];

				if (b != 0 && b->readNextBufferChunk())
					busy = true;
			}

			if (! busy)
				wait (500);
		}
	}

	void timerCallback()
	{
		stopTimer();

		if (sources.size() == 0)
			deleteInstance();
	}

	SharedBufferingAudioSourceThread (const SharedBufferingAudioSourceThread&);
	SharedBufferingAudioSourceThread& operator= (const SharedBufferingAudioSourceThread&);
};

juce_ImplementSingleton (SharedBufferingAudioSourceThread)

BufferingAudioSource::BufferingAudioSource (PositionableAudioSource* source_,
											const bool deleteSourceWhenDeleted_,
											int numberOfSamplesToBuffer_)
	: source (source_),
	  deleteSourceWhenDeleted (deleteSourceWhenDeleted_),
	  numberOfSamplesToBuffer (jmax (1024, numberOfSamplesToBuffer_)),
	  buffer (2, 0),
	  bufferValidStart (0),
	  bufferValidEnd (0),
	  nextPlayPos (0),
	  wasSourceLooping (false)
{
	jassert (source_ != 0);

	jassert (numberOfSamplesToBuffer_ > 1024); // not much point using this class if you're
											   //  not using a larger buffer..
}

BufferingAudioSource::~BufferingAudioSource()
{
	SharedBufferingAudioSourceThread* const thread = SharedBufferingAudioSourceThread::getInstanceWithoutCreating();

	if (thread != 0)
		thread->removeSource (this);

	if (deleteSourceWhenDeleted)
		delete source;
}

void BufferingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate_)
{
	source->prepareToPlay (samplesPerBlockExpected, sampleRate_);

	sampleRate = sampleRate_;

	buffer.setSize (2, jmax (samplesPerBlockExpected * 2, numberOfSamplesToBuffer));
	buffer.clear();

	bufferValidStart = 0;
	bufferValidEnd = 0;

	SharedBufferingAudioSourceThread::getInstance()->addSource (this);

	while (bufferValidEnd - bufferValidStart < jmin (((int) sampleRate_) / 4,
													 buffer.getNumSamples() / 2))
	{
		SharedBufferingAudioSourceThread::getInstance()->notify();
		Thread::sleep (5);
	}
}

void BufferingAudioSource::releaseResources()
{
	SharedBufferingAudioSourceThread* const thread = SharedBufferingAudioSourceThread::getInstanceWithoutCreating();

	if (thread != 0)
		thread->removeSource (this);

	buffer.setSize (2, 0);
	source->releaseResources();
}

void BufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
	const ScopedLock sl (bufferStartPosLock);

	const int validStart = jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos;
	const int validEnd   = jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos;

	if (validStart == validEnd)
	{
		// total cache miss
		info.clearActiveBufferRegion();
	}
	else
	{
		if (validStart > 0)
			info.buffer->clear (info.startSample, validStart);  // partial cache miss at start

		if (validEnd < info.numSamples)
			info.buffer->clear (info.startSample + validEnd,
								info.numSamples - validEnd);	// partial cache miss at end

		if (validStart < validEnd)
		{
			for (int chan = jmin (2, info.buffer->getNumChannels()); --chan >= 0;)
			{
				const int startBufferIndex = (validStart + nextPlayPos) % buffer.getNumSamples();
				const int endBufferIndex = (validEnd + nextPlayPos) % buffer.getNumSamples();

				if (startBufferIndex < endBufferIndex)
				{
					info.buffer->copyFrom (chan, info.startSample + validStart,
										   buffer,
										   chan, startBufferIndex,
										   validEnd - validStart);
				}
				else
				{
					const int initialSize = buffer.getNumSamples() - startBufferIndex;

					info.buffer->copyFrom (chan, info.startSample + validStart,
										   buffer,
										   chan, startBufferIndex,
										   initialSize);

					info.buffer->copyFrom (chan, info.startSample + validStart + initialSize,
										   buffer,
										   chan, 0,
										   (validEnd - validStart) - initialSize);
				}
			}
		}

		nextPlayPos += info.numSamples;

		if (source->isLooping() && nextPlayPos > 0)
			nextPlayPos %= source->getTotalLength();
	}

	SharedBufferingAudioSourceThread* const thread = SharedBufferingAudioSourceThread::getInstanceWithoutCreating();

	if (thread != 0)
		thread->notify();
}

int BufferingAudioSource::getNextReadPosition() const
{
	return (source->isLooping() && nextPlayPos > 0)
					? nextPlayPos % source->getTotalLength()
					: nextPlayPos;
}

void BufferingAudioSource::setNextReadPosition (int newPosition)
{
	const ScopedLock sl (bufferStartPosLock);

	nextPlayPos = newPosition;

	SharedBufferingAudioSourceThread* const thread = SharedBufferingAudioSourceThread::getInstanceWithoutCreating();

	if (thread != 0)
		thread->notify();
}

bool BufferingAudioSource::readNextBufferChunk()
{
	int newBVS, newBVE, sectionToReadStart, sectionToReadEnd;

	{
		const ScopedLock sl (bufferStartPosLock);

		if (wasSourceLooping != isLooping())
		{
			wasSourceLooping = isLooping();
			bufferValidStart = 0;
			bufferValidEnd = 0;
		}

		newBVS = jmax (0, nextPlayPos);
		newBVE = newBVS + buffer.getNumSamples() - 4;
		sectionToReadStart = 0;
		sectionToReadEnd = 0;

		const int maxChunkSize = 2048;

		if (newBVS < bufferValidStart || newBVS >= bufferValidEnd)
		{
			newBVE = jmin (newBVE, newBVS + maxChunkSize);

			sectionToReadStart = newBVS;
			sectionToReadEnd = newBVE;

			bufferValidStart = 0;
			bufferValidEnd = 0;
		}
		else if (abs (newBVS - bufferValidStart) > 512
					|| abs (newBVE - bufferValidEnd) > 512)
		{
			newBVE = jmin (newBVE, bufferValidEnd + maxChunkSize);

			sectionToReadStart = bufferValidEnd;
			sectionToReadEnd = newBVE;

			bufferValidStart = newBVS;
			bufferValidEnd = jmin (bufferValidEnd, newBVE);
		}
	}

	if (sectionToReadStart != sectionToReadEnd)
	{
		const int bufferIndexStart = sectionToReadStart % buffer.getNumSamples();
		const int bufferIndexEnd = sectionToReadEnd % buffer.getNumSamples();

		if (bufferIndexStart < bufferIndexEnd)
		{
			readBufferSection (sectionToReadStart,
							   sectionToReadEnd - sectionToReadStart,
							   bufferIndexStart);
		}
		else
		{
			const int initialSize = buffer.getNumSamples() - bufferIndexStart;

			readBufferSection (sectionToReadStart,
							   initialSize,
							   bufferIndexStart);

			readBufferSection (sectionToReadStart + initialSize,
							   (sectionToReadEnd - sectionToReadStart) - initialSize,
							   0);
		}

		const ScopedLock sl2 (bufferStartPosLock);

		bufferValidStart = newBVS;
		bufferValidEnd = newBVE;

		return true;
	}
	else
	{
		return false;
	}
}

void BufferingAudioSource::readBufferSection (int start, int length, int bufferOffset)
{
	if (source->getNextReadPosition() != start)
		source->setNextReadPosition (start);

	AudioSourceChannelInfo info;
	info.buffer = &buffer;
	info.startSample = bufferOffset;
	info.numSamples = length;

	source->getNextAudioBlock (info);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_BufferingAudioSource.cpp ***/


/*** Start of inlined file: juce_ChannelRemappingAudioSource.cpp ***/
BEGIN_JUCE_NAMESPACE

ChannelRemappingAudioSource::ChannelRemappingAudioSource (AudioSource* const source_,
														  const bool deleteSourceWhenDeleted_)
   : requiredNumberOfChannels (2),
	 source (source_),
	 deleteSourceWhenDeleted (deleteSourceWhenDeleted_),
	 buffer (2, 16)
{
	remappedInfo.buffer = &buffer;
	remappedInfo.startSample = 0;
}

ChannelRemappingAudioSource::~ChannelRemappingAudioSource()
{
	if (deleteSourceWhenDeleted)
		delete source;
}

void ChannelRemappingAudioSource::setNumberOfChannelsToProduce (const int requiredNumberOfChannels_) throw()
{
	const ScopedLock sl (lock);
	requiredNumberOfChannels = requiredNumberOfChannels_;
}

void ChannelRemappingAudioSource::clearAllMappings() throw()
{
	const ScopedLock sl (lock);

	remappedInputs.clear();
	remappedOutputs.clear();
}

void ChannelRemappingAudioSource::setInputChannelMapping (const int destIndex, const int sourceIndex) throw()
{
	const ScopedLock sl (lock);

	while (remappedInputs.size() < destIndex)
		remappedInputs.add (-1);

	remappedInputs.set (destIndex, sourceIndex);
}

void ChannelRemappingAudioSource::setOutputChannelMapping (const int sourceIndex, const int destIndex) throw()
{
	const ScopedLock sl (lock);

	while (remappedOutputs.size() < sourceIndex)
		remappedOutputs.add (-1);

	remappedOutputs.set (sourceIndex, destIndex);
}

int ChannelRemappingAudioSource::getRemappedInputChannel (const int inputChannelIndex) const throw()
{
	const ScopedLock sl (lock);

	if (inputChannelIndex >= 0 && inputChannelIndex < remappedInputs.size())
		return remappedInputs.getUnchecked (inputChannelIndex);

	return -1;
}

int ChannelRemappingAudioSource::getRemappedOutputChannel (const int outputChannelIndex) const throw()
{
	const ScopedLock sl (lock);

	if (outputChannelIndex >= 0 && outputChannelIndex < remappedOutputs.size())
		return remappedOutputs .getUnchecked (outputChannelIndex);

	return -1;
}

void ChannelRemappingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
	source->prepareToPlay (samplesPerBlockExpected, sampleRate);
}

void ChannelRemappingAudioSource::releaseResources()
{
	source->releaseResources();
}

void ChannelRemappingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
{
	const ScopedLock sl (lock);

	buffer.setSize (requiredNumberOfChannels, bufferToFill.numSamples, false, false, true);

	const int numChans = bufferToFill.buffer->getNumChannels();

	int i;
	for (i = 0; i < buffer.getNumChannels(); ++i)
	{
		const int remappedChan = getRemappedInputChannel (i);

		if (remappedChan >= 0 && remappedChan < numChans)
		{
			buffer.copyFrom (i, 0, *bufferToFill.buffer,
							 remappedChan,
							 bufferToFill.startSample,
							 bufferToFill.numSamples);
		}
		else
		{
			buffer.clear (i, 0, bufferToFill.numSamples);
		}
	}

	remappedInfo.numSamples = bufferToFill.numSamples;

	source->getNextAudioBlock (remappedInfo);

	bufferToFill.clearActiveBufferRegion();

	for (i = 0; i < requiredNumberOfChannels; ++i)
	{
		const int remappedChan = getRemappedOutputChannel (i);

		if (remappedChan >= 0 && remappedChan < numChans)
		{
			bufferToFill.buffer->addFrom (remappedChan, bufferToFill.startSample,
										  buffer, i, 0, bufferToFill.numSamples);

		}
	}
}

XmlElement* ChannelRemappingAudioSource::createXml() const throw()
{
	XmlElement* e = new XmlElement ("MAPPINGS");

	String ins, outs;
	int i;

	const ScopedLock sl (lock);

	for (i = 0; i < remappedInputs.size(); ++i)
		ins << remappedInputs.getUnchecked(i) << ' ';

	for (i = 0; i < remappedOutputs.size(); ++i)
		outs << remappedOutputs.getUnchecked(i) << ' ';

	e->setAttribute ("inputs", ins.trimEnd());
	e->setAttribute ("outputs", outs.trimEnd());

	return e;
}

void ChannelRemappingAudioSource::restoreFromXml (const XmlElement& e) throw()
{
	if (e.hasTagName ("MAPPINGS"))
	{
		const ScopedLock sl (lock);

		clearAllMappings();

		StringArray ins, outs;
		ins.addTokens (e.getStringAttribute ("inputs"), false);
		outs.addTokens (e.getStringAttribute ("outputs"), false);

		int i;
		for (i = 0; i < ins.size(); ++i)
			remappedInputs.add (ins[i].getIntValue());

		for (i = 0; i < outs.size(); ++i)
			remappedOutputs.add (outs[i].getIntValue());
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ChannelRemappingAudioSource.cpp ***/


/*** Start of inlined file: juce_IIRFilterAudioSource.cpp ***/
BEGIN_JUCE_NAMESPACE

IIRFilterAudioSource::IIRFilterAudioSource (AudioSource* const inputSource,
											const bool deleteInputWhenDeleted_)
	: input (inputSource),
	  deleteInputWhenDeleted (deleteInputWhenDeleted_)
{
	jassert (inputSource != 0);

	for (int i = 2; --i >= 0;)
		iirFilters.add (new IIRFilter());
}

IIRFilterAudioSource::~IIRFilterAudioSource()
{
	if (deleteInputWhenDeleted)
		delete input;
}

void IIRFilterAudioSource::setFilterParameters (const IIRFilter& newSettings)
{
	for (int i = iirFilters.size(); --i >= 0;)
		iirFilters.getUnchecked(i)->copyCoefficientsFrom (newSettings);
}

void IIRFilterAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
	input->prepareToPlay (samplesPerBlockExpected, sampleRate);

	for (int i = iirFilters.size(); --i >= 0;)
		iirFilters.getUnchecked(i)->reset();
}

void IIRFilterAudioSource::releaseResources()
{
	input->releaseResources();
}

void IIRFilterAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
{
	input->getNextAudioBlock (bufferToFill);

	const int numChannels = bufferToFill.buffer->getNumChannels();

	while (numChannels > iirFilters.size())
		iirFilters.add (new IIRFilter (*iirFilters.getUnchecked (0)));

	for (int i = 0; i < numChannels; ++i)
		iirFilters.getUnchecked(i)
			->processSamples (bufferToFill.buffer->getSampleData (i, bufferToFill.startSample),
							  bufferToFill.numSamples);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_IIRFilterAudioSource.cpp ***/


/*** Start of inlined file: juce_MixerAudioSource.cpp ***/
BEGIN_JUCE_NAMESPACE

MixerAudioSource::MixerAudioSource()
	: tempBuffer (2, 0),
	  currentSampleRate (0.0),
	  bufferSizeExpected (0)
{
}

MixerAudioSource::~MixerAudioSource()
{
	removeAllInputs();
}

void MixerAudioSource::addInputSource (AudioSource* input, const bool deleteWhenRemoved)
{
	if (input != 0 && ! inputs.contains (input))
	{
		double localRate;
		int localBufferSize;

		{
			const ScopedLock sl (lock);
			localRate = currentSampleRate;
			localBufferSize = bufferSizeExpected;
		}

		if (localRate != 0.0)
			input->prepareToPlay (localBufferSize, localRate);

		const ScopedLock sl (lock);

		inputsToDelete.setBit (inputs.size(), deleteWhenRemoved);
		inputs.add (input);
	}
}

void MixerAudioSource::removeInputSource (AudioSource* input, const bool deleteInput)
{
	if (input != 0)
	{
		int index;

		{
			const ScopedLock sl (lock);

			index = inputs.indexOf (input);

			if (index >= 0)
			{
				inputsToDelete.shiftBits (index, 1);
				inputs.remove (index);
			}
		}

		if (index >= 0)
		{
			input->releaseResources();

			if (deleteInput)
				delete input;
		}
	}
}

void MixerAudioSource::removeAllInputs()
{
	VoidArray inputsCopy;
	BigInteger inputsToDeleteCopy;

	{
		const ScopedLock sl (lock);
		inputsCopy = inputs;
		inputsToDeleteCopy = inputsToDelete;
	}

	for (int i = inputsCopy.size(); --i >= 0;)
		if (inputsToDeleteCopy[i])
			delete (AudioSource*) inputsCopy[i];
}

void MixerAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
	tempBuffer.setSize (2, samplesPerBlockExpected);

	const ScopedLock sl (lock);

	currentSampleRate = sampleRate;
	bufferSizeExpected = samplesPerBlockExpected;

	for (int i = inputs.size(); --i >= 0;)
		((AudioSource*) inputs.getUnchecked(i))->prepareToPlay (samplesPerBlockExpected,
																sampleRate);
}

void MixerAudioSource::releaseResources()
{
	const ScopedLock sl (lock);

	for (int i = inputs.size(); --i >= 0;)
		((AudioSource*) inputs.getUnchecked(i))->releaseResources();

	tempBuffer.setSize (2, 0);

	currentSampleRate = 0;
	bufferSizeExpected = 0;
}

void MixerAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
	const ScopedLock sl (lock);

	if (inputs.size() > 0)
	{
		((AudioSource*) inputs.getUnchecked(0))->getNextAudioBlock (info);

		if (inputs.size() > 1)
		{
			tempBuffer.setSize (jmax (1, info.buffer->getNumChannels()),
								info.buffer->getNumSamples());

			AudioSourceChannelInfo info2;
			info2.buffer = &tempBuffer;
			info2.numSamples = info.numSamples;
			info2.startSample = 0;

			for (int i = 1; i < inputs.size(); ++i)
			{
				((AudioSource*) inputs.getUnchecked(i))->getNextAudioBlock (info2);

				for (int chan = 0; chan < info.buffer->getNumChannels(); ++chan)
					info.buffer->addFrom (chan, info.startSample, tempBuffer, chan, 0, info.numSamples);
			}
		}
	}
	else
	{
		info.clearActiveBufferRegion();
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MixerAudioSource.cpp ***/


/*** Start of inlined file: juce_ResamplingAudioSource.cpp ***/
BEGIN_JUCE_NAMESPACE

ResamplingAudioSource::ResamplingAudioSource (AudioSource* const inputSource,
											  const bool deleteInputWhenDeleted_)
	: input (inputSource),
	  deleteInputWhenDeleted (deleteInputWhenDeleted_),
	  ratio (1.0),
	  lastRatio (1.0),
	  buffer (2, 0),
	  sampsInBuffer (0)
{
	jassert (input != 0);
}

ResamplingAudioSource::~ResamplingAudioSource()
{
	if (deleteInputWhenDeleted)
		delete input;
}

void ResamplingAudioSource::setResamplingRatio (const double samplesInPerOutputSample)
{
	jassert (samplesInPerOutputSample > 0);

	const ScopedLock sl (ratioLock);
	ratio = jmax (0.0, samplesInPerOutputSample);
}

void ResamplingAudioSource::prepareToPlay (int samplesPerBlockExpected,
										   double sampleRate)
{
	const ScopedLock sl (ratioLock);

	input->prepareToPlay (samplesPerBlockExpected, sampleRate);

	buffer.setSize (2, roundToInt (samplesPerBlockExpected * ratio) + 32);
	buffer.clear();
	sampsInBuffer = 0;
	bufferPos = 0;
	subSampleOffset = 0.0;

	createLowPass (ratio);
	resetFilters();
}

void ResamplingAudioSource::releaseResources()
{
	input->releaseResources();
	buffer.setSize (2, 0);
}

void ResamplingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
	const ScopedLock sl (ratioLock);

	if (lastRatio != ratio)
	{
		createLowPass (ratio);
		lastRatio = ratio;
	}

	const int sampsNeeded = roundToInt (info.numSamples * ratio) + 2;

	int bufferSize = buffer.getNumSamples();

	if (bufferSize < sampsNeeded + 8)
	{
		bufferPos %= bufferSize;
		bufferSize = sampsNeeded + 32;
		buffer.setSize (buffer.getNumChannels(), bufferSize, true, true);
	}

	bufferPos %= bufferSize;

	int endOfBufferPos = bufferPos + sampsInBuffer;

	while (sampsNeeded > sampsInBuffer)
	{
		endOfBufferPos %= bufferSize;

		int numToDo = jmin (sampsNeeded - sampsInBuffer,
							bufferSize - endOfBufferPos);

		AudioSourceChannelInfo readInfo;
		readInfo.buffer = &buffer;
		readInfo.numSamples = numToDo;
		readInfo.startSample = endOfBufferPos;

		input->getNextAudioBlock (readInfo);

		if (ratio > 1.0001)
		{
			// for down-sampling, pre-apply the filter..

			for (int i = jmin (2, info.buffer->getNumChannels()); --i >= 0;)
				applyFilter (buffer.getSampleData (i, endOfBufferPos), numToDo, filterStates[i]);
		}

		sampsInBuffer += numToDo;
		endOfBufferPos += numToDo;
	}

	float* dl = info.buffer->getSampleData (0, info.startSample);
	float* dr = (info.buffer->getNumChannels() > 1) ? info.buffer->getSampleData (1, info.startSample) : 0;

	const float* const bl = buffer.getSampleData (0, 0);
	const float* const br = buffer.getSampleData (1, 0);

	int nextPos = (bufferPos + 1) % bufferSize;

	for (int m = info.numSamples; --m >= 0;)
	{
		const float alpha = (float) subSampleOffset;
		const float invAlpha = 1.0f - alpha;

		*dl++ = bl [bufferPos] * invAlpha + bl [nextPos] * alpha;

		if (dr != 0)
			*dr++ = br [bufferPos] * invAlpha + br [nextPos] * alpha;

		subSampleOffset += ratio;

		jassert (sampsInBuffer > 0);

		while (subSampleOffset >= 1.0)
		{
			if (++bufferPos >= bufferSize)
				bufferPos = 0;

			--sampsInBuffer;

			nextPos = (bufferPos + 1) % bufferSize;
			subSampleOffset -= 1.0;
		}
	}

	if (ratio < 0.9999)
	{
		// for up-sampling, apply the filter after transposing..

		for (int i = jmin (2, info.buffer->getNumChannels()); --i >= 0;)
			applyFilter (info.buffer->getSampleData (i, info.startSample), info.numSamples, filterStates[i]);
	}
	else if (ratio <= 1.0001)
	{
		// if the filter's not currently being applied, keep it stoked with the last couple of samples to avoid discontinuities
		for (int i = jmin (2, info.buffer->getNumChannels()); --i >= 0;)
		{
			const float* const endOfBuffer = info.buffer->getSampleData (i, info.startSample + info.numSamples - 1);
			FilterState& fs = filterStates[i];

			if (info.numSamples > 1)
			{
				fs.y2 = fs.x2 = *(endOfBuffer - 1);
			}
			else
			{
				fs.y2 = fs.y1;
				fs.x2 = fs.x1;
			}

			fs.y1 = fs.x1 = *endOfBuffer;
		}
	}

	jassert (sampsInBuffer >= 0);
}

void ResamplingAudioSource::createLowPass (const double frequencyRatio)
{
	const double proportionalRate = (frequencyRatio > 1.0) ? 0.5 / frequencyRatio
														   : 0.5 * frequencyRatio;

	const double n = 1.0 / tan (double_Pi * jmax (0.001, proportionalRate));
	const double nSquared = n * n;
	const double c1 = 1.0 / (1.0 + sqrt (2.0) * n + nSquared);

	setFilterCoefficients (c1,
						   c1 * 2.0f,
						   c1,
						   1.0,
						   c1 * 2.0 * (1.0 - nSquared),
						   c1 * (1.0 - sqrt (2.0) * n + nSquared));
}

void ResamplingAudioSource::setFilterCoefficients (double c1, double c2, double c3, double c4, double c5, double c6)
{
	const double a = 1.0 / c4;

	c1 *= a;
	c2 *= a;
	c3 *= a;
	c5 *= a;
	c6 *= a;

	coefficients[0] = c1;
	coefficients[1] = c2;
	coefficients[2] = c3;
	coefficients[3] = c4;
	coefficients[4] = c5;
	coefficients[5] = c6;
}

void ResamplingAudioSource::resetFilters()
{
	zeromem (filterStates, sizeof (filterStates));
}

void ResamplingAudioSource::applyFilter (float* samples, int num, FilterState& fs)
{
	while (--num >= 0)
	{
		const double in = *samples;

		double out = coefficients[0] * in
					 + coefficients[1] * fs.x1
					 + coefficients[2] * fs.x2
					 - coefficients[4] * fs.y1
					 - coefficients[5] * fs.y2;

#if JUCE_INTEL
		if (! (out < -1.0e-8 || out > 1.0e-8))
			out = 0;
#endif

		fs.x2 = fs.x1;
		fs.x1 = in;
		fs.y2 = fs.y1;
		fs.y1 = out;

		*samples++ = (float) out;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ResamplingAudioSource.cpp ***/


/*** Start of inlined file: juce_ToneGeneratorAudioSource.cpp ***/
BEGIN_JUCE_NAMESPACE

ToneGeneratorAudioSource::ToneGeneratorAudioSource()
	: frequency (1000.0),
	  sampleRate (44100.0),
	  currentPhase (0.0),
	  phasePerSample (0.0),
	  amplitude (0.5f)
{
}

ToneGeneratorAudioSource::~ToneGeneratorAudioSource()
{
}

void ToneGeneratorAudioSource::setAmplitude (const float newAmplitude)
{
	amplitude = newAmplitude;
}

void ToneGeneratorAudioSource::setFrequency (const double newFrequencyHz)
{
	frequency = newFrequencyHz;
	phasePerSample = 0.0;
}

void ToneGeneratorAudioSource::prepareToPlay (int /*samplesPerBlockExpected*/,
											  double sampleRate_)
{
	currentPhase = 0.0;
	phasePerSample = 0.0;
	sampleRate = sampleRate_;
}

void ToneGeneratorAudioSource::releaseResources()
{
}

void ToneGeneratorAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
	if (phasePerSample == 0.0)
		phasePerSample = double_Pi * 2.0 / (sampleRate / frequency);

	for (int i = 0; i < info.numSamples; ++i)
	{
		const float sample = amplitude * (float) sin (currentPhase);
		currentPhase += phasePerSample;

		for (int j = info.buffer->getNumChannels(); --j >= 0;)
			*info.buffer->getSampleData (j, info.startSample + i) = sample;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ToneGeneratorAudioSource.cpp ***/


/*** Start of inlined file: juce_AudioDeviceManager.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioDeviceManager::AudioDeviceSetup::AudioDeviceSetup()
	: sampleRate (0),
	  bufferSize (0),
	  useDefaultInputChannels (true),
	  useDefaultOutputChannels (true)
{
}

bool AudioDeviceManager::AudioDeviceSetup::operator== (const AudioDeviceManager::AudioDeviceSetup& other) const
{
	return outputDeviceName == other.outputDeviceName
			&& inputDeviceName == other.inputDeviceName
			&& sampleRate == other.sampleRate
			&& bufferSize == other.bufferSize
			&& inputChannels == other.inputChannels
			&& useDefaultInputChannels == other.useDefaultInputChannels
			&& outputChannels == other.outputChannels
			&& useDefaultOutputChannels == other.useDefaultOutputChannels;
}

AudioDeviceManager::AudioDeviceManager()
	: currentAudioDevice (0),
	  numInputChansNeeded (0),
	  numOutputChansNeeded (2),
	  listNeedsScanning (true),
	  useInputNames (false),
	  inputLevelMeasurementEnabledCount (0),
	  inputLevel (0),
	  tempBuffer (2, 2),
	  defaultMidiOutput (0),
	  cpuUsageMs (0),
	  timeToCpuScale (0)
{
	callbackHandler.owner = this;
}

AudioDeviceManager::~AudioDeviceManager()
{
	currentAudioDevice = 0;
	defaultMidiOutput = 0;
}

void AudioDeviceManager::createDeviceTypesIfNeeded()
{
	if (availableDeviceTypes.size() == 0)
	{
		createAudioDeviceTypes (availableDeviceTypes);

		while (lastDeviceTypeConfigs.size() < availableDeviceTypes.size())
			lastDeviceTypeConfigs.add (new AudioDeviceSetup());

		if (availableDeviceTypes.size() > 0)
			currentDeviceType = availableDeviceTypes.getUnchecked(0)->getTypeName();
	}
}

const OwnedArray <AudioIODeviceType>& AudioDeviceManager::getAvailableDeviceTypes()
{
	scanDevicesIfNeeded();
	return availableDeviceTypes;
}

AudioIODeviceType* juce_createAudioIODeviceType_CoreAudio();
AudioIODeviceType* juce_createAudioIODeviceType_iPhoneAudio();
AudioIODeviceType* juce_createAudioIODeviceType_WASAPI();
AudioIODeviceType* juce_createAudioIODeviceType_DirectSound();
AudioIODeviceType* juce_createAudioIODeviceType_ASIO();
AudioIODeviceType* juce_createAudioIODeviceType_ALSA();
AudioIODeviceType* juce_createAudioIODeviceType_JACK();

void AudioDeviceManager::createAudioDeviceTypes (OwnedArray <AudioIODeviceType>& list)
{
	(void) list; // (to avoid 'unused param' warnings)

	#if JUCE_WINDOWS
	 #if JUCE_WASAPI
	 if (SystemStats::getOperatingSystemType() >= SystemStats::WinVista)
		 list.add (juce_createAudioIODeviceType_WASAPI());
	 #endif

	 #if JUCE_DIRECTSOUND
	 list.add (juce_createAudioIODeviceType_DirectSound());
	 #endif

	 #if JUCE_ASIO
	 list.add (juce_createAudioIODeviceType_ASIO());
	 #endif
	#endif

	#if JUCE_MAC
	 list.add (juce_createAudioIODeviceType_CoreAudio());
	#endif

	#if JUCE_IPHONE
	 list.add (juce_createAudioIODeviceType_iPhoneAudio());
	#endif

	#if JUCE_LINUX && JUCE_ALSA
	 list.add (juce_createAudioIODeviceType_ALSA());
	#endif

	#if JUCE_LINUX && JUCE_JACK
	 list.add (juce_createAudioIODeviceType_JACK());
	#endif
}

const String AudioDeviceManager::initialise (const int numInputChannelsNeeded,
											 const int numOutputChannelsNeeded,
											 const XmlElement* const e,
											 const bool selectDefaultDeviceOnFailure,
											 const String& preferredDefaultDeviceName,
											 const AudioDeviceSetup* preferredSetupOptions)
{
	scanDevicesIfNeeded();

	numInputChansNeeded = numInputChannelsNeeded;
	numOutputChansNeeded = numOutputChannelsNeeded;

	if (e != 0 && e->hasTagName ("DEVICESETUP"))
	{
		lastExplicitSettings = new XmlElement (*e);

		String error;
		AudioDeviceSetup setup;

		if (preferredSetupOptions != 0)
			setup = *preferredSetupOptions;

		if (e->getStringAttribute ("audioDeviceName").isNotEmpty())
		{
			setup.inputDeviceName = setup.outputDeviceName
				= e->getStringAttribute ("audioDeviceName");
		}
		else
		{
			setup.inputDeviceName = e->getStringAttribute ("audioInputDeviceName");
			setup.outputDeviceName = e->getStringAttribute ("audioOutputDeviceName");
		}

		currentDeviceType = e->getStringAttribute ("deviceType");
		if (currentDeviceType.isEmpty())
		{
			AudioIODeviceType* const type = findType (setup.inputDeviceName, setup.outputDeviceName);

			if (type != 0)
				currentDeviceType = type->getTypeName();
			else if (availableDeviceTypes.size() > 0)
				currentDeviceType = availableDeviceTypes[0]->getTypeName();
		}

		setup.bufferSize = e->getIntAttribute ("audioDeviceBufferSize");
		setup.sampleRate = e->getDoubleAttribute ("audioDeviceRate");

		setup.inputChannels.parseString (e->getStringAttribute ("audioDeviceInChans", "11"), 2);
		setup.outputChannels.parseString (e->getStringAttribute ("audioDeviceOutChans", "11"), 2);

		setup.useDefaultInputChannels = ! e->hasAttribute ("audioDeviceInChans");
		setup.useDefaultOutputChannels = ! e->hasAttribute ("audioDeviceOutChans");

		error = setAudioDeviceSetup (setup, true);

		midiInsFromXml.clear();
		forEachXmlChildElementWithTagName (*e, c, "MIDIINPUT")
			midiInsFromXml.add (c->getStringAttribute ("name"));

		const StringArray allMidiIns (MidiInput::getDevices());

		for (int i = allMidiIns.size(); --i >= 0;)
			setMidiInputEnabled (allMidiIns[i], midiInsFromXml.contains (allMidiIns[i]));

		if (error.isNotEmpty() && selectDefaultDeviceOnFailure)
			error = initialise (numInputChannelsNeeded, numOutputChannelsNeeded, 0,
								false, preferredDefaultDeviceName);

		setDefaultMidiOutput (e->getStringAttribute ("defaultMidiOutput"));

		return error;
	}
	else
	{
		AudioDeviceSetup setup;

		if (preferredSetupOptions != 0)
		{
			setup = *preferredSetupOptions;
		}
		else if (preferredDefaultDeviceName.isNotEmpty())
		{
			for (int j = availableDeviceTypes.size(); --j >= 0;)
			{
				AudioIODeviceType* const type = availableDeviceTypes.getUnchecked(j);

				StringArray outs (type->getDeviceNames (false));

				int i;
				for (i = 0; i < outs.size(); ++i)
				{
					if (outs[i].matchesWildcard (preferredDefaultDeviceName, true))
					{
						setup.outputDeviceName = outs[i];
						break;
					}
				}

				StringArray ins (type->getDeviceNames (true));

				for (i = 0; i < ins.size(); ++i)
				{
					if (ins[i].matchesWildcard (preferredDefaultDeviceName, true))
					{
						setup.inputDeviceName = ins[i];
						break;
					}
				}
			}
		}

		insertDefaultDeviceNames (setup);
		return setAudioDeviceSetup (setup, false);
	}
}

void AudioDeviceManager::insertDefaultDeviceNames (AudioDeviceSetup& setup) const
{
	AudioIODeviceType* type = getCurrentDeviceTypeObject();
	if (type != 0)
	{
		if (setup.outputDeviceName.isEmpty())
			setup.outputDeviceName = type->getDeviceNames (false) [type->getDefaultDeviceIndex (false)];

		if (setup.inputDeviceName.isEmpty())
			setup.inputDeviceName = type->getDeviceNames (true) [type->getDefaultDeviceIndex (true)];
	}
}

XmlElement* AudioDeviceManager::createStateXml() const
{
	return lastExplicitSettings != 0 ? new XmlElement (*lastExplicitSettings) : 0;
}

void AudioDeviceManager::scanDevicesIfNeeded()
{
	if (listNeedsScanning)
	{
		listNeedsScanning = false;

		createDeviceTypesIfNeeded();

		for (int i = availableDeviceTypes.size(); --i >= 0;)
			availableDeviceTypes.getUnchecked(i)->scanForDevices();
	}
}

AudioIODeviceType* AudioDeviceManager::findType (const String& inputName, const String& outputName)
{
	scanDevicesIfNeeded();

	for (int i = availableDeviceTypes.size(); --i >= 0;)
	{
		AudioIODeviceType* const type = availableDeviceTypes.getUnchecked(i);

		if ((inputName.isNotEmpty() && type->getDeviceNames (true).contains (inputName, true))
			|| (outputName.isNotEmpty() && type->getDeviceNames (false).contains (outputName, true)))
		{
			return type;
		}
	}

	return 0;
}

void AudioDeviceManager::getAudioDeviceSetup (AudioDeviceSetup& setup)
{
	setup = currentSetup;
}

void AudioDeviceManager::deleteCurrentDevice()
{
	currentAudioDevice = 0;
	currentSetup.inputDeviceName = String::empty;
	currentSetup.outputDeviceName = String::empty;
}

void AudioDeviceManager::setCurrentAudioDeviceType (const String& type,
													const bool treatAsChosenDevice)
{
	for (int i = 0; i < availableDeviceTypes.size(); ++i)
	{
		if (availableDeviceTypes.getUnchecked(i)->getTypeName() == type
			 && currentDeviceType != type)
		{
			currentDeviceType = type;

			AudioDeviceSetup s (*lastDeviceTypeConfigs.getUnchecked(i));
			insertDefaultDeviceNames (s);

			setAudioDeviceSetup (s, treatAsChosenDevice);

			sendChangeMessage (this);
			break;
		}
	}
}

AudioIODeviceType* AudioDeviceManager::getCurrentDeviceTypeObject() const
{
	for (int i = 0; i < availableDeviceTypes.size(); ++i)
		if (availableDeviceTypes[i]->getTypeName() == currentDeviceType)
			return availableDeviceTypes[i];

	return availableDeviceTypes[0];
}

const String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup,
													  const bool treatAsChosenDevice)
{
	jassert (&newSetup != &currentSetup);	// this will have no effect

	if (newSetup == currentSetup && currentAudioDevice != 0)
		return String::empty;

	if (! (newSetup == currentSetup))
		sendChangeMessage (this);

	stopDevice();

	const String newInputDeviceName (numInputChansNeeded == 0 ? String::empty : newSetup.inputDeviceName);
	const String newOutputDeviceName (numOutputChansNeeded == 0 ? String::empty : newSetup.outputDeviceName);

	String error;
	AudioIODeviceType* type = getCurrentDeviceTypeObject();

	if (type == 0 || (newInputDeviceName.isEmpty() && newOutputDeviceName.isEmpty()))
	{
		deleteCurrentDevice();

		if (treatAsChosenDevice)
			updateXml();

		return String::empty;
	}

	if (currentSetup.inputDeviceName != newInputDeviceName
		 || currentSetup.outputDeviceName != newOutputDeviceName
		 || currentAudioDevice == 0)
	{
		deleteCurrentDevice();
		scanDevicesIfNeeded();

		if (newOutputDeviceName.isNotEmpty()
			 && ! type->getDeviceNames (false).contains (newOutputDeviceName))
		{
			return "No such device: " + newOutputDeviceName;
		}

		if (newInputDeviceName.isNotEmpty()
			 && ! type->getDeviceNames (true).contains (newInputDeviceName))
		{
			return "No such device: " + newInputDeviceName;
		}

		currentAudioDevice = type->createDevice (newOutputDeviceName, newInputDeviceName);

		if (currentAudioDevice == 0)
			error = "Can't open the audio device!\n\nThis may be because another application is currently using the same device - if so, you should close any other applications and try again!";
		else
			error = currentAudioDevice->getLastError();

		if (error.isNotEmpty())
		{
			deleteCurrentDevice();
			return error;
		}

		if (newSetup.useDefaultInputChannels)
		{
			inputChannels.clear();
			inputChannels.setRange (0, numInputChansNeeded, true);
		}

		if (newSetup.useDefaultOutputChannels)
		{
			outputChannels.clear();
			outputChannels.setRange (0, numOutputChansNeeded, true);
		}

		if (newInputDeviceName.isEmpty())
			inputChannels.clear();

		if (newOutputDeviceName.isEmpty())
			outputChannels.clear();
	}

	if (! newSetup.useDefaultInputChannels)
		inputChannels = newSetup.inputChannels;

	if (! newSetup.useDefaultOutputChannels)
		outputChannels = newSetup.outputChannels;

	currentSetup = newSetup;

	currentSetup.sampleRate = chooseBestSampleRate (newSetup.sampleRate);

	error = currentAudioDevice->open (inputChannels,
									  outputChannels,
									  currentSetup.sampleRate,
									  currentSetup.bufferSize);

	if (error.isEmpty())
	{
		currentDeviceType = currentAudioDevice->getTypeName();

		currentAudioDevice->start (&callbackHandler);

		currentSetup.sampleRate = currentAudioDevice->getCurrentSampleRate();
		currentSetup.bufferSize = currentAudioDevice->getCurrentBufferSizeSamples();
		currentSetup.inputChannels = currentAudioDevice->getActiveInputChannels();
		currentSetup.outputChannels = currentAudioDevice->getActiveOutputChannels();

		for (int i = 0; i < availableDeviceTypes.size(); ++i)
			if (availableDeviceTypes.getUnchecked (i)->getTypeName() == currentDeviceType)
				*(lastDeviceTypeConfigs.getUnchecked (i)) = currentSetup;

		if (treatAsChosenDevice)
			updateXml();
	}
	else
	{
		deleteCurrentDevice();
	}

	return error;
}

double AudioDeviceManager::chooseBestSampleRate (double rate) const
{
	jassert (currentAudioDevice != 0);

	if (rate > 0)
	{
		bool ok = false;

		for (int i = currentAudioDevice->getNumSampleRates(); --i >= 0;)
		{
			const double sr = currentAudioDevice->getSampleRate (i);

			if (sr == rate)
				ok = true;
		}

		if (! ok)
			rate = 0;
	}

	if (rate == 0)
	{
		double lowestAbove44 = 0.0;

		for (int i = currentAudioDevice->getNumSampleRates(); --i >= 0;)
		{
			const double sr = currentAudioDevice->getSampleRate (i);

			if (sr >= 44100.0 && (lowestAbove44 == 0 || sr < lowestAbove44))
				lowestAbove44 = sr;
		}

		if (lowestAbove44 == 0.0)
			rate = currentAudioDevice->getSampleRate (0);
		else
			rate = lowestAbove44;
	}

	return rate;
}

void AudioDeviceManager::stopDevice()
{
	if (currentAudioDevice != 0)
		currentAudioDevice->stop();

	testSound = 0;
}

void AudioDeviceManager::closeAudioDevice()
{
	stopDevice();
	currentAudioDevice = 0;
}

void AudioDeviceManager::restartLastAudioDevice()
{
	if (currentAudioDevice == 0)
	{
		if (currentSetup.inputDeviceName.isEmpty()
			  && currentSetup.outputDeviceName.isEmpty())
		{
			// This method will only reload the last device that was running
			// before closeAudioDevice() was called - you need to actually open
			// one first, with setAudioDevice().
			jassertfalse
			return;
		}

		AudioDeviceSetup s (currentSetup);
		setAudioDeviceSetup (s, false);
	}
}

void AudioDeviceManager::updateXml()
{
	lastExplicitSettings = new XmlElement ("DEVICESETUP");

	lastExplicitSettings->setAttribute ("deviceType", currentDeviceType);
	lastExplicitSettings->setAttribute ("audioOutputDeviceName", currentSetup.outputDeviceName);
	lastExplicitSettings->setAttribute ("audioInputDeviceName", currentSetup.inputDeviceName);

	if (currentAudioDevice != 0)
	{
		lastExplicitSettings->setAttribute ("audioDeviceRate", currentAudioDevice->getCurrentSampleRate());

		if (currentAudioDevice->getDefaultBufferSize() != currentAudioDevice->getCurrentBufferSizeSamples())
			lastExplicitSettings->setAttribute ("audioDeviceBufferSize", currentAudioDevice->getCurrentBufferSizeSamples());

		if (! currentSetup.useDefaultInputChannels)
			lastExplicitSettings->setAttribute ("audioDeviceInChans", currentSetup.inputChannels.toString (2));

		if (! currentSetup.useDefaultOutputChannels)
			lastExplicitSettings->setAttribute ("audioDeviceOutChans", currentSetup.outputChannels.toString (2));
	}

	for (int i = 0; i < enabledMidiInputs.size(); ++i)
	{
		XmlElement* const m = lastExplicitSettings->createNewChildElement ("MIDIINPUT");
		m->setAttribute ("name", enabledMidiInputs[i]->getName());
	}

	if (midiInsFromXml.size() > 0)
	{
		// Add any midi devices that have been enabled before, but which aren't currently
		// open because the device has been disconnected.
		const StringArray availableMidiDevices (MidiInput::getDevices());

		for (int i = 0; i < midiInsFromXml.size(); ++i)
		{
			if (! availableMidiDevices.contains (midiInsFromXml[i], true))
			{
				XmlElement* const m = lastExplicitSettings->createNewChildElement ("MIDIINPUT");
				m->setAttribute ("name", midiInsFromXml[i]);
			}
		}
	}

	if (defaultMidiOutputName.isNotEmpty())
		lastExplicitSettings->setAttribute ("defaultMidiOutput", defaultMidiOutputName);
}

void AudioDeviceManager::addAudioCallback (AudioIODeviceCallback* newCallback)
{
	{
		const ScopedLock sl (audioCallbackLock);
		if (callbacks.contains (newCallback))
			return;
	}

	if (currentAudioDevice != 0 && newCallback != 0)
		newCallback->audioDeviceAboutToStart (currentAudioDevice);

	const ScopedLock sl (audioCallbackLock);
	callbacks.add (newCallback);
}

void AudioDeviceManager::removeAudioCallback (AudioIODeviceCallback* callback)
{
	if (callback != 0)
	{
		bool needsDeinitialising = currentAudioDevice != 0;

		{
			const ScopedLock sl (audioCallbackLock);

			needsDeinitialising = needsDeinitialising && callbacks.contains (callback);
			callbacks.removeValue (callback);
		}

		if (needsDeinitialising)
			callback->audioDeviceStopped();
	}
}

void AudioDeviceManager::audioDeviceIOCallbackInt (const float** inputChannelData,
												   int numInputChannels,
												   float** outputChannelData,
												   int numOutputChannels,
												   int numSamples)
{
	const ScopedLock sl (audioCallbackLock);

	if (inputLevelMeasurementEnabledCount > 0)
	{
		for (int j = 0; j < numSamples; ++j)
		{
			float s = 0;

			for (int i = 0; i < numInputChannels; ++i)
				s += fabsf (inputChannelData[i][j]);

			s /= numInputChannels;

			const double decayFactor = 0.99992;

			if (s > inputLevel)
				inputLevel = s;
			else if (inputLevel > 0.001f)
				inputLevel *= decayFactor;
			else
				inputLevel = 0;
		}
	}

	if (callbacks.size() > 0)
	{
		const double callbackStartTime = Time::getMillisecondCounterHiRes();

		tempBuffer.setSize (jmax (1, numOutputChannels), jmax (1, numSamples), false, false, true);

		callbacks.getUnchecked(0)->audioDeviceIOCallback (inputChannelData, numInputChannels,
														  outputChannelData, numOutputChannels, numSamples);

		float** const tempChans = tempBuffer.getArrayOfChannels();

		for (int i = callbacks.size(); --i > 0;)
		{
			callbacks.getUnchecked(i)->audioDeviceIOCallback (inputChannelData, numInputChannels,
															  tempChans, numOutputChannels, numSamples);

			for (int chan = 0; chan < numOutputChannels; ++chan)
			{
				const float* const src = tempChans [chan];
				float* const dst = outputChannelData [chan];

				if (src != 0 && dst != 0)
					for (int j = 0; j < numSamples; ++j)
						dst[j] += src[j];
			}
		}

		const double msTaken = Time::getMillisecondCounterHiRes() - callbackStartTime;
		const double filterAmount = 0.2;
		cpuUsageMs += filterAmount * (msTaken - cpuUsageMs);
	}
	else
	{
		for (int i = 0; i < numOutputChannels; ++i)
			zeromem (outputChannelData[i], sizeof (float) * numSamples);
	}

	if (testSound != 0)
	{
		const int numSamps = jmin (numSamples, testSound->getNumSamples() - testSoundPosition);
		const float* const src = testSound->getSampleData (0, testSoundPosition);

		for (int i = 0; i < numOutputChannels; ++i)
			for (int j = 0; j < numSamps; ++j)
				outputChannelData [i][j] += src[j];

		testSoundPosition += numSamps;
		if (testSoundPosition >= testSound->getNumSamples())
			testSound = 0;
	}
}

void AudioDeviceManager::audioDeviceAboutToStartInt (AudioIODevice* const device)
{
	cpuUsageMs = 0;

	const double sampleRate = device->getCurrentSampleRate();
	const int blockSize = device->getCurrentBufferSizeSamples();

	if (sampleRate > 0.0 && blockSize > 0)
	{
		const double msPerBlock = 1000.0 * blockSize / sampleRate;
		timeToCpuScale = (msPerBlock > 0.0) ? (1.0 / msPerBlock) : 0.0;
	}

	{
		const ScopedLock sl (audioCallbackLock);
		for (int i = callbacks.size(); --i >= 0;)
			callbacks.getUnchecked(i)->audioDeviceAboutToStart (device);
	}

	sendChangeMessage (this);
}

void AudioDeviceManager::audioDeviceStoppedInt()
{
	cpuUsageMs = 0;
	timeToCpuScale = 0;
	sendChangeMessage (this);

	const ScopedLock sl (audioCallbackLock);
	for (int i = callbacks.size(); --i >= 0;)
		callbacks.getUnchecked(i)->audioDeviceStopped();
}

double AudioDeviceManager::getCpuUsage() const
{
	return jlimit (0.0, 1.0, timeToCpuScale * cpuUsageMs);
}

void AudioDeviceManager::setMidiInputEnabled (const String& name,
											  const bool enabled)
{
	if (enabled != isMidiInputEnabled (name))
	{
		if (enabled)
		{
			const int index = MidiInput::getDevices().indexOf (name);

			if (index >= 0)
			{
				MidiInput* const min = MidiInput::openDevice (index, &callbackHandler);

				if (min != 0)
				{
					enabledMidiInputs.add (min);
					min->start();
				}
			}
		}
		else
		{
			for (int i = enabledMidiInputs.size(); --i >= 0;)
				if (enabledMidiInputs[i]->getName() == name)
					enabledMidiInputs.remove (i);
		}

		updateXml();
		sendChangeMessage (this);
	}
}

bool AudioDeviceManager::isMidiInputEnabled (const String& name) const
{
	for (int i = enabledMidiInputs.size(); --i >= 0;)
		if (enabledMidiInputs[i]->getName() == name)
			return true;

	return false;
}

void AudioDeviceManager::addMidiInputCallback (const String& name,
											   MidiInputCallback* callback)
{
	removeMidiInputCallback (name, callback);

	if (name.isEmpty())
	{
		midiCallbacks.add (callback);
		midiCallbackDevices.add (0);
	}
	else
	{
		for (int i = enabledMidiInputs.size(); --i >= 0;)
		{
			if (enabledMidiInputs[i]->getName() == name)
			{
				const ScopedLock sl (midiCallbackLock);
				midiCallbacks.add (callback);
				midiCallbackDevices.add (enabledMidiInputs[i]);
				break;
			}
		}
	}
}

void AudioDeviceManager::removeMidiInputCallback (const String& name,
												  MidiInputCallback* /*callback*/)
{
	const ScopedLock sl (midiCallbackLock);

	for (int i = midiCallbacks.size(); --i >= 0;)
	{
		String devName;

		if (midiCallbackDevices.getUnchecked(i) != 0)
			devName = midiCallbackDevices.getUnchecked(i)->getName();

		if (devName == name)
		{
			midiCallbacks.remove (i);
			midiCallbackDevices.remove (i);
		}
	}
}

void AudioDeviceManager::handleIncomingMidiMessageInt (MidiInput* source,
													   const MidiMessage& message)
{
	if (! message.isActiveSense())
	{
		const bool isDefaultSource = (source == 0 || source == enabledMidiInputs.getFirst());

		const ScopedLock sl (midiCallbackLock);

		for (int i = midiCallbackDevices.size(); --i >= 0;)
		{
			MidiInput* const md = midiCallbackDevices.getUnchecked(i);

			if (md == source || (md == 0 && isDefaultSource))
				midiCallbacks.getUnchecked(i)->handleIncomingMidiMessage (source, message);
		}
	}
}

void AudioDeviceManager::setDefaultMidiOutput (const String& deviceName)
{
	if (defaultMidiOutputName != deviceName)
	{
		SortedSet <AudioIODeviceCallback*> oldCallbacks;

		{
			const ScopedLock sl (audioCallbackLock);
			oldCallbacks = callbacks;
			callbacks.clear();
		}

		if (currentAudioDevice != 0)
			for (int i = oldCallbacks.size(); --i >= 0;)
				oldCallbacks.getUnchecked(i)->audioDeviceStopped();

		defaultMidiOutput = 0;
		defaultMidiOutputName = deviceName;

		if (deviceName.isNotEmpty())
			defaultMidiOutput = MidiOutput::openDevice (MidiOutput::getDevices().indexOf (deviceName));

		if (currentAudioDevice != 0)
			for (int i = oldCallbacks.size(); --i >= 0;)
				oldCallbacks.getUnchecked(i)->audioDeviceAboutToStart (currentAudioDevice);

		{
			const ScopedLock sl (audioCallbackLock);
			callbacks = oldCallbacks;
		}

		updateXml();
		sendChangeMessage (this);
	}
}

void AudioDeviceManager::CallbackHandler::audioDeviceIOCallback (const float** inputChannelData,
																 int numInputChannels,
																 float** outputChannelData,
																 int numOutputChannels,
																 int numSamples)
{
	owner->audioDeviceIOCallbackInt (inputChannelData, numInputChannels, outputChannelData, numOutputChannels, numSamples);
}

void AudioDeviceManager::CallbackHandler::audioDeviceAboutToStart (AudioIODevice* device)
{
	owner->audioDeviceAboutToStartInt (device);
}

void AudioDeviceManager::CallbackHandler::audioDeviceStopped()
{
	owner->audioDeviceStoppedInt();
}

void AudioDeviceManager::CallbackHandler::handleIncomingMidiMessage (MidiInput* source, const MidiMessage& message)
{
	owner->handleIncomingMidiMessageInt (source, message);
}

void AudioDeviceManager::playTestSound()
{
	{ // cunningly nested to swap, unlock and delete in that order.
		ScopedPointer <AudioSampleBuffer> oldSound;

		{
			const ScopedLock sl (audioCallbackLock);
			oldSound = testSound;
		}
	}

	testSoundPosition = 0;

	if (currentAudioDevice != 0)
	{
		const double sampleRate = currentAudioDevice->getCurrentSampleRate();
		const int soundLength = (int) sampleRate;

		AudioSampleBuffer* const newSound = new AudioSampleBuffer (1, soundLength);
		float* samples = newSound->getSampleData (0);

		const double frequency = MidiMessage::getMidiNoteInHertz (80);
		const float amplitude = 0.5f;

		const double phasePerSample = double_Pi * 2.0 / (sampleRate / frequency);

		for (int i = 0; i < soundLength; ++i)
			samples[i] = amplitude * (float) sin (i * phasePerSample);

		newSound->applyGainRamp (0, 0, soundLength / 10, 0.0f, 1.0f);
		newSound->applyGainRamp (0, soundLength - soundLength / 4, soundLength / 4, 1.0f, 0.0f);

		const ScopedLock sl (audioCallbackLock);
		testSound = newSound;
	}
}

void AudioDeviceManager::enableInputLevelMeasurement (const bool enableMeasurement)
{
	const ScopedLock sl (audioCallbackLock);

	if (enableMeasurement)
		++inputLevelMeasurementEnabledCount;
	else
		--inputLevelMeasurementEnabledCount;

	inputLevel = 0;
}

double AudioDeviceManager::getCurrentInputLevel() const
{
	jassert (inputLevelMeasurementEnabledCount > 0); // you need to call enableInputLevelMeasurement() before using this!
	return inputLevel;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioDeviceManager.cpp ***/


/*** Start of inlined file: juce_AudioIODevice.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioIODevice::AudioIODevice (const String& deviceName, const String& typeName_)
	: name (deviceName),
	  typeName (typeName_)
{
}

AudioIODevice::~AudioIODevice()
{
}

bool AudioIODevice::hasControlPanel() const
{
	return false;
}

bool AudioIODevice::showControlPanel()
{
	jassertfalse	// this should only be called for devices which return true from
					// their hasControlPanel() method.
	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioIODevice.cpp ***/


/*** Start of inlined file: juce_AudioIODeviceType.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioIODeviceType::AudioIODeviceType (const String& name)
	: typeName (name)
{
}

AudioIODeviceType::~AudioIODeviceType()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioIODeviceType.cpp ***/


/*** Start of inlined file: juce_MidiOutput.cpp ***/
BEGIN_JUCE_NAMESPACE

MidiOutput::MidiOutput()
	: Thread ("midi out"),
	  internal (0),
	  firstMessage (0)
{
}

MidiOutput::PendingMessage::PendingMessage (const uint8* const data, const int len,
											const double sampleNumber)
	: message (data, len, sampleNumber)
{
}

void MidiOutput::sendBlockOfMessages (const MidiBuffer& buffer,
									  const double millisecondCounterToStartAt,
									  double samplesPerSecondForBuffer)
{
	// You've got to call startBackgroundThread() for this to actually work..
	jassert (isThreadRunning());

	// this needs to be a value in the future - RTFM for this method!
	jassert (millisecondCounterToStartAt > 0);

	const double timeScaleFactor = 1000.0 / samplesPerSecondForBuffer;

	MidiBuffer::Iterator i (buffer);

	const uint8* data;
	int len, time;

	while (i.getNextEvent (data, len, time))
	{
		const double eventTime = millisecondCounterToStartAt + timeScaleFactor * time;

		PendingMessage* const m
			= new PendingMessage (data, len, eventTime);

		const ScopedLock sl (lock);

		if (firstMessage == 0 || firstMessage->message.getTimeStamp() > eventTime)
		{
			m->next = firstMessage;
			firstMessage = m;
		}
		else
		{
			PendingMessage* mm = firstMessage;

			while (mm->next != 0 && mm->next->message.getTimeStamp() <= eventTime)
				mm = mm->next;

			m->next = mm->next;
			mm->next = m;
		}
	}

	notify();
}

void MidiOutput::clearAllPendingMessages()
{
	const ScopedLock sl (lock);

	while (firstMessage != 0)
	{
		PendingMessage* const m = firstMessage;
		firstMessage = firstMessage->next;
		delete m;
	}
}

void MidiOutput::startBackgroundThread()
{
	startThread (9);
}

void MidiOutput::stopBackgroundThread()
{
	stopThread (5000);
}

void MidiOutput::run()
{
	while (! threadShouldExit())
	{
		uint32 now = Time::getMillisecondCounter();
		uint32 eventTime = 0;
		uint32 timeToWait = 500;

		PendingMessage* message;

		{
			const ScopedLock sl (lock);
			message = firstMessage;

			if (message != 0)
			{
				eventTime = roundToInt (message->message.getTimeStamp());

				if (eventTime > now + 20)
				{
					timeToWait = eventTime - (now + 20);
					message = 0;
				}
				else
				{
					firstMessage = message->next;
				}
			}
		}

		if (message != 0)
		{
			if (eventTime > now)
			{
				Time::waitForMillisecondCounter (eventTime);

				if (threadShouldExit())
					break;
			}

			if (eventTime > now - 200)
				sendMessageNow (message->message);

			delete message;
		}
		else
		{
			jassert (timeToWait < 1000 * 30);
			wait (timeToWait);
		}
	}

	clearAllPendingMessages();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MidiOutput.cpp ***/


/*** Start of inlined file: juce_AudioDataConverters.cpp ***/
BEGIN_JUCE_NAMESPACE

void AudioDataConverters::convertFloatToInt16LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
	const double maxVal = (double) 0x7fff;
	char* intData = static_cast <char*> (dest);

	if (dest != (void*) source || destBytesPerSample <= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			*(uint16*) intData = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
			intData += destBytesPerSample;
		}
	}
	else
	{
		intData += destBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= destBytesPerSample;
			*(uint16*) intData = ByteOrder::swapIfBigEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
		}
	}
}

void AudioDataConverters::convertFloatToInt16BE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
	const double maxVal = (double) 0x7fff;
	char* intData = static_cast <char*> (dest);

	if (dest != (void*) source || destBytesPerSample <= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			*(uint16*) intData = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
			intData += destBytesPerSample;
		}
	}
	else
	{
		intData += destBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= destBytesPerSample;
			*(uint16*) intData = ByteOrder::swapIfLittleEndian ((uint16) (short) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
		}
	}
}

void AudioDataConverters::convertFloatToInt24LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
	const double maxVal = (double) 0x7fffff;
	char* intData = static_cast <char*> (dest);

	if (dest != (void*) source || destBytesPerSample <= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			ByteOrder::littleEndian24BitToChars ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])), intData);
			intData += destBytesPerSample;
		}
	}
	else
	{
		intData += destBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= destBytesPerSample;
			ByteOrder::littleEndian24BitToChars ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])), intData);
		}
	}
}

void AudioDataConverters::convertFloatToInt24BE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
	const double maxVal = (double) 0x7fffff;
	char* intData = static_cast <char*> (dest);

	if (dest != (void*) source || destBytesPerSample <= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			ByteOrder::bigEndian24BitToChars ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])), intData);
			intData += destBytesPerSample;
		}
	}
	else
	{
		intData += destBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= destBytesPerSample;
			ByteOrder::bigEndian24BitToChars ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])), intData);
		}
	}
}

void AudioDataConverters::convertFloatToInt32LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
	const double maxVal = (double) 0x7fffffff;
	char* intData = static_cast <char*> (dest);

	if (dest != (void*) source || destBytesPerSample <= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			*(uint32*)intData = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
			intData += destBytesPerSample;
		}
	}
	else
	{
		intData += destBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= destBytesPerSample;
			*(uint32*)intData = ByteOrder::swapIfBigEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
		}
	}
}

void AudioDataConverters::convertFloatToInt32BE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
	const double maxVal = (double) 0x7fffffff;
	char* intData = static_cast <char*> (dest);

	if (dest != (void*) source || destBytesPerSample <= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			*(uint32*)intData = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
			intData += destBytesPerSample;
		}
	}
	else
	{
		intData += destBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= destBytesPerSample;
			*(uint32*)intData = ByteOrder::swapIfLittleEndian ((uint32) roundToInt (jlimit (-maxVal, maxVal, maxVal * source[i])));
		}
	}
}

void AudioDataConverters::convertFloatToFloat32LE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
	jassert (dest != (void*) source || destBytesPerSample <= 4); // This op can't be performed on in-place data!

	char* d = static_cast <char*> (dest);

	for (int i = 0; i < numSamples; ++i)
	{
		*(float*) d = source[i];

#if JUCE_BIG_ENDIAN
		*(uint32*) d = ByteOrder::swap (*(uint32*) d);
#endif

		d += destBytesPerSample;
	}
}

void AudioDataConverters::convertFloatToFloat32BE (const float* source, void* dest, int numSamples, const int destBytesPerSample)
{
	jassert (dest != (void*) source || destBytesPerSample <= 4); // This op can't be performed on in-place data!

	char* d = static_cast <char*> (dest);

	for (int i = 0; i < numSamples; ++i)
	{
		*(float*) d = source[i];

#if JUCE_LITTLE_ENDIAN
		*(uint32*) d = ByteOrder::swap (*(uint32*) d);
#endif

		d += destBytesPerSample;
	}
}

void AudioDataConverters::convertInt16LEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
{
	const float scale = 1.0f / 0x7fff;
	const char* intData = static_cast <const char*> (source);

	if (source != (void*) dest || srcBytesPerSample >= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			dest[i] = scale * (short) ByteOrder::swapIfBigEndian (*(uint16*)intData);
			intData += srcBytesPerSample;
		}
	}
	else
	{
		intData += srcBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= srcBytesPerSample;
			dest[i] = scale * (short) ByteOrder::swapIfBigEndian (*(uint16*)intData);
		}
	}
}

void AudioDataConverters::convertInt16BEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
{
	const float scale = 1.0f / 0x7fff;
	const char* intData = static_cast <const char*> (source);

	if (source != (void*) dest || srcBytesPerSample >= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			dest[i] = scale * (short) ByteOrder::swapIfLittleEndian (*(uint16*)intData);
			intData += srcBytesPerSample;
		}
	}
	else
	{
		intData += srcBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= srcBytesPerSample;
			dest[i] = scale * (short) ByteOrder::swapIfLittleEndian (*(uint16*)intData);
		}
	}
}

void AudioDataConverters::convertInt24LEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
{
	const float scale = 1.0f / 0x7fffff;
	const char* intData = static_cast <const char*> (source);

	if (source != (void*) dest || srcBytesPerSample >= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			dest[i] = scale * (short) ByteOrder::littleEndian24Bit (intData);
			intData += srcBytesPerSample;
		}
	}
	else
	{
		intData += srcBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= srcBytesPerSample;
			dest[i] = scale * (short) ByteOrder::littleEndian24Bit (intData);
		}
	}
}

void AudioDataConverters::convertInt24BEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
{
	const float scale = 1.0f / 0x7fffff;
	const char* intData = static_cast <const char*> (source);

	if (source != (void*) dest || srcBytesPerSample >= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			dest[i] = scale * (short) ByteOrder::bigEndian24Bit (intData);
			intData += srcBytesPerSample;
		}
	}
	else
	{
		intData += srcBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= srcBytesPerSample;
			dest[i] = scale * (short) ByteOrder::bigEndian24Bit (intData);
		}
	}
}

void AudioDataConverters::convertInt32LEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
{
	const float scale = 1.0f / 0x7fffffff;
	const char* intData = static_cast <const char*> (source);

	if (source != (void*) dest || srcBytesPerSample >= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			dest[i] = scale * (int) ByteOrder::swapIfBigEndian (*(uint32*) intData);
			intData += srcBytesPerSample;
		}
	}
	else
	{
		intData += srcBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= srcBytesPerSample;
			dest[i] = scale * (int) ByteOrder::swapIfBigEndian (*(uint32*) intData);
		}
	}
}

void AudioDataConverters::convertInt32BEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
{
	const float scale = 1.0f / 0x7fffffff;
	const char* intData = static_cast <const char*> (source);

	if (source != (void*) dest || srcBytesPerSample >= 4)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			dest[i] = scale * (int) ByteOrder::swapIfLittleEndian (*(uint32*) intData);
			intData += srcBytesPerSample;
		}
	}
	else
	{
		intData += srcBytesPerSample * numSamples;

		for (int i = numSamples; --i >= 0;)
		{
			intData -= srcBytesPerSample;
			dest[i] = scale * (int) ByteOrder::swapIfLittleEndian (*(uint32*) intData);
		}
	}
}

void AudioDataConverters::convertFloat32LEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
{
	const char* s = static_cast <const char*> (source);

	for (int i = 0; i < numSamples; ++i)
	{
		dest[i] = *(float*)s;

#if JUCE_BIG_ENDIAN
		uint32* const d = (uint32*) (dest + i);
		*d = ByteOrder::swap (*d);
#endif

		s += srcBytesPerSample;
	}
}

void AudioDataConverters::convertFloat32BEToFloat (const void* const source, float* const dest, int numSamples, const int srcBytesPerSample)
{
	const char* s = static_cast <const char*> (source);

	for (int i = 0; i < numSamples; ++i)
	{
		dest[i] = *(float*)s;

#if JUCE_LITTLE_ENDIAN
		uint32* const d = (uint32*) (dest + i);
		*d = ByteOrder::swap (*d);
#endif

		s += srcBytesPerSample;
	}
}

void AudioDataConverters::convertFloatToFormat (const DataFormat destFormat,
												const float* const source,
												void* const dest,
												const int numSamples)
{
	switch (destFormat)
	{
	case int16LE:
		convertFloatToInt16LE (source, dest, numSamples);
		break;

	case int16BE:
		convertFloatToInt16BE (source, dest, numSamples);
		break;

	case int24LE:
		convertFloatToInt24LE (source, dest, numSamples);
		break;

	case int24BE:
		convertFloatToInt24BE (source, dest, numSamples);
		break;

	case int32LE:
		convertFloatToInt32LE (source, dest, numSamples);
		break;

	case int32BE:
		convertFloatToInt32BE (source, dest, numSamples);
		break;

	case float32LE:
		convertFloatToFloat32LE (source, dest, numSamples);
		break;

	case float32BE:
		convertFloatToFloat32BE (source, dest, numSamples);
		break;

	default:
		jassertfalse
		break;
	}
}

void AudioDataConverters::convertFormatToFloat (const DataFormat sourceFormat,
												const void* const source,
												float* const dest,
												const int numSamples)
{
	switch (sourceFormat)
	{
	case int16LE:
		convertInt16LEToFloat (source, dest, numSamples);
		break;

	case int16BE:
		convertInt16BEToFloat (source, dest, numSamples);
		break;

	case int24LE:
		convertInt24LEToFloat (source, dest, numSamples);
		break;

	case int24BE:
		convertInt24BEToFloat (source, dest, numSamples);
		break;

	case int32LE:
		convertInt32LEToFloat (source, dest, numSamples);
		break;

	case int32BE:
		convertInt32BEToFloat (source, dest, numSamples);
		break;

	case float32LE:
		convertFloat32LEToFloat (source, dest, numSamples);
		break;

	case float32BE:
		convertFloat32BEToFloat (source, dest, numSamples);
		break;

	default:
		jassertfalse
		break;
	}
}

void AudioDataConverters::interleaveSamples (const float** const source,
											 float* const dest,
											 const int numSamples,
											 const int numChannels)
{
	for (int chan = 0; chan < numChannels; ++chan)
	{
		int i = chan;
		const float* src = source [chan];

		for (int j = 0; j < numSamples; ++j)
		{
			dest [i] = src [j];
			i += numChannels;
		}
	}
}

void AudioDataConverters::deinterleaveSamples (const float* const source,
											   float** const dest,
											   const int numSamples,
											   const int numChannels)
{
	for (int chan = 0; chan < numChannels; ++chan)
	{
		int i = chan;
		float* dst = dest [chan];

		for (int j = 0; j < numSamples; ++j)
		{
			dst [j] = source [i];
			i += numChannels;
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioDataConverters.cpp ***/


/*** Start of inlined file: juce_AudioSampleBuffer.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioSampleBuffer::AudioSampleBuffer (const int numChannels_,
									  const int numSamples) throw()
  : numChannels (numChannels_),
	size (numSamples)
{
	jassert (numSamples >= 0);
	jassert (numChannels_ > 0);

	allocateData();
}

AudioSampleBuffer::AudioSampleBuffer (const AudioSampleBuffer& other) throw()
  : numChannels (other.numChannels),
	size (other.size)
{
	allocateData();
	const size_t numBytes = size * sizeof (float);

	for (int i = 0; i < numChannels; ++i)
		memcpy (channels[i], other.channels[i], numBytes);
}

void AudioSampleBuffer::allocateData()
{
	const size_t channelListSize = (numChannels + 1) * sizeof (float*);
	allocatedBytes = (int) (numChannels * size * sizeof (float) + channelListSize + 32);
	allocatedData.malloc (allocatedBytes);
	channels = reinterpret_cast <float**> (allocatedData.getData());

	float* chan = (float*) (allocatedData + channelListSize);
	for (int i = 0; i < numChannels; ++i)
	{
		channels[i] = chan;
		chan += size;
	}

	channels [numChannels] = 0;
}

AudioSampleBuffer::AudioSampleBuffer (float** dataToReferTo,
									  const int numChannels_,
									  const int numSamples) throw()
	: numChannels (numChannels_),
	  size (numSamples),
	  allocatedBytes (0)
{
	jassert (numChannels_ > 0);
	allocateChannels (dataToReferTo);
}

void AudioSampleBuffer::setDataToReferTo (float** dataToReferTo,
										  const int newNumChannels,
										  const int newNumSamples) throw()
{
	jassert (newNumChannels > 0);

	allocatedBytes = 0;
	allocatedData.free();

	numChannels = newNumChannels;
	size = newNumSamples;

	allocateChannels (dataToReferTo);
}

void AudioSampleBuffer::allocateChannels (float** const dataToReferTo)
{
	// (try to avoid doing a malloc here, as that'll blow up things like Pro-Tools)
	if (numChannels < numElementsInArray (preallocatedChannelSpace))
	{
		channels = static_cast <float**> (preallocatedChannelSpace);
	}
	else
	{
		allocatedData.malloc (numChannels + 1, sizeof (float*));
		channels = reinterpret_cast <float**> (allocatedData.getData());
	}

	for (int i = 0; i < numChannels; ++i)
	{
		// you have to pass in the same number of valid pointers as numChannels
		jassert (dataToReferTo[i] != 0);

		channels[i] = dataToReferTo[i];
	}

	channels [numChannels] = 0;
}

AudioSampleBuffer& AudioSampleBuffer::operator= (const AudioSampleBuffer& other) throw()
{
	if (this != &other)
	{
		setSize (other.getNumChannels(), other.getNumSamples(), false, false, false);

		const size_t numBytes = size * sizeof (float);

		for (int i = 0; i < numChannels; ++i)
			memcpy (channels[i], other.channels[i], numBytes);
	}

	return *this;
}

AudioSampleBuffer::~AudioSampleBuffer() throw()
{
}

void AudioSampleBuffer::setSize (const int newNumChannels,
								 const int newNumSamples,
								 const bool keepExistingContent,
								 const bool clearExtraSpace,
								 const bool avoidReallocating) throw()
{
	jassert (newNumChannels > 0);

	if (newNumSamples != size || newNumChannels != numChannels)
	{
		const size_t channelListSize = (newNumChannels + 1) * sizeof (float*);
		const size_t newTotalBytes = (newNumChannels * newNumSamples * sizeof (float)) + channelListSize + 32;

		if (keepExistingContent)
		{
			HeapBlock <char> newData;
			newData.allocate (newTotalBytes, clearExtraSpace);

			const int numChansToCopy = jmin (numChannels, newNumChannels);
			const size_t numBytesToCopy = sizeof (float) * jmin (newNumSamples, size);

			float** const newChannels = reinterpret_cast <float**> (newData.getData());
			float* newChan = reinterpret_cast <float*> (newData + channelListSize);
			for (int i = 0; i < numChansToCopy; ++i)
			{
				memcpy (newChan, channels[i], numBytesToCopy);
				newChannels[i] = newChan;
				newChan += newNumSamples;
			}

			allocatedData.swapWith (newData);
			allocatedBytes = (int) newTotalBytes;
			channels = newChannels;
		}
		else
		{
			if (avoidReallocating && allocatedBytes >= newTotalBytes)
			{
				if (clearExtraSpace)
					zeromem (allocatedData, newTotalBytes);
			}
			else
			{
				allocatedBytes = newTotalBytes;
				allocatedData.allocate (newTotalBytes, clearExtraSpace);
				channels = reinterpret_cast <float**> (allocatedData.getData());
			}

			float* chan = reinterpret_cast <float*> (allocatedData + channelListSize);
			for (int i = 0; i < newNumChannels; ++i)
			{
				channels[i] = chan;
				chan += newNumSamples;
			}
		}

		channels [newNumChannels] = 0;
		size = newNumSamples;
		numChannels = newNumChannels;
	}
}

void AudioSampleBuffer::clear() throw()
{
	for (int i = 0; i < numChannels; ++i)
		zeromem (channels[i], size * sizeof (float));
}

void AudioSampleBuffer::clear (const int startSample,
							   const int numSamples) throw()
{
	jassert (startSample >= 0 && startSample + numSamples <= size);

	for (int i = 0; i < numChannels; ++i)
		zeromem (channels [i] + startSample, numSamples * sizeof (float));
}

void AudioSampleBuffer::clear (const int channel,
							   const int startSample,
							   const int numSamples) throw()
{
	jassert (((unsigned int) channel) < (unsigned int) numChannels);
	jassert (startSample >= 0 && startSample + numSamples <= size);

	zeromem (channels [channel] + startSample, numSamples * sizeof (float));
}

void AudioSampleBuffer::applyGain (const int channel,
								   const int startSample,
								   int numSamples,
								   const float gain) throw()
{
	jassert (((unsigned int) channel) < (unsigned int) numChannels);
	jassert (startSample >= 0 && startSample + numSamples <= size);

	if (gain != 1.0f)
	{
		float* d = channels [channel] + startSample;

		if (gain == 0.0f)
		{
			zeromem (d, sizeof (float) * numSamples);
		}
		else
		{
			while (--numSamples >= 0)
				*d++ *= gain;
		}
	}
}

void AudioSampleBuffer::applyGainRamp (const int channel,
									   const int startSample,
									   int numSamples,
									   float startGain,
									   float endGain) throw()
{
	if (startGain == endGain)
	{
		applyGain (channel, startSample, numSamples, startGain);
	}
	else
	{
		jassert (((unsigned int) channel) < (unsigned int) numChannels);
		jassert (startSample >= 0 && startSample + numSamples <= size);

		const float increment = (endGain - startGain) / numSamples;
		float* d = channels [channel] + startSample;

		while (--numSamples >= 0)
		{
			*d++ *= startGain;
			startGain += increment;
		}
	}
}

void AudioSampleBuffer::applyGain (const int startSample,
								   const int numSamples,
								   const float gain) throw()
{
	for (int i = 0; i < numChannels; ++i)
		applyGain (i, startSample, numSamples, gain);
}

void AudioSampleBuffer::addFrom (const int destChannel,
								 const int destStartSample,
								 const AudioSampleBuffer& source,
								 const int sourceChannel,
								 const int sourceStartSample,
								 int numSamples,
								 const float gain) throw()
{
	jassert (&source != this || sourceChannel != destChannel);
	jassert (((unsigned int) destChannel) < (unsigned int) numChannels);
	jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
	jassert (((unsigned int) sourceChannel) < (unsigned int) source.numChannels);
	jassert (sourceStartSample >= 0 && sourceStartSample + numSamples <= source.size);

	if (gain != 0.0f && numSamples > 0)
	{
		float* d = channels [destChannel] + destStartSample;
		const float* s  = source.channels [sourceChannel] + sourceStartSample;

		if (gain != 1.0f)
		{
			while (--numSamples >= 0)
				*d++ += gain * *s++;
		}
		else
		{
			while (--numSamples >= 0)
				*d++ += *s++;
		}
	}
}

void AudioSampleBuffer::addFrom (const int destChannel,
								 const int destStartSample,
								 const float* source,
								 int numSamples,
								 const float gain) throw()
{
	jassert (((unsigned int) destChannel) < (unsigned int) numChannels);
	jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
	jassert (source != 0);

	if (gain != 0.0f && numSamples > 0)
	{
		float* d = channels [destChannel] + destStartSample;

		if (gain != 1.0f)
		{
			while (--numSamples >= 0)
				*d++ += gain * *source++;
		}
		else
		{
			while (--numSamples >= 0)
				*d++ += *source++;
		}
	}
}

void AudioSampleBuffer::addFromWithRamp (const int destChannel,
										 const int destStartSample,
										 const float* source,
										 int numSamples,
										 float startGain,
										 const float endGain) throw()
{
	jassert (((unsigned int) destChannel) < (unsigned int) numChannels);
	jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
	jassert (source != 0);

	if (startGain == endGain)
	{
		addFrom (destChannel,
				 destStartSample,
				 source,
				 numSamples,
				 startGain);
	}
	else
	{
		if (numSamples > 0 && (startGain != 0.0f || endGain != 0.0f))
		{
			const float increment = (endGain - startGain) / numSamples;
			float* d = channels [destChannel] + destStartSample;

			while (--numSamples >= 0)
			{
				*d++ += startGain * *source++;
				startGain += increment;
			}
		}
	}
}

void AudioSampleBuffer::copyFrom (const int destChannel,
								  const int destStartSample,
								  const AudioSampleBuffer& source,
								  const int sourceChannel,
								  const int sourceStartSample,
								  int numSamples) throw()
{
	jassert (&source != this || sourceChannel != destChannel);
	jassert (((unsigned int) destChannel) < (unsigned int) numChannels);
	jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
	jassert (((unsigned int) sourceChannel) < (unsigned int) source.numChannels);
	jassert (sourceStartSample >= 0 && sourceStartSample + numSamples <= source.size);

	if (numSamples > 0)
	{
		memcpy (channels [destChannel] + destStartSample,
				source.channels [sourceChannel] + sourceStartSample,
				sizeof (float) * numSamples);
	}
}

void AudioSampleBuffer::copyFrom (const int destChannel,
								  const int destStartSample,
								  const float* source,
								  int numSamples) throw()
{
	jassert (((unsigned int) destChannel) < (unsigned int) numChannels);
	jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
	jassert (source != 0);

	if (numSamples > 0)
	{
		memcpy (channels [destChannel] + destStartSample,
				source,
				sizeof (float) * numSamples);
	}
}

void AudioSampleBuffer::copyFrom (const int destChannel,
								  const int destStartSample,
								  const float* source,
								  int numSamples,
								  const float gain) throw()
{
	jassert (((unsigned int) destChannel) < (unsigned int) numChannels);
	jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
	jassert (source != 0);

	if (numSamples > 0)
	{
		float* d = channels [destChannel] + destStartSample;

		if (gain != 1.0f)
		{
			if (gain == 0)
			{
				zeromem (d, sizeof (float) * numSamples);
			}
			else
			{
				while (--numSamples >= 0)
					*d++ = gain * *source++;
			}
		}
		else
		{
			memcpy (d, source, sizeof (float) * numSamples);
		}
	}
}

void AudioSampleBuffer::copyFromWithRamp (const int destChannel,
										  const int destStartSample,
										  const float* source,
										  int numSamples,
										  float startGain,
										  float endGain) throw()
{
	jassert (((unsigned int) destChannel) < (unsigned int) numChannels);
	jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
	jassert (source != 0);

	if (startGain == endGain)
	{
		copyFrom (destChannel,
				  destStartSample,
				  source,
				  numSamples,
				  startGain);
	}
	else
	{
		if (numSamples > 0 && (startGain != 0.0f || endGain != 0.0f))
		{
			const float increment = (endGain - startGain) / numSamples;
			float* d = channels [destChannel] + destStartSample;

			while (--numSamples >= 0)
			{
				*d++ = startGain * *source++;
				startGain += increment;
			}
		}
	}
}

void AudioSampleBuffer::findMinMax (const int channel,
									const int startSample,
									int numSamples,
									float& minVal,
									float& maxVal) const throw()
{
	jassert (((unsigned int) channel) < (unsigned int) numChannels);
	jassert (startSample >= 0 && startSample + numSamples <= size);

	if (numSamples <= 0)
	{
		minVal = 0.0f;
		maxVal = 0.0f;
	}
	else
	{
		const float* d = channels [channel] + startSample;

		float mn = *d++;
		float mx = mn;

		while (--numSamples > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
		{
			const float samp = *d++;

			if (samp > mx)
				mx = samp;

			if (samp < mn)
				mn = samp;
		}

		maxVal = mx;
		minVal = mn;
	}
}

float AudioSampleBuffer::getMagnitude (const int channel,
									   const int startSample,
									   const int numSamples) const throw()
{
	jassert (((unsigned int) channel) < (unsigned int) numChannels);
	jassert (startSample >= 0 && startSample + numSamples <= size);

	float mn, mx;
	findMinMax (channel, startSample, numSamples, mn, mx);

	return jmax (mn, -mn, mx, -mx);
}

float AudioSampleBuffer::getMagnitude (const int startSample,
									   const int numSamples) const throw()
{
	float mag = 0.0f;

	for (int i = 0; i < numChannels; ++i)
		mag = jmax (mag, getMagnitude (i, startSample, numSamples));

	return mag;
}

float AudioSampleBuffer::getRMSLevel (const int channel,
									  const int startSample,
									  const int numSamples) const throw()
{
	jassert (((unsigned int) channel) < (unsigned int) numChannels);
	jassert (startSample >= 0 && startSample + numSamples <= size);

	if (numSamples <= 0 || channel < 0 || channel >= numChannels)
		return 0.0f;

	const float* const data = channels [channel] + startSample;
	double sum = 0.0;

	for (int i = 0; i < numSamples; ++i)
	{
		const float sample = data [i];
		sum += sample * sample;
	}

	return (float) sqrt (sum / numSamples);
}

void AudioSampleBuffer::readFromAudioReader (AudioFormatReader* reader,
											 const int startSample,
											 const int numSamples,
											 const int readerStartSample,
											 const bool useLeftChan,
											 const bool useRightChan) throw()
{
	jassert (reader != 0);
	jassert (startSample >= 0 && startSample + numSamples <= size);

	if (numSamples > 0)
	{
		int* chans[3];

		if (useLeftChan == useRightChan)
		{
			chans[0] = (int*) getSampleData (0, startSample);
			chans[1] = (reader->numChannels > 1 && getNumChannels() > 1) ? (int*) getSampleData (1, startSample) : 0;
		}
		else if (useLeftChan || (reader->numChannels == 1))
		{
			chans[0] = (int*) getSampleData (0, startSample);
			chans[1] = 0;
		}
		else if (useRightChan)
		{
			chans[0] = 0;
			chans[1] = (int*) getSampleData (0, startSample);
		}

		chans[2] = 0;

		reader->read (chans, 2, readerStartSample, numSamples, true);

		if (! reader->usesFloatingPointData)
		{
			for (int j = 0; j < 2; ++j)
			{
				float* const d = reinterpret_cast <float*> (chans[j]);

				if (d != 0)
				{
					const float multiplier = 1.0f / 0x7fffffff;

					for (int i = 0; i < numSamples; ++i)
						d[i] = *(int*)(d + i) * multiplier;
				}
			}
		}

		if (numChannels > 1 && (chans[0] == 0 || chans[1] == 0))
		{
			// if this is a stereo buffer and the source was mono, dupe the first channel..
			memcpy (getSampleData (1, startSample),
					getSampleData (0, startSample),
					sizeof (float) * numSamples);
		}
	}
}

void AudioSampleBuffer::writeToAudioWriter (AudioFormatWriter* writer,
											const int startSample,
											const int numSamples) const throw()
{
	jassert (startSample >= 0 && startSample + numSamples <= size);

	if (numSamples > 0)
	{
		int* chans [3];

		if (writer->isFloatingPoint())
		{
			chans[0] = (int*) getSampleData (0, startSample);

			if (numChannels > 1)
				chans[1] = (int*) getSampleData (1, startSample);
			else
				chans[1] = 0;

			chans[2] = 0;
			writer->write ((const int**) chans, numSamples);
		}
		else
		{
			HeapBlock <int> tempBuffer (numSamples * 2);
			chans[0] = tempBuffer;

			if (numChannels > 1)
				chans[1] = chans[0] + numSamples;
			else
				chans[1] = 0;

			chans[2] = 0;

			for (int j = 0; j < 2; ++j)
			{
				int* const dest = chans[j];

				if (dest != 0)
				{
					const float* const src = channels [j] + startSample;

					for (int i = 0; i < numSamples; ++i)
					{
						const double samp = src[i];

						if (samp <= -1.0)
							dest[i] = std::numeric_limits<int>::min();
						else if (samp >= 1.0)
							dest[i] = std::numeric_limits<int>::max();
						else
							dest[i] = roundToInt (std::numeric_limits<int>::max() * samp);
					}
				}
			}

			writer->write ((const int**) chans, numSamples);
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioSampleBuffer.cpp ***/


/*** Start of inlined file: juce_IIRFilter.cpp ***/
BEGIN_JUCE_NAMESPACE

IIRFilter::IIRFilter()
	: active (false)
{
	reset();
}

IIRFilter::IIRFilter (const IIRFilter& other)
	: active (other.active)
{
	const ScopedLock sl (other.processLock);
	memcpy (coefficients, other.coefficients, sizeof (coefficients));
	reset();
}

IIRFilter::~IIRFilter()
{
}

void IIRFilter::reset() throw()
{
	const ScopedLock sl (processLock);

	x1 = 0;
	x2 = 0;
	y1 = 0;
	y2 = 0;
}

float IIRFilter::processSingleSampleRaw (const float in) throw()
{
	float out = coefficients[0] * in
				 + coefficients[1] * x1
				 + coefficients[2] * x2
				 - coefficients[4] * y1
				 - coefficients[5] * y2;

#if JUCE_INTEL
	if (! (out < -1.0e-8 || out > 1.0e-8))
		out = 0;
#endif

	x2 = x1;
	x1 = in;
	y2 = y1;
	y1 = out;

	return out;
}

void IIRFilter::processSamples (float* const samples,
								const int numSamples) throw()
{
	const ScopedLock sl (processLock);

	if (active)
	{
		for (int i = 0; i < numSamples; ++i)
		{
			const float in = samples[i];

			float out = coefficients[0] * in
						 + coefficients[1] * x1
						 + coefficients[2] * x2
						 - coefficients[4] * y1
						 - coefficients[5] * y2;

#if JUCE_INTEL
			if (! (out < -1.0e-8 || out > 1.0e-8))
				out = 0;
#endif

			x2 = x1;
			x1 = in;
			y2 = y1;
			y1 = out;

			samples[i] = out;
		}
	}
}

void IIRFilter::makeLowPass (const double sampleRate,
							 const double frequency) throw()
{
	jassert (sampleRate > 0);

	const double n = 1.0 / tan (double_Pi * frequency / sampleRate);
	const double nSquared = n * n;
	const double c1 = 1.0 / (1.0 + sqrt (2.0) * n + nSquared);

	setCoefficients (c1,
					 c1 * 2.0f,
					 c1,
					 1.0,
					 c1 * 2.0 * (1.0 - nSquared),
					 c1 * (1.0 - sqrt (2.0) * n + nSquared));
}

void IIRFilter::makeHighPass (const double sampleRate,
							  const double frequency) throw()
{
	const double n = tan (double_Pi * frequency / sampleRate);
	const double nSquared = n * n;
	const double c1 = 1.0 / (1.0 + sqrt (2.0) * n + nSquared);

	setCoefficients (c1,
					 c1 * -2.0f,
					 c1,
					 1.0,
					 c1 * 2.0 * (nSquared - 1.0),
					 c1 * (1.0 - sqrt (2.0) * n + nSquared));
}

void IIRFilter::makeLowShelf (const double sampleRate,
							  const double cutOffFrequency,
							  const double Q,
							  const float gainFactor) throw()
{
	jassert (sampleRate > 0);
	jassert (Q > 0);

	const double A = jmax (0.0f, gainFactor);
	const double aminus1 = A - 1.0;
	const double aplus1 = A + 1.0;
	const double omega = (double_Pi * 2.0 * jmax (cutOffFrequency, 2.0)) / sampleRate;
	const double coso = cos (omega);
	const double beta = sin (omega) * sqrt (A) / Q;
	const double aminus1TimesCoso = aminus1 * coso;

	setCoefficients (A * (aplus1 - aminus1TimesCoso + beta),
					 A * 2.0 * (aminus1 - aplus1 * coso),
					 A * (aplus1 - aminus1TimesCoso - beta),
					 aplus1 + aminus1TimesCoso + beta,
					 -2.0 * (aminus1 + aplus1 * coso),
					 aplus1 + aminus1TimesCoso - beta);
}

void IIRFilter::makeHighShelf (const double sampleRate,
							   const double cutOffFrequency,
							   const double Q,
							   const float gainFactor) throw()
{
	jassert (sampleRate > 0);
	jassert (Q > 0);

	const double A = jmax (0.0f, gainFactor);
	const double aminus1 = A - 1.0;
	const double aplus1 = A + 1.0;
	const double omega = (double_Pi * 2.0 * jmax (cutOffFrequency, 2.0)) / sampleRate;
	const double coso = cos (omega);
	const double beta = sin (omega) * sqrt (A) / Q;
	const double aminus1TimesCoso = aminus1 * coso;

	setCoefficients (A * (aplus1 + aminus1TimesCoso + beta),
					 A * -2.0 * (aminus1 + aplus1 * coso),
					 A * (aplus1 + aminus1TimesCoso - beta),
					 aplus1 - aminus1TimesCoso + beta,
					 2.0 * (aminus1 - aplus1 * coso),
					 aplus1 - aminus1TimesCoso - beta);
}

void IIRFilter::makeBandPass (const double sampleRate,
							  const double centreFrequency,
							  const double Q,
							  const float gainFactor) throw()
{
	jassert (sampleRate > 0);
	jassert (Q > 0);

	const double A = jmax (0.0f, gainFactor);
	const double omega = (double_Pi * 2.0 * jmax (centreFrequency, 2.0)) / sampleRate;
	const double alpha = 0.5 * sin (omega) / Q;
	const double c2 = -2.0 * cos (omega);
	const double alphaTimesA = alpha * A;
	const double alphaOverA = alpha / A;

	setCoefficients (1.0 + alphaTimesA,
					 c2,
					 1.0 - alphaTimesA,
					 1.0 + alphaOverA,
					 c2,
					 1.0 - alphaOverA);
}

void IIRFilter::makeInactive() throw()
{
	const ScopedLock sl (processLock);
	active = false;
}

void IIRFilter::copyCoefficientsFrom (const IIRFilter& other) throw()
{
	const ScopedLock sl (processLock);

	memcpy (coefficients, other.coefficients, sizeof (coefficients));
	active = other.active;
}

void IIRFilter::setCoefficients (double c1,
								 double c2,
								 double c3,
								 double c4,
								 double c5,
								 double c6) throw()
{
	const double a = 1.0 / c4;

	c1 *= a;
	c2 *= a;
	c3 *= a;
	c5 *= a;
	c6 *= a;

	const ScopedLock sl (processLock);

	coefficients[0] = (float) c1;
	coefficients[1] = (float) c2;
	coefficients[2] = (float) c3;
	coefficients[3] = (float) c4;
	coefficients[4] = (float) c5;
	coefficients[5] = (float) c6;

	active = true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_IIRFilter.cpp ***/


/*** Start of inlined file: juce_MidiBuffer.cpp ***/
BEGIN_JUCE_NAMESPACE

MidiBuffer::MidiBuffer() throw()
	: bytesUsed (0)
{
}

MidiBuffer::MidiBuffer (const MidiMessage& message) throw()
	: bytesUsed (0)
{
	addEvent (message, 0);
}

MidiBuffer::MidiBuffer (const MidiBuffer& other) throw()
	: data (other.data),
	  bytesUsed (other.bytesUsed)
{
}

MidiBuffer& MidiBuffer::operator= (const MidiBuffer& other) throw()
{
	bytesUsed = other.bytesUsed;
	data = other.data;

	return *this;
}

void MidiBuffer::swapWith (MidiBuffer& other)
{
	data.swapWith (other.data);
	swapVariables <int> (bytesUsed, other.bytesUsed);
}

MidiBuffer::~MidiBuffer() throw()
{
}

inline uint8* MidiBuffer::getData() const throw()
{
	return static_cast <uint8*> (data.getData());
}

inline int MidiBuffer::getEventTime (const void* const d) throw()
{
	return *static_cast <const int*> (d);
}

inline uint16 MidiBuffer::getEventDataSize (const void* const d) throw()
{
	return *reinterpret_cast <const uint16*> (static_cast <const char*> (d) + sizeof (int));
}

inline uint16 MidiBuffer::getEventTotalSize (const void* const d) throw()
{
	return getEventDataSize (d) + sizeof (int) + sizeof (uint16);
}

void MidiBuffer::clear() throw()
{
	bytesUsed = 0;
}

void MidiBuffer::clear (const int startSample,
						const int numSamples) throw()
{
	uint8* const start = findEventAfter (getData(), startSample - 1);
	uint8* const end   = findEventAfter (start, startSample + numSamples - 1);

	if (end > start)
	{
		const int bytesToMove = bytesUsed - (int) (end - getData());

		if (bytesToMove > 0)
			memmove (start, end, bytesToMove);

		bytesUsed -= (int) (end - start);
	}
}

void MidiBuffer::addEvent (const MidiMessage& m,
						   const int sampleNumber) throw()
{
	addEvent (m.getRawData(), m.getRawDataSize(), sampleNumber);
}

static int findActualEventLength (const uint8* const data,
								  const int maxBytes) throw()
{
	unsigned int byte = (unsigned int) *data;

	int size = 0;

	if (byte == 0xf0 || byte == 0xf7)
	{
		const uint8* d = data + 1;

		while (d < data + maxBytes)
			if (*d++ == 0xf7)
				break;

		size = (int) (d - data);
	}
	else if (byte == 0xff)
	{
		int n;
		const int bytesLeft = MidiMessage::readVariableLengthVal (data + 1, n);
		size = jmin (maxBytes, n + 2 + bytesLeft);
	}
	else if (byte >= 0x80)
	{
		size = jmin (maxBytes, MidiMessage::getMessageLengthFromFirstByte ((uint8) byte));
	}

	return size;
}

void MidiBuffer::addEvent (const uint8* const newData,
						   const int maxBytes,
						   const int sampleNumber) throw()
{
	const int numBytes = findActualEventLength (newData, maxBytes);

	if (numBytes > 0)
	{
		int spaceNeeded = bytesUsed + numBytes + sizeof (int) + sizeof (uint16);
		data.ensureSize ((spaceNeeded + spaceNeeded / 2 + 8) & ~7);

		uint8* d = findEventAfter (getData(), sampleNumber);
		const int bytesToMove = bytesUsed - (int) (d - getData());

		if (bytesToMove > 0)
			memmove (d + numBytes + sizeof (int) + sizeof (uint16), d, bytesToMove);

		*reinterpret_cast <int*> (d) = sampleNumber;
		d += sizeof (int);
		*reinterpret_cast <uint16*> (d) = (uint16) numBytes;
		d += sizeof (uint16);

		memcpy (d, newData, numBytes);

		bytesUsed += numBytes + sizeof (int) + sizeof (uint16);
	}
}

void MidiBuffer::addEvents (const MidiBuffer& otherBuffer,
							const int startSample,
							const int numSamples,
							const int sampleDeltaToAdd) throw()
{
	Iterator i (otherBuffer);
	i.setNextSamplePosition (startSample);

	const uint8* eventData;
	int eventSize, position;

	while (i.getNextEvent (eventData, eventSize, position)
			&& (position < startSample + numSamples || numSamples < 0))
	{
		addEvent (eventData, eventSize, position + sampleDeltaToAdd);
	}
}

void MidiBuffer::ensureSize (size_t minimumNumBytes)
{
	data.ensureSize (minimumNumBytes);
}

bool MidiBuffer::isEmpty() const throw()
{
	return bytesUsed == 0;
}

int MidiBuffer::getNumEvents() const throw()
{
	int n = 0;
	const uint8* d = getData();
	const uint8* const end = d + bytesUsed;

	while (d < end)
	{
		d += getEventTotalSize (d);
		++n;
	}

	return n;
}

int MidiBuffer::getFirstEventTime() const throw()
{
	return bytesUsed > 0 ? getEventTime (data.getData()) : 0;
}

int MidiBuffer::getLastEventTime() const throw()
{
	if (bytesUsed == 0)
		return 0;

	const uint8* d = getData();
	const uint8* const endData = d + bytesUsed;

	for (;;)
	{
		const uint8* const nextOne = d + getEventTotalSize (d);

		if (nextOne >= endData)
			return getEventTime (d);

		d = nextOne;
	}
}

uint8* MidiBuffer::findEventAfter (uint8* d, const int samplePosition) const throw()
{
	const uint8* const endData = getData() + bytesUsed;

	while (d < endData && getEventTime (d) <= samplePosition)
		d += getEventTotalSize (d);

	return d;
}

MidiBuffer::Iterator::Iterator (const MidiBuffer& buffer_) throw()
	: buffer (buffer_),
	  data (buffer_.getData())
{
}

MidiBuffer::Iterator::~Iterator() throw()
{
}

void MidiBuffer::Iterator::setNextSamplePosition (const int samplePosition) throw()
{
	data = buffer.getData();
	const uint8* dataEnd = data + buffer.bytesUsed;

	while (data < dataEnd && getEventTime (data) < samplePosition)
		data += getEventTotalSize (data);
}

bool MidiBuffer::Iterator::getNextEvent (const uint8* &midiData, int& numBytes, int& samplePosition) throw()
{
	if (data >= buffer.getData() + buffer.bytesUsed)
		return false;

	samplePosition = getEventTime (data);
	numBytes = getEventDataSize (data);
	data += sizeof (int) + sizeof (uint16);
	midiData = data;
	data += numBytes;

	return true;
}

bool MidiBuffer::Iterator::getNextEvent (MidiMessage& result, int& samplePosition) throw()
{
	if (data >= buffer.getData() + buffer.bytesUsed)
		return false;

	samplePosition = getEventTime (data);
	const int numBytes = getEventDataSize (data);
	data += sizeof (int) + sizeof (uint16);
	result = MidiMessage (data, numBytes, samplePosition);
	data += numBytes;

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MidiBuffer.cpp ***/


/*** Start of inlined file: juce_MidiFile.cpp ***/
BEGIN_JUCE_NAMESPACE

namespace MidiFileHelpers
{
	static void writeVariableLengthInt (OutputStream& out, unsigned int v)
	{
		unsigned int buffer = v & 0x7F;

		while ((v >>= 7) != 0)
		{
			buffer <<= 8;
			buffer |= ((v & 0x7F) | 0x80);
		}

		for (;;)
		{
			out.writeByte ((char) buffer);

			if (buffer & 0x80)
				buffer >>= 8;
			else
				break;
		}
	}

	static bool parseMidiHeader (const uint8* &data, short& timeFormat, short& fileType, short& numberOfTracks) throw()
	{
		unsigned int ch = (int) ByteOrder::bigEndianInt (data);
		data += 4;

		if (ch != ByteOrder::bigEndianInt ("MThd"))
		{
			bool ok = false;

			if (ch == ByteOrder::bigEndianInt ("RIFF"))
			{
				for (int i = 0; i < 8; ++i)
				{
					ch = ByteOrder::bigEndianInt (data);
					data += 4;

					if (ch == ByteOrder::bigEndianInt ("MThd"))
					{
						ok = true;
						break;
					}
				}
			}

			if (! ok)
				return false;
		}

		unsigned int bytesRemaining = ByteOrder::bigEndianInt (data);
		data += 4;
		fileType = (short) ByteOrder::bigEndianShort (data);
		data += 2;
		numberOfTracks = (short) ByteOrder::bigEndianShort (data);
		data += 2;
		timeFormat = (short) ByteOrder::bigEndianShort (data);
		data += 2;
		bytesRemaining -= 6;
		data += bytesRemaining;

		return true;
	}

	static double convertTicksToSeconds (const double time,
										 const MidiMessageSequence& tempoEvents,
										 const int timeFormat)
	{
		if (timeFormat > 0)
		{
			int numer = 4, denom = 4;
			double tempoTime = 0.0, correctedTempoTime = 0.0;
			const double tickLen = 1.0 / (timeFormat & 0x7fff);
			double secsPerTick = 0.5 * tickLen;
			const int numEvents = tempoEvents.getNumEvents();

			for (int i = 0; i < numEvents; ++i)
			{
				const MidiMessage& m = tempoEvents.getEventPointer(i)->message;

				if (time <= m.getTimeStamp())
					break;

				if (timeFormat > 0)
				{
					correctedTempoTime = correctedTempoTime
											+ (m.getTimeStamp() - tempoTime) * secsPerTick;
				}
				else
				{
					correctedTempoTime = tickLen * m.getTimeStamp() / (((timeFormat & 0x7fff) >> 8) * (timeFormat & 0xff));
				}

				tempoTime = m.getTimeStamp();

				if (m.isTempoMetaEvent())
					secsPerTick = tickLen * m.getTempoSecondsPerQuarterNote();
				else if (m.isTimeSignatureMetaEvent())
					m.getTimeSignatureInfo (numer, denom);

				while (i + 1 < numEvents)
				{
					const MidiMessage& m2 = tempoEvents.getEventPointer(i + 1)->message;
					if (m2.getTimeStamp() == tempoTime)
					{
						++i;

						if (m2.isTempoMetaEvent())
							secsPerTick = tickLen * m2.getTempoSecondsPerQuarterNote();
						else if (m2.isTimeSignatureMetaEvent())
							m2.getTimeSignatureInfo (numer, denom);
					}
					else
					{
						break;
					}
				}

			}

			return correctedTempoTime + (time - tempoTime) * secsPerTick;
		}
		else
		{
			return time / (((timeFormat & 0x7fff) >> 8) * (timeFormat & 0xff));
		}
	}
}

MidiFile::MidiFile()
   : timeFormat ((short) (unsigned short) 0xe728)
{
}

MidiFile::~MidiFile()
{
	clear();
}

void MidiFile::clear()
{
	tracks.clear();
}

int MidiFile::getNumTracks() const throw()
{
	return tracks.size();
}

const MidiMessageSequence* MidiFile::getTrack (const int index) const throw()
{
	return tracks [index];
}

void MidiFile::addTrack (const MidiMessageSequence& trackSequence)
{
	tracks.add (new MidiMessageSequence (trackSequence));
}

short MidiFile::getTimeFormat() const throw()
{
	return timeFormat;
}

void MidiFile::setTicksPerQuarterNote (const int ticks) throw()
{
	timeFormat = (short) ticks;
}

void MidiFile::setSmpteTimeFormat (const int framesPerSecond,
								   const int subframeResolution) throw()
{
	timeFormat = (short) (((-framesPerSecond) << 8) | subframeResolution);
}

void MidiFile::findAllTempoEvents (MidiMessageSequence& tempoChangeEvents) const
{
	for (int i = tracks.size(); --i >= 0;)
	{
		const int numEvents = tracks.getUnchecked(i)->getNumEvents();

		for (int j = 0; j < numEvents; ++j)
		{
			const MidiMessage& m = tracks.getUnchecked(i)->getEventPointer (j)->message;

			if (m.isTempoMetaEvent())
				tempoChangeEvents.addEvent (m);
		}
	}
}

void MidiFile::findAllTimeSigEvents (MidiMessageSequence& timeSigEvents) const
{
	for (int i = tracks.size(); --i >= 0;)
	{
		const int numEvents = tracks.getUnchecked(i)->getNumEvents();

		for (int j = 0; j < numEvents; ++j)
		{
			const MidiMessage& m = tracks.getUnchecked(i)->getEventPointer (j)->message;

			if (m.isTimeSignatureMetaEvent())
				timeSigEvents.addEvent (m);
		}
	}
}

double MidiFile::getLastTimestamp() const
{
	double t = 0.0;

	for (int i = tracks.size(); --i >= 0;)
		t = jmax (t, tracks.getUnchecked(i)->getEndTime());

	return t;
}

bool MidiFile::readFrom (InputStream& sourceStream)
{
	clear();
	MemoryBlock data;

	const int maxSensibleMidiFileSize = 2 * 1024 * 1024;

	// (put a sanity-check on the file size, as midi files are generally small)
	if (sourceStream.readIntoMemoryBlock (data, maxSensibleMidiFileSize))
	{
		size_t size = data.getSize();
		const uint8* d = static_cast <const uint8*> (data.getData());
		short fileType, expectedTracks;

		if (size > 16 && MidiFileHelpers::parseMidiHeader (d, timeFormat, fileType, expectedTracks))
		{
			size -= (int) (d - static_cast <const uint8*> (data.getData()));

			int track = 0;

			while (size > 0 && track < expectedTracks)
			{
				const int chunkType = (int) ByteOrder::bigEndianInt (d);
				d += 4;
				const int chunkSize = (int) ByteOrder::bigEndianInt (d);
				d += 4;

				if (chunkSize <= 0)
					break;

				if (size < 0)
					return false;

				if (chunkType == (int) ByteOrder::bigEndianInt ("MTrk"))
				{
					readNextTrack (d, chunkSize);
				}

				size -= chunkSize + 8;
				d += chunkSize;
				++track;
			}

			return true;
		}
	}

	return false;
}

// a comparator that puts all the note-offs before note-ons that have the same time
int MidiFile::compareElements (const MidiMessageSequence::MidiEventHolder* const first,
							   const MidiMessageSequence::MidiEventHolder* const second)
{
	const double diff = (first->message.getTimeStamp() - second->message.getTimeStamp());

	if (diff == 0)
	{
		if (first->message.isNoteOff() && second->message.isNoteOn())
			return -1;
		else if (first->message.isNoteOn() && second->message.isNoteOff())
			return 1;
		else
			return 0;
	}
	else
	{
		return (diff > 0) ? 1 : -1;
	}
}

void MidiFile::readNextTrack (const uint8* data, int size)
{
	double time = 0;
	char lastStatusByte = 0;

	MidiMessageSequence result;

	while (size > 0)
	{
		int bytesUsed;
		const int delay = MidiMessage::readVariableLengthVal (data, bytesUsed);
		data += bytesUsed;
		size -= bytesUsed;
		time += delay;

		int messSize = 0;
		const MidiMessage mm (data, size, messSize, lastStatusByte, time);

		if (messSize <= 0)
			break;

		size -= messSize;
		data += messSize;

		result.addEvent (mm);

		const char firstByte = *(mm.getRawData());
		if ((firstByte & 0xf0) != 0xf0)
			lastStatusByte = firstByte;
	}

	// use a sort that puts all the note-offs before note-ons that have the same time
	result.list.sort (*this, true);

	result.updateMatchedPairs();

	addTrack (result);
}

void MidiFile::convertTimestampTicksToSeconds()
{
	MidiMessageSequence tempoEvents;
	findAllTempoEvents (tempoEvents);
	findAllTimeSigEvents (tempoEvents);

	for (int i = 0; i < tracks.size(); ++i)
	{
		MidiMessageSequence& ms = *tracks.getUnchecked(i);

		for (int j = ms.getNumEvents(); --j >= 0;)
		{
			MidiMessage& m = ms.getEventPointer(j)->message;

			m.setTimeStamp (MidiFileHelpers::convertTicksToSeconds (m.getTimeStamp(),
																	tempoEvents,
																	timeFormat));
		}
	}
}

bool MidiFile::writeTo (OutputStream& out)
{
	out.writeIntBigEndian ((int) ByteOrder::bigEndianInt ("MThd"));
	out.writeIntBigEndian (6);
	out.writeShortBigEndian (1); // type
	out.writeShortBigEndian ((short) tracks.size());
	out.writeShortBigEndian (timeFormat);

	for (int i = 0; i < tracks.size(); ++i)
		writeTrack (out, i);

	out.flush();

	return true;
}

void MidiFile::writeTrack (OutputStream& mainOut,
						   const int trackNum)
{
	MemoryOutputStream out;

	const MidiMessageSequence& ms = *tracks[trackNum];

	int lastTick = 0;
	char lastStatusByte = 0;

	for (int i = 0; i < ms.getNumEvents(); ++i)
	{
		const MidiMessage& mm = ms.getEventPointer(i)->message;

		const int tick = roundToInt (mm.getTimeStamp());
		const int delta = jmax (0, tick - lastTick);
		MidiFileHelpers::writeVariableLengthInt (out, delta);
		lastTick = tick;

		const char statusByte = *(mm.getRawData());

		if ((statusByte == lastStatusByte)
			 && ((statusByte & 0xf0) != 0xf0)
			 && i > 0
			 && mm.getRawDataSize() > 1)
		{
			out.write (mm.getRawData() + 1, mm.getRawDataSize() - 1);
		}
		else
		{
			out.write (mm.getRawData(), mm.getRawDataSize());
		}

		lastStatusByte = statusByte;
	}

	out.writeByte (0);
	const MidiMessage m (MidiMessage::endOfTrack());
	out.write (m.getRawData(),
			   m.getRawDataSize());

	mainOut.writeIntBigEndian ((int) ByteOrder::bigEndianInt ("MTrk"));
	mainOut.writeIntBigEndian ((int) out.getDataSize());
	mainOut.write (out.getData(), (int) out.getDataSize());
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MidiFile.cpp ***/


/*** Start of inlined file: juce_MidiKeyboardState.cpp ***/
BEGIN_JUCE_NAMESPACE

MidiKeyboardState::MidiKeyboardState()
{
	zerostruct (noteStates);
}

MidiKeyboardState::~MidiKeyboardState()
{
}

void MidiKeyboardState::reset()
{
	const ScopedLock sl (lock);
	zerostruct (noteStates);
	eventsToAdd.clear();
}

bool MidiKeyboardState::isNoteOn (const int midiChannel, const int n) const throw()
{
	jassert (midiChannel >= 0 && midiChannel <= 16);

	return ((unsigned int) n) < 128
			&& (noteStates[n] & (1 << (midiChannel - 1))) != 0;
}

bool MidiKeyboardState::isNoteOnForChannels (const int midiChannelMask, const int n) const throw()
{
	return ((unsigned int) n) < 128
			&& (noteStates[n] & midiChannelMask) != 0;
}

void MidiKeyboardState::noteOn (const int midiChannel, const int midiNoteNumber, const float velocity)
{
	jassert (midiChannel >= 0 && midiChannel <= 16);
	jassert (((unsigned int) midiNoteNumber) < 128);

	const ScopedLock sl (lock);

	if (((unsigned int) midiNoteNumber) < 128)
	{
		const int timeNow = (int) Time::getMillisecondCounter();
		eventsToAdd.addEvent (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity), timeNow);
		eventsToAdd.clear (0, timeNow - 500);

		noteOnInternal (midiChannel, midiNoteNumber, velocity);
	}
}

void MidiKeyboardState::noteOnInternal  (const int midiChannel, const int midiNoteNumber, const float velocity)
{
	if (((unsigned int) midiNoteNumber) < 128)
	{
		noteStates [midiNoteNumber] |= (1 << (midiChannel - 1));

		for (int i = listeners.size(); --i >= 0;)
			listeners.getUnchecked(i)->handleNoteOn (this, midiChannel, midiNoteNumber, velocity);
	}
}

void MidiKeyboardState::noteOff (const int midiChannel, const int midiNoteNumber)
{
	const ScopedLock sl (lock);

	if (isNoteOn (midiChannel, midiNoteNumber))
	{
		const int timeNow = (int) Time::getMillisecondCounter();
		eventsToAdd.addEvent (MidiMessage::noteOff (midiChannel, midiNoteNumber), timeNow);
		eventsToAdd.clear (0, timeNow - 500);

		noteOffInternal (midiChannel, midiNoteNumber);
	}
}

void MidiKeyboardState::noteOffInternal  (const int midiChannel, const int midiNoteNumber)
{
	if (isNoteOn (midiChannel, midiNoteNumber))
	{
		noteStates [midiNoteNumber] &= ~(1 << (midiChannel - 1));

		for (int i = listeners.size(); --i >= 0;)
			listeners.getUnchecked(i)->handleNoteOff (this, midiChannel, midiNoteNumber);
	}
}

void MidiKeyboardState::allNotesOff (const int midiChannel)
{
	const ScopedLock sl (lock);

	if (midiChannel <= 0)
	{
		for (int i = 1; i <= 16; ++i)
			allNotesOff (i);
	}
	else
	{
		for (int i = 0; i < 128; ++i)
			noteOff (midiChannel, i);
	}
}

void MidiKeyboardState::processNextMidiEvent (const MidiMessage& message)
{
	if (message.isNoteOn())
	{
		noteOnInternal (message.getChannel(), message.getNoteNumber(), message.getFloatVelocity());
	}
	else if (message.isNoteOff())
	{
		noteOffInternal (message.getChannel(), message.getNoteNumber());
	}
	else if (message.isAllNotesOff())
	{
		for (int i = 0; i < 128; ++i)
			noteOffInternal (message.getChannel(), i);
	}
}

void MidiKeyboardState::processNextMidiBuffer (MidiBuffer& buffer,
											   const int startSample,
											   const int numSamples,
											   const bool injectIndirectEvents)
{
	MidiBuffer::Iterator i (buffer);
	MidiMessage message (0xf4, 0.0);
	int time;

	const ScopedLock sl (lock);

	while (i.getNextEvent (message, time))
		processNextMidiEvent (message);

	if (injectIndirectEvents)
	{
		MidiBuffer::Iterator i2 (eventsToAdd);
		const int firstEventToAdd = eventsToAdd.getFirstEventTime();
		const double scaleFactor = numSamples / (double) (eventsToAdd.getLastEventTime() + 1 - firstEventToAdd);

		while (i2.getNextEvent (message, time))
		{
			const int pos = jlimit (0, numSamples - 1, roundToInt ((time - firstEventToAdd) * scaleFactor));
			buffer.addEvent (message, startSample + pos);
		}
	}

	eventsToAdd.clear();
}

void MidiKeyboardState::addListener (MidiKeyboardStateListener* const listener) throw()
{
	const ScopedLock sl (lock);
	listeners.addIfNotAlreadyThere (listener);
}

void MidiKeyboardState::removeListener (MidiKeyboardStateListener* const listener) throw()
{
	const ScopedLock sl (lock);
	listeners.removeValue (listener);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MidiKeyboardState.cpp ***/


/*** Start of inlined file: juce_MidiMessage.cpp ***/
BEGIN_JUCE_NAMESPACE

int MidiMessage::readVariableLengthVal (const uint8* data,
										int& numBytesUsed) throw()
{
	numBytesUsed = 0;
	int v = 0;
	int i;

	do
	{
		i = (int) *data++;

		if (++numBytesUsed > 6)
			break;

		v = (v << 7) + (i & 0x7f);

	} while (i & 0x80);

	return v;
}

int MidiMessage::getMessageLengthFromFirstByte (const uint8 firstByte) throw()
{
	// this method only works for valid starting bytes of a short midi message
	jassert (firstByte >= 0x80
			  && firstByte != 0xf0
			  && firstByte != 0xf7);

	static const char messageLengths[] =
	{
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
		2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
		2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
		1, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
	};

	return messageLengths [firstByte & 0x7f];
}

MidiMessage::MidiMessage (const void* const d, const int dataSize, const double t)
   : timeStamp (t),
	 size (dataSize)
{
	jassert (dataSize > 0);

	if (dataSize <= 4)
		data = static_cast<uint8*> (preallocatedData.asBytes);
	else
		data = static_cast<uint8*> (juce_malloc (dataSize));

	memcpy (data, d, dataSize);

	// check that the length matches the data..
	jassert (size > 3 || data[0] >= 0xf0 || getMessageLengthFromFirstByte (data[0]) == size);
}

MidiMessage::MidiMessage (const int byte1, const double t) throw()
   : timeStamp (t),
	 data (static_cast<uint8*> (preallocatedData.asBytes)),
	 size (1)
{
	data[0] = (uint8) byte1;

	// check that the length matches the data..
	jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 1);
}

MidiMessage::MidiMessage (const int byte1, const int byte2, const double t) throw()
   : timeStamp (t),
	 data (static_cast<uint8*> (preallocatedData.asBytes)),
	 size (2)
{
	data[0] = (uint8) byte1;
	data[1] = (uint8) byte2;

	// check that the length matches the data..
	jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 2);
}

MidiMessage::MidiMessage (const int byte1, const int byte2, const int byte3, const double t) throw()
   : timeStamp (t),
	 data (static_cast<uint8*> (preallocatedData.asBytes)),
	 size (3)
{
	data[0] = (uint8) byte1;
	data[1] = (uint8) byte2;
	data[2] = (uint8) byte3;

	// check that the length matches the data..
	jassert (byte1 >= 0xf0 || getMessageLengthFromFirstByte ((uint8) byte1) == 3);
}

MidiMessage::MidiMessage (const MidiMessage& other)
   : timeStamp (other.timeStamp),
	 size (other.size)
{
	if (other.data != static_cast <const uint8*> (other.preallocatedData.asBytes))
	{
		data = static_cast<uint8*> (juce_malloc (size));
		memcpy (data, other.data, size);
	}
	else
	{
		data = static_cast<uint8*> (preallocatedData.asBytes);
		preallocatedData.asInt32 = other.preallocatedData.asInt32;
	}
}

MidiMessage::MidiMessage (const MidiMessage& other, const double newTimeStamp)
   : timeStamp (newTimeStamp),
	 size (other.size)
{
	if (other.data != static_cast <const uint8*> (other.preallocatedData.asBytes))
	{
		data = static_cast<uint8*> (juce_malloc (size));
		memcpy (data, other.data, size);
	}
	else
	{
		data = static_cast<uint8*> (preallocatedData.asBytes);
		preallocatedData.asInt32 = other.preallocatedData.asInt32;
	}
}

MidiMessage::MidiMessage (const void* src_, int sz, int& numBytesUsed, const uint8 lastStatusByte, double t)
	: timeStamp (t),
	  data (static_cast<uint8*> (preallocatedData.asBytes))
{
	const uint8* src = static_cast <const uint8*> (src_);
	unsigned int byte = (unsigned int) *src;

	if (byte < 0x80)
	{
		byte = (unsigned int) (uint8) lastStatusByte;
		numBytesUsed = -1;
	}
	else
	{
		numBytesUsed = 0;
		--sz;
		++src;
	}

	if (byte >= 0x80)
	{
		if (byte == 0xf0)
		{
			const uint8* d = src;

			while (d < src + sz)
			{
				if (*d >= 0x80) // stop if we hit a status byte, and don't include it in this message
				{
					if (*d == 0xf7)   // include an 0xf7 if we hit one
						++d;

					break;
				}

				++d;
			}

			size = 1 + (int) (d - src);

			data = static_cast<uint8*> (juce_malloc (size));
			*data = (uint8) byte;
			memcpy (data + 1, src, size - 1);
		}
		else if (byte == 0xff)
		{
			int n;
			const int bytesLeft = readVariableLengthVal (src + 1, n);
			size = jmin (sz + 1, n + 2 + bytesLeft);

			data = static_cast<uint8*> (juce_malloc (size));
			*data = (uint8) byte;
			memcpy (data + 1, src, size - 1);
		}
		else
		{
			preallocatedData.asInt32 = 0;
			size = getMessageLengthFromFirstByte ((uint8) byte);
			data[0] = (uint8) byte;

			if (size > 1)
			{
				data[1] = src[0];

				if (size > 2)
					data[2] = src[1];
			}
		}

		numBytesUsed += size;
	}
	else
	{
		preallocatedData.asInt32 = 0;
		size = 0;
	}
}

MidiMessage& MidiMessage::operator= (const MidiMessage& other)
{
	if (this != &other)
	{
		timeStamp = other.timeStamp;
		size = other.size;

		if (data != static_cast <const uint8*> (preallocatedData.asBytes))
			juce_free (data);

		if (other.data != static_cast <const uint8*> (other.preallocatedData.asBytes))
		{
			data = static_cast<uint8*> (juce_malloc (size));
			memcpy (data, other.data, size);
		}
		else
		{
			data = static_cast<uint8*> (preallocatedData.asBytes);
			preallocatedData.asInt32 = other.preallocatedData.asInt32;
		}
	}

	return *this;
}

MidiMessage::~MidiMessage()
{
	if (data != static_cast <const uint8*> (preallocatedData.asBytes))
		juce_free (data);
}

int MidiMessage::getChannel() const throw()
{
	if ((data[0] & 0xf0) != 0xf0)
		return (data[0] & 0xf) + 1;
	else
		return 0;
}

bool MidiMessage::isForChannel (const int channel) const throw()
{
	jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16

	return ((data[0] & 0xf) == channel - 1)
			 && ((data[0] & 0xf0) != 0xf0);
}

void MidiMessage::setChannel (const int channel) throw()
{
	jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16

	if ((data[0] & 0xf0) != (uint8) 0xf0)
		data[0] = (uint8) ((data[0] & (uint8)0xf0)
							| (uint8)(channel - 1));
}

bool MidiMessage::isNoteOn (const bool returnTrueForVelocity0) const throw()
{
	return ((data[0] & 0xf0) == 0x90)
			 && (returnTrueForVelocity0 || data[2] != 0);
}

bool MidiMessage::isNoteOff (const bool returnTrueForNoteOnVelocity0) const throw()
{
	return ((data[0] & 0xf0) == 0x80)
			|| (returnTrueForNoteOnVelocity0 && (data[2] == 0) && ((data[0] & 0xf0) == 0x90));
}

bool MidiMessage::isNoteOnOrOff() const throw()
{
	const int d = data[0] & 0xf0;
	return (d == 0x90) || (d == 0x80);
}

int MidiMessage::getNoteNumber() const throw()
{
	return data[1];
}

void MidiMessage::setNoteNumber (const int newNoteNumber) throw()
{
	if (isNoteOnOrOff())
		data[1] = (uint8) jlimit (0, 127, newNoteNumber);
}

uint8 MidiMessage::getVelocity() const throw()
{
	if (isNoteOnOrOff())
		return data[2];
	else
		return 0;
}

float MidiMessage::getFloatVelocity() const throw()
{
	return getVelocity() * (1.0f / 127.0f);
}

void MidiMessage::setVelocity (const float newVelocity) throw()
{
	if (isNoteOnOrOff())
		data[2] = (uint8) jlimit (0, 0x7f, roundToInt (newVelocity * 127.0f));
}

void MidiMessage::multiplyVelocity (const float scaleFactor) throw()
{
	if (isNoteOnOrOff())
		data[2] = (uint8) jlimit (0, 0x7f, roundToInt (scaleFactor * data[2]));
}

bool MidiMessage::isAftertouch() const throw()
{
	return (data[0] & 0xf0) == 0xa0;
}

int MidiMessage::getAfterTouchValue() const throw()
{
	return data[2];
}

const MidiMessage MidiMessage::aftertouchChange (const int channel,
												 const int noteNum,
												 const int aftertouchValue) throw()
{
	jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
	jassert (((unsigned int) noteNum) <= 127);
	jassert (((unsigned int) aftertouchValue) <= 127);

	return MidiMessage (0xa0 | jlimit (0, 15, channel - 1),
						noteNum & 0x7f,
						aftertouchValue & 0x7f);
}

bool MidiMessage::isChannelPressure() const throw()
{
	return (data[0] & 0xf0) == 0xd0;
}

int MidiMessage::getChannelPressureValue() const throw()
{
	jassert (isChannelPressure());

	return data[1];
}

const MidiMessage MidiMessage::channelPressureChange (const int channel,
													  const int pressure) throw()
{
	jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
	jassert (((unsigned int) pressure) <= 127);

	return MidiMessage (0xd0 | jlimit (0, 15, channel - 1),
						pressure & 0x7f);
}

bool MidiMessage::isProgramChange() const throw()
{
	return (data[0] & 0xf0) == 0xc0;
}

int MidiMessage::getProgramChangeNumber() const throw()
{
	return data[1];
}

const MidiMessage MidiMessage::programChange (const int channel,
											  const int programNumber) throw()
{
	jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16

	return MidiMessage (0xc0 | jlimit (0, 15, channel - 1),
						programNumber & 0x7f);
}

bool MidiMessage::isPitchWheel() const throw()
{
	return (data[0] & 0xf0) == 0xe0;
}

int MidiMessage::getPitchWheelValue() const throw()
{
	return data[1] | (data[2] << 7);
}

const MidiMessage MidiMessage::pitchWheel (const int channel,
										   const int position) throw()
{
	jassert (channel > 0 && channel <= 16); // valid channels are numbered 1 to 16
	jassert (((unsigned int) position) <= 0x3fff);

	return MidiMessage (0xe0 | jlimit (0, 15, channel - 1),
						position & 127,
						(position >> 7) & 127);
}

bool MidiMessage::isController() const throw()
{
	return (data[0] & 0xf0) == 0xb0;
}

int MidiMessage::getControllerNumber() const throw()
{
	jassert (isController());

	return data[1];
}

int MidiMessage::getControllerValue() const throw()
{
	jassert (isController());

	return data[2];
}

const MidiMessage MidiMessage::controllerEvent (const int channel,
												const int controllerType,
												const int value) throw()
{
	// the channel must be between 1 and 16 inclusive
	jassert (channel > 0 && channel <= 16);

	return MidiMessage (0xb0 | jlimit (0, 15, channel - 1),
						controllerType & 127,
						value & 127);
}

const MidiMessage MidiMessage::noteOn (const int channel,
									   const int noteNumber,
									   const float velocity) throw()
{
	return noteOn (channel, noteNumber, (uint8)(velocity * 127.0f));
}

const MidiMessage MidiMessage::noteOn (const int channel,
									   const int noteNumber,
									   const uint8 velocity) throw()
{
	jassert (channel > 0 && channel <= 16);
	jassert (((unsigned int) noteNumber) <= 127);

	return MidiMessage (0x90 | jlimit (0, 15, channel - 1),
						noteNumber & 127,
						jlimit (0, 127, roundToInt (velocity)));
}

const MidiMessage MidiMessage::noteOff (const int channel,
										const int noteNumber) throw()
{
	jassert (channel > 0 && channel <= 16);
	jassert (((unsigned int) noteNumber) <= 127);

	return MidiMessage (0x80 | jlimit (0, 15, channel - 1), noteNumber & 127, 0);
}

const MidiMessage MidiMessage::allNotesOff (const int channel) throw()
{
	jassert (channel > 0 && channel <= 16);

	return controllerEvent (channel, 123, 0);
}

bool MidiMessage::isAllNotesOff() const throw()
{
	return (data[0] & 0xf0) == 0xb0
			&& data[1] == 123;
}

const MidiMessage MidiMessage::allSoundOff (const int channel) throw()
{
	return controllerEvent (channel, 120, 0);
}

bool MidiMessage::isAllSoundOff() const throw()
{
	return (data[0] & 0xf0) == 0xb0
			 && data[1] == 120;
}

const MidiMessage MidiMessage::allControllersOff (const int channel) throw()
{
	return controllerEvent (channel, 121, 0);
}

const MidiMessage MidiMessage::masterVolume (const float volume)
{
	const int vol = jlimit (0, 0x3fff, roundToInt (volume * 0x4000));

	uint8 buf[8];
	buf[0] = 0xf0;
	buf[1] = 0x7f;
	buf[2] = 0x7f;
	buf[3] = 0x04;
	buf[4] = 0x01;
	buf[5] = (uint8) (vol & 0x7f);
	buf[6] = (uint8) (vol >> 7);
	buf[7] = 0xf7;

	return MidiMessage (buf, 8);
}

bool MidiMessage::isSysEx() const throw()
{
	return *data == 0xf0;
}

const MidiMessage MidiMessage::createSysExMessage (const uint8* sysexData, const int dataSize)
{
	MemoryBlock mm (dataSize + 2);
	uint8* const m = static_cast <uint8*> (mm.getData());

	m[0] = 0xf0;
	memcpy (m + 1, sysexData, dataSize);
	m[dataSize + 1] = 0xf7;

	return MidiMessage (m, dataSize + 2);
}

const uint8* MidiMessage::getSysExData() const throw()
{
	return (isSysEx()) ? getRawData() + 1 : 0;
}

int MidiMessage::getSysExDataSize() const throw()
{
	return (isSysEx()) ? size - 2 : 0;
}

bool MidiMessage::isMetaEvent() const throw()
{
	return *data == 0xff;
}

bool MidiMessage::isActiveSense() const throw()
{
	return *data == 0xfe;
}

int MidiMessage::getMetaEventType() const throw()
{
	if (*data != 0xff)
		return -1;
	else
		return data[1];
}

int MidiMessage::getMetaEventLength() const throw()
{
	if (*data == 0xff)
	{
		int n;
		return jmin (size - 2, readVariableLengthVal (data + 2, n));
	}

	return 0;
}

const uint8* MidiMessage::getMetaEventData() const throw()
{
	int n;
	const uint8* d = data + 2;
	readVariableLengthVal (d, n);
	return d + n;
}

bool MidiMessage::isTrackMetaEvent() const throw()
{
	return getMetaEventType() == 0;
}

bool MidiMessage::isEndOfTrackMetaEvent() const throw()
{
	return getMetaEventType() == 47;
}

bool MidiMessage::isTextMetaEvent() const throw()
{
	const int t = getMetaEventType();

	return t > 0 && t < 16;
}

const String MidiMessage::getTextFromTextMetaEvent() const
{
	return String (reinterpret_cast <const char*> (getMetaEventData()), getMetaEventLength());
}

bool MidiMessage::isTrackNameEvent() const throw()
{
	return (data[1] == 3)
			&& (*data == 0xff);
}

bool MidiMessage::isTempoMetaEvent() const throw()
{
	return (data[1] == 81)
			&& (*data == 0xff);
}

bool MidiMessage::isMidiChannelMetaEvent() const throw()
{
	return (data[1] == 0x20)
			&& (*data == 0xff)
			&& (data[2] == 1);
}

int MidiMessage::getMidiChannelMetaEventChannel() const throw()
{
	return data[3] + 1;
}

double MidiMessage::getTempoSecondsPerQuarterNote() const throw()
{
	if (! isTempoMetaEvent())
		return 0.0;

	const uint8* const d = getMetaEventData();

	return (((unsigned int) d[0] << 16)
			 | ((unsigned int) d[1] << 8)
			 | d[2])
			/ 1000000.0;
}

double MidiMessage::getTempoMetaEventTickLength (const short timeFormat) const throw()
{
	if (timeFormat > 0)
	{
		if (! isTempoMetaEvent())
			return 0.5 / timeFormat;

		return getTempoSecondsPerQuarterNote() / timeFormat;
	}
	else
	{
		const int frameCode = (-timeFormat) >> 8;
		double framesPerSecond;

		switch (frameCode)
		{
			case 24: framesPerSecond = 24.0;   break;
			case 25: framesPerSecond = 25.0;   break;
			case 29: framesPerSecond = 29.97;  break;
			case 30: framesPerSecond = 30.0;   break;
			default: framesPerSecond = 30.0;   break;
		}

		return (1.0 / framesPerSecond) / (timeFormat & 0xff);
	}
}

const MidiMessage MidiMessage::tempoMetaEvent (int microsecondsPerQuarterNote) throw()
{
	uint8 d[8];
	d[0] = 0xff;
	d[1] = 81;
	d[2] = 3;
	d[3] = (uint8) (microsecondsPerQuarterNote >> 16);
	d[4] = (uint8) ((microsecondsPerQuarterNote >> 8) & 0xff);
	d[5] = (uint8) (microsecondsPerQuarterNote & 0xff);

	return MidiMessage (d, 6, 0.0);
}

bool MidiMessage::isTimeSignatureMetaEvent() const throw()
{
	return (data[1] == 0x58)
			 && (*data == (uint8) 0xff);
}

void MidiMessage::getTimeSignatureInfo (int& numerator, int& denominator) const throw()
{
	if (isTimeSignatureMetaEvent())
	{
		const uint8* const d = getMetaEventData();
		numerator = d[0];
		denominator = 1 << d[1];
	}
	else
	{
		numerator = 4;
		denominator = 4;
	}
}

const MidiMessage MidiMessage::timeSignatureMetaEvent (const int numerator, const int denominator)
{
	uint8 d[8];
	d[0] = 0xff;
	d[1] = 0x58;
	d[2] = 0x04;
	d[3] = (uint8) numerator;

	int n = 1;
	int powerOfTwo = 0;

	while (n < denominator)
	{
		n <<= 1;
		++powerOfTwo;
	}

	d[4] = (uint8) powerOfTwo;
	d[5] = 0x01;
	d[6] = 96;

	return MidiMessage (d, 7, 0.0);
}

const MidiMessage MidiMessage::midiChannelMetaEvent (const int channel) throw()
{
	uint8 d[8];
	d[0] = 0xff;
	d[1] = 0x20;
	d[2] = 0x01;
	d[3] = (uint8) jlimit (0, 0xff, channel - 1);

	return MidiMessage (d, 4, 0.0);
}

bool MidiMessage::isKeySignatureMetaEvent() const throw()
{
	return getMetaEventType() == 89;
}

int MidiMessage::getKeySignatureNumberOfSharpsOrFlats() const throw()
{
	return (int) *getMetaEventData();
}

const MidiMessage MidiMessage::endOfTrack() throw()
{
	return MidiMessage (0xff, 0x2f, 0, 0.0);
}

bool MidiMessage::isSongPositionPointer() const throw()
{
	return *data == 0xf2;
}

int MidiMessage::getSongPositionPointerMidiBeat() const throw()
{
	return data[1] | (data[2] << 7);
}

const MidiMessage MidiMessage::songPositionPointer (const int positionInMidiBeats) throw()
{
	return MidiMessage (0xf2,
						positionInMidiBeats & 127,
						(positionInMidiBeats >> 7) & 127);
}

bool MidiMessage::isMidiStart() const throw()
{
	return *data == 0xfa;
}

const MidiMessage MidiMessage::midiStart() throw()
{
	return MidiMessage (0xfa);
}

bool MidiMessage::isMidiContinue() const throw()
{
	return *data == 0xfb;
}

const MidiMessage MidiMessage::midiContinue() throw()
{
	return MidiMessage (0xfb);
}

bool MidiMessage::isMidiStop() const throw()
{
	return *data == 0xfc;
}

const MidiMessage MidiMessage::midiStop() throw()
{
	return MidiMessage (0xfc);
}

bool MidiMessage::isMidiClock() const throw()
{
	return *data == 0xf8;
}

const MidiMessage MidiMessage::midiClock() throw()
{
	return MidiMessage (0xf8);
}

bool MidiMessage::isQuarterFrame() const throw()
{
	return *data == 0xf1;
}

int MidiMessage::getQuarterFrameSequenceNumber() const throw()
{
	return ((int) data[1]) >> 4;
}

int MidiMessage::getQuarterFrameValue() const throw()
{
	return ((int) data[1]) & 0x0f;
}

const MidiMessage MidiMessage::quarterFrame (const int sequenceNumber,
											 const int value) throw()
{
	return MidiMessage (0xf1, (sequenceNumber << 4) | value);
}

bool MidiMessage::isFullFrame() const throw()
{
	return data[0] == 0xf0
			&& data[1] == 0x7f
			&& size >= 10
			&& data[3] == 0x01
			&& data[4] == 0x01;
}

void MidiMessage::getFullFrameParameters (int& hours,
										  int& minutes,
										  int& seconds,
										  int& frames,
										  MidiMessage::SmpteTimecodeType& timecodeType) const throw()
{
	jassert (isFullFrame());

	timecodeType = (SmpteTimecodeType) (data[5] >> 5);
	hours   = data[5] & 0x1f;
	minutes = data[6];
	seconds = data[7];
	frames  = data[8];
}

const MidiMessage MidiMessage::fullFrame (const int hours,
										  const int minutes,
										  const int seconds,
										  const int frames,
										  MidiMessage::SmpteTimecodeType timecodeType)
{
	uint8 d[10];
	d[0] = 0xf0;
	d[1] = 0x7f;
	d[2] = 0x7f;
	d[3] = 0x01;
	d[4] = 0x01;
	d[5] = (uint8) ((hours & 0x01f) | (timecodeType << 5));
	d[6] = (uint8) minutes;
	d[7] = (uint8) seconds;
	d[8] = (uint8) frames;
	d[9] = 0xf7;

	return MidiMessage (d, 10, 0.0);
}

bool MidiMessage::isMidiMachineControlMessage() const throw()
{
	return data[0] == 0xf0
		&& data[1] == 0x7f
		&& data[3] == 0x06
		&& size > 5;
}

MidiMessage::MidiMachineControlCommand MidiMessage::getMidiMachineControlCommand() const throw()
{
	jassert (isMidiMachineControlMessage());

	return (MidiMachineControlCommand) data[4];
}

const MidiMessage MidiMessage::midiMachineControlCommand (MidiMessage::MidiMachineControlCommand command)
{
	uint8 d[6];
	d[0] = 0xf0;
	d[1] = 0x7f;
	d[2] = 0x00;
	d[3] = 0x06;
	d[4] = (uint8) command;
	d[5] = 0xf7;

	return MidiMessage (d, 6, 0.0);
}

bool MidiMessage::isMidiMachineControlGoto (int& hours,
											int& minutes,
											int& seconds,
											int& frames) const throw()
{
	if (size >= 12
		 && data[0] == 0xf0
		 && data[1] == 0x7f
		 && data[3] == 0x06
		 && data[4] == 0x44
		 && data[5] == 0x06
		 && data[6] == 0x01)
	{
		hours = data[7] % 24;   // (that some machines send out hours > 24)
		minutes = data[8];
		seconds = data[9];
		frames = data[10];

		return true;
	}

	return false;
}

const MidiMessage MidiMessage::midiMachineControlGoto (int hours,
													   int minutes,
													   int seconds,
													   int frames)
{
	uint8 d[12];
	d[0] = 0xf0;
	d[1] = 0x7f;
	d[2] = 0x00;
	d[3] = 0x06;
	d[4] = 0x44;
	d[5] = 0x06;
	d[6] = 0x01;
	d[7] = (uint8) hours;
	d[8] = (uint8) minutes;
	d[9] = (uint8) seconds;
	d[10] = (uint8) frames;
	d[11] = 0xf7;

	return MidiMessage (d, 12, 0.0);
}

const String MidiMessage::getMidiNoteName (int note,
										   bool useSharps,
										   bool includeOctaveNumber,
										   int octaveNumForMiddleC) throw()
{
	static const char* const sharpNoteNames[] = { "C", "C#", "D", "D#", "E",
												  "F", "F#", "G", "G#", "A",
												  "A#", "B" };

	static const char* const flatNoteNames[]  = { "C", "Db", "D", "Eb", "E",
												  "F", "Gb", "G", "Ab", "A",
												  "Bb", "B" };

	if (((unsigned int) note) < 128)
	{
		const String s ((useSharps) ? sharpNoteNames [note % 12]
									: flatNoteNames [note % 12]);

		if (includeOctaveNumber)
			return s + String (note / 12 + (octaveNumForMiddleC - 5));
		else
			return s;
	}

	return String::empty;
}

const double MidiMessage::getMidiNoteInHertz (int noteNumber) throw()
{
	noteNumber -= 12 * 6 + 9; // now 0 = A440
	return 440.0 * pow (2.0, noteNumber / 12.0);
}

const String MidiMessage::getGMInstrumentName (int n) throw()
{
	const char *names[] =
	{
		"Acoustic Grand Piano", "Bright Acoustic Piano", "Electric Grand Piano", "Honky-tonk Piano",
		"Electric Piano 1", "Electric Piano 2", "Harpsichord", "Clavinet", "Celesta", "Glockenspiel",
		"Music Box", "Vibraphone", "Marimba", "Xylophone", "Tubular Bells", "Dulcimer", "Drawbar Organ",
		"Percussive Organ", "Rock Organ", "Church Organ", "Reed Organ", "Accordion", "Harmonica",
		"Tango Accordion", "Acoustic Guitar (nylon)", "Acoustic Guitar (steel)", "Electric Guitar (jazz)",
		"Electric Guitar (clean)", "Electric Guitar (mute)", "Overdriven Guitar", "Distortion Guitar",
		"Guitar Harmonics", "Acoustic Bass", "Electric Bass (finger)", "Electric Bass (pick)",
		"Fretless Bass", "Slap Bass 1", "Slap Bass 2", "Synth Bass 1", "Synth Bass 2", "Violin",
		"Viola", "Cello", "Contrabass", "Tremolo Strings", "Pizzicato Strings", "Orchestral Harp",
		"Timpani", "String Ensemble 1", "String Ensemble 2", "SynthStrings 1", "SynthStrings 2",
		"Choir Aahs", "Voice Oohs", "Synth Voice", "Orchestra Hit", "Trumpet", "Trombone", "Tuba",
		"Muted Trumpet", "French Horn", "Brass Section", "SynthBrass 1", "SynthBrass 2", "Soprano Sax",
		"Alto Sax", "Tenor Sax", "Baritone Sax", "Oboe", "English Horn", "Bassoon", "Clarinet",
		"Piccolo", "Flute", "Recorder", "Pan Flute", "Blown Bottle", "Shakuhachi", "Whistle",
		"Ocarina", "Lead 1 (square)", "Lead 2 (sawtooth)", "Lead 3 (calliope)", "Lead 4 (chiff)",
		"Lead 5 (charang)", "Lead 6 (voice)", "Lead 7 (fifths)", "Lead 8 (bass+lead)", "Pad 1 (new age)",
		"Pad 2 (warm)", "Pad 3 (polysynth)", "Pad 4 (choir)", "Pad 5 (bowed)", "Pad 6 (metallic)",
		"Pad 7 (halo)", "Pad 8 (sweep)", "FX 1 (rain)", "FX 2 (soundtrack)", "FX 3 (crystal)",
		"FX 4 (atmosphere)", "FX 5 (brightness)", "FX 6 (goblins)", "FX 7 (echoes)", "FX 8 (sci-fi)",
		"Sitar", "Banjo", "Shamisen", "Koto", "Kalimba", "Bag pipe", "Fiddle", "Shanai", "Tinkle Bell",
		"Agogo", "Steel Drums", "Woodblock", "Taiko Drum", "Melodic Tom", "Synth Drum", "Reverse Cymbal",
		"Guitar Fret Noise", "Breath Noise", "Seashore", "Bird Tweet", "Telephone Ring", "Helicopter",
		"Applause", "Gunshot"
	};

	return (((unsigned int) n) < 128) ? names[n]
									  : (const char*)0;
}

const String MidiMessage::getGMInstrumentBankName (int n) throw()
{
	const char* names[] =
	{
		"Piano", "Chromatic Percussion", "Organ", "Guitar",
		"Bass", "Strings", "Ensemble", "Brass",
		"Reed", "Pipe", "Synth Lead", "Synth Pad",
		"Synth Effects", "Ethnic", "Percussive", "Sound Effects"
	};

	return (((unsigned int) n) <= 15) ? names[n]
									  : (const char*)0;
}

const String MidiMessage::getRhythmInstrumentName (int n) throw()
{
	const char* names[] =
	{
		"Acoustic Bass Drum", "Bass Drum 1", "Side Stick", "Acoustic Snare",
		"Hand Clap", "Electric Snare", "Low Floor Tom", "Closed Hi-Hat", "High Floor Tom",
		"Pedal Hi-Hat", "Low Tom", "Open Hi-Hat", "Low-Mid Tom", "Hi-Mid Tom", "Crash Cymbal 1",
		"High Tom", "Ride Cymbal 1", "Chinese Cymbal", "Ride Bell", "Tambourine", "Splash Cymbal",
		"Cowbell", "Crash Cymbal 2", "Vibraslap", "Ride Cymbal 2", "Hi Bongo", "Low Bongo",
		"Mute Hi Conga", "Open Hi Conga", "Low Conga", "High Timbale", "Low Timbale", "High Agogo",
		"Low Agogo", "Cabasa", "Maracas", "Short Whistle", "Long Whistle", "Short Guiro",
		"Long Guiro", "Claves", "Hi Wood Block", "Low Wood Block", "Mute Cuica", "Open Cuica",
		"Mute Triangle", "Open Triangle"
	};

	return (n >= 35 && n <= 81) ? names [n - 35]
								: (const char*)0;
}

const String MidiMessage::getControllerName (int n) throw()
{
	const char* names[] =
	{
		"Bank Select", "Modulation Wheel (coarse)", "Breath controller (coarse)",
		0, "Foot Pedal (coarse)", "Portamento Time (coarse)",
		"Data Entry (coarse)", "Volume (coarse)", "Balance (coarse)",
		0, "Pan position (coarse)", "Expression (coarse)", "Effect Control 1 (coarse)",
		"Effect Control 2 (coarse)", 0, 0, "General Purpose Slider 1", "General Purpose Slider 2",
		"General Purpose Slider 3", "General Purpose Slider 4", 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, "Bank Select (fine)", "Modulation Wheel (fine)", "Breath controller (fine)",
		0, "Foot Pedal (fine)", "Portamento Time (fine)", "Data Entry (fine)", "Volume (fine)",
		"Balance (fine)", 0, "Pan position (fine)", "Expression (fine)", "Effect Control 1 (fine)",
		"Effect Control 2 (fine)", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		"Hold Pedal (on/off)", "Portamento (on/off)", "Sustenuto Pedal (on/off)", "Soft Pedal (on/off)",
		"Legato Pedal (on/off)", "Hold 2 Pedal (on/off)", "Sound Variation", "Sound Timbre",
		"Sound Release Time", "Sound Attack Time", "Sound Brightness", "Sound Control 6",
		"Sound Control 7", "Sound Control 8", "Sound Control 9", "Sound Control 10",
		"General Purpose Button 1 (on/off)", "General Purpose Button 2 (on/off)",
		"General Purpose Button 3 (on/off)", "General Purpose Button 4 (on/off)",
		0, 0, 0, 0, 0, 0, 0, "Reverb Level", "Tremolo Level",  "Chorus Level", "Celeste Level",
		"Phaser Level", "Data Button increment", "Data Button decrement", "Non-registered Parameter (fine)",
		"Non-registered Parameter (coarse)", "Registered Parameter (fine)", "Registered Parameter (coarse)",
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "All Sound Off", "All Controllers Off",
		"Local Keyboard (on/off)", "All Notes Off", "Omni Mode Off", "Omni Mode On", "Mono Operation",
		"Poly Operation"
	};

	return (((unsigned int) n) < 128) ? names[n]
									  : (const char*)0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MidiMessage.cpp ***/


/*** Start of inlined file: juce_MidiMessageCollector.cpp ***/
BEGIN_JUCE_NAMESPACE

MidiMessageCollector::MidiMessageCollector()
	: lastCallbackTime (0),
	  sampleRate (44100.0001)
{
}

MidiMessageCollector::~MidiMessageCollector()
{
}

void MidiMessageCollector::reset (const double sampleRate_)
{
	jassert (sampleRate_ > 0);

	const ScopedLock sl (midiCallbackLock);
	sampleRate = sampleRate_;
	incomingMessages.clear();
	lastCallbackTime = Time::getMillisecondCounterHiRes();
}

void MidiMessageCollector::addMessageToQueue (const MidiMessage& message)
{
	// you need to call reset() to set the correct sample rate before using this object
	jassert (sampleRate != 44100.0001);

	// the messages that come in here need to be time-stamped correctly - see MidiInput
	// for details of what the number should be.
	jassert (message.getTimeStamp() != 0);

	const ScopedLock sl (midiCallbackLock);

	const int sampleNumber
		= (int) ((message.getTimeStamp() - 0.001 * lastCallbackTime) * sampleRate);

	incomingMessages.addEvent (message, sampleNumber);

	// if the messages don't get used for over a second, we'd better
	// get rid of any old ones to avoid the queue getting too big
	if (sampleNumber > sampleRate)
		incomingMessages.clear (0, sampleNumber - (int) sampleRate);
}

void MidiMessageCollector::removeNextBlockOfMessages (MidiBuffer& destBuffer,
													  const int numSamples)
{
	// you need to call reset() to set the correct sample rate before using this object
	jassert (sampleRate != 44100.0001);

	const double timeNow = Time::getMillisecondCounterHiRes();
	const double msElapsed = timeNow - lastCallbackTime;

	const ScopedLock sl (midiCallbackLock);
	lastCallbackTime = timeNow;

	if (! incomingMessages.isEmpty())
	{
		int numSourceSamples = jmax (1, roundToInt (msElapsed * 0.001 * sampleRate));

		int startSample = 0;
		int scale = 1 << 16;

		const uint8* midiData;
		int numBytes, samplePosition;

		MidiBuffer::Iterator iter (incomingMessages);

		if (numSourceSamples > numSamples)
		{
			// if our list of events is longer than the buffer we're being
			// asked for, scale them down to squeeze them all in..
			const int maxBlockLengthToUse = numSamples << 5;

			if (numSourceSamples > maxBlockLengthToUse)
			{
				startSample = numSourceSamples - maxBlockLengthToUse;
				numSourceSamples = maxBlockLengthToUse;
				iter.setNextSamplePosition (startSample);
			}

			scale = (numSamples << 10) / numSourceSamples;

			while (iter.getNextEvent (midiData, numBytes, samplePosition))
			{
				samplePosition = ((samplePosition - startSample) * scale) >> 10;

				destBuffer.addEvent (midiData, numBytes,
									 jlimit (0, numSamples - 1, samplePosition));
			}
		}
		else
		{
			// if our event list is shorter than the number we need, put them
			// towards the end of the buffer
			startSample = numSamples - numSourceSamples;

			while (iter.getNextEvent (midiData, numBytes, samplePosition))
			{
				destBuffer.addEvent (midiData, numBytes,
									 jlimit (0, numSamples - 1, samplePosition + startSample));
			}
		}

		incomingMessages.clear();
	}
}

void MidiMessageCollector::handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
{
	MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
	m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);

	addMessageToQueue (m);
}

void MidiMessageCollector::handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber)
{
	MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber));
	m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);

	addMessageToQueue (m);
}

void MidiMessageCollector::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
{
	addMessageToQueue (message);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MidiMessageCollector.cpp ***/


/*** Start of inlined file: juce_MidiMessageSequence.cpp ***/
BEGIN_JUCE_NAMESPACE

MidiMessageSequence::MidiMessageSequence()
{
}

MidiMessageSequence::MidiMessageSequence (const MidiMessageSequence& other)
{
	list.ensureStorageAllocated (other.list.size());

	for (int i = 0; i < other.list.size(); ++i)
		list.add (new MidiEventHolder (other.list.getUnchecked(i)->message));
}

MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
{
	MidiMessageSequence otherCopy (other);
	swapWith (otherCopy);
	return *this;
}

void MidiMessageSequence::swapWith (MidiMessageSequence& other) throw()
{
	list.swapWithArray (other.list);
}

MidiMessageSequence::~MidiMessageSequence()
{
}

void MidiMessageSequence::clear()
{
	list.clear();
}

int MidiMessageSequence::getNumEvents() const
{
	return list.size();
}

MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (const int index) const
{
	return list [index];
}

double MidiMessageSequence::getTimeOfMatchingKeyUp (const int index) const
{
	const MidiEventHolder* const meh = list [index];

	if (meh != 0 && meh->noteOffObject != 0)
		return meh->noteOffObject->message.getTimeStamp();
	else
		return 0.0;
}

int MidiMessageSequence::getIndexOfMatchingKeyUp (const int index) const
{
	const MidiEventHolder* const meh = list [index];

	return (meh != 0) ? list.indexOf (meh->noteOffObject) : -1;
}

int MidiMessageSequence::getIndexOf (MidiEventHolder* const event) const
{
	return list.indexOf (event);
}

int MidiMessageSequence::getNextIndexAtTime (const double timeStamp) const
{
	const int numEvents = list.size();

	int i;
	for (i = 0; i < numEvents; ++i)
		if (list.getUnchecked(i)->message.getTimeStamp() >= timeStamp)
			break;

	return i;
}

double MidiMessageSequence::getStartTime() const
{
	if (list.size() > 0)
		return list.getUnchecked(0)->message.getTimeStamp();
	else
		return 0;
}

double MidiMessageSequence::getEndTime() const
{
	if (list.size() > 0)
		return list.getLast()->message.getTimeStamp();
	else
		return 0;
}

double MidiMessageSequence::getEventTime (const int index) const
{
	if (((unsigned int) index) < (unsigned int) list.size())
		return list.getUnchecked (index)->message.getTimeStamp();

	return 0.0;
}

void MidiMessageSequence::addEvent (const MidiMessage& newMessage,
									double timeAdjustment)
{
	MidiEventHolder* const newOne = new MidiEventHolder (newMessage);

	timeAdjustment += newMessage.getTimeStamp();
	newOne->message.setTimeStamp (timeAdjustment);

	int i;
	for (i = list.size(); --i >= 0;)
		if (list.getUnchecked(i)->message.getTimeStamp() <= timeAdjustment)
			break;

	list.insert (i + 1, newOne);
}

void MidiMessageSequence::deleteEvent (const int index,
									   const bool deleteMatchingNoteUp)
{
	if (((unsigned int) index) < (unsigned int) list.size())
	{
		if (deleteMatchingNoteUp)
			deleteEvent (getIndexOfMatchingKeyUp (index), false);

		list.remove (index);
	}
}

void MidiMessageSequence::addSequence (const MidiMessageSequence& other,
									   double timeAdjustment,
									   double firstAllowableTime,
									   double endOfAllowableDestTimes)
{
	firstAllowableTime -= timeAdjustment;
	endOfAllowableDestTimes -= timeAdjustment;

	for (int i = 0; i < other.list.size(); ++i)
	{
		const MidiMessage& m = other.list.getUnchecked(i)->message;
		const double t = m.getTimeStamp();

		if (t >= firstAllowableTime && t < endOfAllowableDestTimes)
		{
			MidiEventHolder* const newOne = new MidiEventHolder (m);
			newOne->message.setTimeStamp (timeAdjustment + t);

			list.add (newOne);
		}
	}

	sort();
}

int MidiMessageSequence::compareElements (const MidiMessageSequence::MidiEventHolder* const first,
										  const MidiMessageSequence::MidiEventHolder* const second) throw()
{
	const double diff = first->message.getTimeStamp()
						 - second->message.getTimeStamp();

	return (diff > 0) - (diff < 0);
}

void MidiMessageSequence::sort()
{
	list.sort (*this, true);
}

void MidiMessageSequence::updateMatchedPairs()
{
	for (int i = 0; i < list.size(); ++i)
	{
		const MidiMessage& m1 = list.getUnchecked(i)->message;

		if (m1.isNoteOn())
		{
			list.getUnchecked(i)->noteOffObject = 0;
			const int note = m1.getNoteNumber();
			const int chan = m1.getChannel();
			const int len = list.size();

			for (int j = i + 1; j < len; ++j)
			{
				const MidiMessage& m = list.getUnchecked(j)->message;

				if (m.getNoteNumber() == note && m.getChannel() == chan)
				{
					if (m.isNoteOff())
					{
						list.getUnchecked(i)->noteOffObject = list[j];
						break;
					}
					else if (m.isNoteOn())
					{
						list.insert (j, new MidiEventHolder (MidiMessage::noteOff (chan, note)));
						list.getUnchecked(j)->message.setTimeStamp (m.getTimeStamp());
						list.getUnchecked(i)->noteOffObject = list[j];
						break;
					}
				}
			}
		}
	}
}

void MidiMessageSequence::addTimeToMessages (const double delta)
{
	for (int i = list.size(); --i >= 0;)
		list.getUnchecked (i)->message.setTimeStamp (list.getUnchecked (i)->message.getTimeStamp()
													  + delta);
}

void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
													  MidiMessageSequence& destSequence,
													  const bool alsoIncludeMetaEvents) const
{
	for (int i = 0; i < list.size(); ++i)
	{
		const MidiMessage& mm = list.getUnchecked(i)->message;

		if (mm.isForChannel (channelNumberToExtract)
			 || (alsoIncludeMetaEvents && mm.isMetaEvent()))
		{
			destSequence.addEvent (mm);
		}
	}
}

void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
{
	for (int i = 0; i < list.size(); ++i)
	{
		const MidiMessage& mm = list.getUnchecked(i)->message;

		if (mm.isSysEx())
			destSequence.addEvent (mm);
	}
}

void MidiMessageSequence::deleteMidiChannelMessages (const int channelNumberToRemove)
{
	for (int i = list.size(); --i >= 0;)
		if (list.getUnchecked(i)->message.isForChannel (channelNumberToRemove))
			list.remove(i);
}

void MidiMessageSequence::deleteSysExMessages()
{
	for (int i = list.size(); --i >= 0;)
		if (list.getUnchecked(i)->message.isSysEx())
			list.remove(i);
}

void MidiMessageSequence::createControllerUpdatesForTime (const int channelNumber,
														  const double time,
														  OwnedArray<MidiMessage>& dest)
{
	bool doneProg = false;
	bool donePitchWheel = false;
	Array <int> doneControllers;
	doneControllers.ensureStorageAllocated (32);

	for (int i = list.size(); --i >= 0;)
	{
		const MidiMessage& mm = list.getUnchecked(i)->message;

		if (mm.isForChannel (channelNumber)
			 && mm.getTimeStamp() <= time)
		{
			if (mm.isProgramChange())
			{
				if (! doneProg)
				{
					dest.add (new MidiMessage (mm, 0.0));
					doneProg = true;
				}
			}
			else if (mm.isController())
			{
				if (! doneControllers.contains (mm.getControllerNumber()))
				{
					dest.add (new MidiMessage (mm, 0.0));
					doneControllers.add (mm.getControllerNumber());
				}
			}
			else if (mm.isPitchWheel())
			{
				if (! donePitchWheel)
				{
					dest.add (new MidiMessage (mm, 0.0));
					donePitchWheel = true;
				}
			}
		}
	}
}

MidiMessageSequence::MidiEventHolder::MidiEventHolder (const MidiMessage& message_)
   : message (message_),
	 noteOffObject (0)
{
}

MidiMessageSequence::MidiEventHolder::~MidiEventHolder()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MidiMessageSequence.cpp ***/


/*** Start of inlined file: juce_AudioPluginFormat.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioPluginFormat::AudioPluginFormat() throw()
{
}

AudioPluginFormat::~AudioPluginFormat()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioPluginFormat.cpp ***/


/*** Start of inlined file: juce_AudioPluginFormatManager.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioPluginFormatManager::AudioPluginFormatManager() throw()
{
}

AudioPluginFormatManager::~AudioPluginFormatManager() throw()
{
	clearSingletonInstance();
}

juce_ImplementSingleton_SingleThreaded (AudioPluginFormatManager);

void AudioPluginFormatManager::addDefaultFormats()
{
#ifdef JUCE_DEBUG
	// you should only call this method once!
	for (int i = formats.size(); --i >= 0;)
	{
  #if JUCE_PLUGINHOST_VST && ! (JUCE_MAC && JUCE_64BIT)
		jassert (dynamic_cast <VSTPluginFormat*> (formats[i]) == 0);
  #endif

  #if JUCE_PLUGINHOST_AU && JUCE_MAC
		jassert (dynamic_cast <AudioUnitPluginFormat*> (formats[i]) == 0);
  #endif

  #if JUCE_PLUGINHOST_DX && JUCE_WIN32
		jassert (dynamic_cast <DirectXPluginFormat*> (formats[i]) == 0);
  #endif

  #if JUCE_PLUGINHOST_LADSPA && JUCE_LINUX
		jassert (dynamic_cast <LADSPAPluginFormat*> (formats[i]) == 0);
  #endif
	}
#endif

#if JUCE_PLUGINHOST_AU && JUCE_MAC
	formats.add (new AudioUnitPluginFormat());
#endif

#if JUCE_PLUGINHOST_VST && ! (JUCE_MAC && JUCE_64BIT)
	formats.add (new VSTPluginFormat());
#endif

#if JUCE_PLUGINHOST_DX && JUCE_WIN32
	formats.add (new DirectXPluginFormat());
#endif

#if JUCE_PLUGINHOST_LADSPA && JUCE_LINUX
	formats.add (new LADSPAPluginFormat());
#endif
}

int AudioPluginFormatManager::getNumFormats() throw()
{
	return formats.size();
}

AudioPluginFormat* AudioPluginFormatManager::getFormat (const int index) throw()
{
	return formats [index];
}

void AudioPluginFormatManager::addFormat (AudioPluginFormat* const format) throw()
{
	formats.add (format);
}

AudioPluginInstance* AudioPluginFormatManager::createPluginInstance (const PluginDescription& description,
																	 String& errorMessage) const
{
	AudioPluginInstance* result = 0;

	for (int i = 0; i < formats.size(); ++i)
	{
		result = formats.getUnchecked(i)->createInstanceFromDescription (description);

		if (result != 0)
			break;
	}

	if (result == 0)
	{
		if (! doesPluginStillExist (description))
			errorMessage = TRANS ("This plug-in file no longer exists");
		else
			errorMessage = TRANS ("This plug-in failed to load correctly");
	}

	return result;
}

bool AudioPluginFormatManager::doesPluginStillExist (const PluginDescription& description) const
{
	for (int i = 0; i < formats.size(); ++i)
		if (formats.getUnchecked(i)->getName() == description.pluginFormatName)
			return formats.getUnchecked(i)->doesPluginStillExist (description);

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioPluginFormatManager.cpp ***/


/*** Start of inlined file: juce_AudioPluginInstance.cpp ***/
#define JUCE_PLUGIN_HOST 1

BEGIN_JUCE_NAMESPACE

AudioPluginInstance::AudioPluginInstance()
{
}

AudioPluginInstance::~AudioPluginInstance()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioPluginInstance.cpp ***/


/*** Start of inlined file: juce_KnownPluginList.cpp ***/
BEGIN_JUCE_NAMESPACE

KnownPluginList::KnownPluginList()
{
}

KnownPluginList::~KnownPluginList()
{
}

void KnownPluginList::clear()
{
	if (types.size() > 0)
	{
		types.clear();
		sendChangeMessage (this);
	}
}

PluginDescription* KnownPluginList::getTypeForFile (const String& fileOrIdentifier) const throw()
{
	for (int i = 0; i < types.size(); ++i)
		if (types.getUnchecked(i)->fileOrIdentifier == fileOrIdentifier)
			return types.getUnchecked(i);

	return 0;
}

PluginDescription* KnownPluginList::getTypeForIdentifierString (const String& identifierString) const throw()
{
	for (int i = 0; i < types.size(); ++i)
		if (types.getUnchecked(i)->createIdentifierString() == identifierString)
			return types.getUnchecked(i);

	return 0;
}

bool KnownPluginList::addType (const PluginDescription& type)
{
	for (int i = types.size(); --i >= 0;)
	{
		if (types.getUnchecked(i)->isDuplicateOf (type))
		{
			// strange - found a duplicate plugin with different info..
			jassert (types.getUnchecked(i)->name == type.name);
			jassert (types.getUnchecked(i)->isInstrument == type.isInstrument);

			*types.getUnchecked(i) = type;
			return false;
		}
	}

	types.add (new PluginDescription (type));
	sendChangeMessage (this);
	return true;
}

void KnownPluginList::removeType (const int index) throw()
{
	types.remove (index);
	sendChangeMessage (this);
}

static Time getFileModTime (const String& fileOrIdentifier) throw()
{
	if (fileOrIdentifier.startsWithChar ('/')
		|| fileOrIdentifier[1] == ':')
	{
		return File (fileOrIdentifier).getLastModificationTime();
	}

	return Time (0);
}

static bool timesAreDifferent (const Time& t1, const Time& t2) throw()
{
	return t1 != t2 || t1 == Time (0);
}

bool KnownPluginList::isListingUpToDate (const String& fileOrIdentifier) const throw()
{
	if (getTypeForFile (fileOrIdentifier) == 0)
		return false;

	for (int i = types.size(); --i >= 0;)
	{
		const PluginDescription* const d = types.getUnchecked(i);

		if (d->fileOrIdentifier == fileOrIdentifier
			 && timesAreDifferent (d->lastFileModTime, getFileModTime (fileOrIdentifier)))
		{
			return false;
		}
	}

	return true;
}

bool KnownPluginList::scanAndAddFile (const String& fileOrIdentifier,
									  const bool dontRescanIfAlreadyInList,
									  OwnedArray <PluginDescription>& typesFound,
									  AudioPluginFormat& format)
{
	bool addedOne = false;

	if (dontRescanIfAlreadyInList
		 && getTypeForFile (fileOrIdentifier) != 0)
	{
		bool needsRescanning = false;

		for (int i = types.size(); --i >= 0;)
		{
			const PluginDescription* const d = types.getUnchecked(i);

			if (d->fileOrIdentifier == fileOrIdentifier)
			{
				if (timesAreDifferent (d->lastFileModTime, getFileModTime (fileOrIdentifier)))
					needsRescanning = true;
				else
					typesFound.add (new PluginDescription (*d));
			}
		}

		if (! needsRescanning)
			return false;
	}

	OwnedArray <PluginDescription> found;
	format.findAllTypesForFile (found, fileOrIdentifier);

	for (int i = 0; i < found.size(); ++i)
	{
		PluginDescription* const desc = found.getUnchecked(i);
		jassert (desc != 0);

		if (addType (*desc))
			addedOne = true;

		typesFound.add (new PluginDescription (*desc));
	}

	return addedOne;
}

void KnownPluginList::scanAndAddDragAndDroppedFiles (const StringArray& files,
													 OwnedArray <PluginDescription>& typesFound)
{
	for (int i = 0; i < files.size(); ++i)
	{
		bool loaded = false;

		for (int j = 0; j < AudioPluginFormatManager::getInstance()->getNumFormats(); ++j)
		{
			AudioPluginFormat* const format = AudioPluginFormatManager::getInstance()->getFormat (j);

			if (scanAndAddFile (files[i], true, typesFound, *format))
				loaded = true;
		}

		if (! loaded)
		{
			const File f (files[i]);

			if (f.isDirectory())
			{
				StringArray s;

				{
					Array<File> subFiles;
					f.findChildFiles (subFiles, File::findFilesAndDirectories, false);

					for (int j = 0; j < subFiles.size(); ++j)
						s.add (subFiles.getReference(j).getFullPathName());
				}

				scanAndAddDragAndDroppedFiles (s, typesFound);
			}
		}
	}
}

class PluginSorter
{
public:
	KnownPluginList::SortMethod method;

	PluginSorter() throw() {}

	int compareElements (const PluginDescription* const first,
						 const PluginDescription* const second) const throw()
	{
		int diff = 0;

		if (method == KnownPluginList::sortByCategory)
			diff = first->category.compareLexicographically (second->category);
		else if (method == KnownPluginList::sortByManufacturer)
			diff = first->manufacturerName.compareLexicographically (second->manufacturerName);
		else if (method == KnownPluginList::sortByFileSystemLocation)
			diff = first->fileOrIdentifier.replaceCharacter ('\\', '/')
										  .upToLastOccurrenceOf ("/", false, false)
										  .compare (second->fileOrIdentifier.replaceCharacter ('\\', '/')
																			.upToLastOccurrenceOf ("/", false, false));

		if (diff == 0)
			diff = first->name.compareLexicographically (second->name);

		return diff;
	}
};

void KnownPluginList::sort (const SortMethod method)
{
	if (method != defaultOrder)
	{
		PluginSorter sorter;
		sorter.method = method;
		types.sort (sorter, true);

		sendChangeMessage (this);
	}
}

XmlElement* KnownPluginList::createXml() const
{
	XmlElement* const e = new XmlElement ("KNOWNPLUGINS");

	for (int i = 0; i < types.size(); ++i)
		e->addChildElement (types.getUnchecked(i)->createXml());

	return e;
}

void KnownPluginList::recreateFromXml (const XmlElement& xml)
{
	clear();

	if (xml.hasTagName ("KNOWNPLUGINS"))
	{
		forEachXmlChildElement (xml, e)
		{
			PluginDescription info;

			if (info.loadFromXml (*e))
				addType (info);
		}
	}
}

const int menuIdBase = 0x324503f4;

// This is used to turn a bunch of paths into a nested menu structure.
struct PluginFilesystemTree
{
private:
	String folder;
	OwnedArray <PluginFilesystemTree> subFolders;
	Array <PluginDescription*> plugins;

	void addPlugin (PluginDescription* const pd, const String& path)
	{
		if (path.isEmpty())
		{
			plugins.add (pd);
		}
		else
		{
			const String firstSubFolder (path.upToFirstOccurrenceOf ("/", false, false));
			const String remainingPath (path.fromFirstOccurrenceOf ("/", false, false));

			for (int i = subFolders.size(); --i >= 0;)
			{
				if (subFolders.getUnchecked(i)->folder.equalsIgnoreCase (firstSubFolder))
				{
					subFolders.getUnchecked(i)->addPlugin (pd, remainingPath);
					return;
				}
			}

			PluginFilesystemTree* const newFolder = new PluginFilesystemTree();
			newFolder->folder = firstSubFolder;
			subFolders.add (newFolder);

			newFolder->addPlugin (pd, remainingPath);
		}
	}

	// removes any deeply nested folders that don't contain any actual plugins
	void optimise()
	{
		for (int i = subFolders.size(); --i >= 0;)
		{
			PluginFilesystemTree* const sub = subFolders.getUnchecked(i);

			sub->optimise();

			if (sub->plugins.size() == 0)
			{
				for (int j = 0; j < sub->subFolders.size(); ++j)
					subFolders.add (sub->subFolders.getUnchecked(j));

				sub->subFolders.clear (false);
				subFolders.remove (i);
			}
		}
	}

public:
	void buildTree (const Array <PluginDescription*>& allPlugins)
	{
		for (int i = 0; i < allPlugins.size(); ++i)
		{
			String path (allPlugins.getUnchecked(i)
							->fileOrIdentifier.replaceCharacter ('\\', '/')
											  .upToLastOccurrenceOf ("/", false, false));

			if (path.substring (1, 2) == ":")
				path = path.substring (2);

			addPlugin (allPlugins.getUnchecked(i), path);
		}

		optimise();
	}

	void addToMenu (PopupMenu& m, const OwnedArray <PluginDescription>& allPlugins) const
	{
		int i;
		for (i = 0; i < subFolders.size(); ++i)
		{
			const PluginFilesystemTree* const sub = subFolders.getUnchecked(i);

			PopupMenu subMenu;
			sub->addToMenu (subMenu, allPlugins);

#if JUCE_MAC
			// avoid the special AU formatting nonsense on Mac..
			m.addSubMenu (sub->folder.fromFirstOccurrenceOf (":", false, false), subMenu);
#else
			m.addSubMenu (sub->folder, subMenu);
#endif
		}

		for (i = 0; i < plugins.size(); ++i)
		{
			PluginDescription* const plugin = plugins.getUnchecked(i);

			m.addItem (allPlugins.indexOf (plugin) + menuIdBase,
					   plugin->name, true, false);
		}
	}
};

void KnownPluginList::addToMenu (PopupMenu& menu, const SortMethod sortMethod) const
{
	Array <PluginDescription*> sorted;

	{
		PluginSorter sorter;
		sorter.method = sortMethod;

		for (int i = 0; i < types.size(); ++i)
			sorted.addSorted (sorter, types.getUnchecked(i));
	}

	if (sortMethod == sortByCategory
		 || sortMethod == sortByManufacturer)
	{
		String lastSubMenuName;
		PopupMenu sub;

		for (int i = 0; i < sorted.size(); ++i)
		{
			const PluginDescription* const pd = sorted.getUnchecked(i);
			String thisSubMenuName (sortMethod == sortByCategory ? pd->category
																 : pd->manufacturerName);

			if (! thisSubMenuName.containsNonWhitespaceChars())
				thisSubMenuName = "Other";

			if (thisSubMenuName != lastSubMenuName)
			{
				if (sub.getNumItems() > 0)
				{
					menu.addSubMenu (lastSubMenuName, sub);
					sub.clear();
				}

				lastSubMenuName = thisSubMenuName;
			}

			sub.addItem (types.indexOf (pd) + menuIdBase, pd->name, true, false);
		}

		if (sub.getNumItems() > 0)
			menu.addSubMenu (lastSubMenuName, sub);
	}
	else if (sortMethod == sortByFileSystemLocation)
	{
		PluginFilesystemTree root;
		root.buildTree (sorted);
		root.addToMenu (menu, types);
	}
	else
	{
		for (int i = 0; i < sorted.size(); ++i)
		{
			const PluginDescription* const pd = sorted.getUnchecked(i);
			menu.addItem (types.indexOf (pd) + menuIdBase, pd->name, true, false);
		}
	}
}

int KnownPluginList::getIndexChosenByMenu (const int menuResultCode) const
{
	const int i = menuResultCode - menuIdBase;

	return (((unsigned int) i) < (unsigned int) types.size()) ? i : -1;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_KnownPluginList.cpp ***/


/*** Start of inlined file: juce_PluginDescription.cpp ***/
BEGIN_JUCE_NAMESPACE

PluginDescription::PluginDescription() throw()
	: uid (0),
	  isInstrument (false),
	  numInputChannels (0),
	  numOutputChannels (0)
{
}

PluginDescription::~PluginDescription() throw()
{
}

PluginDescription::PluginDescription (const PluginDescription& other) throw()
	: name (other.name),
	  pluginFormatName (other.pluginFormatName),
	  category (other.category),
	  manufacturerName (other.manufacturerName),
	  version (other.version),
	  fileOrIdentifier (other.fileOrIdentifier),
	  lastFileModTime (other.lastFileModTime),
	  uid (other.uid),
	  isInstrument (other.isInstrument),
	  numInputChannels (other.numInputChannels),
	  numOutputChannels (other.numOutputChannels)
{
}

PluginDescription& PluginDescription::operator= (const PluginDescription& other) throw()
{
	name = other.name;
	pluginFormatName = other.pluginFormatName;
	category = other.category;
	manufacturerName = other.manufacturerName;
	version = other.version;
	fileOrIdentifier = other.fileOrIdentifier;
	uid = other.uid;
	isInstrument = other.isInstrument;
	lastFileModTime = other.lastFileModTime;
	numInputChannels = other.numInputChannels;
	numOutputChannels = other.numOutputChannels;

	return *this;
}

bool PluginDescription::isDuplicateOf (const PluginDescription& other) const
{
	return fileOrIdentifier == other.fileOrIdentifier
			&& uid == other.uid;
}

const String PluginDescription::createIdentifierString() const throw()
{
	return pluginFormatName
			+ "-" + name
			+ "-" + String::toHexString (fileOrIdentifier.hashCode())
			+ "-" + String::toHexString (uid);
}

XmlElement* PluginDescription::createXml() const
{
	XmlElement* const e = new XmlElement ("PLUGIN");
	e->setAttribute ("name", name);
	e->setAttribute ("format", pluginFormatName);
	e->setAttribute ("category", category);
	e->setAttribute ("manufacturer", manufacturerName);
	e->setAttribute ("version", version);
	e->setAttribute ("file", fileOrIdentifier);
	e->setAttribute ("uid", String::toHexString (uid));
	e->setAttribute ("isInstrument", isInstrument);
	e->setAttribute ("fileTime", String::toHexString (lastFileModTime.toMilliseconds()));
	e->setAttribute ("numInputs", numInputChannels);
	e->setAttribute ("numOutputs", numOutputChannels);

	return e;
}

bool PluginDescription::loadFromXml (const XmlElement& xml)
{
	if (xml.hasTagName ("PLUGIN"))
	{
		name = xml.getStringAttribute ("name");
		pluginFormatName = xml.getStringAttribute ("format");
		category = xml.getStringAttribute ("category");
		manufacturerName = xml.getStringAttribute ("manufacturer");
		version = xml.getStringAttribute ("version");
		fileOrIdentifier = xml.getStringAttribute ("file");
		uid = xml.getStringAttribute ("uid").getHexValue32();
		isInstrument = xml.getBoolAttribute ("isInstrument", false);
		lastFileModTime = Time (xml.getStringAttribute ("fileTime").getHexValue64());
		numInputChannels = xml.getIntAttribute ("numInputs");
		numOutputChannels = xml.getIntAttribute ("numOutputs");

		return true;
	}

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PluginDescription.cpp ***/


/*** Start of inlined file: juce_PluginDirectoryScanner.cpp ***/
BEGIN_JUCE_NAMESPACE

PluginDirectoryScanner::PluginDirectoryScanner (KnownPluginList& listToAddTo,
												AudioPluginFormat& formatToLookFor,
												FileSearchPath directoriesToSearch,
												const bool recursive,
												const File& deadMansPedalFile_)
	: list (listToAddTo),
	  format (formatToLookFor),
	  deadMansPedalFile (deadMansPedalFile_),
	  nextIndex (0),
	  progress (0)
{
	directoriesToSearch.removeRedundantPaths();

	filesOrIdentifiersToScan = format.searchPathsForPlugins (directoriesToSearch, recursive);

	// If any plugins have crashed recently when being loaded, move them to the
	// end of the list to give the others a chance to load correctly..
	const StringArray crashedPlugins (getDeadMansPedalFile());

	for (int i = 0; i < crashedPlugins.size(); ++i)
	{
		const String f = crashedPlugins[i];

		for (int j = filesOrIdentifiersToScan.size(); --j >= 0;)
			if (f == filesOrIdentifiersToScan[j])
				filesOrIdentifiersToScan.move (j, -1);
	}
}

PluginDirectoryScanner::~PluginDirectoryScanner()
{
}

const String PluginDirectoryScanner::getNextPluginFileThatWillBeScanned() const throw()
{
	return format.getNameOfPluginFromIdentifier (filesOrIdentifiersToScan [nextIndex]);
}

bool PluginDirectoryScanner::scanNextFile (const bool dontRescanIfAlreadyInList)
{
	String file (filesOrIdentifiersToScan [nextIndex]);

	if (file.isNotEmpty())
	{
		if (! list.isListingUpToDate (file))
		{
			OwnedArray <PluginDescription> typesFound;

			// Add this plugin to the end of the dead-man's pedal list in case it crashes...
			StringArray crashedPlugins (getDeadMansPedalFile());
			crashedPlugins.removeString (file);
			crashedPlugins.add (file);
			setDeadMansPedalFile (crashedPlugins);

			list.scanAndAddFile (file,
								 dontRescanIfAlreadyInList,
								 typesFound,
								 format);

			// Managed to load without crashing, so remove it from the dead-man's-pedal..
			crashedPlugins.removeString (file);
			setDeadMansPedalFile (crashedPlugins);

			if (typesFound.size() == 0)
				failedFiles.add (file);
		}

		++nextIndex;
		progress = nextIndex / (float) filesOrIdentifiersToScan.size();
	}

	return nextIndex < filesOrIdentifiersToScan.size();
}

const StringArray PluginDirectoryScanner::getDeadMansPedalFile() throw()
{
	StringArray lines;

	if (deadMansPedalFile != File::nonexistent)
	{
		lines.addLines (deadMansPedalFile.loadFileAsString());
		lines.removeEmptyStrings();
	}

	return lines;
}

void PluginDirectoryScanner::setDeadMansPedalFile (const StringArray& newContents) throw()
{
	if (deadMansPedalFile != File::nonexistent)
		deadMansPedalFile.replaceWithText (newContents.joinIntoString ("\n"), true, true);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PluginDirectoryScanner.cpp ***/


/*** Start of inlined file: juce_PluginListComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

PluginListComponent::PluginListComponent (KnownPluginList& listToEdit,
										  const File& deadMansPedalFile_,
										  PropertiesFile* const propertiesToUse_)
	: list (listToEdit),
	  deadMansPedalFile (deadMansPedalFile_),
	  propertiesToUse (propertiesToUse_)
{
	addAndMakeVisible (listBox = new ListBox (String::empty, this));

	addAndMakeVisible (optionsButton = new TextButton ("Options..."));
	optionsButton->addButtonListener (this);
	optionsButton->setTriggeredOnMouseDown (true);

	setSize (400, 600);
	list.addChangeListener (this);
}

PluginListComponent::~PluginListComponent()
{
	list.removeChangeListener (this);
	deleteAllChildren();
}

void PluginListComponent::resized()
{
	listBox->setBounds (0, 0, getWidth(), getHeight() - 30);
	optionsButton->changeWidthToFitText (24);
	optionsButton->setTopLeftPosition (8, getHeight() - 28);
}

void PluginListComponent::changeListenerCallback (void*)
{
	listBox->updateContent();
	listBox->repaint();
}

int PluginListComponent::getNumRows()
{
	return list.getNumTypes();
}

void PluginListComponent::paintListBoxItem (int row,
											Graphics& g,
											int width, int height,
											bool rowIsSelected)
{
	if (rowIsSelected)
		g.fillAll (findColour (TextEditor::highlightColourId));

	const PluginDescription* const pd = list.getType (row);

	if (pd != 0)
	{
		GlyphArrangement ga;
		ga.addCurtailedLineOfText (Font (height * 0.7f, Font::bold), pd->name, 8.0f, height * 0.8f, width - 10.0f, true);

		g.setColour (Colours::black);
		ga.draw (g);

		const Rectangle<float> bb (ga.getBoundingBox (0, -1, false));

		String desc;
		desc << pd->pluginFormatName
			 << (pd->isInstrument ? " instrument" : " effect")
			 << " - "
			 << pd->numInputChannels << (pd->numInputChannels == 1 ? " in" : " ins")
			 << " / "
			 << pd->numOutputChannels << (pd->numOutputChannels == 1 ? " out" : " outs");

		if (pd->manufacturerName.isNotEmpty())
			desc << " - " << pd->manufacturerName;

		if (pd->version.isNotEmpty())
			desc << " - " << pd->version;

		 if (pd->category.isNotEmpty())
			desc << " - category: '" << pd->category << '\'';

		g.setColour (Colours::grey);

		ga.clear();
		ga.addCurtailedLineOfText (Font (height * 0.6f), desc, bb.getRight() + 10.0f, height * 0.8f, width - bb.getRight() - 12.0f, true);
		ga.draw (g);
	}
}

void PluginListComponent::deleteKeyPressed (int lastRowSelected)
{
	list.removeType (lastRowSelected);
}

void PluginListComponent::buttonClicked (Button* b)
{
	if (optionsButton == b)
	{
		PopupMenu menu;
		menu.addItem (1, TRANS("Clear list"));
		menu.addItem (5, TRANS("Remove selected plugin from list"), listBox->getNumSelectedRows() > 0);
		menu.addItem (6, TRANS("Show folder containing selected plugin"), listBox->getNumSelectedRows() > 0);
		menu.addItem (7, TRANS("Remove any plugins whose files no longer exist"));
		menu.addSeparator();
		menu.addItem (2, TRANS("Sort alphabetically"));
		menu.addItem (3, TRANS("Sort by category"));
		menu.addItem (4, TRANS("Sort by manufacturer"));
		menu.addSeparator();

		for (int i = 0; i < AudioPluginFormatManager::getInstance()->getNumFormats(); ++i)
		{
			AudioPluginFormat* const format = AudioPluginFormatManager::getInstance()->getFormat (i);

			if (format->getDefaultLocationsToSearch().getNumPaths() > 0)
				menu.addItem (10 + i, "Scan for new or updated " + format->getName() + " plugins...");
		}

		const int r = menu.showAt (optionsButton);

		if (r == 1)
		{
			list.clear();
		}
		else if (r == 2)
		{
			list.sort (KnownPluginList::sortAlphabetically);
		}
		else if (r == 3)
		{
			list.sort (KnownPluginList::sortByCategory);
		}
		else if (r == 4)
		{
			list.sort (KnownPluginList::sortByManufacturer);
		}
		else if (r == 5)
		{
			const SparseSet <int> selected (listBox->getSelectedRows());

			for (int i = list.getNumTypes(); --i >= 0;)
				if (selected.contains (i))
					list.removeType (i);
		}
		else if (r == 6)
		{
			const PluginDescription* const desc = list.getType (listBox->getSelectedRow());

			if (desc != 0)
			{
				if (File (desc->fileOrIdentifier).existsAsFile())
					File (desc->fileOrIdentifier).getParentDirectory().startAsProcess();
			}
		}
		else if (r == 7)
		{
			for (int i = list.getNumTypes(); --i >= 0;)
			{
				if (! AudioPluginFormatManager::getInstance()->doesPluginStillExist (*list.getType (i)))
				{
					list.removeType (i);
				}
			}
		}
		else if (r != 0)
		{
			typeToScan = r - 10;
			startTimer (1);
		}
	}
}

void PluginListComponent::timerCallback()
{
	stopTimer();
	scanFor (AudioPluginFormatManager::getInstance()->getFormat (typeToScan));
}

bool PluginListComponent::isInterestedInFileDrag (const StringArray& /*files*/)
{
	return true;
}

void PluginListComponent::filesDropped (const StringArray& files, int, int)
{
	OwnedArray <PluginDescription> typesFound;
	list.scanAndAddDragAndDroppedFiles (files, typesFound);
}

void PluginListComponent::scanFor (AudioPluginFormat* format)
{
	if (format == 0)
		return;

	FileSearchPath path (format->getDefaultLocationsToSearch());

	if (propertiesToUse != 0)
		path = propertiesToUse->getValue ("lastPluginScanPath_" + format->getName(), path.toString());

	{
		AlertWindow aw (TRANS("Select folders to scan..."), String::empty, AlertWindow::NoIcon);
		FileSearchPathListComponent pathList;
		pathList.setSize (500, 300);
		pathList.setPath (path);

		aw.addCustomComponent (&pathList);
		aw.addButton (TRANS("Scan"), 1, KeyPress::returnKey);
		aw.addButton (TRANS("Cancel"), 0, KeyPress (KeyPress::escapeKey));

		if (aw.runModalLoop() == 0)
			return;

		path = pathList.getPath();
	}

	if (propertiesToUse != 0)
	{
		propertiesToUse->setValue ("lastPluginScanPath_" + format->getName(), path.toString());
		propertiesToUse->saveIfNeeded();
	}

	double progress = 0.0;

	AlertWindow aw (TRANS("Scanning for plugins..."),
					TRANS("Searching for all possible plugin files..."), AlertWindow::NoIcon);

	aw.addButton (TRANS("Cancel"), 0, KeyPress (KeyPress::escapeKey));
	aw.addProgressBarComponent (progress);

	aw.enterModalState();

	MessageManager::getInstance()->runDispatchLoopUntil (300);

	PluginDirectoryScanner scanner (list, *format, path, true, deadMansPedalFile);

	for (;;)
	{
		aw.setMessage (TRANS("Testing:\n\n")
						 + scanner.getNextPluginFileThatWillBeScanned());

		MessageManager::getInstance()->runDispatchLoopUntil (20);

		if (! scanner.scanNextFile (true))
			break;

		if (! aw.isCurrentlyModal())
			break;

		progress = scanner.getProgress();
	}

	if (scanner.getFailedFiles().size() > 0)
	{
		StringArray shortNames;

		for (int i = 0; i < scanner.getFailedFiles().size(); ++i)
			shortNames.add (File (scanner.getFailedFiles()[i]).getFileName());

		AlertWindow::showMessageBox (AlertWindow::InfoIcon,
									 TRANS("Scan complete"),
									 TRANS("Note that the following files appeared to be plugin files, but failed to load correctly:\n\n")
										+ shortNames.joinIntoString (", "));
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PluginListComponent.cpp ***/


/*** Start of inlined file: juce_AudioUnitPluginFormat.mm ***/
#if JUCE_PLUGINHOST_AU && ! (JUCE_LINUX || JUCE_WINDOWS)

#include <AudioUnit/AudioUnit.h>
#include <AudioUnit/AUCocoaUIView.h>
#include <CoreAudioKit/AUGenericView.h>

#if JUCE_SUPPORT_CARBON
#include <AudioToolbox/AudioUnitUtilities.h>
#include <AudioUnit/AudioUnitCarbonView.h>
#endif

BEGIN_JUCE_NAMESPACE

#if JUCE_MAC && JUCE_SUPPORT_CARBON

#endif

#if JUCE_MAC

// Change this to disable logging of various activities
#ifndef AU_LOGGING
  #define AU_LOGGING 1
#endif

#if AU_LOGGING
 #define log(a) Logger::writeToLog(a);
#else
 #define log(a)
#endif

static int insideCallback = 0;

static const String osTypeToString (OSType type)
{
	char s[4];
	s[0] = (char) (((uint32) type) >> 24);
	s[1] = (char) (((uint32) type) >> 16);
	s[2] = (char) (((uint32) type) >> 8);
	s[3] = (char) ((uint32) type);
	return String (s, 4);
}

static OSType stringToOSType (const String& s1)
{
	const String s (s1 + "    ");

	return (((OSType) (unsigned char) s[0]) << 24)
		 | (((OSType) (unsigned char) s[1]) << 16)
		 | (((OSType) (unsigned char) s[2]) << 8)
		 | ((OSType) (unsigned char) s[3]);
}

static const char* auIdentifierPrefix = "AudioUnit:";

static const String createAUPluginIdentifier (const ComponentDescription& desc)
{
	jassert (osTypeToString ('abcd') == "abcd"); // agh, must have got the endianness wrong..
	jassert (stringToOSType ("abcd") == (OSType) 'abcd'); // ditto

	String s (auIdentifierPrefix);

	if (desc.componentType == kAudioUnitType_MusicDevice)
		s << "Synths/";
	else if (desc.componentType == kAudioUnitType_MusicEffect
			  || desc.componentType == kAudioUnitType_Effect)
		s << "Effects/";
	else if (desc.componentType == kAudioUnitType_Generator)
		s << "Generators/";
	else if (desc.componentType == kAudioUnitType_Panner)
		s << "Panners/";

	s << osTypeToString (desc.componentType) << ","
	  << osTypeToString (desc.componentSubType) << ","
	  << osTypeToString (desc.componentManufacturer);

	return s;
}

static void getAUDetails (ComponentRecord* comp, String& name, String& manufacturer)
{
	Handle componentNameHandle = NewHandle (sizeof (void*));
	Handle componentInfoHandle = NewHandle (sizeof (void*));

	if (componentNameHandle != 0 && componentInfoHandle != 0)
	{
		ComponentDescription desc;

		if (GetComponentInfo (comp, &desc, componentNameHandle, componentInfoHandle, 0) == noErr)
		{
			ConstStr255Param nameString = (ConstStr255Param) (*componentNameHandle);
			ConstStr255Param infoString = (ConstStr255Param) (*componentInfoHandle);

			if (nameString != 0 && nameString[0] != 0)
			{
				const String all ((const char*) nameString + 1, nameString[0]);
				DBG ("name: "+ all);

				manufacturer = all.upToFirstOccurrenceOf (":", false, false).trim();
				name = all.fromFirstOccurrenceOf (":", false, false).trim();
			}

			if (infoString != 0 && infoString[0] != 0)
			{
				DBG ("info: " + String ((const char*) infoString + 1, infoString[0]));
			}

			if (name.isEmpty())
				name = "<Unknown>";
		}

		DisposeHandle (componentNameHandle);
		DisposeHandle (componentInfoHandle);
	}
}

static bool getComponentDescFromIdentifier (const String& fileOrIdentifier, ComponentDescription& desc,
											String& name, String& version, String& manufacturer)
{
	zerostruct (desc);

	if (fileOrIdentifier.startsWithIgnoreCase (auIdentifierPrefix))
	{
		String s (fileOrIdentifier.substring (jmax (fileOrIdentifier.lastIndexOfChar (':'),
													fileOrIdentifier.lastIndexOfChar ('/')) + 1));

		StringArray tokens;
		tokens.addTokens (s, ",", String::empty);
		tokens.trim();
		tokens.removeEmptyStrings();

		if (tokens.size() == 3)
		{
			desc.componentType = stringToOSType (tokens[0]);
			desc.componentSubType = stringToOSType (tokens[1]);
			desc.componentManufacturer = stringToOSType (tokens[2]);

			ComponentRecord* comp = FindNextComponent (0, &desc);

			if (comp != 0)
			{
				getAUDetails (comp, name, manufacturer);

				return true;
			}
		}
	}

	return false;
}

class AudioUnitPluginWindowCarbon;
class AudioUnitPluginWindowCocoa;

class AudioUnitPluginInstance	 : public AudioPluginInstance
{
public:

	~AudioUnitPluginInstance();

	// AudioPluginInstance methods:

	void fillInPluginDescription (PluginDescription& desc) const
	{
		desc.name = pluginName;
		desc.fileOrIdentifier = createAUPluginIdentifier (componentDesc);
		desc.uid = ((int) componentDesc.componentType)
					^ ((int) componentDesc.componentSubType)
					^ ((int) componentDesc.componentManufacturer);
		desc.lastFileModTime = 0;
		desc.pluginFormatName = "AudioUnit";
		desc.category = getCategory();
		desc.manufacturerName = manufacturer;
		desc.version = version;
		desc.numInputChannels = getNumInputChannels();
		desc.numOutputChannels = getNumOutputChannels();
		desc.isInstrument = (componentDesc.componentType == kAudioUnitType_MusicDevice);
	}

	const String getName() const		{ return pluginName; }
	bool acceptsMidi() const			{ return wantsMidiMessages; }
	bool producesMidi() const		   { return false; }

	// AudioProcessor methods:

	void prepareToPlay (double sampleRate, int estimatedSamplesPerBlock);
	void releaseResources();
	void processBlock (AudioSampleBuffer& buffer,
					   MidiBuffer& midiMessages);

	AudioProcessorEditor* createEditor();

	const String getInputChannelName (const int index) const;
	bool isInputChannelStereoPair (int index) const;

	const String getOutputChannelName (const int index) const;
	bool isOutputChannelStereoPair (int index) const;

	int getNumParameters();
	float getParameter (int index);
	void setParameter (int index, float newValue);
	const String getParameterName (int index);
	const String getParameterText (int index);
	bool isParameterAutomatable (int index) const;

	int getNumPrograms();
	int getCurrentProgram();
	void setCurrentProgram (int index);
	const String getProgramName (int index);
	void changeProgramName (int index, const String& newName);

	void getStateInformation (MemoryBlock& destData);
	void getCurrentProgramStateInformation (MemoryBlock& destData);
	void setStateInformation (const void* data, int sizeInBytes);
	void setCurrentProgramStateInformation (const void* data, int sizeInBytes);

	juce_UseDebuggingNewOperator

private:
	friend class AudioUnitPluginWindowCarbon;
	friend class AudioUnitPluginWindowCocoa;
	friend class AudioUnitPluginFormat;

	ComponentDescription componentDesc;
	String pluginName, manufacturer, version;
	String fileOrIdentifier;
	CriticalSection lock;
	bool initialised, wantsMidiMessages, wasPlaying;

	HeapBlock <AudioBufferList> outputBufferList;
	AudioTimeStamp timeStamp;
	AudioSampleBuffer* currentBuffer;

	AudioUnit audioUnit;
	Array <int> parameterIds;

	bool getComponentDescFromFile (const String& fileOrIdentifier);
	void initialise();

	OSStatus renderGetInput (AudioUnitRenderActionFlags* ioActionFlags,
							 const AudioTimeStamp* inTimeStamp,
							 UInt32 inBusNumber,
							 UInt32 inNumberFrames,
							 AudioBufferList* ioData) const;

	static OSStatus renderGetInputCallback (void* inRefCon,
											AudioUnitRenderActionFlags* ioActionFlags,
											const AudioTimeStamp* inTimeStamp,
											UInt32 inBusNumber,
											UInt32 inNumberFrames,
											AudioBufferList* ioData)
	{
		return ((AudioUnitPluginInstance*) inRefCon)
					->renderGetInput (ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData);
	}

	OSStatus getBeatAndTempo (Float64* outCurrentBeat, Float64* outCurrentTempo) const;
	OSStatus getMusicalTimeLocation (UInt32* outDeltaSampleOffsetToNextBeat, Float32* outTimeSig_Numerator,
									 UInt32* outTimeSig_Denominator, Float64* outCurrentMeasureDownBeat) const;
	OSStatus getTransportState (Boolean* outIsPlaying, Boolean* outTransportStateChanged,
								Float64* outCurrentSampleInTimeLine, Boolean* outIsCycling,
								Float64* outCycleStartBeat, Float64* outCycleEndBeat);

	static OSStatus getBeatAndTempoCallback (void* inHostUserData, Float64* outCurrentBeat, Float64* outCurrentTempo)
	{
		return ((AudioUnitPluginInstance*) inHostUserData)->getBeatAndTempo (outCurrentBeat, outCurrentTempo);
	}

	static OSStatus getMusicalTimeLocationCallback (void* inHostUserData, UInt32* outDeltaSampleOffsetToNextBeat,
													Float32* outTimeSig_Numerator, UInt32* outTimeSig_Denominator,
													Float64* outCurrentMeasureDownBeat)
	{
		return ((AudioUnitPluginInstance*) inHostUserData)
					->getMusicalTimeLocation (outDeltaSampleOffsetToNextBeat, outTimeSig_Numerator,
											  outTimeSig_Denominator, outCurrentMeasureDownBeat);
	}

	static OSStatus getTransportStateCallback (void* inHostUserData, Boolean* outIsPlaying, Boolean* outTransportStateChanged,
											   Float64* outCurrentSampleInTimeLine, Boolean* outIsCycling,
											   Float64* outCycleStartBeat, Float64* outCycleEndBeat)
	{
		return ((AudioUnitPluginInstance*) inHostUserData)
					->getTransportState (outIsPlaying, outTransportStateChanged,
										 outCurrentSampleInTimeLine, outIsCycling,
										 outCycleStartBeat, outCycleEndBeat);
	}

	void getNumChannels (int& numIns, int& numOuts)
	{
		numIns = 0;
		numOuts = 0;

		AUChannelInfo supportedChannels [128];
		UInt32 supportedChannelsSize = sizeof (supportedChannels);

		if (AudioUnitGetProperty (audioUnit, kAudioUnitProperty_SupportedNumChannels, kAudioUnitScope_Global,
								  0, supportedChannels, &supportedChannelsSize) == noErr
			&& supportedChannelsSize > 0)
		{
			for (int i = 0; i < supportedChannelsSize / sizeof (AUChannelInfo); ++i)
			{
				numIns = jmax (numIns, (int) supportedChannels[i].inChannels);
				numOuts = jmax (numOuts, (int) supportedChannels[i].outChannels);
			}
		}
		else
		{
				// (this really means the plugin will take any number of ins/outs as long
				// as they are the same)
			numIns = numOuts = 2;
		}
	}

	const String getCategory() const;

	AudioUnitPluginInstance (const String& fileOrIdentifier);
};

AudioUnitPluginInstance::AudioUnitPluginInstance (const String& fileOrIdentifier)
	: fileOrIdentifier (fileOrIdentifier),
	  initialised (false),
	  wantsMidiMessages (false),
	  audioUnit (0),
	  currentBuffer (0)
{
	try
	{
		++insideCallback;

		log ("Opening AU: " + fileOrIdentifier);

		if (getComponentDescFromFile (fileOrIdentifier))
		{
			ComponentRecord* const comp = FindNextComponent (0, &componentDesc);

			if (comp != 0)
			{
				audioUnit = (AudioUnit) OpenComponent (comp);

				wantsMidiMessages = componentDesc.componentType == kAudioUnitType_MusicDevice
					|| componentDesc.componentType == kAudioUnitType_MusicEffect;
			}
		}

		--insideCallback;
	}
	catch (...)
	{
		--insideCallback;
	}
}

AudioUnitPluginInstance::~AudioUnitPluginInstance()
{
	const ScopedLock sl (lock);

	jassert (insideCallback == 0);

	if (audioUnit != 0)
	{
		AudioUnitUninitialize (audioUnit);
		CloseComponent (audioUnit);
		audioUnit = 0;
	}
}

bool AudioUnitPluginInstance::getComponentDescFromFile (const String& fileOrIdentifier)
{
	zerostruct (componentDesc);

	if (getComponentDescFromIdentifier (fileOrIdentifier, componentDesc, pluginName, version, manufacturer))
		return true;

	const File file (fileOrIdentifier);
	if (! file.hasFileExtension (".component"))
		return false;

	const char* const utf8 = fileOrIdentifier.toUTF8();
	CFURLRef url = CFURLCreateFromFileSystemRepresentation (0, (const UInt8*) utf8,
															strlen (utf8), file.isDirectory());
	if (url != 0)
	{
		CFBundleRef bundleRef = CFBundleCreate (kCFAllocatorDefault, url);
		CFRelease (url);

		if (bundleRef != 0)
		{
			CFTypeRef name = CFBundleGetValueForInfoDictionaryKey (bundleRef, CFSTR("CFBundleName"));

			if (name != 0 && CFGetTypeID (name) == CFStringGetTypeID())
				pluginName = PlatformUtilities::cfStringToJuceString ((CFStringRef) name);

			if (pluginName.isEmpty())
				pluginName = file.getFileNameWithoutExtension();

			CFTypeRef versionString = CFBundleGetValueForInfoDictionaryKey (bundleRef, CFSTR("CFBundleVersion"));

			if (versionString != 0 && CFGetTypeID (versionString) == CFStringGetTypeID())
				version = PlatformUtilities::cfStringToJuceString ((CFStringRef) versionString);

			CFTypeRef manuString = CFBundleGetValueForInfoDictionaryKey (bundleRef, CFSTR("CFBundleGetInfoString"));

			if (manuString != 0 && CFGetTypeID (manuString) == CFStringGetTypeID())
				manufacturer = PlatformUtilities::cfStringToJuceString ((CFStringRef) manuString);

			short resFileId = CFBundleOpenBundleResourceMap (bundleRef);
			UseResFile (resFileId);

			for (int i = 1; i <= Count1Resources ('thng'); ++i)
			{
				Handle h = Get1IndResource ('thng', i);

				if (h != 0)
				{
					HLock (h);
					const uint32* const types = (const uint32*) *h;

					if (types[0] == kAudioUnitType_MusicDevice
						 || types[0] == kAudioUnitType_MusicEffect
						 || types[0] == kAudioUnitType_Effect
						 || types[0] == kAudioUnitType_Generator
						 || types[0] == kAudioUnitType_Panner)
					{
						componentDesc.componentType = types[0];
						componentDesc.componentSubType = types[1];
						componentDesc.componentManufacturer = types[2];
						break;
					}

					HUnlock (h);
					ReleaseResource (h);
				}
			}

			CFBundleCloseBundleResourceMap (bundleRef, resFileId);
			CFRelease (bundleRef);
		}
	}

	return componentDesc.componentType != 0 && componentDesc.componentSubType != 0;
}

void AudioUnitPluginInstance::initialise()
{
	if (initialised || audioUnit == 0)
		return;

	log ("Initialising AU: " + pluginName);

	parameterIds.clear();

	{
		UInt32 paramListSize = 0;
		AudioUnitGetProperty (audioUnit, kAudioUnitProperty_ParameterList, kAudioUnitScope_Global,
							  0, 0, &paramListSize);

		if (paramListSize > 0)
		{
			parameterIds.insertMultiple (0, 0, paramListSize / sizeof (int));

			AudioUnitGetProperty (audioUnit, kAudioUnitProperty_ParameterList, kAudioUnitScope_Global,
								  0, &parameterIds.getReference(0), &paramListSize);
		}
	}

	{
		AURenderCallbackStruct info;
		zerostruct (info);
		info.inputProcRefCon = this;
		info.inputProc = renderGetInputCallback;

		AudioUnitSetProperty (audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input,
							  0, &info, sizeof (info));
	}

	{
		HostCallbackInfo info;
		zerostruct (info);
		info.hostUserData = this;
		info.beatAndTempoProc = getBeatAndTempoCallback;
		info.musicalTimeLocationProc = getMusicalTimeLocationCallback;
		info.transportStateProc = getTransportStateCallback;

		AudioUnitSetProperty (audioUnit, kAudioUnitProperty_HostCallbacks, kAudioUnitScope_Global,
							  0, &info, sizeof (info));
	}

	int numIns, numOuts;
	getNumChannels (numIns, numOuts);
	setPlayConfigDetails (numIns, numOuts, 0, 0);

	initialised = AudioUnitInitialize (audioUnit) == noErr;

	setLatencySamples (0);
}

void AudioUnitPluginInstance::prepareToPlay (double sampleRate_,
											 int samplesPerBlockExpected)
{
	initialise();

	if (initialised)
	{
		int numIns, numOuts;
		getNumChannels (numIns, numOuts);

		setPlayConfigDetails (numIns, numOuts, sampleRate_, samplesPerBlockExpected);

		Float64 latencySecs = 0.0;
		UInt32 latencySize = sizeof (latencySecs);
		AudioUnitGetProperty (audioUnit, kAudioUnitProperty_Latency, kAudioUnitScope_Global,
							  0, &latencySecs, &latencySize);

		setLatencySamples (roundToInt (latencySecs * sampleRate_));

		AudioUnitReset (audioUnit, kAudioUnitScope_Input, 0);
		AudioUnitReset (audioUnit, kAudioUnitScope_Output, 0);
		AudioUnitReset (audioUnit, kAudioUnitScope_Global, 0);

		AudioStreamBasicDescription stream;
		zerostruct (stream);
		stream.mSampleRate = sampleRate_;
		stream.mFormatID = kAudioFormatLinearPCM;
		stream.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved;
		stream.mFramesPerPacket = 1;
		stream.mBytesPerPacket = 4;
		stream.mBytesPerFrame = 4;
		stream.mBitsPerChannel = 32;
		stream.mChannelsPerFrame = numIns;

		OSStatus err = AudioUnitSetProperty (audioUnit,
											 kAudioUnitProperty_StreamFormat,
											 kAudioUnitScope_Input,
											 0, &stream, sizeof (stream));

		stream.mChannelsPerFrame = numOuts;

		err = AudioUnitSetProperty (audioUnit,
									kAudioUnitProperty_StreamFormat,
									kAudioUnitScope_Output,
									0, &stream, sizeof (stream));

		outputBufferList.calloc (sizeof (AudioBufferList) + sizeof (AudioBuffer) * (numOuts + 1), 1);
		outputBufferList->mNumberBuffers = numOuts;

		for (int i = numOuts; --i >= 0;)
			outputBufferList->mBuffers[i].mNumberChannels = 1;

		zerostruct (timeStamp);
		timeStamp.mSampleTime = 0;
		timeStamp.mHostTime = AudioGetCurrentHostTime();
		timeStamp.mFlags = kAudioTimeStampSampleTimeValid | kAudioTimeStampHostTimeValid;

		currentBuffer = 0;
		wasPlaying = false;
	}
}

void AudioUnitPluginInstance::releaseResources()
{
	if (initialised)
	{
		AudioUnitReset (audioUnit, kAudioUnitScope_Input, 0);
		AudioUnitReset (audioUnit, kAudioUnitScope_Output, 0);
		AudioUnitReset (audioUnit, kAudioUnitScope_Global, 0);

		outputBufferList.free();
		currentBuffer = 0;
	}
}

OSStatus AudioUnitPluginInstance::renderGetInput (AudioUnitRenderActionFlags* ioActionFlags,
												  const AudioTimeStamp* inTimeStamp,
												  UInt32 inBusNumber,
												  UInt32 inNumberFrames,
												  AudioBufferList* ioData) const
{
	if (inBusNumber == 0
		 && currentBuffer != 0)
	{
		jassert (inNumberFrames == currentBuffer->getNumSamples()); // if this ever happens, might need to add extra handling

		for (int i = 0; i < ioData->mNumberBuffers; ++i)
		{
			if (i < currentBuffer->getNumChannels())
			{
				memcpy (ioData->mBuffers[i].mData,
						currentBuffer->getSampleData (i, 0),
						sizeof (float) * inNumberFrames);
			}
			else
			{
				zeromem (ioData->mBuffers[i].mData, sizeof (float) * inNumberFrames);
			}
		}
	}

	return noErr;
}

void AudioUnitPluginInstance::processBlock (AudioSampleBuffer& buffer,
											MidiBuffer& midiMessages)
{
	const int numSamples = buffer.getNumSamples();

	if (initialised)
	{
		AudioUnitRenderActionFlags flags = 0;

		timeStamp.mHostTime = AudioGetCurrentHostTime();

		for (int i = getNumOutputChannels(); --i >= 0;)
		{
			outputBufferList->mBuffers[i].mDataByteSize = sizeof (float) * numSamples;
			outputBufferList->mBuffers[i].mData = buffer.getSampleData (i, 0);
		}

		currentBuffer = &buffer;

		if (wantsMidiMessages)
		{
			const uint8* midiEventData;
			int midiEventSize, midiEventPosition;
			MidiBuffer::Iterator i (midiMessages);

			while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
			{
				if (midiEventSize <= 3)
					MusicDeviceMIDIEvent (audioUnit,
										  midiEventData[0], midiEventData[1], midiEventData[2],
										  midiEventPosition);
				else
					MusicDeviceSysEx (audioUnit, midiEventData, midiEventSize);
			}

			midiMessages.clear();
		}

		AudioUnitRender (audioUnit, &flags, &timeStamp,
						 0, numSamples, outputBufferList);

		timeStamp.mSampleTime += numSamples;
	}
	else
	{
		// Not initialised, so just bypass..
		for (int i = getNumInputChannels(); i < getNumOutputChannels(); ++i)
			buffer.clear (i, 0, buffer.getNumSamples());
	}
}

OSStatus AudioUnitPluginInstance::getBeatAndTempo (Float64* outCurrentBeat, Float64* outCurrentTempo) const
{
	AudioPlayHead* const ph = getPlayHead();
	AudioPlayHead::CurrentPositionInfo result;

	if (ph != 0 && ph->getCurrentPosition (result))
	{
		if (outCurrentBeat != 0)
			*outCurrentBeat = result.ppqPosition;

		if (outCurrentTempo != 0)
			*outCurrentTempo = result.bpm;
	}
	else
	{
		if (outCurrentBeat != 0)
			*outCurrentBeat = 0;

		if (outCurrentTempo != 0)
			*outCurrentTempo = 120.0;
	}

	return noErr;
}

OSStatus AudioUnitPluginInstance::getMusicalTimeLocation (UInt32* outDeltaSampleOffsetToNextBeat,
														  Float32* outTimeSig_Numerator,
														  UInt32* outTimeSig_Denominator,
														  Float64* outCurrentMeasureDownBeat) const
{
	AudioPlayHead* const ph = getPlayHead();
	AudioPlayHead::CurrentPositionInfo result;

	if (ph != 0 && ph->getCurrentPosition (result))
	{
		if (outTimeSig_Numerator != 0)
			*outTimeSig_Numerator = result.timeSigNumerator;

		if (outTimeSig_Denominator != 0)
			*outTimeSig_Denominator = result.timeSigDenominator;

		if (outDeltaSampleOffsetToNextBeat != 0)
			*outDeltaSampleOffsetToNextBeat = 0; //xxx

		if (outCurrentMeasureDownBeat != 0)
			*outCurrentMeasureDownBeat = result.ppqPositionOfLastBarStart; //xxx wrong
	}
	else
	{
		if (outDeltaSampleOffsetToNextBeat != 0)
			*outDeltaSampleOffsetToNextBeat = 0;

		if (outTimeSig_Numerator != 0)
			*outTimeSig_Numerator = 4;

		if (outTimeSig_Denominator != 0)
			*outTimeSig_Denominator = 4;

		if (outCurrentMeasureDownBeat != 0)
			*outCurrentMeasureDownBeat = 0;
	}

	return noErr;
}

OSStatus AudioUnitPluginInstance::getTransportState (Boolean* outIsPlaying,
													 Boolean* outTransportStateChanged,
													 Float64* outCurrentSampleInTimeLine,
													 Boolean* outIsCycling,
													 Float64* outCycleStartBeat,
													 Float64* outCycleEndBeat)
{
	AudioPlayHead* const ph = getPlayHead();
	AudioPlayHead::CurrentPositionInfo result;

	if (ph != 0 && ph->getCurrentPosition (result))
	{
		if (outIsPlaying != 0)
			*outIsPlaying = result.isPlaying;

		if (outTransportStateChanged != 0)
		{
			*outTransportStateChanged = result.isPlaying != wasPlaying;
			wasPlaying = result.isPlaying;
		}

		if (outCurrentSampleInTimeLine != 0)
			*outCurrentSampleInTimeLine = roundToInt (result.timeInSeconds * getSampleRate());

		if (outIsCycling != 0)
			*outIsCycling = false;

		if (outCycleStartBeat != 0)
			*outCycleStartBeat = 0;

		if (outCycleEndBeat != 0)
			*outCycleEndBeat = 0;
	}
	else
	{
		if (outIsPlaying != 0)
			*outIsPlaying = false;

		if (outTransportStateChanged != 0)
			*outTransportStateChanged = false;

		if (outCurrentSampleInTimeLine != 0)
			*outCurrentSampleInTimeLine = 0;

		if (outIsCycling != 0)
			*outIsCycling = false;

		if (outCycleStartBeat != 0)
			*outCycleStartBeat = 0;

		if (outCycleEndBeat != 0)
			*outCycleEndBeat = 0;
	}

	return noErr;
}

static VoidArray activeWindows;

class AudioUnitPluginWindowCocoa	: public AudioProcessorEditor
{
public:
	AudioUnitPluginWindowCocoa (AudioUnitPluginInstance& plugin_, const bool createGenericViewIfNeeded)
		: AudioProcessorEditor (&plugin_),
		  plugin (plugin_),
		  wrapper (0)
	{
		addAndMakeVisible (wrapper = new NSViewComponent());

		activeWindows.add (this);

		setOpaque (true);
		setVisible (true);
		setSize (100, 100);

		createView (createGenericViewIfNeeded);
	}

	~AudioUnitPluginWindowCocoa()
	{
		const bool wasValid = isValid();

		wrapper->setView (0);
		activeWindows.removeValue (this);

		if (wasValid)
			plugin.editorBeingDeleted (this);

		delete wrapper;
	}

	bool isValid() const	{ return wrapper->getView() != 0; }

	void paint (Graphics& g)
	{
		g.fillAll (Colours::white);
	}

	void resized()
	{
		wrapper->setSize (getWidth(), getHeight());
	}

private:
	AudioUnitPluginInstance& plugin;
	NSViewComponent* wrapper;

	bool createView (const bool createGenericViewIfNeeded)
	{
		NSView* pluginView = 0;

		UInt32 dataSize = 0;
		Boolean isWritable = false;

		if (AudioUnitGetPropertyInfo (plugin.audioUnit, kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
									  0, &dataSize, &isWritable) == noErr
			 && dataSize != 0
			 && AudioUnitGetPropertyInfo (plugin.audioUnit, kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
										  0, &dataSize, &isWritable) == noErr)
		{
			HeapBlock <AudioUnitCocoaViewInfo> info;
			info.calloc (dataSize, 1);

			if (AudioUnitGetProperty (plugin.audioUnit, kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
									  0, info, &dataSize) == noErr)
			{
				NSString* viewClassName = (NSString*) (info->mCocoaAUViewClass[0]);
				NSString* path = (NSString*) CFURLCopyPath (info->mCocoaAUViewBundleLocation);
				NSBundle* viewBundle = [NSBundle bundleWithPath: [path autorelease]];
				Class viewClass = [viewBundle classNamed: viewClassName];

				if ([viewClass conformsToProtocol: @protocol (AUCocoaUIBase)]
					 && [viewClass instancesRespondToSelector: @selector (interfaceVersion)]
					 && [viewClass instancesRespondToSelector: @selector (uiViewForAudioUnit: withSize:)])
				{
					id factory = [[[viewClass alloc] init] autorelease];
					pluginView = [factory uiViewForAudioUnit: plugin.audioUnit
													withSize: NSMakeSize (getWidth(), getHeight())];
				}

				for (int i = (dataSize - sizeof (CFURLRef)) / sizeof (CFStringRef); --i >= 0;)
				{
					CFRelease (info->mCocoaAUViewClass[i]);
					CFRelease (info->mCocoaAUViewBundleLocation);
				}
			}
		}

		if (createGenericViewIfNeeded && (pluginView == 0))
			pluginView = [[AUGenericView alloc] initWithAudioUnit: plugin.audioUnit];

		wrapper->setView (pluginView);

		if (pluginView != 0)
			setSize ([pluginView frame].size.width,
					 [pluginView frame].size.height);

		return pluginView != 0;
	}
};

#if JUCE_SUPPORT_CARBON

class AudioUnitPluginWindowCarbon   : public AudioProcessorEditor
{
public:

	AudioUnitPluginWindowCarbon (AudioUnitPluginInstance& plugin_)
		: AudioProcessorEditor (&plugin_),
		  plugin (plugin_),
		  viewComponent (0)
	{
		addAndMakeVisible (innerWrapper = new InnerWrapperComponent (this));

		activeWindows.add (this);

		setOpaque (true);
		setVisible (true);
		setSize (400, 300);

		ComponentDescription viewList [16];
		UInt32 viewListSize = sizeof (viewList);
		AudioUnitGetProperty (plugin.audioUnit, kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global,
							  0, &viewList, &viewListSize);

		componentRecord = FindNextComponent (0, &viewList[0]);
	}

	~AudioUnitPluginWindowCarbon()
	{
		innerWrapper = 0;
		activeWindows.removeValue (this);

		if (isValid())
			plugin.editorBeingDeleted (this);
	}

	bool isValid() const throw()		{ return componentRecord != 0; }

	void paint (Graphics& g)
	{
		g.fillAll (Colours::black);
	}

	void resized()
	{
		innerWrapper->setSize (getWidth(), getHeight());
	}

	bool keyStateChanged (bool)
	{
		return false;
	}

	bool keyPressed (const KeyPress&)
	{
		return false;
	}

	void broughtToFront()
	{
		activeWindows.removeValue (this);
		activeWindows.add (this);
	}

	AudioUnit getAudioUnit() const	  { return plugin.audioUnit; }

	AudioUnitCarbonView getViewComponent()
	{
		if (viewComponent == 0 && componentRecord != 0)
			viewComponent = (AudioUnitCarbonView) OpenComponent (componentRecord);

		return viewComponent;
	}

	void closeViewComponent()
	{
		if (viewComponent != 0)
		{
			CloseComponent (viewComponent);
			viewComponent = 0;
		}
	}

	juce_UseDebuggingNewOperator

private:
	AudioUnitPluginInstance& plugin;
	ComponentRecord* componentRecord;
	AudioUnitCarbonView viewComponent;

	class InnerWrapperComponent   : public CarbonViewWrapperComponent
	{
	public:
		InnerWrapperComponent (AudioUnitPluginWindowCarbon* const owner_)
			: owner (owner_)
		{
		}

		~InnerWrapperComponent()
		{
			deleteWindow();
		}

		HIViewRef attachView (WindowRef windowRef, HIViewRef rootView)
		{
			log ("Opening AU GUI: " + owner->plugin.getName());

			AudioUnitCarbonView viewComponent = owner->getViewComponent();

			if (viewComponent == 0)
				return 0;

			Float32Point pos = { 0, 0 };
			Float32Point size = { 250, 200 };

			HIViewRef pluginView = 0;

			AudioUnitCarbonViewCreate (viewComponent,
									   owner->getAudioUnit(),
									   windowRef,
									   rootView,
									   &pos,
									   &size,
									   (ControlRef*) &pluginView);

			return pluginView;
		}

		void removeView (HIViewRef)
		{
			log ("Closing AU GUI: " + owner->plugin.getName());

			owner->closeViewComponent();
		}

	private:
		AudioUnitPluginWindowCarbon* const owner;
	};

	friend class InnerWrapperComponent;
	ScopedPointer<InnerWrapperComponent> innerWrapper;
};

#endif

AudioProcessorEditor* AudioUnitPluginInstance::createEditor()
{
	ScopedPointer<AudioProcessorEditor> w (new AudioUnitPluginWindowCocoa (*this, false));

	if (! static_cast <AudioUnitPluginWindowCocoa*> (static_cast <AudioProcessorEditor*> (w))->isValid())
		w = 0;

#if JUCE_SUPPORT_CARBON
	if (w == 0)
	{
		w = new AudioUnitPluginWindowCarbon (*this);

		if (! static_cast <AudioUnitPluginWindowCarbon*> (static_cast <AudioProcessorEditor*> (w))->isValid())
			w = 0;
	}
#endif

	if (w == 0)
		w = new AudioUnitPluginWindowCocoa (*this, true); // use AUGenericView as a fallback

	return w.release();
}

const String AudioUnitPluginInstance::getCategory() const
{
	const char* result = 0;

	switch (componentDesc.componentType)
	{
	case kAudioUnitType_Effect:
	case kAudioUnitType_MusicEffect:
		result = "Effect";
		break;
	case kAudioUnitType_MusicDevice:
		result = "Synth";
		break;
	case kAudioUnitType_Generator:
		result = "Generator";
		break;
	case kAudioUnitType_Panner:
		result = "Panner";
		break;
	default:
		break;
	}

	return result;
}

int AudioUnitPluginInstance::getNumParameters()
{
	return parameterIds.size();
}

float AudioUnitPluginInstance::getParameter (int index)
{
	const ScopedLock sl (lock);

	Float32 value = 0.0f;

	if (audioUnit != 0 && ((unsigned int) index) < (unsigned int) parameterIds.size())
	{
		AudioUnitGetParameter (audioUnit,
							   (UInt32) parameterIds.getUnchecked (index),
							   kAudioUnitScope_Global, 0,
							   &value);
	}

	return value;
}

void AudioUnitPluginInstance::setParameter (int index, float newValue)
{
	const ScopedLock sl (lock);

	if (audioUnit != 0 && ((unsigned int) index) < (unsigned int) parameterIds.size())
	{
		AudioUnitSetParameter (audioUnit,
							   (UInt32) parameterIds.getUnchecked (index),
							   kAudioUnitScope_Global, 0,
							   newValue, 0);
	}
}

const String AudioUnitPluginInstance::getParameterName (int index)
{
	AudioUnitParameterInfo info;
	zerostruct (info);
	UInt32 sz = sizeof (info);

	String name;

	if (AudioUnitGetProperty (audioUnit,
							  kAudioUnitProperty_ParameterInfo,
							  kAudioUnitScope_Global,
							  parameterIds [index], &info, &sz) == noErr)
	{
		if ((info.flags & kAudioUnitParameterFlag_HasCFNameString) != 0)
			name = PlatformUtilities::cfStringToJuceString (info.cfNameString);
		else
			name = String (info.name, sizeof (info.name));
	}

	return name;
}

const String AudioUnitPluginInstance::getParameterText (int index)
{
	return String (getParameter (index));
}

bool AudioUnitPluginInstance::isParameterAutomatable (int index) const
{
	AudioUnitParameterInfo info;
	UInt32 sz = sizeof (info);

	if (AudioUnitGetProperty (audioUnit,
							  kAudioUnitProperty_ParameterInfo,
							  kAudioUnitScope_Global,
							  parameterIds [index], &info, &sz) == noErr)
	{
		return (info.flags & kAudioUnitParameterFlag_NonRealTime) == 0;
	}

	return true;
}

int AudioUnitPluginInstance::getNumPrograms()
{
	CFArrayRef presets;
	UInt32 sz = sizeof (CFArrayRef);
	int num = 0;

	if (AudioUnitGetProperty (audioUnit,
							  kAudioUnitProperty_FactoryPresets,
							  kAudioUnitScope_Global,
							  0, &presets, &sz) == noErr)
	{
		num = (int) CFArrayGetCount (presets);
		CFRelease (presets);
	}

	return num;
}

int AudioUnitPluginInstance::getCurrentProgram()
{
	AUPreset current;
	current.presetNumber = 0;
	UInt32 sz = sizeof (AUPreset);

	AudioUnitGetProperty (audioUnit,
						  kAudioUnitProperty_FactoryPresets,
						  kAudioUnitScope_Global,
						  0, &current, &sz);

	return current.presetNumber;
}

void AudioUnitPluginInstance::setCurrentProgram (int newIndex)
{
	AUPreset current;
	current.presetNumber = newIndex;
	current.presetName = 0;

	AudioUnitSetProperty (audioUnit,
						  kAudioUnitProperty_FactoryPresets,
						  kAudioUnitScope_Global,
						  0, &current, sizeof (AUPreset));
}

const String AudioUnitPluginInstance::getProgramName (int index)
{
	String s;
	CFArrayRef presets;
	UInt32 sz = sizeof (CFArrayRef);

	if (AudioUnitGetProperty (audioUnit,
							  kAudioUnitProperty_FactoryPresets,
							  kAudioUnitScope_Global,
							  0, &presets, &sz) == noErr)
	{
		for (CFIndex i = 0; i < CFArrayGetCount (presets); ++i)
		{
			const AUPreset* p = (const AUPreset*) CFArrayGetValueAtIndex (presets, i);

			if (p != 0 && p->presetNumber == index)
			{
				s = PlatformUtilities::cfStringToJuceString (p->presetName);
				break;
			}
		}

		CFRelease (presets);
	}

	return s;
}

void AudioUnitPluginInstance::changeProgramName (int index, const String& newName)
{
	jassertfalse // xxx not implemented!
}

const String AudioUnitPluginInstance::getInputChannelName (const int index) const
{
	if (((unsigned int) index) < (unsigned int) getNumInputChannels())
		return "Input " + String (index + 1);

	return String::empty;
}

bool AudioUnitPluginInstance::isInputChannelStereoPair (int index) const
{
	if (((unsigned int) index) >= (unsigned int) getNumInputChannels())
		return false;

	return true;
}

const String AudioUnitPluginInstance::getOutputChannelName (const int index) const
{
	if (((unsigned int) index) < (unsigned int) getNumOutputChannels())
		return "Output " + String (index + 1);

	return String::empty;
}

bool AudioUnitPluginInstance::isOutputChannelStereoPair (int index) const
{
	if (((unsigned int) index) >= (unsigned int) getNumOutputChannels())
		return false;

	return true;
}

void AudioUnitPluginInstance::getStateInformation (MemoryBlock& destData)
{
	getCurrentProgramStateInformation (destData);
}

void AudioUnitPluginInstance::getCurrentProgramStateInformation (MemoryBlock& destData)
{
	CFPropertyListRef propertyList = 0;
	UInt32 sz = sizeof (CFPropertyListRef);

	if (AudioUnitGetProperty (audioUnit,
							  kAudioUnitProperty_ClassInfo,
							  kAudioUnitScope_Global,
							  0, &propertyList, &sz) == noErr)
	{
		CFWriteStreamRef stream = CFWriteStreamCreateWithAllocatedBuffers (kCFAllocatorDefault, kCFAllocatorDefault);
		CFWriteStreamOpen (stream);

		CFIndex bytesWritten = CFPropertyListWriteToStream (propertyList, stream, kCFPropertyListBinaryFormat_v1_0, 0);
		CFWriteStreamClose (stream);

		CFDataRef data = (CFDataRef) CFWriteStreamCopyProperty (stream, kCFStreamPropertyDataWritten);

		destData.setSize (bytesWritten);
		destData.copyFrom (CFDataGetBytePtr (data), 0, destData.getSize());
		CFRelease (data);

		CFRelease (stream);
		CFRelease (propertyList);
	}
}

void AudioUnitPluginInstance::setStateInformation (const void* data, int sizeInBytes)
{
	setCurrentProgramStateInformation (data, sizeInBytes);
}

void AudioUnitPluginInstance::setCurrentProgramStateInformation (const void* data, int sizeInBytes)
{
	CFReadStreamRef stream = CFReadStreamCreateWithBytesNoCopy (kCFAllocatorDefault,
																(const UInt8*) data,
																sizeInBytes,
																kCFAllocatorNull);
	CFReadStreamOpen (stream);

	CFPropertyListFormat format = kCFPropertyListBinaryFormat_v1_0;
	CFPropertyListRef propertyList = CFPropertyListCreateFromStream (kCFAllocatorDefault,
																	 stream,
																	 0,
																	 kCFPropertyListImmutable,
																	 &format,
																	 0);
	CFRelease (stream);

	if (propertyList != 0)
		AudioUnitSetProperty (audioUnit,
							  kAudioUnitProperty_ClassInfo,
							  kAudioUnitScope_Global,
							  0, &propertyList, sizeof (propertyList));
}

AudioUnitPluginFormat::AudioUnitPluginFormat()
{
}

AudioUnitPluginFormat::~AudioUnitPluginFormat()
{
}

void AudioUnitPluginFormat::findAllTypesForFile (OwnedArray <PluginDescription>& results,
												 const String& fileOrIdentifier)
{
	if (! fileMightContainThisPluginType (fileOrIdentifier))
		return;

	PluginDescription desc;
	desc.fileOrIdentifier = fileOrIdentifier;
	desc.uid = 0;

	try
	{
		ScopedPointer <AudioPluginInstance> createdInstance (createInstanceFromDescription (desc));
		AudioUnitPluginInstance* const auInstance = dynamic_cast <AudioUnitPluginInstance*> ((AudioPluginInstance*) createdInstance);

		if (auInstance != 0)
		{
			auInstance->fillInPluginDescription (desc);
			results.add (new PluginDescription (desc));
		}
	}
	catch (...)
	{
		// crashed while loading...
	}
}

AudioPluginInstance* AudioUnitPluginFormat::createInstanceFromDescription (const PluginDescription& desc)
{
	if (fileMightContainThisPluginType (desc.fileOrIdentifier))
	{
		ScopedPointer <AudioUnitPluginInstance> result (new AudioUnitPluginInstance (desc.fileOrIdentifier));

		if (result->audioUnit != 0)
		{
			result->initialise();
			return result.release();
		}
	}

	return 0;
}

const StringArray AudioUnitPluginFormat::searchPathsForPlugins (const FileSearchPath& /*directoriesToSearch*/,
																const bool /*recursive*/)
{
	StringArray result;
	ComponentRecord* comp = 0;
	ComponentDescription desc;
	zerostruct (desc);

	for (;;)
	{
		zerostruct (desc);
		comp = FindNextComponent (comp, &desc);

		if (comp == 0)
			break;

		GetComponentInfo (comp, &desc, 0, 0, 0);

		if (desc.componentType == kAudioUnitType_MusicDevice
			 || desc.componentType == kAudioUnitType_MusicEffect
			 || desc.componentType == kAudioUnitType_Effect
			 || desc.componentType == kAudioUnitType_Generator
			 || desc.componentType == kAudioUnitType_Panner)
		{
			const String s (createAUPluginIdentifier (desc));
			DBG (s);
			result.add (s);
		}
	}

	return result;
}

bool AudioUnitPluginFormat::fileMightContainThisPluginType (const String& fileOrIdentifier)
{
	ComponentDescription desc;

	String name, version, manufacturer;
	if (getComponentDescFromIdentifier (fileOrIdentifier, desc, name, version, manufacturer))
		return FindNextComponent (0, &desc) != 0;

	const File f (fileOrIdentifier);

	return f.hasFileExtension (".component")
			 && f.isDirectory();
}

const String AudioUnitPluginFormat::getNameOfPluginFromIdentifier (const String& fileOrIdentifier)
{
	ComponentDescription desc;
	String name, version, manufacturer;
	getComponentDescFromIdentifier (fileOrIdentifier, desc, name, version, manufacturer);

	if (name.isEmpty())
		name = fileOrIdentifier;

	return name;
}

bool AudioUnitPluginFormat::doesPluginStillExist (const PluginDescription& desc)
{
	if (desc.fileOrIdentifier.startsWithIgnoreCase (auIdentifierPrefix))
		return fileMightContainThisPluginType (desc.fileOrIdentifier);
	else
		return File (desc.fileOrIdentifier).exists();
}

const FileSearchPath AudioUnitPluginFormat::getDefaultLocationsToSearch()
{
	return FileSearchPath ("/(Default AudioUnit locations)");
}

#endif

END_JUCE_NAMESPACE

#undef log

#endif
/*** End of inlined file: juce_AudioUnitPluginFormat.mm ***/


/*** Start of inlined file: juce_VSTPluginFormat.mm ***/
// This file just wraps juce_VSTPluginFormat.cpp in an objective-C wrapper
#define JUCE_MAC_VST_INCLUDED 1

/*** Start of inlined file: juce_VSTPluginFormat.cpp ***/
#if JUCE_PLUGINHOST_VST

#if JUCE_WINDOWS
 #undef _WIN32_WINNT
 #define _WIN32_WINNT 0x500
 #undef STRICT
 #define STRICT
 #include <windows.h>
 #include <float.h>
 #pragma warning (disable : 4312 4355)
#elif JUCE_LINUX
 #include <float.h>
 #include <sys/time.h>
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 #include <X11/Xatom.h>
 #undef Font
 #undef KeyPress
 #undef Drawable
 #undef Time
#else
 #ifndef JUCE_MAC_VST_INCLUDED
  // On the mac, this file needs to be compiled indirectly, by using
  // juce_VSTPluginFormat.mm instead - that wraps it as an objective-C file for cocoa
  #error
 #endif
 #include <Cocoa/Cocoa.h>
 #include <Carbon/Carbon.h>
#endif

#if ! (JUCE_MAC && JUCE_64BIT)

BEGIN_JUCE_NAMESPACE

#if JUCE_MAC && JUCE_SUPPORT_CARBON

#endif

#undef PRAGMA_ALIGN_SUPPORTED
#define VST_FORCE_DEPRECATED 0

#ifdef _MSC_VER
  #pragma warning (push)
  #pragma warning (disable: 4996)
#endif

#include "pluginterfaces/vst2.x/aeffectx.h"

#ifdef _MSC_VER
  #pragma warning (pop)
#endif

#if JUCE_LINUX
 #define Font	   JUCE_NAMESPACE::Font
 #define KeyPress   JUCE_NAMESPACE::KeyPress
 #define Drawable   JUCE_NAMESPACE::Drawable
 #define Time	   JUCE_NAMESPACE::Time
#endif


/*** Start of inlined file: juce_VSTMidiEventList.h ***/
#ifdef __aeffect__

#ifndef __JUCE_VSTMIDIEVENTLIST_JUCEHEADER__
#define __JUCE_VSTMIDIEVENTLIST_JUCEHEADER__

class VSTMidiEventList
{
public:

	VSTMidiEventList()
		: numEventsUsed (0), numEventsAllocated (0)
	{
	}

	~VSTMidiEventList()
	{
		freeEvents();
	}

	void clear()
	{
		numEventsUsed = 0;

		if (events != 0)
			events->numEvents = 0;
	}

	void addEvent (const void* const midiData, const int numBytes, const int frameOffset)
	{
		ensureSize (numEventsUsed + 1);

		VstMidiEvent* const e = (VstMidiEvent*) (events->events [numEventsUsed]);
		events->numEvents = ++numEventsUsed;

		if (numBytes <= 4)
		{
			if (e->type == kVstSysExType)
			{
				juce_free (((VstMidiSysexEvent*) e)->sysexDump);
				e->type = kVstMidiType;
				e->byteSize = sizeof (VstMidiEvent);
				e->noteLength = 0;
				e->noteOffset = 0;
				e->detune = 0;
				e->noteOffVelocity = 0;
			}

			e->deltaFrames = frameOffset;
			memcpy (e->midiData, midiData, numBytes);
		}
		else
		{
			VstMidiSysexEvent* const se = (VstMidiSysexEvent*) e;

			if (se->type == kVstSysExType)
				se->sysexDump = (char*) juce_realloc (se->sysexDump, numBytes);
			else
				se->sysexDump = (char*) juce_malloc (numBytes);

			memcpy (se->sysexDump, midiData, numBytes);

			se->type = kVstSysExType;
			se->byteSize = sizeof (VstMidiSysexEvent);
			se->deltaFrames = frameOffset;
			se->flags = 0;
			se->dumpBytes = numBytes;
			se->resvd1 = 0;
			se->resvd2 = 0;
		}
	}

	// Handy method to pull the events out of an event buffer supplied by the host
	// or plugin.
	static void addEventsToMidiBuffer (const VstEvents* events, MidiBuffer& dest)
	{
		for (int i = 0; i < events->numEvents; ++i)
		{
			const VstEvent* const e = events->events[i];

			if (e != 0)
			{
				if (e->type == kVstMidiType)
				{
					dest.addEvent ((const JUCE_NAMESPACE::uint8*) ((const VstMidiEvent*) e)->midiData,
								   4, e->deltaFrames);
				}
				else if (e->type == kVstSysExType)
				{
					dest.addEvent ((const JUCE_NAMESPACE::uint8*) ((const VstMidiSysexEvent*) e)->sysexDump,
								   (int) ((const VstMidiSysexEvent*) e)->dumpBytes,
								   e->deltaFrames);
				}
			}
		}
	}

	void ensureSize (int numEventsNeeded)
	{
		if (numEventsNeeded > numEventsAllocated)
		{
			numEventsNeeded = (numEventsNeeded + 32) & ~31;

			const int size = 20 + sizeof (VstEvent*) * numEventsNeeded;

			if (events == 0)
				events.calloc (size, 1);
			else
				events.realloc (size, 1);

			for (int i = numEventsAllocated; i < numEventsNeeded; ++i)
			{
				VstMidiEvent* const e = (VstMidiEvent*) juce_calloc (jmax ((int) sizeof (VstMidiEvent),
																		   (int) sizeof (VstMidiSysexEvent)));
				e->type = kVstMidiType;
				e->byteSize = sizeof (VstMidiEvent);

				events->events[i] = (VstEvent*) e;
			}

			numEventsAllocated = numEventsNeeded;
		}
	}

	void freeEvents()
	{
		if (events != 0)
		{
			for (int i = numEventsAllocated; --i >= 0;)
			{
				VstMidiEvent* const e = (VstMidiEvent*) (events->events[i]);

				if (e->type == kVstSysExType)
					juce_free (((VstMidiSysexEvent*) e)->sysexDump);

				juce_free (e);
			}

			events.free();
			numEventsUsed = 0;
			numEventsAllocated = 0;
		}
	}

	HeapBlock <VstEvents> events;

private:
	int numEventsUsed, numEventsAllocated;
};

#endif   // __JUCE_VSTMIDIEVENTLIST_JUCEHEADER__
#endif   // __JUCE_VSTMIDIEVENTLIST_JUCEHEADER__
/*** End of inlined file: juce_VSTMidiEventList.h ***/

#if ! JUCE_WIN32
 static void _fpreset() {}
 static void _clearfp() {}
#endif

extern void juce_callAnyTimersSynchronously();

const int fxbVersionNum = 1;

struct fxProgram
{
	long chunkMagic;	// 'CcnK'
	long byteSize;	  // of this chunk, excl. magic + byteSize
	long fxMagic;	   // 'FxCk'
	long version;
	long fxID;		  // fx unique id
	long fxVersion;
	long numParams;
	char prgName[28];
	float params[1];	// variable no. of parameters
};

struct fxSet
{
	long chunkMagic;	// 'CcnK'
	long byteSize;	  // of this chunk, excl. magic + byteSize
	long fxMagic;	   // 'FxBk'
	long version;
	long fxID;		  // fx unique id
	long fxVersion;
	long numPrograms;
	char future[128];
	fxProgram programs[1];  // variable no. of programs
};

struct fxChunkSet
{
	long chunkMagic;	// 'CcnK'
	long byteSize;	  // of this chunk, excl. magic + byteSize
	long fxMagic;	   // 'FxCh', 'FPCh', or 'FBCh'
	long version;
	long fxID;		  // fx unique id
	long fxVersion;
	long numPrograms;
	char future[128];
	long chunkSize;
	char chunk[8];	  // variable
};

struct fxProgramSet
{
	long chunkMagic;	// 'CcnK'
	long byteSize;	  // of this chunk, excl. magic + byteSize
	long fxMagic;	   // 'FxCh', 'FPCh', or 'FBCh'
	long version;
	long fxID;		  // fx unique id
	long fxVersion;
	long numPrograms;
	char name[28];
	long chunkSize;
	char chunk[8];	  // variable
};

static long vst_swap (const long x) throw()
{
  #ifdef JUCE_LITTLE_ENDIAN
	return (long) ByteOrder::swap ((uint32) x);
  #else
	return x;
  #endif
}

static float vst_swapFloat (const float x) throw()
{
  #ifdef JUCE_LITTLE_ENDIAN
	union { uint32 asInt; float asFloat; } n;
	n.asFloat = x;
	n.asInt = ByteOrder::swap (n.asInt);
	return n.asFloat;
  #else
	return x;
  #endif
}

typedef AEffect* (*MainCall) (audioMasterCallback);

static VstIntPtr VSTCALLBACK audioMaster (AEffect* effect, VstInt32 opcode, VstInt32 index, VstIntPtr value, void* ptr, float opt);

static int shellUIDToCreate = 0;
static int insideVSTCallback = 0;

class VSTPluginWindow;

// Change this to disable logging of various VST activities
#ifndef VST_LOGGING
  #define VST_LOGGING 1
#endif

#if VST_LOGGING
 #define log(a) Logger::writeToLog(a);
#else
 #define log(a)
#endif

#if JUCE_MAC && JUCE_PPC
static void* NewCFMFromMachO (void* const machofp) throw()
{
	void* result = juce_malloc (8);

	((void**) result)[0] = machofp;
	((void**) result)[1] = result;

	return result;
}
#endif

#if JUCE_LINUX

extern Display* display;
extern XContext improbableNumber;

typedef void (*EventProcPtr) (XEvent* ev);

static bool xErrorTriggered;

static int temporaryErrorHandler (Display*, XErrorEvent*)
{
	xErrorTriggered = true;
	return 0;
}

static int getPropertyFromXWindow (Window handle, Atom atom)
{
	XErrorHandler oldErrorHandler = XSetErrorHandler (temporaryErrorHandler);
	xErrorTriggered = false;

	int userSize;
	unsigned long bytes, userCount;
	unsigned char* data;
	Atom userType;

	XGetWindowProperty (display, handle, atom, 0, 1, false, AnyPropertyType,
						&userType,  &userSize, &userCount, &bytes, &data);

	XSetErrorHandler (oldErrorHandler);

	return (userCount == 1 && ! xErrorTriggered) ? *(int*) data
												 : 0;
}

static Window getChildWindow (Window windowToCheck)
{
	Window rootWindow, parentWindow;
	Window* childWindows;
	unsigned int numChildren;

	XQueryTree (display,
				windowToCheck,
				&rootWindow,
				&parentWindow,
				&childWindows,
				&numChildren);

	if (numChildren > 0)
		return childWindows [0];

	return 0;
}

static void translateJuceToXButtonModifiers (const MouseEvent& e, XEvent& ev) throw()
{
	if (e.mods.isLeftButtonDown())
	{
		ev.xbutton.button = Button1;
		ev.xbutton.state |= Button1Mask;
	}
	else if (e.mods.isRightButtonDown())
	{
		ev.xbutton.button = Button3;
		ev.xbutton.state |= Button3Mask;
	}
	else if (e.mods.isMiddleButtonDown())
	{
		ev.xbutton.button = Button2;
		ev.xbutton.state |= Button2Mask;
	}
}

static void translateJuceToXMotionModifiers (const MouseEvent& e, XEvent& ev) throw()
{
	if (e.mods.isLeftButtonDown())
		ev.xmotion.state |= Button1Mask;
	else if (e.mods.isRightButtonDown())
		ev.xmotion.state |= Button3Mask;
	else if (e.mods.isMiddleButtonDown())
		ev.xmotion.state |= Button2Mask;
}

static void translateJuceToXCrossingModifiers (const MouseEvent& e, XEvent& ev) throw()
{
	if (e.mods.isLeftButtonDown())
		ev.xcrossing.state |= Button1Mask;
	else if (e.mods.isRightButtonDown())
		ev.xcrossing.state |= Button3Mask;
	else if (e.mods.isMiddleButtonDown())
		ev.xcrossing.state |= Button2Mask;
}

static void translateJuceToXMouseWheelModifiers (const MouseEvent& e, const float increment, XEvent& ev) throw()
{
	if (increment < 0)
	{
		ev.xbutton.button = Button5;
		ev.xbutton.state |= Button5Mask;
	}
	else if (increment > 0)
	{
		ev.xbutton.button = Button4;
		ev.xbutton.state |= Button4Mask;
	}
}

#endif

static VoidArray activeModules;

class ModuleHandle	: public ReferenceCountedObject
{
public:

	File file;
	MainCall moduleMain;
	String pluginName;

	static ModuleHandle* findOrCreateModule (const File& file)
	{
		for (int i = activeModules.size(); --i >= 0;)
		{
			ModuleHandle* const module = (ModuleHandle*) activeModules.getUnchecked(i);

			if (module->file == file)
				return module;
		}

		_fpreset(); // (doesn't do any harm)
		++insideVSTCallback;
		shellUIDToCreate = 0;

		log ("Attempting to load VST: " + file.getFullPathName());

		ScopedPointer <ModuleHandle> m (new ModuleHandle (file));

		if (! m->open())
			m = 0;

		--insideVSTCallback;
		_fpreset(); // (doesn't do any harm)

		return m.release();
	}

	ModuleHandle (const File& file_)
		: file (file_),
		  moduleMain (0),
#if JUCE_WIN32 || JUCE_LINUX
		  hModule (0)
#elif JUCE_MAC
		  fragId (0),
		  resHandle (0),
		  bundleRef (0),
		  resFileId (0)
#endif
	{
		activeModules.add (this);

#if JUCE_WIN32 || JUCE_LINUX
		fullParentDirectoryPathName = file_.getParentDirectory().getFullPathName();
#elif JUCE_MAC
		FSRef ref;
		PlatformUtilities::makeFSRefFromPath (&ref, file_.getParentDirectory().getFullPathName());
		FSGetCatalogInfo (&ref, kFSCatInfoNone, 0, 0, &parentDirFSSpec, 0);
#endif
	}

	~ModuleHandle()
	{
		activeModules.removeValue (this);

		close();
	}

	juce_UseDebuggingNewOperator

#if JUCE_WIN32 || JUCE_LINUX
	void* hModule;
	String fullParentDirectoryPathName;

	bool open()
	{
#if JUCE_WIN32
		static bool timePeriodSet = false;

		if (! timePeriodSet)
		{
			timePeriodSet = true;
			timeBeginPeriod (2);
		}
#endif

		pluginName = file.getFileNameWithoutExtension();

		hModule = PlatformUtilities::loadDynamicLibrary (file.getFullPathName());

		moduleMain = (MainCall) PlatformUtilities::getProcedureEntryPoint (hModule, "VSTPluginMain");

		if (moduleMain == 0)
			moduleMain = (MainCall) PlatformUtilities::getProcedureEntryPoint (hModule, "main");

		return moduleMain != 0;
	}

	void close()
	{
		_fpreset(); // (doesn't do any harm)

		PlatformUtilities::freeDynamicLibrary (hModule);
	}

	void closeEffect (AEffect* eff)
	{
		eff->dispatcher (eff, effClose, 0, 0, 0, 0);
	}

#else
	CFragConnectionID fragId;
	Handle resHandle;
	CFBundleRef bundleRef;
	FSSpec parentDirFSSpec;
	short resFileId;

	bool open()
	{
		bool ok = false;
		const String filename (file.getFullPathName());

		if (file.hasFileExtension (".vst"))
		{
			const char* const utf8 = filename.toUTF8();
			CFURLRef url = CFURLCreateFromFileSystemRepresentation (0, (const UInt8*) utf8,
																	strlen (utf8), file.isDirectory());

			if (url != 0)
			{
				bundleRef = CFBundleCreate (kCFAllocatorDefault, url);
				CFRelease (url);

				if (bundleRef != 0)
				{
					if (CFBundleLoadExecutable (bundleRef))
					{
						moduleMain = (MainCall) CFBundleGetFunctionPointerForName (bundleRef, CFSTR("main_macho"));

						if (moduleMain == 0)
							moduleMain = (MainCall) CFBundleGetFunctionPointerForName (bundleRef, CFSTR("VSTPluginMain"));

						if (moduleMain != 0)
						{
							CFTypeRef name = CFBundleGetValueForInfoDictionaryKey (bundleRef, CFSTR("CFBundleName"));

							if (name != 0)
							{
								if (CFGetTypeID (name) == CFStringGetTypeID())
								{
									char buffer[1024];

									if (CFStringGetCString ((CFStringRef) name, buffer, sizeof (buffer), CFStringGetSystemEncoding()))
										pluginName = buffer;
								}
							}

							if (pluginName.isEmpty())
								pluginName = file.getFileNameWithoutExtension();

							resFileId = CFBundleOpenBundleResourceMap (bundleRef);

							ok = true;
						}
					}

					if (! ok)
					{
						CFBundleUnloadExecutable (bundleRef);
						CFRelease (bundleRef);
						bundleRef = 0;
					}
				}
			}
		}
#if JUCE_PPC
		else
		{
			FSRef fn;

			if (FSPathMakeRef ((UInt8*) filename.toUTF8(), &fn, 0) == noErr)
			{
				resFileId = FSOpenResFile (&fn, fsRdPerm);

				if (resFileId != -1)
				{
					const int numEffs = Count1Resources ('aEff');

					for (int i = 0; i < numEffs; ++i)
					{
						resHandle = Get1IndResource ('aEff', i + 1);

						if (resHandle != 0)
						{
							OSType type;
							Str255 name;
							SInt16 id;
							GetResInfo (resHandle, &id, &type, name);
							pluginName = String ((const char*) name + 1, name[0]);
							DetachResource (resHandle);
							HLock (resHandle);

							Ptr ptr;
							Str255 errorText;

							OSErr err = GetMemFragment (*resHandle, GetHandleSize (resHandle),
														name, kPrivateCFragCopy,
														&fragId, &ptr, errorText);

							if (err == noErr)
							{
								moduleMain = (MainCall) newMachOFromCFM (ptr);
								ok = true;
							}
							else
							{
								HUnlock (resHandle);
							}

							break;
						}
					}

					if (! ok)
						CloseResFile (resFileId);
				}
			}
		}
#endif

		return ok;
	}

	void close()
	{
#if JUCE_PPC
		if (fragId != 0)
		{
			if (moduleMain != 0)
				disposeMachOFromCFM ((void*) moduleMain);

			CloseConnection (&fragId);
			HUnlock (resHandle);

			if (resFileId != 0)
				CloseResFile (resFileId);
		}
		else
#endif
		if (bundleRef != 0)
		{
			CFBundleCloseBundleResourceMap (bundleRef, resFileId);

			if (CFGetRetainCount (bundleRef) == 1)
				CFBundleUnloadExecutable (bundleRef);

			if (CFGetRetainCount (bundleRef) > 0)
				CFRelease (bundleRef);
		}
	}

	void closeEffect (AEffect* eff)
	{
#if JUCE_PPC
		if (fragId != 0)
		{
			VoidArray thingsToDelete;
			thingsToDelete.add ((void*) eff->dispatcher);
			thingsToDelete.add ((void*) eff->process);
			thingsToDelete.add ((void*) eff->setParameter);
			thingsToDelete.add ((void*) eff->getParameter);
			thingsToDelete.add ((void*) eff->processReplacing);

			eff->dispatcher (eff, effClose, 0, 0, 0, 0);

			for (int i = thingsToDelete.size(); --i >= 0;)
				disposeMachOFromCFM (thingsToDelete[i]);
		}
		else
#endif
		{
			eff->dispatcher (eff, effClose, 0, 0, 0, 0);
		}
	}

#if JUCE_PPC
	static void* newMachOFromCFM (void* cfmfp)
	{
		if (cfmfp == 0)
			return 0;

		UInt32* const mfp = (UInt32*) juce_malloc (sizeof (UInt32) * 6);

		mfp[0] = 0x3d800000 | ((UInt32) cfmfp >> 16);
		mfp[1] = 0x618c0000 | ((UInt32) cfmfp & 0xffff);
		mfp[2] = 0x800c0000;
		mfp[3] = 0x804c0004;
		mfp[4] = 0x7c0903a6;
		mfp[5] = 0x4e800420;

		MakeDataExecutable (mfp, sizeof (UInt32) * 6);
		return mfp;
	}

	static void disposeMachOFromCFM (void* ptr)
	{
		juce_free (ptr);
	}

	void coerceAEffectFunctionCalls (AEffect* eff)
	{
		if (fragId != 0)
		{
			eff->dispatcher = (AEffectDispatcherProc) newMachOFromCFM ((void*) eff->dispatcher);
			eff->process = (AEffectProcessProc) newMachOFromCFM ((void*) eff->process);
			eff->setParameter = (AEffectSetParameterProc) newMachOFromCFM ((void*) eff->setParameter);
			eff->getParameter = (AEffectGetParameterProc) newMachOFromCFM ((void*) eff->getParameter);
			eff->processReplacing = (AEffectProcessProc) newMachOFromCFM ((void*) eff->processReplacing);
		}
	}
#endif

#endif
};

class VSTPluginInstance	 : public AudioPluginInstance,
							  private Timer,
							  private AsyncUpdater
{
public:

	~VSTPluginInstance();

	// AudioPluginInstance methods:

	void fillInPluginDescription (PluginDescription& desc) const
	{
		desc.name = name;
		desc.fileOrIdentifier = module->file.getFullPathName();
		desc.uid = getUID();
		desc.lastFileModTime = module->file.getLastModificationTime();
		desc.pluginFormatName = "VST";
		desc.category = getCategory();

		{
			char buffer [kVstMaxVendorStrLen + 8];
			zerostruct (buffer);
			dispatch (effGetVendorString, 0, 0, buffer, 0);
			desc.manufacturerName = buffer;
		}

		desc.version = getVersion();
		desc.numInputChannels = getNumInputChannels();
		desc.numOutputChannels = getNumOutputChannels();
		desc.isInstrument = (effect != 0 && (effect->flags & effFlagsIsSynth) != 0);
	}

	const String getName() const		{ return name; }
	int getUID() const throw();
	bool acceptsMidi() const		{ return wantsMidiMessages; }
	bool producesMidi() const               { return dispatch (effCanDo, 0, 0, (void*) "sendVstMidiEvent", 0) > 0; }

	// AudioProcessor methods:

	void prepareToPlay (double sampleRate, int estimatedSamplesPerBlock);
	void releaseResources();
	void processBlock (AudioSampleBuffer& buffer,
					   MidiBuffer& midiMessages);

	AudioProcessorEditor* createEditor();

	const String getInputChannelName (const int index) const;
	bool isInputChannelStereoPair (int index) const;

	const String getOutputChannelName (const int index) const;
	bool isOutputChannelStereoPair (int index) const;

	int getNumParameters()				  { return effect != 0 ? effect->numParams : 0; }
	float getParameter (int index);
	void setParameter (int index, float newValue);
	const String getParameterName (int index);
	const String getParameterText (int index);
	bool isParameterAutomatable (int index) const;

	int getNumPrograms()				{ return effect != 0 ? effect->numPrograms : 0; }
	int getCurrentProgram()				 { return dispatch (effGetProgram, 0, 0, 0, 0); }
	void setCurrentProgram (int index);
	const String getProgramName (int index);
	void changeProgramName (int index, const String& newName);

	void getStateInformation (MemoryBlock& destData);
	void getCurrentProgramStateInformation (MemoryBlock& destData);
	void setStateInformation (const void* data, int sizeInBytes);
	void setCurrentProgramStateInformation (const void* data, int sizeInBytes);

	void timerCallback();
	void handleAsyncUpdate();
	VstIntPtr handleCallback (VstInt32 opcode, VstInt32 index, VstInt32 value, void *ptr, float opt);

	juce_UseDebuggingNewOperator

private:
	friend class VSTPluginWindow;
	friend class VSTPluginFormat;

	AEffect* effect;
	String name;
	CriticalSection lock;
	bool wantsMidiMessages, initialised, isPowerOn;
	mutable StringArray programNames;
	AudioSampleBuffer tempBuffer;
	CriticalSection midiInLock;
	MidiBuffer incomingMidi;
	VSTMidiEventList midiEventsToSend;
	VstTimeInfo vstHostTime;
	HeapBlock <float*> channels;

	ReferenceCountedObjectPtr <ModuleHandle> module;

	int dispatch (const int opcode, const int index, const int value, void* const ptr, float opt) const;
	bool restoreProgramSettings (const fxProgram* const prog);
	const String getCurrentProgramName();
	void setParamsInProgramBlock (fxProgram* const prog) throw();
	void updateStoredProgramNames();
	void initialise();
	void handleMidiFromPlugin (const VstEvents* const events);
	void createTempParameterStore (MemoryBlock& dest);
	void restoreFromTempParameterStore (const MemoryBlock& mb);
	const String getParameterLabel (int index) const;

	bool usesChunks() const throw()	 { return effect != 0 && (effect->flags & effFlagsProgramChunks) != 0; }
	void getChunkData (MemoryBlock& mb, bool isPreset, int maxSizeMB) const;
	void setChunkData (const char* data, int size, bool isPreset);
	bool loadFromFXBFile (const void* data, int numBytes);
	bool saveToFXBFile (MemoryBlock& dest, bool isFXB, int maxSizeMB);

	int getVersionNumber() const throw()	{ return effect != 0 ? effect->version : 0; }
	const String getVersion() const throw();
	const String getCategory() const throw();

	bool hasEditor() const throw()	  { return effect != 0 && (effect->flags & effFlagsHasEditor) != 0; }
	void setPower (const bool on);

	VSTPluginInstance (const ReferenceCountedObjectPtr <ModuleHandle>& module);
};

VSTPluginInstance::VSTPluginInstance (const ReferenceCountedObjectPtr <ModuleHandle>& module_)
	: effect (0),
	  wantsMidiMessages (false),
	  initialised (false),
	  isPowerOn (false),
	  tempBuffer (1, 1),
	  module (module_)
{
	try
	{
		_fpreset();

		++insideVSTCallback;

		name = module->pluginName;
		log ("Creating VST instance: " + name);

#if JUCE_MAC
		if (module->resFileId != 0)
			UseResFile (module->resFileId);

#if JUCE_PPC
		if (module->fragId != 0)
		{
			static void* audioMasterCoerced = 0;
			if (audioMasterCoerced == 0)
				audioMasterCoerced = NewCFMFromMachO ((void*) &audioMaster);

			effect = module->moduleMain ((audioMasterCallback) audioMasterCoerced);
		}
		else
#endif
#endif
		{
			effect = module->moduleMain (&audioMaster);
		}

		--insideVSTCallback;

		if (effect != 0 && effect->magic == kEffectMagic)
		{
#if JUCE_PPC
			module->coerceAEffectFunctionCalls (effect);
#endif

			jassert (effect->resvd2 == 0);
			jassert (effect->object != 0);

			_fpreset(); // some dodgy plugs fuck around with this
		}
		else
		{
			effect = 0;
		}
	}
	catch (...)
	{
		--insideVSTCallback;
	}
}

VSTPluginInstance::~VSTPluginInstance()
{
	{
		const ScopedLock sl (lock);

		jassert (insideVSTCallback == 0);

		if (effect != 0 && effect->magic == kEffectMagic)
		{
			try
			{
#if JUCE_MAC
				if (module->resFileId != 0)
					UseResFile (module->resFileId);
#endif

				// Must delete any editors before deleting the plugin instance!
				jassert (getActiveEditor() == 0);

				_fpreset(); // some dodgy plugs fuck around with this

				module->closeEffect (effect);
			}
			catch (...)
			{}
		}

		module = 0;
		effect = 0;
	}
}

void VSTPluginInstance::initialise()
{
	if (initialised || effect == 0)
		return;

	log ("Initialising VST: " + module->pluginName);
	initialised = true;

	dispatch (effIdentify, 0, 0, 0, 0);

	// this code would ask the plugin for its name, but so few plugins
	// actually bother implementing this correctly, that it's better to
	// just ignore it and use the file name instead.

	if (getSampleRate() > 0)
		dispatch (effSetSampleRate, 0, 0, 0, (float) getSampleRate());

	if (getBlockSize() > 0)
		dispatch (effSetBlockSize, 0, jmax (32, getBlockSize()), 0, 0);

	dispatch (effOpen, 0, 0, 0, 0);

	setPlayConfigDetails (effect->numInputs, effect->numOutputs,
						  getSampleRate(), getBlockSize());

	if (getNumPrograms() > 1)
		setCurrentProgram (0);
	else
		dispatch (effSetProgram, 0, 0, 0, 0);

	int i;
	for (i = effect->numInputs; --i >= 0;)
		dispatch (effConnectInput, i, 1, 0, 0);

	for (i = effect->numOutputs; --i >= 0;)
		dispatch (effConnectOutput, i, 1, 0, 0);

	updateStoredProgramNames();

	wantsMidiMessages = dispatch (effCanDo, 0, 0, (void*) "receiveVstMidiEvent", 0) > 0;

	setLatencySamples (effect->initialDelay);
}

void VSTPluginInstance::prepareToPlay (double sampleRate_,
									   int samplesPerBlockExpected)
{
	setPlayConfigDetails (effect->numInputs, effect->numOutputs,
						  sampleRate_, samplesPerBlockExpected);

	setLatencySamples (effect->initialDelay);

	channels.calloc (jmax (16, getNumOutputChannels(), getNumInputChannels()) + 2);

	vstHostTime.tempo = 120.0;
	vstHostTime.timeSigNumerator = 4;
	vstHostTime.timeSigDenominator = 4;
	vstHostTime.sampleRate = sampleRate_;
	vstHostTime.samplePos = 0;
	vstHostTime.flags = kVstNanosValid;  /*| kVstTransportPlaying | kVstTempoValid | kVstTimeSigValid*/;

	initialise();

	if (initialised)
	{
		wantsMidiMessages = wantsMidiMessages
								|| (dispatch (effCanDo, 0, 0, (void*) "receiveVstMidiEvent", 0) > 0);

		if (wantsMidiMessages)
			midiEventsToSend.ensureSize (256);
		else
			midiEventsToSend.freeEvents();

		incomingMidi.clear();

		dispatch (effSetSampleRate, 0, 0, 0, (float) sampleRate_);
		dispatch (effSetBlockSize, 0, jmax (16, samplesPerBlockExpected), 0, 0);

		tempBuffer.setSize (jmax (1, effect->numOutputs), samplesPerBlockExpected);

		if (! isPowerOn)
			setPower (true);

		// dodgy hack to force some plugins to initialise the sample rate..
		if ((! hasEditor()) && getNumParameters() > 0)
		{
			const float old = getParameter (0);
			setParameter (0, (old < 0.5f) ? 1.0f : 0.0f);
			setParameter (0, old);
		}

		dispatch (effStartProcess, 0, 0, 0, 0);
	}
}

void VSTPluginInstance::releaseResources()
{
	if (initialised)
	{
		dispatch (effStopProcess, 0, 0, 0, 0);
		setPower (false);
	}

	tempBuffer.setSize (1, 1);
	incomingMidi.clear();

	midiEventsToSend.freeEvents();
	channels.free();
}

void VSTPluginInstance::processBlock (AudioSampleBuffer& buffer,
									  MidiBuffer& midiMessages)
{
	const int numSamples = buffer.getNumSamples();

	if (initialised)
	{
		AudioPlayHead* playHead = getPlayHead();

		if (playHead != 0)
		{
			AudioPlayHead::CurrentPositionInfo position;
			playHead->getCurrentPosition (position);

			vstHostTime.tempo = position.bpm;
			vstHostTime.timeSigNumerator = position.timeSigNumerator;
			vstHostTime.timeSigDenominator = position.timeSigDenominator;
			vstHostTime.ppqPos = position.ppqPosition;
			vstHostTime.barStartPos = position.ppqPositionOfLastBarStart;
			vstHostTime.flags |= kVstTempoValid | kVstTimeSigValid | kVstPpqPosValid | kVstBarsValid;

			if (position.isPlaying)
				vstHostTime.flags |= kVstTransportPlaying;
			else
				vstHostTime.flags &= ~kVstTransportPlaying;
		}

#if JUCE_WIN32
		vstHostTime.nanoSeconds = timeGetTime() * 1000000.0;
#elif JUCE_LINUX
		timeval micro;
		gettimeofday (&micro, 0);
		vstHostTime.nanoSeconds = micro.tv_usec * 1000.0;
#elif JUCE_MAC
		UnsignedWide micro;
		Microseconds (&micro);
		vstHostTime.nanoSeconds = micro.lo * 1000.0;
#endif

		if (wantsMidiMessages)
		{
			midiEventsToSend.clear();
			midiEventsToSend.ensureSize (1);

			MidiBuffer::Iterator iter (midiMessages);
			const uint8* midiData;
			int numBytesOfMidiData, samplePosition;

			while (iter.getNextEvent (midiData, numBytesOfMidiData, samplePosition))
			{
				midiEventsToSend.addEvent (midiData, numBytesOfMidiData,
										   jlimit (0, numSamples - 1, samplePosition));
			}

			try
			{
				effect->dispatcher (effect, effProcessEvents, 0, 0, midiEventsToSend.events, 0);
			}
			catch (...)
			{}
		}

		int i;
		const int maxChans = jmax (effect->numInputs, effect->numOutputs);

		for (i = 0; i < maxChans; ++i)
			channels[i] = buffer.getSampleData (i);

		channels [maxChans] = 0;

		_clearfp();

		if ((effect->flags & effFlagsCanReplacing) != 0)
		{
			try
			{
				effect->processReplacing (effect, channels, channels, numSamples);
			}
			catch (...)
			{}
		}
		else
		{
			tempBuffer.setSize (effect->numOutputs, numSamples);
			tempBuffer.clear();

			float* outs [64];

			for (i = effect->numOutputs; --i >= 0;)
				outs[i] = tempBuffer.getSampleData (i);

			outs [effect->numOutputs] = 0;

			try
			{
				effect->process (effect, channels, outs, numSamples);
			}
			catch (...)
			{}

			for (i = effect->numOutputs; --i >= 0;)
				buffer.copyFrom (i, 0, outs[i], numSamples);
		}
	}
	else
	{
		// Not initialised, so just bypass..
		for (int i = getNumInputChannels(); i < getNumOutputChannels(); ++i)
			buffer.clear (i, 0, buffer.getNumSamples());
	}

	{
		// copy any incoming midi..
		const ScopedLock sl (midiInLock);

		midiMessages.swapWith (incomingMidi);
		incomingMidi.clear();
	}
}

void VSTPluginInstance::handleMidiFromPlugin (const VstEvents* const events)
{
	if (events != 0)
	{
		const ScopedLock sl (midiInLock);
		VSTMidiEventList::addEventsToMidiBuffer (events, incomingMidi);
	}
}

static Array <VSTPluginWindow*> activeVSTWindows;

class VSTPluginWindow   : public AudioProcessorEditor,
						  #if ! JUCE_MAC
						  public ComponentMovementWatcher,
						  #endif
						  public Timer
{
public:

	VSTPluginWindow (VSTPluginInstance& plugin_)
		: AudioProcessorEditor (&plugin_),
#if ! JUCE_MAC
		  ComponentMovementWatcher (this),
#endif
		  plugin (plugin_),
		  isOpen (false),
		  wasShowing (false),
		  pluginRefusesToResize (false),
		  pluginWantsKeys (false),
		  alreadyInside (false),
		  recursiveResize (false)
	{
#if JUCE_WIN32
		sizeCheckCount = 0;
		pluginHWND = 0;
#elif JUCE_LINUX
		pluginWindow = None;
		pluginProc = None;
#else
		addAndMakeVisible (innerWrapper = new InnerWrapperComponent (this));
#endif

		activeVSTWindows.add (this);

		setSize (1, 1);
		setOpaque (true);
		setVisible (true);
	}

	~VSTPluginWindow()
	{
#if JUCE_MAC
		innerWrapper = 0;
#else
		closePluginWindow();
#endif
		activeVSTWindows.removeValue (this);
		plugin.editorBeingDeleted (this);
	}

#if ! JUCE_MAC
	void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
	{
		if (recursiveResize)
			return;

		Component* const topComp = getTopLevelComponent();

		if (topComp->getPeer() != 0)
		{
			const Point<int> pos (relativePositionToOtherComponent (topComp, Point<int>()));

			recursiveResize = true;

#if JUCE_WIN32
			if (pluginHWND != 0)
				MoveWindow (pluginHWND, pos.getX(), pos.getY(), getWidth(), getHeight(), TRUE);
#elif JUCE_LINUX
			if (pluginWindow != 0)
			{
				XResizeWindow (display, pluginWindow, getWidth(), getHeight());
				XMoveWindow (display, pluginWindow, pos.getX(), pos.getY());
				XMapRaised (display, pluginWindow);
			}
#endif

			recursiveResize = false;
		}
	}

	void componentVisibilityChanged (Component&)
	{
		const bool isShowingNow = isShowing();

		if (wasShowing != isShowingNow)
		{
			wasShowing = isShowingNow;

			if (isShowingNow)
				openPluginWindow();
			else
				closePluginWindow();
		}

		componentMovedOrResized (true, true);
	}

	void componentPeerChanged()
	{
		closePluginWindow();
		openPluginWindow();
	}
#endif

	bool keyStateChanged (bool)
	{
		return pluginWantsKeys;
	}

	bool keyPressed (const KeyPress&)
	{
		return pluginWantsKeys;
	}

#if JUCE_MAC
	void paint (Graphics& g)
	{
		g.fillAll (Colours::black);
	}
#else
	void paint (Graphics& g)
	{
		if (isOpen)
		{
			ComponentPeer* const peer = getPeer();

			if (peer != 0)
			{
				const Point<int> pos (getScreenPosition() - peer->getScreenPosition());
				peer->addMaskedRegion (pos.getX(), pos.getY(), getWidth(), getHeight());

#if JUCE_LINUX
				if (pluginWindow != 0)
				{
					const Rectangle<int> clip (g.getClipBounds());

					XEvent ev;
					zerostruct (ev);
					ev.xexpose.type = Expose;
					ev.xexpose.display = display;
					ev.xexpose.window = pluginWindow;
					ev.xexpose.x = clip.getX();
					ev.xexpose.y = clip.getY();
					ev.xexpose.width = clip.getWidth();
					ev.xexpose.height = clip.getHeight();

					sendEventToChild (&ev);
				}
#endif
			}
		}
		else
		{
			g.fillAll (Colours::black);
		}
	}
#endif

	void timerCallback()
	{
#if JUCE_WIN32
		if (--sizeCheckCount <= 0)
		{
			sizeCheckCount = 10;

			checkPluginWindowSize();
		}
#endif

		try
		{
			static bool reentrant = false;

			if (! reentrant)
			{
				reentrant = true;
				plugin.dispatch (effEditIdle, 0, 0, 0, 0);
				reentrant = false;
			}
		}
		catch (...)
		{}
	}

	void mouseDown (const MouseEvent& e)
	{
#if JUCE_LINUX
		if (pluginWindow == 0)
			return;

		toFront (true);

		XEvent ev;
		zerostruct (ev);
		ev.xbutton.display = display;
		ev.xbutton.type = ButtonPress;
		ev.xbutton.window = pluginWindow;
		ev.xbutton.root = RootWindow (display, DefaultScreen (display));
		ev.xbutton.time = CurrentTime;
		ev.xbutton.x = e.x;
		ev.xbutton.y = e.y;
		ev.xbutton.x_root = e.getScreenX();
		ev.xbutton.y_root = e.getScreenY();

		translateJuceToXButtonModifiers (e, ev);

		sendEventToChild (&ev);

#elif JUCE_WIN32
		(void) e;

		toFront (true);
#endif
	}

	void broughtToFront()
	{
		activeVSTWindows.removeValue (this);
		activeVSTWindows.add (this);

#if JUCE_MAC
		dispatch (effEditTop, 0, 0, 0, 0);
#endif
	}

	juce_UseDebuggingNewOperator

private:
	VSTPluginInstance& plugin;
	bool isOpen, wasShowing, recursiveResize;
	bool pluginWantsKeys, pluginRefusesToResize, alreadyInside;

#if JUCE_WIN32
	HWND pluginHWND;
	void* originalWndProc;
	int sizeCheckCount;
#elif JUCE_LINUX
	Window pluginWindow;
	EventProcPtr pluginProc;
#endif

#if JUCE_MAC
	void openPluginWindow (WindowRef parentWindow)
	{
		if (isOpen || parentWindow == 0)
			return;

		isOpen = true;

		ERect* rect = 0;
		dispatch (effEditGetRect, 0, 0, &rect, 0);
		dispatch (effEditOpen, 0, 0, parentWindow, 0);

		// do this before and after like in the steinberg example
		dispatch (effEditGetRect, 0, 0, &rect, 0);
		dispatch (effGetProgram, 0, 0, 0, 0); // also in steinberg code

		// Install keyboard hooks
		pluginWantsKeys = (dispatch (effKeysRequired, 0, 0, 0, 0) == 0);

		// double-check it's not too tiny
		int w = 250, h = 150;

		if (rect != 0)
		{
			w = rect->right - rect->left;
			h = rect->bottom - rect->top;

			if (w == 0 || h == 0)
			{
				w = 250;
				h = 150;
			}
		}

		w = jmax (w, 32);
		h = jmax (h, 32);

		setSize (w, h);

		startTimer (18 + JUCE_NAMESPACE::Random::getSystemRandom().nextInt (5));
		repaint();
	}

#else
	void openPluginWindow()
	{
		if (isOpen || getWindowHandle() == 0)
			return;

		log ("Opening VST UI: " + plugin.name);
		isOpen = true;

		ERect* rect = 0;
		dispatch (effEditGetRect, 0, 0, &rect, 0);
		dispatch (effEditOpen, 0, 0, getWindowHandle(), 0);

		// do this before and after like in the steinberg example
		dispatch (effEditGetRect, 0, 0, &rect, 0);
		dispatch (effGetProgram, 0, 0, 0, 0); // also in steinberg code

		// Install keyboard hooks
		pluginWantsKeys = (dispatch (effKeysRequired, 0, 0, 0, 0) == 0);

#if JUCE_WIN32
		originalWndProc = 0;
		pluginHWND = GetWindow ((HWND) getWindowHandle(), GW_CHILD);

		if (pluginHWND == 0)
		{
			isOpen = false;
			setSize (300, 150);
			return;
		}

		#pragma warning (push)
		#pragma warning (disable: 4244)

		originalWndProc = (void*) GetWindowLongPtr (pluginHWND, GWL_WNDPROC);

		if (! pluginWantsKeys)
			SetWindowLongPtr (pluginHWND, GWLP_WNDPROC, (LONG_PTR) vstHookWndProc);

		#pragma warning (pop)

		int w, h;
		RECT r;
		GetWindowRect (pluginHWND, &r);
		w = r.right - r.left;
		h = r.bottom - r.top;

		if (rect != 0)
		{
			const int rw = rect->right - rect->left;
			const int rh = rect->bottom - rect->top;

			if ((rw > 50 && rh > 50 && rw < 2000 && rh < 2000 && rw != w && rh != h)
				|| ((w == 0 && rw > 0) || (h == 0 && rh > 0)))
			{
				// very dodgy logic to decide which size is right.
				if (abs (rw - w) > 350 || abs (rh - h) > 350)
				{
					SetWindowPos (pluginHWND, 0,
								  0, 0, rw, rh,
								  SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);

					GetWindowRect (pluginHWND, &r);

					w = r.right - r.left;
					h = r.bottom - r.top;

					pluginRefusesToResize = (w != rw) || (h != rh);

					w = rw;
					h = rh;
				}
			}
		}

#elif JUCE_LINUX
		pluginWindow = getChildWindow ((Window) getWindowHandle());

		if (pluginWindow != 0)
			pluginProc = (EventProcPtr) getPropertyFromXWindow (pluginWindow,
																XInternAtom (display, "_XEventProc", False));

		int w = 250, h = 150;

		if (rect != 0)
		{
			w = rect->right - rect->left;
			h = rect->bottom - rect->top;

			if (w == 0 || h == 0)
			{
				w = 250;
				h = 150;
			}
		}

		if (pluginWindow != 0)
			XMapRaised (display, pluginWindow);
#endif

		// double-check it's not too tiny
		w = jmax (w, 32);
		h = jmax (h, 32);

		setSize (w, h);

#if JUCE_WIN32
		checkPluginWindowSize();
#endif

		startTimer (18 + JUCE_NAMESPACE::Random::getSystemRandom().nextInt (5));
		repaint();
	}
#endif

#if ! JUCE_MAC
	void closePluginWindow()
	{
		if (isOpen)
		{
			log ("Closing VST UI: " + plugin.getName());
			isOpen = false;

			dispatch (effEditClose, 0, 0, 0, 0);

#if JUCE_WIN32
			#pragma warning (push)
			#pragma warning (disable: 4244)

			if (pluginHWND != 0 && IsWindow (pluginHWND))
				SetWindowLongPtr (pluginHWND, GWLP_WNDPROC, (LONG_PTR) originalWndProc);

			#pragma warning (pop)

			stopTimer();

			if (pluginHWND != 0 && IsWindow (pluginHWND))
				DestroyWindow (pluginHWND);

			pluginHWND = 0;
#elif JUCE_LINUX
			stopTimer();
			pluginWindow = 0;
			pluginProc = 0;
#endif
		}
	}
#endif

	int dispatch (const int opcode, const int index, const int value, void* const ptr, float opt)
	{
		return plugin.dispatch (opcode, index, value, ptr, opt);
	}

#if JUCE_WIN32
	void checkPluginWindowSize() throw()
	{
		RECT r;
		GetWindowRect (pluginHWND, &r);
		const int w = r.right - r.left;
		const int h = r.bottom - r.top;

		if (isShowing() && w > 0 && h > 0
			 && (w != getWidth() || h != getHeight())
			 && ! pluginRefusesToResize)
		{
			setSize (w, h);
			sizeCheckCount = 0;
		}
	}

	// hooks to get keyboard events from VST windows..
	static LRESULT CALLBACK vstHookWndProc (HWND hW, UINT message, WPARAM wParam, LPARAM lParam)
	{
		for (int i = activeVSTWindows.size(); --i >= 0;)
		{
			const VSTPluginWindow* const w = (const VSTPluginWindow*) activeVSTWindows.getUnchecked (i);

			if (w->pluginHWND == hW)
			{
				if (message == WM_CHAR
					|| message == WM_KEYDOWN
					|| message == WM_SYSKEYDOWN
					|| message == WM_KEYUP
					|| message == WM_SYSKEYUP
					|| message == WM_APPCOMMAND)
				{
					SendMessage ((HWND) w->getTopLevelComponent()->getWindowHandle(),
								 message, wParam, lParam);
				}

				return CallWindowProc ((WNDPROC) (w->originalWndProc),
									   (HWND) w->pluginHWND,
									   message,
									   wParam,
									   lParam);
			}
		}

		return DefWindowProc (hW, message, wParam, lParam);
	}
#endif

#if JUCE_LINUX

	// overload mouse/keyboard events to forward them to the plugin's inner window..
	void sendEventToChild (XEvent* event)
	{
		if (pluginProc != 0)
		{
			// if the plugin publishes an event procedure, pass the event directly..
			pluginProc (event);
		}
		else if (pluginWindow != 0)
		{
			// if the plugin has a window, then send the event to the window so that
			// its message thread will pick it up..
			XSendEvent (display, pluginWindow, False, 0L, event);
			XFlush (display);
		}
	}

	void mouseEnter (const MouseEvent& e)
	{
		if (pluginWindow != 0)
		{
			XEvent ev;
			zerostruct (ev);
			ev.xcrossing.display = display;
			ev.xcrossing.type = EnterNotify;
			ev.xcrossing.window = pluginWindow;
			ev.xcrossing.root = RootWindow (display, DefaultScreen (display));
			ev.xcrossing.time = CurrentTime;
			ev.xcrossing.x = e.x;
			ev.xcrossing.y = e.y;
			ev.xcrossing.x_root = e.getScreenX();
			ev.xcrossing.y_root = e.getScreenY();
			ev.xcrossing.mode = NotifyNormal; // NotifyGrab, NotifyUngrab
			ev.xcrossing.detail = NotifyAncestor; // NotifyVirtual, NotifyInferior, NotifyNonlinear,NotifyNonlinearVirtual

			translateJuceToXCrossingModifiers (e, ev);

			sendEventToChild (&ev);
		}
	}

	void mouseExit (const MouseEvent& e)
	{
		if (pluginWindow != 0)
		{
			XEvent ev;
			zerostruct (ev);
			ev.xcrossing.display = display;
			ev.xcrossing.type = LeaveNotify;
			ev.xcrossing.window = pluginWindow;
			ev.xcrossing.root = RootWindow (display, DefaultScreen (display));
			ev.xcrossing.time = CurrentTime;
			ev.xcrossing.x = e.x;
			ev.xcrossing.y = e.y;
			ev.xcrossing.x_root = e.getScreenX();
			ev.xcrossing.y_root = e.getScreenY();
			ev.xcrossing.mode = NotifyNormal; // NotifyGrab, NotifyUngrab
			ev.xcrossing.detail = NotifyAncestor; // NotifyVirtual, NotifyInferior, NotifyNonlinear,NotifyNonlinearVirtual
			ev.xcrossing.focus = hasKeyboardFocus (true); // TODO - yes ?

			translateJuceToXCrossingModifiers (e, ev);

			sendEventToChild (&ev);
		}
	}

	void mouseMove (const MouseEvent& e)
	{
		if (pluginWindow != 0)
		{
			XEvent ev;
			zerostruct (ev);
			ev.xmotion.display = display;
			ev.xmotion.type = MotionNotify;
			ev.xmotion.window = pluginWindow;
			ev.xmotion.root = RootWindow (display, DefaultScreen (display));
			ev.xmotion.time = CurrentTime;
			ev.xmotion.is_hint = NotifyNormal;
			ev.xmotion.x = e.x;
			ev.xmotion.y = e.y;
			ev.xmotion.x_root = e.getScreenX();
			ev.xmotion.y_root = e.getScreenY();

			sendEventToChild (&ev);
		}
	}

	void mouseDrag (const MouseEvent& e)
	{
		if (pluginWindow != 0)
		{
			XEvent ev;
			zerostruct (ev);
			ev.xmotion.display = display;
			ev.xmotion.type = MotionNotify;
			ev.xmotion.window = pluginWindow;
			ev.xmotion.root = RootWindow (display, DefaultScreen (display));
			ev.xmotion.time = CurrentTime;
			ev.xmotion.x = e.x ;
			ev.xmotion.y = e.y;
			ev.xmotion.x_root = e.getScreenX();
			ev.xmotion.y_root = e.getScreenY();
			ev.xmotion.is_hint = NotifyNormal;

			translateJuceToXMotionModifiers (e, ev);
			sendEventToChild (&ev);
		}
	}

	void mouseUp (const MouseEvent& e)
	{
		if (pluginWindow != 0)
		{
			XEvent ev;
			zerostruct (ev);
			ev.xbutton.display = display;
			ev.xbutton.type = ButtonRelease;
			ev.xbutton.window = pluginWindow;
			ev.xbutton.root = RootWindow (display, DefaultScreen (display));
			ev.xbutton.time = CurrentTime;
			ev.xbutton.x = e.x;
			ev.xbutton.y = e.y;
			ev.xbutton.x_root = e.getScreenX();
			ev.xbutton.y_root = e.getScreenY();

			translateJuceToXButtonModifiers (e, ev);
			sendEventToChild (&ev);
		}
	}

	void mouseWheelMove (const MouseEvent& e,
						 float incrementX,
						 float incrementY)
	{
		if (pluginWindow != 0)
		{
			XEvent ev;
			zerostruct (ev);
			ev.xbutton.display = display;
			ev.xbutton.type = ButtonPress;
			ev.xbutton.window = pluginWindow;
			ev.xbutton.root = RootWindow (display, DefaultScreen (display));
			ev.xbutton.time = CurrentTime;
			ev.xbutton.x = e.x;
			ev.xbutton.y = e.y;
			ev.xbutton.x_root = e.getScreenX();
			ev.xbutton.y_root = e.getScreenY();

			translateJuceToXMouseWheelModifiers (e, incrementY, ev);
			sendEventToChild (&ev);

			// TODO - put a usleep here ?

			ev.xbutton.type = ButtonRelease;
			sendEventToChild (&ev);
		}
	}
#endif

#if JUCE_MAC

#if ! JUCE_SUPPORT_CARBON
 #error "To build VSTs, you need to enable the JUCE_SUPPORT_CARBON flag in your config!"
#endif

	class InnerWrapperComponent   : public CarbonViewWrapperComponent
	{
	public:
		InnerWrapperComponent (VSTPluginWindow* const owner_)
			: owner (owner_),
			  alreadyInside (false)
		{
		}

		~InnerWrapperComponent()
		{
			deleteWindow();
		}

		HIViewRef attachView (WindowRef windowRef, HIViewRef rootView)
		{
			owner->openPluginWindow (windowRef);
			return 0;
		}

		void removeView (HIViewRef)
		{
			owner->dispatch (effEditClose, 0, 0, 0, 0);
			owner->dispatch (effEditSleep, 0, 0, 0, 0);
		}

		bool getEmbeddedViewSize (int& w, int& h)
		{
			ERect* rect = 0;
			owner->dispatch (effEditGetRect, 0, 0, &rect, 0);
			w = rect->right - rect->left;
			h = rect->bottom - rect->top;
			return true;
		}

		void mouseDown (int x, int y)
		{
			if (! alreadyInside)
			{
				alreadyInside = true;
				getTopLevelComponent()->toFront (true);
				owner->dispatch (effEditMouse, x, y, 0, 0);
				alreadyInside = false;
			}
			else
			{
				PostEvent (::mouseDown, 0);
			}
		}

		void paint()
		{
			ComponentPeer* const peer = getPeer();

			if (peer != 0)
			{
				const Point<int> pos (getScreenPosition() - peer->getScreenPosition());
				ERect r;
				r.left = pos.getX();
				r.right = r.left + getWidth();
				r.top = pos.getY();
				r.bottom = r.top + getHeight();

				owner->dispatch (effEditDraw, 0, 0, &r, 0);
			}
		}

	private:
		VSTPluginWindow* const owner;
		bool alreadyInside;
	};

	friend class InnerWrapperComponent;
	ScopedPointer <InnerWrapperComponent> innerWrapper;

	void resized()
	{
		innerWrapper->setSize (getWidth(), getHeight());
	}
#endif
};

AudioProcessorEditor* VSTPluginInstance::createEditor()
{
	if (hasEditor())
		return new VSTPluginWindow (*this);

	return 0;
}

void VSTPluginInstance::handleAsyncUpdate()
{
	// indicates that something about the plugin has changed..
	updateHostDisplay();
}

bool VSTPluginInstance::restoreProgramSettings (const fxProgram* const prog)
{
	if (vst_swap (prog->chunkMagic) == 'CcnK' && vst_swap (prog->fxMagic) == 'FxCk')
	{
		changeProgramName (getCurrentProgram(), prog->prgName);

		for (int i = 0; i < vst_swap (prog->numParams); ++i)
			setParameter (i, vst_swapFloat (prog->params[i]));

		return true;
	}

	return false;
}

bool VSTPluginInstance::loadFromFXBFile (const void* const data,
										 const int dataSize)
{
	if (dataSize < 28)
		return false;

	const fxSet* const set = (const fxSet*) data;

	if ((vst_swap (set->chunkMagic) != 'CcnK' && vst_swap (set->chunkMagic) != 'KncC')
		 || vst_swap (set->version) > fxbVersionNum)
		return false;

	if (vst_swap (set->fxMagic) == 'FxBk')
	{
		// bank of programs
		if (vst_swap (set->numPrograms) >= 0)
		{
			const int oldProg = getCurrentProgram();
			const int numParams = vst_swap (((const fxProgram*) (set->programs))->numParams);
			const int progLen = sizeof (fxProgram) + (numParams - 1) * sizeof (float);

			for (int i = 0; i < vst_swap (set->numPrograms); ++i)
			{
				if (i != oldProg)
				{
					const fxProgram* const prog = (const fxProgram*) (((const char*) (set->programs)) + i * progLen);
					if (((const char*) prog) - ((const char*) set) >= dataSize)
						return false;

					if (vst_swap (set->numPrograms) > 0)
						setCurrentProgram (i);

					if (! restoreProgramSettings (prog))
						return false;
				}
			}

			if (vst_swap (set->numPrograms) > 0)
				setCurrentProgram (oldProg);

			const fxProgram* const prog = (const fxProgram*) (((const char*) (set->programs)) + oldProg * progLen);
			if (((const char*) prog) - ((const char*) set) >= dataSize)
				return false;

			if (! restoreProgramSettings (prog))
				return false;
		}
	}
	else if (vst_swap (set->fxMagic) == 'FxCk')
	{
		// single program
		const fxProgram* const prog = (const fxProgram*) data;

		if (vst_swap (prog->chunkMagic) != 'CcnK')
			return false;

		changeProgramName (getCurrentProgram(), prog->prgName);

		for (int i = 0; i < vst_swap (prog->numParams); ++i)
			setParameter (i, vst_swapFloat (prog->params[i]));
	}
	else if (vst_swap (set->fxMagic) == 'FBCh' || vst_swap (set->fxMagic) == 'hCBF')
	{
		// non-preset chunk
		const fxChunkSet* const cset = (const fxChunkSet*) data;

		if (vst_swap (cset->chunkSize) + sizeof (fxChunkSet) - 8 > (unsigned int) dataSize)
			return false;

		setChunkData (cset->chunk, vst_swap (cset->chunkSize), false);
	}
	else if (vst_swap (set->fxMagic) == 'FPCh' || vst_swap (set->fxMagic) == 'hCPF')
	{
		// preset chunk
		const fxProgramSet* const cset = (const fxProgramSet*) data;

		if (vst_swap (cset->chunkSize) + sizeof (fxProgramSet) - 8 > (unsigned int) dataSize)
			return false;

		setChunkData (cset->chunk, vst_swap (cset->chunkSize), true);

		changeProgramName (getCurrentProgram(), cset->name);
	}
	else
	{
		return false;
	}

	return true;
}

void VSTPluginInstance::setParamsInProgramBlock (fxProgram* const prog) throw()
{
	const int numParams = getNumParameters();

	prog->chunkMagic = vst_swap ('CcnK');
	prog->byteSize = 0;
	prog->fxMagic = vst_swap ('FxCk');
	prog->version = vst_swap (fxbVersionNum);
	prog->fxID = vst_swap (getUID());
	prog->fxVersion = vst_swap (getVersionNumber());
	prog->numParams = vst_swap (numParams);

	getCurrentProgramName().copyToCString (prog->prgName, sizeof (prog->prgName) - 1);

	for (int i = 0; i < numParams; ++i)
		prog->params[i] = vst_swapFloat (getParameter (i));
}

bool VSTPluginInstance::saveToFXBFile (MemoryBlock& dest, bool isFXB, int maxSizeMB)
{
	const int numPrograms = getNumPrograms();
	const int numParams = getNumParameters();

	if (usesChunks())
	{
		if (isFXB)
		{
			MemoryBlock chunk;
			getChunkData (chunk, false, maxSizeMB);

			const size_t totalLen = sizeof (fxChunkSet) + chunk.getSize() - 8;
			dest.setSize (totalLen, true);

			fxChunkSet* const set = (fxChunkSet*) dest.getData();
			set->chunkMagic = vst_swap ('CcnK');
			set->byteSize = 0;
			set->fxMagic = vst_swap ('FBCh');
			set->version = vst_swap (fxbVersionNum);
			set->fxID = vst_swap (getUID());
			set->fxVersion = vst_swap (getVersionNumber());
			set->numPrograms = vst_swap (numPrograms);
			set->chunkSize = vst_swap ((long) chunk.getSize());

			chunk.copyTo (set->chunk, 0, chunk.getSize());
		}
		else
		{
			MemoryBlock chunk;
			getChunkData (chunk, true, maxSizeMB);

			const size_t totalLen = sizeof (fxProgramSet) + chunk.getSize() - 8;
			dest.setSize (totalLen, true);

			fxProgramSet* const set = (fxProgramSet*) dest.getData();
			set->chunkMagic = vst_swap ('CcnK');
			set->byteSize = 0;
			set->fxMagic = vst_swap ('FPCh');
			set->version = vst_swap (fxbVersionNum);
			set->fxID = vst_swap (getUID());
			set->fxVersion = vst_swap (getVersionNumber());
			set->numPrograms = vst_swap (numPrograms);
			set->chunkSize = vst_swap ((long) chunk.getSize());

			getCurrentProgramName().copyToCString (set->name, sizeof (set->name) - 1);
			chunk.copyTo (set->chunk, 0, chunk.getSize());
		}
	}
	else
	{
		if (isFXB)
		{
			const int progLen = sizeof (fxProgram) + (numParams - 1) * sizeof (float);
			const int len = (sizeof (fxSet) - sizeof (fxProgram)) + progLen * jmax (1, numPrograms);
			dest.setSize (len, true);

			fxSet* const set = (fxSet*) dest.getData();
			set->chunkMagic = vst_swap ('CcnK');
			set->byteSize = 0;
			set->fxMagic = vst_swap ('FxBk');
			set->version = vst_swap (fxbVersionNum);
			set->fxID = vst_swap (getUID());
			set->fxVersion = vst_swap (getVersionNumber());
			set->numPrograms = vst_swap (numPrograms);

			const int oldProgram = getCurrentProgram();
			MemoryBlock oldSettings;
			createTempParameterStore (oldSettings);

			setParamsInProgramBlock ((fxProgram*) (((char*) (set->programs)) + oldProgram * progLen));

			for (int i = 0; i < numPrograms; ++i)
			{
				if (i != oldProgram)
				{
					setCurrentProgram (i);
					setParamsInProgramBlock ((fxProgram*) (((char*) (set->programs)) + i * progLen));
				}
			}

			setCurrentProgram (oldProgram);
			restoreFromTempParameterStore (oldSettings);
		}
		else
		{
			const int totalLen = sizeof (fxProgram) + (numParams - 1) * sizeof (float);
			dest.setSize (totalLen, true);

			setParamsInProgramBlock ((fxProgram*) dest.getData());
		}
	}

	return true;
}

void VSTPluginInstance::getChunkData (MemoryBlock& mb, bool isPreset, int maxSizeMB) const
{
	if (usesChunks())
	{
		void* data = 0;
		const int bytes = dispatch (effGetChunk, isPreset ? 1 : 0, 0, &data, 0.0f);

		if (data != 0 && bytes <= maxSizeMB * 1024 * 1024)
		{
			mb.setSize (bytes);
			mb.copyFrom (data, 0, bytes);
		}
	}
}

void VSTPluginInstance::setChunkData (const char* data, int size, bool isPreset)
{
	if (size > 0 && usesChunks())
	{
		dispatch (effSetChunk, isPreset ? 1 : 0, size, (void*) data, 0.0f);

		if (! isPreset)
			updateStoredProgramNames();
	}
}

void VSTPluginInstance::timerCallback()
{
	if (dispatch (effIdle, 0, 0, 0, 0) == 0)
		stopTimer();
}

int VSTPluginInstance::dispatch (const int opcode, const int index, const int value, void* const ptr, float opt) const
{
	const ScopedLock sl (lock);

	++insideVSTCallback;
	int result = 0;

	try
	{
		if (effect != 0)
		{
#if JUCE_MAC
			if (module->resFileId != 0)
				UseResFile (module->resFileId);

			CGrafPtr oldPort;

			if (getActiveEditor() != 0)
			{
				const Point<int> pos (getActiveEditor()->relativePositionToOtherComponent (getActiveEditor()->getTopLevelComponent(), Point<int>()));

				GetPort (&oldPort);
				SetPortWindowPort ((WindowRef) getActiveEditor()->getWindowHandle());
				SetOrigin (-pos.getX(), -pos.getY());
			}
#endif

			result = effect->dispatcher (effect, opcode, index, value, ptr, opt);

#if JUCE_MAC
			if (getActiveEditor() != 0)
				SetPort (oldPort);

			module->resFileId = CurResFile();
#endif

			--insideVSTCallback;
			return result;
		}
	}
	catch (...)
	{
	}

	--insideVSTCallback;
	return result;
}

// handles non plugin-specific callbacks..

static const int defaultVSTSampleRateValue = 16384;
static const int defaultVSTBlockSizeValue = 512;

static VstIntPtr handleGeneralCallback (VstInt32 opcode, VstInt32 index, VstInt32 value, void *ptr, float opt)
{
	(void) index;
	(void) value;
	(void) opt;

	switch (opcode)
	{
	case audioMasterCanDo:
		{
			static const char* canDos[] = { "supplyIdle",
											"sendVstEvents",
											"sendVstMidiEvent",
											"sendVstTimeInfo",
											"receiveVstEvents",
											"receiveVstMidiEvent",
											"supportShell",
											"shellCategory" };

			for (int i = 0; i < numElementsInArray (canDos); ++i)
				if (strcmp (canDos[i], (const char*) ptr) == 0)
					return 1;

			return 0;
		}

	case audioMasterVersion:
		return 0x2400;
	case audioMasterCurrentId:
		return shellUIDToCreate;
	case audioMasterGetNumAutomatableParameters:
		return 0;
	case audioMasterGetAutomationState:
		return 1;

	case audioMasterGetVendorVersion:
		return 0x0101;
	case audioMasterGetVendorString:
	case audioMasterGetProductString:
		{
			String hostName ("Juce VST Host");

			if (JUCEApplication::getInstance() != 0)
				hostName = JUCEApplication::getInstance()->getApplicationName();

			hostName.copyToCString ((char*) ptr, jmin (kVstMaxVendorStrLen, kVstMaxProductStrLen) - 1);
		}
		break;

	case audioMasterGetSampleRate:
		return (VstIntPtr) defaultVSTSampleRateValue;

	case audioMasterGetBlockSize:
		return (VstIntPtr) defaultVSTBlockSizeValue;

	case audioMasterSetOutputSampleRate:
		return 0;

	default:
		DBG ("*** Unhandled VST Callback: " + String ((int) opcode));
		break;
	}

	return 0;
}

// handles callbacks for a specific plugin
VstIntPtr VSTPluginInstance::handleCallback (VstInt32 opcode, VstInt32 index, VstInt32 value, void *ptr, float opt)
{
	switch (opcode)
	{
	case audioMasterAutomate:
		sendParamChangeMessageToListeners (index, opt);
		break;

	case audioMasterProcessEvents:
		handleMidiFromPlugin ((const VstEvents*) ptr);
		break;

	case audioMasterGetTime:
		#ifdef _MSC_VER
		 #pragma warning (push)
		 #pragma warning (disable: 4311)
		#endif

		return (VstIntPtr) &vstHostTime;

		#ifdef _MSC_VER
		 #pragma warning (pop)
		#endif
		break;

	case audioMasterIdle:
		if (insideVSTCallback == 0 && MessageManager::getInstance()->isThisTheMessageThread())
		{
			++insideVSTCallback;
#if JUCE_MAC
			if (getActiveEditor() != 0)
				dispatch (effEditIdle, 0, 0, 0, 0);
#endif
			juce_callAnyTimersSynchronously();

			handleUpdateNowIfNeeded();

			for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
				ComponentPeer::getPeer (i)->performAnyPendingRepaintsNow();

			--insideVSTCallback;
		}
		break;

	case audioMasterUpdateDisplay:
		triggerAsyncUpdate();
		break;

	case audioMasterTempoAt:
		// returns (10000 * bpm)
		break;

	case audioMasterNeedIdle:
		startTimer (50);
		break;

	case audioMasterSizeWindow:
		if (getActiveEditor() != 0)
			getActiveEditor()->setSize (index, value);

		return 1;

	case audioMasterGetSampleRate:
		return (VstIntPtr) (getSampleRate() > 0 ? getSampleRate() : defaultVSTSampleRateValue);

	case audioMasterGetBlockSize:
		return (VstIntPtr) (getBlockSize() > 0 ? getBlockSize() : defaultVSTBlockSizeValue);

	case audioMasterWantMidi:
		wantsMidiMessages = true;
		break;

	case audioMasterGetDirectory:
	  #if JUCE_MAC
		return (VstIntPtr) (void*) &module->parentDirFSSpec;
	  #else
		return (VstIntPtr) (pointer_sized_uint) module->fullParentDirectoryPathName.toUTF8();
	  #endif

	case audioMasterGetAutomationState:
		// returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
		break;

	// none of these are handled (yet)..
	case audioMasterBeginEdit:
	case audioMasterEndEdit:
	case audioMasterSetTime:
	case audioMasterPinConnected:
	case audioMasterGetParameterQuantization:
	case audioMasterIOChanged:
	case audioMasterGetInputLatency:
	case audioMasterGetOutputLatency:
	case audioMasterGetPreviousPlug:
	case audioMasterGetNextPlug:
	case audioMasterWillReplaceOrAccumulate:
	case audioMasterGetCurrentProcessLevel:
	case audioMasterOfflineStart:
	case audioMasterOfflineRead:
	case audioMasterOfflineWrite:
	case audioMasterOfflineGetCurrentPass:
	case audioMasterOfflineGetCurrentMetaPass:
	case audioMasterVendorSpecific:
	case audioMasterSetIcon:
	case audioMasterGetLanguage:
	case audioMasterOpenWindow:
	case audioMasterCloseWindow:
		break;

	default:
		return handleGeneralCallback (opcode, index, value, ptr, opt);
	}

	return 0;
}

// entry point for all callbacks from the plugin
static VstIntPtr VSTCALLBACK audioMaster (AEffect* effect, VstInt32 opcode, VstInt32 index, VstIntPtr value, void* ptr, float opt)
{
	try
	{
		if (effect != 0 && effect->resvd2 != 0)
		{
			return ((VSTPluginInstance*)(effect->resvd2))
						->handleCallback (opcode, index, value, ptr, opt);
		}

		return handleGeneralCallback (opcode, index, value, ptr, opt);
	}
	catch (...)
	{
		return 0;
	}
}

const String VSTPluginInstance::getVersion() const throw()
{
	unsigned int v = dispatch (effGetVendorVersion, 0, 0, 0, 0);

	String s;

	if (v == 0 || v == -1)
		v = getVersionNumber();

	if (v != 0)
	{
		int versionBits[4];
		int n = 0;

		while (v != 0)
		{
			versionBits [n++] = (v & 0xff);
			v >>= 8;
		}

		s << 'V';

		while (n > 0)
		{
			s << versionBits [--n];

			if (n > 0)
				s << '.';
		}
	}

	return s;
}

int VSTPluginInstance::getUID() const throw()
{
	int uid = effect != 0 ? effect->uniqueID : 0;

	if (uid == 0)
		uid = module->file.hashCode();

	return uid;
}

const String VSTPluginInstance::getCategory() const throw()
{
	const char* result = 0;

	switch (dispatch (effGetPlugCategory, 0, 0, 0, 0))
	{
	case kPlugCategEffect:
		result = "Effect";
		break;

	case kPlugCategSynth:
		result = "Synth";
		break;

	case kPlugCategAnalysis:
		result = "Anaylsis";
		break;

	case kPlugCategMastering:
		result = "Mastering";
		break;

	case kPlugCategSpacializer:
		result = "Spacial";
		break;

	case kPlugCategRoomFx:
		result = "Reverb";
		break;

	case kPlugSurroundFx:
		result = "Surround";
		break;

	case kPlugCategRestoration:
		result = "Restoration";
		break;

	case kPlugCategGenerator:
		result = "Tone generation";
		break;

	default:
		break;
	}

	return result;
}

float VSTPluginInstance::getParameter (int index)
{
	if (effect != 0 && ((unsigned int) index) < (unsigned int) effect->numParams)
	{
		try
		{
			const ScopedLock sl (lock);
			return effect->getParameter (effect, index);
		}
		catch (...)
		{
		}
	}

	return 0.0f;
}

void VSTPluginInstance::setParameter (int index, float newValue)
{
	if (effect != 0 && ((unsigned int) index) < (unsigned int) effect->numParams)
	{
		try
		{
			const ScopedLock sl (lock);

			if (effect->getParameter (effect, index) != newValue)
				effect->setParameter (effect, index, newValue);
		}
		catch (...)
		{
		}
	}
}

const String VSTPluginInstance::getParameterName (int index)
{
	if (effect != 0)
	{
		jassert (index >= 0 && index < effect->numParams);

		char nm [256];
		zerostruct (nm);
		dispatch (effGetParamName, index, 0, nm, 0);
		return String (nm).trim();
	}

	return String::empty;
}

const String VSTPluginInstance::getParameterLabel (int index) const
{
	if (effect != 0)
	{
		jassert (index >= 0 && index < effect->numParams);

		char nm [256];
		zerostruct (nm);
		dispatch (effGetParamLabel, index, 0, nm, 0);
		return String (nm).trim();
	}

	return String::empty;
}

const String VSTPluginInstance::getParameterText (int index)
{
	if (effect != 0)
	{
		jassert (index >= 0 && index < effect->numParams);

		char nm [256];
		zerostruct (nm);
		dispatch (effGetParamDisplay, index, 0, nm, 0);
		return String (nm).trim();
	}

	return String::empty;
}

bool VSTPluginInstance::isParameterAutomatable (int index) const
{
	if (effect != 0)
	{
		jassert (index >= 0 && index < effect->numParams);
		return dispatch (effCanBeAutomated, index, 0, 0, 0) != 0;
	}

	return false;
}

void VSTPluginInstance::createTempParameterStore (MemoryBlock& dest)
{
	dest.setSize (64 + 4 * getNumParameters());
	dest.fillWith (0);

	getCurrentProgramName().copyToCString ((char*) dest.getData(), 63);

	float* const p = (float*) (((char*) dest.getData()) + 64);
	for (int i = 0; i < getNumParameters(); ++i)
		p[i] = getParameter(i);
}

void VSTPluginInstance::restoreFromTempParameterStore (const MemoryBlock& m)
{
	changeProgramName (getCurrentProgram(), (const char*) m.getData());

	float* p = (float*) (((char*) m.getData()) + 64);
	for (int i = 0; i < getNumParameters(); ++i)
		setParameter (i, p[i]);
}

void VSTPluginInstance::setCurrentProgram (int newIndex)
{
	if (getNumPrograms() > 0 && newIndex != getCurrentProgram())
		dispatch (effSetProgram, 0, jlimit (0, getNumPrograms() - 1, newIndex), 0, 0);
}

const String VSTPluginInstance::getProgramName (int index)
{
	if (index == getCurrentProgram())
	{
		return getCurrentProgramName();
	}
	else if (effect != 0)
	{
		char nm [256];
		zerostruct (nm);

		if (dispatch (effGetProgramNameIndexed,
					  jlimit (0, getNumPrograms(), index),
					  -1, nm, 0) != 0)
		{
			return String (nm).trim();
		}
	}

	return programNames [index];
}

void VSTPluginInstance::changeProgramName (int index, const String& newName)
{
	if (index == getCurrentProgram())
	{
		if (getNumPrograms() > 0 && newName != getCurrentProgramName())
			dispatch (effSetProgramName, 0, 0, (void*) newName.substring (0, 24).toCString(), 0.0f);
	}
	else
	{
		jassertfalse // xxx not implemented!
	}
}

void VSTPluginInstance::updateStoredProgramNames()
{
	if (effect != 0 && getNumPrograms() > 0)
	{
		char nm [256];
		zerostruct (nm);

		// only do this if the plugin can't use indexed names..
		if (dispatch (effGetProgramNameIndexed, 0, -1, nm, 0) == 0)
		{
			const int oldProgram = getCurrentProgram();
			MemoryBlock oldSettings;
			createTempParameterStore (oldSettings);

			for (int i = 0; i < getNumPrograms(); ++i)
			{
				setCurrentProgram (i);
				getCurrentProgramName();  // (this updates the list)
			}

			setCurrentProgram (oldProgram);
			restoreFromTempParameterStore (oldSettings);
		}
	}
}

const String VSTPluginInstance::getCurrentProgramName()
{
	if (effect != 0)
	{
		char nm [256];
		zerostruct (nm);
		dispatch (effGetProgramName, 0, 0, nm, 0);

		const int index = getCurrentProgram();
		if (programNames[index].isEmpty())
		{
			while (programNames.size() < index)
				programNames.add (String::empty);

			programNames.set (index, String (nm).trim());
		}

		return String (nm).trim();
	}

	return String::empty;
}

const String VSTPluginInstance::getInputChannelName (const int index) const
{
	if (index >= 0 && index < getNumInputChannels())
	{
		VstPinProperties pinProps;
		if (dispatch (effGetInputProperties, index, 0, &pinProps, 0.0f) != 0)
			return String (pinProps.label, sizeof (pinProps.label));
	}

	return String::empty;
}

bool VSTPluginInstance::isInputChannelStereoPair (int index) const
{
	if (index < 0 || index >= getNumInputChannels())
		return false;

	VstPinProperties pinProps;
	if (dispatch (effGetInputProperties, index, 0, &pinProps, 0.0f) != 0)
		return (pinProps.flags & kVstPinIsStereo) != 0;

	return true;
}

const String VSTPluginInstance::getOutputChannelName (const int index) const
{
	if (index >= 0 && index < getNumOutputChannels())
	{
		VstPinProperties pinProps;
		if (dispatch (effGetOutputProperties, index, 0, &pinProps, 0.0f) != 0)
			return String (pinProps.label, sizeof (pinProps.label));
	}

	return String::empty;
}

bool VSTPluginInstance::isOutputChannelStereoPair (int index) const
{
	if (index < 0 || index >= getNumOutputChannels())
		return false;

	VstPinProperties pinProps;
	if (dispatch (effGetOutputProperties, index, 0, &pinProps, 0.0f) != 0)
		return (pinProps.flags & kVstPinIsStereo) != 0;

	return true;
}

void VSTPluginInstance::setPower (const bool on)
{
	dispatch (effMainsChanged, 0, on ? 1 : 0, 0, 0);
	isPowerOn = on;
}

const int defaultMaxSizeMB = 64;

void VSTPluginInstance::getStateInformation (MemoryBlock& destData)
{
	saveToFXBFile (destData, true, defaultMaxSizeMB);
}

void VSTPluginInstance::getCurrentProgramStateInformation (MemoryBlock& destData)
{
	saveToFXBFile (destData, false, defaultMaxSizeMB);
}

void VSTPluginInstance::setStateInformation (const void* data, int sizeInBytes)
{
	loadFromFXBFile (data, sizeInBytes);
}

void VSTPluginInstance::setCurrentProgramStateInformation (const void* data, int sizeInBytes)
{
	loadFromFXBFile (data, sizeInBytes);
}

VSTPluginFormat::VSTPluginFormat()
{
}

VSTPluginFormat::~VSTPluginFormat()
{
}

void VSTPluginFormat::findAllTypesForFile (OwnedArray <PluginDescription>& results,
										   const String& fileOrIdentifier)
{
	if (! fileMightContainThisPluginType (fileOrIdentifier))
		return;

	PluginDescription desc;
	desc.fileOrIdentifier = fileOrIdentifier;
	desc.uid = 0;

	ScopedPointer <VSTPluginInstance> instance (dynamic_cast <VSTPluginInstance*> (createInstanceFromDescription (desc)));

	if (instance == 0)
		return;

	try
	{
#if JUCE_MAC
		if (instance->module->resFileId != 0)
			UseResFile (instance->module->resFileId);
#endif

		instance->fillInPluginDescription (desc);

		VstPlugCategory category = (VstPlugCategory) instance->dispatch (effGetPlugCategory, 0, 0, 0, 0);

		if (category != kPlugCategShell)
		{
			// Normal plugin...
			results.add (new PluginDescription (desc));

			++insideVSTCallback;
			instance->dispatch (effOpen, 0, 0, 0, 0);
			--insideVSTCallback;
		}
		else
		{
			// It's a shell plugin, so iterate all the subtypes...
			char shellEffectName [64];

			for (;;)
			{
				zerostruct (shellEffectName);
				const int uid = instance->dispatch (effShellGetNextPlugin, 0, 0, shellEffectName, 0);

				if (uid == 0)
				{
					break;
				}
				else
				{
					desc.uid = uid;
					desc.name = shellEffectName;

					bool alreadyThere = false;

					for (int i = results.size(); --i >= 0;)
					{
						PluginDescription* const d = results.getUnchecked(i);

						if (d->isDuplicateOf (desc))
						{
							alreadyThere = true;
							break;
						}
					}

					if (! alreadyThere)
						results.add (new PluginDescription (desc));
				}
			}
		}
	}
	catch (...)
	{
		// crashed while loading...
	}
}

AudioPluginInstance* VSTPluginFormat::createInstanceFromDescription (const PluginDescription& desc)
{
	ScopedPointer <VSTPluginInstance> result;

	if (fileMightContainThisPluginType (desc.fileOrIdentifier))
	{
		File file (desc.fileOrIdentifier);

		const File previousWorkingDirectory (File::getCurrentWorkingDirectory());
		file.getParentDirectory().setAsCurrentWorkingDirectory();

		const ReferenceCountedObjectPtr <ModuleHandle> module (ModuleHandle::findOrCreateModule (file));

		if (module != 0)
		{
			shellUIDToCreate = desc.uid;

			result = new VSTPluginInstance (module);

			if (result->effect != 0)
			{
				result->effect->resvd2 = (VstIntPtr) (pointer_sized_int) (VSTPluginInstance*) result;
				result->initialise();
			}
			else
			{
				result = 0;
			}
		}

		previousWorkingDirectory.setAsCurrentWorkingDirectory();
	}

	return result.release();
}

bool VSTPluginFormat::fileMightContainThisPluginType (const String& fileOrIdentifier)
{
	const File f (fileOrIdentifier);

#if JUCE_MAC
	if (f.isDirectory() && f.hasFileExtension (".vst"))
		return true;

#if JUCE_PPC
	FSRef fileRef;
	if (PlatformUtilities::makeFSRefFromPath (&fileRef, f.getFullPathName()))
	{
		const short resFileId = FSOpenResFile (&fileRef, fsRdPerm);

		if (resFileId != -1)
		{
			const int numEffects = Count1Resources ('aEff');
			CloseResFile (resFileId);

			if (numEffects > 0)
				return true;
		}
	}
#endif

	return false;
#elif JUCE_WIN32
	return f.existsAsFile()
			&& f.hasFileExtension (".dll");
#elif JUCE_LINUX
	return f.existsAsFile()
			&& f.hasFileExtension (".so");
#endif
}

const String VSTPluginFormat::getNameOfPluginFromIdentifier (const String& fileOrIdentifier)
{
	return fileOrIdentifier;
}

bool VSTPluginFormat::doesPluginStillExist (const PluginDescription& desc)
{
	return File (desc.fileOrIdentifier).exists();
}

const StringArray VSTPluginFormat::searchPathsForPlugins (const FileSearchPath& directoriesToSearch, const bool recursive)
{
	StringArray results;

	for (int j = 0; j < directoriesToSearch.getNumPaths(); ++j)
		recursiveFileSearch (results, directoriesToSearch [j], recursive);

	return results;
}

void VSTPluginFormat::recursiveFileSearch (StringArray& results, const File& dir, const bool recursive)
{
	// avoid allowing the dir iterator to be recursive, because we want to avoid letting it delve inside
	// .component or .vst directories.
	DirectoryIterator iter (dir, false, "*", File::findFilesAndDirectories);

	while (iter.next())
	{
		const File f (iter.getFile());
		bool isPlugin = false;

		if (fileMightContainThisPluginType (f.getFullPathName()))
		{
			isPlugin = true;
			results.add (f.getFullPathName());
		}

		if (recursive && (! isPlugin) && f.isDirectory())
			recursiveFileSearch (results, f, true);
	}
}

const FileSearchPath VSTPluginFormat::getDefaultLocationsToSearch()
{
#if JUCE_MAC
	return FileSearchPath ("~/Library/Audio/Plug-Ins/VST;/Library/Audio/Plug-Ins/VST");
#elif JUCE_WIN32
	const String programFiles (File::getSpecialLocation (File::globalApplicationsDirectory).getFullPathName());

	return FileSearchPath (programFiles + "\\Steinberg\\VstPlugins");
#elif JUCE_LINUX
	return FileSearchPath ("/usr/lib/vst");
#endif
}

END_JUCE_NAMESPACE

#endif

#undef log

#endif
/*** End of inlined file: juce_VSTPluginFormat.cpp ***/


/*** End of inlined file: juce_VSTPluginFormat.mm ***/


/*** Start of inlined file: juce_AudioProcessor.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioProcessor::AudioProcessor()
	: playHead (0),
	  activeEditor (0),
	  sampleRate (0),
	  blockSize (0),
	  numInputChannels (0),
	  numOutputChannels (0),
	  latencySamples (0),
	  suspended (false),
	  nonRealtime (false)
{
}

AudioProcessor::~AudioProcessor()
{
	// ooh, nasty - the editor should have been deleted before the filter
	// that it refers to is deleted..
	jassert (activeEditor == 0);

#ifdef JUCE_DEBUG
	// This will fail if you've called beginParameterChangeGesture() for one
	// or more parameters without having made a corresponding call to endParameterChangeGesture...
	jassert (changingParams.countNumberOfSetBits() == 0);
#endif
}

void AudioProcessor::setPlayHead (AudioPlayHead* const newPlayHead) throw()
{
	playHead = newPlayHead;
}

void AudioProcessor::addListener (AudioProcessorListener* const newListener) throw()
{
	const ScopedLock sl (listenerLock);
	listeners.addIfNotAlreadyThere (newListener);
}

void AudioProcessor::removeListener (AudioProcessorListener* const listenerToRemove) throw()
{
	const ScopedLock sl (listenerLock);
	listeners.removeValue (listenerToRemove);
}

void AudioProcessor::setPlayConfigDetails (const int numIns,
										   const int numOuts,
										   const double sampleRate_,
										   const int blockSize_) throw()
{
	numInputChannels = numIns;
	numOutputChannels = numOuts;
	sampleRate = sampleRate_;
	blockSize = blockSize_;
}

void AudioProcessor::setNonRealtime (const bool nonRealtime_) throw()
{
	nonRealtime = nonRealtime_;
}

void AudioProcessor::setLatencySamples (const int newLatency)
{
	if (latencySamples != newLatency)
	{
		latencySamples = newLatency;
		updateHostDisplay();
	}
}

void AudioProcessor::setParameterNotifyingHost (const int parameterIndex,
												const float newValue)
{
	setParameter (parameterIndex, newValue);
	sendParamChangeMessageToListeners (parameterIndex, newValue);
}

void AudioProcessor::sendParamChangeMessageToListeners (const int parameterIndex, const float newValue)
{
	jassert (((unsigned int) parameterIndex) < (unsigned int) getNumParameters());

	for (int i = listeners.size(); --i >= 0;)
	{
		AudioProcessorListener* l;

		{
			const ScopedLock sl (listenerLock);
			l = (AudioProcessorListener*) listeners [i];
		}

		if (l != 0)
			l->audioProcessorParameterChanged (this, parameterIndex, newValue);
	}
}

void AudioProcessor::beginParameterChangeGesture (int parameterIndex)
{
	jassert (((unsigned int) parameterIndex) < (unsigned int) getNumParameters());

#ifdef JUCE_DEBUG
	// This means you've called beginParameterChangeGesture twice in succession without a matching
	// call to endParameterChangeGesture. That might be fine in most hosts, but better to avoid doing it.
	jassert (! changingParams [parameterIndex]);
	changingParams.setBit (parameterIndex);
#endif

	for (int i = listeners.size(); --i >= 0;)
	{
		AudioProcessorListener* l;

		{
			const ScopedLock sl (listenerLock);
			l = (AudioProcessorListener*) listeners [i];
		}

		if (l != 0)
			l->audioProcessorParameterChangeGestureBegin (this, parameterIndex);
	}
}

void AudioProcessor::endParameterChangeGesture (int parameterIndex)
{
	jassert (((unsigned int) parameterIndex) < (unsigned int) getNumParameters());

#ifdef JUCE_DEBUG
	// This means you've called endParameterChangeGesture without having previously called
	// endParameterChangeGesture. That might be fine in most hosts, but better to keep the
	// calls matched correctly.
	jassert (changingParams [parameterIndex]);
	changingParams.clearBit (parameterIndex);
#endif

	for (int i = listeners.size(); --i >= 0;)
	{
		AudioProcessorListener* l;

		{
			const ScopedLock sl (listenerLock);
			l = (AudioProcessorListener*) listeners [i];
		}

		if (l != 0)
			l->audioProcessorParameterChangeGestureEnd (this, parameterIndex);
	}
}

void AudioProcessor::updateHostDisplay()
{
	for (int i = listeners.size(); --i >= 0;)
	{
		AudioProcessorListener* l;

		{
			const ScopedLock sl (listenerLock);
			l = (AudioProcessorListener*) listeners [i];
		}

		if (l != 0)
			l->audioProcessorChanged (this);
	}
}

bool AudioProcessor::isParameterAutomatable (int /*parameterIndex*/) const
{
	return true;
}

bool AudioProcessor::isMetaParameter (int /*parameterIndex*/) const
{
	return false;
}

void AudioProcessor::suspendProcessing (const bool shouldBeSuspended)
{
	const ScopedLock sl (callbackLock);
	suspended = shouldBeSuspended;
}

void AudioProcessor::reset()
{
}

void AudioProcessor::editorBeingDeleted (AudioProcessorEditor* const editor) throw()
{
	const ScopedLock sl (callbackLock);

	jassert (activeEditor == editor);

	if (activeEditor == editor)
		activeEditor = 0;
}

AudioProcessorEditor* AudioProcessor::createEditorIfNeeded()
{
	if (activeEditor != 0)
		return activeEditor;

	AudioProcessorEditor* const ed = createEditor();

	if (ed != 0)
	{
		// you must give your editor comp a size before returning it..
		jassert (ed->getWidth() > 0 && ed->getHeight() > 0);

		const ScopedLock sl (callbackLock);
		activeEditor = ed;
	}

	return ed;
}

void AudioProcessor::getCurrentProgramStateInformation (JUCE_NAMESPACE::MemoryBlock& destData)
{
	getStateInformation (destData);
}

void AudioProcessor::setCurrentProgramStateInformation (const void* data, int sizeInBytes)
{
	setStateInformation (data, sizeInBytes);
}

// magic number to identify memory blocks that we've stored as XML
const uint32 magicXmlNumber = 0x21324356;

void AudioProcessor::copyXmlToBinary (const XmlElement& xml,
									  JUCE_NAMESPACE::MemoryBlock& destData)
{
	const String xmlString (xml.createDocument (String::empty, true, false));
	const int stringLength = xmlString.getNumBytesAsUTF8();

	destData.setSize (stringLength + 10);

	char* const d = (char*) destData.getData();
	*(uint32*) d = ByteOrder::swapIfBigEndian ((const uint32) magicXmlNumber);
	*(uint32*) (d + 4) = ByteOrder::swapIfBigEndian ((const uint32) stringLength);

	xmlString.copyToUTF8 (d + 8, stringLength + 1);
}

XmlElement* AudioProcessor::getXmlFromBinary (const void* data,
											  const int sizeInBytes)
{
	if (sizeInBytes > 8
		 && ByteOrder::littleEndianInt ((const char*) data) == magicXmlNumber)
	{
		const int stringLength = (int) ByteOrder::littleEndianInt (((const char*) data) + 4);

		if (stringLength > 0)
		{
			XmlDocument doc (String::fromUTF8 (((const char*) data) + 8,
											   jmin ((sizeInBytes - 8), stringLength)));

			return doc.getDocumentElement();
		}
	}

	return 0;
}

void AudioProcessorListener::audioProcessorParameterChangeGestureBegin (AudioProcessor*, int)
{
}

void AudioProcessorListener::audioProcessorParameterChangeGestureEnd (AudioProcessor*, int)
{
}

bool AudioPlayHead::CurrentPositionInfo::operator== (const CurrentPositionInfo& other) const throw()
{
	return timeInSeconds == other.timeInSeconds
		&& ppqPosition == other.ppqPosition
		&& editOriginTime == other.editOriginTime
		&& ppqPositionOfLastBarStart == other.ppqPositionOfLastBarStart
		&& frameRate == other.frameRate
		&& isPlaying == other.isPlaying
		&& isRecording == other.isRecording
		&& bpm == other.bpm
		&& timeSigNumerator == other.timeSigNumerator
		&& timeSigDenominator == other.timeSigDenominator;
}

bool AudioPlayHead::CurrentPositionInfo::operator!= (const CurrentPositionInfo& other) const throw()
{
	return ! operator== (other);
}

void AudioPlayHead::CurrentPositionInfo::resetToDefault()
{
	zerostruct (*this);
	timeSigNumerator = 4;
	timeSigDenominator = 4;
	bpm = 120;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioProcessor.cpp ***/


/*** Start of inlined file: juce_AudioProcessorEditor.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioProcessorEditor::AudioProcessorEditor (AudioProcessor* const owner_)
	: owner (owner_)
{
	// the filter must be valid..
	jassert (owner != 0);
}

AudioProcessorEditor::~AudioProcessorEditor()
{
	// if this fails, then the wrapper hasn't called editorBeingDeleted() on the
	// filter for some reason..
	jassert (owner->getActiveEditor() != this);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioProcessorEditor.cpp ***/


/*** Start of inlined file: juce_AudioProcessorGraph.cpp ***/
BEGIN_JUCE_NAMESPACE

const int AudioProcessorGraph::midiChannelIndex = 0x1000;

AudioProcessorGraph::Node::Node (const uint32 id_, AudioProcessor* const processor_)
	: id (id_),
	  processor (processor_),
	  isPrepared (false)
{
	jassert (processor_ != 0);
}

AudioProcessorGraph::Node::~Node()
{
	delete processor;
}

void AudioProcessorGraph::Node::prepare (const double sampleRate, const int blockSize,
										 AudioProcessorGraph* const graph)
{
	if (! isPrepared)
	{
		isPrepared = true;

		AudioProcessorGraph::AudioGraphIOProcessor* const ioProc
			= dynamic_cast <AudioProcessorGraph::AudioGraphIOProcessor*> (processor);

		if (ioProc != 0)
			ioProc->setParentGraph (graph);

		processor->setPlayConfigDetails (processor->getNumInputChannels(),
										 processor->getNumOutputChannels(),
										 sampleRate, blockSize);

		processor->prepareToPlay (sampleRate, blockSize);
	}
}

void AudioProcessorGraph::Node::unprepare()
{
	if (isPrepared)
	{
		isPrepared = false;
		processor->releaseResources();
	}
}

AudioProcessorGraph::AudioProcessorGraph()
	: lastNodeId (0),
	  renderingBuffers (1, 1),
	  currentAudioOutputBuffer (1, 1)
{
}

AudioProcessorGraph::~AudioProcessorGraph()
{
	clearRenderingSequence();
	clear();
}

const String AudioProcessorGraph::getName() const
{
	return "Audio Graph";
}

void AudioProcessorGraph::clear()
{
	nodes.clear();
	connections.clear();
	triggerAsyncUpdate();
}

AudioProcessorGraph::Node* AudioProcessorGraph::getNodeForId (const uint32 nodeId) const
{
	for (int i = nodes.size(); --i >= 0;)
		if (nodes.getUnchecked(i)->id == nodeId)
			return nodes.getUnchecked(i);

	return 0;
}

AudioProcessorGraph::Node* AudioProcessorGraph::addNode (AudioProcessor* const newProcessor,
														 uint32 nodeId)
{
	if (newProcessor == 0)
	{
		jassertfalse
		return 0;
	}

	if (nodeId == 0)
	{
		nodeId = ++lastNodeId;
	}
	else
	{
		// you can't add a node with an id that already exists in the graph..
		jassert (getNodeForId (nodeId) == 0);
		removeNode (nodeId);
	}

	lastNodeId = nodeId;

	Node* const n = new Node (nodeId, newProcessor);
	nodes.add (n);
	triggerAsyncUpdate();

	AudioProcessorGraph::AudioGraphIOProcessor* const ioProc
		= dynamic_cast <AudioProcessorGraph::AudioGraphIOProcessor*> (n->processor);

	if (ioProc != 0)
		ioProc->setParentGraph (this);

	return n;
}

bool AudioProcessorGraph::removeNode (const uint32 nodeId)
{
	disconnectNode (nodeId);

	for (int i = nodes.size(); --i >= 0;)
	{
		if (nodes.getUnchecked(i)->id == nodeId)
		{
			AudioProcessorGraph::AudioGraphIOProcessor* const ioProc
				= dynamic_cast <AudioProcessorGraph::AudioGraphIOProcessor*> (nodes.getUnchecked(i)->processor);

			if (ioProc != 0)
				ioProc->setParentGraph (0);

			nodes.remove (i);
			triggerAsyncUpdate();

			return true;
		}
	}

	return false;
}

const AudioProcessorGraph::Connection* AudioProcessorGraph::getConnectionBetween (const uint32 sourceNodeId,
																				  const int sourceChannelIndex,
																				  const uint32 destNodeId,
																				  const int destChannelIndex) const
{
	for (int i = connections.size(); --i >= 0;)
	{
		const Connection* const c = connections.getUnchecked(i);

		if (c->sourceNodeId == sourceNodeId
			 && c->destNodeId == destNodeId
			 && c->sourceChannelIndex == sourceChannelIndex
			 && c->destChannelIndex == destChannelIndex)
		{
			return c;
		}
	}

	return 0;
}

bool AudioProcessorGraph::isConnected (const uint32 possibleSourceNodeId,
									   const uint32 possibleDestNodeId) const
{
	for (int i = connections.size(); --i >= 0;)
	{
		const Connection* const c = connections.getUnchecked(i);

		if (c->sourceNodeId == possibleSourceNodeId
			 && c->destNodeId == possibleDestNodeId)
		{
			return true;
		}
	}

	return false;
}

bool AudioProcessorGraph::canConnect (const uint32 sourceNodeId,
									  const int sourceChannelIndex,
									  const uint32 destNodeId,
									  const int destChannelIndex) const
{
	if (sourceChannelIndex < 0
		 || destChannelIndex < 0
		 || sourceNodeId == destNodeId
		 || (destChannelIndex == midiChannelIndex) != (sourceChannelIndex == midiChannelIndex))
		return false;

	const Node* const source = getNodeForId (sourceNodeId);

	if (source == 0
		 || (sourceChannelIndex != midiChannelIndex && sourceChannelIndex >= source->processor->getNumOutputChannels())
		 || (sourceChannelIndex == midiChannelIndex && ! source->processor->producesMidi()))
		return false;

	const Node* const dest = getNodeForId (destNodeId);

	if (dest == 0
		 || (destChannelIndex != midiChannelIndex && destChannelIndex >= dest->processor->getNumInputChannels())
		 || (destChannelIndex == midiChannelIndex && ! dest->processor->acceptsMidi()))
		return false;

	return getConnectionBetween (sourceNodeId, sourceChannelIndex,
								 destNodeId, destChannelIndex) == 0;
}

bool AudioProcessorGraph::addConnection (const uint32 sourceNodeId,
										 const int sourceChannelIndex,
										 const uint32 destNodeId,
										 const int destChannelIndex)
{
	if (! canConnect (sourceNodeId, sourceChannelIndex, destNodeId, destChannelIndex))
		return false;

	Connection* const c = new Connection();
	c->sourceNodeId = sourceNodeId;
	c->sourceChannelIndex = sourceChannelIndex;
	c->destNodeId = destNodeId;
	c->destChannelIndex = destChannelIndex;

	connections.add (c);
	triggerAsyncUpdate();

	return true;
}

void AudioProcessorGraph::removeConnection (const int index)
{
	connections.remove (index);
	triggerAsyncUpdate();
}

bool AudioProcessorGraph::removeConnection (const uint32 sourceNodeId, const int sourceChannelIndex,
											const uint32 destNodeId, const int destChannelIndex)
{
	bool doneAnything = false;

	for (int i = connections.size(); --i >= 0;)
	{
		const Connection* const c = connections.getUnchecked(i);

		if (c->sourceNodeId == sourceNodeId
			 && c->destNodeId == destNodeId
			 && c->sourceChannelIndex == sourceChannelIndex
			 && c->destChannelIndex == destChannelIndex)
		{
			removeConnection (i);
			doneAnything = true;
			triggerAsyncUpdate();
		}
	}

	return doneAnything;
}

bool AudioProcessorGraph::disconnectNode (const uint32 nodeId)
{
	bool doneAnything = false;

	for (int i = connections.size(); --i >= 0;)
	{
		const Connection* const c = connections.getUnchecked(i);

		if (c->sourceNodeId == nodeId || c->destNodeId == nodeId)
		{
			removeConnection (i);
			doneAnything = true;
			triggerAsyncUpdate();
		}
	}

	return doneAnything;
}

bool AudioProcessorGraph::removeIllegalConnections()
{
	bool doneAnything = false;

	for (int i = connections.size(); --i >= 0;)
	{
		const Connection* const c = connections.getUnchecked(i);

		const Node* const source = getNodeForId (c->sourceNodeId);
		const Node* const dest = getNodeForId (c->destNodeId);

		if (source == 0 || dest == 0
			 || (c->sourceChannelIndex != midiChannelIndex
				  && (((unsigned int) c->sourceChannelIndex) >= (unsigned int) source->processor->getNumOutputChannels()))
			 || (c->sourceChannelIndex == midiChannelIndex
				  && ! source->processor->producesMidi())
			 || (c->destChannelIndex != midiChannelIndex
				   && (((unsigned int) c->destChannelIndex) >= (unsigned int) dest->processor->getNumInputChannels()))
			 || (c->destChannelIndex == midiChannelIndex
				   && ! dest->processor->acceptsMidi()))
		{
			removeConnection (i);
			doneAnything = true;
			triggerAsyncUpdate();
		}
	}

	return doneAnything;
}

namespace GraphRenderingOps
{

class AudioGraphRenderingOp
{
public:
	AudioGraphRenderingOp() {}
	virtual ~AudioGraphRenderingOp()  {}

	virtual void perform (AudioSampleBuffer& sharedBufferChans,
						  const OwnedArray <MidiBuffer>& sharedMidiBuffers,
						  const int numSamples) = 0;

	juce_UseDebuggingNewOperator
};

class ClearChannelOp : public AudioGraphRenderingOp
{
public:
	ClearChannelOp (const int channelNum_)
		: channelNum (channelNum_)
	{}

	~ClearChannelOp()	{}

	void perform (AudioSampleBuffer& sharedBufferChans, const OwnedArray <MidiBuffer>&, const int numSamples)
	{
		sharedBufferChans.clear (channelNum, 0, numSamples);
	}

private:
	const int channelNum;

	ClearChannelOp (const ClearChannelOp&);
	ClearChannelOp& operator= (const ClearChannelOp&);
};

class CopyChannelOp : public AudioGraphRenderingOp
{
public:
	CopyChannelOp (const int srcChannelNum_, const int dstChannelNum_)
		: srcChannelNum (srcChannelNum_),
		  dstChannelNum (dstChannelNum_)
	{}

	~CopyChannelOp()	{}

	void perform (AudioSampleBuffer& sharedBufferChans, const OwnedArray <MidiBuffer>&, const int numSamples)
	{
		sharedBufferChans.copyFrom (dstChannelNum, 0, sharedBufferChans, srcChannelNum, 0, numSamples);
	}

private:
	const int srcChannelNum, dstChannelNum;

	CopyChannelOp (const CopyChannelOp&);
	CopyChannelOp& operator= (const CopyChannelOp&);
};

class AddChannelOp : public AudioGraphRenderingOp
{
public:
	AddChannelOp (const int srcChannelNum_, const int dstChannelNum_)
		: srcChannelNum (srcChannelNum_),
		  dstChannelNum (dstChannelNum_)
	{}

	~AddChannelOp()	{}

	void perform (AudioSampleBuffer& sharedBufferChans, const OwnedArray <MidiBuffer>&, const int numSamples)
	{
		sharedBufferChans.addFrom (dstChannelNum, 0, sharedBufferChans, srcChannelNum, 0, numSamples);
	}

private:
	const int srcChannelNum, dstChannelNum;

	AddChannelOp (const AddChannelOp&);
	AddChannelOp& operator= (const AddChannelOp&);
};

class ClearMidiBufferOp : public AudioGraphRenderingOp
{
public:
	ClearMidiBufferOp (const int bufferNum_)
		: bufferNum (bufferNum_)
	{}

	~ClearMidiBufferOp()	{}

	void perform (AudioSampleBuffer&, const OwnedArray <MidiBuffer>& sharedMidiBuffers, const int)
	{
		sharedMidiBuffers.getUnchecked (bufferNum)->clear();
	}

private:
	const int bufferNum;

	ClearMidiBufferOp (const ClearMidiBufferOp&);
	ClearMidiBufferOp& operator= (const ClearMidiBufferOp&);
};

class CopyMidiBufferOp : public AudioGraphRenderingOp
{
public:
	CopyMidiBufferOp (const int srcBufferNum_, const int dstBufferNum_)
		: srcBufferNum (srcBufferNum_),
		  dstBufferNum (dstBufferNum_)
	{}

	~CopyMidiBufferOp()	{}

	void perform (AudioSampleBuffer&, const OwnedArray <MidiBuffer>& sharedMidiBuffers, const int)
	{
		*sharedMidiBuffers.getUnchecked (dstBufferNum) = *sharedMidiBuffers.getUnchecked (srcBufferNum);
	}

private:
	const int srcBufferNum, dstBufferNum;

	CopyMidiBufferOp (const CopyMidiBufferOp&);
	CopyMidiBufferOp& operator= (const CopyMidiBufferOp&);
};

class AddMidiBufferOp : public AudioGraphRenderingOp
{
public:
	AddMidiBufferOp (const int srcBufferNum_, const int dstBufferNum_)
		: srcBufferNum (srcBufferNum_),
		  dstBufferNum (dstBufferNum_)
	{}

	~AddMidiBufferOp()	{}

	void perform (AudioSampleBuffer&, const OwnedArray <MidiBuffer>& sharedMidiBuffers, const int numSamples)
	{
		sharedMidiBuffers.getUnchecked (dstBufferNum)
			->addEvents (*sharedMidiBuffers.getUnchecked (srcBufferNum), 0, numSamples, 0);
	}

private:
	const int srcBufferNum, dstBufferNum;

	AddMidiBufferOp (const AddMidiBufferOp&);
	AddMidiBufferOp& operator= (const AddMidiBufferOp&);
};

class ProcessBufferOp : public AudioGraphRenderingOp
{
public:
	ProcessBufferOp (const AudioProcessorGraph::Node::Ptr& node_,
					 const Array <int>& audioChannelsToUse_,
					 const int totalChans_,
					 const int midiBufferToUse_)
		: node (node_),
		  processor (node_->processor),
		  audioChannelsToUse (audioChannelsToUse_),
		  totalChans (jmax (1, totalChans_)),
		  midiBufferToUse (midiBufferToUse_)
	{
		channels.calloc (totalChans);

		while (audioChannelsToUse.size() < totalChans)
			audioChannelsToUse.add (0);
	}

	~ProcessBufferOp()
	{
	}

	void perform (AudioSampleBuffer& sharedBufferChans, const OwnedArray <MidiBuffer>& sharedMidiBuffers, const int numSamples)
	{
		for (int i = totalChans; --i >= 0;)
			channels[i] = sharedBufferChans.getSampleData (audioChannelsToUse.getUnchecked (i), 0);

		AudioSampleBuffer buffer (channels, totalChans, numSamples);

		processor->processBlock (buffer, *sharedMidiBuffers.getUnchecked (midiBufferToUse));
	}

	const AudioProcessorGraph::Node::Ptr node;
	AudioProcessor* const processor;

private:
	Array <int> audioChannelsToUse;
	HeapBlock <float*> channels;
	int totalChans;
	int midiBufferToUse;

	ProcessBufferOp (const ProcessBufferOp&);
	ProcessBufferOp& operator= (const ProcessBufferOp&);
};

class RenderingOpSequenceCalculator
{
public:

	RenderingOpSequenceCalculator (AudioProcessorGraph& graph_,
								   const VoidArray& orderedNodes_,
								   VoidArray& renderingOps)
		: graph (graph_),
		  orderedNodes (orderedNodes_)
	{
		nodeIds.add (-2); // first buffer is read-only zeros
		channels.add (0);

		midiNodeIds.add (-2);

		for (int i = 0; i < orderedNodes.size(); ++i)
		{
			createRenderingOpsForNode ((AudioProcessorGraph::Node*) orderedNodes.getUnchecked(i),
									   renderingOps, i);

			markAnyUnusedBuffersAsFree (i);
		}
	}

	int getNumBuffersNeeded() const	 { return nodeIds.size(); }

	int getNumMidiBuffersNeeded() const	 { return midiNodeIds.size(); }

	juce_UseDebuggingNewOperator

private:
	AudioProcessorGraph& graph;
	const VoidArray& orderedNodes;
	Array <int> nodeIds, channels, midiNodeIds;

	void createRenderingOpsForNode (AudioProcessorGraph::Node* const node,
									VoidArray& renderingOps,
									const int ourRenderingIndex)
	{
		const int numIns = node->processor->getNumInputChannels();
		const int numOuts = node->processor->getNumOutputChannels();
		const int totalChans = jmax (numIns, numOuts);

		Array <int> audioChannelsToUse;
		int midiBufferToUse = -1;

		for (int inputChan = 0; inputChan < numIns; ++inputChan)
		{
			// get a list of all the inputs to this node
			Array <int> sourceNodes, sourceOutputChans;

			for (int i = graph.getNumConnections(); --i >= 0;)
			{
				const AudioProcessorGraph::Connection* const c = graph.getConnection (i);

				if (c->destNodeId == node->id && c->destChannelIndex == inputChan)
				{
					sourceNodes.add (c->sourceNodeId);
					sourceOutputChans.add (c->sourceChannelIndex);
				}
			}

			int bufIndex = -1;

			if (sourceNodes.size() == 0)
			{
				// unconnected input channel

				if (inputChan >= numOuts)
				{
					bufIndex = getReadOnlyEmptyBuffer();
					jassert (bufIndex >= 0);
				}
				else
				{
					bufIndex = getFreeBuffer (false);
					renderingOps.add (new ClearChannelOp (bufIndex));
				}
			}
			else if (sourceNodes.size() == 1)
			{
				// channel with a straightforward single input..
				const int srcNode = sourceNodes.getUnchecked(0);
				const int srcChan = sourceOutputChans.getUnchecked(0);

				bufIndex = getBufferContaining (srcNode, srcChan);

				if (bufIndex < 0)
				{
					// if not found, this is probably a feedback loop
					bufIndex = getReadOnlyEmptyBuffer();
					jassert (bufIndex >= 0);
				}

				if (inputChan < numOuts
					 && isBufferNeededLater (ourRenderingIndex,
											 inputChan,
											 srcNode, srcChan))
				{
					// can't mess up this channel because it's needed later by another node, so we
					// need to use a copy of it..
					const int newFreeBuffer = getFreeBuffer (false);

					renderingOps.add (new CopyChannelOp (bufIndex, newFreeBuffer));

					bufIndex = newFreeBuffer;
				}
			}
			else
			{
				// channel with a mix of several inputs..

				// try to find a re-usable channel from our inputs..
				int reusableInputIndex = -1;

				for (int i = 0; i < sourceNodes.size(); ++i)
				{
					const int sourceBufIndex = getBufferContaining (sourceNodes.getUnchecked(i),
																	sourceOutputChans.getUnchecked(i));

					if (sourceBufIndex >= 0
						&& ! isBufferNeededLater (ourRenderingIndex,
												  inputChan,
												  sourceNodes.getUnchecked(i),
												  sourceOutputChans.getUnchecked(i)))
					{
						// we've found one of our input chans that can be re-used..
						reusableInputIndex = i;
						bufIndex = sourceBufIndex;
						break;
					}
				}

				if (reusableInputIndex < 0)
				{
					// can't re-use any of our input chans, so get a new one and copy everything into it..
					bufIndex = getFreeBuffer (false);
					jassert (bufIndex != 0);

					const int srcIndex = getBufferContaining (sourceNodes.getUnchecked (0),
															  sourceOutputChans.getUnchecked (0));
					if (srcIndex < 0)
					{
						// if not found, this is probably a feedback loop
						renderingOps.add (new ClearChannelOp (bufIndex));
					}
					else
					{
						renderingOps.add (new CopyChannelOp (srcIndex, bufIndex));
					}

					reusableInputIndex = 0;
				}

				for (int j = 0; j < sourceNodes.size(); ++j)
				{
					if (j != reusableInputIndex)
					{
						const int srcIndex = getBufferContaining (sourceNodes.getUnchecked(j),
																  sourceOutputChans.getUnchecked(j));
						if (srcIndex >= 0)
							renderingOps.add (new AddChannelOp (srcIndex, bufIndex));
					}
				}
			}

			jassert (bufIndex >= 0);
			audioChannelsToUse.add (bufIndex);

			if (inputChan < numOuts)
				markBufferAsContaining (bufIndex, node->id, inputChan);
		}

		for (int outputChan = numIns; outputChan < numOuts; ++outputChan)
		{
			const int bufIndex = getFreeBuffer (false);
			jassert (bufIndex != 0);
			audioChannelsToUse.add (bufIndex);

			markBufferAsContaining (bufIndex, node->id, outputChan);
		}

		// Now the same thing for midi..
		Array <int> midiSourceNodes;

		for (int i = graph.getNumConnections(); --i >= 0;)
		{
			const AudioProcessorGraph::Connection* const c = graph.getConnection (i);

			if (c->destNodeId == node->id && c->destChannelIndex == AudioProcessorGraph::midiChannelIndex)
				midiSourceNodes.add (c->sourceNodeId);
		}

		if (midiSourceNodes.size() == 0)
		{
			// No midi inputs..
			midiBufferToUse = getFreeBuffer (true); // need to pick a buffer even if the processor doesn't use midi

			if (node->processor->acceptsMidi() || node->processor->producesMidi())
				renderingOps.add (new ClearMidiBufferOp (midiBufferToUse));
		}
		else if (midiSourceNodes.size() == 1)
		{
			// One midi input..
			midiBufferToUse = getBufferContaining (midiSourceNodes.getUnchecked(0),
												   AudioProcessorGraph::midiChannelIndex);

			if (midiBufferToUse >= 0)
			{
				if (isBufferNeededLater (ourRenderingIndex,
										 AudioProcessorGraph::midiChannelIndex,
										 midiSourceNodes.getUnchecked(0),
										 AudioProcessorGraph::midiChannelIndex))
				{
					// can't mess up this channel because it's needed later by another node, so we
					// need to use a copy of it..
					const int newFreeBuffer = getFreeBuffer (true);
					renderingOps.add (new CopyMidiBufferOp (midiBufferToUse, newFreeBuffer));
					midiBufferToUse = newFreeBuffer;
				}
			}
			else
			{
				 // probably a feedback loop, so just use an empty one..
				 midiBufferToUse = getFreeBuffer (true); // need to pick a buffer even if the processor doesn't use midi
			}
		}
		else
		{
			// More than one midi input being mixed..
			int reusableInputIndex = -1;

			for (int i = 0; i < midiSourceNodes.size(); ++i)
			{
				const int sourceBufIndex = getBufferContaining (midiSourceNodes.getUnchecked(i),
																AudioProcessorGraph::midiChannelIndex);

				if (sourceBufIndex >= 0
					 && ! isBufferNeededLater (ourRenderingIndex,
											   AudioProcessorGraph::midiChannelIndex,
											   midiSourceNodes.getUnchecked(i),
											   AudioProcessorGraph::midiChannelIndex))
				{
					// we've found one of our input buffers that can be re-used..
					reusableInputIndex = i;
					midiBufferToUse = sourceBufIndex;
					break;
				}
			}

			if (reusableInputIndex < 0)
			{
				// can't re-use any of our input buffers, so get a new one and copy everything into it..
				midiBufferToUse = getFreeBuffer (true);
				jassert (midiBufferToUse >= 0);

				const int srcIndex = getBufferContaining (midiSourceNodes.getUnchecked(0),
														  AudioProcessorGraph::midiChannelIndex);
				if (srcIndex >= 0)
					renderingOps.add (new CopyMidiBufferOp (srcIndex, midiBufferToUse));
				else
					renderingOps.add (new ClearMidiBufferOp (midiBufferToUse));

				reusableInputIndex = 0;
			}

			for (int j = 0; j < midiSourceNodes.size(); ++j)
			{
				if (j != reusableInputIndex)
				{
					const int srcIndex = getBufferContaining (midiSourceNodes.getUnchecked(j),
															  AudioProcessorGraph::midiChannelIndex);
					if (srcIndex >= 0)
						renderingOps.add (new AddMidiBufferOp (srcIndex, midiBufferToUse));
				}
			}
		}

		if (node->processor->producesMidi())
			markBufferAsContaining (midiBufferToUse, node->id,
									AudioProcessorGraph::midiChannelIndex);

		renderingOps.add (new ProcessBufferOp (node, audioChannelsToUse,
											   totalChans, midiBufferToUse));
	}

	int getFreeBuffer (const bool forMidi)
	{
		if (forMidi)
		{
			for (int i = 1; i < midiNodeIds.size(); ++i)
				if (midiNodeIds.getUnchecked(i) < 0)
					return i;

			midiNodeIds.add (-1);
			return midiNodeIds.size() - 1;
		}
		else
		{
			for (int i = 1; i < nodeIds.size(); ++i)
				if (nodeIds.getUnchecked(i) < 0)
					return i;

			nodeIds.add (-1);
			channels.add (0);
			return nodeIds.size() - 1;
		}
	}

	int getReadOnlyEmptyBuffer() const
	{
		return 0;
	}

	int getBufferContaining (const int nodeId, const int outputChannel) const
	{
		if (outputChannel == AudioProcessorGraph::midiChannelIndex)
		{
			for (int i = midiNodeIds.size(); --i >= 0;)
				if (midiNodeIds.getUnchecked(i) == nodeId)
					return i;
		}
		else
		{
			for (int i = nodeIds.size(); --i >= 0;)
				if (nodeIds.getUnchecked(i) == nodeId
					 && channels.getUnchecked(i) == outputChannel)
					return i;
		}

		return -1;
	}

	void markAnyUnusedBuffersAsFree (const int stepIndex)
	{
		int i;
		for (i = 0; i < nodeIds.size(); ++i)
		{
			if (nodeIds.getUnchecked(i) >= 0
				 && ! isBufferNeededLater (stepIndex, -1,
										   nodeIds.getUnchecked(i),
										   channels.getUnchecked(i)))
			{
				nodeIds.set (i, -1);
			}
		}

		for (i = 0; i < midiNodeIds.size(); ++i)
		{
			if (midiNodeIds.getUnchecked(i) >= 0
				 && ! isBufferNeededLater (stepIndex, -1,
										   midiNodeIds.getUnchecked(i),
										   AudioProcessorGraph::midiChannelIndex))
			{
				midiNodeIds.set (i, -1);
			}
		}
	}

	bool isBufferNeededLater (int stepIndexToSearchFrom,
							  int inputChannelOfIndexToIgnore,
							  const int nodeId,
							  const int outputChanIndex) const
	{
		while (stepIndexToSearchFrom < orderedNodes.size())
		{
			const AudioProcessorGraph::Node* const node = (const AudioProcessorGraph::Node*) orderedNodes.getUnchecked (stepIndexToSearchFrom);

			if (outputChanIndex == AudioProcessorGraph::midiChannelIndex)
			{
				if (inputChannelOfIndexToIgnore != AudioProcessorGraph::midiChannelIndex
					 && graph.getConnectionBetween (nodeId, AudioProcessorGraph::midiChannelIndex,
													node->id, AudioProcessorGraph::midiChannelIndex) != 0)
					return true;
			}
			else
			{
				for (int i = 0; i < node->processor->getNumInputChannels(); ++i)
					if (i != inputChannelOfIndexToIgnore
						 && graph.getConnectionBetween (nodeId, outputChanIndex,
														node->id, i) != 0)
						return true;
			}

			inputChannelOfIndexToIgnore = -1;
			++stepIndexToSearchFrom;
		}

		return false;
	}

	void markBufferAsContaining (int bufferNum, int nodeId, int outputIndex)
	{
		if (outputIndex == AudioProcessorGraph::midiChannelIndex)
		{
			jassert (bufferNum > 0 && bufferNum < midiNodeIds.size());

			midiNodeIds.set (bufferNum, nodeId);
		}
		else
		{
			jassert (bufferNum >= 0 && bufferNum < nodeIds.size());

			nodeIds.set (bufferNum, nodeId);
			channels.set (bufferNum, outputIndex);
		}
	}

	RenderingOpSequenceCalculator (const RenderingOpSequenceCalculator&);
	RenderingOpSequenceCalculator& operator= (const RenderingOpSequenceCalculator&);
};

}

void AudioProcessorGraph::clearRenderingSequence()
{
	const ScopedLock sl (renderLock);

	for (int i = renderingOps.size(); --i >= 0;)
	{
		GraphRenderingOps::AudioGraphRenderingOp* const r
			= (GraphRenderingOps::AudioGraphRenderingOp*) renderingOps.getUnchecked(i);

		renderingOps.remove (i);
		delete r;
	}
}

bool AudioProcessorGraph::isAnInputTo (const uint32 possibleInputId,
									   const uint32 possibleDestinationId,
									   const int recursionCheck) const
{
	if (recursionCheck > 0)
	{
		for (int i = connections.size(); --i >= 0;)
		{
			const AudioProcessorGraph::Connection* const c = connections.getUnchecked (i);

			if (c->destNodeId == possibleDestinationId
				 && (c->sourceNodeId == possibleInputId
					  || isAnInputTo (possibleInputId, c->sourceNodeId, recursionCheck - 1)))
				return true;
		}
	}

	return false;
}

void AudioProcessorGraph::buildRenderingSequence()
{
	VoidArray newRenderingOps;
	int numRenderingBuffersNeeded = 2;
	int numMidiBuffersNeeded = 1;

	{
		MessageManagerLock mml;

		VoidArray orderedNodes;

		int i;
		for (i = 0; i < nodes.size(); ++i)
		{
			Node* const node = nodes.getUnchecked(i);

			node->prepare (getSampleRate(), getBlockSize(), this);

			int j = 0;
			for (; j < orderedNodes.size(); ++j)
				if (isAnInputTo (node->id,
								 ((Node*) orderedNodes.getUnchecked (j))->id,
								 nodes.size() + 1))
					break;

			orderedNodes.insert (j, node);
		}

		GraphRenderingOps::RenderingOpSequenceCalculator calculator (*this, orderedNodes, newRenderingOps);

		numRenderingBuffersNeeded = calculator.getNumBuffersNeeded();
		numMidiBuffersNeeded = calculator.getNumMidiBuffersNeeded();
	}

	VoidArray oldRenderingOps (renderingOps);

	{
		// swap over to the new rendering sequence..
		const ScopedLock sl (renderLock);

		renderingBuffers.setSize (numRenderingBuffersNeeded, getBlockSize());
		renderingBuffers.clear();

		for (int i = midiBuffers.size(); --i >= 0;)
			midiBuffers.getUnchecked(i)->clear();

		while (midiBuffers.size() < numMidiBuffersNeeded)
			midiBuffers.add (new MidiBuffer());

		renderingOps = newRenderingOps;
	}

	for (int i = oldRenderingOps.size(); --i >= 0;)
		delete (GraphRenderingOps::AudioGraphRenderingOp*) oldRenderingOps.getUnchecked(i);
}

void AudioProcessorGraph::handleAsyncUpdate()
{
	buildRenderingSequence();
}

void AudioProcessorGraph::prepareToPlay (double /*sampleRate*/, int estimatedSamplesPerBlock)
{
	currentAudioInputBuffer = 0;
	currentAudioOutputBuffer.setSize (jmax (1, getNumOutputChannels()), estimatedSamplesPerBlock);
	currentMidiInputBuffer = 0;
	currentMidiOutputBuffer.clear();

	clearRenderingSequence();
	buildRenderingSequence();
}

void AudioProcessorGraph::releaseResources()
{
	for (int i = 0; i < nodes.size(); ++i)
		nodes.getUnchecked(i)->unprepare();

	renderingBuffers.setSize (1, 1);
	midiBuffers.clear();

	currentAudioInputBuffer = 0;
	currentAudioOutputBuffer.setSize (1, 1);
	currentMidiInputBuffer = 0;
	currentMidiOutputBuffer.clear();
}

void AudioProcessorGraph::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
{
	const int numSamples = buffer.getNumSamples();

	const ScopedLock sl (renderLock);

	currentAudioInputBuffer = &buffer;
	currentAudioOutputBuffer.setSize (jmax (1, buffer.getNumChannels()), numSamples);
	currentAudioOutputBuffer.clear();
	currentMidiInputBuffer = &midiMessages;
	currentMidiOutputBuffer.clear();

	int i;
	for (i = 0; i < renderingOps.size(); ++i)
	{
		GraphRenderingOps::AudioGraphRenderingOp* const op
			= (GraphRenderingOps::AudioGraphRenderingOp*) renderingOps.getUnchecked(i);

		op->perform (renderingBuffers, midiBuffers, numSamples);
	}

	for (i = 0; i < buffer.getNumChannels(); ++i)
		buffer.copyFrom (i, 0, currentAudioOutputBuffer, i, 0, numSamples);

	midiMessages.clear();
	midiMessages.addEvents (currentMidiOutputBuffer, 0, buffer.getNumSamples(), 0);
}

const String AudioProcessorGraph::getInputChannelName (const int channelIndex) const
{
	return "Input " + String (channelIndex + 1);
}

const String AudioProcessorGraph::getOutputChannelName (const int channelIndex) const
{
	return "Output " + String (channelIndex + 1);
}

bool AudioProcessorGraph::isInputChannelStereoPair (int /*index*/) const
{
	return true;
}

bool AudioProcessorGraph::isOutputChannelStereoPair (int /*index*/) const
{
	return true;
}

bool AudioProcessorGraph::acceptsMidi() const
{
	return true;
}

bool AudioProcessorGraph::producesMidi() const
{
	return true;
}

void AudioProcessorGraph::getStateInformation (JUCE_NAMESPACE::MemoryBlock& /*destData*/)
{
}

void AudioProcessorGraph::setStateInformation (const void* /*data*/, int /*sizeInBytes*/)
{
}

AudioProcessorGraph::AudioGraphIOProcessor::AudioGraphIOProcessor (const IODeviceType type_)
	: type (type_),
	  graph (0)
{
}

AudioProcessorGraph::AudioGraphIOProcessor::~AudioGraphIOProcessor()
{
}

const String AudioProcessorGraph::AudioGraphIOProcessor::getName() const
{
	switch (type)
	{
	case audioOutputNode:
		return "Audio Output";
	case audioInputNode:
		return "Audio Input";
	case midiOutputNode:
		return "Midi Output";
	case midiInputNode:
		return "Midi Input";
	default:
		break;
	}

	return String::empty;
}

void AudioProcessorGraph::AudioGraphIOProcessor::fillInPluginDescription (PluginDescription& d) const
{
	d.name = getName();
	d.uid = d.name.hashCode();
	d.category = "I/O devices";
	d.pluginFormatName = "Internal";
	d.manufacturerName = "Raw Material Software";
	d.version = "1.0";
	d.isInstrument = false;

	d.numInputChannels = getNumInputChannels();
	if (type == audioOutputNode && graph != 0)
		d.numInputChannels = graph->getNumInputChannels();

	d.numOutputChannels = getNumOutputChannels();
	if (type == audioInputNode && graph != 0)
		d.numOutputChannels = graph->getNumOutputChannels();
}

void AudioProcessorGraph::AudioGraphIOProcessor::prepareToPlay (double, int)
{
	jassert (graph != 0);
}

void AudioProcessorGraph::AudioGraphIOProcessor::releaseResources()
{
}

void AudioProcessorGraph::AudioGraphIOProcessor::processBlock (AudioSampleBuffer& buffer,
															   MidiBuffer& midiMessages)
{
	jassert (graph != 0);

	switch (type)
	{
	case audioOutputNode:
	{
		for (int i = jmin (graph->currentAudioOutputBuffer.getNumChannels(),
						   buffer.getNumChannels()); --i >= 0;)
		{
			graph->currentAudioOutputBuffer.addFrom (i, 0, buffer, i, 0, buffer.getNumSamples());
		}

		break;
	}

	case audioInputNode:
	{
		for (int i = jmin (graph->currentAudioInputBuffer->getNumChannels(),
						   buffer.getNumChannels()); --i >= 0;)
		{
			buffer.copyFrom (i, 0, *graph->currentAudioInputBuffer, i, 0, buffer.getNumSamples());
		}

		break;
	}

	case midiOutputNode:
		graph->currentMidiOutputBuffer.addEvents (midiMessages, 0, buffer.getNumSamples(), 0);
		break;

	case midiInputNode:
		midiMessages.addEvents (*graph->currentMidiInputBuffer, 0, buffer.getNumSamples(), 0);
		break;

	default:
		break;
	}
}

bool AudioProcessorGraph::AudioGraphIOProcessor::acceptsMidi() const
{
	return type == midiOutputNode;
}

bool AudioProcessorGraph::AudioGraphIOProcessor::producesMidi() const
{
	return type == midiInputNode;
}

const String AudioProcessorGraph::AudioGraphIOProcessor::getInputChannelName (const int channelIndex) const
{
	switch (type)
	{
	case audioOutputNode:
		return "Output " + String (channelIndex + 1);
	case midiOutputNode:
		return "Midi Output";
	default:
		break;
	}

	return String::empty;
}

const String AudioProcessorGraph::AudioGraphIOProcessor::getOutputChannelName (const int channelIndex) const
{
	switch (type)
	{
	case audioInputNode:
		return "Input " + String (channelIndex + 1);
	case midiInputNode:
		return "Midi Input";
	default:
		break;
	}

	return String::empty;
}

bool AudioProcessorGraph::AudioGraphIOProcessor::isInputChannelStereoPair (int /*index*/) const
{
	return type == audioInputNode || type == audioOutputNode;
}

bool AudioProcessorGraph::AudioGraphIOProcessor::isOutputChannelStereoPair (int index) const
{
	return isInputChannelStereoPair (index);
}

bool AudioProcessorGraph::AudioGraphIOProcessor::isInput() const
{
	return type == audioInputNode || type == midiInputNode;
}

bool AudioProcessorGraph::AudioGraphIOProcessor::isOutput() const
{
	return type == audioOutputNode || type == midiOutputNode;
}

AudioProcessorEditor* AudioProcessorGraph::AudioGraphIOProcessor::createEditor()
{
	return 0;
}

int AudioProcessorGraph::AudioGraphIOProcessor::getNumParameters()		  { return 0; }
const String AudioProcessorGraph::AudioGraphIOProcessor::getParameterName (int)	 { return String::empty; }

float AudioProcessorGraph::AudioGraphIOProcessor::getParameter (int)		{ return 0.0f; }
const String AudioProcessorGraph::AudioGraphIOProcessor::getParameterText (int)	 { return String::empty; }
void AudioProcessorGraph::AudioGraphIOProcessor::setParameter (int, float)	  { }

int AudioProcessorGraph::AudioGraphIOProcessor::getNumPrograms()			{ return 0; }
int AudioProcessorGraph::AudioGraphIOProcessor::getCurrentProgram()		 { return 0; }
void AudioProcessorGraph::AudioGraphIOProcessor::setCurrentProgram (int)		{ }

const String AudioProcessorGraph::AudioGraphIOProcessor::getProgramName (int)	   { return String::empty; }
void AudioProcessorGraph::AudioGraphIOProcessor::changeProgramName (int, const String&)  { }

void AudioProcessorGraph::AudioGraphIOProcessor::getStateInformation (JUCE_NAMESPACE::MemoryBlock&)
{
}

void AudioProcessorGraph::AudioGraphIOProcessor::setStateInformation (const void*, int)
{
}

void AudioProcessorGraph::AudioGraphIOProcessor::setParentGraph (AudioProcessorGraph* const newGraph)
{
	graph = newGraph;

	if (graph != 0)
	{
		setPlayConfigDetails (type == audioOutputNode ? graph->getNumOutputChannels() : 0,
							  type == audioInputNode ? graph->getNumInputChannels() : 0,
							  getSampleRate(),
							  getBlockSize());

		updateHostDisplay();
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioProcessorGraph.cpp ***/


/*** Start of inlined file: juce_AudioProcessorPlayer.cpp ***/
BEGIN_JUCE_NAMESPACE

AudioProcessorPlayer::AudioProcessorPlayer()
	: processor (0),
	  sampleRate (0),
	  blockSize (0),
	  isPrepared (false),
	  numInputChans (0),
	  numOutputChans (0),
	  tempBuffer (1, 1)
{
}

AudioProcessorPlayer::~AudioProcessorPlayer()
{
	setProcessor (0);
}

void AudioProcessorPlayer::setProcessor (AudioProcessor* const processorToPlay)
{
	if (processor != processorToPlay)
	{
		if (processorToPlay != 0 && sampleRate > 0 && blockSize > 0)
		{
			processorToPlay->setPlayConfigDetails (numInputChans, numOutputChans,
												   sampleRate, blockSize);

			processorToPlay->prepareToPlay (sampleRate, blockSize);
		}

		AudioProcessor* oldOne;

		{
			const ScopedLock sl (lock);
			oldOne = isPrepared ? processor : 0;
			processor = processorToPlay;
			isPrepared = true;
		}

		if (oldOne != 0)
			oldOne->releaseResources();
	}
}

void AudioProcessorPlayer::audioDeviceIOCallback (const float** inputChannelData,
												  int numInputChannels,
												  float** outputChannelData,
												  int numOutputChannels,
												  int numSamples)
{
	// these should have been prepared by audioDeviceAboutToStart()...
	jassert (sampleRate > 0 && blockSize > 0);

	incomingMidi.clear();
	messageCollector.removeNextBlockOfMessages (incomingMidi, numSamples);
	int i, totalNumChans = 0;

	if (numInputChannels > numOutputChannels)
	{
		// if there aren't enough output channels for the number of
		// inputs, we need to create some temporary extra ones (can't
		// use the input data in case it gets written to)
		tempBuffer.setSize (numInputChannels - numOutputChannels, numSamples,
							false, false, true);

		for (i = 0; i < numOutputChannels; ++i)
		{
			channels[totalNumChans] = outputChannelData[i];
			memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * numSamples);
			++totalNumChans;
		}

		for (i = numOutputChannels; i < numInputChannels; ++i)
		{
			channels[totalNumChans] = tempBuffer.getSampleData (i - numOutputChannels, 0);
			memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * numSamples);
			++totalNumChans;
		}
	}
	else
	{
		for (i = 0; i < numInputChannels; ++i)
		{
			channels[totalNumChans] = outputChannelData[i];
			memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * numSamples);
			++totalNumChans;
		}

		for (i = numInputChannels; i < numOutputChannels; ++i)
		{
			channels[totalNumChans] = outputChannelData[i];
			zeromem (channels[totalNumChans], sizeof (float) * numSamples);
			++totalNumChans;
		}
	}

	AudioSampleBuffer buffer (channels, totalNumChans, numSamples);

	const ScopedLock sl (lock);

	if (processor != 0)
		processor->processBlock (buffer, incomingMidi);
}

void AudioProcessorPlayer::audioDeviceAboutToStart (AudioIODevice* device)
{
	const ScopedLock sl (lock);

	sampleRate = device->getCurrentSampleRate();
	blockSize = device->getCurrentBufferSizeSamples();
	numInputChans = device->getActiveInputChannels().countNumberOfSetBits();
	numOutputChans = device->getActiveOutputChannels().countNumberOfSetBits();

	messageCollector.reset (sampleRate);
	zeromem (channels, sizeof (channels));

	if (processor != 0)
	{
		if (isPrepared)
			processor->releaseResources();

		AudioProcessor* const oldProcessor = processor;
		setProcessor (0);
		setProcessor (oldProcessor);
	}
}

void AudioProcessorPlayer::audioDeviceStopped()
{
	const ScopedLock sl (lock);

	if (processor != 0 && isPrepared)
		processor->releaseResources();

	sampleRate = 0.0;
	blockSize = 0;
	isPrepared = false;
	tempBuffer.setSize (1, 1);
}

void AudioProcessorPlayer::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
{
	messageCollector.addMessageToQueue (message);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioProcessorPlayer.cpp ***/


/*** Start of inlined file: juce_GenericAudioProcessorEditor.cpp ***/
BEGIN_JUCE_NAMESPACE

class ProcessorParameterPropertyComp   : public PropertyComponent,
										 public AudioProcessorListener,
										 public AsyncUpdater
{
public:
	ProcessorParameterPropertyComp (const String& name,
									AudioProcessor* const owner_,
									const int index_)
		: PropertyComponent (name),
		  owner (owner_),
		  index (index_)
	{
		addAndMakeVisible (slider = new ParamSlider (owner_, index_));
		owner_->addListener (this);
	}

	~ProcessorParameterPropertyComp()
	{
		owner->removeListener (this);
		deleteAllChildren();
	}

	void refresh()
	{
		slider->setValue (owner->getParameter (index), false);
	}

	void audioProcessorChanged (AudioProcessor*)  {}

	void audioProcessorParameterChanged (AudioProcessor*, int parameterIndex, float)
	{
		if (parameterIndex == index)
			triggerAsyncUpdate();
	}

	void handleAsyncUpdate()
	{
		refresh();
	}

	juce_UseDebuggingNewOperator

private:
	AudioProcessor* const owner;
	const int index;
	Slider* slider;

	class ParamSlider  : public Slider
	{
	public:
		ParamSlider (AudioProcessor* const owner_, const int index_)
			: Slider (String::empty),
			  owner (owner_),
			  index (index_)
		{
			setRange (0.0, 1.0, 0.0);
			setSliderStyle (Slider::LinearBar);
			setTextBoxIsEditable (false);
			setScrollWheelEnabled (false);
		}

		~ParamSlider()
		{
		}

		void valueChanged()
		{
			const float newVal = (float) getValue();

			if (owner->getParameter (index) != newVal)
				owner->setParameter (index, newVal);
		}

		const String getTextFromValue (double /*value*/)
		{
			return owner->getParameterText (index);
		}

		juce_UseDebuggingNewOperator

	private:
		AudioProcessor* const owner;
		const int index;

		ParamSlider (const ParamSlider&);
		ParamSlider& operator= (const ParamSlider&);
	};

	ProcessorParameterPropertyComp (const ProcessorParameterPropertyComp&);
	ProcessorParameterPropertyComp& operator= (const ProcessorParameterPropertyComp&);
};

GenericAudioProcessorEditor::GenericAudioProcessorEditor (AudioProcessor* const owner_)
	: AudioProcessorEditor (owner_)
{
	setOpaque (true);

	addAndMakeVisible (panel = new PropertyPanel());

	Array <PropertyComponent*> params;

	const int numParams = owner_->getNumParameters();
	int totalHeight = 0;

	for (int i = 0; i < numParams; ++i)
	{
		String name (owner_->getParameterName (i));
		if (name.trim().isEmpty())
			name = "Unnamed";

		ProcessorParameterPropertyComp* const pc = new ProcessorParameterPropertyComp (name, owner_, i);
		params.add (pc);
		totalHeight += pc->getPreferredHeight();
	}

	panel->addProperties (params);

	setSize (400, jlimit (25, 400, totalHeight));
}

GenericAudioProcessorEditor::~GenericAudioProcessorEditor()
{
	deleteAllChildren();
}

void GenericAudioProcessorEditor::paint (Graphics& g)
{
	g.fillAll (Colours::white);
}

void GenericAudioProcessorEditor::resized()
{
	panel->setSize (getWidth(), getHeight());
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_GenericAudioProcessorEditor.cpp ***/


/*** Start of inlined file: juce_Sampler.cpp ***/
BEGIN_JUCE_NAMESPACE

SamplerSound::SamplerSound (const String& name_,
							AudioFormatReader& source,
							const BigInteger& midiNotes_,
							const int midiNoteForNormalPitch,
							const double attackTimeSecs,
							const double releaseTimeSecs,
							const double maxSampleLengthSeconds)
	: name (name_),
	  midiNotes (midiNotes_),
	  midiRootNote (midiNoteForNormalPitch)
{
	sourceSampleRate = source.sampleRate;

	if (sourceSampleRate <= 0 || source.lengthInSamples <= 0)
	{
		length = 0;
		attackSamples = 0;
		releaseSamples = 0;
	}
	else
	{
		length = jmin ((int) source.lengthInSamples,
					   (int) (maxSampleLengthSeconds * sourceSampleRate));

		data = new AudioSampleBuffer (jmin (2, (int) source.numChannels), length + 4);

		data->readFromAudioReader (&source, 0, length + 4, 0, true, true);

		attackSamples = roundToInt (attackTimeSecs * sourceSampleRate);
		releaseSamples = roundToInt (releaseTimeSecs * sourceSampleRate);
	}
}

SamplerSound::~SamplerSound()
{
}

bool SamplerSound::appliesToNote (const int midiNoteNumber)
{
	return midiNotes [midiNoteNumber];
}

bool SamplerSound::appliesToChannel (const int /*midiChannel*/)
{
	return true;
}

SamplerVoice::SamplerVoice()
	: pitchRatio (0.0),
	  sourceSamplePosition (0.0),
	  lgain (0.0f),
	  rgain (0.0f),
	  isInAttack (false),
	  isInRelease (false)
{
}

SamplerVoice::~SamplerVoice()
{
}

bool SamplerVoice::canPlaySound (SynthesiserSound* sound)
{
	return dynamic_cast <const SamplerSound*> (sound) != 0;
}

void SamplerVoice::startNote (const int midiNoteNumber,
							  const float velocity,
							  SynthesiserSound* s,
							  const int /*currentPitchWheelPosition*/)
{
	const SamplerSound* const sound = dynamic_cast <const SamplerSound*> (s);
	jassert (sound != 0); // this object can only play SamplerSounds!

	if (sound != 0)
	{
		const double targetFreq = MidiMessage::getMidiNoteInHertz (midiNoteNumber);
		const double naturalFreq = MidiMessage::getMidiNoteInHertz (sound->midiRootNote);

		pitchRatio = (targetFreq * sound->sourceSampleRate) / (naturalFreq * getSampleRate());

		sourceSamplePosition = 0.0;
		lgain = velocity;
		rgain = velocity;

		isInAttack = (sound->attackSamples > 0);
		isInRelease = false;

		if (isInAttack)
		{
			attackReleaseLevel = 0.0f;
			attackDelta = (float) (pitchRatio / sound->attackSamples);
		}
		else
		{
			attackReleaseLevel = 1.0f;
			attackDelta = 0.0f;
		}

		if (sound->releaseSamples > 0)
		{
			releaseDelta = (float) (-pitchRatio / sound->releaseSamples);
		}
		else
		{
			releaseDelta = 0.0f;
		}
	}
}

void SamplerVoice::stopNote (const bool allowTailOff)
{
	if (allowTailOff)
	{
		isInAttack = false;
		isInRelease = true;
	}
	else
	{
		clearCurrentNote();
	}
}

void SamplerVoice::pitchWheelMoved (const int /*newValue*/)
{
}

void SamplerVoice::controllerMoved (const int /*controllerNumber*/,
									const int /*newValue*/)
{
}

void SamplerVoice::renderNextBlock (AudioSampleBuffer& outputBuffer, int startSample, int numSamples)
{
	const SamplerSound* const playingSound = static_cast <SamplerSound*> (getCurrentlyPlayingSound().getObject());

	if (playingSound != 0)
	{
		const float* const inL = playingSound->data->getSampleData (0, 0);
		const float* const inR = playingSound->data->getNumChannels() > 1
									? playingSound->data->getSampleData (1, 0) : 0;

		float* outL = outputBuffer.getSampleData (0, startSample);
		float* outR = outputBuffer.getNumChannels() > 1 ? outputBuffer.getSampleData (1, startSample) : 0;

		while (--numSamples >= 0)
		{
			const int pos = (int) sourceSamplePosition;
			const float alpha = (float) (sourceSamplePosition - pos);
			const float invAlpha = 1.0f - alpha;

			// just using a very simple linear interpolation here..
			float l = (inL [pos] * invAlpha + inL [pos + 1] * alpha);
			float r = (inR != 0) ? (inR [pos] * invAlpha + inR [pos + 1] * alpha)
								 : l;

			l *= lgain;
			r *= rgain;

			if (isInAttack)
			{
				l *= attackReleaseLevel;
				r *= attackReleaseLevel;

				attackReleaseLevel += attackDelta;

				if (attackReleaseLevel >= 1.0f)
				{
					attackReleaseLevel = 1.0f;
					isInAttack = false;
				}
			}
			else if (isInRelease)
			{
				l *= attackReleaseLevel;
				r *= attackReleaseLevel;

				attackReleaseLevel += releaseDelta;

				if (attackReleaseLevel <= 0.0f)
				{
					stopNote (false);
					break;
				}
			}

			if (outR != 0)
			{
				*outL++ += l;
				*outR++ += r;
			}
			else
			{
				*outL++ += (l + r) * 0.5f;
			}

			sourceSamplePosition += pitchRatio;

			if (sourceSamplePosition > playingSound->length)
			{
				stopNote (false);
				break;
			}
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Sampler.cpp ***/


/*** Start of inlined file: juce_Synthesiser.cpp ***/
BEGIN_JUCE_NAMESPACE

SynthesiserSound::SynthesiserSound()
{
}

SynthesiserSound::~SynthesiserSound()
{
}

SynthesiserVoice::SynthesiserVoice()
	: currentSampleRate (44100.0),
	  currentlyPlayingNote (-1),
	  noteOnTime (0),
	  currentlyPlayingSound (0)
{
}

SynthesiserVoice::~SynthesiserVoice()
{
}

bool SynthesiserVoice::isPlayingChannel (const int midiChannel) const
{
	return currentlyPlayingSound != 0
			&& currentlyPlayingSound->appliesToChannel (midiChannel);
}

void SynthesiserVoice::setCurrentPlaybackSampleRate (const double newRate)
{
	currentSampleRate = newRate;
}

void SynthesiserVoice::clearCurrentNote()
{
	currentlyPlayingNote = -1;
	currentlyPlayingSound = 0;
}

Synthesiser::Synthesiser()
	: sampleRate (0),
	  lastNoteOnCounter (0),
	  shouldStealNotes (true)
{
	for (int i = 0; i < numElementsInArray (lastPitchWheelValues); ++i)
		lastPitchWheelValues[i] = 0x2000;
}

Synthesiser::~Synthesiser()
{
}

SynthesiserVoice* Synthesiser::getVoice (const int index) const
{
	const ScopedLock sl (lock);
	return voices [index];
}

void Synthesiser::clearVoices()
{
	const ScopedLock sl (lock);
	voices.clear();
}

void Synthesiser::addVoice (SynthesiserVoice* const newVoice)
{
	const ScopedLock sl (lock);
	voices.add (newVoice);
}

void Synthesiser::removeVoice (const int index)
{
	const ScopedLock sl (lock);
	voices.remove (index);
}

void Synthesiser::clearSounds()
{
	const ScopedLock sl (lock);
	sounds.clear();
}

void Synthesiser::addSound (const SynthesiserSound::Ptr& newSound)
{
	const ScopedLock sl (lock);
	sounds.add (newSound);
}

void Synthesiser::removeSound (const int index)
{
	const ScopedLock sl (lock);
	sounds.remove (index);
}

void Synthesiser::setNoteStealingEnabled (const bool shouldStealNotes_)
{
	shouldStealNotes = shouldStealNotes_;
}

void Synthesiser::setCurrentPlaybackSampleRate (const double newRate)
{
	if (sampleRate != newRate)
	{
		const ScopedLock sl (lock);

		allNotesOff (0, false);

		sampleRate = newRate;

		for (int i = voices.size(); --i >= 0;)
			voices.getUnchecked (i)->setCurrentPlaybackSampleRate (newRate);
	}
}

void Synthesiser::renderNextBlock (AudioSampleBuffer& outputBuffer,
								   const MidiBuffer& midiData,
								   int startSample,
								   int numSamples)
{
	// must set the sample rate before using this!
	jassert (sampleRate != 0);

	const ScopedLock sl (lock);

	MidiBuffer::Iterator midiIterator (midiData);
	midiIterator.setNextSamplePosition (startSample);
	MidiMessage m (0xf4, 0.0);

	while (numSamples > 0)
	{
		int midiEventPos;
		const bool useEvent = midiIterator.getNextEvent (m, midiEventPos)
								&& midiEventPos < startSample + numSamples;

		const int numThisTime = useEvent ? midiEventPos - startSample
										 : numSamples;

		if (numThisTime > 0)
		{
			for (int i = voices.size(); --i >= 0;)
				voices.getUnchecked (i)->renderNextBlock (outputBuffer, startSample, numThisTime);
		}

		if (useEvent)
		{
			if (m.isNoteOn())
			{
				const int channel = m.getChannel();

				noteOn (channel,
						m.getNoteNumber(),
						m.getFloatVelocity());
			}
			else if (m.isNoteOff())
			{
				noteOff (m.getChannel(),
						 m.getNoteNumber(),
						 true);
			}
			else if (m.isAllNotesOff() || m.isAllSoundOff())
			{
				allNotesOff (m.getChannel(), true);
			}
			else if (m.isPitchWheel())
			{
				const int channel = m.getChannel();
				const int wheelPos = m.getPitchWheelValue();
				lastPitchWheelValues [channel - 1] = wheelPos;

				handlePitchWheel (channel, wheelPos);
			}
			else if (m.isController())
			{
				handleController (m.getChannel(),
								  m.getControllerNumber(),
								  m.getControllerValue());
			}
		}

		startSample += numThisTime;
		numSamples -= numThisTime;
	}
}

void Synthesiser::noteOn (const int midiChannel,
						  const int midiNoteNumber,
						  const float velocity)
{
	const ScopedLock sl (lock);

	for (int i = sounds.size(); --i >= 0;)
	{
		SynthesiserSound* const sound = sounds.getUnchecked(i);

		if (sound->appliesToNote (midiNoteNumber)
			 && sound->appliesToChannel (midiChannel))
		{
			startVoice (findFreeVoice (sound, shouldStealNotes),
						sound, midiChannel, midiNoteNumber, velocity);
		}
	}
}

void Synthesiser::startVoice (SynthesiserVoice* const voice,
							  SynthesiserSound* const sound,
							  const int midiChannel,
							  const int midiNoteNumber,
							  const float velocity)
{
	if (voice != 0 && sound != 0)
	{
		if (voice->currentlyPlayingSound != 0)
			voice->stopNote (false);

		voice->startNote (midiNoteNumber,
						  velocity,
						  sound,
						  lastPitchWheelValues [midiChannel - 1]);

		voice->currentlyPlayingNote = midiNoteNumber;
		voice->noteOnTime = ++lastNoteOnCounter;
		voice->currentlyPlayingSound = sound;
	}
}

void Synthesiser::noteOff (const int midiChannel,
						   const int midiNoteNumber,
						   const bool allowTailOff)
{
	const ScopedLock sl (lock);

	for (int i = voices.size(); --i >= 0;)
	{
		SynthesiserVoice* const voice = voices.getUnchecked (i);

		if (voice->getCurrentlyPlayingNote() == midiNoteNumber)
		{
			SynthesiserSound* const sound = voice->getCurrentlyPlayingSound();

			if (sound != 0
				 && sound->appliesToNote (midiNoteNumber)
				 && sound->appliesToChannel (midiChannel))
			{
				voice->stopNote (allowTailOff);

				// the subclass MUST call clearCurrentNote() if it's not tailing off! RTFM for stopNote()!
				jassert (allowTailOff || (voice->getCurrentlyPlayingNote() < 0 && voice->getCurrentlyPlayingSound() == 0));
			}
		}
	}
}

void Synthesiser::allNotesOff (const int midiChannel,
							   const bool allowTailOff)
{
	const ScopedLock sl (lock);

	for (int i = voices.size(); --i >= 0;)
	{
		SynthesiserVoice* const voice = voices.getUnchecked (i);

		if (midiChannel <= 0 || voice->isPlayingChannel (midiChannel))
			voice->stopNote (allowTailOff);
	}
}

void Synthesiser::handlePitchWheel (const int midiChannel,
									const int wheelValue)
{
	const ScopedLock sl (lock);

	for (int i = voices.size(); --i >= 0;)
	{
		SynthesiserVoice* const voice = voices.getUnchecked (i);

		if (midiChannel <= 0 || voice->isPlayingChannel (midiChannel))
		{
			voice->pitchWheelMoved (wheelValue);
		}
	}
}

void Synthesiser::handleController (const int midiChannel,
									const int controllerNumber,
									const int controllerValue)
{
	const ScopedLock sl (lock);

	for (int i = voices.size(); --i >= 0;)
	{
		SynthesiserVoice* const voice = voices.getUnchecked (i);

		if (midiChannel <= 0 || voice->isPlayingChannel (midiChannel))
			voice->controllerMoved (controllerNumber, controllerValue);
	}
}

SynthesiserVoice* Synthesiser::findFreeVoice (SynthesiserSound* soundToPlay,
											  const bool stealIfNoneAvailable) const
{
	const ScopedLock sl (lock);

	for (int i = voices.size(); --i >= 0;)
		if (voices.getUnchecked (i)->getCurrentlyPlayingNote() < 0
			 && voices.getUnchecked (i)->canPlaySound (soundToPlay))
			return voices.getUnchecked (i);

	if (stealIfNoneAvailable)
	{
		// currently this just steals the one that's been playing the longest, but could be made a bit smarter..
		SynthesiserVoice* oldest = 0;

		for (int i = voices.size(); --i >= 0;)
		{
			SynthesiserVoice* const voice = voices.getUnchecked (i);

			if (voice->canPlaySound (soundToPlay)
				 && (oldest == 0 || oldest->noteOnTime > voice->noteOnTime))
				oldest = voice;
		}

		jassert (oldest != 0);
		return oldest;
	}

	return 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Synthesiser.cpp ***/


/*** Start of inlined file: juce_ActionBroadcaster.cpp ***/
BEGIN_JUCE_NAMESPACE

ActionBroadcaster::ActionBroadcaster() throw()
{
	// are you trying to create this object before or after juce has been intialised??
	jassert (MessageManager::instance != 0);
}

ActionBroadcaster::~ActionBroadcaster()
{
	// all event-based objects must be deleted BEFORE juce is shut down!
	jassert (MessageManager::instance != 0);
}

void ActionBroadcaster::addActionListener (ActionListener* const listener)
{
	actionListenerList.addActionListener (listener);
}

void ActionBroadcaster::removeActionListener (ActionListener* const listener)
{
	jassert (actionListenerList.isValidMessageListener());

	if (actionListenerList.isValidMessageListener())
		actionListenerList.removeActionListener (listener);
}

void ActionBroadcaster::removeAllActionListeners()
{
	actionListenerList.removeAllActionListeners();
}

void ActionBroadcaster::sendActionMessage (const String& message) const
{
	actionListenerList.sendActionMessage (message);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ActionBroadcaster.cpp ***/


/*** Start of inlined file: juce_ActionListenerList.cpp ***/
BEGIN_JUCE_NAMESPACE

// special message of our own with a string in it
class ActionMessage  : public Message
{
public:
	const String message;

	ActionMessage (const String& messageText,
				   void* const listener_) throw()
		: message (messageText)
	{
		pointerParameter = listener_;
	}

	~ActionMessage() throw()
	{
	}

private:
	ActionMessage (const ActionMessage&);
	ActionMessage& operator= (const ActionMessage&);
};

ActionListenerList::ActionListenerList() throw()
{
}

ActionListenerList::~ActionListenerList() throw()
{
}

void ActionListenerList::addActionListener (ActionListener* const listener) throw()
{
	const ScopedLock sl (actionListenerLock_);

	jassert (listener != 0);
	jassert (! actionListeners_.contains (listener)); // trying to add a listener to the list twice!

	if (listener != 0)
		actionListeners_.add (listener);
}

void ActionListenerList::removeActionListener  (ActionListener* const listener) throw()
{
	const ScopedLock sl (actionListenerLock_);

	jassert (actionListeners_.contains (listener)); // trying to remove a listener that isn't on the list!

	actionListeners_.removeValue (listener);
}

void ActionListenerList::removeAllActionListeners() throw()
{
	const ScopedLock sl (actionListenerLock_);
	actionListeners_.clear();
}

void ActionListenerList::sendActionMessage (const String& message) const
{
	const ScopedLock sl (actionListenerLock_);

	for (int i = actionListeners_.size(); --i >= 0;)
		postMessage (new ActionMessage (message, static_cast <ActionListener*> (actionListeners_.getUnchecked(i))));
}

void ActionListenerList::handleMessage (const Message& message)
{
	const ActionMessage& am = (const ActionMessage&) message;

	if (actionListeners_.contains (am.pointerParameter))
		static_cast <ActionListener*> (am.pointerParameter)->actionListenerCallback (am.message);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ActionListenerList.cpp ***/


/*** Start of inlined file: juce_AsyncUpdater.cpp ***/
BEGIN_JUCE_NAMESPACE

AsyncUpdater::AsyncUpdater() throw()
   : asyncMessagePending (false)
{
	internalAsyncHandler.owner = this;
}

AsyncUpdater::~AsyncUpdater()
{
}

void AsyncUpdater::triggerAsyncUpdate() throw()
{
	if (! asyncMessagePending)
	{
		asyncMessagePending = true;
		internalAsyncHandler.postMessage (new Message());
	}
}

void AsyncUpdater::cancelPendingUpdate() throw()
{
	asyncMessagePending = false;
}

void AsyncUpdater::handleUpdateNowIfNeeded()
{
	if (asyncMessagePending)
	{
		asyncMessagePending = false;
		handleAsyncUpdate();
	}
}

void AsyncUpdater::AsyncUpdaterInternal::handleMessage (const Message&)
{
	owner->handleUpdateNowIfNeeded();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AsyncUpdater.cpp ***/


/*** Start of inlined file: juce_ChangeBroadcaster.cpp ***/
BEGIN_JUCE_NAMESPACE

ChangeBroadcaster::ChangeBroadcaster() throw()
{
	// are you trying to create this object before or after juce has been intialised??
	jassert (MessageManager::instance != 0);
}

ChangeBroadcaster::~ChangeBroadcaster()
{
	// all event-based objects must be deleted BEFORE juce is shut down!
	jassert (MessageManager::instance != 0);
}

void ChangeBroadcaster::addChangeListener (ChangeListener* const listener) throw()
{
	changeListenerList.addChangeListener (listener);
}

void ChangeBroadcaster::removeChangeListener (ChangeListener* const listener) throw()
{
	jassert (changeListenerList.isValidMessageListener());

	if (changeListenerList.isValidMessageListener())
		changeListenerList.removeChangeListener (listener);
}

void ChangeBroadcaster::removeAllChangeListeners() throw()
{
	changeListenerList.removeAllChangeListeners();
}

void ChangeBroadcaster::sendChangeMessage (void* objectThatHasChanged) throw()
{
	changeListenerList.sendChangeMessage (objectThatHasChanged);
}

void ChangeBroadcaster::sendSynchronousChangeMessage (void* objectThatHasChanged)
{
	changeListenerList.sendSynchronousChangeMessage (objectThatHasChanged);
}

void ChangeBroadcaster::dispatchPendingMessages()
{
	changeListenerList.dispatchPendingMessages();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ChangeBroadcaster.cpp ***/


/*** Start of inlined file: juce_ChangeListenerList.cpp ***/
BEGIN_JUCE_NAMESPACE

ChangeListenerList::ChangeListenerList() throw()
   : lastChangedObject (0),
	 messagePending (false)
{
}

ChangeListenerList::~ChangeListenerList() throw()
{
}

void ChangeListenerList::addChangeListener (ChangeListener* const listener) throw()
{
	const ScopedLock sl (lock);

	jassert (listener != 0);

	if (listener != 0)
		listeners.add (listener);
}

void ChangeListenerList::removeChangeListener (ChangeListener* const listener) throw()
{
	const ScopedLock sl (lock);
	listeners.removeValue (listener);
}

void ChangeListenerList::removeAllChangeListeners() throw()
{
	const ScopedLock sl (lock);
	listeners.clear();
}

void ChangeListenerList::sendChangeMessage (void* const objectThatHasChanged) throw()
{
	const ScopedLock sl (lock);

	if ((! messagePending) && (listeners.size() > 0))
	{
		lastChangedObject = objectThatHasChanged;
		postMessage (new Message (0, 0, 0, objectThatHasChanged));
		messagePending = true;
	}
}

void ChangeListenerList::handleMessage (const Message& message)
{
	sendSynchronousChangeMessage (message.pointerParameter);
}

void ChangeListenerList::sendSynchronousChangeMessage (void* const objectThatHasChanged)
{
	const ScopedLock sl (lock);
	messagePending = false;

	for (int i = listeners.size(); --i >= 0;)
	{
		ChangeListener* const l = static_cast <ChangeListener*> (listeners.getUnchecked (i));

		{
			const ScopedUnlock tempUnlocker (lock);
			l->changeListenerCallback (objectThatHasChanged);
		}

		i = jmin (i, listeners.size());
	}
}

void ChangeListenerList::dispatchPendingMessages()
{
	if (messagePending)
		sendSynchronousChangeMessage (lastChangedObject);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ChangeListenerList.cpp ***/


/*** Start of inlined file: juce_InterprocessConnection.cpp ***/
BEGIN_JUCE_NAMESPACE

InterprocessConnection::InterprocessConnection (const bool callbacksOnMessageThread,
												const uint32 magicMessageHeaderNumber)
	: Thread ("Juce IPC connection"),
	  callbackConnectionState (false),
	  useMessageThread (callbacksOnMessageThread),
	  magicMessageHeader (magicMessageHeaderNumber),
	  pipeReceiveMessageTimeout (-1)
{
}

InterprocessConnection::~InterprocessConnection()
{
	callbackConnectionState = false;
	disconnect();
}

bool InterprocessConnection::connectToSocket (const String& hostName,
											  const int portNumber,
											  const int timeOutMillisecs)
{
	disconnect();

	const ScopedLock sl (pipeAndSocketLock);
	socket = new StreamingSocket();

	if (socket->connect (hostName, portNumber, timeOutMillisecs))
	{
		connectionMadeInt();
		startThread();
		return true;
	}
	else
	{
		socket = 0;
		return false;
	}
}

bool InterprocessConnection::connectToPipe (const String& pipeName,
											const int pipeReceiveMessageTimeoutMs)
{
	disconnect();

	ScopedPointer <NamedPipe> newPipe (new NamedPipe());

	if (newPipe->openExisting (pipeName))
	{
		const ScopedLock sl (pipeAndSocketLock);
		pipeReceiveMessageTimeout = pipeReceiveMessageTimeoutMs;
		initialiseWithPipe (newPipe.release());
		return true;
	}

	return false;
}

bool InterprocessConnection::createPipe (const String& pipeName,
										 const int pipeReceiveMessageTimeoutMs)
{
	disconnect();

	ScopedPointer <NamedPipe> newPipe (new NamedPipe());

	if (newPipe->createNewPipe (pipeName))
	{
		const ScopedLock sl (pipeAndSocketLock);
		pipeReceiveMessageTimeout = pipeReceiveMessageTimeoutMs;
		initialiseWithPipe (newPipe.release());
		return true;
	}

	return false;
}

void InterprocessConnection::disconnect()
{
	if (socket != 0)
		socket->close();

	if (pipe != 0)
	{
		pipe->cancelPendingReads();
		pipe->close();
	}

	stopThread (4000);

	{
		const ScopedLock sl (pipeAndSocketLock);
		socket = 0;
		pipe = 0;
	}

	connectionLostInt();
}

bool InterprocessConnection::isConnected() const
{
	const ScopedLock sl (pipeAndSocketLock);

	return ((socket != 0 && socket->isConnected())
			  || (pipe != 0 && pipe->isOpen()))
			&& isThreadRunning();
}

const String InterprocessConnection::getConnectedHostName() const
{
	if (pipe != 0)
	{
		return "localhost";
	}
	else if (socket != 0)
	{
		if (! socket->isLocal())
			return socket->getHostName();

		return "localhost";
	}

	return String::empty;
}

bool InterprocessConnection::sendMessage (const MemoryBlock& message)
{
	uint32 messageHeader[2];
	messageHeader [0] = ByteOrder::swapIfBigEndian (magicMessageHeader);
	messageHeader [1] = ByteOrder::swapIfBigEndian ((uint32) message.getSize());

	MemoryBlock messageData (sizeof (messageHeader) + message.getSize());
	messageData.copyFrom (messageHeader, 0, sizeof (messageHeader));
	messageData.copyFrom (message.getData(), sizeof (messageHeader), message.getSize());

	size_t bytesWritten = 0;

	const ScopedLock sl (pipeAndSocketLock);

	if (socket != 0)
	{
		bytesWritten = socket->write (messageData.getData(), (int) messageData.getSize());
	}
	else if (pipe != 0)
	{
		bytesWritten = pipe->write (messageData.getData(), (int) messageData.getSize());
	}

	if (bytesWritten < 0)
	{
		// error..
		return false;
	}

	return (bytesWritten == messageData.getSize());
}

void InterprocessConnection::initialiseWithSocket (StreamingSocket* const socket_)
{
	jassert (socket == 0);
	socket = socket_;
	connectionMadeInt();
	startThread();
}

void InterprocessConnection::initialiseWithPipe (NamedPipe* const pipe_)
{
	jassert (pipe == 0);
	pipe = pipe_;
	connectionMadeInt();
	startThread();
}

const int messageMagicNumber = 0xb734128b;

void InterprocessConnection::handleMessage (const Message& message)
{
	if (message.intParameter1 == messageMagicNumber)
	{
		switch (message.intParameter2)
		{
		case 0:
			{
				ScopedPointer <MemoryBlock> data (static_cast <MemoryBlock*> (message.pointerParameter));
				messageReceived (*data);
				break;
			}

		case 1:
			connectionMade();
			break;

		case 2:
			connectionLost();
			break;
		}
	}
}

void InterprocessConnection::connectionMadeInt()
{
	if (! callbackConnectionState)
	{
		callbackConnectionState = true;

		if (useMessageThread)
			postMessage (new Message (messageMagicNumber, 1, 0, 0));
		else
			connectionMade();
	}
}

void InterprocessConnection::connectionLostInt()
{
	if (callbackConnectionState)
	{
		callbackConnectionState = false;

		if (useMessageThread)
			postMessage (new Message (messageMagicNumber, 2, 0, 0));
		else
			connectionLost();
	}
}

void InterprocessConnection::deliverDataInt (const MemoryBlock& data)
{
	jassert (callbackConnectionState);

	if (useMessageThread)
		postMessage (new Message (messageMagicNumber, 0, 0, new MemoryBlock (data)));
	else
		messageReceived (data);
}

bool InterprocessConnection::readNextMessageInt()
{
	const int maximumMessageSize = 1024 * 1024 * 10; // sanity check

	uint32 messageHeader[2];
	const int bytes = (socket != 0) ? socket->read (messageHeader, sizeof (messageHeader), true)
									: pipe->read (messageHeader, sizeof (messageHeader), pipeReceiveMessageTimeout);

	if (bytes == sizeof (messageHeader)
		 && ByteOrder::swapIfBigEndian (messageHeader[0]) == magicMessageHeader)
	{
		int bytesInMessage = (int) ByteOrder::swapIfBigEndian (messageHeader[1]);

		if (bytesInMessage > 0 && bytesInMessage < maximumMessageSize)
		{
			MemoryBlock messageData (bytesInMessage, true);
			int bytesRead = 0;

			while (bytesInMessage > 0)
			{
				if (threadShouldExit())
					return false;

				const int numThisTime = jmin (bytesInMessage, 65536);
				const int bytesIn = (socket != 0) ? socket->read (static_cast <char*> (messageData.getData()) + bytesRead, numThisTime, true)
												  : pipe->read (static_cast <char*> (messageData.getData()) + bytesRead, numThisTime, pipeReceiveMessageTimeout);

				if (bytesIn <= 0)
					break;

				bytesRead += bytesIn;
				bytesInMessage -= bytesIn;
			}

			if (bytesRead >= 0)
				deliverDataInt (messageData);
		}
	}
	else if (bytes < 0)
	{
		{
			const ScopedLock sl (pipeAndSocketLock);
			socket = 0;
		}

		connectionLostInt();
		return false;
	}

	return true;
}

void InterprocessConnection::run()
{
	while (! threadShouldExit())
	{
		if (socket != 0)
		{
			const int ready = socket->waitUntilReady (true, 0);

			if (ready < 0)
			{
				{
					const ScopedLock sl (pipeAndSocketLock);
					socket = 0;
				}

				connectionLostInt();
				break;
			}
			else if (ready > 0)
			{
				if (! readNextMessageInt())
					break;
			}
			else
			{
				Thread::sleep (2);
			}
		}
		else if (pipe != 0)
		{
			if (! pipe->isOpen())
			{
				{
					const ScopedLock sl (pipeAndSocketLock);
					pipe = 0;
				}

				connectionLostInt();
				break;
			}
			else
			{
				if (! readNextMessageInt())
					break;
			}
		}
		else
		{
			break;
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_InterprocessConnection.cpp ***/


/*** Start of inlined file: juce_InterprocessConnectionServer.cpp ***/
BEGIN_JUCE_NAMESPACE

InterprocessConnectionServer::InterprocessConnectionServer()
	: Thread ("Juce IPC server")
{
}

InterprocessConnectionServer::~InterprocessConnectionServer()
{
	stop();
}

bool InterprocessConnectionServer::beginWaitingForSocket (const int portNumber)
{
	stop();

	socket = new StreamingSocket();

	if (socket->createListener (portNumber))
	{
		startThread();
		return true;
	}

	socket = 0;
	return false;
}

void InterprocessConnectionServer::stop()
{
	signalThreadShouldExit();

	if (socket != 0)
		socket->close();

	stopThread (4000);
	socket = 0;
}

void InterprocessConnectionServer::run()
{
	while ((! threadShouldExit()) && socket != 0)
	{
		ScopedPointer <StreamingSocket> clientSocket (socket->waitForNextConnection());

		if (clientSocket != 0)
		{
			InterprocessConnection* newConnection = createConnectionObject();

			if (newConnection != 0)
				newConnection->initialiseWithSocket (clientSocket.release());
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_InterprocessConnectionServer.cpp ***/


/*** Start of inlined file: juce_Message.cpp ***/
BEGIN_JUCE_NAMESPACE

Message::Message() throw()
	: intParameter1 (0),
	  intParameter2 (0),
	  intParameter3 (0),
	  pointerParameter (0)
{
}

Message::Message (const int intParameter1_,
				  const int intParameter2_,
				  const int intParameter3_,
				  void* const pointerParameter_) throw()
	: intParameter1 (intParameter1_),
	  intParameter2 (intParameter2_),
	  intParameter3 (intParameter3_),
	  pointerParameter (pointerParameter_)
{
}

Message::~Message() throw()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Message.cpp ***/


/*** Start of inlined file: juce_MessageListener.cpp ***/
BEGIN_JUCE_NAMESPACE

MessageListener::MessageListener() throw()
{
	// are you trying to create a messagelistener before or after juce has been intialised??
	jassert (MessageManager::instance != 0);

	if (MessageManager::instance != 0)
		MessageManager::instance->messageListeners.add (this);
}

MessageListener::~MessageListener()
{
	if (MessageManager::instance != 0)
		MessageManager::instance->messageListeners.removeValue (this);
}

void MessageListener::postMessage (Message* const message) const throw()
{
	message->messageRecipient = const_cast <MessageListener*> (this);

	if (MessageManager::instance == 0)
		MessageManager::getInstance();

	MessageManager::instance->postMessageToQueue (message);
}

bool MessageListener::isValidMessageListener() const throw()
{
	return (MessageManager::instance != 0)
			 && MessageManager::instance->messageListeners.contains (this);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MessageListener.cpp ***/


/*** Start of inlined file: juce_MessageManager.cpp ***/
BEGIN_JUCE_NAMESPACE

// platform-specific functions..
bool juce_dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages);
bool juce_postMessageToSystemQueue (void* message);

MessageManager* MessageManager::instance = 0;

static const int quitMessageId = 0xfffff321;

MessageManager::MessageManager() throw()
  : quitMessagePosted (false),
	quitMessageReceived (false),
	threadWithLock (0)
{
	messageThreadId = Thread::getCurrentThreadId();
}

MessageManager::~MessageManager() throw()
{
	broadcastListeners = 0;

	doPlatformSpecificShutdown();

	jassert (instance == this);
	instance = 0;  // do this last in case this instance is still needed by doPlatformSpecificShutdown()
}

MessageManager* MessageManager::getInstance() throw()
{
	if (instance == 0)
	{
		instance = new MessageManager();
		doPlatformSpecificInitialisation();
	}

	return instance;
}

void MessageManager::postMessageToQueue (Message* const message)
{
	if (quitMessagePosted || ! juce_postMessageToSystemQueue (message))
		delete message;
}

CallbackMessage::CallbackMessage() throw()  {}
CallbackMessage::~CallbackMessage() throw()  {}

void CallbackMessage::post()
{
	if (MessageManager::instance != 0)
		MessageManager::instance->postCallbackMessage (this);
}

void MessageManager::postCallbackMessage (Message* const message)
{
	message->messageRecipient = 0;
	postMessageToQueue (message);
}

// not for public use..
void MessageManager::deliverMessage (void* const message)
{
	const ScopedPointer <Message> m (static_cast <Message*> (message));
	MessageListener* const recipient = m->messageRecipient;

	JUCE_TRY
	{
		if (messageListeners.contains (recipient))
		{
			recipient->handleMessage (*m);
		}
		else if (recipient == 0)
		{
			if (m->intParameter1 == quitMessageId)
			{
				quitMessageReceived = true;
			}
			else
			{
				CallbackMessage* const cm = dynamic_cast <CallbackMessage*> (static_cast <Message*> (m));

				if (cm != 0)
					cm->messageCallback();
			}
		}
	}
	JUCE_CATCH_EXCEPTION
}

#if ! (JUCE_MAC || JUCE_IPHONE)
void MessageManager::runDispatchLoop()
{
	jassert (isThisTheMessageThread()); // must only be called by the message thread

	runDispatchLoopUntil (-1);
}

void MessageManager::stopDispatchLoop()
{
	Message* const m = new Message (quitMessageId, 0, 0, 0);
	m->messageRecipient = 0;
	postMessageToQueue (m);

	quitMessagePosted = true;
}

bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
{
	jassert (isThisTheMessageThread()); // must only be called by the message thread

	const int64 endTime = Time::currentTimeMillis() + millisecondsToRunFor;

	while ((millisecondsToRunFor < 0 || endTime > Time::currentTimeMillis())
			&& ! quitMessageReceived)
	{
		JUCE_TRY
		{
			if (! juce_dispatchNextMessageOnSystemQueue (millisecondsToRunFor >= 0))
			{
				const int msToWait = (int) (endTime - Time::currentTimeMillis());

				if (msToWait > 0)
					Thread::sleep (jmin (5, msToWait));
			}
		}
		JUCE_CATCH_EXCEPTION
	}

	return ! quitMessageReceived;
}

#endif

void MessageManager::deliverBroadcastMessage (const String& value)
{
	if (broadcastListeners != 0)
		broadcastListeners->sendActionMessage (value);
}

void MessageManager::registerBroadcastListener (ActionListener* const listener) throw()
{
	if (broadcastListeners == 0)
		broadcastListeners = new ActionListenerList();

	broadcastListeners->addActionListener (listener);
}

void MessageManager::deregisterBroadcastListener (ActionListener* const listener) throw()
{
	if (broadcastListeners != 0)
		broadcastListeners->removeActionListener (listener);
}

bool MessageManager::isThisTheMessageThread() const throw()
{
	return Thread::getCurrentThreadId() == messageThreadId;
}

void MessageManager::setCurrentThreadAsMessageThread()
{
	if (messageThreadId != Thread::getCurrentThreadId())
	{
		messageThreadId = Thread::getCurrentThreadId();

		// This is needed on windows to make sure the message window is created by this thread
		doPlatformSpecificShutdown();
		doPlatformSpecificInitialisation();
	}
}

bool MessageManager::currentThreadHasLockedMessageManager() const throw()
{
	const Thread::ThreadID thisThread = Thread::getCurrentThreadId();
	return thisThread == messageThreadId || thisThread == threadWithLock;
}

class MessageManagerLock::SharedEvents   : public ReferenceCountedObject
{
public:
	SharedEvents()   {}
	~SharedEvents()  {}

	WaitableEvent lockedEvent, releaseEvent;

private:
	SharedEvents (const SharedEvents&);
	SharedEvents& operator= (const SharedEvents&);
};

class MessageManagerLock::BlockingMessage   : public CallbackMessage
{
public:
	BlockingMessage (MessageManagerLock::SharedEvents* const events_) : events (events_) {}
	~BlockingMessage() throw()  {}

	void messageCallback()
	{
		events->lockedEvent.signal();
		events->releaseEvent.wait();
	}

	juce_UseDebuggingNewOperator

private:
	ReferenceCountedObjectPtr <MessageManagerLock::SharedEvents> events;

	BlockingMessage (const BlockingMessage&);
	BlockingMessage& operator= (const BlockingMessage&);
};

MessageManagerLock::MessageManagerLock (Thread* const threadToCheck) throw()
	: sharedEvents (0),
	  locked (false)
{
	init (threadToCheck, 0);
}

MessageManagerLock::MessageManagerLock (ThreadPoolJob* const jobToCheckForExitSignal) throw()
	: sharedEvents (0),
	  locked (false)
{
	init (0, jobToCheckForExitSignal);
}

void MessageManagerLock::init (Thread* const threadToCheck, ThreadPoolJob* const job) throw()
{
	if (MessageManager::instance != 0)
	{
		if (MessageManager::instance->currentThreadHasLockedMessageManager())
		{
			locked = true;   // either we're on the message thread, or this is a re-entrant call.
		}
		else
		{
			if (threadToCheck == 0 && job == 0)
			{
				MessageManager::instance->lockingLock.enter();
			}
			else
			{
				while (! MessageManager::instance->lockingLock.tryEnter())
				{
					if ((threadToCheck != 0 && threadToCheck->threadShouldExit())
						  || (job != 0 && job->shouldExit()))
						return;

					Thread::sleep (1);
				}
			}

			sharedEvents = new SharedEvents();
			sharedEvents->incReferenceCount();

			(new BlockingMessage (sharedEvents))->post();

			while (! sharedEvents->lockedEvent.wait (50))
			{
				if ((threadToCheck != 0 && threadToCheck->threadShouldExit())
					  || (job != 0 && job->shouldExit()))
				{
					sharedEvents->releaseEvent.signal();
					sharedEvents->decReferenceCount();
					sharedEvents = 0;
					MessageManager::instance->lockingLock.exit();
					return;
				}
			}

			jassert (MessageManager::instance->threadWithLock == 0);

			MessageManager::instance->threadWithLock = Thread::getCurrentThreadId();
			locked = true;
		}
	}
}

MessageManagerLock::~MessageManagerLock() throw()
{
	if (sharedEvents != 0)
	{
		jassert (MessageManager::instance == 0 || MessageManager::instance->currentThreadHasLockedMessageManager());

		sharedEvents->releaseEvent.signal();
		sharedEvents->decReferenceCount();

		if (MessageManager::instance != 0)
		{
			MessageManager::instance->threadWithLock = 0;
			MessageManager::instance->lockingLock.exit();
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MessageManager.cpp ***/


/*** Start of inlined file: juce_MultiTimer.cpp ***/
BEGIN_JUCE_NAMESPACE

class MultiTimer::MultiTimerCallback	: public Timer
{
public:
	MultiTimerCallback (const int timerId_, MultiTimer& owner_)
		: timerId (timerId_),
		  owner (owner_)
	{
	}

	~MultiTimerCallback()
	{
	}

	void timerCallback()
	{
		owner.timerCallback (timerId);
	}

	const int timerId;

private:
	MultiTimer& owner;
};

MultiTimer::MultiTimer() throw()
{
}

MultiTimer::MultiTimer (const MultiTimer&) throw()
{
}

MultiTimer::~MultiTimer()
{
	const ScopedLock sl (timerListLock);
	timers.clear();
}

void MultiTimer::startTimer (const int timerId, const int intervalInMilliseconds) throw()
{
	const ScopedLock sl (timerListLock);

	for (int i = timers.size(); --i >= 0;)
	{
		MultiTimerCallback* const t = timers.getUnchecked(i);

		if (t->timerId == timerId)
		{
			t->startTimer (intervalInMilliseconds);
			return;
		}
	}

	MultiTimerCallback* const newTimer = new MultiTimerCallback (timerId, *this);
	timers.add (newTimer);
	newTimer->startTimer (intervalInMilliseconds);
}

void MultiTimer::stopTimer (const int timerId) throw()
{
	const ScopedLock sl (timerListLock);

	for (int i = timers.size(); --i >= 0;)
	{
		MultiTimerCallback* const t = timers.getUnchecked(i);

		if (t->timerId == timerId)
			t->stopTimer();
	}
}

bool MultiTimer::isTimerRunning (const int timerId) const throw()
{
	const ScopedLock sl (timerListLock);

	for (int i = timers.size(); --i >= 0;)
	{
		const MultiTimerCallback* const t = timers.getUnchecked(i);
		if (t->timerId == timerId)
			return t->isTimerRunning();
	}

	return false;
}

int MultiTimer::getTimerInterval (const int timerId) const throw()
{
	const ScopedLock sl (timerListLock);

	for (int i = timers.size(); --i >= 0;)
	{
		const MultiTimerCallback* const t = timers.getUnchecked(i);
		if (t->timerId == timerId)
			return t->getTimerInterval();
	}

	return 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MultiTimer.cpp ***/


/*** Start of inlined file: juce_Timer.cpp ***/
BEGIN_JUCE_NAMESPACE

class InternalTimerThread  : private Thread,
							 private MessageListener,
							 private DeletedAtShutdown,
							 private AsyncUpdater
{
public:
	InternalTimerThread()
		: Thread ("Juce Timer"),
		  firstTimer (0),
		  callbackNeeded (false)
	{
		triggerAsyncUpdate();
	}

	~InternalTimerThread() throw()
	{
		stopThread (4000);

		jassert (instance == this || instance == 0);
		if (instance == this)
			instance = 0;
	}

	void run()
	{
		uint32 lastTime = Time::getMillisecondCounter();

		while (! threadShouldExit())
		{
			const uint32 now = Time::getMillisecondCounter();

			if (now <= lastTime)
			{
				wait (2);
				continue;
			}

			const int elapsed = now - lastTime;
			lastTime = now;

			int timeUntilFirstTimer = 1000;

			{
				const ScopedLock sl (lock);
				decrementAllCounters (elapsed);

				if (firstTimer != 0)
					timeUntilFirstTimer = firstTimer->countdownMs;
			}

			if (timeUntilFirstTimer <= 0)
			{
				if (callbackNeeded.set (true))
				{
					postMessage (new Message());

					const uint32 messageDeliveryTimeout = now + 2000;

					while (callbackNeeded.get())
					{
						wait (4);

						if (threadShouldExit())
							return;

						if (Time::getMillisecondCounter() > messageDeliveryTimeout)
							break;
					}
				}
			}
			else
			{
				// don't wait for too long because running this loop also helps keep the
				// Time::getApproximateMillisecondTimer value stay up-to-date
				wait (jlimit (1, 50, timeUntilFirstTimer));
			}
		}
	}

	void callTimers()
	{
		const ScopedLock sl (lock);

		while (firstTimer != 0 && firstTimer->countdownMs <= 0)
		{
			Timer* const t = firstTimer;
			t->countdownMs = t->periodMs;

			removeTimer (t);
			addTimer (t);

			const ScopedUnlock ul (lock);

			JUCE_TRY
			{
				t->timerCallback();
			}
			JUCE_CATCH_EXCEPTION
		}

		callbackNeeded.set (false);
	}

	void handleMessage (const Message&)
	{
		callTimers();
	}

	void callTimersSynchronously()
	{
		if (! isThreadRunning())
		{
			// (This is relied on by some plugins in cases where the MM has
			// had to restart and the async callback never started)
			cancelPendingUpdate();
			triggerAsyncUpdate();
		}

		callTimers();
	}

	static void callAnyTimersSynchronously()
	{
		if (InternalTimerThread::instance != 0)
			InternalTimerThread::instance->callTimersSynchronously();
	}

	static inline void add (Timer* const tim) throw()
	{
		if (instance == 0)
			instance = new InternalTimerThread();

		const ScopedLock sl (instance->lock);
		instance->addTimer (tim);
	}

	static inline void remove (Timer* const tim) throw()
	{
		if (instance != 0)
		{
			const ScopedLock sl (instance->lock);
			instance->removeTimer (tim);
		}
	}

	static inline void resetCounter (Timer* const tim,
									 const int newCounter) throw()
	{
		if (instance != 0)
		{
			tim->countdownMs = newCounter;
			tim->periodMs = newCounter;

			if ((tim->next != 0 && tim->next->countdownMs < tim->countdownMs)
				 || (tim->previous != 0 && tim->previous->countdownMs > tim->countdownMs))
			{
				const ScopedLock sl (instance->lock);
				instance->removeTimer (tim);
				instance->addTimer (tim);
			}
		}
	}

private:
	friend class Timer;
	static InternalTimerThread* instance;
	static CriticalSection lock;
	Timer* volatile firstTimer;

	class AtomicBool
	{
	public:
		AtomicBool (const bool value) throw() : value (static_cast<int32> (value)) {}
		~AtomicBool() throw() {}

		bool get() const throw()	{ return value != 0; }
		bool set (const bool newValue)  { return Atomic::compareAndExchange (value, newValue ? 1 : 0, value) != 0; }

	private:
		int32 value;

		AtomicBool (const AtomicBool&);
		AtomicBool& operator= (const AtomicBool&);
	};

	AtomicBool callbackNeeded;

	void addTimer (Timer* const t) throw()
	{
#ifdef JUCE_DEBUG
		Timer* tt = firstTimer;

		while (tt != 0)
		{
			// trying to add a timer that's already here - shouldn't get to this point,
			// so if you get this assertion, let me know!
			jassert (tt != t);

			tt = tt->next;
		}

		jassert (t->previous == 0 && t->next == 0);
#endif

		Timer* i = firstTimer;

		if (i == 0 || i->countdownMs > t->countdownMs)
		{
			t->next = firstTimer;
			firstTimer = t;
		}
		else
		{
			while (i->next != 0 && i->next->countdownMs <= t->countdownMs)
				i = i->next;

			jassert (i != 0);

			t->next = i->next;
			t->previous = i;
			i->next = t;
		}

		if (t->next != 0)
			t->next->previous = t;

		jassert ((t->next == 0 || t->next->countdownMs >= t->countdownMs)
				  && (t->previous == 0 || t->previous->countdownMs <= t->countdownMs));

		notify();
	}

	void removeTimer (Timer* const t) throw()
	{
#ifdef JUCE_DEBUG
		Timer* tt = firstTimer;
		bool found = false;

		while (tt != 0)
		{
			if (tt == t)
			{
				found = true;
				break;
			}

			tt = tt->next;
		}

		// trying to remove a timer that's not here - shouldn't get to this point,
		// so if you get this assertion, let me know!
		jassert (found);
#endif

		if (t->previous != 0)
		{
			jassert (firstTimer != t);
			t->previous->next = t->next;
		}
		else
		{
			jassert (firstTimer == t);
			firstTimer = t->next;
		}

		if (t->next != 0)
			t->next->previous = t->previous;

		t->next = 0;
		t->previous = 0;
	}

	void decrementAllCounters (const int numMillisecs) const
	{
		Timer* t = firstTimer;

		while (t != 0)
		{
			t->countdownMs -= numMillisecs;
			t = t->next;
		}
	}

	void handleAsyncUpdate()
	{
		startThread (7);
	}

	InternalTimerThread (const InternalTimerThread&);
	InternalTimerThread& operator= (const InternalTimerThread&);
};

InternalTimerThread* InternalTimerThread::instance = 0;
CriticalSection InternalTimerThread::lock;

void juce_callAnyTimersSynchronously()
{
	InternalTimerThread::callAnyTimersSynchronously();
}

#ifdef JUCE_DEBUG
static SortedSet <Timer*> activeTimers;
#endif

Timer::Timer() throw()
   : countdownMs (0),
	 periodMs (0),
	 previous (0),
	 next (0)
{
#ifdef JUCE_DEBUG
	activeTimers.add (this);
#endif
}

Timer::Timer (const Timer&) throw()
   : countdownMs (0),
	 periodMs (0),
	 previous (0),
	 next (0)
{
#ifdef JUCE_DEBUG
	activeTimers.add (this);
#endif
}

Timer::~Timer()
{
	stopTimer();

#ifdef JUCE_DEBUG
	activeTimers.removeValue (this);
#endif
}

void Timer::startTimer (const int interval) throw()
{
	const ScopedLock sl (InternalTimerThread::lock);

#ifdef JUCE_DEBUG
	// this isn't a valid object! Your timer might be a dangling pointer or something..
	jassert (activeTimers.contains (this));
#endif

	if (periodMs == 0)
	{
		countdownMs = interval;
		periodMs = jmax (1, interval);
		InternalTimerThread::add (this);
	}
	else
	{
		InternalTimerThread::resetCounter (this, interval);
	}
}

void Timer::stopTimer() throw()
{
	const ScopedLock sl (InternalTimerThread::lock);

#ifdef JUCE_DEBUG
	// this isn't a valid object! Your timer might be a dangling pointer or something..
	jassert (activeTimers.contains (this));
#endif

	if (periodMs > 0)
	{
		InternalTimerThread::remove (this);
		periodMs = 0;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Timer.cpp ***/

#endif

#if JUCE_BUILD_GUI

/*** Start of inlined file: juce_Component.cpp ***/
BEGIN_JUCE_NAMESPACE

Component* Component::currentlyFocusedComponent = 0;

static Array <Component*> modalComponentStack, modalComponentReturnValueKeys;
static Array <int> modalReturnValues;

enum ComponentMessageNumbers
{
	customCommandMessage   = 0x7fff0001,
	exitModalStateMessage  = 0x7fff0002
};

#define checkMessageManagerIsLocked	 jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());

static uint32 nextComponentUID = 0;

Component::Component()
  : parentComponent_ (0),
	componentUID (++nextComponentUID),
	numDeepMouseListeners (0),
	lookAndFeel_ (0),
	effect_ (0),
	bufferedImage_ (0),
	mouseListeners_ (0),
	keyListeners_ (0),
	componentFlags_ (0)
{
}

Component::Component (const String& name)
  : componentName_ (name),
	parentComponent_ (0),
	componentUID (++nextComponentUID),
	numDeepMouseListeners (0),
	lookAndFeel_ (0),
	effect_ (0),
	bufferedImage_ (0),
	mouseListeners_ (0),
	keyListeners_ (0),
	componentFlags_ (0)
{
}

Component::~Component()
{
	componentListeners.call (&ComponentListener::componentBeingDeleted, *this);

	if (parentComponent_ != 0)
	{
		parentComponent_->removeChildComponent (this);
	}
	else if ((currentlyFocusedComponent == this)
			  || isParentOf (currentlyFocusedComponent))
	{
		giveAwayFocus();
	}

	if (flags.hasHeavyweightPeerFlag)
		removeFromDesktop();

	modalComponentStack.removeValue (this);

	for (int i = childComponentList_.size(); --i >= 0;)
		childComponentList_.getUnchecked(i)->parentComponent_ = 0;

	delete bufferedImage_;
	delete mouseListeners_;
	delete keyListeners_;
}

void Component::setName (const String& name)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (componentName_ != name)
	{
		componentName_ = name;

		if (flags.hasHeavyweightPeerFlag)
		{
			ComponentPeer* const peer = getPeer();

			jassert (peer != 0);
			if (peer != 0)
				peer->setTitle (name);
		}

		BailOutChecker checker (this);
		componentListeners.callChecked (checker, &ComponentListener::componentNameChanged, *this);
	}
}

void Component::setVisible (bool shouldBeVisible)
{
	if (flags.visibleFlag != shouldBeVisible)
	{
		// if component methods are being called from threads other than the message
		// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
		checkMessageManagerIsLocked

		SafePointer<Component> safePointer (this);

		flags.visibleFlag = shouldBeVisible;

		internalRepaint (0, 0, getWidth(), getHeight());

		sendFakeMouseMove();

		if (! shouldBeVisible)
		{
			if (currentlyFocusedComponent == this
				|| isParentOf (currentlyFocusedComponent))
			{
				if (parentComponent_ != 0)
					parentComponent_->grabKeyboardFocus();
				else
					giveAwayFocus();
			}
		}

		if (safePointer != 0)
		{
			sendVisibilityChangeMessage();

			if (safePointer != 0 && flags.hasHeavyweightPeerFlag)
			{
				ComponentPeer* const peer = getPeer();

				jassert (peer != 0);
				if (peer != 0)
				{
					peer->setVisible (shouldBeVisible);
					internalHierarchyChanged();
				}
			}
		}
	}
}

void Component::visibilityChanged()
{
}

void Component::sendVisibilityChangeMessage()
{
	BailOutChecker checker (this);

	visibilityChanged();

	if (! checker.shouldBailOut())
		componentListeners.callChecked (checker, &ComponentListener::componentVisibilityChanged, *this);
}

bool Component::isShowing() const
{
	if (flags.visibleFlag)
	{
		if (parentComponent_ != 0)
		{
			return parentComponent_->isShowing();
		}
		else
		{
			const ComponentPeer* const peer = getPeer();

			return peer != 0 && ! peer->isMinimised();
		}
	}

	return false;
}

class FadeOutProxyComponent  : public Component,
							   public Timer
{
public:
	FadeOutProxyComponent (Component* comp,
						   const int fadeLengthMs,
						   const int deltaXToMove,
						   const int deltaYToMove,
						   const float scaleFactorAtEnd)
	   : lastTime (0),
		 alpha (1.0f),
		 scale (1.0f)
	{
		image = comp->createComponentSnapshot (comp->getLocalBounds());
		setBounds (comp->getBounds());
		comp->getParentComponent()->addAndMakeVisible (this);
		toBehind (comp);

		alphaChangePerMs = -1.0f / (float)fadeLengthMs;

		centreX = comp->getX() + comp->getWidth() * 0.5f;
		xChangePerMs = deltaXToMove / (float)fadeLengthMs;

		centreY = comp->getY() + comp->getHeight() * 0.5f;
		yChangePerMs = deltaYToMove / (float)fadeLengthMs;

		scaleChangePerMs = (scaleFactorAtEnd - 1.0f) / (float)fadeLengthMs;

		setInterceptsMouseClicks (false, false);

		// 30 fps is enough for a fade, but we need a higher rate if it's moving as well..
		startTimer (1000 / ((deltaXToMove == 0 && deltaYToMove == 0) ? 30 : 50));
	}

	~FadeOutProxyComponent()
	{
		delete image;
	}

	void paint (Graphics& g)
	{
		g.setOpacity (alpha);

		g.drawImage (image,
					 0, 0, getWidth(), getHeight(),
					 0, 0, image->getWidth(), image->getHeight());
	}

	void timerCallback()
	{
		const uint32 now = Time::getMillisecondCounter();

		if (lastTime == 0)
			lastTime = now;

		const int msPassed = (now > lastTime) ? now - lastTime : 0;
		lastTime = now;

		alpha += alphaChangePerMs * msPassed;

		if (alpha > 0)
		{
			if (xChangePerMs != 0.0f || yChangePerMs != 0.0f || scaleChangePerMs != 0.0f)
			{
				centreX += xChangePerMs * msPassed;
				centreY += yChangePerMs * msPassed;
				scale += scaleChangePerMs * msPassed;

				const int w = roundToInt (image->getWidth() * scale);
				const int h = roundToInt (image->getHeight() * scale);

				setBounds (roundToInt (centreX) - w / 2,
						   roundToInt (centreY) - h / 2,
						   w, h);
			}

			repaint();
		}
		else
		{
			delete this;
		}
	}

	juce_UseDebuggingNewOperator

private:
	Image* image;
	uint32 lastTime;
	float alpha, alphaChangePerMs;
	float centreX, xChangePerMs;
	float centreY, yChangePerMs;
	float scale, scaleChangePerMs;

	FadeOutProxyComponent (const FadeOutProxyComponent&);
	FadeOutProxyComponent& operator= (const FadeOutProxyComponent&);
};

void Component::fadeOutComponent (const int millisecondsToFade,
								  const int deltaXToMove,
								  const int deltaYToMove,
								  const float scaleFactorAtEnd)
{
	//xxx won't work for comps without parents
	if (isShowing() && millisecondsToFade > 0)
		new FadeOutProxyComponent (this, millisecondsToFade,
								   deltaXToMove, deltaYToMove, scaleFactorAtEnd);

	setVisible (false);
}

bool Component::isValidComponent() const
{
	return (this != 0) && isValidMessageListener();
}

void* Component::getWindowHandle() const
{
	const ComponentPeer* const peer = getPeer();

	if (peer != 0)
		return peer->getNativeHandle();

	return 0;
}

void Component::addToDesktop (int styleWanted, void* nativeWindowToAttachTo)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (isOpaque())
		styleWanted &= ~ComponentPeer::windowIsSemiTransparent;
	else
		styleWanted |= ComponentPeer::windowIsSemiTransparent;

	int currentStyleFlags = 0;

	// don't use getPeer(), so that we only get the peer that's specifically
	// for this comp, and not for one of its parents.
	ComponentPeer* peer = ComponentPeer::getPeerFor (this);

	if (peer != 0)
		currentStyleFlags = peer->getStyleFlags();

	if (styleWanted != currentStyleFlags || ! flags.hasHeavyweightPeerFlag)
	{
		SafePointer<Component> safePointer (this);

#if JUCE_LINUX
		// it's wise to give the component a non-zero size before
		// putting it on the desktop, as X windows get confused by this, and
		// a (1, 1) minimum size is enforced here.
		setSize (jmax (1, getWidth()),
				 jmax (1, getHeight()));
#endif

		const Point<int> topLeft (relativePositionToGlobal (Point<int> (0, 0)));

		bool wasFullscreen = false;
		bool wasMinimised = false;
		ComponentBoundsConstrainer* currentConstainer = 0;
		Rectangle<int> oldNonFullScreenBounds;

		if (peer != 0)
		{
			wasFullscreen = peer->isFullScreen();
			wasMinimised = peer->isMinimised();
			currentConstainer = peer->getConstrainer();
			oldNonFullScreenBounds = peer->getNonFullScreenBounds();

			removeFromDesktop();

			setTopLeftPosition (topLeft.getX(), topLeft.getY());
		}

		if (parentComponent_ != 0)
			parentComponent_->removeChildComponent (this);

		if (safePointer != 0)
		{
			flags.hasHeavyweightPeerFlag = true;

			peer = createNewPeer (styleWanted, nativeWindowToAttachTo);

			Desktop::getInstance().addDesktopComponent (this);

			bounds_.setPosition (topLeft);
			peer->setBounds (topLeft.getX(), topLeft.getY(), getWidth(), getHeight(), false);

			peer->setVisible (isVisible());

			if (wasFullscreen)
			{
				peer->setFullScreen (true);
				peer->setNonFullScreenBounds (oldNonFullScreenBounds);
			}

			if (wasMinimised)
				peer->setMinimised (true);

			if (isAlwaysOnTop())
				peer->setAlwaysOnTop (true);

			peer->setConstrainer (currentConstainer);

			repaint();
		}

		internalHierarchyChanged();
	}
}

void Component::removeFromDesktop()
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (flags.hasHeavyweightPeerFlag)
	{
		ComponentPeer* const peer = ComponentPeer::getPeerFor (this);

		flags.hasHeavyweightPeerFlag = false;

		jassert (peer != 0);
		delete peer;

		Desktop::getInstance().removeDesktopComponent (this);
	}
}

bool Component::isOnDesktop() const throw()
{
	return flags.hasHeavyweightPeerFlag;
}

void Component::userTriedToCloseWindow()
{
	/* This means that the user's trying to get rid of your window with the 'close window' system
	   menu option (on windows) or possibly the task manager - you should really handle this
	   and delete or hide your component in an appropriate way.

	   If you want to ignore the event and don't want to trigger this assertion, just override
	   this method and do nothing.
	*/
	jassertfalse
}

void Component::minimisationStateChanged (bool)
{
}

void Component::setOpaque (const bool shouldBeOpaque)
{
	if (shouldBeOpaque != flags.opaqueFlag)
	{
		flags.opaqueFlag = shouldBeOpaque;

		if (flags.hasHeavyweightPeerFlag)
		{
			const ComponentPeer* const peer = ComponentPeer::getPeerFor (this);

			if (peer != 0)
			{
				// to make it recreate the heavyweight window
				addToDesktop (peer->getStyleFlags());
			}
		}

		repaint();
	}
}

bool Component::isOpaque() const throw()
{
	return flags.opaqueFlag;
}

void Component::setBufferedToImage (const bool shouldBeBuffered)
{
	if (shouldBeBuffered != flags.bufferToImageFlag)
	{
		deleteAndZero (bufferedImage_);
		flags.bufferToImageFlag = shouldBeBuffered;
	}
}

void Component::toFront (const bool setAsForeground)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (flags.hasHeavyweightPeerFlag)
	{
		ComponentPeer* const peer = getPeer();

		if (peer != 0)
		{
			peer->toFront (setAsForeground);

			if (setAsForeground && ! hasKeyboardFocus (true))
				grabKeyboardFocus();
		}
	}
	else if (parentComponent_ != 0)
	{
		Array<Component*>& childList = parentComponent_->childComponentList_;

		if (childList.getLast() != this)
		{
			const int index = childList.indexOf (this);

			if (index >= 0)
			{
				int insertIndex = -1;

				if (! flags.alwaysOnTopFlag)
				{
					insertIndex = childList.size() - 1;

					while (insertIndex > 0 && childList.getUnchecked (insertIndex)->isAlwaysOnTop())
						--insertIndex;
				}

				if (index != insertIndex)
				{
					childList.move (index, insertIndex);
					sendFakeMouseMove();

					repaintParent();
				}
			}
		}

		if (setAsForeground)
		{
			internalBroughtToFront();
			grabKeyboardFocus();
		}
	}
}

void Component::toBehind (Component* const other)
{
	if (other != 0 && other != this)
	{
		// the two components must belong to the same parent..
		jassert (parentComponent_ == other->parentComponent_);

		if (parentComponent_ != 0)
		{
			Array<Component*>& childList = parentComponent_->childComponentList_;

			const int index = childList.indexOf (this);

			if (index >= 0 && childList [index + 1] != other)
			{
				int otherIndex = childList.indexOf (other);

				if (otherIndex >= 0)
				{
					if (index < otherIndex)
						--otherIndex;

					childList.move (index, otherIndex);

					sendFakeMouseMove();
					repaintParent();
				}
			}
		}
		else if (isOnDesktop())
		{
			jassert (other->isOnDesktop());

			if (other->isOnDesktop())
			{
				ComponentPeer* const us = getPeer();
				ComponentPeer* const them = other->getPeer();

				jassert (us != 0 && them != 0);
				if (us != 0 && them != 0)
					us->toBehind (them);
			}
		}
	}
}

void Component::toBack()
{
	Array<Component*>& childList = parentComponent_->childComponentList_;

	if (isOnDesktop())
	{
		jassertfalse //xxx need to add this to native window
	}
	else if (parentComponent_ != 0 && childList.getFirst() != this)
	{
		const int index = childList.indexOf (this);

		if (index > 0)
		{
			int insertIndex = 0;

			if (flags.alwaysOnTopFlag)
			{
				while (insertIndex < childList.size()
						&& ! childList.getUnchecked (insertIndex)->isAlwaysOnTop())
				{
					++insertIndex;
				}
			}

			if (index != insertIndex)
			{
				childList.move (index, insertIndex);

				sendFakeMouseMove();
				repaintParent();
			}
		}
	}
}

void Component::setAlwaysOnTop (const bool shouldStayOnTop)
{
	if (shouldStayOnTop != flags.alwaysOnTopFlag)
	{
		flags.alwaysOnTopFlag = shouldStayOnTop;

		if (isOnDesktop())
		{
			ComponentPeer* const peer = getPeer();

			jassert (peer != 0);
			if (peer != 0)
			{
				if (! peer->setAlwaysOnTop (shouldStayOnTop))
				{
					// some kinds of peer can't change their always-on-top status, so
					// for these, we'll need to create a new window
					const int oldFlags = peer->getStyleFlags();
					removeFromDesktop();
					addToDesktop (oldFlags);
				}
			}
		}

		if (shouldStayOnTop)
			toFront (false);

		internalHierarchyChanged();
	}
}

bool Component::isAlwaysOnTop() const throw()
{
	return flags.alwaysOnTopFlag;
}

int Component::proportionOfWidth (const float proportion) const throw()
{
	return roundToInt (proportion * bounds_.getWidth());
}

int Component::proportionOfHeight (const float proportion) const throw()
{
	return roundToInt (proportion * bounds_.getHeight());
}

int Component::getParentWidth() const throw()
{
	return (parentComponent_ != 0) ? parentComponent_->getWidth()
								   : getParentMonitorArea().getWidth();
}

int Component::getParentHeight() const throw()
{
	return (parentComponent_ != 0) ? parentComponent_->getHeight()
								   : getParentMonitorArea().getHeight();
}

int Component::getScreenX() const
{
	return getScreenPosition().getX();
}

int Component::getScreenY() const
{
	return getScreenPosition().getY();
}

const Point<int> Component::getScreenPosition() const
{
	return (parentComponent_ != 0) ? parentComponent_->getScreenPosition() + getPosition()
								   : (flags.hasHeavyweightPeerFlag ? getPeer()->getScreenPosition()
																   : getPosition());
}

const Rectangle<int> Component::getScreenBounds() const
{
	return bounds_.withPosition (getScreenPosition());
}

const Point<int> Component::relativePositionToGlobal (const Point<int>& relativePosition) const
{
	const Component* c = this;
	Point<int> p (relativePosition);

	do
	{
		if (c->flags.hasHeavyweightPeerFlag)
			return c->getPeer()->relativePositionToGlobal (p);

		p += c->getPosition();
		c = c->parentComponent_;
	}
	while (c != 0);

	return p;
}

const Point<int> Component::globalPositionToRelative (const Point<int>& screenPosition) const
{
	if (flags.hasHeavyweightPeerFlag)
	{
		return getPeer()->globalPositionToRelative (screenPosition);
	}
	else
	{
		if (parentComponent_ != 0)
			return parentComponent_->globalPositionToRelative (screenPosition) - getPosition();

		return screenPosition - getPosition();
	}
}

const Point<int> Component::relativePositionToOtherComponent (const Component* const targetComponent, const Point<int>& positionRelativeToThis) const
{
	Point<int> p (positionRelativeToThis);

	if (targetComponent != 0)
	{
		const Component* c = this;

		do
		{
			if (c == targetComponent)
				return p;

			if (c->flags.hasHeavyweightPeerFlag)
			{
				p = c->getPeer()->relativePositionToGlobal (p);
				break;
			}

			p += c->getPosition();
			c = c->parentComponent_;
		}
		while (c != 0);

		p = targetComponent->globalPositionToRelative (p);
	}

	return p;
}

void Component::setBounds (int x, int y, int w, int h)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (w < 0) w = 0;
	if (h < 0) h = 0;

	const bool wasResized  = (getWidth() != w || getHeight() != h);
	const bool wasMoved	= (getX() != x || getY() != y);

#ifdef JUCE_DEBUG
	// It's a very bad idea to try to resize a window during its paint() method!
	jassert (! (flags.isInsidePaintCall && wasResized && isOnDesktop()));
#endif

	if (wasMoved || wasResized)
	{
		if (flags.visibleFlag)
		{
			// send a fake mouse move to trigger enter/exit messages if needed..
			sendFakeMouseMove();

			if (! flags.hasHeavyweightPeerFlag)
				repaintParent();
		}

		bounds_.setBounds (x, y, w, h);

		if (wasResized)
			repaint();
		else if (! flags.hasHeavyweightPeerFlag)
			repaintParent();

		if (flags.hasHeavyweightPeerFlag)
		{
			ComponentPeer* const peer = getPeer();

			if (peer != 0)
			{
				if (wasMoved && wasResized)
					peer->setBounds (getX(), getY(), getWidth(), getHeight(), false);
				else if (wasMoved)
					peer->setPosition (getX(), getY());
				else if (wasResized)
					peer->setSize (getWidth(), getHeight());
			}
		}

		sendMovedResizedMessages (wasMoved, wasResized);
	}
}

void Component::sendMovedResizedMessages (const bool wasMoved, const bool wasResized)
{
	JUCE_TRY
	{
		if (wasMoved)
			moved();

		if (wasResized)
		{
			resized();

			for (int i = childComponentList_.size(); --i >= 0;)
			{
				childComponentList_.getUnchecked(i)->parentSizeChanged();

				i = jmin (i, childComponentList_.size());
			}
		}

		BailOutChecker checker (this);

		if (parentComponent_ != 0)
			parentComponent_->childBoundsChanged (this);

		if (! checker.shouldBailOut())
			componentListeners.callChecked (checker, &ComponentListener::componentMovedOrResized,
											*this, wasMoved, wasResized);
	}
	JUCE_CATCH_EXCEPTION
}

void Component::setSize (const int w, const int h)
{
	setBounds (getX(), getY(), w, h);
}

void Component::setTopLeftPosition (const int x, const int y)
{
	setBounds (x, y, getWidth(), getHeight());
}

void Component::setTopRightPosition (const int x, const int y)
{
	setTopLeftPosition (x - getWidth(), y);
}

void Component::setBounds (const Rectangle<int>& r)
{
	setBounds (r.getX(),
			   r.getY(),
			   r.getWidth(),
			   r.getHeight());
}

void Component::setBoundsRelative (const float x, const float y,
								   const float w, const float h)
{
	const int pw = getParentWidth();
	const int ph = getParentHeight();

	setBounds (roundToInt (x * pw),
			   roundToInt (y * ph),
			   roundToInt (w * pw),
			   roundToInt (h * ph));
}

void Component::setCentrePosition (const int x, const int y)
{
	setTopLeftPosition (x - getWidth() / 2,
						y - getHeight() / 2);
}

void Component::setCentreRelative (const float x, const float y)
{
	setCentrePosition (roundToInt (getParentWidth() * x),
					   roundToInt (getParentHeight() * y));
}

void Component::centreWithSize (const int width, const int height)
{
	setBounds ((getParentWidth() - width) / 2,
			   (getParentHeight() - height) / 2,
			   width,
			   height);
}

void Component::setBoundsInset (const BorderSize& borders)
{
	setBounds (borders.getLeft(),
			   borders.getTop(),
			   getParentWidth() - (borders.getLeftAndRight()),
			   getParentHeight() - (borders.getTopAndBottom()));
}

void Component::setBoundsToFit (int x, int y, int width, int height,
								const Justification& justification,
								const bool onlyReduceInSize)
{
	// it's no good calling this method unless both the component and
	// target rectangle have a finite size.
	jassert (getWidth() > 0 && getHeight() > 0 && width > 0 && height > 0);

	if (getWidth() > 0 && getHeight() > 0
		 && width > 0 && height > 0)
	{
		int newW, newH;

		if (onlyReduceInSize && getWidth() <= width && getHeight() <= height)
		{
			newW = getWidth();
			newH = getHeight();
		}
		else
		{
			const double imageRatio = getHeight() / (double) getWidth();
			const double targetRatio = height / (double) width;

			if (imageRatio <= targetRatio)
			{
				newW = width;
				newH = jmin (height, roundToInt (newW * imageRatio));
			}
			else
			{
				newH = height;
				newW = jmin (width, roundToInt (newH / imageRatio));
			}
		}

		if (newW > 0 && newH > 0)
		{
			int newX, newY;
			justification.applyToRectangle (newX, newY, newW, newH,
											x, y, width, height);

			setBounds (newX, newY, newW, newH);
		}
	}
}

bool Component::hitTest (int x, int y)
{
	if (! flags.ignoresMouseClicksFlag)
		return true;

	if (flags.allowChildMouseClicksFlag)
	{
		for (int i = getNumChildComponents(); --i >= 0;)
		{
			Component* const c = getChildComponent (i);

			if (c->isVisible()
				&& c->bounds_.contains (x, y)
				&& c->hitTest (x - c->getX(),
							   y - c->getY()))
			{
				return true;
			}
		}
	}

	return false;
}

void Component::setInterceptsMouseClicks (const bool allowClicks,
										  const bool allowClicksOnChildComponents) throw()
{
	flags.ignoresMouseClicksFlag = ! allowClicks;
	flags.allowChildMouseClicksFlag = allowClicksOnChildComponents;
}

void Component::getInterceptsMouseClicks (bool& allowsClicksOnThisComponent,
										  bool& allowsClicksOnChildComponents) const throw()
{
	allowsClicksOnThisComponent = ! flags.ignoresMouseClicksFlag;
	allowsClicksOnChildComponents = flags.allowChildMouseClicksFlag;
}

bool Component::contains (const int x, const int y)
{
	if (((unsigned int) x) < (unsigned int) getWidth()
		&& ((unsigned int) y) < (unsigned int) getHeight()
		 && hitTest (x, y))
	{
		if (parentComponent_ != 0)
		{
			return parentComponent_->contains (x + getX(),
											   y + getY());
		}
		else if (flags.hasHeavyweightPeerFlag)
		{
			const ComponentPeer* const peer = getPeer();

			if (peer != 0)
				return peer->contains (Point<int> (x, y), true);
		}
	}

	return false;
}

bool Component::reallyContains (int x, int y, const bool returnTrueIfWithinAChild)
{
	if (! contains (x, y))
		return false;

	Component* p = this;

	while (p->parentComponent_ != 0)
	{
		x += p->getX();
		y += p->getY();

		p = p->parentComponent_;
	}

	const Component* const c = p->getComponentAt (x, y);

	return (c == this) || (returnTrueIfWithinAChild && isParentOf (c));
}

Component* Component::getComponentAt (const Point<int>& position)
{
	return getComponentAt (position.getX(), position.getY());
}

Component* Component::getComponentAt (const int x, const int y)
{
	if (flags.visibleFlag
		 && ((unsigned int) x) < (unsigned int) getWidth()
		 && ((unsigned int) y) < (unsigned int) getHeight()
		 && hitTest (x, y))
	{
		for (int i = childComponentList_.size(); --i >= 0;)
		{
			Component* const child = childComponentList_.getUnchecked(i);

			Component* const c = child->getComponentAt (x - child->getX(),
														y - child->getY());

			if (c != 0)
				return c;
		}

		return this;
	}

	return 0;
}

void Component::addChildComponent (Component* const child, int zOrder)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (child != 0 && child->parentComponent_ != this)
	{
		if (child->parentComponent_ != 0)
			child->parentComponent_->removeChildComponent (child);
		else
			child->removeFromDesktop();

		child->parentComponent_ = this;

		if (child->isVisible())
			child->repaintParent();

		if (! child->isAlwaysOnTop())
		{
			if (zOrder < 0 || zOrder > childComponentList_.size())
				zOrder = childComponentList_.size();

			while (zOrder > 0)
			{
				if (! childComponentList_.getUnchecked (zOrder - 1)->isAlwaysOnTop())
					break;

				--zOrder;
			}
		}

		childComponentList_.insert (zOrder, child);

		child->internalHierarchyChanged();
		internalChildrenChanged();
	}
}

void Component::addAndMakeVisible (Component* const child, int zOrder)
{
	if (child != 0)
	{
		child->setVisible (true);
		addChildComponent (child, zOrder);
	}
}

void Component::removeChildComponent (Component* const child)
{
	removeChildComponent (childComponentList_.indexOf (child));
}

Component* Component::removeChildComponent (const int index)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	Component* const child = childComponentList_ [index];

	if (child != 0)
	{
		sendFakeMouseMove();
		child->repaintParent();

		childComponentList_.remove (index);
		child->parentComponent_ = 0;

		JUCE_TRY
		{
			if ((currentlyFocusedComponent == child)
				|| child->isParentOf (currentlyFocusedComponent))
			{
				// get rid first to force the grabKeyboardFocus to change to us.
				giveAwayFocus();
				grabKeyboardFocus();
			}
		}
#if JUCE_CATCH_UNHANDLED_EXCEPTIONS
		catch (const std::exception& e)
		{
			currentlyFocusedComponent = 0;
			Desktop::getInstance().triggerFocusCallback();
			JUCEApplication::sendUnhandledException (&e, __FILE__, __LINE__);
		}
		catch (...)
		{
			currentlyFocusedComponent = 0;
			Desktop::getInstance().triggerFocusCallback();
			JUCEApplication::sendUnhandledException (0, __FILE__, __LINE__);
		}
#endif

		child->internalHierarchyChanged();
		internalChildrenChanged();
	}

	return child;
}

void Component::removeAllChildren()
{
	while (childComponentList_.size() > 0)
		removeChildComponent (childComponentList_.size() - 1);
}

void Component::deleteAllChildren()
{
	while (childComponentList_.size() > 0)
		delete (removeChildComponent (childComponentList_.size() - 1));
}

int Component::getNumChildComponents() const throw()
{
	return childComponentList_.size();
}

Component* Component::getChildComponent (const int index) const throw()
{
	return childComponentList_ [index];
}

int Component::getIndexOfChildComponent (const Component* const child) const throw()
{
	return childComponentList_.indexOf (const_cast <Component*> (child));
}

Component* Component::getTopLevelComponent() const throw()
{
	const Component* comp = this;

	while (comp->parentComponent_ != 0)
		comp = comp->parentComponent_;

	return const_cast <Component*> (comp);
}

bool Component::isParentOf (const Component* possibleChild) const throw()
{
	if (! possibleChild->isValidComponent())
	{
		jassert (possibleChild == 0);
		return false;
	}

	while (possibleChild != 0)
	{
		possibleChild = possibleChild->parentComponent_;

		if (possibleChild == this)
			return true;
	}

	return false;
}

void Component::parentHierarchyChanged()
{
}

void Component::childrenChanged()
{
}

void Component::internalChildrenChanged()
{
	if (componentListeners.isEmpty())
	{
		childrenChanged();
	}
	else
	{
		BailOutChecker checker (this);

		childrenChanged();

		if (! checker.shouldBailOut())
			componentListeners.callChecked (checker, &ComponentListener::componentChildrenChanged, *this);
	}
}

void Component::internalHierarchyChanged()
{
	BailOutChecker checker (this);

	parentHierarchyChanged();

	if (checker.shouldBailOut())
		return;

	componentListeners.callChecked (checker, &ComponentListener::componentParentHierarchyChanged, *this);

	if (checker.shouldBailOut())
		return;

	for (int i = childComponentList_.size(); --i >= 0;)
	{
		childComponentList_.getUnchecked (i)->internalHierarchyChanged();

		if (checker.shouldBailOut())
		{
			// you really shouldn't delete the parent component during a callback telling you
			// that it's changed..
			jassertfalse;
			return;
		}

		i = jmin (i, childComponentList_.size());
	}
}

void* Component::runModalLoopCallback (void* userData)
{
	return (void*) (pointer_sized_int) static_cast <Component*> (userData)->runModalLoop();
}

int Component::runModalLoop()
{
	if (! MessageManager::getInstance()->isThisTheMessageThread())
	{
		// use a callback so this can be called from non-gui threads
		return (int) (pointer_sized_int)
					MessageManager::getInstance()
					   ->callFunctionOnMessageThread (&runModalLoopCallback, this);
	}

	SafePointer<Component> prevFocused (getCurrentlyFocusedComponent());

	if (! isCurrentlyModal())
		enterModalState();

	JUCE_TRY
	{
		while (flags.currentlyModalFlag && flags.visibleFlag)
		{
			if  (! MessageManager::getInstance()->runDispatchLoopUntil (20))
				break;

			// check whether this component was deleted during the last message
			if (! isValidMessageListener())
				break;
		}
	}
#if JUCE_CATCH_UNHANDLED_EXCEPTIONS
	catch (const std::exception& e)
	{
		JUCEApplication::sendUnhandledException (&e, __FILE__, __LINE__);
		return 0;
	}
	catch (...)
	{
		JUCEApplication::sendUnhandledException (0, __FILE__, __LINE__);
		return 0;
	}
#endif

	const int modalIndex = modalComponentReturnValueKeys.indexOf (this);
	int returnValue = 0;

	if (modalIndex >= 0)
	{
		modalComponentReturnValueKeys.remove (modalIndex);
		returnValue = modalReturnValues.remove (modalIndex);
	}

	modalComponentStack.removeValue (this);

	if (prevFocused != 0)
		prevFocused->grabKeyboardFocus();

	return returnValue;
}

void Component::enterModalState (const bool takeKeyboardFocus_)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	// Check for an attempt to make a component modal when it already is!
	// This can cause nasty problems..
	jassert (! flags.currentlyModalFlag);

	if (! isCurrentlyModal())
	{
		modalComponentStack.add (this);
		modalComponentReturnValueKeys.add (this);
		modalReturnValues.add (0);

		flags.currentlyModalFlag = true;
		setVisible (true);

		if (takeKeyboardFocus_)
			grabKeyboardFocus();
	}
}

void Component::exitModalState (const int returnValue)
{
	if (isCurrentlyModal())
	{
		if (MessageManager::getInstance()->isThisTheMessageThread())
		{
			const int modalIndex = modalComponentReturnValueKeys.indexOf (this);

			if (modalIndex >= 0)
			{
				modalReturnValues.set (modalIndex, returnValue);
			}
			else
			{
				modalComponentReturnValueKeys.add (this);
				modalReturnValues.add (returnValue);
			}

			modalComponentStack.removeValue (this);

			flags.currentlyModalFlag = false;

			bringModalComponentToFront();
		}
		else
		{
			postMessage (new Message (exitModalStateMessage, returnValue, 0, 0));
		}
	}
}

bool Component::isCurrentlyModal() const throw()
{
	return flags.currentlyModalFlag
			&& getCurrentlyModalComponent() == this;
}

bool Component::isCurrentlyBlockedByAnotherModalComponent() const
{
	Component* const mc = getCurrentlyModalComponent();

	return mc != 0
			&& mc != this
			&& (! mc->isParentOf (this))
			&& ! mc->canModalEventBeSentToComponent (this);
}

int JUCE_CALLTYPE Component::getNumCurrentlyModalComponents() throw()
{
	return modalComponentStack.size();
}

Component* JUCE_CALLTYPE Component::getCurrentlyModalComponent (int index) throw()
{
	Component* const c = static_cast <Component*> (modalComponentStack [modalComponentStack.size() - index - 1]);

	return c->isValidComponent() ? c : 0;
}

void Component::bringModalComponentToFront()
{
	ComponentPeer* lastOne = 0;

	for (int i = 0; i < getNumCurrentlyModalComponents(); ++i)
	{
		Component* const c = getCurrentlyModalComponent (i);

		if (c == 0)
			break;

		ComponentPeer* peer = c->getPeer();

		if (peer != 0 && peer != lastOne)
		{
			if (lastOne == 0)
			{
				peer->toFront (true);
				peer->grabFocus();
			}
			else
				peer->toBehind (lastOne);

			lastOne = peer;
		}
	}
}

void Component::setBroughtToFrontOnMouseClick (const bool shouldBeBroughtToFront) throw()
{
	flags.bringToFrontOnClickFlag = shouldBeBroughtToFront;
}

bool Component::isBroughtToFrontOnMouseClick() const throw()
{
	return flags.bringToFrontOnClickFlag;
}

void Component::setMouseCursor (const MouseCursor& cursor)
{
	if (cursor_ != cursor)
	{
		cursor_ = cursor;

		if (flags.visibleFlag)
			updateMouseCursor();
	}
}

const MouseCursor Component::getMouseCursor()
{
	return cursor_;
}

void Component::updateMouseCursor() const
{
	sendFakeMouseMove();
}

void Component::setRepaintsOnMouseActivity (const bool shouldRepaint) throw()
{
	flags.repaintOnMouseActivityFlag = shouldRepaint;
}

void Component::repaintParent()
{
	if (flags.visibleFlag)
		internalRepaint (0, 0, getWidth(), getHeight());
}

void Component::repaint()
{
	repaint (0, 0, getWidth(), getHeight());
}

void Component::repaint (const int x, const int y,
						 const int w, const int h)
{
	deleteAndZero (bufferedImage_);

	if (flags.visibleFlag)
		internalRepaint (x, y, w, h);
}

void Component::internalRepaint (int x, int y, int w, int h)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (x < 0)
	{
		w += x;
		x = 0;
	}

	if (x + w > getWidth())
		w = getWidth() - x;

	if (w > 0)
	{
		if (y < 0)
		{
			h += y;
			y = 0;
		}

		if (y + h > getHeight())
			h = getHeight() - y;

		if (h > 0)
		{
			if (parentComponent_ != 0)
			{
				x += getX();
				y += getY();

				if (parentComponent_->flags.visibleFlag)
					parentComponent_->internalRepaint (x, y, w, h);
			}
			else if (flags.hasHeavyweightPeerFlag)
			{
				ComponentPeer* const peer = getPeer();

				if (peer != 0)
					peer->repaint (x, y, w, h);
			}
		}
	}
}

void Component::renderComponent (Graphics& g)
{
	const Rectangle<int> clipBounds (g.getClipBounds());

	g.saveState();
	clipObscuredRegions (g, clipBounds, 0, 0);

	if (! g.isClipEmpty())
	{
		if (flags.bufferToImageFlag)
		{
			if (bufferedImage_ == 0)
			{
				bufferedImage_ = Image::createNativeImage (flags.opaqueFlag ? Image::RGB : Image::ARGB,
														   getWidth(), getHeight(), ! flags.opaqueFlag);

				Graphics imG (*bufferedImage_);
				paint (imG);
			}

			g.setColour (Colours::black);
			g.drawImageAt (bufferedImage_, 0, 0);
		}
		else
		{
			paint (g);
		}
	}

	g.restoreState();

	for (int i = 0; i < childComponentList_.size(); ++i)
	{
		Component* const child = childComponentList_.getUnchecked (i);

		if (child->isVisible() && clipBounds.intersects (child->getBounds()))
		{
			g.saveState();

			if (g.reduceClipRegion (child->getX(), child->getY(),
									child->getWidth(), child->getHeight()))
			{
				for (int j = i + 1; j < childComponentList_.size(); ++j)
				{
					const Component* const sibling = childComponentList_.getUnchecked (j);

					if (sibling->flags.opaqueFlag && sibling->isVisible())
						g.excludeClipRegion (sibling->getBounds());
				}

				if (! g.isClipEmpty())
				{
					g.setOrigin (child->getX(), child->getY());
					child->paintEntireComponent (g);
				}
			}

			g.restoreState();
		}
	}

	g.saveState();
	paintOverChildren (g);
	g.restoreState();
}

void Component::paintEntireComponent (Graphics& g)
{
	jassert (! g.isClipEmpty());

#ifdef JUCE_DEBUG
	flags.isInsidePaintCall = true;
#endif

	if (effect_ != 0)
	{
		ScopedPointer<Image> effectImage (Image::createNativeImage (flags.opaqueFlag ? Image::RGB : Image::ARGB,
																	getWidth(), getHeight(),
																	! flags.opaqueFlag));
		{
			Graphics g2 (*effectImage);
			renderComponent (g2);
		}

		effect_->applyEffect (*effectImage, g);
	}
	else
	{
		renderComponent (g);
	}

#ifdef JUCE_DEBUG
	flags.isInsidePaintCall = false;
#endif
}

Image* Component::createComponentSnapshot (const Rectangle<int>& areaToGrab,
										   const bool clipImageToComponentBounds)
{
	Rectangle<int> r (areaToGrab);

	if (clipImageToComponentBounds)
		r = r.getIntersection (getLocalBounds());

	ScopedPointer<Image> componentImage (Image::createNativeImage (flags.opaqueFlag ? Image::RGB : Image::ARGB,
																   jmax (1, r.getWidth()),
																   jmax (1, r.getHeight()),
																   true));

	Graphics imageContext (*componentImage);
	imageContext.setOrigin (-r.getX(), -r.getY());
	paintEntireComponent (imageContext);

	return componentImage.release();
}

void Component::setComponentEffect (ImageEffectFilter* const effect)
{
	if (effect_ != effect)
	{
		effect_ = effect;
		repaint();
	}
}

LookAndFeel& Component::getLookAndFeel() const throw()
{
	const Component* c = this;

	do
	{
		if (c->lookAndFeel_ != 0)
			return *(c->lookAndFeel_);

		c = c->parentComponent_;
	}
	while (c != 0);

	return LookAndFeel::getDefaultLookAndFeel();
}

void Component::setLookAndFeel (LookAndFeel* const newLookAndFeel)
{
	if (lookAndFeel_ != newLookAndFeel)
	{
		lookAndFeel_ = newLookAndFeel;

		sendLookAndFeelChange();
	}
}

void Component::lookAndFeelChanged()
{
}

void Component::sendLookAndFeelChange()
{
	repaint();

	lookAndFeelChanged();

	// (it's not a great idea to do anything that would delete this component
	//  during the lookAndFeelChanged() callback)
	jassert (isValidComponent());

	SafePointer<Component> safePointer (this);

	for (int i = childComponentList_.size(); --i >= 0;)
	{
		childComponentList_.getUnchecked (i)->sendLookAndFeelChange();

		if (safePointer == 0)
			return;

		i = jmin (i, childComponentList_.size());
	}
}

static const var::identifier getColourPropertyId (const int colourId)
{
	String s;
	s.preallocateStorage (18);
	s << "jcclr_" << String::toHexString (colourId);
	return s;
}

const Colour Component::findColour (const int colourId, const bool inheritFromParent) const
{
	var* v = properties.getItem (getColourPropertyId (colourId));

	if (v != 0)
		return Colour ((int) *v);

	if (inheritFromParent && parentComponent_ != 0)
		return parentComponent_->findColour (colourId, true);

	return getLookAndFeel().findColour (colourId);
}

bool Component::isColourSpecified (const int colourId) const
{
	return properties.contains (getColourPropertyId (colourId));
}

void Component::removeColour (const int colourId)
{
	if (properties.remove (getColourPropertyId (colourId)))
		colourChanged();
}

void Component::setColour (const int colourId, const Colour& colour)
{
	if (properties.set (getColourPropertyId (colourId), (int) colour.getARGB()))
		colourChanged();
}

void Component::copyAllExplicitColoursTo (Component& target) const
{
	bool changed = false;

	for (int i = properties.size(); --i >= 0;)
	{
		const var::identifier name (properties.getName(i));

		if (name.name.startsWith ("jcclr_"))
			if (target.properties.set (name, properties [name]))
				changed = true;
	}

	if (changed)
		target.colourChanged();
}

void Component::colourChanged()
{
}

const Rectangle<int> Component::getLocalBounds() const throw()
{
	return Rectangle<int> (0, 0, getWidth(), getHeight());
}

const Rectangle<int> Component::getUnclippedArea() const
{
	int x = 0, y = 0, w = getWidth(), h = getHeight();

	Component* p = parentComponent_;
	int px = getX();
	int py = getY();

	while (p != 0)
	{
		if (! Rectangle<int>::intersectRectangles (x, y, w, h, -px, -py, p->getWidth(), p->getHeight()))
			return Rectangle<int>();

		px += p->getX();
		py += p->getY();
		p = p->parentComponent_;
	}

	return Rectangle<int> (x, y, w, h);
}

void Component::clipObscuredRegions (Graphics& g, const Rectangle<int>& clipRect,
									 const int deltaX, const int deltaY) const
{
	for (int i = childComponentList_.size(); --i >= 0;)
	{
		const Component* const c = childComponentList_.getUnchecked(i);

		if (c->isVisible())
		{
			const Rectangle<int> newClip (clipRect.getIntersection (c->bounds_));

			if (! newClip.isEmpty())
			{
				if (c->isOpaque())
				{
					g.excludeClipRegion (newClip.translated (deltaX, deltaY));
				}
				else
				{
					c->clipObscuredRegions (g, newClip.translated (-c->getX(), -c->getY()),
											c->getX() + deltaX,
											c->getY() + deltaY);
				}
			}
		}
	}
}

void Component::getVisibleArea (RectangleList& result, const bool includeSiblings) const
{
	result.clear();
	const Rectangle<int> unclipped (getUnclippedArea());

	if (! unclipped.isEmpty())
	{
		result.add (unclipped);

		if (includeSiblings)
		{
			const Component* const c = getTopLevelComponent();

			c->subtractObscuredRegions (result, c->relativePositionToOtherComponent (this, Point<int>()),
										c->getLocalBounds(), this);
		}

		subtractObscuredRegions (result, Point<int>(), unclipped, 0);
		result.consolidate();
	}
}

void Component::subtractObscuredRegions (RectangleList& result,
										 const Point<int>& delta,
										 const Rectangle<int>& clipRect,
										 const Component* const compToAvoid) const
{
	for (int i = childComponentList_.size(); --i >= 0;)
	{
		const Component* const c = childComponentList_.getUnchecked(i);

		if (c != compToAvoid && c->isVisible())
		{
			if (c->isOpaque())
			{
				Rectangle<int> childBounds (c->bounds_.getIntersection (clipRect));
				childBounds.translate (delta.getX(), delta.getY());

				result.subtract (childBounds);
			}
			else
			{
				Rectangle<int> newClip (clipRect.getIntersection (c->bounds_));
				newClip.translate (-c->getX(), -c->getY());

				c->subtractObscuredRegions (result, c->getPosition() + delta,
											newClip, compToAvoid);
			}
		}
	}
}

void Component::mouseEnter (const MouseEvent&)
{
	// base class does nothing
}

void Component::mouseExit (const MouseEvent&)
{
	// base class does nothing
}

void Component::mouseDown (const MouseEvent&)
{
	// base class does nothing
}

void Component::mouseUp (const MouseEvent&)
{
	// base class does nothing
}

void Component::mouseDrag (const MouseEvent&)
{
	// base class does nothing
}

void Component::mouseMove (const MouseEvent&)
{
	// base class does nothing
}

void Component::mouseDoubleClick (const MouseEvent&)
{
	// base class does nothing
}

void Component::mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
	// the base class just passes this event up to its parent..

	if (parentComponent_ != 0)
		parentComponent_->mouseWheelMove (e.getEventRelativeTo (parentComponent_),
										  wheelIncrementX, wheelIncrementY);
}

void Component::resized()
{
	// base class does nothing
}

void Component::moved()
{
	// base class does nothing
}

void Component::childBoundsChanged (Component*)
{
	// base class does nothing
}

void Component::parentSizeChanged()
{
	// base class does nothing
}

void Component::addComponentListener (ComponentListener* const newListener)
{
	jassert (isValidComponent());
	componentListeners.add (newListener);
}

void Component::removeComponentListener (ComponentListener* const listenerToRemove)
{
	jassert (isValidComponent());
	componentListeners.remove (listenerToRemove);
}

void Component::inputAttemptWhenModal()
{
	bringModalComponentToFront();
	getLookAndFeel().playAlertSound();
}

bool Component::canModalEventBeSentToComponent (const Component*)
{
	return false;
}

void Component::internalModalInputAttempt()
{
	Component* const current = getCurrentlyModalComponent();

	if (current != 0)
		current->inputAttemptWhenModal();
}

void Component::paint (Graphics&)
{
	// all painting is done in the subclasses

	jassert (! isOpaque()); // if your component's opaque, you've gotta paint it!
}

void Component::paintOverChildren (Graphics&)
{
	// all painting is done in the subclasses
}

void Component::handleMessage (const Message& message)
{
	if (message.intParameter1 == exitModalStateMessage)
	{
		exitModalState (message.intParameter2);
	}
	else if (message.intParameter1 == customCommandMessage)
	{
		handleCommandMessage (message.intParameter2);
	}
}

void Component::postCommandMessage (const int commandId)
{
	postMessage (new Message (customCommandMessage, commandId, 0, 0));
}

void Component::handleCommandMessage (int)
{
	// used by subclasses
}

void Component::addMouseListener (MouseListener* const newListener,
								  const bool wantsEventsForAllNestedChildComponents)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (mouseListeners_ == 0)
		mouseListeners_ = new Array<MouseListener*>();

	if (! mouseListeners_->contains (newListener))
	{
		if (wantsEventsForAllNestedChildComponents)
		{
			mouseListeners_->insert (0, newListener);
			++numDeepMouseListeners;
		}
		else
		{
			mouseListeners_->add (newListener);
		}
	}
}

void Component::removeMouseListener (MouseListener* const listenerToRemove)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (mouseListeners_ != 0)
	{
		const int index = mouseListeners_->indexOf (listenerToRemove);

		if (index >= 0)
		{
			if (index < numDeepMouseListeners)
				--numDeepMouseListeners;

			mouseListeners_->remove (index);
		}
	}
}

void Component::internalMouseEnter (MouseInputSource& source, const Point<int>& relativePos, const Time& time)
{
	if (isCurrentlyBlockedByAnotherModalComponent())
	{
		// if something else is modal, always just show a normal mouse cursor
		source.showMouseCursor (MouseCursor::NormalCursor);
		return;
	}

	if (! flags.mouseInsideFlag)
	{
		flags.mouseInsideFlag = true;
		flags.mouseOverFlag = true;
		flags.draggingFlag = false;

		BailOutChecker checker (this);

		if (flags.repaintOnMouseActivityFlag)
			repaint();

		const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
							 this, time, relativePos,
							 time, 0, false);

		mouseEnter (me);

		if (checker.shouldBailOut())
			return;

		Desktop::getInstance().resetTimer();
		Desktop::getInstance().mouseListeners.callChecked (checker, &MouseListener::mouseEnter, me);

		if (checker.shouldBailOut())
			return;

		if (mouseListeners_ != 0)
		{
			for (int i = mouseListeners_->size(); --i >= 0;)
			{
				mouseListeners_->getUnchecked(i)->mouseEnter (me);

				if (checker.shouldBailOut())
					return;

				i = jmin (i, mouseListeners_->size());
			}
		}

		Component* p = parentComponent_;

		while (p != 0)
		{
			if (p->numDeepMouseListeners > 0)
			{
				BailOutChecker checker (this, p);

				for (int i = p->numDeepMouseListeners; --i >= 0;)
				{
					p->mouseListeners_->getUnchecked(i)->mouseEnter (me);

					if (checker.shouldBailOut())
						return;

					i = jmin (i, p->numDeepMouseListeners);
				}
			}

			p = p->parentComponent_;
		}
	}
}

void Component::internalMouseExit (MouseInputSource& source, const Point<int>& relativePos, const Time& time)
{
	BailOutChecker checker (this);

	if (flags.draggingFlag)
	{
		internalMouseUp (source, relativePos, time, source.getCurrentModifiers().getRawFlags());

		if (checker.shouldBailOut())
			return;
	}

	if (flags.mouseInsideFlag || flags.mouseOverFlag)
	{
		flags.mouseInsideFlag = false;
		flags.mouseOverFlag = false;
		flags.draggingFlag = false;

		if (flags.repaintOnMouseActivityFlag)
			repaint();

		const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
							 this, time, relativePos,
							 time, 0, false);
		mouseExit (me);

		if (checker.shouldBailOut())
			return;

		Desktop::getInstance().resetTimer();
		Desktop::getInstance().mouseListeners.callChecked (checker, &MouseListener::mouseExit, me);

		if (checker.shouldBailOut())
			return;

		if (mouseListeners_ != 0)
		{
			for (int i = mouseListeners_->size(); --i >= 0;)
			{
				((MouseListener*) mouseListeners_->getUnchecked (i))->mouseExit (me);

				if (checker.shouldBailOut())
					return;

				i = jmin (i, mouseListeners_->size());
			}
		}

		Component* p = parentComponent_;

		while (p != 0)
		{
			if (p->numDeepMouseListeners > 0)
			{
				BailOutChecker checker (this, p);

				for (int i = p->numDeepMouseListeners; --i >= 0;)
				{
					p->mouseListeners_->getUnchecked (i)->mouseExit (me);

					if (checker.shouldBailOut())
						return;

					i = jmin (i, p->numDeepMouseListeners);
				}
			}

			p = p->parentComponent_;
		}
	}
}

class InternalDragRepeater  : public Timer
{
public:
	InternalDragRepeater()
	{}

	~InternalDragRepeater()
	{
		clearSingletonInstance();
	}

	juce_DeclareSingleton_SingleThreaded_Minimal (InternalDragRepeater)

	void timerCallback()
	{
		Desktop& desktop = Desktop::getInstance();
		int numMiceDown = 0;

		for (int i = desktop.getNumMouseSources(); --i >= 0;)
		{
			MouseInputSource* const source = desktop.getMouseSource(i);
			if (source->isDragging())
			{
				source->triggerFakeMove();
				++numMiceDown;
			}
		}

		if (numMiceDown == 0)
			deleteInstance();
	}

	juce_UseDebuggingNewOperator

private:
	InternalDragRepeater (const InternalDragRepeater&);
	InternalDragRepeater& operator= (const InternalDragRepeater&);
};

juce_ImplementSingleton_SingleThreaded (InternalDragRepeater)

void Component::beginDragAutoRepeat (const int interval)
{
	if (interval > 0)
	{
		if (InternalDragRepeater::getInstance()->getTimerInterval() != interval)
			InternalDragRepeater::getInstance()->startTimer (interval);
	}
	else
	{
		InternalDragRepeater::deleteInstance();
	}
}

void Component::internalMouseDown (MouseInputSource& source, const Point<int>& relativePos, const Time& time)
{
	Desktop& desktop = Desktop::getInstance();

	BailOutChecker checker (this);

	if (isCurrentlyBlockedByAnotherModalComponent())
	{
		internalModalInputAttempt();

		if (checker.shouldBailOut())
			return;

		// If processing the input attempt has exited the modal loop, we'll allow the event
		// to be delivered..
		if (isCurrentlyBlockedByAnotherModalComponent())
		{
			// allow blocked mouse-events to go to global listeners..
			const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
								 this, time, relativePos, time,
								 source.getNumberOfMultipleClicks(), false);

			desktop.resetTimer();
			desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDown, me);
			return;
		}
	}

	{
		Component* c = this;

		while (c != 0)
		{
			if (c->isBroughtToFrontOnMouseClick())
			{
				c->toFront (true);

				if (checker.shouldBailOut())
					return;
			}

			c = c->parentComponent_;
		}
	}

	if (! flags.dontFocusOnMouseClickFlag)
	{
		grabFocusInternal (focusChangedByMouseClick);

		if (checker.shouldBailOut())
			return;
	}

	flags.draggingFlag = true;
	flags.mouseOverFlag = true;

	if (flags.repaintOnMouseActivityFlag)
		repaint();

	const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
						 this, time, relativePos, time,
						 source.getNumberOfMultipleClicks(), false);
	mouseDown (me);

	if (checker.shouldBailOut())
		return;

	desktop.resetTimer();
	desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDown, me);

	if (checker.shouldBailOut())
		return;

	if (mouseListeners_ != 0)
	{
		for (int i = mouseListeners_->size(); --i >= 0;)
		{
			((MouseListener*) mouseListeners_->getUnchecked (i))->mouseDown (me);

			if (checker.shouldBailOut())
				return;

			i = jmin (i, mouseListeners_->size());
		}
	}

	Component* p = parentComponent_;

	while (p != 0)
	{
		if (p->numDeepMouseListeners > 0)
		{
			BailOutChecker checker (this, p);

			for (int i = p->numDeepMouseListeners; --i >= 0;)
			{
				p->mouseListeners_->getUnchecked (i)->mouseDown (me);

				if (checker.shouldBailOut())
					return;

				i = jmin (i, p->numDeepMouseListeners);
			}
		}

		p = p->parentComponent_;
	}
}

void Component::internalMouseUp (MouseInputSource& source, const Point<int>& relativePos, const Time& time, const ModifierKeys& oldModifiers)
{
	if (flags.draggingFlag)
	{
		Desktop& desktop = Desktop::getInstance();

		flags.draggingFlag = false;

		BailOutChecker checker (this);

		if (flags.repaintOnMouseActivityFlag)
			repaint();

		const MouseEvent me (source, relativePos,
							 oldModifiers, this, time,
							 globalPositionToRelative (source.getLastMouseDownPosition()),
							 source.getLastMouseDownTime(),
							 source.getNumberOfMultipleClicks(),
							 source.hasMouseMovedSignificantlySincePressed());

		mouseUp (me);

		if (checker.shouldBailOut())
			return;

		desktop.resetTimer();
		desktop.mouseListeners.callChecked (checker, &MouseListener::mouseUp, me);

		if (checker.shouldBailOut())
			return;

		if (mouseListeners_ != 0)
		{
			for (int i = mouseListeners_->size(); --i >= 0;)
			{
				((MouseListener*) mouseListeners_->getUnchecked (i))->mouseUp (me);

				if (checker.shouldBailOut())
					return;

				i = jmin (i, mouseListeners_->size());
			}
		}

		{
			Component* p = parentComponent_;

			while (p != 0)
			{
				if (p->numDeepMouseListeners > 0)
				{
					BailOutChecker checker (this, p);

					for (int i = p->numDeepMouseListeners; --i >= 0;)
					{
						p->mouseListeners_->getUnchecked (i)->mouseUp (me);

						if (checker.shouldBailOut())
							return;

						i = jmin (i, p->numDeepMouseListeners);
					}
				}

				p = p->parentComponent_;
			}
		}

		// check for double-click
		if (me.getNumberOfClicks() >= 2)
		{
			const int numListeners = (mouseListeners_ != 0) ? mouseListeners_->size() : 0;

			mouseDoubleClick (me);

			if (checker.shouldBailOut())
				return;

			desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDoubleClick, me);

			if (checker.shouldBailOut())
				return;

			for (int i = numListeners; --i >= 0;)
			{
				if (checker.shouldBailOut())
					return;

				MouseListener* const ml = (MouseListener*)((*mouseListeners_)[i]);
				if (ml != 0)
					ml->mouseDoubleClick (me);
			}

			if (checker.shouldBailOut())
				return;

			Component* p = parentComponent_;

			while (p != 0)
			{
				if (p->numDeepMouseListeners > 0)
				{
					BailOutChecker checker (this, p);

					for (int i = p->numDeepMouseListeners; --i >= 0;)
					{
						p->mouseListeners_->getUnchecked (i)->mouseDoubleClick (me);

						if (checker.shouldBailOut())
							return;

						i = jmin (i, p->numDeepMouseListeners);
					}
				}

				p = p->parentComponent_;
			}
		}
	}
}

void Component::internalMouseDrag (MouseInputSource& source, const Point<int>& relativePos, const Time& time)
{
	if (flags.draggingFlag)
	{
		Desktop& desktop = Desktop::getInstance();

		flags.mouseOverFlag = reallyContains (relativePos.getX(), relativePos.getY(), false);

		BailOutChecker checker (this);

		const MouseEvent me (source, relativePos,
							 source.getCurrentModifiers(), this, time,
							 globalPositionToRelative (source.getLastMouseDownPosition()),
							 source.getLastMouseDownTime(),
							 source.getNumberOfMultipleClicks(),
							 source.hasMouseMovedSignificantlySincePressed());

		mouseDrag (me);

		if (checker.shouldBailOut())
			return;

		desktop.resetTimer();
		desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);

		if (checker.shouldBailOut())
			return;

		if (mouseListeners_ != 0)
		{
			for (int i = mouseListeners_->size(); --i >= 0;)
			{
				((MouseListener*) mouseListeners_->getUnchecked (i))->mouseDrag (me);

				if (checker.shouldBailOut())
					return;

				i = jmin (i, mouseListeners_->size());
			}
		}

		Component* p = parentComponent_;

		while (p != 0)
		{
			if (p->numDeepMouseListeners > 0)
			{
				BailOutChecker checker (this, p);

				for (int i = p->numDeepMouseListeners; --i >= 0;)
				{
					p->mouseListeners_->getUnchecked (i)->mouseDrag (me);

					if (checker.shouldBailOut())
						return;

					i = jmin (i, p->numDeepMouseListeners);
				}
			}

			p = p->parentComponent_;
		}
	}
}

void Component::internalMouseMove (MouseInputSource& source, const Point<int>& relativePos, const Time& time)
{
	Desktop& desktop = Desktop::getInstance();
	BailOutChecker checker (this);

	const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
						 this, time, relativePos,
						 time, 0, false);

	if (isCurrentlyBlockedByAnotherModalComponent())
	{
		// allow blocked mouse-events to go to global listeners..
		desktop.sendMouseMove();
	}
	else
	{
		flags.mouseOverFlag = true;

		mouseMove (me);

		if (checker.shouldBailOut())
			return;

		desktop.resetTimer();
		desktop.mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);

		if (checker.shouldBailOut())
			return;

		if (mouseListeners_ != 0)
		{
			for (int i = mouseListeners_->size(); --i >= 0;)
			{
				((MouseListener*) mouseListeners_->getUnchecked (i))->mouseMove (me);

				if (checker.shouldBailOut())
					return;

				i = jmin (i, mouseListeners_->size());
			}
		}

		Component* p = parentComponent_;

		while (p != 0)
		{
			if (p->numDeepMouseListeners > 0)
			{
				BailOutChecker checker (this, p);

				for (int i = p->numDeepMouseListeners; --i >= 0;)
				{
					p->mouseListeners_->getUnchecked (i)->mouseMove (me);

					if (checker.shouldBailOut())
						return;

					i = jmin (i, p->numDeepMouseListeners);
				}
			}

			p = p->parentComponent_;
		}
	}
}

void Component::internalMouseWheel (MouseInputSource& source, const Point<int>& relativePos,
									const Time& time, const float amountX, const float amountY)
{
	Desktop& desktop = Desktop::getInstance();
	BailOutChecker checker (this);

	const float wheelIncrementX = amountX * (1.0f / 256.0f);
	const float wheelIncrementY = amountY * (1.0f / 256.0f);

	const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
						 this, time, relativePos, time, 0, false);

	if (isCurrentlyBlockedByAnotherModalComponent())
	{
		// allow blocked mouse-events to go to global listeners..
		desktop.mouseListeners.callChecked (checker, &MouseListener::mouseWheelMove, me, wheelIncrementX, wheelIncrementY);
	}
	else
	{
		mouseWheelMove (me, wheelIncrementX, wheelIncrementY);

		if (checker.shouldBailOut())
			return;

		desktop.mouseListeners.callChecked (checker, &MouseListener::mouseWheelMove, me, wheelIncrementX, wheelIncrementY);

		if (checker.shouldBailOut())
			return;

		if (mouseListeners_ != 0)
		{
			for (int i = mouseListeners_->size(); --i >= 0;)
			{
				((MouseListener*) mouseListeners_->getUnchecked (i))->mouseWheelMove (me, wheelIncrementX, wheelIncrementY);

				if (checker.shouldBailOut())
					return;

				i = jmin (i, mouseListeners_->size());
			}
		}

		Component* p = parentComponent_;

		while (p != 0)
		{
			if (p->numDeepMouseListeners > 0)
			{
				BailOutChecker checker (this, p);

				for (int i = p->numDeepMouseListeners; --i >= 0;)
				{
					p->mouseListeners_->getUnchecked (i)->mouseWheelMove (me, wheelIncrementX, wheelIncrementY);

					if (checker.shouldBailOut())
						return;

					i = jmin (i, p->numDeepMouseListeners);
				}
			}

			p = p->parentComponent_;
		}
	}
}

void Component::sendFakeMouseMove() const
{
	Desktop::getInstance().getMainMouseSource().triggerFakeMove();
}

void Component::broughtToFront()
{
}

void Component::internalBroughtToFront()
{
	if (! isValidComponent())
		return;

	if (flags.hasHeavyweightPeerFlag)
		Desktop::getInstance().componentBroughtToFront (this);

	BailOutChecker checker (this);
	broughtToFront();

	if (checker.shouldBailOut())
		return;

	componentListeners.callChecked (checker, &ComponentListener::componentBroughtToFront, *this);

	if (checker.shouldBailOut())
		return;

	// When brought to the front and there's a modal component blocking this one,
	// we need to bring the modal one to the front instead..
	Component* const cm = getCurrentlyModalComponent();

	if (cm != 0 && cm->getTopLevelComponent() != getTopLevelComponent())
		bringModalComponentToFront();
}

void Component::focusGained (FocusChangeType)
{
	// base class does nothing
}

void Component::internalFocusGain (const FocusChangeType cause)
{
	SafePointer<Component> safePointer (this);

	focusGained (cause);

	if (safePointer != 0)
		internalChildFocusChange (cause);
}

void Component::focusLost (FocusChangeType)
{
	// base class does nothing
}

void Component::internalFocusLoss (const FocusChangeType cause)
{
	SafePointer<Component> safePointer (this);

	focusLost (focusChangedDirectly);

	if (safePointer != 0)
		internalChildFocusChange (cause);
}

void Component::focusOfChildComponentChanged (FocusChangeType /*cause*/)
{
	// base class does nothing
}

void Component::internalChildFocusChange (FocusChangeType cause)
{
	const bool childIsNowFocused = hasKeyboardFocus (true);

	if (flags.childCompFocusedFlag != childIsNowFocused)
	{
		flags.childCompFocusedFlag = childIsNowFocused;

		SafePointer<Component> safePointer (this);
		focusOfChildComponentChanged (cause);

		if (safePointer == 0)
			return;
	}

	if (parentComponent_ != 0)
		parentComponent_->internalChildFocusChange (cause);
}

bool Component::isEnabled() const throw()
{
	return (! flags.isDisabledFlag)
			&& (parentComponent_ == 0 || parentComponent_->isEnabled());
}

void Component::setEnabled (const bool shouldBeEnabled)
{
	if (flags.isDisabledFlag == shouldBeEnabled)
	{
		flags.isDisabledFlag = ! shouldBeEnabled;

		// if any parent components are disabled, setting our flag won't make a difference,
		// so no need to send a change message
		if (parentComponent_ == 0 || parentComponent_->isEnabled())
			sendEnablementChangeMessage();
	}
}

void Component::sendEnablementChangeMessage()
{
	SafePointer<Component> safePointer (this);

	enablementChanged();

	if (safePointer == 0)
		return;

	for (int i = getNumChildComponents(); --i >= 0;)
	{
		Component* const c = getChildComponent (i);

		if (c != 0)
		{
			c->sendEnablementChangeMessage();

			if (safePointer == 0)
				return;
		}
	}
}

void Component::enablementChanged()
{
}

void Component::setWantsKeyboardFocus (const bool wantsFocus) throw()
{
	flags.wantsFocusFlag = wantsFocus;
}

void Component::setMouseClickGrabsKeyboardFocus (const bool shouldGrabFocus)
{
	flags.dontFocusOnMouseClickFlag = ! shouldGrabFocus;
}

bool Component::getMouseClickGrabsKeyboardFocus() const throw()
{
	return ! flags.dontFocusOnMouseClickFlag;
}

bool Component::getWantsKeyboardFocus() const throw()
{
	return flags.wantsFocusFlag && ! flags.isDisabledFlag;
}

void Component::setFocusContainer (const bool shouldBeFocusContainer) throw()
{
	flags.isFocusContainerFlag = shouldBeFocusContainer;
}

bool Component::isFocusContainer() const throw()
{
	return flags.isFocusContainerFlag;
}

int Component::getExplicitFocusOrder() const
{
	return properties ["_jexfo"];
}

void Component::setExplicitFocusOrder (const int newFocusOrderIndex)
{
	properties.set ("_jexfo", newFocusOrderIndex);
}

KeyboardFocusTraverser* Component::createFocusTraverser()
{
	if (flags.isFocusContainerFlag || parentComponent_ == 0)
		return new KeyboardFocusTraverser();

	return parentComponent_->createFocusTraverser();
}

void Component::takeKeyboardFocus (const FocusChangeType cause)
{
	// give the focus to this component
	if (currentlyFocusedComponent != this)
	{
		JUCE_TRY
		{
			// get the focus onto our desktop window
			ComponentPeer* const peer = getPeer();

			if (peer != 0)
			{
				SafePointer<Component> safePointer (this);

				peer->grabFocus();

				if (peer->isFocused() && currentlyFocusedComponent != this)
				{
					Component* const componentLosingFocus = currentlyFocusedComponent;

					currentlyFocusedComponent = this;

					Desktop::getInstance().triggerFocusCallback();

					// call this after setting currentlyFocusedComponent so that the one that's
					// losing it has a chance to see where focus is going
					if (componentLosingFocus->isValidComponent())
						componentLosingFocus->internalFocusLoss (cause);

					if (currentlyFocusedComponent == this)
					{
						focusGained (cause);

						if (safePointer != 0)
							internalChildFocusChange (cause);
					}
				}
			}
		}
#if JUCE_CATCH_UNHANDLED_EXCEPTIONS
		catch (const std::exception& e)
		{
			currentlyFocusedComponent = 0;
			Desktop::getInstance().triggerFocusCallback();
			JUCEApplication::sendUnhandledException (&e, __FILE__, __LINE__);
		}
		catch (...)
		{
			currentlyFocusedComponent = 0;
			Desktop::getInstance().triggerFocusCallback();
			JUCEApplication::sendUnhandledException (0, __FILE__, __LINE__);
		}
#endif
	}
}

void Component::grabFocusInternal (const FocusChangeType cause, const bool canTryParent)
{
	if (isShowing())
	{
		if (flags.wantsFocusFlag && (isEnabled() || parentComponent_ == 0))
		{
			takeKeyboardFocus (cause);
		}
		else
		{
			if (isParentOf (currentlyFocusedComponent)
				 && currentlyFocusedComponent->isShowing())
			{
				// do nothing if the focused component is actually a child of ours..
			}
			else
			{
				// find the default child component..
				ScopedPointer <KeyboardFocusTraverser> traverser (createFocusTraverser());

				if (traverser != 0)
				{
					Component* const defaultComp = traverser->getDefaultComponent (this);
					traverser = 0;

					if (defaultComp != 0)
					{
						defaultComp->grabFocusInternal (cause, false);
						return;
					}
				}

				if (canTryParent && parentComponent_ != 0)
				{
					// if no children want it and we're allowed to try our parent comp,
					// then pass up to parent, which will try our siblings.
					parentComponent_->grabFocusInternal (cause, true);
				}
			}
		}
	}
}

void Component::grabKeyboardFocus()
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	grabFocusInternal (focusChangedDirectly);
}

void Component::moveKeyboardFocusToSibling (const bool moveToNext)
{
	// if component methods are being called from threads other than the message
	// thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
	checkMessageManagerIsLocked

	if (parentComponent_ != 0)
	{
		ScopedPointer <KeyboardFocusTraverser> traverser (createFocusTraverser());

		if (traverser != 0)
		{
			Component* const nextComp = moveToNext ? traverser->getNextComponent (this)
												   : traverser->getPreviousComponent (this);
			traverser = 0;

			if (nextComp != 0)
			{
				if (nextComp->isCurrentlyBlockedByAnotherModalComponent())
				{
					SafePointer<Component> nextCompPointer (nextComp);
					internalModalInputAttempt();

					if (nextCompPointer == 0 || nextComp->isCurrentlyBlockedByAnotherModalComponent())
						return;
				}

				nextComp->grabFocusInternal (focusChangedByTabKey);
				return;
			}
		}

		parentComponent_->moveKeyboardFocusToSibling (moveToNext);
	}
}

bool Component::hasKeyboardFocus (const bool trueIfChildIsFocused) const
{
	return (currentlyFocusedComponent == this)
			|| (trueIfChildIsFocused && isParentOf (currentlyFocusedComponent));
}

Component* JUCE_CALLTYPE Component::getCurrentlyFocusedComponent() throw()
{
	return currentlyFocusedComponent;
}

void Component::giveAwayFocus()
{
	// use a copy so we can clear the value before the call
	Component* const componentLosingFocus = currentlyFocusedComponent;
	currentlyFocusedComponent = 0;
	Desktop::getInstance().triggerFocusCallback();

	if (componentLosingFocus->isValidComponent())
		componentLosingFocus->internalFocusLoss (focusChangedDirectly);
}

bool Component::isMouseOver() const throw()
{
	return flags.mouseOverFlag;
}

bool Component::isMouseButtonDown() const throw()
{
	return flags.draggingFlag;
}

bool Component::isMouseOverOrDragging() const throw()
{
	return flags.mouseOverFlag || flags.draggingFlag;
}

bool JUCE_CALLTYPE Component::isMouseButtonDownAnywhere() throw()
{
	return ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown();
}

const Point<int> Component::getMouseXYRelative() const
{
	return globalPositionToRelative (Desktop::getMousePosition());
}

const Rectangle<int> Component::getParentMonitorArea() const
{
	return Desktop::getInstance()
			.getMonitorAreaContaining (relativePositionToGlobal (Point<int> (getWidth() / 2,
																			 getHeight() / 2)));
}

void Component::addKeyListener (KeyListener* const newListener)
{
	if (keyListeners_ == 0)
		keyListeners_ = new VoidArray();

	keyListeners_->addIfNotAlreadyThere (newListener);
}

void Component::removeKeyListener (KeyListener* const listenerToRemove)
{
	if (keyListeners_ != 0)
		keyListeners_->removeValue (listenerToRemove);
}

bool Component::keyPressed (const KeyPress&)
{
	return false;
}

bool Component::keyStateChanged (const bool /*isKeyDown*/)
{
	return false;
}

void Component::modifierKeysChanged (const ModifierKeys& modifiers)
{
	if (parentComponent_ != 0)
		parentComponent_->modifierKeysChanged (modifiers);
}

void Component::internalModifierKeysChanged()
{
	sendFakeMouseMove();

	modifierKeysChanged (ModifierKeys::getCurrentModifiers());
}

ComponentPeer* Component::getPeer() const
{
	if (flags.hasHeavyweightPeerFlag)
		return ComponentPeer::getPeerFor (this);
	else if (parentComponent_ != 0)
		return parentComponent_->getPeer();
	else
		return 0;
}

Component::BailOutChecker::BailOutChecker (Component* const component1, Component* const component2_)
	: safePointer1 (component1), safePointer2 (component2_), component2 (component2_)
{
	jassert (component1 != 0);
}

bool Component::BailOutChecker::shouldBailOut() const throw()
{
	return safePointer1 == 0 || safePointer2.getComponent() != component2;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Component.cpp ***/



/*** Start of inlined file: juce_ComponentListener.cpp ***/
BEGIN_JUCE_NAMESPACE

void ComponentListener::componentMovedOrResized (Component&, bool, bool)	{}
void ComponentListener::componentBroughtToFront (Component&)		{}
void ComponentListener::componentVisibilityChanged (Component&)		 {}
void ComponentListener::componentChildrenChanged (Component&)		   {}
void ComponentListener::componentParentHierarchyChanged (Component&)	{}
void ComponentListener::componentNameChanged (Component&)		   {}
void ComponentListener::componentBeingDeleted (Component&)		  {}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ComponentListener.cpp ***/


/*** Start of inlined file: juce_Desktop.cpp ***/
BEGIN_JUCE_NAMESPACE

Desktop::Desktop()
	: mouseClickCounter (0),
	  kioskModeComponent (0)
{
	createMouseInputSources();
	refreshMonitorSizes();
}

Desktop::~Desktop()
{
	jassert (instance == this);
	instance = 0;

	// doh! If you don't delete all your windows before exiting, you're going to
	// be leaking memory!
	jassert (desktopComponents.size() == 0);
}

Desktop& JUCE_CALLTYPE Desktop::getInstance()
{
	if (instance == 0)
		instance = new Desktop();

	return *instance;
}

Desktop* Desktop::instance = 0;

extern void juce_updateMultiMonitorInfo (Array <Rectangle<int> >& monitorCoords,
										 const bool clipToWorkArea);

void Desktop::refreshMonitorSizes()
{
	const Array <Rectangle<int> > oldClipped (monitorCoordsClipped);
	const Array <Rectangle<int> > oldUnclipped (monitorCoordsUnclipped);

	monitorCoordsClipped.clear();
	monitorCoordsUnclipped.clear();
	juce_updateMultiMonitorInfo (monitorCoordsClipped, true);
	juce_updateMultiMonitorInfo (monitorCoordsUnclipped, false);
	jassert (monitorCoordsClipped.size() > 0
			  && monitorCoordsClipped.size() == monitorCoordsUnclipped.size());

	if (oldClipped != monitorCoordsClipped
		 || oldUnclipped != monitorCoordsUnclipped)
	{
		for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
		{
			ComponentPeer* const p = ComponentPeer::getPeer (i);
			if (p != 0)
				p->handleScreenSizeChange();
		}
	}
}

int Desktop::getNumDisplayMonitors() const throw()
{
	return monitorCoordsClipped.size();
}

const Rectangle<int> Desktop::getDisplayMonitorCoordinates (const int index, const bool clippedToWorkArea) const throw()
{
	return clippedToWorkArea ? monitorCoordsClipped [index]
							 : monitorCoordsUnclipped [index];
}

const RectangleList Desktop::getAllMonitorDisplayAreas (const bool clippedToWorkArea) const throw()
{
	RectangleList rl;

	for (int i = 0; i < getNumDisplayMonitors(); ++i)
		rl.addWithoutMerging (getDisplayMonitorCoordinates (i, clippedToWorkArea));

	return rl;
}

const Rectangle<int> Desktop::getMainMonitorArea (const bool clippedToWorkArea) const throw()
{
	return getDisplayMonitorCoordinates (0, clippedToWorkArea);
}

const Rectangle<int> Desktop::getMonitorAreaContaining (const Point<int>& position, const bool clippedToWorkArea) const
{
	Rectangle<int> best (getMainMonitorArea (clippedToWorkArea));
	double bestDistance = 1.0e10;

	for (int i = getNumDisplayMonitors(); --i >= 0;)
	{
		const Rectangle<int> rect (getDisplayMonitorCoordinates (i, clippedToWorkArea));

		if (rect.contains (position))
			return rect;

		const double distance = rect.getCentre().getDistanceFrom (position);

		if (distance < bestDistance)
		{
			bestDistance = distance;
			best = rect;
		}
	}

	return best;
}

int Desktop::getNumComponents() const throw()
{
	return desktopComponents.size();
}

Component* Desktop::getComponent (const int index) const throw()
{
	return desktopComponents [index];
}

Component* Desktop::findComponentAt (const Point<int>& screenPosition) const
{
	for (int i = desktopComponents.size(); --i >= 0;)
	{
		Component* const c = desktopComponents.getUnchecked(i);
		const Point<int> relative (c->globalPositionToRelative (screenPosition));

		if (c->contains (relative.getX(), relative.getY()))
			return c->getComponentAt (relative.getX(), relative.getY());
	}

	return 0;
}

void Desktop::addDesktopComponent (Component* const c)
{
	jassert (c != 0);
	jassert (! desktopComponents.contains (c));
	desktopComponents.addIfNotAlreadyThere (c);
}

void Desktop::removeDesktopComponent (Component* const c)
{
	desktopComponents.removeValue (c);
}

void Desktop::componentBroughtToFront (Component* const c)
{
	const int index = desktopComponents.indexOf (c);
	jassert (index >= 0);

	if (index >= 0)
	{
		int newIndex = -1;

		if (! c->isAlwaysOnTop())
		{
			newIndex = desktopComponents.size();

			while (newIndex > 0 && desktopComponents.getUnchecked (newIndex - 1)->isAlwaysOnTop())
				--newIndex;

			--newIndex;
		}

		desktopComponents.move (index, newIndex);
	}
}

const Point<int> Desktop::getLastMouseDownPosition() throw()
{
	return getInstance().getMainMouseSource().getLastMouseDownPosition();
}

int Desktop::getMouseButtonClickCounter() throw()
{
	return getInstance().mouseClickCounter;
}

void Desktop::incrementMouseClickCounter() throw()
{
	++mouseClickCounter;
}

int Desktop::getNumDraggingMouseSources() const throw()
{
	int num = 0;
	for (int i = mouseSources.size(); --i >= 0;)
		if (mouseSources.getUnchecked(i)->isDragging())
			++num;

	return num;
}

MouseInputSource* Desktop::getDraggingMouseSource (int index) const throw()
{
	int num = 0;
	for (int i = mouseSources.size(); --i >= 0;)
	{
		MouseInputSource* const mi = mouseSources.getUnchecked(i);

		if (mi->isDragging())
		{
			if (index == num)
				return mi;

			++num;
		}
	}

	return 0;
}

void Desktop::addFocusChangeListener (FocusChangeListener* const listener)
{
	focusListeners.add (listener);
}

void Desktop::removeFocusChangeListener (FocusChangeListener* const listener)
{
	focusListeners.remove (listener);
}

void Desktop::triggerFocusCallback()
{
	triggerAsyncUpdate();
}

void Desktop::handleAsyncUpdate()
{
	Component* currentFocus = Component::getCurrentlyFocusedComponent();
	focusListeners.call (&FocusChangeListener::globalFocusChanged, currentFocus);
}

void Desktop::addGlobalMouseListener (MouseListener* const listener)
{
	mouseListeners.add (listener);
	resetTimer();
}

void Desktop::removeGlobalMouseListener (MouseListener* const listener)
{
	mouseListeners.remove (listener);
	resetTimer();
}

void Desktop::timerCallback()
{
	if (lastFakeMouseMove != getMousePosition())
		sendMouseMove();
}

void Desktop::sendMouseMove()
{
	if (! mouseListeners.isEmpty())
	{
		startTimer (20);

		lastFakeMouseMove = getMousePosition();

		Component* const target = findComponentAt (lastFakeMouseMove);

		if (target != 0)
		{
			Component::BailOutChecker checker (target);
			const Point<int> pos (target->globalPositionToRelative (lastFakeMouseMove));
			const Time now (Time::getCurrentTime());

			const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::getCurrentModifiers(),
								 target, now, pos, now, 0, false);

			if (me.mods.isAnyMouseButtonDown())
				mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
			else
				mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
		}
	}
}

void Desktop::resetTimer()
{
	if (mouseListeners.size() == 0)
		stopTimer();
	else
		startTimer (100);

	lastFakeMouseMove = getMousePosition();
}

extern void juce_setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool allowMenusAndBars);

void Desktop::setKioskModeComponent (Component* componentToUse, const bool allowMenusAndBars)
{
	if (kioskModeComponent != componentToUse)
	{
		// agh! Don't delete a component without first stopping it being the kiosk comp
		jassert (kioskModeComponent == 0 || kioskModeComponent->isValidComponent());
		// agh! Don't remove a component from the desktop if it's the kiosk comp!
		jassert (kioskModeComponent == 0 || kioskModeComponent->isOnDesktop());

		if (kioskModeComponent->isValidComponent())
		{
			juce_setKioskComponent (kioskModeComponent, false, allowMenusAndBars);

			kioskModeComponent->setBounds (kioskComponentOriginalBounds);
		}

		kioskModeComponent = componentToUse;

		if (kioskModeComponent != 0)
		{
			jassert (kioskModeComponent->isValidComponent());

			// Only components that are already on the desktop can be put into kiosk mode!
			jassert (kioskModeComponent->isOnDesktop());

			kioskComponentOriginalBounds = kioskModeComponent->getBounds();

			juce_setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Desktop.cpp ***/


/*** Start of inlined file: juce_ArrowButton.cpp ***/
BEGIN_JUCE_NAMESPACE

ArrowButton::ArrowButton (const String& name,
						  float arrowDirectionInRadians,
						  const Colour& arrowColour)
   : Button (name),
	 colour (arrowColour)
{
	path.lineTo (0.0f, 1.0f);
	path.lineTo (1.0f, 0.5f);
	path.closeSubPath();

	path.applyTransform (AffineTransform::rotation (float_Pi * 2.0f * arrowDirectionInRadians,
													0.5f, 0.5f));

	setComponentEffect (&shadow);
	buttonStateChanged();
}

ArrowButton::~ArrowButton()
{
}

void ArrowButton::paintButton (Graphics& g,
							   bool /*isMouseOverButton*/,
							   bool /*isButtonDown*/)
{
	g.setColour (colour);

	g.fillPath (path, path.getTransformToScaleToFit ((float) offset,
													 (float) offset,
													 (float) (getWidth() - 3),
													 (float) (getHeight() - 3),
													 false));
}

void ArrowButton::buttonStateChanged()
{
	offset = (isDown()) ? 1 : 0;

	shadow.setShadowProperties ((isDown()) ? 1.2f : 3.0f,
								0.3f, -1, 0);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ArrowButton.cpp ***/


/*** Start of inlined file: juce_Button.cpp ***/
BEGIN_JUCE_NAMESPACE

class Button::RepeatTimer  : public Timer
{
public:
	RepeatTimer (Button& owner_) : owner (owner_)   {}
	void timerCallback()	{ owner.repeatTimerCallback(); }

	juce_UseDebuggingNewOperator

private:
	Button& owner;

	RepeatTimer (const RepeatTimer&);
	RepeatTimer& operator= (const RepeatTimer&);
};

Button::Button (const String& name)
  : Component (name),
	text (name),
	buttonPressTime (0),
	lastTimeCallbackTime (0),
	commandManagerToUse (0),
	autoRepeatDelay (-1),
	autoRepeatSpeed (0),
	autoRepeatMinimumDelay (-1),
	radioGroupId (0),
	commandID (0),
	connectedEdgeFlags (0),
	buttonState (buttonNormal),
	lastToggleState (false),
	clickTogglesState (false),
	needsToRelease (false),
	needsRepainting (false),
	isKeyDown (false),
	triggerOnMouseDown (false),
	generateTooltip (false)
{
	setWantsKeyboardFocus (true);
	isOn.addListener (this);
}

Button::~Button()
{
	isOn.removeListener (this);

	if (commandManagerToUse != 0)
		commandManagerToUse->removeListener (this);

	repeatTimer = 0;
	clearShortcuts();
}

void Button::setButtonText (const String& newText)
{
	if (text != newText)
	{
		text = newText;
		repaint();
	}
}

void Button::setTooltip (const String& newTooltip)
{
	SettableTooltipClient::setTooltip (newTooltip);
	generateTooltip = false;
}

const String Button::getTooltip()
{
	if (generateTooltip && commandManagerToUse != 0 && commandID != 0)
	{
		String tt (commandManagerToUse->getDescriptionOfCommand (commandID));

		Array <KeyPress> keyPresses (commandManagerToUse->getKeyMappings()->getKeyPressesAssignedToCommand (commandID));

		for (int i = 0; i < keyPresses.size(); ++i)
		{
			const String key (keyPresses.getReference(i).getTextDescription());

			tt << " [";

			if (key.length() == 1)
				tt << TRANS("shortcut") << ": '" << key << "']";
			else
				tt << key << ']';
		}

		return tt;
	}

	return SettableTooltipClient::getTooltip();
}

void Button::setConnectedEdges (const int connectedEdgeFlags_)
{
	if (connectedEdgeFlags != connectedEdgeFlags_)
	{
		connectedEdgeFlags = connectedEdgeFlags_;
		repaint();
	}
}

void Button::setToggleState (const bool shouldBeOn,
							 const bool sendChangeNotification)
{
	if (shouldBeOn != lastToggleState)
	{
		isOn = shouldBeOn;
		lastToggleState = shouldBeOn;
		repaint();

		if (sendChangeNotification)
		{
			Component::SafePointer<Component> deletionWatcher (this);
			sendClickMessage (ModifierKeys());

			if (deletionWatcher == 0)
				return;
		}

		if (lastToggleState)
			turnOffOtherButtonsInGroup (sendChangeNotification);
	}
}

void Button::setClickingTogglesState (const bool shouldToggle) throw()
{
	clickTogglesState = shouldToggle;

	// if you've got clickTogglesState turned on, you shouldn't also connect the button
	// up to be a command invoker. Instead, your command handler must flip the state of whatever
	// it is that this button represents, and the button will update its state to reflect this
	// in the applicationCommandListChanged() method.
	jassert (commandManagerToUse == 0 || ! clickTogglesState);
}

bool Button::getClickingTogglesState() const throw()
{
	return clickTogglesState;
}

void Button::valueChanged (Value& value)
{
	if (value.refersToSameSourceAs (isOn))
		setToggleState (isOn.getValue(), true);
}

void Button::setRadioGroupId (const int newGroupId)
{
	if (radioGroupId != newGroupId)
	{
		radioGroupId = newGroupId;

		if (lastToggleState)
			turnOffOtherButtonsInGroup (true);
	}
}

void Button::turnOffOtherButtonsInGroup (const bool sendChangeNotification)
{
	Component* const p = getParentComponent();

	if (p != 0 && radioGroupId != 0)
	{
		Component::SafePointer<Component> deletionWatcher (this);

		for (int i = p->getNumChildComponents(); --i >= 0;)
		{
			Component* const c = p->getChildComponent (i);

			if (c != this)
			{
				Button* const b = dynamic_cast <Button*> (c);

				if (b != 0 && b->getRadioGroupId() == radioGroupId)
				{
					b->setToggleState (false, sendChangeNotification);

					if (deletionWatcher == 0)
						return;
				}
			}
		}
	}
}

void Button::enablementChanged()
{
	updateState (0);
	repaint();
}

Button::ButtonState Button::updateState (const MouseEvent* const e)
{
	ButtonState state = buttonNormal;

	if (isEnabled() && isVisible() && ! isCurrentlyBlockedByAnotherModalComponent())
	{
		Point<int> mousePos;

		if (e == 0)
			mousePos = getMouseXYRelative();
		else
			mousePos = e->getEventRelativeTo (this).getPosition();

		const bool over = reallyContains (mousePos.getX(), mousePos.getY(), true);
		const bool down = isMouseButtonDown();

		if ((down && (over || (triggerOnMouseDown && buttonState == buttonDown))) || isKeyDown)
			state = buttonDown;
		else if (over)
			state = buttonOver;
	}

	setState (state);
	return state;
}

void Button::setState (const ButtonState newState)
{
	if (buttonState != newState)
	{
		buttonState = newState;
		repaint();

		if (buttonState == buttonDown)
		{
			buttonPressTime = Time::getApproximateMillisecondCounter();
			lastTimeCallbackTime = buttonPressTime;
		}

		sendStateMessage();
	}
}

bool Button::isDown() const throw()
{
	return buttonState == buttonDown;
}

bool Button::isOver() const throw()
{
	return buttonState != buttonNormal;
}

void Button::buttonStateChanged()
{
}

uint32 Button::getMillisecondsSinceButtonDown() const throw()
{
	const uint32 now = Time::getApproximateMillisecondCounter();
	return now > buttonPressTime ? now - buttonPressTime : 0;
}

void Button::setTriggeredOnMouseDown (const bool isTriggeredOnMouseDown) throw()
{
	triggerOnMouseDown = isTriggeredOnMouseDown;
}

void Button::clicked()
{
}

void Button::clicked (const ModifierKeys& /*modifiers*/)
{
	clicked();
}

static const int clickMessageId = 0x2f3f4f99;

void Button::triggerClick()
{
	postCommandMessage (clickMessageId);
}

void Button::internalClickCallback (const ModifierKeys& modifiers)
{
	if (clickTogglesState)
		setToggleState ((radioGroupId != 0) || ! lastToggleState, false);

	sendClickMessage (modifiers);
}

void Button::flashButtonState()
{
	if (isEnabled())
	{
		needsToRelease = true;
		setState (buttonDown);
		getRepeatTimer().startTimer (100);
	}
}

void Button::handleCommandMessage (int commandId)
{
	if (commandId == clickMessageId)
	{
		if (isEnabled())
		{
			flashButtonState();
			internalClickCallback (ModifierKeys::getCurrentModifiers());
		}
	}
	else
	{
		Component::handleCommandMessage (commandId);
	}
}

void Button::addButtonListener (ButtonListener* const newListener)
{
	buttonListeners.add (newListener);
}

void Button::removeButtonListener (ButtonListener* const listener)
{
	buttonListeners.remove (listener);
}

void Button::sendClickMessage (const ModifierKeys& modifiers)
{
	Component::BailOutChecker checker (this);

	if (commandManagerToUse != 0 && commandID != 0)
	{
		ApplicationCommandTarget::InvocationInfo info (commandID);
		info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromButton;
		info.originatingComponent = this;

		commandManagerToUse->invoke (info, true);
	}

	clicked (modifiers);

	if (! checker.shouldBailOut())
		buttonListeners.callChecked (checker, &ButtonListener::buttonClicked, this);
}

void Button::sendStateMessage()
{
	Component::BailOutChecker checker (this);

	buttonStateChanged();

	if (! checker.shouldBailOut())
		buttonListeners.callChecked (checker, &ButtonListener::buttonStateChanged, this);
}

void Button::paint (Graphics& g)
{
	if (needsToRelease && isEnabled())
	{
		needsToRelease = false;
		needsRepainting = true;
	}

	paintButton (g, isOver(), isDown());
}

void Button::mouseEnter (const MouseEvent& e)
{
	updateState (&e);
}

void Button::mouseExit (const MouseEvent& e)
{
	updateState (&e);
}

void Button::mouseDown (const MouseEvent& e)
{
	updateState (&e);

	if (isDown())
	{
		if (autoRepeatDelay >= 0)
			getRepeatTimer().startTimer (autoRepeatDelay);

		if (triggerOnMouseDown)
			internalClickCallback (e.mods);
	}
}

void Button::mouseUp (const MouseEvent& e)
{
	const bool wasDown = isDown();
	updateState (&e);

	if (wasDown && isOver() && ! triggerOnMouseDown)
		internalClickCallback (e.mods);
}

void Button::mouseDrag (const MouseEvent& e)
{
	const ButtonState oldState = buttonState;
	updateState (&e);

	if (autoRepeatDelay >= 0 && buttonState != oldState && isDown())
		getRepeatTimer().startTimer (autoRepeatSpeed);
}

void Button::focusGained (FocusChangeType)
{
	updateState (0);
	repaint();
}

void Button::focusLost (FocusChangeType)
{
	updateState (0);
	repaint();
}

void Button::setVisible (bool shouldBeVisible)
{
	if (shouldBeVisible != isVisible())
	{
		Component::setVisible (shouldBeVisible);

		if (! shouldBeVisible)
			needsToRelease = false;

		updateState (0);
	}
	else
	{
		Component::setVisible (shouldBeVisible);
	}
}

void Button::parentHierarchyChanged()
{
	Component* const newKeySource = (shortcuts.size() == 0) ? 0 : getTopLevelComponent();

	if (newKeySource != keySource.getComponent())
	{
		if (keySource != 0)
			keySource->removeKeyListener (this);

		keySource = newKeySource;

		if (keySource != 0)
			keySource->addKeyListener (this);
	}
}

void Button::setCommandToTrigger (ApplicationCommandManager* const commandManagerToUse_,
								  const int commandID_,
								  const bool generateTooltip_)
{
	commandID = commandID_;
	generateTooltip = generateTooltip_;

	if (commandManagerToUse != commandManagerToUse_)
	{
		if (commandManagerToUse != 0)
			commandManagerToUse->removeListener (this);

		commandManagerToUse = commandManagerToUse_;

		if (commandManagerToUse != 0)
			commandManagerToUse->addListener (this);

		// if you've got clickTogglesState turned on, you shouldn't also connect the button
		// up to be a command invoker. Instead, your command handler must flip the state of whatever
		// it is that this button represents, and the button will update its state to reflect this
		// in the applicationCommandListChanged() method.
		jassert (commandManagerToUse == 0 || ! clickTogglesState);
	}

	if (commandManagerToUse != 0)
		applicationCommandListChanged();
	else
		setEnabled (true);
}

void Button::applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo& info)
{
	if (info.commandID == commandID
		 && (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) == 0)
	{
		flashButtonState();
	}
}

void Button::applicationCommandListChanged()
{
	if (commandManagerToUse != 0)
	{
		ApplicationCommandInfo info (0);

		ApplicationCommandTarget* const target = commandManagerToUse->getTargetForCommand (commandID, info);

		setEnabled (target != 0 && (info.flags & ApplicationCommandInfo::isDisabled) == 0);

		if (target != 0)
			setToggleState ((info.flags & ApplicationCommandInfo::isTicked) != 0, false);
	}
}

void Button::addShortcut (const KeyPress& key)
{
	if (key.isValid())
	{
		jassert (! isRegisteredForShortcut (key));  // already registered!

		shortcuts.add (key);
		parentHierarchyChanged();
	}
}

void Button::clearShortcuts()
{
	shortcuts.clear();

	parentHierarchyChanged();
}

bool Button::isShortcutPressed() const
{
	if (! isCurrentlyBlockedByAnotherModalComponent())
	{
		for (int i = shortcuts.size(); --i >= 0;)
			if (shortcuts.getReference(i).isCurrentlyDown())
				return true;
	}

	return false;
}

bool Button::isRegisteredForShortcut (const KeyPress& key) const
{
	for (int i = shortcuts.size(); --i >= 0;)
		if (key == shortcuts.getReference(i))
			return true;

	return false;
}

bool Button::keyStateChanged (const bool, Component*)
{
	if (! isEnabled())
		return false;

	const bool wasDown = isKeyDown;
	isKeyDown = isShortcutPressed();

	if (autoRepeatDelay >= 0 && (isKeyDown && ! wasDown))
		getRepeatTimer().startTimer (autoRepeatDelay);

	updateState (0);

	if (isEnabled() && wasDown && ! isKeyDown)
	{
		internalClickCallback (ModifierKeys::getCurrentModifiers());

		// (return immediately - this button may now have been deleted)
		return true;
	}

	return wasDown || isKeyDown;
}

bool Button::keyPressed (const KeyPress&, Component*)
{
	// returning true will avoid forwarding events for keys that we're using as shortcuts
	return isShortcutPressed();
}

bool Button::keyPressed (const KeyPress& key)
{
	if (isEnabled() && key.isKeyCode (KeyPress::returnKey))
	{
		triggerClick();
		return true;
	}

	return false;
}

void Button::setRepeatSpeed (const int initialDelayMillisecs,
							 const int repeatMillisecs,
							 const int minimumDelayInMillisecs) throw()
{
	autoRepeatDelay = initialDelayMillisecs;
	autoRepeatSpeed = repeatMillisecs;
	autoRepeatMinimumDelay = jmin (autoRepeatSpeed, minimumDelayInMillisecs);
}

void Button::repeatTimerCallback()
{
	if (needsRepainting)
	{
		getRepeatTimer().stopTimer();
		updateState (0);
		needsRepainting = false;
	}
	else if (autoRepeatSpeed > 0 && (isKeyDown || (updateState (0) == buttonDown)))
	{
		int repeatSpeed = autoRepeatSpeed;

		if (autoRepeatMinimumDelay >= 0)
		{
			double timeHeldDown = jmin (1.0, getMillisecondsSinceButtonDown() / 4000.0);
			timeHeldDown *= timeHeldDown;

			repeatSpeed = repeatSpeed + (int) (timeHeldDown * (autoRepeatMinimumDelay - repeatSpeed));
		}

		repeatSpeed = jmax (1, repeatSpeed);

		getRepeatTimer().startTimer (repeatSpeed);

		const uint32 now = Time::getApproximateMillisecondCounter();
		const int numTimesToCallback = (now > lastTimeCallbackTime) ? jmax (1, (int) (now - lastTimeCallbackTime) / repeatSpeed) : 1;

		lastTimeCallbackTime = now;

		Component::SafePointer<Component> deletionWatcher (this);

		for (int i = numTimesToCallback; --i >= 0;)
		{
			internalClickCallback (ModifierKeys::getCurrentModifiers());

			if (deletionWatcher == 0 || ! isDown())
				return;
		}
	}
	else if (! needsToRelease)
	{
		getRepeatTimer().stopTimer();
	}
}

Button::RepeatTimer& Button::getRepeatTimer()
{
	if (repeatTimer == 0)
		repeatTimer = new RepeatTimer (*this);

	return *repeatTimer;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Button.cpp ***/


/*** Start of inlined file: juce_DrawableButton.cpp ***/
BEGIN_JUCE_NAMESPACE

DrawableButton::DrawableButton (const String& name,
								const DrawableButton::ButtonStyle buttonStyle)
	: Button (name),
	  style (buttonStyle),
	  edgeIndent (3)
{
	if (buttonStyle == ImageOnButtonBackground)
	{
		backgroundOff = Colour (0xffbbbbff);
		backgroundOn = Colour (0xff3333ff);
	}
	else
	{
		backgroundOff = Colours::transparentBlack;
		backgroundOn = Colour (0xaabbbbff);
	}
}

DrawableButton::~DrawableButton()
{
	deleteImages();
}

void DrawableButton::deleteImages()
{
}

void DrawableButton::setImages (const Drawable* normal,
								const Drawable* over,
								const Drawable* down,
								const Drawable* disabled,
								const Drawable* normalOn,
								const Drawable* overOn,
								const Drawable* downOn,
								const Drawable* disabledOn)
{
	deleteImages();

	jassert (normal != 0); // you really need to give it at least a normal image..

	if (normal != 0)
		normalImage = normal->createCopy();

	if (over != 0)
		overImage = over->createCopy();

	if (down != 0)
		downImage = down->createCopy();

	if (disabled != 0)
		disabledImage = disabled->createCopy();

	if (normalOn != 0)
		normalImageOn = normalOn->createCopy();

	if (overOn != 0)
		overImageOn = overOn->createCopy();

	if (downOn != 0)
		downImageOn = downOn->createCopy();

	if (disabledOn != 0)
		disabledImageOn = disabledOn->createCopy();

	repaint();
}

void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle)
{
	if (style != newStyle)
	{
		style = newStyle;
		repaint();
	}
}

void DrawableButton::setBackgroundColours (const Colour& toggledOffColour,
										   const Colour& toggledOnColour)
{
	if (backgroundOff != toggledOffColour
		 || backgroundOn != toggledOnColour)
	{
		backgroundOff = toggledOffColour;
		backgroundOn = toggledOnColour;

		repaint();
	}
}

const Colour& DrawableButton::getBackgroundColour() const throw()
{
	return getToggleState() ? backgroundOn
							: backgroundOff;
}

void DrawableButton::setEdgeIndent (const int numPixelsIndent)
{
	edgeIndent = numPixelsIndent;
	repaint();
}

void DrawableButton::paintButton (Graphics& g,
								  bool isMouseOverButton,
								  bool isButtonDown)
{
	Rectangle<int> imageSpace;

	if (style == ImageOnButtonBackground)
	{
		const int insetX = getWidth() / 4;
		const int insetY = getHeight() / 4;

		imageSpace.setBounds (insetX, insetY, getWidth() - insetX * 2, getHeight() - insetY * 2);

		getLookAndFeel().drawButtonBackground (g, *this,
											   getBackgroundColour(),
											   isMouseOverButton,
											   isButtonDown);
	}
	else
	{
		g.fillAll (getBackgroundColour());

		const int textH = (style == ImageAboveTextLabel)
							? jmin (16, proportionOfHeight (0.25f))
							: 0;

		const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f));
		const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f));

		imageSpace.setBounds (indentX, indentY,
							  getWidth() - indentX * 2,
							  getHeight() - indentY * 2 - textH);

		if (textH > 0)
		{
			g.setFont ((float) textH);

			g.setColour (Colours::black.withAlpha (isEnabled() ? 1.0f : 0.4f));
			g.drawFittedText (getButtonText(),
							  2, getHeight() - textH - 1,
							  getWidth() - 4, textH,
							  Justification::centred, 1);
		}
	}

	g.setImageResamplingQuality (Graphics::mediumResamplingQuality);
	g.setOpacity (1.0f);

	const Drawable* imageToDraw = 0;

	if (isEnabled())
	{
		imageToDraw = getCurrentImage();
	}
	else
	{
		imageToDraw = getToggleState() ? disabledImageOn
									   : disabledImage;

		if (imageToDraw == 0)
		{
			g.setOpacity (0.4f);
			imageToDraw = getNormalImage();
		}
	}

	if (imageToDraw != 0)
	{
		if (style == ImageRaw)
		{
			imageToDraw->draw (g, 1.0f);
		}
		else
		{
			imageToDraw->drawWithin (g,
									 imageSpace.getX(),
									 imageSpace.getY(),
									 imageSpace.getWidth(),
									 imageSpace.getHeight(),
									 RectanglePlacement::centred,
									 1.0f);
		}
	}
}

const Drawable* DrawableButton::getCurrentImage() const throw()
{
	if (isDown())
		return getDownImage();

	if (isOver())
		return getOverImage();

	return getNormalImage();
}

const Drawable* DrawableButton::getNormalImage() const throw()
{
	return (getToggleState() && normalImageOn != 0) ? normalImageOn
													: normalImage;
}

const Drawable* DrawableButton::getOverImage() const throw()
{
	const Drawable* d = normalImage;

	if (getToggleState())
	{
		if (overImageOn != 0)
			d = overImageOn;
		else if (normalImageOn != 0)
			d = normalImageOn;
		else if (overImage != 0)
			d = overImage;
	}
	else
	{
		if (overImage != 0)
			d = overImage;
	}

	return d;
}

const Drawable* DrawableButton::getDownImage() const throw()
{
	const Drawable* d = normalImage;

	if (getToggleState())
	{
		if (downImageOn != 0)
			d = downImageOn;
		else if (overImageOn != 0)
			d = overImageOn;
		else if (normalImageOn != 0)
			d = normalImageOn;
		else if (downImage != 0)
			d = downImage;
		else
			d = getOverImage();
	}
	else
	{
		if (downImage != 0)
			d = downImage;
		else
			d = getOverImage();
	}

	return d;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DrawableButton.cpp ***/


/*** Start of inlined file: juce_HyperlinkButton.cpp ***/
BEGIN_JUCE_NAMESPACE

HyperlinkButton::HyperlinkButton (const String& linkText,
								  const URL& linkURL)
   : Button (linkText),
	 url (linkURL),
	 font (14.0f, Font::underlined),
	 resizeFont (true),
	 justification (Justification::centred)
{
	setMouseCursor (MouseCursor::PointingHandCursor);
	setTooltip (linkURL.toString (false));
}

HyperlinkButton::~HyperlinkButton()
{
}

void HyperlinkButton::setFont (const Font& newFont,
							   const bool resizeToMatchComponentHeight,
							   const Justification& justificationType)
{
	font = newFont;
	resizeFont = resizeToMatchComponentHeight;
	justification = justificationType;
	repaint();
}

void HyperlinkButton::setURL (const URL& newURL) throw()
{
	url = newURL;
	setTooltip (newURL.toString (false));
}

const Font HyperlinkButton::getFontToUse() const
{
	Font f (font);

	if (resizeFont)
		f.setHeight (getHeight() * 0.7f);

	return f;
}

void HyperlinkButton::changeWidthToFitText()
{
	setSize (getFontToUse().getStringWidth (getName()) + 6, getHeight());
}

void HyperlinkButton::colourChanged()
{
	repaint();
}

void HyperlinkButton::clicked()
{
	if (url.isWellFormed())
		url.launchInDefaultBrowser();
}

void HyperlinkButton::paintButton (Graphics& g,
								   bool isMouseOverButton,
								   bool isButtonDown)
{
	const Colour textColour (findColour (textColourId));

	if (isEnabled())
		g.setColour ((isMouseOverButton) ? textColour.darker ((isButtonDown) ? 1.3f : 0.4f)
										 : textColour);
	else
		g.setColour (textColour.withMultipliedAlpha (0.4f));

	g.setFont (getFontToUse());

	g.drawText (getButtonText(),
				2, 0, getWidth() - 2, getHeight(),
				justification.getOnlyHorizontalFlags() | Justification::verticallyCentred,
				true);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_HyperlinkButton.cpp ***/


/*** Start of inlined file: juce_ImageButton.cpp ***/
BEGIN_JUCE_NAMESPACE

ImageButton::ImageButton (const String& text_)
	: Button (text_),
	  scaleImageToFit (true),
	  preserveProportions (true),
	  alphaThreshold (0),
	  imageX (0),
	  imageY (0),
	  imageW (0),
	  imageH (0),
	  normalImage (0),
	  overImage (0),
	  downImage (0)
{
}

ImageButton::~ImageButton()
{
	deleteImages();
}

void ImageButton::deleteImages()
{
	ImageCache::releaseOrDelete (normalImage);
	ImageCache::releaseOrDelete (overImage);
	ImageCache::releaseOrDelete (downImage);
}

void ImageButton::setImages (const bool resizeButtonNowToFitThisImage,
							 const bool rescaleImagesWhenButtonSizeChanges,
							 const bool preserveImageProportions,
							 Image* const normalImage_,
							 const float imageOpacityWhenNormal,
							 const Colour& overlayColourWhenNormal,
							 Image* const overImage_,
							 const float imageOpacityWhenOver,
							 const Colour& overlayColourWhenOver,
							 Image* const downImage_,
							 const float imageOpacityWhenDown,
							 const Colour& overlayColourWhenDown,
							 const float hitTestAlphaThreshold)
{
	deleteImages();

	normalImage = normalImage_;
	overImage = overImage_;
	downImage = downImage_;

	if (resizeButtonNowToFitThisImage && normalImage != 0)
	{
		imageW = normalImage->getWidth();
		imageH = normalImage->getHeight();

		setSize (imageW, imageH);
	}

	scaleImageToFit = rescaleImagesWhenButtonSizeChanges;
	preserveProportions = preserveImageProportions;

	normalOpacity = imageOpacityWhenNormal;
	normalOverlay = overlayColourWhenNormal;
	overOpacity   = imageOpacityWhenOver;
	overOverlay   = overlayColourWhenOver;
	downOpacity   = imageOpacityWhenDown;
	downOverlay   = overlayColourWhenDown;

	alphaThreshold = (unsigned char) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));

	repaint();
}

Image* ImageButton::getCurrentImage() const
{
	if (isDown() || getToggleState())
		return getDownImage();

	if (isOver())
		return getOverImage();

	return getNormalImage();
}

Image* ImageButton::getNormalImage() const throw()
{
	return normalImage;
}

Image* ImageButton::getOverImage() const throw()
{
	return (overImage != 0) ? overImage
							: normalImage;
}

Image* ImageButton::getDownImage() const throw()
{
	return (downImage != 0) ? downImage
							: getOverImage();
}

void ImageButton::paintButton (Graphics& g,
							   bool isMouseOverButton,
							   bool isButtonDown)
{
	if (! isEnabled())
	{
		isMouseOverButton = false;
		isButtonDown = false;
	}

	Image* const im = getCurrentImage();

	if (im != 0)
	{
		const int iw = im->getWidth();
		const int ih = im->getHeight();
		imageW = getWidth();
		imageH = getHeight();
		imageX = (imageW - iw) >> 1;
		imageY = (imageH - ih) >> 1;

		if (scaleImageToFit)
		{
			if (preserveProportions)
			{
				int newW, newH;
				const float imRatio = ih / (float)iw;
				const float destRatio = imageH / (float)imageW;

				if (imRatio > destRatio)
				{
					newW = roundToInt (imageH / imRatio);
					newH = imageH;
				}
				else
				{
					newW = imageW;
					newH = roundToInt (imageW * imRatio);
				}

				imageX = (imageW - newW) / 2;
				imageY = (imageH - newH) / 2;
				imageW = newW;
				imageH = newH;
			}
			else
			{
				imageX = 0;
				imageY = 0;
			}
		}

		if (! scaleImageToFit)
		{
			imageW = iw;
			imageH = ih;
		}

		getLookAndFeel().drawImageButton (g, im, imageX, imageY, imageW, imageH,
										  isButtonDown ? downOverlay
													   : (isMouseOverButton ? overOverlay
																			: normalOverlay),
										  isButtonDown ? downOpacity
													   : (isMouseOverButton ? overOpacity
																			: normalOpacity),
										  *this);
	}
}

bool ImageButton::hitTest (int x, int y)
{
	if (alphaThreshold == 0)
		return true;

	Image* const im = getCurrentImage();

	return im == 0
			 || (imageW > 0 && imageH > 0
				  && alphaThreshold < im->getPixelAt (((x - imageX) * im->getWidth()) / imageW,
													  ((y - imageY) * im->getHeight()) / imageH).getAlpha());
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ImageButton.cpp ***/


/*** Start of inlined file: juce_ShapeButton.cpp ***/
BEGIN_JUCE_NAMESPACE

ShapeButton::ShapeButton (const String& text_,
						  const Colour& normalColour_,
						  const Colour& overColour_,
						  const Colour& downColour_)
  : Button (text_),
	normalColour (normalColour_),
	overColour (overColour_),
	downColour (downColour_),
	maintainShapeProportions (false),
	outlineWidth (0.0f)
{
}

ShapeButton::~ShapeButton()
{
}

void ShapeButton::setColours (const Colour& newNormalColour,
							  const Colour& newOverColour,
							  const Colour& newDownColour)
{
	normalColour = newNormalColour;
	overColour = newOverColour;
	downColour = newDownColour;
}

void ShapeButton::setOutline (const Colour& newOutlineColour,
							  const float newOutlineWidth)
{
	outlineColour = newOutlineColour;
	outlineWidth = newOutlineWidth;
}

void ShapeButton::setShape (const Path& newShape,
							const bool resizeNowToFitThisShape,
							const bool maintainShapeProportions_,
							const bool hasShadow)
{
	shape = newShape;
	maintainShapeProportions = maintainShapeProportions_;

	shadow.setShadowProperties (3.0f, 0.5f, 0, 0);
	setComponentEffect ((hasShadow) ? &shadow : 0);

	if (resizeNowToFitThisShape)
	{
		Rectangle<float> bounds (shape.getBounds());

		if (hasShadow)
			bounds.expand (4.0f, 4.0f);

		shape.applyTransform (AffineTransform::translation (-bounds.getX(), -bounds.getY()));

		setSize (1 + (int) (bounds.getWidth() + outlineWidth),
				 1 + (int) (bounds.getHeight() + outlineWidth));
	}
}

void ShapeButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
{
	if (! isEnabled())
	{
		isMouseOverButton = false;
		isButtonDown = false;
	}

	g.setColour ((isButtonDown) ? downColour
								: (isMouseOverButton) ? overColour
													  : normalColour);

	int w = getWidth();
	int h = getHeight();

	if (getComponentEffect() != 0)
	{
		w -= 4;
		h -= 4;
	}

	const float offset = (outlineWidth * 0.5f) + (isButtonDown ? 1.5f : 0.0f);

	const AffineTransform trans (shape.getTransformToScaleToFit (offset, offset,
																 w - offset - outlineWidth,
																 h - offset - outlineWidth,
																 maintainShapeProportions));
	g.fillPath (shape, trans);

	if (outlineWidth > 0.0f)
	{
		g.setColour (outlineColour);
		g.strokePath (shape, PathStrokeType (outlineWidth), trans);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ShapeButton.cpp ***/


/*** Start of inlined file: juce_TextButton.cpp ***/
BEGIN_JUCE_NAMESPACE

TextButton::TextButton (const String& name,
						const String& toolTip)
	: Button (name)
{
	setTooltip (toolTip);
}

TextButton::~TextButton()
{
}

void TextButton::paintButton (Graphics& g,
							  bool isMouseOverButton,
							  bool isButtonDown)
{
	getLookAndFeel().drawButtonBackground (g, *this,
										   findColour (getToggleState() ? buttonOnColourId
																		: buttonColourId),
										   isMouseOverButton,
										   isButtonDown);

	getLookAndFeel().drawButtonText (g, *this,
									 isMouseOverButton,
									 isButtonDown);
}

void TextButton::colourChanged()
{
	repaint();
}

const Font TextButton::getFont()
{
	return Font (jmin (15.0f, getHeight() * 0.6f));
}

void TextButton::changeWidthToFitText (const int newHeight)
{
	if (newHeight >= 0)
		setSize (jmax (1, getWidth()), newHeight);

	setSize (getFont().getStringWidth (getButtonText()) + getHeight(),
			 getHeight());
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TextButton.cpp ***/


/*** Start of inlined file: juce_ToggleButton.cpp ***/
BEGIN_JUCE_NAMESPACE

ToggleButton::ToggleButton (const String& buttonText)
	: Button (buttonText)
{
	setClickingTogglesState (true);
}

ToggleButton::~ToggleButton()
{
}

void ToggleButton::paintButton (Graphics& g,
								bool isMouseOverButton,
								bool isButtonDown)
{
	getLookAndFeel().drawToggleButton (g, *this,
									   isMouseOverButton,
									   isButtonDown);
}

void ToggleButton::changeWidthToFitText()
{
	getLookAndFeel().changeToggleButtonWidthToFitText (*this);
}

void ToggleButton::colourChanged()
{
	repaint();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ToggleButton.cpp ***/


/*** Start of inlined file: juce_ToolbarButton.cpp ***/
BEGIN_JUCE_NAMESPACE

ToolbarButton::ToolbarButton (const int itemId_,
							  const String& buttonText,
							  Drawable* const normalImage_,
							  Drawable* const toggledOnImage_)
   : ToolbarItemComponent (itemId_, buttonText, true),
	 normalImage (normalImage_),
	 toggledOnImage (toggledOnImage_)
{
}

ToolbarButton::~ToolbarButton()
{
}

bool ToolbarButton::getToolbarItemSizes (int toolbarDepth,
										 bool /*isToolbarVertical*/,
										 int& preferredSize,
										 int& minSize, int& maxSize)
{
	preferredSize = minSize = maxSize = toolbarDepth;
	return true;
}

void ToolbarButton::paintButtonArea (Graphics& g,
									 int width, int height,
									 bool /*isMouseOver*/,
									 bool /*isMouseDown*/)
{
	Drawable* d = normalImage;

	if (getToggleState() && toggledOnImage != 0)
		d = toggledOnImage;

	if (! isEnabled())
	{
		Image im (Image::ARGB, width, height, true);
		Graphics g2 (im);
		d->drawWithin (g2, 0, 0, width, height, RectanglePlacement::centred, 1.0f);
		im.desaturate();

		g.drawImageAt (&im, 0, 0);
	}
	else
	{
		d->drawWithin (g, 0, 0, width, height, RectanglePlacement::centred, 1.0f);
	}
}

void ToolbarButton::contentAreaChanged (const Rectangle<int>&)
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ToolbarButton.cpp ***/


/*** Start of inlined file: juce_CodeDocument.cpp ***/
BEGIN_JUCE_NAMESPACE

class CodeDocumentLine
{
public:
	CodeDocumentLine (const juce_wchar* const line_,
					  const int lineLength_,
					  const int numNewLineChars,
					  const int lineStartInFile_)
		: line (line_, lineLength_),
		  lineStartInFile (lineStartInFile_),
		  lineLength (lineLength_),
		  lineLengthWithoutNewLines (lineLength_ - numNewLineChars)
	{
	}

	~CodeDocumentLine()
	{
	}

	static void createLines (Array <CodeDocumentLine*>& newLines, const String& text)
	{
		const juce_wchar* const t = text;
		int pos = 0;

		while (t [pos] != 0)
		{
			const int startOfLine = pos;
			int numNewLineChars = 0;

			while (t[pos] != 0)
			{
				if (t[pos] == '\r')
				{
					++numNewLineChars;
					++pos;

					if (t[pos] == '\n')
					{
						++numNewLineChars;
						++pos;
					}

					break;
				}

				if (t[pos] == '\n')
				{
					++numNewLineChars;
					++pos;
					break;
				}

				++pos;
			}

			newLines.add (new CodeDocumentLine (t + startOfLine, pos - startOfLine,
												numNewLineChars, startOfLine));
		}

		jassert (pos == text.length());
	}

	bool endsWithLineBreak() const throw()
	{
		return lineLengthWithoutNewLines != lineLength;
	}

	void updateLength() throw()
	{
		lineLengthWithoutNewLines = lineLength = line.length();

		while (lineLengthWithoutNewLines > 0
				&& (line [lineLengthWithoutNewLines - 1] == '\n'
					|| line [lineLengthWithoutNewLines - 1] == '\r'))
		{
			--lineLengthWithoutNewLines;
		}
	}

	String line;
	int lineStartInFile, lineLength, lineLengthWithoutNewLines;
};

CodeDocument::Iterator::Iterator (CodeDocument* const document_)
	: document (document_),
	  currentLine (document_->lines[0]),
	  line (0),
	  position (0)
{
}

CodeDocument::Iterator::Iterator (const CodeDocument::Iterator& other)
	: document (other.document),
	  currentLine (other.currentLine),
	  line (other.line),
	  position (other.position)
{
}

CodeDocument::Iterator& CodeDocument::Iterator::operator= (const CodeDocument::Iterator& other) throw()
{
	document = other.document;
	currentLine = other.currentLine;
	line = other.line;
	position = other.position;

	return *this;
}

CodeDocument::Iterator::~Iterator() throw()
{
}

juce_wchar CodeDocument::Iterator::nextChar()
{
	if (currentLine == 0)
		return 0;

	jassert (currentLine == document->lines.getUnchecked (line));
	const juce_wchar result = currentLine->line [position - currentLine->lineStartInFile];

	if (++position >= currentLine->lineStartInFile + currentLine->lineLength)
	{
		++line;
		currentLine = document->lines [line];
	}

	return result;
}

void CodeDocument::Iterator::skip()
{
	if (currentLine != 0)
	{
		jassert (currentLine == document->lines.getUnchecked (line));

		if (++position >= currentLine->lineStartInFile + currentLine->lineLength)
		{
			++line;
			currentLine = document->lines [line];
		}
	}
}

void CodeDocument::Iterator::skipToEndOfLine()
{
	if (currentLine != 0)
	{
		jassert (currentLine == document->lines.getUnchecked (line));

		++line;
		currentLine = document->lines [line];

		if (currentLine != 0)
			position = currentLine->lineStartInFile;
		else
			position = document->getNumCharacters();
	}
}

juce_wchar CodeDocument::Iterator::peekNextChar() const
{
	if (currentLine == 0)
		return 0;

	jassert (currentLine == document->lines.getUnchecked (line));
	return const_cast <const String&> (currentLine->line) [position - currentLine->lineStartInFile];
}

void CodeDocument::Iterator::skipWhitespace()
{
	while (CharacterFunctions::isWhitespace (peekNextChar()))
		skip();
}

bool CodeDocument::Iterator::isEOF() const throw()
{
	return currentLine == 0;
}

CodeDocument::Position::Position() throw()
	: owner (0), characterPos (0), line (0),
	  indexInLine (0), positionMaintained (false)
{
}

CodeDocument::Position::Position (const CodeDocument* const ownerDocument,
								  const int line_, const int indexInLine_) throw()
	: owner (const_cast <CodeDocument*> (ownerDocument)),
	  characterPos (0), line (line_),
	  indexInLine (indexInLine_), positionMaintained (false)
{
	setLineAndIndex (line_, indexInLine_);
}

CodeDocument::Position::Position (const CodeDocument* const ownerDocument,
								  const int characterPos_) throw()
	: owner (const_cast <CodeDocument*> (ownerDocument)),
	  positionMaintained (false)
{
	setPosition (characterPos_);
}

CodeDocument::Position::Position (const Position& other) throw()
	: owner (other.owner), characterPos (other.characterPos), line (other.line),
	  indexInLine (other.indexInLine), positionMaintained (false)
{
	jassert (*this == other);
}

CodeDocument::Position::~Position() throw()
{
	setPositionMaintained (false);
}

CodeDocument::Position& CodeDocument::Position::operator= (const Position& other) throw()
{
	if (this != &other)
	{
		const bool wasPositionMaintained = positionMaintained;
		if (owner != other.owner)
			setPositionMaintained (false);

		owner = other.owner;
		line = other.line;
		indexInLine = other.indexInLine;
		characterPos = other.characterPos;
		setPositionMaintained (wasPositionMaintained);

		jassert (*this == other);
	}

	return *this;
}

bool CodeDocument::Position::operator== (const Position& other) const throw()
{
	jassert ((characterPos == other.characterPos)
			   == (line == other.line && indexInLine == other.indexInLine));

	return characterPos == other.characterPos
			&& line == other.line
			&& indexInLine == other.indexInLine
			&& owner == other.owner;
}

bool CodeDocument::Position::operator!= (const Position& other) const throw()
{
	return ! operator== (other);
}

void CodeDocument::Position::setLineAndIndex (const int newLine, const int newIndexInLine) throw()
{
	jassert (owner != 0);

	if (owner->lines.size() == 0)
	{
		line = 0;
		indexInLine = 0;
		characterPos = 0;
	}
	else
	{
		if (newLine >= owner->lines.size())
		{
			line = owner->lines.size() - 1;

			CodeDocumentLine* const l = owner->lines.getUnchecked (line);
			jassert (l != 0);

			indexInLine = l->lineLengthWithoutNewLines;
			characterPos = l->lineStartInFile + indexInLine;
		}
		else
		{
			line = jmax (0, newLine);

			CodeDocumentLine* const l = owner->lines.getUnchecked (line);
			jassert (l != 0);

			if (l->lineLengthWithoutNewLines > 0)
				indexInLine = jlimit (0, l->lineLengthWithoutNewLines, newIndexInLine);
			else
				indexInLine = 0;

			characterPos = l->lineStartInFile + indexInLine;
		}
	}
}

void CodeDocument::Position::setPosition (const int newPosition) throw()
{
	jassert (owner != 0);

	line = 0;
	indexInLine = 0;
	characterPos = 0;

	if (newPosition > 0)
	{
		int lineStart = 0;
		int lineEnd = owner->lines.size();

		for (;;)
		{
			if (lineEnd - lineStart < 4)
			{
				for (int i = lineStart; i < lineEnd; ++i)
				{
					CodeDocumentLine* const l = owner->lines.getUnchecked (i);

					int index = newPosition - l->lineStartInFile;

					if (index >= 0 && (index < l->lineLength || i == lineEnd - 1))
					{
						line = i;
						indexInLine = jmin (l->lineLengthWithoutNewLines, index);
						characterPos = l->lineStartInFile + indexInLine;
					}
				}

				break;
			}
			else
			{
				const int midIndex = (lineStart + lineEnd + 1) / 2;
				CodeDocumentLine* const mid = owner->lines.getUnchecked (midIndex);

				if (newPosition >= mid->lineStartInFile)
					lineStart = midIndex;
				else
					lineEnd = midIndex;
			}
		}
	}
}

void CodeDocument::Position::moveBy (int characterDelta) throw()
{
	jassert (owner != 0);

	if (characterDelta == 1)
	{
		setPosition (getPosition());

		// If moving right, make sure we don't get stuck between the \r and \n characters..
		if (line < owner->lines.size())
		{
			CodeDocumentLine* const l = owner->lines.getUnchecked (line);
			if (indexInLine + characterDelta < l->lineLength
				 && indexInLine + characterDelta >= l->lineLengthWithoutNewLines + 1)
				++characterDelta;
		}
	}

	setPosition (characterPos + characterDelta);
}

const CodeDocument::Position CodeDocument::Position::movedBy (const int characterDelta) const throw()
{
	CodeDocument::Position p (*this);
	p.moveBy (characterDelta);
	return p;
}

const CodeDocument::Position CodeDocument::Position::movedByLines (const int deltaLines) const throw()
{
	CodeDocument::Position p (*this);
	p.setLineAndIndex (getLineNumber() + deltaLines, getIndexInLine());
	return p;
}

const juce_wchar CodeDocument::Position::getCharacter() const throw()
{
	const CodeDocumentLine* const l = owner->lines [line];
	return l == 0 ? 0 : l->line [getIndexInLine()];
}

const String CodeDocument::Position::getLineText() const throw()
{
	const CodeDocumentLine* const l = owner->lines [line];
	return l == 0 ? String::empty : l->line;
}

void CodeDocument::Position::setPositionMaintained (const bool isMaintained) throw()
{
	if (isMaintained != positionMaintained)
	{
		positionMaintained = isMaintained;

		if (owner != 0)
		{
			if (isMaintained)
			{
				jassert (! owner->positionsToMaintain.contains (this));
				owner->positionsToMaintain.add (this);
			}
			else
			{
				jassert (owner->positionsToMaintain.contains (this));
				owner->positionsToMaintain.removeValue (this);
			}
		}
	}
}

CodeDocument::CodeDocument()
	: undoManager (std::numeric_limits<int>::max(), 10000),
	  currentActionIndex (0),
	  indexOfSavedState (-1),
	  maximumLineLength (-1),
	  newLineChars ("\r\n")
{
}

CodeDocument::~CodeDocument()
{
}

const String CodeDocument::getAllContent() const throw()
{
	return getTextBetween (Position (this, 0),
						   Position (this, lines.size(), 0));
}

const String CodeDocument::getTextBetween (const Position& start, const Position& end) const throw()
{
	if (end.getPosition() <= start.getPosition())
		return String::empty;

	const int startLine = start.getLineNumber();
	const int endLine = end.getLineNumber();

	if (startLine == endLine)
	{
		CodeDocumentLine* const line = lines [startLine];
		return (line == 0) ? String::empty : line->line.substring (start.getIndexInLine(), end.getIndexInLine());
	}

	String result;
	result.preallocateStorage (end.getPosition() - start.getPosition() + 4);
	String::Concatenator concatenator (result);

	const int maxLine = jmin (lines.size() - 1, endLine);

	for (int i = jmax (0, startLine); i <= maxLine; ++i)
	{
		const CodeDocumentLine* line = lines.getUnchecked(i);
		int len = line->lineLength;

		if (i == startLine)
		{
			const int index = start.getIndexInLine();
			concatenator.append (line->line.substring (index, len));
		}
		else if (i == endLine)
		{
			len = end.getIndexInLine();
			concatenator.append (line->line.substring (0, len));
		}
		else
		{
			concatenator.append (line->line);
		}
	}

	return result;
}

int CodeDocument::getNumCharacters() const throw()
{
	const CodeDocumentLine* const lastLine = lines.getLast();
	return (lastLine == 0) ? 0 : lastLine->lineStartInFile + lastLine->lineLength;
}

const String CodeDocument::getLine (const int lineIndex) const throw()
{
	const CodeDocumentLine* const line = lines [lineIndex];
	return (line == 0) ? String::empty : line->line;
}

int CodeDocument::getMaximumLineLength() throw()
{
	if (maximumLineLength < 0)
	{
		maximumLineLength = 0;

		for (int i = lines.size(); --i >= 0;)
			maximumLineLength = jmax (maximumLineLength, lines.getUnchecked(i)->lineLength);
	}

	return maximumLineLength;
}

void CodeDocument::deleteSection (const Position& startPosition, const Position& endPosition)
{
	remove (startPosition.getPosition(), endPosition.getPosition(), true);
}

void CodeDocument::insertText (const Position& position, const String& text)
{
	insert (text, position.getPosition(), true);
}

void CodeDocument::replaceAllContent (const String& newContent)
{
	remove (0, getNumCharacters(), true);
	insert (newContent, 0, true);
}

bool CodeDocument::loadFromStream (InputStream& stream)
{
	replaceAllContent (stream.readEntireStreamAsString());
	setSavePoint();
	clearUndoHistory();
	return true;
}

bool CodeDocument::writeToStream (OutputStream& stream)
{
	for (int i = 0; i < lines.size(); ++i)
	{
		String temp (lines.getUnchecked(i)->line); // use a copy to avoid bloating the memory footprint of the stored string.
		const char* utf8 = temp.toUTF8();

		if (! stream.write (utf8, (int) strlen (utf8)))
			return false;
	}

	return true;
}

void CodeDocument::setNewLineCharacters (const String& newLine) throw()
{
	jassert (newLine == "\r\n" || newLine == "\n" || newLine == "\r");
	newLineChars = newLine;
}

void CodeDocument::newTransaction()
{
	undoManager.beginNewTransaction (String::empty);
}

void CodeDocument::undo()
{
	newTransaction();
	undoManager.undo();
}

void CodeDocument::redo()
{
	undoManager.redo();
}

void CodeDocument::clearUndoHistory()
{
	undoManager.clearUndoHistory();
}

void CodeDocument::setSavePoint() throw()
{
	indexOfSavedState = currentActionIndex;
}

bool CodeDocument::hasChangedSinceSavePoint() const throw()
{
	return currentActionIndex != indexOfSavedState;
}

static int getCodeCharacterCategory (const juce_wchar character) throw()
{
	return (CharacterFunctions::isLetterOrDigit (character) || character == '_')
				? 2 : (CharacterFunctions::isWhitespace (character) ? 0 : 1);
}

const CodeDocument::Position CodeDocument::findWordBreakAfter (const Position& position) const throw()
{
	Position p (position);
	const int maxDistance = 256;
	int i = 0;

	while (i < maxDistance
			&& CharacterFunctions::isWhitespace (p.getCharacter())
			&& (i == 0 || (p.getCharacter() != '\n'
							&& p.getCharacter() != '\r')))
	{
		++i;
		p.moveBy (1);
	}

	if (i == 0)
	{
		const int type = getCodeCharacterCategory (p.getCharacter());

		while (i < maxDistance && type == getCodeCharacterCategory (p.getCharacter()))
		{
			++i;
			p.moveBy (1);
		}

		while (i < maxDistance
				&& CharacterFunctions::isWhitespace (p.getCharacter())
				&& (i == 0 || (p.getCharacter() != '\n'
								&& p.getCharacter() != '\r')))
		{
			++i;
			p.moveBy (1);
		}
	}

	return p;
}

const CodeDocument::Position CodeDocument::findWordBreakBefore (const Position& position) const throw()
{
	Position p (position);
	const int maxDistance = 256;
	int i = 0;
	bool stoppedAtLineStart = false;

	while (i < maxDistance)
	{
		const juce_wchar c = p.movedBy (-1).getCharacter();

		if (c == '\r' || c == '\n')
		{
			stoppedAtLineStart = true;

			if (i > 0)
				break;
		}

		if (! CharacterFunctions::isWhitespace (c))
			break;

		p.moveBy (-1);
		++i;
	}

	if (i < maxDistance && ! stoppedAtLineStart)
	{
		const int type = getCodeCharacterCategory (p.movedBy (-1).getCharacter());

		while (i < maxDistance && type == getCodeCharacterCategory (p.movedBy (-1).getCharacter()))
		{
			p.moveBy (-1);
			++i;
		}
	}

	return p;
}

void CodeDocument::checkLastLineStatus()
{
	while (lines.size() > 0
			&& lines.getLast()->lineLength == 0
			&& (lines.size() == 1 || ! lines.getUnchecked (lines.size() - 2)->endsWithLineBreak()))
	{
		// remove any empty lines at the end if the preceding line doesn't end in a newline.
		lines.removeLast();
	}

	const CodeDocumentLine* const lastLine = lines.getLast();

	if (lastLine != 0 && lastLine->endsWithLineBreak())
	{
		// check that there's an empty line at the end if the preceding one ends in a newline..
		lines.add (new CodeDocumentLine (String::empty, 0, 0, lastLine->lineStartInFile + lastLine->lineLength));
	}
}

void CodeDocument::addListener (CodeDocument::Listener* const listener) throw()
{
	listeners.add (listener);
}

void CodeDocument::removeListener (CodeDocument::Listener* const listener) throw()
{
	listeners.remove (listener);
}

void CodeDocument::sendListenerChangeMessage (const int startLine, const int endLine)
{
	Position startPos (this, startLine, 0);
	Position endPos (this, endLine, 0);

	listeners.call (&Listener::codeDocumentChanged, startPos, endPos);
}

class CodeDocumentInsertAction   : public UndoableAction
{
	CodeDocument& owner;
	const String text;
	int insertPos;

	CodeDocumentInsertAction (const CodeDocumentInsertAction&);
	CodeDocumentInsertAction& operator= (const CodeDocumentInsertAction&);

public:
	CodeDocumentInsertAction (CodeDocument& owner_, const String& text_, const int insertPos_) throw()
		: owner (owner_),
		  text (text_),
		  insertPos (insertPos_)
	{
	}

	~CodeDocumentInsertAction() {}

	bool perform()
	{
		owner.currentActionIndex++;
		owner.insert (text, insertPos, false);
		return true;
	}

	bool undo()
	{
		owner.currentActionIndex--;
		owner.remove (insertPos, insertPos + text.length(), false);
		return true;
	}

	int getSizeInUnits()	{ return text.length() + 32; }
};

void CodeDocument::insert (const String& text, const int insertPos, const bool undoable)
{
	if (text.isEmpty())
		return;

	if (undoable)
	{
		undoManager.perform (new CodeDocumentInsertAction (*this, text, insertPos));
	}
	else
	{
		Position pos (this, insertPos);
		const int firstAffectedLine = pos.getLineNumber();
		int lastAffectedLine = firstAffectedLine + 1;

		CodeDocumentLine* const firstLine = lines [firstAffectedLine];
		String textInsideOriginalLine (text);

		if (firstLine != 0)
		{
			const int index = pos.getIndexInLine();
			textInsideOriginalLine = firstLine->line.substring (0, index)
									 + textInsideOriginalLine
									 + firstLine->line.substring (index);
		}

		maximumLineLength = -1;
		Array <CodeDocumentLine*> newLines;
		CodeDocumentLine::createLines (newLines, textInsideOriginalLine);
		jassert (newLines.size() > 0);

		CodeDocumentLine* const newFirstLine = newLines.getUnchecked (0);
		newFirstLine->lineStartInFile = firstLine != 0 ? firstLine->lineStartInFile : 0;
		lines.set (firstAffectedLine, newFirstLine);

		if (newLines.size() > 1)
		{
			for (int i = 1; i < newLines.size(); ++i)
			{
				CodeDocumentLine* const l = newLines.getUnchecked (i);
				lines.insert (firstAffectedLine + i, l);
			}

			lastAffectedLine = lines.size();
		}

		int i, lineStart = newFirstLine->lineStartInFile;
		for (i = firstAffectedLine; i < lines.size(); ++i)
		{
			CodeDocumentLine* const l = lines.getUnchecked (i);
			l->lineStartInFile = lineStart;
			lineStart += l->lineLength;
		}

		checkLastLineStatus();

		const int newTextLength = text.length();
		for (i = 0; i < positionsToMaintain.size(); ++i)
		{
			CodeDocument::Position* const p = positionsToMaintain.getUnchecked(i);

			if (p->getPosition() >= insertPos)
				p->setPosition (p->getPosition() + newTextLength);
		}

		sendListenerChangeMessage (firstAffectedLine, lastAffectedLine);
	}
}

class CodeDocumentDeleteAction  : public UndoableAction
{
	CodeDocument& owner;
	int startPos, endPos;
	String removedText;

	CodeDocumentDeleteAction (const CodeDocumentDeleteAction&);
	CodeDocumentDeleteAction& operator= (const CodeDocumentDeleteAction&);

public:
	CodeDocumentDeleteAction (CodeDocument& owner_, const int startPos_, const int endPos_) throw()
		: owner (owner_),
		  startPos (startPos_),
		  endPos (endPos_)
	{
		removedText = owner.getTextBetween (CodeDocument::Position (&owner, startPos),
											CodeDocument::Position (&owner, endPos));
	}

	~CodeDocumentDeleteAction() {}

	bool perform()
	{
		owner.currentActionIndex++;
		owner.remove (startPos, endPos, false);
		return true;
	}

	bool undo()
	{
		owner.currentActionIndex--;
		owner.insert (removedText, startPos, false);
		return true;
	}

	int getSizeInUnits()	{ return removedText.length() + 32; }
};

void CodeDocument::remove (const int startPos, const int endPos, const bool undoable)
{
	if (endPos <= startPos)
		return;

	if (undoable)
	{
		undoManager.perform (new CodeDocumentDeleteAction (*this, startPos, endPos));
	}
	else
	{
		Position startPosition (this, startPos);
		Position endPosition (this, endPos);

		maximumLineLength = -1;
		const int firstAffectedLine = startPosition.getLineNumber();
		const int endLine = endPosition.getLineNumber();
		int lastAffectedLine = firstAffectedLine + 1;
		CodeDocumentLine* const firstLine = lines.getUnchecked (firstAffectedLine);

		if (firstAffectedLine == endLine)
		{
			firstLine->line = firstLine->line.substring (0, startPosition.getIndexInLine())
							+ firstLine->line.substring (endPosition.getIndexInLine());
			firstLine->updateLength();
		}
		else
		{
			lastAffectedLine = lines.size();

			CodeDocumentLine* const lastLine = lines.getUnchecked (endLine);
			jassert (lastLine != 0);

			firstLine->line = firstLine->line.substring (0, startPosition.getIndexInLine())
								+ lastLine->line.substring (endPosition.getIndexInLine());
			firstLine->updateLength();

			int numLinesToRemove = endLine - firstAffectedLine;
			lines.removeRange (firstAffectedLine + 1, numLinesToRemove);
		}

		int i;
		for (i = firstAffectedLine + 1; i < lines.size(); ++i)
		{
			CodeDocumentLine* const l = lines.getUnchecked (i);
			const CodeDocumentLine* const previousLine = lines.getUnchecked (i - 1);
			l->lineStartInFile = previousLine->lineStartInFile + previousLine->lineLength;
		}

		checkLastLineStatus();

		const int totalChars = getNumCharacters();

		for (i = 0; i < positionsToMaintain.size(); ++i)
		{
			CodeDocument::Position* p = positionsToMaintain.getUnchecked(i);

			if (p->getPosition() > startPosition.getPosition())
				p->setPosition (jmax (startPos, p->getPosition() + startPos - endPos));

			if (p->getPosition() > totalChars)
				p->setPosition (totalChars);
		}

		sendListenerChangeMessage (firstAffectedLine, lastAffectedLine);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_CodeDocument.cpp ***/


/*** Start of inlined file: juce_CodeEditorComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

class CodeEditorComponent::CaretComponent   : public Component,
											  public Timer
{
public:
	CaretComponent()
	{
		setAlwaysOnTop (true);
		setInterceptsMouseClicks (false, false);
	}

	~CaretComponent()
	{
	}

	void paint (Graphics& g)
	{
		if (getParentComponent()->hasKeyboardFocus (true))
			g.fillAll (findColour (CodeEditorComponent::caretColourId));
	}

	void timerCallback()
	{
		setVisible (! isVisible());
	}

	void updatePosition (CodeEditorComponent& owner)
	{
		startTimer (400);
		setVisible (true);

		const Rectangle<int> pos (owner.getCharacterBounds (owner.getCaretPos()));
		setBounds (pos.getX(), pos.getY(), 2, pos.getHeight());
	}

private:
	CaretComponent (const CaretComponent&);
	CaretComponent& operator= (const CaretComponent&);
};

class CodeEditorComponent::CodeEditorLine
{
public:
	CodeEditorLine() throw()
	   : highlightColumnStart (0), highlightColumnEnd (0)
	{
	}

	~CodeEditorLine() throw()
	{
	}

	bool update (CodeDocument& document, int lineNum,
				 CodeDocument::Iterator& source,
				 CodeTokeniser* analyser, const int spacesPerTab,
				 const CodeDocument::Position& selectionStart,
				 const CodeDocument::Position& selectionEnd)
	{
		Array <SyntaxToken> newTokens;
		newTokens.ensureStorageAllocated (8);

		if (analyser == 0)
		{
			newTokens.add (SyntaxToken (document.getLine (lineNum), -1));
		}
		else if (lineNum < document.getNumLines())
		{
			const CodeDocument::Position pos (&document, lineNum, 0);
			createTokens (pos.getPosition(), pos.getLineText(),
						  source, analyser, newTokens);
		}

		replaceTabsWithSpaces (newTokens, spacesPerTab);

		int newHighlightStart = 0;
		int newHighlightEnd = 0;

		if (selectionStart.getLineNumber() <= lineNum && selectionEnd.getLineNumber() >= lineNum)
		{
			const String line (document.getLine (lineNum));

			CodeDocument::Position lineStart (&document, lineNum, 0), lineEnd (&document, lineNum + 1, 0);
			newHighlightStart = indexToColumn (jmax (0, selectionStart.getPosition() - lineStart.getPosition()),
											   line, spacesPerTab);
			newHighlightEnd = indexToColumn (jmin (lineEnd.getPosition() - lineStart.getPosition(), selectionEnd.getPosition() - lineStart.getPosition()),
											 line, spacesPerTab);
		}

		if (newHighlightStart != highlightColumnStart || newHighlightEnd != highlightColumnEnd)
		{
			highlightColumnStart = newHighlightStart;
			highlightColumnEnd = newHighlightEnd;
		}
		else
		{
			if (tokens.size() == newTokens.size())
			{
				bool allTheSame = true;

				for (int i = newTokens.size(); --i >= 0;)
				{
					if (tokens.getReference(i) != newTokens.getReference(i))
					{
						allTheSame = false;
						break;
					}
				}

				if (allTheSame)
					return false;
			}
		}

		tokens.swapWithArray (newTokens);
		return true;
	}

	void draw (CodeEditorComponent& owner, Graphics& g, const Font& font,
			   float x, const int y, const int baselineOffset, const int lineHeight,
			   const Colour& highlightColour) const throw()
	{
		if (highlightColumnStart < highlightColumnEnd)
		{
			g.setColour (highlightColour);
			g.fillRect (roundToInt (x + highlightColumnStart * owner.getCharWidth()), y,
						roundToInt ((highlightColumnEnd - highlightColumnStart) * owner.getCharWidth()), lineHeight);
		}

		int lastType = std::numeric_limits<int>::min();

		for (int i = 0; i < tokens.size(); ++i)
		{
			SyntaxToken& token = tokens.getReference(i);

			if (lastType != token.tokenType)
			{
				lastType = token.tokenType;
				g.setColour (owner.getColourForTokenType (lastType));
			}

			g.drawSingleLineText (token.text, roundToInt (x), y + baselineOffset);

			if (i < tokens.size() - 1)
			{
				if (token.width < 0)
					token.width = font.getStringWidthFloat (token.text);

				x += token.width;
			}
		}
	}

private:
	struct SyntaxToken
	{
		String text;
		int tokenType;
		float width;

		SyntaxToken (const String& text_, const int type) throw()
			: text (text_), tokenType (type), width (-1.0f)
		{
		}

		bool operator!= (const SyntaxToken& other) const throw()
		{
			return text != other.text || tokenType != other.tokenType;
		}
	};

	Array <SyntaxToken> tokens;
	int highlightColumnStart, highlightColumnEnd;

	static void createTokens (int startPosition, const String& lineText,
							  CodeDocument::Iterator& source,
							  CodeTokeniser* analyser,
							  Array <SyntaxToken>& newTokens)
	{
		CodeDocument::Iterator lastIterator (source);
		const int lineLength = lineText.length();

		for (;;)
		{
			int tokenType = analyser->readNextToken (source);
			int tokenStart = lastIterator.getPosition();
			int tokenEnd = source.getPosition();

			if (tokenEnd <= tokenStart)
				break;

			tokenEnd -= startPosition;

			if (tokenEnd > 0)
			{
				tokenStart -= startPosition;
				newTokens.add (SyntaxToken (lineText.substring (jmax (0, tokenStart), tokenEnd),
											tokenType));

				if (tokenEnd >= lineLength)
					break;
			}

			lastIterator = source;
		}

		source = lastIterator;
	}

	static void replaceTabsWithSpaces (Array <SyntaxToken>& tokens, const int spacesPerTab) throw()
	{
		int x = 0;
		for (int i = 0; i < tokens.size(); ++i)
		{
			SyntaxToken& t = tokens.getReference(i);

			for (;;)
			{
				int tabPos = t.text.indexOfChar ('\t');
				if (tabPos < 0)
					break;

				const int spacesNeeded = spacesPerTab - ((tabPos + x) % spacesPerTab);
				t.text = t.text.replaceSection (tabPos, 1, String::repeatedString (" ", spacesNeeded));
			}

			x += t.text.length();
		}
	}

	int indexToColumn (int index, const String& line, int spacesPerTab) const throw()
	{
		jassert (index <= line.length());

		int col = 0;
		for (int i = 0; i < index; ++i)
		{
			if (line[i] != '\t')
				++col;
			else
				col += spacesPerTab - (col % spacesPerTab);
		}

		return col;
	}
};

CodeEditorComponent::CodeEditorComponent (CodeDocument& document_,
										  CodeTokeniser* const codeTokeniser_)
	: document (document_),
	  firstLineOnScreen (0),
	  gutter (5),
	  spacesPerTab (4),
	  lineHeight (0),
	  linesOnScreen (0),
	  columnsOnScreen (0),
	  scrollbarThickness (16),
	  columnToTryToMaintain (-1),
	  useSpacesForTabs (false),
	  xOffset (0),
	  codeTokeniser (codeTokeniser_)
{
	caretPos = CodeDocument::Position (&document_, 0, 0);
	caretPos.setPositionMaintained (true);

	selectionStart = CodeDocument::Position (&document_, 0, 0);
	selectionStart.setPositionMaintained (true);

	selectionEnd = CodeDocument::Position (&document_, 0, 0);
	selectionEnd.setPositionMaintained (true);

	setOpaque (true);
	setMouseCursor (MouseCursor (MouseCursor::IBeamCursor));
	setWantsKeyboardFocus (true);

	addAndMakeVisible (verticalScrollBar = new ScrollBar (true));
	verticalScrollBar->setSingleStepSize (1.0);

	addAndMakeVisible (horizontalScrollBar = new ScrollBar (false));
	horizontalScrollBar->setSingleStepSize (1.0);

	addAndMakeVisible (caret = new CaretComponent());

	Font f (12.0f);
	f.setTypefaceName (Font::getDefaultMonospacedFontName());
	setFont (f);

	resetToDefaultColours();

	verticalScrollBar->addListener (this);
	horizontalScrollBar->addListener (this);
	document.addListener (this);
}

CodeEditorComponent::~CodeEditorComponent()
{
	document.removeListener (this);
	deleteAllChildren();
}

void CodeEditorComponent::loadContent (const String& newContent)
{
	clearCachedIterators (0);
	document.replaceAllContent (newContent);
	document.clearUndoHistory();
	document.setSavePoint();
	caretPos.setPosition (0);
	selectionStart.setPosition (0);
	selectionEnd.setPosition (0);
	scrollToLine (0);
}

void CodeEditorComponent::codeDocumentChanged (const CodeDocument::Position& affectedTextStart,
											   const CodeDocument::Position& affectedTextEnd)
{
	clearCachedIterators (affectedTextStart.getLineNumber());

	triggerAsyncUpdate();

	caret->updatePosition (*this);
	columnToTryToMaintain = -1;

	if (affectedTextEnd.getPosition() >= selectionStart.getPosition()
		 && affectedTextStart.getPosition() <= selectionEnd.getPosition())
		deselectAll();

	if (caretPos.getPosition() > affectedTextEnd.getPosition()
		 || caretPos.getPosition() < affectedTextStart.getPosition())
		moveCaretTo (affectedTextStart, false);

	updateScrollBars();
}

void CodeEditorComponent::resized()
{
	linesOnScreen = (getHeight() - scrollbarThickness) / lineHeight;
	columnsOnScreen = (int) ((getWidth() - scrollbarThickness) / charWidth);
	lines.clear();
	rebuildLineTokens();
	caret->updatePosition (*this);

	verticalScrollBar->setBounds (getWidth() - scrollbarThickness, 0, scrollbarThickness, getHeight() - scrollbarThickness);
	horizontalScrollBar->setBounds (gutter, getHeight() - scrollbarThickness, getWidth() - scrollbarThickness - gutter, scrollbarThickness);
	updateScrollBars();
}

void CodeEditorComponent::paint (Graphics& g)
{
	handleUpdateNowIfNeeded();

	g.fillAll (findColour (CodeEditorComponent::backgroundColourId));

	g.reduceClipRegion (gutter, 0, verticalScrollBar->getX() - gutter, horizontalScrollBar->getY());

	g.setFont (font);
	const int baselineOffset = (int) font.getAscent();
	const Colour defaultColour (findColour (CodeEditorComponent::defaultTextColourId));
	const Colour highlightColour (findColour (CodeEditorComponent::highlightColourId));

	const Rectangle<int> clip (g.getClipBounds());
	const int firstLineToDraw = jmax (0, clip.getY() / lineHeight);
	const int lastLineToDraw = jmin (lines.size(), clip.getBottom() / lineHeight + 1);

	for (int j = firstLineToDraw; j < lastLineToDraw; ++j)
	{
		lines.getUnchecked(j)->draw (*this, g, font,
									 (float) (gutter - xOffset * charWidth),
									 lineHeight * j, baselineOffset, lineHeight,
									 highlightColour);
	}
}

void CodeEditorComponent::setScrollbarThickness (const int thickness) throw()
{
	if (scrollbarThickness != thickness)
	{
		scrollbarThickness = thickness;
		resized();
	}
}

void CodeEditorComponent::handleAsyncUpdate()
{
	rebuildLineTokens();
}

void CodeEditorComponent::rebuildLineTokens()
{
	cancelPendingUpdate();

	const int numNeeded = linesOnScreen + 1;

	int minLineToRepaint = numNeeded;
	int maxLineToRepaint = 0;

	if (numNeeded != lines.size())
	{
		lines.clear();

		for (int i = numNeeded; --i >= 0;)
			lines.add (new CodeEditorLine());

		minLineToRepaint = 0;
		maxLineToRepaint = numNeeded;
	}

	jassert (numNeeded == lines.size());

	CodeDocument::Iterator source (&document);
	getIteratorForPosition (CodeDocument::Position (&document, firstLineOnScreen, 0).getPosition(), source);

	for (int i = 0; i < numNeeded; ++i)
	{
		CodeEditorLine* const line = lines.getUnchecked(i);

		if (line->update (document, firstLineOnScreen + i, source, codeTokeniser, spacesPerTab,
						  selectionStart, selectionEnd))
		{
			minLineToRepaint = jmin (minLineToRepaint, i);
			maxLineToRepaint = jmax (maxLineToRepaint, i);
		}
	}

	if (minLineToRepaint <= maxLineToRepaint)
	{
		repaint (gutter, lineHeight * minLineToRepaint - 1,
				 verticalScrollBar->getX() - gutter,
				 lineHeight * (1 + maxLineToRepaint - minLineToRepaint) + 2);
	}
}

void CodeEditorComponent::moveCaretTo (const CodeDocument::Position& newPos, const bool highlighting)
{
	caretPos = newPos;
	columnToTryToMaintain = -1;

	if (highlighting)
	{
		if (dragType == notDragging)
		{
			if (abs (caretPos.getPosition() - selectionStart.getPosition())
				  < abs (caretPos.getPosition() - selectionEnd.getPosition()))
				dragType = draggingSelectionStart;
			else
				dragType = draggingSelectionEnd;
		}

		if (dragType == draggingSelectionStart)
		{
			selectionStart = caretPos;

			if (selectionEnd.getPosition() < selectionStart.getPosition())
			{
				const CodeDocument::Position temp (selectionStart);
				selectionStart = selectionEnd;
				selectionEnd = temp;

				dragType = draggingSelectionEnd;
			}
		}
		else
		{
			selectionEnd = caretPos;

			if (selectionEnd.getPosition() < selectionStart.getPosition())
			{
				const CodeDocument::Position temp (selectionStart);
				selectionStart = selectionEnd;
				selectionEnd = temp;

				dragType = draggingSelectionStart;
			}
		}

		triggerAsyncUpdate();
	}
	else
	{
		deselectAll();
	}

	caret->updatePosition (*this);
	scrollToKeepCaretOnScreen();
	updateScrollBars();
}

void CodeEditorComponent::deselectAll()
{
	if (selectionStart != selectionEnd)
		triggerAsyncUpdate();

	selectionStart = caretPos;
	selectionEnd = caretPos;
}

void CodeEditorComponent::updateScrollBars()
{
	verticalScrollBar->setRangeLimits (0, jmax (document.getNumLines(), firstLineOnScreen + linesOnScreen));
	verticalScrollBar->setCurrentRange (firstLineOnScreen, linesOnScreen);

	horizontalScrollBar->setRangeLimits (0, jmax ((double) document.getMaximumLineLength(), xOffset + columnsOnScreen));
	horizontalScrollBar->setCurrentRange (xOffset, columnsOnScreen);
}

void CodeEditorComponent::scrollToLineInternal (int newFirstLineOnScreen)
{
	newFirstLineOnScreen = jlimit (0, jmax (0, document.getNumLines() - 1),
								   newFirstLineOnScreen);

	if (newFirstLineOnScreen != firstLineOnScreen)
	{
		firstLineOnScreen = newFirstLineOnScreen;
		caret->updatePosition (*this);

		updateCachedIterators (firstLineOnScreen);
		triggerAsyncUpdate();
	}
}

void CodeEditorComponent::scrollToColumnInternal (double column)
{
	const double newOffset = jlimit (0.0, document.getMaximumLineLength() + 3.0, column);

	if (xOffset != newOffset)
	{
		xOffset = newOffset;
		caret->updatePosition (*this);
		repaint();
	}
}

void CodeEditorComponent::scrollToLine (int newFirstLineOnScreen)
{
	scrollToLineInternal (newFirstLineOnScreen);
	updateScrollBars();
}

void CodeEditorComponent::scrollToColumn (int newFirstColumnOnScreen)
{
	scrollToColumnInternal (newFirstColumnOnScreen);
	updateScrollBars();
}

void CodeEditorComponent::scrollBy (int deltaLines)
{
	scrollToLine (firstLineOnScreen + deltaLines);
}

void CodeEditorComponent::scrollToKeepCaretOnScreen()
{
	if (caretPos.getLineNumber() < firstLineOnScreen)
		scrollBy (caretPos.getLineNumber() - firstLineOnScreen);
	else if (caretPos.getLineNumber() >= firstLineOnScreen + linesOnScreen)
		scrollBy (caretPos.getLineNumber() - (firstLineOnScreen + linesOnScreen - 1));

	const int column = indexToColumn (caretPos.getLineNumber(), caretPos.getIndexInLine());
	if (column >= xOffset + columnsOnScreen - 1)
		scrollToColumn (column + 1 - columnsOnScreen);
	else if (column < xOffset)
		scrollToColumn (column);
}

const Rectangle<int> CodeEditorComponent::getCharacterBounds (const CodeDocument::Position& pos) const throw()
{
	return Rectangle<int> (roundToInt ((gutter - xOffset * charWidth) + indexToColumn (pos.getLineNumber(), pos.getIndexInLine()) * charWidth),
						   (pos.getLineNumber() - firstLineOnScreen) * lineHeight,
						   roundToInt (charWidth),
						   lineHeight);
}

const CodeDocument::Position CodeEditorComponent::getPositionAt (int x, int y)
{
	const int line = y / lineHeight + firstLineOnScreen;
	const int column = roundToInt ((x - (gutter - xOffset * charWidth)) / charWidth);
	const int index = columnToIndex (line, column);

	return CodeDocument::Position (&document, line, index);
}

void CodeEditorComponent::insertTextAtCaret (const String& newText)
{
	document.deleteSection (selectionStart, selectionEnd);

	if (newText.isNotEmpty())
		document.insertText (caretPos, newText);

	scrollToKeepCaretOnScreen();
}

void CodeEditorComponent::insertTabAtCaret()
{
	if (CharacterFunctions::isWhitespace (caretPos.getCharacter())
		 && caretPos.getLineNumber() == caretPos.movedBy (1).getLineNumber())
	{
		moveCaretTo (document.findWordBreakAfter (caretPos), false);
	}

	if (useSpacesForTabs)
	{
		const int caretCol = indexToColumn (caretPos.getLineNumber(), caretPos.getIndexInLine());
		const int spacesNeeded = spacesPerTab - (caretCol % spacesPerTab);
		insertTextAtCaret (String::repeatedString (" ", spacesNeeded));
	}
	else
	{
		insertTextAtCaret ("\t");
	}
}

void CodeEditorComponent::cut()
{
	insertTextAtCaret (String::empty);
}

void CodeEditorComponent::copy()
{
	newTransaction();

	const String selection (document.getTextBetween (selectionStart, selectionEnd));

	if (selection.isNotEmpty())
		SystemClipboard::copyTextToClipboard (selection);
}

void CodeEditorComponent::copyThenCut()
{
	copy();
	cut();
	newTransaction();
}

void CodeEditorComponent::paste()
{
	newTransaction();
	const String clip (SystemClipboard::getTextFromClipboard());

	if (clip.isNotEmpty())
		insertTextAtCaret (clip);

	newTransaction();
}

void CodeEditorComponent::cursorLeft (const bool moveInWholeWordSteps, const bool selecting)
{
	newTransaction();

	if (moveInWholeWordSteps)
		moveCaretTo (document.findWordBreakBefore (caretPos), selecting);
	else
		moveCaretTo (caretPos.movedBy (-1), selecting);
}

void CodeEditorComponent::cursorRight (const bool moveInWholeWordSteps, const bool selecting)
{
	newTransaction();

	if (moveInWholeWordSteps)
		moveCaretTo (document.findWordBreakAfter (caretPos), selecting);
	else
		moveCaretTo (caretPos.movedBy (1), selecting);
}

void CodeEditorComponent::moveLineDelta (const int delta, const bool selecting)
{
	CodeDocument::Position pos (caretPos);
	const int newLineNum = pos.getLineNumber() + delta;

	if (columnToTryToMaintain < 0)
		columnToTryToMaintain = indexToColumn (pos.getLineNumber(), pos.getIndexInLine());

	pos.setLineAndIndex (newLineNum, columnToIndex (newLineNum, columnToTryToMaintain));

	const int colToMaintain = columnToTryToMaintain;
	moveCaretTo (pos, selecting);
	columnToTryToMaintain = colToMaintain;
}

void CodeEditorComponent::cursorDown (const bool selecting)
{
	newTransaction();

	if (caretPos.getLineNumber() == document.getNumLines() - 1)
		moveCaretTo (CodeDocument::Position (&document, std::numeric_limits<int>::max(), std::numeric_limits<int>::max()), selecting);
	else
		moveLineDelta (1, selecting);
}

void CodeEditorComponent::cursorUp (const bool selecting)
{
	newTransaction();

	if (caretPos.getLineNumber() == 0)
		moveCaretTo (CodeDocument::Position (&document, 0, 0), selecting);
	else
		moveLineDelta (-1, selecting);
}

void CodeEditorComponent::pageDown (const bool selecting)
{
	newTransaction();

	scrollBy (jlimit (0, linesOnScreen, 1 + document.getNumLines() - firstLineOnScreen - linesOnScreen));
	moveLineDelta (linesOnScreen, selecting);
}

void CodeEditorComponent::pageUp (const bool selecting)
{
	newTransaction();

	scrollBy (-linesOnScreen);
	moveLineDelta (-linesOnScreen, selecting);
}

void CodeEditorComponent::scrollUp()
{
	newTransaction();
	scrollBy (1);

	if (caretPos.getLineNumber() < firstLineOnScreen)
		moveLineDelta (1, false);
}

void CodeEditorComponent::scrollDown()
{
	newTransaction();
	scrollBy (-1);

	if (caretPos.getLineNumber() >= firstLineOnScreen + linesOnScreen)
		moveLineDelta (-1, false);
}

void CodeEditorComponent::goToStartOfDocument (const bool selecting)
{
	newTransaction();
	moveCaretTo (CodeDocument::Position (&document, 0, 0), selecting);
}

static int findFirstNonWhitespaceChar (const String& line) throw()
{
	const int len = line.length();

	for (int i = 0; i < len; ++i)
		if (! CharacterFunctions::isWhitespace (line [i]))
			return i;

	return 0;
}

void CodeEditorComponent::goToStartOfLine (const bool selecting)
{
	newTransaction();

	int index = findFirstNonWhitespaceChar (caretPos.getLineText());

	if (index >= caretPos.getIndexInLine() && caretPos.getIndexInLine() > 0)
		index = 0;

	moveCaretTo (CodeDocument::Position (&document, caretPos.getLineNumber(), index), selecting);
}

void CodeEditorComponent::goToEndOfDocument (const bool selecting)
{
	newTransaction();
	moveCaretTo (CodeDocument::Position (&document, std::numeric_limits<int>::max(), std::numeric_limits<int>::max()), selecting);
}

void CodeEditorComponent::goToEndOfLine (const bool selecting)
{
	newTransaction();
	moveCaretTo (CodeDocument::Position (&document, caretPos.getLineNumber(), std::numeric_limits<int>::max()), selecting);
}

void CodeEditorComponent::backspace (const bool moveInWholeWordSteps)
{
	if (moveInWholeWordSteps)
	{
		cut(); // in case something is already highlighted
		moveCaretTo (document.findWordBreakBefore (caretPos), true);
	}
	else
	{
		if (selectionStart == selectionEnd)
			selectionStart.moveBy (-1);
	}

	cut();
}

void CodeEditorComponent::deleteForward (const bool moveInWholeWordSteps)
{
	if (moveInWholeWordSteps)
	{
		cut(); // in case something is already highlighted
		moveCaretTo (document.findWordBreakAfter (caretPos), true);
	}
	else
	{
		if (selectionStart == selectionEnd)
			selectionEnd.moveBy (1);
		else
			newTransaction();
	}

	cut();
}

void CodeEditorComponent::selectAll()
{
	newTransaction();
	moveCaretTo (CodeDocument::Position (&document, std::numeric_limits<int>::max(), std::numeric_limits<int>::max()), false);
	moveCaretTo (CodeDocument::Position (&document, 0, 0), true);
}

void CodeEditorComponent::undo()
{
	document.undo();
	scrollToKeepCaretOnScreen();
}

void CodeEditorComponent::redo()
{
	document.redo();
	scrollToKeepCaretOnScreen();
}

void CodeEditorComponent::newTransaction()
{
	document.newTransaction();
	startTimer (600);
}

void CodeEditorComponent::timerCallback()
{
	newTransaction();
}

const Range<int> CodeEditorComponent::getHighlightedRegion() const
{
	return Range<int> (selectionStart.getPosition(), selectionEnd.getPosition());
}

void CodeEditorComponent::setHighlightedRegion (const Range<int>& newRange)
{
	moveCaretTo (CodeDocument::Position (&document, newRange.getStart()), false);
	moveCaretTo (CodeDocument::Position (&document, newRange.getEnd()), true);
}

const String CodeEditorComponent::getTextInRange (const Range<int>& range) const
{
	return document.getTextBetween (CodeDocument::Position (&document, range.getStart()),
									CodeDocument::Position (&document, range.getEnd()));
}

bool CodeEditorComponent::keyPressed (const KeyPress& key)
{
	const bool moveInWholeWordSteps = key.getModifiers().isCtrlDown() || key.getModifiers().isAltDown();
	const bool shiftDown = key.getModifiers().isShiftDown();

	if (key.isKeyCode (KeyPress::leftKey))
	{
		cursorLeft (moveInWholeWordSteps, shiftDown);
	}
	else if (key.isKeyCode (KeyPress::rightKey))
	{
		cursorRight (moveInWholeWordSteps, shiftDown);
	}
	else if (key.isKeyCode (KeyPress::upKey))
	{
		if (key.getModifiers().isCtrlDown() && ! shiftDown)
			scrollDown();
#if JUCE_MAC
		else if (key.getModifiers().isCommandDown())
			goToStartOfDocument (shiftDown);
#endif
		else
			cursorUp (shiftDown);
	}
	else if (key.isKeyCode (KeyPress::downKey))
	{
		if (key.getModifiers().isCtrlDown() && ! shiftDown)
			scrollUp();
#if JUCE_MAC
		else if (key.getModifiers().isCommandDown())
			goToEndOfDocument (shiftDown);
#endif
		else
			cursorDown (shiftDown);
	}
	else if (key.isKeyCode (KeyPress::pageDownKey))
	{
		pageDown (shiftDown);
	}
	else if (key.isKeyCode (KeyPress::pageUpKey))
	{
		pageUp (shiftDown);
	}
	else if (key.isKeyCode (KeyPress::homeKey))
	{
		if (moveInWholeWordSteps)
			goToStartOfDocument (shiftDown);
		else
			goToStartOfLine (shiftDown);
	}
	else if (key.isKeyCode (KeyPress::endKey))
	{
		if (moveInWholeWordSteps)
			goToEndOfDocument (shiftDown);
		else
			goToEndOfLine (shiftDown);
	}
	else if (key.isKeyCode (KeyPress::backspaceKey))
	{
		backspace (moveInWholeWordSteps);
	}
	else if (key.isKeyCode (KeyPress::deleteKey))
	{
		deleteForward (moveInWholeWordSteps);
	}
	else if (key == KeyPress ('c', ModifierKeys::commandModifier, 0))
	{
		copy();
	}
	else if (key == KeyPress ('x', ModifierKeys::commandModifier, 0))
	{
		copyThenCut();
	}
	else if (key == KeyPress ('v', ModifierKeys::commandModifier, 0))
	{
		paste();
	}
	else if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
	{
		undo();
	}
	else if (key == KeyPress ('y', ModifierKeys::commandModifier, 0)
			  || key == KeyPress ('z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0))
	{
		redo();
	}
	else if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
	{
		selectAll();
	}
	else if (key == KeyPress::tabKey || key.getTextCharacter() == '\t')
	{
		insertTabAtCaret();
	}
	else if (key == KeyPress::returnKey)
	{
		newTransaction();
		insertTextAtCaret (document.getNewLineCharacters());
	}
	else if (key.isKeyCode (KeyPress::escapeKey))
	{
		newTransaction();
	}
	else if (key.getTextCharacter() >= ' ')
	{
		insertTextAtCaret (String::charToString (key.getTextCharacter()));
	}
	else
	{
		return false;
	}

	return true;
}

void CodeEditorComponent::mouseDown (const MouseEvent& e)
{
	newTransaction();
	dragType = notDragging;

	if (! e.mods.isPopupMenu())
	{
		beginDragAutoRepeat (100);
		moveCaretTo (getPositionAt (e.x, e.y), e.mods.isShiftDown());
	}
	else
	{
	}
}

void CodeEditorComponent::mouseDrag (const MouseEvent& e)
{
	if (! e.mods.isPopupMenu())
		moveCaretTo (getPositionAt (e.x, e.y), true);
}

void CodeEditorComponent::mouseUp (const MouseEvent&)
{
	newTransaction();
	beginDragAutoRepeat (0);
	dragType = notDragging;
}

void CodeEditorComponent::mouseDoubleClick (const MouseEvent& e)
{
	CodeDocument::Position tokenStart (getPositionAt (e.x, e.y));
	CodeDocument::Position tokenEnd (tokenStart);

	if (e.getNumberOfClicks() > 2)
	{
		tokenStart.setLineAndIndex (tokenStart.getLineNumber(), 0);
		tokenEnd.setLineAndIndex (tokenStart.getLineNumber() + 1, 0);
	}
	else
	{
		while (CharacterFunctions::isLetterOrDigit (tokenEnd.getCharacter()))
			tokenEnd.moveBy (1);

		tokenStart = tokenEnd;

		while (tokenStart.getIndexInLine() > 0
				&& CharacterFunctions::isLetterOrDigit (tokenStart.movedBy (-1).getCharacter()))
			tokenStart.moveBy (-1);
	}

	moveCaretTo (tokenEnd, false);
	moveCaretTo (tokenStart, true);
}

void CodeEditorComponent::mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
	verticalScrollBar->mouseWheelMove (e, 0, wheelIncrementY);
	horizontalScrollBar->mouseWheelMove (e, wheelIncrementX, 0);
}

void CodeEditorComponent::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
{
	if (scrollBarThatHasMoved == verticalScrollBar)
		scrollToLineInternal ((int) newRangeStart);
	else
		scrollToColumnInternal (newRangeStart);
}

void CodeEditorComponent::setTabSize (const int numSpaces, const bool insertSpaces) throw()
{
	useSpacesForTabs = insertSpaces;

	if (spacesPerTab != numSpaces)
	{
		spacesPerTab = numSpaces;
		triggerAsyncUpdate();
	}
}

int CodeEditorComponent::indexToColumn (int lineNum, int index) const throw()
{
	const String line (document.getLine (lineNum));
	jassert (index <= line.length());

	int col = 0;
	for (int i = 0; i < index; ++i)
	{
		if (line[i] != '\t')
			++col;
		else
			col += getTabSize() - (col % getTabSize());
	}

	return col;
}

int CodeEditorComponent::columnToIndex (int lineNum, int column) const throw()
{
	const String line (document.getLine (lineNum));
	const int lineLength = line.length();

	int i, col = 0;
	for (i = 0; i < lineLength; ++i)
	{
		if (line[i] != '\t')
			++col;
		else
			col += getTabSize() - (col % getTabSize());

		if (col > column)
			break;
	}

	return i;
}

void CodeEditorComponent::setFont (const Font& newFont)
{
	font = newFont;
	charWidth = font.getStringWidthFloat ("0");
	lineHeight = roundToInt (font.getHeight());
	resized();
}

void CodeEditorComponent::resetToDefaultColours()
{
	coloursForTokenCategories.clear();

	if (codeTokeniser != 0)
	{
		for (int i = codeTokeniser->getTokenTypes().size(); --i >= 0;)
			setColourForTokenType (i, codeTokeniser->getDefaultColour (i));
	}
}

void CodeEditorComponent::setColourForTokenType (const int tokenType, const Colour& colour)
{
	jassert (tokenType < 256);

	while (coloursForTokenCategories.size() < tokenType)
		coloursForTokenCategories.add (Colours::black);

	coloursForTokenCategories.set (tokenType, colour);
	repaint();
}

const Colour CodeEditorComponent::getColourForTokenType (const int tokenType) const throw()
{
	if (((unsigned int) tokenType) >= (unsigned int) coloursForTokenCategories.size())
		return findColour (CodeEditorComponent::defaultTextColourId);

	return coloursForTokenCategories.getReference (tokenType);
}

void CodeEditorComponent::clearCachedIterators (const int firstLineToBeInvalid) throw()
{
	int i;
	for (i = cachedIterators.size(); --i >= 0;)
		if (cachedIterators.getUnchecked (i)->getLine() < firstLineToBeInvalid)
			break;

	cachedIterators.removeRange (jmax (0, i - 1), cachedIterators.size());
}

void CodeEditorComponent::updateCachedIterators (int maxLineNum)
{
	const int maxNumCachedPositions = 5000;
	const int linesBetweenCachedSources = jmax (10, document.getNumLines() / maxNumCachedPositions);

	if (cachedIterators.size() == 0)
		cachedIterators.add (new CodeDocument::Iterator (&document));

	if (codeTokeniser == 0)
		return;

	for (;;)
	{
		CodeDocument::Iterator* last = cachedIterators.getLast();

		if (last->getLine() >= maxLineNum)
			break;

		CodeDocument::Iterator* t = new CodeDocument::Iterator (*last);
		cachedIterators.add (t);
		const int targetLine = last->getLine() + linesBetweenCachedSources;

		for (;;)
		{
			codeTokeniser->readNextToken (*t);

			if (t->getLine() >= targetLine)
				break;

			if (t->isEOF())
				return;
		}
	}
}

void CodeEditorComponent::getIteratorForPosition (int position, CodeDocument::Iterator& source)
{
	if (codeTokeniser == 0)
		return;

	for (int i = cachedIterators.size(); --i >= 0;)
	{
		CodeDocument::Iterator* t = cachedIterators.getUnchecked (i);
		if (t->getPosition() <= position)
		{
			source = *t;
			break;
		}
	}

	while (source.getPosition() < position)
	{
		const CodeDocument::Iterator original (source);
		codeTokeniser->readNextToken (source);

		if (source.getPosition() > position || source.isEOF())
		{
			source = original;
			break;
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_CodeEditorComponent.cpp ***/


/*** Start of inlined file: juce_CPlusPlusCodeTokeniser.cpp ***/
BEGIN_JUCE_NAMESPACE

CPlusPlusCodeTokeniser::CPlusPlusCodeTokeniser()
{
}

CPlusPlusCodeTokeniser::~CPlusPlusCodeTokeniser()
{
}

namespace CppTokeniser
{

static bool isIdentifierStart (const juce_wchar c) throw()
{
	return CharacterFunctions::isLetter (c)
			|| c == '_' || c == '@';
}

static bool isIdentifierBody (const juce_wchar c) throw()
{
	return CharacterFunctions::isLetter (c)
			|| CharacterFunctions::isDigit (c)
			|| c == '_' || c == '@';
}

static int parseIdentifier (CodeDocument::Iterator& source) throw()
{
	static const juce_wchar* keywords2Char[] =
		{ T("if"), T("do"), T("or"), 0 };

	static const juce_wchar* keywords3Char[] =
		{ T("for"), T("int"), T("new"), T("try"), T("xor"), T("and"), T("asm"), T("not"), 0 };

	static const juce_wchar* keywords4Char[] =
		{ T("bool"), T("void"), T("this"), T("true"), T("long"), T("else"), T("char"),
		  T("enum"), T("case"), T("goto"), T("auto"), 0 };

	static const juce_wchar* keywords5Char[] =
		{  T("while"), T("bitor"), T("break"), T("catch"), T("class"), T("compl"), T("const"), T("false"),
			T("float"), T("short"), T("throw"), T("union"), T("using"), T("or_eq"), 0 };

	static const juce_wchar* keywords6Char[] =
		{ T("return"), T("struct"), T("and_eq"), T("bitand"), T("delete"), T("double"), T("extern"),
		  T("friend"), T("inline"), T("not_eq"), T("public"), T("sizeof"), T("static"), T("signed"),
		  T("switch"), T("typeid"), T("wchar_t"), T("xor_eq"), 0};

	static const juce_wchar* keywordsOther[] =
		{ T("const_cast"), T("continue"), T("default"), T("explicit"), T("mutable"), T("namespace"),
		  T("operator"), T("private"), T("protected"), T("register"), T("reinterpret_cast"), T("static_cast"),
		  T("template"), T("typedef"), T("typename"), T("unsigned"), T("virtual"), T("volatile"),
		  T("@implementation"), T("@interface"), T("@end"), T("@synthesize"), T("@dynamic"), T("@public"),
		  T("@private"), T("@property"), T("@protected"), T("@class"), 0 };

	int tokenLength = 0;
	juce_wchar possibleIdentifier [19];

	while (isIdentifierBody (source.peekNextChar()))
	{
		const juce_wchar c = source.nextChar();

		if (tokenLength < numElementsInArray (possibleIdentifier) - 1)
			possibleIdentifier [tokenLength] = c;

		++tokenLength;
	}

	if (tokenLength > 1 && tokenLength <= 16)
	{
		possibleIdentifier [tokenLength] = 0;
		const juce_wchar** k;

		switch (tokenLength)
		{
			case 2:	 k = keywords2Char; break;
			case 3:	 k = keywords3Char; break;
			case 4:	 k = keywords4Char; break;
			case 5:	 k = keywords5Char; break;
			case 6:	 k = keywords6Char; break;
			default:	k = keywordsOther; break;
		}

		int i = 0;
		while (k[i] != 0)
		{
			if (k[i][0] == possibleIdentifier[0] && CharacterFunctions::compare (k[i], possibleIdentifier) == 0)
				return CPlusPlusCodeTokeniser::tokenType_builtInKeyword;

			++i;
		}
	}

	return CPlusPlusCodeTokeniser::tokenType_identifier;
}

static bool skipNumberSuffix (CodeDocument::Iterator& source)
{
	const juce_wchar c = source.peekNextChar();
	if (c == 'l' || c == 'L' || c == 'u' || c == 'U')
		source.skip();

	if (CharacterFunctions::isLetterOrDigit (source.peekNextChar()))
		return false;

	return true;
}

static bool isHexDigit (const juce_wchar c) throw()
{
	return (c >= '0' && c <= '9')
			|| (c >= 'a' && c <= 'f')
			|| (c >= 'A' && c <= 'F');
}

static bool parseHexLiteral (CodeDocument::Iterator& source) throw()
{
	if (source.nextChar() != '0')
		return false;

	juce_wchar c = source.nextChar();
	if (c != 'x' && c != 'X')
		return false;

	int numDigits = 0;
	while (isHexDigit (source.peekNextChar()))
	{
		++numDigits;
		source.skip();
	}

	if (numDigits == 0)
		return false;

	return skipNumberSuffix (source);
}

static bool isOctalDigit (const juce_wchar c) throw()
{
	return c >= '0' && c <= '7';
}

static bool parseOctalLiteral (CodeDocument::Iterator& source) throw()
{
	if (source.nextChar() != '0')
		return false;

	if (! isOctalDigit (source.nextChar()))
		 return false;

	while (isOctalDigit (source.peekNextChar()))
		source.skip();

	return skipNumberSuffix (source);
}

static bool isDecimalDigit (const juce_wchar c) throw()
{
	return c >= '0' && c <= '9';
}

static bool parseDecimalLiteral (CodeDocument::Iterator& source) throw()
{
	int numChars = 0;
	while (isDecimalDigit (source.peekNextChar()))
	{
		++numChars;
		source.skip();
	}

	if (numChars == 0)
		return false;

	return skipNumberSuffix (source);
}

static bool parseFloatLiteral (CodeDocument::Iterator& source) throw()
{
	int numDigits = 0;

	while (isDecimalDigit (source.peekNextChar()))
	{
		source.skip();
		++numDigits;
	}

	const bool hasPoint = (source.peekNextChar() == '.');

	if (hasPoint)
	{
		source.skip();

		while (isDecimalDigit (source.peekNextChar()))
		{
			source.skip();
			++numDigits;
		}
	}

	if (numDigits == 0)
		return false;

	juce_wchar c = source.peekNextChar();
	const bool hasExponent = (c == 'e' || c == 'E');

	if (hasExponent)
	{
		source.skip();

		c = source.peekNextChar();
		if (c == '+' || c == '-')
			source.skip();

		int numExpDigits = 0;
		while (isDecimalDigit (source.peekNextChar()))
		{
			source.skip();
			++numExpDigits;
		}

		if (numExpDigits == 0)
			return false;
	}

	c = source.peekNextChar();
	if (c == 'f' || c == 'F')
		source.skip();
	else if (! (hasExponent || hasPoint))
		return false;

	return true;
}

static int parseNumber (CodeDocument::Iterator& source)
{
	const CodeDocument::Iterator original (source);

	if (parseFloatLiteral (source))
		return CPlusPlusCodeTokeniser::tokenType_floatLiteral;

	source = original;

	if (parseHexLiteral (source))
		return CPlusPlusCodeTokeniser::tokenType_integerLiteral;

	source = original;

	if (parseOctalLiteral (source))
		return CPlusPlusCodeTokeniser::tokenType_integerLiteral;

	source = original;

	if (parseDecimalLiteral (source))
		return CPlusPlusCodeTokeniser::tokenType_integerLiteral;

	source = original;
	source.skip();

	return CPlusPlusCodeTokeniser::tokenType_error;
}

static void skipQuotedString (CodeDocument::Iterator& source) throw()
{
	const juce_wchar quote = source.nextChar();

	for (;;)
	{
		const juce_wchar c = source.nextChar();

		if (c == quote || c == 0)
			break;

		if (c == '\\')
			source.skip();
	}
}

static void skipComment (CodeDocument::Iterator& source) throw()
{
	bool lastWasStar = false;

	for (;;)
	{
		const juce_wchar c = source.nextChar();

		if (c == 0 || (c == '/' && lastWasStar))
			break;

		lastWasStar = (c == '*');
	}
}

}

int CPlusPlusCodeTokeniser::readNextToken (CodeDocument::Iterator& source)
{
	int result = tokenType_error;
	source.skipWhitespace();

	juce_wchar firstChar = source.peekNextChar();

	switch (firstChar)
	{
	case 0:
		source.skip();
		break;

	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9':
		result = CppTokeniser::parseNumber (source);
		break;

	case '.':
		result = CppTokeniser::parseNumber (source);

		if (result == tokenType_error)
			result = tokenType_punctuation;

		break;

	case ',':
	case ';':
	case ':':
		source.skip();
		result = tokenType_punctuation;
		break;

	case '(':
	case ')':
	case '{':
	case '}':
	case '[':
	case ']':
		source.skip();
		result = tokenType_bracket;
		break;

	case '"':
	case '\'':
		CppTokeniser::skipQuotedString (source);
		result = tokenType_stringLiteral;
		break;

	case '+':
		result = tokenType_operator;
		source.skip();

		if (source.peekNextChar() == '+')
			source.skip();
		else if (source.peekNextChar() == '=')
			source.skip();

		break;

	case '-':
		source.skip();
		result = CppTokeniser::parseNumber (source);

		if (result == tokenType_error)
		{
			result = tokenType_operator;

			if (source.peekNextChar() == '-')
				source.skip();
			else if (source.peekNextChar() == '=')
				source.skip();
		}
		break;

	case '*':
	case '%':
	case '=':
	case '!':
		result = tokenType_operator;
		source.skip();

		if (source.peekNextChar() == '=')
			source.skip();

		break;

	case '/':
		result = tokenType_operator;
		source.skip();

		if (source.peekNextChar() == '=')
		{
			source.skip();
		}
		else if (source.peekNextChar() == '/')
		{
			result = tokenType_comment;
			source.skipToEndOfLine();
		}
		else if (source.peekNextChar() == '*')
		{
			source.skip();
			result = tokenType_comment;
			CppTokeniser::skipComment (source);
		}

		break;

	case '?':
	case '~':
		source.skip();
		result = tokenType_operator;
		break;

	case '<':
		source.skip();
		result = tokenType_operator;

		if (source.peekNextChar() == '=')
		{
			source.skip();
		}
		else if (source.peekNextChar() == '<')
		{
			source.skip();

			if (source.peekNextChar() == '=')
				source.skip();
		}

		break;

	case '>':
		source.skip();
		result = tokenType_operator;

		if (source.peekNextChar() == '=')
		{
			source.skip();
		}
		else if (source.peekNextChar() == '<')
		{
			source.skip();

			if (source.peekNextChar() == '=')
				source.skip();
		}

		break;

	case '|':
		source.skip();
		result = tokenType_operator;

		if (source.peekNextChar() == '=')
		{
			source.skip();
		}
		else if (source.peekNextChar() == '|')
		{
			source.skip();

			if (source.peekNextChar() == '=')
				source.skip();
		}

		break;

	case '&':
		source.skip();
		result = tokenType_operator;

		if (source.peekNextChar() == '=')
		{
			source.skip();
		}
		else if (source.peekNextChar() == '&')
		{
			source.skip();

			if (source.peekNextChar() == '=')
				source.skip();
		}

		break;

	case '^':
		source.skip();
		result = tokenType_operator;

		if (source.peekNextChar() == '=')
		{
			source.skip();
		}
		else if (source.peekNextChar() == '^')
		{
			source.skip();

			if (source.peekNextChar() == '=')
				source.skip();
		}

		break;

	case '#':
		result = tokenType_preprocessor;
		source.skipToEndOfLine();
		break;

	default:
		if (CppTokeniser::isIdentifierStart (firstChar))
			result = CppTokeniser::parseIdentifier (source);
		else
			source.skip();

		break;
	}

	//jassert (result != tokenType_unknown);
	return result;
}

const StringArray CPlusPlusCodeTokeniser::getTokenTypes()
{
	StringArray s;
	s.add ("Error");
	s.add ("Comment");
	s.add ("C++ keyword");
	s.add ("Identifier");
	s.add ("Integer literal");
	s.add ("Float literal");
	s.add ("String literal");
	s.add ("Operator");
	s.add ("Bracket");
	s.add ("Punctuation");
	s.add ("Preprocessor line");
	return s;
}

const Colour CPlusPlusCodeTokeniser::getDefaultColour (const int tokenType)
{
	const uint32 colours[] =
	{
		0xffcc0000,  // error
		0xff00aa00,  // comment
		0xff0000cc,  // keyword
		0xff000000,  // identifier
		0xff880000,  // int literal
		0xff885500,  // float literal
		0xff990099,  // string literal
		0xff225500,  // operator
		0xff000055,  // bracket
		0xff004400,  // punctuation
		0xff660000   // preprocessor
	};

	if (tokenType >= 0 && tokenType < numElementsInArray (colours))
		return Colour (colours [tokenType]);

	return Colours::black;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_CPlusPlusCodeTokeniser.cpp ***/


/*** Start of inlined file: juce_ComboBox.cpp ***/
BEGIN_JUCE_NAMESPACE

ComboBox::ComboBox (const String& name)
	: Component (name),
	  lastCurrentId (0),
	  isButtonDown (false),
	  separatorPending (false),
	  menuActive (false),
	  label (0)
{
	noChoicesMessage = TRANS("(no choices)");
	setRepaintsOnMouseActivity (true);

	lookAndFeelChanged();

	currentId.addListener (this);
}

ComboBox::~ComboBox()
{
	currentId.removeListener (this);

	if (menuActive)
		PopupMenu::dismissAllActiveMenus();

	label = 0;
	deleteAllChildren();
}

void ComboBox::setEditableText (const bool isEditable)
{
	label->setEditable (isEditable, isEditable, false);

	setWantsKeyboardFocus (! isEditable);
	resized();
}

bool ComboBox::isTextEditable() const throw()
{
	return label->isEditable();
}

void ComboBox::setJustificationType (const Justification& justification) throw()
{
	label->setJustificationType (justification);
}

const Justification ComboBox::getJustificationType() const throw()
{
	return label->getJustificationType();
}

void ComboBox::setTooltip (const String& newTooltip)
{
	SettableTooltipClient::setTooltip (newTooltip);
	label->setTooltip (newTooltip);
}

void ComboBox::addItem (const String& newItemText,
						const int newItemId) throw()
{
	// you can't add empty strings to the list..
	jassert (newItemText.isNotEmpty());

	// IDs must be non-zero, as zero is used to indicate a lack of selecion.
	jassert (newItemId != 0);

	// you shouldn't use duplicate item IDs!
	jassert (getItemForId (newItemId) == 0);

	if (newItemText.isNotEmpty() && newItemId != 0)
	{
		if (separatorPending)
		{
			separatorPending = false;

			ItemInfo* const item = new ItemInfo();
			item->itemId = 0;
			item->isEnabled = false;
			item->isHeading = false;
			items.add (item);
		}

		ItemInfo* const item = new ItemInfo();
		item->name = newItemText;
		item->itemId = newItemId;
		item->isEnabled = true;
		item->isHeading = false;
		items.add (item);
	}
}

void ComboBox::addSeparator() throw()
{
	separatorPending = (items.size() > 0);
}

void ComboBox::addSectionHeading (const String& headingName) throw()
{
	// you can't add empty strings to the list..
	jassert (headingName.isNotEmpty());

	if (headingName.isNotEmpty())
	{
		if (separatorPending)
		{
			separatorPending = false;

			ItemInfo* const item = new ItemInfo();
			item->itemId = 0;
			item->isEnabled = false;
			item->isHeading = false;
			items.add (item);
		}

		ItemInfo* const item = new ItemInfo();
		item->name = headingName;
		item->itemId = 0;
		item->isEnabled = true;
		item->isHeading = true;
		items.add (item);
	}
}

void ComboBox::setItemEnabled (const int itemId,
							   const bool shouldBeEnabled) throw()
{
	ItemInfo* const item = getItemForId (itemId);

	if (item != 0)
		item->isEnabled = shouldBeEnabled;
}

void ComboBox::changeItemText (const int itemId,
							   const String& newText) throw()
{
	ItemInfo* const item = getItemForId (itemId);

	jassert (item != 0);

	if (item != 0)
		item->name = newText;
}

void ComboBox::clear (const bool dontSendChangeMessage)
{
	items.clear();
	separatorPending = false;

	if (! label->isEditable())
		setSelectedItemIndex (-1, dontSendChangeMessage);
}

bool ComboBox::ItemInfo::isSeparator() const throw()
{
	return name.isEmpty();
}

bool ComboBox::ItemInfo::isRealItem() const throw()
{
	return ! (isHeading || name.isEmpty());
}

ComboBox::ItemInfo* ComboBox::getItemForId (const int itemId) const throw()
{
	if (itemId != 0)
	{
		for (int i = items.size(); --i >= 0;)
			if (items.getUnchecked(i)->itemId == itemId)
				return items.getUnchecked(i);
	}

	return 0;
}

ComboBox::ItemInfo* ComboBox::getItemForIndex (const int index) const throw()
{
	int n = 0;

	for (int i = 0; i < items.size(); ++i)
	{
		ItemInfo* const item = items.getUnchecked(i);

		if (item->isRealItem())
			if (n++ == index)
				return item;
	}

	return 0;
}

int ComboBox::getNumItems() const throw()
{
	int n = 0;

	for (int i = items.size(); --i >= 0;)
		if (items.getUnchecked(i)->isRealItem())
			++n;

	return n;
}

const String ComboBox::getItemText (const int index) const throw()
{
	const ItemInfo* const item = getItemForIndex (index);

	if (item != 0)
		return item->name;

	return String::empty;
}

int ComboBox::getItemId (const int index) const throw()
{
	const ItemInfo* const item = getItemForIndex (index);

	return (item != 0) ? item->itemId : 0;
}

int ComboBox::indexOfItemId (const int itemId) const throw()
{
	int n = 0;

	for (int i = 0; i < items.size(); ++i)
	{
		const ItemInfo* const item = items.getUnchecked(i);

		if (item->isRealItem())
		{
			if (item->itemId == itemId)
				return n;

			++n;
		}
	}

	return -1;
}

int ComboBox::getSelectedItemIndex() const throw()
{
	int index = indexOfItemId (currentId.getValue());

	if (getText() != getItemText (index))
		index = -1;

	return index;
}

void ComboBox::setSelectedItemIndex (const int index,
									 const bool dontSendChangeMessage) throw()
{
	setSelectedId (getItemId (index), dontSendChangeMessage);
}

int ComboBox::getSelectedId() const throw()
{
	const ItemInfo* const item = getItemForId (currentId.getValue());

	return (item != 0 && getText() == item->name)
				? item->itemId
				: 0;
}

void ComboBox::setSelectedId (const int newItemId,
							  const bool dontSendChangeMessage) throw()
{
	const ItemInfo* const item = getItemForId (newItemId);
	const String newItemText (item != 0 ? item->name : String::empty);

	if (lastCurrentId != newItemId || label->getText() != newItemText)
	{
		if (! dontSendChangeMessage)
			triggerAsyncUpdate();

		label->setText (newItemText, false);
		lastCurrentId = newItemId;
		currentId = newItemId;

		repaint();  // for the benefit of the 'none selected' text
	}
}

void ComboBox::valueChanged (Value&)
{
	if (lastCurrentId != (int) currentId.getValue())
		setSelectedId (currentId.getValue(), false);
}

const String ComboBox::getText() const throw()
{
	return label->getText();
}

void ComboBox::setText (const String& newText,
						const bool dontSendChangeMessage) throw()
{
	for (int i = items.size(); --i >= 0;)
	{
		const ItemInfo* const item = items.getUnchecked(i);

		if (item->isRealItem()
			 && item->name == newText)
		{
			setSelectedId (item->itemId, dontSendChangeMessage);
			return;
		}
	}

	lastCurrentId = 0;
	currentId = 0;

	if (label->getText() != newText)
	{
		label->setText (newText, false);

		if (! dontSendChangeMessage)
			triggerAsyncUpdate();
	}

	repaint();
}

void ComboBox::showEditor()
{
	jassert (isTextEditable()); // you probably shouldn't do this to a non-editable combo box?

	label->showEditor();
}

void ComboBox::setTextWhenNothingSelected (const String& newMessage) throw()
{
	textWhenNothingSelected = newMessage;
	repaint();
}

const String ComboBox::getTextWhenNothingSelected() const throw()
{
	return textWhenNothingSelected;
}

void ComboBox::setTextWhenNoChoicesAvailable (const String& newMessage) throw()
{
	noChoicesMessage = newMessage;
}

const String ComboBox::getTextWhenNoChoicesAvailable() const throw()
{
	return noChoicesMessage;
}

void ComboBox::paint (Graphics& g)
{
	getLookAndFeel().drawComboBox (g,
								   getWidth(),
								   getHeight(),
								   isButtonDown,
								   label->getRight(),
								   0,
								   getWidth() - label->getRight(),
								   getHeight(),
								   *this);

	if (textWhenNothingSelected.isNotEmpty()
		&& label->getText().isEmpty()
		&& ! label->isBeingEdited())
	{
		g.setColour (findColour (textColourId).withMultipliedAlpha (0.5f));
		g.setFont (label->getFont());
		g.drawFittedText (textWhenNothingSelected,
						  label->getX() + 2, label->getY() + 1,
						  label->getWidth() - 4, label->getHeight() - 2,
						  label->getJustificationType(),
						  jmax (1, (int) (label->getHeight() / label->getFont().getHeight())));
	}
}

void ComboBox::resized()
{
	if (getHeight() > 0 && getWidth() > 0)
		getLookAndFeel().positionComboBoxText (*this, *label);
}

void ComboBox::enablementChanged()
{
	repaint();
}

void ComboBox::lookAndFeelChanged()
{
	repaint();

	Label* const newLabel = getLookAndFeel().createComboBoxTextBox (*this);

	if (label != 0)
	{
		newLabel->setEditable (label->isEditable());
		newLabel->setJustificationType (label->getJustificationType());
		newLabel->setTooltip (label->getTooltip());
		newLabel->setText (label->getText(), false);
	}

	label = newLabel;

	addAndMakeVisible (newLabel);

	newLabel->addListener (this);
	newLabel->addMouseListener (this, false);

	newLabel->setColour (Label::backgroundColourId, Colours::transparentBlack);
	newLabel->setColour (Label::textColourId, findColour (ComboBox::textColourId));

	newLabel->setColour (TextEditor::textColourId, findColour (ComboBox::textColourId));
	newLabel->setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
	newLabel->setColour (TextEditor::highlightColourId, findColour (TextEditor::highlightColourId));
	newLabel->setColour (TextEditor::outlineColourId, Colours::transparentBlack);

	resized();
}

void ComboBox::colourChanged()
{
	lookAndFeelChanged();
}

bool ComboBox::keyPressed (const KeyPress& key)
{
	bool used = false;

	if (key.isKeyCode (KeyPress::upKey)
		|| key.isKeyCode (KeyPress::leftKey))
	{
		setSelectedItemIndex (jmax (0, getSelectedItemIndex() - 1));
		used = true;
	}
	else if (key.isKeyCode (KeyPress::downKey)
			  || key.isKeyCode (KeyPress::rightKey))
	{
		setSelectedItemIndex (jmin (getSelectedItemIndex() + 1, getNumItems() - 1));
		used = true;
	}
	else if (key.isKeyCode (KeyPress::returnKey))
	{
		showPopup();
		used = true;
	}

	return used;
}

bool ComboBox::keyStateChanged (const bool isKeyDown)
{
	// only forward key events that aren't used by this component
	return isKeyDown
			&& (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::leftKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::rightKey));
}

void ComboBox::focusGained (FocusChangeType)
{
	repaint();
}

void ComboBox::focusLost (FocusChangeType)
{
	repaint();
}

void ComboBox::labelTextChanged (Label*)
{
	triggerAsyncUpdate();
}

void ComboBox::showPopup()
{
	if (! menuActive)
	{
		const int selectedId = getSelectedId();
		Component::SafePointer<Component> deletionWatcher (this);

		PopupMenu menu;

		menu.setLookAndFeel (&getLookAndFeel());

		for (int i = 0; i < items.size(); ++i)
		{
			const ItemInfo* const item = items.getUnchecked(i);

			if (item->isSeparator())
				menu.addSeparator();
			else if (item->isHeading)
				menu.addSectionHeader (item->name);
			else
				menu.addItem (item->itemId, item->name,
							  item->isEnabled, item->itemId == selectedId);
		}

		if (items.size() == 0)
			menu.addItem (1, noChoicesMessage, false);

		const int itemHeight = jlimit (12, 24, getHeight());

		menuActive = true;
		const int resultId = menu.showAt (this, selectedId,
										  getWidth(), 1, itemHeight);

		if (deletionWatcher == 0)
			return;

		menuActive = false;

		if (resultId != 0)
			setSelectedId (resultId);
	}
}

void ComboBox::mouseDown (const MouseEvent& e)
{
	beginDragAutoRepeat (300);

	isButtonDown = isEnabled();

	if (isButtonDown
		 && (e.eventComponent == this || ! label->isEditable()))
	{
		showPopup();
	}
}

void ComboBox::mouseDrag (const MouseEvent& e)
{
	beginDragAutoRepeat (50);

	if (isButtonDown && ! e.mouseWasClicked())
		showPopup();
}

void ComboBox::mouseUp (const MouseEvent& e2)
{
	if (isButtonDown)
	{
		isButtonDown = false;
		repaint();

		const MouseEvent e (e2.getEventRelativeTo (this));

		if (reallyContains (e.x, e.y, true)
			 && (e2.eventComponent == this || ! label->isEditable()))
		{
			showPopup();
		}
	}
}

void ComboBox::addListener (ComboBoxListener* const listener) throw()
{
	listeners.add (listener);
}

void ComboBox::removeListener (ComboBoxListener* const listener) throw()
{
	listeners.remove (listener);
}

void ComboBox::handleAsyncUpdate()
{
	Component::BailOutChecker checker (this);
	listeners.callChecked (checker, &ComboBoxListener::comboBoxChanged, this);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ComboBox.cpp ***/


/*** Start of inlined file: juce_Label.cpp ***/
BEGIN_JUCE_NAMESPACE

Label::Label (const String& componentName,
			  const String& labelText)
	: Component (componentName),
	  textValue (labelText),
	  lastTextValue (labelText),
	  font (15.0f),
	  justification (Justification::centredLeft),
	  ownerComponent (0),
	  horizontalBorderSize (5),
	  verticalBorderSize (1),
	  minimumHorizontalScale (0.7f),
	  editSingleClick (false),
	  editDoubleClick (false),
	  lossOfFocusDiscardsChanges (false)
{
	setColour (TextEditor::textColourId, Colours::black);
	setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
	setColour (TextEditor::outlineColourId, Colours::transparentBlack);

	textValue.addListener (this);
}

Label::~Label()
{
	textValue.removeListener (this);

	if (ownerComponent != 0)
		ownerComponent->removeComponentListener (this);

	editor = 0;
}

void Label::setText (const String& newText,
					 const bool broadcastChangeMessage)
{
	hideEditor (true);

	if (lastTextValue != newText)
	{
		lastTextValue = newText;
		textValue = newText;
		repaint();

		textWasChanged();

		if (ownerComponent != 0)
			componentMovedOrResized (*ownerComponent, true, true);

		if (broadcastChangeMessage)
			callChangeListeners();
	}
}

const String Label::getText (const bool returnActiveEditorContents) const throw()
{
	return (returnActiveEditorContents && isBeingEdited())
				? editor->getText()
				: textValue.toString();
}

void Label::valueChanged (Value&)
{
	if (lastTextValue != textValue.toString())
		setText (textValue.toString(), true);
}

void Label::setFont (const Font& newFont) throw()
{
	font = newFont;
	repaint();
}

const Font& Label::getFont() const throw()
{
	return font;
}

void Label::setEditable (const bool editOnSingleClick,
						 const bool editOnDoubleClick,
						 const bool lossOfFocusDiscardsChanges_) throw()
{
	editSingleClick = editOnSingleClick;
	editDoubleClick = editOnDoubleClick;
	lossOfFocusDiscardsChanges = lossOfFocusDiscardsChanges_;

	setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
	setFocusContainer (editOnSingleClick || editOnDoubleClick);
}

void Label::setJustificationType (const Justification& justification_) throw()
{
	justification = justification_;
	repaint();
}

void Label::setBorderSize (int h, int v)
{
	horizontalBorderSize = h;
	verticalBorderSize = v;
	repaint();
}

Component* Label::getAttachedComponent() const
{
	return static_cast<Component*> (ownerComponent);
}

void Label::attachToComponent (Component* owner,
							   const bool onLeft)
{
	if (ownerComponent != 0)
		ownerComponent->removeComponentListener (this);

	ownerComponent = owner;

	leftOfOwnerComp = onLeft;

	if (ownerComponent != 0)
	{
		setVisible (owner->isVisible());
		ownerComponent->addComponentListener (this);
		componentParentHierarchyChanged (*ownerComponent);
		componentMovedOrResized (*ownerComponent, true, true);
	}
}

void Label::componentMovedOrResized (Component& component,
									 bool /*wasMoved*/,
									 bool /*wasResized*/)
{
	if (leftOfOwnerComp)
	{
		setSize (jmin (getFont().getStringWidth (textValue.toString()) + 8, component.getX()),
				 component.getHeight());

		setTopRightPosition (component.getX(), component.getY());
	}
	else
	{
		setSize (component.getWidth(),
				 8 + roundToInt (getFont().getHeight()));

		setTopLeftPosition (component.getX(), component.getY() - getHeight());
	}
}

void Label::componentParentHierarchyChanged (Component& component)
{
	if (component.getParentComponent() != 0)
		component.getParentComponent()->addChildComponent (this);
}

void Label::componentVisibilityChanged (Component& component)
{
	setVisible (component.isVisible());
}

void Label::textWasEdited()
{
}

void Label::textWasChanged()
{
}

void Label::showEditor()
{
	if (editor == 0)
	{
		addAndMakeVisible (editor = createEditorComponent());
		editor->setText (getText(), false);
		editor->addListener (this);
		editor->grabKeyboardFocus();
		editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
		editor->addListener (this);

		resized();
		repaint();

		editorShown (editor);

		enterModalState (false);
		editor->grabKeyboardFocus();
	}
}

void Label::editorShown (TextEditor* /*editorComponent*/)
{
}

void Label::editorAboutToBeHidden (TextEditor* /*editorComponent*/)
{
}

bool Label::updateFromTextEditorContents()
{
	jassert (editor != 0);
	const String newText (editor->getText());

	if (textValue.toString() != newText)
	{
		lastTextValue = newText;
		textValue = newText;
		repaint();

		textWasChanged();

		if (ownerComponent != 0)
			componentMovedOrResized (*ownerComponent, true, true);

		return true;
	}

	return false;
}

void Label::hideEditor (const bool discardCurrentEditorContents)
{
	if (editor != 0)
	{
		Component::SafePointer<Component> deletionChecker (this);

		editorAboutToBeHidden (editor);

		const bool changed = (! discardCurrentEditorContents)
							   && updateFromTextEditorContents();

		editor = 0;
		repaint();

		if (changed)
			textWasEdited();

		if (deletionChecker != 0)
			exitModalState (0);

		if (changed && deletionChecker != 0)
			callChangeListeners();
	}
}

void Label::inputAttemptWhenModal()
{
	if (editor != 0)
	{
		if (lossOfFocusDiscardsChanges)
			textEditorEscapeKeyPressed (*editor);
		else
			textEditorReturnKeyPressed (*editor);
	}
}

bool Label::isBeingEdited() const throw()
{
	return editor != 0;
}

TextEditor* Label::createEditorComponent()
{
	TextEditor* const ed = new TextEditor (getName());
	ed->setFont (font);

	// copy these colours from our own settings..
	const int cols[] = { TextEditor::backgroundColourId,
						 TextEditor::textColourId,
						 TextEditor::highlightColourId,
						 TextEditor::highlightedTextColourId,
						 TextEditor::caretColourId,
						 TextEditor::outlineColourId,
						 TextEditor::focusedOutlineColourId,
						 TextEditor::shadowColourId };

	for (int i = 0; i < numElementsInArray (cols); ++i)
		ed->setColour (cols[i], findColour (cols[i]));

	return ed;
}

void Label::paint (Graphics& g)
{
	getLookAndFeel().drawLabel (g, *this);
}

void Label::mouseUp (const MouseEvent& e)
{
	if (editSingleClick
		 && e.mouseWasClicked()
		 && contains (e.x, e.y)
		 && ! e.mods.isPopupMenu())
	{
		showEditor();
	}
}

void Label::mouseDoubleClick (const MouseEvent& e)
{
	if (editDoubleClick && ! e.mods.isPopupMenu())
		showEditor();
}

void Label::resized()
{
	if (editor != 0)
		editor->setBoundsInset (BorderSize (0));
}

void Label::focusGained (FocusChangeType cause)
{
	if (editSingleClick && cause == focusChangedByTabKey)
		showEditor();
}

void Label::enablementChanged()
{
	repaint();
}

void Label::colourChanged()
{
	repaint();
}

void Label::setMinimumHorizontalScale (const float newScale)
{
	if (minimumHorizontalScale != newScale)
	{
		minimumHorizontalScale = newScale;
		repaint();
	}
}

// We'll use a custom focus traverser here to make sure focus goes from the
// text editor to another component rather than back to the label itself.
class LabelKeyboardFocusTraverser   : public KeyboardFocusTraverser
{
public:
	LabelKeyboardFocusTraverser() {}

	Component* getNextComponent (Component* current)
	{
		return KeyboardFocusTraverser::getNextComponent (dynamic_cast <TextEditor*> (current) != 0
															? current->getParentComponent() : current);
	}

	Component* getPreviousComponent (Component* current)
	{
		return KeyboardFocusTraverser::getPreviousComponent (dynamic_cast <TextEditor*> (current) != 0
																? current->getParentComponent() : current);
	}
};

KeyboardFocusTraverser* Label::createFocusTraverser()
{
	return new LabelKeyboardFocusTraverser();
}

void Label::addListener (LabelListener* const listener) throw()
{
	listeners.add (listener);
}

void Label::removeListener (LabelListener* const listener) throw()
{
	listeners.remove (listener);
}

void Label::callChangeListeners()
{
	Component::BailOutChecker checker (this);
	listeners.callChecked (checker, &LabelListener::labelTextChanged, this);
}

void Label::textEditorTextChanged (TextEditor& ed)
{
	if (editor != 0)
	{
		jassert (&ed == editor);

		if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
		{
			if (lossOfFocusDiscardsChanges)
				textEditorEscapeKeyPressed (ed);
			else
				textEditorReturnKeyPressed (ed);
		}
	}
}

void Label::textEditorReturnKeyPressed (TextEditor& ed)
{
	if (editor != 0)
	{
		jassert (&ed == editor);
		(void) ed;

		const bool changed = updateFromTextEditorContents();
		hideEditor (true);

		if (changed)
		{
			Component::SafePointer<Component> deletionChecker (this);
			textWasEdited();

			if (deletionChecker != 0)
				callChangeListeners();
		}
	}
}

void Label::textEditorEscapeKeyPressed (TextEditor& ed)
{
	if (editor != 0)
	{
		jassert (&ed == editor);
		(void) ed;

		editor->setText (textValue.toString(), false);
		hideEditor (true);
	}
}

void Label::textEditorFocusLost (TextEditor& ed)
{
	textEditorTextChanged (ed);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Label.cpp ***/


/*** Start of inlined file: juce_ListBox.cpp ***/
BEGIN_JUCE_NAMESPACE

class ListBoxRowComponent  : public Component,
							 public TooltipClient
{
public:
	ListBoxRowComponent (ListBox& owner_)
		: owner (owner_),
		  row (-1),
		  selected (false),
		  isDragging (false)
	{
	}

	~ListBoxRowComponent()
	{
		deleteAllChildren();
	}

	void paint (Graphics& g)
	{
		if (owner.getModel() != 0)
			owner.getModel()->paintListBoxItem (row, g, getWidth(), getHeight(), selected);
	}

	void update (const int row_, const bool selected_)
	{
		if (row != row_ || selected != selected_)
		{
			repaint();
			row = row_;
			selected = selected_;
		}

		if (owner.getModel() != 0)
		{
			Component* const customComp = owner.getModel()->refreshComponentForRow (row_, selected_, getChildComponent (0));

			if (customComp != 0)
			{
				addAndMakeVisible (customComp);
				customComp->setBounds (0, 0, getWidth(), getHeight());

				for (int i = getNumChildComponents(); --i >= 0;)
					if (getChildComponent (i) != customComp)
						delete getChildComponent (i);
			}
			else
			{
				deleteAllChildren();
			}
		}
	}

	void mouseDown (const MouseEvent& e)
	{
		isDragging = false;
		selectRowOnMouseUp = false;

		if (isEnabled())
		{
			if (! selected)
			{
				owner.selectRowsBasedOnModifierKeys (row, e.mods);

				if (owner.getModel() != 0)
					owner.getModel()->listBoxItemClicked (row, e);
			}
			else
			{
				selectRowOnMouseUp = true;
			}
		}
	}

	void mouseUp (const MouseEvent& e)
	{
		if (isEnabled() && selectRowOnMouseUp && ! isDragging)
		{
			owner.selectRowsBasedOnModifierKeys (row, e.mods);

			if (owner.getModel() != 0)
				owner.getModel()->listBoxItemClicked (row, e);
		}
	}

	void mouseDoubleClick (const MouseEvent& e)
	{
		if (owner.getModel() != 0 && isEnabled())
			owner.getModel()->listBoxItemDoubleClicked (row, e);
	}

	void mouseDrag (const MouseEvent& e)
	{
		if (isEnabled() && owner.getModel() != 0 && ! (e.mouseWasClicked() || isDragging))
		{
			const SparseSet <int> selectedRows (owner.getSelectedRows());

			if (selectedRows.size() > 0)
			{
				const String dragDescription (owner.getModel()->getDragSourceDescription (selectedRows));

				if (dragDescription.isNotEmpty())
				{
					isDragging = true;
					owner.startDragAndDrop (e, dragDescription);
				}
			}
		}
	}

	void resized()
	{
		if (getNumChildComponents() > 0)
			getChildComponent(0)->setBounds (0, 0, getWidth(), getHeight());
	}

	const String getTooltip()
	{
		if (owner.getModel() != 0)
			return owner.getModel()->getTooltipForRow (row);

		return String::empty;
	}

	juce_UseDebuggingNewOperator

	bool neededFlag;

private:
	ListBox& owner;
	int row;
	bool selected, isDragging, selectRowOnMouseUp;

	ListBoxRowComponent (const ListBoxRowComponent&);
	ListBoxRowComponent& operator= (const ListBoxRowComponent&);
};

class ListViewport  : public Viewport
{
public:
	int firstIndex, firstWholeIndex, lastWholeIndex;
	bool hasUpdated;

	ListViewport (ListBox& owner_)
		: owner (owner_)
	{
		setWantsKeyboardFocus (false);

		setViewedComponent (new Component());
		getViewedComponent()->addMouseListener (this, false);
		getViewedComponent()->setWantsKeyboardFocus (false);
	}

	~ListViewport()
	{
		getViewedComponent()->removeMouseListener (this);
		getViewedComponent()->deleteAllChildren();
	}

	ListBoxRowComponent* getComponentForRow (const int row) const throw()
	{
		return static_cast <ListBoxRowComponent*>
				(getViewedComponent()->getChildComponent (row % jmax (1, getViewedComponent()->getNumChildComponents())));
	}

	int getRowNumberOfComponent (Component* const rowComponent) const throw()
	{
		const int index = getIndexOfChildComponent (rowComponent);
		const int num = getViewedComponent()->getNumChildComponents();

		for (int i = num; --i >= 0;)
			if (((firstIndex + i) % jmax (1, num)) == index)
				return firstIndex + i;

		return -1;
	}

	Component* getComponentForRowIfOnscreen (const int row) const throw()
	{
		return (row >= firstIndex && row < firstIndex + getViewedComponent()->getNumChildComponents())
				 ? getComponentForRow (row) : 0;
	}

	void visibleAreaChanged (int, int, int, int)
	{
		updateVisibleArea (true);

		if (owner.getModel() != 0)
			owner.getModel()->listWasScrolled();
	}

	void updateVisibleArea (const bool makeSureItUpdatesContent)
	{
		hasUpdated = false;

		const int newX = getViewedComponent()->getX();
		int newY = getViewedComponent()->getY();
		const int newW = jmax (owner.minimumRowWidth, getMaximumVisibleWidth());
		const int newH = owner.totalItems * owner.getRowHeight();

		if (newY + newH < getMaximumVisibleHeight() && newH > getMaximumVisibleHeight())
			newY = getMaximumVisibleHeight() - newH;

		getViewedComponent()->setBounds (newX, newY, newW, newH);

		if (makeSureItUpdatesContent && ! hasUpdated)
			updateContents();
	}

	void updateContents()
	{
		hasUpdated = true;
		const int rowHeight = owner.getRowHeight();

		if (rowHeight > 0)
		{
			const int y = getViewPositionY();
			const int w = getViewedComponent()->getWidth();

			const int numNeeded = 2 + getMaximumVisibleHeight() / rowHeight;

			while (numNeeded > getViewedComponent()->getNumChildComponents())
				getViewedComponent()->addAndMakeVisible (new ListBoxRowComponent (owner));

			jassert (numNeeded >= 0);

			while (numNeeded < getViewedComponent()->getNumChildComponents())
			{
				Component* const rowToRemove
					= getViewedComponent()->getChildComponent (getViewedComponent()->getNumChildComponents() - 1);

				delete rowToRemove;
			}

			firstIndex = y / rowHeight;
			firstWholeIndex = (y + rowHeight - 1) / rowHeight;
			lastWholeIndex = (y + getMaximumVisibleHeight() - 1) / rowHeight;

			for (int i = 0; i < numNeeded; ++i)
			{
				const int row = i + firstIndex;
				ListBoxRowComponent* const rowComp = getComponentForRow (row);

				if (rowComp != 0)
				{
					rowComp->setBounds (0, row * rowHeight, w, rowHeight);
					rowComp->update (row, owner.isRowSelected (row));
				}
			}
		}

		if (owner.headerComponent != 0)
			owner.headerComponent->setBounds (owner.outlineThickness + getViewedComponent()->getX(),
											  owner.outlineThickness,
											  jmax (owner.getWidth() - owner.outlineThickness * 2,
													getViewedComponent()->getWidth()),
											  owner.headerComponent->getHeight());
	}

	void paint (Graphics& g)
	{
		if (isOpaque())
			g.fillAll (owner.findColour (ListBox::backgroundColourId));
	}

	bool keyPressed (const KeyPress& key)
	{
		if (key.isKeyCode (KeyPress::upKey)
			|| key.isKeyCode (KeyPress::downKey)
			|| key.isKeyCode (KeyPress::pageUpKey)
			|| key.isKeyCode (KeyPress::pageDownKey)
			|| key.isKeyCode (KeyPress::homeKey)
			|| key.isKeyCode (KeyPress::endKey))
		{
			// we want to avoid these keypresses going to the viewport, and instead allow
			// them to pass up to our listbox..
			return false;
		}

		return Viewport::keyPressed (key);
	}

	juce_UseDebuggingNewOperator

private:
	ListBox& owner;

	ListViewport (const ListViewport&);
	ListViewport& operator= (const ListViewport&);
};

ListBox::ListBox (const String& name, ListBoxModel* const model_)
	: Component (name),
	  model (model_),
	  headerComponent (0),
	  totalItems (0),
	  rowHeight (22),
	  minimumRowWidth (0),
	  outlineThickness (0),
	  lastRowSelected (-1),
	  mouseMoveSelects (false),
	  multipleSelection (false),
	  hasDoneInitialUpdate (false)
{
	addAndMakeVisible (viewport = new ListViewport (*this));

	setWantsKeyboardFocus (true);
	colourChanged();
}

ListBox::~ListBox()
{
	deleteAllChildren();
}

void ListBox::setModel (ListBoxModel* const newModel)
{
	if (model != newModel)
	{
		model = newModel;
		updateContent();
	}
}

void ListBox::setMultipleSelectionEnabled (bool b)
{
	multipleSelection = b;
}

void ListBox::setMouseMoveSelectsRows (bool b)
{
	mouseMoveSelects = b;

	if (b)
		addMouseListener (this, true);
}

void ListBox::paint (Graphics& g)
{
	if (! hasDoneInitialUpdate)
		updateContent();

	g.fillAll (findColour (backgroundColourId));
}

void ListBox::paintOverChildren (Graphics& g)
{
	if (outlineThickness > 0)
	{
		g.setColour (findColour (outlineColourId));
		g.drawRect (0, 0, getWidth(), getHeight(), outlineThickness);
	}
}

void ListBox::resized()
{
	viewport->setBoundsInset (BorderSize (outlineThickness + ((headerComponent != 0) ? headerComponent->getHeight() : 0),
										  outlineThickness,
										  outlineThickness,
										  outlineThickness));

	viewport->setSingleStepSizes (20, getRowHeight());

	viewport->updateVisibleArea (false);
}

void ListBox::visibilityChanged()
{
	viewport->updateVisibleArea (true);
}

Viewport* ListBox::getViewport() const throw()
{
	return viewport;
}

void ListBox::updateContent()
{
	hasDoneInitialUpdate = true;
	totalItems = (model != 0) ? model->getNumRows() : 0;

	bool selectionChanged = false;

	if (selected [selected.size() - 1] >= totalItems)
	{
		selected.removeRange (totalItems, std::numeric_limits<int>::max() - totalItems);
		lastRowSelected = getSelectedRow (0);
		selectionChanged = true;
	}

	viewport->updateVisibleArea (isVisible());
	viewport->resized();

	if (selectionChanged && model != 0)
		model->selectedRowsChanged (lastRowSelected);
}

void ListBox::selectRow (const int row,
						 bool dontScroll,
						 bool deselectOthersFirst)
{
	selectRowInternal (row, dontScroll, deselectOthersFirst, false);
}

void ListBox::selectRowInternal (const int row,
								 bool dontScroll,
								 bool deselectOthersFirst,
								 bool isMouseClick)
{
	if (! multipleSelection)
		deselectOthersFirst = true;

	if ((! isRowSelected (row))
		 || (deselectOthersFirst && getNumSelectedRows() > 1))
	{
		if (((unsigned int) row) < (unsigned int) totalItems)
		{
			if (deselectOthersFirst)
				selected.clear();

			selected.addRange (row, 1);

			if (getHeight() == 0 || getWidth() == 0)
				dontScroll = true;

			viewport->hasUpdated = false;

			if (row < viewport->firstWholeIndex && ! dontScroll)
			{
				viewport->setViewPosition (viewport->getViewPositionX(),
										   row * getRowHeight());
			}
			else if (row >= viewport->lastWholeIndex && ! dontScroll)
			{
				const int rowsOnScreen = viewport->lastWholeIndex - viewport->firstWholeIndex;

				if (row >= lastRowSelected + rowsOnScreen
					 && rowsOnScreen < totalItems - 1
					 && ! isMouseClick)
				{
					viewport->setViewPosition (viewport->getViewPositionX(),
											   jlimit (0, jmax (0, totalItems - rowsOnScreen), row)
												   * getRowHeight());
				}
				else
				{
					viewport->setViewPosition (viewport->getViewPositionX(),
											   jmax (0, (row  + 1) * getRowHeight() - viewport->getMaximumVisibleHeight()));
				}
			}

			if (! viewport->hasUpdated)
				viewport->updateContents();

			lastRowSelected = row;
			model->selectedRowsChanged (row);
		}
		else
		{
			if (deselectOthersFirst)
				deselectAllRows();
		}
	}
}

void ListBox::deselectRow (const int row)
{
	if (selected.contains (row))
	{
		selected.removeRange (row, 1);

		if (row == lastRowSelected)
			lastRowSelected = getSelectedRow (0);

		viewport->updateContents();
		model->selectedRowsChanged (lastRowSelected);
	}
}

void ListBox::setSelectedRows (const SparseSet<int>& setOfRowsToBeSelected,
							   const bool sendNotificationEventToModel)
{
	selected = setOfRowsToBeSelected;
	selected.removeRange (totalItems, std::numeric_limits<int>::max() - totalItems);

	if (! isRowSelected (lastRowSelected))
		lastRowSelected = getSelectedRow (0);

	viewport->updateContents();

	if ((model != 0) && sendNotificationEventToModel)
		model->selectedRowsChanged (lastRowSelected);
}

const SparseSet<int> ListBox::getSelectedRows() const
{
	return selected;
}

void ListBox::selectRangeOfRows (int firstRow, int lastRow)
{
	if (multipleSelection && (firstRow != lastRow))
	{
		const int numRows = totalItems - 1;
		firstRow = jlimit (0, jmax (0, numRows), firstRow);
		lastRow = jlimit (0, jmax (0, numRows), lastRow);

		selected.addRange (jmin (firstRow, lastRow),
						   abs (firstRow - lastRow) + 1);

		selected.removeRange (lastRow, 1);
	}

	selectRowInternal (lastRow, false, false, true);
}

void ListBox::flipRowSelection (const int row)
{
	if (isRowSelected (row))
		deselectRow (row);
	else
		selectRowInternal (row, false, false, true);
}

void ListBox::deselectAllRows()
{
	if (! selected.isEmpty())
	{
		selected.clear();
		lastRowSelected = -1;

		viewport->updateContents();

		if (model != 0)
			model->selectedRowsChanged (lastRowSelected);
	}
}

void ListBox::selectRowsBasedOnModifierKeys (const int row,
											 const ModifierKeys& mods)
{
	if (multipleSelection && mods.isCommandDown())
	{
		flipRowSelection (row);
	}
	else if (multipleSelection && mods.isShiftDown() && lastRowSelected >= 0)
	{
		selectRangeOfRows (lastRowSelected, row);
	}
	else if ((! mods.isPopupMenu()) || ! isRowSelected (row))
	{
		selectRowInternal (row, false, true, true);
	}
}

int ListBox::getNumSelectedRows() const
{
	return selected.size();
}

int ListBox::getSelectedRow (const int index) const
{
	return (((unsigned int) index) < (unsigned int) selected.size())
				? selected [index] : -1;
}

bool ListBox::isRowSelected (const int row) const
{
	return selected.contains (row);
}

int ListBox::getLastRowSelected() const
{
	return (isRowSelected (lastRowSelected)) ? lastRowSelected : -1;
}

int ListBox::getRowContainingPosition (const int x, const int y) const throw()
{
	if (((unsigned int) x) < (unsigned int) getWidth())
	{
		const int row = (viewport->getViewPositionY() + y - viewport->getY()) / rowHeight;

		if (((unsigned int) row) < (unsigned int) totalItems)
			return row;
	}

	return -1;
}

int ListBox::getInsertionIndexForPosition (const int x, const int y) const throw()
{
	if (((unsigned int) x) < (unsigned int) getWidth())
	{
		const int row = (viewport->getViewPositionY() + y + rowHeight / 2 - viewport->getY()) / rowHeight;
		return jlimit (0, totalItems, row);
	}

	return -1;
}

Component* ListBox::getComponentForRowNumber (const int row) const throw()
{
	Component* const listRowComp = viewport->getComponentForRowIfOnscreen (row);
	return listRowComp != 0 ? listRowComp->getChildComponent (0) : 0;
}

int ListBox::getRowNumberOfComponent (Component* const rowComponent) const throw()
{
	return viewport->getRowNumberOfComponent (rowComponent);
}

const Rectangle<int> ListBox::getRowPosition (const int rowNumber,
											  const bool relativeToComponentTopLeft) const throw()
{
	const int rowHeight = getRowHeight();
	int y = viewport->getY() + rowHeight * rowNumber;

	if (relativeToComponentTopLeft)
		y -= viewport->getViewPositionY();

	return Rectangle<int> (viewport->getX(), y,
						   viewport->getViewedComponent()->getWidth(), rowHeight);
}

void ListBox::setVerticalPosition (const double proportion)
{
	const int offscreen = viewport->getViewedComponent()->getHeight() - viewport->getHeight();

	viewport->setViewPosition (viewport->getViewPositionX(),
							   jmax (0, roundToInt (proportion * offscreen)));
}

double ListBox::getVerticalPosition() const
{
	const int offscreen = viewport->getViewedComponent()->getHeight() - viewport->getHeight();

	return (offscreen > 0) ? viewport->getViewPositionY() / (double) offscreen
						   : 0;
}

int ListBox::getVisibleRowWidth() const throw()
{
	return viewport->getViewWidth();
}

void ListBox::scrollToEnsureRowIsOnscreen (const int row)
{
	if (row < viewport->firstWholeIndex)
	{
		viewport->setViewPosition (viewport->getViewPositionX(),
								   row * getRowHeight());
	}
	else if (row >= viewport->lastWholeIndex)
	{
		viewport->setViewPosition (viewport->getViewPositionX(),
								   jmax (0, (row  + 1) * getRowHeight() - viewport->getMaximumVisibleHeight()));
	}
}

bool ListBox::keyPressed (const KeyPress& key)
{
	const int numVisibleRows = viewport->getHeight() / getRowHeight();

	const bool multiple = multipleSelection
							&& (lastRowSelected >= 0)
							&& (key.getModifiers().isShiftDown()
								 || key.getModifiers().isCtrlDown()
								 || key.getModifiers().isCommandDown());

	if (key.isKeyCode (KeyPress::upKey))
	{
		if (multiple)
			selectRangeOfRows (lastRowSelected, lastRowSelected - 1);
		else
			selectRow (jmax (0, lastRowSelected - 1));
	}
	else if (key.isKeyCode (KeyPress::returnKey)
			  && isRowSelected (lastRowSelected))
	{
		if (model != 0)
			model->returnKeyPressed (lastRowSelected);
	}
	else if (key.isKeyCode (KeyPress::pageUpKey))
	{
		if (multiple)
			selectRangeOfRows (lastRowSelected, lastRowSelected - numVisibleRows);
		else
			selectRow (jmax (0, jmax (0, lastRowSelected) - numVisibleRows));
	}
	else if (key.isKeyCode (KeyPress::pageDownKey))
	{
		if (multiple)
			selectRangeOfRows (lastRowSelected, lastRowSelected + numVisibleRows);
		else
			selectRow (jmin (totalItems - 1, jmax (0, lastRowSelected) + numVisibleRows));
	}
	else if (key.isKeyCode (KeyPress::homeKey))
	{
		if (multiple && key.getModifiers().isShiftDown())
			selectRangeOfRows (lastRowSelected, 0);
		else
			selectRow (0);
	}
	else if (key.isKeyCode (KeyPress::endKey))
	{
		if (multiple && key.getModifiers().isShiftDown())
			selectRangeOfRows (lastRowSelected, totalItems - 1);
		else
			selectRow (totalItems - 1);
	}
	else if (key.isKeyCode (KeyPress::downKey))
	{
		if (multiple)
			selectRangeOfRows (lastRowSelected, lastRowSelected + 1);
		else
			selectRow (jmin (totalItems - 1, jmax (0, lastRowSelected) + 1));
	}
	else if ((key.isKeyCode (KeyPress::deleteKey) || key.isKeyCode (KeyPress::backspaceKey))
			   && isRowSelected (lastRowSelected))
	{
		if (model != 0)
			model->deleteKeyPressed (lastRowSelected);
	}
	else if (multiple && key == KeyPress ('a', ModifierKeys::commandModifier, 0))
	{
		selectRangeOfRows (0, std::numeric_limits<int>::max());
	}
	else
	{
		return false;
	}

	return true;
}

bool ListBox::keyStateChanged (const bool isKeyDown)
{
	return isKeyDown
			&& (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::pageUpKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::pageDownKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::homeKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::endKey)
				|| KeyPress::isKeyCurrentlyDown (KeyPress::returnKey));
}

void ListBox::mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
	getHorizontalScrollBar()->mouseWheelMove (e, wheelIncrementX, 0);
	getVerticalScrollBar()->mouseWheelMove (e, 0, wheelIncrementY);
}

void ListBox::mouseMove (const MouseEvent& e)
{
	if (mouseMoveSelects)
	{
		const MouseEvent e2 (e.getEventRelativeTo (this));
		selectRow (getRowContainingPosition (e2.x, e2.y), true);
	}
}

void ListBox::mouseExit (const MouseEvent& e)
{
	mouseMove (e);
}

void ListBox::mouseUp (const MouseEvent& e)
{
	if (e.mouseWasClicked() && model != 0)
		model->backgroundClicked();
}

void ListBox::setRowHeight (const int newHeight)
{
	rowHeight = jmax (1, newHeight);
	viewport->setSingleStepSizes (20, rowHeight);
	updateContent();
}

int ListBox::getNumRowsOnScreen() const throw()
{
	return viewport->getMaximumVisibleHeight() / rowHeight;
}

void ListBox::setMinimumContentWidth (const int newMinimumWidth)
{
	minimumRowWidth = newMinimumWidth;
	updateContent();
}

int ListBox::getVisibleContentWidth() const throw()
{
	return viewport->getMaximumVisibleWidth();
}

ScrollBar* ListBox::getVerticalScrollBar() const throw()
{
	return viewport->getVerticalScrollBar();
}

ScrollBar* ListBox::getHorizontalScrollBar() const throw()
{
	return viewport->getHorizontalScrollBar();
}

void ListBox::colourChanged()
{
	setOpaque (findColour (backgroundColourId).isOpaque());
	viewport->setOpaque (isOpaque());
	repaint();
}

void ListBox::setOutlineThickness (const int outlineThickness_)
{
	outlineThickness = outlineThickness_;
	resized();
}

void ListBox::setHeaderComponent (Component* const newHeaderComponent)
{
	if (headerComponent != newHeaderComponent)
	{
		delete headerComponent;
		headerComponent = newHeaderComponent;

		addAndMakeVisible (newHeaderComponent);
		ListBox::resized();
	}
}

void ListBox::repaintRow (const int rowNumber) throw()
{
	const Rectangle<int> r (getRowPosition (rowNumber, true));
	repaint (r.getX(), r.getY(), r.getWidth(), r.getHeight());
}

Image* ListBox::createSnapshotOfSelectedRows (int& imageX, int& imageY)
{
	Rectangle<int> imageArea;
	const int firstRow = getRowContainingPosition (0, 0);

	int i;
	for (i = getNumRowsOnScreen() + 2; --i >= 0;)
	{
		Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i);

		if (rowComp != 0 && isRowSelected (firstRow + i))
		{
			const Point<int> pos (rowComp->relativePositionToOtherComponent (this, Point<int>()));
			const Rectangle<int> rowRect (pos.getX(), pos.getY(), rowComp->getWidth(), rowComp->getHeight());

			if (imageArea.isEmpty())
				imageArea = rowRect;
			else
				imageArea = imageArea.getUnion (rowRect);
		}
	}

	imageArea = imageArea.getIntersection (getLocalBounds());
	imageX = imageArea.getX();
	imageY = imageArea.getY();
	Image* snapshot = Image::createNativeImage (Image::ARGB, imageArea.getWidth(), imageArea.getHeight(), true);

	for (i = getNumRowsOnScreen() + 2; --i >= 0;)
	{
		Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i);

		if (rowComp != 0 && isRowSelected (firstRow + i))
		{
			const Point<int> pos (rowComp->relativePositionToOtherComponent (this, Point<int>()));

			Graphics g (*snapshot);
			g.setOrigin (pos.getX() - imageX, pos.getY() - imageY);
			if (g.reduceClipRegion (0, 0, rowComp->getWidth(), rowComp->getHeight()))
				rowComp->paintEntireComponent (g);
		}
	}

	return snapshot;
}

void ListBox::startDragAndDrop (const MouseEvent& e, const String& dragDescription)
{
	DragAndDropContainer* const dragContainer
		= DragAndDropContainer::findParentDragContainerFor (this);

	if (dragContainer != 0)
	{
		int x, y;
		Image* dragImage = createSnapshotOfSelectedRows (x, y);
		dragImage->multiplyAllAlphas (0.6f);

		MouseEvent e2 (e.getEventRelativeTo (this));
		const Point<int> p (x - e2.x, y - e2.y);
		dragContainer->startDragging (dragDescription, this, dragImage, true, &p);
	}
	else
	{
		// to be able to do a drag-and-drop operation, the listbox needs to
		// be inside a component which is also a DragAndDropContainer.
		jassertfalse
	}
}

Component* ListBoxModel::refreshComponentForRow (int, bool, Component* existingComponentToUpdate)
{
	(void) existingComponentToUpdate;
	jassert (existingComponentToUpdate == 0); // indicates a failure in the code the recycles the components
	return 0;
}

void ListBoxModel::listBoxItemClicked (int, const MouseEvent&)
{
}

void ListBoxModel::listBoxItemDoubleClicked (int, const MouseEvent&)
{
}

void ListBoxModel::backgroundClicked()
{
}

void ListBoxModel::selectedRowsChanged (int)
{
}

void ListBoxModel::deleteKeyPressed (int)
{
}

void ListBoxModel::returnKeyPressed (int)
{
}

void ListBoxModel::listWasScrolled()
{
}

const String ListBoxModel::getDragSourceDescription (const SparseSet<int>&)
{
	return String::empty;
}

const String ListBoxModel::getTooltipForRow (int)
{
	return String::empty;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ListBox.cpp ***/


/*** Start of inlined file: juce_ProgressBar.cpp ***/
BEGIN_JUCE_NAMESPACE

ProgressBar::ProgressBar (double& progress_)
   : progress (progress_),
	 displayPercentage (true),
	 lastCallbackTime (0)
{
	currentValue = jlimit (0.0, 1.0, progress);
}

ProgressBar::~ProgressBar()
{
}

void ProgressBar::setPercentageDisplay (const bool shouldDisplayPercentage)
{
	displayPercentage = shouldDisplayPercentage;
	repaint();
}

void ProgressBar::setTextToDisplay (const String& text)
{
	displayPercentage = false;
	displayedMessage = text;
}

void ProgressBar::lookAndFeelChanged()
{
	setOpaque (findColour (backgroundColourId).isOpaque());
}

void ProgressBar::colourChanged()
{
	lookAndFeelChanged();
}

void ProgressBar::paint (Graphics& g)
{
	String text;

	if (displayPercentage)
	{
		if (currentValue >= 0 && currentValue <= 1.0)
			text << roundToInt (currentValue * 100.0) << '%';
	}
	else
	{
		text = displayedMessage;
	}

	getLookAndFeel().drawProgressBar (g, *this,
									  getWidth(), getHeight(),
									  currentValue, text);
}

void ProgressBar::visibilityChanged()
{
	if (isVisible())
		startTimer (30);
	else
		stopTimer();
}

void ProgressBar::timerCallback()
{
	double newProgress = progress;

	const uint32 now = Time::getMillisecondCounter();
	const int timeSinceLastCallback = (int) (now - lastCallbackTime);
	lastCallbackTime = now;

	if (currentValue != newProgress
		 || newProgress < 0 || newProgress >= 1.0
		 || currentMessage != displayedMessage)
	{
		if (currentValue < newProgress
			 && newProgress >= 0 && newProgress < 1.0
			 && currentValue >= 0 && currentValue < 1.0)
		{
			newProgress = jmin (currentValue + 0.0008 * timeSinceLastCallback,
								newProgress);
		}

		currentValue = newProgress;
		currentMessage = displayedMessage;
		repaint();
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ProgressBar.cpp ***/


/*** Start of inlined file: juce_Slider.cpp ***/
BEGIN_JUCE_NAMESPACE

class SliderPopupDisplayComponent  : public BubbleComponent
{
public:

	SliderPopupDisplayComponent (Slider* const owner_)
		: owner (owner_),
		  font (15.0f, Font::bold)
	{
		setAlwaysOnTop (true);
	}

	~SliderPopupDisplayComponent()
	{
	}

	void paintContent (Graphics& g, int w, int h)
	{
		g.setFont (font);
		g.setColour (Colours::black);

		g.drawFittedText (text, 0, 0, w, h, Justification::centred, 1);
	}

	void getContentSize (int& w, int& h)
	{
		w = font.getStringWidth (text) + 18;
		h = (int) (font.getHeight() * 1.6f);
	}

	void updatePosition (const String& newText)
	{
		if (text != newText)
		{
			text = newText;
			repaint();
		}

		BubbleComponent::setPosition (owner);
	}

	juce_UseDebuggingNewOperator

private:
	Slider* owner;
	Font font;
	String text;

	SliderPopupDisplayComponent (const SliderPopupDisplayComponent&);
	SliderPopupDisplayComponent& operator= (const SliderPopupDisplayComponent&);
};

Slider::Slider (const String& name)
  : Component (name),
	lastCurrentValue (0),
	lastValueMin (0),
	lastValueMax (0),
	minimum (0),
	maximum (10),
	interval (0),
	skewFactor (1.0),
	velocityModeSensitivity (1.0),
	velocityModeOffset (0.0),
	velocityModeThreshold (1),
	rotaryStart (float_Pi * 1.2f),
	rotaryEnd (float_Pi * 2.8f),
	numDecimalPlaces (7),
	sliderRegionStart (0),
	sliderRegionSize (1),
	sliderBeingDragged (-1),
	pixelsForFullDragExtent (250),
	style (LinearHorizontal),
	textBoxPos (TextBoxLeft),
	textBoxWidth (80),
	textBoxHeight (20),
	incDecButtonMode (incDecButtonsNotDraggable),
	editableText (true),
	doubleClickToValue (false),
	isVelocityBased (false),
	userKeyOverridesVelocity (true),
	rotaryStop (true),
	incDecButtonsSideBySide (false),
	sendChangeOnlyOnRelease (false),
	popupDisplayEnabled (false),
	menuEnabled (false),
	menuShown (false),
	scrollWheelEnabled (true),
	snapsToMousePos (true),
	valueBox (0),
	incButton (0),
	decButton (0),
	popupDisplay (0),
	parentForPopupDisplay (0)
{
	setWantsKeyboardFocus (false);
	setRepaintsOnMouseActivity (true);

	lookAndFeelChanged();
	updateText();

	currentValue.addListener (this);
	valueMin.addListener (this);
	valueMax.addListener (this);
}

Slider::~Slider()
{
	currentValue.removeListener (this);
	valueMin.removeListener (this);
	valueMax.removeListener (this);
	popupDisplay = 0;
	deleteAllChildren();
}

void Slider::handleAsyncUpdate()
{
	cancelPendingUpdate();

	Component::BailOutChecker checker (this);
	listeners.callChecked (checker, &SliderListener::sliderValueChanged, this);
}

void Slider::sendDragStart()
{
	startedDragging();

	Component::BailOutChecker checker (this);
	listeners.callChecked (checker, &SliderListener::sliderDragStarted, this);
}

void Slider::sendDragEnd()
{
	stoppedDragging();

	sliderBeingDragged = -1;

	Component::BailOutChecker checker (this);
	listeners.callChecked (checker, &SliderListener::sliderDragEnded, this);
}

void Slider::addListener (SliderListener* const listener)
{
	listeners.add (listener);
}

void Slider::removeListener (SliderListener* const listener)
{
	listeners.remove (listener);
}

void Slider::setSliderStyle (const SliderStyle newStyle)
{
	if (style != newStyle)
	{
		style = newStyle;
		repaint();
		lookAndFeelChanged();
	}
}

void Slider::setRotaryParameters (const float startAngleRadians,
								  const float endAngleRadians,
								  const bool stopAtEnd)
{
	// make sure the values are sensible..
	jassert (rotaryStart >= 0 && rotaryEnd >= 0);
	jassert (rotaryStart < float_Pi * 4.0f && rotaryEnd < float_Pi * 4.0f);
	jassert (rotaryStart < rotaryEnd);

	rotaryStart = startAngleRadians;
	rotaryEnd = endAngleRadians;
	rotaryStop = stopAtEnd;
}

void Slider::setVelocityBasedMode (const bool velBased)
{
	isVelocityBased = velBased;
}

void Slider::setVelocityModeParameters (const double sensitivity,
										const int threshold,
										const double offset,
										const bool userCanPressKeyToSwapMode)
{
	jassert (threshold >= 0);
	jassert (sensitivity > 0);
	jassert (offset >= 0);

	velocityModeSensitivity = sensitivity;
	velocityModeOffset = offset;
	velocityModeThreshold = threshold;
	userKeyOverridesVelocity = userCanPressKeyToSwapMode;
}

void Slider::setSkewFactor (const double factor)
{
	skewFactor = factor;
}

void Slider::setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint)
{
	if (maximum > minimum)
		skewFactor = log (0.5) / log ((sliderValueToShowAtMidPoint - minimum)
										/ (maximum - minimum));
}

void Slider::setMouseDragSensitivity (const int distanceForFullScaleDrag)
{
	jassert (distanceForFullScaleDrag > 0);

	pixelsForFullDragExtent = distanceForFullScaleDrag;
}

void Slider::setIncDecButtonsMode (const IncDecButtonMode mode)
{
	if (incDecButtonMode != mode)
	{
		incDecButtonMode = mode;
		lookAndFeelChanged();
	}
}

void Slider::setTextBoxStyle (const TextEntryBoxPosition newPosition,
							  const bool isReadOnly,
							  const int textEntryBoxWidth,
							  const int textEntryBoxHeight)
{
	textBoxPos = newPosition;
	editableText = ! isReadOnly;
	textBoxWidth = textEntryBoxWidth;
	textBoxHeight = textEntryBoxHeight;

	repaint();
	lookAndFeelChanged();
}

void Slider::setTextBoxIsEditable (const bool shouldBeEditable)
{
	editableText = shouldBeEditable;

	if (valueBox != 0)
		valueBox->setEditable (shouldBeEditable && isEnabled());
}

void Slider::showTextBox()
{
	jassert (editableText); // this should probably be avoided in read-only sliders.

	if (valueBox != 0)
		valueBox->showEditor();
}

void Slider::hideTextBox (const bool discardCurrentEditorContents)
{
	if (valueBox != 0)
	{
		valueBox->hideEditor (discardCurrentEditorContents);

		if (discardCurrentEditorContents)
			updateText();
	}
}

void Slider::setChangeNotificationOnlyOnRelease (const bool onlyNotifyOnRelease)
{
	sendChangeOnlyOnRelease = onlyNotifyOnRelease;
}

void Slider::setSliderSnapsToMousePosition (const bool shouldSnapToMouse)
{
	snapsToMousePos = shouldSnapToMouse;
}

void Slider::setPopupDisplayEnabled (const bool enabled,
									 Component* const parentComponentToUse)
{
	popupDisplayEnabled = enabled;
	parentForPopupDisplay = parentComponentToUse;
}

void Slider::colourChanged()
{
	lookAndFeelChanged();
}

void Slider::lookAndFeelChanged()
{
	const String previousTextBoxContent (valueBox != 0 ? valueBox->getText()
													   : getTextFromValue (currentValue.getValue()));

	deleteAllChildren();
	valueBox = 0;

	LookAndFeel& lf = getLookAndFeel();

	if (textBoxPos != NoTextBox)
	{
		addAndMakeVisible (valueBox = getLookAndFeel().createSliderTextBox (*this));

		valueBox->setWantsKeyboardFocus (false);
		valueBox->setText (previousTextBoxContent, false);

		valueBox->setEditable (editableText && isEnabled());
		valueBox->addListener (this);

		if (style == LinearBar)
			valueBox->addMouseListener (this, false);

		valueBox->setTooltip (getTooltip());
	}

	if (style == IncDecButtons)
	{
		addAndMakeVisible (incButton = lf.createSliderButton (true));
		incButton->addButtonListener (this);

		addAndMakeVisible (decButton = lf.createSliderButton (false));
		decButton->addButtonListener (this);

		if (incDecButtonMode != incDecButtonsNotDraggable)
		{
			incButton->addMouseListener (this, false);
			decButton->addMouseListener (this, false);
		}
		else
		{
			incButton->setRepeatSpeed (300, 100, 20);
			incButton->addMouseListener (decButton, false);

			decButton->setRepeatSpeed (300, 100, 20);
			decButton->addMouseListener (incButton, false);
		}

		incButton->setTooltip (getTooltip());
		decButton->setTooltip (getTooltip());
	}

	setComponentEffect (lf.getSliderEffect());

	resized();
	repaint();
}

void Slider::setRange (const double newMin,
					   const double newMax,
					   const double newInt)
{
	if (minimum != newMin
		|| maximum != newMax
		|| interval != newInt)
	{
		minimum = newMin;
		maximum = newMax;
		interval = newInt;

		// figure out the number of DPs needed to display all values at this
		// interval setting.
		numDecimalPlaces = 7;

		if (newInt != 0)
		{
			int v = abs ((int) (newInt * 10000000));

			while ((v % 10) == 0)
			{
				--numDecimalPlaces;
				v /= 10;
			}
		}

		// keep the current values inside the new range..
		if (style != TwoValueHorizontal && style != TwoValueVertical)
		{
			setValue (getValue(), false, false);
		}
		else
		{
			setMinValue (getMinValue(), false, false);
			setMaxValue (getMaxValue(), false, false);
		}

		updateText();
	}
}

void Slider::triggerChangeMessage (const bool synchronous)
{
	if (synchronous)
		handleAsyncUpdate();
	else
		triggerAsyncUpdate();

	valueChanged();
}

void Slider::valueChanged (Value& value)
{
	if (value.refersToSameSourceAs (currentValue))
	{
		if (style != TwoValueHorizontal && style != TwoValueVertical)
			setValue (currentValue.getValue(), false, false);
	}
	else if (value.refersToSameSourceAs (valueMin))
		setMinValue (valueMin.getValue(), false, false, true);
	else if (value.refersToSameSourceAs (valueMax))
		setMaxValue (valueMax.getValue(), false, false, true);
}

double Slider::getValue() const
{
	// for a two-value style slider, you should use the getMinValue() and getMaxValue()
	// methods to get the two values.
	jassert (style != TwoValueHorizontal && style != TwoValueVertical);

	return currentValue.getValue();
}

void Slider::setValue (double newValue,
					   const bool sendUpdateMessage,
					   const bool sendMessageSynchronously)
{
	// for a two-value style slider, you should use the setMinValue() and setMaxValue()
	// methods to set the two values.
	jassert (style != TwoValueHorizontal && style != TwoValueVertical);

	newValue = constrainedValue (newValue);

	if (style == ThreeValueHorizontal || style == ThreeValueVertical)
	{
		jassert ((double) valueMin.getValue() <= (double) valueMax.getValue());

		newValue = jlimit ((double) valueMin.getValue(),
						   (double) valueMax.getValue(),
						   newValue);
	}

	if (newValue != lastCurrentValue)
	{
		if (valueBox != 0)
			valueBox->hideEditor (true);

		lastCurrentValue = newValue;
		currentValue = newValue;
		updateText();
		repaint();

		if (popupDisplay != 0)
		{
			static_cast <SliderPopupDisplayComponent*> (static_cast <Component*> (popupDisplay))
				->updatePosition (getTextFromValue (newValue));

			popupDisplay->repaint();
		}

		if (sendUpdateMessage)
			triggerChangeMessage (sendMessageSynchronously);
	}
}

double Slider::getMinValue() const
{
	// The minimum value only applies to sliders that are in two- or three-value mode.
	jassert (style == TwoValueHorizontal || style == TwoValueVertical
			  || style == ThreeValueHorizontal || style == ThreeValueVertical);

	return valueMin.getValue();
}

double Slider::getMaxValue() const
{
	// The maximum value only applies to sliders that are in two- or three-value mode.
	jassert (style == TwoValueHorizontal || style == TwoValueVertical
			  || style == ThreeValueHorizontal || style == ThreeValueVertical);

	return valueMax.getValue();
}

void Slider::setMinValue (double newValue, const bool sendUpdateMessage, const bool sendMessageSynchronously, const bool allowNudgingOfOtherValues)
{
	// The minimum value only applies to sliders that are in two- or three-value mode.
	jassert (style == TwoValueHorizontal || style == TwoValueVertical
			  || style == ThreeValueHorizontal || style == ThreeValueVertical);

	newValue = constrainedValue (newValue);

	if (style == TwoValueHorizontal || style == TwoValueVertical)
	{
		if (allowNudgingOfOtherValues && newValue > (double) valueMax.getValue())
			setMaxValue (newValue, sendUpdateMessage, sendMessageSynchronously);

		newValue = jmin ((double) valueMax.getValue(), newValue);
	}
	else
	{
		if (allowNudgingOfOtherValues && newValue > lastCurrentValue)
			setValue (newValue, sendUpdateMessage, sendMessageSynchronously);

		newValue = jmin (lastCurrentValue, newValue);
	}

	if (lastValueMin != newValue)
	{
		lastValueMin = newValue;
		valueMin = newValue;
		repaint();

		if (popupDisplay != 0)
		{
			static_cast <SliderPopupDisplayComponent*> (static_cast <Component*> (popupDisplay))
				->updatePosition (getTextFromValue (newValue));

			popupDisplay->repaint();
		}

		if (sendUpdateMessage)
			triggerChangeMessage (sendMessageSynchronously);
	}
}

void Slider::setMaxValue (double newValue, const bool sendUpdateMessage, const bool sendMessageSynchronously, const bool allowNudgingOfOtherValues)
{
	// The maximum value only applies to sliders that are in two- or three-value mode.
	jassert (style == TwoValueHorizontal || style == TwoValueVertical
			  || style == ThreeValueHorizontal || style == ThreeValueVertical);

	newValue = constrainedValue (newValue);

	if (style == TwoValueHorizontal || style == TwoValueVertical)
	{
		if (allowNudgingOfOtherValues && newValue < (double) valueMin.getValue())
			setMinValue (newValue, sendUpdateMessage, sendMessageSynchronously);

		newValue = jmax ((double) valueMin.getValue(), newValue);
	}
	else
	{
		if (allowNudgingOfOtherValues && newValue < lastCurrentValue)
			setValue (newValue, sendUpdateMessage, sendMessageSynchronously);

		newValue = jmax (lastCurrentValue, newValue);
	}

	if (lastValueMax != newValue)
	{
		lastValueMax = newValue;
		valueMax = newValue;
		repaint();

		if (popupDisplay != 0)
		{
			static_cast <SliderPopupDisplayComponent*> (static_cast <Component*> (popupDisplay))
				->updatePosition (getTextFromValue (valueMax.getValue()));

			popupDisplay->repaint();
		}

		if (sendUpdateMessage)
			triggerChangeMessage (sendMessageSynchronously);
	}
}

void Slider::setDoubleClickReturnValue (const bool isDoubleClickEnabled,
										const double valueToSetOnDoubleClick)
{
	doubleClickToValue = isDoubleClickEnabled;
	doubleClickReturnValue = valueToSetOnDoubleClick;
}

double Slider::getDoubleClickReturnValue (bool& isEnabled_) const
{
	isEnabled_ = doubleClickToValue;
	return doubleClickReturnValue;
}

void Slider::updateText()
{
	if (valueBox != 0)
		valueBox->setText (getTextFromValue (currentValue.getValue()), false);
}

void Slider::setTextValueSuffix (const String& suffix)
{
	if (textSuffix != suffix)
	{
		textSuffix = suffix;
		updateText();
	}
}

const String Slider::getTextValueSuffix() const
{
	return textSuffix;
}

const String Slider::getTextFromValue (double v)
{
	if (getNumDecimalPlacesToDisplay() > 0)
		return String (v, getNumDecimalPlacesToDisplay()) + getTextValueSuffix();
	else
		return String (roundToInt (v)) + getTextValueSuffix();
}

double Slider::getValueFromText (const String& text)
{
	String t (text.trimStart());

	if (t.endsWith (textSuffix))
		t = t.substring (0, t.length() - textSuffix.length());

	while (t.startsWithChar ('+'))
		t = t.substring (1).trimStart();

	return t.initialSectionContainingOnly ("0123456789.,-")
			.getDoubleValue();
}

double Slider::proportionOfLengthToValue (double proportion)
{
	if (skewFactor != 1.0 && proportion > 0.0)
		proportion = exp (log (proportion) / skewFactor);

	return minimum + (maximum - minimum) * proportion;
}

double Slider::valueToProportionOfLength (double value)
{
	const double n = (value - minimum) / (maximum - minimum);

	return skewFactor == 1.0 ? n : pow (n, skewFactor);
}

double Slider::snapValue (double attemptedValue, const bool)
{
	return attemptedValue;
}

void Slider::startedDragging()
{
}

void Slider::stoppedDragging()
{
}

void Slider::valueChanged()
{
}

void Slider::enablementChanged()
{
	repaint();
}

void Slider::setPopupMenuEnabled (const bool menuEnabled_)
{
	menuEnabled = menuEnabled_;
}

void Slider::setScrollWheelEnabled (const bool enabled)
{
	scrollWheelEnabled = enabled;
}

void Slider::labelTextChanged (Label* label)
{
	const double newValue = snapValue (getValueFromText (label->getText()), false);

	if (newValue != (double) currentValue.getValue())
	{
		sendDragStart();
		setValue (newValue, true, true);
		sendDragEnd();
	}

	updateText(); // force a clean-up of the text, needed in case setValue() hasn't done this.
}

void Slider::buttonClicked (Button* button)
{
	if (style == IncDecButtons)
	{
		sendDragStart();

		if (button == incButton)
			setValue (snapValue (getValue() + interval, false), true, true);
		else if (button == decButton)
			setValue (snapValue (getValue() - interval, false), true, true);

		sendDragEnd();
	}
}

double Slider::constrainedValue (double value) const
{
	if (interval > 0)
		value = minimum + interval * floor ((value - minimum) / interval + 0.5);

	if (value <= minimum || maximum <= minimum)
		value = minimum;
	else if (value >= maximum)
		value = maximum;

	return value;
}

float Slider::getLinearSliderPos (const double value)
{
	double sliderPosProportional;

	if (maximum > minimum)
	{
		if (value < minimum)
		{
			sliderPosProportional = 0.0;
		}
		else if (value > maximum)
		{
			sliderPosProportional = 1.0;
		}
		else
		{
			sliderPosProportional = valueToProportionOfLength (value);
			jassert (sliderPosProportional >= 0 && sliderPosProportional <= 1.0);
		}
	}
	else
	{
		sliderPosProportional = 0.5;
	}

	if (isVertical() || style == IncDecButtons)
		sliderPosProportional = 1.0 - sliderPosProportional;

	return (float) (sliderRegionStart + sliderPosProportional * sliderRegionSize);
}

bool Slider::isHorizontal() const
{
	return style == LinearHorizontal
		|| style == LinearBar
		|| style == TwoValueHorizontal
		|| style == ThreeValueHorizontal;
}

bool Slider::isVertical() const
{
	return style == LinearVertical
		|| style == TwoValueVertical
		|| style == ThreeValueVertical;
}

bool Slider::incDecDragDirectionIsHorizontal() const
{
	return incDecButtonMode == incDecButtonsDraggable_Horizontal
			|| (incDecButtonMode == incDecButtonsDraggable_AutoDirection && incDecButtonsSideBySide);
}

float Slider::getPositionOfValue (const double value)
{
	if (isHorizontal() || isVertical())
	{
		return getLinearSliderPos (value);
	}
	else
	{
		jassertfalse // not a valid call on a slider that doesn't work linearly!
		return 0.0f;
	}
}

void Slider::paint (Graphics& g)
{
	if (style != IncDecButtons)
	{
		if (style == Rotary || style == RotaryHorizontalDrag || style == RotaryVerticalDrag)
		{
			const float sliderPos = (float) valueToProportionOfLength (lastCurrentValue);
			jassert (sliderPos >= 0 && sliderPos <= 1.0f);

			getLookAndFeel().drawRotarySlider (g,
											   sliderRect.getX(),
											   sliderRect.getY(),
											   sliderRect.getWidth(),
											   sliderRect.getHeight(),
											   sliderPos,
											   rotaryStart, rotaryEnd,
											   *this);
		}
		else
		{
			getLookAndFeel().drawLinearSlider (g,
											   sliderRect.getX(),
											   sliderRect.getY(),
											   sliderRect.getWidth(),
											   sliderRect.getHeight(),
											   getLinearSliderPos (lastCurrentValue),
											   getLinearSliderPos (lastValueMin),
											   getLinearSliderPos (lastValueMax),
											   style,
											   *this);
		}

		if (style == LinearBar && valueBox == 0)
		{
			g.setColour (findColour (Slider::textBoxOutlineColourId));
			g.drawRect (0, 0, getWidth(), getHeight(), 1);
		}
	}
}

void Slider::resized()
{
	int minXSpace = 0;
	int minYSpace = 0;

	if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
		minXSpace = 30;
	else
		minYSpace = 15;

	const int tbw = jmax (0, jmin (textBoxWidth, getWidth() - minXSpace));
	const int tbh = jmax (0, jmin (textBoxHeight, getHeight() - minYSpace));

	if (style == LinearBar)
	{
		if (valueBox != 0)
			valueBox->setBounds (0, 0, getWidth(), getHeight());
	}
	else
	{
		if (textBoxPos == NoTextBox)
		{
			sliderRect.setBounds (0, 0, getWidth(), getHeight());
		}
		else if (textBoxPos == TextBoxLeft)
		{
			valueBox->setBounds (0, (getHeight() - tbh) / 2, tbw, tbh);
			sliderRect.setBounds (tbw, 0, getWidth() - tbw, getHeight());
		}
		else if (textBoxPos == TextBoxRight)
		{
			valueBox->setBounds (getWidth() - tbw, (getHeight() - tbh) / 2, tbw, tbh);
			sliderRect.setBounds (0, 0, getWidth() - tbw, getHeight());
		}
		else if (textBoxPos == TextBoxAbove)
		{
			valueBox->setBounds ((getWidth() - tbw) / 2, 0, tbw, tbh);
			sliderRect.setBounds (0, tbh, getWidth(), getHeight() - tbh);
		}
		else if (textBoxPos == TextBoxBelow)
		{
			valueBox->setBounds ((getWidth() - tbw) / 2, getHeight() - tbh, tbw, tbh);
			sliderRect.setBounds (0, 0, getWidth(), getHeight() - tbh);
		}
	}

	const int indent = getLookAndFeel().getSliderThumbRadius (*this);

	if (style == LinearBar)
	{
		const int barIndent = 1;
		sliderRegionStart = barIndent;
		sliderRegionSize = getWidth() - barIndent * 2;

		sliderRect.setBounds (sliderRegionStart, barIndent,
							  sliderRegionSize, getHeight() - barIndent * 2);
	}
	else if (isHorizontal())
	{
		sliderRegionStart = sliderRect.getX() + indent;
		sliderRegionSize = jmax (1, sliderRect.getWidth() - indent * 2);

		sliderRect.setBounds (sliderRegionStart, sliderRect.getY(),
							  sliderRegionSize, sliderRect.getHeight());
	}
	else if (isVertical())
	{
		sliderRegionStart = sliderRect.getY() + indent;
		sliderRegionSize = jmax (1, sliderRect.getHeight() - indent * 2);

		sliderRect.setBounds (sliderRect.getX(), sliderRegionStart,
							  sliderRect.getWidth(), sliderRegionSize);
	}
	else
	{
		sliderRegionStart = 0;
		sliderRegionSize = 100;
	}

	if (style == IncDecButtons)
	{
		Rectangle<int> buttonRect (sliderRect);

		if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
			buttonRect.expand (-2, 0);
		else
			buttonRect.expand (0, -2);

		incDecButtonsSideBySide = buttonRect.getWidth() > buttonRect.getHeight();

		if (incDecButtonsSideBySide)
		{
			decButton->setBounds (buttonRect.getX(),
								  buttonRect.getY(),
								  buttonRect.getWidth() / 2,
								  buttonRect.getHeight());

			decButton->setConnectedEdges (Button::ConnectedOnRight);

			incButton->setBounds (buttonRect.getCentreX(),
								  buttonRect.getY(),
								  buttonRect.getWidth() / 2,
								  buttonRect.getHeight());

			incButton->setConnectedEdges (Button::ConnectedOnLeft);
		}
		else
		{
			incButton->setBounds (buttonRect.getX(),
								  buttonRect.getY(),
								  buttonRect.getWidth(),
								  buttonRect.getHeight() / 2);

			incButton->setConnectedEdges (Button::ConnectedOnBottom);

			decButton->setBounds (buttonRect.getX(),
								  buttonRect.getCentreY(),
								  buttonRect.getWidth(),
								  buttonRect.getHeight() / 2);

			decButton->setConnectedEdges (Button::ConnectedOnTop);
		}
	}
}

void Slider::focusOfChildComponentChanged (FocusChangeType)
{
	repaint();
}

void Slider::mouseDown (const MouseEvent& e)
{
	mouseWasHidden = false;
	incDecDragged = false;
	mouseXWhenLastDragged = e.x;
	mouseYWhenLastDragged = e.y;
	mouseDragStartX = e.getMouseDownX();
	mouseDragStartY = e.getMouseDownY();

	if (isEnabled())
	{
		if (e.mods.isPopupMenu() && menuEnabled)
		{
			menuShown = true;

			PopupMenu m;
			m.setLookAndFeel (&getLookAndFeel());
			m.addItem (1, TRANS ("velocity-sensitive mode"), true, isVelocityBased);
			m.addSeparator();

			if (style == Rotary || style == RotaryHorizontalDrag || style == RotaryVerticalDrag)
			{
				PopupMenu rotaryMenu;
				rotaryMenu.addItem (2, TRANS ("use circular dragging"), true, style == Rotary);
				rotaryMenu.addItem (3, TRANS ("use left-right dragging"), true, style == RotaryHorizontalDrag);
				rotaryMenu.addItem (4, TRANS ("use up-down dragging"), true, style == RotaryVerticalDrag);

				m.addSubMenu (TRANS ("rotary mode"), rotaryMenu);
			}

			const int r = m.show();

			if (r == 1)
			{
				setVelocityBasedMode (! isVelocityBased);
			}
			else if (r == 2)
			{
				setSliderStyle (Rotary);
			}
			else if (r == 3)
			{
				setSliderStyle (RotaryHorizontalDrag);
			}
			else if (r == 4)
			{
				setSliderStyle (RotaryVerticalDrag);
			}
		}
		else if (maximum > minimum)
		{
			menuShown = false;

			if (valueBox != 0)
				valueBox->hideEditor (true);

			sliderBeingDragged = 0;

			if (style == TwoValueHorizontal
				 || style == TwoValueVertical
				 || style == ThreeValueHorizontal
				 || style == ThreeValueVertical)
			{
				const float mousePos = (float) (isVertical() ? e.y : e.x);

				const float normalPosDistance = fabsf (getLinearSliderPos (currentValue.getValue()) - mousePos);
				const float minPosDistance = fabsf (getLinearSliderPos (valueMin.getValue()) - 0.1f - mousePos);
				const float maxPosDistance = fabsf (getLinearSliderPos (valueMax.getValue()) + 0.1f - mousePos);

				if (style == TwoValueHorizontal || style == TwoValueVertical)
				{
					if (maxPosDistance <= minPosDistance)
						sliderBeingDragged = 2;
					else
						sliderBeingDragged = 1;
				}
				else if (style == ThreeValueHorizontal || style == ThreeValueVertical)
				{
					if (normalPosDistance >= minPosDistance && maxPosDistance >= minPosDistance)
						sliderBeingDragged = 1;
					else if (normalPosDistance >= maxPosDistance)
						sliderBeingDragged = 2;
				}
			}

			minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();

			lastAngle = rotaryStart + (rotaryEnd - rotaryStart)
										* valueToProportionOfLength (currentValue.getValue());

			valueWhenLastDragged = ((sliderBeingDragged == 2) ? valueMax
															  : ((sliderBeingDragged == 1) ? valueMin
																						   : currentValue)).getValue();

			valueOnMouseDown = valueWhenLastDragged;

			if (popupDisplayEnabled)
			{
				SliderPopupDisplayComponent* const popup = new SliderPopupDisplayComponent (this);
				popupDisplay = popup;

				if (parentForPopupDisplay != 0)
				{
					parentForPopupDisplay->addChildComponent (popup);
				}
				else
				{
					popup->addToDesktop (0);
				}

				popup->setVisible (true);
			}

			sendDragStart();

			mouseDrag (e);
		}
	}
}

void Slider::mouseUp (const MouseEvent&)
{
	if (isEnabled()
		 && (! menuShown)
		 && (maximum > minimum)
		 && (style != IncDecButtons || incDecDragged))
	{
		restoreMouseIfHidden();

		if (sendChangeOnlyOnRelease && valueOnMouseDown != (double) currentValue.getValue())
			triggerChangeMessage (false);

		sendDragEnd();

		popupDisplay = 0;

		if (style == IncDecButtons)
		{
			incButton->setState (Button::buttonNormal);
			decButton->setState (Button::buttonNormal);
		}
	}
}

void Slider::restoreMouseIfHidden()
{
	if (mouseWasHidden)
	{
		mouseWasHidden = false;

		for (int i = Desktop::getInstance().getNumMouseSources(); --i >= 0;)
			Desktop::getInstance().getMouseSource(i)->enableUnboundedMouseMovement (false);

		const double pos = (sliderBeingDragged == 2) ? getMaxValue()
													 : ((sliderBeingDragged == 1) ? getMinValue()
																				  : (double) currentValue.getValue());

		Point<int> mousePos;

		if (style == RotaryHorizontalDrag || style == RotaryVerticalDrag)
		{
			mousePos = Desktop::getLastMouseDownPosition();

			if (style == RotaryHorizontalDrag)
			{
				const double posDiff = valueToProportionOfLength (pos) - valueToProportionOfLength (valueOnMouseDown);
				mousePos += Point<int> (roundToInt (pixelsForFullDragExtent * posDiff), 0);
			}
			else
			{
				const double posDiff = valueToProportionOfLength (valueOnMouseDown) - valueToProportionOfLength (pos);
				mousePos += Point<int> (0, roundToInt (pixelsForFullDragExtent * posDiff));
			}
		}
		else
		{
			const int pixelPos = (int) getLinearSliderPos (pos);

			mousePos = relativePositionToGlobal (Point<int> (isHorizontal() ? pixelPos : (getWidth() / 2),
															 isVertical()   ? pixelPos : (getHeight() / 2)));
		}

		Desktop::setMousePosition (mousePos);
	}
}

void Slider::modifierKeysChanged (const ModifierKeys& modifiers)
{
	if (isEnabled()
		 && style != IncDecButtons
		 && style != Rotary
		 && isVelocityBased == modifiers.isAnyModifierKeyDown())
	{
		restoreMouseIfHidden();
	}
}

static double smallestAngleBetween (double a1, double a2)
{
	return jmin (fabs (a1 - a2),
				 fabs (a1 + double_Pi * 2.0 - a2),
				 fabs (a2 + double_Pi * 2.0 - a1));
}

void Slider::mouseDrag (const MouseEvent& e)
{
	if (isEnabled()
		 && (! menuShown)
		 && (maximum > minimum))
	{
		if (style == Rotary)
		{
			int dx = e.x - sliderRect.getCentreX();
			int dy = e.y - sliderRect.getCentreY();

			if (dx * dx + dy * dy > 25)
			{
				double angle = atan2 ((double) dx, (double) -dy);
				while (angle < 0.0)
					angle += double_Pi * 2.0;

				if (rotaryStop && ! e.mouseWasClicked())
				{
					if (fabs (angle - lastAngle) > double_Pi)
					{
						if (angle >= lastAngle)
							angle -= double_Pi * 2.0;
						else
							angle += double_Pi * 2.0;
					}

					if (angle >= lastAngle)
						angle = jmin (angle, (double) jmax (rotaryStart, rotaryEnd));
					else
						angle = jmax (angle, (double) jmin (rotaryStart, rotaryEnd));
				}
				else
				{
					while (angle < rotaryStart)
						angle += double_Pi * 2.0;

					if (angle > rotaryEnd)
					{
						if (smallestAngleBetween (angle, rotaryStart) <= smallestAngleBetween (angle, rotaryEnd))
							angle = rotaryStart;
						else
							angle = rotaryEnd;
					}
				}

				const double proportion = (angle - rotaryStart) / (rotaryEnd - rotaryStart);

				valueWhenLastDragged = proportionOfLengthToValue (jlimit (0.0, 1.0, proportion));

				lastAngle = angle;
			}
		}
		else
		{
			if (style == LinearBar && e.mouseWasClicked()
				 && valueBox != 0 && valueBox->isEditable())
				return;

			if (style == IncDecButtons && ! incDecDragged)
			{
				if (e.getDistanceFromDragStart() < 10 || e.mouseWasClicked())
					return;

				incDecDragged = true;
				mouseDragStartX = e.x;
				mouseDragStartY = e.y;
			}

			if ((isVelocityBased == (userKeyOverridesVelocity ? e.mods.testFlags (ModifierKeys::ctrlModifier | ModifierKeys::commandModifier | ModifierKeys::altModifier)
															  : false))
				|| ((maximum - minimum) / sliderRegionSize < interval))
			{
				const int mousePos = (isHorizontal() || style == RotaryHorizontalDrag) ? e.x : e.y;

				double scaledMousePos = (mousePos - sliderRegionStart) / (double) sliderRegionSize;

				if (style == RotaryHorizontalDrag
					|| style == RotaryVerticalDrag
					|| style == IncDecButtons
					|| ((style == LinearHorizontal || style == LinearVertical || style == LinearBar)
						&& ! snapsToMousePos))
				{
					const int mouseDiff = (style == RotaryHorizontalDrag
											 || style == LinearHorizontal
											 || style == LinearBar
											 || (style == IncDecButtons && incDecDragDirectionIsHorizontal()))
											? e.x - mouseDragStartX
											: mouseDragStartY - e.y;

					double newPos = valueToProportionOfLength (valueOnMouseDown)
									   + mouseDiff * (1.0 / pixelsForFullDragExtent);

					valueWhenLastDragged = proportionOfLengthToValue (jlimit (0.0, 1.0, newPos));

					if (style == IncDecButtons)
					{
						incButton->setState (mouseDiff < 0 ? Button::buttonNormal : Button::buttonDown);
						decButton->setState (mouseDiff > 0 ? Button::buttonNormal : Button::buttonDown);
					}
				}
				else
				{
					if (isVertical())
						scaledMousePos = 1.0 - scaledMousePos;

					valueWhenLastDragged = proportionOfLengthToValue (jlimit (0.0, 1.0, scaledMousePos));
				}
			}
			else
			{
				const int mouseDiff = (isHorizontal() || style == RotaryHorizontalDrag
										 || (style == IncDecButtons && incDecDragDirectionIsHorizontal()))
										? e.x - mouseXWhenLastDragged
										: e.y - mouseYWhenLastDragged;

				const double maxSpeed = jmax (200, sliderRegionSize);
				double speed = jlimit (0.0, maxSpeed, (double) abs (mouseDiff));

				if (speed != 0)
				{
					speed = 0.2 * velocityModeSensitivity
							  * (1.0 + sin (double_Pi * (1.5 + jmin (0.5, velocityModeOffset
																			+ jmax (0.0, (double) (speed - velocityModeThreshold))
																				/ maxSpeed))));

					if (mouseDiff < 0)
						speed = -speed;

					if (isVertical() || style == RotaryVerticalDrag
						 || (style == IncDecButtons && ! incDecDragDirectionIsHorizontal()))
						speed = -speed;

					const double currentPos = valueToProportionOfLength (valueWhenLastDragged);

					valueWhenLastDragged = proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + speed));

					e.source.enableUnboundedMouseMovement (true, false);
					mouseWasHidden = true;
				}
			}
		}

		valueWhenLastDragged = jlimit (minimum, maximum, valueWhenLastDragged);

		if (sliderBeingDragged == 0)
		{
			setValue (snapValue (valueWhenLastDragged, true),
					  ! sendChangeOnlyOnRelease, true);
		}
		else if (sliderBeingDragged == 1)
		{
			setMinValue (snapValue (valueWhenLastDragged, true),
						 ! sendChangeOnlyOnRelease, false, true);

			if (e.mods.isShiftDown())
				setMaxValue (getMinValue() + minMaxDiff, false, false, true);
			else
				minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();
		}
		else
		{
			jassert (sliderBeingDragged == 2);

			setMaxValue (snapValue (valueWhenLastDragged, true),
						 ! sendChangeOnlyOnRelease, false, true);

			if (e.mods.isShiftDown())
				setMinValue (getMaxValue() - minMaxDiff, false, false, true);
			else
				minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();
		}

		mouseXWhenLastDragged = e.x;
		mouseYWhenLastDragged = e.y;
	}
}

void Slider::mouseDoubleClick (const MouseEvent&)
{
	if (doubleClickToValue
		 && isEnabled()
		 && style != IncDecButtons
		 && minimum <= doubleClickReturnValue
		 && maximum >= doubleClickReturnValue)
	{
		sendDragStart();
		setValue (doubleClickReturnValue, true, true);
		sendDragEnd();
	}
}

void Slider::mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
	if (scrollWheelEnabled && isEnabled()
		 && style != TwoValueHorizontal
		 && style != TwoValueVertical)
	{
		if (maximum > minimum && ! e.mods.isAnyMouseButtonDown())
		{
			if (valueBox != 0)
				valueBox->hideEditor (false);

			const double value = (double) currentValue.getValue();
			const double proportionDelta = (wheelIncrementX != 0 ? -wheelIncrementX : wheelIncrementY) * 0.15f;
			const double currentPos = valueToProportionOfLength (value);
			const double newValue = proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + proportionDelta));

			double delta = (newValue != value)
							? jmax (fabs (newValue - value), interval) : 0;

			if (value > newValue)
				delta = -delta;

			sendDragStart();
			setValue (snapValue (value + delta, false), true, true);
			sendDragEnd();
		}
	}
	else
	{
		Component::mouseWheelMove (e, wheelIncrementX, wheelIncrementY);
	}
}

void SliderListener::sliderDragStarted (Slider*)
{
}

void SliderListener::sliderDragEnded (Slider*)
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Slider.cpp ***/


/*** Start of inlined file: juce_TableHeaderComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

class DragOverlayComp   : public Component
{
public:
	DragOverlayComp (Image* const image_)
		: image (image_)
	{
		image->multiplyAllAlphas (0.8f);
		setAlwaysOnTop (true);
	}

	~DragOverlayComp()
	{
	}

	void paint (Graphics& g)
	{
		g.drawImageAt (image, 0, 0);
	}

private:
	ScopedPointer <Image> image;

	DragOverlayComp (const DragOverlayComp&);
	DragOverlayComp& operator= (const DragOverlayComp&);
};

TableHeaderComponent::TableHeaderComponent()
	: columnsChanged (false),
	  columnsResized (false),
	  sortChanged (false),
	  menuActive (true),
	  stretchToFit (false),
	  columnIdBeingResized (0),
	  columnIdBeingDragged (0),
	  columnIdUnderMouse (0),
	  lastDeliberateWidth (0)
{
}

TableHeaderComponent::~TableHeaderComponent()
{
	dragOverlayComp = 0;
}

void TableHeaderComponent::setPopupMenuActive (const bool hasMenu)
{
	menuActive = hasMenu;
}

bool TableHeaderComponent::isPopupMenuActive() const			{ return menuActive; }

int TableHeaderComponent::getNumColumns (const bool onlyCountVisibleColumns) const
{
	if (onlyCountVisibleColumns)
	{
		int num = 0;

		for (int i = columns.size(); --i >= 0;)
			if (columns.getUnchecked(i)->isVisible())
				++num;

		return num;
	}
	else
	{
		return columns.size();
	}
}

const String TableHeaderComponent::getColumnName (const int columnId) const
{
	const ColumnInfo* const ci = getInfoForId (columnId);
	return ci != 0 ? ci->name : String::empty;
}

void TableHeaderComponent::setColumnName (const int columnId, const String& newName)
{
	ColumnInfo* const ci = getInfoForId (columnId);

	if (ci != 0 && ci->name != newName)
	{
		ci->name = newName;
		sendColumnsChanged();
	}
}

void TableHeaderComponent::addColumn (const String& columnName,
									  const int columnId,
									  const int width,
									  const int minimumWidth,
									  const int maximumWidth,
									  const int propertyFlags,
									  const int insertIndex)
{
	// can't have a duplicate or null ID!
	jassert (columnId != 0 && getIndexOfColumnId (columnId, false) < 0);
	jassert (width > 0);

	ColumnInfo* const ci = new ColumnInfo();
	ci->name = columnName;
	ci->id = columnId;
	ci->width = width;
	ci->lastDeliberateWidth = width;
	ci->minimumWidth = minimumWidth;
	ci->maximumWidth = maximumWidth;
	if (ci->maximumWidth < 0)
		ci->maximumWidth = std::numeric_limits<int>::max();
	jassert (ci->maximumWidth >= ci->minimumWidth);
	ci->propertyFlags = propertyFlags;

	columns.insert (insertIndex, ci);
	sendColumnsChanged();
}

void TableHeaderComponent::removeColumn (const int columnIdToRemove)
{
	const int index = getIndexOfColumnId (columnIdToRemove, false);

	if (index >= 0)
	{
		columns.remove (index);
		sortChanged = true;
		sendColumnsChanged();
	}
}

void TableHeaderComponent::removeAllColumns()
{
	if (columns.size() > 0)
	{
		columns.clear();
		sendColumnsChanged();
	}
}

void TableHeaderComponent::moveColumn (const int columnId, int newIndex)
{
	const int currentIndex = getIndexOfColumnId (columnId, false);
	newIndex = visibleIndexToTotalIndex (newIndex);

	if (columns [currentIndex] != 0 && currentIndex != newIndex)
	{
		columns.move (currentIndex, newIndex);
		sendColumnsChanged();
	}
}

int TableHeaderComponent::getColumnWidth (const int columnId) const
{
	const ColumnInfo* const ci = getInfoForId (columnId);
	return ci != 0 ? ci->width : 0;
}

void TableHeaderComponent::setColumnWidth (const int columnId, const int newWidth)
{
	ColumnInfo* const ci = getInfoForId (columnId);

	if (ci != 0 && ci->width != newWidth)
	{
		const int numColumns = getNumColumns (true);

		ci->lastDeliberateWidth = ci->width
			= jlimit (ci->minimumWidth, ci->maximumWidth, newWidth);

		if (stretchToFit)
		{
			const int index = getIndexOfColumnId (columnId, true) + 1;

			if (((unsigned int) index) < (unsigned int) numColumns)
			{
				const int x = getColumnPosition (index).getX();

				if (lastDeliberateWidth == 0)
					lastDeliberateWidth = getTotalWidth();

				resizeColumnsToFit (visibleIndexToTotalIndex (index), lastDeliberateWidth - x);
			}
		}

		repaint();
		columnsResized = true;
		triggerAsyncUpdate();
	}
}

int TableHeaderComponent::getIndexOfColumnId (const int columnId, const bool onlyCountVisibleColumns) const
{
	int n = 0;

	for (int i = 0; i < columns.size(); ++i)
	{
		if ((! onlyCountVisibleColumns) || columns.getUnchecked(i)->isVisible())
		{
			if (columns.getUnchecked(i)->id == columnId)
				return n;

			++n;
		}
	}

	return -1;
}

int TableHeaderComponent::getColumnIdOfIndex (int index, const bool onlyCountVisibleColumns) const
{
	if (onlyCountVisibleColumns)
		index = visibleIndexToTotalIndex (index);

	const ColumnInfo* const ci = columns [index];
	return (ci != 0) ? ci->id : 0;
}

const Rectangle<int> TableHeaderComponent::getColumnPosition (const int index) const
{
	int x = 0, width = 0, n = 0;

	for (int i = 0; i < columns.size(); ++i)
	{
		x += width;

		if (columns.getUnchecked(i)->isVisible())
		{
			width = columns.getUnchecked(i)->width;

			if (n++ == index)
				break;
		}
		else
		{
			width = 0;
		}
	}

	return Rectangle<int> (x, 0, width, getHeight());
}

int TableHeaderComponent::getColumnIdAtX (const int xToFind) const
{
	if (xToFind >= 0)
	{
		int x = 0;

		for (int i = 0; i < columns.size(); ++i)
		{
			const ColumnInfo* const ci = columns.getUnchecked(i);

			if (ci->isVisible())
			{
				x += ci->width;

				if (xToFind < x)
					return ci->id;
			}
		}
	}

	return 0;
}

int TableHeaderComponent::getTotalWidth() const
{
	int w = 0;

	for (int i = columns.size(); --i >= 0;)
		if (columns.getUnchecked(i)->isVisible())
			w += columns.getUnchecked(i)->width;

	return w;
}

void TableHeaderComponent::setStretchToFitActive (const bool shouldStretchToFit)
{
	stretchToFit = shouldStretchToFit;
	lastDeliberateWidth = getTotalWidth();
	resized();
}

bool TableHeaderComponent::isStretchToFitActive() const
{
	return stretchToFit;
}

void TableHeaderComponent::resizeAllColumnsToFit (int targetTotalWidth)
{
	if (stretchToFit && getWidth() > 0
		 && columnIdBeingResized == 0 && columnIdBeingDragged == 0)
	{
		lastDeliberateWidth = targetTotalWidth;
		resizeColumnsToFit (0, targetTotalWidth);
	}
}

void TableHeaderComponent::resizeColumnsToFit (int firstColumnIndex, int targetTotalWidth)
{
	targetTotalWidth = jmax (targetTotalWidth, 0);

	StretchableObjectResizer sor;
	int i;
	for (i = firstColumnIndex; i < columns.size(); ++i)
	{
		ColumnInfo* const ci = columns.getUnchecked(i);

		if (ci->isVisible())
			sor.addItem (ci->lastDeliberateWidth, ci->minimumWidth, ci->maximumWidth);
	}

	sor.resizeToFit (targetTotalWidth);

	int visIndex = 0;
	for (i = firstColumnIndex; i < columns.size(); ++i)
	{
		ColumnInfo* const ci = columns.getUnchecked(i);

		if (ci->isVisible())
		{
			const int newWidth = jlimit (ci->minimumWidth, ci->maximumWidth,
										 (int) floor (sor.getItemSize (visIndex++)));

			if (newWidth != ci->width)
			{
				ci->width = newWidth;
				repaint();
				columnsResized = true;
				triggerAsyncUpdate();
			}
		}
	}
}

void TableHeaderComponent::setColumnVisible (const int columnId, const bool shouldBeVisible)
{
	ColumnInfo* const ci = getInfoForId (columnId);

	if (ci != 0 && shouldBeVisible != ci->isVisible())
	{
		if (shouldBeVisible)
			ci->propertyFlags |= visible;
		else
			ci->propertyFlags &= ~visible;

		sendColumnsChanged();
		resized();
	}
}

bool TableHeaderComponent::isColumnVisible (const int columnId) const
{
	const ColumnInfo* const ci = getInfoForId (columnId);
	return ci != 0 && ci->isVisible();
}

void TableHeaderComponent::setSortColumnId (const int columnId, const bool sortForwards)
{
	if (getSortColumnId() != columnId || isSortedForwards() != sortForwards)
	{
		for (int i = columns.size(); --i >= 0;)
			columns.getUnchecked(i)->propertyFlags &= ~(sortedForwards | sortedBackwards);

		ColumnInfo* const ci = getInfoForId (columnId);

		if (ci != 0)
			ci->propertyFlags |= (sortForwards ? sortedForwards : sortedBackwards);

		reSortTable();
	}
}

int TableHeaderComponent::getSortColumnId() const
{
	for (int i = columns.size(); --i >= 0;)
		if ((columns.getUnchecked(i)->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
			return columns.getUnchecked(i)->id;

	return 0;
}

bool TableHeaderComponent::isSortedForwards() const
{
	for (int i = columns.size(); --i >= 0;)
		if ((columns.getUnchecked(i)->propertyFlags & (sortedForwards | sortedBackwards)) != 0)
			return (columns.getUnchecked(i)->propertyFlags & sortedForwards) != 0;

	return true;
}

void TableHeaderComponent::reSortTable()
{
	sortChanged = true;
	repaint();
	triggerAsyncUpdate();
}

const String TableHeaderComponent::toString() const
{
	String s;

	XmlElement doc ("TABLELAYOUT");

	doc.setAttribute ("sortedCol", getSortColumnId());
	doc.setAttribute ("sortForwards", isSortedForwards());

	for (int i = 0; i < columns.size(); ++i)
	{
		const ColumnInfo* const ci = columns.getUnchecked (i);

		XmlElement* const e = doc.createNewChildElement ("COLUMN");
		e->setAttribute ("id", ci->id);
		e->setAttribute ("visible", ci->isVisible());
		e->setAttribute ("width", ci->width);
	}

	return doc.createDocument (String::empty, true, false);
}

void TableHeaderComponent::restoreFromString (const String& storedVersion)
{
	XmlDocument doc (storedVersion);
	ScopedPointer <XmlElement> storedXml (doc.getDocumentElement());

	int index = 0;

	if (storedXml != 0 && storedXml->hasTagName ("TABLELAYOUT"))
	{
		forEachXmlChildElement (*storedXml, col)
		{
			const int tabId = col->getIntAttribute ("id");

			ColumnInfo* const ci = getInfoForId (tabId);

			if (ci != 0)
			{
				columns.move (columns.indexOf (ci), index);
				ci->width = col->getIntAttribute ("width");
				setColumnVisible (tabId, col->getBoolAttribute ("visible"));
			}

			++index;
		}

		columnsResized = true;
		sendColumnsChanged();

		setSortColumnId (storedXml->getIntAttribute ("sortedCol"),
						 storedXml->getBoolAttribute ("sortForwards", true));
	}
}

void TableHeaderComponent::addListener (TableHeaderListener* const newListener)
{
	listeners.addIfNotAlreadyThere (newListener);
}

void TableHeaderComponent::removeListener (TableHeaderListener* const listenerToRemove)
{
	listeners.removeValue (listenerToRemove);
}

void TableHeaderComponent::columnClicked (int columnId, const ModifierKeys& mods)
{
	const ColumnInfo* const ci = getInfoForId (columnId);

	if (ci != 0 && (ci->propertyFlags & sortable) != 0 && ! mods.isPopupMenu())
		setSortColumnId (columnId, (ci->propertyFlags & sortedForwards) == 0);
}

void TableHeaderComponent::addMenuItems (PopupMenu& menu, const int /*columnIdClicked*/)
{
	for (int i = 0; i < columns.size(); ++i)
	{
		const ColumnInfo* const ci = columns.getUnchecked(i);

		if ((ci->propertyFlags & appearsOnColumnMenu) != 0)
			menu.addItem (ci->id, ci->name,
						  (ci->propertyFlags & (sortedForwards | sortedBackwards)) == 0,
						  isColumnVisible (ci->id));
	}
}

void TableHeaderComponent::reactToMenuItem (const int menuReturnId, const int /*columnIdClicked*/)
{
	if (getIndexOfColumnId (menuReturnId, false) >= 0)
		setColumnVisible (menuReturnId, ! isColumnVisible (menuReturnId));
}

void TableHeaderComponent::paint (Graphics& g)
{
	LookAndFeel& lf = getLookAndFeel();

	lf.drawTableHeaderBackground (g, *this);

	const Rectangle<int> clip (g.getClipBounds());

	int x = 0;
	for (int i = 0; i < columns.size(); ++i)
	{
		const ColumnInfo* const ci = columns.getUnchecked(i);

		if (ci->isVisible())
		{
			if (x + ci->width > clip.getX()
				 && (ci->id != columnIdBeingDragged
					  || dragOverlayComp == 0
					  || ! dragOverlayComp->isVisible()))
			{
				g.saveState();
				g.setOrigin (x, 0);
				g.reduceClipRegion (0, 0, ci->width, getHeight());

				lf.drawTableHeaderColumn (g, ci->name, ci->id, ci->width, getHeight(),
										  ci->id == columnIdUnderMouse,
										  ci->id == columnIdUnderMouse && isMouseButtonDown(),
										  ci->propertyFlags);

				g.restoreState();
			}

			x += ci->width;

			if (x >= clip.getRight())
				break;
		}
	}
}

void TableHeaderComponent::resized()
{
}

void TableHeaderComponent::mouseMove (const MouseEvent& e)
{
	updateColumnUnderMouse (e.x, e.y);
}

void TableHeaderComponent::mouseEnter (const MouseEvent& e)
{
	updateColumnUnderMouse (e.x, e.y);
}

void TableHeaderComponent::mouseExit (const MouseEvent& e)
{
	updateColumnUnderMouse (e.x, e.y);
}

void TableHeaderComponent::mouseDown (const MouseEvent& e)
{
	repaint();
	columnIdBeingResized = 0;
	columnIdBeingDragged = 0;

	if (columnIdUnderMouse != 0)
	{
		draggingColumnOffset = e.x - getColumnPosition (getIndexOfColumnId (columnIdUnderMouse, true)).getX();

		if (e.mods.isPopupMenu())
			columnClicked (columnIdUnderMouse, e.mods);
	}

	if (menuActive && e.mods.isPopupMenu())
		showColumnChooserMenu (columnIdUnderMouse);
}

void TableHeaderComponent::mouseDrag (const MouseEvent& e)
{
	if (columnIdBeingResized == 0
		 && columnIdBeingDragged == 0
		 && ! (e.mouseWasClicked() || e.mods.isPopupMenu()))
	{
		dragOverlayComp = 0;

		columnIdBeingResized = getResizeDraggerAt (e.getMouseDownX());

		if (columnIdBeingResized != 0)
		{
			const ColumnInfo* const ci = getInfoForId (columnIdBeingResized);
			initialColumnWidth = ci->width;
		}
		else
		{
			beginDrag (e);
		}
	}

	if (columnIdBeingResized != 0)
	{
		const ColumnInfo* const ci = getInfoForId (columnIdBeingResized);

		if (ci != 0)
		{
			int w = jlimit (ci->minimumWidth, ci->maximumWidth,
							initialColumnWidth + e.getDistanceFromDragStartX());

			if (stretchToFit)
			{
				// prevent us dragging a column too far right if we're in stretch-to-fit mode
				int minWidthOnRight = 0;
				for (int i = getIndexOfColumnId (columnIdBeingResized, false) + 1; i < columns.size(); ++i)
					if (columns.getUnchecked (i)->isVisible())
						minWidthOnRight += columns.getUnchecked (i)->minimumWidth;

				const Rectangle<int> currentPos (getColumnPosition (getIndexOfColumnId (columnIdBeingResized, true)));
				w = jmax (ci->minimumWidth, jmin (w, getWidth() - minWidthOnRight - currentPos.getX()));
			}

			setColumnWidth (columnIdBeingResized, w);
		}
	}
	else if (columnIdBeingDragged != 0)
	{
		if (e.y >= -50 && e.y < getHeight() + 50)
		{
			if (dragOverlayComp != 0)
			{
				dragOverlayComp->setVisible (true);
				dragOverlayComp->setBounds (jlimit (0,
													jmax (0, getTotalWidth() - dragOverlayComp->getWidth()),
													e.x - draggingColumnOffset),
											0,
											dragOverlayComp->getWidth(),
											getHeight());

				for (int i = columns.size(); --i >= 0;)
				{
					const int currentIndex = getIndexOfColumnId (columnIdBeingDragged, true);
					int newIndex = currentIndex;

					if (newIndex > 0)
					{
						// if the previous column isn't draggable, we can't move our column
						// past it, because that'd change the undraggable column's position..
						const ColumnInfo* const previous = columns.getUnchecked (newIndex - 1);

						if ((previous->propertyFlags & draggable) != 0)
						{
							const int leftOfPrevious = getColumnPosition (newIndex - 1).getX();
							const int rightOfCurrent = getColumnPosition (newIndex).getRight();

							if (abs (dragOverlayComp->getX() - leftOfPrevious)
								< abs (dragOverlayComp->getRight() - rightOfCurrent))
							{
								--newIndex;
							}
						}
					}

					if (newIndex < columns.size() - 1)
					{
						// if the next column isn't draggable, we can't move our column
						// past it, because that'd change the undraggable column's position..
						const ColumnInfo* const nextCol = columns.getUnchecked (newIndex + 1);

						if ((nextCol->propertyFlags & draggable) != 0)
						{
							const int leftOfCurrent = getColumnPosition (newIndex).getX();
							const int rightOfNext = getColumnPosition (newIndex + 1).getRight();

							if (abs (dragOverlayComp->getX() - leftOfCurrent)
								> abs (dragOverlayComp->getRight() - rightOfNext))
							{
								++newIndex;
							}
						}
					}

					if (newIndex != currentIndex)
						moveColumn (columnIdBeingDragged, newIndex);
					else
						break;
				}
			}
		}
		else
		{
			endDrag (draggingColumnOriginalIndex);
		}
	}
}

void TableHeaderComponent::beginDrag (const MouseEvent& e)
{
	if (columnIdBeingDragged == 0)
	{
		columnIdBeingDragged = getColumnIdAtX (e.getMouseDownX());

		const ColumnInfo* const ci = getInfoForId (columnIdBeingDragged);

		if (ci == 0 || (ci->propertyFlags & draggable) == 0)
		{
			columnIdBeingDragged = 0;
		}
		else
		{
			draggingColumnOriginalIndex = getIndexOfColumnId (columnIdBeingDragged, true);

			const Rectangle<int> columnRect (getColumnPosition (draggingColumnOriginalIndex));

			const int temp = columnIdBeingDragged;
			columnIdBeingDragged = 0;

			addAndMakeVisible (dragOverlayComp = new DragOverlayComp (createComponentSnapshot (columnRect, false)));
			columnIdBeingDragged = temp;

			dragOverlayComp->setBounds (columnRect);

			for (int i = listeners.size(); --i >= 0;)
			{
				listeners.getUnchecked(i)->tableColumnDraggingChanged (this, columnIdBeingDragged);
				i = jmin (i, listeners.size() - 1);
			}
		}
	}
}

void TableHeaderComponent::endDrag (const int finalIndex)
{
	if (columnIdBeingDragged != 0)
	{
		moveColumn (columnIdBeingDragged, finalIndex);

		columnIdBeingDragged = 0;
		repaint();

		for (int i = listeners.size(); --i >= 0;)
		{
			listeners.getUnchecked(i)->tableColumnDraggingChanged (this, 0);
			i = jmin (i, listeners.size() - 1);
		}
	}
}

void TableHeaderComponent::mouseUp (const MouseEvent& e)
{
	mouseDrag (e);

	for (int i = columns.size(); --i >= 0;)
		if (columns.getUnchecked (i)->isVisible())
			columns.getUnchecked (i)->lastDeliberateWidth = columns.getUnchecked (i)->width;

	columnIdBeingResized = 0;
	repaint();

	endDrag (getIndexOfColumnId (columnIdBeingDragged, true));

	updateColumnUnderMouse (e.x, e.y);

	if (columnIdUnderMouse != 0 && e.mouseWasClicked() && ! e.mods.isPopupMenu())
		columnClicked (columnIdUnderMouse, e.mods);

	dragOverlayComp = 0;
}

const MouseCursor TableHeaderComponent::getMouseCursor()
{
	if (columnIdBeingResized != 0 || (getResizeDraggerAt (getMouseXYRelative().getX()) != 0 && ! isMouseButtonDown()))
		return MouseCursor (MouseCursor::LeftRightResizeCursor);

	return Component::getMouseCursor();
}

bool TableHeaderComponent::ColumnInfo::isVisible() const
{
	return (propertyFlags & TableHeaderComponent::visible) != 0;
}

TableHeaderComponent::ColumnInfo* TableHeaderComponent::getInfoForId (const int id) const
{
	for (int i = columns.size(); --i >= 0;)
		if (columns.getUnchecked(i)->id == id)
			return columns.getUnchecked(i);

	return 0;
}

int TableHeaderComponent::visibleIndexToTotalIndex (const int visibleIndex) const
{
	int n = 0;

	for (int i = 0; i < columns.size(); ++i)
	{
		if (columns.getUnchecked(i)->isVisible())
		{
			if (n == visibleIndex)
				return i;

			++n;
		}
	}

	return -1;
}

void TableHeaderComponent::sendColumnsChanged()
{
	if (stretchToFit && lastDeliberateWidth > 0)
		resizeAllColumnsToFit (lastDeliberateWidth);

	repaint();
	columnsChanged = true;
	triggerAsyncUpdate();
}

void TableHeaderComponent::handleAsyncUpdate()
{
	const bool changed = columnsChanged || sortChanged;
	const bool sized = columnsResized || changed;
	const bool sorted = sortChanged;
	columnsChanged = false;
	columnsResized = false;
	sortChanged = false;

	if (sorted)
	{
		for (int i = listeners.size(); --i >= 0;)
		{
			listeners.getUnchecked(i)->tableSortOrderChanged (this);
			i = jmin (i, listeners.size() - 1);
		}
	}

	if (changed)
	{
		for (int i = listeners.size(); --i >= 0;)
		{
			listeners.getUnchecked(i)->tableColumnsChanged (this);
			i = jmin (i, listeners.size() - 1);
		}
	}

	if (sized)
	{
		for (int i = listeners.size(); --i >= 0;)
		{
			listeners.getUnchecked(i)->tableColumnsResized (this);
			i = jmin (i, listeners.size() - 1);
		}
	}
}

int TableHeaderComponent::getResizeDraggerAt (const int mouseX) const
{
	if (((unsigned int) mouseX) < (unsigned int) getWidth())
	{
		const int draggableDistance = 3;
		int x = 0;

		for (int i = 0; i < columns.size(); ++i)
		{
			const ColumnInfo* const ci = columns.getUnchecked(i);

			if (ci->isVisible())
			{
				if (abs (mouseX - (x + ci->width)) <= draggableDistance
					 && (ci->propertyFlags & resizable) != 0)
					return ci->id;

				x += ci->width;
			}
		}
	}

	return 0;
}

void TableHeaderComponent::updateColumnUnderMouse (int x, int y)
{
	const int newCol = (reallyContains (x, y, true) && getResizeDraggerAt (x) == 0)
							? getColumnIdAtX (x) : 0;

	if (newCol != columnIdUnderMouse)
	{
		columnIdUnderMouse = newCol;
		repaint();
	}
}

void TableHeaderComponent::showColumnChooserMenu (const int columnIdClicked)
{
	PopupMenu m;
	addMenuItems (m, columnIdClicked);

	if (m.getNumItems() > 0)
	{
		m.setLookAndFeel (&getLookAndFeel());

		const int result = m.show();

		if (result != 0)
			reactToMenuItem (result, columnIdClicked);
	}
}

void TableHeaderListener::tableColumnDraggingChanged (TableHeaderComponent*, int)
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TableHeaderComponent.cpp ***/


/*** Start of inlined file: juce_TableListBox.cpp ***/
BEGIN_JUCE_NAMESPACE

static const char* const tableColumnPropertyTag = "_tableColumnID";

class TableListRowComp   : public Component,
						   public TooltipClient
{
public:
	TableListRowComp (TableListBox& owner_)
		: owner (owner_),
		  row (-1),
		  isSelected (false)
	{
	}

	~TableListRowComp()
	{
		deleteAllChildren();
	}

	void paint (Graphics& g)
	{
		TableListBoxModel* const model = owner.getModel();

		if (model != 0)
		{
			const TableHeaderComponent* const header = owner.getHeader();

			model->paintRowBackground (g, row, getWidth(), getHeight(), isSelected);

			const int numColumns = header->getNumColumns (true);

			for (int i = 0; i < numColumns; ++i)
			{
				if (! columnsWithComponents [i])
				{
					const int columnId = header->getColumnIdOfIndex (i, true);
					Rectangle<int> columnRect (header->getColumnPosition (i));
					columnRect.setSize (columnRect.getWidth(), getHeight());

					g.saveState();

					g.reduceClipRegion (columnRect);
					g.setOrigin (columnRect.getX(), 0);

					model->paintCell (g, row, columnId, columnRect.getWidth(), columnRect.getHeight(), isSelected);

					g.restoreState();
				}
			}
		}
	}

	void update (const int newRow, const bool isNowSelected)
	{
		if (newRow != row || isNowSelected != isSelected)
		{
			row = newRow;
			isSelected = isNowSelected;
			repaint();
		}

		if (row < owner.getNumRows())
		{
			jassert (row >= 0);

			const var::identifier tagPropertyName ("_tableLastUseNum");
			const int newTag = Random::getSystemRandom().nextInt();

			const TableHeaderComponent* const header = owner.getHeader();
			const int numColumns = header->getNumColumns (true);
			int i;

			columnsWithComponents.clear();

			if (owner.getModel() != 0)
			{
				for (i = 0; i < numColumns; ++i)
				{
					const int columnId = header->getColumnIdOfIndex (i, true);

					Component* const newComp
						= owner.getModel()->refreshComponentForCell (row, columnId, isSelected,
																	 findChildComponentForColumn (columnId));

					if (newComp != 0)
					{
						addAndMakeVisible (newComp);
						newComp->getProperties().set (tagPropertyName, newTag);
						newComp->getProperties().set (tableColumnPropertyTag, columnId);

						const Rectangle<int> columnRect (header->getColumnPosition (i));
						newComp->setBounds (columnRect.getX(), 0, columnRect.getWidth(), getHeight());

						columnsWithComponents.setBit (i);
					}
				}
			}

			for (i = getNumChildComponents(); --i >= 0;)
			{
				Component* const c = getChildComponent (i);

				if ((int) c->getProperties() [tagPropertyName] != newTag)
					delete c;
			}
		}
		else
		{
			columnsWithComponents.clear();
			deleteAllChildren();
		}
	}

	void resized()
	{
		for (int i = getNumChildComponents(); --i >= 0;)
		{
			Component* const c = getChildComponent (i);

			const int columnId = c->getProperties() [tableColumnPropertyTag];

			if (columnId != 0)
			{
				const Rectangle<int> columnRect (owner.getHeader()->getColumnPosition (owner.getHeader()->getIndexOfColumnId (columnId, true)));
				c->setBounds (columnRect.getX(), 0, columnRect.getWidth(), getHeight());
			}
		}
	}

	void mouseDown (const MouseEvent& e)
	{
		isDragging = false;
		selectRowOnMouseUp = false;

		if (isEnabled())
		{
			if (! isSelected)
			{
				owner.selectRowsBasedOnModifierKeys (row, e.mods);

				const int columnId = owner.getHeader()->getColumnIdAtX (e.x);

				if (columnId != 0 && owner.getModel() != 0)
					owner.getModel()->cellClicked (row, columnId, e);
			}
			else
			{
				selectRowOnMouseUp = true;
			}
		}
	}

	void mouseDrag (const MouseEvent& e)
	{
		if (isEnabled() && owner.getModel() != 0 && ! (e.mouseWasClicked() || isDragging))
		{
			const SparseSet <int> selectedRows (owner.getSelectedRows());

			if (selectedRows.size() > 0)
			{
				const String dragDescription (owner.getModel()->getDragSourceDescription (selectedRows));

				if (dragDescription.isNotEmpty())
				{
					isDragging = true;
					owner.startDragAndDrop (e, dragDescription);
				}
			}
		}
	}

	void mouseUp (const MouseEvent& e)
	{
		if (selectRowOnMouseUp && e.mouseWasClicked() && isEnabled())
		{
			owner.selectRowsBasedOnModifierKeys (row, e.mods);

			const int columnId = owner.getHeader()->getColumnIdAtX (e.x);

			if (columnId != 0 && owner.getModel() != 0)
				owner.getModel()->cellClicked (row, columnId, e);
		}
	}

	void mouseDoubleClick (const MouseEvent& e)
	{
		const int columnId = owner.getHeader()->getColumnIdAtX (e.x);

		if (columnId != 0 && owner.getModel() != 0)
			owner.getModel()->cellDoubleClicked (row, columnId, e);
	}

	const String getTooltip()
	{
		const int columnId = owner.getHeader()->getColumnIdAtX (getMouseXYRelative().getX());

		if (columnId != 0 && owner.getModel() != 0)
			return owner.getModel()->getCellTooltip (row, columnId);

		return String::empty;
	}

	juce_UseDebuggingNewOperator

private:
	TableListBox& owner;
	int row;
	bool isSelected, isDragging, selectRowOnMouseUp;
	BigInteger columnsWithComponents;

	Component* findChildComponentForColumn (const int columnId) const
	{
		for (int i = getNumChildComponents(); --i >= 0;)
		{
			Component* const c = getChildComponent (i);

			if ((int) c->getProperties() [tableColumnPropertyTag] == columnId)
				return c;
		}

		return 0;
	}

	TableListRowComp (const TableListRowComp&);
	TableListRowComp& operator= (const TableListRowComp&);
};

class TableListBoxHeader  : public TableHeaderComponent
{
public:
	TableListBoxHeader (TableListBox& owner_)
		: owner (owner_)
	{
	}

	~TableListBoxHeader()
	{
	}

	void addMenuItems (PopupMenu& menu, int columnIdClicked)
	{
		if (owner.isAutoSizeMenuOptionShown())
		{
			menu.addItem (0xf836743, TRANS("Auto-size this column"), columnIdClicked != 0);
			menu.addItem (0xf836744, TRANS("Auto-size all columns"), owner.getHeader()->getNumColumns (true) > 0);
			menu.addSeparator();
		}

		TableHeaderComponent::addMenuItems (menu, columnIdClicked);
	}

	void reactToMenuItem (int menuReturnId, int columnIdClicked)
	{
		if (menuReturnId == 0xf836743)
		{
			owner.autoSizeColumn (columnIdClicked);
		}
		else if (menuReturnId == 0xf836744)
		{
			owner.autoSizeAllColumns();
		}
		else
		{
			TableHeaderComponent::reactToMenuItem (menuReturnId, columnIdClicked);
		}
	}

	juce_UseDebuggingNewOperator

private:
	TableListBox& owner;

	TableListBoxHeader (const TableListBoxHeader&);
	TableListBoxHeader& operator= (const TableListBoxHeader&);
};

TableListBox::TableListBox (const String& name, TableListBoxModel* const model_)
	: ListBox (name, 0),
	  model (model_),
	  autoSizeOptionsShown (true)
{
	ListBox::model = this;

	header = new TableListBoxHeader (*this);
	header->setSize (100, 28);
	header->addListener (this);

	setHeaderComponent (header);
}

TableListBox::~TableListBox()
{
	deleteAllChildren();
}

void TableListBox::setModel (TableListBoxModel* const newModel)
{
	if (model != newModel)
	{
		model = newModel;
		updateContent();
	}
}

int TableListBox::getHeaderHeight() const
{
	return header->getHeight();
}

void TableListBox::setHeaderHeight (const int newHeight)
{
	header->setSize (header->getWidth(), newHeight);
	resized();
}

void TableListBox::autoSizeColumn (const int columnId)
{
	const int width = model != 0 ? model->getColumnAutoSizeWidth (columnId) : 0;

	if (width > 0)
		header->setColumnWidth (columnId, width);
}

void TableListBox::autoSizeAllColumns()
{
	for (int i = 0; i < header->getNumColumns (true); ++i)
		autoSizeColumn (header->getColumnIdOfIndex (i, true));
}

void TableListBox::setAutoSizeMenuOptionShown (const bool shouldBeShown)
{
	autoSizeOptionsShown = shouldBeShown;
}

bool TableListBox::isAutoSizeMenuOptionShown() const
{
	return autoSizeOptionsShown;
}

const Rectangle<int> TableListBox::getCellPosition (const int columnId,
													const int rowNumber,
													const bool relativeToComponentTopLeft) const
{
	Rectangle<int> headerCell (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));

	if (relativeToComponentTopLeft)
		headerCell.translate (header->getX(), 0);

	const Rectangle<int> row (getRowPosition (rowNumber, relativeToComponentTopLeft));

	return Rectangle<int> (headerCell.getX(), row.getY(),
						   headerCell.getWidth(), row.getHeight());
}

void TableListBox::scrollToEnsureColumnIsOnscreen (const int columnId)
{
	ScrollBar* const scrollbar = getHorizontalScrollBar();

	if (scrollbar != 0)
	{
		const Rectangle<int> pos (header->getColumnPosition (header->getIndexOfColumnId (columnId, true)));

		double x = scrollbar->getCurrentRangeStart();
		const double w = scrollbar->getCurrentRangeSize();

		if (pos.getX() < x)
			x = pos.getX();
		else if (pos.getRight() > x + w)
			x += jmax (0.0, pos.getRight() - (x + w));

		scrollbar->setCurrentRangeStart (x);
	}
}

int TableListBox::getNumRows()
{
	return model != 0 ? model->getNumRows() : 0;
}

void TableListBox::paintListBoxItem (int, Graphics&, int, int, bool)
{
}

Component* TableListBox::refreshComponentForRow (int rowNumber, bool isRowSelected_, Component* existingComponentToUpdate)
{
	if (existingComponentToUpdate == 0)
		existingComponentToUpdate = new TableListRowComp (*this);

	static_cast <TableListRowComp*> (existingComponentToUpdate)->update (rowNumber, isRowSelected_);

	return existingComponentToUpdate;
}

void TableListBox::selectedRowsChanged (int row)
{
	if (model != 0)
		model->selectedRowsChanged (row);
}

void TableListBox::deleteKeyPressed (int row)
{
	if (model != 0)
		model->deleteKeyPressed (row);
}

void TableListBox::returnKeyPressed (int row)
{
	if (model != 0)
		model->returnKeyPressed (row);
}

void TableListBox::backgroundClicked()
{
	if (model != 0)
		model->backgroundClicked();
}

void TableListBox::listWasScrolled()
{
	if (model != 0)
		model->listWasScrolled();
}

void TableListBox::tableColumnsChanged (TableHeaderComponent*)
{
	setMinimumContentWidth (header->getTotalWidth());
	repaint();
	updateColumnComponents();
}

void TableListBox::tableColumnsResized (TableHeaderComponent*)
{
	setMinimumContentWidth (header->getTotalWidth());
	repaint();
	updateColumnComponents();
}

void TableListBox::tableSortOrderChanged (TableHeaderComponent*)
{
	if (model != 0)
		model->sortOrderChanged (header->getSortColumnId(),
								 header->isSortedForwards());
}

void TableListBox::tableColumnDraggingChanged (TableHeaderComponent*, int columnIdNowBeingDragged_)
{
	columnIdNowBeingDragged = columnIdNowBeingDragged_;
	repaint();
}

void TableListBox::resized()
{
	ListBox::resized();

	header->resizeAllColumnsToFit (getVisibleContentWidth());
	setMinimumContentWidth (header->getTotalWidth());
}

void TableListBox::updateColumnComponents() const
{
	const int firstRow = getRowContainingPosition (0, 0);

	for (int i = firstRow + getNumRowsOnScreen() + 2; --i >= firstRow;)
	{
		TableListRowComp* const rowComp = dynamic_cast <TableListRowComp*> (getComponentForRowNumber (i));

		if (rowComp != 0)
			rowComp->resized();
	}
}

void TableListBoxModel::cellClicked (int, int, const MouseEvent&)
{
}

void TableListBoxModel::cellDoubleClicked (int, int, const MouseEvent&)
{
}

void TableListBoxModel::backgroundClicked()
{
}

void TableListBoxModel::sortOrderChanged (int, const bool)
{
}

int TableListBoxModel::getColumnAutoSizeWidth (int)
{
	return 0;
}

void TableListBoxModel::selectedRowsChanged (int)
{
}

void TableListBoxModel::deleteKeyPressed (int)
{
}

void TableListBoxModel::returnKeyPressed (int)
{
}

void TableListBoxModel::listWasScrolled()
{
}

const String TableListBoxModel::getCellTooltip (int /*rowNumber*/, int /*columnId*/)
{
	return String::empty;
}

const String TableListBoxModel::getDragSourceDescription (const SparseSet<int>&)
{
	return String::empty;
}

Component* TableListBoxModel::refreshComponentForCell (int, int, bool, Component* existingComponentToUpdate)
{
	(void) existingComponentToUpdate;
	jassert (existingComponentToUpdate == 0); // indicates a failure in the code the recycles the components
	return 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TableListBox.cpp ***/


/*** Start of inlined file: juce_TextEditor.cpp ***/
BEGIN_JUCE_NAMESPACE

// a word or space that can't be broken down any further
struct TextAtom
{

	String atomText;
	float width;
	uint16 numChars;

	bool isWhitespace() const	   { return CharacterFunctions::isWhitespace (atomText[0]); }
	bool isNewLine() const	  { return atomText[0] == '\r' || atomText[0] == '\n'; }

	const String getText (const juce_wchar passwordCharacter) const
	{
		if (passwordCharacter == 0)
			return atomText;
		else
			return String::repeatedString (String::charToString (passwordCharacter),
										   atomText.length());
	}

	const String getTrimmedText (const juce_wchar passwordCharacter) const
	{
		if (passwordCharacter == 0)
			return atomText.substring (0, numChars);
		else if (isNewLine())
			return String::empty;
		else
			return String::repeatedString (String::charToString (passwordCharacter), numChars);
	}
};

// a run of text with a single font and colour
class TextEditor::UniformTextSection
{
public:

	UniformTextSection (const String& text,
						const Font& font_,
						const Colour& colour_,
						const juce_wchar passwordCharacter)
	  : font (font_),
		colour (colour_)
	{
		initialiseAtoms (text, passwordCharacter);
	}

	UniformTextSection (const UniformTextSection& other)
	  : font (other.font),
		colour (other.colour)
	{
		atoms.ensureStorageAllocated (other.atoms.size());

		for (int i = 0; i < other.atoms.size(); ++i)
			atoms.add (new TextAtom (*other.atoms.getUnchecked(i)));
	}

	~UniformTextSection()
	{
		// (no need to delete the atoms, as they're explicitly deleted by the caller)
	}

	void clear()
	{
		for (int i = atoms.size(); --i >= 0;)
			delete getAtom(i);

		atoms.clear();
	}

	int getNumAtoms() const
	{
		return atoms.size();
	}

	TextAtom* getAtom (const int index) const throw()
	{
		return atoms.getUnchecked (index);
	}

	void append (const UniformTextSection& other, const juce_wchar passwordCharacter)
	{
		if (other.atoms.size() > 0)
		{
			TextAtom* const lastAtom = atoms.getLast();
			int i = 0;

			if (lastAtom != 0)
			{
				if (! CharacterFunctions::isWhitespace (lastAtom->atomText.getLastCharacter()))
				{
					TextAtom* const first = other.getAtom(0);

					if (! CharacterFunctions::isWhitespace (first->atomText[0]))
					{
						lastAtom->atomText += first->atomText;
						lastAtom->numChars = (uint16) (lastAtom->numChars + first->numChars);
						lastAtom->width = font.getStringWidthFloat (lastAtom->getText (passwordCharacter));
						delete first;
						++i;
					}
				}
			}

			atoms.ensureStorageAllocated (atoms.size() + other.atoms.size() - i);

			while (i < other.atoms.size())
			{
				atoms.add (other.getAtom(i));
				++i;
			}
		}
	}

	UniformTextSection* split (const int indexToBreakAt,
							   const juce_wchar passwordCharacter)
	{
		UniformTextSection* const section2 = new UniformTextSection (String::empty,
																	 font, colour,
																	 passwordCharacter);
		int index = 0;

		for (int i = 0; i < atoms.size(); ++i)
		{
			TextAtom* const atom = getAtom(i);

			const int nextIndex = index + atom->numChars;

			if (index == indexToBreakAt)
			{
				int j;
				for (j = i; j < atoms.size(); ++j)
					section2->atoms.add (getAtom (j));

				for (j = atoms.size(); --j >= i;)
					atoms.remove (j);

				break;
			}
			else if (indexToBreakAt >= index && indexToBreakAt < nextIndex)
			{
				TextAtom* const secondAtom = new TextAtom();

				secondAtom->atomText = atom->atomText.substring (indexToBreakAt - index);
				secondAtom->width = font.getStringWidthFloat (secondAtom->getText (passwordCharacter));
				secondAtom->numChars = (uint16) secondAtom->atomText.length();

				section2->atoms.add (secondAtom);

				atom->atomText = atom->atomText.substring (0, indexToBreakAt - index);
				atom->width = font.getStringWidthFloat (atom->getText (passwordCharacter));
				atom->numChars = (uint16) (indexToBreakAt - index);

				int j;
				for (j = i + 1; j < atoms.size(); ++j)
					section2->atoms.add (getAtom (j));

				for (j = atoms.size(); --j > i;)
					atoms.remove (j);

				break;
			}

			index = nextIndex;
		}

		return section2;
	}

	void appendAllText (String::Concatenator& concatenator) const
	{
		for (int i = 0; i < atoms.size(); ++i)
			concatenator.append (getAtom(i)->atomText);
	}

	void appendSubstring (String::Concatenator& concatenator,
						  const Range<int>& range) const
	{
		int index = 0;
		for (int i = 0; i < atoms.size(); ++i)
		{
			const TextAtom* const atom = getAtom (i);
			const int nextIndex = index + atom->numChars;

			if (range.getStart() < nextIndex)
			{
				if (range.getEnd() <= index)
					break;

				const Range<int> r ((range - index).getIntersectionWith (Range<int> (0, (int) atom->numChars)));

				if (! r.isEmpty())
					concatenator.append (atom->atomText.substring (r.getStart(), r.getEnd()));
			}

			index = nextIndex;
		}
	}

	int getTotalLength() const
	{
		int total = 0;

		for (int i = atoms.size(); --i >= 0;)
			total += getAtom(i)->numChars;

		return total;
	}

	void setFont (const Font& newFont,
				  const juce_wchar passwordCharacter)
	{
		if (font != newFont)
		{
			font = newFont;

			for (int i = atoms.size(); --i >= 0;)
			{
				TextAtom* const atom = atoms.getUnchecked(i);
				atom->width = newFont.getStringWidthFloat (atom->getText (passwordCharacter));
			}
		}
	}

	juce_UseDebuggingNewOperator

	Font font;
	Colour colour;

private:
	Array <TextAtom*> atoms;

	void initialiseAtoms (const String& textToParse,
						  const juce_wchar passwordCharacter)
	{
		int i = 0;
		const int len = textToParse.length();
		const juce_wchar* const text = textToParse;

		while (i < len)
		{
			int start = i;

			// create a whitespace atom unless it starts with non-ws
			if (CharacterFunctions::isWhitespace (text[i])
				 && text[i] != '\r'
				 && text[i] != '\n')
			{
				while (i < len
						&& CharacterFunctions::isWhitespace (text[i])
						&& text[i] != '\r'
						&& text[i] != '\n')
				{
					++i;
				}
			}
			else
			{
				if (text[i] == '\r')
				{
					++i;

					if ((i < len) && (text[i] == '\n'))
					{
						++start;
						++i;
					}
				}
				else if (text[i] == '\n')
				{
					++i;
				}
				else
				{
					while ((i < len) && ! CharacterFunctions::isWhitespace (text[i]))
						++i;
				}
			}

			TextAtom* const atom = new TextAtom();
			atom->atomText = String (text + start, i - start);

			atom->width = font.getStringWidthFloat (atom->getText (passwordCharacter));
			atom->numChars = (uint16) (i - start);

			atoms.add (atom);
		}
	}

	UniformTextSection& operator= (const UniformTextSection& other);
};

class TextEditor::Iterator
{
public:

	Iterator (const Array <UniformTextSection*>& sections_,
			  const float wordWrapWidth_,
			  const juce_wchar passwordCharacter_)
	  : indexInText (0),
		lineY (0),
		lineHeight (0),
		maxDescent (0),
		atomX (0),
		atomRight (0),
		atom (0),
		currentSection (0),
		sections (sections_),
		sectionIndex (0),
		atomIndex (0),
		wordWrapWidth (wordWrapWidth_),
		passwordCharacter (passwordCharacter_)
	{
		jassert (wordWrapWidth_ > 0);

		if (sections.size() > 0)
		{
			currentSection = sections.getUnchecked (sectionIndex);

			if (currentSection != 0)
				beginNewLine();
		}
	}

	Iterator (const Iterator& other)
	  : indexInText (other.indexInText),
		lineY (other.lineY),
		lineHeight (other.lineHeight),
		maxDescent (other.maxDescent),
		atomX (other.atomX),
		atomRight (other.atomRight),
		atom (other.atom),
		currentSection (other.currentSection),
		sections (other.sections),
		sectionIndex (other.sectionIndex),
		atomIndex (other.atomIndex),
		wordWrapWidth (other.wordWrapWidth),
		passwordCharacter (other.passwordCharacter),
		tempAtom (other.tempAtom)
	{
	}

	~Iterator()
	{
	}

	bool next()
	{
		if (atom == &tempAtom)
		{
			const int numRemaining = tempAtom.atomText.length() - tempAtom.numChars;

			if (numRemaining > 0)
			{
				tempAtom.atomText = tempAtom.atomText.substring (tempAtom.numChars);

				atomX = 0;

				if (tempAtom.numChars > 0)
					lineY += lineHeight;

				indexInText += tempAtom.numChars;

				GlyphArrangement g;
				g.addLineOfText (currentSection->font, atom->getText (passwordCharacter), 0.0f, 0.0f);

				int split;
				for (split = 0; split < g.getNumGlyphs(); ++split)
					if (shouldWrap (g.getGlyph (split).getRight()))
						break;

				if (split > 0 && split <= numRemaining)
				{
					tempAtom.numChars = (uint16) split;
					tempAtom.width = g.getGlyph (split - 1).getRight();
					atomRight = atomX + tempAtom.width;
					return true;
				}
			}
		}

		bool forceNewLine = false;

		if (sectionIndex >= sections.size())
		{
			moveToEndOfLastAtom();
			return false;
		}
		else if (atomIndex >= currentSection->getNumAtoms() - 1)
		{
			if (atomIndex >= currentSection->getNumAtoms())
			{
				if (++sectionIndex >= sections.size())
				{
					moveToEndOfLastAtom();
					return false;
				}

				atomIndex = 0;
				currentSection = sections.getUnchecked (sectionIndex);
			}
			else
			{
				const TextAtom* const lastAtom = currentSection->getAtom (atomIndex);

				if (! lastAtom->isWhitespace())
				{
					// handle the case where the last atom in a section is actually part of the same
					// word as the first atom of the next section...
					float right = atomRight + lastAtom->width;
					float lineHeight2 = lineHeight;
					float maxDescent2 = maxDescent;

					for (int section = sectionIndex + 1; section < sections.size(); ++section)
					{
						const UniformTextSection* const s = sections.getUnchecked (section);

						if (s->getNumAtoms() == 0)
							break;

						const TextAtom* const nextAtom = s->getAtom (0);

						if (nextAtom->isWhitespace())
							break;

						right += nextAtom->width;

						lineHeight2 = jmax (lineHeight2, s->font.getHeight());
						maxDescent2 = jmax (maxDescent2, s->font.getDescent());

						if (shouldWrap (right))
						{
							lineHeight = lineHeight2;
							maxDescent = maxDescent2;

							forceNewLine = true;
							break;
						}

						if (s->getNumAtoms() > 1)
							break;
					}
				}
			}
		}

		if (atom != 0)
		{
			atomX = atomRight;
			indexInText += atom->numChars;

			if (atom->isNewLine())
				beginNewLine();
		}

		atom = currentSection->getAtom (atomIndex);
		atomRight = atomX + atom->width;
		++atomIndex;

		if (shouldWrap (atomRight) || forceNewLine)
		{
			if (atom->isWhitespace())
			{
				// leave whitespace at the end of a line, but truncate it to avoid scrolling
				atomRight = jmin (atomRight, wordWrapWidth);
			}
			else
			{
				atomRight = atom->width;

				if (shouldWrap (atomRight))  // atom too big to fit on a line, so break it up..
				{
					tempAtom = *atom;
					tempAtom.width = 0;
					tempAtom.numChars = 0;
					atom = &tempAtom;

					if (atomX > 0)
						beginNewLine();

					return next();
				}

				beginNewLine();
				return true;
			}
		}

		return true;
	}

	void beginNewLine()
	{
		atomX = 0;
		lineY += lineHeight;

		int tempSectionIndex = sectionIndex;
		int tempAtomIndex = atomIndex;
		const UniformTextSection* section = sections.getUnchecked (tempSectionIndex);

		lineHeight = section->font.getHeight();
		maxDescent = section->font.getDescent();

		float x = (atom != 0) ? atom->width : 0;

		while (! shouldWrap (x))
		{
			if (tempSectionIndex >= sections.size())
				break;

			bool checkSize = false;

			if (tempAtomIndex >= section->getNumAtoms())
			{
				if (++tempSectionIndex >= sections.size())
					break;

				tempAtomIndex = 0;
				section = sections.getUnchecked (tempSectionIndex);
				checkSize = true;
			}

			const TextAtom* const nextAtom = section->getAtom (tempAtomIndex);

			if (nextAtom == 0)
				break;

			x += nextAtom->width;

			if (shouldWrap (x) || nextAtom->isNewLine())
				break;

			if (checkSize)
			{
				lineHeight = jmax (lineHeight, section->font.getHeight());
				maxDescent = jmax (maxDescent, section->font.getDescent());
			}

			++tempAtomIndex;
		}
	}

	void draw (Graphics& g, const UniformTextSection*& lastSection) const
	{
		if (passwordCharacter != 0 || ! atom->isWhitespace())
		{
			if (lastSection != currentSection)
			{
				lastSection = currentSection;
				g.setColour (currentSection->colour);
				g.setFont (currentSection->font);
			}

			jassert (atom->getTrimmedText (passwordCharacter).isNotEmpty());

			GlyphArrangement ga;
			ga.addLineOfText (currentSection->font,
							  atom->getTrimmedText (passwordCharacter),
							  atomX,
							  (float) roundToInt (lineY + lineHeight - maxDescent));
			ga.draw (g);
		}
	}

	void drawSelection (Graphics& g,
						const Range<int>& selection) const
	{
		const int startX = roundToInt (indexToX (selection.getStart()));
		const int endX   = roundToInt (indexToX (selection.getEnd()));

		const int y = roundToInt (lineY);
		const int nextY = roundToInt (lineY + lineHeight);

		g.fillRect (startX, y, endX - startX, nextY - y);
	}

	void drawSelectedText (Graphics& g,
						   const Range<int>& selection,
						   const Colour& selectedTextColour) const
	{
		if (passwordCharacter != 0 || ! atom->isWhitespace())
		{
			GlyphArrangement ga;
			ga.addLineOfText (currentSection->font,
							  atom->getTrimmedText (passwordCharacter),
							  atomX,
							  (float) roundToInt (lineY + lineHeight - maxDescent));

			if (selection.getEnd() < indexInText + atom->numChars)
			{
				GlyphArrangement ga2 (ga);
				ga2.removeRangeOfGlyphs (0, selection.getEnd() - indexInText);
				ga.removeRangeOfGlyphs (selection.getEnd() - indexInText, -1);

				g.setColour (currentSection->colour);
				ga2.draw (g);
			}

			if (selection.getStart() > indexInText)
			{
				GlyphArrangement ga2 (ga);
				ga2.removeRangeOfGlyphs (selection.getStart() - indexInText, -1);
				ga.removeRangeOfGlyphs (0, selection.getStart() - indexInText);

				g.setColour (currentSection->colour);
				ga2.draw (g);
			}

			g.setColour (selectedTextColour);
			ga.draw (g);
		}
	}

	float indexToX (const int indexToFind) const
	{
		if (indexToFind <= indexInText)
			return atomX;

		if (indexToFind >= indexInText + atom->numChars)
			return atomRight;

		GlyphArrangement g;
		g.addLineOfText (currentSection->font,
						 atom->getText (passwordCharacter),
						 atomX, 0.0f);

		if (indexToFind - indexInText >= g.getNumGlyphs())
			return atomRight;

		return jmin (atomRight, g.getGlyph (indexToFind - indexInText).getLeft());
	}

	int xToIndex (const float xToFind) const
	{
		if (xToFind <= atomX || atom->isNewLine())
			return indexInText;

		if (xToFind >= atomRight)
			return indexInText + atom->numChars;

		GlyphArrangement g;
		g.addLineOfText (currentSection->font,
						 atom->getText (passwordCharacter),
						 atomX, 0.0f);

		int j;
		for (j = 0; j < g.getNumGlyphs(); ++j)
			if ((g.getGlyph(j).getLeft() + g.getGlyph(j).getRight()) / 2 > xToFind)
				break;

		return indexInText + j;
	}

	bool getCharPosition (const int index, float& cx, float& cy, float& lineHeight_)
	{
		while (next())
		{
			if (indexInText + atom->numChars > index)
			{
				cx = indexToX (index);
				cy = lineY;
				lineHeight_ = lineHeight;
				return true;
			}
		}

		cx = atomX;
		cy = lineY;
		lineHeight_ = lineHeight;
		return false;
	}

	juce_UseDebuggingNewOperator

	int indexInText;
	float lineY, lineHeight, maxDescent;
	float atomX, atomRight;
	const TextAtom* atom;
	const UniformTextSection* currentSection;

private:
	const Array <UniformTextSection*>& sections;
	int sectionIndex, atomIndex;
	const float wordWrapWidth;
	const juce_wchar passwordCharacter;
	TextAtom tempAtom;

	Iterator& operator= (const Iterator&);

	void moveToEndOfLastAtom()
	{
		if (atom != 0)
		{
			atomX = atomRight;

			if (atom->isNewLine())
			{
				atomX = 0.0f;
				lineY += lineHeight;
			}
		}
	}

	bool shouldWrap (const float x) const
	{
		return (x - 0.0001f) >= wordWrapWidth;
	}
};

class TextEditor::InsertAction  : public UndoableAction
{
	TextEditor& owner;
	const String text;
	const int insertIndex, oldCaretPos, newCaretPos;
	const Font font;
	const Colour colour;

	InsertAction (const InsertAction&);
	InsertAction& operator= (const InsertAction&);

public:
	InsertAction (TextEditor& owner_,
				  const String& text_,
				  const int insertIndex_,
				  const Font& font_,
				  const Colour& colour_,
				  const int oldCaretPos_,
				  const int newCaretPos_)
		: owner (owner_),
		  text (text_),
		  insertIndex (insertIndex_),
		  oldCaretPos (oldCaretPos_),
		  newCaretPos (newCaretPos_),
		  font (font_),
		  colour (colour_)
	{
	}

	~InsertAction()
	{
	}

	bool perform()
	{
		owner.insert (text, insertIndex, font, colour, 0, newCaretPos);
		return true;
	}

	bool undo()
	{
		owner.remove (Range<int> (insertIndex, insertIndex + text.length()), 0, oldCaretPos);
		return true;
	}

	int getSizeInUnits()
	{
		return text.length() + 16;
	}
};

class TextEditor::RemoveAction  : public UndoableAction
{
	TextEditor& owner;
	const Range<int> range;
	const int oldCaretPos, newCaretPos;
	Array <UniformTextSection*> removedSections;

	RemoveAction (const RemoveAction&);
	RemoveAction& operator= (const RemoveAction&);

public:
	RemoveAction (TextEditor& owner_,
				  const Range<int> range_,
				  const int oldCaretPos_,
				  const int newCaretPos_,
				  const Array <UniformTextSection*>& removedSections_)
		: owner (owner_),
		  range (range_),
		  oldCaretPos (oldCaretPos_),
		  newCaretPos (newCaretPos_),
		  removedSections (removedSections_)
	{
	}

	~RemoveAction()
	{
		for (int i = removedSections.size(); --i >= 0;)
		{
			UniformTextSection* const section = removedSections.getUnchecked (i);
			section->clear();
			delete section;
		}
	}

	bool perform()
	{
		owner.remove (range, 0, newCaretPos);
		return true;
	}

	bool undo()
	{
		owner.reinsert (range.getStart(), removedSections);
		owner.moveCursorTo (oldCaretPos, false);
		return true;
	}

	int getSizeInUnits()
	{
		int n = 0;

		for (int i = removedSections.size(); --i >= 0;)
			n += removedSections.getUnchecked (i)->getTotalLength();

		return n + 16;
	}
};

class TextEditor::TextHolderComponent  : public Component,
										 public Timer,
										 public Value::Listener
{
public:
	TextHolderComponent (TextEditor& owner_)
		: owner (owner_)
	{
		setWantsKeyboardFocus (false);
		setInterceptsMouseClicks (false, true);

		owner.getTextValue().addListener (this);
	}

	~TextHolderComponent()
	{
		owner.getTextValue().removeListener (this);
	}

	void paint (Graphics& g)
	{
		owner.drawContent (g);
	}

	void timerCallback()
	{
		owner.timerCallbackInt();
	}

	const MouseCursor getMouseCursor()
	{
		return owner.getMouseCursor();
	}

	void valueChanged (Value&)
	{
		owner.textWasChangedByValue();
	}

private:
	TextEditor& owner;

	TextHolderComponent (const TextHolderComponent&);
	TextHolderComponent& operator= (const TextHolderComponent&);
};

class TextEditorViewport  : public Viewport
{
public:
	TextEditorViewport (TextEditor* const owner_)
		: owner (owner_),
		  lastWordWrapWidth (0)
	{
	}

	~TextEditorViewport()
	{
	}

	void visibleAreaChanged (int, int, int, int)
	{
		const float wordWrapWidth = owner->getWordWrapWidth();

		if (wordWrapWidth != lastWordWrapWidth)
		{
			lastWordWrapWidth = wordWrapWidth;
			owner->updateTextHolderSize();
		}
	}

private:
	TextEditor* const owner;
	float lastWordWrapWidth;

	TextEditorViewport (const TextEditorViewport&);
	TextEditorViewport& operator= (const TextEditorViewport&);
};

namespace TextEditorDefs
{
	const int flashSpeedIntervalMs = 380;

	const int textChangeMessageId = 0x10003001;
	const int returnKeyMessageId  = 0x10003002;
	const int escapeKeyMessageId  = 0x10003003;
	const int focusLossMessageId  = 0x10003004;

	const int maxActionsPerTransaction = 100;
}

TextEditor::TextEditor (const String& name,
						const juce_wchar passwordCharacter_)
	: Component (name),
	  borderSize (1, 1, 1, 3),
	  readOnly (false),
	  multiline (false),
	  wordWrap (false),
	  returnKeyStartsNewLine (false),
	  caretVisible (true),
	  popupMenuEnabled (true),
	  selectAllTextWhenFocused (false),
	  scrollbarVisible (true),
	  wasFocused (false),
	  caretFlashState (true),
	  keepCursorOnScreen (true),
	  tabKeyUsed (false),
	  menuActive (false),
	  valueTextNeedsUpdating (false),
	  cursorX (0),
	  cursorY (0),
	  cursorHeight (0),
	  maxTextLength (0),
	  leftIndent (4),
	  topIndent (4),
	  lastTransactionTime (0),
	  currentFont (14.0f),
	  totalNumChars (0),
	  caretPosition (0),
	  passwordCharacter (passwordCharacter_),
	  dragType (notDragging)
{
	setOpaque (true);

	addAndMakeVisible (viewport = new TextEditorViewport (this));
	viewport->setViewedComponent (textHolder = new TextHolderComponent (*this));
	viewport->setWantsKeyboardFocus (false);
	viewport->setScrollBarsShown (false, false);

	setMouseCursor (MouseCursor::IBeamCursor);
	setWantsKeyboardFocus (true);
}

TextEditor::~TextEditor()
{
	textValue.referTo (Value());
	clearInternal (0);
	viewport = 0;
	textHolder = 0;
}

void TextEditor::newTransaction()
{
	lastTransactionTime = Time::getApproximateMillisecondCounter();
	undoManager.beginNewTransaction();
}

void TextEditor::doUndoRedo (const bool isRedo)
{
	if (! isReadOnly())
	{
		if (isRedo ? undoManager.redo()
				   : undoManager.undo())
		{
			scrollToMakeSureCursorIsVisible();
			repaint();
			textChanged();
		}
	}
}

void TextEditor::setMultiLine (const bool shouldBeMultiLine,
							   const bool shouldWordWrap)
{
	multiline = shouldBeMultiLine;
	wordWrap = shouldWordWrap && shouldBeMultiLine;

	setScrollbarsShown (scrollbarVisible);

	viewport->setViewPosition (0, 0);

	resized();
	scrollToMakeSureCursorIsVisible();
}

bool TextEditor::isMultiLine() const
{
	return multiline;
}

void TextEditor::setScrollbarsShown (bool enabled)
{
	scrollbarVisible = enabled;

	enabled = enabled && isMultiLine();

	viewport->setScrollBarsShown (enabled, enabled);
}

void TextEditor::setReadOnly (const bool shouldBeReadOnly)
{
	readOnly = shouldBeReadOnly;
	enablementChanged();
}

bool TextEditor::isReadOnly() const
{
	return readOnly || ! isEnabled();
}

void TextEditor::setReturnKeyStartsNewLine (const bool shouldStartNewLine)
{
	returnKeyStartsNewLine = shouldStartNewLine;
}

void TextEditor::setTabKeyUsedAsCharacter (const bool shouldTabKeyBeUsed)
{
	tabKeyUsed = shouldTabKeyBeUsed;
}

void TextEditor::setPopupMenuEnabled (const bool b)
{
	popupMenuEnabled = b;
}

void TextEditor::setSelectAllWhenFocused (const bool b)
{
	selectAllTextWhenFocused = b;
}

const Font TextEditor::getFont() const
{
	return currentFont;
}

void TextEditor::setFont (const Font& newFont)
{
	currentFont = newFont;
	scrollToMakeSureCursorIsVisible();
}

void TextEditor::applyFontToAllText (const Font& newFont)
{
	currentFont = newFont;

	const Colour overallColour (findColour (textColourId));

	for (int i = sections.size(); --i >= 0;)
	{
		UniformTextSection* const uts = sections.getUnchecked (i);
		uts->setFont (newFont, passwordCharacter);
		uts->colour = overallColour;
	}

	coalesceSimilarSections();
	updateTextHolderSize();
	scrollToMakeSureCursorIsVisible();
	repaint();
}

void TextEditor::colourChanged()
{
	setOpaque (findColour (backgroundColourId).isOpaque());
	repaint();
}

void TextEditor::setCaretVisible (const bool shouldCaretBeVisible)
{
	caretVisible = shouldCaretBeVisible;

	if (shouldCaretBeVisible)
		textHolder->startTimer (TextEditorDefs::flashSpeedIntervalMs);

	setMouseCursor (shouldCaretBeVisible ? MouseCursor::IBeamCursor
										 : MouseCursor::NormalCursor);
}

void TextEditor::setInputRestrictions (const int maxLen,
									   const String& chars)
{
	maxTextLength = jmax (0, maxLen);
	allowedCharacters = chars;
}

void TextEditor::setTextToShowWhenEmpty (const String& text, const Colour& colourToUse)
{
	textToShowWhenEmpty = text;
	colourForTextWhenEmpty = colourToUse;
}

void TextEditor::setPasswordCharacter (const juce_wchar newPasswordCharacter)
{
	if (passwordCharacter != newPasswordCharacter)
	{
		passwordCharacter = newPasswordCharacter;
		resized();
		repaint();
	}
}

void TextEditor::setScrollBarThickness (const int newThicknessPixels)
{
	viewport->setScrollBarThickness (newThicknessPixels);
}

void TextEditor::setScrollBarButtonVisibility (const bool buttonsVisible)
{
	viewport->setScrollBarButtonVisibility (buttonsVisible);
}

void TextEditor::clear()
{
	clearInternal (0);
	updateTextHolderSize();
	undoManager.clearUndoHistory();
}

void TextEditor::setText (const String& newText,
						  const bool sendTextChangeMessage)
{
	const int newLength = newText.length();

	if (newLength != getTotalNumChars() || getText() != newText)
	{
		const int oldCursorPos = caretPosition;
		const bool cursorWasAtEnd = oldCursorPos >= getTotalNumChars();

		clearInternal (0);
		insert (newText, 0, currentFont, findColour (textColourId), 0, caretPosition);

		// if you're adding text with line-feeds to a single-line text editor, it
		// ain't gonna look right!
		jassert (multiline || ! newText.containsAnyOf ("\r\n"));

		if (cursorWasAtEnd && ! isMultiLine())
			moveCursorTo (getTotalNumChars(), false);
		else
			moveCursorTo (oldCursorPos, false);

		if (sendTextChangeMessage)
			textChanged();

		repaint();
	}

	updateTextHolderSize();
	scrollToMakeSureCursorIsVisible();
	undoManager.clearUndoHistory();
}

Value& TextEditor::getTextValue()
{
	if (valueTextNeedsUpdating)
	{
		valueTextNeedsUpdating = false;
		textValue = getText();
	}

	return textValue;
}

void TextEditor::textWasChangedByValue()
{
	if (textValue.getValueSource().getReferenceCount() > 1)
		setText (textValue.getValue());
}

void TextEditor::textChanged()
{
	updateTextHolderSize();
	postCommandMessage (TextEditorDefs::textChangeMessageId);

	if (textValue.getValueSource().getReferenceCount() > 1)
	{
		valueTextNeedsUpdating = false;
		textValue = getText();
	}
}

void TextEditor::returnPressed()
{
	postCommandMessage (TextEditorDefs::returnKeyMessageId);
}

void TextEditor::escapePressed()
{
	postCommandMessage (TextEditorDefs::escapeKeyMessageId);
}

void TextEditor::addListener (TextEditorListener* const newListener)
{
	listeners.add (newListener);
}

void TextEditor::removeListener (TextEditorListener* const listenerToRemove)
{
	listeners.remove (listenerToRemove);
}

void TextEditor::timerCallbackInt()
{
	const bool newState = (! caretFlashState) && ! isCurrentlyBlockedByAnotherModalComponent();

	if (caretFlashState != newState)
	{
		caretFlashState = newState;

		if (caretFlashState)
			wasFocused = true;

		if (caretVisible
			 && hasKeyboardFocus (false)
			 && ! isReadOnly())
		{
			repaintCaret();
		}
	}

	const unsigned int now = Time::getApproximateMillisecondCounter();

	if (now > lastTransactionTime + 200)
		newTransaction();
}

void TextEditor::repaintCaret()
{
	if (! findColour (caretColourId).isTransparent())
		repaint (borderSize.getLeft() + textHolder->getX() + leftIndent + roundToInt (cursorX) - 1,
				 borderSize.getTop() + textHolder->getY() + topIndent + roundToInt (cursorY) - 1,
				 4,
				 roundToInt (cursorHeight) + 2);
}

void TextEditor::repaintText (const Range<int>& range)
{
	if (! range.isEmpty())
	{
		float x = 0, y = 0, lh = currentFont.getHeight();

		const float wordWrapWidth = getWordWrapWidth();

		if (wordWrapWidth > 0)
		{
			Iterator i (sections, wordWrapWidth, passwordCharacter);

			i.getCharPosition (range.getStart(), x, y, lh);

			const int y1 = (int) y;
			int y2;

			if (range.getEnd() >= getTotalNumChars())
			{
				y2 = textHolder->getHeight();
			}
			else
			{
				i.getCharPosition (range.getEnd(), x, y, lh);
				y2 = (int) (y + lh * 2.0f);
			}

			textHolder->repaint (0, y1, textHolder->getWidth(), y2 - y1);
		}
	}
}

void TextEditor::moveCaret (int newCaretPos)
{
	if (newCaretPos < 0)
		newCaretPos = 0;
	else if (newCaretPos > getTotalNumChars())
		newCaretPos = getTotalNumChars();

	if (newCaretPos != getCaretPosition())
	{
		repaintCaret();
		caretFlashState = true;
		caretPosition = newCaretPos;
		textHolder->startTimer (TextEditorDefs::flashSpeedIntervalMs);
		scrollToMakeSureCursorIsVisible();
		repaintCaret();
	}
}

void TextEditor::setCaretPosition (const int newIndex)
{
	moveCursorTo (newIndex, false);
}

int TextEditor::getCaretPosition() const
{
	return caretPosition;
}

void TextEditor::scrollEditorToPositionCaret (const int desiredCaretX,
											  const int desiredCaretY)

{
	updateCaretPosition();

	int vx = roundToInt (cursorX) - desiredCaretX;
	int vy = roundToInt (cursorY) - desiredCaretY;

	if (desiredCaretX < jmax (1, proportionOfWidth (0.05f)))
	{
		vx += desiredCaretX - proportionOfWidth (0.2f);
	}
	else if (desiredCaretX > jmax (0, viewport->getMaximumVisibleWidth() - (wordWrap ? 2 : 10)))
	{
		vx += desiredCaretX + (isMultiLine() ? proportionOfWidth (0.2f) : 10) - viewport->getMaximumVisibleWidth();
	}

	vx = jlimit (0, jmax (0, textHolder->getWidth() + 8 - viewport->getMaximumVisibleWidth()), vx);

	if (! isMultiLine())
	{
		vy = viewport->getViewPositionY();
	}
	else
	{
		vy = jlimit (0, jmax (0, textHolder->getHeight() - viewport->getMaximumVisibleHeight()), vy);

		const int curH = roundToInt (cursorHeight);

		if (desiredCaretY < 0)
		{
			vy = jmax (0, desiredCaretY + vy);
		}
		else if (desiredCaretY > jmax (0, viewport->getMaximumVisibleHeight() - topIndent - curH))
		{
			vy += desiredCaretY + 2 + curH + topIndent - viewport->getMaximumVisibleHeight();
		}
	}

	viewport->setViewPosition (vx, vy);
}

const Rectangle<int> TextEditor::getCaretRectangle()
{
	updateCaretPosition();

	return Rectangle<int> (roundToInt (cursorX) - viewport->getX(),
						   roundToInt (cursorY) - viewport->getY(),
						   1, roundToInt (cursorHeight));
}

float TextEditor::getWordWrapWidth() const
{
	return (wordWrap) ? (float) (viewport->getMaximumVisibleWidth() - leftIndent - leftIndent / 2)
					  : 1.0e10f;
}

void TextEditor::updateTextHolderSize()
{
	const float wordWrapWidth = getWordWrapWidth();

	if (wordWrapWidth > 0)
	{
		float maxWidth = 0.0f;

		Iterator i (sections, wordWrapWidth, passwordCharacter);

		while (i.next())
			maxWidth = jmax (maxWidth, i.atomRight);

		const int w = leftIndent + roundToInt (maxWidth);
		const int h = topIndent + roundToInt (jmax (i.lineY + i.lineHeight,
													currentFont.getHeight()));

		textHolder->setSize (w + 1, h + 1);
	}
}

int TextEditor::getTextWidth() const
{
	return textHolder->getWidth();
}

int TextEditor::getTextHeight() const
{
	return textHolder->getHeight();
}

void TextEditor::setIndents (const int newLeftIndent,
							 const int newTopIndent)
{
	leftIndent = newLeftIndent;
	topIndent = newTopIndent;
}

void TextEditor::setBorder (const BorderSize& border)
{
	borderSize = border;
	resized();
}

const BorderSize TextEditor::getBorder() const
{
	return borderSize;
}

void TextEditor::setScrollToShowCursor (const bool shouldScrollToShowCursor)
{
	keepCursorOnScreen = shouldScrollToShowCursor;
}

void TextEditor::updateCaretPosition()
{
	cursorHeight = currentFont.getHeight(); // (in case the text is empty and the call below doesn't set this value)
	getCharPosition (caretPosition, cursorX, cursorY, cursorHeight);
}

void TextEditor::scrollToMakeSureCursorIsVisible()
{
	updateCaretPosition();

	if (keepCursorOnScreen)
	{
		int x = viewport->getViewPositionX();
		int y = viewport->getViewPositionY();

		const int relativeCursorX = roundToInt (cursorX) - x;
		const int relativeCursorY = roundToInt (cursorY) - y;

		if (relativeCursorX < jmax (1, proportionOfWidth (0.05f)))
		{
			x += relativeCursorX - proportionOfWidth (0.2f);
		}
		else if (relativeCursorX > jmax (0, viewport->getMaximumVisibleWidth() - (wordWrap ? 2 : 10)))
		{
			x += relativeCursorX + (isMultiLine() ? proportionOfWidth (0.2f) : 10) - viewport->getMaximumVisibleWidth();
		}

		x = jlimit (0, jmax (0, textHolder->getWidth() + 8 - viewport->getMaximumVisibleWidth()), x);

		if (! isMultiLine())
		{
			y = (getHeight() - textHolder->getHeight() - topIndent) / -2;
		}
		else
		{
			const int curH = roundToInt (cursorHeight);

			if (relativeCursorY < 0)
			{
				y = jmax (0, relativeCursorY + y);
			}
			else if (relativeCursorY > jmax (0, viewport->getMaximumVisibleHeight() - topIndent - curH))
			{
				y += relativeCursorY + 2 + curH + topIndent - viewport->getMaximumVisibleHeight();
			}
		}

		viewport->setViewPosition (x, y);
	}
}

void TextEditor::moveCursorTo (const int newPosition,
							   const bool isSelecting)
{
	if (isSelecting)
	{
		moveCaret (newPosition);

		const Range<int> oldSelection (selection);

		if (dragType == notDragging)
		{
			if (abs (getCaretPosition() - selection.getStart()) < abs (getCaretPosition() - selection.getEnd()))
				dragType = draggingSelectionStart;
			else
				dragType = draggingSelectionEnd;
		}

		if (dragType == draggingSelectionStart)
		{
			if (getCaretPosition() >= selection.getEnd())
				dragType = draggingSelectionEnd;

			selection = Range<int>::between (getCaretPosition(), selection.getEnd());
		}
		else
		{
			if (getCaretPosition() < selection.getStart())
				dragType = draggingSelectionStart;

			selection = Range<int>::between (getCaretPosition(), selection.getStart());
		}

		repaintText (selection.getUnionWith (oldSelection));
	}
	else
	{
		dragType = notDragging;

		repaintText (selection);

		moveCaret (newPosition);
		selection = Range<int>::emptyRange (getCaretPosition());
	}
}

int TextEditor::getTextIndexAt (const int x,
								const int y)
{
	return indexAtPosition ((float) (x + viewport->getViewPositionX() - leftIndent),
							(float) (y + viewport->getViewPositionY() - topIndent));
}

void TextEditor::insertTextAtCaret (const String& newText_)
{
	String newText (newText_);

	if (allowedCharacters.isNotEmpty())
		newText = newText.retainCharacters (allowedCharacters);

	if ((! returnKeyStartsNewLine) && newText == "\n")
	{
		returnPressed();
		return;
	}

	if (! isMultiLine())
		newText = newText.replaceCharacters ("\r\n", "  ");
	else
		newText = newText.replace ("\r\n", "\n");

	const int newCaretPos = selection.getStart() + newText.length();
	const int insertIndex = selection.getStart();

	remove (selection, getUndoManager(),
			newText.isNotEmpty() ? newCaretPos - 1 : newCaretPos);

	if (maxTextLength > 0)
		newText = newText.substring (0, maxTextLength - getTotalNumChars());

	if (newText.isNotEmpty())
		insert (newText,
				insertIndex,
				currentFont,
				findColour (textColourId),
				getUndoManager(),
				newCaretPos);

	textChanged();
}

void TextEditor::setHighlightedRegion (const Range<int>& newSelection)
{
	moveCursorTo (newSelection.getStart(), false);
	moveCursorTo (newSelection.getEnd(), true);
}

void TextEditor::copy()
{
	if (passwordCharacter == 0)
	{
		const String selection (getHighlightedText());

		if (selection.isNotEmpty())
			SystemClipboard::copyTextToClipboard (selection);
	}
}

void TextEditor::paste()
{
	if (! isReadOnly())
	{
		const String clip (SystemClipboard::getTextFromClipboard());

		if (clip.isNotEmpty())
			insertTextAtCaret (clip);
	}
}

void TextEditor::cut()
{
	if (! isReadOnly())
	{
		moveCaret (selection.getEnd());
		insertTextAtCaret (String::empty);
	}
}

void TextEditor::drawContent (Graphics& g)
{
	const float wordWrapWidth = getWordWrapWidth();

	if (wordWrapWidth > 0)
	{
		g.setOrigin (leftIndent, topIndent);
		const Rectangle<int> clip (g.getClipBounds());
		Colour selectedTextColour;

		Iterator i (sections, wordWrapWidth, passwordCharacter);

		while (i.lineY + 200.0 < clip.getY() && i.next())
		{}

		if (! selection.isEmpty())
		{
			g.setColour (findColour (highlightColourId)
							.withMultipliedAlpha (hasKeyboardFocus (true) ? 1.0f : 0.5f));

			selectedTextColour = findColour (highlightedTextColourId);

			Iterator i2 (i);

			while (i2.next() && i2.lineY < clip.getBottom())
			{
				if (i2.lineY + i2.lineHeight >= clip.getY()
					 && selection.intersects (Range<int> (i2.indexInText, i2.indexInText + i2.atom->numChars)))
				{
					i2.drawSelection (g, selection);
				}
			}
		}

		const UniformTextSection* lastSection = 0;

		while (i.next() && i.lineY < clip.getBottom())
		{
			if (i.lineY + i.lineHeight >= clip.getY())
			{
				if (selection.intersects (Range<int> (i.indexInText, i.indexInText + i.atom->numChars)))
				{
					i.drawSelectedText (g, selection, selectedTextColour);
					lastSection = 0;
				}
				else
				{
					i.draw (g, lastSection);
				}
			}
		}
	}
}

void TextEditor::paint (Graphics& g)
{
	getLookAndFeel().fillTextEditorBackground (g, getWidth(), getHeight(), *this);
}

void TextEditor::paintOverChildren (Graphics& g)
{
	if (caretFlashState
		 && hasKeyboardFocus (false)
		 && caretVisible
		 && ! isReadOnly())
	{
		g.setColour (findColour (caretColourId));

		g.fillRect (borderSize.getLeft() + textHolder->getX() + leftIndent + cursorX,
					borderSize.getTop() + textHolder->getY() + topIndent + cursorY,
					2.0f, cursorHeight);
	}

	if (textToShowWhenEmpty.isNotEmpty()
		 && (! hasKeyboardFocus (false))
		 && getTotalNumChars() == 0)
	{
		g.setColour (colourForTextWhenEmpty);
		g.setFont (getFont());

		if (isMultiLine())
		{
			g.drawText (textToShowWhenEmpty,
						0, 0, getWidth(), getHeight(),
						Justification::centred, true);
		}
		else
		{
			g.drawText (textToShowWhenEmpty,
						leftIndent, topIndent,
						viewport->getWidth() - leftIndent,
						viewport->getHeight() - topIndent,
						Justification::centredLeft, true);
		}
	}

	getLookAndFeel().drawTextEditorOutline (g, getWidth(), getHeight(), *this);
}

void TextEditor::mouseDown (const MouseEvent& e)
{
	beginDragAutoRepeat (100);
	newTransaction();

	if (wasFocused || ! selectAllTextWhenFocused)
	{
		if (! (popupMenuEnabled && e.mods.isPopupMenu()))
		{
			moveCursorTo (getTextIndexAt (e.x, e.y),
						  e.mods.isShiftDown());
		}
		else
		{
			PopupMenu m;
			m.setLookAndFeel (&getLookAndFeel());
			addPopupMenuItems (m, &e);

			menuActive = true;
			const int result = m.show();
			menuActive = false;

			if (result != 0)
				performPopupMenuAction (result);
		}
	}
}

void TextEditor::mouseDrag (const MouseEvent& e)
{
	if (wasFocused || ! selectAllTextWhenFocused)
	{
		if (! (popupMenuEnabled && e.mods.isPopupMenu()))
		{
			moveCursorTo (getTextIndexAt (e.x, e.y), true);
		}
	}
}

void TextEditor::mouseUp (const MouseEvent& e)
{
	newTransaction();
	textHolder->startTimer (TextEditorDefs::flashSpeedIntervalMs);

	if (wasFocused || ! selectAllTextWhenFocused)
	{
		if (e.mouseWasClicked() && ! (popupMenuEnabled && e.mods.isPopupMenu()))
		{
			moveCaret (getTextIndexAt (e.x, e.y));
		}
	}

	wasFocused = true;
}

void TextEditor::mouseDoubleClick (const MouseEvent& e)
{
	int tokenEnd = getTextIndexAt (e.x, e.y);
	int tokenStart = tokenEnd;

	if (e.getNumberOfClicks() > 3)
	{
		tokenStart = 0;
		tokenEnd = getTotalNumChars();
	}
	else
	{
		const String t (getText());
		const int totalLength = getTotalNumChars();

		while (tokenEnd < totalLength)
		{
			if (CharacterFunctions::isLetterOrDigit (t [tokenEnd]))
				++tokenEnd;
			else
				break;
		}

		tokenStart = tokenEnd;

		while (tokenStart > 0)
		{
			if (CharacterFunctions::isLetterOrDigit (t [tokenStart - 1]))
				--tokenStart;
			else
				break;
		}

		if (e.getNumberOfClicks() > 2)
		{
			while (tokenEnd < totalLength)
			{
				if (t [tokenEnd] != '\r' && t [tokenEnd] != '\n')
					++tokenEnd;
				else
					break;
			}

			while (tokenStart > 0)
			{
				if (t [tokenStart - 1] != '\r' && t [tokenStart - 1] != '\n')
					--tokenStart;
				else
					break;
			}
		}
	}

	moveCursorTo (tokenEnd, false);
	moveCursorTo (tokenStart, true);
}

void TextEditor::mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
	if (! viewport->useMouseWheelMoveIfNeeded (e, wheelIncrementX, wheelIncrementY))
		Component::mouseWheelMove (e, wheelIncrementX, wheelIncrementY);
}

bool TextEditor::keyPressed (const KeyPress& key)
{
	if (isReadOnly() && key != KeyPress ('c', ModifierKeys::commandModifier, 0))
		return false;

	const bool moveInWholeWordSteps = key.getModifiers().isCtrlDown() || key.getModifiers().isAltDown();

	if (key.isKeyCode (KeyPress::leftKey)
		 || key.isKeyCode (KeyPress::upKey))
	{
		newTransaction();

		int newPos;

		if (isMultiLine() && key.isKeyCode (KeyPress::upKey))
			newPos = indexAtPosition (cursorX, cursorY - 1);
		else if (moveInWholeWordSteps)
			newPos = findWordBreakBefore (getCaretPosition());
		else
			newPos = getCaretPosition() - 1;

		moveCursorTo (newPos, key.getModifiers().isShiftDown());
	}
	else if (key.isKeyCode (KeyPress::rightKey)
			  || key.isKeyCode (KeyPress::downKey))
	{
		newTransaction();

		int newPos;

		if (isMultiLine() && key.isKeyCode (KeyPress::downKey))
			newPos = indexAtPosition (cursorX, cursorY + cursorHeight + 1);
		else if (moveInWholeWordSteps)
			newPos = findWordBreakAfter (getCaretPosition());
		else
			newPos = getCaretPosition() + 1;

		moveCursorTo (newPos, key.getModifiers().isShiftDown());
	}
	else if (key.isKeyCode (KeyPress::pageDownKey) && isMultiLine())
	{
		newTransaction();

		moveCursorTo (indexAtPosition (cursorX, cursorY + cursorHeight + viewport->getViewHeight()),
					  key.getModifiers().isShiftDown());
	}
	else if (key.isKeyCode (KeyPress::pageUpKey) && isMultiLine())
	{
		newTransaction();

		moveCursorTo (indexAtPosition (cursorX, cursorY - viewport->getViewHeight()),
					  key.getModifiers().isShiftDown());
	}
	else if (key.isKeyCode (KeyPress::homeKey))
	{
		newTransaction();

		if (isMultiLine() && ! moveInWholeWordSteps)
			moveCursorTo (indexAtPosition (0.0f, cursorY),
						  key.getModifiers().isShiftDown());
		else
			moveCursorTo (0, key.getModifiers().isShiftDown());
	}
	else if (key.isKeyCode (KeyPress::endKey))
	{
		newTransaction();

		if (isMultiLine() && ! moveInWholeWordSteps)
			moveCursorTo (indexAtPosition ((float) textHolder->getWidth(), cursorY),
						  key.getModifiers().isShiftDown());
		else
			moveCursorTo (getTotalNumChars(), key.getModifiers().isShiftDown());
	}
	else if (key.isKeyCode (KeyPress::backspaceKey))
	{
		if (moveInWholeWordSteps)
		{
			moveCursorTo (findWordBreakBefore (getCaretPosition()), true);
		}
		else
		{
			if (selection.isEmpty() && selection.getStart() > 0)
				selection.setStart (selection.getEnd() - 1);
		}

		cut();
	}
	else if (key.isKeyCode (KeyPress::deleteKey))
	{
		if (key.getModifiers().isShiftDown())
			copy();

		if (selection.isEmpty() && selection.getStart() < getTotalNumChars())
			selection.setEnd (selection.getStart() + 1);

		cut();
	}
	else if (key == KeyPress ('c', ModifierKeys::commandModifier, 0)
			  || key == KeyPress (KeyPress::insertKey, ModifierKeys::ctrlModifier, 0))
	{
		newTransaction();
		copy();
	}
	else if (key == KeyPress ('x', ModifierKeys::commandModifier, 0))
	{
		newTransaction();
		copy();
		cut();
	}
	else if (key == KeyPress ('v', ModifierKeys::commandModifier, 0)
			  || key == KeyPress (KeyPress::insertKey, ModifierKeys::shiftModifier, 0))
	{
		newTransaction();
		paste();
	}
	else if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
	{
		newTransaction();
		doUndoRedo (false);
	}
	else if (key == KeyPress ('y', ModifierKeys::commandModifier, 0))
	{
		newTransaction();
		doUndoRedo (true);
	}
	else if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
	{
		newTransaction();
		moveCursorTo (getTotalNumChars(), false);
		moveCursorTo (0, true);
	}
	else if (key == KeyPress::returnKey)
	{
		newTransaction();
		insertTextAtCaret ("\n");
	}
	else if (key.isKeyCode (KeyPress::escapeKey))
	{
		newTransaction();
		moveCursorTo (getCaretPosition(), false);
		escapePressed();
	}
	else if (key.getTextCharacter() >= ' '
			  || (tabKeyUsed && (key.getTextCharacter() == '\t')))
	{
		insertTextAtCaret (String::charToString (key.getTextCharacter()));

		lastTransactionTime = Time::getApproximateMillisecondCounter();
	}
	else
	{
		return false;
	}

	return true;
}

bool TextEditor::keyStateChanged (const bool isKeyDown)
{
	if (! isKeyDown)
		return false;

#if JUCE_WIN32
	if (KeyPress (KeyPress::F4Key, ModifierKeys::altModifier, 0).isCurrentlyDown())
		return false;  // We need to explicitly allow alt-F4 to pass through on Windows
#endif

	// (overridden to avoid forwarding key events to the parent)
	return ! ModifierKeys::getCurrentModifiers().isCommandDown();
}

const int baseMenuItemID = 0x7fff0000;

void TextEditor::addPopupMenuItems (PopupMenu& m, const MouseEvent*)
{
	const bool writable = ! isReadOnly();

	if (passwordCharacter == 0)
	{
		m.addItem (baseMenuItemID + 1, TRANS("cut"), writable);
		m.addItem (baseMenuItemID + 2, TRANS("copy"), ! selection.isEmpty());
		m.addItem (baseMenuItemID + 3, TRANS("paste"), writable);
	}

	m.addItem (baseMenuItemID + 4, TRANS("delete"), writable);
	m.addSeparator();
	m.addItem (baseMenuItemID + 5, TRANS("select all"));
	m.addSeparator();

	if (getUndoManager() != 0)
	{
		m.addItem (baseMenuItemID + 6, TRANS("undo"), undoManager.canUndo());
		m.addItem (baseMenuItemID + 7, TRANS("redo"), undoManager.canRedo());
	}
}

void TextEditor::performPopupMenuAction (const int menuItemID)
{
	switch (menuItemID)
	{
	case baseMenuItemID + 1:
		copy();
		cut();
		break;

	case baseMenuItemID + 2:
		copy();
		break;

	case baseMenuItemID + 3:
		paste();
		break;

	case baseMenuItemID + 4:
		cut();
		break;

	case baseMenuItemID + 5:
		moveCursorTo (getTotalNumChars(), false);
		moveCursorTo (0, true);
		break;

	case baseMenuItemID + 6:
		doUndoRedo (false);
		break;

	case baseMenuItemID + 7:
		doUndoRedo (true);
		break;

	default:
		break;
	}
}

void TextEditor::focusGained (FocusChangeType)
{
	newTransaction();

	caretFlashState = true;

	if (selectAllTextWhenFocused)
	{
		moveCursorTo (0, false);
		moveCursorTo (getTotalNumChars(), true);
	}

	repaint();

	if (caretVisible)
		textHolder->startTimer (TextEditorDefs::flashSpeedIntervalMs);

	ComponentPeer* const peer = getPeer();
	if (peer != 0 && ! isReadOnly())
		peer->textInputRequired (getScreenPosition() - peer->getScreenPosition());
}

void TextEditor::focusLost (FocusChangeType)
{
	newTransaction();

	wasFocused = false;
	textHolder->stopTimer();
	caretFlashState = false;

	postCommandMessage (TextEditorDefs::focusLossMessageId);
	repaint();
}

void TextEditor::resized()
{
	viewport->setBoundsInset (borderSize);
	viewport->setSingleStepSizes (16, roundToInt (currentFont.getHeight()));

	updateTextHolderSize();

	if (! isMultiLine())
	{
		scrollToMakeSureCursorIsVisible();
	}
	else
	{
		updateCaretPosition();
	}
}

void TextEditor::handleCommandMessage (const int commandId)
{
	Component::BailOutChecker checker (this);

	switch (commandId)
	{
	case TextEditorDefs::textChangeMessageId:
		listeners.callChecked (checker, &TextEditorListener::textEditorTextChanged, (TextEditor&) *this);
		break;

	case TextEditorDefs::returnKeyMessageId:
		listeners.callChecked (checker, &TextEditorListener::textEditorReturnKeyPressed, (TextEditor&) *this);
		break;

	case TextEditorDefs::escapeKeyMessageId:
		listeners.callChecked (checker, &TextEditorListener::textEditorEscapeKeyPressed, (TextEditor&) *this);
		break;

	case TextEditorDefs::focusLossMessageId:
		listeners.callChecked (checker, &TextEditorListener::textEditorFocusLost, (TextEditor&) *this);
		break;

	default:
		jassertfalse
		break;
	}
}

void TextEditor::enablementChanged()
{
	setMouseCursor (isReadOnly() ? MouseCursor::NormalCursor
								 : MouseCursor::IBeamCursor);
	repaint();
}

UndoManager* TextEditor::getUndoManager() throw()
{
	return isReadOnly() ? &undoManager : 0;
}

void TextEditor::clearInternal (UndoManager* const um)
{
	remove (Range<int> (0, getTotalNumChars()), um, caretPosition);
}

void TextEditor::insert (const String& text,
						 const int insertIndex,
						 const Font& font,
						 const Colour& colour,
						 UndoManager* const um,
						 const int caretPositionToMoveTo)
{
	if (text.isNotEmpty())
	{
		if (um != 0)
		{
			if (um->getNumActionsInCurrentTransaction() > TextEditorDefs::maxActionsPerTransaction)
				newTransaction();

			um->perform (new InsertAction (*this, text, insertIndex, font, colour,
										   caretPosition, caretPositionToMoveTo));
		}
		else
		{
			repaintText (Range<int> (insertIndex, getTotalNumChars())); // must do this before and after changing the data, in case
																		// a line gets moved due to word wrap

			int index = 0;
			int nextIndex = 0;

			for (int i = 0; i < sections.size(); ++i)
			{
				nextIndex = index + sections.getUnchecked (i)->getTotalLength();

				if (insertIndex == index)
				{
					sections.insert (i, new UniformTextSection (text,
																font, colour,
																passwordCharacter));
					break;
				}
				else if (insertIndex > index && insertIndex < nextIndex)
				{
					splitSection (i, insertIndex - index);
					sections.insert (i + 1, new UniformTextSection (text,
																	font, colour,
																	passwordCharacter));
					break;
				}

				index = nextIndex;
			}

			if (nextIndex == insertIndex)
				sections.add (new UniformTextSection (text,
													  font, colour,
													  passwordCharacter));

			coalesceSimilarSections();
			totalNumChars = -1;
			valueTextNeedsUpdating = true;

			moveCursorTo (caretPositionToMoveTo, false);

			repaintText (Range<int> (insertIndex, getTotalNumChars()));
		}
	}
}

void TextEditor::reinsert (const int insertIndex,
						   const Array <UniformTextSection*>& sectionsToInsert)
{
	int index = 0;
	int nextIndex = 0;

	for (int i = 0; i < sections.size(); ++i)
	{
		nextIndex = index + sections.getUnchecked (i)->getTotalLength();

		if (insertIndex == index)
		{
			for (int j = sectionsToInsert.size(); --j >= 0;)
				sections.insert (i, new UniformTextSection (*sectionsToInsert.getUnchecked(j)));

			break;
		}
		else if (insertIndex > index && insertIndex < nextIndex)
		{
			splitSection (i, insertIndex - index);

			for (int j = sectionsToInsert.size(); --j >= 0;)
				sections.insert (i + 1, new UniformTextSection (*sectionsToInsert.getUnchecked(j)));

			break;
		}

		index = nextIndex;
	}

	if (nextIndex == insertIndex)
	{
		for (int j = 0; j < sectionsToInsert.size(); ++j)
			sections.add (new UniformTextSection (*sectionsToInsert.getUnchecked(j)));
	}

	coalesceSimilarSections();
	totalNumChars = -1;
	valueTextNeedsUpdating = true;
}

void TextEditor::remove (const Range<int>& range,
						 UndoManager* const um,
						 const int caretPositionToMoveTo)
{
	if (! range.isEmpty())
	{
		int index = 0;

		for (int i = 0; i < sections.size(); ++i)
		{
			const int nextIndex = index + sections.getUnchecked(i)->getTotalLength();

			if (range.getStart() > index && range.getStart() < nextIndex)
			{
				splitSection (i, range.getStart() - index);
				--i;
			}
			else if (range.getEnd() > index && range.getEnd() < nextIndex)
			{
				splitSection (i, range.getEnd() - index);
				--i;
			}
			else
			{
				index = nextIndex;

				if (index > range.getEnd())
					break;
			}
		}

		index = 0;

		if (um != 0)
		{
			Array <UniformTextSection*> removedSections;

			for (int i = 0; i < sections.size(); ++i)
			{
				if (range.getEnd() <= range.getStart())
					break;

				UniformTextSection* const section = sections.getUnchecked (i);

				const int nextIndex = index + section->getTotalLength();

				if (range.getStart() <= index && range.getEnd() >= nextIndex)
					removedSections.add (new UniformTextSection (*section));

				index = nextIndex;
			}

			if (um->getNumActionsInCurrentTransaction() > TextEditorDefs::maxActionsPerTransaction)
				newTransaction();

			um->perform (new RemoveAction (*this, range, caretPosition,
										   caretPositionToMoveTo, removedSections));
		}
		else
		{
			Range<int> remainingRange (range);

			for (int i = 0; i < sections.size(); ++i)
			{
				UniformTextSection* const section = sections.getUnchecked (i);

				const int nextIndex = index + section->getTotalLength();

				if (remainingRange.getStart() <= index && remainingRange.getEnd() >= nextIndex)
				{
					sections.remove(i);
					section->clear();
					delete section;

					remainingRange.setEnd (remainingRange.getEnd() - (nextIndex - index));
					if (remainingRange.isEmpty())
						break;

					--i;
				}
				else
				{
					index = nextIndex;
				}
			}

			coalesceSimilarSections();
			totalNumChars = -1;
			valueTextNeedsUpdating = true;

			moveCursorTo (caretPositionToMoveTo, false);

			repaintText (Range<int> (range.getStart(), getTotalNumChars()));
		}
	}
}

const String TextEditor::getText() const
{
	String t;
	t.preallocateStorage (getTotalNumChars());
	String::Concatenator concatenator (t);

	for (int i = 0; i < sections.size(); ++i)
		sections.getUnchecked (i)->appendAllText (concatenator);

	return t;
}

const String TextEditor::getTextInRange (const Range<int>& range) const
{
	String t;

	if (! range.isEmpty())
	{
		t.preallocateStorage (jmin (getTotalNumChars(), range.getLength()));
		String::Concatenator concatenator (t);
		int index = 0;

		for (int i = 0; i < sections.size(); ++i)
		{
			const UniformTextSection* const s = sections.getUnchecked (i);
			const int nextIndex = index + s->getTotalLength();

			if (range.getStart() < nextIndex)
			{
				if (range.getEnd() <= index)
					break;

				s->appendSubstring (concatenator, range - index);
			}

			index = nextIndex;
		}
	}

	return t;
}

const String TextEditor::getHighlightedText() const
{
	return getTextInRange (selection);
}

int TextEditor::getTotalNumChars() const
{
	if (totalNumChars < 0)
	{
		totalNumChars = 0;

		for (int i = sections.size(); --i >= 0;)
			totalNumChars += sections.getUnchecked (i)->getTotalLength();
	}

	return totalNumChars;
}

bool TextEditor::isEmpty() const
{
	return getTotalNumChars() == 0;
}

void TextEditor::getCharPosition (const int index, float& cx, float& cy, float& lineHeight) const
{
	const float wordWrapWidth = getWordWrapWidth();

	if (wordWrapWidth > 0 && sections.size() > 0)
	{
		Iterator i (sections, wordWrapWidth, passwordCharacter);

		i.getCharPosition (index, cx, cy, lineHeight);
	}
	else
	{
		cx = cy = 0;
		lineHeight = currentFont.getHeight();
	}
}

int TextEditor::indexAtPosition (const float x, const float y)
{
	const float wordWrapWidth = getWordWrapWidth();

	if (wordWrapWidth > 0)
	{
		Iterator i (sections, wordWrapWidth, passwordCharacter);

		while (i.next())
		{
			if (i.lineY + i.lineHeight > y)
			{
				if (i.lineY > y)
					return jmax (0, i.indexInText - 1);

				if (i.atomX >= x)
					return i.indexInText;

				if (x < i.atomRight)
					return i.xToIndex (x);
			}
		}
	}

	return getTotalNumChars();
}

static int getCharacterCategory (const juce_wchar character)
{
	return CharacterFunctions::isLetterOrDigit (character)
				? 2 : (CharacterFunctions::isWhitespace (character) ? 0 : 1);
}

int TextEditor::findWordBreakAfter (const int position) const
{
	const String t (getTextInRange (Range<int> (position, position + 512)));
	const int totalLength = t.length();
	int i = 0;

	while (i < totalLength && CharacterFunctions::isWhitespace (t[i]))
		++i;

	const int type = getCharacterCategory (t[i]);

	while (i < totalLength && type == getCharacterCategory (t[i]))
		++i;

	while (i < totalLength && CharacterFunctions::isWhitespace (t[i]))
		++i;

	return position + i;
}

int TextEditor::findWordBreakBefore (const int position) const
{
	if (position <= 0)
		return 0;

	const int startOfBuffer = jmax (0, position - 512);
	const String t (getTextInRange (Range<int> (startOfBuffer, position)));

	int i = position - startOfBuffer;

	while (i > 0 && CharacterFunctions::isWhitespace (t [i - 1]))
		--i;

	if (i > 0)
	{
		const int type = getCharacterCategory (t [i - 1]);

		while (i > 0 && type == getCharacterCategory (t [i - 1]))
			--i;
	}

	jassert (startOfBuffer + i >= 0);
	return startOfBuffer + i;
}

void TextEditor::splitSection (const int sectionIndex,
							   const int charToSplitAt)
{
	jassert (sections[sectionIndex] != 0);

	sections.insert (sectionIndex + 1,
					 sections.getUnchecked (sectionIndex)->split (charToSplitAt, passwordCharacter));
}

void TextEditor::coalesceSimilarSections()
{
	for (int i = 0; i < sections.size() - 1; ++i)
	{
		UniformTextSection* const s1 = sections.getUnchecked (i);
		UniformTextSection* const s2 = sections.getUnchecked (i + 1);

		if (s1->font == s2->font
			 && s1->colour == s2->colour)
		{
			s1->append (*s2, passwordCharacter);
			sections.remove (i + 1);
			delete s2;
			--i;
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TextEditor.cpp ***/


/*** Start of inlined file: juce_Toolbar.cpp ***/
BEGIN_JUCE_NAMESPACE

const char* const Toolbar::toolbarDragDescriptor = "_toolbarItem_";

class ToolbarSpacerComp  : public ToolbarItemComponent
{
public:
	ToolbarSpacerComp (const int itemId_, const float fixedSize_, const bool drawBar_)
		: ToolbarItemComponent (itemId_, String::empty, false),
		  fixedSize (fixedSize_),
		  drawBar (drawBar_)
	{
	}

	~ToolbarSpacerComp()
	{
	}

	bool getToolbarItemSizes (int toolbarThickness, bool /*isToolbarVertical*/,
							  int& preferredSize, int& minSize, int& maxSize)
	{
		if (fixedSize <= 0)
		{
			preferredSize = toolbarThickness * 2;
			minSize = 4;
			maxSize = 32768;
		}
		else
		{
			maxSize = roundToInt (toolbarThickness * fixedSize);
			minSize = drawBar ? maxSize : jmin (4, maxSize);
			preferredSize = maxSize;

			if (getEditingMode() == editableOnPalette)
				preferredSize = maxSize = toolbarThickness / (drawBar ? 3 : 2);
		}

		return true;
	}

	void paintButtonArea (Graphics&, int, int, bool, bool)
	{
	}

	void contentAreaChanged (const Rectangle<int>&)
	{
	}

	int getResizeOrder() const throw()
	{
		return fixedSize <= 0 ? 0 : 1;
	}

	void paint (Graphics& g)
	{
		const int w = getWidth();
		const int h = getHeight();

		if (drawBar)
		{
			g.setColour (findColour (Toolbar::separatorColourId, true));

			const float thickness = 0.2f;

			if (isToolbarVertical())
				g.fillRect (w * 0.1f, h * (0.5f - thickness * 0.5f), w * 0.8f, h * thickness);
			else
				g.fillRect (w * (0.5f - thickness * 0.5f), h * 0.1f, w * thickness, h * 0.8f);
		}

		if (getEditingMode() != normalMode && ! drawBar)
		{
			g.setColour (findColour (Toolbar::separatorColourId, true));

			const int indentX = jmin (2, (w - 3) / 2);
			const int indentY = jmin (2, (h - 3) / 2);
			g.drawRect (indentX, indentY, w - indentX * 2, h - indentY * 2, 1);

			if (fixedSize <= 0)
			{
				float x1, y1, x2, y2, x3, y3, x4, y4, hw, hl;

				if (isToolbarVertical())
				{
					x1 = w * 0.5f;
					y1 = h * 0.4f;
					x2 = x1;
					y2 = indentX * 2.0f;

					x3 = x1;
					y3 = h * 0.6f;
					x4 = x1;
					y4 = h - y2;

					hw = w * 0.15f;
					hl = w * 0.2f;
				}
				else
				{
					x1 = w * 0.4f;
					y1 = h * 0.5f;
					x2 = indentX * 2.0f;
					y2 = y1;

					x3 = w * 0.6f;
					y3 = y1;
					x4 = w - x2;
					y4 = y1;

					hw = h * 0.15f;
					hl = h * 0.2f;
				}

				Path p;
				p.addArrow (x1, y1, x2, y2, 1.5f, hw, hl);
				p.addArrow (x3, y3, x4, y4, 1.5f, hw, hl);
				g.fillPath (p);
			}
		}
	}

	juce_UseDebuggingNewOperator

private:
	const float fixedSize;
	const bool drawBar;

	ToolbarSpacerComp (const ToolbarSpacerComp&);
	ToolbarSpacerComp& operator= (const ToolbarSpacerComp&);
};

class MissingItemsComponent  : public PopupMenuCustomComponent
{
public:
	MissingItemsComponent (Toolbar& owner_, const int height_)
		: PopupMenuCustomComponent (true),
		  owner (owner_),
		  height (height_)
	{
		for (int i = owner_.items.size(); --i >= 0;)
		{
			ToolbarItemComponent* const tc = owner_.items.getUnchecked(i);

			if (dynamic_cast <ToolbarSpacerComp*> (tc) == 0 && ! tc->isVisible())
			{
				oldIndexes.insert (0, i);
				addAndMakeVisible (tc, 0);
			}
		}

		layout (400);
	}

	~MissingItemsComponent()
	{
		// deleting the toolbar while its menu it open??
		jassert (owner.isValidComponent());

		for (int i = 0; i < getNumChildComponents(); ++i)
		{
			ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (getChildComponent (i));

			if (tc != 0)
			{
				tc->setVisible (false);
				const int index = oldIndexes.remove (i);
				owner.addChildComponent (tc, index);
				--i;
			}
		}

		owner.resized();
	}

	void layout (const int preferredWidth)
	{
		const int indent = 8;
		int x = indent;
		int y = indent;
		int maxX = 0;

		for (int i = 0; i < getNumChildComponents(); ++i)
		{
			ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (getChildComponent (i));

			if (tc != 0)
			{
				int preferredSize = 1, minSize = 1, maxSize = 1;

				if (tc->getToolbarItemSizes (height, false, preferredSize, minSize, maxSize))
				{
					if (x + preferredSize > preferredWidth && x > indent)
					{
						x = indent;
						y += height;
					}

					tc->setBounds (x, y, preferredSize, height);

					x += preferredSize;
					maxX = jmax (maxX, x);
				}
			}
		}

		setSize (maxX + 8, y + height + 8);
	}

	void getIdealSize (int& idealWidth, int& idealHeight)
	{
		idealWidth = getWidth();
		idealHeight = getHeight();
	}

	juce_UseDebuggingNewOperator

private:
	Toolbar& owner;
	const int height;
	Array <int> oldIndexes;

	MissingItemsComponent (const MissingItemsComponent&);
	MissingItemsComponent& operator= (const MissingItemsComponent&);
};

Toolbar::Toolbar()
	: vertical (false),
	  isEditingActive (false),
	  toolbarStyle (Toolbar::iconsOnly)
{
	addChildComponent (missingItemsButton = getLookAndFeel().createToolbarMissingItemsButton (*this));

	missingItemsButton->setAlwaysOnTop (true);
	missingItemsButton->addButtonListener (this);
}

Toolbar::~Toolbar()
{
	animator.cancelAllAnimations (true);
	deleteAllChildren();
}

void Toolbar::setVertical (const bool shouldBeVertical)
{
	if (vertical != shouldBeVertical)
	{
		vertical = shouldBeVertical;
		resized();
	}
}

void Toolbar::clear()
{
	for (int i = items.size(); --i >= 0;)
	{
		ToolbarItemComponent* const tc = items.getUnchecked(i);
		items.remove (i);
		delete tc;
	}

	resized();
}

ToolbarItemComponent* Toolbar::createItem (ToolbarItemFactory& factory, const int itemId)
{
	if (itemId == ToolbarItemFactory::separatorBarId)
		return new ToolbarSpacerComp (itemId, 0.1f, true);
	else if (itemId == ToolbarItemFactory::spacerId)
		return new ToolbarSpacerComp (itemId, 0.5f, false);
	else if (itemId == ToolbarItemFactory::flexibleSpacerId)
		return new ToolbarSpacerComp (itemId, 0, false);

	return factory.createItem (itemId);
}

void Toolbar::addItemInternal (ToolbarItemFactory& factory,
							   const int itemId,
							   const int insertIndex)
{
	// An ID can't be zero - this might indicate a mistake somewhere?
	jassert (itemId != 0);

	ToolbarItemComponent* const tc = createItem (factory, itemId);

	if (tc != 0)
	{
#ifdef JUCE_DEBUG
		Array <int> allowedIds;
		factory.getAllToolbarItemIds (allowedIds);

		// If your factory can create an item for a given ID, it must also return
		// that ID from its getAllToolbarItemIds() method!
		jassert (allowedIds.contains (itemId));
#endif

		items.insert (insertIndex, tc);
		addAndMakeVisible (tc, insertIndex);
	}
}

void Toolbar::addItem (ToolbarItemFactory& factory,
					   const int itemId,
					   const int insertIndex)
{
	addItemInternal (factory, itemId, insertIndex);
	resized();
}

void Toolbar::addDefaultItems (ToolbarItemFactory& factoryToUse)
{
	Array <int> ids;
	factoryToUse.getDefaultItemSet (ids);

	clear();

	for (int i = 0; i < ids.size(); ++i)
		addItemInternal (factoryToUse, ids.getUnchecked (i), -1);

	resized();
}

void Toolbar::removeToolbarItem (const int itemIndex)
{
	ToolbarItemComponent* const tc = getItemComponent (itemIndex);

	if (tc != 0)
	{
		items.removeValue (tc);
		delete tc;
		resized();
	}
}

int Toolbar::getNumItems() const throw()
{
	return items.size();
}

int Toolbar::getItemId (const int itemIndex) const throw()
{
	ToolbarItemComponent* const tc = getItemComponent (itemIndex);
	return tc != 0 ? tc->getItemId() : 0;
}

ToolbarItemComponent* Toolbar::getItemComponent (const int itemIndex) const throw()
{
	return items [itemIndex];
}

ToolbarItemComponent* Toolbar::getNextActiveComponent (int index, const int delta) const
{
	for (;;)
	{
		index += delta;
		ToolbarItemComponent* const tc = getItemComponent (index);

		if (tc == 0)
			break;

		if (tc->isActive)
			return tc;
	}

	return 0;
}

void Toolbar::setStyle (const ToolbarItemStyle& newStyle)
{
	if (toolbarStyle != newStyle)
	{
		toolbarStyle = newStyle;
		updateAllItemPositions (false);
	}
}

const String Toolbar::toString() const
{
	String s ("TB:");

	for (int i = 0; i < getNumItems(); ++i)
		s << getItemId(i) << ' ';

	return s.trimEnd();
}

bool Toolbar::restoreFromString (ToolbarItemFactory& factoryToUse,
								 const String& savedVersion)
{
	if (! savedVersion.startsWith ("TB:"))
		return false;

	StringArray tokens;
	tokens.addTokens (savedVersion.substring (3), false);

	clear();

	for (int i = 0; i < tokens.size(); ++i)
		addItemInternal (factoryToUse, tokens[i].getIntValue(), -1);

	resized();
	return true;
}

void Toolbar::paint (Graphics& g)
{
	getLookAndFeel().paintToolbarBackground (g, getWidth(), getHeight(), *this);
}

int Toolbar::getThickness() const throw()
{
	return vertical ? getWidth() : getHeight();
}

int Toolbar::getLength() const throw()
{
	return vertical ? getHeight() : getWidth();
}

void Toolbar::setEditingActive (const bool active)
{
	if (isEditingActive != active)
	{
		isEditingActive = active;
		updateAllItemPositions (false);
	}
}

void Toolbar::resized()
{
	updateAllItemPositions (false);
}

void Toolbar::updateAllItemPositions (const bool animate)
{
	if (getWidth() > 0 && getHeight() > 0)
	{
		StretchableObjectResizer resizer;

		int i;
		for (i = 0; i < items.size(); ++i)
		{
			ToolbarItemComponent* const tc = items.getUnchecked(i);

			tc->setEditingMode (isEditingActive ? ToolbarItemComponent::editableOnToolbar
												: ToolbarItemComponent::normalMode);

			tc->setStyle (toolbarStyle);

			ToolbarSpacerComp* const spacer = dynamic_cast <ToolbarSpacerComp*> (tc);

			int preferredSize = 1, minSize = 1, maxSize = 1;

			if (tc->getToolbarItemSizes (getThickness(), isVertical(),
										 preferredSize, minSize, maxSize))
			{
				tc->isActive = true;
				resizer.addItem (preferredSize, minSize, maxSize,
								 spacer != 0 ? spacer->getResizeOrder() : 2);
			}
			else
			{
				tc->isActive = false;
				tc->setVisible (false);
			}
		}

		resizer.resizeToFit (getLength());

		int totalLength = 0;

		for (i = 0; i < resizer.getNumItems(); ++i)
			totalLength += (int) resizer.getItemSize (i);

		const bool itemsOffTheEnd = totalLength > getLength();

		const int extrasButtonSize = getThickness() / 2;
		missingItemsButton->setSize (extrasButtonSize, extrasButtonSize);
		missingItemsButton->setVisible (itemsOffTheEnd);
		missingItemsButton->setEnabled (! isEditingActive);

		if (vertical)
			missingItemsButton->setCentrePosition (getWidth() / 2,
												   getHeight() - 4 - extrasButtonSize / 2);
		else
			missingItemsButton->setCentrePosition (getWidth() - 4 - extrasButtonSize / 2,
												   getHeight() / 2);

		const int maxLength = itemsOffTheEnd ? (vertical ? missingItemsButton->getY()
														 : missingItemsButton->getX()) - 4
											 : getLength();

		int pos = 0, activeIndex = 0;
		for (i = 0; i < items.size(); ++i)
		{
			ToolbarItemComponent* const tc = items.getUnchecked(i);

			if (tc->isActive)
			{
				const int size = (int) resizer.getItemSize (activeIndex++);

				Rectangle<int> newBounds;
				if (vertical)
					newBounds.setBounds (0, pos, getWidth(), size);
				else
					newBounds.setBounds (pos, 0, size, getHeight());

				if (animate)
				{
					animator.animateComponent (tc, newBounds, 200, 3.0, 0.0);
				}
				else
				{
					animator.cancelAnimation (tc, false);
					tc->setBounds (newBounds);
				}

				pos += size;
				tc->setVisible (pos <= maxLength
								 && ((! tc->isBeingDragged)
									  || tc->getEditingMode() == ToolbarItemComponent::editableOnPalette));
			}
		}
	}
}

void Toolbar::buttonClicked (Button*)
{
	jassert (missingItemsButton->isShowing());

	if (missingItemsButton->isShowing())
	{
		PopupMenu m;
		m.addCustomItem (1, new MissingItemsComponent (*this, getThickness()));
		m.showAt (missingItemsButton);
	}
}

bool Toolbar::isInterestedInDragSource (const String& sourceDescription,
										Component* /*sourceComponent*/)
{
	return sourceDescription == toolbarDragDescriptor && isEditingActive;
}

void Toolbar::itemDragMove (const String&, Component* sourceComponent, int x, int y)
{
	ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (sourceComponent);

	if (tc != 0)
	{
		if (getNumItems() == 0)
		{
			if (tc->getEditingMode() == ToolbarItemComponent::editableOnPalette)
			{
				ToolbarItemPalette* const palette = tc->findParentComponentOfClass ((ToolbarItemPalette*) 0);

				if (palette != 0)
					palette->replaceComponent (tc);
			}
			else
			{
				jassert (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar);
			}

			items.add (tc);
			addChildComponent (tc);
			updateAllItemPositions (false);
		}
		else
		{
			for (int i = getNumItems(); --i >= 0;)
			{
				int currentIndex = getIndexOfChildComponent (tc);

				if (currentIndex < 0)
				{
					if (tc->getEditingMode() == ToolbarItemComponent::editableOnPalette)
					{
						ToolbarItemPalette* const palette = tc->findParentComponentOfClass ((ToolbarItemPalette*) 0);

						if (palette != 0)
							palette->replaceComponent (tc);
					}
					else
					{
						jassert (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar);
					}

					items.add (tc);
					addChildComponent (tc);
					currentIndex = getIndexOfChildComponent (tc);
					updateAllItemPositions (true);
				}

				int newIndex = currentIndex;

				const int dragObjectLeft = vertical ? (y - tc->dragOffsetY) : (x - tc->dragOffsetX);
				const int dragObjectRight = dragObjectLeft + (vertical ? tc->getHeight() : tc->getWidth());

				const Rectangle<int> current (animator.getComponentDestination (getChildComponent (newIndex)));
				ToolbarItemComponent* const prev = getNextActiveComponent (newIndex, -1);

				if (prev != 0)
				{
					const Rectangle<int> previousPos (animator.getComponentDestination (prev));

					if (abs (dragObjectLeft - (vertical ? previousPos.getY() : previousPos.getX())
						  < abs (dragObjectRight - (vertical ? current.getBottom() : current.getRight()))))
					{
						newIndex = getIndexOfChildComponent (prev);
					}
				}

				ToolbarItemComponent* const next = getNextActiveComponent (newIndex, 1);
				if (next != 0)
				{
					const Rectangle<int> nextPos (animator.getComponentDestination (next));

					if (abs (dragObjectLeft - (vertical ? current.getY() : current.getX())
						 > abs (dragObjectRight - (vertical ? nextPos.getBottom() : nextPos.getRight()))))
					{
						newIndex = getIndexOfChildComponent (next) + 1;
					}
				}

				if (newIndex != currentIndex)
				{
					items.removeValue (tc);
					removeChildComponent (tc);
					addChildComponent (tc, newIndex);
					items.insert (newIndex, tc);
					updateAllItemPositions (true);
				}
				else
				{
					break;
				}
			}
		}
	}
}

void Toolbar::itemDragExit (const String&, Component* sourceComponent)
{
	ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (sourceComponent);

	if (tc != 0)
	{
		if (isParentOf (tc))
		{
			items.removeValue (tc);
			removeChildComponent (tc);
			updateAllItemPositions (true);
		}
	}
}

void Toolbar::itemDropped (const String&, Component*, int, int)
{
}

void Toolbar::mouseDown (const MouseEvent& e)
{
	if (e.mods.isPopupMenu())
	{
	}
}

class ToolbarCustomisationDialog   : public DialogWindow
{
public:
	ToolbarCustomisationDialog (ToolbarItemFactory& factory,
								Toolbar* const toolbar_,
								const int optionFlags)
		: DialogWindow (TRANS("Add/remove items from toolbar"), Colours::white, true, true),
		  toolbar (toolbar_)
	{
		setContentComponent (new CustomiserPanel (factory, toolbar, optionFlags), true, true);
		setResizable (true, true);
		setResizeLimits (400, 300, 1500, 1000);
		positionNearBar();
	}

	~ToolbarCustomisationDialog()
	{
		setContentComponent (0, true);
	}

	void closeButtonPressed()
	{
		setVisible (false);
	}

	bool canModalEventBeSentToComponent (const Component* comp)
	{
		return toolbar->isParentOf (comp);
	}

	void positionNearBar()
	{
		const Rectangle<int> screenSize (toolbar->getParentMonitorArea());
		const int tbx = toolbar->getScreenX();
		const int tby = toolbar->getScreenY();
		const int gap = 8;

		int x, y;

		if (toolbar->isVertical())
		{
			y = tby;

			if (tbx > screenSize.getCentreX())
				x = tbx - getWidth() - gap;
			else
				x = tbx + toolbar->getWidth() + gap;
		}
		else
		{
			x = tbx + (toolbar->getWidth() - getWidth()) / 2;

			if (tby > screenSize.getCentreY())
				y = tby - getHeight() - gap;
			else
				y = tby + toolbar->getHeight() + gap;
		}

		setTopLeftPosition (x, y);
	}

private:
	Toolbar* const toolbar;

	class CustomiserPanel  : public Component,
							 private ComboBoxListener,
							 private ButtonListener
	{
	public:
		CustomiserPanel (ToolbarItemFactory& factory_,
						 Toolbar* const toolbar_,
						 const int optionFlags)
		  : factory (factory_),
			toolbar (toolbar_),
			styleBox (0),
			defaultButton (0)
		{
			addAndMakeVisible (palette = new ToolbarItemPalette (factory, toolbar));

			if ((optionFlags & (Toolbar::allowIconsOnlyChoice
								 | Toolbar::allowIconsWithTextChoice
								 | Toolbar::allowTextOnlyChoice)) != 0)
			{
				addAndMakeVisible (styleBox = new ComboBox (String::empty));
				styleBox->setEditableText (false);

				if ((optionFlags & Toolbar::allowIconsOnlyChoice) != 0)
					styleBox->addItem (TRANS("Show icons only"), 1);
				if ((optionFlags & Toolbar::allowIconsWithTextChoice) != 0)
					styleBox->addItem (TRANS("Show icons and descriptions"), 2);
				if ((optionFlags & Toolbar::allowTextOnlyChoice) != 0)
					styleBox->addItem (TRANS("Show descriptions only"), 3);

				if (toolbar_->getStyle() == Toolbar::iconsOnly)
					styleBox->setSelectedId (1);
				else if (toolbar_->getStyle() == Toolbar::iconsWithText)
					styleBox->setSelectedId (2);
				else if (toolbar_->getStyle() == Toolbar::textOnly)
					styleBox->setSelectedId (3);

				styleBox->addListener (this);
			}

			if ((optionFlags & Toolbar::showResetToDefaultsButton) != 0)
			{
				addAndMakeVisible (defaultButton = new TextButton (TRANS ("Restore to default set of items")));
				defaultButton->addButtonListener (this);
			}

			addAndMakeVisible (instructions = new Label (String::empty,
				TRANS ("You can drag the items above and drop them onto a toolbar to add them.\n\nItems on the toolbar can also be dragged around to change their order, or dragged off the edge to delete them.")));
			instructions->setFont (Font (13.0f));

			setSize (500, 300);
		}

		~CustomiserPanel()
		{
			deleteAllChildren();
		}

		void comboBoxChanged (ComboBox*)
		{
			if (styleBox->getSelectedId() == 1)
				toolbar->setStyle (Toolbar::iconsOnly);
			else if (styleBox->getSelectedId() == 2)
				toolbar->setStyle (Toolbar::iconsWithText);
			else if (styleBox->getSelectedId() == 3)
				toolbar->setStyle (Toolbar::textOnly);

			palette->resized(); // to make it update the styles
		}

		void buttonClicked (Button*)
		{
			toolbar->addDefaultItems (factory);
		}

		void paint (Graphics& g)
		{
			Colour background;

			DialogWindow* const dw = findParentComponentOfClass ((DialogWindow*) 0);

			if (dw != 0)
				background = dw->getBackgroundColour();

			g.setColour (background.contrasting().withAlpha (0.3f));
			g.fillRect (palette->getX(), palette->getBottom() - 1, palette->getWidth(), 1);
		}

		void resized()
		{
			palette->setBounds (0, 0, getWidth(), getHeight() - 120);

			if (styleBox != 0)
				styleBox->setBounds (10, getHeight() - 110, 200, 22);

			if (defaultButton != 0)
			{
				defaultButton->changeWidthToFitText (22);
				defaultButton->setTopLeftPosition (240, getHeight() - 110);
			}

			instructions->setBounds (10, getHeight() - 80, getWidth() - 20, 80);
		}

	private:
		ToolbarItemFactory& factory;
		Toolbar* const toolbar;

		Label* instructions;
		ToolbarItemPalette* palette;
		ComboBox* styleBox;
		TextButton* defaultButton;
	};
};

void Toolbar::showCustomisationDialog (ToolbarItemFactory& factory, const int optionFlags)
{
	setEditingActive (true);

	ToolbarCustomisationDialog dw (factory, this, optionFlags);
	dw.runModalLoop();

	jassert (isValidComponent()); // ? deleting the toolbar while it's being edited?
	setEditingActive (false);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Toolbar.cpp ***/


/*** Start of inlined file: juce_ToolbarItemComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

ToolbarItemFactory::ToolbarItemFactory()
{
}

ToolbarItemFactory::~ToolbarItemFactory()
{
}

class ItemDragAndDropOverlayComponent	: public Component
{
public:
	ItemDragAndDropOverlayComponent()
		: isDragging (false)
	{
		setAlwaysOnTop (true);
		setRepaintsOnMouseActivity (true);
		setMouseCursor (MouseCursor::DraggingHandCursor);
	}

	~ItemDragAndDropOverlayComponent()
	{
	}

	void paint (Graphics& g)
	{
		ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (getParentComponent());

		if (isMouseOverOrDragging()
			  && tc != 0
			  && tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
		{
			g.setColour (findColour (Toolbar::editingModeOutlineColourId, true));
			g.drawRect (0, 0, getWidth(), getHeight(),
						jmin (2, (getWidth() - 1) / 2, (getHeight() - 1) / 2));
		}
	}

	void mouseDown (const MouseEvent& e)
	{
		isDragging = false;
		ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (getParentComponent());

		if (tc != 0)
		{
			tc->dragOffsetX = e.x;
			tc->dragOffsetY = e.y;
		}
	}

	void mouseDrag (const MouseEvent& e)
	{
		if (! (isDragging || e.mouseWasClicked()))
		{
			isDragging = true;
			DragAndDropContainer* const dnd = DragAndDropContainer::findParentDragContainerFor (this);

			if (dnd != 0)
			{
				dnd->startDragging (Toolbar::toolbarDragDescriptor, getParentComponent(), 0, true);

				ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (getParentComponent());

				if (tc != 0)
				{
					tc->isBeingDragged = true;

					if (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
						tc->setVisible (false);
				}
			}
		}
	}

	void mouseUp (const MouseEvent&)
	{
		isDragging = false;
		ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (getParentComponent());

		if (tc != 0)
		{
			tc->isBeingDragged = false;

			Toolbar* const tb = tc->getToolbar();

			if (tb != 0)
				tb->updateAllItemPositions (true);
			else if (tc->getEditingMode() == ToolbarItemComponent::editableOnToolbar)
				delete tc;
		}
	}

	void parentSizeChanged()
	{
		setBounds (0, 0, getParentWidth(), getParentHeight());
	}

	juce_UseDebuggingNewOperator

private:
	bool isDragging;

	ItemDragAndDropOverlayComponent (const ItemDragAndDropOverlayComponent&);
	ItemDragAndDropOverlayComponent& operator= (const ItemDragAndDropOverlayComponent&);
};

ToolbarItemComponent::ToolbarItemComponent (const int itemId_,
											const String& labelText,
											const bool isBeingUsedAsAButton_)
	: Button (labelText),
	  itemId (itemId_),
	  mode (normalMode),
	  toolbarStyle (Toolbar::iconsOnly),
	  dragOffsetX (0),
	  dragOffsetY (0),
	  isActive (true),
	  isBeingDragged (false),
	  isBeingUsedAsAButton (isBeingUsedAsAButton_)
{
	// Your item ID can't be 0!
	jassert (itemId_ != 0);
}

ToolbarItemComponent::~ToolbarItemComponent()
{
	jassert (overlayComp == 0 || overlayComp->isValidComponent());
	overlayComp = 0;
}

Toolbar* ToolbarItemComponent::getToolbar() const
{
	return dynamic_cast <Toolbar*> (getParentComponent());
}

bool ToolbarItemComponent::isToolbarVertical() const
{
	const Toolbar* const t = getToolbar();
	return t != 0 && t->isVertical();
}

void ToolbarItemComponent::setStyle (const Toolbar::ToolbarItemStyle& newStyle)
{
	if (toolbarStyle != newStyle)
	{
		toolbarStyle = newStyle;
		repaint();
		resized();
	}
}

void ToolbarItemComponent::paintButton (Graphics& g, bool isMouseOver, bool isMouseDown)
{
	if (isBeingUsedAsAButton)
		getLookAndFeel().paintToolbarButtonBackground (g, getWidth(), getHeight(),
													   isMouseOver, isMouseDown, *this);

	if (toolbarStyle != Toolbar::iconsOnly)
	{
		const int indent = contentArea.getX();
		int y = indent;
		int h = getHeight() - indent * 2;

		if (toolbarStyle == Toolbar::iconsWithText)
		{
			y = contentArea.getBottom() + indent / 2;
			h -= contentArea.getHeight();
		}

		getLookAndFeel().paintToolbarButtonLabel (g, indent, y, getWidth() - indent * 2, h,
												  getButtonText(), *this);
	}

	if (! contentArea.isEmpty())
	{
		g.saveState();
		g.setOrigin (contentArea.getX(), contentArea.getY());
		g.reduceClipRegion (0, 0, contentArea.getWidth(), contentArea.getHeight());

		paintButtonArea (g, contentArea.getWidth(), contentArea.getHeight(), isMouseOver, isMouseDown);

		g.restoreState();
	}
}

void ToolbarItemComponent::resized()
{
	if (toolbarStyle != Toolbar::textOnly)
	{
		const int indent = jmin (proportionOfWidth (0.08f),
								 proportionOfHeight (0.08f));

		contentArea = Rectangle<int> (indent, indent,
									  getWidth() - indent * 2,
									  toolbarStyle == Toolbar::iconsWithText ? proportionOfHeight (0.55f)
																			 : (getHeight() - indent * 2));
	}
	else
	{
		contentArea = Rectangle<int>();
	}

	contentAreaChanged (contentArea);
}

void ToolbarItemComponent::setEditingMode (const ToolbarEditingMode newMode)
{
	if (mode != newMode)
	{
		mode = newMode;
		repaint();

		if (mode == normalMode)
		{
			jassert (overlayComp == 0 || overlayComp->isValidComponent());
			overlayComp = 0;
		}
		else if (overlayComp == 0)
		{
			addAndMakeVisible (overlayComp = new ItemDragAndDropOverlayComponent());
			overlayComp->parentSizeChanged();
		}

		resized();
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ToolbarItemComponent.cpp ***/


/*** Start of inlined file: juce_ToolbarItemPalette.cpp ***/
BEGIN_JUCE_NAMESPACE

ToolbarItemPalette::ToolbarItemPalette (ToolbarItemFactory& factory_,
										Toolbar* const toolbar_)
	: factory (factory_),
	  toolbar (toolbar_)
{
	Component* const itemHolder = new Component();

	Array <int> allIds;
	factory_.getAllToolbarItemIds (allIds);

	for (int i = 0; i < allIds.size(); ++i)
	{
		ToolbarItemComponent* const tc = Toolbar::createItem (factory_, allIds.getUnchecked (i));

		jassert (tc != 0);
		if (tc != 0)
		{
			itemHolder->addAndMakeVisible (tc);
			tc->setEditingMode (ToolbarItemComponent::editableOnPalette);
		}
	}

	viewport = new Viewport();
	viewport->setViewedComponent (itemHolder);
	addAndMakeVisible (viewport);
}

ToolbarItemPalette::~ToolbarItemPalette()
{
	viewport->getViewedComponent()->deleteAllChildren();
	deleteAllChildren();
}

void ToolbarItemPalette::resized()
{
	viewport->setBoundsInset (BorderSize (1));

	Component* const itemHolder = viewport->getViewedComponent();

	const int indent = 8;
	const int preferredWidth = viewport->getWidth() - viewport->getScrollBarThickness() - indent;
	const int height = toolbar->getThickness();
	int x = indent;
	int y = indent;
	int maxX = 0;

	for (int i = 0; i < itemHolder->getNumChildComponents(); ++i)
	{
		ToolbarItemComponent* const tc = dynamic_cast <ToolbarItemComponent*> (itemHolder->getChildComponent (i));

		if (tc != 0)
		{
			tc->setStyle (toolbar->getStyle());

			int preferredSize = 1, minSize = 1, maxSize = 1;

			if (tc->getToolbarItemSizes (height, false, preferredSize, minSize, maxSize))
			{
				if (x + preferredSize > preferredWidth && x > indent)
				{
					x = indent;
					y += height;
				}

				tc->setBounds (x, y, preferredSize, height);

				x += preferredSize + 8;
				maxX = jmax (maxX, x);
			}
		}
	}

	itemHolder->setSize (maxX, y + height + 8);
}

void ToolbarItemPalette::replaceComponent (ToolbarItemComponent* const comp)
{
	ToolbarItemComponent* const tc = Toolbar::createItem (factory, comp->getItemId());

	jassert (tc != 0);

	if (tc != 0)
	{
		tc->setBounds (comp->getBounds());
		tc->setStyle (toolbar->getStyle());
		tc->setEditingMode (comp->getEditingMode());
		viewport->getViewedComponent()->addAndMakeVisible (tc, getIndexOfChildComponent (comp));
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ToolbarItemPalette.cpp ***/


/*** Start of inlined file: juce_TreeView.cpp ***/
BEGIN_JUCE_NAMESPACE

class TreeViewContentComponent  : public Component,
								  public TooltipClient
{
public:
	TreeViewContentComponent (TreeView* const owner_)
		: owner (owner_),
		  buttonUnderMouse (0),
		  isDragging (false)
	{
	}

	~TreeViewContentComponent()
	{
		deleteAllChildren();
	}

	void mouseDown (const MouseEvent& e)
	{
		updateButtonUnderMouse (e);

		isDragging = false;
		needSelectionOnMouseUp = false;

		Rectangle<int> pos;
		TreeViewItem* const item = findItemAt (e.y, pos);

		if (item == 0)
			return;

		// (if the open/close buttons are hidden, we'll treat clicks to the left of the item
		// as selection clicks)
		if (e.x < pos.getX() && owner->openCloseButtonsVisible)
		{
			if (e.x >= pos.getX() - owner->getIndentSize())
				item->setOpen (! item->isOpen());

			// (clicks to the left of an open/close button are ignored)
		}
		else
		{
			// mouse-down inside the body of the item..
			if (! owner->isMultiSelectEnabled())
				item->setSelected (true, true);
			else if (item->isSelected())
				needSelectionOnMouseUp = ! e.mods.isPopupMenu();
			else
				selectBasedOnModifiers (item, e.mods);

			if (e.x >= pos.getX())
				item->itemClicked (e.withNewPosition (e.getPosition() - pos.getPosition()));
		}
	}

	void mouseUp (const MouseEvent& e)
	{
		updateButtonUnderMouse (e);

		if (needSelectionOnMouseUp && e.mouseWasClicked())
		{
			Rectangle<int> pos;
			TreeViewItem* const item = findItemAt (e.y, pos);

			if (item != 0)
				selectBasedOnModifiers (item, e.mods);
		}
	}

	void mouseDoubleClick (const MouseEvent& e)
	{
		if (e.getNumberOfClicks() != 3)  // ignore triple clicks
		{
			Rectangle<int> pos;
			TreeViewItem* const item = findItemAt (e.y, pos);

			if (item != 0 && (e.x >= pos.getX() || ! owner->openCloseButtonsVisible))
				item->itemDoubleClicked (e.withNewPosition (e.getPosition() - pos.getPosition()));
		}
	}

	void mouseDrag (const MouseEvent& e)
	{
		if (isEnabled()
			 && ! (isDragging || e.mouseWasClicked()
					|| e.getDistanceFromDragStart() < 5
					|| e.mods.isPopupMenu()))
		{
			isDragging = true;

			Rectangle<int> pos;
			TreeViewItem* const item = findItemAt (e.getMouseDownY(), pos);

			if (item != 0 && e.getMouseDownX() >= pos.getX())
			{
				const String dragDescription (item->getDragSourceDescription());

				if (dragDescription.isNotEmpty())
				{
					DragAndDropContainer* const dragContainer
						= DragAndDropContainer::findParentDragContainerFor (this);

					if (dragContainer != 0)
					{
						pos.setSize (pos.getWidth(), item->itemHeight);
						Image* dragImage = Component::createComponentSnapshot (pos, true);
						dragImage->multiplyAllAlphas (0.6f);

						Point<int> imageOffset (pos.getX() - e.x, pos.getY() - e.y);
						dragContainer->startDragging (dragDescription, owner, dragImage, true, &imageOffset);
					}
					else
					{
						// to be able to do a drag-and-drop operation, the treeview needs to
						// be inside a component which is also a DragAndDropContainer.
						jassertfalse
					}
				}
			}
		}
	}

	void mouseMove (const MouseEvent& e)
	{
		updateButtonUnderMouse (e);
	}

	void mouseExit (const MouseEvent& e)
	{
		updateButtonUnderMouse (e);
	}

	void paint (Graphics& g);
	TreeViewItem* findItemAt (int y, Rectangle<int>& itemPosition) const;

	static bool isMouseDraggingInChildCompOf (Component* const comp)
	{
		for (int i = Desktop::getInstance().getNumMouseSources(); --i >= 0;)
		{
			MouseInputSource* source = Desktop::getInstance().getMouseSource(i);

			if (source->isDragging())
			{
				Component* const underMouse = source->getComponentUnderMouse();

				if (underMouse != 0 && (comp == underMouse || comp->isParentOf (underMouse)))
					return true;
			}
		}

		return false;
	}

	void updateComponents()
	{
		const int visibleTop = -getY();
		const int visibleBottom = visibleTop + getParentHeight();

		BigInteger itemsToKeep;
		TreeViewItem* item = owner->rootItem;
		int y = (item != 0 && !owner->rootItemVisible) ? -item->itemHeight : 0;

		while (item != 0 && y < visibleBottom)
		{
			y += item->itemHeight;

			if (y >= visibleTop)
			{
				const int index = rowComponentIds.indexOf (item->uid);

				if (index < 0)
				{
					Component* const comp = item->createItemComponent();

					if (comp != 0)
					{
						addAndMakeVisible (comp);
						itemsToKeep.setBit (rowComponentItems.size());
						rowComponentItems.add (item);
						rowComponentIds.add (item->uid);
						rowComponents.add (comp);
					}
				}
				else
				{
					itemsToKeep.setBit (index);
				}
			}

			item = item->getNextVisibleItem (true);
		}

		for (int i = rowComponentItems.size(); --i >= 0;)
		{
			Component* const comp = rowComponents.getUnchecked(i);

			bool keep = false;

			if (isParentOf (comp))
			{
				if (itemsToKeep[i])
				{
					const TreeViewItem* const item = rowComponentItems.getUnchecked(i);

					Rectangle<int> pos (item->getItemPosition (false));
					pos.setSize (pos.getWidth(), item->itemHeight);

					if (pos.getBottom() >= visibleTop && pos.getY() < visibleBottom)
					{
						keep = true;
						comp->setBounds (pos);
					}
				}

				if ((! keep) && isMouseDraggingInChildCompOf (comp))
				{
					keep = true;
					comp->setSize (0, 0);
				}
			}

			if (! keep)
			{
				delete comp;
				rowComponents.remove (i);
				rowComponentIds.remove (i);
				rowComponentItems.remove (i);
			}
		}
	}

	void updateButtonUnderMouse (const MouseEvent& e)
	{
		TreeViewItem* newItem = 0;

		if (owner->openCloseButtonsVisible)
		{
			Rectangle<int> pos;
			TreeViewItem* item = findItemAt (e.y, pos);

			if (item != 0 && e.x < pos.getX() && e.x >= pos.getX() - owner->getIndentSize())
			{
				newItem = item;

				if (! newItem->mightContainSubItems())
					newItem = 0;
			}
		}

		if (buttonUnderMouse != newItem)
		{
			if (buttonUnderMouse != 0 && containsItem (buttonUnderMouse))
			{
				const Rectangle<int> r (buttonUnderMouse->getItemPosition (false));
				repaint (0, r.getY(), r.getX(), buttonUnderMouse->getItemHeight());
			}

			buttonUnderMouse = newItem;

			if (buttonUnderMouse != 0)
			{
				const Rectangle<int> r (buttonUnderMouse->getItemPosition (false));
				repaint (0, r.getY(), r.getX(), buttonUnderMouse->getItemHeight());
			}
		}
	}

	bool isMouseOverButton (TreeViewItem* item) const throw()
	{
		return item == buttonUnderMouse;
	}

	void resized()
	{
		owner->itemsChanged();
	}

	const String getTooltip()
	{
		Rectangle<int> pos;
		TreeViewItem* const item = findItemAt (getMouseXYRelative().getY(), pos);

		if (item != 0)
			return item->getTooltip();

		return owner->getTooltip();
	}

	juce_UseDebuggingNewOperator

private:
	TreeView* const owner;

	Array <TreeViewItem*> rowComponentItems;
	Array <int> rowComponentIds;
	Array <Component*> rowComponents;
	TreeViewItem* buttonUnderMouse;
	bool isDragging, needSelectionOnMouseUp;

	TreeViewContentComponent (const TreeViewContentComponent&);
	TreeViewContentComponent& operator= (const TreeViewContentComponent&);

	void selectBasedOnModifiers (TreeViewItem* const item, const ModifierKeys& modifiers)
	{
		TreeViewItem* firstSelected = 0;

		if (modifiers.isShiftDown() && ((firstSelected = owner->getSelectedItem (0)) != 0))
		{
			TreeViewItem* const lastSelected = owner->getSelectedItem (owner->getNumSelectedItems() - 1);
			jassert (lastSelected != 0);

			int rowStart = firstSelected->getRowNumberInTree();
			int rowEnd = lastSelected->getRowNumberInTree();
			if (rowStart > rowEnd)
				swapVariables (rowStart, rowEnd);

			int ourRow = item->getRowNumberInTree();
			int otherEnd = ourRow < rowEnd ? rowStart : rowEnd;

			if (ourRow > otherEnd)
				swapVariables (ourRow, otherEnd);

			for (int i = ourRow; i <= otherEnd; ++i)
				owner->getItemOnRow (i)->setSelected (true, false);
		}
		else
		{
			const bool cmd  = modifiers.isCommandDown();

			item->setSelected ((! cmd) || (! item->isSelected()), ! cmd);
		}
	}

	bool containsItem (TreeViewItem* const item) const
	{
		for (int i = rowComponentItems.size(); --i >= 0;)
			if (rowComponentItems.getUnchecked(i) == item)
				return true;

		return false;
	}
};

class TreeView::TreeViewport  : public Viewport
{
public:
	TreeViewport() throw()	  {}
	~TreeViewport() throw()	 {}

	void updateComponents()
	{
		TreeViewContentComponent* const tvc = static_cast <TreeViewContentComponent*> (getViewedComponent());
		if (tvc != 0)
			tvc->updateComponents();

		repaint();
	}

	void visibleAreaChanged (int, int, int, int)
	{
		updateComponents();
	}

	juce_UseDebuggingNewOperator

private:
	TreeViewport (const TreeViewport&);
	TreeViewport& operator= (const TreeViewport&);
};

TreeView::TreeView (const String& componentName)
	: Component (componentName),
	  rootItem (0),
	  dragInsertPointHighlight (0),
	  dragTargetGroupHighlight (0),
	  indentSize (24),
	  defaultOpenness (false),
	  needsRecalculating (true),
	  rootItemVisible (true),
	  multiSelectEnabled (false),
	  openCloseButtonsVisible (true)
{
	addAndMakeVisible (viewport = new TreeViewport());
	viewport->setViewedComponent (new TreeViewContentComponent (this));
	viewport->setWantsKeyboardFocus (false);
	setWantsKeyboardFocus (true);
}

TreeView::~TreeView()
{
	if (rootItem != 0)
		rootItem->setOwnerView (0);

	deleteAllChildren();
}

void TreeView::setRootItem (TreeViewItem* const newRootItem)
{
	if (rootItem != newRootItem)
	{
		if (newRootItem != 0)
		{
			jassert (newRootItem->ownerView == 0); // can't use a tree item in more than one tree at once..

			if (newRootItem->ownerView != 0)
				newRootItem->ownerView->setRootItem (0);
		}

		if (rootItem != 0)
			rootItem->setOwnerView (0);

		rootItem = newRootItem;

		if (newRootItem != 0)
			newRootItem->setOwnerView (this);

		needsRecalculating = true;
		handleAsyncUpdate();

		if (rootItem != 0 && (defaultOpenness || ! rootItemVisible))
		{
			rootItem->setOpen (false); // force a re-open
			rootItem->setOpen (true);
		}
	}
}

void TreeView::deleteRootItem()
{
	const ScopedPointer <TreeViewItem> deleter (rootItem);
	setRootItem (0);
}

void TreeView::setRootItemVisible (const bool shouldBeVisible)
{
	rootItemVisible = shouldBeVisible;

	if (rootItem != 0 && (defaultOpenness || ! rootItemVisible))
	{
		rootItem->setOpen (false); // force a re-open
		rootItem->setOpen (true);
	}

	itemsChanged();
}

void TreeView::colourChanged()
{
	setOpaque (findColour (backgroundColourId).isOpaque());
	repaint();
}

void TreeView::setIndentSize (const int newIndentSize)
{
	if (indentSize != newIndentSize)
	{
		indentSize = newIndentSize;
		resized();
	}
}

void TreeView::setDefaultOpenness (const bool isOpenByDefault)
{
	if (defaultOpenness != isOpenByDefault)
	{
		defaultOpenness = isOpenByDefault;
		itemsChanged();
	}
}

void TreeView::setMultiSelectEnabled (const bool canMultiSelect)
{
	multiSelectEnabled = canMultiSelect;
}

void TreeView::setOpenCloseButtonsVisible (const bool shouldBeVisible)
{
	if (openCloseButtonsVisible != shouldBeVisible)
	{
		openCloseButtonsVisible = shouldBeVisible;
		itemsChanged();
	}
}

Viewport* TreeView::getViewport() const throw()
{
	return viewport;
}

void TreeView::clearSelectedItems()
{
	if (rootItem != 0)
		rootItem->deselectAllRecursively();
}

int TreeView::getNumSelectedItems() const throw()
{
	return (rootItem != 0) ? rootItem->countSelectedItemsRecursively() : 0;
}

TreeViewItem* TreeView::getSelectedItem (const int index) const throw()
{
	return (rootItem != 0) ? rootItem->getSelectedItemWithIndex (index) : 0;
}

int TreeView::getNumRowsInTree() const
{
	if (rootItem != 0)
		return rootItem->getNumRows() - (rootItemVisible ? 0 : 1);

	return 0;
}

TreeViewItem* TreeView::getItemOnRow (int index) const
{
	if (! rootItemVisible)
		++index;

	if (rootItem != 0 && index >= 0)
		return rootItem->getItemOnRow (index);

	return 0;
}

TreeViewItem* TreeView::getItemAt (int y) const throw()
{
	TreeViewContentComponent* const tc = static_cast <TreeViewContentComponent*> (viewport->getViewedComponent());
	Rectangle<int> pos;
	return tc->findItemAt (relativePositionToOtherComponent (tc, Point<int> (0, y)).getY(), pos);
}

TreeViewItem* TreeView::findItemFromIdentifierString (const String& identifierString) const
{
	if (rootItem == 0)
		return 0;

	return rootItem->findItemFromIdentifierString (identifierString);
}

XmlElement* TreeView::getOpennessState (const bool alsoIncludeScrollPosition) const
{
	XmlElement* e = 0;

	if (rootItem != 0)
	{
		e = rootItem->getOpennessState();

		if (e != 0 && alsoIncludeScrollPosition)
			e->setAttribute ("scrollPos", viewport->getViewPositionY());
	}

	return e;
}

void TreeView::restoreOpennessState (const XmlElement& newState)
{
	if (rootItem != 0)
	{
		rootItem->restoreOpennessState (newState);

		if (newState.hasAttribute ("scrollPos"))
			viewport->setViewPosition (viewport->getViewPositionX(),
									   newState.getIntAttribute ("scrollPos"));
	}
}

void TreeView::paint (Graphics& g)
{
	g.fillAll (findColour (backgroundColourId));
}

void TreeView::resized()
{
	viewport->setBounds (0, 0, getWidth(), getHeight());

	itemsChanged();
	handleAsyncUpdate();
}

void TreeView::enablementChanged()
{
	repaint();
}

void TreeView::moveSelectedRow (int delta)
{
	if (delta == 0)
		return;

	int rowSelected = 0;

	TreeViewItem* const firstSelected = getSelectedItem (0);
	if (firstSelected != 0)
		rowSelected = firstSelected->getRowNumberInTree();

	rowSelected = jlimit (0, getNumRowsInTree() - 1, rowSelected + delta);

	for (;;)
	{
		TreeViewItem* item = getItemOnRow (rowSelected);

		if (item != 0)
		{
			if (! item->canBeSelected())
			{
				// if the row we want to highlight doesn't allow it, try skipping
				// to the next item..
				const int nextRowToTry = jlimit (0, getNumRowsInTree() - 1,
												 rowSelected + (delta < 0 ? -1 : 1));

				if (rowSelected != nextRowToTry)
				{
					rowSelected = nextRowToTry;
					continue;
				}
				else
				{
					break;
				}
			}

			item->setSelected (true, true);

			scrollToKeepItemVisible (item);
		}

		break;
	}
}

void TreeView::scrollToKeepItemVisible (TreeViewItem* item)
{
	if (item != 0 && item->ownerView == this)
	{
		handleAsyncUpdate();

		item = item->getDeepestOpenParentItem();

		int y = item->y;
		int viewTop = viewport->getViewPositionY();

		if (y < viewTop)
		{
			viewport->setViewPosition (viewport->getViewPositionX(), y);
		}
		else if (y + item->itemHeight > viewTop + viewport->getViewHeight())
		{
			viewport->setViewPosition (viewport->getViewPositionX(),
									   (y + item->itemHeight) - viewport->getViewHeight());
		}
	}
}

bool TreeView::keyPressed (const KeyPress& key)
{
	if (key.isKeyCode (KeyPress::upKey))
	{
		moveSelectedRow (-1);
	}
	else if (key.isKeyCode (KeyPress::downKey))
	{
		moveSelectedRow (1);
	}
	else if (key.isKeyCode (KeyPress::pageDownKey) || key.isKeyCode (KeyPress::pageUpKey))
	{
		if (rootItem != 0)
		{
			int rowsOnScreen = getHeight() / jmax (1, rootItem->itemHeight);

			if (key.isKeyCode (KeyPress::pageUpKey))
				rowsOnScreen = -rowsOnScreen;

			moveSelectedRow (rowsOnScreen);
		}
	}
	else if (key.isKeyCode (KeyPress::homeKey))
	{
		moveSelectedRow (-0x3fffffff);
	}
	else if (key.isKeyCode (KeyPress::endKey))
	{
		moveSelectedRow (0x3fffffff);
	}
	else if (key.isKeyCode (KeyPress::returnKey))
	{
		TreeViewItem* const firstSelected = getSelectedItem (0);
		if (firstSelected != 0)
			firstSelected->setOpen (! firstSelected->isOpen());
	}
	else if (key.isKeyCode (KeyPress::leftKey))
	{
		TreeViewItem* const firstSelected = getSelectedItem (0);

		if (firstSelected != 0)
		{
			if (firstSelected->isOpen())
			{
				firstSelected->setOpen (false);
			}
			else
			{
				TreeViewItem* parent = firstSelected->parentItem;

				if ((! rootItemVisible) && parent == rootItem)
					parent = 0;

				if (parent != 0)
				{
					parent->setSelected (true, true);
					scrollToKeepItemVisible (parent);
				}
			}
		}
	}
	else if (key.isKeyCode (KeyPress::rightKey))
	{
		TreeViewItem* const firstSelected = getSelectedItem (0);

		if (firstSelected != 0)
		{
			if (firstSelected->isOpen() || ! firstSelected->mightContainSubItems())
				moveSelectedRow (1);
			else
				firstSelected->setOpen (true);
		}
	}
	else
	{
		return false;
	}

	return true;
}

void TreeView::itemsChanged() throw()
{
	needsRecalculating = true;
	repaint();
	triggerAsyncUpdate();
}

void TreeView::handleAsyncUpdate()
{
	if (needsRecalculating)
	{
		needsRecalculating = false;

		const ScopedLock sl (nodeAlterationLock);

		if (rootItem != 0)
			rootItem->updatePositions (rootItemVisible ? 0 : -rootItem->itemHeight);

		viewport->updateComponents();

		if (rootItem != 0)
		{
			viewport->getViewedComponent()
				->setSize (jmax (viewport->getMaximumVisibleWidth(), rootItem->totalWidth),
						   rootItem->totalHeight - (rootItemVisible ? 0 : rootItem->itemHeight));
		}
		else
		{
			viewport->getViewedComponent()->setSize (0, 0);
		}
	}
}

class TreeView::InsertPointHighlight   : public Component
{
public:
	InsertPointHighlight()
		: lastItem (0)
	{
		setSize (100, 12);
		setAlwaysOnTop (true);
		setInterceptsMouseClicks (false, false);
	}

	~InsertPointHighlight() {}

	void setTargetPosition (TreeViewItem* const item, int insertIndex, const int x, const int y, const int width) throw()
	{
		lastItem = item;
		lastIndex = insertIndex;
		const int offset = getHeight() / 2;
		setBounds (x - offset, y - offset, width - (x - offset), getHeight());
	}

	void paint (Graphics& g)
	{
		Path p;
		const float h = (float) getHeight();
		p.addEllipse (2.0f, 2.0f, h - 4.0f, h - 4.0f);
		p.startNewSubPath (h - 2.0f, h / 2.0f);
		p.lineTo ((float) getWidth(), h / 2.0f);

		g.setColour (findColour (TreeView::dragAndDropIndicatorColourId, true));
		g.strokePath (p, PathStrokeType (2.0f));
	}

	TreeViewItem* lastItem;
	int lastIndex;

private:
	InsertPointHighlight (const InsertPointHighlight&);
	InsertPointHighlight& operator= (const InsertPointHighlight&);
};

class TreeView::TargetGroupHighlight   : public Component
{
public:
	TargetGroupHighlight()
	{
		setAlwaysOnTop (true);
		setInterceptsMouseClicks (false, false);
	}

	~TargetGroupHighlight() {}

	void setTargetPosition (TreeViewItem* const item) throw()
	{
		Rectangle<int> r (item->getItemPosition (true));
		r.setHeight (item->getItemHeight());
		setBounds (r);
	}

	void paint (Graphics& g)
	{
		g.setColour (findColour (TreeView::dragAndDropIndicatorColourId, true));
		g.drawRoundedRectangle (1.0f, 1.0f, getWidth() - 2.0f, getHeight() - 2.0f, 3.0f, 2.0f);
	}

private:
	TargetGroupHighlight (const TargetGroupHighlight&);
	TargetGroupHighlight& operator= (const TargetGroupHighlight&);
};

void TreeView::showDragHighlight (TreeViewItem* item, int insertIndex, int x, int y) throw()
{
	beginDragAutoRepeat (1000 / 30);

	if (dragInsertPointHighlight == 0)
	{
		addAndMakeVisible (dragInsertPointHighlight = new InsertPointHighlight());
		addAndMakeVisible (dragTargetGroupHighlight = new TargetGroupHighlight());
	}

	dragInsertPointHighlight->setTargetPosition (item, insertIndex, x, y, viewport->getViewWidth());
	dragTargetGroupHighlight->setTargetPosition (item);
}

void TreeView::hideDragHighlight() throw()
{
	deleteAndZero (dragInsertPointHighlight);
	deleteAndZero (dragTargetGroupHighlight);
}

TreeViewItem* TreeView::getInsertPosition (int& x, int& y, int& insertIndex,
										   const StringArray& files, const String& sourceDescription,
										   Component* sourceComponent) const throw()
{
	insertIndex = 0;
	TreeViewItem* item = getItemAt (y);

	if (item == 0)
		return 0;

	Rectangle<int> itemPos (item->getItemPosition (true));
	insertIndex = item->getIndexInParent();
	const int oldY = y;
	y = itemPos.getY();

	if (item->getNumSubItems() == 0 || ! item->isOpen())
	{
		if (files.size() > 0 ? item->isInterestedInFileDrag (files)
							 : item->isInterestedInDragSource (sourceDescription, sourceComponent))
		{
			// Check if we're trying to drag into an empty group item..
			if (oldY > itemPos.getY() + itemPos.getHeight() / 4
				 && oldY < itemPos.getBottom() - itemPos.getHeight() / 4)
			{
				insertIndex = 0;
				x = itemPos.getX() + getIndentSize();
				y = itemPos.getBottom();
				return item;
			}
		}
	}

	if (oldY > itemPos.getCentreY())
	{
		y += item->getItemHeight();

		while (item->isLastOfSiblings() && item->parentItem != 0
				&& item->parentItem->parentItem != 0)
		{
			if (x > itemPos.getX())
				break;

			item = item->parentItem;
			itemPos = item->getItemPosition (true);
			insertIndex = item->getIndexInParent();
		}

		++insertIndex;
	}

	x = itemPos.getX();
	return item->parentItem;
}

void TreeView::handleDrag (const StringArray& files, const String& sourceDescription, Component* sourceComponent, int x, int y)
{
	const bool scrolled = viewport->autoScroll (x, y, 20, 10);

	int insertIndex;
	TreeViewItem* const item = getInsertPosition (x, y, insertIndex, files, sourceDescription, sourceComponent);

	if (item != 0)
	{
		if (scrolled || dragInsertPointHighlight == 0
			 || dragInsertPointHighlight->lastItem != item
			 || dragInsertPointHighlight->lastIndex != insertIndex)
		{
			if (files.size() > 0 ? item->isInterestedInFileDrag (files)
								 : item->isInterestedInDragSource (sourceDescription, sourceComponent))
				showDragHighlight (item, insertIndex, x, y);
			else
				hideDragHighlight();
		}
	}
	else
	{
		hideDragHighlight();
	}
}

void TreeView::handleDrop (const StringArray& files, const String& sourceDescription, Component* sourceComponent, int x, int y)
{
	hideDragHighlight();

	int insertIndex;
	TreeViewItem* const item = getInsertPosition (x, y, insertIndex, files, sourceDescription, sourceComponent);

	if (item != 0)
	{
		if (files.size() > 0)
		{
			if (item->isInterestedInFileDrag (files))
				item->filesDropped (files, insertIndex);
		}
		else
		{
			if (item->isInterestedInDragSource (sourceDescription, sourceComponent))
				item->itemDropped (sourceDescription, sourceComponent, insertIndex);
		}
	}
}

bool TreeView::isInterestedInFileDrag (const StringArray&)
{
	return true;
}

void TreeView::fileDragEnter (const StringArray& files, int x, int y)
{
	fileDragMove (files, x, y);
}

void TreeView::fileDragMove (const StringArray& files, int x, int y)
{
	handleDrag (files, String::empty, 0, x, y);
}

void TreeView::fileDragExit (const StringArray&)
{
	hideDragHighlight();
}

void TreeView::filesDropped (const StringArray& files, int x, int y)
{
	handleDrop (files, String::empty, 0, x, y);
}

bool TreeView::isInterestedInDragSource (const String& /*sourceDescription*/, Component* /*sourceComponent*/)
{
	return true;
}

void TreeView::itemDragEnter (const String& sourceDescription, Component* sourceComponent, int x, int y)
{
	itemDragMove (sourceDescription, sourceComponent, x, y);
}

void TreeView::itemDragMove (const String& sourceDescription, Component* sourceComponent, int x, int y)
{
	handleDrag (StringArray(), sourceDescription, sourceComponent, x, y);
}

void TreeView::itemDragExit (const String& /*sourceDescription*/, Component* /*sourceComponent*/)
{
	hideDragHighlight();
}

void TreeView::itemDropped (const String& sourceDescription, Component* sourceComponent, int x, int y)
{
	handleDrop (StringArray(), sourceDescription, sourceComponent, x, y);
}

void TreeViewContentComponent::paint (Graphics& g)
{
	if (owner->rootItem != 0)
	{
		owner->handleAsyncUpdate();

		if (! owner->rootItemVisible)
			g.setOrigin (0, -owner->rootItem->itemHeight);

		owner->rootItem->paintRecursively (g, getWidth());
	}
}

TreeViewItem* TreeViewContentComponent::findItemAt (int y, Rectangle<int>& itemPosition) const
{
	if (owner->rootItem != 0)
	{
		owner->handleAsyncUpdate();

		if (! owner->rootItemVisible)
			y += owner->rootItem->itemHeight;

		TreeViewItem* const ti = owner->rootItem->findItemRecursively (y);

		if (ti != 0)
			itemPosition = ti->getItemPosition (false);

		return ti;
	}

	return 0;
}

enum TreeViewOpenness
{
	opennessDefault = 0,
	opennessClosed = 1,
	opennessOpen = 2
};

TreeViewItem::TreeViewItem()
	: ownerView (0),
	  parentItem (0),
	  y (0),
	  itemHeight (0),
	  totalHeight (0),
	  selected (false),
	  redrawNeeded (true),
	  drawLinesInside (true),
	  drawsInLeftMargin (false),
	  openness (opennessDefault)
{
	static int nextUID = 0;
	uid = nextUID++;
}

TreeViewItem::~TreeViewItem()
{
}

const String TreeViewItem::getUniqueName() const
{
	return String::empty;
}

void TreeViewItem::itemOpennessChanged (bool)
{
}

int TreeViewItem::getNumSubItems() const throw()
{
	return subItems.size();
}

TreeViewItem* TreeViewItem::getSubItem (const int index) const throw()
{
	return subItems [index];
}

void TreeViewItem::clearSubItems()
{
	if (subItems.size() > 0)
	{
		if (ownerView != 0)
		{
			const ScopedLock sl (ownerView->nodeAlterationLock);
			subItems.clear();
			treeHasChanged();
		}
		else
		{
			subItems.clear();
		}
	}
}

void TreeViewItem::addSubItem (TreeViewItem* const newItem, const int insertPosition)
{
	if (newItem != 0)
	{
		newItem->parentItem = this;
		newItem->setOwnerView (ownerView);
		newItem->y = 0;
		newItem->itemHeight = newItem->getItemHeight();
		newItem->totalHeight = 0;
		newItem->itemWidth = newItem->getItemWidth();
		newItem->totalWidth = 0;

		if (ownerView != 0)
		{
			const ScopedLock sl (ownerView->nodeAlterationLock);
			subItems.insert (insertPosition, newItem);
			treeHasChanged();

			if (newItem->isOpen())
				newItem->itemOpennessChanged (true);
		}
		else
		{
			subItems.insert (insertPosition, newItem);

			if (newItem->isOpen())
				newItem->itemOpennessChanged (true);
		}
	}
}

void TreeViewItem::removeSubItem (const int index, const bool deleteItem)
{
	if (ownerView != 0)
	{
		const ScopedLock sl (ownerView->nodeAlterationLock);

		if (((unsigned int) index) < (unsigned int) subItems.size())
		{
			subItems.remove (index, deleteItem);
			treeHasChanged();
		}
	}
	else
	{
		subItems.remove (index, deleteItem);
	}
}

bool TreeViewItem::isOpen() const throw()
{
	if (openness == opennessDefault)
		return ownerView != 0 && ownerView->defaultOpenness;
	else
		return openness == opennessOpen;
}

void TreeViewItem::setOpen (const bool shouldBeOpen)
{
	if (isOpen() != shouldBeOpen)
	{
		openness = shouldBeOpen ? opennessOpen
								: opennessClosed;

		treeHasChanged();

		itemOpennessChanged (isOpen());
	}
}

bool TreeViewItem::isSelected() const throw()
{
	return selected;
}

void TreeViewItem::deselectAllRecursively()
{
	setSelected (false, false);

	for (int i = 0; i < subItems.size(); ++i)
		subItems.getUnchecked(i)->deselectAllRecursively();
}

void TreeViewItem::setSelected (const bool shouldBeSelected,
								const bool deselectOtherItemsFirst)
{
	if (shouldBeSelected && ! canBeSelected())
		return;

	if (deselectOtherItemsFirst)
		getTopLevelItem()->deselectAllRecursively();

	if (shouldBeSelected != selected)
	{
		selected = shouldBeSelected;
		if (ownerView != 0)
			ownerView->repaint();

		itemSelectionChanged (shouldBeSelected);
	}
}

void TreeViewItem::paintItem (Graphics&, int, int)
{
}

void TreeViewItem::paintOpenCloseButton (Graphics& g, int width, int height, bool isMouseOver)
{
	ownerView->getLookAndFeel()
	   .drawTreeviewPlusMinusBox (g, 0, 0, width, height, ! isOpen(), isMouseOver);
}

void TreeViewItem::itemClicked (const MouseEvent&)
{
}

void TreeViewItem::itemDoubleClicked (const MouseEvent&)
{
	if (mightContainSubItems())
		setOpen (! isOpen());
}

void TreeViewItem::itemSelectionChanged (bool)
{
}

const String TreeViewItem::getTooltip()
{
	return String::empty;
}

const String TreeViewItem::getDragSourceDescription()
{
	return String::empty;
}

bool TreeViewItem::isInterestedInFileDrag (const StringArray&)
{
	return false;
}

void TreeViewItem::filesDropped (const StringArray& /*files*/, int /*insertIndex*/)
{
}

bool TreeViewItem::isInterestedInDragSource (const String& /*sourceDescription*/, Component* /*sourceComponent*/)
{
	return false;
}

void TreeViewItem::itemDropped (const String& /*sourceDescription*/, Component* /*sourceComponent*/, int /*insertIndex*/)
{
}

const Rectangle<int> TreeViewItem::getItemPosition (const bool relativeToTreeViewTopLeft) const throw()
{
	const int indentX = getIndentX();

	int width = itemWidth;

	if (ownerView != 0 && width < 0)
		width = ownerView->viewport->getViewWidth() - indentX;

	Rectangle<int> r (indentX, y, jmax (0, width), totalHeight);

	if (relativeToTreeViewTopLeft)
		r.setPosition (r.getX() - ownerView->viewport->getViewPositionX(),
					   r.getY() - ownerView->viewport->getViewPositionY());

	return r;
}

void TreeViewItem::treeHasChanged() const throw()
{
	if (ownerView != 0)
		ownerView->itemsChanged();
}

void TreeViewItem::repaintItem() const
{
	if (ownerView != 0 && areAllParentsOpen())
	{
		const Rectangle<int> r (getItemPosition (true));
		ownerView->viewport->repaint (0, r.getY(), r.getRight(), r.getHeight());
	}
}

bool TreeViewItem::areAllParentsOpen() const throw()
{
	return parentItem == 0
			|| (parentItem->isOpen() && parentItem->areAllParentsOpen());
}

void TreeViewItem::updatePositions (int newY)
{
	y = newY;
	itemHeight = getItemHeight();
	totalHeight = itemHeight;
	itemWidth = getItemWidth();
	totalWidth = jmax (itemWidth, 0) + getIndentX();

	if (isOpen())
	{
		newY += totalHeight;

		for (int i = 0; i < subItems.size(); ++i)
		{
			TreeViewItem* const ti = subItems.getUnchecked(i);

			ti->updatePositions (newY);
			newY += ti->totalHeight;
			totalHeight += ti->totalHeight;
			totalWidth = jmax (totalWidth, ti->totalWidth);
		}
	}
}

TreeViewItem* TreeViewItem::getDeepestOpenParentItem() throw()
{
	TreeViewItem* result = this;
	TreeViewItem* item = this;

	while (item->parentItem != 0)
	{
		item = item->parentItem;

		if (! item->isOpen())
			result = item;
	}

	return result;
}

void TreeViewItem::setOwnerView (TreeView* const newOwner) throw()
{
	ownerView = newOwner;

	for (int i = subItems.size(); --i >= 0;)
		subItems.getUnchecked(i)->setOwnerView (newOwner);
}

int TreeViewItem::getIndentX() const throw()
{
	const int indentWidth = ownerView->getIndentSize();
	int x = ownerView->rootItemVisible ? indentWidth : 0;

	if (! ownerView->openCloseButtonsVisible)
		x -= indentWidth;

	TreeViewItem* p = parentItem;

	while (p != 0)
	{
		x += indentWidth;
		p = p->parentItem;
	}

	return x;
}

void TreeViewItem::setDrawsInLeftMargin (bool canDrawInLeftMargin) throw()
{
	drawsInLeftMargin = canDrawInLeftMargin;
}

void TreeViewItem::paintRecursively (Graphics& g, int width)
{
	jassert (ownerView != 0);
	if (ownerView == 0)
		return;

	const int indent = getIndentX();
	const int itemW = itemWidth < 0 ? width - indent : itemWidth;

	g.setColour (ownerView->findColour (TreeView::linesColourId));

	const float halfH = itemHeight * 0.5f;
	int depth = 0;
	TreeViewItem* p = parentItem;

	while (p != 0)
	{
		++depth;
		p = p->parentItem;
	}

	if (! ownerView->rootItemVisible)
		--depth;

	const int indentWidth = ownerView->getIndentSize();

	if (depth >= 0 && ownerView->openCloseButtonsVisible)
	{
		float x = (depth + 0.5f) * indentWidth;

		if (depth >= 0)
		{
			if (parentItem != 0 && parentItem->drawLinesInside)
				g.drawLine (x, 0, x, isLastOfSiblings() ? halfH : (float) itemHeight);

			if ((parentItem != 0 && parentItem->drawLinesInside)
				 || (parentItem == 0 && drawLinesInside))
				g.drawLine (x, halfH, x + indentWidth / 2, halfH);
		}

		p = parentItem;
		int d = depth;

		while (p != 0 && --d >= 0)
		{
			x -= (float) indentWidth;

			if ((p->parentItem == 0 || p->parentItem->drawLinesInside)
				 && ! p->isLastOfSiblings())
			{
				g.drawLine (x, 0, x, (float) itemHeight);
			}

			p = p->parentItem;
		}

		if (mightContainSubItems())
		{
			g.saveState();
			g.setOrigin (depth * indentWidth, 0);
			g.reduceClipRegion (0, 0, indentWidth, itemHeight);

			paintOpenCloseButton (g, indentWidth, itemHeight,
				static_cast <TreeViewContentComponent*> (ownerView->viewport->getViewedComponent())
					->isMouseOverButton (this));

			g.restoreState();
		}
	}

	{
		g.saveState();
		g.setOrigin (indent, 0);

		if (g.reduceClipRegion (drawsInLeftMargin ? -indent : 0, 0,
								drawsInLeftMargin ? itemW + indent : itemW, itemHeight))
			paintItem (g, itemW, itemHeight);

		g.restoreState();
	}

	if (isOpen())
	{
		const Rectangle<int> clip (g.getClipBounds());

		for (int i = 0; i < subItems.size(); ++i)
		{
			TreeViewItem* const ti = subItems.getUnchecked(i);

			const int relY = ti->y - y;

			if (relY >= clip.getBottom())
				break;

			if (relY + ti->totalHeight >= clip.getY())
			{
				g.saveState();
				g.setOrigin (0, relY);

				if (g.reduceClipRegion (0, 0, width, ti->totalHeight))
					ti->paintRecursively (g, width);

				g.restoreState();
			}
		}
	}
}

bool TreeViewItem::isLastOfSiblings() const throw()
{
	return parentItem == 0
		|| parentItem->subItems.getLast() == this;
}

int TreeViewItem::getIndexInParent() const throw()
{
	if (parentItem == 0)
		return 0;

	return parentItem->subItems.indexOf (this);
}

TreeViewItem* TreeViewItem::getTopLevelItem() throw()
{
	return (parentItem == 0) ? this
							 : parentItem->getTopLevelItem();
}

int TreeViewItem::getNumRows() const throw()
{
	int num = 1;

	if (isOpen())
	{
		for (int i = subItems.size(); --i >= 0;)
			num += subItems.getUnchecked(i)->getNumRows();
	}

	return num;
}

TreeViewItem* TreeViewItem::getItemOnRow (int index) throw()
{
	if (index == 0)
		return this;

	if (index > 0 && isOpen())
	{
		--index;

		for (int i = 0; i < subItems.size(); ++i)
		{
			TreeViewItem* const item = subItems.getUnchecked(i);

			if (index == 0)
				return item;

			const int numRows = item->getNumRows();

			if (numRows > index)
				return item->getItemOnRow (index);

			index -= numRows;
		}
	}

	return 0;
}

TreeViewItem* TreeViewItem::findItemRecursively (int targetY) throw()
{
	if (((unsigned int) targetY) < (unsigned int) totalHeight)
	{
		const int h = itemHeight;

		if (targetY < h)
			return this;

		if (isOpen())
		{
			targetY -= h;

			for (int i = 0; i < subItems.size(); ++i)
			{
				TreeViewItem* const ti = subItems.getUnchecked(i);

				if (targetY < ti->totalHeight)
					return ti->findItemRecursively (targetY);

				targetY -= ti->totalHeight;
			}
		}
	}

	return 0;
}

int TreeViewItem::countSelectedItemsRecursively() const throw()
{
	int total = 0;

	if (isSelected())
		++total;

	for (int i = subItems.size(); --i >= 0;)
		total += subItems.getUnchecked(i)->countSelectedItemsRecursively();

	return total;
}

TreeViewItem* TreeViewItem::getSelectedItemWithIndex (int index) throw()
{
	if (isSelected())
	{
		if (index == 0)
			return this;

		--index;
	}

	if (index >= 0)
	{
		for (int i = 0; i < subItems.size(); ++i)
		{
			TreeViewItem* const item = subItems.getUnchecked(i);

			TreeViewItem* const found = item->getSelectedItemWithIndex (index);

			if (found != 0)
				return found;

			index -= item->countSelectedItemsRecursively();
		}
	}

	return 0;
}

int TreeViewItem::getRowNumberInTree() const throw()
{
	if (parentItem != 0 && ownerView != 0)
	{
		int n = 1 + parentItem->getRowNumberInTree();

		int ourIndex = parentItem->subItems.indexOf (this);
		jassert (ourIndex >= 0);

		while (--ourIndex >= 0)
			n += parentItem->subItems [ourIndex]->getNumRows();

		if (parentItem->parentItem == 0
			 && ! ownerView->rootItemVisible)
			--n;

		return n;
	}
	else
	{
		return 0;
	}
}

void TreeViewItem::setLinesDrawnForSubItems (const bool drawLines) throw()
{
	drawLinesInside = drawLines;
}

TreeViewItem* TreeViewItem::getNextVisibleItem (const bool recurse) const throw()
{
	if (recurse && isOpen() && subItems.size() > 0)
		return subItems [0];

	if (parentItem != 0)
	{
		const int nextIndex = parentItem->subItems.indexOf (this) + 1;

		if (nextIndex >= parentItem->subItems.size())
			return parentItem->getNextVisibleItem (false);

		return parentItem->subItems [nextIndex];
	}

	return 0;
}

const String TreeViewItem::getItemIdentifierString() const
{
	String s;

	if (parentItem != 0)
		s = parentItem->getItemIdentifierString();

	return s + "/" + getUniqueName().replaceCharacter ('/', '\\');
}

TreeViewItem* TreeViewItem::findItemFromIdentifierString (const String& identifierString)
{
	const String uid (getUniqueName());

	if (uid == identifierString)
		return this;

	if (identifierString.startsWith (uid + "/"))
	{
		const String remainingPath (identifierString.substring (uid.length() + 1));

		bool wasOpen = isOpen();
		setOpen (true);

		for (int i = subItems.size(); --i >= 0;)
		{
			TreeViewItem* item = subItems.getUnchecked(i)->findItemFromIdentifierString (remainingPath);

			if (item != 0)
				return item;
		}

		setOpen (wasOpen);
	}

	return 0;
}

void TreeViewItem::restoreOpennessState (const XmlElement& e) throw()
{
	if (e.hasTagName ("CLOSED"))
	{
		setOpen (false);
	}
	else if (e.hasTagName ("OPEN"))
	{
		setOpen (true);

		forEachXmlChildElement (e, n)
		{
			const String id (n->getStringAttribute ("id"));

			for (int i = 0; i < subItems.size(); ++i)
			{
				TreeViewItem* const ti = subItems.getUnchecked(i);

				if (ti->getUniqueName() == id)
				{
					ti->restoreOpennessState (*n);
					break;
				}
			}
		}
	}
}

XmlElement* TreeViewItem::getOpennessState() const throw()
{
	const String name (getUniqueName());

	if (name.isNotEmpty())
	{
		XmlElement* e;

		if (isOpen())
		{
			e = new XmlElement ("OPEN");

			for (int i = 0; i < subItems.size(); ++i)
				e->addChildElement (subItems.getUnchecked(i)->getOpennessState());
		}
		else
		{
			e = new XmlElement ("CLOSED");
		}

		e->setAttribute ("id", name);

		return e;
	}
	else
	{
		// trying to save the openness for an element that has no name - this won't
		// work because it needs the names to identify what to open.
		jassertfalse
	}

	return 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TreeView.cpp ***/


/*** Start of inlined file: juce_DirectoryContentsDisplayComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

DirectoryContentsDisplayComponent::DirectoryContentsDisplayComponent (DirectoryContentsList& listToShow)
	: fileList (listToShow)
{
}

DirectoryContentsDisplayComponent::~DirectoryContentsDisplayComponent()
{
}

FileBrowserListener::~FileBrowserListener()
{
}

void DirectoryContentsDisplayComponent::addListener (FileBrowserListener* const listener)
{
	listeners.add (listener);
}

void DirectoryContentsDisplayComponent::removeListener (FileBrowserListener* const listener)
{
	listeners.remove (listener);
}

void DirectoryContentsDisplayComponent::sendSelectionChangeMessage()
{
	Component::BailOutChecker checker (dynamic_cast <Component*> (this));
	listeners.callChecked (checker, &FileBrowserListener::selectionChanged);
}

void DirectoryContentsDisplayComponent::sendMouseClickMessage (const File& file, const MouseEvent& e)
{
	if (fileList.getDirectory().exists())
	{
		Component::BailOutChecker checker (dynamic_cast <Component*> (this));
		listeners.callChecked (checker, &FileBrowserListener::fileClicked, file, e);
	}
}

void DirectoryContentsDisplayComponent::sendDoubleClickMessage (const File& file)
{
	if (fileList.getDirectory().exists())
	{
		Component::BailOutChecker checker (dynamic_cast <Component*> (this));
		listeners.callChecked (checker, &FileBrowserListener::fileDoubleClicked, file);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DirectoryContentsDisplayComponent.cpp ***/


/*** Start of inlined file: juce_DirectoryContentsList.cpp ***/
BEGIN_JUCE_NAMESPACE

void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
						  bool* isDirectory, bool* isHidden, int64* fileSize, Time* modTime,
						  Time* creationTime, bool* isReadOnly);
bool juce_findFileNext (void* handle, String& resultFile,
						bool* isDirectory, bool* isHidden, int64* fileSize,
						Time* modTime, Time* creationTime, bool* isReadOnly);
void juce_findFileClose (void* handle);

DirectoryContentsList::DirectoryContentsList (const FileFilter* const fileFilter_,
											  TimeSliceThread& thread_)
   : fileFilter (fileFilter_),
	 thread (thread_),
	 includeDirectories (false),
	 includeFiles (false),
	 ignoreHiddenFiles (true),
	 fileFindHandle (0),
	 shouldStop (true)
{
}

DirectoryContentsList::~DirectoryContentsList()
{
	clear();
}

void DirectoryContentsList::setIgnoresHiddenFiles (const bool shouldIgnoreHiddenFiles)
{
	ignoreHiddenFiles = shouldIgnoreHiddenFiles;
}

const File& DirectoryContentsList::getDirectory() const
{
	return root;
}

void DirectoryContentsList::setDirectory (const File& directory,
										  const bool includeDirectories_,
										  const bool includeFiles_)
{
	if (directory != root
		 || includeDirectories != includeDirectories_
		 || includeFiles != includeFiles_)
	{
		clear();

		root = directory;
		includeDirectories = includeDirectories_;
		includeFiles = includeFiles_;

		refresh();
	}
}

void DirectoryContentsList::clear()
{
	shouldStop = true;
	thread.removeTimeSliceClient (this);

	if (fileFindHandle != 0)
	{
		juce_findFileClose (fileFindHandle);
		fileFindHandle = 0;
	}

	if (files.size() > 0)
	{
		files.clear();
		changed();
	}
}

void DirectoryContentsList::refresh()
{
	clear();

	if (root.isDirectory())
	{
		String fileFound;
		bool fileFoundIsDir, isHidden, isReadOnly;
		int64 fileSize;
		Time modTime, creationTime;

		String path (root.getFullPathName());
		if (! path.endsWithChar (File::separator))
			path += File::separator;

		jassert (fileFindHandle == 0);

		fileFindHandle = juce_findFileStart (path, "*", fileFound,
											 &fileFoundIsDir,
											 &isHidden,
											 &fileSize,
											 &modTime,
											 &creationTime,
											 &isReadOnly);

		if (fileFindHandle != 0 && fileFound.isNotEmpty())
		{
			if (addFile (fileFound, fileFoundIsDir, isHidden,
						 fileSize, modTime, creationTime, isReadOnly))
			{
				changed();
			}
		}

		shouldStop = false;

		thread.addTimeSliceClient (this);
	}
}

int DirectoryContentsList::getNumFiles() const
{
	return files.size();
}

bool DirectoryContentsList::getFileInfo (const int index,
										 FileInfo& result) const
{
	const ScopedLock sl (fileListLock);
	const FileInfo* const info = files [index];

	if (info != 0)
	{
		result = *info;
		return true;
	}

	return false;
}

const File DirectoryContentsList::getFile (const int index) const
{
	const ScopedLock sl (fileListLock);
	const FileInfo* const info = files [index];

	if (info != 0)
		return root.getChildFile (info->filename);

	return File::nonexistent;
}

bool DirectoryContentsList::isStillLoading() const
{
	return fileFindHandle != 0;
}

void DirectoryContentsList::changed()
{
	sendChangeMessage (this);
}

bool DirectoryContentsList::useTimeSlice()
{
	const uint32 startTime = Time::getApproximateMillisecondCounter();
	bool hasChanged = false;

	for (int i = 100; --i >= 0;)
	{
		if (! checkNextFile (hasChanged))
		{
			if (hasChanged)
				changed();

			return false;
		}

		if (shouldStop || (Time::getApproximateMillisecondCounter() > startTime + 150))
			break;
	}

	if (hasChanged)
		changed();

	return true;
}

bool DirectoryContentsList::checkNextFile (bool& hasChanged)
{
	if (fileFindHandle != 0)
	{
		String fileFound;
		bool fileFoundIsDir, isHidden, isReadOnly;
		int64 fileSize;
		Time modTime, creationTime;

		if (juce_findFileNext (fileFindHandle, fileFound,
							   &fileFoundIsDir, &isHidden,
							   &fileSize,
							   &modTime,
							   &creationTime,
							   &isReadOnly))
		{
			if (addFile (fileFound, fileFoundIsDir, isHidden, fileSize,
						 modTime, creationTime, isReadOnly))
			{
				hasChanged = true;
			}

			return true;
		}
		else
		{
			juce_findFileClose (fileFindHandle);
			fileFindHandle = 0;
		}
	}

	return false;
}

int DirectoryContentsList::compareElements (const DirectoryContentsList::FileInfo* const first,
											const DirectoryContentsList::FileInfo* const second)
{
#if JUCE_WINDOWS
	if (first->isDirectory != second->isDirectory)
		return first->isDirectory ? -1 : 1;
#endif

	return first->filename.compareIgnoreCase (second->filename);
}

bool DirectoryContentsList::addFile (const String& filename,
									 const bool isDir,
									 const bool isHidden,
									 const int64 fileSize,
									 const Time& modTime,
									 const Time& creationTime,
									 const bool isReadOnly)
{
	if (filename == ".."
		 || filename == "."
		 || (ignoreHiddenFiles && isHidden))
		return false;

	const File file (root.getChildFile (filename));

	if (((isDir && includeDirectories) || ((! isDir) && includeFiles))
		 && (fileFilter == 0
			  || ((! isDir) && fileFilter->isFileSuitable (file))
			  || (isDir && fileFilter->isDirectorySuitable (file))))
	{
		ScopedPointer <FileInfo> info (new FileInfo());

		info->filename = filename;
		info->fileSize = fileSize;
		info->modificationTime = modTime;
		info->creationTime = creationTime;
		info->isDirectory = isDir;
		info->isReadOnly = isReadOnly;

		const ScopedLock sl (fileListLock);

		for (int i = files.size(); --i >= 0;)
			if (files.getUnchecked(i)->filename == info->filename)
				return false;

		files.addSorted (*this, info.release());
		return true;
	}

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DirectoryContentsList.cpp ***/


/*** Start of inlined file: juce_FileBrowserComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

FileBrowserComponent::FileBrowserComponent (int flags_,
											const File& initialFileOrDirectory,
											const FileFilter* fileFilter_,
											FilePreviewComponent* previewComp_)
   : FileFilter (String::empty),
	 fileFilter (fileFilter_),
	 flags (flags_),
	 previewComp (previewComp_),
	 thread ("Juce FileBrowser")
{
	// You need to specify one or other of the open/save flags..
	jassert ((flags & (saveMode | openMode)) != 0);
	jassert ((flags & (saveMode | openMode)) != (saveMode | openMode));

	// You need to specify at least one of these flags..
	jassert ((flags & (canSelectFiles | canSelectDirectories)) != 0);

	String filename;

	if (initialFileOrDirectory == File::nonexistent)
	{
		currentRoot = File::getCurrentWorkingDirectory();
	}
	else if (initialFileOrDirectory.isDirectory())
	{
		currentRoot = initialFileOrDirectory;
	}
	else
	{
		chosenFiles.add (initialFileOrDirectory);
		currentRoot = initialFileOrDirectory.getParentDirectory();
		filename = initialFileOrDirectory.getFileName();
	}

	fileList = new DirectoryContentsList (this, thread);

	if ((flags & useTreeView) != 0)
	{
		FileTreeComponent* const tree = new FileTreeComponent (*fileList);

		if ((flags & canSelectMultipleItems) != 0)
			tree->setMultiSelectEnabled (true);

		addAndMakeVisible (tree);
		fileListComponent = tree;
	}
	else
	{
		FileListComponent* const list = new FileListComponent (*fileList);
		list->setOutlineThickness (1);

		if ((flags & canSelectMultipleItems) != 0)
			list->setMultipleSelectionEnabled (true);

		addAndMakeVisible (list);
		fileListComponent = list;
	}

	fileListComponent->addListener (this);

	addAndMakeVisible (currentPathBox = new ComboBox ("path"));
	currentPathBox->setEditableText (true);

	StringArray rootNames, rootPaths;
	const BigInteger separators (getRoots (rootNames, rootPaths));

	for (int i = 0; i < rootNames.size(); ++i)
	{
		if (separators [i])
			currentPathBox->addSeparator();

		currentPathBox->addItem (rootNames[i], i + 1);
	}

	currentPathBox->addSeparator();
	currentPathBox->addListener (this);

	addAndMakeVisible (filenameBox = new TextEditor());
	filenameBox->setMultiLine (false);
	filenameBox->setSelectAllWhenFocused (true);
	filenameBox->setText (filename, false);

	filenameBox->addListener (this);
	filenameBox->setReadOnly ((flags & (filenameBoxIsReadOnly | canSelectMultipleItems)) != 0);

	Label* label = new Label ("f", TRANS("file:"));
	addAndMakeVisible (label);
	label->attachToComponent (filenameBox, true);

	addAndMakeVisible (goUpButton = getLookAndFeel().createFileBrowserGoUpButton());

	goUpButton->addButtonListener (this);
	goUpButton->setTooltip (TRANS ("go up to parent directory"));

	if (previewComp != 0)
		addAndMakeVisible (previewComp);

	setRoot (currentRoot);

	thread.startThread (4);
}

FileBrowserComponent::~FileBrowserComponent()
{
	if (previewComp != 0)
		removeChildComponent (previewComp);

	deleteAllChildren();
	fileList = 0;
	thread.stopThread (10000);
}

void FileBrowserComponent::addListener (FileBrowserListener* const newListener)
{
	listeners.add (newListener);
}

void FileBrowserComponent::removeListener (FileBrowserListener* const listener)
{
	listeners.remove (listener);
}

bool FileBrowserComponent::isSaveMode() const throw()
{
	return (flags & saveMode) != 0;
}

int FileBrowserComponent::getNumSelectedFiles() const throw()
{
	if (chosenFiles.size() == 0 && currentFileIsValid())
		return 1;

	return chosenFiles.size();
}

const File FileBrowserComponent::getSelectedFile (int index) const throw()
{
	if (! filenameBox->isReadOnly())
		return currentRoot.getChildFile (filenameBox->getText());
	else
		return chosenFiles[index];
}

bool FileBrowserComponent::currentFileIsValid() const
{
	if (isSaveMode())
		return ! getSelectedFile (0).isDirectory();
	else
		return getSelectedFile (0).exists();
}

const File FileBrowserComponent::getHighlightedFile() const throw()
{
	return fileListComponent->getSelectedFile (0);
}

bool FileBrowserComponent::isFileSuitable (const File& file) const
{
	return (flags & canSelectFiles) != 0 ? (fileFilter == 0 || fileFilter->isFileSuitable (file))
										 : false;
}

bool FileBrowserComponent::isDirectorySuitable (const File&) const
{
	return true;
}

bool FileBrowserComponent::isFileOrDirSuitable (const File& f) const
{
	if (f.isDirectory())
		return (flags & canSelectDirectories) != 0 && (fileFilter == 0 || fileFilter->isDirectorySuitable (f));

	return (flags & canSelectFiles) != 0 && f.exists()
			 && (fileFilter == 0 || fileFilter->isFileSuitable (f));
}

const File FileBrowserComponent::getRoot() const
{
	return currentRoot;
}

void FileBrowserComponent::setRoot (const File& newRootDirectory)
{
	if (currentRoot != newRootDirectory)
	{
		fileListComponent->scrollToTop();

		String path (newRootDirectory.getFullPathName());

		if (path.isEmpty())
			path += File::separator;

		StringArray rootNames, rootPaths;
		getRoots (rootNames, rootPaths);

		if (! rootPaths.contains (path, true))
		{
			bool alreadyListed = false;

			for (int i = currentPathBox->getNumItems(); --i >= 0;)
			{
				if (currentPathBox->getItemText (i).equalsIgnoreCase (path))
				{
					alreadyListed = true;
					break;
				}
			}

			if (! alreadyListed)
				currentPathBox->addItem (path, currentPathBox->getNumItems() + 2);
		}
	}

	currentRoot = newRootDirectory;
	fileList->setDirectory (currentRoot, true, true);

	String currentRootName (currentRoot.getFullPathName());
	if (currentRootName.isEmpty())
		currentRootName += File::separator;

	currentPathBox->setText (currentRootName, true);

	goUpButton->setEnabled (currentRoot.getParentDirectory().isDirectory()
							 && currentRoot.getParentDirectory() != currentRoot);
}

void FileBrowserComponent::goUp()
{
	setRoot (getRoot().getParentDirectory());
}

void FileBrowserComponent::refresh()
{
	fileList->refresh();
}

const String FileBrowserComponent::getActionVerb() const
{
	return isSaveMode() ? TRANS("Save") : TRANS("Open");
}

FilePreviewComponent* FileBrowserComponent::getPreviewComponent() const throw()
{
	return previewComp;
}

void FileBrowserComponent::resized()
{
	getLookAndFeel()
		.layoutFileBrowserComponent (*this, fileListComponent,
									 previewComp, currentPathBox,
									 filenameBox, goUpButton);
}

void FileBrowserComponent::sendListenerChangeMessage()
{
	Component::BailOutChecker checker (this);

	if (previewComp != 0)
		previewComp->selectedFileChanged (getSelectedFile (0));

	// You shouldn't delete the browser when the file gets changed!
	jassert (! checker.shouldBailOut());

	listeners.callChecked (checker, &FileBrowserListener::selectionChanged);
}

void FileBrowserComponent::selectionChanged()
{
	StringArray newFilenames;
	bool resetChosenFiles = true;

	for (int i = 0; i < fileListComponent->getNumSelectedFiles(); ++i)
	{
		const File f (fileListComponent->getSelectedFile (i));

		if (isFileOrDirSuitable (f))
		{
			if (resetChosenFiles)
			{
				chosenFiles.clear();
				resetChosenFiles = false;
			}

			chosenFiles.add (f);
			newFilenames.add (f.getRelativePathFrom (getRoot()));
		}
	}

	if (newFilenames.size() > 0)
		filenameBox->setText (newFilenames.joinIntoString (", "), false);

	sendListenerChangeMessage();
}

void FileBrowserComponent::fileClicked (const File& f, const MouseEvent& e)
{
	Component::BailOutChecker checker (this);
	listeners.callChecked (checker, &FileBrowserListener::fileClicked, f, e);
}

void FileBrowserComponent::fileDoubleClicked (const File& f)
{
	if (f.isDirectory())
	{
		setRoot (f);
	}
	else
	{
		Component::BailOutChecker checker (this);
		listeners.callChecked (checker, &FileBrowserListener::fileDoubleClicked, f);
	}
}

bool FileBrowserComponent::keyPressed (const KeyPress& key)
{
#if JUCE_LINUX || JUCE_WINDOWS
	if (key.getModifiers().isCommandDown()
		 && (key.getKeyCode() == 'H' || key.getKeyCode() == 'h'))
	{
		fileList->setIgnoresHiddenFiles (! fileList->ignoresHiddenFiles());
		fileList->refresh();
		return true;
	}
#endif

	return false;
}

void FileBrowserComponent::textEditorTextChanged (TextEditor&)
{
	sendListenerChangeMessage();
}

void FileBrowserComponent::textEditorReturnKeyPressed (TextEditor&)
{
	if (filenameBox->getText().containsChar (File::separator))
	{
		const File f (currentRoot.getChildFile (filenameBox->getText()));

		if (f.isDirectory())
		{
			setRoot (f);
			chosenFiles.clear();
			filenameBox->setText (String::empty);
		}
		else
		{
			setRoot (f.getParentDirectory());
			chosenFiles.clear();
			chosenFiles.add (f);
			filenameBox->setText (f.getFileName());
		}
	}
	else
	{
		fileDoubleClicked (getSelectedFile (0));
	}
}

void FileBrowserComponent::textEditorEscapeKeyPressed (TextEditor&)
{
}

void FileBrowserComponent::textEditorFocusLost (TextEditor&)
{
	if (! isSaveMode())
		selectionChanged();
}

void FileBrowserComponent::buttonClicked (Button*)
{
	goUp();
}

void FileBrowserComponent::comboBoxChanged (ComboBox*)
{
	const String newText (currentPathBox->getText().trim().unquoted());

	if (newText.isNotEmpty())
	{
		const int index = currentPathBox->getSelectedId() - 1;

		StringArray rootNames, rootPaths;
		getRoots (rootNames, rootPaths);

		if (rootPaths [index].isNotEmpty())
		{
			setRoot (File (rootPaths [index]));
		}
		else
		{
			File f (newText);

			for (;;)
			{
				if (f.isDirectory())
				{
					setRoot (f);
					break;
				}

				if (f.getParentDirectory() == f)
					break;

				f = f.getParentDirectory();
			}
		}
	}
}

const BigInteger FileBrowserComponent::getRoots (StringArray& rootNames, StringArray& rootPaths)
{
	BigInteger separators;

#if JUCE_WINDOWS
	Array<File> roots;
	File::findFileSystemRoots (roots);
	rootPaths.clear();

	for (int i = 0; i < roots.size(); ++i)
	{
		const File& drive = roots.getReference(i);

		String name (drive.getFullPathName());
		rootPaths.add (name);

		if (drive.isOnHardDisk())
		{
			String volume (drive.getVolumeLabel());

			if (volume.isEmpty())
				volume = TRANS("Hard Drive");

			name << " [" << drive.getVolumeLabel() << ']';
		}
		else if (drive.isOnCDRomDrive())
		{
			name << TRANS(" [CD/DVD drive]");
		}

		rootNames.add (name);
	}

	separators.setBit (rootPaths.size());

	rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
	rootNames.add ("Documents");
	rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
	rootNames.add ("Desktop");
#endif

#if JUCE_MAC
	rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
	rootNames.add ("Home folder");
	rootPaths.add (File::getSpecialLocation (File::userDocumentsDirectory).getFullPathName());
	rootNames.add ("Documents");
	rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
	rootNames.add ("Desktop");

	separators.setBit (rootPaths.size());

	Array <File> volumes;
	File vol ("/Volumes");
	vol.findChildFiles (volumes, File::findDirectories, false);

	for (int i = 0; i < volumes.size(); ++i)
	{
		const File& volume = volumes.getReference(i);

		if (volume.isDirectory() && ! volume.getFileName().startsWithChar ('.'))
		{
			rootPaths.add (volume.getFullPathName());
			rootNames.add (volume.getFileName());
		}
	}
#endif

#if JUCE_LINUX
	rootPaths.add ("/");
	rootNames.add ("/");
	rootPaths.add (File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
	rootNames.add ("Home folder");
	rootPaths.add (File::getSpecialLocation (File::userDesktopDirectory).getFullPathName());
	rootNames.add ("Desktop");
#endif

	return separators;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileBrowserComponent.cpp ***/


/*** Start of inlined file: juce_FileChooser.cpp ***/
BEGIN_JUCE_NAMESPACE

FileChooser::FileChooser (const String& chooserBoxTitle,
						  const File& currentFileOrDirectory,
						  const String& fileFilters,
						  const bool useNativeDialogBox_)
	: title (chooserBoxTitle),
	  filters (fileFilters),
	  startingFile (currentFileOrDirectory),
	  useNativeDialogBox (useNativeDialogBox_)
{
#if JUCE_LINUX
	useNativeDialogBox = false;
#endif

	if (! fileFilters.containsNonWhitespaceChars())
		filters = "*";
}

FileChooser::~FileChooser()
{
}

bool FileChooser::browseForFileToOpen (FilePreviewComponent* previewComponent)
{
	return showDialog (false, true, false, false, false, previewComponent);
}

bool FileChooser::browseForMultipleFilesToOpen (FilePreviewComponent* previewComponent)
{
	return showDialog (false, true, false, false, true, previewComponent);
}

bool FileChooser::browseForMultipleFilesOrDirectories (FilePreviewComponent* previewComponent)
{
	return showDialog (true, true, false, false, true, previewComponent);
}

bool FileChooser::browseForFileToSave (const bool warnAboutOverwritingExistingFiles)
{
	return showDialog (false, true, true, warnAboutOverwritingExistingFiles, false, 0);
}

bool FileChooser::browseForDirectory()
{
	return showDialog (true, false, false, false, false, 0);
}

const File FileChooser::getResult() const
{
	// if you've used a multiple-file select, you should use the getResults() method
	// to retrieve all the files that were chosen.
	jassert (results.size() <= 1);

	return results.getFirst();
}

const Array<File>& FileChooser::getResults() const
{
	return results;
}

bool FileChooser::showDialog (const bool selectsDirectories,
							  const bool selectsFiles,
							  const bool isSave,
							  const bool warnAboutOverwritingExistingFiles,
							  const bool selectMultipleFiles,
							  FilePreviewComponent* const previewComponent)
{
	Component::SafePointer<Component> previouslyFocused (Component::getCurrentlyFocusedComponent());

	results.clear();

	// the preview component needs to be the right size before you pass it in here..
	jassert (previewComponent == 0 || (previewComponent->getWidth() > 10
										&& previewComponent->getHeight() > 10));

#if JUCE_WINDOWS
	if (useNativeDialogBox && ! (selectsFiles && selectsDirectories))
#elif JUCE_MAC
	if (useNativeDialogBox && (previewComponent == 0))
#else
	if (false)
#endif
	{
		showPlatformDialog (results, title, startingFile, filters,
							selectsDirectories, selectsFiles, isSave,
							warnAboutOverwritingExistingFiles,
							selectMultipleFiles,
							previewComponent);
	}
	else
	{
		WildcardFileFilter wildcard (selectsFiles ? filters : String::empty,
									 selectsDirectories ? "*" : String::empty,
									 String::empty);

		int flags = isSave ? FileBrowserComponent::saveMode
						   : FileBrowserComponent::openMode;

		if (selectsFiles)
			flags |= FileBrowserComponent::canSelectFiles;

		if (selectsDirectories)
			flags |= FileBrowserComponent::canSelectDirectories;

		if (selectMultipleFiles)
			flags |= FileBrowserComponent::canSelectMultipleItems;

		FileBrowserComponent browserComponent (flags, startingFile, &wildcard, previewComponent);

		FileChooserDialogBox box (title, String::empty,
								  browserComponent,
								  warnAboutOverwritingExistingFiles,
								  browserComponent.findColour (AlertWindow::backgroundColourId));

		if (box.show())
		{
			for (int i = 0; i < browserComponent.getNumSelectedFiles(); ++i)
				results.add (browserComponent.getSelectedFile (i));
		}
	}

	if (previouslyFocused != 0)
		previouslyFocused->grabKeyboardFocus();

	return results.size() > 0;
}

FilePreviewComponent::FilePreviewComponent()
{
}

FilePreviewComponent::~FilePreviewComponent()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileChooser.cpp ***/


/*** Start of inlined file: juce_FileChooserDialogBox.cpp ***/
BEGIN_JUCE_NAMESPACE

FileChooserDialogBox::FileChooserDialogBox (const String& name,
											const String& instructions,
											FileBrowserComponent& chooserComponent,
											const bool warnAboutOverwritingExistingFiles_,
											const Colour& backgroundColour)
	: ResizableWindow (name, backgroundColour, true),
	  warnAboutOverwritingExistingFiles (warnAboutOverwritingExistingFiles_)
{
	content = new ContentComponent();
	content->setName (name);
	content->instructions = instructions;
	content->chooserComponent = &chooserComponent;

	content->addAndMakeVisible (&chooserComponent);

	content->okButton = new TextButton (chooserComponent.getActionVerb());
	content->addAndMakeVisible (content->okButton);
	content->okButton->addButtonListener (this);
	content->okButton->setEnabled (chooserComponent.currentFileIsValid());
	content->okButton->addShortcut (KeyPress (KeyPress::returnKey, 0, 0));

	content->cancelButton = new TextButton (TRANS("Cancel"));
	content->addAndMakeVisible (content->cancelButton);
	content->cancelButton->addButtonListener (this);
	content->cancelButton->addShortcut (KeyPress (KeyPress::escapeKey, 0, 0));

	setContentComponent (content);

	setResizable (true, true);
	setResizeLimits (300, 300, 1200, 1000);

	content->chooserComponent->addListener (this);
}

FileChooserDialogBox::~FileChooserDialogBox()
{
	content->chooserComponent->removeListener (this);
}

bool FileChooserDialogBox::show (int w, int h)
{
	if (w <= 0)
	{
		Component* const previewComp = content->chooserComponent->getPreviewComponent();
		if (previewComp != 0)
			w = 400 + previewComp->getWidth();
		else
			w = 600;
	}

	if (h <= 0)
		h = 500;

	centreWithSize (w, h);

	const bool ok = (runModalLoop() != 0);
	setVisible (false);
	return ok;
}

void FileChooserDialogBox::buttonClicked (Button* button)
{
	if (button == content->okButton)
	{
		if (warnAboutOverwritingExistingFiles
			 && content->chooserComponent->isSaveMode()
			 && content->chooserComponent->getSelectedFile(0).exists())
		{
			if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
												TRANS("File already exists"),
												TRANS("There's already a file called:")
												  + "\n\n" + content->chooserComponent->getSelectedFile(0).getFullPathName()
												  + "\n\n" + TRANS("Are you sure you want to overwrite it?"),
												TRANS("overwrite"),
												TRANS("cancel")))
			{
				return;
			}
		}

		exitModalState (1);
	}
	else if (button == content->cancelButton)
		closeButtonPressed();
}

void FileChooserDialogBox::closeButtonPressed()
{
	setVisible (false);
}

void FileChooserDialogBox::selectionChanged()
{
	content->okButton->setEnabled (content->chooserComponent->currentFileIsValid());
}

void FileChooserDialogBox::fileClicked (const File&, const MouseEvent&)
{
}

void FileChooserDialogBox::fileDoubleClicked (const File&)
{
	selectionChanged();
	content->okButton->triggerClick();
}

FileChooserDialogBox::ContentComponent::ContentComponent()
{
	setInterceptsMouseClicks (false, true);
}

FileChooserDialogBox::ContentComponent::~ContentComponent()
{
	delete okButton;
	delete cancelButton;
}

void FileChooserDialogBox::ContentComponent::paint (Graphics& g)
{
	g.setColour (getLookAndFeel().findColour (FileChooserDialogBox::titleTextColourId));
	text.draw (g);
}

void FileChooserDialogBox::ContentComponent::resized()
{
	getLookAndFeel().createFileChooserHeaderText (getName(), instructions, text, getWidth());

	const Rectangle<float> bb (text.getBoundingBox (0, text.getNumGlyphs(), false));

	const int y = roundToInt (bb.getBottom()) + 10;
	const int buttonHeight = 26;
	const int buttonY = getHeight() - buttonHeight - 8;

	chooserComponent->setBounds (0, y, getWidth(), buttonY - y - 20);

	okButton->setBounds (proportionOfWidth (0.25f), buttonY,
						 proportionOfWidth (0.2f), buttonHeight);

	cancelButton->setBounds (proportionOfWidth (0.55f), buttonY,
							 proportionOfWidth (0.2f), buttonHeight);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileChooserDialogBox.cpp ***/


/*** Start of inlined file: juce_FileFilter.cpp ***/
BEGIN_JUCE_NAMESPACE

FileFilter::FileFilter (const String& filterDescription)
	: description (filterDescription)
{
}

FileFilter::~FileFilter()
{
}

const String& FileFilter::getDescription() const throw()
{
	return description;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileFilter.cpp ***/


/*** Start of inlined file: juce_FileListComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

Image* juce_createIconForFile (const File& file);

FileListComponent::FileListComponent (DirectoryContentsList& listToShow)
	: ListBox (String::empty, 0),
	  DirectoryContentsDisplayComponent (listToShow)
{
	setModel (this);
	fileList.addChangeListener (this);
}

FileListComponent::~FileListComponent()
{
	fileList.removeChangeListener (this);
	deleteAllChildren();
}

int FileListComponent::getNumSelectedFiles() const
{
	return getNumSelectedRows();
}

const File FileListComponent::getSelectedFile (int index) const
{
	return fileList.getFile (getSelectedRow (index));
}

void FileListComponent::scrollToTop()
{
	getVerticalScrollBar()->setCurrentRangeStart (0);
}

void FileListComponent::changeListenerCallback (void*)
{
	updateContent();

	if (lastDirectory != fileList.getDirectory())
	{
		lastDirectory = fileList.getDirectory();
		deselectAllRows();
	}
}

class FileListItemComponent  : public Component,
							   public TimeSliceClient,
							   public AsyncUpdater
{
public:

	FileListItemComponent (FileListComponent& owner_, TimeSliceThread& thread_)
		: owner (owner_), thread (thread_),
		  highlighted (false), index (0), icon (0)
	{
	}

	~FileListItemComponent()
	{
		thread.removeTimeSliceClient (this);

		clearIcon();
	}

	void paint (Graphics& g)
	{
		getLookAndFeel().drawFileBrowserRow (g, getWidth(), getHeight(),
											 file.getFileName(),
											 icon,
											 fileSize, modTime,
											 isDirectory, highlighted,
											 index);
	}

	void mouseDown (const MouseEvent& e)
	{
		owner.selectRowsBasedOnModifierKeys (index, e.mods);
		owner.sendMouseClickMessage (file, e);
	}

	void mouseDoubleClick (const MouseEvent&)
	{
		owner.sendDoubleClickMessage (file);
	}

	void update (const File& root,
				 const DirectoryContentsList::FileInfo* const fileInfo,
				 const int index_,
				 const bool highlighted_)
	{
		thread.removeTimeSliceClient (this);

		if (highlighted_ != highlighted
			 || index_ != index)
		{
			index = index_;
			highlighted = highlighted_;
			repaint();
		}

		File newFile;
		String newFileSize;
		String newModTime;

		if (fileInfo != 0)
		{
			newFile = root.getChildFile (fileInfo->filename);
			newFileSize = File::descriptionOfSizeInBytes (fileInfo->fileSize);
			newModTime = fileInfo->modificationTime.formatted (T("%d %b '%y %H:%M"));
		}

		if (newFile != file
			 || fileSize != newFileSize
			 || modTime != newModTime)
		{
			file = newFile;
			fileSize = newFileSize;
			modTime = newModTime;

			isDirectory = fileInfo != 0 && fileInfo->isDirectory;
			repaint();

			clearIcon();
		}

		if (file != File::nonexistent
			&& icon == 0 && ! isDirectory)
		{
			updateIcon (true);

			if (icon == 0)
				thread.addTimeSliceClient (this);
		}
	}

	bool useTimeSlice()
	{
		updateIcon (false);
		return false;
	}

	void handleAsyncUpdate()
	{
		repaint();
	}

	juce_UseDebuggingNewOperator

private:
	FileListComponent& owner;
	TimeSliceThread& thread;
	bool highlighted;
	int index;
	File file;
	String fileSize;
	String modTime;
	Image* icon;
	bool isDirectory;

	void clearIcon()
	{
		ImageCache::release (icon);
		icon = 0;
	}

	void updateIcon (const bool onlyUpdateIfCached)
	{
		if (icon == 0)
		{
			const int hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
			Image* im = ImageCache::getFromHashCode (hashCode);

			if (im == 0 && ! onlyUpdateIfCached)
			{
				im = juce_createIconForFile (file);

				if (im != 0)
					ImageCache::addImageToCache (im, hashCode);
			}

			if (im != 0)
			{
				icon = im;
				triggerAsyncUpdate();
			}
		}
	}
};

int FileListComponent::getNumRows()
{
	return fileList.getNumFiles();
}

void FileListComponent::paintListBoxItem (int, Graphics&, int, int, bool)
{
}

Component* FileListComponent::refreshComponentForRow (int row, bool isSelected, Component* existingComponentToUpdate)
{
	FileListItemComponent* comp = dynamic_cast <FileListItemComponent*> (existingComponentToUpdate);

	if (comp == 0)
	{
		delete existingComponentToUpdate;
		comp = new FileListItemComponent (*this, fileList.getTimeSliceThread());
	}

	DirectoryContentsList::FileInfo fileInfo;

	if (fileList.getFileInfo (row, fileInfo))
		comp->update (fileList.getDirectory(), &fileInfo, row, isSelected);
	else
		comp->update (fileList.getDirectory(), 0, row, isSelected);

	return comp;
}

void FileListComponent::selectedRowsChanged (int /*lastRowSelected*/)
{
	sendSelectionChangeMessage();
}

void FileListComponent::deleteKeyPressed (int /*currentSelectedRow*/)
{
}

void FileListComponent::returnKeyPressed (int currentSelectedRow)
{
	sendDoubleClickMessage (fileList.getFile (currentSelectedRow));
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileListComponent.cpp ***/


/*** Start of inlined file: juce_FilenameComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

FilenameComponent::FilenameComponent (const String& name,
									  const File& currentFile,
									  const bool canEditFilename,
									  const bool isDirectory,
									  const bool isForSaving,
									  const String& fileBrowserWildcard,
									  const String& enforcedSuffix_,
									  const String& textWhenNothingSelected)
	: Component (name),
	  maxRecentFiles (30),
	  isDir (isDirectory),
	  isSaving (isForSaving),
	  isFileDragOver (false),
	  wildcard (fileBrowserWildcard),
	  enforcedSuffix (enforcedSuffix_)
{
	addAndMakeVisible (filenameBox = new ComboBox ("fn"));
	filenameBox->setEditableText (canEditFilename);
	filenameBox->addListener (this);
	filenameBox->setTextWhenNothingSelected (textWhenNothingSelected);
	filenameBox->setTextWhenNoChoicesAvailable (TRANS("(no recently seleced files)"));

	browseButton = 0;
	setBrowseButtonText ("...");

	setCurrentFile (currentFile, true);
}

FilenameComponent::~FilenameComponent()
{
	deleteAllChildren();
}

void FilenameComponent::paintOverChildren (Graphics& g)
{
	if (isFileDragOver)
	{
		g.setColour (Colours::red.withAlpha (0.2f));
		g.drawRect (0, 0, getWidth(), getHeight(), 3);
	}
}

void FilenameComponent::resized()
{
	getLookAndFeel().layoutFilenameComponent (*this, filenameBox, browseButton);
}

void FilenameComponent::setBrowseButtonText (const String& newBrowseButtonText)
{
	browseButtonText = newBrowseButtonText;
	lookAndFeelChanged();
}

void FilenameComponent::lookAndFeelChanged()
{
	deleteAndZero (browseButton);

	addAndMakeVisible (browseButton = getLookAndFeel().createFilenameComponentBrowseButton (browseButtonText));
	browseButton->setConnectedEdges (Button::ConnectedOnLeft);
	resized();

	browseButton->addButtonListener (this);
}

void FilenameComponent::setTooltip (const String& newTooltip)
{
	SettableTooltipClient::setTooltip (newTooltip);
	filenameBox->setTooltip (newTooltip);
}

void FilenameComponent::setDefaultBrowseTarget (const File& newDefaultDirectory)
{
	defaultBrowseFile = newDefaultDirectory;
}

void FilenameComponent::buttonClicked (Button*)
{
	FileChooser fc (TRANS("Choose a new file"),
					getCurrentFile() == File::nonexistent ? defaultBrowseFile
														  : getCurrentFile(),
					wildcard);

	if (isDir ? fc.browseForDirectory()
			  : (isSaving ? fc.browseForFileToSave (false)
						  : fc.browseForFileToOpen()))
	{
		setCurrentFile (fc.getResult(), true);
	}
}

void FilenameComponent::comboBoxChanged (ComboBox*)
{
	setCurrentFile (getCurrentFile(), true);
}

bool FilenameComponent::isInterestedInFileDrag (const StringArray&)
{
	return true;
}

void FilenameComponent::filesDropped (const StringArray& filenames, int, int)
{
	isFileDragOver = false;
	repaint();

	const File f (filenames[0]);

	if (f.exists() && (f.isDirectory() == isDir))
		setCurrentFile (f, true);
}

void FilenameComponent::fileDragEnter (const StringArray&, int, int)
{
	isFileDragOver = true;
	repaint();
}

void FilenameComponent::fileDragExit (const StringArray&)
{
	isFileDragOver = false;
	repaint();
}

const File FilenameComponent::getCurrentFile() const
{
	File f (filenameBox->getText());

	if (enforcedSuffix.isNotEmpty())
		f = f.withFileExtension (enforcedSuffix);

	return f;
}

void FilenameComponent::setCurrentFile (File newFile,
										const bool addToRecentlyUsedList,
										const bool sendChangeNotification)
{
	if (enforcedSuffix.isNotEmpty())
		newFile = newFile.withFileExtension (enforcedSuffix);

	if (newFile.getFullPathName() != lastFilename)
	{
		lastFilename = newFile.getFullPathName();

		if (addToRecentlyUsedList)
			addRecentlyUsedFile (newFile);

		filenameBox->setText (lastFilename, true);

		if (sendChangeNotification)
			triggerAsyncUpdate();
	}
}

void FilenameComponent::setFilenameIsEditable (const bool shouldBeEditable)
{
	filenameBox->setEditableText (shouldBeEditable);
}

const StringArray FilenameComponent::getRecentlyUsedFilenames() const
{
	StringArray names;

	for (int i = 0; i < filenameBox->getNumItems(); ++i)
		names.add (filenameBox->getItemText (i));

	return names;
}

void FilenameComponent::setRecentlyUsedFilenames (const StringArray& filenames)
{
	if (filenames != getRecentlyUsedFilenames())
	{
		filenameBox->clear();

		for (int i = 0; i < jmin (filenames.size(), maxRecentFiles); ++i)
			filenameBox->addItem (filenames[i], i + 1);
	}
}

void FilenameComponent::setMaxNumberOfRecentFiles (const int newMaximum)
{
	maxRecentFiles = jmax (1, newMaximum);

	setRecentlyUsedFilenames (getRecentlyUsedFilenames());
}

void FilenameComponent::addRecentlyUsedFile (const File& file)
{
	StringArray files (getRecentlyUsedFilenames());

	if (file.getFullPathName().isNotEmpty())
	{
		files.removeString (file.getFullPathName(), true);
		files.insert (0, file.getFullPathName());

		setRecentlyUsedFilenames (files);
	}
}

void FilenameComponent::addListener (FilenameComponentListener* const listener)
{
	listeners.add (listener);
}

void FilenameComponent::removeListener (FilenameComponentListener* const listener)
{
	listeners.remove (listener);
}

void FilenameComponent::handleAsyncUpdate()
{
	Component::BailOutChecker checker (this);
	listeners.callChecked (checker, &FilenameComponentListener::filenameComponentChanged, this);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FilenameComponent.cpp ***/


/*** Start of inlined file: juce_FileSearchPathListComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

FileSearchPathListComponent::FileSearchPathListComponent()
{
	addAndMakeVisible (listBox = new ListBox (String::empty, this));
	listBox->setColour (ListBox::backgroundColourId, Colours::black.withAlpha (0.02f));
	listBox->setColour (ListBox::outlineColourId, Colours::black.withAlpha (0.1f));
	listBox->setOutlineThickness (1);

	addAndMakeVisible (addButton = new TextButton ("+"));
	addButton->addButtonListener (this);
	addButton->setConnectedEdges (Button::ConnectedOnLeft | Button::ConnectedOnRight | Button::ConnectedOnBottom | Button::ConnectedOnTop);

	addAndMakeVisible (removeButton = new TextButton ("-"));
	removeButton->addButtonListener (this);
	removeButton->setConnectedEdges (Button::ConnectedOnLeft | Button::ConnectedOnRight | Button::ConnectedOnBottom | Button::ConnectedOnTop);

	addAndMakeVisible (changeButton = new TextButton (TRANS("change...")));
	changeButton->addButtonListener (this);

	addAndMakeVisible (upButton = new DrawableButton (String::empty, DrawableButton::ImageOnButtonBackground));
	upButton->addButtonListener (this);

	{
		Path arrowPath;
		arrowPath.addArrow (50.0f, 100.0f, 50.0f, 0.0f, 40.0f, 100.0f, 50.0f);
		DrawablePath arrowImage;
		arrowImage.setFill (Colours::black.withAlpha (0.4f));
		arrowImage.setPath (arrowPath);

		upButton->setImages (&arrowImage);
	}

	addAndMakeVisible (downButton = new DrawableButton (String::empty, DrawableButton::ImageOnButtonBackground));
	downButton->addButtonListener (this);

	{
		Path arrowPath;
		arrowPath.addArrow (50.0f, 0.0f, 50.0f, 100.0f, 40.0f, 100.0f, 50.0f);
		DrawablePath arrowImage;
		arrowImage.setFill (Colours::black.withAlpha (0.4f));
		arrowImage.setPath (arrowPath);

		downButton->setImages (&arrowImage);
	}

	updateButtons();
}

FileSearchPathListComponent::~FileSearchPathListComponent()
{
	deleteAllChildren();
}

void FileSearchPathListComponent::updateButtons()
{
	const bool anythingSelected = listBox->getNumSelectedRows() > 0;

	removeButton->setEnabled (anythingSelected);
	changeButton->setEnabled (anythingSelected);
	upButton->setEnabled (anythingSelected);
	downButton->setEnabled (anythingSelected);
}

void FileSearchPathListComponent::changed()
{
	listBox->updateContent();
	listBox->repaint();
	updateButtons();
}

void FileSearchPathListComponent::setPath (const FileSearchPath& newPath)
{
	if (newPath.toString() != path.toString())
	{
		path = newPath;
		changed();
	}
}

void FileSearchPathListComponent::setDefaultBrowseTarget (const File& newDefaultDirectory)
{
	defaultBrowseTarget = newDefaultDirectory;
}

int FileSearchPathListComponent::getNumRows()
{
	return path.getNumPaths();
}

void FileSearchPathListComponent::paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected)
{
	if (rowIsSelected)
		g.fillAll (findColour (TextEditor::highlightColourId));

	g.setColour (findColour (ListBox::textColourId));
	Font f (height * 0.7f);
	f.setHorizontalScale (0.9f);
	g.setFont (f);

	g.drawText (path [rowNumber].getFullPathName(),
				4, 0, width - 6, height,
				Justification::centredLeft, true);
}

void FileSearchPathListComponent::deleteKeyPressed (int row)
{
	if (((unsigned int) row) < (unsigned int) path.getNumPaths())
	{
		path.remove (row);
		changed();
	}
}

void FileSearchPathListComponent::returnKeyPressed (int row)
{
	FileChooser chooser (TRANS("Change folder..."), path [row], "*");

	if (chooser.browseForDirectory())
	{
		path.remove (row);
		path.add (chooser.getResult(), row);
		changed();
	}
}

void FileSearchPathListComponent::listBoxItemDoubleClicked (int row, const MouseEvent&)
{
	returnKeyPressed (row);
}

void FileSearchPathListComponent::selectedRowsChanged (int)
{
	updateButtons();
}

void FileSearchPathListComponent::paint (Graphics& g)
{
	g.fillAll (findColour (backgroundColourId));
}

void FileSearchPathListComponent::resized()
{
	const int buttonH = 22;
	const int buttonY = getHeight() - buttonH - 4;
	listBox->setBounds (2, 2, getWidth() - 4, buttonY - 5);

	addButton->setBounds (2, buttonY, buttonH, buttonH);
	removeButton->setBounds (addButton->getRight(), buttonY, buttonH, buttonH);

	changeButton->changeWidthToFitText (buttonH);
	downButton->setSize (buttonH * 2, buttonH);
	upButton->setSize (buttonH * 2, buttonH);

	downButton->setTopRightPosition (getWidth() - 2, buttonY);
	upButton->setTopRightPosition (downButton->getX() - 4, buttonY);
	changeButton->setTopRightPosition (upButton->getX() - 8, buttonY);
}

bool FileSearchPathListComponent::isInterestedInFileDrag (const StringArray&)
{
	return true;
}

void FileSearchPathListComponent::filesDropped (const StringArray& filenames, int, int mouseY)
{
	for (int i = filenames.size(); --i >= 0;)
	{
		const File f (filenames[i]);

		if (f.isDirectory())
		{
			const int row = listBox->getRowContainingPosition (0, mouseY - listBox->getY());
			path.add (f, row);
			changed();
		}
	}
}

void FileSearchPathListComponent::buttonClicked (Button* button)
{
	const int currentRow = listBox->getSelectedRow();

	if (button == removeButton)
	{
		deleteKeyPressed (currentRow);
	}
	else if (button == addButton)
	{
		File start (defaultBrowseTarget);

		if (start == File::nonexistent)
			start = path [0];

		if (start == File::nonexistent)
			start = File::getCurrentWorkingDirectory();

		FileChooser chooser (TRANS("Add a folder..."), start, "*");

		if (chooser.browseForDirectory())
		{
			path.add (chooser.getResult(), currentRow);
		}
	}
	else if (button == changeButton)
	{
		returnKeyPressed (currentRow);
	}
	else if (button == upButton)
	{
		if (currentRow > 0 && currentRow < path.getNumPaths())
		{
			const File f (path[currentRow]);
			path.remove (currentRow);
			path.add (f, currentRow - 1);
			listBox->selectRow (currentRow - 1);
		}
	}
	else if (button == downButton)
	{
		if (currentRow >= 0 && currentRow < path.getNumPaths() - 1)
		{
			const File f (path[currentRow]);
			path.remove (currentRow);
			path.add (f, currentRow + 1);
			listBox->selectRow (currentRow + 1);
		}
	}

	changed();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileSearchPathListComponent.cpp ***/


/*** Start of inlined file: juce_FileTreeComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

Image* juce_createIconForFile (const File& file);

class FileListTreeItem   : public TreeViewItem,
						   public TimeSliceClient,
						   public AsyncUpdater,
						   public ChangeListener
{
public:

	FileListTreeItem (FileTreeComponent& owner_,
					  DirectoryContentsList* const parentContentsList_,
					  const int indexInContentsList_,
					  const File& file_,
					  TimeSliceThread& thread_)
		: file (file_),
		  owner (owner_),
		  parentContentsList (parentContentsList_),
		  indexInContentsList (indexInContentsList_),
		  subContentsList (0),
		  canDeleteSubContentsList (false),
		  thread (thread_),
		  icon (0)
	{
		DirectoryContentsList::FileInfo fileInfo;

		if (parentContentsList_ != 0
			 && parentContentsList_->getFileInfo (indexInContentsList_, fileInfo))
		{
			fileSize = File::descriptionOfSizeInBytes (fileInfo.fileSize);
			modTime = fileInfo.modificationTime.formatted (T("%d %b '%y %H:%M"));
			isDirectory = fileInfo.isDirectory;
		}
		else
		{
			isDirectory = true;
		}
	}

	~FileListTreeItem()
	{
		thread.removeTimeSliceClient (this);

		clearSubItems();
		ImageCache::release (icon);

		if (canDeleteSubContentsList)
			delete subContentsList;
	}

	bool mightContainSubItems()		 { return isDirectory; }
	const String getUniqueName() const	  { return file.getFullPathName(); }
	int getItemHeight() const		   { return 22; }

	const String getDragSourceDescription()	 { return owner.getDragAndDropDescription(); }

	void itemOpennessChanged (bool isNowOpen)
	{
		if (isNowOpen)
		{
			clearSubItems();

			isDirectory = file.isDirectory();

			if (isDirectory)
			{
				if (subContentsList == 0)
				{
					jassert (parentContentsList != 0);

					DirectoryContentsList* const l = new DirectoryContentsList (parentContentsList->getFilter(), thread);
					l->setDirectory (file, true, true);

					setSubContentsList (l);
					canDeleteSubContentsList = true;
				}

				changeListenerCallback (0);
			}
		}
	}

	void setSubContentsList (DirectoryContentsList* newList)
	{
		jassert (subContentsList == 0);
		subContentsList = newList;
		newList->addChangeListener (this);
	}

	void changeListenerCallback (void*)
	{
		clearSubItems();

		if (isOpen() && subContentsList != 0)
		{
			for (int i = 0; i < subContentsList->getNumFiles(); ++i)
			{
				FileListTreeItem* const item
					= new FileListTreeItem (owner, subContentsList, i, subContentsList->getFile(i), thread);

				addSubItem (item);
			}
		}
	}

	void paintItem (Graphics& g, int width, int height)
	{
		if (file != File::nonexistent)
		{
			updateIcon (true);

			if (icon == 0)
				thread.addTimeSliceClient (this);
		}

		owner.getLookAndFeel()
			.drawFileBrowserRow (g, width, height,
								 file.getFileName(),
								 icon,
								 fileSize, modTime,
								 isDirectory, isSelected(),
								 indexInContentsList);
	}

	void itemClicked (const MouseEvent& e)
	{
		owner.sendMouseClickMessage (file, e);
	}

	void itemDoubleClicked (const MouseEvent& e)
	{
		TreeViewItem::itemDoubleClicked (e);

		owner.sendDoubleClickMessage (file);
	}

	void itemSelectionChanged (bool)
	{
		owner.sendSelectionChangeMessage();
	}

	bool useTimeSlice()
	{
		updateIcon (false);
		thread.removeTimeSliceClient (this);
		return false;
	}

	void handleAsyncUpdate()
	{
		owner.repaint();
	}

	const File file;

	juce_UseDebuggingNewOperator

private:
	FileTreeComponent& owner;
	DirectoryContentsList* parentContentsList;
	int indexInContentsList;
	DirectoryContentsList* subContentsList;
	bool isDirectory, canDeleteSubContentsList;
	TimeSliceThread& thread;
	Image* icon;
	String fileSize;
	String modTime;

	void updateIcon (const bool onlyUpdateIfCached)
	{
		if (icon == 0)
		{
			const int hashCode = (file.getFullPathName() + "_iconCacheSalt").hashCode();
			Image* im = ImageCache::getFromHashCode (hashCode);

			if (im == 0 && ! onlyUpdateIfCached)
			{
				im = juce_createIconForFile (file);

				if (im != 0)
					ImageCache::addImageToCache (im, hashCode);
			}

			if (im != 0)
			{
				icon = im;
				triggerAsyncUpdate();
			}
		}
	}
};

FileTreeComponent::FileTreeComponent (DirectoryContentsList& listToShow)
	: DirectoryContentsDisplayComponent (listToShow)
{
	FileListTreeItem* const root
		= new FileListTreeItem (*this, 0, 0, listToShow.getDirectory(),
								listToShow.getTimeSliceThread());

	root->setSubContentsList (&listToShow);
	setRootItemVisible (false);
	setRootItem (root);
}

FileTreeComponent::~FileTreeComponent()
{
	deleteRootItem();
}

const File FileTreeComponent::getSelectedFile (const int index) const
{
	const FileListTreeItem* const item = dynamic_cast <const FileListTreeItem*> (getSelectedItem (index));

	if (item != 0)
		return item->file;

	return File::nonexistent;
}

void FileTreeComponent::scrollToTop()
{
	getViewport()->getVerticalScrollBar()->setCurrentRangeStart (0);
}

void FileTreeComponent::setDragAndDropDescription (const String& description)
{
	dragAndDropDescription = description;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FileTreeComponent.cpp ***/


/*** Start of inlined file: juce_ImagePreviewComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

ImagePreviewComponent::ImagePreviewComponent()
{
}

ImagePreviewComponent::~ImagePreviewComponent()
{
}

void ImagePreviewComponent::getThumbSize (int& w, int& h) const
{
	const int availableW = proportionOfWidth (0.97f);
	const int availableH = getHeight() - 13 * 4;

	const double scale = jmin (1.0,
							   availableW / (double) w,
							   availableH / (double) h);

	w = roundToInt (scale * w);
	h = roundToInt (scale * h);
}

void ImagePreviewComponent::selectedFileChanged (const File& file)
{
	if (fileToLoad != file)
	{
		fileToLoad = file;
		startTimer (100);
	}
}

void ImagePreviewComponent::timerCallback()
{
	stopTimer();

	currentThumbnail = 0;
	currentDetails = String::empty;
	repaint();

	ScopedPointer <FileInputStream> in (fileToLoad.createInputStream());

	if (in != 0)
	{
		ImageFileFormat* const format = ImageFileFormat::findImageFormatForStream (*in);

		if (format != 0)
		{
			currentThumbnail = format->decodeImage (*in);

			if (currentThumbnail != 0)
			{
				int w = currentThumbnail->getWidth();
				int h = currentThumbnail->getHeight();

				currentDetails
					<< fileToLoad.getFileName() << "\n"
					<< format->getFormatName() << "\n"
					<< w << " x " << h << " pixels\n"
					<< File::descriptionOfSizeInBytes (fileToLoad.getSize());

				getThumbSize (w, h);

				currentThumbnail = currentThumbnail->createCopy (w, h);
			}
		}
	}
}

void ImagePreviewComponent::paint (Graphics& g)
{
	if (currentThumbnail != 0)
	{
		g.setFont (13.0f);

		int w = currentThumbnail->getWidth();
		int h = currentThumbnail->getHeight();
		getThumbSize (w, h);

		const int numLines = 4;
		const int totalH = 13 * numLines + h + 4;
		const int y = (getHeight() - totalH) / 2;

		g.drawImageWithin (currentThumbnail,
						   (getWidth() - w) / 2, y, w, h,
						   RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize,
						   false);

		g.drawFittedText (currentDetails,
						  0, y + h + 4, getWidth(), 100,
						  Justification::centredTop, numLines);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ImagePreviewComponent.cpp ***/


/*** Start of inlined file: juce_WildcardFileFilter.cpp ***/
BEGIN_JUCE_NAMESPACE

WildcardFileFilter::WildcardFileFilter (const String& fileWildcardPatterns,
										const String& directoryWildcardPatterns,
										const String& description_)
	: FileFilter (description_.isEmpty() ? fileWildcardPatterns
										 : (description_ + " (" + fileWildcardPatterns + ")"))
{
	parse (fileWildcardPatterns, fileWildcards);
	parse (directoryWildcardPatterns, directoryWildcards);
}

WildcardFileFilter::~WildcardFileFilter()
{
}

bool WildcardFileFilter::isFileSuitable (const File& file) const
{
	return match (file, fileWildcards);
}

bool WildcardFileFilter::isDirectorySuitable (const File& file) const
{
	return match (file, directoryWildcards);
}

void WildcardFileFilter::parse (const String& pattern, StringArray& result)
{
	result.addTokens (pattern.toLowerCase(), ";,", "\"'");

	result.trim();
	result.removeEmptyStrings();

	// special case for *.*, because people use it to mean "any file", but it
	// would actually ignore files with no extension.
	for (int i = result.size(); --i >= 0;)
		if (result[i] == "*.*")
			result.set (i, "*");
}

bool WildcardFileFilter::match (const File& file, const StringArray& wildcards)
{
	const String filename (file.getFileName());

	for (int i = wildcards.size(); --i >= 0;)
		if (filename.matchesWildcard (wildcards[i], true))
			return true;

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_WildcardFileFilter.cpp ***/


/*** Start of inlined file: juce_KeyboardFocusTraverser.cpp ***/
BEGIN_JUCE_NAMESPACE

KeyboardFocusTraverser::KeyboardFocusTraverser()
{
}

KeyboardFocusTraverser::~KeyboardFocusTraverser()
{
}

namespace KeyboardFocusHelpers
{
	// This will sort a set of components, so that they are ordered in terms of
	// left-to-right and then top-to-bottom.
	class ScreenPositionComparator
	{
	public:
		ScreenPositionComparator() {}

		static int compareElements (const Component* const first, const Component* const second)
		{
			int explicitOrder1 = first->getExplicitFocusOrder();
			if (explicitOrder1 <= 0)
				explicitOrder1 = std::numeric_limits<int>::max() / 2;

			int explicitOrder2 = second->getExplicitFocusOrder();
			if (explicitOrder2 <= 0)
				explicitOrder2 = std::numeric_limits<int>::max() / 2;

			if (explicitOrder1 != explicitOrder2)
				return explicitOrder1 - explicitOrder2;

			const int diff = first->getY() - second->getY();

			return (diff == 0) ? first->getX() - second->getX()
							   : diff;
		}
	};

	static void findAllFocusableComponents (Component* const parent, Array <Component*>& comps)
	{
		if (parent->getNumChildComponents() > 0)
		{
			Array <Component*> localComps;
			ScreenPositionComparator comparator;

			int i;
			for (i = parent->getNumChildComponents(); --i >= 0;)
			{
				Component* const c = parent->getChildComponent (i);

				if (c->isVisible() && c->isEnabled())
					localComps.addSorted (comparator, c);
			}

			for (i = 0; i < localComps.size(); ++i)
			{
				Component* const c = localComps.getUnchecked (i);

				if (c->getWantsKeyboardFocus())
					comps.add (c);

				if (! c->isFocusContainer())
					findAllFocusableComponents (c, comps);
			}
		}
	}
}

static Component* getIncrementedComponent (Component* const current, const int delta)
{
	Component* focusContainer = current->getParentComponent();

	if (focusContainer != 0)
	{
		while (focusContainer->getParentComponent() != 0 && ! focusContainer->isFocusContainer())
			focusContainer = focusContainer->getParentComponent();

		if (focusContainer != 0)
		{
			Array <Component*> comps;
			KeyboardFocusHelpers::findAllFocusableComponents (focusContainer, comps);

			if (comps.size() > 0)
			{
				const int index = comps.indexOf (current);
				return comps [(index + comps.size() + delta) % comps.size()];
			}
		}
	}

	return 0;
}

Component* KeyboardFocusTraverser::getNextComponent (Component* current)
{
	return getIncrementedComponent (current, 1);
}

Component* KeyboardFocusTraverser::getPreviousComponent (Component* current)
{
	return getIncrementedComponent (current, -1);
}

Component* KeyboardFocusTraverser::getDefaultComponent (Component* parentComponent)
{
	Array <Component*> comps;

	if (parentComponent != 0)
		KeyboardFocusHelpers::findAllFocusableComponents (parentComponent, comps);

	return comps.getFirst();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_KeyboardFocusTraverser.cpp ***/


/*** Start of inlined file: juce_KeyListener.cpp ***/
BEGIN_JUCE_NAMESPACE

bool KeyListener::keyStateChanged (const bool, Component*)
{
	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_KeyListener.cpp ***/


/*** Start of inlined file: juce_KeyMappingEditorComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

// N.B. these two includes are put here deliberately to avoid problems with
// old GCCs failing on long include paths

const int maxKeys = 3;

class KeyMappingChangeButton  : public Button
{
public:
	KeyMappingChangeButton (KeyMappingEditorComponent* const owner_,
							const CommandID commandID_,
							const String& keyName,
							const int keyNum_)
		: Button (keyName),
		  owner (owner_),
		  commandID (commandID_),
		  keyNum (keyNum_)
	{
		setWantsKeyboardFocus (false);
		setTriggeredOnMouseDown (keyNum >= 0);

		if (keyNum_ < 0)
			setTooltip (TRANS("adds a new key-mapping"));
		else
			setTooltip (TRANS("click to change this key-mapping"));
	}

	~KeyMappingChangeButton()
	{
	}

	void paintButton (Graphics& g, bool /*isOver*/, bool /*isDown*/)
	{
		getLookAndFeel().drawKeymapChangeButton (g, getWidth(), getHeight(), *this,
												 keyNum >= 0 ? getName() : String::empty);
	}

	void clicked()
	{
		if (keyNum >= 0)
		{
			// existing key clicked..
			PopupMenu m;
			m.addItem (1, TRANS("change this key-mapping"));
			m.addSeparator();
			m.addItem (2, TRANS("remove this key-mapping"));

			const int res = m.show();

			if (res == 1)
			{
				owner->assignNewKey (commandID, keyNum);
			}
			else if (res == 2)
			{
				owner->getMappings()->removeKeyPress (commandID, keyNum);
			}
		}
		else
		{
			// + button pressed..
			owner->assignNewKey (commandID, -1);
		}
	}

	void fitToContent (const int h) throw()
	{
		if (keyNum < 0)
		{
			setSize (h, h);
		}
		else
		{
			Font f (h * 0.6f);
			setSize (jlimit (h * 4, h * 8, 6 + f.getStringWidth (getName())), h);
		}
	}

	juce_UseDebuggingNewOperator

private:
	KeyMappingEditorComponent* const owner;
	const CommandID commandID;
	const int keyNum;

	KeyMappingChangeButton (const KeyMappingChangeButton&);
	KeyMappingChangeButton& operator= (const KeyMappingChangeButton&);
};

class KeyMappingItemComponent  : public Component
{
public:
	KeyMappingItemComponent (KeyMappingEditorComponent* const owner_,
							 const CommandID commandID_)
		: owner (owner_),
		  commandID (commandID_)
	{
		setInterceptsMouseClicks (false, true);

		const bool isReadOnly = owner_->isCommandReadOnly (commandID);

		const Array <KeyPress> keyPresses (owner_->getMappings()->getKeyPressesAssignedToCommand (commandID));

		for (int i = 0; i < jmin (maxKeys, keyPresses.size()); ++i)
		{
			KeyMappingChangeButton* const kb
				= new KeyMappingChangeButton (owner_, commandID,
											  owner_->getDescriptionForKeyPress (keyPresses.getReference (i)), i);

			kb->setEnabled (! isReadOnly);
			addAndMakeVisible (kb);
		}

		KeyMappingChangeButton* const kb
			= new KeyMappingChangeButton (owner_, commandID, String::empty, -1);

		addChildComponent (kb);
		kb->setVisible (keyPresses.size() < maxKeys && ! isReadOnly);
	}

	~KeyMappingItemComponent()
	{
		deleteAllChildren();
	}

	void paint (Graphics& g)
	{
		g.setFont (getHeight() * 0.7f);
		g.setColour (findColour (KeyMappingEditorComponent::textColourId));

		g.drawFittedText (owner->getMappings()->getCommandManager()->getNameOfCommand (commandID),
						  4, 0, jmax (40, getChildComponent (0)->getX() - 5), getHeight(),
						  Justification::centredLeft, true);
	}

	void resized()
	{
		int x = getWidth() - 4;

		for (int i = getNumChildComponents(); --i >= 0;)
		{
			KeyMappingChangeButton* const kb = dynamic_cast <KeyMappingChangeButton*> (getChildComponent (i));

			kb->fitToContent (getHeight() - 2);
			kb->setTopRightPosition (x, 1);
			x -= kb->getWidth() + 5;
		}
	}

	juce_UseDebuggingNewOperator

private:
	KeyMappingEditorComponent* const owner;
	const CommandID commandID;

	KeyMappingItemComponent (const KeyMappingItemComponent&);
	KeyMappingItemComponent& operator= (const KeyMappingItemComponent&);
};

class KeyMappingTreeViewItem  : public TreeViewItem
{
public:
	KeyMappingTreeViewItem (KeyMappingEditorComponent* const owner_,
							const CommandID commandID_)
		: owner (owner_),
		  commandID (commandID_)
	{
	}

	~KeyMappingTreeViewItem()
	{
	}

	const String getUniqueName() const          { return String ((int) commandID) + "_id"; }
	bool mightContainSubItems()		 { return false; }
	int getItemHeight() const		   { return 20; }

	Component* createItemComponent()
	{
		return new KeyMappingItemComponent (owner, commandID);
	}

	juce_UseDebuggingNewOperator

private:
	KeyMappingEditorComponent* const owner;
	const CommandID commandID;

	KeyMappingTreeViewItem (const KeyMappingTreeViewItem&);
	KeyMappingTreeViewItem& operator= (const KeyMappingTreeViewItem&);
};

class KeyCategoryTreeViewItem  : public TreeViewItem
{
public:
	KeyCategoryTreeViewItem (KeyMappingEditorComponent* const owner_,
							 const String& name)
		: owner (owner_),
		  categoryName (name)
	{
	}

	~KeyCategoryTreeViewItem()
	{
	}

	const String getUniqueName() const          { return categoryName + "_cat"; }
	bool mightContainSubItems()		 { return true; }
	int getItemHeight() const		   { return 28; }

	void paintItem (Graphics& g, int width, int height)
	{
		g.setFont (height * 0.6f, Font::bold);
		g.setColour (owner->findColour (KeyMappingEditorComponent::textColourId));

		g.drawText (categoryName,
					2, 0, width - 2, height,
					Justification::centredLeft, true);
	}

	void itemOpennessChanged (bool isNowOpen)
	{
		if (isNowOpen)
		{
			if (getNumSubItems() == 0)
			{
				Array <CommandID> commands (owner->getMappings()->getCommandManager()->getCommandsInCategory (categoryName));

				for (int i = 0; i < commands.size(); ++i)
				{
					if (owner->shouldCommandBeIncluded (commands[i]))
						addSubItem (new KeyMappingTreeViewItem (owner, commands[i]));
				}
			}
		}
		else
		{
			clearSubItems();
		}
	}

	juce_UseDebuggingNewOperator

private:
	KeyMappingEditorComponent* owner;
	String categoryName;

	KeyCategoryTreeViewItem (const KeyCategoryTreeViewItem&);
	KeyCategoryTreeViewItem& operator= (const KeyCategoryTreeViewItem&);
};

KeyMappingEditorComponent::KeyMappingEditorComponent (KeyPressMappingSet* const mappingManager,
													  const bool showResetToDefaultButton)
	: mappings (mappingManager)
{
	jassert (mappingManager != 0); // can't be null!

	mappingManager->addChangeListener (this);

	setLinesDrawnForSubItems (false);

	resetButton = 0;

	if (showResetToDefaultButton)
	{
		addAndMakeVisible (resetButton = new TextButton (TRANS("reset to defaults")));
		resetButton->addButtonListener (this);
	}

	addAndMakeVisible (tree = new TreeView());
	tree->setColour (TreeView::backgroundColourId, findColour (backgroundColourId));
	tree->setRootItemVisible (false);
	tree->setDefaultOpenness (true);
	tree->setRootItem (this);
}

KeyMappingEditorComponent::~KeyMappingEditorComponent()
{
	mappings->removeChangeListener (this);
	deleteAllChildren();
}

bool KeyMappingEditorComponent::mightContainSubItems()
{
	return true;
}

const String KeyMappingEditorComponent::getUniqueName() const
{
	return "keys";
}

void KeyMappingEditorComponent::setColours (const Colour& mainBackground,
											const Colour& textColour)
{
	setColour (backgroundColourId, mainBackground);
	setColour (textColourId, textColour);
	tree->setColour (TreeView::backgroundColourId, mainBackground);
}

void KeyMappingEditorComponent::parentHierarchyChanged()
{
	changeListenerCallback (0);
}

void KeyMappingEditorComponent::resized()
{
	int h = getHeight();

	if (resetButton != 0)
	{
		const int buttonHeight = 20;
		h -= buttonHeight + 8;
		int x = getWidth() - 8;
		const int y = h + 6;

		resetButton->changeWidthToFitText (buttonHeight);
		resetButton->setTopRightPosition (x, y);
	}

	tree->setBounds (0, 0, getWidth(), h);
}

void KeyMappingEditorComponent::buttonClicked (Button* button)
{
	if (button == resetButton)
	{
		if (AlertWindow::showOkCancelBox (AlertWindow::QuestionIcon,
										  TRANS("Reset to defaults"),
										  TRANS("Are you sure you want to reset all the key-mappings to their default state?"),
										  TRANS("Reset")))
		{
			mappings->resetToDefaultMappings();
		}
	}
}

void KeyMappingEditorComponent::changeListenerCallback (void*)
{
	ScopedPointer <XmlElement> openness (tree->getOpennessState (true));

	clearSubItems();

	const StringArray categories (mappings->getCommandManager()->getCommandCategories());

	for (int i = 0; i < categories.size(); ++i)
	{
		const Array <CommandID> commands (mappings->getCommandManager()->getCommandsInCategory (categories[i]));
		int count = 0;

		for (int j = 0; j < commands.size(); ++j)
			if (shouldCommandBeIncluded (commands[j]))
				++count;

		if (count > 0)
			addSubItem (new KeyCategoryTreeViewItem (this, categories[i]));
	}

	if (openness != 0)
		tree->restoreOpennessState (*openness);
}

class KeyEntryWindow  : public AlertWindow
{
public:
	KeyEntryWindow (KeyMappingEditorComponent* const owner_)
		: AlertWindow (TRANS("New key-mapping"),
					   TRANS("Please press a key combination now..."),
					   AlertWindow::NoIcon),
		  owner (owner_)
	{
		addButton (TRANS("ok"), 1);
		addButton (TRANS("cancel"), 0);

		// (avoid return + escape keys getting processed by the buttons..)
		for (int i = getNumChildComponents(); --i >= 0;)
			getChildComponent (i)->setWantsKeyboardFocus (false);

		setWantsKeyboardFocus (true);
		grabKeyboardFocus();
	}

	~KeyEntryWindow()
	{
	}

	bool keyPressed (const KeyPress& key)
	{
		lastPress = key;
		String message (TRANS("Key: ") + owner->getDescriptionForKeyPress (key));

		const CommandID previousCommand = owner->getMappings()->findCommandForKeyPress (key);

		if (previousCommand != 0)
		{
			message << "\n\n"
					<< TRANS("(Currently assigned to \"")
					<< owner->getMappings()->getCommandManager()->getNameOfCommand (previousCommand)
					<< "\")";
		}

		setMessage (message);

		return true;
	}

	bool keyStateChanged (bool)
	{
		return true;
	}

	KeyPress lastPress;

	juce_UseDebuggingNewOperator

private:
	KeyMappingEditorComponent* owner;

	KeyEntryWindow (const KeyEntryWindow&);
	KeyEntryWindow& operator= (const KeyEntryWindow&);
};

void KeyMappingEditorComponent::assignNewKey (const CommandID commandID, const int index)
{
	KeyEntryWindow entryWindow (this);

	if (entryWindow.runModalLoop() != 0)
	{
		entryWindow.setVisible (false);

		if (entryWindow.lastPress.isValid())
		{
			const CommandID previousCommand = mappings->findCommandForKeyPress (entryWindow.lastPress);

			if (previousCommand != 0)
			{
				if (! AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
													TRANS("Change key-mapping"),
													TRANS("This key is already assigned to the command \"")
													  + mappings->getCommandManager()->getNameOfCommand (previousCommand)
													  + TRANS("\"\n\nDo you want to re-assign it to this new command instead?"),
													TRANS("re-assign"),
													TRANS("cancel")))
				{
					return;
				}
			}

			mappings->removeKeyPress (entryWindow.lastPress);

			if (index >= 0)
				mappings->removeKeyPress (commandID, index);

			mappings->addKeyPress (commandID, entryWindow.lastPress, index);
		}
	}
}

bool KeyMappingEditorComponent::shouldCommandBeIncluded (const CommandID commandID)
{
	const ApplicationCommandInfo* const ci = mappings->getCommandManager()->getCommandForID (commandID);

	return (ci != 0) && ((ci->flags & ApplicationCommandInfo::hiddenFromKeyEditor) == 0);
}

bool KeyMappingEditorComponent::isCommandReadOnly (const CommandID commandID)
{
	const ApplicationCommandInfo* const ci = mappings->getCommandManager()->getCommandForID (commandID);

	return (ci != 0) && ((ci->flags & ApplicationCommandInfo::readOnlyInKeyEditor) != 0);
}

const String KeyMappingEditorComponent::getDescriptionForKeyPress (const KeyPress& key)
{
	return key.getTextDescription();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_KeyMappingEditorComponent.cpp ***/


/*** Start of inlined file: juce_KeyPress.cpp ***/
BEGIN_JUCE_NAMESPACE

KeyPress::KeyPress() throw()
	: keyCode (0),
	  mods (0),
	  textCharacter (0)
{
}

KeyPress::KeyPress (const int keyCode_,
					const ModifierKeys& mods_,
					const juce_wchar textCharacter_) throw()
	: keyCode (keyCode_),
	  mods (mods_),
	  textCharacter (textCharacter_)
{
}

KeyPress::KeyPress (const int keyCode_) throw()
	: keyCode (keyCode_),
	  textCharacter (0)
{
}

KeyPress::KeyPress (const KeyPress& other) throw()
	: keyCode (other.keyCode),
	  mods (other.mods),
	  textCharacter (other.textCharacter)
{
}

KeyPress& KeyPress::operator= (const KeyPress& other) throw()
{
	keyCode = other.keyCode;
	mods = other.mods;
	textCharacter = other.textCharacter;

	return *this;
}

bool KeyPress::operator== (const KeyPress& other) const throw()
{
	return mods.getRawFlags() == other.mods.getRawFlags()
			&& (textCharacter == other.textCharacter
				 || textCharacter == 0
				 || other.textCharacter == 0)
			&& (keyCode == other.keyCode
				 || (keyCode < 256
					  && other.keyCode < 256
					  && CharacterFunctions::toLowerCase ((juce_wchar) keyCode)
						   == CharacterFunctions::toLowerCase ((juce_wchar) other.keyCode)));
}

bool KeyPress::operator!= (const KeyPress& other) const throw()
{
	return ! operator== (other);
}

bool KeyPress::isCurrentlyDown() const
{
	return isKeyCurrentlyDown (keyCode)
			&& (ModifierKeys::getCurrentModifiers().getRawFlags() & ModifierKeys::allKeyboardModifiers)
				  == (mods.getRawFlags() & ModifierKeys::allKeyboardModifiers);
}

namespace KeyPressHelpers
{
	struct KeyNameAndCode
	{
		const char* name;
		int code;
	};

	static const KeyNameAndCode translations[] =
	{
		{ "spacebar",       KeyPress::spaceKey },
		{ "return",         KeyPress::returnKey },
		{ "escape",         KeyPress::escapeKey },
		{ "backspace",      KeyPress::backspaceKey },
		{ "cursor left",    KeyPress::leftKey },
		{ "cursor right",   KeyPress::rightKey },
		{ "cursor up",      KeyPress::upKey },
		{ "cursor down",    KeyPress::downKey },
		{ "page up",        KeyPress::pageUpKey },
		{ "page down",      KeyPress::pageDownKey },
		{ "home",           KeyPress::homeKey },
		{ "end",            KeyPress::endKey },
		{ "delete",         KeyPress::deleteKey },
		{ "insert",         KeyPress::insertKey },
		{ "tab",            KeyPress::tabKey },
		{ "play",           KeyPress::playKey },
		{ "stop",           KeyPress::stopKey },
		{ "fast forward",   KeyPress::fastForwardKey },
		{ "rewind",         KeyPress::rewindKey }
	};

	static const String numberPadPrefix()      { return "numpad "; }
}

const KeyPress KeyPress::createFromDescription (const String& desc)
{
	int modifiers = 0;

	if (desc.containsWholeWordIgnoreCase ("ctrl")
		 || desc.containsWholeWordIgnoreCase ("control")
		 || desc.containsWholeWordIgnoreCase ("ctl"))
		modifiers |= ModifierKeys::ctrlModifier;

	if (desc.containsWholeWordIgnoreCase ("shift")
		 || desc.containsWholeWordIgnoreCase ("shft"))
		modifiers |= ModifierKeys::shiftModifier;

	if (desc.containsWholeWordIgnoreCase ("alt")
		 || desc.containsWholeWordIgnoreCase ("option"))
		modifiers |= ModifierKeys::altModifier;

	if (desc.containsWholeWordIgnoreCase ("command")
		 || desc.containsWholeWordIgnoreCase ("cmd"))
		modifiers |= ModifierKeys::commandModifier;

	int key = 0;

	for (int i = 0; i < numElementsInArray (KeyPressHelpers::translations); ++i)
	{
		if (desc.containsWholeWordIgnoreCase (String (KeyPressHelpers::translations[i].name)))
		{
			key = KeyPressHelpers::translations[i].code;
			break;
		}
	}

	if (key == 0)
	{
		// see if it's a numpad key..
		if (desc.containsIgnoreCase (KeyPressHelpers::numberPadPrefix()))
		{
			const juce_wchar lastChar = desc.trimEnd().getLastCharacter();

			if (lastChar >= '0' && lastChar <= '9')
				key = numberPad0 + lastChar - '0';
			else if (lastChar == '+')
				key = numberPadAdd;
			else if (lastChar == '-')
				key = numberPadSubtract;
			else if (lastChar == '*')
				key = numberPadMultiply;
			else if (lastChar == '/')
				key = numberPadDivide;
			else if (lastChar == '.')
				key = numberPadDecimalPoint;
			else if (lastChar == '=')
				key = numberPadEquals;
			else if (desc.endsWith ("separator"))
				key = numberPadSeparator;
			else if (desc.endsWith ("delete"))
				key = numberPadDelete;
		}

		if (key == 0)
		{
			// see if it's a function key..
			for (int i = 1; i <= 12; ++i)
				if (desc.containsWholeWordIgnoreCase ("f" + String (i)))
					key = F1Key + i - 1;

			if (key == 0)
			{
				// give up and use the hex code..
				const int hexCode = desc.fromFirstOccurrenceOf ("#", false, false)
										.toLowerCase()
										.retainCharacters ("0123456789abcdef")
										.getHexValue32();

				if (hexCode > 0)
					key = hexCode;
				else
					key = CharacterFunctions::toUpperCase (desc.getLastCharacter());
			}
		}
	}

	return KeyPress (key, ModifierKeys (modifiers), 0);
}

const String KeyPress::getTextDescription() const
{
	String desc;

	if (keyCode > 0)
	{
		// some keyboard layouts use a shift-key to get the slash, but in those cases, we
		// want to store it as being a slash, not shift+whatever.
		if (textCharacter == '/')
			return "/";

		if (mods.isCtrlDown())
			desc << "ctrl + ";

		if (mods.isShiftDown())
			desc << "shift + ";

#if JUCE_MAC
		  // only do this on the mac, because on Windows ctrl and command are the same,
		  // and this would get confusing
		  if (mods.isCommandDown())
			  desc << "command + ";

		if (mods.isAltDown())
			desc << "option + ";
#else
		if (mods.isAltDown())
			desc << "alt + ";
#endif

		for (int i = 0; i < numElementsInArray (KeyPressHelpers::translations); ++i)
			if (keyCode == KeyPressHelpers::translations[i].code)
				return desc + KeyPressHelpers::translations[i].name;

		if (keyCode >= F1Key && keyCode <= F16Key)
			desc << 'F' << (1 + keyCode - F1Key);
		else if (keyCode >= numberPad0 && keyCode <= numberPad9)
			desc << KeyPressHelpers::numberPadPrefix() << (keyCode - numberPad0);
		else if (keyCode >= 33 && keyCode < 176)
			desc += CharacterFunctions::toUpperCase ((juce_wchar) keyCode);
		else if (keyCode == numberPadAdd)
			desc << KeyPressHelpers::numberPadPrefix() << '+';
		else if (keyCode == numberPadSubtract)
			desc << KeyPressHelpers::numberPadPrefix() << '-';
		else if (keyCode == numberPadMultiply)
			desc << KeyPressHelpers::numberPadPrefix() << '*';
		else if (keyCode == numberPadDivide)
			desc << KeyPressHelpers::numberPadPrefix() << '/';
		else if (keyCode == numberPadSeparator)
			desc << KeyPressHelpers::numberPadPrefix() << "separator";
		else if (keyCode == numberPadDecimalPoint)
			desc << KeyPressHelpers::numberPadPrefix() << '.';
		else if (keyCode == numberPadDelete)
			desc << KeyPressHelpers::numberPadPrefix() << "delete";
		else
			desc << '#' << String::toHexString (keyCode);
	}

	return desc;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_KeyPress.cpp ***/


/*** Start of inlined file: juce_KeyPressMappingSet.cpp ***/
BEGIN_JUCE_NAMESPACE

KeyPressMappingSet::KeyPressMappingSet (ApplicationCommandManager* const commandManager_)
	: commandManager (commandManager_)
{
	// A manager is needed to get the descriptions of commands, and will be called when
	// a command is invoked. So you can't leave this null..
	jassert (commandManager_ != 0);

	Desktop::getInstance().addFocusChangeListener (this);
}

KeyPressMappingSet::KeyPressMappingSet (const KeyPressMappingSet& other)
	: commandManager (other.commandManager)
{
	Desktop::getInstance().addFocusChangeListener (this);
}

KeyPressMappingSet::~KeyPressMappingSet()
{
	Desktop::getInstance().removeFocusChangeListener (this);
}

const Array <KeyPress> KeyPressMappingSet::getKeyPressesAssignedToCommand (const CommandID commandID) const
{
	for (int i = 0; i < mappings.size(); ++i)
		if (mappings.getUnchecked(i)->commandID == commandID)
			return mappings.getUnchecked (i)->keypresses;

	return Array <KeyPress> ();
}

void KeyPressMappingSet::addKeyPress (const CommandID commandID,
									  const KeyPress& newKeyPress,
									  int insertIndex)
{
	// If you specify an upper-case letter but no shift key, how is the user supposed to press it!?
	// Stick to lower-case letters when defining a keypress, to avoid ambiguity.
	jassert (! (CharacterFunctions::isUpperCase (newKeyPress.getTextCharacter())
				 && ! newKeyPress.getModifiers().isShiftDown()));

	if (findCommandForKeyPress (newKeyPress) != commandID)
	{
		removeKeyPress (newKeyPress);

		if (newKeyPress.isValid())
		{
			for (int i = mappings.size(); --i >= 0;)
			{
				if (mappings.getUnchecked(i)->commandID == commandID)
				{
					mappings.getUnchecked(i)->keypresses.insert (insertIndex, newKeyPress);

					sendChangeMessage (this);
					return;
				}
			}

			const ApplicationCommandInfo* const ci = commandManager->getCommandForID (commandID);

			if (ci != 0)
			{
				CommandMapping* const cm = new CommandMapping();
				cm->commandID = commandID;
				cm->keypresses.add (newKeyPress);
				cm->wantsKeyUpDownCallbacks = (ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) != 0;

				mappings.add (cm);
				sendChangeMessage (this);
			}
		}
	}
}

void KeyPressMappingSet::resetToDefaultMappings()
{
	mappings.clear();

	for (int i = 0; i < commandManager->getNumCommands(); ++i)
	{
		const ApplicationCommandInfo* const ci = commandManager->getCommandForIndex (i);

		for (int j = 0; j < ci->defaultKeypresses.size(); ++j)
		{
			addKeyPress (ci->commandID,
						 ci->defaultKeypresses.getReference (j));
		}
	}

	sendChangeMessage (this);
}

void KeyPressMappingSet::resetToDefaultMapping (const CommandID commandID)
{
	clearAllKeyPresses (commandID);

	const ApplicationCommandInfo* const ci = commandManager->getCommandForID (commandID);

	for (int j = 0; j < ci->defaultKeypresses.size(); ++j)
	{
		addKeyPress (ci->commandID,
					 ci->defaultKeypresses.getReference (j));
	}
}

void KeyPressMappingSet::clearAllKeyPresses()
{
	if (mappings.size() > 0)
	{
		sendChangeMessage (this);
		mappings.clear();
	}
}

void KeyPressMappingSet::clearAllKeyPresses (const CommandID commandID)
{
	for (int i = mappings.size(); --i >= 0;)
	{
		if (mappings.getUnchecked(i)->commandID == commandID)
		{
			mappings.remove (i);
			sendChangeMessage (this);
		}
	}
}

void KeyPressMappingSet::removeKeyPress (const KeyPress& keypress)
{
	if (keypress.isValid())
	{
		for (int i = mappings.size(); --i >= 0;)
		{
			CommandMapping* const cm = mappings.getUnchecked(i);

			for (int j = cm->keypresses.size(); --j >= 0;)
			{
				if (keypress == cm->keypresses [j])
				{
					cm->keypresses.remove (j);
					sendChangeMessage (this);
				}
			}
		}
	}
}

void KeyPressMappingSet::removeKeyPress (const CommandID commandID, const int keyPressIndex)
{
	for (int i = mappings.size(); --i >= 0;)
	{
		if (mappings.getUnchecked(i)->commandID == commandID)
		{
			mappings.getUnchecked(i)->keypresses.remove (keyPressIndex);
			sendChangeMessage (this);
			break;
		}
	}
}

CommandID KeyPressMappingSet::findCommandForKeyPress (const KeyPress& keyPress) const throw()
{
	for (int i = 0; i < mappings.size(); ++i)
		if (mappings.getUnchecked(i)->keypresses.contains (keyPress))
			return mappings.getUnchecked(i)->commandID;

	return 0;
}

bool KeyPressMappingSet::containsMapping (const CommandID commandID, const KeyPress& keyPress) const throw()
{
	for (int i = mappings.size(); --i >= 0;)
		if (mappings.getUnchecked(i)->commandID == commandID)
			return mappings.getUnchecked(i)->keypresses.contains (keyPress);

	return false;
}

void KeyPressMappingSet::invokeCommand (const CommandID commandID,
										const KeyPress& key,
										const bool isKeyDown,
										const int millisecsSinceKeyPressed,
										Component* const originatingComponent) const
{
	ApplicationCommandTarget::InvocationInfo info (commandID);

	info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromKeyPress;
	info.isKeyDown = isKeyDown;
	info.keyPress = key;
	info.millisecsSinceKeyPressed = millisecsSinceKeyPressed;
	info.originatingComponent = originatingComponent;

	commandManager->invoke (info, false);
}

bool KeyPressMappingSet::restoreFromXml (const XmlElement& xmlVersion)
{
	if (xmlVersion.hasTagName ("KEYMAPPINGS"))
	{
		if (xmlVersion.getBoolAttribute ("basedOnDefaults", true))
		{
			// if the XML was created as a set of differences from the default mappings,
			// (i.e. by calling createXml (true)), then we need to first restore the defaults.
			resetToDefaultMappings();
		}
		else
		{
			// if the XML was created calling createXml (false), then we need to clear all
			// the keys and treat the xml as describing the entire set of mappings.
			clearAllKeyPresses();
		}

		forEachXmlChildElement (xmlVersion, map)
		{
			const CommandID commandId = map->getStringAttribute ("commandId").getHexValue32();

			if (commandId != 0)
			{
				const KeyPress key (KeyPress::createFromDescription (map->getStringAttribute ("key")));

				if (map->hasTagName ("MAPPING"))
				{
					addKeyPress (commandId, key);
				}
				else if (map->hasTagName ("UNMAPPING"))
				{
					if (containsMapping (commandId, key))
						removeKeyPress (key);
				}
			}
		}

		return true;
	}

	return false;
}

XmlElement* KeyPressMappingSet::createXml (const bool saveDifferencesFromDefaultSet) const
{
	ScopedPointer <KeyPressMappingSet> defaultSet;

	if (saveDifferencesFromDefaultSet)
	{
		defaultSet = new KeyPressMappingSet (commandManager);
		defaultSet->resetToDefaultMappings();
	}

	XmlElement* const doc = new XmlElement ("KEYMAPPINGS");

	doc->setAttribute ("basedOnDefaults", saveDifferencesFromDefaultSet);

	int i;
	for (i = 0; i < mappings.size(); ++i)
	{
		const CommandMapping* const cm = mappings.getUnchecked(i);

		for (int j = 0; j < cm->keypresses.size(); ++j)
		{
			if (defaultSet == 0
				 || ! defaultSet->containsMapping (cm->commandID, cm->keypresses.getReference (j)))
			{
				XmlElement* const map = doc->createNewChildElement ("MAPPING");

				map->setAttribute ("commandId", String::toHexString ((int) cm->commandID));
				map->setAttribute ("description", commandManager->getDescriptionOfCommand (cm->commandID));
				map->setAttribute ("key", cm->keypresses.getReference (j).getTextDescription());
			}
		}
	}

	if (defaultSet != 0)
	{
		for (i = 0; i < defaultSet->mappings.size(); ++i)
		{
			const CommandMapping* const cm = defaultSet->mappings.getUnchecked(i);

			for (int j = 0; j < cm->keypresses.size(); ++j)
			{
				if (! containsMapping (cm->commandID, cm->keypresses.getReference (j)))
				{
					XmlElement* const map = doc->createNewChildElement ("UNMAPPING");

					map->setAttribute ("commandId", String::toHexString ((int) cm->commandID));
					map->setAttribute ("description", commandManager->getDescriptionOfCommand (cm->commandID));
					map->setAttribute ("key", cm->keypresses.getReference (j).getTextDescription());
				}
			}
		}
	}

	return doc;
}

bool KeyPressMappingSet::keyPressed (const KeyPress& key,
									 Component* originatingComponent)
{
	bool used = false;

	const CommandID commandID = findCommandForKeyPress (key);

	const ApplicationCommandInfo* const ci = commandManager->getCommandForID (commandID);

	if (ci != 0
		 && (ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) == 0)
	{
		ApplicationCommandInfo info (0);

		if (commandManager->getTargetForCommand (commandID, info) != 0
			 && (info.flags & ApplicationCommandInfo::isDisabled) == 0)
		{
			invokeCommand (commandID, key, true, 0, originatingComponent);
			used = true;
		}
		else
		{
			if (originatingComponent != 0)
				originatingComponent->getLookAndFeel().playAlertSound();
		}
	}

	return used;
}

bool KeyPressMappingSet::keyStateChanged (const bool /*isKeyDown*/, Component* originatingComponent)
{
	bool used = false;
	const uint32 now = Time::getMillisecondCounter();

	for (int i = mappings.size(); --i >= 0;)
	{
		CommandMapping* const cm =  mappings.getUnchecked(i);

		if (cm->wantsKeyUpDownCallbacks)
		{
			for (int j = cm->keypresses.size(); --j >= 0;)
			{
				const KeyPress key (cm->keypresses.getReference (j));
				const bool isDown = key.isCurrentlyDown();

				int keyPressEntryIndex = 0;
				bool wasDown = false;

				for (int k = keysDown.size(); --k >= 0;)
				{
					if (key == keysDown.getUnchecked(k)->key)
					{
						keyPressEntryIndex = k;
						wasDown = true;
						used = true;
						break;
					}
				}

				if (isDown != wasDown)
				{
					int millisecs = 0;

					if (isDown)
					{
						KeyPressTime* const k = new KeyPressTime();
						k->key = key;
						k->timeWhenPressed = now;

						keysDown.add (k);
					}
					else
					{
						const uint32 pressTime = keysDown.getUnchecked (keyPressEntryIndex)->timeWhenPressed;

						if (now > pressTime)
							millisecs = now - pressTime;

						keysDown.remove (keyPressEntryIndex);
					}

					invokeCommand (cm->commandID, key, isDown, millisecs, originatingComponent);
					used = true;
				}
			}
		}
	}

	return used;
}

void KeyPressMappingSet::globalFocusChanged (Component* focusedComponent)
{
	if (focusedComponent != 0)
		focusedComponent->keyStateChanged (false);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_KeyPressMappingSet.cpp ***/


/*** Start of inlined file: juce_ModifierKeys.cpp ***/
BEGIN_JUCE_NAMESPACE

ModifierKeys::ModifierKeys (const int flags_) throw()
	: flags (flags_)
{
}

ModifierKeys::ModifierKeys (const ModifierKeys& other) throw()
	: flags (other.flags)
{
}

ModifierKeys& ModifierKeys::operator= (const ModifierKeys& other) throw()
{
	flags = other.flags;
	return *this;
}

ModifierKeys ModifierKeys::currentModifiers;

const ModifierKeys ModifierKeys::getCurrentModifiers() throw()
{
	return currentModifiers;
}

int ModifierKeys::getNumMouseButtonsDown() const throw()
{
	int num = 0;

	if (isLeftButtonDown())	 ++num;
	if (isRightButtonDown())	++num;
	if (isMiddleButtonDown())   ++num;

	return num;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ModifierKeys.cpp ***/


/*** Start of inlined file: juce_ComponentAnimator.cpp ***/
BEGIN_JUCE_NAMESPACE

class ComponentAnimator::AnimationTask
{
public:
	AnimationTask (Component* const comp)
		: component (comp)
	{
	}

	Component::SafePointer<Component> component;
	Rectangle<int> destination;
	int msElapsed, msTotal;
	double startSpeed, midSpeed, endSpeed, lastProgress;
	double left, top, right, bottom;

	bool useTimeslice (const int elapsed)
	{
		if (component == 0)
			return false;

		msElapsed += elapsed;
		double newProgress = msElapsed / (double) msTotal;

		if (newProgress >= 0 && newProgress < 1.0)
		{
			newProgress = timeToDistance (newProgress);
			const double delta = (newProgress - lastProgress) / (1.0 - lastProgress);
			jassert (newProgress >= lastProgress);
			lastProgress = newProgress;

			left += (destination.getX() - left) * delta;
			top += (destination.getY() - top) * delta;
			right += (destination.getRight() - right) * delta;
			bottom += (destination.getBottom() - bottom) * delta;

			if (delta < 1.0)
			{
				const Rectangle<int> newBounds (roundToInt (left),
												roundToInt (top),
												roundToInt (right - left),
												roundToInt (bottom - top));

				if (newBounds != destination)
				{
					component->setBounds (newBounds);
					return true;
				}
			}
		}

		component->setBounds (destination);
		return false;
	}

	void moveToFinalDestination()
	{
		if (component != 0)
			component->setBounds (destination);
	}

private:
	inline double timeToDistance (const double time) const
	{
		return (time < 0.5) ? time * (startSpeed + time * (midSpeed - startSpeed))
							: 0.5 * (startSpeed + 0.5 * (midSpeed - startSpeed))
								+ (time - 0.5) * (midSpeed + (time - 0.5) * (endSpeed - midSpeed));
	}
};

ComponentAnimator::ComponentAnimator()
	: lastTime (0)
{
}

ComponentAnimator::~ComponentAnimator()
{
	cancelAllAnimations (false);
	jassert (tasks.size() == 0);
}

ComponentAnimator::AnimationTask* ComponentAnimator::findTaskFor (Component* const component) const
{
	for (int i = tasks.size(); --i >= 0;)
		if (component == tasks.getUnchecked(i)->component.getComponent())
			return tasks.getUnchecked(i);

	return 0;
}

void ComponentAnimator::animateComponent (Component* const component,
										  const Rectangle<int>& finalPosition,
										  const int millisecondsToSpendMoving,
										  const double startSpeed,
										  const double endSpeed)
{
	if (component != 0)
	{
		AnimationTask* at = findTaskFor (component);

		if (at == 0)
		{
			at = new AnimationTask (component);
			tasks.add (at);
			sendChangeMessage (this);
		}

		at->msElapsed = 0;
		at->lastProgress = 0;
		at->msTotal = jmax (1, millisecondsToSpendMoving);
		at->destination = finalPosition;

		// the speeds must be 0 or greater!
		jassert (startSpeed >= 0 && endSpeed >= 0)

		const double invTotalDistance = 4.0 / (startSpeed + endSpeed + 2.0);
		at->startSpeed = jmax (0.0, startSpeed * invTotalDistance);
		at->midSpeed = invTotalDistance;
		at->endSpeed = jmax (0.0, endSpeed * invTotalDistance);

		at->left = component->getX();
		at->top = component->getY();
		at->right = component->getRight();
		at->bottom = component->getBottom();

		if (! isTimerRunning())
		{
			lastTime = Time::getMillisecondCounter();
			startTimer (1000 / 50);
		}
	}
}

void ComponentAnimator::cancelAllAnimations (const bool moveComponentsToTheirFinalPositions)
{
	for (int i = tasks.size(); --i >= 0;)
	{
		AnimationTask* const at = tasks.getUnchecked(i);

		if (moveComponentsToTheirFinalPositions)
			at->moveToFinalDestination();

		delete at;
		tasks.remove (i);
		sendChangeMessage (this);
	}
}

void ComponentAnimator::cancelAnimation (Component* const component,
										 const bool moveComponentToItsFinalPosition)
{
	AnimationTask* const at = findTaskFor (component);

	if (at != 0)
	{
		if (moveComponentToItsFinalPosition)
			at->moveToFinalDestination();

		tasks.removeValue (at);
		delete at;
		sendChangeMessage (this);
	}
}

const Rectangle<int> ComponentAnimator::getComponentDestination (Component* const component)
{
	AnimationTask* const at = findTaskFor (component);

	if (at != 0)
		return at->destination;
	else if (component != 0)
		return component->getBounds();

	return Rectangle<int>();
}

bool ComponentAnimator::isAnimating (Component* component) const
{
	return findTaskFor (component) != 0;
}

void ComponentAnimator::timerCallback()
{
	const uint32 timeNow = Time::getMillisecondCounter();

	if (lastTime == 0 || lastTime == timeNow)
		lastTime = timeNow;

	const int elapsed = timeNow - lastTime;

	for (int i = tasks.size(); --i >= 0;)
	{
		AnimationTask* const at = tasks.getUnchecked(i);

		if (! at->useTimeslice (elapsed))
		{
			tasks.remove (i);
			delete at;
			sendChangeMessage (this);
		}
	}

	lastTime = timeNow;

	if (tasks.size() == 0)
		stopTimer();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ComponentAnimator.cpp ***/


/*** Start of inlined file: juce_ComponentBoundsConstrainer.cpp ***/
BEGIN_JUCE_NAMESPACE

ComponentBoundsConstrainer::ComponentBoundsConstrainer() throw()
	: minW (0),
	  maxW (0x3fffffff),
	  minH (0),
	  maxH (0x3fffffff),
	  minOffTop (0),
	  minOffLeft (0),
	  minOffBottom (0),
	  minOffRight (0),
	  aspectRatio (0.0)
{
}

ComponentBoundsConstrainer::~ComponentBoundsConstrainer()
{
}

void ComponentBoundsConstrainer::setMinimumWidth (const int minimumWidth) throw()
{
	minW = minimumWidth;
}

void ComponentBoundsConstrainer::setMaximumWidth (const int maximumWidth) throw()
{
	maxW = maximumWidth;
}

void ComponentBoundsConstrainer::setMinimumHeight (const int minimumHeight) throw()
{
	minH = minimumHeight;
}

void ComponentBoundsConstrainer::setMaximumHeight (const int maximumHeight) throw()
{
	maxH = maximumHeight;
}

void ComponentBoundsConstrainer::setMinimumSize (const int minimumWidth, const int minimumHeight) throw()
{
	jassert (maxW >= minimumWidth);
	jassert (maxH >= minimumHeight);
	jassert (minimumWidth > 0 && minimumHeight > 0);

	minW = minimumWidth;
	minH = minimumHeight;

	if (minW > maxW)
		maxW = minW;

	if (minH > maxH)
		maxH = minH;
}

void ComponentBoundsConstrainer::setMaximumSize (const int maximumWidth, const int maximumHeight) throw()
{
	jassert (maximumWidth >= minW);
	jassert (maximumHeight >= minH);
	jassert (maximumWidth > 0 && maximumHeight > 0);

	maxW = jmax (minW, maximumWidth);
	maxH = jmax (minH, maximumHeight);
}

void ComponentBoundsConstrainer::setSizeLimits (const int minimumWidth,
												const int minimumHeight,
												const int maximumWidth,
												const int maximumHeight) throw()
{
	jassert (maximumWidth >= minimumWidth);
	jassert (maximumHeight >= minimumHeight);
	jassert (maximumWidth > 0 && maximumHeight > 0);
	jassert (minimumWidth > 0 && minimumHeight > 0);

	minW = jmax (0, minimumWidth);
	minH = jmax (0, minimumHeight);
	maxW = jmax (minW, maximumWidth);
	maxH = jmax (minH, maximumHeight);
}

void ComponentBoundsConstrainer::setMinimumOnscreenAmounts (const int minimumWhenOffTheTop,
															const int minimumWhenOffTheLeft,
															const int minimumWhenOffTheBottom,
															const int minimumWhenOffTheRight) throw()
{
	minOffTop = minimumWhenOffTheTop;
	minOffLeft = minimumWhenOffTheLeft;
	minOffBottom = minimumWhenOffTheBottom;
	minOffRight = minimumWhenOffTheRight;
}

void ComponentBoundsConstrainer::setFixedAspectRatio (const double widthOverHeight) throw()
{
	aspectRatio = jmax (0.0, widthOverHeight);
}

double ComponentBoundsConstrainer::getFixedAspectRatio() const throw()
{
	return aspectRatio;
}

void ComponentBoundsConstrainer::setBoundsForComponent (Component* const component,
														const Rectangle<int>& targetBounds,
														const bool isStretchingTop,
														const bool isStretchingLeft,
														const bool isStretchingBottom,
														const bool isStretchingRight)
{
	jassert (component != 0);

	Rectangle<int> limits, bounds (targetBounds);
	BorderSize border;

	Component* const parent = component->getParentComponent();

	if (parent == 0)
	{
		ComponentPeer* peer = component->getPeer();
		if (peer != 0)
			border = peer->getFrameSize();

		limits = Desktop::getInstance().getMonitorAreaContaining (bounds.getCentre());
	}
	else
	{
		limits.setSize (parent->getWidth(), parent->getHeight());
	}

	border.addTo (bounds);

	checkBounds (bounds,
				 border.addedTo (component->getBounds()), limits,
				 isStretchingTop, isStretchingLeft,
				 isStretchingBottom, isStretchingRight);

	border.subtractFrom (bounds);

	applyBoundsToComponent (component, bounds);
}

void ComponentBoundsConstrainer::checkComponentBounds (Component* component)
{
	setBoundsForComponent (component, component->getBounds(),
						   false, false, false, false);
}

void ComponentBoundsConstrainer::applyBoundsToComponent (Component* component,
														 const Rectangle<int>& bounds)
{
	component->setBounds (bounds);
}

void ComponentBoundsConstrainer::resizeStart()
{
}

void ComponentBoundsConstrainer::resizeEnd()
{
}

void ComponentBoundsConstrainer::checkBounds (Rectangle<int>& bounds,
											  const Rectangle<int>& old,
											  const Rectangle<int>& limits,
											  const bool isStretchingTop,
											  const bool isStretchingLeft,
											  const bool isStretchingBottom,
											  const bool isStretchingRight)
{
	int x = bounds.getX();
	int y = bounds.getY();
	int w = bounds.getWidth();
	int h = bounds.getHeight();

	// constrain the size if it's being stretched..
	if (isStretchingLeft)
	{
		x = jlimit (old.getRight() - maxW, old.getRight() - minW, x);
		w = old.getRight() - x;
	}

	if (isStretchingRight)
	{
		w = jlimit (minW, maxW, w);
	}

	if (isStretchingTop)
	{
		y = jlimit (old.getBottom() - maxH, old.getBottom() - minH, y);
		h = old.getBottom() - y;
	}

	if (isStretchingBottom)
	{
		h = jlimit (minH, maxH, h);
	}

	// constrain the aspect ratio if one has been specified..
	if (aspectRatio > 0.0 && w > 0 && h > 0)
	{
		bool adjustWidth;

		if ((isStretchingTop || isStretchingBottom) && ! (isStretchingLeft || isStretchingRight))
		{
			adjustWidth = true;
		}
		else if ((isStretchingLeft || isStretchingRight) && ! (isStretchingTop || isStretchingBottom))
		{
			adjustWidth = false;
		}
		else
		{
			const double oldRatio = (old.getHeight() > 0) ? fabs (old.getWidth() / (double) old.getHeight()) : 0.0;
			const double newRatio = fabs (w / (double) h);

			adjustWidth = (oldRatio > newRatio);
		}

		if (adjustWidth)
		{
			w = roundToInt (h * aspectRatio);

			if (w > maxW || w < minW)
			{
				w = jlimit (minW, maxW, w);
				h = roundToInt (w / aspectRatio);
			}
		}
		else
		{
			h = roundToInt (w / aspectRatio);

			if (h > maxH || h < minH)
			{
				h = jlimit (minH, maxH, h);
				w = roundToInt (h * aspectRatio);
			}
		}

		if ((isStretchingTop || isStretchingBottom) && ! (isStretchingLeft || isStretchingRight))
		{
			x = old.getX() + (old.getWidth() - w) / 2;
		}
		else if ((isStretchingLeft || isStretchingRight) && ! (isStretchingTop || isStretchingBottom))
		{
			y = old.getY() + (old.getHeight() - h) / 2;
		}
		else
		{
			if (isStretchingLeft)
				x = old.getRight() - w;

			if (isStretchingTop)
				y = old.getBottom() - h;
		}
	}

	// ...and constrain the position if limits have been set for that.
	if (minOffTop > 0 || minOffLeft > 0 || minOffBottom > 0 || minOffRight > 0)
	{
		if (minOffTop > 0)
		{
			const int limit = limits.getY() + jmin (minOffTop - h, 0);

			if (y < limit)
			{
				if (isStretchingTop)
					h -= (limit - y);

				y = limit;
			}
		}

		if (minOffLeft > 0)
		{
			const int limit = limits.getX() + jmin (minOffLeft - w, 0);

			if (x < limit)
			{
				if (isStretchingLeft)
					w -= (limit - x);

				x = limit;
			}
		}

		if (minOffBottom > 0)
		{
			const int limit = limits.getBottom() - jmin (minOffBottom, h);

			if (y > limit)
			{
				if (isStretchingBottom)
					h += (limit - y);
				else
					y = limit;
			}
		}

		if (minOffRight > 0)
		{
			const int limit = limits.getRight() - jmin (minOffRight, w);

			if (x > limit)
			{
				if (isStretchingRight)
					w += (limit - x);
				else
					x = limit;
			}
		}
	}

	jassert (w >= 0 && h >= 0);
	bounds = Rectangle<int> (x, y, w, h);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ComponentBoundsConstrainer.cpp ***/


/*** Start of inlined file: juce_ComponentMovementWatcher.cpp ***/
BEGIN_JUCE_NAMESPACE

ComponentMovementWatcher::ComponentMovementWatcher (Component* const component_)
	: component (component_),
	  lastPeer (0),
	  reentrant (false)
{
	jassert (component != 0); // can't use this with a null pointer..

	component->addComponentListener (this);

	registerWithParentComps();
}

ComponentMovementWatcher::~ComponentMovementWatcher()
{
	component->removeComponentListener (this);

	unregister();
}

void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
{
	// agh! don't delete the target component without deleting this object first!
	jassert (component != 0);

	if (! reentrant)
	{
		reentrant = true;

		ComponentPeer* const peer = component->getPeer();

		if (peer != lastPeer)
		{
			componentPeerChanged();

			if (component == 0)
				return;

			lastPeer = peer;
		}

		unregister();
		registerWithParentComps();

		reentrant = false;

		componentMovedOrResized (*component, true, true);
	}
}

void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
{
	// agh! don't delete the target component without deleting this object first!
	jassert (component != 0);

	if (wasMoved)
	{
		const Point<int> pos (component->relativePositionToOtherComponent (component->getTopLevelComponent(), Point<int>()));

		wasMoved = lastBounds.getPosition() != pos;
		lastBounds.setPosition (pos);
	}

	wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
	lastBounds.setSize (component->getWidth(), component->getHeight());

	if (wasMoved || wasResized)
		componentMovedOrResized (wasMoved, wasResized);
}

void ComponentMovementWatcher::registerWithParentComps() throw()
{
	Component* p = component->getParentComponent();

	while (p != 0)
	{
		p->addComponentListener (this);
		registeredParentComps.add (p);
		p = p->getParentComponent();
	}
}

void ComponentMovementWatcher::unregister() throw()
{
	for (int i = registeredParentComps.size(); --i >= 0;)
		static_cast <Component*> (registeredParentComps.getUnchecked(i))->removeComponentListener (this);

	registeredParentComps.clear();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ComponentMovementWatcher.cpp ***/


/*** Start of inlined file: juce_GroupComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

GroupComponent::GroupComponent (const String& componentName,
								const String& labelText)
	: Component (componentName),
	  text (labelText),
	  justification (Justification::left)
{
	setInterceptsMouseClicks  (false, true);
}

GroupComponent::~GroupComponent()
{
}

void GroupComponent::setText (const String& newText)
{
	if (text != newText)
	{
		text = newText;
		repaint();
	}
}

const String GroupComponent::getText() const
{
	return text;
}

void GroupComponent::setTextLabelPosition (const Justification& newJustification)
{
	if (justification.getFlags() != newJustification.getFlags())
	{
		justification = newJustification;
		repaint();
	}
}

void GroupComponent::paint (Graphics& g)
{
	getLookAndFeel()
		.drawGroupComponentOutline (g, getWidth(), getHeight(),
									text, justification,
									*this);
}

void GroupComponent::enablementChanged()
{
	repaint();
}

void GroupComponent::colourChanged()
{
	repaint();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_GroupComponent.cpp ***/


/*** Start of inlined file: juce_MultiDocumentPanel.cpp ***/
BEGIN_JUCE_NAMESPACE

MultiDocumentPanelWindow::MultiDocumentPanelWindow (const Colour& backgroundColour)
	: DocumentWindow (String::empty, backgroundColour,
					  DocumentWindow::maximiseButton | DocumentWindow::closeButton, false)
{
}

MultiDocumentPanelWindow::~MultiDocumentPanelWindow()
{
}

void MultiDocumentPanelWindow::maximiseButtonPressed()
{
	MultiDocumentPanel* const owner = getOwner();

	jassert (owner != 0); // these windows are only designed to be used inside a MultiDocumentPanel!
	if (owner != 0)
		owner->setLayoutMode (MultiDocumentPanel::MaximisedWindowsWithTabs);
}

void MultiDocumentPanelWindow::closeButtonPressed()
{
	MultiDocumentPanel* const owner = getOwner();

	jassert (owner != 0); // these windows are only designed to be used inside a MultiDocumentPanel!
	if (owner != 0)
		owner->closeDocument (getContentComponent(), true);
}

void MultiDocumentPanelWindow::activeWindowStatusChanged()
{
	DocumentWindow::activeWindowStatusChanged();
	updateOrder();
}

void MultiDocumentPanelWindow::broughtToFront()
{
	DocumentWindow::broughtToFront();
	updateOrder();
}

void MultiDocumentPanelWindow::updateOrder()
{
	MultiDocumentPanel* const owner = getOwner();

	if (owner != 0)
		owner->updateOrder();
}

MultiDocumentPanel* MultiDocumentPanelWindow::getOwner() const throw()
{
	// (unable to use the syntax findParentComponentOfClass <MultiDocumentPanel> () because of a VC6 compiler bug)
	return findParentComponentOfClass ((MultiDocumentPanel*) 0);
}

class MDITabbedComponentInternal   : public TabbedComponent
{
public:
	MDITabbedComponentInternal()
		: TabbedComponent (TabbedButtonBar::TabsAtTop)
	{
	}

	~MDITabbedComponentInternal()
	{
	}

	void currentTabChanged (int, const String&)
	{
		// (unable to use the syntax findParentComponentOfClass <MultiDocumentPanel> () because of a VC6 compiler bug)
		MultiDocumentPanel* const owner = findParentComponentOfClass ((MultiDocumentPanel*) 0);

		if (owner != 0)
			owner->updateOrder();
	}
};

MultiDocumentPanel::MultiDocumentPanel()
	: mode (MaximisedWindowsWithTabs),
	  tabComponent (0),
	  backgroundColour (Colours::lightblue),
	  maximumNumDocuments (0),
	  numDocsBeforeTabsUsed (0)
{
	setOpaque (true);
}

MultiDocumentPanel::~MultiDocumentPanel()
{
	closeAllDocuments (false);
}

static bool shouldDeleteComp (Component* const c)
{
	return c->getProperties() ["mdiDocumentDelete_"];
}

bool MultiDocumentPanel::closeAllDocuments (const bool checkItsOkToCloseFirst)
{
	while (components.size() > 0)
		if (! closeDocument (components.getLast(), checkItsOkToCloseFirst))
			return false;

	return true;
}

MultiDocumentPanelWindow* MultiDocumentPanel::createNewDocumentWindow()
{
	return new MultiDocumentPanelWindow (backgroundColour);
}

void MultiDocumentPanel::addWindow (Component* component)
{
	MultiDocumentPanelWindow* const dw = createNewDocumentWindow();

	dw->setResizable (true, false);
	dw->setContentComponent (component, false, true);
	dw->setName (component->getName());

	const var bkg (component->getProperties() ["mdiDocumentBkg_"]);
	dw->setBackgroundColour (bkg.isVoid() ? backgroundColour : Colour ((int) bkg));

	int x = 4;
	Component* const topComp = getChildComponent (getNumChildComponents() - 1);

	if (topComp != 0 && topComp->getX() == x && topComp->getY() == x)
		x += 16;

	dw->setTopLeftPosition (x, x);

	const var pos (component->getProperties() ["mdiDocumentPos_"]);
	if (pos.toString().isNotEmpty())
		dw->restoreWindowStateFromString (pos.toString());

	addAndMakeVisible (dw);
	dw->toFront (true);
}

bool MultiDocumentPanel::addDocument (Component* const component,
									  const Colour& docColour,
									  const bool deleteWhenRemoved)
{
	// If you try passing a full DocumentWindow or ResizableWindow in here, you'll end up
	// with a frame-within-a-frame! Just pass in the bare content component.
	jassert (dynamic_cast <ResizableWindow*> (component) == 0);

	if (component == 0 || (maximumNumDocuments > 0 && components.size() >= maximumNumDocuments))
		return false;

	components.add (component);
	component->getProperties().set ("mdiDocumentDelete_", deleteWhenRemoved);
	component->getProperties().set ("mdiDocumentBkg_", (int) docColour.getARGB());
	component->addComponentListener (this);

	if (mode == FloatingWindows)
	{
		if (isFullscreenWhenOneDocument())
		{
			if (components.size() == 1)
			{
				addAndMakeVisible (component);
			}
			else
			{
				if (components.size() == 2)
					addWindow (components.getFirst());

				addWindow (component);
			}
		}
		else
		{
		   addWindow (component);
		}
	}
	else
	{
		if (tabComponent == 0 && components.size() > numDocsBeforeTabsUsed)
		{
			addAndMakeVisible (tabComponent = new MDITabbedComponentInternal());

			Array <Component*> temp (components);

			for (int i = 0; i < temp.size(); ++i)
				tabComponent->addTab (temp[i]->getName(), docColour, temp[i], false);

			resized();
		}
		else
		{
			if (tabComponent != 0)
				tabComponent->addTab (component->getName(), docColour, component, false);
			else
				addAndMakeVisible (component);
		}

		setActiveDocument (component);
	}

	resized();
	activeDocumentChanged();
	return true;
}

bool MultiDocumentPanel::closeDocument (Component* component,
										const bool checkItsOkToCloseFirst)
{
	if (components.contains (component))
	{
		if (checkItsOkToCloseFirst && ! tryToCloseDocument (component))
			return false;

		component->removeComponentListener (this);

		const bool shouldDelete = shouldDeleteComp (component);
		component->getProperties().remove ("mdiDocumentDelete_");
		component->getProperties().remove ("mdiDocumentBkg_");

		if (mode == FloatingWindows)
		{
			for (int i = getNumChildComponents(); --i >= 0;)
			{
				MultiDocumentPanelWindow* const dw = dynamic_cast <MultiDocumentPanelWindow*> (getChildComponent (i));

				if (dw != 0 && dw->getContentComponent() == component)
				{
					dw->setContentComponent (0, false);
					delete dw;
					break;
				}
			}

			if (shouldDelete)
				delete component;

			components.removeValue (component);

			if (isFullscreenWhenOneDocument() && components.size() == 1)
			{
				for (int i = getNumChildComponents(); --i >= 0;)
				{
					MultiDocumentPanelWindow* const dw = dynamic_cast <MultiDocumentPanelWindow*> (getChildComponent (i));

					if (dw != 0)
					{
						dw->setContentComponent (0, false);
						delete dw;
					}
				}

				addAndMakeVisible (components.getFirst());
			}
		}
		else
		{
			jassert (components.indexOf (component) >= 0);

			if (tabComponent != 0)
			{
				for (int i = tabComponent->getNumTabs(); --i >= 0;)
					if (tabComponent->getTabContentComponent (i) == component)
						tabComponent->removeTab (i);
			}
			else
			{
				removeChildComponent (component);
			}

			if (shouldDelete)
				delete component;

			if (tabComponent != 0 && tabComponent->getNumTabs() <= numDocsBeforeTabsUsed)
				deleteAndZero (tabComponent);

			components.removeValue (component);

			if (components.size() > 0 && tabComponent == 0)
				addAndMakeVisible (components.getFirst());
		}

		resized();
		activeDocumentChanged();
	}
	else
	{
		jassertfalse
	}

	return true;
}

int MultiDocumentPanel::getNumDocuments() const throw()
{
	return components.size();
}

Component* MultiDocumentPanel::getDocument (const int index) const throw()
{
	return components [index];
}

Component* MultiDocumentPanel::getActiveDocument() const throw()
{
	if (mode == FloatingWindows)
	{
		for (int i = getNumChildComponents(); --i >= 0;)
		{
			MultiDocumentPanelWindow* const dw = dynamic_cast <MultiDocumentPanelWindow*> (getChildComponent (i));

			if (dw != 0 && dw->isActiveWindow())
				return dw->getContentComponent();
		}
	}

	return components.getLast();
}

void MultiDocumentPanel::setActiveDocument (Component* component)
{
	if (mode == FloatingWindows)
	{
		component = getContainerComp (component);

		if (component != 0)
			component->toFront (true);
	}
	else if (tabComponent != 0)
	{
		jassert (components.indexOf (component) >= 0);

		for (int i = tabComponent->getNumTabs(); --i >= 0;)
		{
			if (tabComponent->getTabContentComponent (i) == component)
			{
				tabComponent->setCurrentTabIndex (i);
				break;
			}
		}
	}
	else
	{
		component->grabKeyboardFocus();
	}
}

void MultiDocumentPanel::activeDocumentChanged()
{
}

void MultiDocumentPanel::setMaximumNumDocuments (const int newNumber)
{
	maximumNumDocuments = newNumber;
}

void MultiDocumentPanel::useFullscreenWhenOneDocument (const bool shouldUseTabs)
{
	numDocsBeforeTabsUsed = shouldUseTabs ? 1 : 0;
}

bool MultiDocumentPanel::isFullscreenWhenOneDocument() const throw()
{
	return numDocsBeforeTabsUsed != 0;
}

void MultiDocumentPanel::setLayoutMode (const LayoutMode newLayoutMode)
{
	if (mode != newLayoutMode)
	{
		mode = newLayoutMode;

		if (mode == FloatingWindows)
		{
			deleteAndZero (tabComponent);
		}
		else
		{
			for (int i = getNumChildComponents(); --i >= 0;)
			{
				MultiDocumentPanelWindow* const dw = dynamic_cast <MultiDocumentPanelWindow*> (getChildComponent (i));

				if (dw != 0)
				{
					dw->getContentComponent()->getProperties().set ("mdiDocumentPos_", dw->getWindowStateAsString());
					dw->setContentComponent (0, false);
					delete dw;
				}
			}
		}

		resized();

		const Array <Component*> tempComps (components);
		components.clear();

		for (int i = 0; i < tempComps.size(); ++i)
		{
			Component* const c = tempComps.getUnchecked(i);

			addDocument (c,
						 Colour ((int) c->getProperties().getWithDefault ("mdiDocumentBkg_", (int) Colours::white.getARGB())),
						 shouldDeleteComp (c));
		}
	}
}

void MultiDocumentPanel::setBackgroundColour (const Colour& newBackgroundColour)
{
	if (backgroundColour != newBackgroundColour)
	{
		backgroundColour = newBackgroundColour;
		setOpaque (newBackgroundColour.isOpaque());
		repaint();
	}
}

void MultiDocumentPanel::paint (Graphics& g)
{
	g.fillAll (backgroundColour);
}

void MultiDocumentPanel::resized()
{
	if (mode == MaximisedWindowsWithTabs || components.size() == numDocsBeforeTabsUsed)
	{
		for (int i = getNumChildComponents(); --i >= 0;)
			getChildComponent (i)->setBounds (0, 0, getWidth(), getHeight());
	}

	setWantsKeyboardFocus (components.size() == 0);
}

Component* MultiDocumentPanel::getContainerComp (Component* c) const
{
	if (mode == FloatingWindows)
	{
		for (int i = 0; i < getNumChildComponents(); ++i)
		{
			MultiDocumentPanelWindow* const dw = dynamic_cast <MultiDocumentPanelWindow*> (getChildComponent (i));

			if (dw != 0 && dw->getContentComponent() == c)
			{
				c = dw;
				break;
			}
		}
	}

	return c;
}

void MultiDocumentPanel::componentNameChanged (Component&)
{
	if (mode == FloatingWindows)
	{
		for (int i = 0; i < getNumChildComponents(); ++i)
		{
			MultiDocumentPanelWindow* const dw = dynamic_cast <MultiDocumentPanelWindow*> (getChildComponent (i));

			if (dw != 0)
				dw->setName (dw->getContentComponent()->getName());
		}
	}
	else if (tabComponent != 0)
	{
		for (int i = tabComponent->getNumTabs(); --i >= 0;)
			tabComponent->setTabName (i, tabComponent->getTabContentComponent (i)->getName());
	}
}

void MultiDocumentPanel::updateOrder()
{
	const Array <Component*> oldList (components);

	if (mode == FloatingWindows)
	{
		components.clear();

		for (int i = 0; i < getNumChildComponents(); ++i)
		{
			MultiDocumentPanelWindow* const dw = dynamic_cast <MultiDocumentPanelWindow*> (getChildComponent (i));

			if (dw != 0)
				components.add (dw->getContentComponent());
		}
	}
	else
	{
		if (tabComponent != 0)
		{
			Component* const current = tabComponent->getCurrentContentComponent();

			if (current != 0)
			{
				components.removeValue (current);
				components.add (current);
			}
		}
	}

	if (components != oldList)
		activeDocumentChanged();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MultiDocumentPanel.cpp ***/


/*** Start of inlined file: juce_ResizableBorderComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

ResizableBorderComponent::Zone::Zone (int zoneFlags) throw()
	: zone (zoneFlags)
{
}

ResizableBorderComponent::Zone::Zone (const ResizableBorderComponent::Zone& other) throw() : zone (other.zone)	{}
ResizableBorderComponent::Zone& ResizableBorderComponent::Zone::operator= (const ResizableBorderComponent::Zone& other) throw()		   { zone = other.zone; return *this; }
bool ResizableBorderComponent::Zone::operator== (const ResizableBorderComponent::Zone& other) const throw()	   { return zone == other.zone; }
bool ResizableBorderComponent::Zone::operator!= (const ResizableBorderComponent::Zone& other) const throw()	   { return zone != other.zone; }

const ResizableBorderComponent::Zone ResizableBorderComponent::Zone::fromPositionOnBorder (const Rectangle<int>& totalSize,
																						   const BorderSize& border,
																						   const Point<int>& position)
{
	int z = 0;

	if (totalSize.contains (position)
		 && ! border.subtractedFrom (totalSize).contains (position))
	{
		const int minW = jmax (totalSize.getWidth() / 10, jmin (10, totalSize.getWidth() / 3));
		if (position.getX() < jmax (border.getLeft(), minW))
			z |= left;
		else if (position.getX() >= totalSize.getWidth() - jmax (border.getRight(), minW))
			z |= right;

		const int minH = jmax (totalSize.getHeight() / 10, jmin (10, totalSize.getHeight() / 3));
		if (position.getY() < jmax (border.getTop(), minH))
			z |= top;
		else if (position.getY() >= totalSize.getHeight() - jmax (border.getBottom(), minH))
			z |= bottom;
	}

	return Zone (z);
}

const MouseCursor ResizableBorderComponent::Zone::getMouseCursor() const throw()
{
	MouseCursor::StandardCursorType mc = MouseCursor::NormalCursor;

	switch (zone)
	{
		case (left | top):	  mc = MouseCursor::TopLeftCornerResizeCursor; break;
		case top:		   mc = MouseCursor::TopEdgeResizeCursor; break;
		case (right | top):	 mc = MouseCursor::TopRightCornerResizeCursor; break;
		case left:		  mc = MouseCursor::LeftEdgeResizeCursor; break;
		case right:		 mc = MouseCursor::RightEdgeResizeCursor; break;
		case (left | bottom):   mc = MouseCursor::BottomLeftCornerResizeCursor; break;
		case bottom:		mc = MouseCursor::BottomEdgeResizeCursor; break;
		case (right | bottom):  mc = MouseCursor::BottomRightCornerResizeCursor; break;
		default:		break;
	}

	return mc;
}

const Rectangle<int> ResizableBorderComponent::Zone::resizeRectangleBy (Rectangle<int> b, const Point<int>& offset) const throw()
{
	if (isDraggingWholeObject())
		return b + offset;

	if (isDraggingLeftEdge())
		b.setLeft (b.getX() + offset.getX());

	if (isDraggingRightEdge())
		b.setWidth (jmax (0, b.getWidth() + offset.getX()));

	if (isDraggingTopEdge())
		b.setTop (b.getY() + offset.getY());

	if (isDraggingBottomEdge())
		b.setHeight (jmax (0, b.getHeight() + offset.getY()));

	return b;
}

ResizableBorderComponent::ResizableBorderComponent (Component* const componentToResize,
													ComponentBoundsConstrainer* const constrainer_)
   : component (componentToResize),
	 constrainer (constrainer_),
	 borderSize (5),
	 mouseZone (0)
{
}

ResizableBorderComponent::~ResizableBorderComponent()
{
}

void ResizableBorderComponent::paint (Graphics& g)
{
	getLookAndFeel().drawResizableFrame (g, getWidth(), getHeight(), borderSize);
}

void ResizableBorderComponent::mouseEnter (const MouseEvent& e)
{
	updateMouseZone (e);
}

void ResizableBorderComponent::mouseMove (const MouseEvent& e)
{
	updateMouseZone (e);
}

void ResizableBorderComponent::mouseDown (const MouseEvent& e)
{
	if (component == 0)
	{
		jassertfalse // You've deleted the component that this resizer was supposed to be using!
		return;
	}

	updateMouseZone (e);

	originalBounds = component->getBounds();

	if (constrainer != 0)
		constrainer->resizeStart();
}

void ResizableBorderComponent::mouseDrag (const MouseEvent& e)
{
	if (component == 0)
	{
		jassertfalse // You've deleted the component that this resizer was supposed to be using!
		return;
	}

	const Rectangle<int> bounds (mouseZone.resizeRectangleBy (originalBounds, e.getOffsetFromDragStart()));

	if (constrainer != 0)
		constrainer->setBoundsForComponent (component, bounds,
											mouseZone.isDraggingTopEdge(),
											mouseZone.isDraggingLeftEdge(),
											mouseZone.isDraggingBottomEdge(),
											mouseZone.isDraggingRightEdge());
	else
		component->setBounds (bounds);
}

void ResizableBorderComponent::mouseUp (const MouseEvent&)
{
	if (constrainer != 0)
		constrainer->resizeEnd();
}

bool ResizableBorderComponent::hitTest (int x, int y)
{
	return x < borderSize.getLeft()
			|| x >= getWidth() - borderSize.getRight()
			|| y < borderSize.getTop()
			|| y >= getHeight() - borderSize.getBottom();
}

void ResizableBorderComponent::setBorderThickness (const BorderSize& newBorderSize)
{
	if (borderSize != newBorderSize)
	{
		borderSize = newBorderSize;
		repaint();
	}
}

const BorderSize ResizableBorderComponent::getBorderThickness() const
{
	return borderSize;
}

void ResizableBorderComponent::updateMouseZone (const MouseEvent& e)
{
	Zone newZone (Zone::fromPositionOnBorder (getLocalBounds(), borderSize, e.getPosition()));

	if (mouseZone != newZone)
	{
		mouseZone = newZone;
		setMouseCursor (newZone.getMouseCursor());
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ResizableBorderComponent.cpp ***/


/*** Start of inlined file: juce_ResizableCornerComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

ResizableCornerComponent::ResizableCornerComponent (Component* const componentToResize,
													ComponentBoundsConstrainer* const constrainer_)
   : component (componentToResize),
	 constrainer (constrainer_)
{
	setRepaintsOnMouseActivity (true);
	setMouseCursor (MouseCursor::BottomRightCornerResizeCursor);
}

ResizableCornerComponent::~ResizableCornerComponent()
{
}

void ResizableCornerComponent::paint (Graphics& g)
{
	getLookAndFeel()
		.drawCornerResizer (g, getWidth(), getHeight(),
							isMouseOverOrDragging(),
							isMouseButtonDown());
}

void ResizableCornerComponent::mouseDown (const MouseEvent&)
{
	if (component == 0)
	{
		jassertfalse; // You've deleted the component that this resizer is supposed to be controlling!
		return;
	}

	originalBounds = component->getBounds();

	if (constrainer != 0)
		constrainer->resizeStart();
}

void ResizableCornerComponent::mouseDrag (const MouseEvent& e)
{
	if (component == 0)
	{
		jassertfalse; // You've deleted the component that this resizer is supposed to be controlling!
		return;
	}

	Rectangle<int> r (originalBounds.withSize (originalBounds.getWidth() + e.getDistanceFromDragStartX(),
											   originalBounds.getHeight() + e.getDistanceFromDragStartY()));

	if (constrainer != 0)
		constrainer->setBoundsForComponent (component, r, false, false, true, true);
	else
		component->setBounds (r);
}

void ResizableCornerComponent::mouseUp (const MouseEvent&)
{
	if (constrainer != 0)
		constrainer->resizeStart();
}

bool ResizableCornerComponent::hitTest (int x, int y)
{
	if (getWidth() <= 0)
		return false;

	const int yAtX = getHeight() - (getHeight() * x / getWidth());

	return y >= yAtX - getHeight() / 4;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ResizableCornerComponent.cpp ***/


/*** Start of inlined file: juce_ScrollBar.cpp ***/
BEGIN_JUCE_NAMESPACE

class ScrollBar::ScrollbarButton  : public Button
{
public:
	int direction;

	ScrollbarButton (const int direction_, ScrollBar& owner_)
		: Button (String::empty),
		  direction (direction_),
		  owner (owner_)
	{
		setWantsKeyboardFocus (false);
	}

	~ScrollbarButton()
	{
	}

	void paintButton (Graphics& g, bool isMouseOver, bool isMouseDown)
	{
		getLookAndFeel()
			.drawScrollbarButton (g, owner,
								  getWidth(), getHeight(),
								  direction,
								  owner.isVertical(),
								  isMouseOver, isMouseDown);
	}

	void clicked()
	{
		owner.moveScrollbarInSteps ((direction == 1 || direction == 2) ? 1 : -1);
	}

	juce_UseDebuggingNewOperator

private:
	ScrollBar& owner;

	ScrollbarButton (const ScrollbarButton&);
	ScrollbarButton& operator= (const ScrollbarButton&);
};

ScrollBar::ScrollBar (const bool vertical_,
					  const bool buttonsAreVisible)
	: totalRange (0.0, 1.0),
	  visibleRange (0.0, 0.1),
	  singleStepSize (0.1),
	  thumbAreaStart (0),
	  thumbAreaSize (0),
	  thumbStart (0),
	  thumbSize (0),
	  initialDelayInMillisecs (100),
	  repeatDelayInMillisecs (50),
	  minimumDelayInMillisecs (10),
	  vertical (vertical_),
	  isDraggingThumb (false),
	  alwaysVisible (false),
	  upButton (0),
	  downButton (0)
{
	setButtonVisibility (buttonsAreVisible);

	setRepaintsOnMouseActivity (true);
	setFocusContainer (true);
}

ScrollBar::~ScrollBar()
{
	deleteAllChildren();
}

void ScrollBar::setRangeLimits (const Range<double>& newRangeLimit)
{
	if (totalRange != newRangeLimit)
	{
		totalRange = newRangeLimit;
		setCurrentRange (visibleRange);
		updateThumbPosition();
	}
}

void ScrollBar::setRangeLimits (const double newMinimum, const double newMaximum)
{
	jassert (newMaximum >= newMinimum); // these can't be the wrong way round!
	setRangeLimits (Range<double> (newMinimum, newMaximum));
}

void ScrollBar::setCurrentRange (const Range<double>& newRange)
{
	const Range<double> constrainedRange (totalRange.constrainRange (newRange));

	if (visibleRange != constrainedRange)
	{
		visibleRange = constrainedRange;

		updateThumbPosition();
		triggerAsyncUpdate();
	}
}

void ScrollBar::setCurrentRange (const double newStart, const double newSize)
{
	setCurrentRange (Range<double> (newStart, newStart + newSize));
}

void ScrollBar::setCurrentRangeStart (const double newStart)
{
	setCurrentRange (visibleRange.movedToStartAt (newStart));
}

void ScrollBar::setSingleStepSize (const double newSingleStepSize)
{
	singleStepSize = newSingleStepSize;
}

void ScrollBar::moveScrollbarInSteps (const int howManySteps)
{
	setCurrentRange (visibleRange + howManySteps * singleStepSize);
}

void ScrollBar::moveScrollbarInPages (const int howManyPages)
{
	setCurrentRange (visibleRange + howManyPages * visibleRange.getLength());
}

void ScrollBar::scrollToTop()
{
	setCurrentRange (visibleRange.movedToStartAt (getMinimumRangeLimit()));
}

void ScrollBar::scrollToBottom()
{
	setCurrentRange (visibleRange.movedToEndAt (getMaximumRangeLimit()));
}

void ScrollBar::setButtonRepeatSpeed (const int initialDelayInMillisecs_,
									  const int repeatDelayInMillisecs_,
									  const int minimumDelayInMillisecs_)
{
	initialDelayInMillisecs = initialDelayInMillisecs_;
	repeatDelayInMillisecs = repeatDelayInMillisecs_;
	minimumDelayInMillisecs = minimumDelayInMillisecs_;

	if (upButton != 0)
	{
		upButton->setRepeatSpeed (initialDelayInMillisecs,  repeatDelayInMillisecs,  minimumDelayInMillisecs);
		downButton->setRepeatSpeed (initialDelayInMillisecs,  repeatDelayInMillisecs,  minimumDelayInMillisecs);
	}
}

void ScrollBar::addListener (ScrollBarListener* const listener)
{
	listeners.add (listener);
}

void ScrollBar::removeListener (ScrollBarListener* const listener)
{
	listeners.remove (listener);
}

void ScrollBar::handleAsyncUpdate()
{
	double start = visibleRange.getStart(); // (need to use a temp variable for VC7 compatibility)
	listeners.call (&ScrollBarListener::scrollBarMoved, this, start);
}

void ScrollBar::updateThumbPosition()
{
	int newThumbSize = roundToInt (totalRange.getLength() > 0 ? (visibleRange.getLength() * thumbAreaSize) / totalRange.getLength()
															  : thumbAreaSize);

	if (newThumbSize < getLookAndFeel().getMinimumScrollbarThumbSize (*this))
		newThumbSize = jmin (getLookAndFeel().getMinimumScrollbarThumbSize (*this), thumbAreaSize - 1);

	if (newThumbSize > thumbAreaSize)
		newThumbSize = thumbAreaSize;

	int newThumbStart = thumbAreaStart;

	if (totalRange.getLength() > visibleRange.getLength())
		newThumbStart += roundToInt (((visibleRange.getStart() - totalRange.getStart()) * (thumbAreaSize - newThumbSize))
										 / (totalRange.getLength() - visibleRange.getLength()));

	setVisible (alwaysVisible || (totalRange.getLength() > visibleRange.getLength() && visibleRange.getLength() > 0.0));

	if (thumbStart != newThumbStart  || thumbSize != newThumbSize)
	{
		const int repaintStart = jmin (thumbStart, newThumbStart) - 4;
		const int repaintSize = jmax (thumbStart + thumbSize, newThumbStart + newThumbSize) + 8 - repaintStart;

		if (vertical)
			repaint (0, repaintStart, getWidth(), repaintSize);
		else
			repaint (repaintStart, 0, repaintSize, getHeight());

		thumbStart = newThumbStart;
		thumbSize = newThumbSize;
	}
}

void ScrollBar::setOrientation (const bool shouldBeVertical)
{
	if (vertical != shouldBeVertical)
	{
		vertical = shouldBeVertical;

		if (upButton != 0)
		{
			upButton->direction	= vertical ? 0 : 3;
			downButton->direction  = vertical ? 2 : 1;
		}

		updateThumbPosition();
	}
}

void ScrollBar::setButtonVisibility (const bool buttonsAreVisible)
{
	delete upButton;
	upButton = 0;
	delete downButton;
	downButton = 0;

	if (buttonsAreVisible)
	{
		addAndMakeVisible (upButton   = new ScrollbarButton (vertical ? 0 : 3, *this));
		addAndMakeVisible (downButton = new ScrollbarButton (vertical ? 2 : 1, *this));

		setButtonRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
	}

	updateThumbPosition();
}

void ScrollBar::setAutoHide (const bool shouldHideWhenFullRange)
{
	alwaysVisible = ! shouldHideWhenFullRange;
	updateThumbPosition();
}

void ScrollBar::paint (Graphics& g)
{
	if (thumbAreaSize > 0)
	{
		LookAndFeel& lf = getLookAndFeel();

		const int thumb = (thumbAreaSize > lf.getMinimumScrollbarThumbSize (*this))
								? thumbSize : 0;

		if (vertical)
		{
			lf.drawScrollbar (g, *this,
							  0, thumbAreaStart,
							  getWidth(), thumbAreaSize,
							  vertical,
							  thumbStart, thumb,
							  isMouseOver(), isMouseButtonDown());
		}
		else
		{
			lf.drawScrollbar (g, *this,
							  thumbAreaStart, 0,
							  thumbAreaSize, getHeight(),
							  vertical,
							  thumbStart, thumb,
							  isMouseOver(), isMouseButtonDown());
		}
	}
}

void ScrollBar::lookAndFeelChanged()
{
	setComponentEffect (getLookAndFeel().getScrollbarEffect());
}

void ScrollBar::resized()
{
	const int length = ((vertical) ? getHeight() : getWidth());

	const int buttonSize = (upButton != 0) ? jmin (getLookAndFeel().getScrollbarButtonSize (*this), (length >> 1))
										   : 0;

	if (length < 32 + getLookAndFeel().getMinimumScrollbarThumbSize (*this))
	{
		thumbAreaStart = length >> 1;
		thumbAreaSize = 0;
	}
	else
	{
		thumbAreaStart = buttonSize;
		thumbAreaSize = length - (buttonSize << 1);
	}

	if (upButton != 0)
	{
		if (vertical)
		{
			upButton->setBounds (0, 0, getWidth(), buttonSize);
			downButton->setBounds (0, thumbAreaStart + thumbAreaSize, getWidth(), buttonSize);
		}
		else
		{
			upButton->setBounds (0, 0, buttonSize, getHeight());
			downButton->setBounds (thumbAreaStart + thumbAreaSize, 0, buttonSize, getHeight());
		}
	}

	updateThumbPosition();
}

void ScrollBar::mouseDown (const MouseEvent& e)
{
	isDraggingThumb = false;
	lastMousePos = vertical ? e.y : e.x;
	dragStartMousePos = lastMousePos;
	dragStartRange = visibleRange.getStart();

	if (dragStartMousePos < thumbStart)
	{
		moveScrollbarInPages (-1);
		startTimer (400);
	}
	else if (dragStartMousePos >= thumbStart + thumbSize)
	{
		moveScrollbarInPages (1);
		startTimer (400);
	}
	else
	{
		isDraggingThumb = (thumbAreaSize > getLookAndFeel().getMinimumScrollbarThumbSize (*this))
							&& (thumbAreaSize > thumbSize);
	}
}

void ScrollBar::mouseDrag (const MouseEvent& e)
{
	if (isDraggingThumb)
	{
		const int deltaPixels = ((vertical) ? e.y : e.x) - dragStartMousePos;

		setCurrentRangeStart (dragStartRange
								+ deltaPixels * (totalRange.getLength() - visibleRange.getLength())
									/ (thumbAreaSize - thumbSize));
	}
	else
	{
		lastMousePos = (vertical) ? e.y : e.x;
	}
}

void ScrollBar::mouseUp (const MouseEvent&)
{
	isDraggingThumb = false;
	stopTimer();
	repaint();
}

void ScrollBar::mouseWheelMove (const MouseEvent&,
								float wheelIncrementX,
								float wheelIncrementY)
{
	float increment = vertical ? wheelIncrementY : wheelIncrementX;

	if (increment < 0)
		increment = jmin (increment * 10.0f, -1.0f);
	else if (increment > 0)
		increment = jmax (increment * 10.0f, 1.0f);

	setCurrentRange (visibleRange - singleStepSize * increment);
}

void ScrollBar::timerCallback()
{
	if (isMouseButtonDown())
	{
		startTimer (40);

		if (lastMousePos < thumbStart)
			setCurrentRange (visibleRange - visibleRange.getLength());
		else if (lastMousePos > thumbStart + thumbSize)
			setCurrentRangeStart (visibleRange.getEnd());
	}
	else
	{
		stopTimer();
	}
}

bool ScrollBar::keyPressed (const KeyPress& key)
{
	if (! isVisible())
		return false;

	if (key.isKeyCode (KeyPress::upKey) || key.isKeyCode (KeyPress::leftKey))
		moveScrollbarInSteps (-1);
	else if (key.isKeyCode (KeyPress::downKey) || key.isKeyCode (KeyPress::rightKey))
		moveScrollbarInSteps (1);
	else if (key.isKeyCode (KeyPress::pageUpKey))
		moveScrollbarInPages (-1);
	else if (key.isKeyCode (KeyPress::pageDownKey))
		moveScrollbarInPages (1);
	else if (key.isKeyCode (KeyPress::homeKey))
		scrollToTop();
	else if (key.isKeyCode (KeyPress::endKey))
		scrollToBottom();
	else
		return false;

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ScrollBar.cpp ***/


/*** Start of inlined file: juce_StretchableLayoutManager.cpp ***/
BEGIN_JUCE_NAMESPACE

StretchableLayoutManager::StretchableLayoutManager()
	: totalSize (0)
{
}

StretchableLayoutManager::~StretchableLayoutManager()
{
}

void StretchableLayoutManager::clearAllItems()
{
	items.clear();
	totalSize = 0;
}

void StretchableLayoutManager::setItemLayout (const int itemIndex,
											  const double minimumSize,
											  const double maximumSize,
											  const double preferredSize)
{
	ItemLayoutProperties* layout = getInfoFor (itemIndex);

	if (layout == 0)
	{
		layout = new ItemLayoutProperties();
		layout->itemIndex = itemIndex;

		int i;
		for (i = 0; i < items.size(); ++i)
			if (items.getUnchecked (i)->itemIndex > itemIndex)
				break;

		items.insert (i, layout);
	}

	layout->minSize = minimumSize;
	layout->maxSize = maximumSize;
	layout->preferredSize = preferredSize;
	layout->currentSize = 0;
}

bool StretchableLayoutManager::getItemLayout (const int itemIndex,
											  double& minimumSize,
											  double& maximumSize,
											  double& preferredSize) const
{
	const ItemLayoutProperties* const layout = getInfoFor (itemIndex);

	if (layout != 0)
	{
		minimumSize = layout->minSize;
		maximumSize = layout->maxSize;
		preferredSize = layout->preferredSize;
		return true;
	}

	return false;
}

void StretchableLayoutManager::setTotalSize (const int newTotalSize)
{
	totalSize = newTotalSize;

	fitComponentsIntoSpace (0, items.size(), totalSize, 0);
}

int StretchableLayoutManager::getItemCurrentPosition (const int itemIndex) const
{
	int pos = 0;

	for (int i = 0; i < itemIndex; ++i)
	{
		const ItemLayoutProperties* const layout = getInfoFor (i);

		if (layout != 0)
			pos += layout->currentSize;
	}

	return pos;
}

int StretchableLayoutManager::getItemCurrentAbsoluteSize (const int itemIndex) const
{
	const ItemLayoutProperties* const layout = getInfoFor (itemIndex);

	if (layout != 0)
		return layout->currentSize;

	return 0;
}

double StretchableLayoutManager::getItemCurrentRelativeSize (const int itemIndex) const
{
	const ItemLayoutProperties* const layout = getInfoFor (itemIndex);

	if (layout != 0)
		return -layout->currentSize / (double) totalSize;

	return 0;
}

void StretchableLayoutManager::setItemPosition (const int itemIndex,
												int newPosition)
{
	for (int i = items.size(); --i >= 0;)
	{
		const ItemLayoutProperties* const layout = items.getUnchecked(i);

		if (layout->itemIndex == itemIndex)
		{
			int realTotalSize = jmax (totalSize, getMinimumSizeOfItems (0, items.size()));
			const int minSizeAfterThisComp = getMinimumSizeOfItems (i, items.size());
			const int maxSizeAfterThisComp = getMaximumSizeOfItems (i + 1, items.size());

			newPosition = jmax (newPosition, totalSize - maxSizeAfterThisComp - layout->currentSize);
			newPosition = jmin (newPosition, realTotalSize - minSizeAfterThisComp);

			int endPos = fitComponentsIntoSpace (0, i, newPosition, 0);

			endPos += layout->currentSize;

			fitComponentsIntoSpace (i + 1, items.size(), totalSize - endPos, endPos);
			updatePrefSizesToMatchCurrentPositions();
			break;
		}
	}
}

void StretchableLayoutManager::layOutComponents (Component** const components,
												 int numComponents,
												 int x, int y, int w, int h,
												 const bool vertically,
												 const bool resizeOtherDimension)
{
	setTotalSize (vertically ? h : w);
	int pos = vertically ? y : x;

	for (int i = 0; i < numComponents; ++i)
	{
		const ItemLayoutProperties* const layout = getInfoFor (i);

		if (layout != 0)
		{
			Component* const c = components[i];

			if (c != 0)
			{
				if (i == numComponents - 1)
				{
					// if it's the last item, crop it to exactly fit the available space..
					if (resizeOtherDimension)
					{
						if (vertically)
							c->setBounds (x, pos, w, jmax (layout->currentSize, h - pos));
						else
							c->setBounds (pos, y, jmax (layout->currentSize, w - pos), h);
					}
					else
					{
						if (vertically)
							c->setBounds (c->getX(), pos, c->getWidth(), jmax (layout->currentSize, h - pos));
						else
							c->setBounds (pos, c->getY(), jmax (layout->currentSize, w - pos), c->getHeight());
					}
				}
				else
				{
					if (resizeOtherDimension)
					{
						if (vertically)
							c->setBounds (x, pos, w, layout->currentSize);
						else
							c->setBounds (pos, y, layout->currentSize, h);
					}
					else
					{
						if (vertically)
							c->setBounds (c->getX(), pos, c->getWidth(), layout->currentSize);
						else
							c->setBounds (pos, c->getY(), layout->currentSize, c->getHeight());
					}
				}
			}

			pos += layout->currentSize;
		}
	}
}

StretchableLayoutManager::ItemLayoutProperties* StretchableLayoutManager::getInfoFor (const int itemIndex) const
{
	for (int i = items.size(); --i >= 0;)
		if (items.getUnchecked(i)->itemIndex == itemIndex)
			return items.getUnchecked(i);

	return 0;
}

int StretchableLayoutManager::fitComponentsIntoSpace (const int startIndex,
													  const int endIndex,
													  const int availableSpace,
													  int startPos)
{
	// calculate the total sizes
	int i;
	double totalIdealSize = 0.0;
	int totalMinimums = 0;

	for (i = startIndex; i < endIndex; ++i)
	{
		ItemLayoutProperties* const layout = items.getUnchecked (i);

		layout->currentSize = sizeToRealSize (layout->minSize, totalSize);

		totalMinimums += layout->currentSize;
		totalIdealSize += sizeToRealSize (layout->preferredSize, availableSpace);
	}

	if (totalIdealSize <= 0)
		totalIdealSize = 1.0;

	// now calc the best sizes..
	int extraSpace = availableSpace - totalMinimums;

	while (extraSpace > 0)
	{
		int numWantingMoreSpace = 0;
		int numHavingTakenExtraSpace = 0;

		// first figure out how many comps want a slice of the extra space..
		for (i = startIndex; i < endIndex; ++i)
		{
			ItemLayoutProperties* const layout = items.getUnchecked (i);

			double sizeWanted = sizeToRealSize (layout->preferredSize, availableSpace);

			const int bestSize = jlimit (layout->currentSize,
										 jmax (layout->currentSize,
											   sizeToRealSize (layout->maxSize, totalSize)),
										 roundToInt (sizeWanted * availableSpace / totalIdealSize));

			if (bestSize > layout->currentSize)
				++numWantingMoreSpace;
		}

		// ..share out the extra space..
		for (i = startIndex; i < endIndex; ++i)
		{
			ItemLayoutProperties* const layout = items.getUnchecked (i);

			double sizeWanted = sizeToRealSize (layout->preferredSize, availableSpace);

			int bestSize = jlimit (layout->currentSize,
								   jmax (layout->currentSize, sizeToRealSize (layout->maxSize, totalSize)),
								   roundToInt (sizeWanted * availableSpace / totalIdealSize));

			const int extraWanted = bestSize - layout->currentSize;

			if (extraWanted > 0)
			{
				const int extraAllowed = jmin (extraWanted,
												extraSpace / jmax (1, numWantingMoreSpace));

				if (extraAllowed > 0)
				{
					++numHavingTakenExtraSpace;
					--numWantingMoreSpace;

					layout->currentSize += extraAllowed;
					extraSpace -= extraAllowed;
				}
			}
		}

		if (numHavingTakenExtraSpace <= 0)
			break;
	}

	// ..and calculate the end position
	for (i = startIndex; i < endIndex; ++i)
	{
		ItemLayoutProperties* const layout = items.getUnchecked(i);
		startPos += layout->currentSize;
	}

	return startPos;
}

int StretchableLayoutManager::getMinimumSizeOfItems (const int startIndex,
													 const int endIndex) const
{
	int totalMinimums = 0;

	for (int i = startIndex; i < endIndex; ++i)
		totalMinimums += sizeToRealSize (items.getUnchecked (i)->minSize, totalSize);

	return totalMinimums;
}

int StretchableLayoutManager::getMaximumSizeOfItems (const int startIndex, const int endIndex) const
{
	int totalMaximums = 0;

	for (int i = startIndex; i < endIndex; ++i)
		totalMaximums += sizeToRealSize (items.getUnchecked (i)->maxSize, totalSize);

	return totalMaximums;
}

void StretchableLayoutManager::updatePrefSizesToMatchCurrentPositions()
{
	for (int i = 0; i < items.size(); ++i)
	{
		ItemLayoutProperties* const layout = items.getUnchecked (i);

		layout->preferredSize
			= (layout->preferredSize < 0) ? getItemCurrentRelativeSize (i)
										  : getItemCurrentAbsoluteSize (i);
	}
}

int StretchableLayoutManager::sizeToRealSize (double size, int totalSpace)
{
	if (size < 0)
		size *= -totalSpace;

	return roundToInt (size);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_StretchableLayoutManager.cpp ***/


/*** Start of inlined file: juce_StretchableLayoutResizerBar.cpp ***/
BEGIN_JUCE_NAMESPACE

StretchableLayoutResizerBar::StretchableLayoutResizerBar (StretchableLayoutManager* layout_,
														  const int itemIndex_,
														  const bool isVertical_)
	: layout (layout_),
	  itemIndex (itemIndex_),
	  isVertical (isVertical_)
{
	setRepaintsOnMouseActivity (true);
	setMouseCursor (MouseCursor (isVertical_ ? MouseCursor::LeftRightResizeCursor
												: MouseCursor::UpDownResizeCursor));
}

StretchableLayoutResizerBar::~StretchableLayoutResizerBar()
{
}

void StretchableLayoutResizerBar::paint (Graphics& g)
{
	getLookAndFeel().drawStretchableLayoutResizerBar (g,
													  getWidth(), getHeight(),
													  isVertical,
													  isMouseOver(),
													  isMouseButtonDown());
}

void StretchableLayoutResizerBar::mouseDown (const MouseEvent&)
{
	mouseDownPos = layout->getItemCurrentPosition (itemIndex);
}

void StretchableLayoutResizerBar::mouseDrag (const MouseEvent& e)
{
	const int desiredPos = mouseDownPos + (isVertical ? e.getDistanceFromDragStartX()
													  : e.getDistanceFromDragStartY());

	layout->setItemPosition (itemIndex, desiredPos);

	hasBeenMoved();
}

void StretchableLayoutResizerBar::hasBeenMoved()
{
	if (getParentComponent() != 0)
		getParentComponent()->resized();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_StretchableLayoutResizerBar.cpp ***/


/*** Start of inlined file: juce_StretchableObjectResizer.cpp ***/
BEGIN_JUCE_NAMESPACE

StretchableObjectResizer::StretchableObjectResizer()
{
}

StretchableObjectResizer::~StretchableObjectResizer()
{
}

void StretchableObjectResizer::addItem (const double size,
										const double minSize, const double maxSize,
										const int order)
{
	// the order must be >= 0 but less than the maximum integer value.
	jassert (order >= 0 && order < std::numeric_limits<int>::max());

	Item* const item = new Item();
	item->size = size;
	item->minSize = minSize;
	item->maxSize = maxSize;
	item->order = order;
	items.add (item);
}

double StretchableObjectResizer::getItemSize (const int index) const throw()
{
	const Item* const it = items [index];
	return it != 0 ? it->size : 0;
}

void StretchableObjectResizer::resizeToFit (const double targetSize)
{
	int order = 0;

	for (;;)
	{
		double currentSize = 0;
		double minSize = 0;
		double maxSize = 0;

		int nextHighestOrder = std::numeric_limits<int>::max();

		for (int i = 0; i < items.size(); ++i)
		{
			const Item* const it = items.getUnchecked(i);
			currentSize += it->size;

			if (it->order <= order)
			{
				minSize += it->minSize;
				maxSize += it->maxSize;
			}
			else
			{
				minSize += it->size;
				maxSize += it->size;
				nextHighestOrder = jmin (nextHighestOrder, it->order);
			}
		}

		const double thisIterationTarget = jlimit (minSize, maxSize, targetSize);

		if (thisIterationTarget >= currentSize)
		{
			const double availableExtraSpace = maxSize - currentSize;
			const double targetAmountOfExtraSpace = thisIterationTarget - currentSize;
			const double scale = targetAmountOfExtraSpace / availableExtraSpace;

			for (int i = 0; i < items.size(); ++i)
			{
				Item* const it = items.getUnchecked(i);

				if (it->order <= order)
					it->size = jmin (it->maxSize, it->size + (it->maxSize - it->size) * scale);
			}
		}
		else
		{
			const double amountOfSlack = currentSize - minSize;
			const double targetAmountOfSlack = thisIterationTarget - minSize;
			const double scale = targetAmountOfSlack / amountOfSlack;

			for (int i = 0; i < items.size(); ++i)
			{
				Item* const it = items.getUnchecked(i);

				if (it->order <= order)
					it->size = jmax (it->minSize, it->minSize + (it->size - it->minSize) * scale);
			}
		}

		if (nextHighestOrder < std::numeric_limits<int>::max())
			order = nextHighestOrder;
		else
			break;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_StretchableObjectResizer.cpp ***/


/*** Start of inlined file: juce_TabbedButtonBar.cpp ***/
BEGIN_JUCE_NAMESPACE

TabBarButton::TabBarButton (const String& name,
							TabbedButtonBar* const owner_,
							const int index)
	: Button (name),
	  owner (owner_),
	  tabIndex (index),
	  overlapPixels (0)
{
	shadow.setShadowProperties (2.2f, 0.7f, 0, 0);
	setComponentEffect (&shadow);
	setWantsKeyboardFocus (false);
}

TabBarButton::~TabBarButton()
{
}

void TabBarButton::paintButton (Graphics& g,
								bool isMouseOverButton,
								bool isButtonDown)
{
	int x, y, w, h;
	getActiveArea (x, y, w, h);

	g.setOrigin (x, y);

	getLookAndFeel()
		.drawTabButton (g, w, h,
						owner->getTabBackgroundColour (tabIndex),
						tabIndex, getButtonText(), *this,
						owner->getOrientation(),
						isMouseOverButton, isButtonDown,
						getToggleState());
}

void TabBarButton::clicked (const ModifierKeys& mods)
{
	if (mods.isPopupMenu())
		owner->popupMenuClickOnTab (tabIndex, getButtonText());
	else
		owner->setCurrentTabIndex (tabIndex);
}

bool TabBarButton::hitTest (int mx, int my)
{
	int x, y, w, h;
	getActiveArea (x, y, w, h);

	if (owner->getOrientation() == TabbedButtonBar::TabsAtLeft
		 || owner->getOrientation() == TabbedButtonBar::TabsAtRight)
	{
		if (((unsigned int) mx) < (unsigned int) getWidth()
			 && my >= y + overlapPixels
			 && my < y + h - overlapPixels)
			return true;
	}
	else
	{
		if (mx >= x + overlapPixels && mx < x + w - overlapPixels
			 && ((unsigned int) my) < (unsigned int) getHeight())
			return true;
	}

	Path p;
	getLookAndFeel()
		.createTabButtonShape (p, w, h, tabIndex, getButtonText(), *this,
							   owner->getOrientation(),
							   false, false, getToggleState());

	return p.contains ((float) (mx - x),
					   (float) (my - y));
}

int TabBarButton::getBestTabLength (const int depth)
{
	return jlimit (depth * 2,
				   depth * 7,
				   getLookAndFeel().getTabButtonBestWidth (tabIndex, getButtonText(), depth, *this));
}

void TabBarButton::getActiveArea (int& x, int& y, int& w, int& h)
{
	x = 0;
	y = 0;
	int r = getWidth();
	int b = getHeight();

	const int spaceAroundImage = getLookAndFeel().getTabButtonSpaceAroundImage();

	if (owner->getOrientation() != TabbedButtonBar::TabsAtLeft)
		r -= spaceAroundImage;

	if (owner->getOrientation() != TabbedButtonBar::TabsAtRight)
		x += spaceAroundImage;

	if (owner->getOrientation() != TabbedButtonBar::TabsAtBottom)
		y += spaceAroundImage;

	if (owner->getOrientation() != TabbedButtonBar::TabsAtTop)
		b -= spaceAroundImage;

	w = r - x;
	h = b - y;
}

class TabAreaBehindFrontButtonComponent  : public Component
{
public:
	TabAreaBehindFrontButtonComponent (TabbedButtonBar* const owner_)
		: owner (owner_)
	{
		setInterceptsMouseClicks (false, false);
	}

	~TabAreaBehindFrontButtonComponent()
	{
	}

	void paint (Graphics& g)
	{
		getLookAndFeel()
			.drawTabAreaBehindFrontButton (g, getWidth(), getHeight(),
										   *owner, owner->getOrientation());
	}

	void enablementChanged()
	{
		repaint();
	}

private:
	TabbedButtonBar* const owner;

	TabAreaBehindFrontButtonComponent (const TabAreaBehindFrontButtonComponent&);
	TabAreaBehindFrontButtonComponent& operator= (const TabAreaBehindFrontButtonComponent&);
};

TabbedButtonBar::TabbedButtonBar (const Orientation orientation_)
	: orientation (orientation_),
	  currentTabIndex (-1),
	  extraTabsButton (0)
{
	setInterceptsMouseClicks (false, true);
	addAndMakeVisible (behindFrontTab = new TabAreaBehindFrontButtonComponent (this));
	setFocusContainer (true);
}

TabbedButtonBar::~TabbedButtonBar()
{
	deleteAllChildren();
}

void TabbedButtonBar::setOrientation (const Orientation newOrientation)
{
	orientation = newOrientation;

	for (int i = getNumChildComponents(); --i >= 0;)
		getChildComponent (i)->resized();

	resized();
}

TabBarButton* TabbedButtonBar::createTabButton (const String& name, const int index)
{
	return new TabBarButton (name, this, index);
}

void TabbedButtonBar::clearTabs()
{
	tabs.clear();
	tabColours.clear();
	currentTabIndex = -1;

	deleteAndZero (extraTabsButton);
	removeChildComponent (behindFrontTab);
	deleteAllChildren();
	addChildComponent (behindFrontTab);

	setCurrentTabIndex (-1);
}

void TabbedButtonBar::addTab (const String& tabName,
							  const Colour& tabBackgroundColour,
							  int insertIndex)
{
	jassert (tabName.isNotEmpty()); // you have to give them all a name..

	if (tabName.isNotEmpty())
	{
		if (((unsigned int) insertIndex) > (unsigned int) tabs.size())
			insertIndex = tabs.size();

		for (int i = tabs.size(); --i >= insertIndex;)
		{
			TabBarButton* const tb = getTabButton (i);

			if (tb != 0)
				tb->tabIndex++;
		}

		tabs.insert (insertIndex, tabName);
		tabColours.insert (insertIndex, tabBackgroundColour);

		TabBarButton* const tb = createTabButton (tabName, insertIndex);
		jassert (tb != 0); // your createTabButton() mustn't return zero!

		addAndMakeVisible (tb, insertIndex);

		resized();

		if (currentTabIndex < 0)
			setCurrentTabIndex (0);
	}
}

void TabbedButtonBar::setTabName (const int tabIndex,
								  const String& newName)
{
	if (((unsigned int) tabIndex) < (unsigned int) tabs.size()
		 && tabs[tabIndex] != newName)
	{
		tabs.set (tabIndex, newName);

		TabBarButton* const tb = getTabButton (tabIndex);

		if (tb != 0)
			tb->setButtonText (newName);

		resized();
	}
}

void TabbedButtonBar::removeTab (const int tabIndex)
{
	if (((unsigned int) tabIndex) < (unsigned int) tabs.size())
	{
		const int oldTabIndex = currentTabIndex;
		if (currentTabIndex == tabIndex)
			currentTabIndex = -1;

		tabs.remove (tabIndex);
		tabColours.remove (tabIndex);

		delete getTabButton (tabIndex);

		for (int i = tabIndex + 1; i <= tabs.size(); ++i)
		{
			TabBarButton* const tb = getTabButton (i);

			if (tb != 0)
				tb->tabIndex--;
		}

		resized();

		setCurrentTabIndex (jlimit (0, jmax (0, tabs.size() - 1), oldTabIndex));
	}
}

void TabbedButtonBar::moveTab (const int currentIndex,
							   const int newIndex)
{
	tabs.move (currentIndex, newIndex);
	tabColours.move (currentIndex, newIndex);
	resized();
}

int TabbedButtonBar::getNumTabs() const
{
	return tabs.size();
}

const StringArray TabbedButtonBar::getTabNames() const
{
	return tabs;
}

void TabbedButtonBar::setCurrentTabIndex (int newIndex, const bool sendChangeMessage_)
{
	if (currentTabIndex != newIndex)
	{
		if (((unsigned int) newIndex) >= (unsigned int) tabs.size())
			newIndex = -1;

		currentTabIndex = newIndex;

		for (int i = 0; i < getNumChildComponents(); ++i)
		{
			TabBarButton* const tb = dynamic_cast <TabBarButton*> (getChildComponent (i));

			if (tb != 0)
				tb->setToggleState (tb->tabIndex == newIndex, false);
		}

		resized();

		if (sendChangeMessage_)
			sendChangeMessage (this);

		currentTabChanged (newIndex, newIndex >= 0 ? tabs [newIndex] : String::empty);
	}
}

TabBarButton* TabbedButtonBar::getTabButton (const int index) const
{
	for (int i = getNumChildComponents(); --i >= 0;)
	{
		TabBarButton* const tb = dynamic_cast <TabBarButton*> (getChildComponent (i));

		if (tb != 0 && tb->tabIndex == index)
			return tb;
	}

	return 0;
}

void TabbedButtonBar::lookAndFeelChanged()
{
	deleteAndZero (extraTabsButton);
	resized();
}

void TabbedButtonBar::resized()
{
	const double minimumScale = 0.7;
	int depth = getWidth();
	int length = getHeight();

	if (orientation == TabsAtTop || orientation == TabsAtBottom)
		swapVariables (depth, length);

	const int overlap = getLookAndFeel().getTabButtonOverlap (depth)
							+ getLookAndFeel().getTabButtonSpaceAroundImage() * 2;

	int i, totalLength = overlap;
	int numVisibleButtons = tabs.size();

	for (i = 0; i < getNumChildComponents(); ++i)
	{
		TabBarButton* const tb = dynamic_cast <TabBarButton*> (getChildComponent (i));

		if (tb != 0)
		{
			totalLength += tb->getBestTabLength (depth) - overlap;
			tb->overlapPixels = overlap / 2;
		}
	}

	double scale = 1.0;

	if (totalLength > length)
		scale = jmax (minimumScale, length / (double) totalLength);

	const bool isTooBig = totalLength * scale > length;
	int tabsButtonPos = 0;

	if (isTooBig)
	{
		if (extraTabsButton == 0)
		{
			addAndMakeVisible (extraTabsButton = getLookAndFeel().createTabBarExtrasButton());
			extraTabsButton->addButtonListener (this);
			extraTabsButton->setAlwaysOnTop (true);
			extraTabsButton->setTriggeredOnMouseDown (true);
		}

		const int buttonSize = jmin (proportionOfWidth (0.7f), proportionOfHeight (0.7f));
		extraTabsButton->setSize (buttonSize, buttonSize);

		if (orientation == TabsAtTop || orientation == TabsAtBottom)
		{
			tabsButtonPos = getWidth() - buttonSize / 2 - 1;
			extraTabsButton->setCentrePosition (tabsButtonPos, getHeight() / 2);
		}
		else
		{
			tabsButtonPos = getHeight() - buttonSize / 2 - 1;
			extraTabsButton->setCentrePosition (getWidth() / 2, tabsButtonPos);
		}

		totalLength = 0;

		for (i = 0; i < tabs.size(); ++i)
		{
			TabBarButton* const tb = getTabButton (i);

			if (tb != 0)
			{
				const int newLength = totalLength + tb->getBestTabLength (depth);

				if (i > 0 && newLength * minimumScale > tabsButtonPos)
				{
					totalLength += overlap;
					break;
				}

				numVisibleButtons = i + 1;
				totalLength = newLength - overlap;

			}
		}

		scale = jmax (minimumScale, tabsButtonPos / (double) totalLength);
	}
	else
	{
		deleteAndZero (extraTabsButton);
	}

	int pos = 0;

	TabBarButton* frontTab = 0;

	for (i = 0; i < tabs.size(); ++i)
	{
		TabBarButton* const tb = getTabButton (i);

		if (tb != 0)
		{
			const int bestLength = roundToInt (scale * tb->getBestTabLength (depth));

			if (i < numVisibleButtons)
			{
				if (orientation == TabsAtTop || orientation == TabsAtBottom)
					tb->setBounds (pos, 0, bestLength, getHeight());
				else
					tb->setBounds (0, pos, getWidth(), bestLength);

				tb->toBack();

				if (tb->tabIndex == currentTabIndex)
					frontTab = tb;

				tb->setVisible (true);
			}
			else
			{
				tb->setVisible (false);
			}

			pos += bestLength - overlap;
		}
	}

	behindFrontTab->setBounds (0, 0, getWidth(), getHeight());

	if (frontTab != 0)
	{
		frontTab->toFront (false);
		behindFrontTab->toBehind (frontTab);
	}
}

const Colour TabbedButtonBar::getTabBackgroundColour (const int tabIndex)
{
	return tabColours [tabIndex];
}

void TabbedButtonBar::setTabBackgroundColour (const int tabIndex, const Colour& newColour)
{
	if (((unsigned int) tabIndex) < (unsigned int) tabColours.size()
		 && tabColours [tabIndex] != newColour)
	{
		tabColours.set (tabIndex, newColour);
		repaint();
	}
}

void TabbedButtonBar::buttonClicked (Button* button)
{
	if (extraTabsButton == button)
	{
		PopupMenu m;

		for (int i = 0; i < tabs.size(); ++i)
		{
			TabBarButton* const tb = getTabButton (i);

			if (tb != 0 && ! tb->isVisible())
				m.addItem (tb->tabIndex + 1, tabs[i], true, i == currentTabIndex);
		}

		const int res = m.showAt (extraTabsButton);

		if (res != 0)
			setCurrentTabIndex (res - 1);
	}
}

void TabbedButtonBar::currentTabChanged (const int, const String&)
{
}

void TabbedButtonBar::popupMenuClickOnTab (const int, const String&)
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TabbedButtonBar.cpp ***/


/*** Start of inlined file: juce_TabbedComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

class TabCompButtonBar  : public TabbedButtonBar
{
public:
	TabCompButtonBar (TabbedComponent* const owner_,
					  const TabbedButtonBar::Orientation orientation)
		: TabbedButtonBar (orientation),
		  owner (owner_)
	{
	}

	~TabCompButtonBar()
	{
	}

	void currentTabChanged (int newCurrentTabIndex,
							const String& newTabName)
	{
		owner->changeCallback (newCurrentTabIndex, newTabName);
	}

	void popupMenuClickOnTab (int tabIndex,
							  const String& tabName)
	{
		owner->popupMenuClickOnTab (tabIndex, tabName);
	}

	const Colour getTabBackgroundColour (const int tabIndex)
	{
		return owner->tabs->getTabBackgroundColour (tabIndex);
	}

	TabBarButton* createTabButton (const String& tabName, int tabIndex)
	{
		return owner->createTabButton (tabName, tabIndex);
	}

	juce_UseDebuggingNewOperator

private:
	TabbedComponent* const owner;

	TabCompButtonBar (const TabCompButtonBar&);
	TabCompButtonBar& operator= (const TabCompButtonBar&);
};

TabbedComponent::TabbedComponent (const TabbedButtonBar::Orientation orientation)
	: panelComponent (0),
	  tabDepth (30),
	  outlineThickness (1),
	  edgeIndent (0)
{
	addAndMakeVisible (tabs = new TabCompButtonBar (this, orientation));
}

TabbedComponent::~TabbedComponent()
{
	clearTabs();
	delete tabs;
}

void TabbedComponent::setOrientation (const TabbedButtonBar::Orientation orientation)
{
	tabs->setOrientation (orientation);
	resized();
}

TabbedButtonBar::Orientation TabbedComponent::getOrientation() const throw()
{
	return tabs->getOrientation();
}

void TabbedComponent::setTabBarDepth (const int newDepth)
{
	if (tabDepth != newDepth)
	{
		tabDepth = newDepth;
		resized();
	}
}

TabBarButton* TabbedComponent::createTabButton (const String& tabName, const int tabIndex)
{
	return new TabBarButton (tabName, tabs, tabIndex);
}

void TabbedComponent::clearTabs()
{
	if (panelComponent != 0)
	{
		panelComponent->setVisible (false);
		removeChildComponent (panelComponent);
		panelComponent = 0;
	}

	tabs->clearTabs();

	for (int i = contentComponents.size(); --i >= 0;)
	{
		Component* const c = contentComponents.getUnchecked(i);

		// be careful not to delete these components until they've been removed from the tab component
		jassert (c == 0 || c->isValidComponent());

		if (c != 0 && (bool) c->getProperties() ["deleteByTabComp_"])
			delete c;
	}

	contentComponents.clear();
}

void TabbedComponent::addTab (const String& tabName,
							  const Colour& tabBackgroundColour,
							  Component* const contentComponent,
							  const bool deleteComponentWhenNotNeeded,
							  const int insertIndex)
{
	contentComponents.insert (insertIndex, contentComponent);

	if (contentComponent != 0)
		contentComponent->getProperties().set ("deleteByTabComp_", deleteComponentWhenNotNeeded);

	tabs->addTab (tabName, tabBackgroundColour, insertIndex);
}

void TabbedComponent::setTabName (const int tabIndex,
								  const String& newName)
{
	tabs->setTabName (tabIndex, newName);
}

void TabbedComponent::removeTab (const int tabIndex)
{
	Component* const c = contentComponents [tabIndex];

	if (c != 0 && (bool) c->getProperties() ["deleteByTabComp_"])
	{
		if (c == panelComponent)
			panelComponent = 0;

		delete c;
	}

	contentComponents.remove (tabIndex);

	tabs->removeTab (tabIndex);
}

int TabbedComponent::getNumTabs() const
{
	return tabs->getNumTabs();
}

const StringArray TabbedComponent::getTabNames() const
{
	return tabs->getTabNames();
}

Component* TabbedComponent::getTabContentComponent (const int tabIndex) const throw()
{
	return contentComponents [tabIndex];
}

const Colour TabbedComponent::getTabBackgroundColour (const int tabIndex) const throw()
{
	return tabs->getTabBackgroundColour (tabIndex);
}

void TabbedComponent::setTabBackgroundColour (const int tabIndex, const Colour& newColour)
{
	tabs->setTabBackgroundColour (tabIndex, newColour);

	if (getCurrentTabIndex() == tabIndex)
		repaint();
}

void TabbedComponent::setCurrentTabIndex (const int newTabIndex, const bool sendChangeMessage)
{
	tabs->setCurrentTabIndex (newTabIndex, sendChangeMessage);
}

int TabbedComponent::getCurrentTabIndex() const
{
	return tabs->getCurrentTabIndex();
}

const String& TabbedComponent::getCurrentTabName() const
{
	return tabs->getCurrentTabName();
}

void TabbedComponent::setOutline (int thickness)
{
	outlineThickness = thickness;
	repaint();
}

void TabbedComponent::setIndent (const int indentThickness)
{
	edgeIndent = indentThickness;
}

void TabbedComponent::paint (Graphics& g)
{
	g.fillAll (findColour (backgroundColourId));

	const TabbedButtonBar::Orientation o = getOrientation();

	int x = 0;
	int y = 0;
	int r = getWidth();
	int b = getHeight();

	if (o == TabbedButtonBar::TabsAtTop)
		y += tabDepth;
	else if (o == TabbedButtonBar::TabsAtBottom)
		b -= tabDepth;
	else if (o == TabbedButtonBar::TabsAtLeft)
		x += tabDepth;
	else if (o == TabbedButtonBar::TabsAtRight)
		r -= tabDepth;

	g.reduceClipRegion (x, y, r - x, b - y);
	g.fillAll (tabs->getTabBackgroundColour (getCurrentTabIndex()));

	if (outlineThickness > 0)
	{
		if (o == TabbedButtonBar::TabsAtTop)
			--y;
		else if (o == TabbedButtonBar::TabsAtBottom)
			++b;
		else if (o == TabbedButtonBar::TabsAtLeft)
			--x;
		else if (o == TabbedButtonBar::TabsAtRight)
			++r;

		g.setColour (findColour (outlineColourId));
		g.drawRect (x, y, r - x, b - y, outlineThickness);
	}
}

void TabbedComponent::resized()
{
	const TabbedButtonBar::Orientation o = getOrientation();
	const int indent = edgeIndent + outlineThickness;
	BorderSize indents (indent);

	if (o == TabbedButtonBar::TabsAtTop)
	{
		tabs->setBounds (0, 0, getWidth(), tabDepth);
		indents.setTop (tabDepth + edgeIndent);
	}
	else if (o == TabbedButtonBar::TabsAtBottom)
	{
		tabs->setBounds (0, getHeight() - tabDepth, getWidth(), tabDepth);
		indents.setBottom (tabDepth + edgeIndent);
	}
	else if (o == TabbedButtonBar::TabsAtLeft)
	{
		tabs->setBounds (0, 0, tabDepth, getHeight());
		indents.setLeft (tabDepth + edgeIndent);
	}
	else if (o == TabbedButtonBar::TabsAtRight)
	{
		tabs->setBounds (getWidth() - tabDepth, 0, tabDepth, getHeight());
		indents.setRight (tabDepth + edgeIndent);
	}

	const Rectangle<int> bounds (indents.subtractedFrom (getLocalBounds()));

	for (int i = contentComponents.size(); --i >= 0;)
		if (contentComponents.getUnchecked (i) != 0)
			contentComponents.getUnchecked (i)->setBounds (bounds);
}

void TabbedComponent::lookAndFeelChanged()
{
	for (int i = contentComponents.size(); --i >= 0;)
		if (contentComponents.getUnchecked (i) != 0)
			contentComponents.getUnchecked (i)->lookAndFeelChanged();
}

void TabbedComponent::changeCallback (const int newCurrentTabIndex,
									  const String& newTabName)
{
	if (panelComponent != 0)
	{
		panelComponent->setVisible (false);
		removeChildComponent (panelComponent);
		panelComponent = 0;
	}

	if (getCurrentTabIndex() >= 0)
	{
		panelComponent = contentComponents [getCurrentTabIndex()];

		if (panelComponent != 0)
		{
			// do these ops as two stages instead of addAndMakeVisible() so that the
			// component has always got a parent when it gets the visibilityChanged() callback
			addChildComponent (panelComponent);
			panelComponent->setVisible (true);
			panelComponent->toFront (true);
		}

		repaint();
	}

	resized();

	currentTabChanged (newCurrentTabIndex, newTabName);
}

void TabbedComponent::currentTabChanged (const int, const String&)
{
}

void TabbedComponent::popupMenuClickOnTab (const int, const String&)
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TabbedComponent.cpp ***/


/*** Start of inlined file: juce_Viewport.cpp ***/
BEGIN_JUCE_NAMESPACE

Viewport::Viewport (const String& componentName)
  : Component (componentName),
	contentComp (0),
	lastVX (0),
	lastVY (0),
	lastVW (0),
	lastVH (0),
	scrollBarThickness (0),
	singleStepX (16),
	singleStepY (16),
	showHScrollbar (true),
	showVScrollbar (true)
{
	// content holder is used to clip the contents so they don't overlap the scrollbars
	addAndMakeVisible (contentHolder = new Component());
	contentHolder->setInterceptsMouseClicks (false, true);

	verticalScrollBar = new ScrollBar (true);
	horizontalScrollBar = new ScrollBar (false);

	addChildComponent (verticalScrollBar);
	addChildComponent (horizontalScrollBar);

	verticalScrollBar->addListener (this);
	horizontalScrollBar->addListener (this);

	setInterceptsMouseClicks (false, true);
	setWantsKeyboardFocus (true);
}

Viewport::~Viewport()
{
	contentHolder->deleteAllChildren();
	deleteAllChildren();
}

void Viewport::visibleAreaChanged (int, int, int, int)
{
}

void Viewport::setViewedComponent (Component* const newViewedComponent)
{
	if (contentComp.getComponent() != newViewedComponent)
	{
		{
			ScopedPointer<Component> oldCompDeleter (contentComp);
			contentComp = 0;
		}

		contentComp = newViewedComponent;

		if (contentComp != 0)
		{
			contentComp->setTopLeftPosition (0, 0);
			contentHolder->addAndMakeVisible (contentComp);
			contentComp->addComponentListener (this);
		}

		updateVisibleRegion();
	}
}

int Viewport::getMaximumVisibleWidth() const throw()
{
	return jmax (0, getWidth() - (verticalScrollBar->isVisible() ? getScrollBarThickness() : 0));
}

int Viewport::getMaximumVisibleHeight() const throw()
{
	return jmax (0, getHeight() - (horizontalScrollBar->isVisible() ? getScrollBarThickness() : 0));
}

void Viewport::setViewPosition (const int xPixelsOffset,
								const int yPixelsOffset)
{
	if (contentComp != 0)
		contentComp->setTopLeftPosition (-xPixelsOffset,
										 -yPixelsOffset);
}

void Viewport::setViewPositionProportionately (const double x,
											   const double y)
{
	if (contentComp != 0)
		setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
						 jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
}

bool Viewport::autoScroll (int mouseX, int mouseY, int activeBorderThickness, int maximumSpeed)
{
	if (contentComp != 0)
	{
		int dx = 0, dy = 0;

		if (mouseX < activeBorderThickness)
			dx = activeBorderThickness - mouseX;
		else if (mouseX >= contentHolder->getWidth() - activeBorderThickness)
			dx = (contentHolder->getWidth() - activeBorderThickness) - mouseX;

		if (dx < 0)
			dx = jmax (dx, -maximumSpeed, contentHolder->getWidth() - contentComp->getRight());
		else
			dx = jmin (dx, maximumSpeed, -contentComp->getX());

		if (mouseY < activeBorderThickness)
			dy = activeBorderThickness - mouseY;
		else if (mouseY >= contentHolder->getHeight() - activeBorderThickness)
			dy = (contentHolder->getHeight() - activeBorderThickness) - mouseY;

		if (dy < 0)
			dy = jmax (dy, -maximumSpeed, contentHolder->getHeight() - contentComp->getBottom());
		else
			dy = jmin (dy, maximumSpeed, -contentComp->getY());

		if (dx != 0 || dy != 0)
		{
			contentComp->setTopLeftPosition (contentComp->getX() + dx,
											 contentComp->getY() + dy);

			return true;
		}
	}

	return false;
}

void Viewport::componentMovedOrResized (Component&, bool, bool)
{
	updateVisibleRegion();
}

void Viewport::resized()
{
	updateVisibleRegion();
}

void Viewport::updateVisibleRegion()
{
	if (contentComp != 0)
	{
		const int newVX = -contentComp->getX();
		const int newVY = -contentComp->getY();

		if (newVX == 0 && newVY == 0
			&& contentComp->getWidth() <= getWidth()
			&& contentComp->getHeight() <= getHeight())
		{
			horizontalScrollBar->setVisible (false);
			verticalScrollBar->setVisible (false);
		}

		horizontalScrollBar->setRangeLimits (0.0, contentComp->getWidth());
		horizontalScrollBar->setCurrentRange (newVX, getMaximumVisibleWidth());
		horizontalScrollBar->setSingleStepSize (singleStepX);

		if (! (contentComp->getWidth() > 0 && showHScrollbar && getHeight() > getScrollBarThickness()))
			horizontalScrollBar->setVisible (false);

		verticalScrollBar->setRangeLimits (0.0, contentComp->getHeight());
		verticalScrollBar->setCurrentRange (newVY, getMaximumVisibleHeight());
		verticalScrollBar->setSingleStepSize (singleStepY);

		if (! (contentComp->getHeight() > 0 && showVScrollbar && getWidth() > getScrollBarThickness()))
			verticalScrollBar->setVisible (false);

		if (verticalScrollBar->isVisible())
		{
			horizontalScrollBar->setCurrentRange (newVX, getMaximumVisibleWidth());
			verticalScrollBar->setCurrentRange (newVY, getMaximumVisibleHeight());

			verticalScrollBar
				->setBounds (getMaximumVisibleWidth(), 0,
							 getScrollBarThickness(), getMaximumVisibleHeight());
		}

		if (horizontalScrollBar->isVisible())
		{
			horizontalScrollBar->setCurrentRange (newVX, getMaximumVisibleWidth());

			horizontalScrollBar
				->setBounds (0, getMaximumVisibleHeight(),
							 getMaximumVisibleWidth(), getScrollBarThickness());
		}

		contentHolder->setSize (getMaximumVisibleWidth(),
								getMaximumVisibleHeight());

		const int newVW = jmin (contentComp->getRight(),  getMaximumVisibleWidth());
		const int newVH = jmin (contentComp->getBottom(), getMaximumVisibleHeight());

		if (newVX != lastVX
			 || newVY != lastVY
			 || newVW != lastVW
			 || newVH != lastVH)
		{
			lastVX = newVX;
			lastVY = newVY;
			lastVW = newVW;
			lastVH = newVH;

			visibleAreaChanged (newVX, newVY, newVW, newVH);
		}

		horizontalScrollBar->handleUpdateNowIfNeeded();
		verticalScrollBar->handleUpdateNowIfNeeded();
	}
	else
	{
		horizontalScrollBar->setVisible (false);
		verticalScrollBar->setVisible (false);
	}
}

void Viewport::setSingleStepSizes (const int stepX,
								   const int stepY)
{
	singleStepX = stepX;
	singleStepY = stepY;
	updateVisibleRegion();
}

void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
								   const bool showHorizontalScrollbarIfNeeded)
{
	showVScrollbar = showVerticalScrollbarIfNeeded;
	showHScrollbar = showHorizontalScrollbarIfNeeded;
	updateVisibleRegion();
}

void Viewport::setScrollBarThickness (const int thickness)
{
	scrollBarThickness = thickness;
	updateVisibleRegion();
}

int Viewport::getScrollBarThickness() const throw()
{
	return (scrollBarThickness > 0) ? scrollBarThickness
									: getLookAndFeel().getDefaultScrollbarWidth();
}

void Viewport::setScrollBarButtonVisibility (const bool buttonsVisible)
{
	verticalScrollBar->setButtonVisibility (buttonsVisible);
	horizontalScrollBar->setButtonVisibility (buttonsVisible);
}

void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
{
	if (scrollBarThatHasMoved == horizontalScrollBar)
	{
		setViewPosition (roundToInt (newRangeStart), getViewPositionY());
	}
	else if (scrollBarThatHasMoved == verticalScrollBar)
	{
		setViewPosition (getViewPositionX(), roundToInt (newRangeStart));
	}
}

void Viewport::mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
	if (! useMouseWheelMoveIfNeeded (e, wheelIncrementX, wheelIncrementY))
		Component::mouseWheelMove (e, wheelIncrementX, wheelIncrementY);
}

bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
	if (! (e.mods.isAltDown() || e.mods.isCtrlDown()))
	{
		const bool hasVertBar = verticalScrollBar->isVisible();
		const bool hasHorzBar = horizontalScrollBar->isVisible();

		if (hasHorzBar && (wheelIncrementX != 0 || e.mods.isShiftDown() || ! hasVertBar))
		{
			if (wheelIncrementX == 0 && ! hasVertBar)
				wheelIncrementX = wheelIncrementY;

			horizontalScrollBar->mouseWheelMove (e.getEventRelativeTo (horizontalScrollBar),
												 wheelIncrementX, wheelIncrementY);
			return true;
		}
		else if (hasVertBar && wheelIncrementY != 0)
		{
			verticalScrollBar->mouseWheelMove (e.getEventRelativeTo (verticalScrollBar),
											   wheelIncrementX, wheelIncrementY);
			return true;
		}
	}

	return false;
}

bool Viewport::keyPressed (const KeyPress& key)
{
	const bool isUpDownKey = key.isKeyCode (KeyPress::upKey)
								|| key.isKeyCode (KeyPress::downKey)
								|| key.isKeyCode (KeyPress::pageUpKey)
								|| key.isKeyCode (KeyPress::pageDownKey)
								|| key.isKeyCode (KeyPress::homeKey)
								|| key.isKeyCode (KeyPress::endKey);

	if (verticalScrollBar->isVisible() && isUpDownKey)
		return verticalScrollBar->keyPressed (key);

	const bool isLeftRightKey = key.isKeyCode (KeyPress::leftKey)
								 || key.isKeyCode (KeyPress::rightKey);

	if (horizontalScrollBar->isVisible() && (isUpDownKey || isLeftRightKey))
		return horizontalScrollBar->keyPressed (key);

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Viewport.cpp ***/


/*** Start of inlined file: juce_LookAndFeel.cpp ***/
BEGIN_JUCE_NAMESPACE

static const Colour createBaseColour (const Colour& buttonColour,
									  const bool hasKeyboardFocus,
									  const bool isMouseOverButton,
									  const bool isButtonDown) throw()
{
	const float sat = hasKeyboardFocus ? 1.3f : 0.9f;
	const Colour baseColour (buttonColour.withMultipliedSaturation (sat));

	if (isButtonDown)
		return baseColour.contrasting (0.2f);
	else if (isMouseOverButton)
		return baseColour.contrasting (0.1f);

	return baseColour;
}

LookAndFeel::LookAndFeel()
{
	/* if this fails it means you're trying to create a LookAndFeel object before
	   the static Colours have been initialised. That ain't gonna work. It probably
	   means that you're using a static LookAndFeel object and that your compiler has
	   decided to intialise it before the Colours class.
	*/
	jassert (Colours::white == Colour (0xffffffff));

	// set up the standard set of colours..
	const int textButtonColour = 0xffbbbbff;
	const int textHighlightColour = 0x401111ee;
	const int standardOutlineColour = 0xb2808080;

	static const int standardColours[] =
	{
		TextButton::buttonColourId,		 textButtonColour,
		TextButton::buttonOnColourId,		   0xff4444ff,
		TextButton::textColourOnId,		 0xff000000,
		TextButton::textColourOffId,		0xff000000,

		ComboBox::buttonColourId,		   0xffbbbbff,
		ComboBox::outlineColourId,		  standardOutlineColour,

		ToggleButton::textColourId,		 0xff000000,

		TextEditor::backgroundColourId,		 0xffffffff,
		TextEditor::textColourId,		   0xff000000,
		TextEditor::highlightColourId,		  textHighlightColour,
		TextEditor::highlightedTextColourId,	0xff000000,
		TextEditor::caretColourId,		  0xff000000,
		TextEditor::outlineColourId,		0x00000000,
		TextEditor::focusedOutlineColourId,	 textButtonColour,
		TextEditor::shadowColourId,		 0x38000000,

		Label::backgroundColourId,		  0x00000000,
		Label::textColourId,			0xff000000,
		Label::outlineColourId,			 0x00000000,

		ScrollBar::backgroundColourId,		  0x00000000,
		ScrollBar::thumbColourId,		   0xffffffff,
		ScrollBar::trackColourId,		   0xffffffff,

		TreeView::linesColourId,			0x4c000000,
		TreeView::backgroundColourId,		   0x00000000,
		TreeView::dragAndDropIndicatorColourId,	 0x80ff0000,

		PopupMenu::backgroundColourId,		  0xffffffff,
		PopupMenu::textColourId,			0xff000000,
		PopupMenu::headerTextColourId,		  0xff000000,
		PopupMenu::highlightedTextColourId,	 0xffffffff,
		PopupMenu::highlightedBackgroundColourId,   0x991111aa,

		ComboBox::textColourId,			 0xff000000,
		ComboBox::backgroundColourId,		   0xffffffff,
		ComboBox::arrowColourId,			0x99000000,

		ListBox::backgroundColourId,		0xffffffff,
		ListBox::outlineColourId,		   standardOutlineColour,
		ListBox::textColourId,			  0xff000000,

		Slider::backgroundColourId,		 0x00000000,
		Slider::thumbColourId,			  textButtonColour,
		Slider::trackColourId,			  0x7fffffff,
		Slider::rotarySliderFillColourId,	   0x7f0000ff,
		Slider::rotarySliderOutlineColourId,	0x66000000,
		Slider::textBoxTextColourId,		0xff000000,
		Slider::textBoxBackgroundColourId,	  0xffffffff,
		Slider::textBoxHighlightColourId,	   textHighlightColour,
		Slider::textBoxOutlineColourId,		 standardOutlineColour,

		ResizableWindow::backgroundColourId,	0xff777777,
		//DocumentWindow::textColourId,		   0xff000000, // (this is deliberately not set)

		AlertWindow::backgroundColourId,		0xffededed,
		AlertWindow::textColourId,		  0xff000000,
		AlertWindow::outlineColourId,		   0xff666666,

		ProgressBar::backgroundColourId,		0xffeeeeee,
		ProgressBar::foregroundColourId,		0xffaaaaee,

		TooltipWindow::backgroundColourId,	  0xffeeeebb,
		TooltipWindow::textColourId,		0xff000000,
		TooltipWindow::outlineColourId,		 0x4c000000,

		TabbedComponent::backgroundColourId,	0x00000000,
		TabbedComponent::outlineColourId,	   0xff777777,
		TabbedButtonBar::tabOutlineColourId,	0x80000000,
		TabbedButtonBar::frontOutlineColourId,	  0x90000000,

		Toolbar::backgroundColourId,		0xfff6f8f9,
		Toolbar::separatorColourId,		 0x4c000000,
		Toolbar::buttonMouseOverBackgroundColourId, 0x4c0000ff,
		Toolbar::buttonMouseDownBackgroundColourId, 0x800000ff,
		Toolbar::labelTextColourId,		 0xff000000,
		Toolbar::editingModeOutlineColourId,	0xffff0000,

		HyperlinkButton::textColourId,		  0xcc1111ee,

		GroupComponent::outlineColourId,		0x66000000,
		GroupComponent::textColourId,		   0xff000000,

		DirectoryContentsDisplayComponent::highlightColourId,   textHighlightColour,
		DirectoryContentsDisplayComponent::textColourId,	0xff000000,

		0x1000440, /*LassoComponent::lassoFillColourId*/	0x66dddddd,
		0x1000441, /*LassoComponent::lassoOutlineColourId*/	 0x99111111,

		MidiKeyboardComponent::whiteNoteColourId,		   0xffffffff,
		MidiKeyboardComponent::blackNoteColourId,		   0xff000000,
		MidiKeyboardComponent::keySeparatorLineColourId,	0x66000000,
		MidiKeyboardComponent::mouseOverKeyOverlayColourId,	 0x80ffff00,
		MidiKeyboardComponent::keyDownOverlayColourId,	  0xffb6b600,
		MidiKeyboardComponent::textLabelColourId,		   0xff000000,
		MidiKeyboardComponent::upDownButtonBackgroundColourId,  0xffd3d3d3,
		MidiKeyboardComponent::upDownButtonArrowColourId,	   0xff000000,

		CodeEditorComponent::backgroundColourId,		0xffffffff,
		CodeEditorComponent::caretColourId,			 0xff000000,
		CodeEditorComponent::highlightColourId,		 textHighlightColour,
		CodeEditorComponent::defaultTextColourId,		   0xff000000,

		ColourSelector::backgroundColourId,			 0xffe5e5e5,
		ColourSelector::labelTextColourId,			  0xff000000,

		KeyMappingEditorComponent::backgroundColourId,	  0x00000000,
		KeyMappingEditorComponent::textColourId,		0xff000000,

		FileSearchPathListComponent::backgroundColourId,	0xffffffff,

		FileChooserDialogBox::titleTextColourId,		0xff000000,
	};

	for (int i = 0; i < numElementsInArray (standardColours); i += 2)
		setColour (standardColours [i], Colour (standardColours [i + 1]));

	static String defaultSansName, defaultSerifName, defaultFixedName;

	if (defaultSansName.isEmpty())
		Font::getPlatformDefaultFontNames (defaultSansName, defaultSerifName, defaultFixedName);

	defaultSans = defaultSansName;
	defaultSerif = defaultSerifName;
	defaultFixed = defaultFixedName;
}

LookAndFeel::~LookAndFeel()
{
}

const Colour LookAndFeel::findColour (const int colourId) const throw()
{
	const int index = colourIds.indexOf (colourId);

	if (index >= 0)
		return colours [index];

	jassertfalse
	return Colours::black;
}

void LookAndFeel::setColour (const int colourId, const Colour& colour) throw()
{
	const int index = colourIds.indexOf (colourId);

	if (index >= 0)
		colours.set (index, colour);

	colourIds.add (colourId);
	colours.add (colour);
}

bool LookAndFeel::isColourSpecified (const int colourId) const throw()
{
	return colourIds.contains (colourId);
}

static LookAndFeel* defaultLF = 0;
static LookAndFeel* currentDefaultLF = 0;

LookAndFeel& LookAndFeel::getDefaultLookAndFeel() throw()
{
	// if this happens, your app hasn't initialised itself properly.. if you're
	// trying to hack your own main() function, have a look at
	// JUCEApplication::initialiseForGUI()
	jassert (currentDefaultLF != 0);

	return *currentDefaultLF;
}

void LookAndFeel::setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel) throw()
{
	if (newDefaultLookAndFeel == 0)
	{
		if (defaultLF == 0)
			defaultLF = new LookAndFeel();

		newDefaultLookAndFeel = defaultLF;
	}

	currentDefaultLF = newDefaultLookAndFeel;

	for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
	{
		Component* const c = Desktop::getInstance().getComponent (i);

		if (c != 0)
			c->sendLookAndFeelChange();
	}
}

void LookAndFeel::clearDefaultLookAndFeel() throw()
{
	if (currentDefaultLF == defaultLF)
		currentDefaultLF = 0;

	deleteAndZero (defaultLF);
}

const Typeface::Ptr LookAndFeel::getTypefaceForFont (const Font& font)
{
	String faceName (font.getTypefaceName());

	if (faceName == Font::getDefaultSansSerifFontName())
		faceName = defaultSans;
	else if (faceName == Font::getDefaultSerifFontName())
		faceName = defaultSerif;
	else if (faceName == Font::getDefaultMonospacedFontName())
		faceName = defaultFixed;

	Font f (font);
	f.setTypefaceName (faceName);
	return Typeface::createSystemTypefaceFor (f);
}

void LookAndFeel::setDefaultSansSerifTypefaceName (const String& newName)
{
	defaultSans = newName;
}

const MouseCursor LookAndFeel::getMouseCursorFor (Component& component)
{
	return component.getMouseCursor();
}

void LookAndFeel::drawButtonBackground (Graphics& g,
										Button& button,
										const Colour& backgroundColour,
										bool isMouseOverButton,
										bool isButtonDown)
{
	const int width = button.getWidth();
	const int height = button.getHeight();

	const float outlineThickness = button.isEnabled() ? ((isButtonDown || isMouseOverButton) ? 1.2f : 0.7f) : 0.4f;
	const float halfThickness = outlineThickness * 0.5f;

	const float indentL = button.isConnectedOnLeft()   ? 0.1f : halfThickness;
	const float indentR = button.isConnectedOnRight()  ? 0.1f : halfThickness;
	const float indentT = button.isConnectedOnTop()	? 0.1f : halfThickness;
	const float indentB = button.isConnectedOnBottom() ? 0.1f : halfThickness;

	const Colour baseColour (createBaseColour (backgroundColour,
											   button.hasKeyboardFocus (true),
											   isMouseOverButton, isButtonDown)
							   .withMultipliedAlpha (button.isEnabled() ? 1.0f : 0.5f));

	drawGlassLozenge (g,
					  indentL,
					  indentT,
					  width - indentL - indentR,
					  height - indentT - indentB,
					  baseColour, outlineThickness, -1.0f,
					  button.isConnectedOnLeft(),
					  button.isConnectedOnRight(),
					  button.isConnectedOnTop(),
					  button.isConnectedOnBottom());
}

const Font LookAndFeel::getFontForTextButton (TextButton& button)
{
	return button.getFont();
}

void LookAndFeel::drawButtonText (Graphics& g, TextButton& button,
								  bool /*isMouseOverButton*/, bool /*isButtonDown*/)
{
	Font font (getFontForTextButton (button));
	g.setFont (font);
	g.setColour (button.findColour (button.getToggleState() ? TextButton::textColourOnId
															: TextButton::textColourOffId)
					   .withMultipliedAlpha (button.isEnabled() ? 1.0f : 0.5f));

	const int yIndent = jmin (4, button.proportionOfHeight (0.3f));
	const int cornerSize = jmin (button.getHeight(), button.getWidth()) / 2;

	const int fontHeight = roundToInt (font.getHeight() * 0.6f);
	const int leftIndent  = jmin (fontHeight, 2 + cornerSize / (button.isConnectedOnLeft() ? 4 : 2));
	const int rightIndent = jmin (fontHeight, 2 + cornerSize / (button.isConnectedOnRight() ? 4 : 2));

	g.drawFittedText (button.getButtonText(),
					  leftIndent,
					  yIndent,
					  button.getWidth() - leftIndent - rightIndent,
					  button.getHeight() - yIndent * 2,
					  Justification::centred, 2);
}

void LookAndFeel::drawTickBox (Graphics& g,
							   Component& component,
							   float x, float y, float w, float h,
							   const bool ticked,
							   const bool isEnabled,
							   const bool isMouseOverButton,
							   const bool isButtonDown)
{
	const float boxSize = w * 0.7f;

	drawGlassSphere (g, x, y + (h - boxSize) * 0.5f, boxSize,
					 createBaseColour (component.findColour (TextButton::buttonColourId)
												.withMultipliedAlpha (isEnabled ? 1.0f : 0.5f),
									   true,
									   isMouseOverButton,
									   isButtonDown),
					 isEnabled ? ((isButtonDown || isMouseOverButton) ? 1.1f : 0.5f) : 0.3f);

	if (ticked)
	{
		Path tick;
		tick.startNewSubPath (1.5f, 3.0f);
		tick.lineTo (3.0f, 6.0f);
		tick.lineTo (6.0f, 0.0f);

		g.setColour (isEnabled ? Colours::black : Colours::grey);

		const AffineTransform trans (AffineTransform::scale (w / 9.0f, h / 9.0f)
										 .translated (x, y));

		g.strokePath (tick, PathStrokeType (2.5f), trans);
	}
}

void LookAndFeel::drawToggleButton (Graphics& g,
									ToggleButton& button,
									bool isMouseOverButton,
									bool isButtonDown)
{
	if (button.hasKeyboardFocus (true))
	{
		g.setColour (button.findColour (TextEditor::focusedOutlineColourId));
		g.drawRect (0, 0, button.getWidth(), button.getHeight());
	}

	float fontSize = jmin (15.0f, button.getHeight() * 0.75f);
	const float tickWidth = fontSize * 1.1f;

	drawTickBox (g, button, 4.0f, (button.getHeight() - tickWidth) * 0.5f,
				 tickWidth, tickWidth,
				 button.getToggleState(),
				 button.isEnabled(),
				 isMouseOverButton,
				 isButtonDown);

	g.setColour (button.findColour (ToggleButton::textColourId));
	g.setFont (fontSize);

	if (! button.isEnabled())
		g.setOpacity (0.5f);

	const int textX = (int) tickWidth + 5;

	g.drawFittedText (button.getButtonText(),
					  textX, 0,
					  button.getWidth() - textX - 2, button.getHeight(),
					  Justification::centredLeft, 10);
}

void LookAndFeel::changeToggleButtonWidthToFitText (ToggleButton& button)
{
	Font font (jmin (15.0f, button.getHeight() * 0.6f));

	const int tickWidth = jmin (24, button.getHeight());

	button.setSize (font.getStringWidth (button.getButtonText()) + tickWidth + 8,
					button.getHeight());
}

AlertWindow* LookAndFeel::createAlertWindow (const String& title,
											 const String& message,
											 const String& button1,
											 const String& button2,
											 const String& button3,
											 AlertWindow::AlertIconType iconType,
											 int numButtons,
											 Component* associatedComponent)
{
	AlertWindow* aw = new AlertWindow (title, message, iconType, associatedComponent);

	if (numButtons == 1)
	{
		aw->addButton (button1, 0,
					   KeyPress (KeyPress::escapeKey, 0, 0),
					   KeyPress (KeyPress::returnKey, 0, 0));
	}
	else
	{
		const KeyPress button1ShortCut (CharacterFunctions::toLowerCase (button1[0]), 0, 0);
		KeyPress button2ShortCut (CharacterFunctions::toLowerCase (button2[0]), 0, 0);
		if (button1ShortCut == button2ShortCut)
			button2ShortCut = KeyPress();

		if (numButtons == 2)
		{
			aw->addButton (button1, 1, KeyPress (KeyPress::returnKey, 0, 0), button1ShortCut);
			aw->addButton (button2, 0, KeyPress (KeyPress::escapeKey, 0, 0), button2ShortCut);
		}
		else if (numButtons == 3)
		{
			aw->addButton (button1, 1, button1ShortCut);
			aw->addButton (button2, 2, button2ShortCut);
			aw->addButton (button3, 0, KeyPress (KeyPress::escapeKey, 0, 0));
		}
	}

	return aw;
}

void LookAndFeel::drawAlertBox (Graphics& g,
								AlertWindow& alert,
								const Rectangle<int>& textArea,
								TextLayout& textLayout)
{
	g.fillAll (alert.findColour (AlertWindow::backgroundColourId));

	int iconSpaceUsed = 0;
	Justification alignment (Justification::horizontallyCentred);

	const int iconWidth = 80;
	int iconSize = jmin (iconWidth + 50, alert.getHeight() + 20);

	if (alert.containsAnyExtraComponents() || alert.getNumButtons() > 2)
		iconSize = jmin (iconSize, textArea.getHeight() + 50);

	const Rectangle<int> iconRect (iconSize / -10, iconSize / -10,
								   iconSize, iconSize);

	if (alert.getAlertType() != AlertWindow::NoIcon)
	{
		Path icon;
		uint32 colour;
		char character;

		if (alert.getAlertType() == AlertWindow::WarningIcon)
		{
			colour = 0x55ff5555;
			character = '!';

			icon.addTriangle (iconRect.getX() + iconRect.getWidth() * 0.5f, (float) iconRect.getY(),
							  (float) iconRect.getRight(), (float) iconRect.getBottom(),
							  (float) iconRect.getX(), (float) iconRect.getBottom());

			icon = icon.createPathWithRoundedCorners (5.0f);
		}
		else
		{
			colour = alert.getAlertType() == AlertWindow::InfoIcon ? 0x605555ff : 0x40b69900;
			character = alert.getAlertType() == AlertWindow::InfoIcon ? 'i' : '?';

			icon.addEllipse ((float) iconRect.getX(), (float) iconRect.getY(),
							 (float) iconRect.getWidth(), (float) iconRect.getHeight());
		}

		GlyphArrangement ga;
		ga.addFittedText (Font (iconRect.getHeight() * 0.9f, Font::bold),
						  String::charToString (character),
						  (float) iconRect.getX(), (float) iconRect.getY(),
						  (float) iconRect.getWidth(), (float) iconRect.getHeight(),
						  Justification::centred, false);
		ga.createPath (icon);

		icon.setUsingNonZeroWinding (false);
		g.setColour (Colour (colour));
		g.fillPath (icon);

		iconSpaceUsed = iconWidth;
		alignment = Justification::left;
	}

	g.setColour (alert.findColour (AlertWindow::textColourId));

	textLayout.drawWithin (g,
						   textArea.getX() + iconSpaceUsed, textArea.getY(),
						   textArea.getWidth() - iconSpaceUsed, textArea.getHeight(),
						   alignment.getFlags() | Justification::top);

	g.setColour (alert.findColour (AlertWindow::outlineColourId));
	g.drawRect (0, 0, alert.getWidth(), alert.getHeight());
}

int LookAndFeel::getAlertBoxWindowFlags()
{
	return ComponentPeer::windowAppearsOnTaskbar
			| ComponentPeer::windowHasDropShadow;
}

int LookAndFeel::getAlertWindowButtonHeight()
{
	return 28;
}

const Font LookAndFeel::getAlertWindowFont()
{
	return Font (12.0f);
}

void LookAndFeel::drawProgressBar (Graphics& g, ProgressBar& progressBar,
								   int width, int height,
								   double progress, const String& textToShow)
{
	const Colour background (progressBar.findColour (ProgressBar::backgroundColourId));
	const Colour foreground (progressBar.findColour (ProgressBar::foregroundColourId));

	g.fillAll (background);

	if (progress >= 0.0f && progress < 1.0f)
	{
		drawGlassLozenge (g, 1.0f, 1.0f,
						  (float) jlimit (0.0, width - 2.0, progress * (width - 2.0)),
						  (float) (height - 2),
						  foreground,
						  0.5f, 0.0f,
						  true, true, true, true);
	}
	else
	{
		// spinning bar..
		g.setColour (foreground);

		const int stripeWidth = height * 2;
		const int position = (Time::getMillisecondCounter() / 15) % stripeWidth;

		Path p;

		for (float x = (float) (- position); x < width + stripeWidth; x += stripeWidth)
			p.addQuadrilateral (x, 0.0f,
								x + stripeWidth * 0.5f, 0.0f,
								x, (float) height,
								x - stripeWidth * 0.5f, (float) height);

		Image im (Image::ARGB, width, height, true);

		{
			Graphics g2 (im);
			drawGlassLozenge (g2, 1.0f, 1.0f,
							  (float) (width - 2),
							  (float) (height - 2),
							  foreground,
							  0.5f, 0.0f,
							  true, true, true, true);
		}

		g.setTiledImageFill (im, 0, 0, 0.85f);
		g.fillPath (p);
	}

	if (textToShow.isNotEmpty())
	{
		g.setColour (Colour::contrasting (background, foreground));
		g.setFont (height * 0.6f);

		g.drawText (textToShow, 0, 0, width, height, Justification::centred, false);
	}
}

void LookAndFeel::drawSpinningWaitAnimation (Graphics& g, const Colour& colour, int x, int y, int w, int h)
{
	const float radius = jmin (w, h) * 0.4f;
	const float thickness = radius * 0.15f;
	Path p;
	p.addRoundedRectangle (radius * 0.4f, thickness * -0.5f,
						   radius * 0.6f, thickness,
						   thickness * 0.5f);

	const float cx = x + w * 0.5f;
	const float cy = y + h * 0.5f;

	const uint32 animationIndex = (Time::getMillisecondCounter() / (1000 / 10)) % 12;

	for (int i = 0; i < 12; ++i)
	{
		const int n = (i + 12 - animationIndex) % 12;
		g.setColour (colour.withMultipliedAlpha ((n + 1) / 12.0f));

		g.fillPath (p, AffineTransform::rotation (i * (float_Pi / 6.0f))
									   .translated (cx, cy));
	}
}

void LookAndFeel::drawScrollbarButton (Graphics& g,
									   ScrollBar& scrollbar,
									   int width, int height,
									   int buttonDirection,
									   bool /*isScrollbarVertical*/,
									   bool /*isMouseOverButton*/,
									   bool isButtonDown)
{
	Path p;

	if (buttonDirection == 0)
		p.addTriangle (width * 0.5f, height * 0.2f,
					   width * 0.1f, height * 0.7f,
					   width * 0.9f, height * 0.7f);
	else if (buttonDirection == 1)
		p.addTriangle (width * 0.8f, height * 0.5f,
					   width * 0.3f, height * 0.1f,
					   width * 0.3f, height * 0.9f);
	else if (buttonDirection == 2)
		p.addTriangle (width * 0.5f, height * 0.8f,
					   width * 0.1f, height * 0.3f,
					   width * 0.9f, height * 0.3f);
	else if (buttonDirection == 3)
		p.addTriangle (width * 0.2f, height * 0.5f,
					   width * 0.7f, height * 0.1f,
					   width * 0.7f, height * 0.9f);

	if (isButtonDown)
		g.setColour (scrollbar.findColour (ScrollBar::thumbColourId).contrasting (0.2f));
	else
		g.setColour (scrollbar.findColour (ScrollBar::thumbColourId));

	g.fillPath (p);

	g.setColour (Colour (0x80000000));
	g.strokePath (p, PathStrokeType (0.5f));
}

void LookAndFeel::drawScrollbar (Graphics& g,
								 ScrollBar& scrollbar,
								 int x, int y,
								 int width, int height,
								 bool isScrollbarVertical,
								 int thumbStartPosition,
								 int thumbSize,
								 bool /*isMouseOver*/,
								 bool /*isMouseDown*/)
{
	g.fillAll (scrollbar.findColour (ScrollBar::backgroundColourId));

	Path slotPath, thumbPath;

	const float slotIndent = jmin (width, height) > 15 ? 1.0f : 0.0f;
	const float slotIndentx2 = slotIndent * 2.0f;
	const float thumbIndent = slotIndent + 1.0f;
	const float thumbIndentx2 = thumbIndent * 2.0f;

	float gx1 = 0.0f, gy1 = 0.0f, gx2 = 0.0f, gy2 = 0.0f;

	if (isScrollbarVertical)
	{
		slotPath.addRoundedRectangle (x + slotIndent,
									  y + slotIndent,
									  width - slotIndentx2,
									  height - slotIndentx2,
									  (width - slotIndentx2) * 0.5f);

		if (thumbSize > 0)
			thumbPath.addRoundedRectangle (x + thumbIndent,
										   thumbStartPosition + thumbIndent,
										   width - thumbIndentx2,
										   thumbSize - thumbIndentx2,
										   (width - thumbIndentx2) * 0.5f);
		gx1 = (float) x;
		gx2 = x + width * 0.7f;
	}
	else
	{
		slotPath.addRoundedRectangle (x + slotIndent,
									  y + slotIndent,
									  width - slotIndentx2,
									  height - slotIndentx2,
									  (height - slotIndentx2) * 0.5f);

		if (thumbSize > 0)
			thumbPath.addRoundedRectangle (thumbStartPosition + thumbIndent,
										   y + thumbIndent,
										   thumbSize - thumbIndentx2,
										   height - thumbIndentx2,
										   (height - thumbIndentx2) * 0.5f);
		gy1 = (float) y;
		gy2 = y + height * 0.7f;
	}

	const Colour thumbColour (scrollbar.findColour (ScrollBar::thumbColourId));

	g.setGradientFill (ColourGradient (thumbColour.overlaidWith (Colour (0x44000000)), gx1, gy1,
									   thumbColour.overlaidWith (Colour (0x19000000)), gx2, gy2, false));
	g.fillPath (slotPath);

	if (isScrollbarVertical)
	{
		gx1 = x + width * 0.6f;
		gx2 = (float) x + width;
	}
	else
	{
		gy1 = y + height * 0.6f;
		gy2 = (float) y + height;
	}

	g.setGradientFill (ColourGradient (Colours::transparentBlack,gx1, gy1,
					   Colour (0x19000000), gx2, gy2, false));
	g.fillPath (slotPath);

	g.setColour (thumbColour);
	g.fillPath (thumbPath);

	g.setGradientFill (ColourGradient (Colour (0x10000000), gx1, gy1,
					   Colours::transparentBlack, gx2, gy2, false));

	g.saveState();

	if (isScrollbarVertical)
		g.reduceClipRegion (x + width / 2, y, width, height);
	else
		g.reduceClipRegion (x, y + height / 2, width, height);

	g.fillPath (thumbPath);
	g.restoreState();

	g.setColour (Colour (0x4c000000));
	g.strokePath (thumbPath, PathStrokeType (0.4f));
}

ImageEffectFilter* LookAndFeel::getScrollbarEffect()
{
	return 0;
}

int LookAndFeel::getMinimumScrollbarThumbSize (ScrollBar& scrollbar)
{
	return jmin (scrollbar.getWidth(), scrollbar.getHeight()) * 2;
}

int LookAndFeel::getDefaultScrollbarWidth()
{
	return 18;
}

int LookAndFeel::getScrollbarButtonSize (ScrollBar& scrollbar)
{
	return 2 + (scrollbar.isVertical() ? scrollbar.getWidth()
									   : scrollbar.getHeight());
}

const Path LookAndFeel::getTickShape (const float height)
{
	static const unsigned char tickShapeData[] =
	{
		109,0,224,168,68,0,0,119,67,108,0,224,172,68,0,128,146,67,113,0,192,148,68,0,0,219,67,0,96,110,68,0,224,56,68,113,0,64,51,68,0,32,130,68,0,64,20,68,0,224,
		162,68,108,0,128,3,68,0,128,168,68,113,0,128,221,67,0,192,175,68,0,0,207,67,0,32,179,68,113,0,0,201,67,0,224,173,68,0,0,181,67,0,224,161,68,108,0,128,168,67,
		0,128,154,68,113,0,128,141,67,0,192,138,68,0,128,108,67,0,64,131,68,113,0,0,62,67,0,128,119,68,0,0,5,67,0,128,114,68,113,0,0,102,67,0,192,88,68,0,128,155,
		67,0,192,88,68,113,0,0,190,67,0,192,88,68,0,128,232,67,0,224,131,68,108,0,128,246,67,0,192,139,68,113,0,64,33,68,0,128,87,68,0,0,93,68,0,224,26,68,113,0,
		96,140,68,0,128,188,67,0,224,168,68,0,0,119,67,99,101
	};

	Path p;
	p.loadPathFromData (tickShapeData, sizeof (tickShapeData));
	p.scaleToFit (0, 0, height * 2.0f, height, true);
	return p;
}

const Path LookAndFeel::getCrossShape (const float height)
{
	static const unsigned char crossShapeData[] =
	{
		109,0,0,17,68,0,96,145,68,108,0,192,13,68,0,192,147,68,113,0,0,213,67,0,64,174,68,0,0,168,67,0,64,174,68,113,0,0,104,67,0,64,174,68,0,0,5,67,0,64,
		153,68,113,0,0,18,67,0,64,153,68,0,0,24,67,0,64,153,68,113,0,0,135,67,0,64,153,68,0,128,207,67,0,224,130,68,108,0,0,220,67,0,0,126,68,108,0,0,204,67,
		0,128,117,68,113,0,0,138,67,0,64,82,68,0,0,138,67,0,192,57,68,113,0,0,138,67,0,192,37,68,0,128,210,67,0,64,10,68,113,0,128,220,67,0,64,45,68,0,0,8,
		68,0,128,78,68,108,0,192,14,68,0,0,87,68,108,0,64,20,68,0,0,80,68,113,0,192,57,68,0,0,32,68,0,128,88,68,0,0,32,68,113,0,64,112,68,0,0,32,68,0,
		128,124,68,0,64,68,68,113,0,0,121,68,0,192,67,68,0,128,119,68,0,192,67,68,113,0,192,108,68,0,192,67,68,0,32,89,68,0,96,82,68,113,0,128,69,68,0,0,97,68,
		0,0,56,68,0,64,115,68,108,0,64,49,68,0,128,124,68,108,0,192,55,68,0,96,129,68,113,0,0,92,68,0,224,146,68,0,192,129,68,0,224,146,68,113,0,64,110,68,0,64,
		168,68,0,64,87,68,0,64,168,68,113,0,128,66,68,0,64,168,68,0,64,27,68,0,32,150,68,99,101
	};

	Path p;
	p.loadPathFromData (crossShapeData, sizeof (crossShapeData));
	p.scaleToFit (0, 0, height * 2.0f, height, true);
	return p;
}

void LookAndFeel::drawTreeviewPlusMinusBox (Graphics& g, int x, int y, int w, int h, bool isPlus, bool /*isMouseOver*/)
{
	const int boxSize = ((jmin (16, w, h) << 1) / 3) | 1;

	x += (w - boxSize) >> 1;
	y += (h - boxSize) >> 1;
	w = boxSize;
	h = boxSize;

	g.setColour (Colour (0xe5ffffff));
	g.fillRect (x, y, w, h);

	g.setColour (Colour (0x80000000));
	g.drawRect (x, y, w, h);

	const float size = boxSize / 2 + 1.0f;
	const float centre = (float) (boxSize / 2);

	g.fillRect (x + (w - size) * 0.5f, y + centre, size, 1.0f);

	if (isPlus)
		g.fillRect (x + centre, y + (h - size) * 0.5f, 1.0f, size);
}

void LookAndFeel::drawBubble (Graphics& g,
							  float tipX, float tipY,
							  float boxX, float boxY,
							  float boxW, float boxH)
{
	int side = 0;

	if (tipX < boxX)
		side = 1;
	else if (tipX > boxX + boxW)
		side = 3;
	else if (tipY > boxY + boxH)
		side = 2;

	const float indent = 2.0f;
	Path p;
	p.addBubble (boxX + indent,
				 boxY + indent,
				 boxW - indent * 2.0f,
				 boxH - indent * 2.0f,
				 5.0f,
				 tipX, tipY,
				 side,
				 0.5f,
				 jmin (15.0f, boxW * 0.3f, boxH * 0.3f));

	//xxx need to take comp as param for colour
	g.setColour (findColour (TooltipWindow::backgroundColourId).withAlpha (0.9f));
	g.fillPath (p);

	//xxx as above
	g.setColour (findColour (TooltipWindow::textColourId).withAlpha (0.4f));
	g.strokePath (p, PathStrokeType (1.33f));
}

const Font LookAndFeel::getPopupMenuFont()
{
	return Font (17.0f);
}

void LookAndFeel::getIdealPopupMenuItemSize (const String& text,
											 const bool isSeparator,
											 int standardMenuItemHeight,
											 int& idealWidth,
											 int& idealHeight)
{
	if (isSeparator)
	{
		idealWidth = 50;
		idealHeight = standardMenuItemHeight > 0 ? standardMenuItemHeight / 2 : 10;
	}
	else
	{
		Font font (getPopupMenuFont());

		if (standardMenuItemHeight > 0 && font.getHeight() > standardMenuItemHeight / 1.3f)
			font.setHeight (standardMenuItemHeight / 1.3f);

		idealHeight = standardMenuItemHeight > 0 ? standardMenuItemHeight : roundToInt (font.getHeight() * 1.3f);
		idealWidth = font.getStringWidth (text) + idealHeight * 2;
	}
}

void LookAndFeel::drawPopupMenuBackground (Graphics& g, int width, int height)
{
	const Colour background (findColour (PopupMenu::backgroundColourId));

	g.fillAll (background);
	g.setColour (background.overlaidWith (Colour (0x2badd8e6)));

	for (int i = 0; i < height; i += 3)
		g.fillRect (0, i, width, 1);

#if ! JUCE_MAC
	g.setColour (findColour (PopupMenu::textColourId).withAlpha (0.6f));
	g.drawRect (0, 0, width, height);
#endif
}

void LookAndFeel::drawPopupMenuUpDownArrow (Graphics& g,
											int width, int height,
											bool isScrollUpArrow)
{
	const Colour background (findColour (PopupMenu::backgroundColourId));

	g.setGradientFill (ColourGradient (background, 0.0f, height * 0.5f,
									   background.withAlpha (0.0f),
									   0.0f, isScrollUpArrow ? ((float) height) : 0.0f,
									   false));

	g.fillRect (1, 1, width - 2, height - 2);

	const float hw = width * 0.5f;
	const float arrowW = height * 0.3f;
	const float y1 = height * (isScrollUpArrow ? 0.6f : 0.3f);
	const float y2 = height * (isScrollUpArrow ? 0.3f : 0.6f);

	Path p;
	p.addTriangle (hw - arrowW, y1,
				   hw + arrowW, y1,
				   hw, y2);

	g.setColour (findColour (PopupMenu::textColourId).withAlpha (0.5f));
	g.fillPath (p);
}

void LookAndFeel::drawPopupMenuItem (Graphics& g,
									 int width, int height,
									 const bool isSeparator,
									 const bool isActive,
									 const bool isHighlighted,
									 const bool isTicked,
									 const bool hasSubMenu,
									 const String& text,
									 const String& shortcutKeyText,
									 Image* image,
									 const Colour* const textColourToUse)
{
	const float halfH = height * 0.5f;

	if (isSeparator)
	{
		const float separatorIndent = 5.5f;

		g.setColour (Colour (0x33000000));
		g.drawLine (separatorIndent, halfH, width - separatorIndent, halfH);

		g.setColour (Colour (0x66ffffff));
		g.drawLine (separatorIndent, halfH + 1.0f, width - separatorIndent, halfH + 1.0f);
	}
	else
	{
		Colour textColour (findColour (PopupMenu::textColourId));

		if (textColourToUse != 0)
			textColour = *textColourToUse;

		if (isHighlighted)
		{
			g.setColour (findColour (PopupMenu::highlightedBackgroundColourId));
			g.fillRect (1, 1, width - 2, height - 2);

			g.setColour (findColour (PopupMenu::highlightedTextColourId));
		}
		else
		{
			g.setColour (textColour);
		}

		if (! isActive)
			g.setOpacity (0.3f);

		Font font (getPopupMenuFont());

		if (font.getHeight() > height / 1.3f)
			font.setHeight (height / 1.3f);

		g.setFont (font);

		const int leftBorder = (height * 5) / 4;
		const int rightBorder = 4;

		if (image != 0)
		{
			g.drawImageWithin (image,
							   2, 1, leftBorder - 4, height - 2,
							   RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, false);
		}
		else if (isTicked)
		{
			const Path tick (getTickShape (1.0f));
			const float th = font.getAscent();
			const float ty = halfH - th * 0.5f;

			g.fillPath (tick, tick.getTransformToScaleToFit (2.0f, ty, (float) (leftBorder - 4),
															 th, true));
		}

		g.drawFittedText (text,
						  leftBorder, 0,
						  width - (leftBorder + rightBorder), height,
						  Justification::centredLeft, 1);

		if (shortcutKeyText.isNotEmpty())
		{
			Font f2 (font);
			f2.setHeight (f2.getHeight() * 0.75f);
			f2.setHorizontalScale (0.95f);
			g.setFont (f2);

			g.drawText (shortcutKeyText,
						leftBorder,
						0,
						width - (leftBorder + rightBorder + 4),
						height,
						Justification::centredRight,
						true);
		}

		if (hasSubMenu)
		{
			const float arrowH = 0.6f * getPopupMenuFont().getAscent();
			const float x = width - height * 0.6f;

			Path p;
			p.addTriangle (x, halfH - arrowH * 0.5f,
						   x, halfH + arrowH * 0.5f,
						   x + arrowH * 0.6f, halfH);

			g.fillPath (p);
		}
	}
}

int LookAndFeel::getMenuWindowFlags()
{
	return ComponentPeer::windowHasDropShadow;
}

void LookAndFeel::drawMenuBarBackground (Graphics& g, int width, int height,
										 bool, MenuBarComponent& menuBar)
{
	const Colour baseColour (createBaseColour (menuBar.findColour (PopupMenu::backgroundColourId), false, false, false));

	if (menuBar.isEnabled())
	{
		drawShinyButtonShape (g,
							  -4.0f, 0.0f,
							  width + 8.0f, (float) height,
							  0.0f,
							  baseColour,
							  0.4f,
							  true, true, true, true);
	}
	else
	{
		g.fillAll (baseColour);
	}
}

const Font LookAndFeel::getMenuBarFont (MenuBarComponent& menuBar, int /*itemIndex*/, const String& /*itemText*/)
{
	return Font (menuBar.getHeight() * 0.7f);
}

int LookAndFeel::getMenuBarItemWidth (MenuBarComponent& menuBar, int itemIndex, const String& itemText)
{
	return getMenuBarFont (menuBar, itemIndex, itemText)
			.getStringWidth (itemText) + menuBar.getHeight();
}

void LookAndFeel::drawMenuBarItem (Graphics& g,
								   int width, int height,
								   int itemIndex,
								   const String& itemText,
								   bool isMouseOverItem,
								   bool isMenuOpen,
								   bool /*isMouseOverBar*/,
								   MenuBarComponent& menuBar)
{
	if (! menuBar.isEnabled())
	{
		g.setColour (menuBar.findColour (PopupMenu::textColourId)
							.withMultipliedAlpha (0.5f));
	}
	else if (isMenuOpen || isMouseOverItem)
	{
		g.fillAll (menuBar.findColour (PopupMenu::highlightedBackgroundColourId));
		g.setColour (menuBar.findColour (PopupMenu::highlightedTextColourId));
	}
	else
	{
		g.setColour (menuBar.findColour (PopupMenu::textColourId));
	}

	g.setFont (getMenuBarFont (menuBar, itemIndex, itemText));
	g.drawFittedText (itemText, 0, 0, width, height, Justification::centred, 1);
}

void LookAndFeel::fillTextEditorBackground (Graphics& g, int /*width*/, int /*height*/,
											TextEditor& textEditor)
{
	g.fillAll (textEditor.findColour (TextEditor::backgroundColourId));
}

void LookAndFeel::drawTextEditorOutline (Graphics& g, int width, int height, TextEditor& textEditor)
{
	if (textEditor.isEnabled())
	{
		if (textEditor.hasKeyboardFocus (true) && ! textEditor.isReadOnly())
		{
			const int border = 2;

			g.setColour (textEditor.findColour (TextEditor::focusedOutlineColourId));
			g.drawRect (0, 0, width, height, border);

			g.setOpacity (1.0f);
			const Colour shadowColour (textEditor.findColour (TextEditor::shadowColourId).withMultipliedAlpha (0.75f));
			g.drawBevel (0, 0, width, height + 2, border + 2, shadowColour, shadowColour);
		}
		else
		{
			g.setColour (textEditor.findColour (TextEditor::outlineColourId));
			g.drawRect (0, 0, width, height);

			g.setOpacity (1.0f);
			const Colour shadowColour (textEditor.findColour (TextEditor::shadowColourId));
			g.drawBevel (0, 0, width, height + 2, 3, shadowColour, shadowColour);
		}
	}
}

void LookAndFeel::drawComboBox (Graphics& g, int width, int height,
								const bool isButtonDown,
								int buttonX, int buttonY,
								int buttonW, int buttonH,
								ComboBox& box)
{
	g.fillAll (box.findColour (ComboBox::backgroundColourId));

	if (box.isEnabled() && box.hasKeyboardFocus (false))
	{
		g.setColour (box.findColour (TextButton::buttonColourId));
		g.drawRect (0, 0, width, height, 2);
	}
	else
	{
		g.setColour (box.findColour (ComboBox::outlineColourId));
		g.drawRect (0, 0, width, height);
	}

	const float outlineThickness = box.isEnabled() ? (isButtonDown ? 1.2f : 0.5f) : 0.3f;

	const Colour baseColour (createBaseColour (box.findColour (ComboBox::buttonColourId),
											   box.hasKeyboardFocus (true),
											   false, isButtonDown)
							   .withMultipliedAlpha (box.isEnabled() ? 1.0f : 0.5f));

	drawGlassLozenge (g,
					  buttonX + outlineThickness, buttonY + outlineThickness,
					  buttonW - outlineThickness * 2.0f, buttonH - outlineThickness * 2.0f,
					  baseColour, outlineThickness, -1.0f,
					  true, true, true, true);

	if (box.isEnabled())
	{
		const float arrowX = 0.3f;
		const float arrowH = 0.2f;

		Path p;
		p.addTriangle (buttonX + buttonW * 0.5f,		buttonY + buttonH * (0.45f - arrowH),
					   buttonX + buttonW * (1.0f - arrowX), buttonY + buttonH * 0.45f,
					   buttonX + buttonW * arrowX,	  buttonY + buttonH * 0.45f);

		p.addTriangle (buttonX + buttonW * 0.5f,		buttonY + buttonH * (0.55f + arrowH),
					   buttonX + buttonW * (1.0f - arrowX), buttonY + buttonH * 0.55f,
					   buttonX + buttonW * arrowX,	  buttonY + buttonH * 0.55f);

		g.setColour (box.findColour (ComboBox::arrowColourId));
		g.fillPath (p);
	}
}

const Font LookAndFeel::getComboBoxFont (ComboBox& box)
{
	return Font (jmin (15.0f, box.getHeight() * 0.85f));
}

Label* LookAndFeel::createComboBoxTextBox (ComboBox&)
{
	return new Label (String::empty, String::empty);
}

void LookAndFeel::positionComboBoxText (ComboBox& box, Label& label)
{
	label.setBounds (1, 1,
					 box.getWidth() + 3 - box.getHeight(),
					 box.getHeight() - 2);

	label.setFont (getComboBoxFont (box));
}

void LookAndFeel::drawLabel (Graphics& g, Label& label)
{
	g.fillAll (label.findColour (Label::backgroundColourId));

	if (! label.isBeingEdited())
	{
		const float alpha = label.isEnabled() ? 1.0f : 0.5f;

		g.setColour (label.findColour (Label::textColourId).withMultipliedAlpha (alpha));
		g.setFont (label.getFont());
		g.drawFittedText (label.getText(),
						  label.getHorizontalBorderSize(),
						  label.getVerticalBorderSize(),
						  label.getWidth() - 2 * label.getHorizontalBorderSize(),
						  label.getHeight() - 2 * label.getVerticalBorderSize(),
						  label.getJustificationType(),
						  jmax (1, (int) (label.getHeight() / label.getFont().getHeight())),
						  label.getMinimumHorizontalScale());

		g.setColour (label.findColour (Label::outlineColourId).withMultipliedAlpha (alpha));
		g.drawRect (0, 0, label.getWidth(), label.getHeight());
	}
	else if (label.isEnabled())
	{
		g.setColour (label.findColour (Label::outlineColourId));
		g.drawRect (0, 0, label.getWidth(), label.getHeight());
	}
}

void LookAndFeel::drawLinearSliderBackground (Graphics& g,
											  int x, int y,
											  int width, int height,
											  float /*sliderPos*/,
											  float /*minSliderPos*/,
											  float /*maxSliderPos*/,
											  const Slider::SliderStyle /*style*/,
											  Slider& slider)
{
	const float sliderRadius = (float) (getSliderThumbRadius (slider) - 2);

	const Colour trackColour (slider.findColour (Slider::trackColourId));
	const Colour gradCol1 (trackColour.overlaidWith (Colours::black.withAlpha (slider.isEnabled() ? 0.25f : 0.13f)));
	const Colour gradCol2 (trackColour.overlaidWith (Colour (0x14000000)));
	Path indent;

	if (slider.isHorizontal())
	{
		const float iy = y + height * 0.5f - sliderRadius * 0.5f;
		const float ih = sliderRadius;

		g.setGradientFill (ColourGradient (gradCol1, 0.0f, iy,
										   gradCol2, 0.0f, iy + ih, false));

		indent.addRoundedRectangle (x - sliderRadius * 0.5f, iy,
									width + sliderRadius, ih,
									5.0f);
		g.fillPath (indent);
	}
	else
	{
		const float ix = x + width * 0.5f - sliderRadius * 0.5f;
		const float iw = sliderRadius;

		g.setGradientFill (ColourGradient (gradCol1, ix, 0.0f,
										   gradCol2, ix + iw, 0.0f, false));

		indent.addRoundedRectangle (ix, y - sliderRadius * 0.5f,
									iw, height + sliderRadius,
									5.0f);
		g.fillPath (indent);
	}

	g.setColour (Colour (0x4c000000));
	g.strokePath (indent, PathStrokeType (0.5f));
}

void LookAndFeel::drawLinearSliderThumb (Graphics& g,
										 int x, int y,
										 int width, int height,
										 float sliderPos,
										 float minSliderPos,
										 float maxSliderPos,
										 const Slider::SliderStyle style,
										 Slider& slider)
{
	const float sliderRadius = (float) (getSliderThumbRadius (slider) - 2);

	Colour knobColour (createBaseColour (slider.findColour (Slider::thumbColourId),
										 slider.hasKeyboardFocus (false) && slider.isEnabled(),
										 slider.isMouseOverOrDragging() && slider.isEnabled(),
										 slider.isMouseButtonDown() && slider.isEnabled()));

	const float outlineThickness = slider.isEnabled() ? 0.8f : 0.3f;

	if (style == Slider::LinearHorizontal || style == Slider::LinearVertical)
	{
		float kx, ky;

		if (style == Slider::LinearVertical)
		{
			kx = x + width * 0.5f;
			ky = sliderPos;
		}
		else
		{
			kx = sliderPos;
			ky = y + height * 0.5f;
		}

		drawGlassSphere (g,
						 kx - sliderRadius,
						 ky - sliderRadius,
						 sliderRadius * 2.0f,
						 knobColour, outlineThickness);
	}
	else
	{
		if (style == Slider::ThreeValueVertical)
		{
			drawGlassSphere (g, x + width * 0.5f - sliderRadius,
							 sliderPos - sliderRadius,
							 sliderRadius * 2.0f,
							 knobColour, outlineThickness);
		}
		else if (style == Slider::ThreeValueHorizontal)
		{
			drawGlassSphere (g,sliderPos - sliderRadius,
							 y + height * 0.5f - sliderRadius,
							 sliderRadius * 2.0f,
							 knobColour, outlineThickness);
		}

		if (style == Slider::TwoValueVertical || style == Slider::ThreeValueVertical)
		{
			const float sr = jmin (sliderRadius, width * 0.4f);

			drawGlassPointer (g, jmax (0.0f, x + width * 0.5f - sliderRadius * 2.0f),
							  minSliderPos - sliderRadius,
							  sliderRadius * 2.0f, knobColour, outlineThickness, 1);

			drawGlassPointer (g, jmin (x + width - sliderRadius * 2.0f, x + width * 0.5f), maxSliderPos - sr,
							  sliderRadius * 2.0f, knobColour, outlineThickness, 3);
		}
		else if (style == Slider::TwoValueHorizontal || style == Slider::ThreeValueHorizontal)
		{
			const float sr = jmin (sliderRadius, height * 0.4f);

			drawGlassPointer (g, minSliderPos - sr,
							  jmax (0.0f, y + height * 0.5f - sliderRadius * 2.0f),
							  sliderRadius * 2.0f, knobColour, outlineThickness, 2);

			drawGlassPointer (g, maxSliderPos - sliderRadius,
							  jmin (y + height - sliderRadius * 2.0f, y + height * 0.5f),
							  sliderRadius * 2.0f, knobColour, outlineThickness, 4);
		}
	}
}

void LookAndFeel::drawLinearSlider (Graphics& g,
									int x, int y,
									int width, int height,
									float sliderPos,
									float minSliderPos,
									float maxSliderPos,
									const Slider::SliderStyle style,
									Slider& slider)
{
	g.fillAll (slider.findColour (Slider::backgroundColourId));

	if (style == Slider::LinearBar)
	{
		const bool isMouseOver = slider.isMouseOverOrDragging() && slider.isEnabled();

		Colour baseColour (createBaseColour (slider.findColour (Slider::thumbColourId)
												   .withMultipliedSaturation (slider.isEnabled() ? 1.0f : 0.5f),
											 false,
											 isMouseOver,
											 isMouseOver || slider.isMouseButtonDown()));

		drawShinyButtonShape (g,
							  (float) x, (float) y, sliderPos - (float) x, (float) height, 0.0f,
							  baseColour,
							  slider.isEnabled() ? 0.9f : 0.3f,
							  true, true, true, true);
	}
	else
	{
		drawLinearSliderBackground (g, x, y, width, height, sliderPos, minSliderPos, maxSliderPos, style, slider);
		drawLinearSliderThumb (g, x, y, width, height, sliderPos, minSliderPos, maxSliderPos, style, slider);
	}
}

int LookAndFeel::getSliderThumbRadius (Slider& slider)
{
	return jmin (7,
				 slider.getHeight() / 2,
				 slider.getWidth() / 2) + 2;
}

void LookAndFeel::drawRotarySlider (Graphics& g,
									int x, int y,
									int width, int height,
									float sliderPos,
									const float rotaryStartAngle,
									const float rotaryEndAngle,
									Slider& slider)
{
	const float radius = jmin (width / 2, height / 2) - 2.0f;
	const float centreX = x + width * 0.5f;
	const float centreY = y + height * 0.5f;
	const float rx = centreX - radius;
	const float ry = centreY - radius;
	const float rw = radius * 2.0f;
	const float angle = rotaryStartAngle + sliderPos * (rotaryEndAngle - rotaryStartAngle);
	const bool isMouseOver = slider.isMouseOverOrDragging() && slider.isEnabled();

	if (radius > 12.0f)
	{
		if (slider.isEnabled())
			g.setColour (slider.findColour (Slider::rotarySliderFillColourId).withAlpha (isMouseOver ? 1.0f : 0.7f));
		else
			g.setColour (Colour (0x80808080));

		const float thickness = 0.7f;

		{
			Path filledArc;
			filledArc.addPieSegment (rx, ry, rw, rw,
									rotaryStartAngle,
									angle,
									thickness);

			g.fillPath (filledArc);
		}

		if (thickness > 0)
		{
			const float innerRadius = radius * 0.2f;
			Path p;
			p.addTriangle (-innerRadius, 0.0f,
						   0.0f, -radius * thickness * 1.1f,
						   innerRadius, 0.0f);

			p.addEllipse (-innerRadius, -innerRadius, innerRadius * 2.0f, innerRadius * 2.0f);

			g.fillPath (p, AffineTransform::rotation (angle).translated (centreX, centreY));
		}

		if (slider.isEnabled())
			g.setColour (slider.findColour (Slider::rotarySliderOutlineColourId));
		else
			g.setColour (Colour (0x80808080));

		Path outlineArc;
		outlineArc.addPieSegment (rx, ry, rw, rw, rotaryStartAngle, rotaryEndAngle, thickness);
		outlineArc.closeSubPath();

		g.strokePath (outlineArc, PathStrokeType (slider.isEnabled() ? (isMouseOver ? 2.0f : 1.2f) : 0.3f));
	}
	else
	{
		if (slider.isEnabled())
			g.setColour (slider.findColour (Slider::rotarySliderFillColourId).withAlpha (isMouseOver ? 1.0f : 0.7f));
		else
			g.setColour (Colour (0x80808080));

		Path p;
		p.addEllipse (-0.4f * rw, -0.4f * rw, rw * 0.8f, rw * 0.8f);
		PathStrokeType (rw * 0.1f).createStrokedPath (p, p);

		p.addLineSegment (0.0f, 0.0f, 0.0f, -radius, rw * 0.2f);

		g.fillPath (p, AffineTransform::rotation (angle).translated (centreX, centreY));
	}
}

Button* LookAndFeel::createSliderButton (const bool isIncrement)
{
	return new TextButton (isIncrement ? "+" : "-", String::empty);
}

class SliderLabelComp : public Label
{
public:
	SliderLabelComp() : Label (String::empty, String::empty) {}
	~SliderLabelComp()	{}

	void mouseWheelMove (const MouseEvent&, float, float) {}
};

Label* LookAndFeel::createSliderTextBox (Slider& slider)
{
	Label* const l = new SliderLabelComp();

	l->setJustificationType (Justification::centred);

	l->setColour (Label::textColourId, slider.findColour (Slider::textBoxTextColourId));

	l->setColour (Label::backgroundColourId,
				  (slider.getSliderStyle() == Slider::LinearBar) ? Colours::transparentBlack
																 : slider.findColour (Slider::textBoxBackgroundColourId));
	l->setColour (Label::outlineColourId, slider.findColour (Slider::textBoxOutlineColourId));

	l->setColour (TextEditor::textColourId, slider.findColour (Slider::textBoxTextColourId));

	l->setColour (TextEditor::backgroundColourId,
				  slider.findColour (Slider::textBoxBackgroundColourId)
						.withAlpha (slider.getSliderStyle() == Slider::LinearBar ? 0.7f : 1.0f));

	l->setColour (TextEditor::outlineColourId, slider.findColour (Slider::textBoxOutlineColourId));

	return l;
}

ImageEffectFilter* LookAndFeel::getSliderEffect()
{
	return 0;
}

static const TextLayout layoutTooltipText (const String& text) throw()
{
	const float tooltipFontSize = 12.0f;
	const int maxToolTipWidth = 400;

	const Font f (tooltipFontSize, Font::bold);
	TextLayout tl (text, f);
	tl.layout (maxToolTipWidth, Justification::left, true);

	return tl;
}

void LookAndFeel::getTooltipSize (const String& tipText, int& width, int& height)
{
	const TextLayout tl (layoutTooltipText (tipText));

	width = tl.getWidth() + 14;
	height = tl.getHeight() + 6;
}

void LookAndFeel::drawTooltip (Graphics& g, const String& text, int width, int height)
{
	g.fillAll (findColour (TooltipWindow::backgroundColourId));

	const Colour textCol (findColour (TooltipWindow::textColourId));

#if ! JUCE_MAC // The mac windows already have a non-optional 1 pix outline, so don't double it here..
	g.setColour (findColour (TooltipWindow::outlineColourId));
	g.drawRect (0, 0, width, height, 1);
#endif

	const TextLayout tl (layoutTooltipText (text));

	g.setColour (findColour (TooltipWindow::textColourId));
	tl.drawWithin (g, 0, 0, width, height, Justification::centred);
}

Button* LookAndFeel::createFilenameComponentBrowseButton (const String& text)
{
	return new TextButton (text, TRANS("click to browse for a different file"));
}

void LookAndFeel::layoutFilenameComponent (FilenameComponent& filenameComp,
										   ComboBox* filenameBox,
										   Button* browseButton)
{
	browseButton->setSize (80, filenameComp.getHeight());

	TextButton* const tb = dynamic_cast <TextButton*> (browseButton);

	if (tb != 0)
		tb->changeWidthToFitText();

	browseButton->setTopRightPosition (filenameComp.getWidth(), 0);

	filenameBox->setBounds (0, 0, browseButton->getX(), filenameComp.getHeight());
}

void LookAndFeel::drawImageButton (Graphics& g, Image* image,
								   int imageX, int imageY, int imageW, int imageH,
								   const Colour& overlayColour,
								   float imageOpacity,
								   ImageButton& button)
{
	if (! button.isEnabled())
		imageOpacity *= 0.3f;

	if (! overlayColour.isOpaque())
	{
		g.setOpacity (imageOpacity);

		g.drawImage (image, imageX, imageY, imageW, imageH,
					 0, 0, image->getWidth(), image->getHeight(), false);
	}

	if (! overlayColour.isTransparent())
	{
		g.setColour (overlayColour);

		g.drawImage (image, imageX, imageY, imageW, imageH,
					 0, 0, image->getWidth(), image->getHeight(), true);
	}
}

void LookAndFeel::drawCornerResizer (Graphics& g,
									 int w, int h,
									 bool /*isMouseOver*/,
									 bool /*isMouseDragging*/)
{
	const float lineThickness = jmin (w, h) * 0.075f;

	for (float i = 0.0f; i < 1.0f; i += 0.3f)
	{
		g.setColour (Colours::lightgrey);

		g.drawLine (w * i,
					h + 1.0f,
					w + 1.0f,
					h * i,
					lineThickness);

		g.setColour (Colours::darkgrey);

		g.drawLine (w * i + lineThickness,
					h + 1.0f,
					w + 1.0f,
					h * i + lineThickness,
					lineThickness);
	}
}

void LookAndFeel::drawResizableFrame (Graphics&, int /*w*/, int /*h*/,
									  const BorderSize& /*borders*/)
{
}

void LookAndFeel::fillResizableWindowBackground (Graphics& g, int /*w*/, int /*h*/,
												 const BorderSize& /*border*/, ResizableWindow& window)
{
   g.fillAll (window.getBackgroundColour());
}

void LookAndFeel::drawResizableWindowBorder (Graphics& g, int w, int h,
											 const BorderSize& border, ResizableWindow&)
{
	g.setColour (Colour (0x80000000));
	g.drawRect (0, 0, w, h);

	g.setColour (Colour (0x19000000));
	g.drawRect (border.getLeft() - 1,
				border.getTop() - 1,
				w + 2 - border.getLeftAndRight(),
				h + 2 - border.getTopAndBottom());
}

void LookAndFeel::drawDocumentWindowTitleBar (DocumentWindow& window,
											  Graphics& g, int w, int h,
											  int titleSpaceX, int titleSpaceW,
											  const Image* icon,
											  bool drawTitleTextOnLeft)
{
	const bool isActive = window.isActiveWindow();

	g.setGradientFill (ColourGradient (window.getBackgroundColour(),
									   0.0f, 0.0f,
									   window.getBackgroundColour().contrasting (isActive ? 0.15f : 0.05f),
									   0.0f, (float) h, false));
	g.fillAll();

	Font font (h * 0.65f, Font::bold);
	g.setFont (font);

	int textW = font.getStringWidth (window.getName());
	int iconW = 0;
	int iconH = 0;

	if (icon != 0)
	{
		iconH = (int) font.getHeight();
		iconW = icon->getWidth() * iconH / icon->getHeight() + 4;
	}

	textW = jmin (titleSpaceW, textW + iconW);
	int textX = drawTitleTextOnLeft ? titleSpaceX
									: jmax (titleSpaceX, (w - textW) / 2);

	if (textX + textW > titleSpaceX + titleSpaceW)
		textX = titleSpaceX + titleSpaceW - textW;

	if (icon != 0)
	{
		g.setOpacity (isActive ? 1.0f : 0.6f);
		g.drawImageWithin (icon, textX, (h - iconH) / 2, iconW, iconH,
						   RectanglePlacement::centred, false);
		textX += iconW;
		textW -= iconW;
	}

	if (window.isColourSpecified (DocumentWindow::textColourId) || isColourSpecified (DocumentWindow::textColourId))
		g.setColour (findColour (DocumentWindow::textColourId));
	else
		g.setColour (window.getBackgroundColour().contrasting (isActive ? 0.7f : 0.4f));

	g.drawText (window.getName(), textX, 0, textW, h, Justification::centredLeft, true);
}

class GlassWindowButton   : public Button
{
public:

	GlassWindowButton (const String& name, const Colour& col,
					   const Path& normalShape_,
					   const Path& toggledShape_) throw()
		: Button (name),
		  colour (col),
		  normalShape (normalShape_),
		  toggledShape (toggledShape_)
	{
	}

	~GlassWindowButton()
	{
	}

	void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
	{
		float alpha = isMouseOverButton ? (isButtonDown ? 1.0f : 0.8f) : 0.55f;

		if (! isEnabled())
			alpha *= 0.5f;

		float x = 0, y = 0, diam;

		if (getWidth() < getHeight())
		{
			diam = (float) getWidth();
			y = (getHeight() - getWidth()) * 0.5f;
		}
		else
		{
			diam = (float) getHeight();
			y = (getWidth() - getHeight()) * 0.5f;
		}

		x += diam * 0.05f;
		y += diam * 0.05f;
		diam *= 0.9f;

		g.setGradientFill (ColourGradient (Colour::greyLevel (0.9f).withAlpha (alpha), 0, y + diam,
										   Colour::greyLevel (0.6f).withAlpha (alpha), 0, y, false));
		g.fillEllipse (x, y, diam, diam);

		x += 2.0f;
		y += 2.0f;
		diam -= 4.0f;

		LookAndFeel::drawGlassSphere (g, x, y, diam, colour.withAlpha (alpha), 1.0f);

		Path& p = getToggleState() ? toggledShape : normalShape;

		const AffineTransform t (p.getTransformToScaleToFit (x + diam * 0.3f, y + diam * 0.3f,
															 diam * 0.4f, diam * 0.4f, true));

		g.setColour (Colours::black.withAlpha (alpha * 0.6f));
		g.fillPath (p, t);
	}

	juce_UseDebuggingNewOperator

private:
	Colour colour;
	Path normalShape, toggledShape;

	GlassWindowButton (const GlassWindowButton&);
	GlassWindowButton& operator= (const GlassWindowButton&);
};

Button* LookAndFeel::createDocumentWindowButton (int buttonType)
{
	Path shape;
	const float crossThickness = 0.25f;

	if (buttonType == DocumentWindow::closeButton)
	{
		shape.addLineSegment (0.0f, 0.0f, 1.0f, 1.0f, crossThickness * 1.4f);
		shape.addLineSegment (1.0f, 0.0f, 0.0f, 1.0f, crossThickness * 1.4f);

		return new GlassWindowButton ("close", Colour (0xffdd1100), shape, shape);
	}
	else if (buttonType == DocumentWindow::minimiseButton)
	{
		shape.addLineSegment (0.0f, 0.5f, 1.0f, 0.5f, crossThickness);

		return new GlassWindowButton ("minimise", Colour (0xffaa8811), shape, shape);
	}
	else if (buttonType == DocumentWindow::maximiseButton)
	{
		shape.addLineSegment (0.5f, 0.0f, 0.5f, 1.0f, crossThickness);
		shape.addLineSegment (0.0f, 0.5f, 1.0f, 0.5f, crossThickness);

		Path fullscreenShape;
		fullscreenShape.startNewSubPath (45.0f, 100.0f);
		fullscreenShape.lineTo (0.0f, 100.0f);
		fullscreenShape.lineTo (0.0f, 0.0f);
		fullscreenShape.lineTo (100.0f, 0.0f);
		fullscreenShape.lineTo (100.0f, 45.0f);
		fullscreenShape.addRectangle (45.0f, 45.0f, 100.0f, 100.0f);
		PathStrokeType (30.0f).createStrokedPath (fullscreenShape, fullscreenShape);

		return new GlassWindowButton ("maximise", Colour (0xff119911), shape, fullscreenShape);
	}

	jassertfalse
	return 0;
}

void LookAndFeel::positionDocumentWindowButtons (DocumentWindow&,
												 int titleBarX,
												 int titleBarY,
												 int titleBarW,
												 int titleBarH,
												 Button* minimiseButton,
												 Button* maximiseButton,
												 Button* closeButton,
												 bool positionTitleBarButtonsOnLeft)
{
	const int buttonW = titleBarH - titleBarH / 8;

	int x = positionTitleBarButtonsOnLeft ? titleBarX + 4
										  : titleBarX + titleBarW - buttonW - buttonW / 4;

	if (closeButton != 0)
	{
		closeButton->setBounds (x, titleBarY, buttonW, titleBarH);
		x += positionTitleBarButtonsOnLeft ? buttonW : -(buttonW + buttonW / 4);
	}

	if (positionTitleBarButtonsOnLeft)
		swapVariables (minimiseButton, maximiseButton);

	if (maximiseButton != 0)
	{
		maximiseButton->setBounds (x, titleBarY, buttonW, titleBarH);
		x += positionTitleBarButtonsOnLeft ? buttonW : -buttonW;
	}

	if (minimiseButton != 0)
		minimiseButton->setBounds (x, titleBarY, buttonW, titleBarH);
}

int LookAndFeel::getDefaultMenuBarHeight()
{
	return 24;
}

DropShadower* LookAndFeel::createDropShadowerForComponent (Component*)
{
	return new DropShadower (0.4f, 1, 5, 10);
}

void LookAndFeel::drawStretchableLayoutResizerBar (Graphics& g,
												   int w, int h,
												   bool /*isVerticalBar*/,
												   bool isMouseOver,
												   bool isMouseDragging)
{
	float alpha = 0.5f;

	if (isMouseOver || isMouseDragging)
	{
		g.fillAll (Colour (0x190000ff));
		alpha = 1.0f;
	}

	const float cx = w * 0.5f;
	const float cy = h * 0.5f;
	const float cr = jmin (w, h) * 0.4f;

	g.setGradientFill (ColourGradient (Colours::white.withAlpha (alpha), cx + cr * 0.1f, cy + cr,
									   Colours::black.withAlpha (alpha), cx, cy - cr * 4.0f,
									   true));

	g.fillEllipse (cx - cr, cy - cr, cr * 2.0f, cr * 2.0f);
}

void LookAndFeel::drawGroupComponentOutline (Graphics& g, int width, int height,
											 const String& text,
											 const Justification& position,
											 GroupComponent& group)
{
	const float textH = 15.0f;
	const float indent = 3.0f;
	const float textEdgeGap = 4.0f;
	float cs = 5.0f;

	Font f (textH);

	Path p;
	float x = indent;
	float y = f.getAscent() - 3.0f;
	float w = jmax (0.0f, width - x * 2.0f);
	float h = jmax (0.0f, height - y  - indent);
	cs = jmin (cs, w * 0.5f, h * 0.5f);
	const float cs2 = 2.0f * cs;

	float textW = text.isEmpty() ? 0 : jlimit (0.0f, jmax (0.0f, w - cs2 - textEdgeGap * 2), f.getStringWidth (text) + textEdgeGap * 2.0f);
	float textX = cs + textEdgeGap;

	if (position.testFlags (Justification::horizontallyCentred))
		textX = cs + (w - cs2 - textW) * 0.5f;
	else if (position.testFlags (Justification::right))
		textX = w - cs - textW - textEdgeGap;

	p.startNewSubPath (x + textX + textW, y);
	p.lineTo (x + w - cs, y);

	p.addArc (x + w - cs2, y, cs2, cs2, 0, float_Pi * 0.5f);
	p.lineTo (x + w, y + h - cs);

	p.addArc (x + w - cs2, y + h - cs2, cs2, cs2, float_Pi * 0.5f, float_Pi);
	p.lineTo (x + cs, y + h);

	p.addArc (x, y + h - cs2, cs2, cs2, float_Pi, float_Pi * 1.5f);
	p.lineTo (x, y + cs);

	p.addArc (x, y, cs2, cs2, float_Pi * 1.5f, float_Pi * 2.0f);
	p.lineTo (x + textX, y);

	const float alpha = group.isEnabled() ? 1.0f : 0.5f;

	g.setColour (group.findColour (GroupComponent::outlineColourId)
					.withMultipliedAlpha (alpha));

	g.strokePath (p, PathStrokeType (2.0f));

	g.setColour (group.findColour (GroupComponent::textColourId)
					.withMultipliedAlpha (alpha));
	g.setFont (f);
	g.drawText (text,
				roundToInt (x + textX), 0,
				roundToInt (textW),
				roundToInt (textH),
				Justification::centred, true);
}

int LookAndFeel::getTabButtonOverlap (int tabDepth)
{
	return 1 + tabDepth / 3;
}

int LookAndFeel::getTabButtonSpaceAroundImage()
{
	return 4;
}

void LookAndFeel::createTabButtonShape (Path& p,
										int width, int height,
										int /*tabIndex*/,
										const String& /*text*/,
										Button& /*button*/,
										TabbedButtonBar::Orientation orientation,
										const bool /*isMouseOver*/,
										const bool /*isMouseDown*/,
										const bool /*isFrontTab*/)
{
	const float w = (float) width;
	const float h = (float) height;

	float length = w;
	float depth = h;

	if (orientation == TabbedButtonBar::TabsAtLeft
		 || orientation == TabbedButtonBar::TabsAtRight)
	{
		swapVariables (length, depth);
	}

	const float indent = (float) getTabButtonOverlap ((int) depth);
	const float overhang = 4.0f;

	if (orientation == TabbedButtonBar::TabsAtLeft)
	{
		p.startNewSubPath (w, 0.0f);
		p.lineTo (0.0f, indent);
		p.lineTo (0.0f, h - indent);
		p.lineTo (w, h);
		p.lineTo (w + overhang, h + overhang);
		p.lineTo (w + overhang, -overhang);
	}
	else if (orientation == TabbedButtonBar::TabsAtRight)
	{
		p.startNewSubPath (0.0f, 0.0f);
		p.lineTo (w, indent);
		p.lineTo (w, h - indent);
		p.lineTo (0.0f, h);
		p.lineTo (-overhang, h + overhang);
		p.lineTo (-overhang, -overhang);
	}
	else if (orientation == TabbedButtonBar::TabsAtBottom)
	{
		p.startNewSubPath (0.0f, 0.0f);
		p.lineTo (indent, h);
		p.lineTo (w - indent, h);
		p.lineTo (w, 0.0f);
		p.lineTo (w + overhang, -overhang);
		p.lineTo (-overhang, -overhang);
	}
	else
	{
		p.startNewSubPath (0.0f, h);
		p.lineTo (indent, 0.0f);
		p.lineTo (w - indent, 0.0f);
		p.lineTo (w, h);
		p.lineTo (w + overhang, h + overhang);
		p.lineTo (-overhang, h + overhang);
	}

	p.closeSubPath();

	p = p.createPathWithRoundedCorners (3.0f);
}

void LookAndFeel::fillTabButtonShape (Graphics& g,
									  const Path& path,
									  const Colour& preferredColour,
									  int /*tabIndex*/,
									  const String& /*text*/,
									  Button& button,
									  TabbedButtonBar::Orientation /*orientation*/,
									  const bool /*isMouseOver*/,
									  const bool /*isMouseDown*/,
									  const bool isFrontTab)
{
	g.setColour (isFrontTab ? preferredColour
							: preferredColour.withMultipliedAlpha (0.9f));

	g.fillPath (path);

	g.setColour (button.findColour (isFrontTab ? TabbedButtonBar::frontOutlineColourId
											   : TabbedButtonBar::tabOutlineColourId, false)
					.withMultipliedAlpha (button.isEnabled() ? 1.0f : 0.5f));

	g.strokePath (path, PathStrokeType (isFrontTab ? 1.0f : 0.5f));
}

void LookAndFeel::drawTabButtonText (Graphics& g,
									 int x, int y, int w, int h,
									 const Colour& preferredBackgroundColour,
									 int /*tabIndex*/,
									 const String& text,
									 Button& button,
									 TabbedButtonBar::Orientation orientation,
									 const bool isMouseOver,
									 const bool isMouseDown,
									 const bool isFrontTab)
{
	int length = w;
	int depth = h;

	if (orientation == TabbedButtonBar::TabsAtLeft
		 || orientation == TabbedButtonBar::TabsAtRight)
	{
		swapVariables (length, depth);
	}

	Font font (depth * 0.6f);
	font.setUnderline (button.hasKeyboardFocus (false));

	GlyphArrangement textLayout;
	textLayout.addFittedText (font, text.trim(),
							  0.0f, 0.0f, (float) length, (float) depth,
							  Justification::centred,
							  jmax (1, depth / 12));

	AffineTransform transform;

	if (orientation == TabbedButtonBar::TabsAtLeft)
	{
		transform = transform.rotated (float_Pi * -0.5f)
							 .translated ((float) x, (float) (y + h));
	}
	else if (orientation  == TabbedButtonBar::TabsAtRight)
	{
		transform = transform.rotated (float_Pi * 0.5f)
							 .translated ((float) (x + w), (float) y);
	}
	else
	{
		transform = transform.translated ((float) x, (float) y);
	}

	if (isFrontTab && (button.isColourSpecified (TabbedButtonBar::frontTextColourId) || isColourSpecified (TabbedButtonBar::frontTextColourId)))
		g.setColour (findColour (TabbedButtonBar::frontTextColourId));
	else if (button.isColourSpecified (TabbedButtonBar::tabTextColourId) || isColourSpecified (TabbedButtonBar::tabTextColourId))
		g.setColour (findColour (TabbedButtonBar::tabTextColourId));
	else
		g.setColour (preferredBackgroundColour.contrasting());

	if (! (isMouseOver || isMouseDown))
		g.setOpacity (0.8f);

	if (! button.isEnabled())
		g.setOpacity (0.3f);

	textLayout.draw (g, transform);
}

int LookAndFeel::getTabButtonBestWidth (int /*tabIndex*/,
										const String& text,
										int tabDepth,
										Button&)
{
	Font f (tabDepth * 0.6f);
	return f.getStringWidth (text.trim()) + getTabButtonOverlap (tabDepth) * 2;
}

void LookAndFeel::drawTabButton (Graphics& g,
								 int w, int h,
								 const Colour& preferredColour,
								 int tabIndex,
								 const String& text,
								 Button& button,
								 TabbedButtonBar::Orientation orientation,
								 const bool isMouseOver,
								 const bool isMouseDown,
								 const bool isFrontTab)
{
	int length = w;
	int depth = h;

	if (orientation == TabbedButtonBar::TabsAtLeft
			|| orientation == TabbedButtonBar::TabsAtRight)
	{
		swapVariables (length, depth);
	}

	Path tabShape;

	createTabButtonShape (tabShape, w, h,
						  tabIndex, text, button, orientation,
						  isMouseOver, isMouseDown, isFrontTab);

	fillTabButtonShape (g, tabShape, preferredColour,
						tabIndex, text, button, orientation,
						isMouseOver, isMouseDown, isFrontTab);

	const int indent = getTabButtonOverlap (depth);
	int x = 0, y = 0;

	if (orientation == TabbedButtonBar::TabsAtLeft
		 || orientation == TabbedButtonBar::TabsAtRight)
	{
		y += indent;
		h -= indent * 2;
	}
	else
	{
		x += indent;
		w -= indent * 2;
	}

	drawTabButtonText (g, x, y, w, h, preferredColour,
					   tabIndex, text, button, orientation,
					   isMouseOver, isMouseDown, isFrontTab);
}

void LookAndFeel::drawTabAreaBehindFrontButton (Graphics& g,
												int w, int h,
												TabbedButtonBar& tabBar,
												TabbedButtonBar::Orientation orientation)
{
	const float shadowSize = 0.2f;

	float x1 = 0.0f, y1 = 0.0f, x2 = 0.0f, y2 = 0.0f;
	Rectangle<int> shadowRect;

	if (orientation == TabbedButtonBar::TabsAtLeft)
	{
		x1 = (float) w;
		x2 = w * (1.0f - shadowSize);
		shadowRect.setBounds ((int) x2, 0, w - (int) x2, h);
	}
	else if (orientation == TabbedButtonBar::TabsAtRight)
	{
		x2 = w * shadowSize;
		shadowRect.setBounds (0, 0, (int) x2, h);
	}
	else if (orientation == TabbedButtonBar::TabsAtBottom)
	{
		y2 = h * shadowSize;
		shadowRect.setBounds (0, 0, w, (int) y2);
	}
	else
	{
		y1 = (float) h;
		y2 = h * (1.0f - shadowSize);
		shadowRect.setBounds (0, (int) y2, w, h - (int) y2);
	}

	g.setGradientFill (ColourGradient (Colours::black.withAlpha (tabBar.isEnabled() ? 0.3f : 0.15f), x1, y1,
									   Colours::transparentBlack, x2, y2, false));

	shadowRect.expand (2, 2);
	g.fillRect (shadowRect);

	g.setColour (Colour (0x80000000));

	if (orientation == TabbedButtonBar::TabsAtLeft)
	{
		g.fillRect (w - 1, 0, 1, h);
	}
	else if (orientation == TabbedButtonBar::TabsAtRight)
	{
		g.fillRect (0, 0, 1, h);
	}
	else if (orientation == TabbedButtonBar::TabsAtBottom)
	{
		g.fillRect (0, 0, w, 1);
	}
	else
	{
		g.fillRect (0, h - 1, w, 1);
	}
}

Button* LookAndFeel::createTabBarExtrasButton()
{
	const float thickness = 7.0f;
	const float indent = 22.0f;

	Path p;
	p.addEllipse (-10.0f, -10.0f, 120.0f, 120.0f);

	DrawablePath ellipse;
	ellipse.setPath (p);
	ellipse.setFill (Colour (0x99ffffff));

	p.clear();
	p.addEllipse (0.0f, 0.0f, 100.0f, 100.0f);
	p.addRectangle (indent, 50.0f - thickness, 100.0f - indent * 2.0f, thickness * 2.0f);
	p.addRectangle (50.0f - thickness, indent, thickness * 2.0f, 50.0f - indent - thickness);
	p.addRectangle (50.0f - thickness, 50.0f + thickness, thickness * 2.0f, 50.0f - indent - thickness);
	p.setUsingNonZeroWinding (false);

	DrawablePath dp;
	dp.setPath (p);
	dp.setFill (Colour (0x59000000));

	DrawableComposite normalImage;
	normalImage.insertDrawable (ellipse);
	normalImage.insertDrawable (dp);

	dp.setFill (Colour (0xcc000000));

	DrawableComposite overImage;
	overImage.insertDrawable (ellipse);
	overImage.insertDrawable (dp);

	DrawableButton* db = new DrawableButton ("tabs", DrawableButton::ImageFitted);
	db->setImages (&normalImage, &overImage, 0);
	return db;
}

void LookAndFeel::drawTableHeaderBackground (Graphics& g, TableHeaderComponent& header)
{
	g.fillAll (Colours::white);

	const int w = header.getWidth();
	const int h = header.getHeight();

	g.setGradientFill (ColourGradient (Colour (0xffe8ebf9), 0.0f, h * 0.5f,
									   Colour (0xfff6f8f9), 0.0f, h - 1.0f,
									   false));
	g.fillRect (0, h / 2, w, h);

	g.setColour (Colour (0x33000000));
	g.fillRect (0, h - 1, w, 1);

	for (int i = header.getNumColumns (true); --i >= 0;)
		g.fillRect (header.getColumnPosition (i).getRight() - 1, 0, 1, h - 1);
}

void LookAndFeel::drawTableHeaderColumn (Graphics& g, const String& columnName, int /*columnId*/,
										 int width, int height,
										 bool isMouseOver, bool isMouseDown,
										 int columnFlags)
{
	if (isMouseDown)
		g.fillAll (Colour (0x8899aadd));
	else if (isMouseOver)
		g.fillAll (Colour (0x5599aadd));

	int rightOfText = width - 4;

	if ((columnFlags & (TableHeaderComponent::sortedForwards | TableHeaderComponent::sortedBackwards)) != 0)
	{
		const float top = height * ((columnFlags & TableHeaderComponent::sortedForwards) != 0 ? 0.35f : (1.0f - 0.35f));
		const float bottom = height - top;

		const float w = height * 0.5f;
		const float x = rightOfText - (w * 1.25f);
		rightOfText = (int) x;

		Path sortArrow;
		sortArrow.addTriangle (x, bottom, x + w * 0.5f, top, x + w, bottom);

		g.setColour (Colour (0x99000000));
		g.fillPath (sortArrow);
	}

	g.setColour (Colours::black);
	g.setFont (height * 0.5f, Font::bold);
	const int textX = 4;
	g.drawFittedText (columnName, textX, 0, rightOfText - textX, height, Justification::centredLeft, 1);
}

void LookAndFeel::paintToolbarBackground (Graphics& g, int w, int h, Toolbar& toolbar)
{
	const Colour background (toolbar.findColour (Toolbar::backgroundColourId));

	g.setGradientFill (ColourGradient (background, 0.0f, 0.0f,
									   background.darker (0.1f),
									   toolbar.isVertical() ? w - 1.0f : 0.0f,
									   toolbar.isVertical() ? 0.0f : h - 1.0f,
									   false));
	g.fillAll();
}

Button* LookAndFeel::createToolbarMissingItemsButton (Toolbar& /*toolbar*/)
{
	return createTabBarExtrasButton();
}

void LookAndFeel::paintToolbarButtonBackground (Graphics& g, int /*width*/, int /*height*/,
												bool isMouseOver, bool isMouseDown,
												ToolbarItemComponent& component)
{
	if (isMouseDown)
		g.fillAll (component.findColour (Toolbar::buttonMouseDownBackgroundColourId, true));
	else if (isMouseOver)
		g.fillAll (component.findColour (Toolbar::buttonMouseOverBackgroundColourId, true));
}

void LookAndFeel::paintToolbarButtonLabel (Graphics& g, int x, int y, int width, int height,
										   const String& text, ToolbarItemComponent& component)
{
	g.setColour (component.findColour (Toolbar::labelTextColourId, true)
					.withAlpha (component.isEnabled() ? 1.0f : 0.25f));

	const float fontHeight = jmin (14.0f, height * 0.85f);
	g.setFont (fontHeight);

	g.drawFittedText (text,
					  x, y, width, height,
					  Justification::centred,
					  jmax (1, height / (int) fontHeight));
}

void LookAndFeel::drawPropertyPanelSectionHeader (Graphics& g, const String& name,
												  bool isOpen, int width, int height)
{
	const int buttonSize = (height * 3) / 4;
	const int buttonIndent = (height - buttonSize) / 2;

	drawTreeviewPlusMinusBox (g, buttonIndent, buttonIndent, buttonSize, buttonSize, ! isOpen, false);

	const int textX = buttonIndent * 2 + buttonSize + 2;

	g.setColour (Colours::black);
	g.setFont (height * 0.7f, Font::bold);
	g.drawText (name, textX, 0, width - textX - 4, height, Justification::centredLeft, true);
}

void LookAndFeel::drawPropertyComponentBackground (Graphics& g, int width, int height,
												   PropertyComponent&)
{
	g.setColour (Colour (0x66ffffff));
	g.fillRect (0, 0, width, height - 1);
}

void LookAndFeel::drawPropertyComponentLabel (Graphics& g, int, int height,
											  PropertyComponent& component)
{
	g.setColour (Colours::black);

	if (! component.isEnabled())
		g.setOpacity (0.6f);

	g.setFont (jmin (height, 24) * 0.65f);

	const Rectangle<int> r (getPropertyComponentContentPosition (component));

	g.drawFittedText (component.getName(),
					  3, r.getY(), r.getX() - 5, r.getHeight(),
					  Justification::centredLeft, 2);
}

const Rectangle<int> LookAndFeel::getPropertyComponentContentPosition (PropertyComponent& component)
{
	return Rectangle<int> (component.getWidth() / 3, 1,
						   component.getWidth() - component.getWidth() / 3 - 1, component.getHeight() - 3);
}

void LookAndFeel::createFileChooserHeaderText (const String& title,
											   const String& instructions,
											   GlyphArrangement& text,
											   int width)
{
	text.clear();

	text.addJustifiedText (Font (17.0f, Font::bold), title,
						   8.0f, 22.0f, width - 16.0f,
						   Justification::centred);

	text.addJustifiedText (Font (14.0f), instructions,
						   8.0f, 24.0f + 16.0f, width - 16.0f,
						   Justification::centred);
}

void LookAndFeel::drawFileBrowserRow (Graphics& g, int width, int height,
									  const String& filename, Image* icon,
									  const String& fileSizeDescription,
									  const String& fileTimeDescription,
									  const bool isDirectory,
									  const bool isItemSelected,
									  const int /*itemIndex*/)
{
	if (isItemSelected)
		g.fillAll (findColour (DirectoryContentsDisplayComponent::highlightColourId));

	g.setColour (findColour (DirectoryContentsDisplayComponent::textColourId));
	g.setFont (height * 0.7f);

	Image* im = icon;
	Image* toRelease = 0;

	if (im == 0)
	{
		toRelease = im = (isDirectory ? getDefaultFolderImage()
									  : getDefaultDocumentFileImage());
	}

	const int x = 32;

	if (im != 0)
	{
		g.drawImageWithin (im, 2, 2, x - 4, height - 4,
						   RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize,
						   false);

		ImageCache::release (toRelease);
	}

	if (width > 450 && ! isDirectory)
	{
		const int sizeX = roundToInt (width * 0.7f);
		const int dateX = roundToInt (width * 0.8f);

		g.drawFittedText (filename,
						  x, 0, sizeX - x, height,
						  Justification::centredLeft, 1);

		g.setFont (height * 0.5f);
		g.setColour (Colours::darkgrey);

		if (! isDirectory)
		{
			g.drawFittedText (fileSizeDescription,
							  sizeX, 0, dateX - sizeX - 8, height,
							  Justification::centredRight, 1);

			g.drawFittedText (fileTimeDescription,
							  dateX, 0, width - 8 - dateX, height,
							  Justification::centredRight, 1);
		}
	}
	else
	{
		g.drawFittedText (filename,
						  x, 0, width - x, height,
						  Justification::centredLeft, 1);

	}
}

Button* LookAndFeel::createFileBrowserGoUpButton()
{
	DrawableButton* goUpButton = new DrawableButton ("up", DrawableButton::ImageOnButtonBackground);

	Path arrowPath;
	arrowPath.addArrow (50.0f, 100.0f, 50.0f, 0.0f, 40.0f, 100.0f, 50.0f);

	DrawablePath arrowImage;
	arrowImage.setFill (Colours::black.withAlpha (0.4f));
	arrowImage.setPath (arrowPath);

	goUpButton->setImages (&arrowImage);

	return goUpButton;
}

void LookAndFeel::layoutFileBrowserComponent (FileBrowserComponent& browserComp,
											  DirectoryContentsDisplayComponent* fileListComponent,
											  FilePreviewComponent* previewComp,
											  ComboBox* currentPathBox,
											  TextEditor* filenameBox,
											  Button* goUpButton)
{
	const int x = 8;
	int w = browserComp.getWidth() - x - x;

	if (previewComp != 0)
	{
		const int previewWidth = w / 3;
		previewComp->setBounds (x + w - previewWidth, 0, previewWidth, browserComp.getHeight());

		w -= previewWidth + 4;
	}

	int y = 4;

	const int controlsHeight = 22;
	const int bottomSectionHeight = controlsHeight + 8;
	const int upButtonWidth = 50;

	currentPathBox->setBounds (x, y, w - upButtonWidth - 6, controlsHeight);
	goUpButton->setBounds (x + w - upButtonWidth, y, upButtonWidth, controlsHeight);

	y += controlsHeight + 4;

	Component* const listAsComp = dynamic_cast <Component*> (fileListComponent);
	listAsComp->setBounds (x, y, w, browserComp.getHeight() - y - bottomSectionHeight);

	y = listAsComp->getBottom() + 4;
	filenameBox->setBounds (x + 50, y, w - 50, controlsHeight);
}

Image* LookAndFeel::getDefaultFolderImage()
{
	static const unsigned char foldericon_png[] = { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,32,0,0,0,28,8,6,0,0,0,0,194,189,34,0,0,0,4,103,65,77,65,0,0,175,200,55,5,
		138,233,0,0,0,25,116,69,88,116,83,111,102,116,119,97,114,101,0,65,100,111,98,101,32,73,109,97,103,101,82,101,97,100,121,113,201,101,60,0,0,9,46,73,68,65,84,120,218,98,252,255,255,63,3,50,240,41,95,192,
		197,205,198,32,202,204,202,33,241,254,235,47,133,47,191,24,180,213,164,133,152,69,24,222,44,234,42,77,188,245,31,170,129,145,145,145,1,29,128,164,226,91,86,113,252,248,207,200,171,37,39,204,239,170,43,
		254,206,218,88,231,61,62,61,0,1,196,2,149,96,116,200,158,102,194,202,201,227,197,193,206,166,194,204,193,33,195,202,204,38,42,197,197,42,196,193,202,33,240,241,231,15,134,151,95,127,9,2,149,22,0,241,47,
		152,230,128,134,245,204,63,191,188,103,83,144,16,16,228,229,102,151,76,239,217,32,199,204,198,169,205,254,159,65,245,203,79,6,169,131,151,30,47,1,42,91,10,196,127,208,236,101,76,235,90,43,101,160,40,242,
		19,32,128,64,78,98,52,12,41,149,145,215,52,89,162,38,35,107,39,196,203,203,192,206,194,206,192,197,198,202,192,203,197,198,192,205,193,206,240,252,227,103,134,139,55,175,191,127,243,242,78,219,187,207,
		63,215,255,98,23,48,228,227,96,83,98,102,102,85,225,224,228,80,20,224,230,86,226,225,228,150,103,101,97,101,230,227,228,96,224,0,234,191,243,252,5,195,222,19,199,38,191,127,112,161,83,66,199,86,141,131,
		149,69,146,133,153,69,137,149,133,89,157,141,131,77,83,140,143,243,219,255,31,159,123,0,2,136,69,90,207,129,157,71,68,42,66,71,73,209,210,81,91,27,24,142,140,12,127,255,253,103,0,185,236,31,3,144,6,50,
		148,68,216,25,216,24,117,4,239,11,243,214,49,50,51,84,178,48,114,240,112,177,114,177,240,115,113,49,241,112,112,48,176,179,178,51,176,48,49,3,85,255,99,248,253,247,15,195,247,159,191,25,30,191,126,253,
		71,74,76,200,66,75,197,119,138,168,144,160,150,168,0,183,160,152,32,15,175,188,184,32,199,175,191,127,25,214,31,184,120,247,236,209,253,159,0,2,136,133,95,70,93,74,88,80,196,83,69,66,130,149,9,104,219,
		151,31,191,193,150,194,146,6,136,102,102,98,100,16,227,231,103,16,23,210,230,101,101,102,100,248,255,143,137,225,223,63,6,6,22,102,38,134,239,191,126,49,220,123,241,134,225,227,247,175,64,7,252,101,96,
		97,249,207,192,193,198,200,160,171,34,192,108,165,235,104,42,204,207,101,42,194,199,197,192,199,201,198,192,197,193,202,192,198,202,194,176,247,194,3,134,155,183,110,61,188,127,124,221,19,128,0,92,146,
		49,14,64,64,16,69,63,153,85,16,52,18,74,71,112,6,87,119,0,165,160,86,138,32,172,216,29,49,182,84,253,169,94,94,230,127,17,87,133,34,146,174,3,88,126,240,219,164,147,113,31,145,244,152,112,179,211,130,
		34,31,203,113,162,233,6,36,49,163,174,74,124,140,60,141,144,165,161,220,228,25,3,24,105,255,17,168,101,1,139,245,188,93,104,251,73,239,235,50,90,189,111,175,0,98,249,254,254,249,175,239,223,190,126,6,
		5,27,19,47,90,170,102,0,249,158,129,129,141,133,25,228,20,6,38,38,72,74,7,185,243,243,247,239,12,23,31,60,98,228,231,253,207,144,227,107,206,32,202,199,193,240,249,251,127,134,95,191,255,49,124,249,250,
		159,225,237,239,95,12,63,127,1,35,229,31,194,71,32,71,63,123,251,245,223,197,27,183,159,189,187,178,103,61,80,232,59,64,0,177,48,252,5,134,225,255,191,223,126,254,250,13,182,132,1,41,167,176,3,53,128,
		188,254,226,253,103,96,212,252,96,120,247,249,203,255,79,223,191,254,255,250,235,199,191,239,63,191,255,87,145,17,100,73,116,181,100,252,249,243,63,195,149,123,223,193,14,132,101,55,96,52,3,125,255,15,
		204,254,15,132,160,232,253,13,20,124,248,226,227,223,23,207,30,221,120,119,255,226,109,160,210,31,0,1,196,242,231,219,135,175,140,255,126,190,7,197,37,35,19,34,216,65,248,211,143,111,255,79,223,121,240,
		255,211,183,79,76,220,156,172,12,236,204,140,140,252,124,28,140,250,226,82,140,106,82,34,140,124,156,156,12,175,222,253,1,90,4,137,162,63,127,33,161,6,178,242,215,239,255,224,160,255,15,198,12,64,7,48,
		128,211,200,253,151,111,254,254,248,240,236,44,80,217,71,80,246,4,8,32,160,31,255,255,100,102,248,243,238,199,159,63,16,221,16,19,128,248,31,195,181,199,207,254,255,253,247,133,49,212,78,27,104,8,11,40,
		94,25,184,216,89,129,108,38,70,144,242,183,31,17,105,230,63,148,248,15,97,49,252,248,249,15,20,85,72,105,9,148,187,254,49,220,127,254,242,207,243,75,135,14,128,130,31,84,64,1,4,16,203,247,143,175,127,
		48,253,254,246,234,7,48,206,96,137,13,4,64,65,248,234,195,7,6,7,3,57,70,33,46,97,134,111,63,254,50,252,5,250,244,51,216,103,255,192,185,0,150,91,80,44,135,242,127,253,129,164,23,24,96,102,250,207,112,
		255,213,219,255,247,31,63,188,251,246,201,173,199,176,2,13,32,128,88,62,188,121,241,243,211,231,207,31,126,2,147,236,63,168,6,144,193,223,190,255,254,207,198,198,192,40,35,44,206,240,252,205,79,6,132,
		223,24,224,150,32,251,28,25,128,211,29,19,170,24,51,48,88,111,61,127,206,248,254,245,179,139,192,18,247,219,239,239,95,192,249,9,32,128,88,126,124,249,248,231,203,183,111,159,128,33,240,15,24,68,160,180,
		2,204,223,140,12,111,63,127,102,16,228,229,4,6,53,35,195,31,176,119,25,112,3,70,84,55,0,203,50,112,33,134,108,249,103,160,7,159,189,126,253,235,235,227,203,7,255,255,251,247,13,86,63,0,4,16,168,46,248,
		199,250,231,243,235,159,191,126,254,248,245,251,47,23,11,51,51,48,184,152,24,94,127,250,248,95,68,136,151,241,243,55,96,208,51,160,218,255,31,139,27,144,197,254,98,201,202,79,223,124,96,120,245,232,250,
		185,119,143,174,95,250,243,243,219,119,152,60,64,0,129,2,234,223,183,215,15,95,48,254,255,253,3,146,109,192,229,5,195,135,47,159,25,248,184,121,24,126,0,227,29,88,240,49,252,101,36,14,255,1,90,249,7,156,
		222,17,24,24,164,12,207,223,189,99,248,250,252,230,97,96,229,245,2,104,231,111,152,3,0,2,8,228,128,191,15,239,220,120,255,255,223,159,47,160,116,0,42,44,222,124,250,244,239,207,255,63,12,236,108,236,64,
		67,65,81,0,52,244,63,113,248,47,52,10,96,14,98,2,230,191,119,223,127,48,60,121,254,248,235,151,55,207,46,1,163,252,35,114,128,1,4,16,40,10,254,191,121,249,252,199,175,159,63,191,254,2,230,45,118,22,22,
		134,219,207,94,252,231,224,100,103,250,247,15,148,32,64,85,12,34,14,254,227,72,6,255,225,9,240,63,138,26,46,96,214,189,249,244,37,195,139,167,143,30,124,253,246,253,9,40,245,255,71,202,30,0,1,196,2,226,
		0,243,232,159,239,63,127,124,253,11,202,94,64,169,23,31,62,50,138,137,242,49,50,0,211,195,223,255,80,7,252,199,159,6,224,137,145,9,146,231,153,160,165,218,23,96,29,240,244,237,59,134,111,175,31,95,250,
		252,230,241,83,244,182,1,64,0,177,192,28,14,76,132,31,128,169,19,88,220,126,253,207,206,198,196,32,38,36,0,244,61,11,176,148,251,139,145,3,208,29,0,178,16,82,228,66,42,174,223,192,26,8,152,162,25,222,
		125,248,200,240,242,253,39,134,151,79,238,126,254,242,242,238,177,15,47,30,190,5,215,242,72,0,32,128,224,14,96,254,255,231,61,168,92,123,241,254,253,127,1,62,78,6,78,110,78,134,223,64,195,254,50,98,183,
		24,36,12,202,179,224,202,9,88,228,253,132,90,250,246,211,71,134,55,175,94,254,122,255,250,249,247,15,175,159,126,249,251,237,195,135,95,175,110,31,122,117,251,244,49,160,150,111,255,209,218,128,0,1,152,
		44,183,21,0,65,32,136,110,247,254,255,243,122,9,187,64,105,174,74,22,138,25,173,80,208,194,188,238,156,151,217,217,15,32,182,197,37,83,201,4,31,243,178,169,232,242,214,224,223,252,103,175,35,85,1,41,129,
		228,148,142,8,214,30,32,149,6,161,204,109,182,53,236,184,156,78,142,147,195,153,89,35,198,3,87,166,249,220,227,198,59,218,48,252,223,185,111,30,1,132,228,128,127,31,222,124,248,248,27,24,152,28,60,220,
		220,12,44,172,172,224,224,103,5,102,98,144,133,160,236,244,229,231,47,134,239,223,127,49,188,121,251,158,225,241,179,103,12,31,223,189,254,251,227,221,139,55,191,62,188,120,246,235,205,189,59,207,238,
		94,58,241,228,254,109,144,101,159,128,248,51,40,9,32,97,80,217,255,15,221,1,0,1,4,143,130,207,159,191,126,252,246,234,213,111,94,126,94,118,73,94,9,198,127,64,223,126,252,246,147,225,243,215,239,12,223,
		128,229,198,251,15,239,24,62,189,126,249,227,203,171,135,47,63,189,122,252,228,235,155,199,247,95,63,188,118,227,197,227,123,247,127,255,250,249,30,104,198,7,32,126,11,181,252,7,212,183,160,4,247,7,155,
		197,48,0,16,64,112,7,60,121,241,238,189,16,207,15,134,63,63,216,25,95,125,248,198,112,227,241,27,134,15,239,223,50,124,126,245,228,253,143,55,143,158,191,123,116,237,226,171,135,55,175,126,253,252,225,
		229,183,47,159,95,254,253,245,227,253,175,159,223,223,193,124,7,181,20,84,105,252,70,143,103,124,0,32,128,224,14,224,102,253,251,81,144,253,223,235,167,207,30,254,124,127,231,252,155,143,175,159,188,250,
		246,254,249,125,96,60,62,248,250,233,253,147,119,207,238,221,6,150,214,175,129,106,191,130,18,19,146,133,120,125,72,8,0,4,16,34,27,190,121,112,251,3,211,159,69,143,110,223,229,120,255,232,230,221,215,
		79,239,62,4,102,203,207,72,241,9,11,218,63,72,89,137,20,207,98,100,93,16,0,8,32,70,144,1,64,14,168,209,199,7,196,194,160,166,27,212,135,95,96,65,10,173,95,254,34,219,6,51,128,88,7,96,235,21,129,0,64,0,
		193,28,192,8,174,53,33,152,1,155,133,184,12,196,165,4,151,133,232,0,32,192,0,151,97,210,163,246,134,208,52,0,0,0,0,73,69,78,68,174,66,96,130,0,0};

	return ImageCache::getFromMemory (foldericon_png, sizeof (foldericon_png));
}

Image* LookAndFeel::getDefaultDocumentFileImage()
{
	static const unsigned char fileicon_png[] = { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,32,0,0,0,32,8,6,0,0,0,115,122,122,244,0,0,0,4,103,65,77,65,0,0,175,200,55,5,
		138,233,0,0,0,25,116,69,88,116,83,111,102,116,119,97,114,101,0,65,100,111,98,101,32,73,109,97,103,101,82,101,97,100,121,113,201,101,60,0,0,4,99,73,68,65,84,120,218,98,252,255,255,63,3,12,48,50,50,50,1,
		169,127,200,98,148,2,160,153,204,64,243,254,226,146,7,8,32,22,52,203,255,107,233,233,91,76,93,176,184,232,239,239,95,127,24,40,112,8,19,51,203,255,179,23,175,108,1,90,190,28,104,54,43,80,232,207,127,44,
		62,3,8,32,6,144,24,84,156,25,132,189,252,3,146,255,83,9,220,127,254,242,134,162,138,170,10,208,92,144,3,152,97,118,33,99,128,0,98,66,114,11,200,1,92,255,254,252,225,32,215,215,32,127,64,240,127,80,60,
		50,40,72,136,169,47,95,179,118,130,136,148,140,0,40,80,128,33,193,136,174,7,32,128,144,29,192,8,117,41,59,209,22,66,241,191,255,16,12,244,19,195,63,48,134,240,255,0,9,115,125,93,239,252,130,130,108,168,
		249,44,232,102,0,4,16,19,22,62,51,33,11,255,195,44,4,211,255,25,96,16,33,6,117,24,56,226,25,24,202,139,10,75,226,51,115,66,160,105,13,197,17,0,1,196,68,172,79,255,33,91,206,192,192,128,176,22,17,10,200,
		234,32,161,240,31,24,10,255,24,152,153,153,184,39,244,247,117,107,234,234,105,131,66,1,154,224,193,0,32,128,240,58,0,22,180,255,144,18,13,40,136,33,113,140,36,255,15,17,26,48,12,81,15,145,255,254,251,
		31,131,0,59,171,84,81,73,105,33,208,216,191,200,161,12,16,64,44,248,131,251,63,10,31,198,253,143,38,6,83,7,11,33,228,232,2,123,4,202,226,228,96,151,132,166,49,144,35,126,131,196,0,2,136,5,103,60,51,252,
		71,49,12,213,130,255,168,226,232,150,254,255,15,143,6,80,202,3,133,16,200,198,63,127,193,229,17,39,16,127,135,217,7,16,64,88,67,0,28,143,255,25,225,46,135,249,18,155,133,240,178,4,205,145,8,62,52,186,
		32,234,152,160,118,194,179,35,64,0,177,96,11,123,144,236,95,104,92,162,228,113,36,11,81,125,140,112,56,186,131,96,226,176,172,137,148,229,193,0,32,128,88,112,167,248,255,112,223,48,34,165,110,6,124,190,
		253,143,61,106,192,9,19,73,28,25,0,4,16,206,40,248,251,15,45,104,209,130,21,51,222,145,18,238,127,180,68,8,244,250,95,164,16,66,6,0,1,196,130,45,253,195,12,250,135,53,206,255,195,131,18,213,98,236,81,
		243,31,154,11,144,115,8,50,0,8,32,156,81,0,203,227,12,80,223,98,230,4,68,72,96,38,78,84,11,65,9,250,47,146,3,145,1,64,0,97,117,192,95,112,34,68,138,130,255,176,224,251,143,226,51,6,6,68,29,192,136,20,
		77,200,69,54,35,3,36,49,255,69,77,132,112,0,16,64,44,56,139,94,36,7,96,102,59,164,108,249,31,181,82,98,64,203,174,255,144,234,142,127,88,146,33,64,0,97,205,134,240,120,67,75,76,136,224,198,140,22,6,44,
		142,66,201,41,255,177,231,2,128,0,194,25,5,255,254,161,134,192,127,6,28,229,0,129,242,1,150,56,33,81,138,209,28,96,0,8,32,172,81,0,78,3,104,190,68,182,224,31,146,197,224,56,6,146,140,176,202,135,17,169,
		96,130,40,64,56,0,139,93,0,1,132,61,10,64,248,31,106,156,162,199,55,204,65,255,144,178,38,74,84,252,71,51,239,63,246,68,8,16,64,44,216,74,1,88,217,13,203,191,32,1,80,58,7,133,224,127,6,68,114,6,241,65,
		81,197,8,101,255,71,114,33,92,237,127,228,52,128,233,2,128,0,98,193,149,3,64,117,193,255,127,255,81,75,191,127,168,5,18,136,255,31,45,161,49,32,151,134,72,252,127,12,216,203,98,128,0,98,193,210,144,135,
		248,30,201,242,127,208,252,140,145,27,160,113,206,136,148,197,192,121,159,17,53,184,225,149,17,22,23,0,4,16,11,182,150,237,63,168,207,96,142,248,143,163,72,6,203,253,67,13,61,6,104,14,66,46,17,254,65,
		19,40,182,16,0,8,32,22,108,109,235,255,176,234,24,35,79,255,199,222,30,64,81,135,90,35,194,211,4,142,92,0,16,64,88,29,0,107,7,254,251,247,31,53,78,241,54,207,80,29,135,209,96,249,143,189,46,0,8,32,116,
		7,252,101,102,103,103,228,103,99,96,248,193,198,137,53,248,49,125,204,128,225,227,255,88,18,54,47,176,25,202,205,195,205,6,109,11,194,149,0,4,16,35,204,85,208,254,27,159,128,176,176,142,166,182,142,21,
		48,4,248,129,41,143,13,217,16,70,52,95,147,0,254,0,187,69,95,223,188,122,125,235,206,141,107,7,129,252,247,64,123,193,237,66,128,0,66,118,0,168,189,198,3,196,252,32,135,64,105,54,228,230,19,185,29,100,
		168,175,191,0,241,7,32,254,4,196,159,129,246,254,2,73,2,4,16,11,90,72,125,135,210,63,161,138,153,169,212,75,255,15,117,196,15,40,134,119,215,1,2,12,0,187,0,132,247,216,161,197,124,0,0,0,0,73,69,78,68,
		174,66,96,130,0,0};

	return ImageCache::getFromMemory (fileicon_png, sizeof (fileicon_png));
}

void LookAndFeel::playAlertSound()
{
	PlatformUtilities::beep();
}

void LookAndFeel::drawLevelMeter (Graphics& g, int width, int height, float level)
{
	g.setColour (Colours::white.withAlpha (0.7f));
	g.fillRoundedRectangle (0.0f, 0.0f, (float) width, (float) height, 3.0f);
	g.setColour (Colours::black.withAlpha (0.2f));
	g.drawRoundedRectangle (1.0f, 1.0f, width - 2.0f, height - 2.0f, 3.0f, 1.0f);

	const int totalBlocks = 7;
	const int numBlocks = roundToInt (totalBlocks * level);
	const float w = (width - 6.0f) / (float) totalBlocks;

	for (int i = 0; i < totalBlocks; ++i)
	{
		if (i >= numBlocks)
			g.setColour (Colours::lightblue.withAlpha (0.6f));
		else
			g.setColour (i < totalBlocks - 1 ? Colours::blue.withAlpha (0.5f)
											 : Colours::red);

		g.fillRoundedRectangle (3.0f + i * w + w * 0.1f, 3.0f, w * 0.8f, height - 6.0f, w * 0.4f);
	}
}

void LookAndFeel::drawKeymapChangeButton (Graphics& g, int width, int height, Button& button, const String& keyDescription)
{
	const Colour textColour (button.findColour (KeyMappingEditorComponent::textColourId, true));

	if (keyDescription.isNotEmpty())
	{
		if (button.isEnabled())
		{
			const float alpha = button.isDown() ? 0.3f : (button.isOver() ? 0.15f : 0.08f);
			g.fillAll (textColour.withAlpha (alpha));

			g.setOpacity (0.3f);
			g.drawBevel (0, 0, width, height, 2);
		}

		g.setColour (textColour);
		g.setFont (height * 0.6f);
		g.drawFittedText (keyDescription,
						  3, 0, width - 6, height,
						  Justification::centred, 1);
	}
	else
	{
		const float thickness = 7.0f;
		const float indent = 22.0f;

		Path p;
		p.addEllipse (0.0f, 0.0f, 100.0f, 100.0f);
		p.addRectangle (indent, 50.0f - thickness, 100.0f - indent * 2.0f, thickness * 2.0f);
		p.addRectangle (50.0f - thickness, indent, thickness * 2.0f, 50.0f - indent - thickness);
		p.addRectangle (50.0f - thickness, 50.0f + thickness, thickness * 2.0f, 50.0f - indent - thickness);
		p.setUsingNonZeroWinding (false);

		g.setColour (textColour.withAlpha (button.isDown() ? 0.7f : (button.isOver() ? 0.5f : 0.3f)));
		g.fillPath (p, p.getTransformToScaleToFit (2.0f, 2.0f, width - 4.0f, height - 4.0f, true));
	}

	if (button.hasKeyboardFocus (false))
	{
		g.setColour (textColour.withAlpha (0.4f));
		g.drawRect (0, 0, width, height);
	}
}

static void createRoundedPath (Path& p,
							   const float x, const float y,
							   const float w, const float h,
							   const float cs,
							   const bool curveTopLeft, const bool curveTopRight,
							   const bool curveBottomLeft, const bool curveBottomRight) throw()
{
	const float cs2 = 2.0f * cs;

	if (curveTopLeft)
	{
		p.startNewSubPath (x, y + cs);
		p.addArc (x, y, cs2, cs2, float_Pi * 1.5f, float_Pi * 2.0f);
	}
	else
	{
		p.startNewSubPath (x, y);
	}

	if (curveTopRight)
	{
		p.lineTo (x + w - cs, y);
		p.addArc (x + w - cs2, y, cs2, cs2, 0.0f, float_Pi * 0.5f);
	}
	else
	{
		p.lineTo (x + w, y);
	}

	if (curveBottomRight)
	{
		p.lineTo (x + w, y + h - cs);
		p.addArc (x + w - cs2, y + h - cs2, cs2, cs2, float_Pi * 0.5f, float_Pi);
	}
	else
	{
		p.lineTo (x + w, y + h);
	}

	if (curveBottomLeft)
	{
		p.lineTo (x + cs, y + h);
		p.addArc (x, y + h - cs2, cs2, cs2, float_Pi, float_Pi * 1.5f);
	}
	else
	{
		p.lineTo (x, y + h);
	}

	p.closeSubPath();
}

void LookAndFeel::drawShinyButtonShape (Graphics& g,
										float x, float y, float w, float h,
										float maxCornerSize,
										const Colour& baseColour,
										const float strokeWidth,
										const bool flatOnLeft,
										const bool flatOnRight,
										const bool flatOnTop,
										const bool flatOnBottom) throw()
{
	if (w <= strokeWidth * 1.1f || h <= strokeWidth * 1.1f)
		return;

	const float cs = jmin (maxCornerSize, w * 0.5f, h * 0.5f);

	Path outline;
	createRoundedPath (outline, x, y, w, h, cs,
						! (flatOnLeft || flatOnTop),
						! (flatOnRight || flatOnTop),
						! (flatOnLeft || flatOnBottom),
						! (flatOnRight || flatOnBottom));

	ColourGradient cg (baseColour, 0.0f, y,
					   baseColour.overlaidWith (Colour (0x070000ff)), 0.0f, y + h,
					   false);

	cg.addColour (0.5, baseColour.overlaidWith (Colour (0x33ffffff)));
	cg.addColour (0.51, baseColour.overlaidWith (Colour (0x110000ff)));

	g.setGradientFill (cg);
	g.fillPath (outline);

	g.setColour (Colour (0x80000000));
	g.strokePath (outline, PathStrokeType (strokeWidth));
}

void LookAndFeel::drawGlassSphere (Graphics& g,
								   const float x, const float y,
								   const float diameter,
								   const Colour& colour,
								   const float outlineThickness) throw()
{
	if (diameter <= outlineThickness)
		return;

	Path p;
	p.addEllipse (x, y, diameter, diameter);

	{
		ColourGradient cg (Colours::white.overlaidWith (colour.withMultipliedAlpha (0.3f)), 0, y,
						   Colours::white.overlaidWith (colour.withMultipliedAlpha (0.3f)), 0, y + diameter, false);

		cg.addColour (0.4, Colours::white.overlaidWith (colour));

		g.setGradientFill (cg);
		g.fillPath (p);
	}

	g.setGradientFill (ColourGradient (Colours::white, 0, y + diameter * 0.06f,
									   Colours::transparentWhite, 0, y + diameter * 0.3f, false));
	g.fillEllipse (x + diameter * 0.2f, y + diameter * 0.05f, diameter * 0.6f, diameter * 0.4f);

	ColourGradient cg (Colours::transparentBlack,
					   x + diameter * 0.5f, y + diameter * 0.5f,
					   Colours::black.withAlpha (0.5f * outlineThickness * colour.getFloatAlpha()),
					   x, y + diameter * 0.5f, true);

	cg.addColour (0.7, Colours::transparentBlack);
	cg.addColour (0.8, Colours::black.withAlpha (0.1f * outlineThickness));

	g.setGradientFill (cg);
	g.fillPath (p);

	g.setColour (Colours::black.withAlpha (0.5f * colour.getFloatAlpha()));
	g.drawEllipse (x, y, diameter, diameter, outlineThickness);
}

void LookAndFeel::drawGlassPointer (Graphics& g,
									const float x, const float y,
									const float diameter,
									const Colour& colour, const float outlineThickness,
									const int direction) throw()
{
	if (diameter <= outlineThickness)
		return;

	Path p;
	p.startNewSubPath (x + diameter * 0.5f, y);
	p.lineTo (x + diameter, y + diameter * 0.6f);
	p.lineTo (x + diameter, y + diameter);
	p.lineTo (x, y + diameter);
	p.lineTo (x, y + diameter * 0.6f);
	p.closeSubPath();

	p.applyTransform (AffineTransform::rotation (direction * (float_Pi * 0.5f), x + diameter * 0.5f, y + diameter * 0.5f));

	{
		ColourGradient cg (Colours::white.overlaidWith (colour.withMultipliedAlpha (0.3f)), 0, y,
						   Colours::white.overlaidWith (colour.withMultipliedAlpha (0.3f)), 0, y + diameter, false);

		cg.addColour (0.4, Colours::white.overlaidWith (colour));

		g.setGradientFill (cg);
		g.fillPath (p);
	}

	ColourGradient cg (Colours::transparentBlack,
					   x + diameter * 0.5f, y + diameter * 0.5f,
					   Colours::black.withAlpha (0.5f * outlineThickness * colour.getFloatAlpha()),
					   x - diameter * 0.2f, y + diameter * 0.5f, true);

	cg.addColour (0.5, Colours::transparentBlack);
	cg.addColour (0.7, Colours::black.withAlpha (0.07f * outlineThickness));

	g.setGradientFill (cg);
	g.fillPath (p);

	g.setColour (Colours::black.withAlpha (0.5f * colour.getFloatAlpha()));
	g.strokePath (p, PathStrokeType (outlineThickness));
}

void LookAndFeel::drawGlassLozenge (Graphics& g,
									const float x, const float y,
									const float width, const float height,
									const Colour& colour,
									const float outlineThickness,
									const float cornerSize,
									const bool flatOnLeft,
									const bool flatOnRight,
									const bool flatOnTop,
									const bool flatOnBottom) throw()
{
	if (width <= outlineThickness || height <= outlineThickness)
		return;

	const int intX = (int) x;
	const int intY = (int) y;
	const int intW = (int) width;
	const int intH = (int) height;

	const float cs = cornerSize < 0 ? jmin (width * 0.5f, height * 0.5f) : cornerSize;
	const float edgeBlurRadius = height * 0.75f + (height - cs * 2.0f);
	const int intEdge = (int) edgeBlurRadius;

	Path outline;
	createRoundedPath (outline, x, y, width, height, cs,
						! (flatOnLeft || flatOnTop),
						! (flatOnRight || flatOnTop),
						! (flatOnLeft || flatOnBottom),
						! (flatOnRight || flatOnBottom));

	{
		ColourGradient cg (colour.darker (0.2f), 0, y,
						   colour.darker (0.2f), 0, y + height, false);

		cg.addColour (0.03, colour.withMultipliedAlpha (0.3f));
		cg.addColour (0.4, colour);
		cg.addColour (0.97, colour.withMultipliedAlpha (0.3f));

		g.setGradientFill (cg);
		g.fillPath (outline);
	}

	ColourGradient cg (Colours::transparentBlack, x + edgeBlurRadius, y + height * 0.5f,
					   colour.darker (0.2f), x, y + height * 0.5f, true);

	cg.addColour (jlimit (0.0, 1.0, 1.0 - (cs * 0.5f) / edgeBlurRadius), Colours::transparentBlack);
	cg.addColour (jlimit (0.0, 1.0, 1.0 - (cs * 0.25f) / edgeBlurRadius), colour.darker (0.2f).withMultipliedAlpha (0.3f));

	if (! (flatOnLeft || flatOnTop || flatOnBottom))
	{
		g.saveState();
		g.setGradientFill (cg);
		g.reduceClipRegion (intX, intY, intEdge, intH);
		g.fillPath (outline);
		g.restoreState();
	}

	if (! (flatOnRight || flatOnTop || flatOnBottom))
	{
		cg.x1 = x + width - edgeBlurRadius;
		cg.x2 = x + width;

		g.saveState();
		g.setGradientFill (cg);
		g.reduceClipRegion (intX + intW - intEdge, intY, 2 + intEdge, intH);
		g.fillPath (outline);
		g.restoreState();
	}

	{
		const float leftIndent = flatOnLeft ? 0.0f : cs * 0.4f;
		const float rightIndent = flatOnRight ? 0.0f : cs * 0.4f;

		Path highlight;
		createRoundedPath (highlight,
						   x + leftIndent,
						   y + cs * 0.1f,
						   width - (leftIndent + rightIndent),
						   height * 0.4f, cs * 0.4f,
						   ! (flatOnLeft || flatOnTop),
						   ! (flatOnRight || flatOnTop),
						   ! (flatOnLeft || flatOnBottom),
						   ! (flatOnRight || flatOnBottom));

		g.setGradientFill (ColourGradient (colour.brighter (10.0f), 0, y + height * 0.06f,
										   Colours::transparentWhite, 0, y + height * 0.4f, false));
		g.fillPath (highlight);
	}

	g.setColour (colour.darker().withMultipliedAlpha (1.5f));
	g.strokePath (outline, PathStrokeType (outlineThickness));
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_LookAndFeel.cpp ***/


/*** Start of inlined file: juce_OldSchoolLookAndFeel.cpp ***/
BEGIN_JUCE_NAMESPACE

OldSchoolLookAndFeel::OldSchoolLookAndFeel()
{
	setColour (TextButton::buttonColourId,	  Colour (0xffbbbbff));
	setColour (ListBox::outlineColourId,		findColour (ComboBox::outlineColourId));
	setColour (ScrollBar::thumbColourId,		Colour (0xffbbbbdd));
	setColour (ScrollBar::backgroundColourId,	   Colours::transparentBlack);
	setColour (Slider::thumbColourId,		   Colours::white);
	setColour (Slider::trackColourId,		   Colour (0x7f000000));
	setColour (Slider::textBoxOutlineColourId,	  Colours::grey);
	setColour (ProgressBar::backgroundColourId,	 Colours::white.withAlpha (0.6f));
	setColour (ProgressBar::foregroundColourId,	 Colours::green.withAlpha (0.7f));
	setColour (PopupMenu::backgroundColourId,		 Colour (0xffeef5f8));
	setColour (PopupMenu::highlightedBackgroundColourId,  Colour (0xbfa4c2ce));
	setColour (PopupMenu::highlightedTextColourId,	Colours::black);
	setColour (TextEditor::focusedOutlineColourId,  findColour (TextButton::buttonColourId));

	scrollbarShadow.setShadowProperties (2.2f, 0.5f, 0, 0);
}

OldSchoolLookAndFeel::~OldSchoolLookAndFeel()
{
}

void OldSchoolLookAndFeel::drawButtonBackground (Graphics& g,
												 Button& button,
												 const Colour& backgroundColour,
												 bool isMouseOverButton,
												 bool isButtonDown)
{
	const int width = button.getWidth();
	const int height = button.getHeight();

	const float indent = 2.0f;
	const int cornerSize = jmin (roundToInt (width * 0.4f),
								 roundToInt (height * 0.4f));

	Path p;
	p.addRoundedRectangle (indent, indent,
						   width - indent * 2.0f,
						   height - indent * 2.0f,
						   (float) cornerSize);

	Colour bc (backgroundColour.withMultipliedSaturation (0.3f));

	if (isMouseOverButton)
	{
		if (isButtonDown)
			bc = bc.brighter();
		else if (bc.getBrightness() > 0.5f)
			bc = bc.darker (0.1f);
		else
			bc = bc.brighter (0.1f);
	}

	g.setColour (bc);
	g.fillPath (p);

	g.setColour (bc.contrasting().withAlpha ((isMouseOverButton) ? 0.6f : 0.4f));
	g.strokePath (p, PathStrokeType ((isMouseOverButton) ? 2.0f : 1.4f));
}

void OldSchoolLookAndFeel::drawTickBox (Graphics& g,
										Component& /*component*/,
										float x, float y, float w, float h,
										const bool ticked,
										const bool isEnabled,
										const bool /*isMouseOverButton*/,
										const bool isButtonDown)
{
	Path box;
	box.addRoundedRectangle (0.0f, 2.0f, 6.0f, 6.0f, 1.0f);

	g.setColour (isEnabled ? Colours::blue.withAlpha (isButtonDown ? 0.3f : 0.1f)
						   : Colours::lightgrey.withAlpha (0.1f));

	AffineTransform trans (AffineTransform::scale (w / 9.0f, h / 9.0f).translated (x, y));

	g.fillPath (box, trans);

	g.setColour (Colours::black.withAlpha (0.6f));
	g.strokePath (box, PathStrokeType (0.9f), trans);

	if (ticked)
	{
		Path tick;
		tick.startNewSubPath (1.5f, 3.0f);
		tick.lineTo (3.0f, 6.0f);
		tick.lineTo (6.0f, 0.0f);

		g.setColour (isEnabled ? Colours::black : Colours::grey);
		g.strokePath (tick, PathStrokeType (2.5f), trans);
	}
}

void OldSchoolLookAndFeel::drawToggleButton (Graphics& g,
											 ToggleButton& button,
											 bool isMouseOverButton,
											 bool isButtonDown)
{
	if (button.hasKeyboardFocus (true))
	{
		g.setColour (button.findColour (TextEditor::focusedOutlineColourId));
		g.drawRect (0, 0, button.getWidth(), button.getHeight());
	}

	const int tickWidth = jmin (20, button.getHeight() - 4);

	drawTickBox (g, button, 4.0f, (button.getHeight() - tickWidth) * 0.5f,
				 (float) tickWidth, (float) tickWidth,
				 button.getToggleState(),
				 button.isEnabled(),
				 isMouseOverButton,
				 isButtonDown);

	g.setColour (button.findColour (ToggleButton::textColourId));
	g.setFont (jmin (15.0f, button.getHeight() * 0.6f));

	if (! button.isEnabled())
		g.setOpacity (0.5f);

	const int textX = tickWidth + 5;

	g.drawFittedText (button.getButtonText(),
					  textX, 4,
					  button.getWidth() - textX - 2, button.getHeight() - 8,
					  Justification::centredLeft, 10);
}

void OldSchoolLookAndFeel::drawProgressBar (Graphics& g, ProgressBar& progressBar,
											int width, int height,
											double progress, const String& textToShow)
{
	if (progress < 0 || progress >= 1.0)
	{
		LookAndFeel::drawProgressBar (g, progressBar, width, height, progress, textToShow);
	}
	else
	{
		const Colour background (progressBar.findColour (ProgressBar::backgroundColourId));
		const Colour foreground (progressBar.findColour (ProgressBar::foregroundColourId));

		g.fillAll (background);
		g.setColour (foreground);

		g.fillRect (1, 1,
					jlimit (0, width - 2, roundToInt (progress * (width - 2))),
					height - 2);

		if (textToShow.isNotEmpty())
		{
			g.setColour (Colour::contrasting (background, foreground));
			g.setFont (height * 0.6f);

			g.drawText (textToShow, 0, 0, width, height, Justification::centred, false);
		}
	}
}

void OldSchoolLookAndFeel::drawScrollbarButton (Graphics& g,
												ScrollBar& bar,
												int width, int height,
												int buttonDirection,
												bool isScrollbarVertical,
												bool isMouseOverButton,
												bool isButtonDown)
{
	if (isScrollbarVertical)
		width -= 2;
	else
		height -= 2;

	Path p;

	if (buttonDirection == 0)
		p.addTriangle (width * 0.5f, height * 0.2f,
					   width * 0.1f, height * 0.7f,
					   width * 0.9f, height * 0.7f);
	else if (buttonDirection == 1)
		p.addTriangle (width * 0.8f, height * 0.5f,
					   width * 0.3f, height * 0.1f,
					   width * 0.3f, height * 0.9f);
	else if (buttonDirection == 2)
		p.addTriangle (width * 0.5f, height * 0.8f,
					   width * 0.1f, height * 0.3f,
					   width * 0.9f, height * 0.3f);
	else if (buttonDirection == 3)
		p.addTriangle (width * 0.2f, height * 0.5f,
					   width * 0.7f, height * 0.1f,
					   width * 0.7f, height * 0.9f);

	if (isButtonDown)
		g.setColour (Colours::white);
	else if (isMouseOverButton)
		g.setColour (Colours::white.withAlpha (0.7f));
	else
		g.setColour (bar.findColour (ScrollBar::thumbColourId).withAlpha (0.5f));

	g.fillPath (p);

	g.setColour (Colours::black.withAlpha (0.5f));
	g.strokePath (p, PathStrokeType (0.5f));
}

void OldSchoolLookAndFeel::drawScrollbar (Graphics& g,
										  ScrollBar& bar,
										  int x, int y,
										  int width, int height,
										  bool isScrollbarVertical,
										  int thumbStartPosition,
										  int thumbSize,
										  bool isMouseOver,
										  bool isMouseDown)
{
	g.fillAll (bar.findColour (ScrollBar::backgroundColourId));

	g.setColour (bar.findColour (ScrollBar::thumbColourId)
					.withAlpha ((isMouseOver || isMouseDown) ? 0.4f : 0.15f));

	if (thumbSize > 0.0f)
	{
		Rectangle<int> thumb;

		if (isScrollbarVertical)
		{
			width -= 2;
			g.fillRect (x + roundToInt (width * 0.35f), y,
						roundToInt (width * 0.3f), height);

			thumb.setBounds (x + 1, thumbStartPosition,
							 width - 2, thumbSize);
		}
		else
		{
			height -= 2;
			g.fillRect (x, y + roundToInt (height * 0.35f),
						width, roundToInt (height * 0.3f));

			thumb.setBounds (thumbStartPosition, y + 1,
							 thumbSize, height - 2);
		}

		g.setColour (bar.findColour (ScrollBar::thumbColourId)
						.withAlpha ((isMouseOver || isMouseDown) ? 0.95f : 0.7f));

		g.fillRect (thumb);

		g.setColour (Colours::black.withAlpha ((isMouseOver || isMouseDown) ? 0.4f : 0.25f));
		g.drawRect (thumb.getX(), thumb.getY(), thumb.getWidth(), thumb.getHeight());

		if (thumbSize > 16)
		{
			for (int i = 3; --i >= 0;)
			{
				const float linePos = thumbStartPosition + thumbSize / 2 + (i - 1) * 4.0f;
				g.setColour (Colours::black.withAlpha (0.15f));

				if (isScrollbarVertical)
				{
					g.drawLine (x + width * 0.2f, linePos, width * 0.8f, linePos);
					g.setColour (Colours::white.withAlpha (0.15f));
					g.drawLine (width * 0.2f, linePos - 1, width * 0.8f, linePos - 1);
				}
				else
				{
					g.drawLine (linePos, height * 0.2f, linePos, height * 0.8f);
					g.setColour (Colours::white.withAlpha (0.15f));
					g.drawLine (linePos - 1, height * 0.2f, linePos - 1, height * 0.8f);
				}
			}
		}
	}
}

ImageEffectFilter* OldSchoolLookAndFeel::getScrollbarEffect()
{
	return &scrollbarShadow;
}

void OldSchoolLookAndFeel::drawPopupMenuBackground (Graphics& g, int width, int height)
{
	g.fillAll (findColour (PopupMenu::backgroundColourId));

	g.setColour (Colours::black.withAlpha (0.6f));
	g.drawRect (0, 0, width, height);
}

void OldSchoolLookAndFeel::drawMenuBarBackground (Graphics& g, int /*width*/, int /*height*/,
												  bool, MenuBarComponent& menuBar)
{
	g.fillAll (menuBar.findColour (PopupMenu::backgroundColourId));
}

void OldSchoolLookAndFeel::drawTextEditorOutline (Graphics& g, int width, int height, TextEditor& textEditor)
{
	if (textEditor.isEnabled())
	{
		g.setColour (textEditor.findColour (TextEditor::outlineColourId));
		g.drawRect (0, 0, width, height);
	}
}

void OldSchoolLookAndFeel::drawComboBox (Graphics& g, int width, int height,
										 const bool isButtonDown,
										 int buttonX, int buttonY,
										 int buttonW, int buttonH,
										 ComboBox& box)
{
	g.fillAll (box.findColour (ComboBox::backgroundColourId));

	g.setColour (box.findColour ((isButtonDown) ? ComboBox::buttonColourId
												: ComboBox::backgroundColourId));
	g.fillRect (buttonX, buttonY, buttonW, buttonH);

	g.setColour (box.findColour (ComboBox::outlineColourId));
	g.drawRect (0, 0, width, height);

	const float arrowX = 0.2f;
	const float arrowH = 0.3f;

	if (box.isEnabled())
	{
		Path p;
		p.addTriangle (buttonX + buttonW * 0.5f,		buttonY + buttonH * (0.45f - arrowH),
					   buttonX + buttonW * (1.0f - arrowX), buttonY + buttonH * 0.45f,
					   buttonX + buttonW * arrowX,	  buttonY + buttonH * 0.45f);

		p.addTriangle (buttonX + buttonW * 0.5f,		buttonY + buttonH * (0.55f + arrowH),
					   buttonX + buttonW * (1.0f - arrowX), buttonY + buttonH * 0.55f,
					   buttonX + buttonW * arrowX,	  buttonY + buttonH * 0.55f);

		g.setColour (box.findColour ((isButtonDown) ? ComboBox::backgroundColourId
													: ComboBox::buttonColourId));
		g.fillPath (p);
	}
}

const Font OldSchoolLookAndFeel::getComboBoxFont (ComboBox& box)
{
	Font f (jmin (15.0f, box.getHeight() * 0.85f));
	f.setHorizontalScale (0.9f);
	return f;
}

static void drawTriangle (Graphics& g, float x1, float y1, float x2, float y2, float x3, float y3, const Colour& fill, const Colour& outline) throw()
{
	Path p;
	p.addTriangle (x1, y1, x2, y2, x3, y3);
	g.setColour (fill);
	g.fillPath (p);

	g.setColour (outline);
	g.strokePath (p, PathStrokeType (0.3f));
}

void OldSchoolLookAndFeel::drawLinearSlider (Graphics& g,
											 int x, int y,
											 int w, int h,
											 float sliderPos,
											 float minSliderPos,
											 float maxSliderPos,
											 const Slider::SliderStyle style,
											 Slider& slider)
{
	g.fillAll (slider.findColour (Slider::backgroundColourId));

	if (style == Slider::LinearBar)
	{
		g.setColour (slider.findColour (Slider::thumbColourId));
		g.fillRect (x, y, (int) sliderPos - x, h);

		g.setColour (slider.findColour (Slider::textBoxTextColourId).withMultipliedAlpha (0.5f));
		g.drawRect (x, y, (int) sliderPos - x, h);
	}
	else
	{
		g.setColour (slider.findColour (Slider::trackColourId)
						   .withMultipliedAlpha (slider.isEnabled() ? 1.0f : 0.3f));

		if (slider.isHorizontal())
		{
			g.fillRect (x, y + roundToInt (h * 0.6f),
						w, roundToInt (h * 0.2f));
		}
		else
		{
			g.fillRect (x + roundToInt (w * 0.5f - jmin (3.0f, w * 0.1f)), y,
						jmin (4, roundToInt (w * 0.2f)), h);
		}

		float alpha = 0.35f;

		if (slider.isEnabled())
			alpha = slider.isMouseOverOrDragging() ? 1.0f : 0.7f;

		const Colour fill (slider.findColour (Slider::thumbColourId).withAlpha (alpha));
		const Colour outline (Colours::black.withAlpha (slider.isEnabled() ? 0.7f : 0.35f));

		if (style == Slider::TwoValueVertical || style == Slider::ThreeValueVertical)
		{
			drawTriangle (g, x + w * 0.5f + jmin (4.0f, w * 0.3f), minSliderPos,
						  x + w * 0.5f - jmin (8.0f, w * 0.4f), minSliderPos - 7.0f,
						  x + w * 0.5f - jmin (8.0f, w * 0.4f), minSliderPos,
						  fill, outline);

			drawTriangle (g, x + w * 0.5f + jmin (4.0f, w * 0.3f), maxSliderPos,
						  x + w * 0.5f - jmin (8.0f, w * 0.4f), maxSliderPos,
						  x + w * 0.5f - jmin (8.0f, w * 0.4f), maxSliderPos + 7.0f,
						  fill, outline);
		}
		else if (style == Slider::TwoValueHorizontal || style == Slider::ThreeValueHorizontal)
		{
			drawTriangle (g, minSliderPos, y + h * 0.6f - jmin (4.0f, h * 0.3f),
						  minSliderPos - 7.0f, y + h * 0.9f ,
						  minSliderPos, y + h * 0.9f,
						  fill, outline);

			drawTriangle (g, maxSliderPos, y + h * 0.6f - jmin (4.0f, h * 0.3f),
						  maxSliderPos, y + h * 0.9f,
						  maxSliderPos + 7.0f, y + h * 0.9f,
						  fill, outline);
		}

		if (style == Slider::LinearHorizontal || style == Slider::ThreeValueHorizontal)
		{
			drawTriangle (g, sliderPos, y + h * 0.9f,
						  sliderPos - 7.0f, y + h * 0.2f,
						  sliderPos + 7.0f, y + h * 0.2f,
						  fill, outline);
		}
		else if (style == Slider::LinearVertical || style == Slider::ThreeValueVertical)
		{
			drawTriangle (g, x + w * 0.5f - jmin (4.0f, w * 0.3f), sliderPos,
						  x + w * 0.5f + jmin (8.0f, w * 0.4f), sliderPos - 7.0f,
						  x + w * 0.5f + jmin (8.0f, w * 0.4f), sliderPos + 7.0f,
						  fill, outline);
		}
	}
}

Button* OldSchoolLookAndFeel::createSliderButton (const bool isIncrement)
{
	if (isIncrement)
		return new ArrowButton ("u", 0.75f, Colours::white.withAlpha (0.8f));
	else
		return new ArrowButton ("d", 0.25f, Colours::white.withAlpha (0.8f));
}

ImageEffectFilter* OldSchoolLookAndFeel::getSliderEffect()
{
	return &scrollbarShadow;
}

int OldSchoolLookAndFeel::getSliderThumbRadius (Slider&)
{
	return 8;
}

void OldSchoolLookAndFeel::drawCornerResizer (Graphics& g,
											  int w, int h,
											  bool isMouseOver,
											  bool isMouseDragging)
{
	g.setColour ((isMouseOver || isMouseDragging) ? Colours::lightgrey
												  : Colours::darkgrey);

	const float lineThickness = jmin (w, h) * 0.1f;

	for (float i = 0.0f; i < 1.0f; i += 0.3f)
	{
		g.drawLine (w * i,
					h + 1.0f,
					w + 1.0f,
					h * i,
					lineThickness);
	}
}

Button* OldSchoolLookAndFeel::createDocumentWindowButton (int buttonType)
{
	Path shape;

	if (buttonType == DocumentWindow::closeButton)
	{
		shape.addLineSegment (0.0f, 0.0f, 1.0f, 1.0f, 0.35f);
		shape.addLineSegment (1.0f, 0.0f, 0.0f, 1.0f, 0.35f);

		ShapeButton* const b = new ShapeButton ("close",
												Colour (0x7fff3333),
												Colour (0xd7ff3333),
												Colour (0xf7ff3333));

		b->setShape (shape, true, true, true);
		return b;
	}
	else if (buttonType == DocumentWindow::minimiseButton)
	{
		shape.addLineSegment (0.0f, 0.5f, 1.0f, 0.5f, 0.25f);

		DrawableButton* b = new DrawableButton ("minimise", DrawableButton::ImageFitted);
		DrawablePath dp;
		dp.setPath (shape);
		dp.setFill (Colours::black.withAlpha (0.3f));
		b->setImages (&dp);
		return b;
	}
	else if (buttonType == DocumentWindow::maximiseButton)
	{
		shape.addLineSegment (0.5f, 0.0f, 0.5f, 1.0f, 0.25f);
		shape.addLineSegment (0.0f, 0.5f, 1.0f, 0.5f, 0.25f);

		DrawableButton* b = new DrawableButton ("maximise", DrawableButton::ImageFitted);
		DrawablePath dp;
		dp.setPath (shape);
		dp.setFill (Colours::black.withAlpha (0.3f));
		b->setImages (&dp);
		return b;
	}

	jassertfalse
	return 0;
}

void OldSchoolLookAndFeel::positionDocumentWindowButtons (DocumentWindow&,
														  int titleBarX,
														  int titleBarY,
														  int titleBarW,
														  int titleBarH,
														  Button* minimiseButton,
														  Button* maximiseButton,
														  Button* closeButton,
														  bool positionTitleBarButtonsOnLeft)
{
	titleBarY += titleBarH / 8;
	titleBarH -= titleBarH / 4;

	const int buttonW = titleBarH;

	int x = positionTitleBarButtonsOnLeft ? titleBarX + 4
										  : titleBarX + titleBarW - buttonW - 4;

	if (closeButton != 0)
	{
		closeButton->setBounds (x, titleBarY, buttonW, titleBarH);
		x += positionTitleBarButtonsOnLeft ? buttonW + buttonW / 5
										   : -(buttonW + buttonW / 5);
	}

	if (positionTitleBarButtonsOnLeft)
		swapVariables (minimiseButton, maximiseButton);

	if (maximiseButton != 0)
	{
		maximiseButton->setBounds (x, titleBarY - 2, buttonW, titleBarH);
		x += positionTitleBarButtonsOnLeft ? buttonW : -buttonW;
	}

	if (minimiseButton != 0)
		minimiseButton->setBounds (x, titleBarY - 2, buttonW, titleBarH);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_OldSchoolLookAndFeel.cpp ***/


/*** Start of inlined file: juce_MenuBarComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

class DummyMenuComponent  : public Component
{
	DummyMenuComponent (const DummyMenuComponent&);
	DummyMenuComponent& operator= (const DummyMenuComponent&);

public:
	DummyMenuComponent()	{}
	~DummyMenuComponent()   {}

	void inputAttemptWhenModal()
	{
		exitModalState (0);
	}
};

MenuBarComponent::MenuBarComponent (MenuBarModel* model_)
	: model (0),
	  itemUnderMouse (-1),
	  currentPopupIndex (-1),
	  indexToShowAgain (-1),
	  lastMouseX (0),
	  lastMouseY (0),
	  inModalState (false)
{
	setRepaintsOnMouseActivity (true);
	setWantsKeyboardFocus (false);
	setMouseClickGrabsKeyboardFocus (false);

	setModel (model_);
}

MenuBarComponent::~MenuBarComponent()
{
	setModel (0);

	Desktop::getInstance().removeGlobalMouseListener (this);
	currentPopup = 0;
}

void MenuBarComponent::setModel (MenuBarModel* const newModel)
{
	if (model != newModel)
	{
		if (model != 0)
			model->removeListener (this);

		model = newModel;

		if (model != 0)
			model->addListener (this);

		repaint();
		menuBarItemsChanged (0);
	}
}

void MenuBarComponent::paint (Graphics& g)
{
	const bool isMouseOverBar = currentPopupIndex >= 0 || itemUnderMouse >= 0 || isMouseOver();

	getLookAndFeel().drawMenuBarBackground (g,
											getWidth(),
											getHeight(),
											isMouseOverBar,
											*this);

	if (model != 0)
	{
		for (int i = 0; i < menuNames.size(); ++i)
		{
			g.saveState();
			g.setOrigin (xPositions [i], 0);
			g.reduceClipRegion (0, 0, xPositions[i + 1] - xPositions[i], getHeight());

			getLookAndFeel().drawMenuBarItem (g,
											  xPositions[i + 1] - xPositions[i],
											  getHeight(),
											  i,
											  menuNames[i],
											  i == itemUnderMouse,
											  i == currentPopupIndex,
											  isMouseOverBar,
											  *this);

			g.restoreState();
		}
	}
}

void MenuBarComponent::resized()
{
	xPositions.clear();
	int x = 2;
	xPositions.add (x);

	for (int i = 0; i < menuNames.size(); ++i)
	{
		x += getLookAndFeel().getMenuBarItemWidth (*this, i, menuNames[i]);

		xPositions.add (x);
	}
}

int MenuBarComponent::getItemAt (const int x, const int y)
{
	for (int i = 0; i < xPositions.size(); ++i)
		if (x >= xPositions[i] && x < xPositions[i + 1])
			return reallyContains (x, y, true) ? i : -1;

	return -1;
}

void MenuBarComponent::repaintMenuItem (int index)
{
	if (((unsigned int) index) < (unsigned int) xPositions.size())
	{
		const int x1 = xPositions [index];
		const int x2 = xPositions [index + 1];

		repaint (x1 - 2, 0, x2 - x1 + 4, getHeight());
	}
}

void MenuBarComponent::updateItemUnderMouse (int x, int y)
{
	const int newItem = getItemAt (x, y);

	if (itemUnderMouse != newItem)
	{
		repaintMenuItem (itemUnderMouse);
		itemUnderMouse = newItem;
		repaintMenuItem (itemUnderMouse);
	}
}

void MenuBarComponent::hideCurrentMenu()
{
	currentPopup = 0;
	repaint();
}

void MenuBarComponent::showMenu (int index)
{
	if (index != currentPopupIndex)
	{
		if (inModalState)
		{
			hideCurrentMenu();
			indexToShowAgain = index;
			return;
		}

		indexToShowAgain = -1;
		currentPopupIndex = -1;
		itemUnderMouse = index;
		currentPopup = 0;
		menuBarItemsChanged (0);

		Component::SafePointer<Component> prevFocused (getCurrentlyFocusedComponent());
		Component::SafePointer<Component> deletionChecker (this);

		enterModalState (false);
		inModalState = true;
		int result = 0;
		ApplicationCommandManager* managerOfChosenCommand = 0;

		Desktop::getInstance().addGlobalMouseListener (this);

		for (;;)
		{
			const int x = getScreenX() + xPositions [itemUnderMouse];
			const int w = xPositions [itemUnderMouse + 1] - xPositions [itemUnderMouse];

			currentPopupIndex = itemUnderMouse;
			indexToShowAgain = -1;
			repaint();

			if (((unsigned int) itemUnderMouse) < (unsigned int) menuNames.size())
			{
				PopupMenu m (model->getMenuForIndex (itemUnderMouse,
													 menuNames [itemUnderMouse]));

				if (m.lookAndFeel == 0)
					m.setLookAndFeel (&getLookAndFeel());

				currentPopup = m.createMenuComponent (x, getScreenY(),
													  w, getHeight(),
													  0, w, 0, 0,
													  true, this,
													  &managerOfChosenCommand,
													  this);
			}

			if (currentPopup == 0)
			{
				currentPopup = new DummyMenuComponent();
				addAndMakeVisible (currentPopup);
			}

			currentPopup->enterModalState (false);
			currentPopup->toFront (false);  // need to do this after making it modal, or it could
											// be stuck behind other comps that are already modal..
			result = currentPopup->runModalLoop();

			if (deletionChecker == 0)
				return;

			const int lastPopupIndex = currentPopupIndex;
			currentPopup = 0;
			currentPopupIndex = -1;

			if (result != 0)
			{
				topLevelIndexClicked = lastPopupIndex;
				break;
			}
			else if (indexToShowAgain >= 0)
			{
				menuBarItemsChanged (0);
				repaint();
				itemUnderMouse = indexToShowAgain;

				if (((unsigned int) itemUnderMouse) >= (unsigned int) menuNames.size())
					break;
			}
			else
			{
				break;
			}
		}

		Desktop::getInstance().removeGlobalMouseListener (this);

		inModalState = false;
		exitModalState (0);

		if (prevFocused != 0)
			prevFocused->grabKeyboardFocus();

		const Point<int> mousePos (getMouseXYRelative());
		updateItemUnderMouse (mousePos.getX(), mousePos.getY());
		repaint();

		if (result != 0)
		{
			if (managerOfChosenCommand != 0)
			{
				ApplicationCommandTarget::InvocationInfo info (result);
				info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromMenu;

				managerOfChosenCommand->invoke (info, true);
			}

			postCommandMessage (result);
		}
	}
}

void MenuBarComponent::handleCommandMessage (int commandId)
{
	if (model != 0)
		model->menuItemSelected (commandId, topLevelIndexClicked);
}

void MenuBarComponent::mouseEnter (const MouseEvent& e)
{
	if (e.eventComponent == this)
		updateItemUnderMouse (e.x, e.y);
}

void MenuBarComponent::mouseExit (const MouseEvent& e)
{
	if (e.eventComponent == this)
		updateItemUnderMouse (e.x, e.y);
}

void MenuBarComponent::mouseDown (const MouseEvent& e)
{
	if (currentPopupIndex < 0)
	{
		const MouseEvent e2 (e.getEventRelativeTo (this));
		updateItemUnderMouse (e2.x, e2.y);

		currentPopupIndex = -2;
		showMenu (itemUnderMouse);
	}
}

void MenuBarComponent::mouseDrag (const MouseEvent& e)
{
	const MouseEvent e2 (e.getEventRelativeTo (this));

	const int item = getItemAt (e2.x, e2.y);

	if (item >= 0)
		showMenu (item);
}

void MenuBarComponent::mouseUp (const MouseEvent& e)
{
	const MouseEvent e2 (e.getEventRelativeTo (this));

	updateItemUnderMouse (e2.x, e2.y);

	if (itemUnderMouse < 0 && dynamic_cast <DummyMenuComponent*> (static_cast <Component*> (currentPopup)) != 0)
		hideCurrentMenu();
}

void MenuBarComponent::mouseMove (const MouseEvent& e)
{
	const MouseEvent e2 (e.getEventRelativeTo (this));

	if (lastMouseX != e2.x || lastMouseY != e2.y)
	{
		if (currentPopupIndex >= 0)
		{
			const int item = getItemAt (e2.x, e2.y);

			if (item >= 0)
				showMenu (item);
		}
		else
		{
			updateItemUnderMouse (e2.x, e2.y);
		}

		lastMouseX = e2.x;
		lastMouseY = e2.y;
	}
}

bool MenuBarComponent::keyPressed (const KeyPress& key)
{
	bool used = false;
	const int numMenus = menuNames.size();
	const int currentIndex = jlimit (0, menuNames.size() - 1, currentPopupIndex);

	if (key.isKeyCode (KeyPress::leftKey))
	{
		showMenu ((currentIndex + numMenus - 1) % numMenus);
		used = true;
	}
	else if (key.isKeyCode (KeyPress::rightKey))
	{
		showMenu ((currentIndex + 1) % numMenus);
		used = true;
	}

	return used;
}

void MenuBarComponent::inputAttemptWhenModal()
{
	hideCurrentMenu();
}

void MenuBarComponent::menuBarItemsChanged (MenuBarModel* /*menuBarModel*/)
{
	StringArray newNames;

	if (model != 0)
		newNames = model->getMenuBarNames();

	if (newNames != menuNames)
	{
		menuNames = newNames;
		repaint();
		resized();
	}
}

void MenuBarComponent::menuCommandInvoked (MenuBarModel* /*menuBarModel*/,
										   const ApplicationCommandTarget::InvocationInfo& info)
{
	if (model == 0
		 || (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) != 0)
		return;

	for (int i = 0; i < menuNames.size(); ++i)
	{
		const PopupMenu menu (model->getMenuForIndex (i, menuNames [i]));

		if (menu.containsCommandItem (info.commandID))
		{
			itemUnderMouse = i;
			repaintMenuItem (i);
			startTimer (200);

			break;
		}
	}
}

void MenuBarComponent::timerCallback()
{
	stopTimer();

	const Point<int> mousePos (getMouseXYRelative());
	updateItemUnderMouse (mousePos.getX(), mousePos.getY());
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MenuBarComponent.cpp ***/


/*** Start of inlined file: juce_MenuBarModel.cpp ***/
BEGIN_JUCE_NAMESPACE

MenuBarModel::MenuBarModel() throw()
	: manager (0)
{
}

MenuBarModel::~MenuBarModel()
{
	setApplicationCommandManagerToWatch (0);
}

void MenuBarModel::menuItemsChanged()
{
	triggerAsyncUpdate();
}

void MenuBarModel::setApplicationCommandManagerToWatch (ApplicationCommandManager* const newManager) throw()
{
	if (manager != newManager)
	{
		if (manager != 0)
			manager->removeListener (this);

		manager = newManager;

		if (manager != 0)
			manager->addListener (this);
	}
}

void MenuBarModel::addListener (MenuBarModelListener* const newListener) throw()
{
	listeners.add (newListener);
}

void MenuBarModel::removeListener (MenuBarModelListener* const listenerToRemove) throw()
{
	// Trying to remove a listener that isn't on the list!
	// If this assertion happens because this object is a dangling pointer, make sure you've not
	// deleted this menu model while it's still being used by something (e.g. by a MenuBarComponent)
	jassert (listeners.contains (listenerToRemove));

	listeners.remove (listenerToRemove);
}

void MenuBarModel::handleAsyncUpdate()
{
	listeners.call (&MenuBarModelListener::menuBarItemsChanged, this);
}

void MenuBarModel::applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo& info)
{
	listeners.call (&MenuBarModelListener::menuCommandInvoked, this, info);
}

void MenuBarModel::applicationCommandListChanged()
{
	menuItemsChanged();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MenuBarModel.cpp ***/


/*** Start of inlined file: juce_PopupMenu.cpp ***/
BEGIN_JUCE_NAMESPACE

class PopupMenu::Item
{
public:

	Item()
	  : itemId (0), active (true), isSeparator (true), isTicked (false),
		usesColour (false), customComp (0), commandManager (0)
	{
	}

	Item (const int itemId_,
		  const String& text_,
		  const bool active_,
		  const bool isTicked_,
		  const Image* im,
		  const Colour& textColour_,
		  const bool usesColour_,
		  PopupMenuCustomComponent* const customComp_,
		  const PopupMenu* const subMenu_,
		  ApplicationCommandManager* const commandManager_)
	  : itemId (itemId_), text (text_), textColour (textColour_),
		active (active_), isSeparator (false), isTicked (isTicked_),
		usesColour (usesColour_), customComp (customComp_),
		commandManager (commandManager_)
	{
		if (subMenu_ != 0)
			subMenu = new PopupMenu (*subMenu_);

		if (im != 0)
			image = im->createCopy();

		if (commandManager_ != 0 && itemId_ != 0)
		{
			String shortcutKey;

			Array <KeyPress> keyPresses (commandManager_->getKeyMappings()
											->getKeyPressesAssignedToCommand (itemId_));

			for (int i = 0; i < keyPresses.size(); ++i)
			{
				const String key (keyPresses.getReference(i).getTextDescription());

				if (shortcutKey.isNotEmpty())
					shortcutKey << ", ";

				if (key.length() == 1)
					shortcutKey << "shortcut: '" << key << '\'';
				else
					shortcutKey << key;
			}

			shortcutKey = shortcutKey.trim();

			if (shortcutKey.isNotEmpty())
				text << "<end>" << shortcutKey;
		}
	}

	Item (const Item& other)
		: itemId (other.itemId),
		  text (other.text),
		  textColour (other.textColour),
		  active (other.active),
		  isSeparator (other.isSeparator),
		  isTicked (other.isTicked),
		  usesColour (other.usesColour),
		  customComp (other.customComp),
		  commandManager (other.commandManager)
	{
		if (other.subMenu != 0)
			subMenu = new PopupMenu (*(other.subMenu));

		if (other.image != 0)
			image = other.image->createCopy();
	}

	~Item()
	{
		customComp = 0;
	}

	bool canBeTriggered() const throw()
	{
		return active && ! (isSeparator || (subMenu != 0));
	}

	bool hasActiveSubMenu() const throw()
	{
		return active && (subMenu != 0);
	}

	const int itemId;
	String text;
	const Colour textColour;
	const bool active, isSeparator, isTicked, usesColour;
	ScopedPointer <Image> image;
	ReferenceCountedObjectPtr <PopupMenuCustomComponent> customComp;
	ScopedPointer <PopupMenu> subMenu;
	ApplicationCommandManager* const commandManager;

	juce_UseDebuggingNewOperator

private:
	Item& operator= (const Item&);
};

class PopupMenu::ItemComponent  : public Component
{
public:

	ItemComponent (const PopupMenu::Item& itemInfo_)
	  : itemInfo (itemInfo_),
		isHighlighted (false)
	{
		if (itemInfo.customComp != 0)
			addAndMakeVisible (itemInfo.customComp);
	}

	~ItemComponent()
	{
		if (itemInfo.customComp != 0)
			removeChildComponent (itemInfo.customComp);
	}

	void getIdealSize (int& idealWidth,
					   int& idealHeight,
					   const int standardItemHeight)
	{
		if (itemInfo.customComp != 0)
		{
			itemInfo.customComp->getIdealSize (idealWidth, idealHeight);
		}
		else
		{
			getLookAndFeel().getIdealPopupMenuItemSize (itemInfo.text,
														itemInfo.isSeparator,
														standardItemHeight,
														idealWidth,
														idealHeight);
		}
	}

	void paint (Graphics& g)
	{
		if (itemInfo.customComp == 0)
		{
			String mainText (itemInfo.text);
			String endText;
			const int endIndex = mainText.indexOf ("<end>");

			if (endIndex >= 0)
			{
				endText = mainText.substring (endIndex + 5).trim();
				mainText = mainText.substring (0, endIndex);
			}

			getLookAndFeel()
				.drawPopupMenuItem (g, getWidth(), getHeight(),
									itemInfo.isSeparator,
									itemInfo.active,
									isHighlighted,
									itemInfo.isTicked,
									itemInfo.subMenu != 0,
									mainText, endText,
									itemInfo.image,
									itemInfo.usesColour ? &(itemInfo.textColour) : 0);
		}
	}

	void resized()
	{
		if (getNumChildComponents() > 0)
			getChildComponent(0)->setBounds (2, 0, getWidth() - 4, getHeight());
	}

	void setHighlighted (bool shouldBeHighlighted)
	{
		shouldBeHighlighted = shouldBeHighlighted && itemInfo.active;

		if (isHighlighted != shouldBeHighlighted)
		{
			isHighlighted = shouldBeHighlighted;

			if (itemInfo.customComp != 0)
			{
				itemInfo.customComp->isHighlighted = shouldBeHighlighted;
				itemInfo.customComp->repaint();
			}

			repaint();
		}
	}

	PopupMenu::Item itemInfo;

	juce_UseDebuggingNewOperator

private:
	bool isHighlighted;

	ItemComponent (const ItemComponent&);
	ItemComponent& operator= (const ItemComponent&);
};

namespace PopupMenuSettings
{
	static const int scrollZone = 24;
	static const int borderSize = 2;
	static const int timerInterval = 50;
	static const int dismissCommandId = 0x6287345f;
}

class PopupMenu::Window  : public Component,
						   private Timer
{
public:

	Window()
	   : Component ("menu"),
		 owner (0),
		 currentChild (0),
		 activeSubMenu (0),
		 menuBarComponent (0),
		 managerOfChosenCommand (0),
		 minimumWidth (0),
		 maximumNumColumns (7),
		 standardItemHeight (0),
		 isOver (false),
		 hasBeenOver (false),
		 isDown (false),
		 needsToScroll (false),
		 hideOnExit (false),
		 disableMouseMoves (false),
		 hasAnyJuceCompHadFocus (false),
		 numColumns (0),
		 contentHeight (0),
		 childYOffset (0),
		 timeEnteredCurrentChildComp (0),
		 scrollAcceleration (1.0)
	{
		menuCreationTime = lastFocused = lastScroll = Time::getMillisecondCounter();
		setWantsKeyboardFocus (true);
		setMouseClickGrabsKeyboardFocus (false);

		setOpaque (true);
		setAlwaysOnTop (true);

		Desktop::getInstance().addGlobalMouseListener (this);

		getActiveWindows().add (this);
	}

	~Window()
	{
		getActiveWindows().removeValue (this);

		Desktop::getInstance().removeGlobalMouseListener (this);

		jassert (activeSubMenu == 0 || activeSubMenu->isValidComponent());
		activeSubMenu = 0;

		deleteAllChildren();
	}

	static Window* create (const PopupMenu& menu,
						   const bool dismissOnMouseUp,
						   Window* const owner_,
						   const int minX, const int maxX,
						   const int minY, const int maxY,
						   const int minimumWidth,
						   const int maximumNumColumns,
						   const int standardItemHeight,
						   const bool alignToRectangle,
						   const int itemIdThatMustBeVisible,
						   Component* const menuBarComponent,
						   ApplicationCommandManager** managerOfChosenCommand,
						   Component* const componentAttachedTo)
	{
		if (menu.items.size() > 0)
		{
			int totalItems = 0;

			ScopedPointer <Window> mw (new Window());
			mw->setLookAndFeel (menu.lookAndFeel);
			mw->setWantsKeyboardFocus (false);
			mw->minimumWidth = minimumWidth;
			mw->maximumNumColumns = maximumNumColumns;
			mw->standardItemHeight = standardItemHeight;
			mw->dismissOnMouseUp = dismissOnMouseUp;

			for (int i = 0; i < menu.items.size(); ++i)
			{
				PopupMenu::Item* const item = menu.items.getUnchecked(i);

				mw->addItem (*item);
				++totalItems;
			}

			if (totalItems > 0)
			{
				mw->owner = owner_;
				mw->menuBarComponent = menuBarComponent;
				mw->managerOfChosenCommand = managerOfChosenCommand;
				mw->componentAttachedTo = componentAttachedTo;
				mw->componentAttachedToOriginal = componentAttachedTo;

				mw->calculateWindowPos (minX, maxX, minY, maxY, alignToRectangle);
				mw->setTopLeftPosition (mw->windowPos.getX(),
										mw->windowPos.getY());
				mw->updateYPositions();

				if (itemIdThatMustBeVisible != 0)
				{
					const int y = minY - mw->windowPos.getY();
					mw->ensureItemIsVisible (itemIdThatMustBeVisible,
											 (((unsigned int) y) < (unsigned int) mw->windowPos.getHeight()) ? y : -1);
				}

				mw->resizeToBestWindowPos();
				mw->addToDesktop (ComponentPeer::windowIsTemporary
									| mw->getLookAndFeel().getMenuWindowFlags());

				return mw.release();
			}
		}

		return 0;
	}

	void paint (Graphics& g)
	{
		getLookAndFeel().drawPopupMenuBackground (g, getWidth(), getHeight());
	}

	void paintOverChildren (Graphics& g)
	{
		if (isScrolling())
		{
			LookAndFeel& lf = getLookAndFeel();

			if (isScrollZoneActive (false))
				lf.drawPopupMenuUpDownArrow (g, getWidth(), PopupMenuSettings::scrollZone, true);

			if (isScrollZoneActive (true))
			{
				g.setOrigin (0, getHeight() - PopupMenuSettings::scrollZone);
				lf.drawPopupMenuUpDownArrow (g, getWidth(), PopupMenuSettings::scrollZone, false);
			}
		}
	}

	bool isScrollZoneActive (bool bottomOne) const
	{
		return isScrolling()
				&& (bottomOne
						? childYOffset < contentHeight - windowPos.getHeight()
						: childYOffset > 0);
	}

	void addItem (const PopupMenu::Item& item)
	{
		PopupMenu::ItemComponent* const mic = new PopupMenu::ItemComponent (item);
		addAndMakeVisible (mic);

		int itemW = 80;
		int itemH = 16;
		mic->getIdealSize (itemW, itemH, standardItemHeight);
		mic->setSize (itemW, jlimit (2, 600, itemH));
		mic->addMouseListener (this, false);
	}

	// hide this and all sub-comps
	void hide (const PopupMenu::Item* const item)
	{
		if (isVisible())
		{
			jassert (activeSubMenu == 0 || activeSubMenu->isValidComponent());

			activeSubMenu = 0;
			currentChild = 0;

			exitModalState (item != 0 ? item->itemId : 0);
			setVisible (false);

			if (item != 0
				 && item->commandManager != 0
				 && item->itemId != 0)
			{
				*managerOfChosenCommand = item->commandManager;
			}
		}
	}

	void dismissMenu (const PopupMenu::Item* const item)
	{
		if (owner != 0)
		{
			owner->dismissMenu (item);
		}
		else
		{
			if (item != 0)
			{
				// need a copy of this on the stack as the one passed in will get deleted during this call
				const PopupMenu::Item mi (*item);
				hide (&mi);
			}
			else
			{
				hide (0);
			}
		}
	}

	void mouseMove (const MouseEvent&)
	{
		timerCallback();
	}

	void mouseDown (const MouseEvent&)
	{
		timerCallback();
	}

	void mouseDrag (const MouseEvent&)
	{
		timerCallback();
	}

	void mouseUp (const MouseEvent&)
	{
		timerCallback();
	}

	void mouseWheelMove (const MouseEvent&, float /*amountX*/, float amountY)
	{
		alterChildYPos (roundToInt (-10.0f * amountY * PopupMenuSettings::scrollZone));
		lastMouse = Point<int> (-1, -1);
	}

	bool keyPressed (const KeyPress& key)
	{
		if (key.isKeyCode (KeyPress::downKey))
		{
			selectNextItem (1);
		}
		else if (key.isKeyCode (KeyPress::upKey))
		{
			selectNextItem (-1);
		}
		else if (key.isKeyCode (KeyPress::leftKey))
		{
			if (owner != 0)
			{
				Component::SafePointer<Window> parentWindow (owner);
				PopupMenu::ItemComponent* currentChildOfParent = parentWindow->currentChild;

				hide (0);

				if (parentWindow != 0)
					parentWindow->setCurrentlyHighlightedChild (currentChildOfParent);

				disableTimerUntilMouseMoves();
			}
			else if (menuBarComponent != 0)
			{
				menuBarComponent->keyPressed (key);
			}
		}
		else if (key.isKeyCode (KeyPress::rightKey))
		{
			disableTimerUntilMouseMoves();

			if (showSubMenuFor (currentChild))
			{
				jassert (activeSubMenu == 0 || activeSubMenu->isValidComponent());

				if (activeSubMenu != 0 && activeSubMenu->isVisible())
					activeSubMenu->selectNextItem (1);
			}
			else if (menuBarComponent != 0)
			{
				menuBarComponent->keyPressed (key);
			}
		}
		else if (key.isKeyCode (KeyPress::returnKey))
		{
			triggerCurrentlyHighlightedItem();
		}
		else if (key.isKeyCode (KeyPress::escapeKey))
		{
			dismissMenu (0);
		}
		else
		{
			return false;
		}

		return true;
	}

	void inputAttemptWhenModal()
	{
		timerCallback();

		if (! isOverAnyMenu())
		{
			if (componentAttachedTo != 0)
			{
				// we want to dismiss the menu, but if we do it synchronously, then
				// the mouse-click will be allowed to pass through. That's good, except
				// when the user clicks on the button that orginally popped the menu up,
				// as they'll expect the menu to go away, and in fact it'll just
				// come back. So only dismiss synchronously if they're not on the original
				// comp that we're attached to.
				const Point<int> mousePos (componentAttachedTo->getMouseXYRelative());

				if (componentAttachedTo->reallyContains (mousePos.getX(), mousePos.getY(), true))
				{
					postCommandMessage (PopupMenuSettings::dismissCommandId); // dismiss asynchrounously
					return;
				}
			}

			dismissMenu (0);
		}
	}

	void handleCommandMessage (int commandId)
	{
		Component::handleCommandMessage (commandId);

		if (commandId == PopupMenuSettings::dismissCommandId)
			dismissMenu (0);
	}

	void timerCallback()
	{
		if (! isVisible())
			return;

		if (componentAttachedTo != componentAttachedToOriginal)
		{
			dismissMenu (0);
			return;
		}

		Window* currentlyModalWindow = dynamic_cast <Window*> (Component::getCurrentlyModalComponent());

		if (currentlyModalWindow != 0 && ! treeContains (currentlyModalWindow))
			return;

		startTimer (PopupMenuSettings::timerInterval);  // do this in case it was called from a mouse
														// move rather than a real timer callback

		const Point<int> globalMousePos (Desktop::getMousePosition());
		const Point<int> localMousePos (globalPositionToRelative (globalMousePos));

		const uint32 now = Time::getMillisecondCounter();

		if (now > timeEnteredCurrentChildComp + 100
			 && reallyContains (localMousePos.getX(), localMousePos.getY(), true)
			 && currentChild->isValidComponent()
			 && (! disableMouseMoves)
			 && ! (activeSubMenu != 0 && activeSubMenu->isVisible()))
		{
			showSubMenuFor (currentChild);
		}

		if (globalMousePos != lastMouse || now > lastMouseMoveTime + 350)
		{
			highlightItemUnderMouse (globalMousePos, localMousePos);
		}

		bool overScrollArea = false;

		if (isScrolling()
			 && (isOver || (isDown && ((unsigned int) localMousePos.getX()) < (unsigned int) getWidth()))
			 && ((isScrollZoneActive (false) && localMousePos.getY() < PopupMenuSettings::scrollZone)
				  || (isScrollZoneActive (true) && localMousePos.getY() > getHeight() - PopupMenuSettings::scrollZone)))
		{
			if (now > lastScroll + 20)
			{
				scrollAcceleration = jmin (4.0, scrollAcceleration * 1.04);
				int amount = 0;

				for (int i = 0; i < getNumChildComponents() && amount == 0; ++i)
					amount = ((int) scrollAcceleration) * getChildComponent (i)->getHeight();

				alterChildYPos (localMousePos.getY() < PopupMenuSettings::scrollZone ? -amount : amount);

				lastScroll = now;
			}

			overScrollArea = true;
			lastMouse = Point<int> (-1, -1); // trigger a mouse-move
		}
		else
		{
			scrollAcceleration = 1.0;
		}

		const bool wasDown = isDown;
		bool isOverAny = isOverAnyMenu();

		if (hideOnExit && hasBeenOver && (! isOverAny) && activeSubMenu != 0)
		{
			activeSubMenu->updateMouseOverStatus (globalMousePos);
			isOverAny = isOverAnyMenu();
		}

		if (hideOnExit && hasBeenOver && ! isOverAny)
		{
			hide (0);
		}
		else
		{
			isDown = hasBeenOver
						&& (ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown()
							|| ModifierKeys::getCurrentModifiersRealtime().isAnyMouseButtonDown());

			bool anyFocused = Process::isForegroundProcess();

			if (anyFocused && Component::getCurrentlyFocusedComponent() == 0)
			{
				// because no component at all may have focus, our test here will
				// only be triggered when something has focus and then loses it.
				anyFocused = ! hasAnyJuceCompHadFocus;

				for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
				{
					if (ComponentPeer::getPeer (i)->isFocused())
					{
						anyFocused = true;
						hasAnyJuceCompHadFocus = true;
						break;
					}
				}
			}

			if (! anyFocused)
			{
				if (now > lastFocused + 10)
				{
					wasHiddenBecauseOfAppChange() = true;
					dismissMenu (0);

					return;  // may have been deleted by the previous call..
				}
			}
			else if (wasDown && now > menuCreationTime + 250
					   && ! (isDown || overScrollArea))
			{
				isOver = reallyContains (localMousePos.getX(), localMousePos.getY(), true);

				if (isOver)
				{
					triggerCurrentlyHighlightedItem();
				}
				else if ((hasBeenOver || ! dismissOnMouseUp) && ! isOverAny)
				{
					dismissMenu (0);
				}

				return;  // may have been deleted by the previous calls..
			}
			else
			{
				lastFocused = now;
			}
		}
	}

	static Array<Window*>& getActiveWindows()
	{
		static Array<Window*> activeMenuWindows;
		return activeMenuWindows;
	}

	static bool& wasHiddenBecauseOfAppChange() throw()
	{
		static bool b = false;
		return b;
	}

	juce_UseDebuggingNewOperator

private:
	Window* owner;
	PopupMenu::ItemComponent* currentChild;
	ScopedPointer <Window> activeSubMenu;
	Component* menuBarComponent;
	ApplicationCommandManager** managerOfChosenCommand;
	Component::SafePointer<Component> componentAttachedTo;
	Component* componentAttachedToOriginal;
	Rectangle<int> windowPos;
	Point<int> lastMouse;
	int minimumWidth, maximumNumColumns, standardItemHeight;
	bool isOver, hasBeenOver, isDown, needsToScroll;
	bool dismissOnMouseUp, hideOnExit, disableMouseMoves, hasAnyJuceCompHadFocus;
	int numColumns, contentHeight, childYOffset;
	Array <int> columnWidths;
	uint32 menuCreationTime, lastFocused, lastScroll, lastMouseMoveTime, timeEnteredCurrentChildComp;
	double scrollAcceleration;

	bool overlaps (const Rectangle<int>& r) const
	{
		return r.intersects (getBounds())
				|| (owner != 0 && owner->overlaps (r));
	}

	bool isOverAnyMenu() const
	{
		return (owner != 0) ? owner->isOverAnyMenu()
							: isOverChildren();
	}

	bool isOverChildren() const
	{
		jassert (activeSubMenu == 0 || activeSubMenu->isValidComponent());

		return isVisible()
				&& (isOver || (activeSubMenu != 0 && activeSubMenu->isOverChildren()));
	}

	void updateMouseOverStatus (const Point<int>& globalMousePos)
	{
		const Point<int> relPos (globalPositionToRelative (globalMousePos));
		isOver = reallyContains (relPos.getX(), relPos.getY(), true);

		if (activeSubMenu != 0)
			activeSubMenu->updateMouseOverStatus (globalMousePos);
	}

	bool treeContains (const Window* const window) const throw()
	{
		const Window* mw = this;

		while (mw->owner != 0)
			mw = mw->owner;

		while (mw != 0)
		{
			if (mw == window)
				return true;

			mw = mw->activeSubMenu;
		}

		return false;
	}

	void calculateWindowPos (const int minX, const int maxX,
							 const int minY, const int maxY,
							 const bool alignToRectangle)
	{
		const Rectangle<int> mon (Desktop::getInstance()
									 .getMonitorAreaContaining (Point<int> ((minX + maxX) / 2,
																			(minY + maxY) / 2),
#if JUCE_MAC
																true));
#else
																false)); // on windows, don't stop the menu overlapping the taskbar
#endif

		int x, y, widthToUse, heightToUse;
		layoutMenuItems (mon.getWidth() - 24, widthToUse, heightToUse);

		if (alignToRectangle)
		{
			x = minX;

			const int spaceUnder = mon.getHeight() - (maxY - mon.getY());
			const int spaceOver = minY - mon.getY();

			if (heightToUse < spaceUnder - 30 || spaceUnder >= spaceOver)
				y = maxY;
			else
				y = minY - heightToUse;
		}
		else
		{
			bool tendTowardsRight = (minX + maxX) / 2 < mon.getCentreX();

			if (owner != 0)
			{
				if (owner->owner != 0)
				{
					const bool ownerGoingRight = (owner->getX() + owner->getWidth() / 2
													> owner->owner->getX() + owner->owner->getWidth() / 2);

					if (ownerGoingRight && maxX + widthToUse < mon.getRight() - 4)
						tendTowardsRight = true;
					else if ((! ownerGoingRight) && minX > widthToUse + 4)
						tendTowardsRight = false;
				}
				else if (maxX + widthToUse < mon.getRight() - 32)
				{
					tendTowardsRight = true;
				}
			}

			const int biggestSpace = jmax (mon.getRight() - maxX,
										   minX - mon.getX()) - 32;

			if (biggestSpace < widthToUse)
			{
				layoutMenuItems (biggestSpace + (maxX - minX) / 3, widthToUse, heightToUse);

				if (numColumns > 1)
					layoutMenuItems (biggestSpace - 4, widthToUse, heightToUse);

				tendTowardsRight = (mon.getRight() - maxX) >= (minX - mon.getX());
			}

			if (tendTowardsRight)
				x = jmin (mon.getRight() - widthToUse - 4, maxX);
			else
				x = jmax (mon.getX() + 4, minX - widthToUse);

			y = minY;
			if ((minY + maxY) / 2 > mon.getCentreY())
				y = jmax (mon.getY(), maxY - heightToUse);
		}

		x = jmax (mon.getX() + 1, jmin (mon.getRight() - (widthToUse + 6), x));
		y = jmax (mon.getY() + 1, jmin (mon.getBottom() - (heightToUse + 6), y));

		windowPos.setBounds (x, y, widthToUse, heightToUse);

		// sets this flag if it's big enough to obscure any of its parent menus
		hideOnExit = (owner != 0)
					  && owner->windowPos.intersects (windowPos.expanded (-4, -4));
	}

	void layoutMenuItems (const int maxMenuW, int& width, int& height)
	{
		numColumns = 0;
		contentHeight = 0;
		const int maxMenuH = getParentHeight() - 24;
		int totalW;

		do
		{
			++numColumns;
			totalW = workOutBestSize (maxMenuW);

			if (totalW > maxMenuW)
			{
				numColumns = jmax (1, numColumns - 1);
				totalW = workOutBestSize (maxMenuW); // to update col widths
				break;
			}
			else if (totalW > maxMenuW / 2 || contentHeight < maxMenuH)
			{
				break;
			}

		} while (numColumns < maximumNumColumns);

		const int actualH = jmin (contentHeight, maxMenuH);

		needsToScroll = contentHeight > actualH;

		width = updateYPositions();
		height = actualH + PopupMenuSettings::borderSize * 2;
	}

	int workOutBestSize (const int maxMenuW)
	{
		int totalW = 0;
		contentHeight = 0;
		int childNum = 0;

		for (int col = 0; col < numColumns; ++col)
		{
			int i, colW = 50, colH = 0;

			const int numChildren = jmin (getNumChildComponents() - childNum,
										  (getNumChildComponents() + numColumns - 1) / numColumns);

			for (i = numChildren; --i >= 0;)
			{
				colW = jmax (colW, getChildComponent (childNum + i)->getWidth());
				colH += getChildComponent (childNum + i)->getHeight();
			}

			colW = jmin (maxMenuW / jmax (1, numColumns - 2), colW + PopupMenuSettings::borderSize * 2);

			columnWidths.set (col, colW);
			totalW += colW;
			contentHeight = jmax (contentHeight, colH);

			childNum += numChildren;
		}

		if (totalW < minimumWidth)
		{
			totalW = minimumWidth;

			for (int col = 0; col < numColumns; ++col)
				columnWidths.set (0, totalW / numColumns);
		}

		return totalW;
	}

	void ensureItemIsVisible (const int itemId, int wantedY)
	{
		jassert (itemId != 0)

		for (int i = getNumChildComponents(); --i >= 0;)
		{
			PopupMenu::ItemComponent* const m = static_cast <PopupMenu::ItemComponent*> (getChildComponent (i));

			if (m != 0
				&& m->itemInfo.itemId == itemId
				&& windowPos.getHeight() > PopupMenuSettings::scrollZone * 4)
			{
				const int currentY = m->getY();

				if (wantedY > 0 || currentY < 0 || m->getBottom() > windowPos.getHeight())
				{
					if (wantedY < 0)
						wantedY = jlimit (PopupMenuSettings::scrollZone,
										  jmax (PopupMenuSettings::scrollZone,
												windowPos.getHeight() - (PopupMenuSettings::scrollZone + m->getHeight())),
										  currentY);

					const Rectangle<int> mon (Desktop::getInstance().getMonitorAreaContaining (windowPos.getPosition(), true));

					int deltaY = wantedY - currentY;

					windowPos.setSize (jmin (windowPos.getWidth(), mon.getWidth()),
									   jmin (windowPos.getHeight(), mon.getHeight()));

					const int newY = jlimit (mon.getY(),
											 mon.getBottom() - windowPos.getHeight(),
											 windowPos.getY() + deltaY);

					deltaY -= newY - windowPos.getY();

					childYOffset -= deltaY;
					windowPos.setPosition (windowPos.getX(), newY);

					updateYPositions();
				}

				break;
			}
		}
	}

	void resizeToBestWindowPos()
	{
		Rectangle<int> r (windowPos);

		if (childYOffset < 0)
		{
			r.setBounds (r.getX(), r.getY() - childYOffset,
						 r.getWidth(), r.getHeight() + childYOffset);
		}
		else if (childYOffset > 0)
		{
			const int spaceAtBottom = r.getHeight() - (contentHeight - childYOffset);

			if (spaceAtBottom > 0)
				r.setSize (r.getWidth(), r.getHeight() - spaceAtBottom);
		}

		setBounds (r);
		updateYPositions();
	}

	void alterChildYPos (const int delta)
	{
		if (isScrolling())
		{
			childYOffset += delta;

			if (delta < 0)
			{
				childYOffset = jmax (childYOffset, 0);
			}
			else if (delta > 0)
			{
				childYOffset = jmin (childYOffset,
									 contentHeight - windowPos.getHeight() + PopupMenuSettings::borderSize);
			}

			updateYPositions();
		}
		else
		{
			childYOffset = 0;
		}

		resizeToBestWindowPos();
		repaint();
	}

	int updateYPositions()
	{
		int x = 0;
		int childNum = 0;

		for (int col = 0; col < numColumns; ++col)
		{
			const int numChildren = jmin (getNumChildComponents() - childNum,
										  (getNumChildComponents() + numColumns - 1) / numColumns);

			const int colW = columnWidths [col];

			int y = PopupMenuSettings::borderSize - (childYOffset + (getY() - windowPos.getY()));

			for (int i = 0; i < numChildren; ++i)
			{
				Component* const c = getChildComponent (childNum + i);
				c->setBounds (x, y, colW, c->getHeight());
				y += c->getHeight();
			}

			x += colW;
			childNum += numChildren;
		}

		return x;
	}

	bool isScrolling() const throw()
	{
		return childYOffset != 0 || needsToScroll;
	}

	void setCurrentlyHighlightedChild (PopupMenu::ItemComponent* const child)
	{
		if (currentChild->isValidComponent())
			currentChild->setHighlighted (false);

		currentChild = child;

		if (currentChild != 0)
		{
			currentChild->setHighlighted (true);
			timeEnteredCurrentChildComp = Time::getApproximateMillisecondCounter();
		}
	}

	bool showSubMenuFor (PopupMenu::ItemComponent* const childComp)
	{
		jassert (activeSubMenu == 0 || activeSubMenu->isValidComponent());
		activeSubMenu = 0;

		if (childComp->isValidComponent() && childComp->itemInfo.hasActiveSubMenu())
		{
			const Point<int> topLeft (childComp->relativePositionToGlobal (Point<int>()));
			const Point<int> bottomRight (childComp->relativePositionToGlobal (Point<int> (childComp->getWidth(), childComp->getHeight())));

			activeSubMenu = Window::create (*(childComp->itemInfo.subMenu),
											dismissOnMouseUp,
											this,
											topLeft.getX(), bottomRight.getX(), topLeft.getY(), bottomRight.getY(),
											0, maximumNumColumns,
											standardItemHeight,
											false, 0, menuBarComponent,
											managerOfChosenCommand,
											componentAttachedTo);

			if (activeSubMenu != 0)
			{
				activeSubMenu->setVisible (true);
				activeSubMenu->enterModalState (false);
				activeSubMenu->toFront (false);
				return true;
			}
		}

		return false;
	}

	void highlightItemUnderMouse (const Point<int>& globalMousePos, const Point<int>& localMousePos)
	{
		isOver = reallyContains (localMousePos.getX(), localMousePos.getY(), true);

		if (isOver)
			hasBeenOver = true;

		if (lastMouse.getDistanceFrom (globalMousePos) > 2)
		{
			lastMouseMoveTime = Time::getApproximateMillisecondCounter();

			if (disableMouseMoves && isOver)
				disableMouseMoves = false;
		}

		if (disableMouseMoves || (activeSubMenu != 0 && activeSubMenu->isOverChildren()))
			return;

		bool isMovingTowardsMenu = false;

		jassert (activeSubMenu == 0 || activeSubMenu->isValidComponent())

		if (isOver && (activeSubMenu != 0) && globalMousePos != lastMouse)
		{
			// try to intelligently guess whether the user is moving the mouse towards a currently-open
			// submenu. To do this, look at whether the mouse stays inside a triangular region that
			// extends from the last mouse pos to the submenu's rectangle..

			float subX = (float) activeSubMenu->getScreenX();

			if (activeSubMenu->getX() > getX())
			{
				lastMouse -= Point<int> (2, 0);  // to enlarge the triangle a bit, in case the mouse only moves a couple of pixels
			}
			else
			{
				lastMouse += Point<int> (2, 0);
				subX += activeSubMenu->getWidth();
			}

			Path areaTowardsSubMenu;
			areaTowardsSubMenu.addTriangle ((float) lastMouse.getX(),
											(float) lastMouse.getY(),
											subX,
											(float) activeSubMenu->getScreenY(),
											subX,
											(float) (activeSubMenu->getScreenY() + activeSubMenu->getHeight()));

			isMovingTowardsMenu = areaTowardsSubMenu.contains ((float) globalMousePos.getX(), (float) globalMousePos.getY());
		}

		lastMouse = globalMousePos;

		if (! isMovingTowardsMenu)
		{
			Component* c = getComponentAt (localMousePos.getX(), localMousePos.getY());
			if (c == this)
				c = 0;

			PopupMenu::ItemComponent* mic = dynamic_cast <PopupMenu::ItemComponent*> (c);

			if (mic == 0 && c != 0)
				mic = c->findParentComponentOfClass ((PopupMenu::ItemComponent*) 0);

			if (mic != currentChild
				 && (isOver || (activeSubMenu == 0) || ! activeSubMenu->isVisible()))
			{
				if (isOver && (c != 0) && (activeSubMenu != 0))
				{
					activeSubMenu->hide (0);
				}

				if (! isOver)
					mic = 0;

				setCurrentlyHighlightedChild (mic);
			}
		}
	}

	void triggerCurrentlyHighlightedItem()
	{
		if (currentChild->isValidComponent()
			 && currentChild->itemInfo.canBeTriggered()
			 && (currentChild->itemInfo.customComp == 0
				  || currentChild->itemInfo.customComp->isTriggeredAutomatically))
		{
			dismissMenu (&currentChild->itemInfo);
		}
	}

	void selectNextItem (const int delta)
	{
		disableTimerUntilMouseMoves();
		PopupMenu::ItemComponent* mic = 0;
		bool wasLastOne = (currentChild == 0);
		const int numItems = getNumChildComponents();

		for (int i = 0; i < numItems + 1; ++i)
		{
			int index = (delta > 0) ? i : (numItems - 1 - i);
			index = (index + numItems) % numItems;

			mic = dynamic_cast <PopupMenu::ItemComponent*> (getChildComponent (index));

			if (mic != 0 && (mic->itemInfo.canBeTriggered() || mic->itemInfo.hasActiveSubMenu())
				 && wasLastOne)
				break;

			if (mic == currentChild)
				wasLastOne = true;
		}

		setCurrentlyHighlightedChild (mic);
	}

	void disableTimerUntilMouseMoves()
	{
		disableMouseMoves = true;

		if (owner != 0)
			owner->disableTimerUntilMouseMoves();
	}

	Window (const Window&);
	Window& operator= (const Window&);
};

PopupMenu::PopupMenu()
	: lookAndFeel (0),
	  separatorPending (false)
{
}

PopupMenu::PopupMenu (const PopupMenu& other)
	: lookAndFeel (other.lookAndFeel),
	  separatorPending (false)
{
	items.ensureStorageAllocated (other.items.size());

	for (int i = 0; i < other.items.size(); ++i)
		items.add (new Item (*other.items.getUnchecked(i)));
}

PopupMenu& PopupMenu::operator= (const PopupMenu& other)
{
	if (this != &other)
	{
		lookAndFeel = other.lookAndFeel;

		clear();
		items.ensureStorageAllocated (other.items.size());

		for (int i = 0; i < other.items.size(); ++i)
			items.add (new Item (*other.items.getUnchecked(i)));
	}

	return *this;
}

PopupMenu::~PopupMenu()
{
	clear();
}

void PopupMenu::clear()
{
	items.clear();
	separatorPending = false;
}

void PopupMenu::addSeparatorIfPending()
{
	if (separatorPending)
	{
		separatorPending = false;

		if (items.size() > 0)
			items.add (new Item());
	}
}

void PopupMenu::addItem (const int itemResultId,
						 const String& itemText,
						 const bool isActive,
						 const bool isTicked,
						 const Image* const iconToUse)
{
	jassert (itemResultId != 0);	// 0 is used as a return value to indicate that the user
									// didn't pick anything, so you shouldn't use it as the id
									// for an item..

	addSeparatorIfPending();

	items.add (new Item (itemResultId, itemText, isActive, isTicked,
						 iconToUse, Colours::black, false, 0, 0, 0));
}

void PopupMenu::addCommandItem (ApplicationCommandManager* commandManager,
								const int commandID,
								const String& displayName)
{
	jassert (commandManager != 0 && commandID != 0);

	const ApplicationCommandInfo* const registeredInfo = commandManager->getCommandForID (commandID);

	if (registeredInfo != 0)
	{
		ApplicationCommandInfo info (*registeredInfo);
		ApplicationCommandTarget* const target = commandManager->getTargetForCommand (commandID, info);

		addSeparatorIfPending();

		items.add (new Item (commandID,
							 displayName.isNotEmpty() ? displayName
													  : info.shortName,
							 target != 0 && (info.flags & ApplicationCommandInfo::isDisabled) == 0,
							 (info.flags & ApplicationCommandInfo::isTicked) != 0,
							 0,
							 Colours::black,
							 false,
							 0, 0,
							 commandManager));
	}
}

void PopupMenu::addColouredItem (const int itemResultId,
								 const String& itemText,
								 const Colour& itemTextColour,
								 const bool isActive,
								 const bool isTicked,
								 const Image* const iconToUse)
{
	jassert (itemResultId != 0);	// 0 is used as a return value to indicate that the user
									// didn't pick anything, so you shouldn't use it as the id
									// for an item..

	addSeparatorIfPending();

	items.add (new Item (itemResultId, itemText, isActive, isTicked,
						 iconToUse, itemTextColour, true, 0, 0, 0));
}

void PopupMenu::addCustomItem (const int itemResultId,
							   PopupMenuCustomComponent* const customComponent)
{
	jassert (itemResultId != 0);	// 0 is used as a return value to indicate that the user
									// didn't pick anything, so you shouldn't use it as the id
									// for an item..

	addSeparatorIfPending();

	items.add (new Item (itemResultId, String::empty, true, false, 0,
						 Colours::black, false, customComponent, 0, 0));
}

class NormalComponentWrapper : public PopupMenuCustomComponent
{
public:
	NormalComponentWrapper (Component* const comp,
							const int w, const int h,
							const bool triggerMenuItemAutomaticallyWhenClicked)
		: PopupMenuCustomComponent (triggerMenuItemAutomaticallyWhenClicked),
		  width (w),
		  height (h)
	{
		addAndMakeVisible (comp);
	}

	~NormalComponentWrapper() {}

	void getIdealSize (int& idealWidth, int& idealHeight)
	{
		idealWidth = width;
		idealHeight = height;
	}

	void resized()
	{
		if (getChildComponent(0) != 0)
			getChildComponent(0)->setBounds (0, 0, getWidth(), getHeight());
	}

	juce_UseDebuggingNewOperator

private:
	const int width, height;

	NormalComponentWrapper (const NormalComponentWrapper&);
	NormalComponentWrapper& operator= (const NormalComponentWrapper&);
};

void PopupMenu::addCustomItem (const int itemResultId,
							   Component* customComponent,
							   int idealWidth, int idealHeight,
							   const bool triggerMenuItemAutomaticallyWhenClicked)
{
	addCustomItem (itemResultId,
				   new NormalComponentWrapper (customComponent,
											   idealWidth, idealHeight,
											   triggerMenuItemAutomaticallyWhenClicked));
}

void PopupMenu::addSubMenu (const String& subMenuName,
							const PopupMenu& subMenu,
							const bool isActive,
							Image* const iconToUse,
							const bool isTicked)
{
	addSeparatorIfPending();

	items.add (new Item (0, subMenuName, isActive && (subMenu.getNumItems() > 0), isTicked,
						 iconToUse, Colours::black, false, 0, &subMenu, 0));
}

void PopupMenu::addSeparator()
{
	separatorPending = true;
}

class HeaderItemComponent  : public PopupMenuCustomComponent
{
public:
	HeaderItemComponent (const String& name)
		: PopupMenuCustomComponent (false)
	{
		setName (name);
	}

	~HeaderItemComponent()
	{
	}

	void paint (Graphics& g)
	{
		Font f (getLookAndFeel().getPopupMenuFont());
		f.setBold (true);
		g.setFont (f);
		g.setColour (findColour (PopupMenu::headerTextColourId));

		g.drawFittedText (getName(),
						  12, 0, getWidth() - 16, proportionOfHeight (0.8f),
						  Justification::bottomLeft, 1);
	}

	void getIdealSize (int& idealWidth,
					   int& idealHeight)
	{
		getLookAndFeel().getIdealPopupMenuItemSize (getName(), false, -1, idealWidth, idealHeight);
		idealHeight += idealHeight / 2;
		idealWidth += idealWidth / 4;
	}

	juce_UseDebuggingNewOperator
};

void PopupMenu::addSectionHeader (const String& title)
{
	addCustomItem (0X4734a34f, new HeaderItemComponent (title));
}

Component* PopupMenu::createMenuComponent (const int x, const int y, const int w, const int h,
										   const int itemIdThatMustBeVisible,
										   const int minimumWidth,
										   const int maximumNumColumns,
										   const int standardItemHeight,
										   const bool alignToRectangle,
										   Component* menuBarComponent,
										   ApplicationCommandManager** managerOfChosenCommand,
										   Component* const componentAttachedTo)
{
	Window* const pw
		= Window::create (*this,
						  ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown(),
						  0,
						  x, x + w,
						  y, y + h,
						  minimumWidth,
						  maximumNumColumns,
						  standardItemHeight,
						  alignToRectangle,
						  itemIdThatMustBeVisible,
						  menuBarComponent,
						  managerOfChosenCommand,
						  componentAttachedTo);

	if (pw != 0)
		pw->setVisible (true);

	return pw;
}

int PopupMenu::showMenu (const int x, const int y, const int w, const int h,
						 const int itemIdThatMustBeVisible,
						 const int minimumWidth,
						 const int maximumNumColumns,
						 const int standardItemHeight,
						 const bool alignToRectangle,
						 Component* const componentAttachedTo)
{
	Component::SafePointer<Component> prevFocused (Component::getCurrentlyFocusedComponent());
	Component::SafePointer<Component> prevTopLevel ((prevFocused != 0) ? prevFocused->getTopLevelComponent() : 0);

	Window::wasHiddenBecauseOfAppChange() = false;

	int result = 0;
	ApplicationCommandManager* managerOfChosenCommand = 0;

	ScopedPointer <Component> popupComp (createMenuComponent (x, y, w, h,
															  itemIdThatMustBeVisible,
															  minimumWidth,
															  maximumNumColumns > 0 ? maximumNumColumns : 7,
															  standardItemHeight,
															  alignToRectangle, 0,
															  &managerOfChosenCommand,
															  componentAttachedTo));

	if (popupComp != 0)
	{
		popupComp->enterModalState (false);
		popupComp->toFront (false);  // need to do this after making it modal, or it could
									 // be stuck behind other comps that are already modal..

		result = popupComp->runModalLoop();
		popupComp = 0;

		if (! Window::wasHiddenBecauseOfAppChange())
		{
			if (prevTopLevel != 0)
				prevTopLevel->toFront (true);

			if (prevFocused != 0)
				prevFocused->grabKeyboardFocus();
		}
	}

	if (managerOfChosenCommand != 0 && result != 0)
	{
		ApplicationCommandTarget::InvocationInfo info (result);
		info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromMenu;

		managerOfChosenCommand->invoke (info, true);
	}

	return result;
}

int PopupMenu::show (const int itemIdThatMustBeVisible,
					 const int minimumWidth,
					 const int maximumNumColumns,
					 const int standardItemHeight)
{
	const Point<int> mousePos (Desktop::getMousePosition());

	return showAt (mousePos.getX(), mousePos.getY(),
				   itemIdThatMustBeVisible,
				   minimumWidth,
				   maximumNumColumns,
				   standardItemHeight);
}

int PopupMenu::showAt (const int screenX,
					   const int screenY,
					   const int itemIdThatMustBeVisible,
					   const int minimumWidth,
					   const int maximumNumColumns,
					   const int standardItemHeight)
{
	return showMenu (screenX, screenY, 1, 1,
					 itemIdThatMustBeVisible,
					 minimumWidth, maximumNumColumns,
					 standardItemHeight,
					 false, 0);
}

int PopupMenu::showAt (Component* componentToAttachTo,
					   const int itemIdThatMustBeVisible,
					   const int minimumWidth,
					   const int maximumNumColumns,
					   const int standardItemHeight)
{
	if (componentToAttachTo != 0)
	{
		return showMenu (componentToAttachTo->getScreenX(),
						 componentToAttachTo->getScreenY(),
						 componentToAttachTo->getWidth(),
						 componentToAttachTo->getHeight(),
						 itemIdThatMustBeVisible,
						 minimumWidth,
						 maximumNumColumns,
						 standardItemHeight,
						 true, componentToAttachTo);
	}
	else
	{
		return show (itemIdThatMustBeVisible,
					 minimumWidth,
					 maximumNumColumns,
					 standardItemHeight);
	}
}

void JUCE_CALLTYPE PopupMenu::dismissAllActiveMenus()
{
	for (int i = Window::getActiveWindows().size(); --i >= 0;)
	{
		Window* const pmw = Window::getActiveWindows()[i];

		if (pmw != 0)
			pmw->dismissMenu (0);
	}
}

int PopupMenu::getNumItems() const throw()
{
	int num = 0;

	for (int i = items.size(); --i >= 0;)
		if (! (items.getUnchecked(i))->isSeparator)
			++num;

	return num;
}

bool PopupMenu::containsCommandItem (const int commandID) const
{
	for (int i = items.size(); --i >= 0;)
	{
		const Item* mi = items.getUnchecked (i);

		if ((mi->itemId == commandID && mi->commandManager != 0)
			|| (mi->subMenu != 0 && mi->subMenu->containsCommandItem (commandID)))
		{
			return true;
		}
	}

	return false;
}

bool PopupMenu::containsAnyActiveItems() const throw()
{
	for (int i = items.size(); --i >= 0;)
	{
		const Item* const mi = items.getUnchecked (i);

		if (mi->subMenu != 0)
		{
			if (mi->subMenu->containsAnyActiveItems())
				return true;
		}
		else if (mi->active)
		{
			return true;
		}
	}

	return false;
}

void PopupMenu::setLookAndFeel (LookAndFeel* const newLookAndFeel)
{
	lookAndFeel = newLookAndFeel;
}

PopupMenuCustomComponent::PopupMenuCustomComponent (const bool isTriggeredAutomatically_)
	: isHighlighted (false),
	  isTriggeredAutomatically (isTriggeredAutomatically_)
{
}

PopupMenuCustomComponent::~PopupMenuCustomComponent()
{
}

void PopupMenuCustomComponent::triggerMenuItem()
{
	PopupMenu::ItemComponent* const mic = dynamic_cast <PopupMenu::ItemComponent*> (getParentComponent());

	if (mic != 0)
	{
		PopupMenu::Window* const pmw = dynamic_cast <PopupMenu::Window*> (mic->getParentComponent());

		if (pmw != 0)
		{
			pmw->dismissMenu (&mic->itemInfo);
		}
		else
		{
			// something must have gone wrong with the component hierarchy if this happens..
			jassertfalse
		}
	}
	else
	{
		// why isn't this component inside a menu? Not much point triggering the item if
		// there's no menu.
		jassertfalse
	}
}

PopupMenu::MenuItemIterator::MenuItemIterator (const PopupMenu& menu_)
	: subMenu (0),
	  itemId (0),
	  isSeparator (false),
	  isTicked (false),
	  isEnabled (false),
	  isCustomComponent (false),
	  isSectionHeader (false),
	  customColour (0),
	  customImage (0),
	  menu (menu_),
	  index (0)
{
}

PopupMenu::MenuItemIterator::~MenuItemIterator()
{
}

bool PopupMenu::MenuItemIterator::next()
{
	if (index >= menu.items.size())
		return false;

	const Item* const item = menu.items.getUnchecked (index);
	++index;

	itemName = item->customComp != 0 ? item->customComp->getName() : item->text;
	subMenu = item->subMenu;
	itemId = item->itemId;

	isSeparator = item->isSeparator;
	isTicked = item->isTicked;
	isEnabled = item->active;
	isSectionHeader = dynamic_cast <HeaderItemComponent*> ((PopupMenuCustomComponent*) item->customComp) != 0;
	isCustomComponent = (! isSectionHeader) && item->customComp != 0;
	customColour = item->usesColour ? &(item->textColour) : 0;
	customImage = item->image;
	commandManager = item->commandManager;

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PopupMenu.cpp ***/


/*** Start of inlined file: juce_ComponentDragger.cpp ***/
BEGIN_JUCE_NAMESPACE

ComponentDragger::ComponentDragger()
	: constrainer (0)
{
}

ComponentDragger::~ComponentDragger()
{
}

void ComponentDragger::startDraggingComponent (Component* const componentToDrag,
											   ComponentBoundsConstrainer* const constrainer_)
{
	jassert (componentToDrag->isValidComponent());

	if (componentToDrag != 0)
	{
		constrainer = constrainer_;
		originalPos = componentToDrag->relativePositionToGlobal (Point<int>());
	}
}

void ComponentDragger::dragComponent (Component* const componentToDrag, const MouseEvent& e)
{
	jassert (componentToDrag->isValidComponent());
	jassert (e.mods.isAnyMouseButtonDown()); // (the event has to be a drag event..)

	if (componentToDrag != 0)
	{
		Rectangle<int> bounds (componentToDrag->getBounds().withPosition (originalPos));

		const Component* const parentComp = componentToDrag->getParentComponent();
		if (parentComp != 0)
			bounds.setPosition (parentComp->globalPositionToRelative (originalPos));

		bounds.setPosition (bounds.getPosition() + e.getOffsetFromDragStart());

		if (constrainer != 0)
			constrainer->setBoundsForComponent (componentToDrag, bounds, false, false, false, false);
		else
			componentToDrag->setBounds (bounds);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ComponentDragger.cpp ***/


/*** Start of inlined file: juce_DragAndDropContainer.cpp ***/
BEGIN_JUCE_NAMESPACE

bool juce_performDragDropFiles (const StringArray& files, const bool copyFiles, bool& shouldStop);
bool juce_performDragDropText (const String& text, bool& shouldStop);

class DragImageComponent  : public Component,
							public Timer
{
public:
	DragImageComponent (Image* const im,
						const String& desc,
						Component* const sourceComponent,
						Component* const mouseDragSource_,
						DragAndDropContainer* const o,
						const Point<int>& imageOffset_)
		: image (im),
		  source (sourceComponent),
		  mouseDragSource (mouseDragSource_),
		  owner (o),
		  dragDesc (desc),
		  imageOffset (imageOffset_),
		  hasCheckedForExternalDrag (false),
		  drawImage (true)
	{
		setSize (im->getWidth(), im->getHeight());

		if (mouseDragSource == 0)
			mouseDragSource = source;

		mouseDragSource->addMouseListener (this, false);

		startTimer (200);

		setInterceptsMouseClicks (false, false);
		setAlwaysOnTop (true);
	}

	~DragImageComponent()
	{
		if (owner->dragImageComponent == this)
			owner->dragImageComponent.release();

		if (mouseDragSource != 0)
		{
			mouseDragSource->removeMouseListener (this);

			if (getCurrentlyOver() != 0 && getCurrentlyOver()->isInterestedInDragSource (dragDesc, source))
				getCurrentlyOver()->itemDragExit (dragDesc, source);
		}
	}

	void paint (Graphics& g)
	{
		if (isOpaque())
			g.fillAll (Colours::white);

		if (drawImage)
		{
			g.setOpacity (1.0f);
			g.drawImageAt (image, 0, 0);
		}
	}

	DragAndDropTarget* findTarget (const Point<int>& screenPos, Point<int>& relativePos)
	{
		Component* hit = getParentComponent();

		if (hit == 0)
		{
			hit = Desktop::getInstance().findComponentAt (screenPos);
		}
		else
		{
			const Point<int> relPos (hit->globalPositionToRelative (screenPos));
			hit = hit->getComponentAt (relPos.getX(), relPos.getY());
		}

		// (note: use a local copy of the dragDesc member in case the callback runs
		// a modal loop and deletes this object before the method completes)
		const String dragDescLocal (dragDesc);

		while (hit != 0)
		{
			DragAndDropTarget* const ddt = dynamic_cast <DragAndDropTarget*> (hit);

			if (ddt != 0 && ddt->isInterestedInDragSource (dragDescLocal, source))
			{
				relativePos = hit->globalPositionToRelative (screenPos);
				return ddt;
			}

			hit = hit->getParentComponent();
		}

		return 0;
	}

	void mouseUp (const MouseEvent& e)
	{
		if (e.originalComponent != this)
		{
			if (mouseDragSource != 0)
				mouseDragSource->removeMouseListener (this);

			bool dropAccepted = false;
			DragAndDropTarget* ddt = 0;
			Point<int> relPos;

			if (isVisible())
			{
				setVisible (false);
				ddt = findTarget (e.getScreenPosition(), relPos);

				// fade this component and remove it - it'll be deleted later by the timer callback

				dropAccepted = ddt != 0;

				setVisible (true);

				if (dropAccepted || source == 0)
				{
					fadeOutComponent (120);
				}
				else
				{
					const Point<int> target (source->relativePositionToGlobal (Point<int> (source->getWidth() / 2,
																						   source->getHeight() / 2)));

					const Point<int> ourCentre (relativePositionToGlobal (Point<int> (getWidth() / 2,
																					  getHeight() / 2)));

					fadeOutComponent (120,
									  target.getX() - ourCentre.getX(),
									  target.getY() - ourCentre.getY());
				}
			}

			if (getParentComponent() != 0)
				getParentComponent()->removeChildComponent (this);

			if (dropAccepted && ddt != 0)
			{
				// (note: use a local copy of the dragDesc member in case the callback runs
				// a modal loop and deletes this object before the method completes)
				const String dragDescLocal (dragDesc);

				currentlyOverComp = 0;

				ddt->itemDropped (dragDescLocal, source, relPos.getX(), relPos.getY());
			}

			// careful - this object could now be deleted..
		}
	}

	void updateLocation (const bool canDoExternalDrag, const Point<int>& screenPos)
	{
		// (note: use a local copy of the dragDesc member in case the callback runs
		// a modal loop and deletes this object before it returns)
		const String dragDescLocal (dragDesc);

		Point<int> newPos (screenPos + imageOffset);

		if (getParentComponent() != 0)
			newPos = getParentComponent()->globalPositionToRelative (newPos);

		//if (newX != getX() || newY != getY())
		{
			setTopLeftPosition (newPos.getX(), newPos.getY());

			Point<int> relPos;
			DragAndDropTarget* const ddt = findTarget (screenPos, relPos);
			Component* ddtComp = dynamic_cast <Component*> (ddt);

			drawImage = (ddt == 0) || ddt->shouldDrawDragImageWhenOver();

			if (ddtComp != currentlyOverComp)
			{
				if (currentlyOverComp != 0 && source != 0
					  && getCurrentlyOver()->isInterestedInDragSource (dragDescLocal, source))
				{
					getCurrentlyOver()->itemDragExit (dragDescLocal, source);
				}

				currentlyOverComp = ddtComp;

				if (ddt != 0 && ddt->isInterestedInDragSource (dragDescLocal, source))
					ddt->itemDragEnter (dragDescLocal, source, relPos.getX(), relPos.getY());
			}

			if (getCurrentlyOver() != 0 && getCurrentlyOver()->isInterestedInDragSource (dragDescLocal, source))
				getCurrentlyOver()->itemDragMove (dragDescLocal, source, relPos.getX(), relPos.getY());

			if (getCurrentlyOver() == 0
				 && canDoExternalDrag
				 && ! hasCheckedForExternalDrag)
			{
				if (Desktop::getInstance().findComponentAt (screenPos) == 0)
				{
					hasCheckedForExternalDrag = true;
					StringArray files;
					bool canMoveFiles = false;

					if (owner->shouldDropFilesWhenDraggedExternally (dragDescLocal, source, files, canMoveFiles)
						 && files.size() > 0)
					{
						Component::SafePointer<Component> cdw (this);
						setVisible (false);

						if (ModifierKeys::getCurrentModifiersRealtime().isAnyMouseButtonDown())
							DragAndDropContainer::performExternalDragDropOfFiles (files, canMoveFiles);

						if (cdw != 0)
							delete this;

						return;
					}
				}
			}
		}
	}

	void mouseDrag (const MouseEvent& e)
	{
		if (e.originalComponent != this)
			updateLocation (true, e.getScreenPosition());
	}

	void timerCallback()
	{
		if (source == 0)
		{
			delete this;
		}
		else if (! isMouseButtonDownAnywhere())
		{
			if (mouseDragSource != 0)
				mouseDragSource->removeMouseListener (this);

			delete this;
		}
	}

private:
	ScopedPointer<Image> image;
	Component::SafePointer<Component> source;
	Component::SafePointer<Component> mouseDragSource;
	DragAndDropContainer* const owner;

	Component::SafePointer<Component> currentlyOverComp;
	DragAndDropTarget* getCurrentlyOver()
	{
		return dynamic_cast <DragAndDropTarget*> (static_cast <Component*> (currentlyOverComp));
	}

	String dragDesc;
	const Point<int> imageOffset;
	bool hasCheckedForExternalDrag, drawImage;

	DragImageComponent (const DragImageComponent&);
	DragImageComponent& operator= (const DragImageComponent&);
};

DragAndDropContainer::DragAndDropContainer()
{
}

DragAndDropContainer::~DragAndDropContainer()
{
	dragImageComponent = 0;
}

void DragAndDropContainer::startDragging (const String& sourceDescription,
										  Component* sourceComponent,
										  Image* dragImage_,
										  const bool allowDraggingToExternalWindows,
										  const Point<int>* imageOffsetFromMouse)
{
	ScopedPointer <Image> dragImage (dragImage_);

	if (dragImageComponent == 0)
	{
		Component* const thisComp = dynamic_cast <Component*> (this);

		if (thisComp == 0)
		{
			jassertfalse;   // Your DragAndDropContainer needs to be a Component!
			return;
		}

		MouseInputSource* draggingSource = Desktop::getInstance().getDraggingMouseSource (0);

		if (draggingSource == 0 || ! draggingSource->isDragging())
		{
			jassertfalse;   // You must call startDragging() from within a mouseDown or mouseDrag callback!
			return;
		}

		const Point<int> lastMouseDown (Desktop::getLastMouseDownPosition());
		Point<int> imageOffset;

		if (dragImage == 0)
		{
			dragImage = sourceComponent->createComponentSnapshot (sourceComponent->getLocalBounds());

			if (dragImage->getFormat() != Image::ARGB)
			{
				Image* newIm = Image::createNativeImage (Image::ARGB, dragImage->getWidth(), dragImage->getHeight(), true);
				Graphics g2 (*newIm);
				g2.drawImageAt (dragImage, 0, 0);

				dragImage = newIm;
			}

			dragImage->multiplyAllAlphas (0.6f);

			const int lo = 150;
			const int hi = 400;

			Point<int> relPos (sourceComponent->globalPositionToRelative (lastMouseDown));
			Point<int> clipped (dragImage->getBounds().getConstrainedPoint (relPos));

			for (int y = dragImage->getHeight(); --y >= 0;)
			{
				const double dy = (y - clipped.getY()) * (y - clipped.getY());

				for (int x = dragImage->getWidth(); --x >= 0;)
				{
					const int dx = x - clipped.getX();
					const int distance = roundToInt (sqrt (dx * dx + dy));

					if (distance > lo)
					{
						const float alpha = (distance > hi) ? 0
															: (hi - distance) / (float) (hi - lo)
															   + Random::getSystemRandom().nextFloat() * 0.008f;

						dragImage->multiplyAlphaAt (x, y, alpha);
					}
				}
			}

			imageOffset = -clipped;
		}
		else
		{
			if (imageOffsetFromMouse == 0)
				imageOffset = Point<int> (dragImage->getWidth() / -2,
										  dragImage->getHeight() / -2);
			else
				imageOffset = *imageOffsetFromMouse;
		}

		dragImageComponent = new DragImageComponent (dragImage.release(), sourceDescription, sourceComponent,
													 draggingSource->getComponentUnderMouse(), this, imageOffset);

		currentDragDesc = sourceDescription;

		if (allowDraggingToExternalWindows)
		{
			if (! Desktop::canUseSemiTransparentWindows())
				dragImageComponent->setOpaque (true);

			dragImageComponent->addToDesktop (ComponentPeer::windowIgnoresMouseClicks
											   | ComponentPeer::windowIsTemporary
											   | ComponentPeer::windowIgnoresKeyPresses);
		}
		else
			thisComp->addChildComponent (dragImageComponent);

		static_cast <DragImageComponent*> (static_cast <Component*> (dragImageComponent))->updateLocation (false, lastMouseDown);
		dragImageComponent->setVisible (true);
	}
}

bool DragAndDropContainer::isDragAndDropActive() const
{
	return dragImageComponent != 0;
}

const String DragAndDropContainer::getCurrentDragDescription() const
{
	return (dragImageComponent != 0) ? currentDragDesc
									 : String::empty;
}

DragAndDropContainer* DragAndDropContainer::findParentDragContainerFor (Component* c)
{
	if (c == 0)
		return 0;

	// (unable to use the syntax findParentComponentOfClass <DragAndDropContainer> () because of a VC6 compiler bug)
	return c->findParentComponentOfClass ((DragAndDropContainer*) 0);
}

bool DragAndDropContainer::shouldDropFilesWhenDraggedExternally (const String&, Component*, StringArray&, bool&)
{
	return false;
}

void DragAndDropTarget::itemDragEnter (const String&, Component*, int, int)
{
}

void DragAndDropTarget::itemDragMove (const String&, Component*, int, int)
{
}

void DragAndDropTarget::itemDragExit (const String&, Component*)
{
}

bool DragAndDropTarget::shouldDrawDragImageWhenOver()
{
	return true;
}

void FileDragAndDropTarget::fileDragEnter (const StringArray&, int, int)
{
}

void FileDragAndDropTarget::fileDragMove (const StringArray&, int, int)
{
}

void FileDragAndDropTarget::fileDragExit (const StringArray&)
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DragAndDropContainer.cpp ***/


/*** Start of inlined file: juce_MouseCursor.cpp ***/
BEGIN_JUCE_NAMESPACE

void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY);
void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type);
void juce_deleteMouseCursor (void* const cursorHandle, const bool isStandard);

class MouseCursor::SharedCursorHandle
{
public:
	explicit SharedCursorHandle (const MouseCursor::StandardCursorType type)
		: handle (juce_createStandardMouseCursor (type)),
		  refCount (1),
		  standardType (type),
		  isStandard (true)
	{
	}

	SharedCursorHandle (const Image& image, const int hotSpotX, const int hotSpotY)
		: handle (juce_createMouseCursorFromImage (image, hotSpotX, hotSpotY)),
		  refCount (1),
		  standardType (MouseCursor::NormalCursor),
		  isStandard (false)
	{
	}

	static SharedCursorHandle* createStandard (const MouseCursor::StandardCursorType type)
	{
		const ScopedLock sl (getLock());

		for (int i = getCursors().size(); --i >= 0;)
		{
			SharedCursorHandle* const sc = getCursors().getUnchecked(i);

			if (sc->standardType == type)
				return sc->retain();
		}

		SharedCursorHandle* const sc = new SharedCursorHandle (type);
		getCursors().add (sc);
		return sc;
	}

	SharedCursorHandle* retain() throw()
	{
		Atomic::increment (refCount);
		return this;
	}

	void release()
	{
		if (Atomic::decrementAndReturn (refCount) == 0)
		{
			if (isStandard)
			{
				const ScopedLock sl (getLock());
				getCursors().removeValue (this);
			}

			delete this;
		}
	}

	void* getHandle() const throw()	 { return handle; }

	juce_UseDebuggingNewOperator

private:
	void* const handle;
	int32 refCount;
	const MouseCursor::StandardCursorType standardType;
	const bool isStandard;

	static CriticalSection& getLock()
	{
		static CriticalSection lock;
		return lock;
	}

	static Array <SharedCursorHandle*>& getCursors()
	{
		static Array <SharedCursorHandle*> cursors;
		return cursors;
	}

	~SharedCursorHandle()
	{
		juce_deleteMouseCursor (handle, isStandard);
	}

	SharedCursorHandle& operator= (const SharedCursorHandle&);
};

MouseCursor::MouseCursor()
	: cursorHandle (SharedCursorHandle::createStandard (NormalCursor))
{
	jassert (cursorHandle != 0);
}

MouseCursor::MouseCursor (const StandardCursorType type)
	: cursorHandle (SharedCursorHandle::createStandard (type))
{
	jassert (cursorHandle != 0);
}

MouseCursor::MouseCursor (const Image& image, const int hotSpotX, const int hotSpotY)
	: cursorHandle (new SharedCursorHandle (image, hotSpotX, hotSpotY))
{
}

MouseCursor::MouseCursor (const MouseCursor& other)
	: cursorHandle (other.cursorHandle->retain())
{
}

MouseCursor::~MouseCursor()
{
	cursorHandle->release();
}

MouseCursor& MouseCursor::operator= (const MouseCursor& other)
{
	other.cursorHandle->retain();
	cursorHandle->release();
	cursorHandle = other.cursorHandle;
	return *this;
}

bool MouseCursor::operator== (const MouseCursor& other) const throw()
{
	return getHandle() == other.getHandle();
}

bool MouseCursor::operator!= (const MouseCursor& other) const throw()
{
	return getHandle() != other.getHandle();
}

void* MouseCursor::getHandle() const throw()
{
	return cursorHandle->getHandle();
}

void MouseCursor::showWaitCursor() throw()
{
	Desktop::getInstance().getMainMouseSource().showMouseCursor (MouseCursor::WaitCursor);
}

void MouseCursor::hideWaitCursor() throw()
{
	Desktop::getInstance().getMainMouseSource().revealCursor();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MouseCursor.cpp ***/


/*** Start of inlined file: juce_MouseEvent.cpp ***/
BEGIN_JUCE_NAMESPACE

MouseEvent::MouseEvent (MouseInputSource& source_,
						const Point<int>& position,
						const ModifierKeys& mods_,
						Component* const originator,
						const Time& eventTime_,
						const Point<int> mouseDownPos_,
						const Time& mouseDownTime_,
						const int numberOfClicks_,
						const bool mouseWasDragged) throw()
	: x (position.getX()),
	  y (position.getY()),
	  mods (mods_),
	  eventComponent (originator),
	  originalComponent (originator),
	  eventTime (eventTime_),
	  source (source_),
	  mouseDownPos (mouseDownPos_),
	  mouseDownTime (mouseDownTime_),
	  numberOfClicks (numberOfClicks_),
	  wasMovedSinceMouseDown (mouseWasDragged)
{
}

MouseEvent::~MouseEvent() throw()
{
}

const MouseEvent MouseEvent::getEventRelativeTo (Component* const otherComponent) const throw()
{
	if (otherComponent == 0)
	{
		jassertfalse
		return *this;
	}

	return MouseEvent (source, eventComponent->relativePositionToOtherComponent (otherComponent, getPosition()),
					   mods, originalComponent, eventTime,
					   eventComponent->relativePositionToOtherComponent (otherComponent, mouseDownPos),
					   mouseDownTime, numberOfClicks, wasMovedSinceMouseDown);
}

const MouseEvent MouseEvent::withNewPosition (const Point<int>& newPosition) const throw()
{
	return MouseEvent (source, newPosition, mods, originalComponent,
					   eventTime, mouseDownPos, mouseDownTime,
					   numberOfClicks, wasMovedSinceMouseDown);
}

bool MouseEvent::mouseWasClicked() const throw()
{
	return ! wasMovedSinceMouseDown;
}

int MouseEvent::getMouseDownX() const throw()
{
	return mouseDownPos.getX();
}

int MouseEvent::getMouseDownY() const throw()
{
	return mouseDownPos.getY();
}

const Point<int> MouseEvent::getMouseDownPosition() const throw()
{
	return mouseDownPos;
}

int MouseEvent::getDistanceFromDragStartX() const throw()
{
	return x - mouseDownPos.getX();
}

int MouseEvent::getDistanceFromDragStartY() const throw()
{
	return y - mouseDownPos.getY();
}

int MouseEvent::getDistanceFromDragStart() const throw()
{
	return mouseDownPos.getDistanceFrom (getPosition());
}

const Point<int> MouseEvent::getOffsetFromDragStart() const throw()
{
	return getPosition() - mouseDownPos;
}

int MouseEvent::getLengthOfMousePress() const throw()
{
	if (mouseDownTime.toMilliseconds() > 0)
		return jmax (0, (int) (eventTime - mouseDownTime).inMilliseconds());

	return 0;
}

const Point<int> MouseEvent::getPosition() const throw()
{
	return Point<int> (x, y);
}

int MouseEvent::getScreenX() const
{
	return getScreenPosition().getX();
}

int MouseEvent::getScreenY() const
{
	return getScreenPosition().getY();
}

const Point<int> MouseEvent::getScreenPosition() const
{
	return eventComponent->relativePositionToGlobal (Point<int> (x, y));
}

int MouseEvent::getMouseDownScreenX() const
{
	return getMouseDownScreenPosition().getX();
}

int MouseEvent::getMouseDownScreenY() const
{
	return getMouseDownScreenPosition().getY();
}

const Point<int> MouseEvent::getMouseDownScreenPosition() const
{
	return eventComponent->relativePositionToGlobal (mouseDownPos);
}

static int doubleClickTimeOutMs = 400;

void MouseEvent::setDoubleClickTimeout (const int newTime) throw()
{
	doubleClickTimeOutMs = newTime;
}

int MouseEvent::getDoubleClickTimeout() throw()
{
	return doubleClickTimeOutMs;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MouseEvent.cpp ***/


/*** Start of inlined file: juce_MouseInputSource.cpp ***/
BEGIN_JUCE_NAMESPACE

class MouseInputSourceInternal   : public AsyncUpdater
{
public:

	MouseInputSourceInternal (MouseInputSource& source_, const int index_, const bool isMouseDevice_)
		: index (index_), isMouseDevice (isMouseDevice_), source (source_), lastPeer (0), lastTime (0),
		  isUnboundedMouseModeOn (false), isCursorVisibleUntilOffscreen (false), currentCursorHandle (0)
	{
		zerostruct (mouseDowns);
	}

	~MouseInputSourceInternal()
	{
	}

	bool isDragging() const throw()
	{
		return buttonState.isAnyMouseButtonDown();
	}

	Component* getComponentUnderMouse() const
	{
		return static_cast <Component*> (componentUnderMouse);
	}

	const ModifierKeys getCurrentModifiers() const
	{
		return ModifierKeys::getCurrentModifiers().withoutMouseButtons().withFlags (buttonState.getRawFlags());
	}

	ComponentPeer* getPeer()
	{
		if (! ComponentPeer::isValidPeer (lastPeer))
			lastPeer = 0;

		return lastPeer;
	}

	Component* findComponentAt (const Point<int>& screenPos)
	{
		ComponentPeer* const peer = getPeer();

		if (peer != 0)
		{
			Component* const comp = peer->getComponent();
			const Point<int> relativePos (comp->globalPositionToRelative (screenPos));

			// (the contains() call is needed to test for overlapping desktop windows)
			if (comp->contains (relativePos.getX(), relativePos.getY()))
				return comp->getComponentAt (relativePos);
		}

		return 0;
	}

	const Point<int> getScreenPosition() const throw()
	{
		return lastScreenPos + unboundedMouseOffset;
	}

	void sendMouseEnter (Component* const comp, const Point<int>& screenPos, const int64 time)
	{
		//DBG ("Mouse " + String (source.getIndex()) + " enter: " + comp->globalPositionToRelative (screenPos).toString() + " - Comp: " + String::toHexString ((int) comp));
		comp->internalMouseEnter (source, comp->globalPositionToRelative (screenPos), time);
	}

	void sendMouseExit (Component* const comp, const Point<int>& screenPos, const int64 time)
	{
		//DBG ("Mouse " + String (source.getIndex()) + " exit: " + comp->globalPositionToRelative (screenPos).toString() + " - Comp: " + String::toHexString ((int) comp));
		comp->internalMouseExit (source, comp->globalPositionToRelative (screenPos), time);
	}

	void sendMouseMove (Component* const comp, const Point<int>& screenPos, const int64 time)
	{
		//DBG ("Mouse " + String (source.getIndex()) + " move: " + comp->globalPositionToRelative (screenPos).toString() + " - Comp: " + String::toHexString ((int) comp));
		comp->internalMouseMove (source, comp->globalPositionToRelative (screenPos), time);
	}

	void sendMouseDown (Component* const comp, const Point<int>& screenPos, const int64 time)
	{
		//DBG ("Mouse " + String (source.getIndex()) + " down: " + comp->globalPositionToRelative (screenPos).toString() + " - Comp: " + String::toHexString ((int) comp));
		comp->internalMouseDown (source, comp->globalPositionToRelative (screenPos), time);
	}

	void sendMouseDrag (Component* const comp, const Point<int>& screenPos, const int64 time)
	{
		//DBG ("Mouse " + String (source.getIndex()) + " drag: " + comp->globalPositionToRelative (screenPos).toString() + " - Comp: " + String::toHexString ((int) comp));
		comp->internalMouseDrag (source, comp->globalPositionToRelative (screenPos), time);
	}

	void sendMouseUp (Component* const comp, const Point<int>& screenPos, const int64 time)
	{
		//DBG ("Mouse " + String (source.getIndex()) + " up: " + comp->globalPositionToRelative (screenPos).toString() + " - Comp: " + String::toHexString ((int) comp));
		comp->internalMouseUp (source, comp->globalPositionToRelative (screenPos), time, getCurrentModifiers());
	}

	void sendMouseWheel (Component* const comp, const Point<int>& screenPos, const int64 time, float x, float y)
	{
		//DBG ("Mouse " + String (source.getIndex()) + " wheel: " + comp->globalPositionToRelative (screenPos).toString() + " - Comp: " + String::toHexString ((int) comp));
		comp->internalMouseWheel (source, comp->globalPositionToRelative (screenPos), time, x, y);
	}

	void setButtons (const Point<int>& screenPos, const int64 time, const ModifierKeys& newButtonState)
	{
		if (buttonState != newButtonState)
		{
			// (ignore secondary clicks when there's already a button down)
			if (buttonState.isAnyMouseButtonDown() == newButtonState.isAnyMouseButtonDown())
			{
				buttonState = newButtonState;
				return;
			}

			if (buttonState.isAnyMouseButtonDown())
			{
				Component* const current = getComponentUnderMouse();

				if (current != 0)
					sendMouseUp (current, screenPos + unboundedMouseOffset, time);

				enableUnboundedMouseMovement (false, false);
			}

			buttonState = newButtonState;

			if (buttonState.isAnyMouseButtonDown())
			{
				Desktop::getInstance().incrementMouseClickCounter();

				Component* const current = getComponentUnderMouse();

				if (current != 0)
				{
					registerMouseDown (screenPos, time, current);
					sendMouseDown (current, screenPos, time);
				}
			}
		}
	}

	void setComponentUnderMouse (Component* const newComponent, const Point<int>& screenPos, const int64 time)
	{
		Component* current = getComponentUnderMouse();

		if (newComponent != current)
		{
			Component::SafePointer<Component> safeNewComp (newComponent);
			const ModifierKeys originalButtonState (buttonState);

			if (current != 0)
			{
				setButtons (screenPos, time, ModifierKeys());
				sendMouseExit (current, screenPos, time);
				buttonState = originalButtonState;
			}

			componentUnderMouse = safeNewComp;
			current = getComponentUnderMouse();

			if (current != 0)
				sendMouseEnter (current, screenPos, time);

			revealCursor (false);
			setButtons (screenPos, time, originalButtonState);
		}
	}

	void setPeer (ComponentPeer* const newPeer, const Point<int>& screenPos, const int64 time)
	{
		ModifierKeys::updateCurrentModifiers();

		if (newPeer != lastPeer)
		{
			setComponentUnderMouse (0, screenPos, time);
			lastPeer = newPeer;
			setComponentUnderMouse (findComponentAt (screenPos), screenPos, time);
		}
	}

	void setScreenPos (const Point<int>& newScreenPos, const int64 time, const bool forceUpdate)
	{
		if (! isDragging())
			setComponentUnderMouse (findComponentAt (newScreenPos), newScreenPos, time);

		if (newScreenPos != lastScreenPos || forceUpdate)
		{
			cancelPendingUpdate();

			lastScreenPos = newScreenPos;
			Component* const current = getComponentUnderMouse();

			if (current != 0)
			{
				if (isDragging())
				{
					registerMouseDrag (newScreenPos);
					sendMouseDrag (current, newScreenPos + unboundedMouseOffset, time);

					if (isUnboundedMouseModeOn)
						handleUnboundedDrag (current);
				}
				else
				{
					sendMouseMove (current, newScreenPos, time);
				}
			}

			revealCursor (false);
		}
	}

	void handleEvent (ComponentPeer* const newPeer, const Point<int>& positionWithinPeer, const int64 time, const ModifierKeys& newMods)
	{
		jassert (newPeer != 0);
		lastTime = time;
		const Point<int> screenPos (newPeer->relativePositionToGlobal (positionWithinPeer));

		if (isDragging() && newMods.isAnyMouseButtonDown())
		{
			setScreenPos (screenPos, time, false);
		}
		else
		{
			setPeer (newPeer, screenPos, time);

			ComponentPeer* peer = getPeer();
			if (peer != 0)
			{
				setButtons (screenPos, time, newMods);

				peer = getPeer();
				if (peer != 0)
					setScreenPos (peer->relativePositionToGlobal (positionWithinPeer), time, false);
			}
		}
	}

	void handleWheel (ComponentPeer* const peer, const Point<int>& positionWithinPeer, int64 time, float x, float y)
	{
		jassert (peer != 0);
		lastTime = time;
		const Point<int> screenPos (peer->relativePositionToGlobal (positionWithinPeer));

		setPeer (peer, screenPos, time);
		setScreenPos (screenPos, time, false);
		triggerFakeMove();

		if (! isDragging())
		{
			Component* current = getComponentUnderMouse();
			if (current != 0)
				sendMouseWheel (current, screenPos, time, x, y);
		}
	}

	const Time getLastMouseDownTime() const throw()
	{
		return Time (mouseDowns[0].time);
	}

	const Point<int> getLastMouseDownPosition() const throw()
	{
		return mouseDowns[0].position;
	}

	int getNumberOfMultipleClicks() const throw()
	{
		int numClicks = 0;

		if (mouseDowns[0].time != 0)
		{
			if (! mouseMovedSignificantlySincePressed)
				++numClicks;

			for (int i = 1; i < numElementsInArray (mouseDowns); ++i)
			{
				if (mouseDowns[0].time - mouseDowns[i].time < (int) (MouseEvent::getDoubleClickTimeout() * (1.0 + 0.25 * (i - 1)))
					&& abs (mouseDowns[0].position.getX() - mouseDowns[i].position.getX()) < 8
					&& abs (mouseDowns[0].position.getY() - mouseDowns[i].position.getY()) < 8
					&& mouseDowns[0].component == mouseDowns[i].component)
				{
					++numClicks;
				}
				else
				{
					break;
				}
			}
		}

		return numClicks;
	}

	bool hasMouseMovedSignificantlySincePressed() const throw()
	{
		return mouseMovedSignificantlySincePressed
				|| lastTime > mouseDowns[0].time + 300;
	}

	void triggerFakeMove()
	{
		triggerAsyncUpdate();
	}

	void handleAsyncUpdate()
	{
		if (! isDragging())
			setScreenPos (Desktop::getMousePosition(), jmax (lastTime, Time::currentTimeMillis()), true);
	}

	void enableUnboundedMouseMovement (bool enable, bool keepCursorVisibleUntilOffscreen)
	{
		enable = enable && isDragging();
		isCursorVisibleUntilOffscreen = keepCursorVisibleUntilOffscreen;

		if (enable != isUnboundedMouseModeOn)
		{
			if ((! enable) && ((! isCursorVisibleUntilOffscreen) || ! unboundedMouseOffset.isOrigin()))
			{
				// when released, return the mouse to within the component's bounds
				Component* current = getComponentUnderMouse();
				if (current != 0)
					Desktop::setMousePosition (current->getScreenBounds()
													.getConstrainedPoint (current->getMouseXYRelative()));
			}

			isUnboundedMouseModeOn = enable;
			unboundedMouseOffset = Point<int>();

			revealCursor (true);
		}
	}

	void handleUnboundedDrag (Component* current)
	{
		const Rectangle<int> screenArea (current->getParentMonitorArea().expanded (-2, -2));

		if (! screenArea.contains (lastScreenPos))
		{
			const Point<int> componentCentre (current->getScreenBounds().getCentre());
			unboundedMouseOffset += (lastScreenPos - componentCentre);
			Desktop::setMousePosition (componentCentre);
		}
		else if (isCursorVisibleUntilOffscreen
				  && (! unboundedMouseOffset.isOrigin())
				  && screenArea.contains (lastScreenPos + unboundedMouseOffset))
		{
			Desktop::setMousePosition (lastScreenPos + unboundedMouseOffset);
			unboundedMouseOffset = Point<int>();
		}
	}

	void showMouseCursor (MouseCursor cursor, bool forcedUpdate)
	{
		if (isUnboundedMouseModeOn && ((! unboundedMouseOffset.isOrigin()) || ! isCursorVisibleUntilOffscreen))
		{
			cursor = MouseCursor::NoCursor;
			forcedUpdate = true;
		}

		if (forcedUpdate || cursor.getHandle() != currentCursorHandle)
		{
			currentCursorHandle = cursor.getHandle();
			cursor.showInWindow (getPeer());
		}
	}

	void hideCursor()
	{
		showMouseCursor (MouseCursor::NoCursor, true);
	}

	void revealCursor (bool forcedUpdate)
	{
		MouseCursor mc (MouseCursor::NormalCursor);

		Component* current = getComponentUnderMouse();
		if (current != 0)
			mc = current->getLookAndFeel().getMouseCursorFor (*current);

		showMouseCursor (mc, forcedUpdate);
	}

	int index;
	bool isMouseDevice;
	Point<int> lastScreenPos;
	ModifierKeys buttonState;

private:
	MouseInputSource& source;
	Component::SafePointer<Component> componentUnderMouse;
	ComponentPeer* lastPeer;

	Point<int> unboundedMouseOffset;
	bool isUnboundedMouseModeOn, isCursorVisibleUntilOffscreen;
	void* currentCursorHandle;

	struct RecentMouseDown
	{
		Point<int> position;
		int64 time;
		Component* component;
	};

	RecentMouseDown mouseDowns[4];
	bool mouseMovedSignificantlySincePressed;
	int64 lastTime;

	void registerMouseDown (const Point<int>& screenPos, const int64 time, Component* const component) throw()
	{
		for (int i = numElementsInArray (mouseDowns); --i > 0;)
			mouseDowns[i] = mouseDowns[i - 1];

		mouseDowns[0].position = screenPos;
		mouseDowns[0].time = time;
		mouseDowns[0].component = component;
		mouseMovedSignificantlySincePressed = false;
	}

	void registerMouseDrag (const Point<int>& screenPos) throw()
	{
		mouseMovedSignificantlySincePressed = mouseMovedSignificantlySincePressed
			   || mouseDowns[0].position.getDistanceFrom (screenPos) >= 4;
	}

	MouseInputSourceInternal (const MouseInputSourceInternal&);
	MouseInputSourceInternal& operator= (const MouseInputSourceInternal&);
};

MouseInputSource::MouseInputSource (const int index, const bool isMouseDevice)
{
	pimpl = new MouseInputSourceInternal (*this, index, isMouseDevice);
}

MouseInputSource::~MouseInputSource()
{
}

bool MouseInputSource::isMouse() const				  { return pimpl->isMouseDevice; }
bool MouseInputSource::isTouch() const				  { return ! isMouse(); }
bool MouseInputSource::canHover() const				 { return isMouse(); }
bool MouseInputSource::hasMouseWheel() const				{ return isMouse(); }
int MouseInputSource::getIndex() const				  { return pimpl->index; }
bool MouseInputSource::isDragging() const				   { return pimpl->isDragging(); }
const Point<int> MouseInputSource::getScreenPosition() const		{ return pimpl->getScreenPosition(); }
const ModifierKeys MouseInputSource::getCurrentModifiers() const	{ return pimpl->getCurrentModifiers(); }
Component* MouseInputSource::getComponentUnderMouse() const		 { return pimpl->getComponentUnderMouse(); }
void MouseInputSource::triggerFakeMove() const			  { pimpl->triggerFakeMove(); }
int MouseInputSource::getNumberOfMultipleClicks() const throw()	 { return pimpl->getNumberOfMultipleClicks(); }
const Time MouseInputSource::getLastMouseDownTime() const throw()	   { return pimpl->getLastMouseDownTime(); }
const Point<int> MouseInputSource::getLastMouseDownPosition() const throw()	 { return pimpl->getLastMouseDownPosition(); }
bool MouseInputSource::hasMouseMovedSignificantlySincePressed() const throw()   { return pimpl->hasMouseMovedSignificantlySincePressed(); }
bool MouseInputSource::canDoUnboundedMovement() const throw()	   { return isMouse(); }
void MouseInputSource::enableUnboundedMouseMovement (bool isEnabled, bool keepCursorVisibleUntilOffscreen)	{ pimpl->enableUnboundedMouseMovement (isEnabled, keepCursorVisibleUntilOffscreen); }
bool MouseInputSource::hasMouseCursor() const throw()		   { return isMouse(); }
void MouseInputSource::showMouseCursor (const MouseCursor& cursor)	  { pimpl->showMouseCursor (cursor, false); }
void MouseInputSource::hideCursor()					 { pimpl->hideCursor(); }
void MouseInputSource::revealCursor()				   { pimpl->revealCursor (false); }
void MouseInputSource::forceMouseCursorUpdate()			 { pimpl->revealCursor (true); }

void MouseInputSource::handleEvent (ComponentPeer* peer, const Point<int>& positionWithinPeer, const int64 time, const ModifierKeys& mods)
{
	pimpl->handleEvent (peer, positionWithinPeer, time, mods.withOnlyMouseButtons());
}

void MouseInputSource::handleWheel (ComponentPeer* const peer, const Point<int>& positionWithinPeer, const int64 time, const float x, const float y)
{
	pimpl->handleWheel (peer, positionWithinPeer, time, x, y);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MouseInputSource.cpp ***/


/*** Start of inlined file: juce_MouseHoverDetector.cpp ***/
BEGIN_JUCE_NAMESPACE

MouseHoverDetector::MouseHoverDetector (const int hoverTimeMillisecs_)
	: source (0),
	  hoverTimeMillisecs (hoverTimeMillisecs_),
	  hasJustHovered (false)
{
	internalTimer.owner = this;
}

MouseHoverDetector::~MouseHoverDetector()
{
	setHoverComponent (0);
}

void MouseHoverDetector::setHoverTimeMillisecs (const int newTimeInMillisecs)
{
	hoverTimeMillisecs = newTimeInMillisecs;
}

void MouseHoverDetector::setHoverComponent (Component* const newSourceComponent)
{
	if (source != newSourceComponent)
	{
		internalTimer.stopTimer();
		hasJustHovered = false;

		if (source != 0)
		{
			// ! you need to delete the hover detector before deleting its component
			jassert (source->isValidComponent());

			source->removeMouseListener (&internalTimer);
		}

		source = newSourceComponent;

		if (newSourceComponent != 0)
			newSourceComponent->addMouseListener (&internalTimer, false);
	}
}

void MouseHoverDetector::hoverTimerCallback()
{
	internalTimer.stopTimer();

	if (source != 0)
	{
		const Point<int> pos (source->getMouseXYRelative());

		if (source->reallyContains (pos.getX(), pos.getY(), false))
		{
			hasJustHovered = true;
			mouseHovered (pos.getX(), pos.getY());
		}
	}
}

void MouseHoverDetector::checkJustHoveredCallback()
{
	if (hasJustHovered)
	{
		hasJustHovered = false;
		mouseMovedAfterHover();
	}
}

void MouseHoverDetector::HoverDetectorInternal::timerCallback()
{
	owner->hoverTimerCallback();
}

void MouseHoverDetector::HoverDetectorInternal::mouseEnter (const MouseEvent&)
{
	stopTimer();
	owner->checkJustHoveredCallback();
}

void MouseHoverDetector::HoverDetectorInternal::mouseExit (const MouseEvent&)
{
	stopTimer();
	owner->checkJustHoveredCallback();
}

void MouseHoverDetector::HoverDetectorInternal::mouseDown (const MouseEvent&)
{
	stopTimer();
	owner->checkJustHoveredCallback();
}

void MouseHoverDetector::HoverDetectorInternal::mouseUp (const MouseEvent&)
{
	stopTimer();
	owner->checkJustHoveredCallback();
}

void MouseHoverDetector::HoverDetectorInternal::mouseMove (const MouseEvent& e)
{
	if (lastX != e.x || lastY != e.y) // to avoid fake mouse-moves setting it off
	{
		lastX = e.x;
		lastY = e.y;

		if (owner->source != 0)
			startTimer (owner->hoverTimeMillisecs);

		owner->checkJustHoveredCallback();
	}
}

void MouseHoverDetector::HoverDetectorInternal::mouseWheelMove (const MouseEvent&, float, float)
{
	stopTimer();
	owner->checkJustHoveredCallback();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MouseHoverDetector.cpp ***/


/*** Start of inlined file: juce_MouseListener.cpp ***/
BEGIN_JUCE_NAMESPACE

void MouseListener::mouseEnter (const MouseEvent&)
{
}

void MouseListener::mouseExit (const MouseEvent&)
{
}

void MouseListener::mouseDown (const MouseEvent&)
{
}

void MouseListener::mouseUp (const MouseEvent&)
{
}

void MouseListener::mouseDrag (const MouseEvent&)
{
}

void MouseListener::mouseMove (const MouseEvent&)
{
}

void MouseListener::mouseDoubleClick (const MouseEvent&)
{
}

void MouseListener::mouseWheelMove (const MouseEvent&, float, float)
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MouseListener.cpp ***/


/*** Start of inlined file: juce_BooleanPropertyComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

BooleanPropertyComponent::BooleanPropertyComponent (const String& name,
													const String& buttonTextWhenTrue,
													const String& buttonTextWhenFalse)
	: PropertyComponent (name),
	  onText (buttonTextWhenTrue),
	  offText (buttonTextWhenFalse)
{
	createButton();
	button->addButtonListener (this);
}

BooleanPropertyComponent::BooleanPropertyComponent (const Value& valueToControl,
													const String& name,
													const String& buttonText)
	: PropertyComponent (name),
	  onText (buttonText),
	  offText (buttonText)
{
	createButton();
	button->setButtonText (buttonText);
	button->getToggleStateValue().referTo (valueToControl);
	button->setClickingTogglesState (true);
}

BooleanPropertyComponent::~BooleanPropertyComponent()
{
	deleteAllChildren();
}

void BooleanPropertyComponent::createButton()
{
	addAndMakeVisible (button = new ToggleButton (String::empty));
	button->setClickingTogglesState (false);
}

void BooleanPropertyComponent::setState (const bool newState)
{
	button->setToggleState (newState, true);
}

bool BooleanPropertyComponent::getState() const
{
	return button->getToggleState();
}

void BooleanPropertyComponent::paint (Graphics& g)
{
	PropertyComponent::paint (g);

	const Rectangle<int> r (button->getBounds());
	g.setColour (Colours::white);
	g.fillRect (r);

	g.setColour (findColour (ComboBox::outlineColourId));
	g.drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight());
}

void BooleanPropertyComponent::refresh()
{
	button->setToggleState (getState(), false);
	button->setButtonText (button->getToggleState() ? onText : offText);
}

void BooleanPropertyComponent::buttonClicked (Button*)
{
	setState (! getState());
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_BooleanPropertyComponent.cpp ***/


/*** Start of inlined file: juce_ButtonPropertyComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

ButtonPropertyComponent::ButtonPropertyComponent (const String& name,
												  const bool triggerOnMouseDown)
	: PropertyComponent (name)
{
	addAndMakeVisible (button = new TextButton (String::empty));
	button->setTriggeredOnMouseDown (triggerOnMouseDown);
	button->addButtonListener (this);
}

ButtonPropertyComponent::~ButtonPropertyComponent()
{
	deleteAllChildren();
}

void ButtonPropertyComponent::refresh()
{
	button->setButtonText (getButtonText());
}

void ButtonPropertyComponent::buttonClicked (Button*)
{
	buttonClicked();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ButtonPropertyComponent.cpp ***/


/*** Start of inlined file: juce_ChoicePropertyComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

ChoicePropertyComponent::ChoicePropertyComponent (const String& name)
	: PropertyComponent (name),
	  comboBox (0)
{
}

ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
												  const String& name,
												  const StringArray& choices_,
												  const Array <int>* choiceIDs)
	: PropertyComponent (name),
	  choices (choices_),
	  comboBox (0)
{
	createComboBox (choiceIDs);

	comboBox->getSelectedIdAsValue().referTo (valueToControl);
}

ChoicePropertyComponent::~ChoicePropertyComponent()
{
	deleteAllChildren();
}

void ChoicePropertyComponent::createComboBox (const Array <int>* choiceIDs)
{
	// The array of IDs must contain the same number of values as the choices list!
	jassert (choiceIDs == 0 || choiceIDs->size() == choices.size());

	addAndMakeVisible (comboBox = new ComboBox (String::empty));

	for (int i = 0; i < choices.size(); ++i)
	{
		if (choices[i].isNotEmpty())
			comboBox->addItem (choices[i], choiceIDs == 0 ? (i + 1)
														  : ((*choiceIDs)[i]));
		else
			comboBox->addSeparator();
	}

	comboBox->setEditableText (false);
}

void ChoicePropertyComponent::setIndex (const int newIndex)
{
	comboBox->setSelectedId (comboBox->getItemId (newIndex));
}

int ChoicePropertyComponent::getIndex() const
{
	return comboBox->getSelectedItemIndex();
}

const StringArray& ChoicePropertyComponent::getChoices() const
{
	return choices;
}

void ChoicePropertyComponent::refresh()
{
	if (comboBox == 0)
	{
		createComboBox (0);
		comboBox->addListener (this);
	}

	comboBox->setSelectedId (getIndex() + 1, true);
}

void ChoicePropertyComponent::comboBoxChanged (ComboBox*)
{
	const int newIndex = comboBox->getSelectedId() - 1;

	if (newIndex != getIndex())
		setIndex (newIndex);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ChoicePropertyComponent.cpp ***/


/*** Start of inlined file: juce_PropertyComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

PropertyComponent::PropertyComponent (const String& name,
									  const int preferredHeight_)
	: Component (name),
	  preferredHeight (preferredHeight_)
{
	jassert (name.isNotEmpty());
}

PropertyComponent::~PropertyComponent()
{
}

void PropertyComponent::paint (Graphics& g)
{
	getLookAndFeel().drawPropertyComponentBackground (g, getWidth(), getHeight(), *this);
	getLookAndFeel().drawPropertyComponentLabel (g, getWidth(), getHeight(), *this);
}

void PropertyComponent::resized()
{
	if (getNumChildComponents() > 0)
		getChildComponent (0)->setBounds (getLookAndFeel().getPropertyComponentContentPosition (*this));
}

void PropertyComponent::enablementChanged()
{
	repaint();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PropertyComponent.cpp ***/


/*** Start of inlined file: juce_PropertyPanel.cpp ***/
BEGIN_JUCE_NAMESPACE

class PropertyPanel::PropertyHolderComponent  : public Component
{
public:
	PropertyHolderComponent()
	{
	}

	~PropertyHolderComponent()
	{
		deleteAllChildren();
	}

	void paint (Graphics&)
	{
	}

	void updateLayout (int width);
	void refreshAll() const;

private:
	PropertyHolderComponent (const PropertyHolderComponent&);
	PropertyHolderComponent& operator= (const PropertyHolderComponent&);
};

class PropertySectionComponent  : public Component
{
public:
	PropertySectionComponent (const String& sectionTitle,
							  const Array <PropertyComponent*>& newProperties,
							  const bool open)
		: Component (sectionTitle),
		  titleHeight (sectionTitle.isNotEmpty() ? 22 : 0),
		  isOpen_ (open)
	{
		for (int i = newProperties.size(); --i >= 0;)
		{
			addAndMakeVisible (newProperties.getUnchecked(i));
			newProperties.getUnchecked(i)->refresh();
		}
	}

	~PropertySectionComponent()
	{
		deleteAllChildren();
	}

	void paint (Graphics& g)
	{
		if (titleHeight > 0)
			getLookAndFeel().drawPropertyPanelSectionHeader (g, getName(), isOpen(), getWidth(), titleHeight);
	}

	void resized()
	{
		int y = titleHeight;

		for (int i = getNumChildComponents(); --i >= 0;)
		{
			PropertyComponent* const pec = dynamic_cast <PropertyComponent*> (getChildComponent (i));

			if (pec != 0)
			{
				const int prefH = pec->getPreferredHeight();
				pec->setBounds (1, y, getWidth() - 2, prefH);
				y += prefH;
			}
		}
	}

	int getPreferredHeight() const
	{
		int y = titleHeight;

		if (isOpen())
		{
			for (int i = 0; i < getNumChildComponents(); ++i)
			{
				PropertyComponent* pec = dynamic_cast <PropertyComponent*> (getChildComponent (i));

				if (pec != 0)
					y += pec->getPreferredHeight();
			}
		}

		return y;
	}

	void setOpen (const bool open)
	{
		if (isOpen_ != open)
		{
			isOpen_ = open;

			for (int i = 0; i < getNumChildComponents(); ++i)
			{
				PropertyComponent* pec = dynamic_cast <PropertyComponent*> (getChildComponent (i));

				if (pec != 0)
					pec->setVisible (open);
			}

			// (unable to use the syntax findParentComponentOfClass <DragAndDropContainer> () because of a VC6 compiler bug)
			PropertyPanel* const pp = findParentComponentOfClass ((PropertyPanel*) 0);

			if (pp != 0)
				pp->resized();
		}
	}

	bool isOpen() const
	{
		return isOpen_;
	}

	void refreshAll() const
	{
		for (int i = 0; i < getNumChildComponents(); ++i)
		{
			PropertyComponent* pec = dynamic_cast <PropertyComponent*> (getChildComponent (i));

			if (pec != 0)
				pec->refresh();
		}
	}

	void mouseDown (const MouseEvent&)
	{
	}

	void mouseUp (const MouseEvent& e)
	{
		if (e.getMouseDownX() < titleHeight
			 && e.x < titleHeight
			 && e.y < titleHeight
			 && e.getNumberOfClicks() != 2)
		{
			setOpen (! isOpen());
		}
	}

	void mouseDoubleClick (const MouseEvent& e)
	{
		if (e.y < titleHeight)
			setOpen (! isOpen());
	}

private:
	int titleHeight;
	bool isOpen_;

	PropertySectionComponent (const PropertySectionComponent&);
	PropertySectionComponent& operator= (const PropertySectionComponent&);
};

void PropertyPanel::PropertyHolderComponent::updateLayout (const int width)
{
	int y = 0;

	for (int i = getNumChildComponents(); --i >= 0;)
	{
		PropertySectionComponent* const section
			= dynamic_cast <PropertySectionComponent*> (getChildComponent (i));

		if (section != 0)
		{
			const int prefH = section->getPreferredHeight();
			section->setBounds (0, y, width, prefH);
			y += prefH;
		}
	}

	setSize (width, y);
	repaint();
}

void PropertyPanel::PropertyHolderComponent::refreshAll() const
{
	for (int i = getNumChildComponents(); --i >= 0;)
	{
		PropertySectionComponent* const section
			= dynamic_cast <PropertySectionComponent*> (getChildComponent (i));

		if (section != 0)
			section->refreshAll();
	}
}

PropertyPanel::PropertyPanel()
{
	messageWhenEmpty = TRANS("(nothing selected)");

	addAndMakeVisible (viewport = new Viewport());
	viewport->setViewedComponent (propertyHolderComponent = new PropertyHolderComponent());
	viewport->setFocusContainer (true);
}

PropertyPanel::~PropertyPanel()
{
	clear();
	deleteAllChildren();
}

void PropertyPanel::paint (Graphics& g)
{
	if (propertyHolderComponent->getNumChildComponents() == 0)
	{
		g.setColour (Colours::black.withAlpha (0.5f));
		g.setFont (14.0f);
		g.drawText (messageWhenEmpty, 0, 0, getWidth(), 30,
					Justification::centred, true);
	}
}

void PropertyPanel::resized()
{
	viewport->setBounds (0, 0, getWidth(), getHeight());
	updatePropHolderLayout();
}

void PropertyPanel::clear()
{
	if (propertyHolderComponent->getNumChildComponents() > 0)
	{
		propertyHolderComponent->deleteAllChildren();
		repaint();
	}
}

void PropertyPanel::addProperties (const Array <PropertyComponent*>& newProperties)
{
	if (propertyHolderComponent->getNumChildComponents() == 0)
		repaint();

	propertyHolderComponent->addAndMakeVisible (new PropertySectionComponent (String::empty,
																			  newProperties,
																			  true), 0);
	updatePropHolderLayout();
}

void PropertyPanel::addSection (const String& sectionTitle,
								const Array <PropertyComponent*>& newProperties,
								const bool shouldBeOpen)
{
	jassert (sectionTitle.isNotEmpty());

	if (propertyHolderComponent->getNumChildComponents() == 0)
		repaint();

	propertyHolderComponent->addAndMakeVisible (new PropertySectionComponent (sectionTitle,
																			  newProperties,
																			  shouldBeOpen), 0);

	updatePropHolderLayout();
}

void PropertyPanel::updatePropHolderLayout() const
{
	const int maxWidth = viewport->getMaximumVisibleWidth();
	propertyHolderComponent->updateLayout (maxWidth);

	const int newMaxWidth = viewport->getMaximumVisibleWidth();
	if (maxWidth != newMaxWidth)
	{
		// need to do this twice because of scrollbars changing the size, etc.
		propertyHolderComponent->updateLayout (newMaxWidth);
	}
}

void PropertyPanel::refreshAll() const
{
	propertyHolderComponent->refreshAll();
}

const StringArray PropertyPanel::getSectionNames() const
{
	StringArray s;

	for (int i = 0; i < propertyHolderComponent->getNumChildComponents(); ++i)
	{
		PropertySectionComponent* const section = dynamic_cast <PropertySectionComponent*> (propertyHolderComponent->getChildComponent (i));

		if (section != 0 && section->getName().isNotEmpty())
			s.add (section->getName());
	}

	return s;
}

bool PropertyPanel::isSectionOpen (const int sectionIndex) const
{
	int index = 0;

	for (int i = 0; i < propertyHolderComponent->getNumChildComponents(); ++i)
	{
		PropertySectionComponent* const section = dynamic_cast <PropertySectionComponent*> (propertyHolderComponent->getChildComponent (i));

		if (section != 0 && section->getName().isNotEmpty())
		{
			if (index == sectionIndex)
				return section->isOpen();

			++index;
		}
	}

	return false;
}

void PropertyPanel::setSectionOpen (const int sectionIndex, const bool shouldBeOpen)
{
	int index = 0;

	for (int i = 0; i < propertyHolderComponent->getNumChildComponents(); ++i)
	{
		PropertySectionComponent* const section = dynamic_cast <PropertySectionComponent*> (propertyHolderComponent->getChildComponent (i));

		if (section != 0 && section->getName().isNotEmpty())
		{
			if (index == sectionIndex)
			{
				section->setOpen (shouldBeOpen);
				break;
			}

			++index;
		}
	}
}

void PropertyPanel::setSectionEnabled (const int sectionIndex, const bool shouldBeEnabled)
{
	int index = 0;

	for (int i = 0; i < propertyHolderComponent->getNumChildComponents(); ++i)
	{
		PropertySectionComponent* const section = dynamic_cast <PropertySectionComponent*> (propertyHolderComponent->getChildComponent (i));

		if (section != 0 && section->getName().isNotEmpty())
		{
			if (index == sectionIndex)
			{
				section->setEnabled (shouldBeEnabled);
				break;
			}

			++index;
		}
	}
}

XmlElement* PropertyPanel::getOpennessState() const
{
	XmlElement* const xml = new XmlElement ("PROPERTYPANELSTATE");

	xml->setAttribute ("scrollPos", viewport->getViewPositionY());

	const StringArray sections (getSectionNames());

	for (int i = 0; i < sections.size(); ++i)
	{
		if (sections[i].isNotEmpty())
		{
			XmlElement* const e = xml->createNewChildElement ("SECTION");
			e->setAttribute ("name", sections[i]);
			e->setAttribute ("open", isSectionOpen (i) ? 1 : 0);
		}
	}

	return xml;
}

void PropertyPanel::restoreOpennessState (const XmlElement& xml)
{
	if (xml.hasTagName ("PROPERTYPANELSTATE"))
	{
		const StringArray sections (getSectionNames());

		forEachXmlChildElementWithTagName (xml, e, "SECTION")
		{
			setSectionOpen (sections.indexOf (e->getStringAttribute ("name")),
							e->getBoolAttribute ("open"));
		}

		viewport->setViewPosition (viewport->getViewPositionX(),
								   xml.getIntAttribute ("scrollPos", viewport->getViewPositionY()));
	}
}

void PropertyPanel::setMessageWhenEmpty (const String& newMessage)
{
	if (messageWhenEmpty != newMessage)
	{
		messageWhenEmpty = newMessage;
		repaint();
	}
}

const String& PropertyPanel::getMessageWhenEmpty() const
{
	return messageWhenEmpty;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PropertyPanel.cpp ***/


/*** Start of inlined file: juce_SliderPropertyComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

SliderPropertyComponent::SliderPropertyComponent (const String& name,
												  const double rangeMin,
												  const double rangeMax,
												  const double interval,
												  const double skewFactor)
	: PropertyComponent (name)
{
	addAndMakeVisible (slider = new Slider (name));

	slider->setRange (rangeMin, rangeMax, interval);
	slider->setSkewFactor (skewFactor);
	slider->setSliderStyle (Slider::LinearBar);

	slider->addListener (this);
}

SliderPropertyComponent::SliderPropertyComponent (const Value& valueToControl,
												  const String& name,
												  const double rangeMin,
												  const double rangeMax,
												  const double interval,
												  const double skewFactor)
	: PropertyComponent (name)
{
	addAndMakeVisible (slider = new Slider (name));

	slider->setRange (rangeMin, rangeMax, interval);
	slider->setSkewFactor (skewFactor);
	slider->setSliderStyle (Slider::LinearBar);

	slider->getValueObject().referTo (valueToControl);
}

SliderPropertyComponent::~SliderPropertyComponent()
{
	deleteAllChildren();
}

void SliderPropertyComponent::setValue (const double /*newValue*/)
{
}

double SliderPropertyComponent::getValue() const
{
	return slider->getValue();
}

void SliderPropertyComponent::refresh()
{
	slider->setValue (getValue(), false);
}

void SliderPropertyComponent::sliderValueChanged (Slider*)
{
	if (getValue() != slider->getValue())
		setValue (slider->getValue());
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_SliderPropertyComponent.cpp ***/


/*** Start of inlined file: juce_TextPropertyComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

class TextPropLabel  : public Label
{
	TextPropertyComponent& owner;
	int maxChars;
	bool isMultiline;

public:
	TextPropLabel (TextPropertyComponent& owner_,
				   const int maxChars_, const bool isMultiline_)
		: Label (String::empty, String::empty),
		  owner (owner_),
		  maxChars (maxChars_),
		  isMultiline (isMultiline_)
	{
		setEditable (true, true, false);

		setColour (backgroundColourId, Colours::white);
		setColour (outlineColourId, findColour (ComboBox::outlineColourId));
	}

	~TextPropLabel()
	{
	}

	TextEditor* createEditorComponent()
	{
		TextEditor* const textEditor = Label::createEditorComponent();

		textEditor->setInputRestrictions (maxChars);

		if (isMultiline)
		{
			textEditor->setMultiLine (true, true);
			textEditor->setReturnKeyStartsNewLine (true);
		}

		return textEditor;
	}

	void textWasEdited()
	{
		owner.textWasEdited();
	}
};

TextPropertyComponent::TextPropertyComponent (const String& name,
											  const int maxNumChars,
											  const bool isMultiLine)
	: PropertyComponent (name)
{
	createEditor (maxNumChars, isMultiLine);
}

TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
											  const String& name,
											  const int maxNumChars,
											  const bool isMultiLine)
	: PropertyComponent (name)
{
	createEditor (maxNumChars, isMultiLine);

	textEditor->getTextValue().referTo (valueToControl);
}

TextPropertyComponent::~TextPropertyComponent()
{
	deleteAllChildren();
}

void TextPropertyComponent::setText (const String& newText)
{
	textEditor->setText (newText, true);
}

const String TextPropertyComponent::getText() const
{
	return textEditor->getText();
}

void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
{
	addAndMakeVisible (textEditor = new TextPropLabel (*this, maxNumChars, isMultiLine));

	if (isMultiLine)
	{
		textEditor->setJustificationType (Justification::topLeft);
		preferredHeight = 120;
	}
}

void TextPropertyComponent::refresh()
{
	textEditor->setText (getText(), false);
}

void TextPropertyComponent::textWasEdited()
{
	const String newText (textEditor->getText());

	if (getText() != newText)
		setText (newText);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TextPropertyComponent.cpp ***/


/*** Start of inlined file: juce_AudioDeviceSelectorComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

class SimpleDeviceManagerInputLevelMeter  : public Component,
											public Timer
{
public:
	SimpleDeviceManagerInputLevelMeter (AudioDeviceManager* const manager_)
		: manager (manager_),
		  level (0)
	{
		startTimer (50);
		manager->enableInputLevelMeasurement (true);
	}

	~SimpleDeviceManagerInputLevelMeter()
	{
		manager->enableInputLevelMeasurement (false);
	}

	void timerCallback()
	{
		const float newLevel = (float) manager->getCurrentInputLevel();

		if (fabsf (level - newLevel) > 0.005f)
		{
			level = newLevel;
			repaint();
		}
	}

	void paint (Graphics& g)
	{
		getLookAndFeel().drawLevelMeter (g, getWidth(), getHeight(),
										 (float) exp (log (level) / 3.0)); // (add a bit of a skew to make the level more obvious)
	}

private:
	AudioDeviceManager* const manager;
	float level;

	SimpleDeviceManagerInputLevelMeter (const SimpleDeviceManagerInputLevelMeter&);
	SimpleDeviceManagerInputLevelMeter& operator= (const SimpleDeviceManagerInputLevelMeter&);
};

class AudioDeviceSelectorComponent::MidiInputSelectorComponentListBox  : public ListBox,
																		 public ListBoxModel
{
public:

	MidiInputSelectorComponentListBox (AudioDeviceManager& deviceManager_,
									   const String& noItemsMessage_,
									   const int minNumber_,
									   const int maxNumber_)
		: ListBox (String::empty, 0),
		  deviceManager (deviceManager_),
		  noItemsMessage (noItemsMessage_),
		  minNumber (minNumber_),
		  maxNumber (maxNumber_)
	{
		items = MidiInput::getDevices();

		setModel (this);
		setOutlineThickness (1);
	}

	~MidiInputSelectorComponentListBox()
	{
	}

	int getNumRows()
	{
		return items.size();
	}

	void paintListBoxItem (int row,
						   Graphics& g,
						   int width, int height,
						   bool rowIsSelected)
	{
		if (((unsigned int) row) < (unsigned int) items.size())
		{
			if (rowIsSelected)
				g.fillAll (findColour (TextEditor::highlightColourId)
							   .withMultipliedAlpha (0.3f));

			const String item (items [row]);
			bool enabled = deviceManager.isMidiInputEnabled (item);

			const int x = getTickX();
			const float tickW = height * 0.75f;

			getLookAndFeel().drawTickBox (g, *this, x - tickW, (height - tickW) / 2, tickW, tickW,
										  enabled, true, true, false);

			g.setFont (height * 0.6f);
			g.setColour (findColour (ListBox::textColourId, true).withMultipliedAlpha (enabled ? 1.0f : 0.6f));
			g.drawText (item, x, 0, width - x - 2, height, Justification::centredLeft, true);
		}
	}

	void listBoxItemClicked (int row, const MouseEvent& e)
	{
		selectRow (row);

		if (e.x < getTickX())
			flipEnablement (row);
	}

	void listBoxItemDoubleClicked (int row, const MouseEvent&)
	{
		flipEnablement (row);
	}

	void returnKeyPressed (int row)
	{
		flipEnablement (row);
	}

	void paint (Graphics& g)
	{
		ListBox::paint (g);

		if (items.size() == 0)
		{
			g.setColour (Colours::grey);
			g.setFont (13.0f);
			g.drawText (noItemsMessage,
						0, 0, getWidth(), getHeight() / 2,
						Justification::centred, true);
		}
	}

	int getBestHeight (const int preferredHeight)
	{
		const int extra = getOutlineThickness() * 2;

		return jmax (getRowHeight() * 2 + extra,
					 jmin (getRowHeight() * getNumRows() + extra,
						   preferredHeight));
	}

	juce_UseDebuggingNewOperator

private:
	AudioDeviceManager& deviceManager;
	const String noItemsMessage;
	StringArray items;
	int minNumber, maxNumber;

	void flipEnablement (const int row)
	{
		if (((unsigned int) row) < (unsigned int) items.size())
		{
			const String item (items [row]);
			deviceManager.setMidiInputEnabled (item, ! deviceManager.isMidiInputEnabled (item));
		}
	}

	int getTickX() const
	{
		return getRowHeight() + 5;
	}

	MidiInputSelectorComponentListBox (const MidiInputSelectorComponentListBox&);
	MidiInputSelectorComponentListBox& operator= (const MidiInputSelectorComponentListBox&);
};

class AudioDeviceSettingsPanel : public Component,
								 public ComboBoxListener,
								 public ChangeListener,
								 public ButtonListener
{
public:
	AudioDeviceSettingsPanel (AudioIODeviceType* type_,
							  AudioIODeviceType::DeviceSetupDetails& setup_,
							  const bool hideAdvancedOptionsWithButton)
		: type (type_),
		  setup (setup_)
	{
		sampleRateDropDown = 0;
		sampleRateLabel = 0;
		bufferSizeDropDown = 0;
		bufferSizeLabel = 0;
		outputDeviceDropDown = 0;
		outputDeviceLabel = 0;
		inputDeviceDropDown = 0;
		inputDeviceLabel = 0;
		testButton = 0;
		inputLevelMeter = 0;
		showUIButton = 0;
		inputChanList = 0;
		outputChanList = 0;
		inputChanLabel = 0;
		outputChanLabel = 0;
		showAdvancedSettingsButton = 0;

		if (hideAdvancedOptionsWithButton)
		{
			addAndMakeVisible (showAdvancedSettingsButton = new TextButton (TRANS("Show advanced settings...")));
			showAdvancedSettingsButton->addButtonListener (this);
		}

		type->scanForDevices();

		setup.manager->addChangeListener (this);
		changeListenerCallback (0);
	}

	~AudioDeviceSettingsPanel()
	{
		setup.manager->removeChangeListener (this);

		deleteAndZero (outputDeviceLabel);
		deleteAndZero (inputDeviceLabel);
		deleteAndZero (sampleRateLabel);
		deleteAndZero (bufferSizeLabel);
		deleteAndZero (showUIButton);
		deleteAndZero (inputChanLabel);
		deleteAndZero (outputChanLabel);
		deleteAndZero (showAdvancedSettingsButton);

		deleteAllChildren();
	}

	void resized()
	{
		const int lx = proportionOfWidth (0.35f);
		const int w = proportionOfWidth (0.4f);
		const int h = 24;
		const int space = 6;
		const int dh = h + space;
		int y = 0;

		if (outputDeviceDropDown != 0)
		{
			outputDeviceDropDown->setBounds (lx, y, w, h);

			if (testButton != 0)
				testButton->setBounds (proportionOfWidth (0.77f),
									   outputDeviceDropDown->getY(),
									   proportionOfWidth (0.18f),
									   h);
			y += dh;
		}

		if (inputDeviceDropDown != 0)
		{
			inputDeviceDropDown->setBounds (lx, y, w, h);

			inputLevelMeter->setBounds (proportionOfWidth (0.77f),
										inputDeviceDropDown->getY(),
										proportionOfWidth (0.18f),
										h);
			y += dh;
		}

		const int maxBoxHeight = 100;//(getHeight() - y - dh * 2) / numBoxes;

		if (outputChanList != 0)
		{
			const int bh = outputChanList->getBestHeight (maxBoxHeight);
			outputChanList->setBounds (lx, y, proportionOfWidth (0.55f), bh);
			y += bh + space;
		}

		if (inputChanList != 0)
		{
			const int bh = inputChanList->getBestHeight (maxBoxHeight);
			inputChanList->setBounds (lx, y, proportionOfWidth (0.55f), bh);
			y += bh + space;
		}

		y += space * 2;

		if (showAdvancedSettingsButton != 0)
		{
			showAdvancedSettingsButton->changeWidthToFitText (h);
			showAdvancedSettingsButton->setTopLeftPosition (lx, y);
		}

		if (sampleRateDropDown != 0)
		{
			sampleRateDropDown->setVisible (showAdvancedSettingsButton == 0
											 || ! showAdvancedSettingsButton->isVisible());

			sampleRateDropDown->setBounds (lx, y, w, h);
			y += dh;
		}

		if (bufferSizeDropDown != 0)
		{
			bufferSizeDropDown->setVisible (showAdvancedSettingsButton == 0
											 || ! showAdvancedSettingsButton->isVisible());
			bufferSizeDropDown->setBounds (lx, y, w, h);
			y += dh;
		}

		if (showUIButton != 0)
		{
			showUIButton->setVisible (showAdvancedSettingsButton == 0
										|| ! showAdvancedSettingsButton->isVisible());
			showUIButton->changeWidthToFitText (h);
			showUIButton->setTopLeftPosition (lx, y);
		}
	}

	void comboBoxChanged (ComboBox* comboBoxThatHasChanged)
	{
		if (comboBoxThatHasChanged == 0)
			return;

		AudioDeviceManager::AudioDeviceSetup config;
		setup.manager->getAudioDeviceSetup (config);
		String error;

		if (comboBoxThatHasChanged == outputDeviceDropDown
			  || comboBoxThatHasChanged == inputDeviceDropDown)
		{
			if (outputDeviceDropDown != 0)
				config.outputDeviceName = outputDeviceDropDown->getSelectedId() < 0 ? String::empty
																					: outputDeviceDropDown->getText();

			if (inputDeviceDropDown != 0)
				config.inputDeviceName = inputDeviceDropDown->getSelectedId() < 0 ? String::empty
																				  : inputDeviceDropDown->getText();

			if (! type->hasSeparateInputsAndOutputs())
				config.inputDeviceName = config.outputDeviceName;

			if (comboBoxThatHasChanged == inputDeviceDropDown)
				config.useDefaultInputChannels = true;
			else
				config.useDefaultOutputChannels = true;

			error = setup.manager->setAudioDeviceSetup (config, true);

			showCorrectDeviceName (inputDeviceDropDown, true);
			showCorrectDeviceName (outputDeviceDropDown, false);

			updateControlPanelButton();
			resized();
		}
		else if (comboBoxThatHasChanged == sampleRateDropDown)
		{
			if (sampleRateDropDown->getSelectedId() > 0)
			{
				config.sampleRate = sampleRateDropDown->getSelectedId();
				error = setup.manager->setAudioDeviceSetup (config, true);
			}
		}
		else if (comboBoxThatHasChanged == bufferSizeDropDown)
		{
			if (bufferSizeDropDown->getSelectedId() > 0)
			{
				config.bufferSize = bufferSizeDropDown->getSelectedId();
				error = setup.manager->setAudioDeviceSetup (config, true);
			}
		}

		if (error.isNotEmpty())
		{
			AlertWindow::showMessageBox (AlertWindow::WarningIcon,
										 "Error when trying to open audio device!",
										 error);
		}
	}

	void buttonClicked (Button* button)
	{
		if (button == showAdvancedSettingsButton)
		{
			showAdvancedSettingsButton->setVisible (false);
			resized();
		}
		else if (button == showUIButton)
		{
			AudioIODevice* const device = setup.manager->getCurrentAudioDevice();

			if (device != 0 && device->showControlPanel())
			{
				setup.manager->closeAudioDevice();
				setup.manager->restartLastAudioDevice();
				getTopLevelComponent()->toFront (true);
			}
		}
		else if (button == testButton && testButton != 0)
		{
			setup.manager->playTestSound();
		}
	}

	void updateControlPanelButton()
	{
		AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice();

		deleteAndZero (showUIButton);

		if (currentDevice != 0 && currentDevice->hasControlPanel())
		{
			addAndMakeVisible (showUIButton = new TextButton (TRANS ("show this device's control panel"),
															  TRANS ("opens the device's own control panel")));
			showUIButton->addButtonListener (this);
		}

		resized();
	}

	void changeListenerCallback (void*)
	{
		AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice();

		if (setup.maxNumOutputChannels > 0 || ! type->hasSeparateInputsAndOutputs())
		{
			if (outputDeviceDropDown == 0)
			{
				outputDeviceDropDown = new ComboBox (String::empty);
				outputDeviceDropDown->addListener (this);
				addAndMakeVisible (outputDeviceDropDown);

				outputDeviceLabel = new Label (String::empty,
											   type->hasSeparateInputsAndOutputs() ? TRANS ("output:")
																				   : TRANS ("device:"));
				outputDeviceLabel->attachToComponent (outputDeviceDropDown, true);

				if (setup.maxNumOutputChannels > 0)
				{
					addAndMakeVisible (testButton = new TextButton (TRANS ("Test")));
					testButton->addButtonListener (this);
				}
			}

			addNamesToDeviceBox (*outputDeviceDropDown, false);
		}

		if (setup.maxNumInputChannels > 0 && type->hasSeparateInputsAndOutputs())
		{
			if (inputDeviceDropDown == 0)
			{
				inputDeviceDropDown = new ComboBox (String::empty);
				inputDeviceDropDown->addListener (this);
				addAndMakeVisible (inputDeviceDropDown);

				inputDeviceLabel = new Label (String::empty, TRANS ("input:"));
				inputDeviceLabel->attachToComponent (inputDeviceDropDown, true);

				addAndMakeVisible (inputLevelMeter
					= new SimpleDeviceManagerInputLevelMeter (setup.manager));
			}

			addNamesToDeviceBox (*inputDeviceDropDown, true);
		}

		updateControlPanelButton();
		showCorrectDeviceName (inputDeviceDropDown, true);
		showCorrectDeviceName (outputDeviceDropDown, false);

		if (currentDevice != 0)
		{
			if (setup.maxNumOutputChannels > 0
				 && setup.minNumOutputChannels < setup.manager->getCurrentAudioDevice()->getOutputChannelNames().size())
			{
				if (outputChanList == 0)
				{
					addAndMakeVisible (outputChanList
						= new ChannelSelectorListBox (setup, ChannelSelectorListBox::audioOutputType,
													  TRANS ("(no audio output channels found)")));
					outputChanLabel = new Label (String::empty, TRANS ("active output channels:"));
					outputChanLabel->attachToComponent (outputChanList, true);
				}

				outputChanList->refresh();
			}
			else
			{
				deleteAndZero (outputChanLabel);
				deleteAndZero (outputChanList);
			}

			if (setup.maxNumInputChannels > 0
				 && setup.minNumInputChannels < setup.manager->getCurrentAudioDevice()->getInputChannelNames().size())
			{
				if (inputChanList == 0)
				{
					addAndMakeVisible (inputChanList
						= new ChannelSelectorListBox (setup, ChannelSelectorListBox::audioInputType,
													  TRANS ("(no audio input channels found)")));
					inputChanLabel = new Label (String::empty, TRANS ("active input channels:"));
					inputChanLabel->attachToComponent (inputChanList, true);
				}

				inputChanList->refresh();
			}
			else
			{
				deleteAndZero (inputChanLabel);
				deleteAndZero (inputChanList);
			}

			// sample rate..
			{
				if (sampleRateDropDown == 0)
				{
					addAndMakeVisible (sampleRateDropDown = new ComboBox (String::empty));
					sampleRateDropDown->addListener (this);

					delete sampleRateLabel;
					sampleRateLabel = new Label (String::empty, TRANS ("sample rate:"));
					sampleRateLabel->attachToComponent (sampleRateDropDown, true);
				}
				else
				{
					sampleRateDropDown->clear();
					sampleRateDropDown->removeListener (this);
				}

				const int numRates = currentDevice->getNumSampleRates();

				for (int i = 0; i < numRates; ++i)
				{
					const int rate = roundToInt (currentDevice->getSampleRate (i));
					sampleRateDropDown->addItem (String (rate) + " Hz", rate);
				}

				sampleRateDropDown->setSelectedId (roundToInt (currentDevice->getCurrentSampleRate()), true);
				sampleRateDropDown->addListener (this);
			}

			// buffer size
			{
				if (bufferSizeDropDown == 0)
				{
					addAndMakeVisible (bufferSizeDropDown = new ComboBox (String::empty));
					bufferSizeDropDown->addListener (this);

					delete bufferSizeLabel;
					bufferSizeLabel = new Label (String::empty, TRANS ("audio buffer size:"));
					bufferSizeLabel->attachToComponent (bufferSizeDropDown, true);
				}
				else
				{
					bufferSizeDropDown->clear();
				}

				const int numBufferSizes = currentDevice->getNumBufferSizesAvailable();
				double currentRate = currentDevice->getCurrentSampleRate();
				if (currentRate == 0)
					currentRate = 48000.0;

				for (int i = 0; i < numBufferSizes; ++i)
				{
					const int bs = currentDevice->getBufferSizeSamples (i);
					bufferSizeDropDown->addItem (String (bs)
												  + " samples ("
												  + String (bs * 1000.0 / currentRate, 1)
												  + " ms)",
												 bs);
				}

				bufferSizeDropDown->setSelectedId (currentDevice->getCurrentBufferSizeSamples(), true);
			}
		}
		else
		{
			jassert (setup.manager->getCurrentAudioDevice() == 0); // not the correct device type!

			deleteAndZero (sampleRateLabel);
			deleteAndZero (bufferSizeLabel);
			deleteAndZero (sampleRateDropDown);
			deleteAndZero (bufferSizeDropDown);

			if (outputDeviceDropDown != 0)
				outputDeviceDropDown->setSelectedId (-1, true);

			if (inputDeviceDropDown != 0)
				inputDeviceDropDown->setSelectedId (-1, true);
		}

		resized();
		setSize (getWidth(), getLowestY() + 4);
	}

private:
	AudioIODeviceType* const type;
	const AudioIODeviceType::DeviceSetupDetails setup;

	ComboBox* outputDeviceDropDown;
	ComboBox* inputDeviceDropDown;
	ComboBox* sampleRateDropDown;
	ComboBox* bufferSizeDropDown;
	Label* outputDeviceLabel;
	Label* inputDeviceLabel;
	Label* sampleRateLabel;
	Label* bufferSizeLabel;
	Label* inputChanLabel;
	Label* outputChanLabel;
	TextButton* testButton;
	Component* inputLevelMeter;
	TextButton* showUIButton;
	TextButton* showAdvancedSettingsButton;

	void showCorrectDeviceName (ComboBox* const box, const bool isInput)
	{
		if (box != 0)
		{
			AudioIODevice* const currentDevice = dynamic_cast <AudioIODevice*> (setup.manager->getCurrentAudioDevice());

			const int index = type->getIndexOfDevice (currentDevice, isInput);

			box->setSelectedId (index + 1, true);

			if (testButton != 0 && ! isInput)
				testButton->setEnabled (index >= 0);
		}
	}

	void addNamesToDeviceBox (ComboBox& combo, bool isInputs)
	{
		const StringArray devs (type->getDeviceNames (isInputs));

		combo.clear (true);

		for (int i = 0; i < devs.size(); ++i)
			combo.addItem (devs[i], i + 1);

		combo.addItem (TRANS("<< none >>"), -1);
		combo.setSelectedId (-1, true);
	}

	int getLowestY() const
	{
		int y = 0;

		for (int i = getNumChildComponents(); --i >= 0;)
			y = jmax (y, getChildComponent (i)->getBottom());

		return y;
	}

public:

	class ChannelSelectorListBox  : public ListBox,
									public ListBoxModel
	{
	public:
		enum BoxType
		{
			audioInputType,
			audioOutputType
		};

		ChannelSelectorListBox (const AudioIODeviceType::DeviceSetupDetails& setup_,
								const BoxType type_,
								const String& noItemsMessage_)
			: ListBox (String::empty, 0),
			  setup (setup_),
			  type (type_),
			  noItemsMessage (noItemsMessage_)
		{
			refresh();
			setModel (this);
			setOutlineThickness (1);
		}

		~ChannelSelectorListBox()
		{
		}

		void refresh()
		{
			items.clear();

			AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice();

			if (currentDevice != 0)
			{
				if (type == audioInputType)
					items = currentDevice->getInputChannelNames();
				else if (type == audioOutputType)
					items = currentDevice->getOutputChannelNames();

				if (setup.useStereoPairs)
				{
					StringArray pairs;

					for (int i = 0; i < items.size(); i += 2)
					{
						const String name (items[i]);
						const String name2 (items[i + 1]);
						String commonBit;

						for (int j = 0; j < name.length(); ++j)
							if (name.substring (0, j).equalsIgnoreCase (name2.substring (0, j)))
								commonBit = name.substring (0, j);

						// Make sure we only split the name at a space, because otherwise, things
						// like "input 11" + "input 12" would become "input 11 + 2"
						while (commonBit.isNotEmpty() && ! CharacterFunctions::isWhitespace (commonBit.getLastCharacter()))
							commonBit = commonBit.dropLastCharacters (1);

						pairs.add (name.trim() + " + " + name2.substring (commonBit.length()).trim());
					}

					items = pairs;
				}
			}

			updateContent();
			repaint();
		}

		int getNumRows()
		{
			return items.size();
		}

		void paintListBoxItem (int row,
							   Graphics& g,
							   int width, int height,
							   bool rowIsSelected)
		{
			if (((unsigned int) row) < (unsigned int) items.size())
			{
				if (rowIsSelected)
					g.fillAll (findColour (TextEditor::highlightColourId)
								   .withMultipliedAlpha (0.3f));

				const String item (items [row]);
				bool enabled = false;

				AudioDeviceManager::AudioDeviceSetup config;
				setup.manager->getAudioDeviceSetup (config);

				if (setup.useStereoPairs)
				{
					if (type == audioInputType)
						enabled = config.inputChannels [row * 2] || config.inputChannels [row * 2 + 1];
					else if (type == audioOutputType)
						enabled = config.outputChannels [row * 2] || config.outputChannels [row * 2 + 1];
				}
				else
				{
					if (type == audioInputType)
						enabled = config.inputChannels [row];
					else if (type == audioOutputType)
						enabled = config.outputChannels [row];
				}

				const int x = getTickX();
				const float tickW = height * 0.75f;

				getLookAndFeel().drawTickBox (g, *this, x - tickW, (height - tickW) / 2, tickW, tickW,
											  enabled, true, true, false);

				g.setFont (height * 0.6f);
				g.setColour (findColour (ListBox::textColourId, true).withMultipliedAlpha (enabled ? 1.0f : 0.6f));
				g.drawText (item, x, 0, width - x - 2, height, Justification::centredLeft, true);
			}
		}

		void listBoxItemClicked (int row, const MouseEvent& e)
		{
			selectRow (row);

			if (e.x < getTickX())
				flipEnablement (row);
		}

		void listBoxItemDoubleClicked (int row, const MouseEvent&)
		{
			flipEnablement (row);
		}

		void returnKeyPressed (int row)
		{
			flipEnablement (row);
		}

		void paint (Graphics& g)
		{
			ListBox::paint (g);

			if (items.size() == 0)
			{
				g.setColour (Colours::grey);
				g.setFont (13.0f);
				g.drawText (noItemsMessage,
							0, 0, getWidth(), getHeight() / 2,
							Justification::centred, true);
			}
		}

		int getBestHeight (int maxHeight)
		{
			return getRowHeight() * jlimit (2, jmax (2, maxHeight / getRowHeight()),
											getNumRows())
					   + getOutlineThickness() * 2;
		}

		juce_UseDebuggingNewOperator

	private:
		const AudioIODeviceType::DeviceSetupDetails setup;
		const BoxType type;
		const String noItemsMessage;
		StringArray items;

		void flipEnablement (const int row)
		{
			jassert (type == audioInputType || type == audioOutputType);

			if (((unsigned int) row) < (unsigned int) items.size())
			{
				AudioDeviceManager::AudioDeviceSetup config;
				setup.manager->getAudioDeviceSetup (config);

				if (setup.useStereoPairs)
				{
					BigInteger bits;
					BigInteger& original = (type == audioInputType ? config.inputChannels
																   : config.outputChannels);

					int i;
					for (i = 0; i < 256; i += 2)
						bits.setBit (i / 2, original [i] || original [i + 1]);

					if (type == audioInputType)
					{
						config.useDefaultInputChannels = false;
						flipBit (bits, row, setup.minNumInputChannels / 2, setup.maxNumInputChannels / 2);
					}
					else
					{
						config.useDefaultOutputChannels = false;
						flipBit (bits, row, setup.minNumOutputChannels / 2, setup.maxNumOutputChannels / 2);
					}

					for (i = 0; i < 256; ++i)
						original.setBit (i, bits [i / 2]);
				}
				else
				{
					if (type == audioInputType)
					{
						config.useDefaultInputChannels = false;
						flipBit (config.inputChannels, row, setup.minNumInputChannels, setup.maxNumInputChannels);
					}
					else
					{
						config.useDefaultOutputChannels = false;
						flipBit (config.outputChannels, row, setup.minNumOutputChannels, setup.maxNumOutputChannels);
					}
				}

				String error (setup.manager->setAudioDeviceSetup (config, true));

				if (! error.isEmpty())
				{
					//xxx
				}
			}
		}

		static void flipBit (BigInteger& chans, int index, int minNumber, int maxNumber)
		{
			const int numActive = chans.countNumberOfSetBits();

			if (chans [index])
			{
				if (numActive > minNumber)
					chans.setBit (index, false);
			}
			else
			{
				if (numActive >= maxNumber)
				{
					const int firstActiveChan = chans.findNextSetBit();

					chans.setBit (index > firstActiveChan
									 ? firstActiveChan : chans.getHighestBit(),
								  false);
				}

				chans.setBit (index, true);
			}
		}

		int getTickX() const
		{
			return getRowHeight() + 5;
		}

		ChannelSelectorListBox (const ChannelSelectorListBox&);
		ChannelSelectorListBox& operator= (const ChannelSelectorListBox&);
	};

private:
	ChannelSelectorListBox* inputChanList;
	ChannelSelectorListBox* outputChanList;

	AudioDeviceSettingsPanel (const AudioDeviceSettingsPanel&);
	AudioDeviceSettingsPanel& operator= (const AudioDeviceSettingsPanel&);
};

AudioDeviceSelectorComponent::AudioDeviceSelectorComponent (AudioDeviceManager& deviceManager_,
															const int minInputChannels_,
															const int maxInputChannels_,
															const int minOutputChannels_,
															const int maxOutputChannels_,
															const bool showMidiInputOptions,
															const bool showMidiOutputSelector,
															const bool showChannelsAsStereoPairs_,
															const bool hideAdvancedOptionsWithButton_)
	: deviceManager (deviceManager_),
	  deviceTypeDropDown (0),
	  deviceTypeDropDownLabel (0),
	  audioDeviceSettingsComp (0),
	  minOutputChannels (minOutputChannels_),
	  maxOutputChannels (maxOutputChannels_),
	  minInputChannels (minInputChannels_),
	  maxInputChannels (maxInputChannels_),
	  showChannelsAsStereoPairs (showChannelsAsStereoPairs_),
	  hideAdvancedOptionsWithButton (hideAdvancedOptionsWithButton_)
{
	jassert (minOutputChannels >= 0 && minOutputChannels <= maxOutputChannels);
	jassert (minInputChannels >= 0 && minInputChannels <= maxInputChannels);

	if (deviceManager_.getAvailableDeviceTypes().size() > 1)
	{
		deviceTypeDropDown = new ComboBox (String::empty);

		for (int i = 0; i < deviceManager_.getAvailableDeviceTypes().size(); ++i)
		{
			deviceTypeDropDown
				->addItem (deviceManager_.getAvailableDeviceTypes().getUnchecked(i)->getTypeName(),
						   i + 1);
		}

		addAndMakeVisible (deviceTypeDropDown);
		deviceTypeDropDown->addListener (this);

		deviceTypeDropDownLabel = new Label (String::empty, TRANS ("audio device type:"));
		deviceTypeDropDownLabel->setJustificationType (Justification::centredRight);
		deviceTypeDropDownLabel->attachToComponent (deviceTypeDropDown, true);
	}

	if (showMidiInputOptions)
	{
		addAndMakeVisible (midiInputsList
							= new MidiInputSelectorComponentListBox (deviceManager,
																	 TRANS("(no midi inputs available)"),
																	 0, 0));

		midiInputsLabel = new Label (String::empty, TRANS ("active midi inputs:"));
		midiInputsLabel->setJustificationType (Justification::topRight);
		midiInputsLabel->attachToComponent (midiInputsList, true);
	}
	else
	{
		midiInputsList = 0;
		midiInputsLabel = 0;
	}

	if (showMidiOutputSelector)
	{
		addAndMakeVisible (midiOutputSelector = new ComboBox (String::empty));
		midiOutputSelector->addListener (this);

		midiOutputLabel = new Label ("lm", TRANS("Midi Output:"));
		midiOutputLabel->attachToComponent (midiOutputSelector, true);
	}
	else
	{
		midiOutputSelector = 0;
		midiOutputLabel = 0;
	}

	deviceManager_.addChangeListener (this);
	changeListenerCallback (0);
}

AudioDeviceSelectorComponent::~AudioDeviceSelectorComponent()
{
	deviceManager.removeChangeListener (this);
	deleteAllChildren();
}

void AudioDeviceSelectorComponent::resized()
{
	const int lx = proportionOfWidth (0.35f);
	const int w = proportionOfWidth (0.4f);
	const int h = 24;
	const int space = 6;
	const int dh = h + space;
	int y = 15;

	if (deviceTypeDropDown != 0)
	{
		deviceTypeDropDown->setBounds (lx, y, proportionOfWidth (0.3f), h);
		y += dh + space * 2;
	}

	if (audioDeviceSettingsComp != 0)
	{
		audioDeviceSettingsComp->setBounds (0, y, getWidth(), audioDeviceSettingsComp->getHeight());
		y += audioDeviceSettingsComp->getHeight() + space;
	}

	if (midiInputsList != 0)
	{
		const int bh = midiInputsList->getBestHeight (jmin (h * 8, getHeight() - y - space - h));
		midiInputsList->setBounds (lx, y, w, bh);
		y += bh + space;
	}

	if (midiOutputSelector != 0)
		midiOutputSelector->setBounds (lx, y, w, h);
}

void AudioDeviceSelectorComponent::childBoundsChanged (Component* child)
{
	if (child == audioDeviceSettingsComp)
		resized();
}

void AudioDeviceSelectorComponent::buttonClicked (Button*)
{
	AudioIODevice* const device = deviceManager.getCurrentAudioDevice();

	if (device != 0 && device->hasControlPanel())
	{
		if (device->showControlPanel())
			deviceManager.restartLastAudioDevice();

		getTopLevelComponent()->toFront (true);
	}
}

void AudioDeviceSelectorComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
{
	if (comboBoxThatHasChanged == deviceTypeDropDown)
	{
		AudioIODeviceType* const type = deviceManager.getAvailableDeviceTypes() [deviceTypeDropDown->getSelectedId() - 1];

		if (type != 0)
		{
			deleteAndZero (audioDeviceSettingsComp);

			deviceManager.setCurrentAudioDeviceType (type->getTypeName(), true);

			changeListenerCallback (0); // needed in case the type hasn't actally changed
		}
	}
	else if (comboBoxThatHasChanged == midiOutputSelector)
	{
		deviceManager.setDefaultMidiOutput (midiOutputSelector->getText());
	}
}

void AudioDeviceSelectorComponent::changeListenerCallback (void*)
{
	if (deviceTypeDropDown != 0)
	{
		deviceTypeDropDown->setText (deviceManager.getCurrentAudioDeviceType(), false);
	}

	if (audioDeviceSettingsComp == 0
		 || audioDeviceSettingsCompType != deviceManager.getCurrentAudioDeviceType())
	{
		audioDeviceSettingsCompType = deviceManager.getCurrentAudioDeviceType();

		deleteAndZero (audioDeviceSettingsComp);

		AudioIODeviceType* const type
			= deviceManager.getAvailableDeviceTypes() [deviceTypeDropDown == 0
														? 0 : deviceTypeDropDown->getSelectedId() - 1];

		if (type != 0)
		{
			AudioIODeviceType::DeviceSetupDetails details;
			details.manager = &deviceManager;
			details.minNumInputChannels = minInputChannels;
			details.maxNumInputChannels = maxInputChannels;
			details.minNumOutputChannels = minOutputChannels;
			details.maxNumOutputChannels = maxOutputChannels;
			details.useStereoPairs = showChannelsAsStereoPairs;

			audioDeviceSettingsComp = new AudioDeviceSettingsPanel (type, details, hideAdvancedOptionsWithButton);

			if (audioDeviceSettingsComp != 0)
			{
				addAndMakeVisible (audioDeviceSettingsComp);
				audioDeviceSettingsComp->resized();
			}
		}
	}

	if (midiInputsList != 0)
	{
		midiInputsList->updateContent();
		midiInputsList->repaint();
	}

	if (midiOutputSelector != 0)
	{
		midiOutputSelector->clear();

		const StringArray midiOuts (MidiOutput::getDevices());

		midiOutputSelector->addItem (TRANS("<< none >>"), -1);
		midiOutputSelector->addSeparator();

		for (int i = 0; i < midiOuts.size(); ++i)
			midiOutputSelector->addItem (midiOuts[i], i + 1);

		int current = -1;

		if (deviceManager.getDefaultMidiOutput() != 0)
			current = 1 + midiOuts.indexOf (deviceManager.getDefaultMidiOutputName());

		midiOutputSelector->setSelectedId (current, true);
	}

	resized();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AudioDeviceSelectorComponent.cpp ***/


/*** Start of inlined file: juce_BubbleComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

BubbleComponent::BubbleComponent()
  : side (0),
	allowablePlacements (above | below | left | right),
	arrowTipX (0.0f),
	arrowTipY (0.0f)
{
	setInterceptsMouseClicks (false, false);

	shadow.setShadowProperties (5.0f, 0.35f, 0, 0);
	setComponentEffect (&shadow);
}

BubbleComponent::~BubbleComponent()
{
}

void BubbleComponent::paint (Graphics& g)
{
	int x = content.getX();
	int y = content.getY();
	int w = content.getWidth();
	int h = content.getHeight();

	int cw, ch;
	getContentSize (cw, ch);

	if (side == 3)
		x += w - cw;
	else if (side != 1)
		x += (w - cw) / 2;

	w = cw;

	if (side == 2)
		y += h - ch;
	else if (side != 0)
		y += (h - ch) / 2;

	h = ch;

	getLookAndFeel().drawBubble (g, arrowTipX, arrowTipY,
								 (float) x, (float) y,
								 (float) w, (float) h);

	const int cx = x + (w - cw) / 2;
	const int cy = y + (h - ch) / 2;

	const int indent = 3;

	g.setOrigin (cx + indent, cy + indent);
	g.reduceClipRegion (0, 0, cw - indent * 2, ch - indent * 2);

	paintContent (g, cw - indent * 2, ch - indent * 2);
}

void BubbleComponent::setAllowedPlacement (const int newPlacement)
{
	allowablePlacements = newPlacement;
}

void BubbleComponent::setPosition (Component* componentToPointTo)
{
	jassert (componentToPointTo->isValidComponent());

	Point<int> pos;

	if (getParentComponent() != 0)
		pos = componentToPointTo->relativePositionToOtherComponent (getParentComponent(), pos);
	else
		pos = componentToPointTo->relativePositionToGlobal (pos);

	setPosition (Rectangle<int> (pos.getX(), pos.getY(), componentToPointTo->getWidth(), componentToPointTo->getHeight()));
}

void BubbleComponent::setPosition (const int arrowTipX_,
								   const int arrowTipY_)
{
	setPosition (Rectangle<int> (arrowTipX_, arrowTipY_, 1, 1));
}

void BubbleComponent::setPosition (const Rectangle<int>& rectangleToPointTo)
{
	Rectangle<int> availableSpace;

	if (getParentComponent() != 0)
	{
		availableSpace.setSize (getParentComponent()->getWidth(),
								getParentComponent()->getHeight());
	}
	else
	{
		availableSpace = getParentMonitorArea();
	}

	int x = 0;
	int y = 0;
	int w = 150;
	int h = 30;

	getContentSize (w, h);
	w += 30;
	h += 30;

	const float edgeIndent = 2.0f;
	const int arrowLength = jmin (10, h / 3, w / 3);

	int spaceAbove = ((allowablePlacements & above) != 0) ? jmax (0, rectangleToPointTo.getY() - availableSpace.getY()) : -1;
	int spaceBelow = ((allowablePlacements & below) != 0) ? jmax (0, availableSpace.getBottom() - rectangleToPointTo.getBottom()) : -1;
	int spaceLeft  = ((allowablePlacements & left)  != 0) ? jmax (0, rectangleToPointTo.getX() - availableSpace.getX()) : -1;
	int spaceRight = ((allowablePlacements & right) != 0) ? jmax (0, availableSpace.getRight() - rectangleToPointTo.getRight()) : -1;

	// look at whether the component is elongated, and if so, try to position next to its longer dimension.
	if (rectangleToPointTo.getWidth() > rectangleToPointTo.getHeight() * 2
		 && (spaceAbove > h + 20 || spaceBelow > h + 20))
	{
		spaceLeft = spaceRight = 0;
	}
	else if (rectangleToPointTo.getWidth() < rectangleToPointTo.getHeight() / 2
			  && (spaceLeft > w + 20 || spaceRight > w + 20))
	{
		spaceAbove = spaceBelow = 0;
	}

	if (jmax (spaceAbove, spaceBelow) >= jmax (spaceLeft, spaceRight))
	{
		x = rectangleToPointTo.getX() + (rectangleToPointTo.getWidth() - w) / 2;
		arrowTipX = w * 0.5f;
		content.setSize (w, h - arrowLength);

		if (spaceAbove >= spaceBelow)
		{
			// above
			y = rectangleToPointTo.getY() - h;
			content.setPosition (0, 0);
			arrowTipY = h - edgeIndent;
			side = 2;
		}
		else
		{
			// below
			y = rectangleToPointTo.getBottom();
			content.setPosition (0, arrowLength);
			arrowTipY = edgeIndent;
			side = 0;
		}
	}
	else
	{
		y = rectangleToPointTo.getY() + (rectangleToPointTo.getHeight() - h) / 2;
		arrowTipY = h * 0.5f;
		content.setSize (w - arrowLength, h);

		if (spaceLeft > spaceRight)
		{
			// on the left
			x = rectangleToPointTo.getX() - w;
			content.setPosition (0, 0);
			arrowTipX = w - edgeIndent;
			side = 3;
		}
		else
		{
			// on the right
			x = rectangleToPointTo.getRight();
			content.setPosition (arrowLength, 0);
			arrowTipX = edgeIndent;
			side = 1;
		}
	}

	setBounds (x, y, w, h);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_BubbleComponent.cpp ***/


/*** Start of inlined file: juce_BubbleMessageComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

BubbleMessageComponent::BubbleMessageComponent (int fadeOutLengthMs)
	: fadeOutLength (fadeOutLengthMs),
	  deleteAfterUse (false)
{
}

BubbleMessageComponent::~BubbleMessageComponent()
{
	fadeOutComponent (fadeOutLength);
}

void BubbleMessageComponent::showAt (int x, int y,
									 const String& text,
									 const int numMillisecondsBeforeRemoving,
									 const bool removeWhenMouseClicked,
									 const bool deleteSelfAfterUse)
{
	textLayout.clear();
	textLayout.setText (text, Font (14.0f));
	textLayout.layout (256, Justification::centredLeft, true);

	setPosition (x, y);

	init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
}

void BubbleMessageComponent::showAt (Component* const component,
									 const String& text,
									 const int numMillisecondsBeforeRemoving,
									 const bool removeWhenMouseClicked,
									 const bool deleteSelfAfterUse)
{
	textLayout.clear();
	textLayout.setText (text, Font (14.0f));
	textLayout.layout (256, Justification::centredLeft, true);

	setPosition (component);

	init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
}

void BubbleMessageComponent::init (const int numMillisecondsBeforeRemoving,
								   const bool removeWhenMouseClicked,
								   const bool deleteSelfAfterUse)
{
	setVisible (true);

	deleteAfterUse = deleteSelfAfterUse;

	if (numMillisecondsBeforeRemoving > 0)
		expiryTime = Time::getMillisecondCounter() + numMillisecondsBeforeRemoving;
	else
		expiryTime = 0;

	startTimer (77);

	mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();

	if (! (removeWhenMouseClicked && isShowing()))
		mouseClickCounter += 0xfffff;

	repaint();
}

void BubbleMessageComponent::getContentSize (int& w, int& h)
{
	w = textLayout.getWidth() + 16;
	h = textLayout.getHeight() + 16;
}

void BubbleMessageComponent::paintContent (Graphics& g, int w, int h)
{
	g.setColour (findColour (TooltipWindow::textColourId));

	textLayout.drawWithin (g, 0, 0, w, h, Justification::centred);
}

void BubbleMessageComponent::timerCallback()
{
	if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
	{
		stopTimer();
		setVisible (false);

		if (deleteAfterUse)
			delete this;
	}
	else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
	{
		stopTimer();
		fadeOutComponent (fadeOutLength);

		if (deleteAfterUse)
			delete this;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_BubbleMessageComponent.cpp ***/


/*** Start of inlined file: juce_ColourSelector.cpp ***/
BEGIN_JUCE_NAMESPACE

static const int swatchesPerRow = 8;
static const int swatchHeight = 22;

class ColourComponentSlider  : public Slider
{
public:
	ColourComponentSlider (const String& name)
		: Slider (name)
	{
		setRange (0.0, 255.0, 1.0);
	}

	~ColourComponentSlider()
	{
	}

	const String getTextFromValue (double value)
	{
		return String::toHexString ((int) value).toUpperCase().paddedLeft ('0', 2);
	}

	double getValueFromText (const String& text)
	{
		return (double) text.getHexValue32();
	}

private:
	ColourComponentSlider (const ColourComponentSlider&);
	ColourComponentSlider& operator= (const ColourComponentSlider&);
};

class ColourSpaceMarker  : public Component
{
public:
	ColourSpaceMarker()
	{
		setInterceptsMouseClicks (false, false);
	}

	~ColourSpaceMarker()
	{
	}

	void paint (Graphics& g)
	{
		g.setColour (Colour::greyLevel (0.1f));
		g.drawEllipse (1.0f, 1.0f, getWidth() - 2.0f, getHeight() - 2.0f, 1.0f);
		g.setColour (Colour::greyLevel (0.9f));
		g.drawEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f, 1.0f);
	}

private:
	ColourSpaceMarker (const ColourSpaceMarker&);
	ColourSpaceMarker& operator= (const ColourSpaceMarker&);
};

class ColourSelector::ColourSpaceView  : public Component
{
public:
	ColourSpaceView (ColourSelector* owner_,
					 float& h_, float& s_, float& v_,
					 const int edgeSize)
		: owner (owner_),
		  h (h_), s (s_), v (v_),
		  lastHue (0.0f),
		  edge (edgeSize)
	{
		addAndMakeVisible (marker = new ColourSpaceMarker());
		setMouseCursor (MouseCursor::CrosshairCursor);
	}

	~ColourSpaceView()
	{
		deleteAllChildren();
	}

	void paint (Graphics& g)
	{
		if (colours == 0)
		{
			const int width = getWidth() / 2;
			const int height = getHeight() / 2;
			colours = new Image (Image::RGB, width, height, false);

			Image::BitmapData pixels (*colours, 0, 0, width, height, true);

			for (int y = 0; y < height; ++y)
			{
				const float v = 1.0f - y / (float) height;

				for (int x = 0; x < width; ++x)
				{
					const float s = x / (float) width;
					const Colour col (h, s, v, 1.0f);

					PixelRGB* const pix = (PixelRGB*) pixels.getPixelPointer (x, y);
					pix->set (col.getPixelARGB());
				}
			}
		}

		g.setOpacity (1.0f);
		g.drawImage (colours, edge, edge, getWidth() - edge * 2, getHeight() - edge * 2,
					 0, 0, colours->getWidth(), colours->getHeight());
	}

	void mouseDown (const MouseEvent& e)
	{
		mouseDrag (e);
	}

	void mouseDrag (const MouseEvent& e)
	{
		const float s = (e.x - edge) / (float) (getWidth() - edge * 2);
		const float v = 1.0f - (e.y - edge) / (float) (getHeight() - edge * 2);

		owner->setSV (s, v);
	}

	void updateIfNeeded()
	{
		if (lastHue != h)
		{
			lastHue = h;
			colours = 0;
			repaint();
		}

		updateMarker();
	}

	void resized()
	{
		colours = 0;
		updateMarker();
	}

private:
	ColourSelector* const owner;
	float& h;
	float& s;
	float& v;
	float lastHue;
	ColourSpaceMarker* marker;
	const int edge;

	ScopedPointer <Image> colours;

	void updateMarker() const throw()
	{
		marker->setBounds (roundToInt ((getWidth() - edge * 2) * s),
						   roundToInt ((getHeight() - edge * 2) * (1.0f - v)),
						   edge * 2, edge * 2);
	}

	ColourSpaceView (const ColourSpaceView&);
	ColourSpaceView& operator= (const ColourSpaceView&);
};

class HueSelectorMarker  : public Component
{
public:
	HueSelectorMarker()
	{
		setInterceptsMouseClicks (false, false);
	}

	~HueSelectorMarker()
	{
	}

	void paint (Graphics& g)
	{
		Path p;
		p.addTriangle (1.0f, 1.0f,
					   getWidth() * 0.3f, getHeight() * 0.5f,
					   1.0f, getHeight() - 1.0f);

		p.addTriangle (getWidth() - 1.0f, 1.0f,
					   getWidth() * 0.7f, getHeight() * 0.5f,
					   getWidth() - 1.0f, getHeight() - 1.0f);

		g.setColour (Colours::white.withAlpha (0.75f));
		g.fillPath (p);

		g.setColour (Colours::black.withAlpha (0.75f));
		g.strokePath (p, PathStrokeType (1.2f));
	}

private:
	HueSelectorMarker (const HueSelectorMarker&);
	HueSelectorMarker& operator= (const HueSelectorMarker&);
};

class ColourSelector::HueSelectorComp  : public Component
{
public:
	HueSelectorComp (ColourSelector* owner_,
					 float& h_, float& s_, float& v_,
					 const int edgeSize)
		: owner (owner_),
		  h (h_), s (s_), v (v_),
		  lastHue (0.0f),
		  edge (edgeSize)
	{
		addAndMakeVisible (marker = new HueSelectorMarker());
	}

	~HueSelectorComp()
	{
		deleteAllChildren();
	}

	void paint (Graphics& g)
	{
		const float yScale = 1.0f / (getHeight() - edge * 2);

		const Rectangle<int> clip (g.getClipBounds());

		for (int y = jmin (clip.getBottom(), getHeight() - edge); --y >= jmax (edge, clip.getY());)
		{
			g.setColour (Colour ((y - edge) * yScale, 1.0f, 1.0f, 1.0f));
			g.fillRect (edge, y, getWidth() - edge * 2, 1);
		}
	}

	void resized()
	{
		marker->setBounds (0, roundToInt ((getHeight() - edge * 2) * h),
						   getWidth(), edge * 2);
	}

	void mouseDown (const MouseEvent& e)
	{
		mouseDrag (e);
	}

	void mouseDrag (const MouseEvent& e)
	{
		const float hue = (e.y - edge) / (float) (getHeight() - edge * 2);

		owner->setHue (hue);
	}

	void updateIfNeeded()
	{
		resized();
	}

private:
	ColourSelector* const owner;
	float& h;
	float& s;
	float& v;
	float lastHue;
	HueSelectorMarker* marker;
	const int edge;

	HueSelectorComp (const HueSelectorComp&);
	HueSelectorComp& operator= (const HueSelectorComp&);
};

class ColourSelector::SwatchComponent   : public Component
{
public:
	SwatchComponent (ColourSelector* owner_, int index_)
		: owner (owner_),
		  index (index_)
	{
	}

	~SwatchComponent()
	{
	}

	void paint (Graphics& g)
	{
		const Colour colour (owner->getSwatchColour (index));

		g.fillCheckerBoard (0, 0, getWidth(), getHeight(),
							6, 6,
							Colour (0xffdddddd).overlaidWith (colour),
							Colour (0xffffffff).overlaidWith (colour));
	}

	void mouseDown (const MouseEvent&)
	{
		PopupMenu m;
		m.addItem (1, TRANS("Use this swatch as the current colour"));
		m.addSeparator();
		m.addItem (2, TRANS("Set this swatch to the current colour"));

		const int r = m.showAt (this);

		if (r == 1)
		{
			owner->setCurrentColour (owner->getSwatchColour (index));
		}
		else if (r == 2)
		{
			if (owner->getSwatchColour (index) != owner->getCurrentColour())
			{
				owner->setSwatchColour (index, owner->getCurrentColour());
				repaint();
			}
		}
	}

private:
	ColourSelector* const owner;
	const int index;

	SwatchComponent (const SwatchComponent&);
	SwatchComponent& operator= (const SwatchComponent&);
};

ColourSelector::ColourSelector (const int flags_,
								const int edgeGap_,
								const int gapAroundColourSpaceComponent)
	: colour (Colours::white),
	  flags (flags_),
	  topSpace (0),
	  edgeGap (edgeGap_)
{
	// not much point having a selector with no components in it!
	jassert ((flags_ & (showColourAtTop | showSliders | showColourspace)) != 0);

	updateHSV();

	if ((flags & showSliders) != 0)
	{
		addAndMakeVisible (sliders[0] = new ColourComponentSlider (TRANS ("red")));
		addAndMakeVisible (sliders[1] = new ColourComponentSlider (TRANS ("green")));
		addAndMakeVisible (sliders[2] = new ColourComponentSlider (TRANS ("blue")));
		addChildComponent (sliders[3] = new ColourComponentSlider (TRANS ("alpha")));

		sliders[3]->setVisible ((flags & showAlphaChannel) != 0);

		for (int i = 4; --i >= 0;)
			sliders[i]->addListener (this);
	}
	else
	{
		zeromem (sliders, sizeof (sliders));
	}

	if ((flags & showColourspace) != 0)
	{
		addAndMakeVisible (colourSpace = new ColourSpaceView (this, h, s, v, gapAroundColourSpaceComponent));
		addAndMakeVisible (hueSelector = new HueSelectorComp (this, h, s, v, gapAroundColourSpaceComponent));
	}
	else
	{
		colourSpace = 0;
		hueSelector = 0;
	}

	update();
}

ColourSelector::~ColourSelector()
{
	dispatchPendingMessages();
	swatchComponents.clear();
	deleteAllChildren();
}

const Colour ColourSelector::getCurrentColour() const
{
	return ((flags & showAlphaChannel) != 0) ? colour
											 : colour.withAlpha ((uint8) 0xff);
}

void ColourSelector::setCurrentColour (const Colour& c)
{
	if (c != colour)
	{
		colour = ((flags & showAlphaChannel) != 0) ? c : c.withAlpha ((uint8) 0xff);

		updateHSV();
		update();
	}
}

void ColourSelector::setHue (float newH)
{
	newH = jlimit (0.0f, 1.0f, newH);

	if (h != newH)
	{
		h = newH;
		colour = Colour (h, s, v, colour.getFloatAlpha());
		update();
	}
}

void ColourSelector::setSV (float newS, float newV)
{
	newS = jlimit (0.0f, 1.0f, newS);
	newV = jlimit (0.0f, 1.0f, newV);

	if (s != newS || v != newV)
	{
		s = newS;
		v = newV;
		colour = Colour (h, s, v, colour.getFloatAlpha());
		update();
	}
}

void ColourSelector::updateHSV()
{
	colour.getHSB (h, s, v);
}

void ColourSelector::update()
{
	if (sliders[0] != 0)
	{
		sliders[0]->setValue ((int) colour.getRed());
		sliders[1]->setValue ((int) colour.getGreen());
		sliders[2]->setValue ((int) colour.getBlue());
		sliders[3]->setValue ((int) colour.getAlpha());
	}

	if (colourSpace != 0)
	{
		colourSpace->updateIfNeeded();
		hueSelector->updateIfNeeded();
	}

	if ((flags & showColourAtTop) != 0)
		repaint (0, edgeGap, getWidth(), topSpace - edgeGap);

	sendChangeMessage (this);
}

void ColourSelector::paint (Graphics& g)
{
	g.fillAll (findColour (backgroundColourId));

	if ((flags & showColourAtTop) != 0)
	{
		const Colour colour (getCurrentColour());

		g.fillCheckerBoard (edgeGap, edgeGap, getWidth() - edgeGap - edgeGap, topSpace - edgeGap - edgeGap,
							10, 10,
							Colour (0xffdddddd).overlaidWith (colour),
							Colour (0xffffffff).overlaidWith (colour));

		g.setColour (Colours::white.overlaidWith (colour).contrasting());
		g.setFont (14.0f, true);
		g.drawText (colour.toDisplayString ((flags & showAlphaChannel) != 0),
					0, edgeGap, getWidth(), topSpace - edgeGap * 2,
					Justification::centred, false);
	}

	if ((flags & showSliders) != 0)
	{
		g.setColour (findColour (labelTextColourId));
		g.setFont (11.0f);

		for (int i = 4; --i >= 0;)
		{
			if (sliders[i]->isVisible())
				g.drawText (sliders[i]->getName() + ":",
							0, sliders[i]->getY(),
							sliders[i]->getX() - 8, sliders[i]->getHeight(),
							Justification::centredRight, false);
		}
	}
}

void ColourSelector::resized()
{
	const int numSliders = ((flags & showAlphaChannel) != 0) ? 4 : 3;
	const int numSwatches = getNumSwatches();

	const int swatchSpace = numSwatches > 0 ? edgeGap + swatchHeight * ((numSwatches + 7) / swatchesPerRow) : 0;
	const int sliderSpace = ((flags & showSliders) != 0)  ? jmin (22 * numSliders + edgeGap, proportionOfHeight (0.3f)) : 0;
	topSpace = ((flags & showColourAtTop) != 0) ? jmin (30 + edgeGap * 2, proportionOfHeight (0.2f)) : edgeGap;

	int y = topSpace;

	if ((flags & showColourspace) != 0)
	{
		const int hueWidth = jmin (50, proportionOfWidth (0.15f));

		colourSpace->setBounds (edgeGap, y,
								getWidth() - hueWidth - edgeGap - 4,
								getHeight() - topSpace - sliderSpace - swatchSpace - edgeGap);

		hueSelector->setBounds (colourSpace->getRight() + 4, y,
								getWidth() - edgeGap - (colourSpace->getRight() + 4),
								colourSpace->getHeight());

		y = getHeight() - sliderSpace - swatchSpace - edgeGap;
	}

	if ((flags & showSliders) != 0)
	{
		const int sliderHeight = jmax (4, sliderSpace / numSliders);

		for (int i = 0; i < numSliders; ++i)
		{
			sliders[i]->setBounds (proportionOfWidth (0.2f), y,
								   proportionOfWidth (0.72f), sliderHeight - 2);

			y += sliderHeight;
		}
	}

	if (numSwatches > 0)
	{
		const int startX = 8;
		const int xGap = 4;
		const int yGap = 4;
		const int swatchWidth = (getWidth() - startX * 2) / swatchesPerRow;
		y += edgeGap;

		if (swatchComponents.size() != numSwatches)
		{
			swatchComponents.clear();

			for (int i = 0; i < numSwatches; ++i)
			{
				SwatchComponent* const sc = new SwatchComponent (this, i);
				swatchComponents.add (sc);
				addAndMakeVisible (sc);
			}
		}

		int x = startX;

		for (int i = 0; i < swatchComponents.size(); ++i)
		{
			SwatchComponent* const sc = swatchComponents.getUnchecked(i);

			sc->setBounds (x + xGap / 2,
						   y + yGap / 2,
						   swatchWidth - xGap,
						   swatchHeight - yGap);

			if (((i + 1) % swatchesPerRow) == 0)
			{
				x = startX;
				y += swatchHeight;
			}
			else
			{
				x += swatchWidth;
			}
		}
	}
}

void ColourSelector::sliderValueChanged (Slider*)
{
	if (sliders[0] != 0)
		setCurrentColour (Colour ((uint8) sliders[0]->getValue(),
								  (uint8) sliders[1]->getValue(),
								  (uint8) sliders[2]->getValue(),
								  (uint8) sliders[3]->getValue()));
}

int ColourSelector::getNumSwatches() const
{
	return 0;
}

const Colour ColourSelector::getSwatchColour (const int) const
{
	jassertfalse // if you've overridden getNumSwatches(), you also need to implement this method
	return Colours::black;
}

void ColourSelector::setSwatchColour (const int, const Colour&) const
{
	jassertfalse // if you've overridden getNumSwatches(), you also need to implement this method
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ColourSelector.cpp ***/


/*** Start of inlined file: juce_DropShadower.cpp ***/
BEGIN_JUCE_NAMESPACE

class ShadowWindow  : public Component
{
	Component* owner;
	Image** shadowImageSections;
	const int type;   // 0 = left, 1 = right, 2 = top, 3 = bottom. left + right are full-height

public:
	ShadowWindow (Component* const owner_,
				  const int type_,
				  Image** const shadowImageSections_)
		: owner (owner_),
		  shadowImageSections (shadowImageSections_),
		  type (type_)
	{
		setInterceptsMouseClicks (false, false);

		if (owner_->isOnDesktop())
		{
			setSize (1, 1); // to keep the OS happy by not having zero-size windows
			addToDesktop (ComponentPeer::windowIgnoresMouseClicks
							| ComponentPeer::windowIsTemporary
							| ComponentPeer::windowIgnoresKeyPresses);
		}
		else if (owner_->getParentComponent() != 0)
		{
			owner_->getParentComponent()->addChildComponent (this);
		}
	}

	~ShadowWindow()
	{
	}

	void paint (Graphics& g)
	{
		Image* const topLeft	  = shadowImageSections [type * 3];
		Image* const bottomRight  = shadowImageSections [type * 3 + 1];
		Image* const filler	   = shadowImageSections [type * 3 + 2];

		g.setOpacity (1.0f);

		if (type < 2)
		{
			int imH = jmin (topLeft->getHeight(), getHeight() / 2);
			g.drawImage (topLeft,
						 0, 0, topLeft->getWidth(), imH,
						 0, 0, topLeft->getWidth(), imH);

			imH = jmin (bottomRight->getHeight(), getHeight() - getHeight() / 2);
			g.drawImage (bottomRight,
						 0, getHeight() - imH, bottomRight->getWidth(), imH,
						 0, bottomRight->getHeight() - imH, bottomRight->getWidth(), imH);

			g.setTiledImageFill (*filler, 0, 0, 1.0f);
			g.fillRect (0, topLeft->getHeight(), getWidth(), getHeight() - (topLeft->getHeight() + bottomRight->getHeight()));
		}
		else
		{
			int imW = jmin (topLeft->getWidth(), getWidth() / 2);
			g.drawImage (topLeft,
						 0, 0, imW, topLeft->getHeight(),
						 0, 0, imW, topLeft->getHeight());

			imW = jmin (bottomRight->getWidth(), getWidth() - getWidth() / 2);
			g.drawImage (bottomRight,
						 getWidth() - imW, 0, imW, bottomRight->getHeight(),
						 bottomRight->getWidth() - imW, 0, imW, bottomRight->getHeight());

			g.setTiledImageFill (*filler, 0, 0, 1.0f);
			g.fillRect (topLeft->getWidth(), 0, getWidth() - (topLeft->getWidth() + bottomRight->getWidth()), getHeight());
		}
	}

	void resized()
	{
		repaint();  // (needed for correct repainting)
	}

private:
	ShadowWindow (const ShadowWindow&);
	ShadowWindow& operator= (const ShadowWindow&);
};

DropShadower::DropShadower (const float alpha_,
							const int xOffset_,
							const int yOffset_,
							const float blurRadius_)
   : owner (0),
	 numShadows (0),
	 shadowEdge (jmax (xOffset_, yOffset_) + (int) blurRadius_),
	 xOffset (xOffset_),
	 yOffset (yOffset_),
	 alpha (alpha_),
	 blurRadius (blurRadius_),
	 inDestructor (false),
	 reentrant (false)
{
}

DropShadower::~DropShadower()
{
	if (owner != 0)
		owner->removeComponentListener (this);

	inDestructor = true;

	deleteShadowWindows();
}

void DropShadower::deleteShadowWindows()
{
	if (numShadows > 0)
	{
		int i;
		for (i = numShadows; --i >= 0;)
			delete shadowWindows[i];

		for (i = 12; --i >= 0;)
			delete shadowImageSections[i];

		numShadows = 0;
	}
}

void DropShadower::setOwner (Component* componentToFollow)
{
	if (componentToFollow != owner)
	{
		if (owner != 0)
			owner->removeComponentListener (this);

		// (the component can't be null)
		jassert (componentToFollow != 0);

		owner = componentToFollow;

		jassert (owner != 0);
		jassert (owner->isOpaque()); // doesn't work properly for semi-transparent comps!

		owner->addComponentListener (this);

		updateShadows();
	}
}

void DropShadower::componentMovedOrResized (Component&, bool /*wasMoved*/, bool /*wasResized*/)
{
	updateShadows();
}

void DropShadower::componentBroughtToFront (Component&)
{
	bringShadowWindowsToFront();
}

void DropShadower::componentChildrenChanged (Component&)
{
}

void DropShadower::componentParentHierarchyChanged (Component&)
{
	deleteShadowWindows();
	updateShadows();
}

void DropShadower::componentVisibilityChanged (Component&)
{
	updateShadows();
}

void DropShadower::updateShadows()
{
	if (reentrant || inDestructor || (owner == 0))
		return;

	reentrant = true;

	ComponentPeer* const nw = owner->getPeer();

	const bool isOwnerVisible = owner->isVisible()
								 && (nw == 0 || ! nw->isMinimised());

	const bool createShadowWindows  = numShadows == 0
										 && owner->getWidth() > 0
										 && owner->getHeight() > 0
										 && isOwnerVisible
										 && (Desktop::canUseSemiTransparentWindows()
											  || owner->getParentComponent() != 0);

	if (createShadowWindows)
	{
		// keep a cached version of the image to save doing the gaussian too often
		String imageId;
		imageId << shadowEdge << ',' << xOffset << ',' << yOffset << ',' << alpha;

		const int hash = imageId.hashCode();

		Image* bigIm = ImageCache::getFromHashCode (hash);

		if (bigIm == 0)
		{
			bigIm = Image::createNativeImage (Image::ARGB, shadowEdge * 5, shadowEdge * 5, true);

			Graphics bigG (*bigIm);
			bigG.setColour (Colours::black.withAlpha (alpha));
			bigG.fillRect (shadowEdge + xOffset,
						   shadowEdge + yOffset,
						   bigIm->getWidth() - (shadowEdge * 2),
						   bigIm->getHeight() - (shadowEdge * 2));

			ImageConvolutionKernel blurKernel (roundToInt (blurRadius * 2.0f));
			blurKernel.createGaussianBlur (blurRadius);

			blurKernel.applyToImage (*bigIm, 0,
									 Rectangle<int> (xOffset, yOffset,
													 bigIm->getWidth(), bigIm->getHeight()));

			ImageCache::addImageToCache (bigIm, hash);
		}

		const int iw = bigIm->getWidth();
		const int ih = bigIm->getHeight();
		const int shadowEdge2 = shadowEdge * 2;

		setShadowImage (bigIm, 0, shadowEdge, shadowEdge2, 0, 0);
		setShadowImage (bigIm, 1, shadowEdge, shadowEdge2, 0, ih - shadowEdge2);
		setShadowImage (bigIm, 2, shadowEdge, shadowEdge, 0, shadowEdge2);
		setShadowImage (bigIm, 3, shadowEdge, shadowEdge2, iw - shadowEdge, 0);
		setShadowImage (bigIm, 4, shadowEdge, shadowEdge2, iw - shadowEdge, ih - shadowEdge2);
		setShadowImage (bigIm, 5, shadowEdge, shadowEdge, iw - shadowEdge, shadowEdge2);
		setShadowImage (bigIm, 6, shadowEdge, shadowEdge, shadowEdge, 0);
		setShadowImage (bigIm, 7, shadowEdge, shadowEdge, iw - shadowEdge2, 0);
		setShadowImage (bigIm, 8, shadowEdge, shadowEdge, shadowEdge2, 0);
		setShadowImage (bigIm, 9, shadowEdge, shadowEdge, shadowEdge, ih - shadowEdge);
		setShadowImage (bigIm, 10, shadowEdge, shadowEdge, iw - shadowEdge2, ih - shadowEdge);
		setShadowImage (bigIm, 11, shadowEdge, shadowEdge, shadowEdge2, ih - shadowEdge);

		ImageCache::release (bigIm);

		for (int i = 0; i < 4; ++i)
		{
			shadowWindows[numShadows] = new ShadowWindow (owner, i, shadowImageSections);
			++numShadows;
		}
	}

	if (numShadows > 0)
	{
		for (int i = numShadows; --i >= 0;)
		{
			shadowWindows[i]->setAlwaysOnTop (owner->isAlwaysOnTop());
			shadowWindows[i]->setVisible (isOwnerVisible);
		}

		const int x = owner->getX();
		const int y = owner->getY() - shadowEdge;
		const int w = owner->getWidth();
		const int h = owner->getHeight() + shadowEdge + shadowEdge;

		shadowWindows[0]->setBounds (x - shadowEdge,
									 y,
									 shadowEdge,
									 h);

		shadowWindows[1]->setBounds (x + w,
									 y,
									 shadowEdge,
									 h);

		shadowWindows[2]->setBounds (x,
									 y,
									 w,
									 shadowEdge);

		shadowWindows[3]->setBounds (x,
									 owner->getBottom(),
									 w,
									 shadowEdge);
	}

	reentrant = false;

	if (createShadowWindows)
		bringShadowWindowsToFront();
}

void DropShadower::setShadowImage (Image* const src, const int num, const int w, const int h,
								   const int sx, const int sy)
{
	shadowImageSections[num] = new Image (Image::ARGB, w, h, true);

	Graphics g (*shadowImageSections[num]);
	g.drawImage (src, 0, 0, w, h, sx, sy, w, h);
}

void DropShadower::bringShadowWindowsToFront()
{
	if (! (inDestructor || reentrant))
	{
		updateShadows();

		reentrant = true;

		for (int i = numShadows; --i >= 0;)
			shadowWindows[i]->toBehind (owner);

		reentrant = false;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DropShadower.cpp ***/


/*** Start of inlined file: juce_MagnifierComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

class MagnifyingPeer	: public ComponentPeer
{
public:

	MagnifyingPeer (Component* const component_,
					MagnifierComponent* const magnifierComp_)
		: ComponentPeer (component_, 0),
		  magnifierComp (magnifierComp_)
	{
	}

	~MagnifyingPeer()
	{
	}

	void* getNativeHandle() const		   { return 0; }
	void setVisible (bool)			  {}
	void setTitle (const String&)		   {}
	void setPosition (int, int)			 {}
	void setSize (int, int)			 {}
	void setBounds (int, int, int, int, bool)	   {}
	void setMinimised (bool)			{}
	bool isMinimised() const			{ return false; }
	void setFullScreen (bool)			   {}
	bool isFullScreen() const			   { return false; }
	const BorderSize getFrameSize() const	   { return BorderSize (0); }
	bool setAlwaysOnTop (bool)			  { return true; }
	void toFront (bool)				 {}
	void toBehind (ComponentPeer*)		  {}
	void setIcon (const Image&)			 {}

	bool isFocused() const
	{
		return magnifierComp->hasKeyboardFocus (true);
	}

	void grabFocus()
	{
		ComponentPeer* peer = magnifierComp->getPeer();
		if (peer != 0)
			peer->grabFocus();
	}

	void textInputRequired (const Point<int>& position)
	{
		ComponentPeer* peer = magnifierComp->getPeer();
		if (peer != 0)
			peer->textInputRequired (position);
	}

	const Rectangle<int> getBounds() const
	{
		return Rectangle<int> (magnifierComp->getScreenX(), magnifierComp->getScreenY(),
							   component->getWidth(), component->getHeight());
	}

	const Point<int> getScreenPosition() const
	{
		return magnifierComp->getScreenPosition();
	}

	const Point<int> relativePositionToGlobal (const Point<int>& relativePosition)
	{
		const double zoom = magnifierComp->getScaleFactor();
		return magnifierComp->relativePositionToGlobal (Point<int> (roundToInt (relativePosition.getX() * zoom),
																	roundToInt (relativePosition.getY() * zoom)));
	}

	const Point<int> globalPositionToRelative (const Point<int>& screenPosition)
	{
		const Point<int> p (magnifierComp->globalPositionToRelative (screenPosition));
		const double zoom = magnifierComp->getScaleFactor();

		return Point<int> (roundToInt (p.getX() / zoom),
						   roundToInt (p.getY() / zoom));
	}

	bool contains (const Point<int>& position, bool) const
	{
		return ((unsigned int) position.getX()) < (unsigned int) magnifierComp->getWidth()
				&& ((unsigned int) position.getY()) < (unsigned int) magnifierComp->getHeight();
	}

	void repaint (int x, int y, int w, int h)
	{
		const double zoom = magnifierComp->getScaleFactor();

		magnifierComp->repaint ((int) (x * zoom),
								(int) (y * zoom),
								roundToInt (w * zoom) + 1,
								roundToInt (h * zoom) + 1);
	}

	void performAnyPendingRepaintsNow()
	{
	}

	juce_UseDebuggingNewOperator

private:
	MagnifierComponent* const magnifierComp;

	MagnifyingPeer (const MagnifyingPeer&);
	MagnifyingPeer& operator= (const MagnifyingPeer&);
};

class PeerHolderComp	: public Component
{
public:
	PeerHolderComp (MagnifierComponent* const magnifierComp_)
		: magnifierComp (magnifierComp_)
	{
		setVisible (true);
	}

	~PeerHolderComp()
	{
	}

	ComponentPeer* createNewPeer (int, void*)
	{
		return new MagnifyingPeer (this, magnifierComp);
	}

	void childBoundsChanged (Component* c)
	{
		if (c != 0)
		{
			setSize (c->getWidth(), c->getHeight());
			magnifierComp->childBoundsChanged (this);
		}
	}

	void mouseWheelMove (const MouseEvent& e, float ix, float iy)
	{
		// unhandled mouse wheel moves can be referred upwards to the parent comp..
		Component* const p = magnifierComp->getParentComponent();

		if (p != 0)
			p->mouseWheelMove (e.getEventRelativeTo (p), ix, iy);
	}

private:
	MagnifierComponent* const magnifierComp;

	PeerHolderComp (const PeerHolderComp&);
	PeerHolderComp& operator= (const PeerHolderComp&);
};

MagnifierComponent::MagnifierComponent (Component* const content_,
										const bool deleteContentCompWhenNoLongerNeeded)
	: content (content_),
	  scaleFactor (0.0),
	  peer (0),
	  deleteContent (deleteContentCompWhenNoLongerNeeded),
	  quality (Graphics::lowResamplingQuality),
	  mouseSource (0, true)
{
	holderComp = new PeerHolderComp (this);
	setScaleFactor (1.0);
}

MagnifierComponent::~MagnifierComponent()
{
	delete holderComp;

	if (deleteContent)
		delete content;
}

void MagnifierComponent::setScaleFactor (double newScaleFactor)
{
	jassert (newScaleFactor > 0.0);  // hmm - unlikely to work well with a negative scale factor

	newScaleFactor = jlimit (1.0 / 8.0, 1000.0, newScaleFactor);

	if (scaleFactor != newScaleFactor)
	{
		scaleFactor = newScaleFactor;

		if (scaleFactor == 1.0)
		{
			holderComp->removeFromDesktop();
			peer = 0;
			addChildComponent (content);
			childBoundsChanged (content);
		}
		else
		{
			holderComp->addAndMakeVisible (content);
			holderComp->childBoundsChanged (content);
			childBoundsChanged (holderComp);
			holderComp->addToDesktop (0);
			peer = holderComp->getPeer();
		}

		repaint();
	}
}

void MagnifierComponent::setResamplingQuality (Graphics::ResamplingQuality newQuality)
{
	quality = newQuality;
}

void MagnifierComponent::paint (Graphics& g)
{
	const int w = holderComp->getWidth();
	const int h = holderComp->getHeight();

	if (w == 0 || h == 0)
		return;

	const Rectangle<int> r (g.getClipBounds());

	const int srcX = (int) (r.getX() / scaleFactor);
	const int srcY = (int) (r.getY() / scaleFactor);
	int srcW = roundToInt (r.getRight() / scaleFactor) - srcX;
	int srcH = roundToInt (r.getBottom() / scaleFactor) - srcY;

	if (scaleFactor >= 1.0)
	{
		++srcW;
		++srcH;
	}

	Image temp (Image::ARGB, jmax (w, srcX + srcW), jmax (h, srcY + srcH), false);
	temp.clear (srcX, srcY, srcW, srcH);

	{
		Graphics g2 (temp);
		g2.reduceClipRegion (srcX, srcY, srcW, srcH);
		holderComp->paintEntireComponent (g2);
	}

	g.setImageResamplingQuality (quality);
	g.drawImageTransformed (&temp, temp.getBounds(),
							AffineTransform::scale ((float) scaleFactor, (float) scaleFactor),
							false);
}

void MagnifierComponent::childBoundsChanged (Component* c)
{
	if (c != 0)
		setSize (roundToInt (c->getWidth() * scaleFactor),
				 roundToInt (c->getHeight() * scaleFactor));
}

void MagnifierComponent::passOnMouseEventToPeer (const MouseEvent& e)
{
	if (peer != 0)
		mouseSource.handleEvent (peer, Point<int> (scaleInt (e.x), scaleInt (e.y)),
								 e.eventTime.toMilliseconds(), ModifierKeys::getCurrentModifiers());
}

void MagnifierComponent::mouseDown (const MouseEvent& e)
{
	passOnMouseEventToPeer (e);
}

void MagnifierComponent::mouseUp (const MouseEvent& e)
{
	passOnMouseEventToPeer (e);
}

void MagnifierComponent::mouseDrag (const MouseEvent& e)
{
	passOnMouseEventToPeer (e);
}

void MagnifierComponent::mouseMove (const MouseEvent& e)
{
	passOnMouseEventToPeer (e);
}

void MagnifierComponent::mouseEnter (const MouseEvent& e)
{
	passOnMouseEventToPeer (e);
}

void MagnifierComponent::mouseExit (const MouseEvent& e)
{
	passOnMouseEventToPeer (e);
}

void MagnifierComponent::mouseWheelMove (const MouseEvent& e, float ix, float iy)
{
	if (peer != 0)
		peer->handleMouseWheel (e.source.getIndex(),
								Point<int> (scaleInt (e.x), scaleInt (e.y)), e.eventTime.toMilliseconds(),
								ix * 256.0f, iy * 256.0f);
	else
		Component::mouseWheelMove (e, ix, iy);
}

int MagnifierComponent::scaleInt (const int n) const
{
	return roundToInt (n / scaleFactor);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MagnifierComponent.cpp ***/


/*** Start of inlined file: juce_MidiKeyboardComponent.cpp ***/
BEGIN_JUCE_NAMESPACE

class MidiKeyboardUpDownButton  : public Button
{
public:
	MidiKeyboardUpDownButton (MidiKeyboardComponent* const owner_,
							  const int delta_)
		: Button (String::empty),
		  owner (owner_),
		  delta (delta_)
	{
		setOpaque (true);
	}

	~MidiKeyboardUpDownButton()
	{
	}

	void clicked()
	{
		int note = owner->getLowestVisibleKey();

		if (delta < 0)
			note = (note - 1) / 12;
		else
			note = note / 12 + 1;

		owner->setLowestVisibleKey (note * 12);
	}

	void paintButton (Graphics& g,
					  bool isMouseOverButton,
					  bool isButtonDown)
	{
		owner->drawUpDownButton (g, getWidth(), getHeight(),
								 isMouseOverButton, isButtonDown,
								 delta > 0);
	}

private:
	MidiKeyboardComponent* const owner;
	const int delta;

	MidiKeyboardUpDownButton (const MidiKeyboardUpDownButton&);
	MidiKeyboardUpDownButton& operator= (const MidiKeyboardUpDownButton&);
};

MidiKeyboardComponent::MidiKeyboardComponent (MidiKeyboardState& state_,
											  const Orientation orientation_)
	: state (state_),
	  xOffset (0),
	  blackNoteLength (1),
	  keyWidth (16.0f),
	  orientation (orientation_),
	  midiChannel (1),
	  midiInChannelMask (0xffff),
	  velocity (1.0f),
	  noteUnderMouse (-1),
	  mouseDownNote (-1),
	  rangeStart (0),
	  rangeEnd (127),
	  firstKey (12 * 4),
	  canScroll (true),
	  mouseDragging (false),
	  useMousePositionForVelocity (true),
	  keyMappingOctave (6),
	  octaveNumForMiddleC (3)
{
	addChildComponent (scrollDown = new MidiKeyboardUpDownButton (this, -1));
	addChildComponent (scrollUp   = new MidiKeyboardUpDownButton (this, 1));

	// initialise with a default set of querty key-mappings..
	const char* const keymap = "awsedftgyhujkolp;";

	for (int i = String (keymap).length(); --i >= 0;)
		setKeyPressForNote (KeyPress (keymap[i], 0, 0), i);

	setOpaque (true);
	setWantsKeyboardFocus (true);

	state.addListener (this);
}

MidiKeyboardComponent::~MidiKeyboardComponent()
{
	state.removeListener (this);
	jassert (mouseDownNote < 0 && keysPressed.countNumberOfSetBits() == 0); // leaving stuck notes!

	deleteAllChildren();
}

void MidiKeyboardComponent::setKeyWidth (const float widthInPixels)
{
	keyWidth = widthInPixels;
	resized();
}

void MidiKeyboardComponent::setOrientation (const Orientation newOrientation)
{
	if (orientation != newOrientation)
	{
		orientation = newOrientation;
		resized();
	}
}

void MidiKeyboardComponent::setAvailableRange (const int lowestNote,
											   const int highestNote)
{
	jassert (lowestNote >= 0 && lowestNote <= 127);
	jassert (highestNote >= 0 && highestNote <= 127);
	jassert (lowestNote <= highestNote);

	if (rangeStart != lowestNote || rangeEnd != highestNote)
	{
		rangeStart = jlimit (0, 127, lowestNote);
		rangeEnd = jlimit (0, 127, highestNote);
		firstKey = jlimit (rangeStart, rangeEnd, firstKey);
		resized();
	}
}

void MidiKeyboardComponent::setLowestVisibleKey (int noteNumber)
{
	noteNumber = jlimit (rangeStart, rangeEnd, noteNumber);

	if (noteNumber != firstKey)
	{
		firstKey = noteNumber;
		sendChangeMessage (this);
		resized();
	}
}

void MidiKeyboardComponent::setScrollButtonsVisible (const bool canScroll_)
{
	if (canScroll != canScroll_)
	{
		canScroll = canScroll_;
		resized();
	}
}

void MidiKeyboardComponent::colourChanged()
{
	repaint();
}

void MidiKeyboardComponent::setMidiChannel (const int midiChannelNumber)
{
	jassert (midiChannelNumber > 0 && midiChannelNumber <= 16);

	if (midiChannel != midiChannelNumber)
	{
		resetAnyKeysInUse();
		midiChannel = jlimit (1, 16, midiChannelNumber);
	}
}

void MidiKeyboardComponent::setMidiChannelsToDisplay (const int midiChannelMask)
{
	midiInChannelMask = midiChannelMask;
	triggerAsyncUpdate();
}

void MidiKeyboardComponent::setVelocity (const float velocity_, const bool useMousePositionForVelocity_)
{
	velocity = jlimit (0.0f, 1.0f, velocity_);
	useMousePositionForVelocity = useMousePositionForVelocity_;
}

void MidiKeyboardComponent::getKeyPosition (int midiNoteNumber, const float keyWidth, int& x, int& w) const
{
	jassert (midiNoteNumber >= 0 && midiNoteNumber < 128);

	static const float blackNoteWidth = 0.7f;

	static const float notePos[] = { 0.0f, 1 - blackNoteWidth * 0.6f,
									 1.0f, 2 - blackNoteWidth * 0.4f,
									 2.0f, 3.0f, 4 - blackNoteWidth * 0.7f,
									 4.0f, 5 - blackNoteWidth * 0.5f,
									 5.0f, 6 - blackNoteWidth * 0.3f,
									 6.0f };

	static const float widths[] = { 1.0f, blackNoteWidth,
									1.0f, blackNoteWidth,
									1.0f, 1.0f, blackNoteWidth,
									1.0f, blackNoteWidth,
									1.0f, blackNoteWidth,
									1.0f };

	const int octave = midiNoteNumber / 12;
	const int note = midiNoteNumber % 12;

	x = roundToInt (octave * 7.0f * keyWidth + notePos [note] * keyWidth);
	w = roundToInt (widths [note] * keyWidth);
}

void MidiKeyboardComponent::getKeyPos (int midiNoteNumber, int& x, int& w) const
{
	getKeyPosition (midiNoteNumber, keyWidth, x, w);

	int rx, rw;
	getKeyPosition (rangeStart, keyWidth, rx, rw);

	x -= xOffset + rx;
}

int MidiKeyboardComponent::getKeyStartPosition (const int midiNoteNumber) const
{
	int x, y;
	getKeyPos (midiNoteNumber, x, y);
	return x;
}

static const uint8 whiteNotes[] = { 0, 2, 4, 5, 7, 9, 11 };
static const uint8 blackNotes[] = { 1, 3, 6, 8, 10 };

int MidiKeyboardComponent::xyToNote (const Point<int>& pos, float& mousePositionVelocity)
{
	if (! reallyContains (pos.getX(), pos.getY(), false))
		return -1;

	Point<int> p (pos);

	if (orientation != horizontalKeyboard)
	{
		p = Point<int> (p.getY(), p.getX());

		if (orientation == verticalKeyboardFacingLeft)
			p = Point<int> (p.getX(), getWidth() - p.getY());
		else
			p = Point<int> (getHeight() - p.getX(), p.getY());
	}

	return remappedXYToNote (p + Point<int> (xOffset, 0), mousePositionVelocity);
}

int MidiKeyboardComponent::remappedXYToNote (const Point<int>& pos, float& mousePositionVelocity) const
{
	if (pos.getY() < blackNoteLength)
	{
		for (int octaveStart = 12 * (rangeStart / 12); octaveStart <= rangeEnd; octaveStart += 12)
		{
			for (int i = 0; i < 5; ++i)
			{
				const int note = octaveStart + blackNotes [i];

				if (note >= rangeStart && note <= rangeEnd)
				{
					int kx, kw;
					getKeyPos (note, kx, kw);
					kx += xOffset;

					if (pos.getX() >= kx && pos.getX() < kx + kw)
					{
						mousePositionVelocity = pos.getY() / (float) blackNoteLength;
						return note;
					}
				}
			}
		}
	}

	for (int octaveStart = 12 * (rangeStart / 12); octaveStart <= rangeEnd; octaveStart += 12)
	{
		for (int i = 0; i < 7; ++i)
		{
			const int note = octaveStart + whiteNotes [i];

			if (note >= rangeStart && note <= rangeEnd)
			{
				int kx, kw;
				getKeyPos (note, kx, kw);
				kx += xOffset;

				if (pos.getX() >= kx && pos.getX() < kx + kw)
				{
					const int whiteNoteLength = (orientation == horizontalKeyboard) ? getHeight() : getWidth();
					mousePositionVelocity = pos.getY() / (float) whiteNoteLength;
					return note;
				}
			}
		}
	}

	mousePositionVelocity = 0;
	return -1;
}

void MidiKeyboardComponent::repaintNote (const int noteNum)
{
	if (noteNum >= rangeStart && noteNum <= rangeEnd)
	{
		int x, w;
		getKeyPos (noteNum, x, w);

		if (orientation == horizontalKeyboard)
			repaint (x, 0, w, getHeight());
		else if (orientation == verticalKeyboardFacingLeft)
			repaint (0, x, getWidth(), w);
		else if (orientation == verticalKeyboardFacingRight)
			repaint (0, getHeight() - x - w, getWidth(), w);
	}
}

void MidiKeyboardComponent::paint (Graphics& g)
{
	g.fillAll (Colours::white.overlaidWith (findColour (whiteNoteColourId)));

	const Colour lineColour (findColour (keySeparatorLineColourId));
	const Colour textColour (findColour (textLabelColourId));

	int x, w, octave;

	for (octave = 0; octave < 128; octave += 12)
	{
		for (int white = 0; white < 7; ++white)
		{
			const int noteNum = octave + whiteNotes [white];

			if (noteNum >= rangeStart && noteNum <= rangeEnd)
			{
				getKeyPos (noteNum, x, w);

				if (orientation == horizontalKeyboard)
					drawWhiteNote (noteNum, g, x, 0, w, getHeight(),
								   state.isNoteOnForChannels (midiInChannelMask, noteNum),
								   noteUnderMouse == noteNum,
								   lineColour, textColour);
				else if (orientation == verticalKeyboardFacingLeft)
					drawWhiteNote (noteNum, g, 0, x, getWidth(), w,
								   state.isNoteOnForChannels (midiInChannelMask, noteNum),
								   noteUnderMouse == noteNum,
								   lineColour, textColour);
				else if (orientation == verticalKeyboardFacingRight)
					drawWhiteNote (noteNum, g, 0, getHeight() - x - w, getWidth(), w,
								   state.isNoteOnForChannels (midiInChannelMask, noteNum),
								   noteUnderMouse == noteNum,
								   lineColour, textColour);
			}
		}
	}

	float x1 = 0.0f, y1 = 0.0f, x2 = 0.0f, y2 = 0.0f;

	if (orientation == verticalKeyboardFacingLeft)
	{
		x1 = getWidth() - 1.0f;
		x2 = getWidth() - 5.0f;
	}
	else if (orientation == verticalKeyboardFacingRight)
		x2 = 5.0f;
	else
		y2 = 5.0f;

	g.setGradientFill (ColourGradient (Colours::black.withAlpha (0.3f), x1, y1,
									   Colours::transparentBlack, x2, y2, false));

	getKeyPos (rangeEnd, x, w);
	x += w;

	if (orientation == verticalKeyboardFacingLeft)
		g.fillRect (getWidth() - 5, 0, 5, x);
	else if (orientation == verticalKeyboardFacingRight)
		g.fillRect (0, 0, 5, x);
	else
		g.fillRect (0, 0, x, 5);

	g.setColour (lineColour);

	if (orientation == verticalKeyboardFacingLeft)
		g.fillRect (0, 0, 1, x);
	else if (orientation == verticalKeyboardFacingRight)
		g.fillRect (getWidth() - 1, 0, 1, x);
	else
		g.fillRect (0, getHeight() - 1, x, 1);

	const Colour blackNoteColour (findColour (blackNoteColourId));

	for (octave = 0; octave < 128; octave += 12)
	{
		for (int black = 0; black < 5; ++black)
		{
			const int noteNum = octave + blackNotes [black];

			if (noteNum >= rangeStart && noteNum <= rangeEnd)
			{
				getKeyPos (noteNum, x, w);

				if (orientation == horizontalKeyboard)
					drawBlackNote (noteNum, g, x, 0, w, blackNoteLength,
								   state.isNoteOnForChannels (midiInChannelMask, noteNum),
								   noteUnderMouse == noteNum,
								   blackNoteColour);
				else if (orientation == verticalKeyboardFacingLeft)
					drawBlackNote (noteNum, g, getWidth() - blackNoteLength, x, blackNoteLength, w,
								   state.isNoteOnForChannels (midiInChannelMask, noteNum),
								   noteUnderMouse == noteNum,
								   blackNoteColour);
				else if (orientation == verticalKeyboardFacingRight)
					drawBlackNote (noteNum, g, 0, getHeight() - x - w, blackNoteLength, w,
								   state.isNoteOnForChannels (midiInChannelMask, noteNum),
								   noteUnderMouse == noteNum,
								   blackNoteColour);
			}
		}
	}
}

void MidiKeyboardComponent::drawWhiteNote (int midiNoteNumber,
										   Graphics& g, int x, int y, int w, int h,
										   bool isDown, bool isOver,
										   const Colour& lineColour,
										   const Colour& textColour)
{
	Colour c (Colours::transparentWhite);

	if (isDown)
		c = findColour (keyDownOverlayColourId);

	if (isOver)
		c = c.overlaidWith (findColour (mouseOverKeyOverlayColourId));

	g.setColour (c);
	g.fillRect (x, y, w, h);

	const String text (getWhiteNoteText (midiNoteNumber));

	if (! text.isEmpty())
	{
		g.setColour (textColour);

		Font f (jmin (12.0f, keyWidth * 0.9f));
		f.setHorizontalScale (0.8f);
		g.setFont (f);
		Justification justification (Justification::centredBottom);

		if (orientation == verticalKeyboardFacingLeft)
			justification = Justification::centredLeft;
		else if (orientation == verticalKeyboardFacingRight)
			justification = Justification::centredRight;

		g.drawFittedText (text, x + 2, y + 2, w - 4, h - 4, justification, 1);
	}

	g.setColour (lineColour);

	if (orientation == horizontalKeyboard)
		g.fillRect (x, y, 1, h);
	else if (orientation == verticalKeyboardFacingLeft)
		g.fillRect (x, y, w, 1);
	else if (orientation == verticalKeyboardFacingRight)
		g.fillRect (x, y + h - 1, w, 1);

	if (midiNoteNumber == rangeEnd)
	{
		if (orientation == horizontalKeyboard)
			g.fillRect (x + w, y, 1, h);
		else if (orientation == verticalKeyboardFacingLeft)
			g.fillRect (x, y + h, w, 1);
		else if (orientation == verticalKeyboardFacingRight)
			g.fillRect (x, y - 1, w, 1);
	}
}

void MidiKeyboardComponent::drawBlackNote (int /*midiNoteNumber*/,
										   Graphics& g, int x, int y, int w, int h,
										   bool isDown, bool isOver,
										   const Colour& noteFillColour)
{
	Colour c (noteFillColour);

	if (isDown)
		c = c.overlaidWith (findColour (keyDownOverlayColourId));

	if (isOver)
		c = c.overlaidWith (findColour (mouseOverKeyOverlayColourId));

	g.setColour (c);
	g.fillRect (x, y, w, h);

	if (isDown)
	{
		g.setColour (noteFillColour);
		g.drawRect (x, y, w, h);
	}
	else
	{
		const int xIndent = jmax (1, jmin (w, h) / 8);

		g.setColour (c.brighter());

		if (orientation == horizontalKeyboard)
			g.fillRect (x + xIndent, y, w - xIndent * 2, 7 * h / 8);
		else if (orientation == verticalKeyboardFacingLeft)
			g.fillRect (x + w / 8, y + xIndent, w - w / 8, h - xIndent * 2);
		else if (orientation == verticalKeyboardFacingRight)
			g.fillRect (x, y + xIndent, 7 * w / 8, h - xIndent * 2);
	}
}

void MidiKeyboardComponent::setOctaveForMiddleC (const int octaveNumForMiddleC_) throw()
{
	octaveNumForMiddleC = octaveNumForMiddleC_;
	repaint();
}

const String MidiKeyboardComponent::getWhiteNoteText (const int midiNoteNumber)
{
	if (keyWidth > 14.0f && midiNoteNumber % 12 == 0)
		return MidiMessage::getMidiNoteName (midiNoteNumber, true, true, octaveNumForMiddleC);

	return String::empty;
}

void MidiKeyboardComponent::drawUpDownButton (Graphics& g, int w, int h,
											  const bool isMouseOver,
											  const bool isButtonDown,
											  const bool movesOctavesUp)
{
	g.fillAll (findColour (upDownButtonBackgroundColourId));

	float angle;

	if (orientation == MidiKeyboardComponent::horizontalKeyboard)
		angle = movesOctavesUp ? 0.0f : 0.5f;
	else if (orientation == MidiKeyboardComponent::verticalKeyboardFacingLeft)
		angle = movesOctavesUp ? 0.25f : 0.75f;
	else
		angle = movesOctavesUp ? 0.75f : 0.25f;

	Path path;
	path.lineTo (0.0f, 1.0f);
	path.lineTo (1.0f, 0.5f);
	path.closeSubPath();

	path.applyTransform (AffineTransform::rotation (float_Pi * 2.0f * angle, 0.5f, 0.5f));

	g.setColour (findColour (upDownButtonArrowColourId)
				  .withAlpha (isButtonDown ? 1.0f : (isMouseOver ? 0.6f : 0.4f)));

	g.fillPath (path, path.getTransformToScaleToFit (1.0f, 1.0f,
													 w - 2.0f,
													 h - 2.0f,
													 true));
}

void MidiKeyboardComponent::resized()
{
	int w = getWidth();
	int h = getHeight();

	if (w > 0 && h > 0)
	{
		if (orientation != horizontalKeyboard)
			swapVariables (w, h);

		blackNoteLength = roundToInt (h * 0.7f);

		int kx2, kw2;
		getKeyPos (rangeEnd, kx2, kw2);

		kx2 += kw2;

		if (firstKey != rangeStart)
		{
			int kx1, kw1;
			getKeyPos (rangeStart, kx1, kw1);

			if (kx2 - kx1 <= w)
			{
				firstKey = rangeStart;
				sendChangeMessage (this);
				repaint();
			}
		}

		const bool showScrollButtons = canScroll && (firstKey > rangeStart || kx2 > w + xOffset * 2);

		scrollDown->setVisible (showScrollButtons);
		scrollUp->setVisible (showScrollButtons);

		xOffset = 0;

		if (showScrollButtons)
		{
			const int scrollButtonW = jmin (12, w / 2);

			if (orientation == horizontalKeyboard)
			{
				scrollDown->setBounds (0, 0, scrollButtonW, getHeight());
				scrollUp->setBounds (getWidth() - scrollButtonW, 0, scrollButtonW, getHeight());
			}
			else if (orientation == verticalKeyboardFacingLeft)
			{
				scrollDown->setBounds (0, 0, getWidth(), scrollButtonW);
				scrollUp->setBounds (0, getHeight() - scrollButtonW, getWidth(), scrollButtonW);
			}
			else if (orientation == verticalKeyboardFacingRight)
			{
				scrollDown->setBounds (0, getHeight() - scrollButtonW, getWidth(), scrollButtonW);
				scrollUp->setBounds (0, 0, getWidth(), scrollButtonW);
			}

			int endOfLastKey, kw;
			getKeyPos (rangeEnd, endOfLastKey, kw);
			endOfLastKey += kw;

			float mousePositionVelocity;
			const int spaceAvailable = w - scrollButtonW * 2;
			const int lastStartKey = remappedXYToNote (Point<int> (endOfLastKey - spaceAvailable, 0), mousePositionVelocity) + 1;

			if (lastStartKey >= 0 && firstKey > lastStartKey)
			{
				firstKey = jlimit (rangeStart, rangeEnd, lastStartKey);
				sendChangeMessage (this);
			}

			int newOffset = 0;
			getKeyPos (firstKey, newOffset, kw);
			xOffset = newOffset - scrollButtonW;
		}
		else
		{
			firstKey = rangeStart;
		}

		timerCallback();
		repaint();
	}
}

void MidiKeyboardComponent::handleNoteOn (MidiKeyboardState*, int /*midiChannel*/, int /*midiNoteNumber*/, float /*velocity*/)
{
	triggerAsyncUpdate();
}

void MidiKeyboardComponent::handleNoteOff (MidiKeyboardState*, int /*midiChannel*/, int /*midiNoteNumber*/)
{
	triggerAsyncUpdate();
}

void MidiKeyboardComponent::handleAsyncUpdate()
{
	for (int i = rangeStart; i <= rangeEnd; ++i)
	{
		if (keysCurrentlyDrawnDown[i] != state.isNoteOnForChannels (midiInChannelMask, i))
		{
			keysCurrentlyDrawnDown.setBit (i, state.isNoteOnForChannels (midiInChannelMask, i));
			repaintNote (i);
		}
	}
}

void MidiKeyboardComponent::resetAnyKeysInUse()
{
	if (keysPressed.countNumberOfSetBits() > 0 || mouseDownNote > 0)
	{
		state.allNotesOff (midiChannel);
		keysPressed.clear();
		mouseDownNote = -1;
	}
}

void MidiKeyboardComponent::updateNoteUnderMouse (const Point<int>& pos)
{
	float mousePositionVelocity = 0.0f;
	const int newNote = (mouseDragging || isMouseOver())
							? xyToNote (pos, mousePositionVelocity) : -1;

	if (noteUnderMouse != newNote)
	{
		if (mouseDownNote >= 0)
		{
			state.noteOff (midiChannel, mouseDownNote);
			mouseDownNote = -1;
		}

		if (mouseDragging && newNote >= 0)
		{
			if (! useMousePositionForVelocity)
				mousePositionVelocity = 1.0f;

			state.noteOn (midiChannel, newNote, mousePositionVelocity * velocity);
			mouseDownNote = newNote;
		}

		repaintNote (noteUnderMouse);
		noteUnderMouse = newNote;
		repaintNote (noteUnderMouse);
	}
	else if (mouseDownNote >= 0 && ! mouseDragging)
	{
		state.noteOff (midiChannel, mouseDownNote);
		mouseDownNote = -1;
	}
}

void MidiKeyboardComponent::mouseMove (const MouseEvent& e)
{
	updateNoteUnderMouse (e.getPosition());
	stopTimer();
}

void MidiKeyboardComponent::mouseDrag (const MouseEvent& e)
{
	float mousePositionVelocity;
	const int newNote = xyToNote (e.getPosition(), mousePositionVelocity);

	if (newNote >= 0)
		mouseDraggedToKey (newNote, e);

	updateNoteUnderMouse (e.getPosition());
}

bool MidiKeyboardComponent::mouseDownOnKey (int /*midiNoteNumber*/, const MouseEvent&)
{
	return true;
}

void MidiKeyboardComponent::mouseDraggedToKey (int /*midiNoteNumber*/, const MouseEvent&)
{
}

void MidiKeyboardComponent::mouseDown (const MouseEvent& e)
{
	float mousePositionVelocity;
	const int newNote = xyToNote (e.getPosition(), mousePositionVelocity);
	mouseDragging = false;

	if (newNote >= 0 && mouseDownOnKey (newNote, e))
	{
		repaintNote (noteUnderMouse);
		noteUnderMouse = -1;
		mouseDragging = true;

		updateNoteUnderMouse (e.getPosition());
		startTimer (500);
	}
}

void MidiKeyboardComponent::mouseUp (const MouseEvent& e)
{
	mouseDragging = false;
	updateNoteUnderMouse (e.getPosition());

	stopTimer();
}

void MidiKeyboardComponent::mouseEnter (const MouseEvent& e)
{
	updateNoteUnderMouse (e.getPosition());
}

void MidiKeyboardComponent::mouseExit (const MouseEvent& e)
{
	updateNoteUnderMouse (e.getPosition());
}

void MidiKeyboardComponent::mouseWheelMove (const MouseEvent&, float ix, float iy)
{
	setLowestVisibleKey (getLowestVisibleKey() + roundToInt ((ix != 0 ? ix : iy) * 5.0f));
}

void MidiKeyboardComponent::timerCallback()
{
	updateNoteUnderMouse (getMouseXYRelative());
}

void MidiKeyboardComponent::clearKeyMappings()
{
	resetAnyKeysInUse();
	keyPressNotes.clear();
	keyPresses.clear();
}

void MidiKeyboardComponent::setKeyPressForNote (const KeyPress& key,
												const int midiNoteOffsetFromC)
{
	removeKeyPressForNote (midiNoteOffsetFromC);

	keyPressNotes.add (midiNoteOffsetFromC);
	keyPresses.add (key);
}

void MidiKeyboardComponent::removeKeyPressForNote (const int midiNoteOffsetFromC)
{
	for (int i = keyPressNotes.size(); --i >= 0;)
	{
		if (keyPressNotes.getUnchecked (i) == midiNoteOffsetFromC)
		{
			keyPressNotes.remove (i);
			keyPresses.remove (i);
		}
	}
}

void MidiKeyboardComponent::setKeyPressBaseOctave (const int newOctaveNumber)
{
	jassert (newOctaveNumber >= 0 && newOctaveNumber <= 10);

	keyMappingOctave = newOctaveNumber;
}

bool MidiKeyboardComponent::keyStateChanged (const bool /*isKeyDown*/)
{
	bool keyPressUsed = false;

	for (int i = keyPresses.size(); --i >= 0;)
	{
		const int note = 12 * keyMappingOctave + keyPressNotes.getUnchecked (i);

		if (keyPresses.getReference(i).isCurrentlyDown())
		{
			if (! keysPressed [note])
			{
				keysPressed.setBit (note);
				state.noteOn (midiChannel, note, velocity);
				keyPressUsed = true;
			}
		}
		else
		{
			if (keysPressed [note])
			{
				keysPressed.clearBit (note);
				state.noteOff (midiChannel, note);
				keyPressUsed = true;
			}
		}
	}

	return keyPressUsed;
}

void MidiKeyboardComponent::focusLost (FocusChangeType)
{
	resetAnyKeysInUse();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_MidiKeyboardComponent.cpp ***/


/*** Start of inlined file: juce_OpenGLComponent.cpp ***/
#if JUCE_OPENGL

BEGIN_JUCE_NAMESPACE

extern void juce_glViewport (const int w, const int h);

OpenGLPixelFormat::OpenGLPixelFormat (const int bitsPerRGBComponent,
									  const int alphaBits_,
									  const int depthBufferBits_,
									  const int stencilBufferBits_)
	: redBits (bitsPerRGBComponent),
	  greenBits (bitsPerRGBComponent),
	  blueBits (bitsPerRGBComponent),
	  alphaBits (alphaBits_),
	  depthBufferBits (depthBufferBits_),
	  stencilBufferBits (stencilBufferBits_),
	  accumulationBufferRedBits (0),
	  accumulationBufferGreenBits (0),
	  accumulationBufferBlueBits (0),
	  accumulationBufferAlphaBits (0),
	  fullSceneAntiAliasingNumSamples (0)
{
}

OpenGLPixelFormat::OpenGLPixelFormat (const OpenGLPixelFormat& other)
	: redBits (other.redBits),
	  greenBits (other.greenBits),
	  blueBits (other.blueBits),
	  alphaBits (other.alphaBits),
	  depthBufferBits (other.depthBufferBits),
	  stencilBufferBits (other.stencilBufferBits),
	  accumulationBufferRedBits (other.accumulationBufferRedBits),
	  accumulationBufferGreenBits (other.accumulationBufferGreenBits),
	  accumulationBufferBlueBits (other.accumulationBufferBlueBits),
	  accumulationBufferAlphaBits (other.accumulationBufferAlphaBits),
	  fullSceneAntiAliasingNumSamples (other.fullSceneAntiAliasingNumSamples)
{
}

OpenGLPixelFormat& OpenGLPixelFormat::operator= (const OpenGLPixelFormat& other)
{
	redBits = other.redBits;
	greenBits = other.greenBits;
	blueBits = other.blueBits;
	alphaBits = other.alphaBits;
	depthBufferBits = other.depthBufferBits;
	stencilBufferBits = other.stencilBufferBits;
	accumulationBufferRedBits = other.accumulationBufferRedBits;
	accumulationBufferGreenBits = other.accumulationBufferGreenBits;
	accumulationBufferBlueBits = other.accumulationBufferBlueBits;
	accumulationBufferAlphaBits = other.accumulationBufferAlphaBits;
	fullSceneAntiAliasingNumSamples = other.fullSceneAntiAliasingNumSamples;
	return *this;
}

bool OpenGLPixelFormat::operator== (const OpenGLPixelFormat& other) const
{
	return redBits == other.redBits
			&& greenBits == other.greenBits
			&& blueBits == other.blueBits
			&& alphaBits == other.alphaBits
			&& depthBufferBits == other.depthBufferBits
			&& stencilBufferBits == other.stencilBufferBits
			&& accumulationBufferRedBits == other.accumulationBufferRedBits
			&& accumulationBufferGreenBits == other.accumulationBufferGreenBits
			&& accumulationBufferBlueBits == other.accumulationBufferBlueBits
			&& accumulationBufferAlphaBits == other.accumulationBufferAlphaBits
			&& fullSceneAntiAliasingNumSamples == other.fullSceneAntiAliasingNumSamples;
}

static VoidArray knownContexts;

OpenGLContext::OpenGLContext() throw()
{
	knownContexts.add (this);
}

OpenGLContext::~OpenGLContext()
{
	knownContexts.removeValue (this);
}

OpenGLContext* OpenGLContext::getCurrentContext()
{
	for (int i = knownContexts.size(); --i >= 0;)
	{
		OpenGLContext* const oglc = (OpenGLContext*) knownContexts.getUnchecked(i);

		if (oglc->isActive())
			return oglc;
	}

	return 0;
}

class OpenGLComponent::OpenGLComponentWatcher  : public ComponentMovementWatcher
{
public:

	OpenGLComponentWatcher (OpenGLComponent* const owner_)
		: ComponentMovementWatcher (owner_),
		  owner (owner_),
		  wasShowing (false)
	{
	}

	~OpenGLComponentWatcher() {}

	void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
	{
		owner->updateContextPosition();
	}

	void componentPeerChanged()
	{
		const ScopedLock sl (owner->getContextLock());
		owner->deleteContext();
	}

	void componentVisibilityChanged (Component&)
	{
		const bool isShowingNow = owner->isShowing();

		if (wasShowing != isShowingNow)
		{
			wasShowing = isShowingNow;
			owner->updateContextPosition();
		}
	}

	juce_UseDebuggingNewOperator

private:
	OpenGLComponent* const owner;
	bool wasShowing;
};

OpenGLComponent::OpenGLComponent (const OpenGLType type_)
	: type (type_),
	  contextToShareListsWith (0),
	  needToUpdateViewport (true)
{
	setOpaque (true);
	componentWatcher = new OpenGLComponentWatcher (this);
}

OpenGLComponent::~OpenGLComponent()
{
	deleteContext();
	componentWatcher = 0;
}

void OpenGLComponent::deleteContext()
{
	const ScopedLock sl (contextLock);
	context = 0;
}

void OpenGLComponent::updateContextPosition()
{
	needToUpdateViewport = true;

	if (getWidth() > 0 && getHeight() > 0)
	{
		Component* const topComp = getTopLevelComponent();

		if (topComp->getPeer() != 0)
		{
			const ScopedLock sl (contextLock);

			if (context != 0)
				context->updateWindowPosition (getScreenX() - topComp->getScreenX(),
											   getScreenY() - topComp->getScreenY(),
											   getWidth(),
											   getHeight(),
											   topComp->getHeight());
		}
	}
}

const OpenGLPixelFormat OpenGLComponent::getPixelFormat() const
{
	OpenGLPixelFormat pf;

	const ScopedLock sl (contextLock);
	if (context != 0)
		pf = context->getPixelFormat();

	return pf;
}

void OpenGLComponent::setPixelFormat (const OpenGLPixelFormat& formatToUse)
{
	if (! (preferredPixelFormat == formatToUse))
	{
		const ScopedLock sl (contextLock);
		deleteContext();
		preferredPixelFormat = formatToUse;
	}
}

void OpenGLComponent::shareWith (OpenGLContext* c)
{
	if (contextToShareListsWith != c)
	{
		const ScopedLock sl (contextLock);
		deleteContext();
		contextToShareListsWith = c;
	}
}

bool OpenGLComponent::makeCurrentContextActive()
{
	if (context == 0)
	{
		const ScopedLock sl (contextLock);

		if (isShowing() && getTopLevelComponent()->getPeer() != 0)
		{
			context = createContext();

			if (context != 0)
			{
				updateContextPosition();

				if (context->makeActive())
					newOpenGLContextCreated();
			}
		}
	}

	return context != 0 && context->makeActive();
}

void OpenGLComponent::makeCurrentContextInactive()
{
	if (context != 0)
		context->makeInactive();
}

bool OpenGLComponent::isActiveContext() const throw()
{
	return context != 0 && context->isActive();
}

void OpenGLComponent::swapBuffers()
{
	if (context != 0)
		context->swapBuffers();
}

void OpenGLComponent::paint (Graphics&)
{
	if (renderAndSwapBuffers())
	{
		ComponentPeer* const peer = getPeer();

		if (peer != 0)
		{
			const Point<int> topLeft (getScreenPosition() - peer->getScreenPosition());
			peer->addMaskedRegion (topLeft.getX(), topLeft.getY(), getWidth(), getHeight());
		}
	}
}

bool OpenGLComponent::renderAndSwapBuffers()
{
	const ScopedLock sl (contextLock);

	if (! makeCurrentContextActive())
		return false;

	if (needToUpdateViewport)
	{
		needToUpdateViewport = false;
		juce_glViewport (getWidth(), getHeight());
	}

	renderOpenGL();
	swapBuffers();

	return true;
}

void OpenGLComponent::internalRepaint (int x, int y, int w, int h)
{
	Component::internalRepaint (x, y, w, h);

	if (context != 0)
		context->repaint();
}

END_JUCE_NAMESPACE

#endif
/*** End of inlined file: juce_OpenGLComponent.cpp ***/


/*** Start of inlined file: juce_PreferencesPanel.cpp ***/
BEGIN_JUCE_NAMESPACE

PreferencesPanel::PreferencesPanel()
	: buttonSize (70)
{
}

PreferencesPanel::~PreferencesPanel()
{
	currentPage = 0;
	deleteAllChildren();
}

void PreferencesPanel::addSettingsPage (const String& title,
										const Drawable* icon,
										const Drawable* overIcon,
										const Drawable* downIcon)
{
	DrawableButton* button = new DrawableButton (title, DrawableButton::ImageAboveTextLabel);
	button->setImages (icon, overIcon, downIcon);
	button->setRadioGroupId (1);
	button->addButtonListener (this);
	button->setClickingTogglesState (true);
	button->setWantsKeyboardFocus (false);
	addAndMakeVisible (button);

	resized();

	if (currentPage == 0)
		setCurrentPage (title);
}

void PreferencesPanel::addSettingsPage (const String& title,
										const char* imageData,
										const int imageDataSize)
{
	DrawableImage icon, iconOver, iconDown;
	icon.setImage (ImageCache::getFromMemory (imageData, imageDataSize), true);

	iconOver.setImage (ImageCache::getFromMemory (imageData, imageDataSize), true);
	iconOver.setOverlayColour (Colours::black.withAlpha (0.12f));

	iconDown.setImage (ImageCache::getFromMemory (imageData, imageDataSize), true);
	iconDown.setOverlayColour (Colours::black.withAlpha (0.25f));

	addSettingsPage (title, &icon, &iconOver, &iconDown);
}

class PrefsDialogWindow  : public DialogWindow
{
public:
	PrefsDialogWindow (const String& dialogtitle,
					   const Colour& backgroundColour)
		: DialogWindow (dialogtitle, backgroundColour, true)
	{
	}

	~PrefsDialogWindow()
	{
	}

	void closeButtonPressed()
	{
		exitModalState (0);
	}

private:
	PrefsDialogWindow (const PrefsDialogWindow&);
	PrefsDialogWindow& operator= (const PrefsDialogWindow&);
};

void PreferencesPanel::showInDialogBox (const String& dialogtitle,
										int dialogWidth,
										int dialogHeight,
										const Colour& backgroundColour)
{
	setSize (dialogWidth, dialogHeight);

	PrefsDialogWindow dw (dialogtitle, backgroundColour);

	dw.setContentComponent (this, true, true);
	dw.centreAroundComponent (0, dw.getWidth(), dw.getHeight());
	dw.runModalLoop();
	dw.setContentComponent (0, false, false);
}

void PreferencesPanel::resized()
{
	int x = 0;

	for (int i = 0; i < getNumChildComponents(); ++i)
	{
		Component* c = getChildComponent (i);

		if (dynamic_cast <DrawableButton*> (c) == 0)
		{
			c->setBounds (0, buttonSize + 5, getWidth(), getHeight() - buttonSize - 5);
		}
		else
		{
			c->setBounds (x, 0, buttonSize, buttonSize);
			x += buttonSize;
		}
	}
}

void PreferencesPanel::paint (Graphics& g)
{
	g.setColour (Colours::grey);
	g.fillRect (0, buttonSize + 2, getWidth(), 1);
}

void PreferencesPanel::setCurrentPage (const String& pageName)
{
	if (currentPageName != pageName)
	{
		currentPageName = pageName;

		currentPage = 0;
		currentPage = createComponentForPage (pageName);

		if (currentPage != 0)
		{
			addAndMakeVisible (currentPage);
			currentPage->toBack();
			resized();
		}

		for (int i = 0; i < getNumChildComponents(); ++i)
		{
			DrawableButton* db = dynamic_cast <DrawableButton*> (getChildComponent (i));

			if (db != 0 && db->getName() == pageName)
			{
				db->setToggleState (true, false);
				break;
			}
		}
	}
}

void PreferencesPanel::buttonClicked (Button*)
{
	for (int i = 0; i < getNumChildComponents(); ++i)
	{
		DrawableButton* db = dynamic_cast <DrawableButton*> (getChildComponent (i));

		if (db != 0 && db->getToggleState())
		{
			setCurrentPage (db->getName());
			break;
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PreferencesPanel.cpp ***/


/*** Start of inlined file: juce_SystemTrayIconComponent.cpp ***/
#if JUCE_WINDOWS || JUCE_LINUX

BEGIN_JUCE_NAMESPACE

SystemTrayIconComponent::SystemTrayIconComponent()
{
	addToDesktop (0);
}

SystemTrayIconComponent::~SystemTrayIconComponent()
{
}

END_JUCE_NAMESPACE

#endif
/*** End of inlined file: juce_SystemTrayIconComponent.cpp ***/


/*** Start of inlined file: juce_AlertWindow.cpp ***/
BEGIN_JUCE_NAMESPACE

class AlertWindowTextEditor  : public TextEditor
{
public:
	AlertWindowTextEditor (const String& name, const bool isPasswordBox)
		: TextEditor (name, isPasswordBox ? getDefaultPasswordChar() :  0)
	{
		setSelectAllWhenFocused (true);
	}

	~AlertWindowTextEditor()
	{
	}

	void returnPressed()
	{
		// pass these up the component hierarchy to be trigger the buttons
		getParentComponent()->keyPressed (KeyPress (KeyPress::returnKey, 0, '\n'));
	}

	void escapePressed()
	{
		// pass these up the component hierarchy to be trigger the buttons
		getParentComponent()->keyPressed (KeyPress (KeyPress::escapeKey, 0, 0));
	}

private:
	AlertWindowTextEditor (const AlertWindowTextEditor&);
	AlertWindowTextEditor& operator= (const AlertWindowTextEditor&);

	static juce_wchar getDefaultPasswordChar() throw()
	{
	  #if JUCE_LINUX
		return 0x2022;
	  #else
		return 0x25cf;
	  #endif
	}
};

AlertWindow::AlertWindow (const String& title,
						  const String& message,
						  AlertIconType iconType,
						  Component* associatedComponent_)
   : TopLevelWindow (title, true),
	 alertIconType (iconType),
	 associatedComponent (associatedComponent_)
{
	if (message.isEmpty())
		text = " "; // to force an update if the message is empty

	setMessage (message);

	for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
	{
		Component* const c = Desktop::getInstance().getComponent (i);

		if (c != 0 && c->isAlwaysOnTop() && c->isShowing())
		{
			setAlwaysOnTop (true);
			break;
		}
	}

	if (JUCEApplication::getInstance() == 0)
		setAlwaysOnTop (true); // for a plugin, make it always-on-top because the host windows are often top-level

	lookAndFeelChanged();

	constrainer.setMinimumOnscreenAmounts (0x10000, 0x10000, 0x10000, 0x10000);
}

AlertWindow::~AlertWindow()
{
	for (int i = customComps.size(); --i >= 0;)
		removeChildComponent ((Component*) customComps[i]);

	deleteAllChildren();
}

void AlertWindow::userTriedToCloseWindow()
{
	exitModalState (0);
}

void AlertWindow::setMessage (const String& message)
{
	const String newMessage (message.substring (0, 2048));

	if (text != newMessage)
	{
		text = newMessage;

		font.setHeight (15.0f);

		Font titleFont (font.getHeight() * 1.1f, Font::bold);
		textLayout.setText (getName() + "\n\n", titleFont);

		textLayout.appendText (text, font);

		updateLayout (true);
		repaint();
	}
}

void AlertWindow::buttonClicked (Button* button)
{
	for (int i = 0; i < buttons.size(); i++)
	{
		TextButton* const c = (TextButton*) buttons[i];

		if (button->getName() == c->getName())
		{
			if (c->getParentComponent() != 0)
				c->getParentComponent()->exitModalState (c->getCommandID());

			break;
		}
	}
}

void AlertWindow::addButton (const String& name,
							 const int returnValue,
							 const KeyPress& shortcutKey1,
							 const KeyPress& shortcutKey2)
{
	TextButton* const b = new TextButton (name, String::empty);

	b->setWantsKeyboardFocus (true);
	b->setMouseClickGrabsKeyboardFocus (false);
	b->setCommandToTrigger (0, returnValue, false);
	b->addShortcut (shortcutKey1);
	b->addShortcut (shortcutKey2);
	b->addButtonListener (this);
	b->changeWidthToFitText (getLookAndFeel().getAlertWindowButtonHeight());

	addAndMakeVisible (b, 0);
	buttons.add (b);

	updateLayout (false);
}

int AlertWindow::getNumButtons() const
{
	return buttons.size();
}

void AlertWindow::addTextEditor (const String& name,
								 const String& initialContents,
								 const String& onScreenLabel,
								 const bool isPasswordBox)
{
	AlertWindowTextEditor* const tc = new AlertWindowTextEditor (name, isPasswordBox);

	tc->setColour (TextEditor::outlineColourId, findColour (ComboBox::outlineColourId));
	tc->setFont (font);
	tc->setText (initialContents);
	tc->setCaretPosition (initialContents.length());
	addAndMakeVisible (tc);
	textBoxes.add (tc);
	allComps.add (tc);
	textboxNames.add (onScreenLabel);

	updateLayout (false);
}

const String AlertWindow::getTextEditorContents (const String& nameOfTextEditor) const
{
	for (int i = textBoxes.size(); --i >= 0;)
		if (((TextEditor*)textBoxes[i])->getName() == nameOfTextEditor)
			return ((TextEditor*)textBoxes[i])->getText();

	return String::empty;
}

void AlertWindow::addComboBox (const String& name,
							   const StringArray& items,
							   const String& onScreenLabel)
{
	ComboBox* const cb = new ComboBox (name);

	for (int i = 0; i < items.size(); ++i)
		cb->addItem (items[i], i + 1);

	addAndMakeVisible (cb);
	cb->setSelectedItemIndex (0);

	comboBoxes.add (cb);
	allComps.add (cb);

	comboBoxNames.add (onScreenLabel);

	updateLayout (false);
}

ComboBox* AlertWindow::getComboBoxComponent (const String& nameOfList) const
{
	for (int i = comboBoxes.size(); --i >= 0;)
		if (((ComboBox*) comboBoxes[i])->getName() == nameOfList)
			return (ComboBox*) comboBoxes[i];

	return 0;
}

class AlertTextComp : public TextEditor
{
public:
	AlertTextComp (const String& message,
				   const Font& font)
	{
		setReadOnly (true);
		setMultiLine (true, true);
		setCaretVisible (false);
		setScrollbarsShown (true);
		lookAndFeelChanged();
		setWantsKeyboardFocus (false);

		setFont (font);
		setText (message, false);

		bestWidth = 2 * (int) sqrt (font.getHeight() * font.getStringWidth (message));

		setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
		setColour (TextEditor::outlineColourId, Colours::transparentBlack);
		setColour (TextEditor::shadowColourId, Colours::transparentBlack);
	}

	~AlertTextComp()
	{
	}

	int getPreferredWidth() const throw()	{ return bestWidth; }

	void updateLayout (const int width)
	{
		TextLayout text;
		text.appendText (getText(), getFont());
		text.layout (width - 8, Justification::topLeft, true);
		setSize (width, jmin (width, text.getHeight() + (int) getFont().getHeight()));
	}

private:
	int bestWidth;

	AlertTextComp (const AlertTextComp&);
	AlertTextComp& operator= (const AlertTextComp&);
};

void AlertWindow::addTextBlock (const String& text)
{
	AlertTextComp* const c = new AlertTextComp (text, font);

	textBlocks.add (c);
	allComps.add (c);

	addAndMakeVisible (c);

	updateLayout (false);
}

void AlertWindow::addProgressBarComponent (double& progressValue)
{
	ProgressBar* const pb = new ProgressBar (progressValue);

	progressBars.add (pb);
	allComps.add (pb);

	addAndMakeVisible (pb);

	updateLayout (false);
}

void AlertWindow::addCustomComponent (Component* const component)
{
	customComps.add (component);
	allComps.add (component);

	addAndMakeVisible (component);

	updateLayout (false);
}

int AlertWindow::getNumCustomComponents() const
{
	return customComps.size();
}

Component* AlertWindow::getCustomComponent (const int index) const
{
	return (Component*) customComps [index];
}

Component* AlertWindow::removeCustomComponent (const int index)
{
	Component* const c = getCustomComponent (index);

	if (c != 0)
	{
		customComps.removeValue (c);
		allComps.removeValue (c);
		removeChildComponent (c);

		updateLayout (false);
	}

	return c;
}

void AlertWindow::paint (Graphics& g)
{
	getLookAndFeel().drawAlertBox (g, *this, textArea, textLayout);

	g.setColour (findColour (textColourId));
	g.setFont (getLookAndFeel().getAlertWindowFont());

	int i;
	for (i = textBoxes.size(); --i >= 0;)
	{
		const TextEditor* const te = (TextEditor*) textBoxes[i];

		g.drawFittedText (textboxNames[i],
						  te->getX(), te->getY() - 14,
						  te->getWidth(), 14,
						  Justification::centredLeft, 1);
	}

	for (i = comboBoxNames.size(); --i >= 0;)
	{
		const ComboBox* const cb = (ComboBox*) comboBoxes[i];

		g.drawFittedText (comboBoxNames[i],
						  cb->getX(), cb->getY() - 14,
						  cb->getWidth(), 14,
						  Justification::centredLeft, 1);
	}

	for (i = customComps.size(); --i >= 0;)
	{
		const Component* const c = (Component*) customComps[i];

		g.drawFittedText (c->getName(),
						  c->getX(), c->getY() - 14,
						  c->getWidth(), 14,
						  Justification::centredLeft, 1);
	}
}

void AlertWindow::updateLayout (const bool onlyIncreaseSize)
{
	const int titleH = 24;
	const int iconWidth = 80;

	const int wid = jmax (font.getStringWidth (text),
						  font.getStringWidth (getName()));

	const int sw = (int) sqrt (font.getHeight() * wid);
	int w = jmin (300 + sw * 2, (int) (getParentWidth() * 0.7f));
	const int edgeGap = 10;
	const int labelHeight = 18;
	int iconSpace;

	if (alertIconType == NoIcon)
	{
		textLayout.layout (w, Justification::horizontallyCentred, true);
		iconSpace = 0;
	}
	else
	{
		textLayout.layout (w, Justification::left, true);
		iconSpace = iconWidth;
	}

	w = jmax (350, textLayout.getWidth() + iconSpace + edgeGap * 4);
	w = jmin (w, (int) (getParentWidth() * 0.7f));

	const int textLayoutH = textLayout.getHeight();
	const int textBottom = 16 + titleH + textLayoutH;
	int h = textBottom;

	int buttonW = 40;
	int i;
	for (i = 0; i < buttons.size(); ++i)
		buttonW += 16 + ((const TextButton*) buttons[i])->getWidth();

	w = jmax (buttonW, w);

	h += (textBoxes.size() + comboBoxes.size() + progressBars.size()) * 50;

	if (buttons.size() > 0)
		h += 20 + ((TextButton*) buttons[0])->getHeight();

	for (i = customComps.size(); --i >= 0;)
	{
		Component* c = (Component*) customComps[i];
		w = jmax (w, (c->getWidth() * 100) / 80);
		h += 10 + c->getHeight();

		if (c->getName().isNotEmpty())
			h += labelHeight;
	}

	for (i = textBlocks.size(); --i >= 0;)
	{
		const AlertTextComp* const ac = (AlertTextComp*) textBlocks[i];
		w = jmax (w, ac->getPreferredWidth());
	}

	w = jmin (w, (int) (getParentWidth() * 0.7f));

	for (i = textBlocks.size(); --i >= 0;)
	{
		AlertTextComp* const ac = (AlertTextComp*) textBlocks[i];
		ac->updateLayout ((int) (w * 0.8f));
		h += ac->getHeight() + 10;
	}

	h = jmin (getParentHeight() - 50, h);

	if (onlyIncreaseSize)
	{
		w = jmax (w, getWidth());
		h = jmax (h, getHeight());
	}

	if (! isVisible())
	{
		centreAroundComponent (associatedComponent, w, h);
	}
	else
	{
		const int cx = getX() + getWidth() / 2;
		const int cy = getY() + getHeight() / 2;

		setBounds (cx - w / 2,
				   cy - h / 2,
				   w, h);
	}

	textArea.setBounds (edgeGap, edgeGap, w - (edgeGap * 2), h - edgeGap);

	const int spacer = 16;
	int totalWidth = -spacer;

	for (i = buttons.size(); --i >= 0;)
		totalWidth += ((TextButton*) buttons[i])->getWidth() + spacer;

	int x = (w - totalWidth) / 2;
	int y = (int) (getHeight() * 0.95f);

	for (i = 0; i < buttons.size(); ++i)
	{
		TextButton* const c = (TextButton*) buttons[i];
		int ny = proportionOfHeight (0.95f) - c->getHeight();
		c->setTopLeftPosition (x, ny);
		if (ny < y)
			y = ny;

		x += c->getWidth() + spacer;

		c->toFront (false);
	}

	y = textBottom;

	for (i = 0; i < allComps.size(); ++i)
	{
		Component* const c = (Component*) allComps[i];
		int h = 22;

		const int comboIndex = comboBoxes.indexOf (c);
		if (comboIndex >= 0 && comboBoxNames [comboIndex].isNotEmpty())
			y += labelHeight;

		const int tbIndex = textBoxes.indexOf (c);
		if (tbIndex >= 0 && textboxNames[tbIndex].isNotEmpty())
			y += labelHeight;

		if (customComps.contains (c))
		{
			if (c->getName().isNotEmpty())
				y += labelHeight;

			c->setTopLeftPosition (proportionOfWidth (0.1f), y);
			h = c->getHeight();
		}
		else if (textBlocks.contains (c))
		{
			c->setTopLeftPosition ((getWidth() - c->getWidth()) / 2, y);
			h = c->getHeight();
		}
		else
		{
			c->setBounds (proportionOfWidth (0.1f), y, proportionOfWidth (0.8f), h);
		}

		y += h + 10;
	}

	setWantsKeyboardFocus (getNumChildComponents() == 0);
}

bool AlertWindow::containsAnyExtraComponents() const
{
	return textBoxes.size()
			+ comboBoxes.size()
			+ progressBars.size()
			+ customComps.size() > 0;
}

void AlertWindow::mouseDown (const MouseEvent&)
{
	dragger.startDraggingComponent (this, &constrainer);
}

void AlertWindow::mouseDrag (const MouseEvent& e)
{
	dragger.dragComponent (this, e);
}

bool AlertWindow::keyPressed (const KeyPress& key)
{
	for (int i = buttons.size(); --i >= 0;)
	{
		TextButton* const b = (TextButton*) buttons[i];

		if (b->isRegisteredForShortcut (key))
		{
			b->triggerClick();
			return true;
		}
	}

	if (key.isKeyCode (KeyPress::escapeKey) && buttons.size() == 0)
	{
		exitModalState (0);
		return true;
	}
	else if (key.isKeyCode (KeyPress::returnKey) && buttons.size() == 1)
	{
		((TextButton*) buttons.getFirst())->triggerClick();
		return true;
	}

	return false;
}

void AlertWindow::lookAndFeelChanged()
{
	const int flags = getLookAndFeel().getAlertBoxWindowFlags();

	setUsingNativeTitleBar ((flags & ComponentPeer::windowHasTitleBar) != 0);
	setDropShadowEnabled (isOpaque() && (flags & ComponentPeer::windowHasDropShadow) != 0);
}

int AlertWindow::getDesktopWindowStyleFlags() const
{
	return getLookAndFeel().getAlertBoxWindowFlags();
}

struct AlertWindowInfo
{
	String title, message, button1, button2, button3;
	AlertWindow::AlertIconType iconType;
	int numButtons;
	Component::SafePointer<Component> associatedComponent;

	int run() const
	{
		return (int) (pointer_sized_int)
				MessageManager::getInstance()->callFunctionOnMessageThread (showCallback, (void*) this);
	}

private:
	int show() const
	{
		LookAndFeel& lf = associatedComponent != 0 ? associatedComponent->getLookAndFeel()
												   : LookAndFeel::getDefaultLookAndFeel();

		ScopedPointer <Component> alertBox (lf.createAlertWindow (title, message, button1, button2, button3,
																  iconType, numButtons, associatedComponent));

		jassert (alertBox != 0); // you have to return one of these!

		return alertBox->runModalLoop();
	}

	static void* showCallback (void* userData)
	{
		return (void*) (pointer_sized_int) ((const AlertWindowInfo*) userData)->show();
	}
};

void AlertWindow::showMessageBox (AlertIconType iconType,
								  const String& title,
								  const String& message,
								  const String& buttonText,
								  Component* associatedComponent)
{
	AlertWindowInfo info;
	info.title = title;
	info.message = message;
	info.button1 = buttonText.isEmpty() ? TRANS("ok") : buttonText;
	info.iconType = iconType;
	info.numButtons = 1;
	info.associatedComponent = associatedComponent;

	info.run();
}

bool AlertWindow::showOkCancelBox (AlertIconType iconType,
								   const String& title,
								   const String& message,
								   const String& button1Text,
								   const String& button2Text,
								   Component* associatedComponent)
{
	AlertWindowInfo info;
	info.title = title;
	info.message = message;
	info.button1 = button1Text.isEmpty() ? TRANS("ok")     : button1Text;
	info.button2 = button2Text.isEmpty() ? TRANS("cancel") : button2Text;
	info.iconType = iconType;
	info.numButtons = 2;
	info.associatedComponent = associatedComponent;

	return info.run() != 0;
}

int AlertWindow::showYesNoCancelBox (AlertIconType iconType,
									 const String& title,
									 const String& message,
									 const String& button1Text,
									 const String& button2Text,
									 const String& button3Text,
									 Component* associatedComponent)
{
	AlertWindowInfo info;
	info.title = title;
	info.message = message;
	info.button1 = button1Text.isEmpty() ? TRANS("yes")     : button1Text;
	info.button2 = button2Text.isEmpty() ? TRANS("no")      : button2Text;
	info.button3 = button3Text.isEmpty() ? TRANS("cancel")  : button3Text;
	info.iconType = iconType;
	info.numButtons = 3;
	info.associatedComponent = associatedComponent;

	return info.run();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AlertWindow.cpp ***/


/*** Start of inlined file: juce_ComponentPeer.cpp ***/
BEGIN_JUCE_NAMESPACE

//#define JUCE_ENABLE_REPAINT_DEBUGGING 1

static VoidArray heavyweightPeers;

ComponentPeer::ComponentPeer (Component* const component_,
							  const int styleFlags_) throw()
	: component (component_),
	  styleFlags (styleFlags_),
	  lastPaintTime (0),
	  constrainer (0),
	  lastDragAndDropCompUnderMouse (0),
	  fakeMouseMessageSent (false),
	  isWindowMinimised (false)
{
	heavyweightPeers.add (this);
}

ComponentPeer::~ComponentPeer()
{
	heavyweightPeers.removeValue (this);

	Desktop::getInstance().triggerFocusCallback();
}

int ComponentPeer::getNumPeers() throw()
{
	return heavyweightPeers.size();
}

ComponentPeer* ComponentPeer::getPeer (const int index) throw()
{
	return (ComponentPeer*) heavyweightPeers [index];
}

ComponentPeer* ComponentPeer::getPeerFor (const Component* const component) throw()
{
	for (int i = heavyweightPeers.size(); --i >= 0;)
	{
		ComponentPeer* const peer = (ComponentPeer*) heavyweightPeers.getUnchecked(i);

		if (peer->getComponent() == component)
			return peer;
	}

	return 0;
}

bool ComponentPeer::isValidPeer (const ComponentPeer* const peer) throw()
{
	return heavyweightPeers.contains (const_cast <ComponentPeer*> (peer));
}

void ComponentPeer::updateCurrentModifiers() throw()
{
	ModifierKeys::updateCurrentModifiers();
}

void ComponentPeer::handleMouseEvent (const int touchIndex, const Point<int>& positionWithinPeer, const ModifierKeys& newMods, const int64 time)
{
	MouseInputSource* const mouse = Desktop::getInstance().getMouseSource (touchIndex);
	jassert (mouse != 0); // not enough sources!

	mouse->handleEvent (this, positionWithinPeer, time, newMods);
}

void ComponentPeer::handleMouseWheel (const int touchIndex, const Point<int>& positionWithinPeer, const int64 time, const float x, const float y)
{
	MouseInputSource* const mouse = Desktop::getInstance().getMouseSource (touchIndex);
	jassert (mouse != 0); // not enough sources!

	mouse->handleWheel (this, positionWithinPeer, time, x, y);
}

void ComponentPeer::handlePaint (LowLevelGraphicsContext& contextToPaintTo)
{
	Graphics g (&contextToPaintTo);

#if JUCE_ENABLE_REPAINT_DEBUGGING
	g.saveState();
#endif

	JUCE_TRY
	{
		component->paintEntireComponent (g);
	}
	JUCE_CATCH_EXCEPTION

#if JUCE_ENABLE_REPAINT_DEBUGGING
	// enabling this code will fill all areas that get repainted with a colour overlay, to show
	// clearly when things are being repainted.
	{
		g.restoreState();

		g.fillAll (Colour ((uint8) Random::getSystemRandom().nextInt (255),
						   (uint8) Random::getSystemRandom().nextInt (255),
						   (uint8) Random::getSystemRandom().nextInt (255),
						   (uint8) 0x50));
	}
#endif
}

bool ComponentPeer::handleKeyPress (const int keyCode,
									const juce_wchar textCharacter)
{
	updateCurrentModifiers();

	Component* target = Component::getCurrentlyFocusedComponent() != 0
							? Component::getCurrentlyFocusedComponent()
							: component;

	if (target->isCurrentlyBlockedByAnotherModalComponent())
	{
		Component* const currentModalComp = Component::getCurrentlyModalComponent();

		if (currentModalComp != 0)
			target = currentModalComp;
	}

	const KeyPress keyInfo (keyCode,
							ModifierKeys::getCurrentModifiers().getRawFlags()
							   & ModifierKeys::allKeyboardModifiers,
							textCharacter);

	bool keyWasUsed = false;

	while (target != 0)
	{
		const Component::SafePointer<Component> deletionChecker (target);

		if (target->keyListeners_ != 0)
		{
			for (int i = target->keyListeners_->size(); --i >= 0;)
			{
				keyWasUsed = ((KeyListener*) target->keyListeners_->getUnchecked(i))->keyPressed (keyInfo, target);

				if (keyWasUsed || deletionChecker == 0)
					return keyWasUsed;

				i = jmin (i, target->keyListeners_->size());
			}
		}

		keyWasUsed = target->keyPressed (keyInfo);

		if (keyWasUsed || deletionChecker == 0)
			break;

		if (keyInfo.isKeyCode (KeyPress::tabKey) && Component::getCurrentlyFocusedComponent() != 0)
		{
			Component* const currentlyFocused = Component::getCurrentlyFocusedComponent();
			currentlyFocused->moveKeyboardFocusToSibling (! keyInfo.getModifiers().isShiftDown());
			keyWasUsed = (currentlyFocused != Component::getCurrentlyFocusedComponent());
			break;
		}

		target = target->parentComponent_;
	}

	return keyWasUsed;
}

bool ComponentPeer::handleKeyUpOrDown (const bool isKeyDown)
{
	updateCurrentModifiers();

	Component* target = Component::getCurrentlyFocusedComponent() != 0
							? Component::getCurrentlyFocusedComponent()
							: component;

	if (target->isCurrentlyBlockedByAnotherModalComponent())
	{
		Component* const currentModalComp = Component::getCurrentlyModalComponent();

		if (currentModalComp != 0)
			target = currentModalComp;
	}

	bool keyWasUsed = false;

	while (target != 0)
	{
		const Component::SafePointer<Component> deletionChecker (target);

		keyWasUsed = target->keyStateChanged (isKeyDown);

		if (keyWasUsed || deletionChecker == 0)
			break;

		if (target->keyListeners_ != 0)
		{
			for (int i = target->keyListeners_->size(); --i >= 0;)
			{
				keyWasUsed = ((KeyListener*) target->keyListeners_->getUnchecked(i))->keyStateChanged (isKeyDown, target);

				if (keyWasUsed || deletionChecker == 0)
					return keyWasUsed;

				i = jmin (i, target->keyListeners_->size());
			}
		}

		target = target->parentComponent_;
	}

	return keyWasUsed;
}

void ComponentPeer::handleModifierKeysChange()
{
	updateCurrentModifiers();

	Component* target = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();

	if (target == 0)
		target = Component::getCurrentlyFocusedComponent();

	if (target == 0)
		target = component;

	if (target != 0)
		target->internalModifierKeysChanged();
}

TextInputTarget* ComponentPeer::findCurrentTextInputTarget()
{
	Component* const c = Component::getCurrentlyFocusedComponent();
	if (component->isParentOf (c))
		return dynamic_cast <TextInputTarget*> (c);

	return 0;
}

void ComponentPeer::handleBroughtToFront()
{
	updateCurrentModifiers();

	if (component != 0)
		component->internalBroughtToFront();
}

void ComponentPeer::setConstrainer (ComponentBoundsConstrainer* const newConstrainer) throw()
{
	constrainer = newConstrainer;
}

void ComponentPeer::handleMovedOrResized()
{
	jassert (component->isValidComponent());
	updateCurrentModifiers();

	const bool nowMinimised = isMinimised();

	if (component->flags.hasHeavyweightPeerFlag && ! nowMinimised)
	{
		const Component::SafePointer<Component> deletionChecker (component);

		const Rectangle<int> newBounds (getBounds());
		const bool wasMoved   = (component->getPosition() != newBounds.getPosition());
		const bool wasResized = (component->getWidth() != newBounds.getWidth() || component->getHeight() != newBounds.getHeight());

		if (wasMoved || wasResized)
		{
			component->bounds_ = newBounds;

			if (wasResized)
				component->repaint();

			component->sendMovedResizedMessages (wasMoved, wasResized);

			if (deletionChecker == 0)
				return;
		}
	}

	if (isWindowMinimised != nowMinimised)
	{
		isWindowMinimised = nowMinimised;
		component->minimisationStateChanged (nowMinimised);
		component->sendVisibilityChangeMessage();
	}

	if (! isFullScreen())
		lastNonFullscreenBounds = component->getBounds();
}

void ComponentPeer::handleFocusGain()
{
	updateCurrentModifiers();

	if (component->isParentOf (lastFocusedComponent))
	{
		Component::currentlyFocusedComponent = lastFocusedComponent;
		Desktop::getInstance().triggerFocusCallback();
		lastFocusedComponent->internalFocusGain (Component::focusChangedDirectly);
	}
	else
	{
		if (! component->isCurrentlyBlockedByAnotherModalComponent())
			component->grabKeyboardFocus();
		else
			Component::bringModalComponentToFront();
	}
}

void ComponentPeer::handleFocusLoss()
{
	updateCurrentModifiers();

	if (component->hasKeyboardFocus (true))
	{
		lastFocusedComponent = Component::currentlyFocusedComponent;

		if (lastFocusedComponent != 0)
		{
			Component::currentlyFocusedComponent = 0;
			Desktop::getInstance().triggerFocusCallback();
			lastFocusedComponent->internalFocusLoss (Component::focusChangedByMouseClick);
		}
	}
}

Component* ComponentPeer::getLastFocusedSubcomponent() const throw()
{
	return (component->isParentOf (lastFocusedComponent) && lastFocusedComponent->isShowing())
				? static_cast <Component*> (lastFocusedComponent)
				: component;
}

void ComponentPeer::handleScreenSizeChange()
{
	updateCurrentModifiers();

	component->parentSizeChanged();
	handleMovedOrResized();
}

void ComponentPeer::setNonFullScreenBounds (const Rectangle<int>& newBounds) throw()
{
	lastNonFullscreenBounds = newBounds;
}

const Rectangle<int>& ComponentPeer::getNonFullScreenBounds() const throw()
{
	return lastNonFullscreenBounds;
}

static FileDragAndDropTarget* findDragAndDropTarget (Component* c,
													 const StringArray& files,
													 FileDragAndDropTarget* const lastOne)
{
	while (c != 0)
	{
		FileDragAndDropTarget* const t = dynamic_cast <FileDragAndDropTarget*> (c);

		if (t != 0 && (t == lastOne || t->isInterestedInFileDrag (files)))
			return t;

		c = c->getParentComponent();
	}

	return 0;
}

void ComponentPeer::handleFileDragMove (const StringArray& files, const Point<int>& position)
{
	updateCurrentModifiers();

	FileDragAndDropTarget* lastTarget
		= dynamic_cast<FileDragAndDropTarget*> (static_cast<Component*> (dragAndDropTargetComponent));

	FileDragAndDropTarget* newTarget = 0;

	Component* const compUnderMouse = component->getComponentAt (position);

	if (compUnderMouse != lastDragAndDropCompUnderMouse)
	{
		lastDragAndDropCompUnderMouse = compUnderMouse;
		newTarget = findDragAndDropTarget (compUnderMouse, files, lastTarget);

		if (newTarget != lastTarget)
		{
			if (lastTarget != 0)
				lastTarget->fileDragExit (files);

			dragAndDropTargetComponent = 0;

			if (newTarget != 0)
			{
				dragAndDropTargetComponent = dynamic_cast <Component*> (newTarget);
				const Point<int> pos (component->relativePositionToOtherComponent (dragAndDropTargetComponent, position));
				newTarget->fileDragEnter (files, pos.getX(), pos.getY());
			}
		}
	}
	else
	{
		newTarget = lastTarget;
	}

	if (newTarget != 0)
	{
		Component* const targetComp = dynamic_cast <Component*> (newTarget);
		const Point<int> pos (component->relativePositionToOtherComponent (targetComp, position));

		newTarget->fileDragMove (files, pos.getX(), pos.getY());
	}
}

void ComponentPeer::handleFileDragExit (const StringArray& files)
{
	handleFileDragMove (files, Point<int> (-1, -1));

	jassert (dragAndDropTargetComponent == 0);
	lastDragAndDropCompUnderMouse = 0;
}

void ComponentPeer::handleFileDragDrop (const StringArray& files, const Point<int>& position)
{
	handleFileDragMove (files, position);

	if (dragAndDropTargetComponent != 0)
	{
		FileDragAndDropTarget* const target
			= dynamic_cast<FileDragAndDropTarget*> (static_cast<Component*> (dragAndDropTargetComponent));

		dragAndDropTargetComponent = 0;
		lastDragAndDropCompUnderMouse = 0;

		if (target != 0)
		{
			Component* const targetComp = dynamic_cast <Component*> (target);

			if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
			{
				targetComp->internalModalInputAttempt();

				if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
					return;
			}

			const Point<int> pos (component->relativePositionToOtherComponent (targetComp, position));
			target->filesDropped (files, pos.getX(), pos.getY());
		}
	}
}

void ComponentPeer::handleUserClosingWindow()
{
	updateCurrentModifiers();

	component->userTriedToCloseWindow();
}

void ComponentPeer::bringModalComponentToFront()
{
	Component::bringModalComponentToFront();
}

void ComponentPeer::clearMaskedRegion() throw()
{
	maskedRegion.clear();
}

void ComponentPeer::addMaskedRegion (int x, int y, int w, int h) throw()
{
	maskedRegion.add (x, y, w, h);
}

const StringArray ComponentPeer::getAvailableRenderingEngines() throw()
{
	StringArray s;
	s.add ("Software Renderer");
	return s;
}

int ComponentPeer::getCurrentRenderingEngine() throw()
{
	return 0;
}

void ComponentPeer::setCurrentRenderingEngine (int /*index*/) throw()
{
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ComponentPeer.cpp ***/


/*** Start of inlined file: juce_DialogWindow.cpp ***/
BEGIN_JUCE_NAMESPACE

DialogWindow::DialogWindow (const String& name,
							const Colour& backgroundColour_,
							const bool escapeKeyTriggersCloseButton_,
							const bool addToDesktop_)
	: DocumentWindow (name, backgroundColour_, DocumentWindow::closeButton, addToDesktop_),
	  escapeKeyTriggersCloseButton (escapeKeyTriggersCloseButton_)
{
}

DialogWindow::~DialogWindow()
{
}

void DialogWindow::resized()
{
	DocumentWindow::resized();

	const KeyPress esc (KeyPress::escapeKey, 0, 0);

	if (escapeKeyTriggersCloseButton
		 && getCloseButton() != 0
		 && ! getCloseButton()->isRegisteredForShortcut (esc))
	{
		getCloseButton()->addShortcut (esc);
	}
}

class TempDialogWindow : public DialogWindow
{
public:
	TempDialogWindow (const String& title, const Colour& colour, const bool escapeCloses)
		: DialogWindow (title, colour, escapeCloses, true)
	{
	}

	~TempDialogWindow()
	{
	}

	void closeButtonPressed()
	{
		setVisible (false);
	}

private:
	TempDialogWindow (const TempDialogWindow&);
	TempDialogWindow& operator= (const TempDialogWindow&);

};

int DialogWindow::showModalDialog (const String& dialogTitle,
								   Component* contentComponent,
								   Component* componentToCentreAround,
								   const Colour& colour,
								   const bool escapeKeyTriggersCloseButton,
								   const bool shouldBeResizable,
								   const bool useBottomRightCornerResizer)
{
	TempDialogWindow dw (dialogTitle, colour, escapeKeyTriggersCloseButton);

	dw.setContentComponent (contentComponent, true, true);
	dw.centreAroundComponent (componentToCentreAround, dw.getWidth(), dw.getHeight());
	dw.setResizable (shouldBeResizable, useBottomRightCornerResizer);
	const int result = dw.runModalLoop();
	dw.setContentComponent (0, false);
	return result;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DialogWindow.cpp ***/


/*** Start of inlined file: juce_DocumentWindow.cpp ***/
BEGIN_JUCE_NAMESPACE

class DocumentWindow::ButtonListenerProxy  : public ButtonListener
{
public:
	ButtonListenerProxy (DocumentWindow& owner_)
		: owner (owner_)
	{
	}

	void buttonClicked (Button* button)
	{
		if (button == owner.getMinimiseButton())
			owner.minimiseButtonPressed();
		else if (button == owner.getMaximiseButton())
			owner.maximiseButtonPressed();
		else if (button == owner.getCloseButton())
			owner.closeButtonPressed();
	}

	juce_UseDebuggingNewOperator

private:
	DocumentWindow& owner;

	ButtonListenerProxy (const ButtonListenerProxy&);
	ButtonListenerProxy& operator= (const ButtonListenerProxy&);
};

DocumentWindow::DocumentWindow (const String& title,
								const Colour& backgroundColour,
								const int requiredButtons_,
								const bool addToDesktop_)
	: ResizableWindow (title, backgroundColour, addToDesktop_),
	  titleBarHeight (26),
	  menuBarHeight (24),
	  requiredButtons (requiredButtons_),
#if JUCE_MAC
	  positionTitleBarButtonsOnLeft (true),
#else
	  positionTitleBarButtonsOnLeft (false),
#endif
	  drawTitleTextCentred (true),
	  menuBarModel (0)
{
	setResizeLimits (128, 128, 32768, 32768);

	lookAndFeelChanged();
}

DocumentWindow::~DocumentWindow()
{
	for (int i = numElementsInArray (titleBarButtons); --i >= 0;)
		titleBarButtons[i] = 0;

	menuBar = 0;
}

void DocumentWindow::repaintTitleBar()
{
	const Rectangle<int> titleBarArea (getTitleBarArea());
	repaint (titleBarArea.getX(), titleBarArea.getY(),
			 titleBarArea.getWidth(), titleBarArea.getHeight());
}

void DocumentWindow::setName (const String& newName)
{
	if (newName != getName())
	{
		Component::setName (newName);
		repaintTitleBar();
	}
}

void DocumentWindow::setIcon (const Image* imageToUse)
{
	titleBarIcon = imageToUse != 0 ? imageToUse->createCopy() : 0;
	repaintTitleBar();
}

void DocumentWindow::setTitleBarHeight (const int newHeight)
{
	titleBarHeight = newHeight;
	resized();
	repaintTitleBar();
}

void DocumentWindow::setTitleBarButtonsRequired (const int requiredButtons_,
												 const bool positionTitleBarButtonsOnLeft_)
{
	requiredButtons = requiredButtons_;
	positionTitleBarButtonsOnLeft = positionTitleBarButtonsOnLeft_;
	lookAndFeelChanged();
}

void DocumentWindow::setTitleBarTextCentred (const bool textShouldBeCentred)
{
	drawTitleTextCentred = textShouldBeCentred;
	repaintTitleBar();
}

void DocumentWindow::setMenuBar (MenuBarModel* menuBarModel_,
								 const int menuBarHeight_)
{
	if (menuBarModel != menuBarModel_)
	{
		menuBar = 0;

		menuBarModel = menuBarModel_;
		menuBarHeight = (menuBarHeight_ > 0) ? menuBarHeight_
											 : getLookAndFeel().getDefaultMenuBarHeight();

		if (menuBarModel != 0)
		{
			// (call the Component method directly to avoid the assertion in ResizableWindow)
			Component::addAndMakeVisible (menuBar = new MenuBarComponent (menuBarModel));
			menuBar->setEnabled (isActiveWindow());
		}

		resized();
	}
}

void DocumentWindow::closeButtonPressed()
{
	/*  If you've got a close button, you have to override this method to get
		rid of your window!

		If the window is just a pop-up, you should override this method and make
		it delete the window in whatever way is appropriate for your app. E.g. you
		might just want to call "delete this".

		If your app is centred around this window such that the whole app should quit when
		the window is closed, then you will probably want to use this method as an opportunity
		to call JUCEApplication::quit(), and leave the window to be deleted later by your
		JUCEApplication::shutdown() method. (Doing it this way means that your window will
		still get cleaned-up if the app is quit by some other means (e.g. a cmd-Q on the mac
		or closing it via the taskbar icon on Windows).
	*/
	jassertfalse
}

void DocumentWindow::minimiseButtonPressed()
{
	setMinimised (true);
}

void DocumentWindow::maximiseButtonPressed()
{
	setFullScreen (! isFullScreen());
}

void DocumentWindow::paint (Graphics& g)
{
	ResizableWindow::paint (g);

	if (resizableBorder == 0)
	{
		g.setColour (getBackgroundColour().overlaidWith (Colour (0x80000000)));

		const BorderSize border (getBorderThickness());

		g.fillRect (0, 0, getWidth(), border.getTop());
		g.fillRect (0, border.getTop(), border.getLeft(), getHeight() - border.getTopAndBottom());
		g.fillRect (getWidth() - border.getRight(), border.getTop(), border.getRight(), getHeight() - border.getTopAndBottom());
		g.fillRect (0, getHeight() - border.getBottom(), getWidth(), border.getBottom());
	}

	const Rectangle<int> titleBarArea (getTitleBarArea());
	g.setOrigin (titleBarArea.getX(), titleBarArea.getY());
	g.reduceClipRegion (0, 0, titleBarArea.getWidth(), titleBarArea.getHeight());

	int titleSpaceX1 = 6;
	int titleSpaceX2 = titleBarArea.getWidth() - 6;

	for (int i = 0; i < 3; ++i)
	{
		if (titleBarButtons[i] != 0)
		{
			if (positionTitleBarButtonsOnLeft)
				titleSpaceX1 = jmax (titleSpaceX1, titleBarButtons[i]->getRight() + (getWidth() - titleBarButtons[i]->getRight()) / 8);
			else
				titleSpaceX2 = jmin (titleSpaceX2, titleBarButtons[i]->getX() - (titleBarButtons[i]->getX() / 8));
		}
	}

	getLookAndFeel().drawDocumentWindowTitleBar (*this, g,
												 titleBarArea.getWidth(),
												 titleBarArea.getHeight(),
												 titleSpaceX1,
												 jmax (1, titleSpaceX2 - titleSpaceX1),
												 titleBarIcon,
												 ! drawTitleTextCentred);
}

void DocumentWindow::resized()
{
	ResizableWindow::resized();

	if (titleBarButtons[1] != 0)
		titleBarButtons[1]->setToggleState (isFullScreen(), false);

	const Rectangle<int> titleBarArea (getTitleBarArea());

	getLookAndFeel()
		.positionDocumentWindowButtons (*this,
										titleBarArea.getX(), titleBarArea.getY(),
										titleBarArea.getWidth(), titleBarArea.getHeight(),
										titleBarButtons[0],
										titleBarButtons[1],
										titleBarButtons[2],
										positionTitleBarButtonsOnLeft);

	if (menuBar != 0)
		menuBar->setBounds (titleBarArea.getX(), titleBarArea.getBottom(),
							titleBarArea.getWidth(), menuBarHeight);
}

const BorderSize DocumentWindow::getBorderThickness()
{
	return BorderSize ((isFullScreen() || isUsingNativeTitleBar())
							? 0 : (resizableBorder != 0 ? 4 : 1));
}

const BorderSize DocumentWindow::getContentComponentBorder()
{
	BorderSize border (getBorderThickness());

	border.setTop (border.getTop()
						+ (isUsingNativeTitleBar() ? 0 : titleBarHeight)
						+ (menuBar != 0 ? menuBarHeight : 0));

	return border;
}

int DocumentWindow::getTitleBarHeight() const
{
	return isUsingNativeTitleBar() ? 0 : jmin (titleBarHeight, getHeight() - 4);
}

const Rectangle<int> DocumentWindow::getTitleBarArea()
{
	const BorderSize border (getBorderThickness());

	return Rectangle<int> (border.getLeft(), border.getTop(),
						   getWidth() - border.getLeftAndRight(),
						   getTitleBarHeight());
}

Button* DocumentWindow::getCloseButton() const throw()
{
	return titleBarButtons[2];
}

Button* DocumentWindow::getMinimiseButton() const throw()
{
	return titleBarButtons[0];
}

Button* DocumentWindow::getMaximiseButton() const throw()
{
	return titleBarButtons[1];
}

int DocumentWindow::getDesktopWindowStyleFlags() const
{
	int flags = ResizableWindow::getDesktopWindowStyleFlags();

	if ((requiredButtons & minimiseButton) != 0)
		flags |= ComponentPeer::windowHasMinimiseButton;

	if ((requiredButtons & maximiseButton) != 0)
		flags |= ComponentPeer::windowHasMaximiseButton;

	if ((requiredButtons & closeButton) != 0)
		flags |= ComponentPeer::windowHasCloseButton;

	return flags;
}

void DocumentWindow::lookAndFeelChanged()
{
	int i;
	for (i = numElementsInArray (titleBarButtons); --i >= 0;)
		titleBarButtons[i] = 0;

	if (! isUsingNativeTitleBar())
	{
		titleBarButtons[0] = ((requiredButtons & minimiseButton) != 0)
								? getLookAndFeel().createDocumentWindowButton (minimiseButton) : 0;

		titleBarButtons[1] = ((requiredButtons & maximiseButton) != 0)
								? getLookAndFeel().createDocumentWindowButton (maximiseButton) : 0;

		titleBarButtons[2] = ((requiredButtons & closeButton) != 0)
								? getLookAndFeel().createDocumentWindowButton (closeButton) : 0;

		for (i = 0; i < 3; ++i)
		{
			if (titleBarButtons[i] != 0)
			{
				if (buttonListener == 0)
					buttonListener = new ButtonListenerProxy (*this);

				titleBarButtons[i]->addButtonListener (buttonListener);
				titleBarButtons[i]->setWantsKeyboardFocus (false);

				// (call the Component method directly to avoid the assertion in ResizableWindow)
				Component::addAndMakeVisible (titleBarButtons[i]);
			}
		}

		if (getCloseButton() != 0)
		{
#if JUCE_MAC
			getCloseButton()->addShortcut (KeyPress ('w', ModifierKeys::commandModifier, 0));
#else
			getCloseButton()->addShortcut (KeyPress (KeyPress::F4Key, ModifierKeys::altModifier, 0));
#endif
		}
	}

	activeWindowStatusChanged();

	ResizableWindow::lookAndFeelChanged();
}

void DocumentWindow::parentHierarchyChanged()
{
	lookAndFeelChanged();
}

void DocumentWindow::activeWindowStatusChanged()
{
	ResizableWindow::activeWindowStatusChanged();

	for (int i = numElementsInArray (titleBarButtons); --i >= 0;)
		if (titleBarButtons[i] != 0)
			titleBarButtons[i]->setEnabled (isActiveWindow());

	if (menuBar != 0)
		menuBar->setEnabled (isActiveWindow());
}

void DocumentWindow::mouseDoubleClick (const MouseEvent& e)
{
	if (getTitleBarArea().contains (e.x, e.y)
		 && getMaximiseButton() != 0)
	{
		getMaximiseButton()->triggerClick();
	}
}

void DocumentWindow::userTriedToCloseWindow()
{
	closeButtonPressed();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DocumentWindow.cpp ***/


/*** Start of inlined file: juce_ResizableWindow.cpp ***/
BEGIN_JUCE_NAMESPACE

ResizableWindow::ResizableWindow (const String& name,
								  const bool addToDesktop_)
	: TopLevelWindow (name, addToDesktop_),
	  resizeToFitContent (false),
	  fullscreen (false),
	  lastNonFullScreenPos (50, 50, 256, 256),
	  constrainer (0)
#ifdef JUCE_DEBUG
	  , hasBeenResized (false)
#endif
{
	defaultConstrainer.setMinimumOnscreenAmounts (0x10000, 16, 24, 16);

	lastNonFullScreenPos.setBounds (50, 50, 256, 256);

	if (addToDesktop_)
		Component::addToDesktop (getDesktopWindowStyleFlags());
}

ResizableWindow::ResizableWindow (const String& name,
								  const Colour& backgroundColour_,
								  const bool addToDesktop_)
	: TopLevelWindow (name, addToDesktop_),
	  resizeToFitContent (false),
	  fullscreen (false),
	  lastNonFullScreenPos (50, 50, 256, 256),
	  constrainer (0)
#ifdef JUCE_DEBUG
	  , hasBeenResized (false)
#endif
{
	setBackgroundColour (backgroundColour_);

	defaultConstrainer.setMinimumOnscreenAmounts (0x10000, 16, 24, 16);

	if (addToDesktop_)
		Component::addToDesktop (getDesktopWindowStyleFlags());
}

ResizableWindow::~ResizableWindow()
{
	resizableCorner = 0;
	resizableBorder = 0;
	contentComponent = 0;

	// have you been adding your own components directly to this window..? tut tut tut.
	// Read the instructions for using a ResizableWindow!
	jassert (getNumChildComponents() == 0);
}

int ResizableWindow::getDesktopWindowStyleFlags() const
{
	int flags = TopLevelWindow::getDesktopWindowStyleFlags();

	if (isResizable() && (flags & ComponentPeer::windowHasTitleBar) != 0)
		flags |= ComponentPeer::windowIsResizable;

	return flags;
}

void ResizableWindow::setContentComponent (Component* const newContentComponent,
										   const bool deleteOldOne,
										   const bool resizeToFit)
{
	resizeToFitContent = resizeToFit;

	if (newContentComponent != static_cast <Component*> (contentComponent))
	{
		if (! deleteOldOne)
			removeChildComponent (contentComponent.release());

		contentComponent = newContentComponent;

		Component::addAndMakeVisible (contentComponent);
	}

	if (resizeToFit)
		childBoundsChanged (contentComponent);

	resized(); // must always be called to position the new content comp
}

void ResizableWindow::setContentComponentSize (int width, int height)
{
	jassert (width > 0 && height > 0); // not a great idea to give it a zero size..

	const BorderSize border (getContentComponentBorder());

	setSize (width + border.getLeftAndRight(),
			 height + border.getTopAndBottom());
}

const BorderSize ResizableWindow::getBorderThickness()
{
	return BorderSize (isUsingNativeTitleBar() ? 0 : ((resizableBorder != 0 && ! isFullScreen()) ? 5 : 3));
}

const BorderSize ResizableWindow::getContentComponentBorder()
{
	return getBorderThickness();
}

void ResizableWindow::moved()
{
	updateLastPos();
}

void ResizableWindow::visibilityChanged()
{
	TopLevelWindow::visibilityChanged();

	updateLastPos();
}

void ResizableWindow::resized()
{
	if (resizableBorder != 0)
	{
		resizableBorder->setVisible (! isFullScreen());
		resizableBorder->setBorderThickness (getBorderThickness());

		resizableBorder->setSize (getWidth(), getHeight());
		resizableBorder->toBack();
	}

	if (resizableCorner != 0)
	{
		resizableCorner->setVisible (! isFullScreen());

		const int resizerSize = 18;
		resizableCorner->setBounds (getWidth() - resizerSize,
									getHeight() - resizerSize,
									resizerSize, resizerSize);
	}

	if (contentComponent != 0)
		contentComponent->setBoundsInset (getContentComponentBorder());

	updateLastPos();

#ifdef JUCE_DEBUG
	hasBeenResized = true;
#endif
}

void ResizableWindow::childBoundsChanged (Component* child)
{
	if ((child == contentComponent) && (child != 0) && resizeToFitContent)
	{
		// not going to look very good if this component has a zero size..
		jassert (child->getWidth() > 0);
		jassert (child->getHeight() > 0);

		const BorderSize borders (getContentComponentBorder());

		setSize (child->getWidth() + borders.getLeftAndRight(),
				 child->getHeight() + borders.getTopAndBottom());
	}
}

void ResizableWindow::activeWindowStatusChanged()
{
	const BorderSize borders (getContentComponentBorder());

	repaint (0, 0, getWidth(), borders.getTop());
	repaint (0, borders.getTop(), borders.getLeft(), getHeight() - borders.getBottom() - borders.getTop());
	repaint (0, getHeight() - borders.getBottom(), getWidth(), borders.getBottom());
	repaint (getWidth() - borders.getRight(), borders.getTop(), borders.getRight(), getHeight() - borders.getBottom() - borders.getTop());
}

void ResizableWindow::setResizable (const bool shouldBeResizable,
									const bool useBottomRightCornerResizer)
{
	if (shouldBeResizable)
	{
		if (useBottomRightCornerResizer)
		{
			resizableBorder = 0;

			if (resizableCorner == 0)
			{
				Component::addChildComponent (resizableCorner = new ResizableCornerComponent (this, constrainer));
				resizableCorner->setAlwaysOnTop (true);
			}
		}
		else
		{
			resizableCorner = 0;

			if (resizableBorder == 0)
				Component::addChildComponent (resizableBorder = new ResizableBorderComponent (this, constrainer));
		}
	}
	else
	{
		resizableCorner = 0;
		resizableBorder = 0;
	}

	if (isUsingNativeTitleBar())
		recreateDesktopWindow();

	childBoundsChanged (contentComponent);
	resized();
}

bool ResizableWindow::isResizable() const throw()
{
	return resizableCorner != 0
		|| resizableBorder != 0;
}

void ResizableWindow::setResizeLimits (const int newMinimumWidth,
									   const int newMinimumHeight,
									   const int newMaximumWidth,
									   const int newMaximumHeight) throw()
{
	// if you've set up a custom constrainer then these settings won't have any effect..
	jassert (constrainer == &defaultConstrainer || constrainer == 0);

	if (constrainer == 0)
		setConstrainer (&defaultConstrainer);

	defaultConstrainer.setSizeLimits (newMinimumWidth, newMinimumHeight,
									  newMaximumWidth, newMaximumHeight);

	setBoundsConstrained (getBounds());
}

void ResizableWindow::setConstrainer (ComponentBoundsConstrainer* newConstrainer)
{
	if (constrainer != newConstrainer)
	{
		constrainer = newConstrainer;

		const bool useBottomRightCornerResizer = resizableCorner != 0;
		const bool shouldBeResizable = useBottomRightCornerResizer || resizableBorder != 0;

		resizableCorner = 0;
		resizableBorder = 0;

		setResizable (shouldBeResizable, useBottomRightCornerResizer);

		ComponentPeer* const peer = getPeer();
		if (peer != 0)
			peer->setConstrainer (newConstrainer);
	}
}

void ResizableWindow::setBoundsConstrained (const Rectangle<int>& bounds)
{
	if (constrainer != 0)
		constrainer->setBoundsForComponent (this, bounds, false, false, false, false);
	else
		setBounds (bounds);
}

void ResizableWindow::paint (Graphics& g)
{
	getLookAndFeel().fillResizableWindowBackground (g, getWidth(), getHeight(),
													getBorderThickness(), *this);

	if (! isFullScreen())
	{
		getLookAndFeel().drawResizableWindowBorder (g, getWidth(), getHeight(),
													getBorderThickness(), *this);
	}

#ifdef JUCE_DEBUG
	/* If this fails, then you've probably written a subclass with a resized()
	   callback but forgotten to make it call its parent class's resized() method.

	   It's important when you override methods like resized(), moved(),
	   etc., that you make sure the base class methods also get called.

	   Of course you shouldn't really be overriding ResizableWindow::resized() anyway,
	   because your content should all be inside the content component - and it's the
	   content component's resized() method that you should be using to do your
	   layout.
	*/
	jassert (hasBeenResized || (getWidth() == 0 && getHeight() == 0));
#endif
}

void ResizableWindow::lookAndFeelChanged()
{
	resized();

	if (isOnDesktop())
	{
		Component::addToDesktop (getDesktopWindowStyleFlags());

		ComponentPeer* const peer = getPeer();
		if (peer != 0)
			peer->setConstrainer (constrainer);
	}
}

const Colour ResizableWindow::getBackgroundColour() const throw()
{
	return findColour (backgroundColourId, false);
}

void ResizableWindow::setBackgroundColour (const Colour& newColour)
{
	Colour backgroundColour (newColour);

	if (! Desktop::canUseSemiTransparentWindows())
		backgroundColour = newColour.withAlpha (1.0f);

	setColour (backgroundColourId, backgroundColour);

	setOpaque (backgroundColour.isOpaque());
	repaint();
}

bool ResizableWindow::isFullScreen() const
{
	if (isOnDesktop())
	{
		ComponentPeer* const peer = getPeer();
		return peer != 0 && peer->isFullScreen();
	}

	return fullscreen;
}

void ResizableWindow::setFullScreen (const bool shouldBeFullScreen)
{
	if (shouldBeFullScreen != isFullScreen())
	{
		updateLastPos();
		fullscreen = shouldBeFullScreen;

		if (isOnDesktop())
		{
			ComponentPeer* const peer = getPeer();

			if (peer != 0)
			{
				// keep a copy of this intact in case the real one gets messed-up while we're un-maximising
				const Rectangle<int> lastPos (lastNonFullScreenPos);

				peer->setFullScreen (shouldBeFullScreen);

				if (! shouldBeFullScreen)
					setBounds (lastPos);
			}
			else
			{
				jassertfalse
			}
		}
		else
		{
			if (shouldBeFullScreen)
				setBounds (0, 0, getParentWidth(), getParentHeight());
			else
				setBounds (lastNonFullScreenPos);
		}

		resized();
	}
}

bool ResizableWindow::isMinimised() const
{
	ComponentPeer* const peer = getPeer();

	return (peer != 0) && peer->isMinimised();
}

void ResizableWindow::setMinimised (const bool shouldMinimise)
{
	if (shouldMinimise != isMinimised())
	{
		ComponentPeer* const peer = getPeer();

		if (peer != 0)
		{
			updateLastPos();
			peer->setMinimised (shouldMinimise);
		}
		else
		{
			jassertfalse
		}
	}
}

void ResizableWindow::updateLastPos()
{
	if (isShowing() && ! (isFullScreen() || isMinimised()))
	{
		lastNonFullScreenPos = getBounds();
	}
}

void ResizableWindow::parentSizeChanged()
{
	if (isFullScreen() && getParentComponent() != 0)
	{
		setBounds (0, 0, getParentWidth(), getParentHeight());
	}
}

const String ResizableWindow::getWindowStateAsString()
{
	updateLastPos();
	return (isFullScreen() ? "fs " : "") + lastNonFullScreenPos.toString();
}

bool ResizableWindow::restoreWindowStateFromString (const String& s)
{
	StringArray tokens;
	tokens.addTokens (s, false);
	tokens.removeEmptyStrings();
	tokens.trim();

	const bool fs = tokens[0].startsWithIgnoreCase ("fs");
	const int firstCoord = fs ? 1 : 0;

	if (tokens.size() != firstCoord + 4)
		return false;

	Rectangle<int> newPos (tokens[firstCoord].getIntValue(),
						   tokens[firstCoord + 1].getIntValue(),
						   tokens[firstCoord + 2].getIntValue(),
						   tokens[firstCoord + 3].getIntValue());

	if (newPos.isEmpty())
		return false;

	const Rectangle<int> screen (Desktop::getInstance().getMonitorAreaContaining (newPos.getCentre()));

	ComponentPeer* const peer = isOnDesktop() ? getPeer() : 0;
	if (peer != 0)
		peer->getFrameSize().addTo (newPos);

	if (! screen.contains (newPos))
	{
		newPos.setSize (jmin (newPos.getWidth(), screen.getWidth()),
						jmin (newPos.getHeight(), screen.getHeight()));

		newPos.setPosition (jlimit (screen.getX(), screen.getRight() - newPos.getWidth(), newPos.getX()),
							jlimit (screen.getY(), screen.getBottom() - newPos.getHeight(), newPos.getY()));
	}

	if (peer != 0)
	{
		peer->getFrameSize().subtractFrom (newPos);
		peer->setNonFullScreenBounds (newPos);
	}

	lastNonFullScreenPos = newPos;
	setFullScreen (fs);

	if (! fs)
		setBoundsConstrained (newPos);

	return true;
}

void ResizableWindow::mouseDown (const MouseEvent&)
{
	if (! isFullScreen())
		dragger.startDraggingComponent (this, constrainer);
}

void ResizableWindow::mouseDrag (const MouseEvent& e)
{
	if (! isFullScreen())
		dragger.dragComponent (this, e);
}

#ifdef JUCE_DEBUG
void ResizableWindow::addChildComponent (Component* const child, int zOrder)
{
	/* Agh! You shouldn't add components directly to a ResizableWindow - this class
	   manages its child components automatically, and if you add your own it'll cause
	   trouble. Instead, use setContentComponent() to give it a component which
	   will be automatically resized and kept in the right place - then you can add
	   subcomponents to the content comp. See the notes for the ResizableWindow class
	   for more info.

	   If you really know what you're doing and want to avoid this assertion, just call
	   Component::addChildComponent directly.
   */
	jassertfalse

	Component::addChildComponent (child, zOrder);
}

void ResizableWindow::addAndMakeVisible (Component* const child, int zOrder)
{
	/* Agh! You shouldn't add components directly to a ResizableWindow - this class
	   manages its child components automatically, and if you add your own it'll cause
	   trouble. Instead, use setContentComponent() to give it a component which
	   will be automatically resized and kept in the right place - then you can add
	   subcomponents to the content comp. See the notes for the ResizableWindow class
	   for more info.

	   If you really know what you're doing and want to avoid this assertion, just call
	   Component::addAndMakeVisible directly.
   */
	jassertfalse

	Component::addAndMakeVisible (child, zOrder);
}
#endif

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ResizableWindow.cpp ***/


/*** Start of inlined file: juce_SplashScreen.cpp ***/
BEGIN_JUCE_NAMESPACE

SplashScreen::SplashScreen()
	: backgroundImage (0)
{
	setOpaque (true);
}

SplashScreen::~SplashScreen()
{
	ImageCache::releaseOrDelete (backgroundImage);
}

void SplashScreen::show (const String& title,
						 Image* const backgroundImage_,
						 const int minimumTimeToDisplayFor,
						 const bool useDropShadow,
						 const bool removeOnMouseClick)
{
	backgroundImage = backgroundImage_;

	jassert (backgroundImage_ != 0);

	if (backgroundImage_ != 0)
	{
		setOpaque (! backgroundImage_->hasAlphaChannel());

		show (title,
			  backgroundImage_->getWidth(),
			  backgroundImage_->getHeight(),
			  minimumTimeToDisplayFor,
			  useDropShadow,
			  removeOnMouseClick);
	}
}

void SplashScreen::show (const String& title,
						 const int width,
						 const int height,
						 const int minimumTimeToDisplayFor,
						 const bool useDropShadow,
						 const bool removeOnMouseClick)
{
	setName (title);
	setAlwaysOnTop (true);
	setVisible (true);
	centreWithSize (width, height);

	addToDesktop (useDropShadow ? ComponentPeer::windowHasDropShadow : 0);
	toFront (false);

	MessageManager::getInstance()->runDispatchLoopUntil (300);

	repaint();

	originalClickCounter = removeOnMouseClick
								? Desktop::getMouseButtonClickCounter()
								: std::numeric_limits<int>::max();

	earliestTimeToDelete = Time::getCurrentTime() + RelativeTime::milliseconds (minimumTimeToDisplayFor);

	startTimer (50);
}

void SplashScreen::paint (Graphics& g)
{
	if (backgroundImage != 0)
	{
		g.setOpacity (1.0f);

		g.drawImage (backgroundImage,
					 0, 0, getWidth(), getHeight(),
					 0, 0, backgroundImage->getWidth(), backgroundImage->getHeight());
	}
}

void SplashScreen::timerCallback()
{
	if (Time::getCurrentTime() > earliestTimeToDelete
		 || Desktop::getMouseButtonClickCounter() > originalClickCounter)
	{
		delete this;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_SplashScreen.cpp ***/


/*** Start of inlined file: juce_ThreadWithProgressWindow.cpp ***/
BEGIN_JUCE_NAMESPACE

ThreadWithProgressWindow::ThreadWithProgressWindow (const String& title,
													const bool hasProgressBar,
													const bool hasCancelButton,
													const int timeOutMsWhenCancelling_,
													const String& cancelButtonText)
  : Thread ("Juce Progress Window"),
	progress (0.0),
	timeOutMsWhenCancelling (timeOutMsWhenCancelling_)
{
	alertWindow = LookAndFeel::getDefaultLookAndFeel()
					.createAlertWindow (title, String::empty, cancelButtonText,
										String::empty, String::empty,
										AlertWindow::NoIcon, hasCancelButton ? 1 : 0, 0);

	if (hasProgressBar)
		alertWindow->addProgressBarComponent (progress);
}

ThreadWithProgressWindow::~ThreadWithProgressWindow()
{
	stopThread (timeOutMsWhenCancelling);
}

bool ThreadWithProgressWindow::runThread (const int priority)
{
	startThread (priority);
	startTimer (100);

	{
		const ScopedLock sl (messageLock);
		alertWindow->setMessage (message);
	}

	const bool finishedNaturally = alertWindow->runModalLoop() != 0;

	stopThread (timeOutMsWhenCancelling);

	alertWindow->setVisible (false);

	return finishedNaturally;
}

void ThreadWithProgressWindow::setProgress (const double newProgress)
{
	progress = newProgress;
}

void ThreadWithProgressWindow::setStatusMessage (const String& newStatusMessage)
{
	const ScopedLock sl (messageLock);
	message = newStatusMessage;
}

void ThreadWithProgressWindow::timerCallback()
{
	if (! isThreadRunning())
	{
		// thread has finished normally..
		alertWindow->exitModalState (1);
		alertWindow->setVisible (false);
	}
	else
	{
		const ScopedLock sl (messageLock);
		alertWindow->setMessage (message);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ThreadWithProgressWindow.cpp ***/


/*** Start of inlined file: juce_TooltipWindow.cpp ***/
BEGIN_JUCE_NAMESPACE

TooltipWindow::TooltipWindow (Component* const parentComponent,
							  const int millisecondsBeforeTipAppears_)
	: Component ("tooltip"),
	  millisecondsBeforeTipAppears (millisecondsBeforeTipAppears_),
	  mouseClicks (0),
	  lastHideTime (0),
	  lastComponentUnderMouse (0),
	  changedCompsSinceShown (true)
{
	if (Desktop::getInstance().getMainMouseSource().canHover())
		startTimer (123);

	setAlwaysOnTop (true);
	setOpaque (true);

	if (parentComponent != 0)
		parentComponent->addChildComponent (this);
}

TooltipWindow::~TooltipWindow()
{
	hide();
}

void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) throw()
{
	millisecondsBeforeTipAppears = newTimeMs;
}

void TooltipWindow::paint (Graphics& g)
{
	getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
}

void TooltipWindow::mouseEnter (const MouseEvent&)
{
	hide();
}

void TooltipWindow::showFor (const String& tip)
{
	jassert (tip.isNotEmpty());
	tipShowing = tip;

	Point<int> mousePos (Desktop::getMousePosition());

	if (getParentComponent() != 0)
		mousePos = getParentComponent()->globalPositionToRelative (mousePos);

	int x, y, w, h;
	getLookAndFeel().getTooltipSize (tip, w, h);

	if (mousePos.getX() > getParentWidth() / 2)
		x = mousePos.getX() - (w + 12);
	else
		x = mousePos.getX() + 24;

	if (mousePos.getY() > getParentHeight() / 2)
		y = mousePos.getY() - (h + 6);
	else
		y = mousePos.getY() + 6;

	setBounds (x, y, w, h);
	setVisible (true);

	if (getParentComponent() == 0)
	{
		addToDesktop (ComponentPeer::windowHasDropShadow
						| ComponentPeer::windowIsTemporary
						| ComponentPeer::windowIgnoresKeyPresses);
	}

	toFront (false);
}

const String TooltipWindow::getTipFor (Component* const c)
{
	if (c != 0
		 && Process::isForegroundProcess()
		 && ! Component::isMouseButtonDownAnywhere())
	{
		TooltipClient* const ttc = dynamic_cast <TooltipClient*> (c);

		if (ttc != 0 && ! c->isCurrentlyBlockedByAnotherModalComponent())
			return ttc->getTooltip();
	}

	return String::empty;
}

void TooltipWindow::hide()
{
	tipShowing = String::empty;
	removeFromDesktop();
	setVisible (false);
}

void TooltipWindow::timerCallback()
{
	const unsigned int now = Time::getApproximateMillisecondCounter();
	Component* const newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
	const String newTip (getTipFor (newComp));

	const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
	lastComponentUnderMouse = newComp;
	lastTipUnderMouse = newTip;

	const int clickCount = Desktop::getInstance().getMouseButtonClickCounter();
	const bool mouseWasClicked = clickCount > mouseClicks;
	mouseClicks = clickCount;

	const Point<int> mousePos (Desktop::getMousePosition());
	const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
	lastMousePos = mousePos;

	if (tipChanged || mouseWasClicked || mouseMovedQuickly)
		lastCompChangeTime = now;

	if (isVisible() || now < lastHideTime + 500)
	{
		// if a tip is currently visible (or has just disappeared), update to a new one
		// immediately if needed..
		if (newComp == 0 || mouseWasClicked || newTip.isEmpty())
		{
			if (isVisible())
			{
				lastHideTime = now;
				hide();
			}
		}
		else if (tipChanged)
		{
			showFor (newTip);
		}
	}
	else
	{
		// if there isn't currently a tip, but one is needed, only let it
		// appear after a timeout..
		if (newTip.isNotEmpty()
			 && newTip != tipShowing
			 && now > lastCompChangeTime + millisecondsBeforeTipAppears)
		{
			showFor (newTip);
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TooltipWindow.cpp ***/


/*** Start of inlined file: juce_TopLevelWindow.cpp ***/
BEGIN_JUCE_NAMESPACE

class TopLevelWindowManager  : public Timer,
							   public DeletedAtShutdown
{
public:

	TopLevelWindowManager()
		: currentActive (0)
	{
	}

	~TopLevelWindowManager()
	{
		clearSingletonInstance();
	}

	juce_DeclareSingleton_SingleThreaded_Minimal (TopLevelWindowManager)

	void timerCallback()
	{
		startTimer (jmin (1731, getTimerInterval() * 2));

		TopLevelWindow* active = 0;

		if (Process::isForegroundProcess())
		{
			active = currentActive;

			Component* const c = Component::getCurrentlyFocusedComponent();

			TopLevelWindow* tlw = dynamic_cast <TopLevelWindow*> (c);

			if (tlw == 0 && c != 0)
				// (unable to use the syntax findParentComponentOfClass <TopLevelWindow> () because of a VC6 compiler bug)
				tlw = c->findParentComponentOfClass ((TopLevelWindow*) 0);

			if (tlw != 0)
				active = tlw;
		}

		if (active != currentActive)
		{
			currentActive = active;

			for (int i = windows.size(); --i >= 0;)
			{
				TopLevelWindow* const tlw = (TopLevelWindow*) windows.getUnchecked (i);
				tlw->setWindowActive (isWindowActive (tlw));

				i = jmin (i, windows.size() - 1);
			}

			Desktop::getInstance().triggerFocusCallback();
		}
	}

	bool addWindow (TopLevelWindow* const w) throw()
	{
		windows.add (w);
		startTimer (10);

		return isWindowActive (w);
	}

	void removeWindow (TopLevelWindow* const w) throw()
	{
		startTimer (10);

		if (currentActive == w)
			currentActive = 0;

		windows.removeValue (w);

		if (windows.size() == 0)
			deleteInstance();
	}

	VoidArray windows;

private:
	TopLevelWindow* currentActive;

	bool isWindowActive (TopLevelWindow* const tlw) const throw()
	{
		return (tlw == currentActive
				 || tlw->isParentOf (currentActive)
				 || tlw->hasKeyboardFocus (true))
				&& tlw->isShowing();
	}

	TopLevelWindowManager (const TopLevelWindowManager&);
	TopLevelWindowManager& operator= (const TopLevelWindowManager&);
};

juce_ImplementSingleton_SingleThreaded (TopLevelWindowManager)

void juce_CheckCurrentlyFocusedTopLevelWindow()
{
	if (TopLevelWindowManager::getInstanceWithoutCreating() != 0)
		TopLevelWindowManager::getInstanceWithoutCreating()->startTimer (20);
}

TopLevelWindow::TopLevelWindow (const String& name,
								const bool addToDesktop_)
	: Component (name),
	  useDropShadow (true),
	  useNativeTitleBar (false),
	  windowIsActive_ (false)
{
	setOpaque (true);

	if (addToDesktop_)
		Component::addToDesktop (getDesktopWindowStyleFlags());
	else
		setDropShadowEnabled (true);

	setWantsKeyboardFocus (true);
	setBroughtToFrontOnMouseClick (true);
	windowIsActive_ = TopLevelWindowManager::getInstance()->addWindow (this);
}

TopLevelWindow::~TopLevelWindow()
{
	shadower = 0;
	TopLevelWindowManager::getInstance()->removeWindow (this);
}

void TopLevelWindow::focusOfChildComponentChanged (FocusChangeType)
{
	if (hasKeyboardFocus (true))
		TopLevelWindowManager::getInstance()->timerCallback();
	else
		TopLevelWindowManager::getInstance()->startTimer (10);
}

void TopLevelWindow::setWindowActive (const bool isNowActive) throw()
{
	if (windowIsActive_ != isNowActive)
	{
		windowIsActive_ = isNowActive;
		activeWindowStatusChanged();
	}
}

void TopLevelWindow::activeWindowStatusChanged()
{
}

void TopLevelWindow::parentHierarchyChanged()
{
	setDropShadowEnabled (useDropShadow);
}

void TopLevelWindow::visibilityChanged()
{
	if (isShowing())
		toFront (true);
}

int TopLevelWindow::getDesktopWindowStyleFlags() const
{
	int flags = ComponentPeer::windowAppearsOnTaskbar;

	if (useDropShadow)
		flags |= ComponentPeer::windowHasDropShadow;

	if (useNativeTitleBar)
		flags |= ComponentPeer::windowHasTitleBar;

	return flags;
}

void TopLevelWindow::setDropShadowEnabled (const bool useShadow)
{
	useDropShadow = useShadow;

	if (isOnDesktop())
	{
		shadower = 0;
		Component::addToDesktop (getDesktopWindowStyleFlags());
	}
	else
	{
		if (useShadow && isOpaque())
		{
			if (shadower == 0)
			{
				shadower = getLookAndFeel().createDropShadowerForComponent (this);

				if (shadower != 0)
					shadower->setOwner (this);
			}
		}
		else
		{
			shadower = 0;
		}
	}
}

void TopLevelWindow::setUsingNativeTitleBar (const bool useNativeTitleBar_)
{
	if (useNativeTitleBar != useNativeTitleBar_)
	{
		useNativeTitleBar = useNativeTitleBar_;
		recreateDesktopWindow();
		sendLookAndFeelChange();
	}
}

void TopLevelWindow::recreateDesktopWindow()
{
	if (isOnDesktop())
	{
		Component::addToDesktop (getDesktopWindowStyleFlags());
		toFront (true);
	}
}

void TopLevelWindow::addToDesktop (int windowStyleFlags, void* nativeWindowToAttachTo)
{
	/* It's not recommended to change the desktop window flags directly for a TopLevelWindow,
	   because this class needs to make sure its layout corresponds with settings like whether
	   it's got a native title bar or not.

	   If you need custom flags for your window, you can override the getDesktopWindowStyleFlags()
	   method. If you do this, it's best to call the base class's getDesktopWindowStyleFlags()
	   method, then add or remove whatever flags are necessary from this value before returning it.
	*/

	jassert ((windowStyleFlags & ~ComponentPeer::windowIsSemiTransparent)
			   == (getDesktopWindowStyleFlags() & ~ComponentPeer::windowIsSemiTransparent));

	Component::addToDesktop (windowStyleFlags, nativeWindowToAttachTo);

	if (windowStyleFlags != getDesktopWindowStyleFlags())
		sendLookAndFeelChange();
}

void TopLevelWindow::centreAroundComponent (Component* c, const int width, const int height)
{
	if (c == 0)
		c = TopLevelWindow::getActiveTopLevelWindow();

	if (c == 0)
	{
		centreWithSize (width, height);
	}
	else
	{
		Point<int> p (c->relativePositionToGlobal (Point<int> ((c->getWidth() - width) / 2,
															   (c->getHeight() - height) / 2)));

		Rectangle<int> parentArea (c->getParentMonitorArea());

		if (getParentComponent() != 0)
		{
			p = getParentComponent()->globalPositionToRelative (p);
			parentArea.setBounds (0, 0, getParentWidth(), getParentHeight());
		}

		parentArea.reduce (12, 12);

		setBounds (jlimit (parentArea.getX(), jmax (parentArea.getX(), parentArea.getRight() - width), p.getX()),
				   jlimit (parentArea.getY(), jmax (parentArea.getY(), parentArea.getBottom() - height), p.getY()),
				   width, height);
	}
}

int TopLevelWindow::getNumTopLevelWindows() throw()
{
	return TopLevelWindowManager::getInstance()->windows.size();
}

TopLevelWindow* TopLevelWindow::getTopLevelWindow (const int index) throw()
{
	return static_cast <TopLevelWindow*> (TopLevelWindowManager::getInstance()->windows [index]);
}

TopLevelWindow* TopLevelWindow::getActiveTopLevelWindow() throw()
{
	TopLevelWindow* best = 0;
	int bestNumTWLParents = -1;

	for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
	{
		TopLevelWindow* const tlw = TopLevelWindow::getTopLevelWindow (i);

		if (tlw->isActiveWindow())
		{
			int numTWLParents = 0;

			const Component* c = tlw->getParentComponent();

			while (c != 0)
			{
				if (dynamic_cast <const TopLevelWindow*> (c) != 0)
					++numTWLParents;

				c = c->getParentComponent();
			}

			if (bestNumTWLParents < numTWLParents)
			{
				best = tlw;
				bestNumTWLParents = numTWLParents;
			}
		}
	}

	return best;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TopLevelWindow.cpp ***/


/*** Start of inlined file: juce_Colour.cpp ***/
BEGIN_JUCE_NAMESPACE

namespace ColourHelpers
{
	static uint8 floatAlphaToInt (const float alpha) throw()
	{
		return (uint8) jlimit (0, 0xff, roundToInt (alpha * 255.0f));
	}

	static void convertHSBtoRGB (float h, float s, float v,
								 uint8& r, uint8& g, uint8& b) throw()
	{
		v = jlimit (0.0f, 1.0f, v);
		v *= 255.0f;
		const uint8 intV = (uint8) roundToInt (v);

		if (s <= 0)
		{
			r = intV;
			g = intV;
			b = intV;
		}
		else
		{
			s = jmin (1.0f, s);
			h = jlimit (0.0f, 1.0f, h);
			h = (h - floorf (h)) * 6.0f + 0.00001f; // need a small adjustment to compensate for rounding errors
			const float f = h - floorf (h);

			const uint8 x = (uint8) roundToInt (v * (1.0f - s));
			const float y = v * (1.0f - s * f);
			const float z = v * (1.0f - (s * (1.0f - f)));

			if (h < 1.0f)
			{
				r = intV;
				g = (uint8) roundToInt (z);
				b = x;
			}
			else if (h < 2.0f)
			{
				r = (uint8) roundToInt (y);
				g = intV;
				b = x;
			}
			else if (h < 3.0f)
			{
				r = x;
				g = intV;
				b = (uint8) roundToInt (z);
			}
			else if (h < 4.0f)
			{
				r = x;
				g = (uint8) roundToInt (y);
				b = intV;
			}
			else if (h < 5.0f)
			{
				r = (uint8) roundToInt (z);
				g = x;
				b = intV;
			}
			else if (h < 6.0f)
			{
				r = intV;
				g = x;
				b = (uint8) roundToInt (y);
			}
			else
			{
				r = 0;
				g = 0;
				b = 0;
			}
		}
	}
}

Colour::Colour() throw()
	: argb (0)
{
}

Colour::Colour (const Colour& other) throw()
	: argb (other.argb)
{
}

Colour& Colour::operator= (const Colour& other) throw()
{
	argb = other.argb;
	return *this;
}

bool Colour::operator== (const Colour& other) const throw()
{
	return argb.getARGB() == other.argb.getARGB();
}

bool Colour::operator!= (const Colour& other) const throw()
{
	return argb.getARGB() != other.argb.getARGB();
}

Colour::Colour (const uint32 argb_) throw()
	: argb (argb_)
{
}

Colour::Colour (const uint8 red,
				const uint8 green,
				const uint8 blue) throw()
{
	argb.setARGB (0xff, red, green, blue);
}

const Colour Colour::fromRGB (const uint8 red,
							  const uint8 green,
							  const uint8 blue) throw()
{
	return Colour (red, green, blue);
}

Colour::Colour (const uint8 red,
				const uint8 green,
				const uint8 blue,
				const uint8 alpha) throw()
{
	argb.setARGB (alpha, red, green, blue);
}

const Colour Colour::fromRGBA (const uint8 red,
							   const uint8 green,
							   const uint8 blue,
							   const uint8 alpha) throw()
{
	return Colour (red, green, blue, alpha);
}

Colour::Colour (const uint8 red,
				const uint8 green,
				const uint8 blue,
				const float alpha) throw()
{
	argb.setARGB (ColourHelpers::floatAlphaToInt (alpha), red, green, blue);
}

const Colour Colour::fromRGBAFloat (const uint8 red,
									const uint8 green,
									const uint8 blue,
									const float alpha) throw()
{
	return Colour (red, green, blue, alpha);
}

Colour::Colour (const float hue,
				const float saturation,
				const float brightness,
				const float alpha) throw()
{
	uint8 r = getRed(), g = getGreen(), b = getBlue();
	ColourHelpers::convertHSBtoRGB (hue, saturation, brightness, r, g, b);

	argb.setARGB (ColourHelpers::floatAlphaToInt (alpha), r, g, b);
}

const Colour Colour::fromHSV (const float hue,
							  const float saturation,
							  const float brightness,
							  const float alpha) throw()
{
	return Colour (hue, saturation, brightness, alpha);
}

Colour::Colour (const float hue,
				const float saturation,
				const float brightness,
				const uint8 alpha) throw()
{
	uint8 r = getRed(), g = getGreen(), b = getBlue();
	ColourHelpers::convertHSBtoRGB (hue, saturation, brightness, r, g, b);

	argb.setARGB (alpha, r, g, b);
}

Colour::~Colour() throw()
{
}

const PixelARGB Colour::getPixelARGB() const throw()
{
	PixelARGB p (argb);
	p.premultiply();
	return p;
}

uint32 Colour::getARGB() const throw()
{
	return argb.getARGB();
}

bool Colour::isTransparent() const throw()
{
	return getAlpha() == 0;
}

bool Colour::isOpaque() const throw()
{
	return getAlpha() == 0xff;
}

const Colour Colour::withAlpha (const uint8 newAlpha) const throw()
{
	PixelARGB newCol (argb);
	newCol.setAlpha (newAlpha);
	return Colour (newCol.getARGB());
}

const Colour Colour::withAlpha (const float newAlpha) const throw()
{
	jassert (newAlpha >= 0 && newAlpha <= 1.0f);

	PixelARGB newCol (argb);
	newCol.setAlpha (ColourHelpers::floatAlphaToInt (newAlpha));
	return Colour (newCol.getARGB());
}

const Colour Colour::withMultipliedAlpha (const float alphaMultiplier) const throw()
{
	jassert (alphaMultiplier >= 0);

	PixelARGB newCol (argb);
	newCol.setAlpha ((uint8) jmin (0xff, roundToInt (alphaMultiplier * newCol.getAlpha())));
	return Colour (newCol.getARGB());
}

const Colour Colour::overlaidWith (const Colour& src) const throw()
{
	const int destAlpha = getAlpha();

	if (destAlpha > 0)
	{
		const int invA = 0xff - (int) src.getAlpha();
		const int resA = 0xff - (((0xff - destAlpha) * invA) >> 8);

		if (resA > 0)
		{
			const int da = (invA * destAlpha) / resA;

			return Colour ((uint8) (src.getRed()   + ((((int) getRed() - src.getRed())	 * da) >> 8)),
						   (uint8) (src.getGreen() + ((((int) getGreen() - src.getGreen()) * da) >> 8)),
						   (uint8) (src.getBlue()  + ((((int) getBlue() - src.getBlue())   * da) >> 8)),
						   (uint8) resA);
		}

		return *this;
	}
	else
	{
		return src;
	}
}

const Colour Colour::interpolatedWith (const Colour& other, float proportionOfOther) const throw()
{
	if (proportionOfOther <= 0)
		return *this;

	if (proportionOfOther >= 1.0f)
		return other;

	PixelARGB c1 (getPixelARGB());
	const PixelARGB c2 (other.getPixelARGB());
	c1.tween (c2, roundToInt (proportionOfOther * 255.0f));
	c1.unpremultiply();

	return Colour (c1.getARGB());
}

float Colour::getFloatRed() const throw()
{
	return getRed() / 255.0f;
}

float Colour::getFloatGreen() const throw()
{
	return getGreen() / 255.0f;
}

float Colour::getFloatBlue() const throw()
{
	return getBlue() / 255.0f;
}

float Colour::getFloatAlpha() const throw()
{
	return getAlpha() / 255.0f;
}

void Colour::getHSB (float& h, float& s, float& v) const throw()
{
	const int r = getRed();
	const int g = getGreen();
	const int b = getBlue();

	const int hi = jmax (r, g, b);
	const int lo = jmin (r, g, b);

	if (hi != 0)
	{
		s = (hi - lo) / (float) hi;

		if (s != 0)
		{
			const float invDiff = 1.0f / (hi - lo);

			const float red   = (hi - r) * invDiff;
			const float green = (hi - g) * invDiff;
			const float blue  = (hi - b) * invDiff;

			if (r == hi)
				h = blue - green;
			else if (g == hi)
				h = 2.0f + red - blue;
			else
				h = 4.0f + green - red;

			h *= 1.0f / 6.0f;

			if (h < 0)
				++h;
		}
		else
		{
			h = 0;
		}
	}
	else
	{
		s = 0;
		h = 0;
	}

	v = hi / 255.0f;
}

float Colour::getHue() const throw()
{
	float h, s, b;
	getHSB (h, s, b);
	return h;
}

const Colour Colour::withHue (const float hue) const throw()
{
	float h, s, b;
	getHSB (h, s, b);

	return Colour (hue, s, b, getAlpha());
}

const Colour Colour::withRotatedHue (const float amountToRotate) const throw()
{
	float h, s, b;
	getHSB (h, s, b);

	h += amountToRotate;
	h -= floorf (h);

	return Colour (h, s, b, getAlpha());
}

float Colour::getSaturation() const throw()
{
	float h, s, b;
	getHSB (h, s, b);
	return s;
}

const Colour Colour::withSaturation (const float saturation) const throw()
{
	float h, s, b;
	getHSB (h, s, b);

	return Colour (h, saturation, b, getAlpha());
}

const Colour Colour::withMultipliedSaturation (const float amount) const throw()
{
	float h, s, b;
	getHSB (h, s, b);

	return Colour (h, jmin (1.0f, s * amount), b, getAlpha());
}

float Colour::getBrightness() const throw()
{
	float h, s, b;
	getHSB (h, s, b);
	return b;
}

const Colour Colour::withBrightness (const float brightness) const throw()
{
	float h, s, b;
	getHSB (h, s, b);

	return Colour (h, s, brightness, getAlpha());
}

const Colour Colour::withMultipliedBrightness (const float amount) const throw()
{
	float h, s, b;
	getHSB (h, s, b);

	b *= amount;

	if (b > 1.0f)
		b = 1.0f;

	return Colour (h, s, b, getAlpha());
}

const Colour Colour::brighter (float amount) const throw()
{
	amount = 1.0f / (1.0f + amount);

	return Colour ((uint8) (255 - (amount * (255 - getRed()))),
				   (uint8) (255 - (amount * (255 - getGreen()))),
				   (uint8) (255 - (amount * (255 - getBlue()))),
				   getAlpha());
}

const Colour Colour::darker (float amount) const throw()
{
	amount = 1.0f / (1.0f + amount);

	return Colour ((uint8) (amount * getRed()),
				   (uint8) (amount * getGreen()),
				   (uint8) (amount * getBlue()),
				   getAlpha());
}

const Colour Colour::greyLevel (const float brightness) throw()
{
	const uint8 level
		= (uint8) jlimit (0x00, 0xff, roundToInt (brightness * 255.0f));

	return Colour (level, level, level);
}

const Colour Colour::contrasting (const float amount) const throw()
{
	return overlaidWith ((((int) getRed() + (int) getGreen() + (int) getBlue() >= 3 * 128)
							? Colours::black
							: Colours::white).withAlpha (amount));
}

const Colour Colour::contrasting (const Colour& colour1,
								  const Colour& colour2) throw()
{
	const float b1 = colour1.getBrightness();
	const float b2 = colour2.getBrightness();
	float best = 0.0f;
	float bestDist = 0.0f;

	for (float i = 0.0f; i < 1.0f; i += 0.02f)
	{
		const float d1 = fabsf (i - b1);
		const float d2 = fabsf (i - b2);
		const float dist = jmin (d1, d2, 1.0f - d1, 1.0f - d2);

		if (dist > bestDist)
		{
			best = i;
			bestDist = dist;
		}
	}

	return colour1.overlaidWith (colour2.withMultipliedAlpha (0.5f))
				  .withBrightness (best);
}

const String Colour::toString() const
{
	return String::toHexString ((int) argb.getARGB());
}

const Colour Colour::fromString (const String& encodedColourString)
{
	return Colour ((uint32) encodedColourString.getHexValue32());
}

const String Colour::toDisplayString (const bool includeAlphaValue) const
{
	return String::toHexString ((int) (argb.getARGB() & (includeAlphaValue ? 0xffffffff : 0xffffff)))
				  .paddedLeft ('0', includeAlphaValue ? 8 : 6)
				  .toUpperCase();
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Colour.cpp ***/


/*** Start of inlined file: juce_ColourGradient.cpp ***/
BEGIN_JUCE_NAMESPACE

ColourGradient::ColourGradient() throw()
{
#ifdef JUCE_DEBUG
	x1 = 987654.0f;
#endif
}

ColourGradient::ColourGradient (const Colour& colour1,
								const float x1_,
								const float y1_,
								const Colour& colour2,
								const float x2_,
								const float y2_,
								const bool isRadial_) throw()
	: x1 (x1_),
	  y1 (y1_),
	  x2 (x2_),
	  y2 (y2_),
	  isRadial (isRadial_)
{
	colours.add (0);
	colours.add (colour1.getARGB());

	colours.add (1 << 16);
	colours.add (colour2.getARGB());
}

ColourGradient::~ColourGradient() throw()
{
}

void ColourGradient::clearColours() throw()
{
	colours.clear();
}

void ColourGradient::addColour (const double proportionAlongGradient,
								const Colour& colour) throw()
{
	// must be within the two end-points
	jassert (proportionAlongGradient >= 0 && proportionAlongGradient <= 1.0);

	const uint32 pos = jlimit (0, 65535, roundToInt (proportionAlongGradient * 65536.0));

	int i;
	for (i = 0; i < colours.size(); i += 2)
		if (colours.getUnchecked(i) > pos)
			break;

	colours.insert (i, pos);
	colours.insert (i + 1, colour.getARGB());
}

void ColourGradient::multiplyOpacity (const float multiplier) throw()
{
	for (int i = 1; i < colours.size(); i += 2)
	{
		const Colour c (colours.getUnchecked(i));
		colours.set (i, c.withMultipliedAlpha (multiplier).getARGB());
	}
}

int ColourGradient::getNumColours() const throw()
{
	return colours.size() >> 1;
}

double ColourGradient::getColourPosition (const int index) const throw()
{
	return jlimit (0.0, 1.0, colours [index << 1] / 65535.0);
 }

const Colour ColourGradient::getColour (const int index) const throw()
{
	return Colour (colours [(index << 1) + 1]);
}

const Colour ColourGradient::getColourAtPosition (const float position) const throw()
{
	jassert (colours.getUnchecked (0) == 0); // the first colour specified has to go at position 0

	const int integerPos = jlimit (0, 65535, roundToInt (position * 65536.0f));

	if (integerPos <= 0 || colours.size() <= 2)
		return getColour (0);

	int i = colours.size() - 2;
	while (integerPos < (int) colours.getUnchecked(i))
		i -= 2;

	if (i >= colours.size() - 2)
		return Colour (colours.getUnchecked(i));

	const int pos1 = colours.getUnchecked (i);
	const Colour col1 (colours.getUnchecked (i + 1));

	const int pos2 = colours.getUnchecked (i + 2);
	const Colour col2 (colours.getUnchecked (i + 3));

	return col1.interpolatedWith (col2, (integerPos - pos1) / (float) (pos2 - pos1));
}

int ColourGradient::createLookupTable (const AffineTransform& transform, HeapBlock <PixelARGB>& lookupTable) const throw()
{
#ifdef JUCE_DEBUG
	// trying to use the object without setting its co-ordinates? Have a careful read of
	// the comments for the constructors.
	jassert (x1 != 987654.0f);
#endif

	const int numColours = colours.size() >> 1;

	float tx1 = x1, ty1 = y1, tx2 = x2, ty2 = y2;
	transform.transformPoint (tx1, ty1);
	transform.transformPoint (tx2, ty2);
	const double distance = juce_hypot (tx1 - tx2, ty1 - ty2);

	const int numEntries = jlimit (1, (numColours - 1) << 8, 3 * (int) distance);
	lookupTable.malloc (numEntries);

	if (numColours >= 2)
	{
		jassert (colours.getUnchecked (0) == 0); // the first colour specified has to go at position 0

		PixelARGB pix1 (colours.getUnchecked (1));
		pix1.premultiply();
		int index = 0;

		for (int j = 2; j < colours.size(); j += 2)
		{
			const int numToDo = ((colours.getUnchecked (j) * (numEntries - 1)) >> 16) - index;
			PixelARGB pix2 (colours.getUnchecked (j + 1));
			pix2.premultiply();

			for (int i = 0; i < numToDo; ++i)
			{
				jassert (index >= 0 && index < numEntries);

				lookupTable[index] = pix1;
				lookupTable[index].tween (pix2, (i << 8) / numToDo);
				++index;
			}

			pix1 = pix2;
		}

		while (index < numEntries)
			lookupTable [index++] = pix1;
	}
	else
	{
		jassertfalse // no colours specified!
	}

	return numEntries;
}

bool ColourGradient::isOpaque() const throw()
{
	for (int i = 1; i < colours.size(); i += 2)
		if (PixelARGB (colours.getUnchecked(i)).getAlpha() < 0xff)
			return false;

	return true;
}

bool ColourGradient::isInvisible() const throw()
{
	for (int i = 1; i < colours.size(); i += 2)
		if (PixelARGB (colours.getUnchecked(i)).getAlpha() > 0)
			return false;

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ColourGradient.cpp ***/


/*** Start of inlined file: juce_Colours.cpp ***/
BEGIN_JUCE_NAMESPACE

const Colour Colours::transparentBlack (0);
const Colour Colours::transparentWhite (0x00ffffff);

const Colour Colours::aliceblue (0xfff0f8ff);
const Colour Colours::antiquewhite (0xfffaebd7);
const Colour Colours::aqua (0xff00ffff);
const Colour Colours::aquamarine (0xff7fffd4);
const Colour Colours::azure (0xfff0ffff);
const Colour Colours::beige (0xfff5f5dc);
const Colour Colours::bisque (0xffffe4c4);
const Colour Colours::black (0xff000000);
const Colour Colours::blanchedalmond (0xffffebcd);
const Colour Colours::blue (0xff0000ff);
const Colour Colours::blueviolet (0xff8a2be2);
const Colour Colours::brown (0xffa52a2a);
const Colour Colours::burlywood (0xffdeb887);
const Colour Colours::cadetblue (0xff5f9ea0);
const Colour Colours::chartreuse (0xff7fff00);
const Colour Colours::chocolate (0xffd2691e);
const Colour Colours::coral (0xffff7f50);
const Colour Colours::cornflowerblue (0xff6495ed);
const Colour Colours::cornsilk (0xfffff8dc);
const Colour Colours::crimson (0xffdc143c);
const Colour Colours::cyan (0xff00ffff);
const Colour Colours::darkblue (0xff00008b);
const Colour Colours::darkcyan (0xff008b8b);
const Colour Colours::darkgoldenrod (0xffb8860b);
const Colour Colours::darkgrey (0xff555555);
const Colour Colours::darkgreen (0xff006400);
const Colour Colours::darkkhaki (0xffbdb76b);
const Colour Colours::darkmagenta (0xff8b008b);
const Colour Colours::darkolivegreen (0xff556b2f);
const Colour Colours::darkorange (0xffff8c00);
const Colour Colours::darkorchid (0xff9932cc);
const Colour Colours::darkred (0xff8b0000);
const Colour Colours::darksalmon (0xffe9967a);
const Colour Colours::darkseagreen (0xff8fbc8f);
const Colour Colours::darkslateblue (0xff483d8b);
const Colour Colours::darkslategrey (0xff2f4f4f);
const Colour Colours::darkturquoise (0xff00ced1);
const Colour Colours::darkviolet (0xff9400d3);
const Colour Colours::deeppink (0xffff1493);
const Colour Colours::deepskyblue (0xff00bfff);
const Colour Colours::dimgrey (0xff696969);
const Colour Colours::dodgerblue (0xff1e90ff);
const Colour Colours::firebrick (0xffb22222);
const Colour Colours::floralwhite (0xfffffaf0);
const Colour Colours::forestgreen (0xff228b22);
const Colour Colours::fuchsia (0xffff00ff);
const Colour Colours::gainsboro (0xffdcdcdc);
const Colour Colours::gold (0xffffd700);
const Colour Colours::goldenrod (0xffdaa520);
const Colour Colours::grey (0xff808080);
const Colour Colours::green (0xff008000);
const Colour Colours::greenyellow (0xffadff2f);
const Colour Colours::honeydew (0xfff0fff0);
const Colour Colours::hotpink (0xffff69b4);
const Colour Colours::indianred (0xffcd5c5c);
const Colour Colours::indigo (0xff4b0082);
const Colour Colours::ivory (0xfffffff0);
const Colour Colours::khaki (0xfff0e68c);
const Colour Colours::lavender (0xffe6e6fa);
const Colour Colours::lavenderblush (0xfffff0f5);
const Colour Colours::lemonchiffon (0xfffffacd);
const Colour Colours::lightblue (0xffadd8e6);
const Colour Colours::lightcoral (0xfff08080);
const Colour Colours::lightcyan (0xffe0ffff);
const Colour Colours::lightgoldenrodyellow (0xfffafad2);
const Colour Colours::lightgreen (0xff90ee90);
const Colour Colours::lightgrey (0xffd3d3d3);
const Colour Colours::lightpink (0xffffb6c1);
const Colour Colours::lightsalmon (0xffffa07a);
const Colour Colours::lightseagreen (0xff20b2aa);
const Colour Colours::lightskyblue (0xff87cefa);
const Colour Colours::lightslategrey (0xff778899);
const Colour Colours::lightsteelblue (0xffb0c4de);
const Colour Colours::lightyellow (0xffffffe0);
const Colour Colours::lime (0xff00ff00);
const Colour Colours::limegreen (0xff32cd32);
const Colour Colours::linen (0xfffaf0e6);
const Colour Colours::magenta (0xffff00ff);
const Colour Colours::maroon (0xff800000);
const Colour Colours::mediumaquamarine (0xff66cdaa);
const Colour Colours::mediumblue (0xff0000cd);
const Colour Colours::mediumorchid (0xffba55d3);
const Colour Colours::mediumpurple (0xff9370db);
const Colour Colours::mediumseagreen (0xff3cb371);
const Colour Colours::mediumslateblue (0xff7b68ee);
const Colour Colours::mediumspringgreen (0xff00fa9a);
const Colour Colours::mediumturquoise (0xff48d1cc);
const Colour Colours::mediumvioletred (0xffc71585);
const Colour Colours::midnightblue (0xff191970);
const Colour Colours::mintcream (0xfff5fffa);
const Colour Colours::mistyrose (0xffffe4e1);
const Colour Colours::navajowhite (0xffffdead);
const Colour Colours::navy (0xff000080);
const Colour Colours::oldlace (0xfffdf5e6);
const Colour Colours::olive (0xff808000);
const Colour Colours::olivedrab (0xff6b8e23);
const Colour Colours::orange (0xffffa500);
const Colour Colours::orangered (0xffff4500);
const Colour Colours::orchid (0xffda70d6);
const Colour Colours::palegoldenrod (0xffeee8aa);
const Colour Colours::palegreen (0xff98fb98);
const Colour Colours::paleturquoise (0xffafeeee);
const Colour Colours::palevioletred (0xffdb7093);
const Colour Colours::papayawhip (0xffffefd5);
const Colour Colours::peachpuff (0xffffdab9);
const Colour Colours::peru (0xffcd853f);
const Colour Colours::pink (0xffffc0cb);
const Colour Colours::plum (0xffdda0dd);
const Colour Colours::powderblue (0xffb0e0e6);
const Colour Colours::purple (0xff800080);
const Colour Colours::red (0xffff0000);
const Colour Colours::rosybrown (0xffbc8f8f);
const Colour Colours::royalblue (0xff4169e1);
const Colour Colours::saddlebrown (0xff8b4513);
const Colour Colours::salmon (0xfffa8072);
const Colour Colours::sandybrown (0xfff4a460);
const Colour Colours::seagreen (0xff2e8b57);
const Colour Colours::seashell (0xfffff5ee);
const Colour Colours::sienna (0xffa0522d);
const Colour Colours::silver (0xffc0c0c0);
const Colour Colours::skyblue (0xff87ceeb);
const Colour Colours::slateblue (0xff6a5acd);
const Colour Colours::slategrey (0xff708090);
const Colour Colours::snow (0xfffffafa);
const Colour Colours::springgreen (0xff00ff7f);
const Colour Colours::steelblue (0xff4682b4);
const Colour Colours::tan (0xffd2b48c);
const Colour Colours::teal (0xff008080);
const Colour Colours::thistle (0xffd8bfd8);
const Colour Colours::tomato (0xffff6347);
const Colour Colours::turquoise (0xff40e0d0);
const Colour Colours::violet (0xffee82ee);
const Colour Colours::wheat (0xfff5deb3);
const Colour Colours::white (0xffffffff);
const Colour Colours::whitesmoke (0xfff5f5f5);
const Colour Colours::yellow (0xffffff00);
const Colour Colours::yellowgreen (0xff9acd32);

const Colour Colours::findColourForName (const String& colourName,
										 const Colour& defaultColour)
{
	static const int presets[] =
	{
		// (first value is the string's hashcode, second is ARGB)

		0x05978fff, 0xff000000, /* black */
		0x06bdcc29, 0xffffffff, /* white */
		0x002e305a, 0xff0000ff, /* blue */
		0x00308adf, 0xff808080, /* grey */
		0x05e0cf03, 0xff008000, /* green */
		0x0001b891, 0xffff0000, /* red */
		0xd43c6474, 0xffffff00, /* yellow */
		0x620886da, 0xfff0f8ff, /* aliceblue */
		0x20a2676a, 0xfffaebd7, /* antiquewhite */
		0x002dcebc, 0xff00ffff, /* aqua */
		0x46bb5f7e, 0xff7fffd4, /* aquamarine */
		0x0590228f, 0xfff0ffff, /* azure */
		0x05947fe4, 0xfff5f5dc, /* beige */
		0xad388e35, 0xffffe4c4, /* bisque */
		0x00674f7e, 0xffffebcd, /* blanchedalmond */
		0x39129959, 0xff8a2be2, /* blueviolet */
		0x059a8136, 0xffa52a2a, /* brown */
		0x89cea8f9, 0xffdeb887, /* burlywood */
		0x0fa260cf, 0xff5f9ea0, /* cadetblue */
		0x6b748956, 0xff7fff00, /* chartreuse */
		0x2903623c, 0xffd2691e, /* chocolate */
		0x05a74431, 0xffff7f50, /* coral */
		0x618d42dd, 0xff6495ed, /* cornflowerblue */
		0xe4b479fd, 0xfffff8dc, /* cornsilk */
		0x3d8c4edf, 0xffdc143c, /* crimson */
		0x002ed323, 0xff00ffff, /* cyan */
		0x67cc74d0, 0xff00008b, /* darkblue */
		0x67cd1799, 0xff008b8b, /* darkcyan */
		0x31bbd168, 0xffb8860b, /* darkgoldenrod */
		0x67cecf55, 0xff555555, /* darkgrey */
		0x920b194d, 0xff006400, /* darkgreen */
		0x923edd4c, 0xffbdb76b, /* darkkhaki */
		0x5c293873, 0xff8b008b, /* darkmagenta */
		0x6b6671fe, 0xff556b2f, /* darkolivegreen */
		0xbcfd2524, 0xffff8c00, /* darkorange */
		0xbcfdf799, 0xff9932cc, /* darkorchid */
		0x55ee0d5b, 0xff8b0000, /* darkred */
		0xc2e5f564, 0xffe9967a, /* darksalmon */
		0x61be858a, 0xff8fbc8f, /* darkseagreen */
		0xc2b0f2bd, 0xff483d8b, /* darkslateblue */
		0xc2b34d42, 0xff2f4f4f, /* darkslategrey */
		0x7cf2b06b, 0xff00ced1, /* darkturquoise */
		0xc8769375, 0xff9400d3, /* darkviolet */
		0x25832862, 0xffff1493, /* deeppink */
		0xfcad568f, 0xff00bfff, /* deepskyblue */
		0x634c8b67, 0xff696969, /* dimgrey */
		0x45c1ce55, 0xff1e90ff, /* dodgerblue */
		0xef19e3cb, 0xffb22222, /* firebrick */
		0xb852b195, 0xfffffaf0, /* floralwhite */
		0xd086fd06, 0xff228b22, /* forestgreen */
		0xe106b6d7, 0xffff00ff, /* fuchsia */
		0x7880d61e, 0xffdcdcdc, /* gainsboro */
		0x00308060, 0xffffd700, /* gold */
		0xb3b3bc1e, 0xffdaa520, /* goldenrod */
		0xbab8a537, 0xffadff2f, /* greenyellow */
		0xe4cacafb, 0xfff0fff0, /* honeydew */
		0x41892743, 0xffff69b4, /* hotpink */
		0xd5796f1a, 0xffcd5c5c, /* indianred */
		0xb969fed2, 0xff4b0082, /* indigo */
		0x05fef6a9, 0xfffffff0, /* ivory */
		0x06149302, 0xfff0e68c, /* khaki */
		0xad5a05c7, 0xffe6e6fa, /* lavender */
		0x7c4d5b99, 0xfffff0f5, /* lavenderblush */
		0x195756f0, 0xfffffacd, /* lemonchiffon */
		0x28e4ea70, 0xffadd8e6, /* lightblue */
		0xf3c7ccdb, 0xfff08080, /* lightcoral */
		0x28e58d39, 0xffe0ffff, /* lightcyan */
		0x21234e3c, 0xfffafad2, /* lightgoldenrodyellow */
		0xf40157ad, 0xff90ee90, /* lightgreen */
		0x28e744f5, 0xffd3d3d3, /* lightgrey */
		0x28eb3b8c, 0xffffb6c1, /* lightpink */
		0x9fb78304, 0xffffa07a, /* lightsalmon */
		0x50632b2a, 0xff20b2aa, /* lightseagreen */
		0x68fb7b25, 0xff87cefa, /* lightskyblue */
		0xa8a35ba2, 0xff778899, /* lightslategrey */
		0xa20d484f, 0xffb0c4de, /* lightsteelblue */
		0xaa2cf10a, 0xffffffe0, /* lightyellow */
		0x0032afd5, 0xff00ff00, /* lime */
		0x607bbc4e, 0xff32cd32, /* limegreen */
		0x06234efa, 0xfffaf0e6, /* linen */
		0x316858a9, 0xffff00ff, /* magenta */
		0xbf8ca470, 0xff800000, /* maroon */
		0xbd58e0b3, 0xff66cdaa, /* mediumaquamarine */
		0x967dfd4f, 0xff0000cd, /* mediumblue */
		0x056f5c58, 0xffba55d3, /* mediumorchid */
		0x07556b71, 0xff9370db, /* mediumpurple */
		0x5369b689, 0xff3cb371, /* mediumseagreen */
		0x066be19e, 0xff7b68ee, /* mediumslateblue */
		0x3256b281, 0xff00fa9a, /* mediumspringgreen */
		0xc0ad9f4c, 0xff48d1cc, /* mediumturquoise */
		0x628e63dd, 0xffc71585, /* mediumvioletred */
		0x168eb32a, 0xff191970, /* midnightblue */
		0x4306b960, 0xfff5fffa, /* mintcream */
		0x4cbc0e6b, 0xffffe4e1, /* mistyrose */
		0xe97218a6, 0xffffdead, /* navajowhite */
		0x00337bb6, 0xff000080, /* navy */
		0xadd2d33e, 0xfffdf5e6, /* oldlace */
		0x064ee1db, 0xff808000, /* olive */
		0x9e33a98a, 0xff6b8e23, /* olivedrab */
		0xc3de262e, 0xffffa500, /* orange */
		0x58bebba3, 0xffff4500, /* orangered */
		0xc3def8a3, 0xffda70d6, /* orchid */
		0x28cb4834, 0xffeee8aa, /* palegoldenrod */
		0x3d9dd619, 0xff98fb98, /* palegreen */
		0x74022737, 0xffafeeee, /* paleturquoise */
		0x15e2ebc8, 0xffdb7093, /* palevioletred */
		0x5fd898e2, 0xffffefd5, /* papayawhip */
		0x93e1b776, 0xffffdab9, /* peachpuff */
		0x003472f8, 0xffcd853f, /* peru */
		0x00348176, 0xffffc0cb, /* pink */
		0x00348d94, 0xffdda0dd, /* plum */
		0xd036be93, 0xffb0e0e6, /* powderblue */
		0xc5c507bc, 0xff800080, /* purple */
		0xa89d65b3, 0xffbc8f8f, /* rosybrown */
		0xbd9413e1, 0xff4169e1, /* royalblue */
		0xf456044f, 0xff8b4513, /* saddlebrown */
		0xc9c6f66e, 0xfffa8072, /* salmon */
		0x0bb131e1, 0xfff4a460, /* sandybrown */
		0x34636c14, 0xff2e8b57, /* seagreen */
		0x3507fb41, 0xfffff5ee, /* seashell */
		0xca348772, 0xffa0522d, /* sienna */
		0xca37d30d, 0xffc0c0c0, /* silver */
		0x80da74fb, 0xff87ceeb, /* skyblue */
		0x44a8dd73, 0xff6a5acd, /* slateblue */
		0x44ab37f8, 0xff708090, /* slategrey */
		0x0035f183, 0xfffffafa, /* snow */
		0xd5440d16, 0xff00ff7f, /* springgreen */
		0x3e1524a5, 0xff4682b4, /* steelblue */
		0x0001bfa1, 0xffd2b48c, /* tan */
		0x0036425c, 0xff008080, /* teal */
		0xafc8858f, 0xffd8bfd8, /* thistle */
		0xcc41600a, 0xffff6347, /* tomato */
		0xfeea9b21, 0xff40e0d0, /* turquoise */
		0xcf57947f, 0xffee82ee, /* violet */
		0x06bdbae7, 0xfff5deb3, /* wheat */
		0x10802ee6, 0xfff5f5f5, /* whitesmoke */
		0xe1b5130f, 0xff9acd32  /* yellowgreen */
	};

	const int hash = colourName.trim().toLowerCase().hashCode();

	for (int i = 0; i < numElementsInArray (presets); i += 2)
		if (presets [i] == hash)
			return Colour (presets [i + 1]);

	return defaultColour;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Colours.cpp ***/


/*** Start of inlined file: juce_EdgeTable.cpp ***/
BEGIN_JUCE_NAMESPACE

const int juce_edgeTableDefaultEdgesPerLine = 32;

EdgeTable::EdgeTable (const Rectangle<int>& bounds_,
					  const Path& path, const AffineTransform& transform)
   : bounds (bounds_),
	 maxEdgesPerLine (juce_edgeTableDefaultEdgesPerLine),
	 lineStrideElements ((juce_edgeTableDefaultEdgesPerLine << 1) + 1),
	 needToCheckEmptinesss (true)
{
	table.malloc ((bounds.getHeight() + 1) * lineStrideElements);
	int* t = table;

	for (int i = bounds.getHeight(); --i >= 0;)
	{
		*t = 0;
		t += lineStrideElements;
	}

	const int topLimit = bounds.getY() << 8;
	const int heightLimit = bounds.getHeight() << 8;
	const int leftLimit = bounds.getX() << 8;
	const int rightLimit = bounds.getRight() << 8;

	PathFlatteningIterator iter (path, transform);

	while (iter.next())
	{
		int y1 = roundToInt (iter.y1 * 256.0f);
		int y2 = roundToInt (iter.y2 * 256.0f);

		if (y1 != y2)
		{
			y1 -= topLimit;
			y2 -= topLimit;

			const int startY = y1;
			int direction = -1;

			if (y1 > y2)
			{
				swapVariables (y1, y2);
				direction = 1;
			}

			if (y1 < 0)
				y1 = 0;

			if (y2 > heightLimit)
				y2 = heightLimit;

			if (y1 < y2)
			{
				const double startX = 256.0f * iter.x1;
				const double multiplier = (iter.x2 - iter.x1) / (iter.y2 - iter.y1);
				const int stepSize = jlimit (1, 256, 256 / (1 + (int) fabs (multiplier)));

				do
				{
					const int step = jmin (stepSize, y2 - y1, 256 - (y1 & 255));
					int x = roundToInt (startX + multiplier * ((y1 + (step >> 1)) - startY));

					if (x < leftLimit)
						x = leftLimit;
					else if (x >= rightLimit)
						x = rightLimit - 1;

					addEdgePoint (x, y1 >> 8, direction * step);
					y1 += step;
				}
				while (y1 < y2);
			}
		}
	}

	sanitiseLevels (path.isUsingNonZeroWinding());
}

EdgeTable::EdgeTable (const Rectangle<int>& rectangleToAdd)
   : bounds (rectangleToAdd),
	 maxEdgesPerLine (juce_edgeTableDefaultEdgesPerLine),
	 lineStrideElements ((juce_edgeTableDefaultEdgesPerLine << 1) + 1),
	 needToCheckEmptinesss (true)
{
	table.malloc (jmax (1, bounds.getHeight()) * lineStrideElements);
	table[0] = 0;

	const int x1 = rectangleToAdd.getX() << 8;
	const int x2 = rectangleToAdd.getRight() << 8;

	int* t = table;
	for (int i = rectangleToAdd.getHeight(); --i >= 0;)
	{
		t[0] = 2;
		t[1] = x1;
		t[2] = 255;
		t[3] = x2;
		t[4] = 0;
		t += lineStrideElements;
	}
}

EdgeTable::EdgeTable (const RectangleList& rectanglesToAdd)
   : bounds (rectanglesToAdd.getBounds()),
	 maxEdgesPerLine (juce_edgeTableDefaultEdgesPerLine),
	 lineStrideElements ((juce_edgeTableDefaultEdgesPerLine << 1) + 1),
	 needToCheckEmptinesss (true)
{
	table.malloc (jmax (1, bounds.getHeight()) * lineStrideElements);

	int* t = table;
	for (int i = bounds.getHeight(); --i >= 0;)
	{
		*t = 0;
		t += lineStrideElements;
	}

	for (RectangleList::Iterator iter (rectanglesToAdd); iter.next();)
	{
		const Rectangle<int>* const r = iter.getRectangle();

		const int x1 = r->getX() << 8;
		const int x2 = r->getRight() << 8;
		int y = r->getY() - bounds.getY();

		for (int j = r->getHeight(); --j >= 0;)
		{
			addEdgePoint (x1, y, 255);
			addEdgePoint (x2, y, -255);
			++y;
		}
	}

	sanitiseLevels (true);
}

EdgeTable::EdgeTable (const float x, const float y, const float w, const float h)
   : bounds (Rectangle<int> ((int) floorf (x), roundToInt (y * 256.0f) >> 8, 2 + (int) w, 2 + (int) h)),
	 maxEdgesPerLine (juce_edgeTableDefaultEdgesPerLine),
	 lineStrideElements ((juce_edgeTableDefaultEdgesPerLine << 1) + 1),
	 needToCheckEmptinesss (true)
{
	jassert (w > 0 && h > 0);
	table.malloc (jmax (1, bounds.getHeight()) * lineStrideElements);
	table[0] = 0;

	const int x1 = roundToInt (x * 256.0f);
	const int x2 = roundToInt ((x + w) * 256.0f);

	int y1 = roundToInt (y * 256.0f) - (bounds.getY() << 8);
	jassert (y1 < 256);
	int y2 = roundToInt ((y + h) * 256.0f) - (bounds.getY() << 8);

	if (x2 <= x1 || y2 <= y1)
	{
		bounds.setHeight (0);
		return;
	}

	int lineY = 0;
	int* t = table;

	if ((y1 >> 8) == (y2 >> 8))
	{
		t[0] = 2;
		t[1] = x1;
		t[2] = y2 - y1;
		t[3] = x2;
		t[4] = 0;
		++lineY;
		t += lineStrideElements;
	}
	else
	{
		t[0] = 2;
		t[1] = x1;
		t[2] = 255 - (y1 & 255);
		t[3] = x2;
		t[4] = 0;
		++lineY;
		t += lineStrideElements;

		while (lineY < (y2 >> 8))
		{
			t[0] = 2;
			t[1] = x1;
			t[2] = 255;
			t[3] = x2;
			t[4] = 0;
			++lineY;
			t += lineStrideElements;
		}

		jassert (lineY < bounds.getHeight());
		t[0] = 2;
		t[1] = x1;
		t[2] = y2 & 255;
		t[3] = x2;
		t[4] = 0;
		++lineY;
		t += lineStrideElements;
	}

	while (lineY < bounds.getHeight())
	{
		t[0] = 0;
		t += lineStrideElements;
		++lineY;
	}
}

EdgeTable::EdgeTable (const EdgeTable& other)
   : table (0)
{
	operator= (other);
}

EdgeTable& EdgeTable::operator= (const EdgeTable& other)
{
	bounds = other.bounds;
	maxEdgesPerLine = other.maxEdgesPerLine;
	lineStrideElements = other.lineStrideElements;
	needToCheckEmptinesss = other.needToCheckEmptinesss;

	table.malloc (jmax (1, bounds.getHeight()) * lineStrideElements);
	copyEdgeTableData (table, lineStrideElements, other.table, lineStrideElements, bounds.getHeight());
	return *this;
}

EdgeTable::~EdgeTable()
{
}

void EdgeTable::copyEdgeTableData (int* dest, const int destLineStride, const int* src, const int srcLineStride, int numLines) throw()
{
	while (--numLines >= 0)
	{
		memcpy (dest, src, (src[0] * 2 + 1) * sizeof (int));
		src += srcLineStride;
		dest += destLineStride;
	}
}

void EdgeTable::sanitiseLevels (const bool useNonZeroWinding) throw()
{
	// Convert the table from relative windings to absolute levels..
	int* lineStart = table;

	for (int i = bounds.getHeight(); --i >= 0;)
	{
		int* line = lineStart;
		lineStart += lineStrideElements;

		int num = *line;
		if (num == 0)
			continue;

		int level = 0;

		if (useNonZeroWinding)
		{
			while (--num > 0)
			{
				line += 2;
				level += *line;
				int corrected = abs (level);
				if (corrected >> 8)
					corrected = 255;

				*line = corrected;
			}
		}
		else
		{
			while (--num > 0)
			{
				line += 2;
				level += *line;
				int corrected = abs (level);
				if (corrected >> 8)
				{
					corrected &= 511;
					if (corrected >> 8)
						corrected = 511 - corrected;
				}

				*line = corrected;
			}
		}

		line[2] = 0; // force the last level to 0, just in case something went wrong in creating the table
	}
}

void EdgeTable::remapTableForNumEdges (const int newNumEdgesPerLine) throw()
{
	if (newNumEdgesPerLine != maxEdgesPerLine)
	{
		maxEdgesPerLine = newNumEdgesPerLine;

		jassert (bounds.getHeight() > 0);
		const int newLineStrideElements = maxEdgesPerLine * 2 + 1;

		HeapBlock <int> newTable (bounds.getHeight() * newLineStrideElements);

		copyEdgeTableData (newTable, newLineStrideElements, table, lineStrideElements, bounds.getHeight());

		table.swapWith (newTable);
		lineStrideElements = newLineStrideElements;
	}
}

void EdgeTable::optimiseTable() throw()
{
	int maxLineElements = 0;

	for (int i = bounds.getHeight(); --i >= 0;)
		maxLineElements = jmax (maxLineElements, table [i * lineStrideElements]);

	remapTableForNumEdges (maxLineElements);
}

void EdgeTable::addEdgePoint (const int x, const int y, const int winding) throw()
{
	jassert (y >= 0 && y < bounds.getHeight());

	int* line = table + lineStrideElements * y;
	const int numPoints = line[0];
	int n = numPoints << 1;

	if (n > 0)
	{
		while (n > 0)
		{
			const int cx = line [n - 1];

			if (cx <= x)
			{
				if (cx == x)
				{
					line [n] += winding;
					return;
				}

				break;
			}

			n -= 2;
		}

		if (numPoints >= maxEdgesPerLine)
		{
			remapTableForNumEdges (maxEdgesPerLine + juce_edgeTableDefaultEdgesPerLine);
			jassert (numPoints < maxEdgesPerLine);
			line = table + lineStrideElements * y;
		}

		memmove (line + (n + 3), line + (n + 1), sizeof (int) * ((numPoints << 1) - n));
	}

	line [n + 1] = x;
	line [n + 2] = winding;
	line[0]++;
}

void EdgeTable::translate (float dx, int dy) throw()
{
	bounds.setPosition (bounds.getX() + (int) floorf (dx), bounds.getY() + dy);

	int* lineStart = table;
	const int intDx = (int) (dx * 256.0f);

	for (int i = bounds.getHeight(); --i >= 0;)
	{
		int* line = lineStart;
		lineStart += lineStrideElements;
		int num = *line++;

		while (--num >= 0)
		{
			*line += intDx;
			line += 2;
		}
	}
}

void EdgeTable::intersectWithEdgeTableLine (const int y, const int* otherLine) throw()
{
	jassert (y >= 0 && y < bounds.getHeight());

	int* dest = table + lineStrideElements * y;
	if (dest[0] == 0)
		return;

	int otherNumPoints = *otherLine;
	if (otherNumPoints == 0)
	{
		*dest = 0;
		return;
	}

	const int right = bounds.getRight() << 8;

	// optimise for the common case where our line lies entirely within a
	// single pair of points, as happens when clipping to a simple rect.
	if (otherNumPoints == 2 && otherLine[2] >= 255)
	{
		clipEdgeTableLineToRange (dest, otherLine[1], jmin (right, otherLine[3]));
		return;
	}

	++otherLine;
	const size_t lineSizeBytes = (dest[0] * 2 + 1) * sizeof (int);
	int* temp = (int*) alloca (lineSizeBytes);
	memcpy (temp, dest, lineSizeBytes);

	const int* src1 = temp;
	int srcNum1 = *src1++;
	int x1 = *src1++;

	const int* src2 = otherLine;
	int srcNum2 = otherNumPoints;
	int x2 = *src2++;

	int destIndex = 0, destTotal = 0;
	int level1 = 0, level2 = 0;
	int lastX = std::numeric_limits<int>::min(), lastLevel = 0;

	while (srcNum1 > 0 && srcNum2 > 0)
	{
		int nextX;

		if (x1 < x2)
		{
			nextX = x1;
			level1 = *src1++;
			x1 = *src1++;
			--srcNum1;
		}
		else if (x1 == x2)
		{
			nextX = x1;
			level1 = *src1++;
			level2 = *src2++;
			x1 = *src1++;
			x2 = *src2++;
			--srcNum1;
			--srcNum2;
		}
		else
		{
			nextX = x2;
			level2 = *src2++;
			x2 = *src2++;
			--srcNum2;
		}

		if (nextX > lastX)
		{
			if (nextX >= right)
				break;

			lastX = nextX;

			const int nextLevel = (level1 * (level2 + 1)) >> 8;
			jassert (((unsigned int) nextLevel) < (unsigned int) 256);

			if (nextLevel != lastLevel)
			{
				if (destTotal >= maxEdgesPerLine)
				{
					dest[0] = destTotal;
					remapTableForNumEdges (maxEdgesPerLine + juce_edgeTableDefaultEdgesPerLine);
					dest = table + lineStrideElements * y;
				}

				++destTotal;
				lastLevel = nextLevel;
				dest[++destIndex] = nextX;
				dest[++destIndex] = nextLevel;
			}
		}
	}

	if (lastLevel > 0)
	{
		if (destTotal >= maxEdgesPerLine)
		{
			dest[0] = destTotal;
			remapTableForNumEdges (maxEdgesPerLine + juce_edgeTableDefaultEdgesPerLine);
			dest = table + lineStrideElements * y;
		}

		++destTotal;
		dest[++destIndex] = right;
		dest[++destIndex] = 0;
	}

	dest[0] = destTotal;

#if JUCE_DEBUG
	int last = std::numeric_limits<int>::min();
	for (int i = 0; i < dest[0]; ++i)
	{
		jassert (dest[i * 2 + 1] > last);
		last = dest[i * 2 + 1];
	}

	jassert (dest [dest[0] * 2] == 0);
#endif
}

void EdgeTable::clipEdgeTableLineToRange (int* dest, const int x1, const int x2) throw()
{
	int* lastItem = dest + (dest[0] * 2 - 1);

	if (x2 < lastItem[0])
	{
		if (x2 <= dest[1])
		{
			dest[0] = 0;
			return;
		}

		while (x2 < lastItem[-2])
		{
			--(dest[0]);
			lastItem -= 2;
		}

		lastItem[0] = x2;
		lastItem[1] = 0;
	}

	if (x1 > dest[1])
	{
		while (lastItem[0] > x1)
			lastItem -= 2;

		const int itemsRemoved = (int) (lastItem - (dest + 1)) / 2;

		if (itemsRemoved > 0)
		{
			dest[0] -= itemsRemoved;
			memmove (dest + 1, lastItem, dest[0] * (sizeof (int) * 2));
		}

		dest[1] = x1;
	}
}

void EdgeTable::clipToRectangle (const Rectangle<int>& r) throw()
{
	const Rectangle<int> clipped (r.getIntersection (bounds));

	if (clipped.isEmpty())
	{
		needToCheckEmptinesss = false;
		bounds.setHeight (0);
	}
	else
	{
		const int top = clipped.getY() - bounds.getY();
		const int bottom = clipped.getBottom() - bounds.getY();

		if (bottom < bounds.getHeight())
			bounds.setHeight (bottom);

		if (clipped.getRight() < bounds.getRight())
			bounds.setRight (clipped.getRight());

		for (int i = top; --i >= 0;)
			table [lineStrideElements * i] = 0;

		if (clipped.getX() > bounds.getX())
		{
			const int x1 = clipped.getX() << 8;
			const int x2 = jmin (bounds.getRight(), clipped.getRight()) << 8;
			int* line = table + lineStrideElements * top;

			for (int i = bottom - top; --i >= 0;)
			{
				if (line[0] != 0)
					clipEdgeTableLineToRange (line, x1, x2);

				line += lineStrideElements;
			}
		}

		needToCheckEmptinesss = true;
	}
}

void EdgeTable::excludeRectangle (const Rectangle<int>& r) throw()
{
	const Rectangle<int> clipped (r.getIntersection (bounds));

	if (! clipped.isEmpty())
	{
		const int top = clipped.getY() - bounds.getY();
		const int bottom = clipped.getBottom() - bounds.getY();

		//XXX optimise here by shortening the table if it fills top or bottom

		const int rectLine[] = { 4, std::numeric_limits<int>::min(), 255,
								 clipped.getX() << 8, 0,
								 clipped.getRight() << 8, 255,
								 std::numeric_limits<int>::max(), 0 };

		for (int i = top; i < bottom; ++i)
			intersectWithEdgeTableLine (i, rectLine);

		needToCheckEmptinesss = true;
	}
}

void EdgeTable::clipToEdgeTable (const EdgeTable& other)
{
	const Rectangle<int> clipped (other.bounds.getIntersection (bounds));

	if (clipped.isEmpty())
	{
		needToCheckEmptinesss = false;
		bounds.setHeight (0);
	}
	else
	{
		const int top = clipped.getY() - bounds.getY();
		const int bottom = clipped.getBottom() - bounds.getY();

		if (bottom < bounds.getHeight())
			bounds.setHeight (bottom);

		if (clipped.getRight() < bounds.getRight())
			bounds.setRight (clipped.getRight());

		int i = 0;
		for (i = top; --i >= 0;)
			table [lineStrideElements * i] = 0;

		const int* otherLine = other.table + other.lineStrideElements * (clipped.getY() - other.bounds.getY());

		for (i = top; i < bottom; ++i)
		{
			intersectWithEdgeTableLine (i, otherLine);
			otherLine += other.lineStrideElements;
		}

		needToCheckEmptinesss = true;
	}
}

void EdgeTable::clipLineToMask (int x, int y, const uint8* mask, int maskStride, int numPixels) throw()
{
	y -= bounds.getY();

	if (y < 0 || y >= bounds.getHeight())
		return;

	needToCheckEmptinesss = true;

	if (numPixels <= 0)
	{
		table [lineStrideElements * y] = 0;
		return;
	}

	int* tempLine = (int*) alloca ((numPixels * 2 + 4) * sizeof (int));
	int destIndex = 0, lastLevel = 0;

	while (--numPixels >= 0)
	{
		const int alpha = *mask;
		mask += maskStride;

		if (alpha != lastLevel)
		{
			tempLine[++destIndex] = (x << 8);
			tempLine[++destIndex] = alpha;
			lastLevel = alpha;
		}

		++x;
	}

	if (lastLevel > 0)
	{
		tempLine[++destIndex] = (x << 8);
		tempLine[++destIndex] = 0;
	}

	tempLine[0] = destIndex >> 1;

	intersectWithEdgeTableLine (y, tempLine);
}

bool EdgeTable::isEmpty() throw()
{
	if (needToCheckEmptinesss)
	{
		needToCheckEmptinesss = false;
		int* t = table;

		for (int i = bounds.getHeight(); --i >= 0;)
		{
			if (t[0] > 1)
				return false;

			t += lineStrideElements;
		}

		bounds.setHeight (0);
	}

	return bounds.getHeight() == 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_EdgeTable.cpp ***/


/*** Start of inlined file: juce_FillType.cpp ***/
BEGIN_JUCE_NAMESPACE

FillType::FillType() throw()
	: colour (0xff000000), image (0)
{
}

FillType::FillType (const Colour& colour_) throw()
	: colour (colour_), image (0)
{
}

FillType::FillType (const ColourGradient& gradient_) throw()
	: colour (0xff000000), gradient (new ColourGradient (gradient_)), image (0)
{
}

FillType::FillType (const Image& image_, const AffineTransform& transform_) throw()
	: colour (0xff000000), image (&image_), transform (transform_)
{
}

FillType::FillType (const FillType& other) throw()
	: colour (other.colour),
	  gradient (other.gradient != 0 ? new ColourGradient (*other.gradient) : 0),
	  image (other.image), transform (other.transform)
{
}

FillType& FillType::operator= (const FillType& other) throw()
{
	if (this != &other)
	{
		colour = other.colour;
		gradient = (other.gradient != 0 ? new ColourGradient (*other.gradient) : 0);
		image = other.image;
		transform = other.transform;
	}

	return *this;
}

FillType::~FillType() throw()
{
}

void FillType::setColour (const Colour& newColour) throw()
{
	gradient = 0;
	image = 0;
	colour = newColour;
}

void FillType::setGradient (const ColourGradient& newGradient) throw()
{
	if (gradient != 0)
	{
		*gradient = newGradient;
	}
	else
	{
		image = 0;
		gradient = new ColourGradient (newGradient);
		colour = Colours::black;
	}
}

void FillType::setTiledImage (const Image& image_, const AffineTransform& transform_) throw()
{
	gradient = 0;
	image = &image_;
	transform = transform_;
	colour = Colours::black;
}

void FillType::setOpacity (const float newOpacity) throw()
{
	colour = colour.withAlpha (newOpacity);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_FillType.cpp ***/


/*** Start of inlined file: juce_Graphics.cpp ***/
BEGIN_JUCE_NAMESPACE

static const Graphics::ResamplingQuality defaultQuality = Graphics::mediumResamplingQuality;

template <typename Type>
static bool areCoordsSensibleNumbers (Type x, Type y, Type w, Type h)
{
	const int maxVal = 0x3fffffff;

	return (int) x >= -maxVal && (int) x <= maxVal
		&& (int) y >= -maxVal && (int) y <= maxVal
		&& (int) w >= -maxVal && (int) w <= maxVal
		&& (int) h >= -maxVal && (int) h <= maxVal;
}

LowLevelGraphicsContext::LowLevelGraphicsContext()
{
}

LowLevelGraphicsContext::~LowLevelGraphicsContext()
{
}

Graphics::Graphics (Image& imageToDrawOnto)
	: context (imageToDrawOnto.createLowLevelContext()),
	  contextToDelete (context),
	  saveStatePending (false)
{
}

Graphics::Graphics (LowLevelGraphicsContext* const internalContext) throw()
	: context (internalContext),
	  saveStatePending (false)
{
}

Graphics::~Graphics()
{
}

void Graphics::resetToDefaultState()
{
	saveStateIfPending();
	context->setFill (FillType());
	context->setFont (Font());
	context->setInterpolationQuality (defaultQuality);
}

bool Graphics::isVectorDevice() const
{
	return context->isVectorDevice();
}

bool Graphics::reduceClipRegion (const int x, const int y, const int w, const int h)
{
	saveStateIfPending();
	return context->clipToRectangle (Rectangle<int> (x, y, w, h));
}

bool Graphics::reduceClipRegion (const RectangleList& clipRegion)
{
	saveStateIfPending();
	return context->clipToRectangleList (clipRegion);
}

bool Graphics::reduceClipRegion (const Path& path, const AffineTransform& transform)
{
	saveStateIfPending();
	context->clipToPath (path, transform);
	return ! context->isClipEmpty();
}

bool Graphics::reduceClipRegion (const Image& image, const Rectangle<int>& sourceClipRegion, const AffineTransform& transform)
{
	saveStateIfPending();
	context->clipToImageAlpha (image, sourceClipRegion, transform);
	return ! context->isClipEmpty();
}

void Graphics::excludeClipRegion (const Rectangle<int>& rectangleToExclude)
{
	saveStateIfPending();
	context->excludeClipRectangle (rectangleToExclude);
}

bool Graphics::isClipEmpty() const
{
	return context->isClipEmpty();
}

const Rectangle<int> Graphics::getClipBounds() const
{
	return context->getClipBounds();
}

void Graphics::saveState()
{
	saveStateIfPending();
	saveStatePending = true;
}

void Graphics::restoreState()
{
	if (saveStatePending)
		saveStatePending = false;
	else
		context->restoreState();
}

void Graphics::saveStateIfPending()
{
	if (saveStatePending)
	{
		saveStatePending = false;
		context->saveState();
	}
}

void Graphics::setOrigin (const int newOriginX, const int newOriginY)
{
	saveStateIfPending();
	context->setOrigin (newOriginX, newOriginY);
}

bool Graphics::clipRegionIntersects (const int x, const int y, const int w, const int h) const
{
	return context->clipRegionIntersects (Rectangle<int> (x, y, w, h));
}

void Graphics::setColour (const Colour& newColour)
{
	saveStateIfPending();
	context->setFill (newColour);
}

void Graphics::setOpacity (const float newOpacity)
{
	saveStateIfPending();
	context->setOpacity (newOpacity);
}

void Graphics::setGradientFill (const ColourGradient& gradient)
{
	setFillType (gradient);
}

void Graphics::setTiledImageFill (const Image& imageToUse, const int anchorX, const int anchorY, const float opacity)
{
	saveStateIfPending();
	context->setFill (FillType (imageToUse, AffineTransform::translation ((float) anchorX, (float) anchorY)));
	context->setOpacity (opacity);
}

void Graphics::setFillType (const FillType& newFill)
{
	saveStateIfPending();
	context->setFill (newFill);
}

void Graphics::setFont (const Font& newFont)
{
	saveStateIfPending();
	context->setFont (newFont);
}

void Graphics::setFont (const float newFontHeight, const int newFontStyleFlags)
{
	saveStateIfPending();
	Font f (context->getFont());
	f.setSizeAndStyle (newFontHeight, newFontStyleFlags, 1.0f, 0);
	context->setFont (f);
}

void Graphics::drawSingleLineText (const String& text, const int startX, const int baselineY) const
{
	if (text.isNotEmpty()
		 && startX < context->getClipBounds().getRight())
	{
		GlyphArrangement arr;
		arr.addLineOfText (context->getFont(), text, (float) startX, (float) baselineY);
		arr.draw (*this);
	}
}

void Graphics::drawTextAsPath (const String& text, const AffineTransform& transform) const
{
	if (text.isNotEmpty())
	{
		GlyphArrangement arr;
		arr.addLineOfText (context->getFont(), text, 0.0f, 0.0f);
		arr.draw (*this, transform);
	}
}

void Graphics::drawMultiLineText (const String& text, const int startX, const int baselineY, const int maximumLineWidth) const
{
	if (text.isNotEmpty()
		 && startX < context->getClipBounds().getRight())
	{
		GlyphArrangement arr;
		arr.addJustifiedText (context->getFont(), text,
							  (float) startX, (float) baselineY, (float) maximumLineWidth,
							  Justification::left);
		arr.draw (*this);
	}
}

void Graphics::drawText (const String& text,
						 const int x, const int y, const int width, const int height,
						 const Justification& justificationType,
						 const bool useEllipsesIfTooBig) const
{
	if (text.isNotEmpty() && context->clipRegionIntersects (Rectangle<int> (x, y, width, height)))
	{
		GlyphArrangement arr;

		arr.addCurtailedLineOfText (context->getFont(), text,
									0.0f, 0.0f, (float) width,
									useEllipsesIfTooBig);

		arr.justifyGlyphs (0, arr.getNumGlyphs(),
						   (float) x, (float) y, (float) width, (float) height,
						   justificationType);
		arr.draw (*this);
	}
}

void Graphics::drawFittedText (const String& text,
							   const int x, const int y, const int width, const int height,
							   const Justification& justification,
							   const int maximumNumberOfLines,
							   const float minimumHorizontalScale) const
{
	if (text.isNotEmpty()
		 && width > 0 && height > 0
		 && context->clipRegionIntersects (Rectangle<int> (x, y, width, height)))
	{
		GlyphArrangement arr;

		arr.addFittedText (context->getFont(), text,
						   (float) x, (float) y, (float) width, (float) height,
						   justification,
						   maximumNumberOfLines,
						   minimumHorizontalScale);

		arr.draw (*this);
	}
}

void Graphics::fillRect (int x, int y, int width, int height) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	context->fillRect (Rectangle<int> (x, y, width, height), false);
}

void Graphics::fillRect (const Rectangle<int>& r) const
{
	context->fillRect (r, false);
}

void Graphics::fillRect (const float x, const float y, const float width, const float height) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	Path p;
	p.addRectangle (x, y, width, height);
	fillPath (p);
}

void Graphics::setPixel (int x, int y) const
{
	context->fillRect (Rectangle<int> (x, y, 1, 1), false);
}

void Graphics::fillAll() const
{
	fillRect (context->getClipBounds());
}

void Graphics::fillAll (const Colour& colourToUse) const
{
	if (! colourToUse.isTransparent())
	{
		const Rectangle<int> clip (context->getClipBounds());

		context->saveState();
		context->setFill (colourToUse);
		context->fillRect (clip, false);
		context->restoreState();
	}
}

void Graphics::fillPath (const Path& path, const AffineTransform& transform) const
{
	if ((! context->isClipEmpty()) && ! path.isEmpty())
		context->fillPath (path, transform);
}

void Graphics::strokePath (const Path& path,
						   const PathStrokeType& strokeType,
						   const AffineTransform& transform) const
{
	Path stroke;
	strokeType.createStrokedPath (stroke, path, transform);
	fillPath (stroke);
}

void Graphics::drawRect (const int x, const int y, const int width, const int height,
						 const int lineThickness) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	context->fillRect (Rectangle<int> (x, y, width, lineThickness), false);
	context->fillRect (Rectangle<int> (x, y + lineThickness, lineThickness, height - lineThickness * 2), false);
	context->fillRect (Rectangle<int> (x + width - lineThickness, y + lineThickness, lineThickness, height - lineThickness * 2), false);
	context->fillRect (Rectangle<int> (x, y + height - lineThickness, width, lineThickness), false);
}

void Graphics::drawRect (const float x, const float y, const float width, const float height, const float lineThickness) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	Path p;
	p.addRectangle (x, y, width, lineThickness);
	p.addRectangle (x, y + lineThickness, lineThickness, height - lineThickness * 2.0f);
	p.addRectangle (x + width - lineThickness, y + lineThickness, lineThickness, height - lineThickness * 2.0f);
	p.addRectangle (x, y + height - lineThickness, width, lineThickness);
	fillPath (p);
}

void Graphics::drawRect (const Rectangle<int>& r, const int lineThickness) const
{
	drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(), lineThickness);
}

void Graphics::drawBevel (const int x, const int y, const int width, const int height,
						  const int bevelThickness, const Colour& topLeftColour, const Colour& bottomRightColour,
						  const bool useGradient, const bool sharpEdgeOnOutside) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	if (clipRegionIntersects (x, y, width, height))
	{
		context->saveState();

		const float oldOpacity = 1.0f;//xxx state->colour.getFloatAlpha();
		const float ramp = oldOpacity / bevelThickness;

		for (int i = bevelThickness; --i >= 0;)
		{
			const float op = useGradient ? ramp * (sharpEdgeOnOutside ? bevelThickness - i : i)
										 : oldOpacity;

			context->setFill (topLeftColour.withMultipliedAlpha (op));
			context->fillRect (Rectangle<int> (x + i, y + i, width - i * 2, 1), false);
			context->setFill (topLeftColour.withMultipliedAlpha (op * 0.75f));
			context->fillRect (Rectangle<int> (x + i, y + i + 1, 1, height - i * 2 - 2), false);
			context->setFill (bottomRightColour.withMultipliedAlpha (op));
			context->fillRect (Rectangle<int> (x + i, y + height - i - 1, width - i * 2, 1), false);
			context->setFill (bottomRightColour.withMultipliedAlpha (op  * 0.75f));
			context->fillRect (Rectangle<int> (x + width - i - 1, y + i + 1, 1, height - i * 2 - 2), false);
		}

		context->restoreState();
	}
}

void Graphics::fillEllipse (const float x, const float y, const float width, const float height) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	Path p;
	p.addEllipse (x, y, width, height);
	fillPath (p);
}

void Graphics::drawEllipse (const float x, const float y, const float width, const float height,
							const float lineThickness) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	Path p;
	p.addEllipse (x, y, width, height);
	strokePath (p, PathStrokeType (lineThickness));
}

void Graphics::fillRoundedRectangle (const float x, const float y, const float width, const float height, const float cornerSize) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	Path p;
	p.addRoundedRectangle (x, y, width, height, cornerSize);
	fillPath (p);
}

void Graphics::fillRoundedRectangle (const Rectangle<float>& r, const float cornerSize) const
{
	fillRoundedRectangle (r.getX(), r.getY(), r.getWidth(), r.getHeight(), cornerSize);
}

void Graphics::drawRoundedRectangle (const float x, const float y, const float width, const float height,
									 const float cornerSize, const float lineThickness) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (x, y, width, height));

	Path p;
	p.addRoundedRectangle (x, y, width, height, cornerSize);
	strokePath (p, PathStrokeType (lineThickness));
}

void Graphics::drawRoundedRectangle (const Rectangle<float>& r, const float cornerSize, const float lineThickness) const
{
	drawRoundedRectangle (r.getX(), r.getY(), r.getWidth(), r.getHeight(), cornerSize, lineThickness);
}

void Graphics::drawArrow (const float startX, const float startY, const float endX, const float endY,
						  const float lineThickness, const float arrowheadWidth, const float arrowheadLength) const
{
	Path p;
	p.addArrow (startX, startY, endX, endY,
				lineThickness, arrowheadWidth, arrowheadLength);
	fillPath (p);
}

void Graphics::fillCheckerBoard (int x, int y, int width, int height,
								 const int checkWidth, const int checkHeight,
								 const Colour& colour1, const Colour& colour2) const
{
	jassert (checkWidth > 0 && checkHeight > 0); // can't be zero or less!

	if (checkWidth > 0 && checkHeight > 0)
	{
		context->saveState();

		if (colour1 == colour2)
		{
			context->setFill (colour1);
			context->fillRect (Rectangle<int> (x, y, width, height), false);
		}
		else
		{
			const Rectangle<int> clip (context->getClipBounds());

			const int right  = jmin (x + width, clip.getRight());
			const int bottom = jmin (y + height, clip.getBottom());

			int cy = 0;
			while (y < bottom)
			{
				int cx = cy;

				for (int xx = x; xx < right; xx += checkWidth)
				{
					context->setFill (((cx++ & 1) == 0) ? colour1 : colour2);
					context->fillRect (Rectangle<int> (xx, y, jmin (checkWidth, right - xx), jmin (checkHeight, bottom - y)),
									   false);
				}

				++cy;
				y += checkHeight;
			}
		}

		context->restoreState();
	}
}

void Graphics::drawVerticalLine (const int x, float top, float bottom) const
{
	context->drawVerticalLine (x, top, bottom);
}

void Graphics::drawHorizontalLine (const int y, float left, float right) const
{
	context->drawHorizontalLine (y, left, right);
}

void Graphics::drawLine (float x1, float y1, float x2, float y2) const
{
	context->drawLine (x1, y1, x2, y2);
}

void Graphics::drawLine (const float startX, const float startY,
						 const float endX, const float endY,
						 const float lineThickness) const
{
	Path p;
	p.addLineSegment (startX, startY, endX, endY, lineThickness);
	fillPath (p);
}

void Graphics::drawLine (const Line& line) const
{
	drawLine (line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY());
}

void Graphics::drawLine (const Line& line, const float lineThickness) const
{
	drawLine (line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY(), lineThickness);
}

void Graphics::drawDashedLine (const float startX, const float startY,
							   const float endX, const float endY,
							   const float* const dashLengths,
							   const int numDashLengths,
							   const float lineThickness) const
{
	const double dx = endX - startX;
	const double dy = endY - startY;
	const double totalLen = juce_hypot (dx, dy);

	if (totalLen >= 0.5)
	{
		const double onePixAlpha = 1.0 / totalLen;

		double alpha = 0.0;
		float x = startX;
		float y = startY;
		int n = 0;

		while (alpha < 1.0f)
		{
			alpha = jmin (1.0, alpha + dashLengths[n++] * onePixAlpha);
			n = n % numDashLengths;

			const float oldX = x;
			const float oldY = y;

			x = (float) (startX + dx * alpha);
			y = (float) (startY + dy * alpha);

			if ((n & 1) != 0)
			{
				if (lineThickness != 1.0f)
					drawLine (oldX, oldY, x, y, lineThickness);
				else
					drawLine (oldX, oldY, x, y);
			}
		}
	}
}

void Graphics::setImageResamplingQuality (const Graphics::ResamplingQuality newQuality)
{
	saveStateIfPending();
	context->setInterpolationQuality (newQuality);
}

void Graphics::drawImageAt (const Image* const imageToDraw,
							const int topLeftX, const int topLeftY,
							const bool fillAlphaChannelWithCurrentBrush) const
{
	if (imageToDraw != 0)
	{
		const int imageW = imageToDraw->getWidth();
		const int imageH = imageToDraw->getHeight();

		drawImage (imageToDraw,
				   topLeftX, topLeftY, imageW, imageH,
				   0, 0, imageW, imageH,
				   fillAlphaChannelWithCurrentBrush);
	}
}

void Graphics::drawImageWithin (const Image* const imageToDraw,
								const int destX, const int destY,
								const int destW, const int destH,
								const RectanglePlacement& placementWithinTarget,
								const bool fillAlphaChannelWithCurrentBrush) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (destX, destY, destW, destH));

	if (imageToDraw != 0)
	{
		const int imageW = imageToDraw->getWidth();
		const int imageH = imageToDraw->getHeight();

		if (imageW > 0 && imageH > 0)
		{
			double newX = 0.0, newY = 0.0;
			double newW = imageW;
			double newH = imageH;

			placementWithinTarget.applyTo (newX, newY, newW, newH,
										   destX, destY, destW, destH);

			if (newW > 0 && newH > 0)
			{
				drawImage (imageToDraw,
						   roundToInt (newX), roundToInt (newY),
						   roundToInt (newW), roundToInt (newH),
						   0, 0, imageW, imageH,
						   fillAlphaChannelWithCurrentBrush);
			}
		}
	}
}

void Graphics::drawImage (const Image* const imageToDraw,
						  int dx, int dy, int dw, int dh,
						  int sx, int sy, int sw, int sh,
						  const bool fillAlphaChannelWithCurrentBrush) const
{
	// passing in a silly number can cause maths problems in rendering!
	jassert (areCoordsSensibleNumbers (dx, dy, dw, dh));
	jassert (areCoordsSensibleNumbers (sx, sy, sw, sh));

	if (context->clipRegionIntersects  (Rectangle<int> (dx, dy, dw, dh)))
	{
		drawImageTransformed (imageToDraw, Rectangle<int> (sx, sy, sw, sh),
							  AffineTransform::scale (dw / (float) sw, dh / (float) sh)
											  .translated ((float) dx, (float) dy),
							  fillAlphaChannelWithCurrentBrush);
	}
}

void Graphics::drawImageTransformed (const Image* const imageToDraw,
									 const Rectangle<int>& imageSubRegion,
									 const AffineTransform& transform,
									 const bool fillAlphaChannelWithCurrentBrush) const
{
	if (imageToDraw != 0 && ! context->isClipEmpty())
	{
		const Rectangle<int> srcClip (imageSubRegion.getIntersection (imageToDraw->getBounds()));

		if (fillAlphaChannelWithCurrentBrush)
		{
			context->saveState();
			context->clipToImageAlpha (*imageToDraw, srcClip, transform);
			fillAll();
			context->restoreState();
		}
		else
		{
			context->drawImage (*imageToDraw, srcClip, transform, false);
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Graphics.cpp ***/


/*** Start of inlined file: juce_Justification.cpp ***/
BEGIN_JUCE_NAMESPACE

Justification::Justification (const Justification& other) throw()
	: flags (other.flags)
{
}

Justification& Justification::operator= (const Justification& other) throw()
{
	flags = other.flags;
	return *this;
}

int Justification::getOnlyVerticalFlags() const throw()
{
	return flags & (top | bottom | verticallyCentred);
}

int Justification::getOnlyHorizontalFlags() const throw()
{
	return flags & (left | right | horizontallyCentred | horizontallyJustified);
}

void Justification::applyToRectangle (int& x, int& y,
									  const int w, const int h,
									  const int spaceX, const int spaceY,
									  const int spaceW, const int spaceH) const throw()
{
	if ((flags & horizontallyCentred) != 0)
	{
		x = spaceX + ((spaceW - w) >> 1);
	}
	else if ((flags & right) != 0)
	{
		x = spaceX + spaceW - w;
	}
	else
	{
		x = spaceX;
	}

	if ((flags & verticallyCentred) != 0)
	{
		y = spaceY + ((spaceH - h) >> 1);
	}
	else if ((flags & bottom) != 0)
	{
		y = spaceY + spaceH - h;
	}
	else
	{
		y = spaceY;
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Justification.cpp ***/


/*** Start of inlined file: juce_LowLevelGraphicsPostScriptRenderer.cpp ***/
BEGIN_JUCE_NAMESPACE

// this will throw an assertion if you try to draw something that's not
// possible in postscript
#define WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS 0

#if defined (JUCE_DEBUG) && WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS
 #define notPossibleInPostscriptAssert jassertfalse
#else
 #define notPossibleInPostscriptAssert
#endif

LowLevelGraphicsPostScriptRenderer::LowLevelGraphicsPostScriptRenderer (OutputStream& resultingPostScript,
																		const String& documentTitle,
																		const int totalWidth_,
																		const int totalHeight_)
	: out (resultingPostScript),
	  totalWidth (totalWidth_),
	  totalHeight (totalHeight_),
	  needToClip (true)
{
	stateStack.add (new SavedState());
	stateStack.getLast()->clip = Rectangle<int> (0, 0, totalWidth_, totalHeight_);

	const float scale = jmin ((520.0f / totalWidth_), (750.0f / totalHeight));

	out << "%!PS-Adobe-3.0 EPSF-3.0"
		   "\n%%BoundingBox: 0 0 600 824"
		   "\n%%Pages: 0"
		   "\n%%Creator: Raw Material Software JUCE"
		   "\n%%Title: " << documentTitle <<
		   "\n%%CreationDate: none"
		   "\n%%LanguageLevel: 2"
		   "\n%%EndComments"
		   "\n%%BeginProlog"
		   "\n%%BeginResource: JRes"
		   "\n/bd {bind def} bind def"
		   "\n/c {setrgbcolor} bd"
		   "\n/m {moveto} bd"
		   "\n/l {lineto} bd"
		   "\n/rl {rlineto} bd"
		   "\n/ct {curveto} bd"
		   "\n/cp {closepath} bd"
		   "\n/pr {3 index 3 index moveto 1 index 0 rlineto 0 1 index rlineto pop neg 0 rlineto pop pop closepath} bd"
		   "\n/doclip {initclip newpath} bd"
		   "\n/endclip {clip newpath} bd"
		   "\n%%EndResource"
		   "\n%%EndProlog"
		   "\n%%BeginSetup"
		   "\n%%EndSetup"
		   "\n%%Page: 1 1"
		   "\n%%BeginPageSetup"
		   "\n%%EndPageSetup\n\n"
		<< "40 800 translate\n"
		<< scale << ' ' << scale << " scale\n\n";
}

LowLevelGraphicsPostScriptRenderer::~LowLevelGraphicsPostScriptRenderer()
{
}

bool LowLevelGraphicsPostScriptRenderer::isVectorDevice() const
{
	return true;
}

void LowLevelGraphicsPostScriptRenderer::setOrigin (int x, int y)
{
	if (x != 0 || y != 0)
	{
		stateStack.getLast()->xOffset += x;
		stateStack.getLast()->yOffset += y;
		needToClip = true;
	}
}

bool LowLevelGraphicsPostScriptRenderer::clipToRectangle (const Rectangle<int>& r)
{
	needToClip = true;
	return stateStack.getLast()->clip.clipTo (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
}

bool LowLevelGraphicsPostScriptRenderer::clipToRectangleList (const RectangleList& clipRegion)
{
	needToClip = true;
	return stateStack.getLast()->clip.clipTo (clipRegion);
}

void LowLevelGraphicsPostScriptRenderer::excludeClipRectangle (const Rectangle<int>& r)
{
	needToClip = true;
	stateStack.getLast()->clip.subtract (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
}

void LowLevelGraphicsPostScriptRenderer::clipToPath (const Path& path, const AffineTransform& transform)
{
	writeClip();

	Path p (path);
	p.applyTransform (transform.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset));
	writePath (p);
	out << "clip\n";
}

void LowLevelGraphicsPostScriptRenderer::clipToImageAlpha (const Image& /*sourceImage*/, const Rectangle<int>& /*srcClip*/, const AffineTransform& /*transform*/)
{
	needToClip = true;
	jassertfalse // xxx
}

bool LowLevelGraphicsPostScriptRenderer::clipRegionIntersects (const Rectangle<int>& r)
{
	return stateStack.getLast()->clip.intersectsRectangle (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
}

const Rectangle<int> LowLevelGraphicsPostScriptRenderer::getClipBounds() const
{
	return stateStack.getLast()->clip.getBounds().translated (-stateStack.getLast()->xOffset,
															  -stateStack.getLast()->yOffset);
}

bool LowLevelGraphicsPostScriptRenderer::isClipEmpty() const
{
	return stateStack.getLast()->clip.isEmpty();
}

LowLevelGraphicsPostScriptRenderer::SavedState::SavedState()
	: xOffset (0),
	  yOffset (0)
{
}

LowLevelGraphicsPostScriptRenderer::SavedState::~SavedState()
{
}

void LowLevelGraphicsPostScriptRenderer::saveState()
{
	stateStack.add (new SavedState (*stateStack.getLast()));
}

void LowLevelGraphicsPostScriptRenderer::restoreState()
{
	jassert (stateStack.size() > 0);

	if (stateStack.size() > 0)
		stateStack.removeLast();
}

void LowLevelGraphicsPostScriptRenderer::writeClip()
{
	if (needToClip)
	{
		needToClip = false;

		out << "doclip ";

		int itemsOnLine = 0;

		for (RectangleList::Iterator i (stateStack.getLast()->clip); i.next();)
		{
			if (++itemsOnLine == 6)
			{
				itemsOnLine = 0;
				out << '\n';
			}

			const Rectangle<int>& r = *i.getRectangle();

			out << r.getX() << ' ' << -r.getY() << ' '
				<< r.getWidth() << ' ' << -r.getHeight() << " pr ";
		}

		out << "endclip\n";
	}
}

void LowLevelGraphicsPostScriptRenderer::writeColour (const Colour& colour)
{
	Colour c (Colours::white.overlaidWith (colour));

	if (lastColour != c)
	{
		lastColour = c;

		out << String (c.getFloatRed(), 3) << ' '
			<< String (c.getFloatGreen(), 3) << ' '
			<< String (c.getFloatBlue(), 3) << " c\n";
	}
}

void LowLevelGraphicsPostScriptRenderer::writeXY (const float x, const float y) const
{
	out << String (x, 2) << ' '
		<< String (-y, 2) << ' ';
}

void LowLevelGraphicsPostScriptRenderer::writePath (const Path& path) const
{
	out << "newpath ";

	float lastX = 0.0f;
	float lastY = 0.0f;
	int itemsOnLine = 0;

	Path::Iterator i (path);

	while (i.next())
	{
		if (++itemsOnLine == 4)
		{
			itemsOnLine = 0;
			out << '\n';
		}

		switch (i.elementType)
		{
		case Path::Iterator::startNewSubPath:
			writeXY (i.x1, i.y1);
			lastX = i.x1;
			lastY = i.y1;
			out << "m ";
			break;

		case Path::Iterator::lineTo:
			writeXY (i.x1, i.y1);
			lastX = i.x1;
			lastY = i.y1;
			out << "l ";
			break;

		case Path::Iterator::quadraticTo:
			{
				const float cp1x = lastX + (i.x1 - lastX) * 2.0f / 3.0f;
				const float cp1y = lastY + (i.y1 - lastY) * 2.0f / 3.0f;
				const float cp2x = cp1x + (i.x2 - lastX) / 3.0f;
				const float cp2y = cp1y + (i.y2 - lastY) / 3.0f;

				writeXY (cp1x, cp1y);
				writeXY (cp2x, cp2y);
				writeXY (i.x2, i.y2);
				out << "ct ";
				lastX = i.x2;
				lastY = i.y2;
			}
			break;

		case Path::Iterator::cubicTo:
			writeXY (i.x1, i.y1);
			writeXY (i.x2, i.y2);
			writeXY (i.x3, i.y3);
			out << "ct ";
			lastX = i.x3;
			lastY = i.y3;
			break;

		case Path::Iterator::closePath:
			out << "cp ";
			break;

		default:
			jassertfalse
			break;
		}
	}

	out << '\n';
}

void LowLevelGraphicsPostScriptRenderer::writeTransform (const AffineTransform& trans) const
{
	out << "[ "
		<< trans.mat00 << ' '
		<< trans.mat10 << ' '
		<< trans.mat01 << ' '
		<< trans.mat11 << ' '
		<< trans.mat02 << ' '
		<< trans.mat12 << " ] concat ";
}

void LowLevelGraphicsPostScriptRenderer::setFill (const FillType& fillType)
{
	stateStack.getLast()->fillType = fillType;
}

void LowLevelGraphicsPostScriptRenderer::setOpacity (float /*opacity*/)
{
}

void LowLevelGraphicsPostScriptRenderer::setInterpolationQuality (Graphics::ResamplingQuality /*quality*/)
{
}

void LowLevelGraphicsPostScriptRenderer::fillRect (const Rectangle<int>& r, const bool /*replaceExistingContents*/)
{
	if (stateStack.getLast()->fillType.isColour())
	{
		writeClip();
		writeColour (stateStack.getLast()->fillType.colour);

		Rectangle<int> r2 (r.translated (stateStack.getLast()->xOffset,  stateStack.getLast()->yOffset));

		out << r2.getX() << ' ' << -r2.getBottom() << ' ' << r2.getWidth() << ' ' << r2.getHeight() << " rectfill\n";
	}
	else
	{
		Path p;
		p.addRectangle (r);
		fillPath (p, AffineTransform::identity);
	}

}

void LowLevelGraphicsPostScriptRenderer::fillPath (const Path& path, const AffineTransform& t)
{
	if (stateStack.getLast()->fillType.isColour())
	{
		writeClip();

		Path p (path);
		p.applyTransform (t.translated ((float) stateStack.getLast()->xOffset,
										(float) stateStack.getLast()->yOffset));
		writePath (p);

		writeColour (stateStack.getLast()->fillType.colour);

		out << "fill\n";
	}
	else if (stateStack.getLast()->fillType.isGradient())
	{
		// this doesn't work correctly yet - it could be improved to handle solid gradients, but
		// postscript can't do semi-transparent ones.
		notPossibleInPostscriptAssert   // you can disable this warning by setting the WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS flag at the top of this file

		writeClip();
		out << "gsave ";

		{
			Path p (path);
			p.applyTransform (t.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset));
			writePath (p);
			out << "clip\n";
		}

		const Rectangle<int> bounds (stateStack.getLast()->clip.getBounds());

		// ideally this would draw lots of lines or ellipses to approximate the gradient, but for the
		// time-being, this just fills it with the average colour..
		writeColour (stateStack.getLast()->fillType.gradient->getColourAtPosition (0.5f));
		out << bounds.getX() << ' ' << -bounds.getBottom() << ' ' << bounds.getWidth() << ' ' << bounds.getHeight() << " rectfill\n";

		out << "grestore\n";
	}
}

void LowLevelGraphicsPostScriptRenderer::writeImage (const Image& im,
													 const int sx, const int sy,
													 const int maxW, const int maxH) const
{
	out << "{<\n";

	const int w = jmin (maxW, im.getWidth());
	const int h = jmin (maxH, im.getHeight());

	int charsOnLine = 0;
	const Image::BitmapData srcData (im, 0, 0, w, h);
	Colour pixel;

	for (int y = h; --y >= 0;)
	{
		for (int x = 0; x < w; ++x)
		{
			const uint8* pixelData = srcData.getPixelPointer (x, y);

			if (x >= sx && y >= sy)
			{
				if (im.isARGB())
				{
					PixelARGB p (*(const PixelARGB*) pixelData);
					p.unpremultiply();
					pixel = Colours::white.overlaidWith (Colour (p.getARGB()));
				}
				else if (im.isRGB())
				{
					pixel = Colour (((const PixelRGB*) pixelData)->getARGB());
				}
				else
				{
					pixel = Colour ((uint8) 0, (uint8) 0, (uint8) 0, *pixelData);
				}
			}
			else
			{
				pixel = Colours::transparentWhite;
			}

			const uint8 pixelValues[3] = { pixel.getRed(), pixel.getGreen(), pixel.getBlue() };

			out << String::toHexString (pixelValues, 3, 0);
			charsOnLine += 3;

			if (charsOnLine > 100)
			{
				out << '\n';
				charsOnLine = 0;
			}
		}
	}

	out << "\n>}\n";
}

void LowLevelGraphicsPostScriptRenderer::drawImage (const Image& sourceImage, const Rectangle<int>& srcClip,
													const AffineTransform& transform, const bool /*fillEntireClipAsTiles*/)
{
	const int w = jmin (sourceImage.getWidth(), srcClip.getRight());
	const int h = jmin (sourceImage.getHeight(), srcClip.getBottom());

	writeClip();

	out << "gsave ";
	writeTransform (transform.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset)
							 .scaled (1.0f, -1.0f));

	RectangleList imageClip;
	sourceImage.createSolidAreaMask (imageClip, 0.5f);
	imageClip.clipTo (srcClip);

	out << "newpath ";
	int itemsOnLine = 0;

	for (RectangleList::Iterator i (imageClip); i.next();)
	{
		if (++itemsOnLine == 6)
		{
			out << '\n';
			itemsOnLine = 0;
		}

		const Rectangle<int>& r = *i.getRectangle();

		out << r.getX() << ' ' << r.getY() << ' ' << r.getWidth() << ' ' << r.getHeight() << " pr ";
	}

	out << " clip newpath\n";

	out << w << ' ' << h << " scale\n";
	out << w << ' ' << h << " 8 [" << w << " 0 0 -" << h << ' ' << (int) 0 << ' ' << h << " ]\n";

	writeImage (sourceImage, srcClip.getX(), srcClip.getY(), srcClip.getWidth(), srcClip.getHeight());

	out << "false 3 colorimage grestore\n";
	needToClip = true;
}

void LowLevelGraphicsPostScriptRenderer::drawLine (double x1, double y1, double x2, double y2)
{
	Path p;
	p.addLineSegment ((float) x1, (float) y1, (float) x2, (float) y2, 1.0f);
	fillPath (p, AffineTransform::identity);
}

void LowLevelGraphicsPostScriptRenderer::drawVerticalLine (const int x, double top, double bottom)
{
	drawLine (x, top, x, bottom);
}

void LowLevelGraphicsPostScriptRenderer::drawHorizontalLine (const int y, double left, double right)
{
	drawLine (left, y, right, y);
}

void LowLevelGraphicsPostScriptRenderer::setFont (const Font& newFont)
{
	stateStack.getLast()->font = newFont;
}

const Font LowLevelGraphicsPostScriptRenderer::getFont()
{
	return stateStack.getLast()->font;
}

void LowLevelGraphicsPostScriptRenderer::drawGlyph (int glyphNumber, const AffineTransform& transform)
{
	Path p;
	Font& font = stateStack.getLast()->font;
	font.getTypeface()->getOutlineForGlyph (glyphNumber, p);
	fillPath (p, AffineTransform::scale (font.getHeight() * font.getHorizontalScale(), font.getHeight()).followedBy (transform));
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_LowLevelGraphicsPostScriptRenderer.cpp ***/


/*** Start of inlined file: juce_LowLevelGraphicsSoftwareRenderer.cpp ***/
BEGIN_JUCE_NAMESPACE

#if (JUCE_WINDOWS || JUCE_LINUX) && ! JUCE_64BIT
 #define JUCE_USE_SSE_INSTRUCTIONS 1
#endif

#if JUCE_MSVC
 #if JUCE_DEBUG
  #pragma optimize ("t", on)  // optimise just this file, to avoid sluggish graphics when debugging
  #pragma warning (disable: 4714) // warning about forcedinline methods not being inlined
 #endif

 #pragma warning (push)
 #pragma warning (disable: 4127) // "expression is constant" warning
#endif

template <class PixelType, bool replaceExisting = false>
class SolidColourEdgeTableRenderer
{
public:
	SolidColourEdgeTableRenderer (const Image::BitmapData& data_, const PixelARGB& colour) throw()
		: data (data_),
		  sourceColour (colour)
	{
		if (sizeof (PixelType) == 3)
		{
			areRGBComponentsEqual = sourceColour.getRed() == sourceColour.getGreen()
										&& sourceColour.getGreen() == sourceColour.getBlue();
			filler[0].set (sourceColour);
			filler[1].set (sourceColour);
			filler[2].set (sourceColour);
			filler[3].set (sourceColour);
		}
	}

	forcedinline void setEdgeTableYPos (const int y) throw()
	{
		linePixels = (PixelType*) data.getLinePointer (y);
	}

	forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) const throw()
	{
		if (replaceExisting)
			linePixels[x].set (sourceColour);
		else
			linePixels[x].blend (sourceColour, alphaLevel);
	}

	forcedinline void handleEdgeTableLine (const int x, int width, const int alphaLevel) const throw()
	{
		PixelARGB p (sourceColour);
		p.multiplyAlpha (alphaLevel);

		PixelType* dest = linePixels + x;

		if (replaceExisting || p.getAlpha() >= 0xff)
			replaceLine (dest, p, width);
		else
			blendLine (dest, p, width);
	}

private:
	const Image::BitmapData& data;
	PixelType* linePixels;
	PixelARGB sourceColour;
	PixelRGB filler [4];
	bool areRGBComponentsEqual;

	inline void blendLine (PixelType* dest, const PixelARGB& colour, int width) const throw()
	{
		do
		{
			dest->blend (colour);
			++dest;
		} while (--width > 0);
	}

	forcedinline void replaceLine (PixelRGB* dest, const PixelARGB& colour, int width) const throw()
	{
		if (areRGBComponentsEqual)  // if all the component values are the same, we can cheat..
		{
			memset (dest, colour.getRed(), width * 3);
		}
		else
		{
			if (width >> 5)
			{
				const int* const intFiller = (const int*) filler;

				while (width > 8 && (((pointer_sized_int) dest) & 7) != 0)
				{
					dest->set (colour);
					++dest;
					--width;
				}

				while (width > 4)
				{
					((int*) dest) [0] = intFiller[0];
					((int*) dest) [1] = intFiller[1];
					((int*) dest) [2] = intFiller[2];
					dest = (PixelRGB*) (((uint8*) dest) + 12);
					width -= 4;
				}
			}

			while (--width >= 0)
			{
				dest->set (colour);
				++dest;
			}
		}
	}

	forcedinline void replaceLine (PixelAlpha* dest, const PixelARGB& colour, int width) const throw()
	{
		memset (dest, colour.getAlpha(), width);
	}

	forcedinline void replaceLine (PixelARGB* dest, const PixelARGB& colour, int width) const throw()
	{
		do
		{
			dest->set (colour);
			++dest;

		} while (--width > 0);
	}

	SolidColourEdgeTableRenderer (const SolidColourEdgeTableRenderer&);
	SolidColourEdgeTableRenderer& operator= (const SolidColourEdgeTableRenderer&);
};

class LinearGradientPixelGenerator
{
public:
	LinearGradientPixelGenerator (const ColourGradient& gradient, const AffineTransform& transform, const PixelARGB* const lookupTable_, const int numEntries_)
		: lookupTable (lookupTable_), numEntries (numEntries_)
	{
		jassert (numEntries_ >= 0);
		float x1 = gradient.x1;
		float y1 = gradient.y1;
		float x2 = gradient.x2;
		float y2 = gradient.y2;

		if (! transform.isIdentity())
		{
			const Line l (x2, y2, x1, y1);
			const Point<float> p3 = l.getPointAlongLine (0.0f, 100.0f);
			float x3 = p3.getX();
			float y3 = p3.getY();

			transform.transformPoint (x1, y1);
			transform.transformPoint (x2, y2);
			transform.transformPoint (x3, y3);

			const Line l2 (x2, y2, x3, y3);
			const float prop = l2.findNearestPointTo (x1, y1);
			const Point<float> newP2 (l2.getPointAlongLineProportionally (prop));

			x2 = newP2.getX();
			y2 = newP2.getY();
		}

		vertical = fabs (x1 - x2) < 0.001f;
		horizontal = fabs (y1 - y2) < 0.001f;

		if (vertical)
		{
			scale = roundToInt ((numEntries << (int) numScaleBits) / (double) (y2 - y1));
			start = roundToInt (y1 * scale);
		}
		else if (horizontal)
		{
			scale = roundToInt ((numEntries << (int) numScaleBits) / (double) (x2 - x1));
			start = roundToInt (x1 * scale);
		}
		else
		{
			grad = (y2 - y1) / (double) (x1 - x2);
			yTerm = y1 - x1 / grad;
			scale = roundToInt ((numEntries << (int) numScaleBits) / (yTerm * grad - (y2 * grad - x2)));
			grad *= scale;
		}
	}

	forcedinline void setY (const int y) throw()
	{
		if (vertical)
			linePix = lookupTable [jlimit (0, numEntries, (y * scale - start) >> (int) numScaleBits)];
		else if (! horizontal)
			start = roundToInt ((y - yTerm) * grad);
	}

	inline const PixelARGB getPixel (const int x) const throw()
	{
		return vertical ? linePix
						: lookupTable [jlimit (0, numEntries, (x * scale - start) >> (int) numScaleBits)];
	}

private:
	const PixelARGB* const lookupTable;
	const int numEntries;
	PixelARGB linePix;
	int start, scale;
	double grad, yTerm;
	bool vertical, horizontal;
	enum { numScaleBits = 12 };

	LinearGradientPixelGenerator (const LinearGradientPixelGenerator&);
	LinearGradientPixelGenerator& operator= (const LinearGradientPixelGenerator&);
};

class RadialGradientPixelGenerator
{
public:
	RadialGradientPixelGenerator (const ColourGradient& gradient, const AffineTransform&,
								  const PixelARGB* const lookupTable_, const int numEntries_) throw()
		: lookupTable (lookupTable_),
		  numEntries (numEntries_),
		  gx1 (gradient.x1),
		  gy1 (gradient.y1)
	{
		jassert (numEntries_ >= 0);
		const float gdx = gradient.x1 - gradient.x2;
		const float gdy = gradient.y1 - gradient.y2;
		maxDist = gdx * gdx + gdy * gdy;
		invScale = numEntries / sqrt (maxDist);
		jassert (roundToInt (sqrt (maxDist) * invScale) <= numEntries);
	}

	forcedinline void setY (const int y) throw()
	{
		dy = y - gy1;
		dy *= dy;
	}

	inline const PixelARGB getPixel (const int px) const throw()
	{
		double x = px - gx1;
		x *= x;
		x += dy;

		return lookupTable [x >= maxDist ? numEntries : roundToInt (sqrt (x) * invScale)];
	}

protected:
	const PixelARGB* const lookupTable;
	const int numEntries;
	const double gx1, gy1;
	double maxDist, invScale, dy;

	RadialGradientPixelGenerator (const RadialGradientPixelGenerator&);
	RadialGradientPixelGenerator& operator= (const RadialGradientPixelGenerator&);
};

class TransformedRadialGradientPixelGenerator   : public RadialGradientPixelGenerator
{
public:
	TransformedRadialGradientPixelGenerator (const ColourGradient& gradient, const AffineTransform& transform,
											 const PixelARGB* const lookupTable_, const int numEntries_) throw()
		: RadialGradientPixelGenerator (gradient, transform, lookupTable_, numEntries_),
		  inverseTransform (transform.inverted())
	{
		tM10 = inverseTransform.mat10;
		tM00 = inverseTransform.mat00;
	}

	forcedinline void setY (const int y) throw()
	{
		lineYM01 = inverseTransform.mat01 * y + inverseTransform.mat02 - gx1;
		lineYM11 = inverseTransform.mat11 * y + inverseTransform.mat12 - gy1;
	}

	inline const PixelARGB getPixel (const int px) const throw()
	{
		double x = px;
		const double y = tM10 * x + lineYM11;
		x = tM00 * x + lineYM01;
		x *= x;
		x += y * y;

		if (x >= maxDist)
			return lookupTable [numEntries];
		else
			return lookupTable [jmin (numEntries, roundToInt (sqrt (x) * invScale))];
	}

private:
	double tM10, tM00, lineYM01, lineYM11;
	const AffineTransform inverseTransform;

	TransformedRadialGradientPixelGenerator (const TransformedRadialGradientPixelGenerator&);
	TransformedRadialGradientPixelGenerator& operator= (const TransformedRadialGradientPixelGenerator&);
};

template <class PixelType, class GradientType>
class GradientEdgeTableRenderer  : public GradientType
{
public:
	GradientEdgeTableRenderer (const Image::BitmapData& destData_, const ColourGradient& gradient, const AffineTransform& transform,
							   const PixelARGB* const lookupTable_, const int numEntries_) throw()
		: GradientType (gradient, transform, lookupTable_, numEntries_ - 1),
		  destData (destData_)
	{
	}

	forcedinline void setEdgeTableYPos (const int y) throw()
	{
		linePixels = (PixelType*) destData.getLinePointer (y);
		GradientType::setY (y);
	}

	forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) const throw()
	{
		linePixels[x].blend (GradientType::getPixel (x), alphaLevel);
	}

	forcedinline void handleEdgeTableLine (int x, int width, const int alphaLevel) const throw()
	{
		PixelType* dest = linePixels + x;

		if (alphaLevel < 0xff)
		{
			do
			{
				(dest++)->blend (GradientType::getPixel (x++), alphaLevel);
			} while (--width > 0);
		}
		else
		{
			do
			{
				(dest++)->blend (GradientType::getPixel (x++));
			} while (--width > 0);
		}
	}

private:
	const Image::BitmapData& destData;
	PixelType* linePixels;

	GradientEdgeTableRenderer (const GradientEdgeTableRenderer&);
	GradientEdgeTableRenderer& operator= (const GradientEdgeTableRenderer&);
};

static forcedinline int safeModulo (int n, const int divisor) throw()
{
	jassert (divisor > 0);
	n %= divisor;
	return (n < 0) ? (n + divisor) : n;
}

template <class DestPixelType, class SrcPixelType, bool repeatPattern>
class ImageFillEdgeTableRenderer
{
public:
	ImageFillEdgeTableRenderer (const Image::BitmapData& destData_,
								const Image::BitmapData& srcData_,
								const int extraAlpha_,
								const int x, const int y) throw()
		: destData (destData_),
		  srcData (srcData_),
		  extraAlpha (extraAlpha_ + 1),
		  xOffset (repeatPattern ? safeModulo (x, srcData_.width) - srcData_.width : x),
		  yOffset (repeatPattern ? safeModulo (y, srcData_.height) - srcData_.height : y)
	{
	}

	forcedinline void setEdgeTableYPos (int y) throw()
	{
		linePixels = (DestPixelType*) destData.getLinePointer (y);

		y -= yOffset;
		if (repeatPattern)
		{
			jassert (y >= 0);
			y %= srcData.height;
		}

		sourceLineStart = (SrcPixelType*) srcData.getLinePointer (y);
	}

	forcedinline void handleEdgeTablePixel (int x, int alphaLevel) const throw()
	{
		alphaLevel = (alphaLevel * extraAlpha) >> 8;

		linePixels[x].blend (sourceLineStart [repeatPattern ? ((x - xOffset) % srcData.width) : (x - xOffset)], alphaLevel);
	}

	forcedinline void handleEdgeTableLine (int x, int width, int alphaLevel) const throw()
	{
		DestPixelType* dest = linePixels + x;
		alphaLevel = (alphaLevel * extraAlpha) >> 8;
		x -= xOffset;

		jassert (repeatPattern || (x >= 0 && x + width <= srcData.width));

		if (alphaLevel < 0xfe)
		{
			do
			{
				dest++ ->blend (sourceLineStart [repeatPattern ? (x++ % srcData.width) : x++], alphaLevel);
			} while (--width > 0);
		}
		else
		{
			if (repeatPattern)
			{
				do
				{
					dest++ ->blend (sourceLineStart [x++ % srcData.width]);
				} while (--width > 0);
			}
			else
			{
				copyRow (dest, sourceLineStart + x, width);
			}
		}
	}

	void clipEdgeTableLine (EdgeTable& et, int x, int y, int width) throw()
	{
		jassert (x - xOffset >= 0 && x + width - xOffset <= srcData.width);
		SrcPixelType* s = (SrcPixelType*) srcData.getLinePointer (y - yOffset);
		uint8* mask = (uint8*) (s + x - xOffset);

		if (sizeof (SrcPixelType) == sizeof (PixelARGB))
			mask += PixelARGB::indexA;

		et.clipLineToMask (x, y, mask, sizeof (SrcPixelType), width);
	}

private:
	const Image::BitmapData& destData;
	const Image::BitmapData& srcData;
	const int extraAlpha, xOffset, yOffset;
	DestPixelType* linePixels;
	SrcPixelType* sourceLineStart;

	template <class PixelType1, class PixelType2>
	forcedinline static void copyRow (PixelType1* dest, PixelType2* src, int width) throw()
	{
		do
		{
			dest++ ->blend (*src++);
		} while (--width > 0);
	}

	forcedinline static void copyRow (PixelRGB* dest, PixelRGB* src, int width) throw()
	{
		memcpy (dest, src, width * sizeof (PixelRGB));
	}

	ImageFillEdgeTableRenderer (const ImageFillEdgeTableRenderer&);
	ImageFillEdgeTableRenderer& operator= (const ImageFillEdgeTableRenderer&);
};

template <class DestPixelType, class SrcPixelType, bool repeatPattern>
class TransformedImageFillEdgeTableRenderer
{
public:
	TransformedImageFillEdgeTableRenderer (const Image::BitmapData& destData_,
										   const Image::BitmapData& srcData_,
										   const AffineTransform& transform,
										   const int extraAlpha_,
										   const bool betterQuality_) throw()
		: interpolator (transform),
		  destData (destData_),
		  srcData (srcData_),
		  extraAlpha (extraAlpha_ + 1),
		  betterQuality (betterQuality_),
		  pixelOffset (betterQuality_ ? 0.5f : 0.0f),
		  pixelOffsetInt (betterQuality_ ? -128 : 0),
		  maxX (srcData_.width - 1),
		  maxY (srcData_.height - 1),
		  scratchSize (2048)
	{
		scratchBuffer.malloc (scratchSize);
	}

	~TransformedImageFillEdgeTableRenderer() throw()
	{
	}

	forcedinline void setEdgeTableYPos (const int newY) throw()
	{
		y = newY;
		linePixels = (DestPixelType*) destData.getLinePointer (newY);
	}

	forcedinline void handleEdgeTablePixel (const int x, int alphaLevel) throw()
	{
		alphaLevel *= extraAlpha;
		alphaLevel >>= 8;

		SrcPixelType p;
		generate (&p, x, 1);

		linePixels[x].blend (p, alphaLevel);
	}

	forcedinline void handleEdgeTableLine (const int x, int width, int alphaLevel) throw()
	{
		if (width > scratchSize)
		{
			scratchSize = width;
			scratchBuffer.malloc (scratchSize);
		}

		SrcPixelType* span = scratchBuffer;
		generate (span, x, width);

		DestPixelType* dest = linePixels + x;
		alphaLevel *= extraAlpha;
		alphaLevel >>= 8;

		if (alphaLevel < 0xfe)
		{
			do
			{
				dest++ ->blend (*span++, alphaLevel);
			} while (--width > 0);
		}
		else
		{
			do
			{
				dest++ ->blend (*span++);
			} while (--width > 0);
		}
	}

	void clipEdgeTableLine (EdgeTable& et, int x, int y_, int width) throw()
	{
		if (width > scratchSize)
		{
			scratchSize = width;
			scratchBuffer.malloc (scratchSize);
		}

		y = y_;
		generate (scratchBuffer, x, width);

		et.clipLineToMask (x, y_,
						   reinterpret_cast<uint8*> (scratchBuffer.getData()) + SrcPixelType::indexA,
						   sizeof (SrcPixelType), width);
	}

private:

	void generate (PixelARGB* dest, const int x, int numPixels) throw()
	{
		this->interpolator.setStartOfLine (x + pixelOffset, y + pixelOffset, numPixels);

		do
		{
			int hiResX, hiResY;
			this->interpolator.next (hiResX, hiResY);
			hiResX += pixelOffsetInt;
			hiResY += pixelOffsetInt;

			int loResX = hiResX >> 8;
			int loResY = hiResY >> 8;

			if (repeatPattern)
			{
				loResX = safeModulo (loResX, srcData.width);
				loResY = safeModulo (loResY, srcData.height);
			}

			if (betterQuality
				 && ((unsigned int) loResX) < (unsigned int) maxX
				 && ((unsigned int) loResY) < (unsigned int) maxY)
			{
				uint32 c[4] = { 256 * 128, 256 * 128, 256 * 128, 256 * 128 };
				hiResX &= 255;
				hiResY &= 255;

				const uint8* src = this->srcData.getPixelPointer (loResX, loResY);

				uint32 weight = (256 - hiResX) * (256 - hiResY);
				c[0] += weight * src[0];
				c[1] += weight * src[1];
				c[2] += weight * src[2];
				c[3] += weight * src[3];

				weight = hiResX * (256 - hiResY);
				c[0] += weight * src[4];
				c[1] += weight * src[5];
				c[2] += weight * src[6];
				c[3] += weight * src[7];

				src += this->srcData.lineStride;

				weight = (256 - hiResX) * hiResY;
				c[0] += weight * src[0];
				c[1] += weight * src[1];
				c[2] += weight * src[2];
				c[3] += weight * src[3];

				weight = hiResX * hiResY;
				c[0] += weight * src[4];
				c[1] += weight * src[5];
				c[2] += weight * src[6];
				c[3] += weight * src[7];

				dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 16),
							   (uint8) (c[PixelARGB::indexR] >> 16),
							   (uint8) (c[PixelARGB::indexG] >> 16),
							   (uint8) (c[PixelARGB::indexB] >> 16));
			}
			else
			{
				if (! repeatPattern)
				{
					// Beyond the edges, just repeat the edge pixels and leave the anti-aliasing to be handled by the edgetable
					if (loResX < 0)	 loResX = 0;
					if (loResY < 0)	 loResY = 0;
					if (loResX > maxX)  loResX = maxX;
					if (loResY > maxY)  loResY = maxY;
				}

				dest->set (*(const PixelARGB*) this->srcData.getPixelPointer (loResX, loResY));
			}

			++dest;

		} while (--numPixels > 0);
	}

	void generate (PixelRGB* dest, const int x, int numPixels) throw()
	{
		this->interpolator.setStartOfLine (x + pixelOffset, y + pixelOffset, numPixels);

		do
		{
			int hiResX, hiResY;
			this->interpolator.next (hiResX, hiResY);
			hiResX += pixelOffsetInt;
			hiResY += pixelOffsetInt;
			int loResX = hiResX >> 8;
			int loResY = hiResY >> 8;

			if (repeatPattern)
			{
				loResX = safeModulo (loResX, srcData.width);
				loResY = safeModulo (loResY, srcData.height);
			}

			if (betterQuality
				 && ((unsigned int) loResX) < (unsigned int) maxX
				 && ((unsigned int) loResY) < (unsigned int) maxY)
			{
				uint32 c[3] = { 256 * 128, 256 * 128, 256 * 128 };
				hiResX &= 255;
				hiResY &= 255;

				const uint8* src = this->srcData.getPixelPointer (loResX, loResY);

				unsigned int weight = (256 - hiResX) * (256 - hiResY);
				c[0] += weight * src[0];
				c[1] += weight * src[1];
				c[2] += weight * src[2];

				weight = hiResX * (256 - hiResY);
				c[0] += weight * src[3];
				c[1] += weight * src[4];
				c[2] += weight * src[5];

				src += this->srcData.lineStride;

				weight = (256 - hiResX) * hiResY;
				c[0] += weight * src[0];
				c[1] += weight * src[1];
				c[2] += weight * src[2];

				weight = hiResX * hiResY;
				c[0] += weight * src[3];
				c[1] += weight * src[4];
				c[2] += weight * src[5];

				dest->setARGB ((uint8) 255,
							   (uint8) (c[PixelRGB::indexR] >> 16),
							   (uint8) (c[PixelRGB::indexG] >> 16),
							   (uint8) (c[PixelRGB::indexB] >> 16));
			}
			else
			{
				if (! repeatPattern)
				{
					// Beyond the edges, just repeat the edge pixels and leave the anti-aliasing to be handled by the edgetable
					if (loResX < 0)	 loResX = 0;
					if (loResY < 0)	 loResY = 0;
					if (loResX > maxX)  loResX = maxX;
					if (loResY > maxY)  loResY = maxY;
				}

				dest->set (*(const PixelRGB*) this->srcData.getPixelPointer (loResX, loResY));
			}

			++dest;

		} while (--numPixels > 0);
	}

	void generate (PixelAlpha* dest, const int x, int numPixels) throw()
	{
		this->interpolator.setStartOfLine (x + pixelOffset, y + pixelOffset, numPixels);

		do
		{
			int hiResX, hiResY;
			this->interpolator.next (hiResX, hiResY);
			hiResX += pixelOffsetInt;
			hiResY += pixelOffsetInt;
			int loResX = hiResX >> 8;
			int loResY = hiResY >> 8;

			if (repeatPattern)
			{
				loResX = safeModulo (loResX, srcData.width);
				loResY = safeModulo (loResY, srcData.height);
			}

			if (betterQuality
				 && ((unsigned int) loResX) < (unsigned int) maxX
				 && ((unsigned int) loResY) < (unsigned int) maxY)
			{
				hiResX &= 255;
				hiResY &= 255;

				const uint8* src = this->srcData.getPixelPointer (loResX, loResY);
				uint32 c = 256 * 128;
				c += src[0] * ((256 - hiResX) * (256 - hiResY));
				c += src[1] * (hiResX * (256 - hiResY));
				src += this->srcData.lineStride;
				c += src[0] * ((256 - hiResX) * hiResY);
				c += src[1] * (hiResX * hiResY);

				*((uint8*) dest) = (uint8) c;
			}
			else
			{
				if (! repeatPattern)
				{
					// Beyond the edges, just repeat the edge pixels and leave the anti-aliasing to be handled by the edgetable
					if (loResX < 0)	 loResX = 0;
					if (loResY < 0)	 loResY = 0;
					if (loResX > maxX)  loResX = maxX;
					if (loResY > maxY)  loResY = maxY;
				}

				*((uint8*) dest) = *(this->srcData.getPixelPointer (loResX, loResY));
			}

			++dest;

		} while (--numPixels > 0);
	}

	class TransformedImageSpanInterpolator
	{
	public:
		TransformedImageSpanInterpolator (const AffineTransform& transform) throw()
			: inverseTransform (transform.inverted())
		{}

		void setStartOfLine (float x, float y, const int numPixels) throw()
		{
			float x1 = x, y1 = y;
			inverseTransform.transformPoint (x1, y1);
			x += numPixels;
			inverseTransform.transformPoint (x, y);

			xBresenham.set ((int) (x1 * 256.0f), (int) (x * 256.0f), numPixels);
			yBresenham.set ((int) (y1 * 256.0f), (int) (y * 256.0f), numPixels);
		}

		void next (int& x, int& y) throw()
		{
			x = xBresenham.n;
			xBresenham.stepToNext();
			y = yBresenham.n;
			yBresenham.stepToNext();
		}

	private:
		class BresenhamInterpolator
		{
		public:
			BresenhamInterpolator() throw() {}

			void set (const int n1, const int n2, const int numSteps_) throw()
			{
				numSteps = jmax (1, numSteps_);
				step = (n2 - n1) / numSteps;
				remainder = modulo = (n2 - n1) % numSteps;
				n = n1;

				if (modulo <= 0)
				{
					modulo += numSteps;
					remainder += numSteps;
					--step;
				}

				modulo -= numSteps;
			}

			forcedinline void stepToNext() throw()
			{
				modulo += remainder;
				n += step;

				if (modulo > 0)
				{
					modulo -= numSteps;
					++n;
				}
			}

			int n;

		private:
			int numSteps, step, modulo, remainder;
		};

		const AffineTransform inverseTransform;
		BresenhamInterpolator xBresenham, yBresenham;

		TransformedImageSpanInterpolator (const TransformedImageSpanInterpolator&);
		TransformedImageSpanInterpolator& operator= (const TransformedImageSpanInterpolator&);
	};

	TransformedImageSpanInterpolator interpolator;
	const Image::BitmapData& destData;
	const Image::BitmapData& srcData;
	const int extraAlpha;
	const bool betterQuality;
	const float pixelOffset;
	const int pixelOffsetInt, maxX, maxY;
	int y;
	DestPixelType* linePixels;
	HeapBlock <SrcPixelType> scratchBuffer;
	int scratchSize;

	TransformedImageFillEdgeTableRenderer (const TransformedImageFillEdgeTableRenderer&);
	TransformedImageFillEdgeTableRenderer& operator= (const TransformedImageFillEdgeTableRenderer&);
};

class LLGCSavedState
{
public:
	LLGCSavedState (const Rectangle<int>& clip_, const int xOffset_, const int yOffset_,
					const Font& font_, const FillType& fillType_,
					const Graphics::ResamplingQuality interpolationQuality_) throw()
		: edgeTable (new EdgeTableHolder (EdgeTable (clip_))),
		  xOffset (xOffset_), yOffset (yOffset_),
		  font (font_), fillType (fillType_),
		  interpolationQuality (interpolationQuality_)
	{
	}

	LLGCSavedState (const LLGCSavedState& other) throw()
		: edgeTable (other.edgeTable), xOffset (other.xOffset),
		  yOffset (other.yOffset), font (other.font),
		  fillType (other.fillType), interpolationQuality (other.interpolationQuality)
	{
	}

	~LLGCSavedState() throw()
	{
	}

	bool clipToRectangle (const Rectangle<int>& r) throw()
	{
		dupeEdgeTableIfMultiplyReferenced();
		edgeTable->edgeTable.clipToRectangle (r.translated (xOffset, yOffset));
		return ! edgeTable->edgeTable.isEmpty();
	}

	bool clipToRectangleList (const RectangleList& r) throw()
	{
		dupeEdgeTableIfMultiplyReferenced();
		RectangleList offsetList (r);
		offsetList.offsetAll (xOffset, yOffset);
		EdgeTable e2 (offsetList);
		edgeTable->edgeTable.clipToEdgeTable (e2);

		return ! edgeTable->edgeTable.isEmpty();
	}

	bool excludeClipRectangle (const Rectangle<int>& r) throw()
	{
		dupeEdgeTableIfMultiplyReferenced();
		edgeTable->edgeTable.excludeRectangle (r.translated (xOffset, yOffset));
		return ! edgeTable->edgeTable.isEmpty();
	}

	void clipToPath (const Path& p, const AffineTransform& transform) throw()
	{
		dupeEdgeTableIfMultiplyReferenced();
		EdgeTable et (edgeTable->edgeTable.getMaximumBounds(), p, transform.translated ((float) xOffset, (float) yOffset));
		edgeTable->edgeTable.clipToEdgeTable (et);
	}

	void fillEdgeTable (Image& image, EdgeTable& et, const bool replaceContents = false) throw()
	{
		et.clipToEdgeTable (edgeTable->edgeTable);

		Image::BitmapData destData (image, 0, 0, image.getWidth(), image.getHeight(), true);

		if (fillType.isGradient())
		{
			jassert (! replaceContents); // that option is just for solid colours

			ColourGradient g2 (*(fillType.gradient));
			g2.multiplyOpacity (fillType.getOpacity());
			g2.x1 -= 0.5f; g2.y1 -= 0.5f;
			g2.x2 -= 0.5f; g2.y2 -= 0.5f;
			AffineTransform transform (fillType.transform.translated ((float) xOffset, (float) yOffset));
			const bool isIdentity = transform.isOnlyTranslation();

			if (isIdentity)
			{
				// If our translation doesn't involve any distortion, we can speed it up..
				transform.transformPoint (g2.x1, g2.y1);
				transform.transformPoint (g2.x2, g2.y2);
				transform = AffineTransform::identity;
			}

			HeapBlock <PixelARGB> lookupTable;
			const int numLookupEntries = g2.createLookupTable (transform, lookupTable);
			jassert (numLookupEntries > 0);

			switch (image.getFormat())
			{
				case Image::ARGB:   renderGradient (et, destData, g2, transform, lookupTable, numLookupEntries, isIdentity, (PixelARGB*) 0); break;
				case Image::RGB:	renderGradient (et, destData, g2, transform, lookupTable, numLookupEntries, isIdentity, (PixelRGB*) 0); break;
				default:		renderGradient (et, destData, g2, transform, lookupTable, numLookupEntries, isIdentity, (PixelAlpha*) 0); break;
			}
		}
		else if (fillType.isTiledImage())
		{
			renderImage (image, *(fillType.image), fillType.image->getBounds(), fillType.transform, &et);
		}
		else
		{
			const PixelARGB fillColour (fillType.colour.getPixelARGB());

			switch (image.getFormat())
			{
				case Image::ARGB:   renderSolidFill (et, destData, fillColour, replaceContents, (PixelARGB*) 0); break;
				case Image::RGB:	renderSolidFill (et, destData, fillColour, replaceContents, (PixelRGB*) 0); break;
				default:		renderSolidFill (et, destData, fillColour, replaceContents, (PixelAlpha*) 0); break;
			}
		}
	}

	void renderImage (Image& destImage, const Image& sourceImage, const Rectangle<int>& srcClip,
					  const AffineTransform& t, const EdgeTable* const tiledFillClipRegion) throw()
	{
		const AffineTransform transform (t.translated ((float) xOffset, (float) yOffset));

		const Image::BitmapData destData (destImage, 0, 0, destImage.getWidth(), destImage.getHeight(), true);
		const Image::BitmapData srcData (sourceImage, srcClip.getX(), srcClip.getY(), srcClip.getWidth(), srcClip.getHeight());
		const int alpha = fillType.colour.getAlpha();
		const bool betterQuality = (interpolationQuality != Graphics::lowResamplingQuality);

		if (transform.isOnlyTranslation())
		{
			// If our translation doesn't involve any distortion, just use a simple blit..
			int tx = (int) (transform.getTranslationX() * 256.0f);
			int ty = (int) (transform.getTranslationY() * 256.0f);

			if ((! betterQuality) || ((tx | ty) & 224) == 0)
			{
				tx = ((tx + 128) >> 8);
				ty = ((ty + 128) >> 8);

				if (tiledFillClipRegion != 0)
				{
					blittedRenderImage (sourceImage, destImage, *tiledFillClipRegion, destData, srcData, alpha, tx, ty, true);
				}
				else
				{
					EdgeTable et (Rectangle<int> (tx, ty, srcClip.getWidth(), srcClip.getHeight()).getIntersection (destImage.getBounds()));
					et.clipToEdgeTable (edgeTable->edgeTable);

					if (! et.isEmpty())
						blittedRenderImage (sourceImage, destImage, et, destData, srcData, alpha, tx, ty, false);
				}

				return;
			}
		}

		if (transform.isSingularity())
			return;

		if (tiledFillClipRegion != 0)
		{
			transformedRenderImage (sourceImage, destImage, *tiledFillClipRegion, destData, srcData, alpha, transform, betterQuality, true);
		}
		else
		{
			Path p;
			p.addRectangle (0.0f, 0.0f, (float) srcClip.getWidth(), (float) srcClip.getHeight());

			EdgeTable et (edgeTable->edgeTable.getMaximumBounds(), p, transform);
			et.clipToEdgeTable (edgeTable->edgeTable);

			if (! et.isEmpty())
				transformedRenderImage (sourceImage, destImage, et, destData, srcData, alpha, transform, betterQuality, false);
		}
	}

	void clipToImageAlpha (const Image& image, const Rectangle<int>& srcClip, const AffineTransform& t) throw()
	{
		if (! image.hasAlphaChannel())
		{
			Path p;
			p.addRectangle (srcClip);
			clipToPath (p, t);
			return;
		}

		dupeEdgeTableIfMultiplyReferenced();

		const AffineTransform transform (t.translated ((float) xOffset, (float) yOffset));
		const Image::BitmapData srcData (image, srcClip.getX(), srcClip.getY(), srcClip.getWidth(), srcClip.getHeight());
		const bool betterQuality = (interpolationQuality != Graphics::lowResamplingQuality);
		EdgeTable& et = edgeTable->edgeTable;

		if (transform.isOnlyTranslation())
		{
			// If our translation doesn't involve any distortion, just use a simple blit..
			const int tx = (int) (transform.getTranslationX() * 256.0f);
			const int ty = (int) (transform.getTranslationY() * 256.0f);

			if ((! betterQuality) || ((tx | ty) & 224) == 0)
			{
				const int imageX = ((tx + 128) >> 8);
				const int imageY = ((ty + 128) >> 8);

				if (image.getFormat() == Image::ARGB)
					straightClipImage (et, srcData, imageX, imageY, (PixelARGB*)0);
				else
					straightClipImage (et, srcData, imageX, imageY, (PixelAlpha*)0);

				return;
			}
		}

		if (transform.isSingularity())
		{
			et.clipToRectangle (Rectangle<int>());
			return;
		}

		{
			Path p;
			p.addRectangle (0, 0, (float) srcData.width, (float) srcData.height);
			EdgeTable et2 (et.getMaximumBounds(), p, transform);
			et.clipToEdgeTable (et2);
		}

		if (! et.isEmpty())
		{
			if (image.getFormat() == Image::ARGB)
				transformedClipImage (et, srcData, transform, betterQuality, (PixelARGB*)0);
			else
				transformedClipImage (et, srcData, transform, betterQuality, (PixelAlpha*)0);
		}
	}

	template <class SrcPixelType>
	void transformedClipImage (EdgeTable& et, const Image::BitmapData& srcData, const AffineTransform& transform, const bool betterQuality, const SrcPixelType *) throw()
	{
		TransformedImageFillEdgeTableRenderer <SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, transform, 255, betterQuality);

		for (int y = 0; y < et.getMaximumBounds().getHeight(); ++y)
			renderer.clipEdgeTableLine (et, et.getMaximumBounds().getX(), y + et.getMaximumBounds().getY(),
										et.getMaximumBounds().getWidth());
	}

	template <class SrcPixelType>
	void straightClipImage (EdgeTable& et, const Image::BitmapData& srcData, int imageX, int imageY, const SrcPixelType *) throw()
	{
		Rectangle<int> r (imageX, imageY, srcData.width, srcData.height);
		et.clipToRectangle (r);

		ImageFillEdgeTableRenderer <SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, 255, imageX, imageY);

		for (int y = 0; y < r.getHeight(); ++y)
			renderer.clipEdgeTableLine (et, r.getX(), y + r.getY(), r.getWidth());
	}

	class EdgeTableHolder  : public ReferenceCountedObject
	{
	public:
		EdgeTableHolder (const EdgeTable& e) throw() : edgeTable (e) {}

		EdgeTable edgeTable;
	};

	ReferenceCountedObjectPtr<EdgeTableHolder> edgeTable;
	int xOffset, yOffset;
	Font font;
	FillType fillType;
	Graphics::ResamplingQuality interpolationQuality;

private:
	LLGCSavedState& operator= (const LLGCSavedState&);

	void dupeEdgeTableIfMultiplyReferenced() throw()
	{
		if (edgeTable->getReferenceCount() > 1)
			edgeTable = new EdgeTableHolder (edgeTable->edgeTable);
	}

	template <class DestPixelType>
	void renderGradient (EdgeTable& et, const Image::BitmapData& destData, const ColourGradient& g, const AffineTransform& transform,
						 const PixelARGB* const lookupTable, const int numLookupEntries, const bool isIdentity, DestPixelType*) throw()
	{
		jassert (destData.pixelStride == sizeof (DestPixelType));

		if (g.isRadial)
		{
			if (isIdentity)
			{
				GradientEdgeTableRenderer <DestPixelType, RadialGradientPixelGenerator> renderer (destData, g, transform, lookupTable, numLookupEntries);
				et.iterate (renderer);
			}
			else
			{
				GradientEdgeTableRenderer <DestPixelType, TransformedRadialGradientPixelGenerator> renderer (destData, g, transform, lookupTable, numLookupEntries);
				et.iterate (renderer);
			}
		}
		else
		{
			GradientEdgeTableRenderer <DestPixelType, LinearGradientPixelGenerator> renderer (destData, g, transform, lookupTable, numLookupEntries);
			et.iterate (renderer);
		}
	}

	template <class DestPixelType>
	void renderSolidFill (EdgeTable& et, const Image::BitmapData& destData, const PixelARGB& fillColour, const bool replaceContents, DestPixelType*) throw()
	{
		jassert (destData.pixelStride == sizeof (DestPixelType));
		if (replaceContents)
		{
			SolidColourEdgeTableRenderer <DestPixelType, true> r (destData, fillColour);
			et.iterate (r);
		}
		else
		{
			SolidColourEdgeTableRenderer <DestPixelType, false> r (destData, fillColour);
			et.iterate (r);
		}
	}

	void transformedRenderImage (const Image& srcImage, Image& destImage, const EdgeTable& et, const Image::BitmapData& destData, const Image::BitmapData& srcData,
								 const int alpha, const AffineTransform& transform, const bool betterQuality, const bool repeatPattern) throw()
	{
		switch (destImage.getFormat())
		{
		case Image::ARGB:
			switch (srcImage.getFormat())
			{
			case Image::ARGB:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelARGB, PixelARGB, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelARGB, PixelARGB, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			case Image::RGB:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelARGB, PixelRGB, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelARGB, PixelRGB, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			default:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelARGB, PixelAlpha, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelARGB, PixelAlpha, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			}
			break;
		case Image::RGB:
			switch (srcImage.getFormat())
			{
			case Image::ARGB:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelRGB, PixelARGB, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelRGB, PixelARGB, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			case Image::RGB:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelRGB, PixelRGB, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelRGB, PixelRGB, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			default:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelRGB, PixelAlpha, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelRGB, PixelAlpha, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			}
			break;
		default:
			switch (srcImage.getFormat())
			{
			case Image::ARGB:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelAlpha, PixelARGB, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelAlpha, PixelARGB, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			case Image::RGB:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelAlpha, PixelRGB, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelAlpha, PixelRGB, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			default:
				if (repeatPattern) { TransformedImageFillEdgeTableRenderer <PixelAlpha, PixelAlpha, true> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				else		   { TransformedImageFillEdgeTableRenderer <PixelAlpha, PixelAlpha, false> r (destData, srcData, transform, alpha, betterQuality); et.iterate (r); }
				break;
			}
			break;
		}
	}

	void blittedRenderImage (const Image& srcImage, Image& destImage, const EdgeTable& et, const Image::BitmapData& destData,
							 const Image::BitmapData& srcData, const int alpha, int x, int y, const bool repeatPattern) throw()
	{
		switch (destImage.getFormat())
		{
			case Image::ARGB:
				switch (srcImage.getFormat())
				{
				case Image::ARGB:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelARGB, PixelARGB, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelARGB, PixelARGB, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				case Image::RGB:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelARGB, PixelRGB, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelARGB, PixelRGB, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				default:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelARGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelARGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				}
			break;
			case Image::RGB:
				switch (srcImage.getFormat())
				{
				case Image::ARGB:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelRGB, PixelARGB, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelRGB, PixelARGB, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				case Image::RGB:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelRGB, PixelRGB, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelRGB, PixelRGB, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				default:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelRGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelRGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				}
			break;
			default:
				switch (srcImage.getFormat())
				{
				case Image::ARGB:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelAlpha, PixelARGB, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelAlpha, PixelARGB, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				case Image::RGB:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelAlpha, PixelRGB, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelAlpha, PixelRGB, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				default:
					if (repeatPattern) { ImageFillEdgeTableRenderer <PixelAlpha, PixelAlpha, true> r (destData, srcData, alpha, x, y); et.iterate (r); }
					else		   { ImageFillEdgeTableRenderer <PixelAlpha, PixelAlpha, false> r (destData, srcData, alpha, x, y); et.iterate (r); }
					break;
				}
			break;
		}
	}
};

LowLevelGraphicsSoftwareRenderer::LowLevelGraphicsSoftwareRenderer (Image& image_)
	: image (image_)
{
	currentState = new LLGCSavedState (image_.getBounds(), 0, 0, Font(),
									   FillType(), Graphics::mediumResamplingQuality);
}

LowLevelGraphicsSoftwareRenderer::~LowLevelGraphicsSoftwareRenderer()
{
}

bool LowLevelGraphicsSoftwareRenderer::isVectorDevice() const
{
	return false;
}

void LowLevelGraphicsSoftwareRenderer::setOrigin (int x, int y)
{
	currentState->xOffset += x;
	currentState->yOffset += y;
}

bool LowLevelGraphicsSoftwareRenderer::clipToRectangle (const Rectangle<int>& r)
{
	return currentState->clipToRectangle (r);
}

bool LowLevelGraphicsSoftwareRenderer::clipToRectangleList (const RectangleList& clipRegion)
{
	return currentState->clipToRectangleList (clipRegion);
}

void LowLevelGraphicsSoftwareRenderer::excludeClipRectangle (const Rectangle<int>& r)
{
	currentState->excludeClipRectangle (r);
}

void LowLevelGraphicsSoftwareRenderer::clipToPath (const Path& path, const AffineTransform& transform)
{
	currentState->clipToPath (path, transform);
}

void LowLevelGraphicsSoftwareRenderer::clipToImageAlpha (const Image& sourceImage, const Rectangle<int>& srcClip, const AffineTransform& transform)
{
	currentState->clipToImageAlpha (sourceImage, srcClip, transform);
}

bool LowLevelGraphicsSoftwareRenderer::clipRegionIntersects (const Rectangle<int>& r)
{
	return currentState->edgeTable->edgeTable.getMaximumBounds()
				.intersects (r.translated (currentState->xOffset, currentState->yOffset));
}

const Rectangle<int> LowLevelGraphicsSoftwareRenderer::getClipBounds() const
{
	return currentState->edgeTable->edgeTable.getMaximumBounds().translated (-currentState->xOffset, -currentState->yOffset);
}

bool LowLevelGraphicsSoftwareRenderer::isClipEmpty() const
{
	return currentState->edgeTable->edgeTable.isEmpty();
}

void LowLevelGraphicsSoftwareRenderer::saveState()
{
	stateStack.add (new LLGCSavedState (*currentState));
}

void LowLevelGraphicsSoftwareRenderer::restoreState()
{
	LLGCSavedState* const top = stateStack.getLast();

	if (top != 0)
	{
		currentState = top;
		stateStack.removeLast (1, false);
	}
	else
	{
		jassertfalse // trying to pop with an empty stack!
	}
}

void LowLevelGraphicsSoftwareRenderer::setFill (const FillType& fillType)
{
	currentState->fillType = fillType;
}

void LowLevelGraphicsSoftwareRenderer::setOpacity (float newOpacity)
{
	currentState->fillType.setOpacity (newOpacity);
}

void LowLevelGraphicsSoftwareRenderer::setInterpolationQuality (Graphics::ResamplingQuality quality)
{
	currentState->interpolationQuality = quality;
}

void LowLevelGraphicsSoftwareRenderer::fillRect (const Rectangle<int>& r, const bool replaceExistingContents)
{
	const Rectangle<int>& totalClip = currentState->edgeTable->edgeTable.getMaximumBounds();
	const Rectangle<int> clipped (totalClip.getIntersection (r.translated (currentState->xOffset, currentState->yOffset)));

	if (! clipped.isEmpty())
	{
		EdgeTable et (clipped);
		currentState->fillEdgeTable (image, et, replaceExistingContents);
	}
}

void LowLevelGraphicsSoftwareRenderer::fillPath (const Path& path, const AffineTransform& transform)
{
	EdgeTable et (currentState->edgeTable->edgeTable.getMaximumBounds(),
				  path, transform.translated ((float) currentState->xOffset,
											  (float) currentState->yOffset));

	currentState->fillEdgeTable (image, et);
}

void LowLevelGraphicsSoftwareRenderer::drawImage (const Image& sourceImage, const Rectangle<int>& srcClip,
												  const AffineTransform& transform, const bool fillEntireClipAsTiles)
{
	jassert (sourceImage.getBounds().contains (srcClip));

	currentState->renderImage (image, sourceImage, srcClip, transform,
							   fillEntireClipAsTiles ? &(currentState->edgeTable->edgeTable) : 0);
}

void LowLevelGraphicsSoftwareRenderer::drawLine (double x1, double y1, double x2, double y2)
{
	Path p;
	p.addLineSegment ((float) x1, (float) y1, (float) x2, (float) y2, 1.0f);
	fillPath (p, AffineTransform::identity);
}

void LowLevelGraphicsSoftwareRenderer::drawVerticalLine (const int x, double top, double bottom)
{
	if (bottom > top)
	{
		EdgeTable et ((float) (x + currentState->xOffset), (float) (top + currentState->yOffset), 1.0f, (float) (bottom - top));
		currentState->fillEdgeTable (image, et);
	}
}

void LowLevelGraphicsSoftwareRenderer::drawHorizontalLine (const int y, double left, double right)
{
	if (right > left)
	{
		EdgeTable et ((float) (left + currentState->xOffset), (float) (y + currentState->yOffset),
					  (float) (right - left), 1.0f);
		currentState->fillEdgeTable (image, et);
	}
}

class GlyphCache  : private DeletedAtShutdown
{
public:
	GlyphCache() throw()
		: accessCounter (0), hits (0), misses (0)
	{
		for (int i = 120; --i >= 0;)
			glyphs.add (new CachedGlyph());
	}

	~GlyphCache() throw()
	{
		clearSingletonInstance();
	}

	juce_DeclareSingleton_SingleThreaded_Minimal (GlyphCache);

	void drawGlyph (LLGCSavedState& state, Image& image, const Font& font, const int glyphNumber, float x, float y) throw()
	{
		++accessCounter;
		int oldestCounter = std::numeric_limits<int>::max();
		CachedGlyph* oldest = 0;

		for (int i = glyphs.size(); --i >= 0;)
		{
			CachedGlyph* const glyph = glyphs.getUnchecked (i);

			if (glyph->glyph == glyphNumber && glyph->font == font)
			{
				++hits;
				glyph->lastAccessCount = accessCounter;
				glyph->draw (state, image, x, y);
				return;
			}

			if (glyph->lastAccessCount <= oldestCounter)
			{
				oldestCounter = glyph->lastAccessCount;
				oldest = glyph;
			}
		}

		if (hits + ++misses > (glyphs.size() << 4))
		{
			if (misses * 2 > hits)
			{
				for (int i = 32; --i >= 0;)
					glyphs.add (new CachedGlyph());
			}

			hits = misses = 0;
			oldest = glyphs.getLast();
		}

		jassert (oldest != 0);
		oldest->lastAccessCount = accessCounter;
		oldest->generate (font, glyphNumber);
		oldest->draw (state, image, x, y);
	}

	class CachedGlyph
	{
	public:
		CachedGlyph() : glyph (0), lastAccessCount (0) {}
		~CachedGlyph()  {}

		void draw (LLGCSavedState& state, Image& image, const float x, const float y) const throw()
		{
			if (edgeTable != 0)
			{
				EdgeTable et (*edgeTable);
				et.translate (x, roundToInt (y));
				state.fillEdgeTable (image, et, false);
			}
		}

		void generate (const Font& newFont, const int glyphNumber) throw()
		{
			font = newFont;
			glyph = glyphNumber;
			edgeTable = 0;

			Path glyphPath;
			font.getTypeface()->getOutlineForGlyph (glyphNumber, glyphPath);

			if (! glyphPath.isEmpty())
			{
				const float fontHeight = font.getHeight();
				const AffineTransform transform (AffineTransform::scale (fontHeight * font.getHorizontalScale(), fontHeight)
																 .translated (0.0f, -0.5f));

				edgeTable = new EdgeTable (glyphPath.getBoundsTransformed (transform).getSmallestIntegerContainer().expanded (1, 0),
										   glyphPath, transform);
			}
		}

		int glyph, lastAccessCount;
		Font font;

		juce_UseDebuggingNewOperator

	private:
		ScopedPointer <EdgeTable> edgeTable;

		CachedGlyph (const CachedGlyph&);
		CachedGlyph& operator= (const CachedGlyph&);
	};

	juce_UseDebuggingNewOperator

private:
	OwnedArray <CachedGlyph> glyphs;
	int accessCounter, hits, misses;

	GlyphCache (const GlyphCache&);
	GlyphCache& operator= (const GlyphCache&);
};

juce_ImplementSingleton_SingleThreaded (GlyphCache);

void LowLevelGraphicsSoftwareRenderer::setFont (const Font& newFont)
{
	currentState->font = newFont;
}

const Font LowLevelGraphicsSoftwareRenderer::getFont()
{
	return currentState->font;
}

void LowLevelGraphicsSoftwareRenderer::drawGlyph (int glyphNumber, const AffineTransform& transform)
{
	Font& f = currentState->font;

	if (transform.isOnlyTranslation())
	{
		GlyphCache::getInstance()->drawGlyph (*currentState, image, f, glyphNumber,
											  transform.getTranslationX() + (float) currentState->xOffset,
											  transform.getTranslationY() + (float) currentState->yOffset);
	}
	else
	{
		Path p;
		f.getTypeface()->getOutlineForGlyph (glyphNumber, p);
		fillPath (p, AffineTransform::scale (f.getHeight() * f.getHorizontalScale(), f.getHeight()).followedBy (transform));
	}
}

#if JUCE_MSVC
 #pragma warning (pop)

 #if JUCE_DEBUG
  #pragma optimize ("", on)  // resets optimisations to the project defaults
 #endif
#endif

END_JUCE_NAMESPACE
/*** End of inlined file: juce_LowLevelGraphicsSoftwareRenderer.cpp ***/


/*** Start of inlined file: juce_RectanglePlacement.cpp ***/
BEGIN_JUCE_NAMESPACE

RectanglePlacement::RectanglePlacement (const RectanglePlacement& other) throw()
	: flags (other.flags)
{
}

RectanglePlacement& RectanglePlacement::operator= (const RectanglePlacement& other) throw()
{
	flags = other.flags;
	return *this;
}

void RectanglePlacement::applyTo (double& x, double& y,
								  double& w, double& h,
								  const double dx, const double dy,
								  const double dw, const double dh) const throw()
{
	if (w == 0 || h == 0)
		return;

	if ((flags & stretchToFit) != 0)
	{
		x = dx;
		y = dy;
		w = dw;
		h = dh;
	}
	else
	{
		double scale = (flags & fillDestination) != 0 ? jmax (dw / w, dh / h)
													  : jmin (dw / w, dh / h);

		if ((flags & onlyReduceInSize) != 0)
			scale = jmin (scale, 1.0);

		if ((flags & onlyIncreaseInSize) != 0)
			scale = jmax (scale, 1.0);

		w *= scale;
		h *= scale;

		if ((flags & xLeft) != 0)
			x = dx;
		else if ((flags & xRight) != 0)
			x = dx + dw - w;
		else
			x = dx + (dw - w) * 0.5;

		if ((flags & yTop) != 0)
			y = dy;
		else if ((flags & yBottom) != 0)
			y = dy + dh - h;
		else
			y = dy + (dh - h) * 0.5;
	}
}

const AffineTransform RectanglePlacement::getTransformToFit (float x, float y,
															 float w, float h,
															 const float dx, const float dy,
															 const float dw, const float dh) const throw()
{
	if (w == 0 || h == 0)
		return AffineTransform::identity;

	const float scaleX = dw / w;
	const float scaleY = dh / h;

	if ((flags & stretchToFit) != 0)
		return AffineTransform::translation (-x, -y)
					.scaled (scaleX, scaleY)
					.translated (dx, dy);

	float scale = (flags & fillDestination) != 0 ? jmax (scaleX, scaleY)
												 : jmin (scaleX, scaleY);

	if ((flags & onlyReduceInSize) != 0)
		scale = jmin (scale, 1.0f);

	if ((flags & onlyIncreaseInSize) != 0)
		scale = jmax (scale, 1.0f);

	w *= scale;
	h *= scale;

	float newX = dx;

	if ((flags & xRight) != 0)
		newX += dw - w;		 // right
	else if ((flags & xLeft) == 0)
		newX += (dw - w) / 2.0f;	// centre

	float newY = dy;

	if ((flags & yBottom) != 0)
		newY += dh - h;		 // bottom
	else if ((flags & yTop) == 0)
		newY += (dh - h) / 2.0f;	// centre

	return AffineTransform::translation (-x, -y)
				.scaled (scale, scale)
				.translated (newX, newY);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_RectanglePlacement.cpp ***/


/*** Start of inlined file: juce_Drawable.cpp ***/
BEGIN_JUCE_NAMESPACE

Drawable::RenderingContext::RenderingContext (Graphics& g_,
											  const AffineTransform& transform_,
											  const float opacity_) throw()
	: g (g_),
	  transform (transform_),
	  opacity (opacity_)
{
}

Drawable::Drawable()
{
}

Drawable::~Drawable()
{
}

void Drawable::draw (Graphics& g, const float opacity,
					 const AffineTransform& transform) const
{
	render (RenderingContext (g, transform, opacity));
}

void Drawable::drawAt (Graphics& g, const float x, const float y, const float opacity) const
{
	draw (g, opacity, AffineTransform::translation (x, y));
}

void Drawable::drawWithin (Graphics& g,
						   const int destX,
						   const int destY,
						   const int destW,
						   const int destH,
						   const RectanglePlacement& placement,
						   const float opacity) const
{
	if (destW > 0 && destH > 0)
	{
		Rectangle<float> bounds (getBounds());

		draw (g, opacity,
			  placement.getTransformToFit (bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
										   (float) destX, (float) destY,
										   (float) destW, (float) destH));
	}
}

Drawable* Drawable::createFromImageData (const void* data, const size_t numBytes)
{
	Drawable* result = 0;

	Image* const image = ImageFileFormat::loadFrom (data, (int) numBytes);

	if (image != 0)
	{
		DrawableImage* const di = new DrawableImage();
		di->setImage (image, true);
		result = di;
	}
	else
	{
		const String asString (String::createStringFromData (data, (int) numBytes));

		XmlDocument doc (asString);
		ScopedPointer <XmlElement> outer (doc.getDocumentElement (true));

		if (outer != 0 && outer->hasTagName ("svg"))
		{
			ScopedPointer <XmlElement> svg (doc.getDocumentElement());

			if (svg != 0)
				result = Drawable::createFromSVG (*svg);
		}
	}

	return result;
}

Drawable* Drawable::createFromImageDataStream (InputStream& dataSource)
{
	MemoryBlock mb;
	dataSource.readIntoMemoryBlock (mb);

	return createFromImageData (mb.getData(), mb.getSize());
}

Drawable* Drawable::createFromImageFile (const File& file)
{
	const ScopedPointer <FileInputStream> fin (file.createInputStream());

	return fin != 0 ? createFromImageDataStream (*fin) : 0;
}

Drawable* Drawable::createFromValueTree (const ValueTree& tree) throw()
{
	Drawable* d = DrawablePath::createFromValueTree (tree);

	if (d == 0)
	{
		d = DrawableComposite::createFromValueTree (tree);

		if (d == 0)
		{
			d = DrawableImage::createFromValueTree (tree);

			if (d == 0)
				d = DrawableText::createFromValueTree (tree);
		}
	}

	return d;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Drawable.cpp ***/


/*** Start of inlined file: juce_DrawableComposite.cpp ***/
BEGIN_JUCE_NAMESPACE

DrawableComposite::DrawableComposite()
{
}

DrawableComposite::~DrawableComposite()
{
}

void DrawableComposite::insertDrawable (Drawable* drawable,
										const AffineTransform& transform,
										const int index)
{
	if (drawable != 0)
	{
		if (! drawables.contains (drawable))
		{
			drawables.insert (index, drawable);

			if (transform.isIdentity())
				transforms.insert (index, 0);
			else
				transforms.insert (index, new AffineTransform (transform));
		}
		else
		{
			jassertfalse // trying to add a drawable that's already in here!
		}
	}
}

void DrawableComposite::insertDrawable (const Drawable& drawable,
										const AffineTransform& transform,
										const int index)
{
	insertDrawable (drawable.createCopy(), transform, index);
}

void DrawableComposite::removeDrawable (const int index, const bool deleteDrawable)
{
	drawables.remove (index, deleteDrawable);
	transforms.remove (index);
}

void DrawableComposite::bringToFront (const int index)
{
	if (index >= 0 && index < drawables.size() - 1)
	{
		drawables.move (index, -1);
		transforms.move (index, -1);
	}
}

void DrawableComposite::render (const Drawable::RenderingContext& context) const
{
	if (drawables.size() > 0 && context.opacity > 0)
	{
		if (context.opacity >= 1.0f || drawables.size() == 1)
		{
			Drawable::RenderingContext contextCopy (context);

			for (int i = 0; i < drawables.size(); ++i)
			{
				const AffineTransform* const t = transforms.getUnchecked(i);
				contextCopy.transform = (t == 0) ? context.transform
												 : t->followedBy (context.transform);

				drawables.getUnchecked(i)->render (contextCopy);
			}
		}
		else
		{
			// To correctly render a whole composite layer with an overall transparency,
			// we need to render everything opaquely into a temp buffer, then blend that
			// with the target opacity...
			const Rectangle<int> clipBounds (context.g.getClipBounds());
			Image tempImage (Image::ARGB, clipBounds.getWidth(), clipBounds.getHeight(), true);

			{
				Graphics tempG (tempImage);
				tempG.setOrigin (-clipBounds.getX(), -clipBounds.getY());
				Drawable::RenderingContext tempContext (tempG, context.transform, 1.0f);
				render (tempContext);
			}

			context.g.setOpacity (context.opacity);
			context.g.drawImageAt (&tempImage, clipBounds.getX(), clipBounds.getY());
		}
	}
}

const Rectangle<float> DrawableComposite::getBounds() const
{
	Rectangle<float> bounds;

	for (int i = 0; i < drawables.size(); ++i)
	{
		const Drawable* const d = drawables.getUnchecked(i);
		const AffineTransform* const t = transforms.getUnchecked(i);

		const Rectangle<float> childBounds (t == 0 ? d->getBounds()
												   : d->getBounds().transformed (*t));

		if (bounds.isEmpty())
			bounds = childBounds;
		else if (! childBounds.isEmpty())
			bounds = bounds.getUnion (childBounds);
	}

	return bounds;
}

bool DrawableComposite::hitTest (float x, float y) const
{
	for (int i = 0; i < drawables.size(); ++i)
	{
		float tx = x;
		float ty = y;

		const AffineTransform* const t = transforms.getUnchecked(i);

		if (t != 0)
			t->inverted().transformPoint (tx, ty);

		if (drawables.getUnchecked(i)->hitTest (tx, ty))
			return true;
	}

	return false;
}

Drawable* DrawableComposite::createCopy() const
{
	DrawableComposite* const dc = new DrawableComposite();

	for (int i = 0; i < drawables.size(); ++i)
	{
		dc->drawables.add (drawables.getUnchecked(i)->createCopy());

		const AffineTransform* const t = transforms.getUnchecked(i);
		dc->transforms.add (t != 0 ? new AffineTransform (*t) : 0);
	}

	return dc;
}

ValueTree DrawableComposite::createValueTree() const throw()
{
	ValueTree v ("Group");

	if (getName().isNotEmpty())
		v.setProperty ("id", getName(), 0);

	for (int i = 0; i < drawables.size(); ++i)
	{
		Drawable* const d = drawables.getUnchecked(i);

		ValueTree child (d->createValueTree());

		AffineTransform* transform = transforms.getUnchecked(i);

		if (transform != 0)
		{
			String t;
			t <<  transform->mat00 << " " << transform->mat01 << " " << transform->mat02 << " "
			  << transform->mat10 << " " << transform->mat11 << " " << transform->mat12;

			child.setProperty ("transform", t, 0);
		}

		v.addChild (child, -1, 0);
	}

	return v;
}

DrawableComposite* DrawableComposite::createFromValueTree (const ValueTree& tree) throw()
{
	if (! tree.hasType ("Group"))
		return 0;

	DrawableComposite* dc = new DrawableComposite();
	dc->setName (tree ["id"]);

	for (int i = 0; i < tree.getNumChildren(); ++i)
	{
		ValueTree childTree (tree.getChild (i));
		Drawable* d = Drawable::createFromValueTree (childTree);

		if (d != 0)
		{
			AffineTransform transform;
			const String transformAtt (childTree ["transform"].toString());

			if (transformAtt.isNotEmpty())
			{
				StringArray tokens;
				tokens.addTokens (transformAtt.trim(), false);
				tokens.removeEmptyStrings (true);

				if (tokens.size() == 6)
				{
					float f[6];
					for (int j = 0; j < 6; ++j)
						f[j] = (float) tokens[j].getDoubleValue();

					transform = AffineTransform (f[0], f[1], f[2], f[3], f[4], f[5]);
				}
			}

			dc->insertDrawable (d, transform);
		}
	}

	return dc;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DrawableComposite.cpp ***/


/*** Start of inlined file: juce_DrawableImage.cpp ***/
BEGIN_JUCE_NAMESPACE

DrawableImage::DrawableImage()
	: image (0),
	  canDeleteImage (false),
	  opacity (1.0f),
	  overlayColour (0x00000000)
{
}

DrawableImage::~DrawableImage()
{
	clearImage();
}

void DrawableImage::clearImage()
{
	if (canDeleteImage && image != 0)
		ImageCache::releaseOrDelete (image);

	image = 0;
}

void DrawableImage::setImage (const Image& imageToCopy)
{
	clearImage();
	image = new Image (imageToCopy);
	canDeleteImage = true;
}

void DrawableImage::setImage (Image* imageToUse,
							  const bool releaseWhenNotNeeded)
{
	clearImage();
	image = imageToUse;
	canDeleteImage = releaseWhenNotNeeded;
}

void DrawableImage::setOpacity (const float newOpacity)
{
	opacity = newOpacity;
}

void DrawableImage::setOverlayColour (const Colour& newOverlayColour)
{
	overlayColour = newOverlayColour;
}

void DrawableImage::render (const Drawable::RenderingContext& context) const
{
	if (image != 0)
	{
		if (opacity > 0.0f && ! overlayColour.isOpaque())
		{
			context.g.setOpacity (context.opacity * opacity);
			context.g.drawImageTransformed (image, image->getBounds(),
											context.transform, false);
		}

		if (! overlayColour.isTransparent())
		{
			context.g.setColour (overlayColour.withMultipliedAlpha (context.opacity));
			context.g.drawImageTransformed (image, image->getBounds(),
											context.transform, true);
		}
	}
}

const Rectangle<float> DrawableImage::getBounds() const
{
	if (image == 0)
		return Rectangle<float>();

	return Rectangle<float> (0, 0, (float) image->getWidth(), (float) image->getHeight());
}

bool DrawableImage::hitTest (float x, float y) const
{
	return image != 0
			&& x >= 0.0f
			&& y >= 0.0f
			&& x < image->getWidth()
			&& y < image->getHeight()
			&& image->getPixelAt (roundToInt (x), roundToInt (y)).getAlpha() >= 127;
}

Drawable* DrawableImage::createCopy() const
{
	DrawableImage* const di = new DrawableImage();

	di->opacity = opacity;
	di->overlayColour = overlayColour;

	if (image != 0)
	{
		if ((! canDeleteImage) || ! ImageCache::isImageInCache (image))
		{
			di->setImage (*image);
		}
		else
		{
			ImageCache::incReferenceCount (image);
			di->setImage (image, true);
		}
	}

	return di;
}

ValueTree DrawableImage::createValueTree() const throw()
{
	ValueTree v ("Image");

	if (getName().isNotEmpty())
		v.setProperty ("id", getName(), 0);

	if (opacity < 1.0f)
		v.setProperty ("opacity", (double) opacity, 0);

	if (! overlayColour.isTransparent())
		v.setProperty ("overlay", String::toHexString ((int) overlayColour.getARGB()), 0);

	if (image != 0)
	{
		MemoryOutputStream imageData;
		PNGImageFormat pngFormat;
		if (pngFormat.writeImageToStream (*image, imageData))
		{
			String base64 (MemoryBlock (imageData.getData(), imageData.getDataSize()).toBase64Encoding());

			for (int i = (base64.length() & ~127); i >= 0; i -= 128)
				base64 = base64.substring (0, i) + "\n" + base64.substring (i);

			v.setProperty ("data", base64, 0);
		}
	}

	return v;
}

DrawableImage* DrawableImage::createFromValueTree (const ValueTree& tree) throw()
{
	if (! tree.hasType ("Image"))
		return 0;

	DrawableImage* di = new DrawableImage();

	di->setName (tree ["id"]);
	di->opacity = tree.hasProperty ("opacity") ? (float) tree ["opacity"] : 1.0f;
	di->overlayColour = Colour (tree ["overlay"].toString().getHexValue32());

	MemoryBlock imageData;
	if (imageData.fromBase64Encoding (tree ["data"]))
	{
		Image* const im = ImageFileFormat::loadFrom (imageData.getData(), (int) imageData.getSize());
		if (im == 0)
			return false;

		di->setImage (im, true);
	}

	return di;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DrawableImage.cpp ***/


/*** Start of inlined file: juce_DrawablePath.cpp ***/
BEGIN_JUCE_NAMESPACE

DrawablePath::DrawablePath()
	: mainFill (Colours::black),
	  strokeFill (Colours::transparentBlack),
	  strokeType (0.0f)
{
}

DrawablePath::~DrawablePath()
{
}

void DrawablePath::setPath (const Path& newPath) throw()
{
	path = newPath;
	updateOutline();
}

void DrawablePath::setFill (const FillType& newFill) throw()
{
	mainFill = newFill;
}

void DrawablePath::setStrokeFill (const FillType& newFill) throw()
{
	strokeFill = newFill;
}

void DrawablePath::setStrokeType (const PathStrokeType& newStrokeType) throw()
{
	strokeType = newStrokeType;
	updateOutline();
}

void DrawablePath::setStrokeThickness (const float newThickness) throw()
{
	setStrokeType (PathStrokeType (newThickness, strokeType.getJointStyle(), strokeType.getEndStyle()));
}

void DrawablePath::render (const Drawable::RenderingContext& context) const
{
	{
		FillType f (mainFill);
		if (f.isGradient())
			f.gradient->multiplyOpacity (context.opacity);

		f.transform = f.transform.followedBy (context.transform);
		context.g.setFillType (f);
		context.g.fillPath (path, context.transform);
	}

	if (strokeType.getStrokeThickness() > 0.0f)
	{
		FillType f (strokeFill);
		if (f.isGradient())
			f.gradient->multiplyOpacity (context.opacity);

		f.transform = f.transform.followedBy (context.transform);
		context.g.setFillType (f);
		context.g.fillPath (stroke, context.transform);
	}
}

void DrawablePath::updateOutline()
{
	stroke.clear();
	strokeType.createStrokedPath (stroke, path, AffineTransform::identity, 4.0f);
}

const Rectangle<float> DrawablePath::getBounds() const
{
	if (strokeType.getStrokeThickness() > 0.0f)
		return stroke.getBounds();
	else
		return path.getBounds();
}

bool DrawablePath::hitTest (float x, float y) const
{
	return path.contains (x, y)
			|| stroke.contains (x, y);
}

Drawable* DrawablePath::createCopy() const
{
	DrawablePath* const dp = new DrawablePath();

	dp->path = path;
	dp->stroke = stroke;
	dp->mainFill = mainFill;
	dp->strokeFill = strokeFill;
	dp->strokeType = strokeType;
	return dp;
}

static const FillType readFillTypeFromTree (const ValueTree& v)
{
	const String type (v["type"].toString());

	if (type.equalsIgnoreCase ("solid"))
	{
		const String colour (v ["colour"].toString());
		return Colour (colour.isEmpty() ? (uint32) 0xff000000
										: (uint32) colour.getHexValue32());
	}
	else if (type.equalsIgnoreCase ("gradient"))
	{
		ColourGradient g;
		g.x1 = v["x1"];
		g.y1 = v["y1"];
		g.x2 = v["x2"];
		g.y2 = v["y2"];
		g.isRadial = v["radial"];

		StringArray colours;
		colours.addTokens (v["colours"].toString(), false);

		for (int i = 0; i < colours.size() / 2; ++i)
			g.addColour (colours[i * 2].getDoubleValue(),
						 Colour ((uint32)  colours[i * 2 + 1].getHexValue32()));

		return g;
	}

	jassertfalse
	return FillType();
}

static ValueTree createTreeForFillType (const String& tagName, const FillType& fillType)
{
	ValueTree v (tagName);

	if (fillType.isColour())
	{
		v.setProperty ("type", "solid", 0);
		v.setProperty ("colour", String::toHexString ((int) fillType.colour.getARGB()), 0);
	}
	else if (fillType.isGradient())
	{
		v.setProperty ("type", "gradient", 0);
		v.setProperty ("x1", fillType.gradient->x1, 0);
		v.setProperty ("y1", fillType.gradient->y1, 0);
		v.setProperty ("x2", fillType.gradient->x2, 0);
		v.setProperty ("y2", fillType.gradient->y2, 0);
		v.setProperty ("radial", fillType.gradient->isRadial, 0);

		String s;
		for (int i = 0; i < fillType.gradient->getNumColours(); ++i)
			s << " " << fillType.gradient->getColourPosition (i)
			  << " " << String::toHexString ((int) fillType.gradient->getColour(i).getARGB());

		v.setProperty ("colours", s.trimStart(), 0);
	}
	else
	{
		jassertfalse //xxx
	}

	return v;
}

ValueTree DrawablePath::createValueTree() const throw()
{
	ValueTree v ("Path");

	v.addChild (createTreeForFillType ("fill", mainFill), -1, 0);
	v.addChild (createTreeForFillType ("stroke", strokeFill), -1, 0);

	if (getName().isNotEmpty())
		v.setProperty ("id", getName(), 0);

	v.setProperty ("strokeWidth", (double) strokeType.getStrokeThickness(), 0);
	v.setProperty ("jointStyle", strokeType.getJointStyle() == PathStrokeType::mitered
									? "miter" : (strokeType.getJointStyle() == PathStrokeType::curved ? "curved" : "bevel"), 0);
	v.setProperty ("capStyle", strokeType.getEndStyle() == PathStrokeType::butt
									? "butt" : (strokeType.getEndStyle() == PathStrokeType::square ? "square" : "round"), 0);
	v.setProperty ("path", path.toString(), 0);

	return v;
}

DrawablePath* DrawablePath::createFromValueTree (const ValueTree& tree) throw()
{
	if (! tree.hasType ("Path"))
		return 0;

	DrawablePath* p = new DrawablePath();

	p->setName (tree ["id"]);
	p->mainFill = readFillTypeFromTree (tree.getChildWithName ("fill"));
	p->strokeFill = readFillTypeFromTree (tree.getChildWithName ("stroke"));

	const String jointStyle (tree ["jointStyle"].toString());
	const String endStyle (tree ["capStyle"].toString());

	p->strokeType
		= PathStrokeType (tree ["strokeWidth"],
						  jointStyle.equalsIgnoreCase ("curved") ? PathStrokeType::curved
																 : (jointStyle.equalsIgnoreCase ("bevel") ? PathStrokeType::beveled
																										  : PathStrokeType::mitered),
						  endStyle.equalsIgnoreCase ("square") ? PathStrokeType::square
															   : (endStyle.equalsIgnoreCase ("round") ? PathStrokeType::rounded
																									  : PathStrokeType::butt));

	p->path.clear();
	p->path.restoreFromString (tree ["path"]);
	p->updateOutline();

	return p;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DrawablePath.cpp ***/


/*** Start of inlined file: juce_DrawableText.cpp ***/
BEGIN_JUCE_NAMESPACE

DrawableText::DrawableText()
	: colour (Colours::white)
{
}

DrawableText::~DrawableText()
{
}

void DrawableText::setText (const GlyphArrangement& newText)
{
	text = newText;
}

void DrawableText::setText (const String& newText, const Font& fontToUse)
{
	text.clear();
	text.addLineOfText (fontToUse, newText, 0.0f, 0.0f);
}

void DrawableText::setColour (const Colour& newColour)
{
	colour = newColour;
}

void DrawableText::render (const Drawable::RenderingContext& context) const
{
	context.g.setColour (colour.withMultipliedAlpha (context.opacity));
	text.draw (context.g, context.transform);
}

const Rectangle<float> DrawableText::getBounds() const
{
	return text.getBoundingBox (0, -1, false);
}

bool DrawableText::hitTest (float x, float y) const
{
	return text.findGlyphIndexAt (x, y) >= 0;
}

Drawable* DrawableText::createCopy() const
{
	DrawableText* const dt = new DrawableText();

	dt->text = text;
	dt->colour = colour;

	return dt;
}

ValueTree DrawableText::createValueTree() const throw()
{
	ValueTree v ("Text");

	if (getName().isNotEmpty())
		v.setProperty ("id", getName(), 0);

	jassertfalse // xxx not finished!
	return v;
}

DrawableText* DrawableText::createFromValueTree (const ValueTree& tree) throw()
{
	if (! tree.hasType ("Text"))
		return 0;

	DrawableText* dt = new DrawableText();

	dt->setName (tree ["id"]);

	jassertfalse // xxx not finished!

	return dt;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DrawableText.cpp ***/


/*** Start of inlined file: juce_SVGParser.cpp ***/
BEGIN_JUCE_NAMESPACE

class SVGState
{
public:

	SVGState (const XmlElement* const topLevel)
		: topLevelXml (topLevel),
		  elementX (0), elementY (0),
		  width (512), height (512),
		  viewBoxW (0), viewBoxH (0)
	{
	}

	~SVGState()
	{
	}

	Drawable* parseSVGElement (const XmlElement& xml)
	{
		if (! xml.hasTagName ("svg"))
			return 0;

		DrawableComposite* const drawable = new DrawableComposite();

		drawable->setName (xml.getStringAttribute ("id"));

		SVGState newState (*this);

		if (xml.hasAttribute ("transform"))
			newState.addTransform (xml);

		newState.elementX = getCoordLength (xml.getStringAttribute ("x", String (newState.elementX)), viewBoxW);
		newState.elementY = getCoordLength (xml.getStringAttribute ("y", String (newState.elementY)), viewBoxH);
		newState.width = getCoordLength (xml.getStringAttribute ("width", String (newState.width)), viewBoxW);
		newState.height = getCoordLength (xml.getStringAttribute ("height", String (newState.height)), viewBoxH);

		if (xml.hasAttribute ("viewBox"))
		{
			const String viewParams (xml.getStringAttribute ("viewBox"));
			int i = 0;
			float vx, vy, vw, vh;

			if (parseCoords (viewParams, vx, vy, i, true)
				 && parseCoords (viewParams, vw, vh, i, true)
				 && vw > 0
				 && vh > 0)
			{
				newState.viewBoxW = vw;
				newState.viewBoxH = vh;

				int placementFlags = 0;

				const String aspect (xml.getStringAttribute ("preserveAspectRatio"));

				if (aspect.containsIgnoreCase ("none"))
				{
					placementFlags = RectanglePlacement::stretchToFit;
				}
				else
				{
					if (aspect.containsIgnoreCase ("slice"))
						placementFlags |= RectanglePlacement::fillDestination;

					if (aspect.containsIgnoreCase ("xMin"))
						placementFlags |= RectanglePlacement::xLeft;
					else if (aspect.containsIgnoreCase ("xMax"))
						placementFlags |= RectanglePlacement::xRight;
					else
						placementFlags |= RectanglePlacement::xMid;

					if (aspect.containsIgnoreCase ("yMin"))
						placementFlags |= RectanglePlacement::yTop;
					else if (aspect.containsIgnoreCase ("yMax"))
						placementFlags |= RectanglePlacement::yBottom;
					else
						placementFlags |= RectanglePlacement::yMid;
				}

				const RectanglePlacement placement (placementFlags);

				newState.transform
					= placement.getTransformToFit (vx, vy, vw, vh,
												   0.0f, 0.0f, newState.width, newState.height)
							   .followedBy (newState.transform);
			}
		}
		else
		{
			if (viewBoxW == 0)
				newState.viewBoxW = newState.width;

			if (viewBoxH == 0)
				newState.viewBoxH = newState.height;
		}

		newState.parseSubElements (xml, drawable);

		return drawable;
	}

private:

	const XmlElement* const topLevelXml;
	float elementX, elementY, width, height, viewBoxW, viewBoxH;
	AffineTransform transform;
	String cssStyleText;

	void parseSubElements (const XmlElement& xml, DrawableComposite* const parentDrawable)
	{
		forEachXmlChildElement (xml, e)
		{
			Drawable* d = 0;

			if (e->hasTagName ("g"))
				d = parseGroupElement (*e);
			else if (e->hasTagName ("svg"))
				d = parseSVGElement (*e);
			else if (e->hasTagName ("path"))
				d = parsePath (*e);
			else if (e->hasTagName ("rect"))
				d = parseRect (*e);
			else if (e->hasTagName ("circle"))
				d = parseCircle (*e);
			else if (e->hasTagName ("ellipse"))
				d = parseEllipse (*e);
			else if (e->hasTagName ("line"))
				d = parseLine (*e);
			else if (e->hasTagName ("polyline"))
				d = parsePolygon (*e, true);
			else if (e->hasTagName ("polygon"))
				d = parsePolygon (*e, false);
			else if (e->hasTagName ("text"))
				d = parseText (*e);
			else if (e->hasTagName ("switch"))
				d = parseSwitch (*e);
			else if (e->hasTagName ("style"))
				parseCSSStyle (*e);

			parentDrawable->insertDrawable (d);
		}
	}

	DrawableComposite* parseSwitch (const XmlElement& xml)
	{
		const XmlElement* const group = xml.getChildByName ("g");

		if (group != 0)
			return parseGroupElement (*group);

		return 0;
	}

	DrawableComposite* parseGroupElement (const XmlElement& xml)
	{
		DrawableComposite* const drawable = new DrawableComposite();

		drawable->setName (xml.getStringAttribute ("id"));

		if (xml.hasAttribute ("transform"))
		{
			SVGState newState (*this);
			newState.addTransform (xml);

			newState.parseSubElements (xml, drawable);
		}
		else
		{
			parseSubElements (xml, drawable);
		}

		return drawable;
	}

	Drawable* parsePath (const XmlElement& xml) const
	{
		const String d (xml.getStringAttribute ("d").trimStart());
		Path path;

		if (getStyleAttribute (&xml, "fill-rule").trim().equalsIgnoreCase ("evenodd"))
			path.setUsingNonZeroWinding (false);

		int index = 0;
		float lastX = 0, lastY = 0;
		float lastX2 = 0, lastY2 = 0;
		juce_wchar lastCommandChar = 0;
		bool isRelative = true;
		bool carryOn = true;

		const String validCommandChars ("MmLlHhVvCcSsQqTtAaZz");

		while (d[index] != 0)
		{
			float x, y, x2, y2, x3, y3;

			if (validCommandChars.containsChar (d[index]))
			{
				lastCommandChar = d [index++];
				isRelative = (lastCommandChar >= 'a' && lastCommandChar <= 'z');
			}

			switch (lastCommandChar)
			{
			case 'M':
			case 'm':
			case 'L':
			case 'l':
				if (parseCoords (d, x, y, index, false))
				{
					if (isRelative)
					{
						x += lastX;
						y += lastY;
					}

					if (lastCommandChar == 'M' || lastCommandChar == 'm')
					{
						path.startNewSubPath (x, y);
						lastCommandChar = 'l';
					}
					else
						path.lineTo (x, y);

					lastX2 = lastX;
					lastY2 = lastY;
					lastX = x;
					lastY = y;
				}
				else
				{
					++index;
				}

				break;

			case 'H':
			case 'h':
				if (parseCoord (d, x, index, false, true))
				{
					if (isRelative)
						x += lastX;

					path.lineTo (x, lastY);

					lastX2 = lastX;
					lastX = x;
				}
				else
				{
					++index;
				}
				break;

			case 'V':
			case 'v':
				if (parseCoord (d, y, index, false, false))
				{
					if (isRelative)
						y += lastY;

					path.lineTo (lastX, y);

					lastY2 = lastY;
					lastY = y;
				}
				else
				{
					++index;
				}
				break;

			case 'C':
			case 'c':
				if (parseCoords (d, x, y, index, false)
					 && parseCoords (d, x2, y2, index, false)
					 && parseCoords (d, x3, y3, index, false))
				{
					if (isRelative)
					{
						x += lastX;
						y += lastY;
						x2 += lastX;
						y2 += lastY;
						x3 += lastX;
						y3 += lastY;
					}

					path.cubicTo (x, y, x2, y2, x3, y3);

					lastX2 = x2;
					lastY2 = y2;
					lastX = x3;
					lastY = y3;
				}
				else
				{
					++index;
				}
				break;

			case 'S':
			case 's':
				if (parseCoords (d, x, y, index, false)
					 && parseCoords (d, x3, y3, index, false))
				{
					if (isRelative)
					{
						x += lastX;
						y += lastY;
						x3 += lastX;
						y3 += lastY;
					}

					x2 = lastX + (lastX - lastX2);
					y2 = lastY + (lastY - lastY2);
					path.cubicTo (x2, y2, x, y, x3, y3);

					lastX2 = x;
					lastY2 = y;
					lastX = x3;
					lastY = y3;
				}
				else
				{
					++index;
				}
				break;

			case 'Q':
			case 'q':
				if (parseCoords (d, x, y, index, false)
					 && parseCoords (d, x2, y2, index, false))
				{
					if (isRelative)
					{
						x += lastX;
						y += lastY;
						x2 += lastX;
						y2 += lastY;
					}

					path.quadraticTo (x, y, x2, y2);

					lastX2 = x;
					lastY2 = y;
					lastX = x2;
					lastY = y2;
				}
				else
				{
					++index;
				}
				break;

			case 'T':
			case 't':
				if (parseCoords (d, x, y, index, false))
				{
					if (isRelative)
					{
						x += lastX;
						y += lastY;
					}

					x2 = lastX + (lastX - lastX2);
					y2 = lastY + (lastY - lastY2);
					path.quadraticTo (x2, y2, x, y);

					lastX2 = x2;
					lastY2 = y2;
					lastX = x;
					lastY = y;
				}
				else
				{
					++index;
				}
				break;

			case 'A':
			case 'a':
				if (parseCoords (d, x, y, index, false))
				{
					String num;

					if (parseNextNumber (d, num, index, false))
					{
						const float angle = num.getFloatValue() * (180.0f / float_Pi);

						if (parseNextNumber (d, num, index, false))
						{
							const bool largeArc = num.getIntValue() != 0;

							if (parseNextNumber (d, num, index, false))
							{
								const bool sweep = num.getIntValue() != 0;

								if (parseCoords (d, x2, y2, index, false))
								{
									if (isRelative)
									{
										x2 += lastX;
										y2 += lastY;
									}

									if (lastX != x2 || lastY != y2)
									{
										double centreX, centreY, startAngle, deltaAngle;
										double rx = x, ry = y;

										endpointToCentreParameters (lastX, lastY, x2, y2,
																	angle, largeArc, sweep,
																	rx, ry, centreX, centreY,
																	startAngle, deltaAngle);

										path.addCentredArc ((float) centreX, (float) centreY,
															(float) rx, (float) ry,
															angle, (float) startAngle, (float) (startAngle + deltaAngle),
															false);

										path.lineTo (x2, y2);
									}

									lastX2 = lastX;
									lastY2 = lastY;
									lastX = x2;
									lastY = y2;
								}
							}
						}
					}
				}
				else
				{
					++index;
				}

				break;

			case 'Z':
			case 'z':
				path.closeSubPath();
				while (CharacterFunctions::isWhitespace (d [index]))
					++index;

				break;

			default:
				carryOn = false;
				break;
			}

			if (! carryOn)
				break;
		}

		return parseShape (xml, path);
	}

	Drawable* parseRect (const XmlElement& xml) const
	{
		Path rect;

		const bool hasRX = xml.hasAttribute ("rx");
		const bool hasRY = xml.hasAttribute ("ry");

		if (hasRX || hasRY)
		{
			float rx = getCoordLength (xml.getStringAttribute ("rx"), viewBoxW);
			float ry = getCoordLength (xml.getStringAttribute ("ry"), viewBoxH);

			if (! hasRX)
				rx = ry;
			else if (! hasRY)
				ry = rx;

			rect.addRoundedRectangle (getCoordLength (xml.getStringAttribute ("x"), viewBoxW),
									  getCoordLength (xml.getStringAttribute ("y"), viewBoxH),
									  getCoordLength (xml.getStringAttribute ("width"), viewBoxW),
									  getCoordLength (xml.getStringAttribute ("height"), viewBoxH),
									  rx, ry);
		}
		else
		{
			rect.addRectangle (getCoordLength (xml.getStringAttribute ("x"), viewBoxW),
							   getCoordLength (xml.getStringAttribute ("y"), viewBoxH),
							   getCoordLength (xml.getStringAttribute ("width"), viewBoxW),
							   getCoordLength (xml.getStringAttribute ("height"), viewBoxH));
		}

		return parseShape (xml, rect);
	}

	Drawable* parseCircle (const XmlElement& xml) const
	{
		Path circle;

		const float cx = getCoordLength (xml.getStringAttribute ("cx"), viewBoxW);
		const float cy = getCoordLength (xml.getStringAttribute ("cy"), viewBoxH);
		const float radius = getCoordLength (xml.getStringAttribute ("r"), viewBoxW);

		circle.addEllipse (cx - radius, cy - radius, radius * 2.0f, radius * 2.0f);

		return parseShape (xml, circle);
	}

	Drawable* parseEllipse (const XmlElement& xml) const
	{
		Path ellipse;

		const float cx = getCoordLength (xml.getStringAttribute ("cx"), viewBoxW);
		const float cy = getCoordLength (xml.getStringAttribute ("cy"), viewBoxH);
		const float radiusX = getCoordLength (xml.getStringAttribute ("rx"), viewBoxW);
		const float radiusY = getCoordLength (xml.getStringAttribute ("ry"), viewBoxH);

		ellipse.addEllipse (cx - radiusX, cy - radiusY, radiusX * 2.0f, radiusY * 2.0f);

		return parseShape (xml, ellipse);
	}

	Drawable* parseLine (const XmlElement& xml) const
	{
		Path line;

		const float x1 = getCoordLength (xml.getStringAttribute ("x1"), viewBoxW);
		const float y1 = getCoordLength (xml.getStringAttribute ("y1"), viewBoxH);
		const float x2 = getCoordLength (xml.getStringAttribute ("x2"), viewBoxW);
		const float y2 = getCoordLength (xml.getStringAttribute ("y2"), viewBoxH);

		line.startNewSubPath (x1, y1);
		line.lineTo (x2, y2);

		return parseShape (xml, line);
	}

	Drawable* parsePolygon (const XmlElement& xml, const bool isPolyline) const
	{
		const String points (xml.getStringAttribute ("points"));
		Path path;

		int index = 0;
		float x, y;

		if (parseCoords (points, x, y, index, true))
		{
			float firstX = x;
			float firstY = y;
			float lastX = 0, lastY = 0;

			path.startNewSubPath (x, y);

			while (parseCoords (points, x, y, index, true))
			{
				lastX = x;
				lastY = y;
				path.lineTo (x, y);
			}

			if ((! isPolyline) || (firstX == lastX && firstY == lastY))
				path.closeSubPath();
		}

		return parseShape (xml, path);
	}

	Drawable* parseShape (const XmlElement& xml, Path& path,
						  const bool shouldParseTransform = true) const
	{
		if (shouldParseTransform && xml.hasAttribute ("transform"))
		{
			SVGState newState (*this);
			newState.addTransform (xml);

			return newState.parseShape (xml, path, false);
		}

		DrawablePath* dp = new DrawablePath();
		dp->setName (xml.getStringAttribute ("id"));
		dp->setFill (Colours::transparentBlack);

		path.applyTransform (transform);
		dp->setPath (path);

		Path::Iterator iter (path);

		bool containsClosedSubPath = false;
		while (iter.next())
		{
			if (iter.elementType == Path::Iterator::closePath)
			{
				containsClosedSubPath = true;
				break;
			}
		}

		dp->setFill (getPathFillType (path,
									  getStyleAttribute (&xml, "fill"),
									  getStyleAttribute (&xml, "fill-opacity"),
									  getStyleAttribute (&xml, "opacity"),
									  containsClosedSubPath ? Colours::black
															: Colours::transparentBlack));

		const String strokeType (getStyleAttribute (&xml, "stroke"));

		if (strokeType.isNotEmpty() && ! strokeType.equalsIgnoreCase ("none"))
		{
			dp->setStrokeFill (getPathFillType (path, strokeType,
												getStyleAttribute (&xml, "stroke-opacity"),
												getStyleAttribute (&xml, "opacity"),
												Colours::transparentBlack));

			dp->setStrokeType (getStrokeFor (&xml));
		}

		return dp;
	}

	const XmlElement* findLinkedElement (const XmlElement* e) const
	{
		const String id (e->getStringAttribute ("xlink:href"));

		if (! id.startsWithChar ('#'))
			return 0;

		return findElementForId (topLevelXml, id.substring (1));
	}

	void addGradientStopsIn (ColourGradient& cg, const XmlElement* const fillXml) const
	{
		if (fillXml == 0)
			return;

		forEachXmlChildElementWithTagName (*fillXml, e, "stop")
		{
			int index = 0;
			Colour col (parseColour (getStyleAttribute  (e, "stop-color"), index, Colours::black));

			const String opacity (getStyleAttribute (e, "stop-opacity", "1"));
			col = col.withMultipliedAlpha (jlimit (0.0f, 1.0f, opacity.getFloatValue()));

			double offset = e->getDoubleAttribute ("offset");

			if (e->getStringAttribute ("offset").containsChar ('%'))
				offset *= 0.01;

			cg.addColour (jlimit (0.0, 1.0, offset), col);
		}
	}

	const FillType getPathFillType (const Path& path,
									const String& fill,
									const String& fillOpacity,
									const String& overallOpacity,
									const Colour& defaultColour) const
	{
		float opacity = 1.0f;

		if (overallOpacity.isNotEmpty())
			opacity = jlimit (0.0f, 1.0f, overallOpacity.getFloatValue());

		if (fillOpacity.isNotEmpty())
			opacity *= (jlimit (0.0f, 1.0f, fillOpacity.getFloatValue()));

		if (fill.startsWithIgnoreCase ("url"))
		{
			const String id (fill.fromFirstOccurrenceOf ("#", false, false)
								 .upToLastOccurrenceOf (")", false, false).trim());

			const XmlElement* const fillXml = findElementForId (topLevelXml, id);

			if (fillXml != 0
				 && (fillXml->hasTagName ("linearGradient")
					  || fillXml->hasTagName ("radialGradient")))
			{
				const XmlElement* inheritedFrom = findLinkedElement (fillXml);

				ColourGradient gradient;

				addGradientStopsIn (gradient, inheritedFrom);
				addGradientStopsIn (gradient, fillXml);

				if (gradient.getNumColours() > 0)
				{
					gradient.addColour (0.0, gradient.getColour (0));
					gradient.addColour (1.0, gradient.getColour (gradient.getNumColours() - 1));
				}
				else
				{
					gradient.addColour (0.0, Colours::black);
					gradient.addColour (1.0, Colours::black);
				}

				if (overallOpacity.isNotEmpty())
					gradient.multiplyOpacity (overallOpacity.getFloatValue());

				jassert (gradient.getNumColours() > 0);

				gradient.isRadial = fillXml->hasTagName ("radialGradient");

				float width = viewBoxW;
				float height = viewBoxH;
				float dx = 0.0f;
				float dy = 0.0f;

				const bool userSpace = fillXml->getStringAttribute ("gradientUnits").equalsIgnoreCase ("userSpaceOnUse");

				if (! userSpace)
				{
					const Rectangle<float> bounds (path.getBounds());
					dx = bounds.getX();
					dy = bounds.getY();
					width = bounds.getWidth();
					height = bounds.getHeight();
				}

				if (gradient.isRadial)
				{
					gradient.x1 = dx + getCoordLength (fillXml->getStringAttribute ("cx", "50%"), width);
					gradient.y1 = dy + getCoordLength (fillXml->getStringAttribute ("cy", "50%"), height);

					const float radius = getCoordLength (fillXml->getStringAttribute ("r", "50%"), width);

					gradient.x2 = gradient.x1 + radius;
					gradient.y2 = gradient.y1;

					//xxx (the fx, fy focal point isn't handled properly here..)
				}
				else
				{
					gradient.x1 = dx + getCoordLength (fillXml->getStringAttribute ("x1", "0%"), width);
					gradient.y1 = dy + getCoordLength (fillXml->getStringAttribute ("y1", "0%"), height);

					gradient.x2 = dx + getCoordLength (fillXml->getStringAttribute ("x2", "100%"), width);
					gradient.y2 = dy + getCoordLength (fillXml->getStringAttribute ("y2", "0%"), height);

					if (gradient.x1 == gradient.x2 && gradient.y1 == gradient.y2)
						return Colour (gradient.getColour (gradient.getNumColours() - 1));
				}

				FillType type (gradient);
				type.transform = parseTransform (fillXml->getStringAttribute ("gradientTransform"))
								   .followedBy (transform);
				return type;
			}
		}

		if (fill.equalsIgnoreCase ("none"))
			return Colours::transparentBlack;

		int i = 0;
		const Colour colour (parseColour (fill, i, defaultColour));
		return colour.withMultipliedAlpha (opacity);
	}

	const PathStrokeType getStrokeFor (const XmlElement* const xml) const
	{
		const String width (getStyleAttribute (xml, "stroke-width"));
		const String cap (getStyleAttribute (xml, "stroke-linecap"));
		const String join (getStyleAttribute (xml, "stroke-linejoin"));

		//const String mitreLimit (getStyleAttribute (xml, "stroke-miterlimit"));
		//const String dashArray (getStyleAttribute (xml, "stroke-dasharray"));
		//const String dashOffset (getStyleAttribute (xml, "stroke-dashoffset"));

		PathStrokeType::JointStyle joinStyle = PathStrokeType::mitered;
		PathStrokeType::EndCapStyle capStyle = PathStrokeType::butt;

		if (join.equalsIgnoreCase ("round"))
			joinStyle = PathStrokeType::curved;
		else if (join.equalsIgnoreCase ("bevel"))
			joinStyle = PathStrokeType::beveled;

		if (cap.equalsIgnoreCase ("round"))
			capStyle = PathStrokeType::rounded;
		else if (cap.equalsIgnoreCase ("square"))
			capStyle = PathStrokeType::square;

		float ox = 0.0f, oy = 0.0f;
		transform.transformPoint (ox, oy);
		float x = getCoordLength (width, viewBoxW), y = 0.0f;
		transform.transformPoint (x, y);

		return PathStrokeType (width.isNotEmpty() ? juce_hypotf (x - ox, y - oy) : 1.0f,
							   joinStyle, capStyle);
	}

	Drawable* parseText (const XmlElement& xml)
	{
		Array <float> xCoords, yCoords, dxCoords, dyCoords;

		getCoordList (xCoords, getInheritedAttribute (&xml, "x"), true, true);
		getCoordList (yCoords, getInheritedAttribute (&xml, "y"), true, false);
		getCoordList (dxCoords, getInheritedAttribute (&xml, "dx"), true, true);
		getCoordList (dyCoords, getInheritedAttribute (&xml, "dy"), true, false);

		//xxx not done text yet!

		forEachXmlChildElement (xml, e)
		{
			if (e->isTextElement())
			{
				const String text (e->getText());

				Path path;
				Drawable* s = parseShape (*e, path);
				delete s;
			}
			else if (e->hasTagName ("tspan"))
			{
				Drawable* s = parseText (*e);
				delete s;
			}
		}

		return 0;
	}

	void addTransform (const XmlElement& xml)
	{
		transform = parseTransform (xml.getStringAttribute ("transform"))
						.followedBy (transform);
	}

	bool parseCoord (const String& s, float& value, int& index,
					 const bool allowUnits, const bool isX) const
	{
		String number;

		if (! parseNextNumber (s, number, index, allowUnits))
		{
			value = 0;
			return false;
		}

		value = getCoordLength (number, isX ? viewBoxW : viewBoxH);
		return true;
	}

	bool parseCoords (const String& s, float& x, float& y,
					  int& index, const bool allowUnits) const
	{
		return parseCoord (s, x, index, allowUnits, true)
			&& parseCoord (s, y, index, allowUnits, false);
	}

	float getCoordLength (const String& s, const float sizeForProportions) const
	{
		float n = s.getFloatValue();
		const int len = s.length();

		if (len > 2)
		{
			const float dpi = 96.0f;

			const juce_wchar n1 = s [len - 2];
			const juce_wchar n2 = s [len - 1];

			if (n1 == 'i' && n2 == 'n')
				n *= dpi;
			else if (n1 == 'm' && n2 == 'm')
				n *= dpi / 25.4f;
			else if (n1 == 'c' && n2 == 'm')
				n *= dpi / 2.54f;
			else if (n1 == 'p' && n2 == 'c')
				n *= 15.0f;
			else if (n2 == '%')
				n *= 0.01f * sizeForProportions;
		}

		return n;
	}

	void getCoordList (Array <float>& coords, const String& list,
					   const bool allowUnits, const bool isX) const
	{
		int index = 0;
		float value;

		while (parseCoord (list, value, index, allowUnits, isX))
			coords.add (value);
	}

	void parseCSSStyle (const XmlElement& xml)
	{
		cssStyleText = xml.getAllSubText() + "\n" + cssStyleText;
	}

	const String getStyleAttribute (const XmlElement* xml, const String& attributeName,
									const String& defaultValue = String::empty) const
	{
		if (xml->hasAttribute (attributeName))
			return xml->getStringAttribute (attributeName, defaultValue);

		const String styleAtt (xml->getStringAttribute ("style"));

		if (styleAtt.isNotEmpty())
		{
			const String value (getAttributeFromStyleList (styleAtt, attributeName, String::empty));

			if (value.isNotEmpty())
				return value;
		}
		else if (xml->hasAttribute ("class"))
		{
			const String className ("." + xml->getStringAttribute ("class"));

			int index = cssStyleText.indexOfIgnoreCase (className + " ");

			if (index < 0)
				index = cssStyleText.indexOfIgnoreCase (className + "{");

			if (index >= 0)
			{
				const int openBracket = cssStyleText.indexOfChar (index, '{');

				if (openBracket > index)
				{
					const int closeBracket = cssStyleText.indexOfChar (openBracket, '}');

					if (closeBracket > openBracket)
					{
						const String value (getAttributeFromStyleList (cssStyleText.substring (openBracket + 1, closeBracket), attributeName, defaultValue));

						if (value.isNotEmpty())
							return value;
					}
				}
			}
		}

		xml = const_cast <XmlElement*> (topLevelXml)->findParentElementOf (xml);

		if (xml != 0)
			return getStyleAttribute (xml, attributeName, defaultValue);

		return defaultValue;
	}

	const String getInheritedAttribute (const XmlElement* xml, const String& attributeName) const
	{
		if (xml->hasAttribute (attributeName))
			return xml->getStringAttribute (attributeName);

		xml = const_cast <XmlElement*> (topLevelXml)->findParentElementOf (xml);

		if (xml != 0)
			return getInheritedAttribute  (xml, attributeName);

		return String::empty;
	}

	static bool isIdentifierChar (const juce_wchar c)
	{
		return CharacterFunctions::isLetter (c) || c == '-';
	}

	static const String getAttributeFromStyleList (const String& list, const String& attributeName, const String& defaultValue)
	{
		int i = 0;

		for (;;)
		{
			i = list.indexOf (i, attributeName);

			if (i < 0)
				break;

			if ((i == 0 || (i > 0 && ! isIdentifierChar (list [i - 1])))
				 && ! isIdentifierChar (list [i + attributeName.length()]))
			{
				i = list.indexOfChar (i, ':');

				if (i < 0)
					break;

				int end = list.indexOfChar (i, ';');

				if (end < 0)
					end = 0x7ffff;

				return list.substring (i + 1, end).trim();
			}

			++i;
		}

		return defaultValue;
	}

	static bool parseNextNumber (const String& source, String& value, int& index, const bool allowUnits)
	{
		const juce_wchar* const s = source;

		while (CharacterFunctions::isWhitespace (s[index]) || s[index] == ',')
			++index;

		int start = index;

		if (CharacterFunctions::isDigit (s[index]) || s[index] == '.' || s[index] == '-')
			++index;

		while (CharacterFunctions::isDigit (s[index]) || s[index] == '.')
			++index;

		if ((s[index] == 'e' || s[index] == 'E')
			 && (CharacterFunctions::isDigit (s[index + 1])
				  || s[index + 1] == '-'
				  || s[index + 1] == '+'))
		{
			index += 2;

			while (CharacterFunctions::isDigit (s[index]))
				++index;
		}

		if (allowUnits)
		{
			while (CharacterFunctions::isLetter (s[index]))
				++index;
		}

		if (index == start)
			return false;

		value = String (s + start, index - start);

		while (CharacterFunctions::isWhitespace (s[index]) || s[index] == ',')
			++index;

		return true;
	}

	static const Colour parseColour (const String& s, int& index, const Colour& defaultColour)
	{
		if (s [index] == '#')
		{
			uint32 hex [6];
			zeromem (hex, sizeof (hex));
			int numChars = 0;

			for (int i = 6; --i >= 0;)
			{
				const int hexValue = CharacterFunctions::getHexDigitValue (s [++index]);

				if (hexValue >= 0)
					hex [numChars++] = hexValue;
				else
					break;
			}

			if (numChars <= 3)
				return Colour ((uint8) (hex [0] * 0x11),
							   (uint8) (hex [1] * 0x11),
							   (uint8) (hex [2] * 0x11));
			else
				return Colour ((uint8) ((hex [0] << 4) + hex [1]),
							   (uint8) ((hex [2] << 4) + hex [3]),
							   (uint8) ((hex [4] << 4) + hex [5]));
		}
		else if (s [index] == 'r'
				  && s [index + 1] == 'g'
				  && s [index + 2] == 'b')
		{
			const int openBracket = s.indexOfChar (index, '(');
			const int closeBracket = s.indexOfChar (openBracket, ')');

			if (openBracket >= 3 && closeBracket > openBracket)
			{
				index = closeBracket;

				StringArray tokens;
				tokens.addTokens (s.substring (openBracket + 1, closeBracket), ",", "");
				tokens.trim();
				tokens.removeEmptyStrings();

				if (tokens[0].containsChar ('%'))
					return Colour ((uint8) roundToInt (2.55 * tokens[0].getDoubleValue()),
								   (uint8) roundToInt (2.55 * tokens[1].getDoubleValue()),
								   (uint8) roundToInt (2.55 * tokens[2].getDoubleValue()));
				else
					return Colour ((uint8) tokens[0].getIntValue(),
								   (uint8) tokens[1].getIntValue(),
								   (uint8) tokens[2].getIntValue());
			}
		}

		return Colours::findColourForName (s, defaultColour);
	}

	static const AffineTransform parseTransform (String t)
	{
		AffineTransform result;

		while (t.isNotEmpty())
		{
			StringArray tokens;
			tokens.addTokens (t.fromFirstOccurrenceOf ("(", false, false)
							   .upToFirstOccurrenceOf (")", false, false),
							  ", ", String::empty);

			tokens.removeEmptyStrings (true);

			float numbers [6];

			for (int i = 0; i < 6; ++i)
				numbers[i] = tokens[i].getFloatValue();

			AffineTransform trans;

			if (t.startsWithIgnoreCase ("matrix"))
			{
				trans = AffineTransform (numbers[0], numbers[2], numbers[4],
										 numbers[1], numbers[3], numbers[5]);
			}
			else if (t.startsWithIgnoreCase ("translate"))
			{
				jassert (tokens.size() == 2);
				trans = AffineTransform::translation (numbers[0], numbers[1]);
			}
			else if (t.startsWithIgnoreCase ("scale"))
			{
				if (tokens.size() == 1)
					trans = AffineTransform::scale (numbers[0], numbers[0]);
				else
					trans = AffineTransform::scale (numbers[0], numbers[1]);
			}
			else if (t.startsWithIgnoreCase ("rotate"))
			{
				if (tokens.size() != 3)
					trans = AffineTransform::rotation (numbers[0] / (180.0f / float_Pi));
				else
					trans = AffineTransform::rotation (numbers[0] / (180.0f / float_Pi),
													   numbers[1], numbers[2]);
			}
			else if (t.startsWithIgnoreCase ("skewX"))
			{
				trans = AffineTransform (1.0f, tanf (numbers[0] * (float_Pi / 180.0f)), 0.0f,
										 0.0f, 1.0f, 0.0f);
			}
			else if (t.startsWithIgnoreCase ("skewY"))
			{
				trans = AffineTransform (1.0f, 0.0f, 0.0f,
										 tanf (numbers[0] * (float_Pi / 180.0f)), 1.0f, 0.0f);
			}

			result = trans.followedBy (result);
			t = t.fromFirstOccurrenceOf (")", false, false).trimStart();
		}

		return result;
	}

	static void endpointToCentreParameters (const double x1, const double y1,
											const double x2, const double y2,
											const double angle,
											const bool largeArc, const bool sweep,
											double& rx, double& ry,
											double& centreX, double& centreY,
											double& startAngle, double& deltaAngle)
	{
		const double midX = (x1 - x2) * 0.5;
		const double midY = (y1 - y2) * 0.5;

		const double cosAngle = cos (angle);
		const double sinAngle = sin (angle);
		const double xp = cosAngle * midX + sinAngle * midY;
		const double yp = cosAngle * midY - sinAngle * midX;
		const double xp2 = xp * xp;
		const double yp2 = yp * yp;

		double rx2 = rx * rx;
		double ry2 = ry * ry;

		const double s = (xp2 / rx2) + (yp2 / ry2);
		double c;

		if (s <= 1.0)
		{
			c = sqrt (jmax (0.0, ((rx2 * ry2) - (rx2 * yp2) - (ry2 * xp2))
								   / (( rx2 * yp2) + (ry2 * xp2))));

			if (largeArc == sweep)
				c = -c;
		}
		else
		{
			const double s2 = sqrt (s);
			rx *= s2;
			ry *= s2;
			rx2 = rx * rx;
			ry2 = ry * ry;
			c = 0;
		}

		const double cpx = ((rx * yp) / ry) * c;
		const double cpy = ((-ry * xp) / rx) * c;

		centreX = ((x1 + x2) * 0.5) + (cosAngle * cpx) - (sinAngle * cpy);
		centreY = ((y1 + y2) * 0.5) + (sinAngle * cpx) + (cosAngle * cpy);

		const double ux = (xp - cpx) / rx;
		const double uy = (yp - cpy) / ry;
		const double vx = (-xp - cpx) / rx;
		const double vy = (-yp - cpy) / ry;

		const double length = juce_hypot (ux, uy);

		startAngle = acos (jlimit (-1.0, 1.0, ux / length));

		if (uy < 0)
			startAngle = -startAngle;

		startAngle += double_Pi * 0.5;

		deltaAngle = acos (jlimit (-1.0, 1.0, ((ux * vx) + (uy * vy))
												/ (length * juce_hypot (vx, vy))));

		if ((ux * vy) - (uy * vx) < 0)
			deltaAngle = -deltaAngle;

		if (sweep)
		{
			if (deltaAngle < 0)
				deltaAngle += double_Pi * 2.0;
		}
		else
		{
			if (deltaAngle > 0)
				deltaAngle -= double_Pi * 2.0;
		}

		deltaAngle = fmod (deltaAngle, double_Pi * 2.0);
	}

	static const XmlElement* findElementForId (const XmlElement* const parent, const String& id)
	{
		forEachXmlChildElement (*parent, e)
		{
			if (e->compareAttribute ("id", id))
				return e;

			const XmlElement* const found = findElementForId (e, id);

			if (found != 0)
				return found;
		}

		return 0;
	}

	SVGState& operator= (const SVGState&);
};

Drawable* Drawable::createFromSVG (const XmlElement& svgDocument)
{
	SVGState state (&svgDocument);
	return state.parseSVGElement (svgDocument);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_SVGParser.cpp ***/


/*** Start of inlined file: juce_DropShadowEffect.cpp ***/
BEGIN_JUCE_NAMESPACE

#if JUCE_MSVC
  #pragma optimize ("t", on)  // try to avoid slowing everything down in debug builds
#endif

DropShadowEffect::DropShadowEffect()
  : offsetX (0),
	offsetY (0),
	radius (4),
	opacity (0.6f)
{
}

DropShadowEffect::~DropShadowEffect()
{
}

void DropShadowEffect::setShadowProperties (const float newRadius,
											const float newOpacity,
											const int newShadowOffsetX,
											const int newShadowOffsetY)
{
	radius = jmax (1.1f, newRadius);
	offsetX = newShadowOffsetX;
	offsetY = newShadowOffsetY;
	opacity = newOpacity;
}

void DropShadowEffect::applyEffect (Image& image, Graphics& g)
{
	const int w = image.getWidth();
	const int h = image.getHeight();

	Image shadowImage (Image::SingleChannel, w, h, false);

	const Image::BitmapData srcData (image, 0, 0, w, h);
	const Image::BitmapData destData (shadowImage, 0, 0, w, h, true);

	const int filter = roundToInt (63.0f / radius);
	const int radiusMinus1 = roundToInt ((radius - 1.0f) * 63.0f);

	for (int x = w; --x >= 0;)
	{
		int shadowAlpha = 0;

		const PixelARGB* src = ((const PixelARGB*) srcData.data) + x;
		uint8* shadowPix = destData.data + x;

		for (int y = h; --y >= 0;)
		{
			shadowAlpha = ((shadowAlpha * radiusMinus1 + (src->getAlpha() << 6)) * filter) >> 12;

			*shadowPix = (uint8) shadowAlpha;
			src = (const PixelARGB*) (((const uint8*) src) + srcData.lineStride);
			shadowPix += destData.lineStride;
		}
	}

	for (int y = h; --y >= 0;)
	{
		int shadowAlpha = 0;
		uint8* shadowPix = destData.getLinePointer (y);

		for (int x = w; --x >= 0;)
		{
			shadowAlpha = ((shadowAlpha * radiusMinus1 + (*shadowPix << 6)) * filter) >> 12;
			*shadowPix++ = (uint8) shadowAlpha;
		}
	}

	g.setColour (Colours::black.withAlpha (opacity));
	g.drawImageAt (&shadowImage, offsetX, offsetY, true);

	g.setOpacity (1.0f);
	g.drawImageAt (&image, 0, 0);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_DropShadowEffect.cpp ***/


/*** Start of inlined file: juce_GlowEffect.cpp ***/
BEGIN_JUCE_NAMESPACE

GlowEffect::GlowEffect()
  : radius (2.0f),
	colour (Colours::white)
{
}

GlowEffect::~GlowEffect()
{
}

void GlowEffect::setGlowProperties (const float newRadius,
									const Colour& newColour)
{
	radius = newRadius;
	colour = newColour;
}

void GlowEffect::applyEffect (Image& image, Graphics& g)
{
	Image temp (image.getFormat(), image.getWidth(), image.getHeight(), true);

	ImageConvolutionKernel blurKernel (roundToInt (radius * 2.0f));

	blurKernel.createGaussianBlur (radius);
	blurKernel.rescaleAllValues (radius);

	blurKernel.applyToImage (temp, &image, image.getBounds());

	g.setColour (colour);
	g.drawImageAt (&temp, 0, 0, true);

	g.setOpacity (1.0f);
	g.drawImageAt (&image, 0, 0, false);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_GlowEffect.cpp ***/


/*** Start of inlined file: juce_ReduceOpacityEffect.cpp ***/
BEGIN_JUCE_NAMESPACE

ReduceOpacityEffect::ReduceOpacityEffect (const float opacity_)
	: opacity (opacity_)
{
}

ReduceOpacityEffect::~ReduceOpacityEffect()
{
}

void ReduceOpacityEffect::setOpacity (const float newOpacity)
{
	opacity = jlimit (0.0f, 1.0f, newOpacity);
}

void ReduceOpacityEffect::applyEffect (Image& image, Graphics& g)
{
	g.setOpacity (opacity);
	g.drawImageAt (&image, 0, 0);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ReduceOpacityEffect.cpp ***/


/*** Start of inlined file: juce_Font.cpp ***/
BEGIN_JUCE_NAMESPACE

namespace FontValues
{
	static float limitFontHeight (const float height) throw()
	{
		return jlimit (0.1f, 10000.0f, height);
	}

	static const float defaultFontHeight = 14.0f;
}

Font::SharedFontInternal::SharedFontInternal (const String& typefaceName_, const float height_, const float horizontalScale_,
											  const float kerning_, const float ascent_, const int styleFlags_,
											  Typeface* const typeface_) throw()
	: typefaceName (typefaceName_),
	  height (height_),
	  horizontalScale (horizontalScale_),
	  kerning (kerning_),
	  ascent (ascent_),
	  styleFlags (styleFlags_),
	  typeface (typeface_)
{
}

Font::SharedFontInternal::SharedFontInternal (const SharedFontInternal& other) throw()
	: typefaceName (other.typefaceName),
	  height (other.height),
	  horizontalScale (other.horizontalScale),
	  kerning (other.kerning),
	  ascent (other.ascent),
	  styleFlags (other.styleFlags),
	  typeface (other.typeface)
{
}

Font::Font() throw()
	: font (new SharedFontInternal (getDefaultSansSerifFontName(), FontValues::defaultFontHeight,
									1.0f, 0, 0, Font::plain, 0))
{
}

Font::Font (const float fontHeight, const int styleFlags_) throw()
	: font (new SharedFontInternal (getDefaultSansSerifFontName(), FontValues::limitFontHeight (fontHeight),
									1.0f, 0, 0, styleFlags_, 0))
{
}

Font::Font (const String& typefaceName_,
			const float fontHeight,
			const int styleFlags_) throw()
	: font (new SharedFontInternal (typefaceName_, FontValues::limitFontHeight (fontHeight),
									1.0f, 0, 0, styleFlags_, 0))
{
}

Font::Font (const Font& other) throw()
	: font (other.font)
{
}

Font& Font::operator= (const Font& other) throw()
{
	font = other.font;
	return *this;
}

Font::~Font() throw()
{
}

Font::Font (const Typeface::Ptr& typeface) throw()
	: font (new SharedFontInternal (typeface->getName(), FontValues::defaultFontHeight,
									1.0f, 0, 0, Font::plain, typeface))
{
}

bool Font::operator== (const Font& other) const throw()
{
	return font == other.font
			|| (font->height == other.font->height
				&& font->styleFlags == other.font->styleFlags
				&& font->horizontalScale == other.font->horizontalScale
				&& font->kerning == other.font->kerning
				&& font->typefaceName == other.font->typefaceName);
}

bool Font::operator!= (const Font& other) const throw()
{
	return ! operator== (other);
}

void Font::dupeInternalIfShared() throw()
{
	if (font->getReferenceCount() > 1)
		font = new SharedFontInternal (*font);
}

const String Font::getDefaultSansSerifFontName() throw()
{
	static const String name ("<Sans-Serif>");
	return name;
}

const String Font::getDefaultSerifFontName() throw()
{
	static const String name ("<Serif>");
	return name;
}

const String Font::getDefaultMonospacedFontName() throw()
{
	static const String name ("<Monospaced>");
	return name;
}

void Font::setTypefaceName (const String& faceName) throw()
{
	if (faceName != font->typefaceName)
	{
		dupeInternalIfShared();
		font->typefaceName = faceName;
		font->typeface = 0;
		font->ascent = 0;
	}
}

static String fallbackFont;

const String Font::getFallbackFontName() throw()
{
	return fallbackFont;
}

void Font::setFallbackFontName (const String& name) throw()
{
	fallbackFont = name;
}

void Font::setHeight (float newHeight) throw()
{
	newHeight = FontValues::limitFontHeight (newHeight);

	if (font->height != newHeight)
	{
		dupeInternalIfShared();
		font->height = newHeight;
	}
}

void Font::setHeightWithoutChangingWidth (float newHeight) throw()
{
	newHeight = FontValues::limitFontHeight (newHeight);

	if (font->height != newHeight)
	{
		dupeInternalIfShared();
		font->horizontalScale *= (font->height / newHeight);
		font->height = newHeight;
	}
}

void Font::setStyleFlags (const int newFlags) throw()
{
	if (font->styleFlags != newFlags)
	{
		dupeInternalIfShared();
		font->styleFlags = newFlags;
		font->typeface = 0;
		font->ascent = 0;
	}
}

void Font::setSizeAndStyle (float newHeight,
							const int newStyleFlags,
							const float newHorizontalScale,
							const float newKerningAmount) throw()
{
	newHeight = FontValues::limitFontHeight (newHeight);

	if (font->height != newHeight
		 || font->horizontalScale != newHorizontalScale
		 || font->kerning != newKerningAmount)
	{
		dupeInternalIfShared();
		font->height = newHeight;
		font->horizontalScale = newHorizontalScale;
		font->kerning = newKerningAmount;
	}

	setStyleFlags (newStyleFlags);
}

void Font::setHorizontalScale (const float scaleFactor) throw()
{
	dupeInternalIfShared();
	font->horizontalScale = scaleFactor;
}

void Font::setExtraKerningFactor (const float extraKerning) throw()
{
	dupeInternalIfShared();
	font->kerning = extraKerning;
}

void Font::setBold (const bool shouldBeBold) throw()
{
	setStyleFlags (shouldBeBold ? (font->styleFlags | bold)
								: (font->styleFlags & ~bold));
}

bool Font::isBold() const throw()
{
	return (font->styleFlags & bold) != 0;
}

void Font::setItalic (const bool shouldBeItalic) throw()
{
	setStyleFlags (shouldBeItalic ? (font->styleFlags | italic)
								  : (font->styleFlags & ~italic));
}

bool Font::isItalic() const throw()
{
	return (font->styleFlags & italic) != 0;
}

void Font::setUnderline (const bool shouldBeUnderlined) throw()
{
	setStyleFlags (shouldBeUnderlined ? (font->styleFlags | underlined)
									  : (font->styleFlags & ~underlined));
}

bool Font::isUnderlined() const throw()
{
	return (font->styleFlags & underlined) != 0;
}

float Font::getAscent() const throw()
{
	if (font->ascent == 0)
		font->ascent = getTypeface()->getAscent();

	return font->height * font->ascent;
}

float Font::getDescent() const throw()
{
	return font->height - getAscent();
}

int Font::getStringWidth (const String& text) const throw()
{
	return roundToInt (getStringWidthFloat (text));
}

float Font::getStringWidthFloat (const String& text) const throw()
{
	float w = getTypeface()->getStringWidth (text);

	if (font->kerning != 0)
		w += font->kerning * text.length();

	return w * font->height * font->horizontalScale;
}

void Font::getGlyphPositions (const String& text, Array <int>& glyphs, Array <float>& xOffsets) const throw()
{
	getTypeface()->getGlyphPositions (text, glyphs, xOffsets);

	const float scale = font->height * font->horizontalScale;
	const int num = xOffsets.size();

	if (num > 0)
	{
		float* const x = &(xOffsets.getReference(0));

		if (font->kerning != 0)
		{
			for (int i = 0; i < num; ++i)
				x[i] = (x[i] + i * font->kerning) * scale;
		}
		else
		{
			for (int i = 0; i < num; ++i)
				x[i] *= scale;
		}
	}
}

void Font::findFonts (Array<Font>& destArray) throw()
{
	const StringArray names (findAllTypefaceNames());

	for (int i = 0; i < names.size(); ++i)
		destArray.add (Font (names[i], FontValues::defaultFontHeight, Font::plain));
}

class TypefaceCache  : public DeletedAtShutdown
{
public:
	TypefaceCache (int numToCache = 10) throw()
		: counter (1)
	{
		while (--numToCache >= 0)
			faces.add (new CachedFace());
	}

	~TypefaceCache()
	{
		clearSingletonInstance();
	}

	juce_DeclareSingleton_SingleThreaded_Minimal (TypefaceCache)

	const Typeface::Ptr findTypefaceFor (const Font& font) throw()
	{
		const int flags = font.getStyleFlags() & (Font::bold | Font::italic);
		const String faceName (font.getTypefaceName());

		int i;
		for (i = faces.size(); --i >= 0;)
		{
			CachedFace* const face = faces.getUnchecked(i);

			if (face->flags == flags
				 && face->typefaceName == faceName)
			{
				face->lastUsageCount = ++counter;
				return face->typeFace;
			}
		}

		int replaceIndex = 0;
		int bestLastUsageCount = std::numeric_limits<int>::max();

		for (i = faces.size(); --i >= 0;)
		{
			const int lu = faces.getUnchecked(i)->lastUsageCount;

			if (bestLastUsageCount > lu)
			{
				bestLastUsageCount = lu;
				replaceIndex = i;
			}
		}

		CachedFace* const face = faces.getUnchecked (replaceIndex);
		face->typefaceName = faceName;
		face->flags = flags;
		face->lastUsageCount = ++counter;
		face->typeFace = LookAndFeel::getDefaultLookAndFeel().getTypefaceForFont (font);
		jassert (face->typeFace != 0); // the look and feel must return a typeface!

		return face->typeFace;
	}

	juce_UseDebuggingNewOperator

private:
	struct CachedFace
	{
		CachedFace() throw()
			: lastUsageCount (0), flags (-1)
		{
		}

		String typefaceName;
		int lastUsageCount;
		int flags;
		Typeface::Ptr typeFace;
	};

	int counter;
	OwnedArray <CachedFace> faces;

	TypefaceCache (const TypefaceCache&);
	TypefaceCache& operator= (const TypefaceCache&);
};

juce_ImplementSingleton_SingleThreaded (TypefaceCache)

Typeface* Font::getTypeface() const throw()
{
	if (font->typeface == 0)
		font->typeface = TypefaceCache::getInstance()->findTypefaceFor (*this);

	return font->typeface;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Font.cpp ***/


/*** Start of inlined file: juce_GlyphArrangement.cpp ***/
BEGIN_JUCE_NAMESPACE

PositionedGlyph::PositionedGlyph (const float x_, const float y_, const float w_, const Font& font_,
								  const juce_wchar character_, const int glyph_)
	: x (x_),
	  y (y_),
	  w (w_),
	  font (font_),
	  character (character_),
	  glyph (glyph_)
{
}

PositionedGlyph::PositionedGlyph (const PositionedGlyph& other)
	: x (other.x),
	  y (other.y),
	  w (other.w),
	  font (other.font),
	  character (other.character),
	  glyph (other.glyph)
{
}

void PositionedGlyph::draw (const Graphics& g) const
{
	if (! isWhitespace())
	{
		g.getInternalContext()->setFont (font);
		g.getInternalContext()->drawGlyph (glyph, AffineTransform::translation (x, y));
	}
}

void PositionedGlyph::draw (const Graphics& g,
							const AffineTransform& transform) const
{
	if (! isWhitespace())
	{
		g.getInternalContext()->setFont (font);
		g.getInternalContext()->drawGlyph (glyph, AffineTransform::translation (x, y)
																  .followedBy (transform));
	}
}

void PositionedGlyph::createPath (Path& path) const
{
	if (! isWhitespace())
	{
		Typeface* const t = font.getTypeface();

		if (t != 0)
		{
			Path p;
			t->getOutlineForGlyph (glyph, p);

			path.addPath (p, AffineTransform::scale (font.getHeight() * font.getHorizontalScale(), font.getHeight())
											 .translated (x, y));
		}
	}
}

bool PositionedGlyph::hitTest (float px, float py) const
{
	if (getBounds().contains (px, py) && ! isWhitespace())
	{
		Typeface* const t = font.getTypeface();

		if (t != 0)
		{
			Path p;
			t->getOutlineForGlyph (glyph, p);

			AffineTransform::translation (-x, -y)
							.scaled (1.0f / (font.getHeight() * font.getHorizontalScale()), 1.0f / font.getHeight())
							.transformPoint (px, py);

			return p.contains (px, py);
		}
	}

	return false;
}

void PositionedGlyph::moveBy (const float deltaX,
							  const float deltaY)
{
	x += deltaX;
	y += deltaY;
}

GlyphArrangement::GlyphArrangement()
{
	glyphs.ensureStorageAllocated (128);
}

GlyphArrangement::GlyphArrangement (const GlyphArrangement& other)
{
	addGlyphArrangement (other);
}

GlyphArrangement& GlyphArrangement::operator= (const GlyphArrangement& other)
{
	if (this != &other)
	{
		clear();
		addGlyphArrangement (other);
	}

	return *this;
}

GlyphArrangement::~GlyphArrangement()
{
}

void GlyphArrangement::clear()
{
	glyphs.clear();
}

PositionedGlyph& GlyphArrangement::getGlyph (const int index) const
{
	jassert (((unsigned int) index) < (unsigned int) glyphs.size());

	return *glyphs [index];
}

void GlyphArrangement::addGlyphArrangement (const GlyphArrangement& other)
{
	glyphs.ensureStorageAllocated (glyphs.size() + other.glyphs.size());

	for (int i = 0; i < other.glyphs.size(); ++i)
		glyphs.add (new PositionedGlyph (*other.glyphs.getUnchecked (i)));
}

void GlyphArrangement::removeRangeOfGlyphs (int startIndex, const int num)
{
	glyphs.removeRange (startIndex, num < 0 ? glyphs.size() : num);
}

void GlyphArrangement::addLineOfText (const Font& font,
									  const String& text,
									  const float xOffset,
									  const float yOffset)
{
	addCurtailedLineOfText (font, text,
							xOffset, yOffset,
							1.0e10f, false);
}

void GlyphArrangement::addCurtailedLineOfText (const Font& font,
											   const String& text,
											   float xOffset,
											   const float yOffset,
											   const float maxWidthPixels,
											   const bool useEllipsis)
{
	if (text.isNotEmpty())
	{
		Array <int> newGlyphs;
		Array <float> xOffsets;
		font.getGlyphPositions (text, newGlyphs, xOffsets);
		const int textLen = newGlyphs.size();

		const juce_wchar* const unicodeText = text;

		for (int i = 0; i < textLen; ++i)
		{
			const float thisX = xOffsets.getUnchecked (i);
			const float nextX = xOffsets.getUnchecked (i + 1);

			if (nextX > maxWidthPixels + 1.0f)
			{
				// curtail the string if it's too wide..
				if (useEllipsis && textLen > 3 && glyphs.size() >= 3)
					insertEllipsis (font, xOffset + maxWidthPixels, 0, glyphs.size());

				break;
			}
			else
			{
				glyphs.add (new PositionedGlyph (xOffset + thisX, yOffset, nextX - thisX,
												 font, unicodeText[i], newGlyphs.getUnchecked(i)));
			}
		}
	}
}

int GlyphArrangement::insertEllipsis (const Font& font, const float maxXPos,
									  const int startIndex, int endIndex)
{
	int numDeleted = 0;

	if (glyphs.size() > 0)
	{
		Array<int> dotGlyphs;
		Array<float> dotXs;
		font.getGlyphPositions ("..", dotGlyphs, dotXs);

		const float dx = dotXs[1];
		float xOffset = 0.0f, yOffset = 0.0f;

		while (endIndex > startIndex)
		{
			const PositionedGlyph* pg = glyphs.getUnchecked (--endIndex);
			xOffset = pg->x;
			yOffset = pg->y;

			glyphs.remove (endIndex);
			++numDeleted;

			if (xOffset + dx * 3 <= maxXPos)
				break;
		}

		for (int i = 3; --i >= 0;)
		{
			glyphs.insert (endIndex++, new PositionedGlyph (xOffset, yOffset, dx,
															font, '.', dotGlyphs.getFirst()));
			--numDeleted;
			xOffset += dx;

			if (xOffset > maxXPos)
				break;
		}
	}

	return numDeleted;
}

void GlyphArrangement::addJustifiedText (const Font& font,
										 const String& text,
										 float x, float y,
										 const float maxLineWidth,
										 const Justification& horizontalLayout)
{
	int lineStartIndex = glyphs.size();
	addLineOfText (font, text, x, y);

	const float originalY = y;

	while (lineStartIndex < glyphs.size())
	{
		int i = lineStartIndex;

		if (glyphs.getUnchecked(i)->getCharacter() != '\n'
			  && glyphs.getUnchecked(i)->getCharacter() != '\r')
			++i;

		const float lineMaxX = glyphs.getUnchecked (lineStartIndex)->getLeft() + maxLineWidth;
		int lastWordBreakIndex = -1;

		while (i < glyphs.size())
		{
			const PositionedGlyph* pg = glyphs.getUnchecked (i);
			const juce_wchar c = pg->getCharacter();

			if (c == '\r' || c == '\n')
			{
				++i;

				if (c == '\r' && i < glyphs.size()
					 && glyphs.getUnchecked(i)->getCharacter() == '\n')
					++i;

				break;
			}
			else if (pg->isWhitespace())
			{
				lastWordBreakIndex = i + 1;
			}
			else if (pg->getRight() - 0.0001f >= lineMaxX)
			{
				if (lastWordBreakIndex >= 0)
					i = lastWordBreakIndex;

				break;
			}

			++i;
		}

		const float currentLineStartX = glyphs.getUnchecked (lineStartIndex)->getLeft();
		float currentLineEndX = currentLineStartX;

		for (int j = i; --j >= lineStartIndex;)
		{
			if (! glyphs.getUnchecked (j)->isWhitespace())
			{
				currentLineEndX = glyphs.getUnchecked (j)->getRight();
				break;
			}
		}

		float deltaX = 0.0f;

		if (horizontalLayout.testFlags (Justification::horizontallyJustified))
			spreadOutLine (lineStartIndex, i - lineStartIndex, maxLineWidth);
		else if (horizontalLayout.testFlags (Justification::horizontallyCentred))
			deltaX = (maxLineWidth - (currentLineEndX - currentLineStartX)) * 0.5f;
		else if (horizontalLayout.testFlags (Justification::right))
			deltaX = maxLineWidth - (currentLineEndX - currentLineStartX);

		moveRangeOfGlyphs (lineStartIndex, i - lineStartIndex,
						   x + deltaX - currentLineStartX, y - originalY);

		lineStartIndex = i;

		y += font.getHeight();
	}
}

void GlyphArrangement::addFittedText (const Font& f,
									  const String& text,
									  const float x, const float y,
									  const float width, const float height,
									  const Justification& layout,
									  int maximumLines,
									  const float minimumHorizontalScale)
{
	// doesn't make much sense if this is outside a sensible range of 0.5 to 1.0
	jassert (minimumHorizontalScale > 0 && minimumHorizontalScale <= 1.0f);

	if (text.containsAnyOf ("\r\n"))
	{
		GlyphArrangement ga;
		ga.addJustifiedText (f, text, x, y, width, layout);

		const Rectangle<float> bb (ga.getBoundingBox (0, -1, false));

		float dy = y - bb.getY();

		if (layout.testFlags (Justification::verticallyCentred))
			dy += (height - bb.getHeight()) * 0.5f;
		else if (layout.testFlags (Justification::bottom))
			dy += height - bb.getHeight();

		ga.moveRangeOfGlyphs (0, -1, 0.0f, dy);

		glyphs.ensureStorageAllocated (glyphs.size() + ga.glyphs.size());

		for (int i = 0; i < ga.glyphs.size(); ++i)
			glyphs.add (ga.glyphs.getUnchecked (i));

		ga.glyphs.clear (false);
		return;
	}

	int startIndex = glyphs.size();
	addLineOfText (f, text.trim(), x, y);

	if (glyphs.size() > startIndex)
	{
		float lineWidth = glyphs.getUnchecked (glyphs.size() - 1)->getRight()
							- glyphs.getUnchecked (startIndex)->getLeft();

		if (lineWidth <= 0)
			return;

		if (lineWidth * minimumHorizontalScale < width)
		{
			if (lineWidth > width)
				stretchRangeOfGlyphs (startIndex, glyphs.size() - startIndex,
									  width / lineWidth);

			justifyGlyphs (startIndex, glyphs.size() - startIndex,
						   x, y, width, height, layout);
		}
		else if (maximumLines <= 1)
		{
			fitLineIntoSpace (startIndex, glyphs.size() - startIndex,
							  x, y, width, height, f, layout, minimumHorizontalScale);
		}
		else
		{
			Font font (f);
			String txt (text.trim());
			const int length = txt.length();
			const int originalStartIndex = startIndex;
			int numLines = 1;

			if (length <= 12 && ! txt.containsAnyOf (" -\t\r\n"))
				maximumLines = 1;

			maximumLines = jmin (maximumLines, length);

			while (numLines < maximumLines)
			{
				++numLines;

				const float newFontHeight = height / (float) numLines;

				if (newFontHeight < font.getHeight())
				{
					font.setHeight (jmax (8.0f, newFontHeight));

					removeRangeOfGlyphs (startIndex, -1);
					addLineOfText (font, txt, x, y);

					lineWidth = glyphs.getUnchecked (glyphs.size() - 1)->getRight()
									- glyphs.getUnchecked (startIndex)->getLeft();
				}

				if (numLines > lineWidth / width || newFontHeight < 8.0f)
					break;
			}

			if (numLines < 1)
				numLines = 1;

			float lineY = y;
			float widthPerLine = lineWidth / numLines;
			int lastLineStartIndex = 0;

			for (int line = 0; line < numLines; ++line)
			{
				int i = startIndex;
				lastLineStartIndex = i;
				float lineStartX = glyphs.getUnchecked (startIndex)->getLeft();

				if (line == numLines - 1)
				{
					widthPerLine = width;
					i = glyphs.size();
				}
				else
				{
					while (i < glyphs.size())
					{
						lineWidth = (glyphs.getUnchecked (i)->getRight() - lineStartX);

						if (lineWidth > widthPerLine)
						{
							// got to a point where the line's too long, so skip forward to find a
							// good place to break it..
							const int searchStartIndex = i;

							while (i < glyphs.size())
							{
								if ((glyphs.getUnchecked (i)->getRight() - lineStartX) * minimumHorizontalScale < width)
								{
									if (glyphs.getUnchecked (i)->isWhitespace()
										 || glyphs.getUnchecked (i)->getCharacter() == '-')
									{
										++i;
										break;
									}
								}
								else
								{
									// can't find a suitable break, so try looking backwards..
									i = searchStartIndex;

									for (int back = 1; back < jmin (5, i - startIndex - 1); ++back)
									{
										if (glyphs.getUnchecked (i - back)->isWhitespace()
											 || glyphs.getUnchecked (i - back)->getCharacter() == '-')
										{
											i -= back - 1;
											break;
										}
									}

									break;
								}

								++i;
							}

							break;
						}

						++i;
					}

					int wsStart = i;
					while (wsStart > 0 && glyphs.getUnchecked (wsStart - 1)->isWhitespace())
						--wsStart;

					int wsEnd = i;

					while (wsEnd < glyphs.size() && glyphs.getUnchecked (wsEnd)->isWhitespace())
						++wsEnd;

					removeRangeOfGlyphs (wsStart, wsEnd - wsStart);
					i = jmax (wsStart, startIndex + 1);
				}

				i -= fitLineIntoSpace (startIndex, i - startIndex,
									   x, lineY, width, font.getHeight(), font,
									   layout.getOnlyHorizontalFlags() | Justification::verticallyCentred,
									   minimumHorizontalScale);

				startIndex = i;
				lineY += font.getHeight();

				if (startIndex >= glyphs.size())
					break;
			}

			justifyGlyphs (originalStartIndex, glyphs.size() - originalStartIndex,
						   x, y, width, height, layout.getFlags() & ~Justification::horizontallyJustified);
		}
	}
}

void GlyphArrangement::moveRangeOfGlyphs (int startIndex, int num,
										  const float dx, const float dy)
{
	jassert (startIndex >= 0);

	if (dx != 0.0f || dy != 0.0f)
	{
		if (num < 0 || startIndex + num > glyphs.size())
			num = glyphs.size() - startIndex;

		while (--num >= 0)
			glyphs.getUnchecked (startIndex++)->moveBy (dx, dy);
	}
}

int GlyphArrangement::fitLineIntoSpace (int start, int numGlyphs, float x, float y, float w, float h, const Font& font,
										const Justification& justification, float minimumHorizontalScale)
{
	int numDeleted = 0;
	const float lineStartX = glyphs.getUnchecked (start)->getLeft();
	float lineWidth = glyphs.getUnchecked (start + numGlyphs - 1)->getRight() - lineStartX;

	if (lineWidth > w)
	{
		if (minimumHorizontalScale < 1.0f)
		{
			stretchRangeOfGlyphs (start, numGlyphs, jmax (minimumHorizontalScale, w / lineWidth));
			lineWidth = glyphs.getUnchecked (start + numGlyphs - 1)->getRight() - lineStartX - 0.5f;
		}

		if (lineWidth > w)
		{
			numDeleted = insertEllipsis (font, lineStartX + w, start, start + numGlyphs);
			numGlyphs -= numDeleted;
		}
	}

	justifyGlyphs (start, numGlyphs, x, y, w, h, justification);
	return numDeleted;
}

void GlyphArrangement::stretchRangeOfGlyphs (int startIndex, int num,
											 const float horizontalScaleFactor)
{
	jassert (startIndex >= 0);

	if (num < 0 || startIndex + num > glyphs.size())
		num = glyphs.size() - startIndex;

	if (num > 0)
	{
		const float xAnchor = glyphs.getUnchecked (startIndex)->getLeft();

		while (--num >= 0)
		{
			PositionedGlyph* const pg = glyphs.getUnchecked (startIndex++);

			pg->x = xAnchor + (pg->x - xAnchor) * horizontalScaleFactor;
			pg->font.setHorizontalScale (pg->font.getHorizontalScale() * horizontalScaleFactor);
			pg->w *= horizontalScaleFactor;
		}
	}
}

const Rectangle<float> GlyphArrangement::getBoundingBox (int startIndex, int num, const bool includeWhitespace) const
{
	jassert (startIndex >= 0);

	if (num < 0 || startIndex + num > glyphs.size())
		num = glyphs.size() - startIndex;

	Rectangle<float> result;
	bool isFirst = true;

	while (--num >= 0)
	{
		const PositionedGlyph* const pg = glyphs.getUnchecked (startIndex++);

		if (includeWhitespace || ! pg->isWhitespace())
		{
			if (isFirst)
			{
				isFirst = false;
				result = pg->getBounds();
			}
			else
			{
				result = result.getUnion (pg->getBounds());
			}
		}
	}

	return result;
}

void GlyphArrangement::justifyGlyphs (const int startIndex, const int num,
									  const float x, const float y, const float width, const float height,
									  const Justification& justification)
{
	jassert (num >= 0 && startIndex >= 0);

	if (glyphs.size() > 0 && num > 0)
	{
		const Rectangle<float> bb (getBoundingBox (startIndex, num, ! justification.testFlags (Justification::horizontallyJustified
																								| Justification::horizontallyCentred)));
		float deltaX = 0.0f;

		if (justification.testFlags (Justification::horizontallyJustified))
			deltaX = x - bb.getX();
		else if (justification.testFlags (Justification::horizontallyCentred))
			deltaX = x + (width - bb.getWidth()) * 0.5f - bb.getX();
		else if (justification.testFlags (Justification::right))
			deltaX = (x + width) - bb.getRight();
		else
			deltaX = x - bb.getX();

		float deltaY = 0.0f;

		if (justification.testFlags (Justification::top))
			deltaY = y - bb.getY();
		else if (justification.testFlags (Justification::bottom))
			deltaY = (y + height) - bb.getBottom();
		else
			deltaY = y + (height - bb.getHeight()) * 0.5f - bb.getY();

		moveRangeOfGlyphs (startIndex, num, deltaX, deltaY);

		if (justification.testFlags (Justification::horizontallyJustified))
		{
			int lineStart = 0;
			float baseY = glyphs.getUnchecked (startIndex)->getBaselineY();

			int i;
			for (i = 0; i < num; ++i)
			{
				const float glyphY = glyphs.getUnchecked (startIndex + i)->getBaselineY();

				if (glyphY != baseY)
				{
					spreadOutLine (startIndex + lineStart, i - lineStart, width);

					lineStart = i;
					baseY = glyphY;
				}
			}

			if (i > lineStart)
				spreadOutLine (startIndex + lineStart, i - lineStart, width);
		}
	}
}

void GlyphArrangement::spreadOutLine (const int start, const int num, const float targetWidth)
{
	if (start + num < glyphs.size()
		 && glyphs.getUnchecked (start + num - 1)->getCharacter() != '\r'
		 && glyphs.getUnchecked (start + num - 1)->getCharacter() != '\n')
	{
		int numSpaces = 0;
		int spacesAtEnd = 0;

		for (int i = 0; i < num; ++i)
		{
			if (glyphs.getUnchecked (start + i)->isWhitespace())
			{
				++spacesAtEnd;
				++numSpaces;
			}
			else
			{
				spacesAtEnd = 0;
			}
		}

		numSpaces -= spacesAtEnd;

		if (numSpaces > 0)
		{
			const float startX = glyphs.getUnchecked (start)->getLeft();
			const float endX = glyphs.getUnchecked (start + num - 1 - spacesAtEnd)->getRight();

			const float extraPaddingBetweenWords
				= (targetWidth - (endX - startX)) / (float) numSpaces;

			float deltaX = 0.0f;

			for (int i = 0; i < num; ++i)
			{
				glyphs.getUnchecked (start + i)->moveBy (deltaX, 0.0f);

				if (glyphs.getUnchecked (start + i)->isWhitespace())
					deltaX += extraPaddingBetweenWords;
			}
		}
	}
}

void GlyphArrangement::draw (const Graphics& g) const
{
	for (int i = 0; i < glyphs.size(); ++i)
	{
		const PositionedGlyph* const pg = glyphs.getUnchecked(i);

		if (pg->font.isUnderlined())
		{
			const float lineThickness = (pg->font.getDescent()) * 0.3f;

			float nextX = pg->x + pg->w;

			if (i < glyphs.size() - 1 && glyphs.getUnchecked (i + 1)->y == pg->y)
				nextX = glyphs.getUnchecked (i + 1)->x;

			g.fillRect (pg->x, pg->y + lineThickness * 2.0f,
						nextX - pg->x, lineThickness);
		}

		pg->draw (g);
	}
}

void GlyphArrangement::draw (const Graphics& g, const AffineTransform& transform) const
{
	for (int i = 0; i < glyphs.size(); ++i)
	{
		const PositionedGlyph* const pg = glyphs.getUnchecked(i);

		if (pg->font.isUnderlined())
		{
			const float lineThickness = (pg->font.getDescent()) * 0.3f;

			float nextX = pg->x + pg->w;

			if (i < glyphs.size() - 1 && glyphs.getUnchecked (i + 1)->y == pg->y)
				nextX = glyphs.getUnchecked (i + 1)->x;

			Path p;
			p.addLineSegment (pg->x, pg->y + lineThickness * 2.0f,
							  nextX, pg->y + lineThickness * 2.0f,
							  lineThickness);

			g.fillPath (p, transform);
		}

		pg->draw (g, transform);
	}
}

void GlyphArrangement::createPath (Path& path) const
{
	for (int i = 0; i < glyphs.size(); ++i)
		glyphs.getUnchecked (i)->createPath (path);
}

int GlyphArrangement::findGlyphIndexAt (float x, float y) const
{
	for (int i = 0; i < glyphs.size(); ++i)
		if (glyphs.getUnchecked (i)->hitTest (x, y))
			return i;

	return -1;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_GlyphArrangement.cpp ***/


/*** Start of inlined file: juce_TextLayout.cpp ***/
BEGIN_JUCE_NAMESPACE

class TextLayout::Token
{
public:
	String text;
	Font font;
	int x, y, w, h;
	int line, lineHeight;
	bool isWhitespace, isNewLine;

	Token (const String& t,
		   const Font& f,
		   const bool isWhitespace_)
		: text (t),
		  font (f),
		  x(0),
		  y(0),
		  isWhitespace (isWhitespace_)
	{
		w = font.getStringWidth (t);
		h = roundToInt (f.getHeight());
		isNewLine = t.containsChar ('\n') || t.containsChar ('\r');
	}

	Token (const Token& other)
		: text (other.text),
		  font (other.font),
		  x (other.x),
		  y (other.y),
		  w (other.w),
		  h (other.h),
		  line (other.line),
		  lineHeight (other.lineHeight),
		  isWhitespace (other.isWhitespace),
		  isNewLine (other.isNewLine)
	{
	}

	~Token()
	{
	}

	void draw (Graphics& g,
			   const int xOffset,
			   const int yOffset)
	{
		if (! isWhitespace)
		{
			g.setFont (font);
			g.drawSingleLineText (text.trimEnd(),
								  xOffset + x,
								  yOffset + y + (lineHeight - h)
									+ roundToInt (font.getAscent()));
		}
	}

	juce_UseDebuggingNewOperator
};

TextLayout::TextLayout()
	: totalLines (0)
{
	tokens.ensureStorageAllocated (64);
}

TextLayout::TextLayout (const String& text, const Font& font)
	: totalLines (0)
{
	tokens.ensureStorageAllocated (64);
	appendText (text, font);
}

TextLayout::TextLayout (const TextLayout& other)
	: totalLines (0)
{
	*this = other;
}

TextLayout& TextLayout::operator= (const TextLayout& other)
{
	if (this != &other)
	{
		clear();

		totalLines = other.totalLines;

		for (int i = 0; i < other.tokens.size(); ++i)
			tokens.add (new Token (*other.tokens.getUnchecked(i)));
	}

	return *this;
}

TextLayout::~TextLayout()
{
	clear();
}

void TextLayout::clear()
{
	tokens.clear();
	totalLines = 0;
}

void TextLayout::appendText (const String& text, const Font& font)
{
	const juce_wchar* t = text;
	String currentString;
	int lastCharType = 0;

	for (;;)
	{
		const juce_wchar c = *t++;
		if (c == 0)
			break;

		int charType;
		if (c == '\r' || c == '\n')
		{
			charType = 0;
		}
		else if (CharacterFunctions::isWhitespace (c))
		{
			charType = 2;
		}
		else
		{
			charType = 1;
		}

		if (charType == 0 || charType != lastCharType)
		{
			if (currentString.isNotEmpty())
			{
				tokens.add (new Token (currentString, font,
									   lastCharType == 2 || lastCharType == 0));
			}

			currentString = String::charToString (c);

			if (c == '\r' && *t == '\n')
				currentString += *t++;
		}
		else
		{
			currentString += c;
		}

		lastCharType = charType;
	}

	if (currentString.isNotEmpty())
		tokens.add (new Token (currentString, font, lastCharType == 2));
}

void TextLayout::setText (const String& text, const Font& font)
{
	clear();
	appendText (text, font);
}

void TextLayout::layout (int maxWidth,
						 const Justification& justification,
						 const bool attemptToBalanceLineLengths)
{
	if (attemptToBalanceLineLengths)
	{
		const int originalW = maxWidth;
		int bestWidth = maxWidth;
		float bestLineProportion = 0.0f;

		while (maxWidth > originalW / 2)
		{
			layout (maxWidth, justification, false);

			if (getNumLines() <= 1)
				return;

			const int lastLineW = getLineWidth (getNumLines() - 1);
			const int lastButOneLineW = getLineWidth (getNumLines() - 2);

			const float prop = lastLineW / (float) lastButOneLineW;

			if (prop > 0.9f)
				return;

			if (prop > bestLineProportion)
			{
				bestLineProportion = prop;
				bestWidth = maxWidth;
			}

			maxWidth -= 10;
		}

		layout (bestWidth, justification, false);
	}
	else
	{
		int x = 0;
		int y = 0;
		int h = 0;
		totalLines = 0;
		int i;

		for (i = 0; i < tokens.size(); ++i)
		{
			Token* const t = tokens.getUnchecked(i);
			t->x = x;
			t->y = y;
			t->line = totalLines;
			x += t->w;
			h = jmax (h, t->h);

			const Token* nextTok = tokens [i + 1];

			if (nextTok == 0)
				break;

			if (t->isNewLine || ((! nextTok->isWhitespace) && x + nextTok->w > maxWidth))
			{
				// finished a line, so go back and update the heights of the things on it
				for (int j = i; j >= 0; --j)
				{
					Token* const tok = tokens.getUnchecked(j);

					if (tok->line == totalLines)
						tok->lineHeight = h;
					else
						break;
				}

				x = 0;
				y += h;
				h = 0;
				++totalLines;
			}
		}

		// finished a line, so go back and update the heights of the things on it
		for (int j = jmin (i, tokens.size() - 1); j >= 0; --j)
		{
			Token* const t = tokens.getUnchecked(j);

			if (t->line == totalLines)
				t->lineHeight = h;
			else
				break;
		}

		++totalLines;

		if (! justification.testFlags (Justification::left))
		{
			int totalW = getWidth();

			for (i = totalLines; --i >= 0;)
			{
				const int lineW = getLineWidth (i);

				int dx = 0;
				if (justification.testFlags (Justification::horizontallyCentred))
					dx = (totalW - lineW) / 2;
				else if (justification.testFlags (Justification::right))
					dx = totalW - lineW;

				for (int j = tokens.size(); --j >= 0;)
				{
					Token* const t = tokens.getUnchecked(j);

					if (t->line == i)
						t->x += dx;
				}
			}
		}
	}
}

int TextLayout::getLineWidth (const int lineNumber) const
{
	int maxW = 0;

	for (int i = tokens.size(); --i >= 0;)
	{
		const Token* const t = tokens.getUnchecked(i);

		if (t->line == lineNumber && ! t->isWhitespace)
			maxW = jmax (maxW, t->x + t->w);
	}

	return maxW;
}

int TextLayout::getWidth() const
{
	int maxW = 0;

	for (int i = tokens.size(); --i >= 0;)
	{
		const Token* const t = tokens.getUnchecked(i);
		if (! t->isWhitespace)
			maxW = jmax (maxW, t->x + t->w);
	}

	return maxW;
}

int TextLayout::getHeight() const
{
	int maxH = 0;

	for (int i = tokens.size(); --i >= 0;)
	{
		const Token* const t = tokens.getUnchecked(i);

		if (! t->isWhitespace)
			maxH = jmax (maxH, t->y + t->h);
	}

	return maxH;
}

void TextLayout::draw (Graphics& g,
					   const int xOffset,
					   const int yOffset) const
{
	for (int i = tokens.size(); --i >= 0;)
		tokens.getUnchecked(i)->draw (g, xOffset, yOffset);
}

void TextLayout::drawWithin (Graphics& g,
							 int x, int y, int w, int h,
							 const Justification& justification) const
{
	justification.applyToRectangle (x, y, getWidth(), getHeight(),
									x, y, w, h);

	draw (g, x, y);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_TextLayout.cpp ***/


/*** Start of inlined file: juce_Typeface.cpp ***/
BEGIN_JUCE_NAMESPACE

Typeface::Typeface (const String& name_) throw()
	: name (name_)
{
}

Typeface::~Typeface()
{
}

class CustomTypeface::GlyphInfo
{
public:
	GlyphInfo (const juce_wchar character_, const Path& path_, const float width_) throw()
		: character (character_), path (path_), width (width_)
	{
	}

	~GlyphInfo() throw()
	{
	}

	struct KerningPair
	{
		juce_wchar character2;
		float kerningAmount;
	};

	void addKerningPair (const juce_wchar subsequentCharacter,
						 const float extraKerningAmount) throw()
	{
		KerningPair kp;
		kp.character2 = subsequentCharacter;
		kp.kerningAmount = extraKerningAmount;
		kerningPairs.add (kp);
	}

	float getHorizontalSpacing (const juce_wchar subsequentCharacter) const throw()
	{
		if (subsequentCharacter != 0)
		{
			for (int i = kerningPairs.size(); --i >= 0;)
				if (kerningPairs.getReference(i).character2 == subsequentCharacter)
					return width + kerningPairs.getReference(i).kerningAmount;
		}

		return width;
	}

	const juce_wchar character;
	const Path path;
	float width;
	Array <KerningPair> kerningPairs;

	juce_UseDebuggingNewOperator

private:
	GlyphInfo (const GlyphInfo&);
	GlyphInfo& operator= (const GlyphInfo&);
};

CustomTypeface::CustomTypeface()
	: Typeface (String::empty)
{
	clear();
}

CustomTypeface::CustomTypeface (InputStream& serialisedTypefaceStream)
	: Typeface (String::empty)
{
	clear();

	GZIPDecompressorInputStream gzin (&serialisedTypefaceStream, false);
	BufferedInputStream in (&gzin, 32768, false);

	name = in.readString();
	isBold = in.readBool();
	isItalic = in.readBool();
	ascent = in.readFloat();
	defaultCharacter = (juce_wchar) in.readShort();

	int i, numChars = in.readInt();

	for (i = 0; i < numChars; ++i)
	{
		const juce_wchar c = (juce_wchar) in.readShort();
		const float width = in.readFloat();

		Path p;
		p.loadPathFromStream (in);
		addGlyph (c, p, width);
	}

	const int numKerningPairs = in.readInt();

	for (i = 0; i < numKerningPairs; ++i)
	{
		const juce_wchar char1 = (juce_wchar) in.readShort();
		const juce_wchar char2 = (juce_wchar) in.readShort();

		addKerningPair (char1, char2, in.readFloat());
	}
}

CustomTypeface::~CustomTypeface()
{
}

void CustomTypeface::clear()
{
	defaultCharacter = 0;
	ascent = 1.0f;
	isBold = isItalic = false;
	zeromem (lookupTable, sizeof (lookupTable));
	glyphs.clear();
}

void CustomTypeface::setCharacteristics (const String& name_, const float ascent_, const bool isBold_,
										 const bool isItalic_, const juce_wchar defaultCharacter_) throw()
{
	name = name_;
	defaultCharacter = defaultCharacter_;
	ascent = ascent_;
	isBold = isBold_;
	isItalic = isItalic_;
}

void CustomTypeface::addGlyph (const juce_wchar character, const Path& path, const float width) throw()
{
	// Check that you're not trying to add the same character twice..
	jassert (findGlyph (character, false) == 0);

	if (((unsigned int) character) < (unsigned int) numElementsInArray (lookupTable))
		lookupTable [character] = (short) glyphs.size();

	glyphs.add (new GlyphInfo (character, path, width));
}

void CustomTypeface::addKerningPair (const juce_wchar char1, const juce_wchar char2, const float extraAmount) throw()
{
	if (extraAmount != 0)
	{
		GlyphInfo* const g = findGlyph (char1, true);
		jassert (g != 0); // can only add kerning pairs for characters that exist!

		if (g != 0)
			g->addKerningPair (char2, extraAmount);
	}
}

CustomTypeface::GlyphInfo* CustomTypeface::findGlyph (const juce_wchar character, const bool loadIfNeeded) throw()
{
	if (((unsigned int) character) < (unsigned int) numElementsInArray (lookupTable) && lookupTable [character] > 0)
		return glyphs [(int) lookupTable [(int) character]];

	for (int i = 0; i < glyphs.size(); ++i)
	{
		GlyphInfo* const g = glyphs.getUnchecked(i);
		if (g->character == character)
			return g;
	}

	if (loadIfNeeded && loadGlyphIfPossible (character))
		return findGlyph (character, false);

	return 0;
}

CustomTypeface::GlyphInfo* CustomTypeface::findGlyphSubstituting (const juce_wchar character) throw()
{
	GlyphInfo* glyph = findGlyph (character, true);

	if (glyph == 0)
	{
		if (CharacterFunctions::isWhitespace (character) && character != L' ')
			glyph = findGlyph (L' ', true);

		if (glyph == 0)
		{
			const Font fallbackFont (Font::getFallbackFontName(), 10, 0);
			Typeface* const fallbackTypeface = fallbackFont.getTypeface();
			if (fallbackTypeface != 0 && fallbackTypeface != this)
			{
				//xxx
			}

			if (glyph == 0)
				glyph = findGlyph (defaultCharacter, true);
		}
	}

	return glyph;
}

bool CustomTypeface::loadGlyphIfPossible (const juce_wchar /*characterNeeded*/)
{
	return false;
}

void CustomTypeface::addGlyphsFromOtherTypeface (Typeface& typefaceToCopy, juce_wchar characterStartIndex, int numCharacters) throw()
{
	setCharacteristics (name, typefaceToCopy.getAscent(), isBold, isItalic, defaultCharacter);

	for (int i = 0; i < numCharacters; ++i)
	{
		const juce_wchar c = (juce_wchar) (characterStartIndex + i);

		Array <int> glyphIndexes;
		Array <float> offsets;
		typefaceToCopy.getGlyphPositions (String::charToString (c), glyphIndexes, offsets);

		const int glyphIndex = glyphIndexes.getFirst();

		if (glyphIndex >= 0 && glyphIndexes.size() > 0)
		{
			const float glyphWidth = offsets[1];

			Path p;
			typefaceToCopy.getOutlineForGlyph (glyphIndex, p);

			addGlyph (c, p, glyphWidth);

			for (int j = glyphs.size() - 1; --j >= 0;)
			{
				const juce_wchar char2 = glyphs.getUnchecked (j)->character;
				glyphIndexes.clearQuick();
				offsets.clearQuick();
				typefaceToCopy.getGlyphPositions (String::charToString (c) + String::charToString (char2), glyphIndexes, offsets);

				if (offsets.size() > 1)
					addKerningPair (c, char2, offsets[1] - glyphWidth);
			}
		}
	}
}

bool CustomTypeface::writeToStream (OutputStream& outputStream)
{
	GZIPCompressorOutputStream out (&outputStream);

	out.writeString (name);
	out.writeBool (isBold);
	out.writeBool (isItalic);
	out.writeFloat (ascent);
	out.writeShort ((short) (unsigned short) defaultCharacter);
	out.writeInt (glyphs.size());

	int i, numKerningPairs = 0;

	for (i = 0; i < glyphs.size(); ++i)
	{
		const GlyphInfo* const g = glyphs.getUnchecked (i);
		out.writeShort ((short) (unsigned short) g->character);
		out.writeFloat (g->width);
		g->path.writePathToStream (out);

		numKerningPairs += g->kerningPairs.size();
	}

	out.writeInt (numKerningPairs);

	for (i = 0; i < glyphs.size(); ++i)
	{
		const GlyphInfo* const g = glyphs.getUnchecked (i);

		for (int j = 0; j < g->kerningPairs.size(); ++j)
		{
			const GlyphInfo::KerningPair& p = g->kerningPairs.getReference (j);
			out.writeShort ((short) (unsigned short) g->character);
			out.writeShort ((short) (unsigned short) p.character2);
			out.writeFloat (p.kerningAmount);
		}
	}

	return true;
}

float CustomTypeface::getAscent() const
{
	return ascent;
}

float CustomTypeface::getDescent() const
{
	return 1.0f - ascent;
}

float CustomTypeface::getStringWidth (const String& text)
{
	float x = 0;
	const juce_wchar* t = text;

	while (*t != 0)
	{
		const GlyphInfo* const glyph = findGlyphSubstituting (*t++);

		if (glyph != 0)
			x += glyph->getHorizontalSpacing (*t);
	}

	return x;
}

void CustomTypeface::getGlyphPositions (const String& text, Array <int>& resultGlyphs, Array<float>& xOffsets)
{
	xOffsets.add (0);
	float x = 0;
	const juce_wchar* t = text;

	while (*t != 0)
	{
		const juce_wchar c = *t++;
		const GlyphInfo* const glyph = findGlyphSubstituting (c);

		if (glyph != 0)
		{
			x += glyph->getHorizontalSpacing (*t);
			resultGlyphs.add ((int) glyph->character);
			xOffsets.add (x);
		}
	}
}

bool CustomTypeface::getOutlineForGlyph (int glyphNumber, Path& path)
{
	const GlyphInfo* const glyph = findGlyphSubstituting ((juce_wchar) glyphNumber);
	if (glyph != 0)
	{
		path = glyph->path;
		return true;
	}

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Typeface.cpp ***/


/*** Start of inlined file: juce_AffineTransform.cpp ***/
BEGIN_JUCE_NAMESPACE

AffineTransform::AffineTransform() throw()
	: mat00 (1.0f),
	  mat01 (0),
	  mat02 (0),
	  mat10 (0),
	  mat11 (1.0f),
	  mat12 (0)
{
}

AffineTransform::AffineTransform (const AffineTransform& other) throw()
  : mat00 (other.mat00),
	mat01 (other.mat01),
	mat02 (other.mat02),
	mat10 (other.mat10),
	mat11 (other.mat11),
	mat12 (other.mat12)
{
}

AffineTransform::AffineTransform (const float mat00_,
								  const float mat01_,
								  const float mat02_,
								  const float mat10_,
								  const float mat11_,
								  const float mat12_) throw()
 :  mat00 (mat00_),
	mat01 (mat01_),
	mat02 (mat02_),
	mat10 (mat10_),
	mat11 (mat11_),
	mat12 (mat12_)
{
}

AffineTransform& AffineTransform::operator= (const AffineTransform& other) throw()
{
	mat00 = other.mat00;
	mat01 = other.mat01;
	mat02 = other.mat02;
	mat10 = other.mat10;
	mat11 = other.mat11;
	mat12 = other.mat12;

	return *this;
}

bool AffineTransform::operator== (const AffineTransform& other) const throw()
{
	return mat00 == other.mat00
		&& mat01 == other.mat01
		&& mat02 == other.mat02
		&& mat10 == other.mat10
		&& mat11 == other.mat11
		&& mat12 == other.mat12;
}

bool AffineTransform::operator!= (const AffineTransform& other) const throw()
{
	return ! operator== (other);
}

bool AffineTransform::isIdentity() const throw()
{
	return (mat01 == 0)
		&& (mat02 == 0)
		&& (mat10 == 0)
		&& (mat12 == 0)
		&& (mat00 == 1.0f)
		&& (mat11 == 1.0f);
}

const AffineTransform AffineTransform::identity;

const AffineTransform AffineTransform::followedBy (const AffineTransform& other) const throw()
{
	return AffineTransform (other.mat00 * mat00 + other.mat01 * mat10,
							other.mat00 * mat01 + other.mat01 * mat11,
							other.mat00 * mat02 + other.mat01 * mat12 + other.mat02,
							other.mat10 * mat00 + other.mat11 * mat10,
							other.mat10 * mat01 + other.mat11 * mat11,
							other.mat10 * mat02 + other.mat11 * mat12 + other.mat12);
}

const AffineTransform AffineTransform::followedBy (const float omat00,
												   const float omat01,
												   const float omat02,
												   const float omat10,
												   const float omat11,
												   const float omat12) const throw()
{
	return AffineTransform (omat00 * mat00 + omat01 * mat10,
							omat00 * mat01 + omat01 * mat11,
							omat00 * mat02 + omat01 * mat12 + omat02,
							omat10 * mat00 + omat11 * mat10,
							omat10 * mat01 + omat11 * mat11,
							omat10 * mat02 + omat11 * mat12 + omat12);
}

const AffineTransform AffineTransform::translated (const float dx,
												   const float dy) const throw()
{
	return AffineTransform (mat00, mat01, mat02 + dx,
							mat10, mat11, mat12 + dy);
}

const AffineTransform AffineTransform::translation (const float dx,
													const float dy) throw()
{
	return AffineTransform (1.0f, 0, dx,
							0, 1.0f, dy);
}

const AffineTransform AffineTransform::rotated (const float rad) const throw()
{
	const float cosRad = cosf (rad);
	const float sinRad = sinf (rad);

	return followedBy (cosRad, -sinRad, 0,
					   sinRad, cosRad, 0);
}

const AffineTransform AffineTransform::rotation (const float rad) throw()
{
	const float cosRad = cosf (rad);
	const float sinRad = sinf (rad);

	return AffineTransform (cosRad, -sinRad, 0,
							sinRad, cosRad, 0);
}

const AffineTransform AffineTransform::rotated (const float angle,
												const float pivotX,
												const float pivotY) const throw()
{
	return translated (-pivotX, -pivotY)
			.rotated (angle)
			.translated (pivotX, pivotY);
}

const AffineTransform AffineTransform::rotation (const float angle,
												 const float pivotX,
												 const float pivotY) throw()
{
	return translation (-pivotX, -pivotY)
			.rotated (angle)
			.translated (pivotX, pivotY);
}

const AffineTransform AffineTransform::scaled (const float factorX,
											   const float factorY) const throw()
{
	return AffineTransform (factorX * mat00, factorX * mat01, factorX * mat02,
							factorY * mat10, factorY * mat11, factorY * mat12);
}

const AffineTransform AffineTransform::scale (const float factorX,
											  const float factorY) throw()
{
	return AffineTransform (factorX, 0, 0,
							0, factorY, 0);
}

const AffineTransform AffineTransform::sheared (const float shearX,
												const float shearY) const throw()
{
	return followedBy (1.0f, shearX, 0,
					   shearY, 1.0f, 0);
}

const AffineTransform AffineTransform::inverted() const throw()
{
	double determinant = (mat00 * mat11 - mat10 * mat01);

	if (determinant != 0.0)
	{
		determinant = 1.0 / determinant;

		const float dst00 = (float) (mat11 * determinant);
		const float dst10 = (float) (-mat10 * determinant);
		const float dst01 = (float) (-mat01 * determinant);
		const float dst11 = (float) (mat00 * determinant);

		return AffineTransform (dst00, dst01, -mat02 * dst00 - mat12 * dst01,
								dst10, dst11, -mat02 * dst10 - mat12 * dst11);
	}
	else
	{
		// singularity..
		return *this;
	}
}

bool AffineTransform::isSingularity() const throw()
{
	return (mat00 * mat11 - mat10 * mat01) == 0.0;
}

bool AffineTransform::isOnlyTranslation() const throw()
{
	return (mat01 == 0)
		&& (mat10 == 0)
		&& (mat00 == 1.0f)
		&& (mat11 == 1.0f);
}

void AffineTransform::transformPoint (float& x,
									  float& y) const throw()
{
	const float oldX = x;
	x = mat00 * oldX + mat01 * y + mat02;
	y = mat10 * oldX + mat11 * y + mat12;
}

void AffineTransform::transformPoint (double& x,
									  double& y) const throw()
{
	const double oldX = x;
	x = mat00 * oldX + mat01 * y + mat02;
	y = mat10 * oldX + mat11 * y + mat12;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_AffineTransform.cpp ***/


/*** Start of inlined file: juce_BorderSize.cpp ***/
BEGIN_JUCE_NAMESPACE

BorderSize::BorderSize() throw()
	: top (0),
	  left (0),
	  bottom (0),
	  right (0)
{
}

BorderSize::BorderSize (const BorderSize& other) throw()
	: top (other.top),
	  left (other.left),
	  bottom (other.bottom),
	  right (other.right)
{
}

BorderSize::BorderSize (const int topGap,
						const int leftGap,
						const int bottomGap,
						const int rightGap) throw()
	: top (topGap),
	  left (leftGap),
	  bottom (bottomGap),
	  right (rightGap)
{
}

BorderSize::BorderSize (const int allGaps) throw()
	: top (allGaps),
	  left (allGaps),
	  bottom (allGaps),
	  right (allGaps)
{
}

BorderSize::~BorderSize() throw()
{
}

void BorderSize::setTop (const int newTopGap) throw()
{
	top = newTopGap;
}

void BorderSize::setLeft (const int newLeftGap) throw()
{
	left = newLeftGap;
}

void BorderSize::setBottom (const int newBottomGap) throw()
{
	bottom = newBottomGap;
}

void BorderSize::setRight (const int newRightGap) throw()
{
	right = newRightGap;
}

const Rectangle<int> BorderSize::subtractedFrom (const Rectangle<int>& r) const throw()
{
	return Rectangle<int> (r.getX() + left,
						   r.getY() + top,
						   r.getWidth() - (left + right),
						   r.getHeight() - (top + bottom));
}

void BorderSize::subtractFrom (Rectangle<int>& r) const throw()
{
	r.setBounds (r.getX() + left,
				 r.getY() + top,
				 r.getWidth() - (left + right),
				 r.getHeight() - (top + bottom));
}

const Rectangle<int> BorderSize::addedTo (const Rectangle<int>& r) const throw()
{
	return Rectangle<int> (r.getX() - left,
						   r.getY() - top,
						   r.getWidth() + (left + right),
						   r.getHeight() + (top + bottom));
}

void BorderSize::addTo (Rectangle<int>& r) const throw()
{
	r.setBounds (r.getX() - left,
				 r.getY() - top,
				 r.getWidth() + (left + right),
				 r.getHeight() + (top + bottom));
}

bool BorderSize::operator== (const BorderSize& other) const throw()
{
	return top == other.top
		&& left == other.left
		&& bottom == other.bottom
		&& right == other.right;
}

bool BorderSize::operator!= (const BorderSize& other) const throw()
{
	return ! operator== (other);
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_BorderSize.cpp ***/


/*** Start of inlined file: juce_Line.cpp ***/
BEGIN_JUCE_NAMESPACE

static bool juce_lineIntersection (const float x1, const float y1,
								   const float x2, const float y2,
								   const float x3, const float y3,
								   const float x4, const float y4,
								   float& intersectionX,
								   float& intersectionY) throw()
{
	if (x2 != x3 || y2 != y3)
	{
		const float dx1 = x2 - x1;
		const float dy1 = y2 - y1;
		const float dx2 = x4 - x3;
		const float dy2 = y4 - y3;
		const float divisor = dx1 * dy2 - dx2 * dy1;

		if (divisor == 0)
		{
			if (! ((dx1 == 0 && dy1 == 0) || (dx2 == 0 && dy2 == 0)))
			{
				if (dy1 == 0 && dy2 != 0)
				{
					const float along = (y1 - y3) / dy2;
					intersectionX = x3 + along * dx2;
					intersectionY = y1;

					return along >= 0 && along <= 1.0f;
				}
				else if (dy2 == 0 && dy1 != 0)
				{
					const float along = (y3 - y1) / dy1;
					intersectionX = x1 + along * dx1;
					intersectionY = y3;

					return along >= 0 && along <= 1.0f;
				}
				else if (dx1 == 0 && dx2 != 0)
				{
					const float along = (x1 - x3) / dx2;
					intersectionX = x1;
					intersectionY = y3 + along * dy2;

					return along >= 0 && along <= 1.0f;
				}
				else if (dx2 == 0 && dx1 != 0)
				{
					const float along = (x3 - x1) / dx1;
					intersectionX = x3;
					intersectionY = y1 + along * dy1;

					return along >= 0 && along <= 1.0f;
				}
			}

			intersectionX = 0.5f * (x2 + x3);
			intersectionY = 0.5f * (y2 + y3);

			return false;
		}

		const float along1 = ((y1 - y3) * dx2 - (x1 - x3) * dy2) / divisor;

		intersectionX = x1 + along1 * dx1;
		intersectionY = y1 + along1 * dy1;

		if (along1 < 0 || along1 > 1.0f)
			return false;

		const float along2 = ((y1 - y3) * dx1 - (x1 - x3) * dy1) / divisor;

		return along2 >= 0 && along2 <= 1.0f;
	}

	intersectionX = x2;
	intersectionY = y2;
	return true;
}

Line::Line() throw()
	: startX (0.0f),
	  startY (0.0f),
	  endX (0.0f),
	  endY (0.0f)
{
}

Line::Line (const Line& other) throw()
	: startX (other.startX),
	  startY (other.startY),
	  endX (other.endX),
	  endY (other.endY)
{
}

Line::Line (const float startX_, const float startY_,
			const float endX_, const float endY_) throw()
	: startX (startX_),
	  startY (startY_),
	  endX (endX_),
	  endY (endY_)
{
}

Line::Line (const Point<float>& start,
			const Point<float>& end) throw()
	: startX (start.getX()),
	  startY (start.getY()),
	  endX (end.getX()),
	  endY (end.getY())
{
}

Line& Line::operator= (const Line& other) throw()
{
	startX = other.startX;
	startY = other.startY;
	endX = other.endX;
	endY = other.endY;

	return *this;
}

Line::~Line() throw()
{
}

const Point<float> Line::getStart() const throw()
{
	return Point<float> (startX, startY);
}

const Point<float> Line::getEnd() const throw()
{
	return Point<float> (endX, endY);
}

void Line::setStart (const float newStartX,
					 const float newStartY) throw()
{
	startX = newStartX;
	startY = newStartY;
}

void Line::setStart (const Point<float>& newStart) throw()
{
	startX = newStart.getX();
	startY = newStart.getY();
}

void Line::setEnd (const float newEndX,
				   const float newEndY) throw()
{
	endX = newEndX;
	endY = newEndY;
}

void Line::setEnd (const Point<float>& newEnd) throw()
{
	endX = newEnd.getX();
	endY = newEnd.getY();
}

bool Line::operator== (const Line& other) const throw()
{
	return startX == other.startX
			&& startY == other.startY
			&& endX == other.endX
			&& endY == other.endY;
}

bool Line::operator!= (const Line& other) const throw()
{
	return startX != other.startX
			|| startY != other.startY
			|| endX != other.endX
			|| endY != other.endY;
}

void Line::applyTransform (const AffineTransform& transform) throw()
{
	transform.transformPoint (startX, startY);
	transform.transformPoint (endX, endY);
}

float Line::getLength() const throw()
{
	return (float) juce_hypot (startX - endX,
							   startY - endY);
}

float Line::getAngle() const throw()
{
	return atan2f (endX - startX,
				   endY - startY);
}

const Point<float> Line::getPointAlongLine (const float distanceFromStart) const throw()
{
	const float alpha = distanceFromStart / getLength();

	return Point<float> (startX + (endX - startX) * alpha,
						 startY + (endY - startY) * alpha);
}

const Point<float> Line::getPointAlongLine (const float offsetX,
											const float offsetY) const throw()
{
	const float dx = endX - startX;
	const float dy = endY - startY;
	const double length = juce_hypot (dx, dy);

	if (length == 0)
		return Point<float> (startX, startY);
	else
		return Point<float> (startX + (float) (((dx * offsetX) - (dy * offsetY)) / length),
							 startY + (float) (((dy * offsetX) + (dx * offsetY)) / length));
}

const Point<float> Line::getPointAlongLineProportionally (const float alpha) const throw()
{
	return Point<float> (startX + (endX - startX) * alpha,
						 startY + (endY - startY) * alpha);
}

float Line::getDistanceFromLine (const float x,
								 const float y) const throw()
{
	const double dx = endX - startX;
	const double dy = endY - startY;
	const double length = dx * dx + dy * dy;

	if (length > 0)
	{
		const double prop = ((x - startX) * dx + (y - startY) * dy) / length;

		if (prop >= 0.0f && prop < 1.0f)
		{
			return (float) juce_hypot (x - (startX + prop * dx),
									   y - (startY + prop * dy));
		}
	}

	return (float) jmin (juce_hypot (x - startX, y - startY),
						 juce_hypot (x - endX, y - endY));
}

float Line::findNearestPointTo (const float x,
								const float y) const throw()
{
	const double dx = endX - startX;
	const double dy = endY - startY;
	const double length = dx * dx + dy * dy;

	if (length <= 0.0)
		return 0.0f;

	return jlimit (0.0f, 1.0f,
				   (float) (((x - startX) * dx + (y - startY) * dy) / length));
}

const Line Line::withShortenedStart (const float distanceToShortenBy) const throw()
{
	const float length = getLength();

	return Line (getPointAlongLine (jmin (distanceToShortenBy, length)),
				 getEnd());
}

const Line Line::withShortenedEnd (const float distanceToShortenBy) const throw()
{
	const float length = getLength();

	return Line (getStart(),
				 getPointAlongLine (length - jmin (distanceToShortenBy, length)));
}

bool Line::clipToPath (const Path& path,
					   const bool keepSectionOutsidePath) throw()
{
	const bool startInside = path.contains (startX, startY);
	const bool endInside = path.contains (endX, endY);

	if (startInside == endInside)
	{
		if (keepSectionOutsidePath != startInside)
		{
			// entirely outside the path
			return false;
		}
		else
		{
			// entirely inside the path
			startX = 0.0f;
			startY = 0.0f;
			endX = 0.0f;
			endY = 0.0f;

			return true;
		}
	}
	else
	{
		bool changed = false;
		PathFlatteningIterator iter (path, AffineTransform::identity);

		while (iter.next())
		{
			float ix, iy;

			if (intersects (Line (iter.x1, iter.y1,
								  iter.x2, iter.y2),
							ix, iy))
			{
				if ((startInside && keepSectionOutsidePath)
					 || (endInside && ! keepSectionOutsidePath))
				{
					setStart (ix, iy);
				}
				else
				{
					setEnd (ix, iy);
				}

				changed = true;
			}
		}

		return changed;
	}
}

bool Line::intersects (const Line& line,
					   float& intersectionX,
					   float& intersectionY) const throw()
{
	return juce_lineIntersection (startX, startY,
								  endX, endY,
								  line.startX, line.startY,
								  line.endX, line.endY,
								  intersectionX,
								  intersectionY);
}

bool Line::isVertical() const throw()
{
	return startX == endX;
}

bool Line::isHorizontal() const throw()
{
	return startY == endY;
}

bool Line::isPointAbove (const float x, const float y) const throw()
{
	return startX != endX
			&& y < ((endY - startY) * (x - startX)) / (endX - startX) + startY;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Line.cpp ***/


/*** Start of inlined file: juce_Path.cpp ***/
BEGIN_JUCE_NAMESPACE

// tests that some co-ords aren't NaNs
#define CHECK_COORDS_ARE_VALID(x, y) \
	jassert (x == x && y == y);

namespace PathHelpers
{
	static const float ellipseAngularIncrement = 0.05f;

	static void perpendicularOffset (const float x1, const float y1,
									 const float x2, const float y2,
									 const float offsetX, const float offsetY,
									 float& resultX, float& resultY) throw()
	{
		const float dx = x2 - x1;
		const float dy = y2 - y1;
		const float len = juce_hypotf (dx, dy);

		if (len == 0)
		{
			resultX = x1;
			resultY = y1;
		}
		else
		{
			resultX = x1 + ((dx * offsetX) - (dy * offsetY)) / len;
			resultY = y1 + ((dy * offsetX) + (dx * offsetY)) / len;
		}
	}

	static const String nextToken (const juce_wchar*& t)
	{
		while (CharacterFunctions::isWhitespace (*t))
			++t;

		const juce_wchar* const start = t;

		while (*t != 0 && ! CharacterFunctions::isWhitespace (*t))
			++t;

		const int length = (int) (t - start);

		while (CharacterFunctions::isWhitespace (*t))
			++t;

		return String (start, length);
	}
}

const float Path::lineMarker	   = 100001.0f;
const float Path::moveMarker	   = 100002.0f;
const float Path::quadMarker	   = 100003.0f;
const float Path::cubicMarker	  = 100004.0f;
const float Path::closeSubPathMarker   = 100005.0f;

Path::Path()
	: numElements (0),
	  pathXMin (0),
	  pathXMax (0),
	  pathYMin (0),
	  pathYMax (0),
	  useNonZeroWinding (true)
{
}

Path::~Path()
{
}

Path::Path (const Path& other)
	: numElements (other.numElements),
	  pathXMin (other.pathXMin),
	  pathXMax (other.pathXMax),
	  pathYMin (other.pathYMin),
	  pathYMax (other.pathYMax),
	  useNonZeroWinding (other.useNonZeroWinding)
{
	if (numElements > 0)
	{
		data.setAllocatedSize ((int) numElements);
		memcpy (data.elements, other.data.elements, numElements * sizeof (float));
	}
}

Path& Path::operator= (const Path& other)
{
	if (this != &other)
	{
		data.ensureAllocatedSize ((int) other.numElements);

		numElements = other.numElements;
		pathXMin = other.pathXMin;
		pathXMax = other.pathXMax;
		pathYMin = other.pathYMin;
		pathYMax = other.pathYMax;
		useNonZeroWinding = other.useNonZeroWinding;

		if (numElements > 0)
			memcpy (data.elements, other.data.elements, numElements * sizeof (float));
	}

	return *this;
}

void Path::clear() throw()
{
	numElements = 0;
	pathXMin = 0;
	pathYMin = 0;
	pathYMax = 0;
	pathXMax = 0;
}

void Path::swapWithPath (Path& other)
{
	data.swapWith (other.data);
	swapVariables <size_t> (numElements, other.numElements);
	swapVariables <float> (pathXMin, other.pathXMin);
	swapVariables <float> (pathXMax, other.pathXMax);
	swapVariables <float> (pathYMin, other.pathYMin);
	swapVariables <float> (pathYMax, other.pathYMax);
	swapVariables <bool> (useNonZeroWinding, other.useNonZeroWinding);
}

void Path::setUsingNonZeroWinding (const bool isNonZero) throw()
{
	useNonZeroWinding = isNonZero;
}

void Path::scaleToFit (const float x, const float y, const float w, const float h,
					   const bool preserveProportions) throw()
{
	applyTransform (getTransformToScaleToFit (x, y, w, h, preserveProportions));
}

bool Path::isEmpty() const throw()
{
	size_t i = 0;

	while (i < numElements)
	{
		const float type = data.elements [i++];

		if (type == moveMarker)
		{
			i += 2;
		}
		else if (type == lineMarker
				 || type == quadMarker
				 || type == cubicMarker)
		{
			return false;
		}
	}

	return true;
}

const Rectangle<float> Path::getBounds() const throw()
{
	return Rectangle<float> (pathXMin, pathYMin,
							 pathXMax - pathXMin,
							 pathYMax - pathYMin);
}

const Rectangle<float> Path::getBoundsTransformed (const AffineTransform& transform) const throw()
{
	return getBounds().transformed (transform);
}

void Path::startNewSubPath (const float x, const float y)
{
	CHECK_COORDS_ARE_VALID (x, y);

	if (numElements == 0)
	{
		pathXMin = pathXMax = x;
		pathYMin = pathYMax = y;
	}
	else
	{
		pathXMin = jmin (pathXMin, x);
		pathXMax = jmax (pathXMax, x);
		pathYMin = jmin (pathYMin, y);
		pathYMax = jmax (pathYMax, y);
	}

	data.ensureAllocatedSize ((int) numElements + 3);

	data.elements [numElements++] = moveMarker;
	data.elements [numElements++] = x;
	data.elements [numElements++] = y;
}

void Path::lineTo (const float x, const float y)
{
	CHECK_COORDS_ARE_VALID (x, y);

	if (numElements == 0)
		startNewSubPath (0, 0);

	data.ensureAllocatedSize ((int) numElements + 3);

	data.elements [numElements++] = lineMarker;
	data.elements [numElements++] = x;
	data.elements [numElements++] = y;

	pathXMin = jmin (pathXMin, x);
	pathXMax = jmax (pathXMax, x);
	pathYMin = jmin (pathYMin, y);
	pathYMax = jmax (pathYMax, y);
}

void Path::quadraticTo (const float x1, const float y1,
						const float x2, const float y2)
{
	CHECK_COORDS_ARE_VALID (x1, y1);
	CHECK_COORDS_ARE_VALID (x2, y2);

	if (numElements == 0)
		startNewSubPath (0, 0);

	data.ensureAllocatedSize ((int) numElements + 5);

	data.elements [numElements++] = quadMarker;
	data.elements [numElements++] = x1;
	data.elements [numElements++] = y1;
	data.elements [numElements++] = x2;
	data.elements [numElements++] = y2;

	pathXMin = jmin (pathXMin, x1, x2);
	pathXMax = jmax (pathXMax, x1, x2);
	pathYMin = jmin (pathYMin, y1, y2);
	pathYMax = jmax (pathYMax, y1, y2);
}

void Path::cubicTo (const float x1, const float y1,
					const float x2, const float y2,
					const float x3, const float y3)
{
	CHECK_COORDS_ARE_VALID (x1, y1);
	CHECK_COORDS_ARE_VALID (x2, y2);
	CHECK_COORDS_ARE_VALID (x3, y3);

	if (numElements == 0)
		startNewSubPath (0, 0);

	data.ensureAllocatedSize ((int) numElements + 7);

	data.elements [numElements++] = cubicMarker;
	data.elements [numElements++] = x1;
	data.elements [numElements++] = y1;
	data.elements [numElements++] = x2;
	data.elements [numElements++] = y2;
	data.elements [numElements++] = x3;
	data.elements [numElements++] = y3;

	pathXMin = jmin (pathXMin, x1, x2, x3);
	pathXMax = jmax (pathXMax, x1, x2, x3);
	pathYMin = jmin (pathYMin, y1, y2, y3);
	pathYMax = jmax (pathYMax, y1, y2, y3);
}

void Path::closeSubPath()
{
	if (numElements > 0
		 && data.elements [numElements - 1] != closeSubPathMarker)
	{
		data.ensureAllocatedSize ((int) numElements + 1);
		data.elements [numElements++] = closeSubPathMarker;
	}
}

const Point<float> Path::getCurrentPosition() const
{
	size_t i = numElements - 1;

	if (i > 0 && data.elements[i] == closeSubPathMarker)
	{
		while (i >= 0)
		{
			if (data.elements[i] == moveMarker)
			{
				i += 2;
				break;
			}

			--i;
		}
	}

	if (i > 0)
		return Point<float> (data.elements [i - 1], data.elements [i]);

	return Point<float>();
}

void Path::addRectangle (const float x, const float y,
						 const float w, const float h)
{
	float x1 = x, y1 = y, x2 = x + w, y2 = y + h;

	if (w < 0)
		swapVariables (x1, x2);

	if (h < 0)
		swapVariables (y1, y2);

	data.ensureAllocatedSize ((int) numElements + 13);

	if (numElements == 0)
	{
		pathXMin = x1;
		pathXMax = x2;
		pathYMin = y1;
		pathYMax = y2;
	}
	else
	{
		pathXMin = jmin (pathXMin, x1);
		pathXMax = jmax (pathXMax, x2);
		pathYMin = jmin (pathYMin, y1);
		pathYMax = jmax (pathYMax, y2);
	}

	data.elements [numElements++] = moveMarker;
	data.elements [numElements++] = x1;
	data.elements [numElements++] = y2;
	data.elements [numElements++] = lineMarker;
	data.elements [numElements++] = x1;
	data.elements [numElements++] = y1;
	data.elements [numElements++] = lineMarker;
	data.elements [numElements++] = x2;
	data.elements [numElements++] = y1;
	data.elements [numElements++] = lineMarker;
	data.elements [numElements++] = x2;
	data.elements [numElements++] = y2;
	data.elements [numElements++] = closeSubPathMarker;
}

void Path::addRectangle (const Rectangle<int>& rectangle)
{
	addRectangle ((float) rectangle.getX(), (float) rectangle.getY(),
				  (float) rectangle.getWidth(), (float) rectangle.getHeight());
}

void Path::addRoundedRectangle (const float x, const float y,
								const float w, const float h,
								float csx,
								float csy)
{
	csx = jmin (csx, w * 0.5f);
	csy = jmin (csy, h * 0.5f);
	const float cs45x = csx * 0.45f;
	const float cs45y = csy * 0.45f;
	const float x2 = x + w;
	const float y2 = y + h;

	startNewSubPath (x + csx, y);
	lineTo (x2 - csx, y);
	cubicTo (x2 - cs45x, y, x2, y + cs45y, x2, y + csy);
	lineTo (x2, y2 - csy);
	cubicTo (x2, y2 - cs45y, x2 - cs45x, y2, x2 - csx, y2);
	lineTo (x + csx, y2);
	cubicTo (x + cs45x, y2, x, y2 - cs45y, x, y2 - csy);
	lineTo (x, y + csy);
	cubicTo (x, y + cs45y, x + cs45x, y, x + csx, y);
	closeSubPath();
}

void Path::addRoundedRectangle (const float x, const float y,
								const float w, const float h,
								float cs)
{
	addRoundedRectangle (x, y, w, h, cs, cs);
}

void Path::addTriangle (const float x1, const float y1,
						const float x2, const float y2,
						const float x3, const float y3)
{
	startNewSubPath (x1, y1);
	lineTo (x2, y2);
	lineTo (x3, y3);
	closeSubPath();
}

void Path::addQuadrilateral (const float x1, const float y1,
							 const float x2, const float y2,
							 const float x3, const float y3,
							 const float x4, const float y4)
{
	startNewSubPath (x1, y1);
	lineTo (x2, y2);
	lineTo (x3, y3);
	lineTo (x4, y4);
	closeSubPath();
}

void Path::addEllipse (const float x, const float y,
					   const float w, const float h)
{
	const float hw = w * 0.5f;
	const float hw55 = hw * 0.55f;
	const float hh = h * 0.5f;
	const float hh45 = hh * 0.55f;
	const float cx = x + hw;
	const float cy = y + hh;

	startNewSubPath (cx, cy - hh);
	cubicTo (cx + hw55, cy - hh, cx + hw, cy - hh45, cx + hw, cy);
	cubicTo (cx + hw, cy + hh45, cx + hw55, cy + hh, cx, cy + hh);
	cubicTo (cx - hw55, cy + hh, cx - hw, cy + hh45, cx - hw, cy);
	cubicTo (cx - hw, cy - hh45, cx - hw55, cy - hh, cx, cy - hh);
	closeSubPath();
}

void Path::addArc (const float x, const float y,
				   const float w, const float h,
				   const float fromRadians,
				   const float toRadians,
				   const bool startAsNewSubPath)
{
	const float radiusX = w / 2.0f;
	const float radiusY = h / 2.0f;

	addCentredArc (x + radiusX,
				   y + radiusY,
				   radiusX, radiusY,
				   0.0f,
				   fromRadians, toRadians,
				   startAsNewSubPath);
}

void Path::addCentredArc (const float centreX, const float centreY,
						  const float radiusX, const float radiusY,
						  const float rotationOfEllipse,
						  const float fromRadians,
						  const float toRadians,
						  const bool startAsNewSubPath)
{
	if (radiusX > 0.0f && radiusY > 0.0f)
	{
		const AffineTransform rotation (AffineTransform::rotation (rotationOfEllipse, centreX, centreY));
		float angle = fromRadians;

		if (startAsNewSubPath)
		{
			float x = centreX + radiusX * sinf (angle);
			float y = centreY - radiusY * cosf (angle);

			if (rotationOfEllipse != 0)
				rotation.transformPoint (x, y);

			startNewSubPath (x, y);
		}

		if (fromRadians < toRadians)
		{
			if (startAsNewSubPath)
				angle += PathHelpers::ellipseAngularIncrement;

			while (angle < toRadians)
			{
				float x = centreX + radiusX * sinf (angle);
				float y = centreY - radiusY * cosf (angle);

				if (rotationOfEllipse != 0)
					rotation.transformPoint (x, y);

				lineTo (x, y);

				angle += PathHelpers::ellipseAngularIncrement;
			}
		}
		else
		{
			if (startAsNewSubPath)
				angle -= PathHelpers::ellipseAngularIncrement;

			while (angle > toRadians)
			{
				float x = centreX + radiusX * sinf (angle);
				float y = centreY - radiusY * cosf (angle);

				if (rotationOfEllipse != 0)
					rotation.transformPoint (x, y);

				lineTo (x, y);

				angle -= PathHelpers::ellipseAngularIncrement;
			}
		}

		float x = centreX + radiusX * sinf (toRadians);
		float y = centreY - radiusY * cosf (toRadians);

		if (rotationOfEllipse != 0)
			rotation.transformPoint (x, y);

		lineTo (x, y);
	}
}

void Path::addPieSegment (const float x, const float y,
						  const float width, const float height,
						  const float fromRadians,
						  const float toRadians,
						  const float innerCircleProportionalSize)
{
	float hw = width * 0.5f;
	float hh = height * 0.5f;
	const float centreX = x + hw;
	const float centreY = y + hh;

	startNewSubPath (centreX + hw * sinf (fromRadians),
					 centreY - hh * cosf (fromRadians));

	addArc (x, y, width, height, fromRadians, toRadians);

	if (fabs (fromRadians - toRadians) > float_Pi * 1.999f)
	{
		closeSubPath();

		if (innerCircleProportionalSize > 0)
		{
			hw *= innerCircleProportionalSize;
			hh *= innerCircleProportionalSize;

			startNewSubPath (centreX + hw * sinf (toRadians),
							 centreY - hh * cosf (toRadians));

			addArc (centreX - hw, centreY - hh, hw * 2.0f, hh * 2.0f,
					toRadians, fromRadians);
		}
	}
	else
	{
		if (innerCircleProportionalSize > 0)
		{
			hw *= innerCircleProportionalSize;
			hh *= innerCircleProportionalSize;

			addArc (centreX - hw, centreY - hh, hw * 2.0f, hh * 2.0f,
					toRadians, fromRadians);
		}
		else
		{
			lineTo (centreX, centreY);
		}
	}

	closeSubPath();
}

void Path::addLineSegment (const float startX, const float startY,
						   const float endX, const float endY,
						   float lineThickness)
{
	lineThickness *= 0.5f;

	float x, y;

	PathHelpers::perpendicularOffset (startX, startY, endX, endY,
									  0, lineThickness, x, y);
	startNewSubPath (x, y);

	PathHelpers::perpendicularOffset (startX, startY, endX, endY,
									  0, -lineThickness, x, y);
	lineTo (x, y);

	PathHelpers::perpendicularOffset (endX, endY, startX, startY,
									  0, lineThickness, x, y);
	lineTo (x, y);

	PathHelpers::perpendicularOffset (endX, endY, startX, startY,
									  0, -lineThickness, x, y);
	lineTo (x, y);

	closeSubPath();
}

void Path::addArrow (const float startX, const float startY,
					 const float endX, const float endY,
					 float lineThickness,
					 float arrowheadWidth,
					 float arrowheadLength)
{
	lineThickness *= 0.5f;
	arrowheadWidth *= 0.5f;
	arrowheadLength = jmin (arrowheadLength, 0.8f * juce_hypotf (startX - endX,
																 startY - endY));

	float x, y;

	PathHelpers::perpendicularOffset (startX, startY, endX, endY,
									  0, lineThickness, x, y);
	startNewSubPath (x, y);

	PathHelpers::perpendicularOffset (startX, startY, endX, endY,
									  0, -lineThickness, x, y);
	lineTo (x, y);

	PathHelpers::perpendicularOffset (endX, endY, startX, startY,
									  arrowheadLength, lineThickness, x, y);
	lineTo (x, y);

	PathHelpers::perpendicularOffset (endX, endY, startX, startY,
									  arrowheadLength, arrowheadWidth, x, y);
	lineTo (x, y);

	PathHelpers::perpendicularOffset (endX, endY, startX, startY,
									  0, 0, x, y);
	lineTo (x, y);

	PathHelpers::perpendicularOffset (endX, endY, startX, startY,
									  arrowheadLength, -arrowheadWidth, x, y);
	lineTo (x, y);

	PathHelpers::perpendicularOffset (endX, endY, startX, startY,
									  arrowheadLength, -lineThickness, x, y);
	lineTo (x, y);

	closeSubPath();
}

void Path::addStar (const float centreX,
					const float centreY,
					const int numberOfPoints,
					const float innerRadius,
					const float outerRadius,
					const float startAngle)
{
	jassert (numberOfPoints > 1); // this would be silly.

	if (numberOfPoints > 1)
	{
		const float angleBetweenPoints = float_Pi * 2.0f / numberOfPoints;

		for (int i = 0; i < numberOfPoints; ++i)
		{
			float angle = startAngle + i * angleBetweenPoints;

			const float x = centreX + outerRadius * sinf (angle);
			const float y = centreY - outerRadius * cosf (angle);

			if (i == 0)
				startNewSubPath (x, y);
			else
				lineTo (x, y);

			angle += angleBetweenPoints * 0.5f;

			lineTo (centreX + innerRadius * sinf (angle),
					centreY - innerRadius * cosf (angle));
		}

		closeSubPath();
	}
}

void Path::addBubble (float x, float y,
					  float w, float h,
					  float cs,
					  float tipX,
					  float tipY,
					  int whichSide,
					  float arrowPos,
					  float arrowWidth)
{
	if (w > 1.0f && h > 1.0f)
	{
		cs = jmin (cs, w * 0.5f, h * 0.5f);
		const float cs2 = 2.0f * cs;

		startNewSubPath (x + cs, y);

		if (whichSide == 0)
		{
			const float halfArrowW = jmin (arrowWidth, w - cs2) * 0.5f;
			const float arrowX1 = x + cs + jmax (0.0f, (w - cs2) * arrowPos - halfArrowW);
			lineTo (arrowX1, y);
			lineTo (tipX, tipY);
			lineTo (arrowX1 + halfArrowW * 2.0f, y);
		}

		lineTo (x + w - cs, y);

		if (cs > 0.0f)
			addArc (x + w - cs2, y, cs2, cs2, 0, float_Pi * 0.5f);

		if (whichSide == 3)
		{
			const float halfArrowH = jmin (arrowWidth, h - cs2) * 0.5f;
			const float arrowY1 = y + cs + jmax (0.0f, (h - cs2) * arrowPos - halfArrowH);
			lineTo (x + w, arrowY1);
			lineTo (tipX, tipY);
			lineTo (x + w, arrowY1 + halfArrowH * 2.0f);
		}

		lineTo (x + w, y + h - cs);

		if (cs > 0.0f)
			addArc (x + w - cs2, y + h - cs2, cs2, cs2, float_Pi * 0.5f, float_Pi);

		if (whichSide == 2)
		{
			const float halfArrowW = jmin (arrowWidth, w - cs2) * 0.5f;
			const float arrowX1 = x + cs + jmax (0.0f, (w - cs2) * arrowPos - halfArrowW);
			lineTo (arrowX1 + halfArrowW * 2.0f, y + h);
			lineTo (tipX, tipY);
			lineTo (arrowX1, y + h);
		}

		lineTo (x + cs, y + h);

		if (cs > 0.0f)
			addArc (x, y + h - cs2, cs2, cs2, float_Pi, float_Pi * 1.5f);

		if (whichSide == 1)
		{
			const float halfArrowH = jmin (arrowWidth, h - cs2) * 0.5f;
			const float arrowY1 = y + cs + jmax (0.0f, (h - cs2) * arrowPos - halfArrowH);
			lineTo (x, arrowY1 + halfArrowH * 2.0f);
			lineTo (tipX, tipY);
			lineTo (x, arrowY1);
		}

		lineTo (x, y + cs);

		if (cs > 0.0f)
			addArc (x, y, cs2, cs2, float_Pi * 1.5f, float_Pi * 2.0f - PathHelpers::ellipseAngularIncrement);

		closeSubPath();
	}
}

void Path::addPath (const Path& other)
{
	size_t i = 0;

	while (i < other.numElements)
	{
		const float type = other.data.elements [i++];

		if (type == moveMarker)
		{
			startNewSubPath (other.data.elements [i],
							 other.data.elements [i + 1]);

			i += 2;
		}
		else if (type == lineMarker)
		{
			lineTo (other.data.elements [i],
					other.data.elements [i + 1]);

			i += 2;
		}
		else if (type == quadMarker)
		{
			quadraticTo (other.data.elements [i],
						 other.data.elements [i + 1],
						 other.data.elements [i + 2],
						 other.data.elements [i + 3]);
			i += 4;
		}
		else if (type == cubicMarker)
		{
			cubicTo (other.data.elements [i],
					 other.data.elements [i + 1],
					 other.data.elements [i + 2],
					 other.data.elements [i + 3],
					 other.data.elements [i + 4],
					 other.data.elements [i + 5]);

			i += 6;
		}
		else if (type == closeSubPathMarker)
		{
			closeSubPath();
		}
		else
		{
			// something's gone wrong with the element list!
			jassertfalse
		}
	}
}

void Path::addPath (const Path& other,
					const AffineTransform& transformToApply)
{
	size_t i = 0;

	while (i < other.numElements)
	{
		const float type = other.data.elements [i++];

		if (type == closeSubPathMarker)
		{
			closeSubPath();
		}
		else
		{
			float x = other.data.elements [i++];
			float y = other.data.elements [i++];
			transformToApply.transformPoint (x, y);

			if (type == moveMarker)
			{
				startNewSubPath (x, y);
			}
			else if (type == lineMarker)
			{
				lineTo (x, y);
			}
			else if (type == quadMarker)
			{
				float x2 = other.data.elements [i++];
				float y2 = other.data.elements [i++];
				transformToApply.transformPoint (x2, y2);

				quadraticTo (x, y, x2, y2);
			}
			else if (type == cubicMarker)
			{
				float x2 = other.data.elements [i++];
				float y2 = other.data.elements [i++];
				float x3 = other.data.elements [i++];
				float y3 = other.data.elements [i++];
				transformToApply.transformPoint (x2, y2);
				transformToApply.transformPoint (x3, y3);

				cubicTo (x, y, x2, y2, x3, y3);
			}
			else
			{
				// something's gone wrong with the element list!
				jassertfalse
			}
		}
	}
}

void Path::applyTransform (const AffineTransform& transform) throw()
{
	size_t i = 0;
	pathYMin = pathXMin = 0;
	pathYMax = pathXMax = 0;
	bool setMaxMin = false;

	while (i < numElements)
	{
		const float type = data.elements [i++];

		if (type == moveMarker)
		{
			transform.transformPoint (data.elements [i],
									  data.elements [i + 1]);

			if (setMaxMin)
			{
				pathXMin = jmin (pathXMin, data.elements [i]);
				pathXMax = jmax (pathXMax, data.elements [i]);
				pathYMin = jmin (pathYMin, data.elements [i + 1]);
				pathYMax = jmax (pathYMax, data.elements [i + 1]);
			}
			else
			{
				pathXMin = pathXMax = data.elements [i];
				pathYMin = pathYMax = data.elements [i + 1];
				setMaxMin = true;
			}

			i += 2;
		}
		else if (type == lineMarker)
		{
			transform.transformPoint (data.elements [i],
									  data.elements [i + 1]);

			pathXMin = jmin (pathXMin, data.elements [i]);
			pathXMax = jmax (pathXMax, data.elements [i]);
			pathYMin = jmin (pathYMin, data.elements [i + 1]);
			pathYMax = jmax (pathYMax, data.elements [i + 1]);

			i += 2;
		}
		else if (type == quadMarker)
		{
			transform.transformPoint (data.elements [i],
									  data.elements [i + 1]);

			transform.transformPoint (data.elements [i + 2],
									  data.elements [i + 3]);

			pathXMin = jmin (pathXMin, data.elements [i], data.elements [i + 2]);
			pathXMax = jmax (pathXMax, data.elements [i], data.elements [i + 2]);
			pathYMin = jmin (pathYMin, data.elements [i + 1], data.elements [i + 3]);
			pathYMax = jmax (pathYMax, data.elements [i + 1], data.elements [i + 3]);

			i += 4;
		}
		else if (type == cubicMarker)
		{
			transform.transformPoint (data.elements [i],
									  data.elements [i + 1]);

			transform.transformPoint (data.elements [i + 2],
									  data.elements [i + 3]);

			transform.transformPoint (data.elements [i + 4],
									  data.elements [i + 5]);

			pathXMin = jmin (pathXMin, data.elements [i], data.elements [i + 2], data.elements [i + 4]);
			pathXMax = jmax (pathXMax, data.elements [i], data.elements [i + 2], data.elements [i + 4]);
			pathYMin = jmin (pathYMin, data.elements [i + 1], data.elements [i + 3], data.elements [i + 5]);
			pathYMax = jmax (pathYMax, data.elements [i + 1], data.elements [i + 3], data.elements [i + 5]);

			i += 6;
		}
	}
}

const AffineTransform Path::getTransformToScaleToFit (const float x, const float y,
													  const float w, const float h,
													  const bool preserveProportions,
													  const Justification& justification) const
{
	Rectangle<float> bounds (getBounds());

	if (preserveProportions)
	{
		if (w <= 0 || h <= 0 || bounds.isEmpty())
			return AffineTransform::identity;

		float newW, newH;
		const float srcRatio = bounds.getHeight() / bounds.getWidth();

		if (srcRatio > h / w)
		{
			newW = h / srcRatio;
			newH = h;
		}
		else
		{
			newW = w;
			newH = w * srcRatio;
		}

		float newXCentre = x;
		float newYCentre = y;

		if (justification.testFlags (Justification::left))
			newXCentre += newW * 0.5f;
		else if (justification.testFlags (Justification::right))
			newXCentre += w - newW * 0.5f;
		else
			newXCentre += w * 0.5f;

		if (justification.testFlags (Justification::top))
			newYCentre += newH * 0.5f;
		else if (justification.testFlags (Justification::bottom))
			newYCentre += h - newH * 0.5f;
		else
			newYCentre += h * 0.5f;

		return AffineTransform::translation (bounds.getWidth() * -0.5f - bounds.getX(),
											 bounds.getHeight() * -0.5f - bounds.getY())
					.scaled (newW / bounds.getWidth(), newH / bounds.getHeight())
					.translated (newXCentre, newYCentre);
	}
	else
	{
		return AffineTransform::translation (-bounds.getX(), -bounds.getY())
					.scaled (w / bounds.getWidth(), h / bounds.getHeight())
					.translated (x, y);
	}
}

bool Path::contains (const float x, const float y, const float tolerence) const
{
	if (x <= pathXMin || x >= pathXMax
		 || y <= pathYMin || y >= pathYMax)
		return false;

	PathFlatteningIterator i (*this, AffineTransform::identity, tolerence);

	int positiveCrossings = 0;
	int negativeCrossings = 0;

	while (i.next())
	{
		if ((i.y1 <= y && i.y2 > y)
			 || (i.y2 <= y && i.y1 > y))
		{
			const float intersectX = i.x1 + (i.x2 - i.x1) * (y - i.y1) / (i.y2 - i.y1);

			if (intersectX <= x)
			{
				if (i.y1 < i.y2)
					++positiveCrossings;
				else
					++negativeCrossings;
			}
		}
	}

	return (useNonZeroWinding) ? (negativeCrossings != positiveCrossings)
							   : ((negativeCrossings + positiveCrossings) & 1) != 0;
}

bool Path::intersectsLine (const float x1, const float y1,
						   const float x2, const float y2,
						   const float tolerence)
{
	PathFlatteningIterator i (*this, AffineTransform::identity, tolerence);

	const Line line1 (x1, y1, x2, y2);

	while (i.next())
	{
		const Line line2 (i.x1, i.y1, i.x2, i.y2);

		float ix, iy;
		if (line1.intersects (line2, ix, iy))
			return true;
	}

	return false;
}

const Path Path::createPathWithRoundedCorners (const float cornerRadius) const
{
	if (cornerRadius <= 0.01f)
		return *this;

	size_t indexOfPathStart = 0, indexOfPathStartThis = 0;
	size_t n = 0;
	bool lastWasLine = false, firstWasLine = false;
	Path p;

	while (n < numElements)
	{
		const float type = data.elements [n++];

		if (type == moveMarker)
		{
			indexOfPathStart = p.numElements;
			indexOfPathStartThis = n - 1;
			const float x = data.elements [n++];
			const float y = data.elements [n++];
			p.startNewSubPath (x, y);
			lastWasLine = false;
			firstWasLine = (data.elements [n] == lineMarker);
		}
		else if (type == lineMarker || type == closeSubPathMarker)
		{
			float startX = 0, startY = 0, joinX = 0, joinY = 0, endX, endY;

			if (type == lineMarker)
			{
				endX = data.elements [n++];
				endY = data.elements [n++];

				if (n > 8)
				{
					startX = data.elements [n - 8];
					startY = data.elements [n - 7];
					joinX = data.elements [n - 5];
					joinY = data.elements [n - 4];
				}
			}
			else
			{
				endX = data.elements [indexOfPathStartThis + 1];
				endY = data.elements [indexOfPathStartThis + 2];

				if (n > 6)
				{
					startX = data.elements [n - 6];
					startY = data.elements [n - 5];
					joinX = data.elements [n - 3];
					joinY = data.elements [n - 2];
				}
			}

			if (lastWasLine)
			{
				const double len1 = juce_hypot (startX - joinX,
												startY - joinY);

				if (len1 > 0)
				{
					const double propNeeded = jmin (0.5, cornerRadius / len1);

					p.data.elements [p.numElements - 2] = (float) (joinX - (joinX - startX) * propNeeded);
					p.data.elements [p.numElements - 1] = (float) (joinY - (joinY - startY) * propNeeded);
				}

				const double len2 = juce_hypot (endX - joinX,
												endY - joinY);

				if (len2 > 0)
				{
					const double propNeeded = jmin (0.5, cornerRadius / len2);

					p.quadraticTo (joinX, joinY,
								   (float) (joinX + (endX - joinX) * propNeeded),
								   (float) (joinY + (endY - joinY) * propNeeded));
				}

				p.lineTo (endX, endY);
			}
			else if (type == lineMarker)
			{
				p.lineTo (endX, endY);
				lastWasLine = true;
			}

			if (type == closeSubPathMarker)
			{
				if (firstWasLine)
				{
					startX = data.elements [n - 3];
					startY = data.elements [n - 2];
					joinX = endX;
					joinY = endY;
					endX = data.elements [indexOfPathStartThis + 4];
					endY = data.elements [indexOfPathStartThis + 5];

					const double len1 = juce_hypot (startX - joinX,
													startY - joinY);

					if (len1 > 0)
					{
						const double propNeeded = jmin (0.5, cornerRadius / len1);

						p.data.elements [p.numElements - 2] = (float) (joinX - (joinX - startX) * propNeeded);
						p.data.elements [p.numElements - 1] = (float) (joinY - (joinY - startY) * propNeeded);
					}

					const double len2 = juce_hypot (endX - joinX,
													endY - joinY);

					if (len2 > 0)
					{
						const double propNeeded = jmin (0.5, cornerRadius / len2);

						endX = (float) (joinX + (endX - joinX) * propNeeded);
						endY = (float) (joinY + (endY - joinY) * propNeeded);

						p.quadraticTo (joinX, joinY, endX, endY);

						p.data.elements [indexOfPathStart + 1] = endX;
						p.data.elements [indexOfPathStart + 2] = endY;
					}
				}

				p.closeSubPath();
			}
		}
		else if (type == quadMarker)
		{
			lastWasLine = false;
			const float x1 = data.elements [n++];
			const float y1 = data.elements [n++];
			const float x2 = data.elements [n++];
			const float y2 = data.elements [n++];
			p.quadraticTo (x1, y1, x2, y2);
		}
		else if (type == cubicMarker)
		{
			lastWasLine = false;
			const float x1 = data.elements [n++];
			const float y1 = data.elements [n++];
			const float x2 = data.elements [n++];
			const float y2 = data.elements [n++];
			const float x3 = data.elements [n++];
			const float y3 = data.elements [n++];
			p.cubicTo (x1, y1, x2, y2, x3, y3);
		}
	}

	return p;
}

void Path::loadPathFromStream (InputStream& source)
{
	while (! source.isExhausted())
	{
		switch (source.readByte())
		{
		case 'm':
		{
			const float x = source.readFloat();
			const float y = source.readFloat();
			startNewSubPath (x, y);
			break;
		}

		case 'l':
		{
			const float x = source.readFloat();
			const float y = source.readFloat();
			lineTo (x, y);
			break;
		}

		case 'q':
		{
			const float x1 = source.readFloat();
			const float y1 = source.readFloat();
			const float x2 = source.readFloat();
			const float y2 = source.readFloat();
			quadraticTo (x1, y1, x2, y2);
			break;
		}

		case 'b':
		{
			const float x1 = source.readFloat();
			const float y1 = source.readFloat();
			const float x2 = source.readFloat();
			const float y2 = source.readFloat();
			const float x3 = source.readFloat();
			const float y3 = source.readFloat();
			cubicTo (x1, y1, x2, y2, x3, y3);
			break;
		}

		case 'c':
			closeSubPath();
			break;

		case 'n':
			useNonZeroWinding = true;
			break;

		case 'z':
			useNonZeroWinding = false;
			break;

		case 'e':
			return; // end of path marker

		default:
			jassertfalse // illegal char in the stream
			break;
		}
	}
}

void Path::loadPathFromData (const void* const data, const int numberOfBytes)
{
	MemoryInputStream in (data, numberOfBytes, false);
	loadPathFromStream (in);
}

void Path::writePathToStream (OutputStream& dest) const
{
	dest.writeByte (useNonZeroWinding ? 'n' : 'z');

	size_t i = 0;
	while (i < numElements)
	{
		const float type = data.elements [i++];

		if (type == moveMarker)
		{
			dest.writeByte ('m');
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
		}
		else if (type == lineMarker)
		{
			dest.writeByte ('l');
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
		}
		else if (type == quadMarker)
		{
			dest.writeByte ('q');
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
		}
		else if (type == cubicMarker)
		{
			dest.writeByte ('b');
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
			dest.writeFloat (data.elements [i++]);
		}
		else if (type == closeSubPathMarker)
		{
			dest.writeByte ('c');
		}
	}

	dest.writeByte ('e'); // marks the end-of-path
}

const String Path::toString() const
{
	MemoryOutputStream s (2048, 2048);
	if (! useNonZeroWinding)
		s << 'a';

	size_t i = 0;
	float lastMarker = 0.0f;

	while (i < numElements)
	{
		const float marker = data.elements [i++];
		char markerChar = 0;
		int numCoords = 0;

		if (marker == moveMarker)
		{
			markerChar = 'm';
			numCoords = 2;
		}
		else if (marker == lineMarker)
		{
			markerChar = 'l';
			numCoords = 2;
		}
		else if (marker == quadMarker)
		{
			markerChar = 'q';
			numCoords = 4;
		}
		else if (marker == cubicMarker)
		{
			markerChar = 'c';
			numCoords = 6;
		}
		else
		{
			jassert (marker == closeSubPathMarker);
			markerChar = 'z';
		}

		if (marker != lastMarker)
		{
			if (s.getDataSize() != 0)
				s << ' ';

			s << markerChar;
			lastMarker = marker;
		}

		while (--numCoords >= 0 && i < numElements)
		{
			String coord (data.elements [i++], 3);

			while (coord.endsWithChar ('0') && coord != "0")
				coord = coord.dropLastCharacters (1);

			if (coord.endsWithChar ('.'))
				coord = coord.dropLastCharacters (1);

			if (s.getDataSize() != 0)
				s << ' ';

			s << coord;
		}
	}

	return s.toUTF8();
}

void Path::restoreFromString (const String& stringVersion)
{
	clear();
	setUsingNonZeroWinding (true);

	const juce_wchar* t = stringVersion;
	juce_wchar marker = 'm';
	int numValues = 2;
	float values [6];

	while (*t != 0)
	{
		const String token (PathHelpers::nextToken (t));
		const juce_wchar firstChar = token[0];
		int startNum = 0;

		if (firstChar == 'm' || firstChar == 'l')
		{
			marker = firstChar;
			numValues = 2;
		}
		else if (firstChar == 'q')
		{
			marker = firstChar;
			numValues = 4;
		}
		else if (firstChar == 'c')
		{
			marker = firstChar;
			numValues = 6;
		}
		else if (firstChar == 'z')
		{
			marker = firstChar;
			numValues = 0;
		}
		else if (firstChar == 'a')
		{
			setUsingNonZeroWinding (false);
			continue;
		}
		else
		{
			++startNum;
			values [0] = token.getFloatValue();
		}

		for (int i = startNum; i < numValues; ++i)
			values [i] = PathHelpers::nextToken (t).getFloatValue();

		switch (marker)
		{
		case 'm':
			startNewSubPath (values[0], values[1]);
			break;

		case 'l':
			lineTo (values[0], values[1]);
			break;

		case 'q':
			quadraticTo (values[0], values[1],
						 values[2], values[3]);
			break;

		case 'c':
			cubicTo (values[0], values[1],
					 values[2], values[3],
					 values[4], values[5]);
			break;

		case 'z':
			closeSubPath();
			break;

		default:
			jassertfalse // illegal string format?
			break;
		}
	}
}

Path::Iterator::Iterator (const Path& path_)
	: path (path_),
	  index (0)
{
}

Path::Iterator::~Iterator()
{
}

bool Path::Iterator::next()
{
	const float* const elements = path.data.elements;

	if (index < path.numElements)
	{
		const float type = elements [index++];

		if (type == moveMarker)
		{
			elementType = startNewSubPath;
			x1 = elements [index++];
			y1 = elements [index++];
		}
		else if (type == lineMarker)
		{
			elementType = lineTo;
			x1 = elements [index++];
			y1 = elements [index++];
		}
		else if (type == quadMarker)
		{
			elementType = quadraticTo;
			x1 = elements [index++];
			y1 = elements [index++];
			x2 = elements [index++];
			y2 = elements [index++];
		}
		else if (type == cubicMarker)
		{
			elementType = cubicTo;
			x1 = elements [index++];
			y1 = elements [index++];
			x2 = elements [index++];
			y2 = elements [index++];
			x3 = elements [index++];
			y3 = elements [index++];
		}
		else if (type == closeSubPathMarker)
		{
			elementType = closePath;
		}

		return true;
	}

	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Path.cpp ***/


/*** Start of inlined file: juce_PathIterator.cpp ***/
BEGIN_JUCE_NAMESPACE

#if JUCE_MSVC
  #pragma optimize ("t", on)
#endif

PathFlatteningIterator::PathFlatteningIterator (const Path& path_,
												const AffineTransform& transform_,
												float tolerence_)
	: x2 (0),
	  y2 (0),
	  closesSubPath (false),
	  subPathIndex (-1),
	  path (path_),
	  transform (transform_),
	  points (path_.data.elements),
	  tolerence (tolerence_ * tolerence_),
	  subPathCloseX (0),
	  subPathCloseY (0),
	  isIdentityTransform (transform_.isIdentity()),
	  stackBase (32),
	  index (0),
	  stackSize (32)
{
	stackPos = stackBase;
}

PathFlatteningIterator::~PathFlatteningIterator()
{
}

bool PathFlatteningIterator::next()
{
	x1 = x2;
	y1 = y2;

	float x3 = 0;
	float y3 = 0;
	float x4 = 0;
	float y4 = 0;
	float type;

	for (;;)
	{
		if (stackPos == stackBase)
		{
			if (index >= path.numElements)
			{
				return false;
			}
			else
			{
				type = points [index++];

				if (type != Path::closeSubPathMarker)
				{
					x2 = points [index++];
					y2 = points [index++];

					if (! isIdentityTransform)
						transform.transformPoint (x2, y2);

					if (type == Path::quadMarker)
					{
						x3 = points [index++];
						y3 = points [index++];

						if (! isIdentityTransform)
							transform.transformPoint (x3, y3);
					}
					else if (type == Path::cubicMarker)
					{
						x3 = points [index++];
						y3 = points [index++];
						x4 = points [index++];
						y4 = points [index++];

						if (! isIdentityTransform)
						{
							transform.transformPoint (x3, y3);
							transform.transformPoint (x4, y4);
						}
					}
				}
			}
		}
		else
		{
			type = *--stackPos;

			if (type != Path::closeSubPathMarker)
			{
				x2 = *--stackPos;
				y2 = *--stackPos;

				if (type == Path::quadMarker)
				{
					x3 = *--stackPos;
					y3 = *--stackPos;
				}
				else if (type == Path::cubicMarker)
				{
					x3 = *--stackPos;
					y3 = *--stackPos;
					x4 = *--stackPos;
					y4 = *--stackPos;
				}
			}
		}

		if (type == Path::lineMarker)
		{
			++subPathIndex;

			closesSubPath = (stackPos == stackBase)
							 && (index < path.numElements)
							 && (points [index] == Path::closeSubPathMarker)
							 && x2 == subPathCloseX
							 && y2 == subPathCloseY;

			return true;
		}
		else if (type == Path::quadMarker)
		{
			const size_t offset = (size_t) (stackPos - stackBase);

			if (offset >= stackSize - 10)
			{
				stackSize <<= 1;
				stackBase.realloc (stackSize);
				stackPos = stackBase + offset;
			}

			const float dx1 = x1 - x2;
			const float dy1 = y1 - y2;
			const float dx2 = x2 - x3;
			const float dy2 = y2 - y3;

			const float m1x = (x1 + x2) * 0.5f;
			const float m1y = (y1 + y2) * 0.5f;
			const float m2x = (x2 + x3) * 0.5f;
			const float m2y = (y2 + y3) * 0.5f;
			const float m3x = (m1x + m2x) * 0.5f;
			const float m3y = (m1y + m2y) * 0.5f;

			if (dx1*dx1 + dy1*dy1 + dx2*dx2 + dy2*dy2 > tolerence)
			{
				*stackPos++ = y3;
				*stackPos++ = x3;
				*stackPos++ = m2y;
				*stackPos++ = m2x;
				*stackPos++ = Path::quadMarker;

				*stackPos++ = m3y;
				*stackPos++ = m3x;
				*stackPos++ = m1y;
				*stackPos++ = m1x;
				*stackPos++ = Path::quadMarker;
			}
			else
			{
				*stackPos++ = y3;
				*stackPos++ = x3;
				*stackPos++ = Path::lineMarker;

				*stackPos++ = m3y;
				*stackPos++ = m3x;
				*stackPos++ = Path::lineMarker;
			}

			jassert (stackPos < stackBase + stackSize);
		}
		else if (type == Path::cubicMarker)
		{
			const size_t offset = (size_t) (stackPos - stackBase);

			if (offset >= stackSize - 16)
			{
				stackSize <<= 1;
				stackBase.realloc (stackSize);
				stackPos = stackBase + offset;
			}

			const float dx1 = x1 - x2;
			const float dy1 = y1 - y2;
			const float dx2 = x2 - x3;
			const float dy2 = y2 - y3;
			const float dx3 = x3 - x4;
			const float dy3 = y3 - y4;

			const float m1x = (x1 + x2) * 0.5f;
			const float m1y = (y1 + y2) * 0.5f;
			const float m2x = (x3 + x2) * 0.5f;
			const float m2y = (y3 + y2) * 0.5f;
			const float m3x = (x3 + x4) * 0.5f;
			const float m3y = (y3 + y4) * 0.5f;
			const float m4x = (m1x + m2x) * 0.5f;
			const float m4y = (m1y + m2y) * 0.5f;
			const float m5x = (m3x + m2x) * 0.5f;
			const float m5y = (m3y + m2y) * 0.5f;

			if (dx1*dx1 + dy1*dy1 + dx2*dx2
				+ dy2*dy2 + dx3*dx3 + dy3*dy3 > tolerence)
			{
				*stackPos++ = y4;
				*stackPos++ = x4;
				*stackPos++ = m3y;
				*stackPos++ = m3x;
				*stackPos++ = m5y;
				*stackPos++ = m5x;
				*stackPos++ = Path::cubicMarker;

				*stackPos++ = (m4y + m5y) * 0.5f;
				*stackPos++ = (m4x + m5x) * 0.5f;
				*stackPos++ = m4y;
				*stackPos++ = m4x;
				*stackPos++ = m1y;
				*stackPos++ = m1x;
				*stackPos++ = Path::cubicMarker;
			}
			else
			{
				*stackPos++ = y4;
				*stackPos++ = x4;
				*stackPos++ = Path::lineMarker;

				*stackPos++ = m5y;
				*stackPos++ = m5x;
				*stackPos++ = Path::lineMarker;

				*stackPos++ = m4y;
				*stackPos++ = m4x;
				*stackPos++ = Path::lineMarker;
			}
		}
		else if (type == Path::closeSubPathMarker)
		{
			if (x2 != subPathCloseX || y2 != subPathCloseY)
			{
				x1 = x2;
				y1 = y2;
				x2 = subPathCloseX;
				y2 = subPathCloseY;
				closesSubPath = true;

				return true;
			}
		}
		else
		{
			jassert (type == Path::moveMarker);

			subPathIndex = -1;
			subPathCloseX = x1 = x2;
			subPathCloseY = y1 = y2;
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PathIterator.cpp ***/


/*** Start of inlined file: juce_PathStrokeType.cpp ***/
BEGIN_JUCE_NAMESPACE

PathStrokeType::PathStrokeType (const float strokeThickness,
								const JointStyle jointStyle_,
								const EndCapStyle endStyle_) throw()
	: thickness (strokeThickness),
	  jointStyle (jointStyle_),
	  endStyle (endStyle_)
{
}

PathStrokeType::PathStrokeType (const PathStrokeType& other) throw()
	: thickness (other.thickness),
	  jointStyle (other.jointStyle),
	  endStyle (other.endStyle)
{
}

PathStrokeType& PathStrokeType::operator= (const PathStrokeType& other) throw()
{
	thickness = other.thickness;
	jointStyle = other.jointStyle;
	endStyle = other.endStyle;
	return *this;
}

PathStrokeType::~PathStrokeType() throw()
{
}

bool PathStrokeType::operator== (const PathStrokeType& other) const throw()
{
	return thickness == other.thickness
		&& jointStyle == other.jointStyle
		&& endStyle == other.endStyle;
}

bool PathStrokeType::operator!= (const PathStrokeType& other) const throw()
{
	return ! operator== (other);
}

static bool lineIntersection (const float x1, const float y1,
							  const float x2, const float y2,
							  const float x3, const float y3,
							  const float x4, const float y4,
							  float& intersectionX,
							  float& intersectionY,
							  float& distanceBeyondLine1EndSquared) throw()
{
	if (x2 != x3 || y2 != y3)
	{
		const float dx1 = x2 - x1;
		const float dy1 = y2 - y1;
		const float dx2 = x4 - x3;
		const float dy2 = y4 - y3;
		const float divisor = dx1 * dy2 - dx2 * dy1;

		if (divisor == 0)
		{
			if (! ((dx1 == 0 && dy1 == 0) || (dx2 == 0 && dy2 == 0)))
			{
				if (dy1 == 0 && dy2 != 0)
				{
					const float along = (y1 - y3) / dy2;
					intersectionX = x3 + along * dx2;
					intersectionY = y1;

					distanceBeyondLine1EndSquared = intersectionX - x2;
					distanceBeyondLine1EndSquared *= distanceBeyondLine1EndSquared;
					if ((x2 > x1) == (intersectionX < x2))
						distanceBeyondLine1EndSquared = -distanceBeyondLine1EndSquared;

					return along >= 0 && along <= 1.0f;
				}
				else if (dy2 == 0 && dy1 != 0)
				{
					const float along = (y3 - y1) / dy1;
					intersectionX = x1 + along * dx1;
					intersectionY = y3;

					distanceBeyondLine1EndSquared = (along - 1.0f) * dx1;
					distanceBeyondLine1EndSquared *= distanceBeyondLine1EndSquared;
					if (along < 1.0f)
						distanceBeyondLine1EndSquared = -distanceBeyondLine1EndSquared;

					return along >= 0 && along <= 1.0f;
				}
				else if (dx1 == 0 && dx2 != 0)
				{
					const float along = (x1 - x3) / dx2;
					intersectionX = x1;
					intersectionY = y3 + along * dy2;

					distanceBeyondLine1EndSquared = intersectionY - y2;
					distanceBeyondLine1EndSquared *= distanceBeyondLine1EndSquared;

					if ((y2 > y1) == (intersectionY < y2))
						distanceBeyondLine1EndSquared = -distanceBeyondLine1EndSquared;

					return along >= 0 && along <= 1.0f;
				}
				else if (dx2 == 0 && dx1 != 0)
				{
					const float along = (x3 - x1) / dx1;
					intersectionX = x3;
					intersectionY = y1 + along * dy1;

					distanceBeyondLine1EndSquared = (along - 1.0f) * dy1;
					distanceBeyondLine1EndSquared *= distanceBeyondLine1EndSquared;
					if (along < 1.0f)
						distanceBeyondLine1EndSquared = -distanceBeyondLine1EndSquared;

					return along >= 0 && along <= 1.0f;
				}
			}

			intersectionX = 0.5f * (x2 + x3);
			intersectionY = 0.5f * (y2 + y3);

			distanceBeyondLine1EndSquared = 0.0f;
			return false;
		}
		else
		{
			const float along1 = ((y1 - y3) * dx2 - (x1 - x3) * dy2) / divisor;

			intersectionX = x1 + along1 * dx1;
			intersectionY = y1 + along1 * dy1;

			if (along1 >= 0 && along1 <= 1.0f)
			{
				const float along2 = ((y1 - y3) * dx1 - (x1 - x3) * dy1);

				if (along2 >= 0 && along2 <= divisor)
				{
					distanceBeyondLine1EndSquared = 0.0f;
					return true;
				}
			}

			distanceBeyondLine1EndSquared = along1 - 1.0f;
			distanceBeyondLine1EndSquared *= distanceBeyondLine1EndSquared;
			distanceBeyondLine1EndSquared *= (dx1 * dx1 + dy1 * dy1);

			if (along1 < 1.0f)
				distanceBeyondLine1EndSquared = -distanceBeyondLine1EndSquared;

			return false;
		}
	}

	intersectionX = x2;
	intersectionY = y2;

	distanceBeyondLine1EndSquared = 0.0f;
	return true;
}

namespace PathFunctions
{
	// part of stroke drawing stuff
	static void addEdgeAndJoint (Path& destPath,
								 const PathStrokeType::JointStyle style,
								 const float maxMiterExtensionSquared, const float width,
								 const float x1, const float y1,
								 const float x2, const float y2,
								 const float x3, const float y3,
								 const float x4, const float y4,
								 const float midX, const float midY)
	{
		if (style == PathStrokeType::beveled
			|| (x3 == x4 && y3 == y4)
			|| (x1 == x2 && y1 == y2))
		{
			destPath.lineTo (x2, y2);
			destPath.lineTo (x3, y3);
		}
		else
		{
			float jx, jy, distanceBeyondLine1EndSquared;

			// if they intersect, use this point..
			if (lineIntersection (x1, y1, x2, y2,
								  x3, y3, x4, y4,
								  jx, jy, distanceBeyondLine1EndSquared))
			{
				destPath.lineTo (jx, jy);
			}
			else
			{
				if (style == PathStrokeType::mitered)
				{
					if (distanceBeyondLine1EndSquared < maxMiterExtensionSquared
						&& distanceBeyondLine1EndSquared > 0.0f)
					{
						destPath.lineTo (jx, jy);
					}
					else
					{
						// the end sticks out too far, so just use a blunt joint
						destPath.lineTo (x2, y2);
						destPath.lineTo (x3, y3);
					}
				}
				else
				{
					// curved joints
					float angle1 = atan2f (x2 - midX, y2 - midY);
					float angle2 = atan2f (x3 - midX, y3 - midY);
					const float angleIncrement = 0.1f;

					destPath.lineTo (x2, y2);

					if (fabs (angle1 - angle2) > angleIncrement)
					{
						if (angle2 > angle1 + float_Pi
							 || (angle2 < angle1 && angle2 >= angle1 - float_Pi))
						{
							if (angle2 > angle1)
								angle2 -= float_Pi * 2.0f;

							jassert (angle1 <= angle2 + float_Pi);

							angle1 -= angleIncrement;
							while (angle1 > angle2)
							{
								destPath.lineTo (midX + width * sinf (angle1),
												 midY + width * cosf (angle1));

								angle1 -= angleIncrement;
							}
						}
						else
						{
							if (angle1 > angle2)
								angle1 -= float_Pi * 2.0f;

							jassert (angle1 >= angle2 - float_Pi);

							angle1 += angleIncrement;
							while (angle1 < angle2)
							{
								destPath.lineTo (midX + width * sinf (angle1),
												 midY + width * cosf (angle1));

								angle1 += angleIncrement;
							}
						}
					}

					destPath.lineTo (x3, y3);
				}
			}
		}
	}

	static void addLineEnd (Path& destPath,
							const PathStrokeType::EndCapStyle style,
							const float x1, const float y1,
							const float x2, const float y2,
							const float width)
	{
		if (style == PathStrokeType::butt)
		{
			destPath.lineTo (x2, y2);
		}
		else
		{
			float offx1, offy1, offx2, offy2;

			float dx = x2 - x1;
			float dy = y2 - y1;
			const float len = juce_hypotf (dx, dy);

			if (len == 0)
			{
				offx1 = offx2 = x1;
				offy1 = offy2 = y1;
			}
			else
			{
				const float offset = width / len;
				dx *= offset;
				dy *= offset;

				offx1 = x1 + dy;
				offy1 = y1 - dx;
				offx2 = x2 + dy;
				offy2 = y2 - dx;
			}

			if (style == PathStrokeType::square)
			{
				// sqaure ends
				destPath.lineTo (offx1, offy1);
				destPath.lineTo (offx2, offy2);
				destPath.lineTo (x2, y2);
			}
			else
			{
				// rounded ends
				const float midx = (offx1 + offx2) * 0.5f;
				const float midy = (offy1 + offy2) * 0.5f;

				destPath.cubicTo (x1 + (offx1 - x1) * 0.55f, y1 + (offy1 - y1) * 0.55f,
								  offx1 + (midx - offx1) * 0.45f, offy1 + (midy - offy1) * 0.45f,
								  midx, midy);

				destPath.cubicTo (midx + (offx2 - midx) * 0.55f, midy + (offy2 - midy) * 0.55f,
								  offx2 + (x2 - offx2) * 0.45f, offy2 + (y2 - offy2) * 0.45f,
								  x2, y2);
			}
		}
	}

	struct LineSection
	{
		LineSection()	   {}
		LineSection (int)	   {}

		float x1, y1, x2, y2;	  // original line
		float lx1, ly1, lx2, ly2;  // the left-hand stroke
		float rx1, ry1, rx2, ry2;  // the right-hand stroke
	};

	static void addSubPath (Path& destPath, const Array <LineSection>& subPath,
							const bool isClosed,
							const float width, const float maxMiterExtensionSquared,
							const PathStrokeType::JointStyle jointStyle, const PathStrokeType::EndCapStyle endStyle)
	{
		jassert (subPath.size() > 0);

		const LineSection& firstLine = subPath.getReference (0);

		float lastX1 = firstLine.lx1;
		float lastY1 = firstLine.ly1;
		float lastX2 = firstLine.lx2;
		float lastY2 = firstLine.ly2;

		if (isClosed)
		{
			destPath.startNewSubPath (lastX1, lastY1);
		}
		else
		{
			destPath.startNewSubPath (firstLine.rx2, firstLine.ry2);

			addLineEnd (destPath, endStyle,
						firstLine.rx2, firstLine.ry2,
						lastX1, lastY1,
						width);
		}

		int i;
		for (i = 1; i < subPath.size(); ++i)
		{
			const LineSection& l = subPath.getReference (i);

			addEdgeAndJoint (destPath, jointStyle,
							 maxMiterExtensionSquared, width,
							 lastX1, lastY1, lastX2, lastY2,
							 l.lx1, l.ly1, l.lx2, l.ly2,
							 l.x1, l.y1);

			lastX1 = l.lx1;
			lastY1 = l.ly1;
			lastX2 = l.lx2;
			lastY2 = l.ly2;
		}

		const LineSection& lastLine = subPath.getReference (subPath.size() - 1);

		if (isClosed)
		{
			const LineSection& l = subPath.getReference (0);

			addEdgeAndJoint (destPath, jointStyle,
							 maxMiterExtensionSquared, width,
							 lastX1, lastY1, lastX2, lastY2,
							 l.lx1, l.ly1, l.lx2, l.ly2,
							 l.x1, l.y1);

			destPath.closeSubPath();
			destPath.startNewSubPath (lastLine.rx1, lastLine.ry1);
		}
		else
		{
			destPath.lineTo (lastX2, lastY2);

			addLineEnd (destPath, endStyle,
						lastX2, lastY2,
						lastLine.rx1, lastLine.ry1,
						width);
		}

		lastX1 = lastLine.rx1;
		lastY1 = lastLine.ry1;
		lastX2 = lastLine.rx2;
		lastY2 = lastLine.ry2;

		for (i = subPath.size() - 1; --i >= 0;)
		{
			const LineSection& l = subPath.getReference (i);

			addEdgeAndJoint (destPath, jointStyle,
							 maxMiterExtensionSquared, width,
							 lastX1, lastY1, lastX2, lastY2,
							 l.rx1, l.ry1, l.rx2, l.ry2,
							 l.x2, l.y2);

			lastX1 = l.rx1;
			lastY1 = l.ry1;
			lastX2 = l.rx2;
			lastY2 = l.ry2;
		}

		if (isClosed)
		{
			addEdgeAndJoint (destPath, jointStyle,
							 maxMiterExtensionSquared, width,
							 lastX1, lastY1, lastX2, lastY2,
							 lastLine.rx1, lastLine.ry1, lastLine.rx2, lastLine.ry2,
							 lastLine.x2, lastLine.y2);
		}
		else
		{
			// do the last line
			destPath.lineTo (lastX2, lastY2);
		}

		destPath.closeSubPath();
	}
}

void PathStrokeType::createStrokedPath (Path& destPath,
										const Path& source,
										const AffineTransform& transform,
										const float extraAccuracy) const
{
	if (thickness <= 0)
	{
		destPath.clear();
		return;
	}

	const Path* sourcePath = &source;
	Path temp;

	if (sourcePath == &destPath)
	{
		destPath.swapWithPath (temp);
		sourcePath = &temp;
	}
	else
	{
		destPath.clear();
	}

	destPath.setUsingNonZeroWinding (true);

	const float maxMiterExtensionSquared = 9.0f * thickness * thickness;
	const float width = 0.5f * thickness;

	// Iterate the path, creating a list of the
	// left/right-hand lines along either side of it...
	PathFlatteningIterator it (*sourcePath, transform, 9.0f / extraAccuracy);

	using namespace PathFunctions;
	Array <LineSection> subPath;
	LineSection l;
	l.x1 = 0;
	l.y1 = 0;

	const float minSegmentLength = 2.0f / (extraAccuracy * extraAccuracy);

	while (it.next())
	{
		if (it.subPathIndex == 0)
		{
			if (subPath.size() > 0)
			{
				addSubPath (destPath, subPath, false, width, maxMiterExtensionSquared, jointStyle, endStyle);
				subPath.clearQuick();
			}

			l.x1 = it.x1;
			l.y1 = it.y1;
		}

		l.x2 = it.x2;
		l.y2 = it.y2;

		float dx = l.x2 - l.x1;
		float dy = l.y2 - l.y1;

		const float hypotSquared = dx*dx + dy*dy;

		if (it.closesSubPath || hypotSquared > minSegmentLength || it.isLastInSubpath())
		{
			const float len = sqrtf (hypotSquared);

			if (len == 0)
			{
				l.rx1 = l.rx2 = l.lx1 = l.lx2 = l.x1;
				l.ry1 = l.ry2 = l.ly1 = l.ly2 = l.y1;
			}
			else
			{
				const float offset = width / len;
				dx *= offset;
				dy *= offset;

				l.rx2 = l.x1 - dy;
				l.ry2 = l.y1 + dx;
				l.lx1 = l.x1 + dy;
				l.ly1 = l.y1 - dx;

				l.lx2 = l.x2 + dy;
				l.ly2 = l.y2 - dx;
				l.rx1 = l.x2 - dy;
				l.ry1 = l.y2 + dx;
			}

			subPath.add (l);

			if (it.closesSubPath)
			{
				addSubPath (destPath, subPath, true, width, maxMiterExtensionSquared, jointStyle, endStyle);
				subPath.clearQuick();
			}
			else
			{
				l.x1 = it.x2;
				l.y1 = it.y2;
			}
		}
	}

	if (subPath.size() > 0)
		addSubPath (destPath, subPath, false, width, maxMiterExtensionSquared, jointStyle, endStyle);
}

void PathStrokeType::createDashedStroke (Path& destPath,
										 const Path& sourcePath,
										 const float* dashLengths,
										 int numDashLengths,
										 const AffineTransform& transform,
										 const float extraAccuracy) const
{
	if (thickness <= 0)
		return;

	// this should really be an even number..
	jassert ((numDashLengths & 1) == 0);

	Path newDestPath;
	PathFlatteningIterator it (sourcePath, transform, 9.0f / extraAccuracy);

	bool first = true;
	int dashNum = 0;
	float pos = 0.0f, lineLen = 0.0f, lineEndPos = 0.0f;
	float dx = 0.0f, dy = 0.0f;

	for (;;)
	{
		const bool isSolid = ((dashNum & 1) == 0);
		const float dashLen = dashLengths [dashNum++ % numDashLengths];

		jassert (dashLen > 0); // must be a positive increment!
		if (dashLen <= 0)
			break;

		pos += dashLen;

		while (pos > lineEndPos)
		{
			if (! it.next())
			{
				if (isSolid && ! first)
					newDestPath.lineTo (it.x2, it.y2);

				createStrokedPath (destPath, newDestPath, AffineTransform::identity, extraAccuracy);
				return;
			}

			if (isSolid && ! first)
				newDestPath.lineTo (it.x1, it.y1);
			else
				newDestPath.startNewSubPath (it.x1, it.y1);

			dx = it.x2 - it.x1;
			dy = it.y2 - it.y1;
			lineLen = juce_hypotf (dx, dy);
			lineEndPos += lineLen;
			first = it.closesSubPath;
		}

		const float alpha = (pos - (lineEndPos - lineLen)) / lineLen;

		if (isSolid)
			newDestPath.lineTo (it.x1 + dx * alpha,
								it.y1 + dy * alpha);
		else
			newDestPath.startNewSubPath (it.x1 + dx * alpha,
										 it.y1 + dy * alpha);
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PathStrokeType.cpp ***/


/*** Start of inlined file: juce_PositionedRectangle.cpp ***/
BEGIN_JUCE_NAMESPACE

PositionedRectangle::PositionedRectangle() throw()
	: x (0.0),
	  y (0.0),
	  w (0.0),
	  h (0.0),
	  xMode (anchorAtLeftOrTop | absoluteFromParentTopLeft),
	  yMode (anchorAtLeftOrTop | absoluteFromParentTopLeft),
	  wMode (absoluteSize),
	  hMode (absoluteSize)
{
}

PositionedRectangle::PositionedRectangle (const PositionedRectangle& other) throw()
	: x (other.x),
	  y (other.y),
	  w (other.w),
	  h (other.h),
	  xMode (other.xMode),
	  yMode (other.yMode),
	  wMode (other.wMode),
	  hMode (other.hMode)
{
}

PositionedRectangle& PositionedRectangle::operator= (const PositionedRectangle& other) throw()
{
	x = other.x;
	y = other.y;
	w = other.w;
	h = other.h;
	xMode = other.xMode;
	yMode = other.yMode;
	wMode = other.wMode;
	hMode = other.hMode;

	return *this;
}

PositionedRectangle::~PositionedRectangle() throw()
{
}

bool PositionedRectangle::operator== (const PositionedRectangle& other) const throw()
{
	return x == other.x
		&& y == other.y
		&& w == other.w
		&& h == other.h
		&& xMode == other.xMode
		&& yMode == other.yMode
		&& wMode == other.wMode
		&& hMode == other.hMode;
}

bool PositionedRectangle::operator!= (const PositionedRectangle& other) const throw()
{
	return ! operator== (other);
}

PositionedRectangle::PositionedRectangle (const String& stringVersion) throw()
{
	StringArray tokens;
	tokens.addTokens (stringVersion, false);

	decodePosString (tokens [0], xMode, x);
	decodePosString (tokens [1], yMode, y);
	decodeSizeString (tokens [2], wMode, w);
	decodeSizeString (tokens [3], hMode, h);
}

const String PositionedRectangle::toString() const throw()
{
	String s;
	s.preallocateStorage (12);

	addPosDescription (s, xMode, x);
	s << ' ';
	addPosDescription (s, yMode, y);
	s << ' ';
	addSizeDescription (s, wMode, w);
	s << ' ';
	addSizeDescription (s, hMode, h);

	return s;
}

const Rectangle<int> PositionedRectangle::getRectangle (const Rectangle<int>& target) const throw()
{
	jassert (! target.isEmpty());

	double x_, y_, w_, h_;
	applyPosAndSize (x_, w_, x, w, xMode, wMode, target.getX(), target.getWidth());
	applyPosAndSize (y_, h_, y, h, yMode, hMode, target.getY(), target.getHeight());

	return Rectangle<int> (roundToInt (x_), roundToInt (y_),
						   roundToInt (w_), roundToInt (h_));
}

void PositionedRectangle::getRectangleDouble (const Rectangle<int>& target,
											  double& x_, double& y_,
											  double& w_, double& h_) const throw()
{
	jassert (! target.isEmpty());

	applyPosAndSize (x_, w_, x, w, xMode, wMode, target.getX(), target.getWidth());
	applyPosAndSize (y_, h_, y, h, yMode, hMode, target.getY(), target.getHeight());
}

void PositionedRectangle::applyToComponent (Component& comp) const throw()
{
	comp.setBounds (getRectangle (Rectangle<int> (0, 0, comp.getParentWidth(), comp.getParentHeight())));
}

void PositionedRectangle::updateFrom (const Rectangle<int>& rectangle,
									  const Rectangle<int>& target) throw()
{
	updatePosAndSize (x, w, rectangle.getX(), rectangle.getWidth(), xMode, wMode, target.getX(), target.getWidth());
	updatePosAndSize (y, h, rectangle.getY(), rectangle.getHeight(), yMode, hMode, target.getY(), target.getHeight());
}

void PositionedRectangle::updateFromDouble (const double newX, const double newY,
											const double newW, const double newH,
											const Rectangle<int>& target) throw()
{
	updatePosAndSize (x, w, newX, newW, xMode, wMode, target.getX(), target.getWidth());
	updatePosAndSize (y, h, newY, newH, yMode, hMode, target.getY(), target.getHeight());
}

void PositionedRectangle::updateFromComponent (const Component& comp) throw()
{
	if (comp.getParentComponent() == 0 && ! comp.isOnDesktop())
		updateFrom (comp.getBounds(), Rectangle<int>());
	else
		updateFrom (comp.getBounds(), Rectangle<int> (0, 0, comp.getParentWidth(), comp.getParentHeight()));
}

PositionedRectangle::AnchorPoint PositionedRectangle::getAnchorPointX() const throw()
{
	return (AnchorPoint) (xMode & (anchorAtLeftOrTop | anchorAtRightOrBottom | anchorAtCentre));
}

PositionedRectangle::PositionMode PositionedRectangle::getPositionModeX() const throw()
{
	return (PositionMode) (xMode & (absoluteFromParentTopLeft
									 | absoluteFromParentBottomRight
									 | absoluteFromParentCentre
									 | proportionOfParentSize));
}

PositionedRectangle::AnchorPoint PositionedRectangle::getAnchorPointY() const throw()
{
	return (AnchorPoint) (yMode & (anchorAtLeftOrTop | anchorAtRightOrBottom | anchorAtCentre));
}

PositionedRectangle::PositionMode PositionedRectangle::getPositionModeY() const throw()
{
	return (PositionMode) (yMode & (absoluteFromParentTopLeft
									 | absoluteFromParentBottomRight
									 | absoluteFromParentCentre
									 | proportionOfParentSize));
}

PositionedRectangle::SizeMode PositionedRectangle::getWidthMode() const throw()
{
	return (SizeMode) wMode;
}

PositionedRectangle::SizeMode PositionedRectangle::getHeightMode() const throw()
{
	return (SizeMode) hMode;
}

void PositionedRectangle::setModes (const AnchorPoint xAnchor,
									const PositionMode xMode_,
									const AnchorPoint yAnchor,
									const PositionMode yMode_,
									const SizeMode widthMode,
									const SizeMode heightMode,
									const Rectangle<int>& target) throw()
{
	if (xMode != (xAnchor | xMode_) || wMode != widthMode)
	{
		double tx, tw;
		applyPosAndSize (tx, tw, x, w, xMode, wMode, target.getX(), target.getWidth());

		xMode = (uint8) (xAnchor | xMode_);
		wMode = (uint8) widthMode;

		updatePosAndSize (x, w, tx, tw, xMode, wMode, target.getX(), target.getWidth());
	}

	if (yMode != (yAnchor | yMode_) || hMode != heightMode)
	{
		double ty, th;
		applyPosAndSize (ty, th, y, h, yMode, hMode, target.getY(), target.getHeight());

		yMode = (uint8) (yAnchor | yMode_);
		hMode = (uint8) heightMode;

		updatePosAndSize (y, h, ty, th, yMode, hMode, target.getY(), target.getHeight());
	}
}

bool PositionedRectangle::isPositionAbsolute() const throw()
{
	return xMode == absoluteFromParentTopLeft
		&& yMode == absoluteFromParentTopLeft
		&& wMode == absoluteSize
		&& hMode == absoluteSize;
}

void PositionedRectangle::addPosDescription (String& s, const uint8 mode, const double value) const throw()
{
	if ((mode & proportionOfParentSize) != 0)
	{
		s << (roundToInt (value * 100000.0) / 1000.0) << '%';
	}
	else
	{
		s << (roundToInt (value * 100.0) / 100.0);

		if ((mode & absoluteFromParentBottomRight) != 0)
			s << 'R';
		else if ((mode & absoluteFromParentCentre) != 0)
			s << 'C';
	}

	if ((mode & anchorAtRightOrBottom) != 0)
		s << 'r';
	else if ((mode & anchorAtCentre) != 0)
		s << 'c';
}

void PositionedRectangle::addSizeDescription (String& s, const uint8 mode, const double value) const throw()
{
	if (mode == proportionalSize)
		s << (roundToInt (value * 100000.0) / 1000.0) << '%';
	else if (mode == parentSizeMinusAbsolute)
		s << (roundToInt (value * 100.0) / 100.0) << 'M';
	else
		s << (roundToInt (value * 100.0) / 100.0);
}

void PositionedRectangle::decodePosString (const String& s, uint8& mode, double& value) throw()
{
	if (s.containsChar ('r'))
		mode = anchorAtRightOrBottom;
	else if (s.containsChar ('c'))
		mode = anchorAtCentre;
	else
		mode = anchorAtLeftOrTop;

	if (s.containsChar ('%'))
	{
		mode |= proportionOfParentSize;
		value = s.removeCharacters ("%rcRC").getDoubleValue() / 100.0;
	}
	else
	{
		if (s.containsChar ('R'))
			mode |= absoluteFromParentBottomRight;
		else if (s.containsChar ('C'))
			mode |= absoluteFromParentCentre;
		else
			mode |= absoluteFromParentTopLeft;

		value = s.removeCharacters ("rcRC").getDoubleValue();
	}
}

void PositionedRectangle::decodeSizeString (const String& s, uint8& mode, double& value) throw()
{
	if (s.containsChar ('%'))
	{
		mode = proportionalSize;
		value = s.upToFirstOccurrenceOf ("%", false, false).getDoubleValue() / 100.0;
	}
	else if (s.containsChar ('M'))
	{
		mode = parentSizeMinusAbsolute;
		value = s.getDoubleValue();
	}
	else
	{
		mode = absoluteSize;
		value = s.getDoubleValue();
	}
}

void PositionedRectangle::applyPosAndSize (double& xOut, double& wOut,
										   const double x_, const double w_,
										   const uint8 xMode_, const uint8 wMode_,
										   const int parentPos,
										   const int parentSize) const throw()
{
	if (wMode_ == proportionalSize)
		wOut = roundToInt (w_ * parentSize);
	else if (wMode_ == parentSizeMinusAbsolute)
		wOut = jmax (0, parentSize - roundToInt (w_));
	else
		wOut = roundToInt (w_);

	if ((xMode_ & proportionOfParentSize) != 0)
		xOut = parentPos + x_ * parentSize;
	else if ((xMode_ & absoluteFromParentBottomRight) != 0)
		xOut = (parentPos + parentSize) - x_;
	else if ((xMode_ & absoluteFromParentCentre) != 0)
		xOut = x_ + (parentPos + parentSize / 2);
	else
		xOut = x_ + parentPos;

	if ((xMode_ & anchorAtRightOrBottom) != 0)
		xOut -= wOut;
	else if ((xMode_ & anchorAtCentre) != 0)
		xOut -= wOut / 2;
}

void PositionedRectangle::updatePosAndSize (double& xOut, double& wOut,
											double x_, const double w_,
											const uint8 xMode_, const uint8 wMode_,
											const int parentPos,
											const int parentSize) const throw()
{
	if (wMode_ == proportionalSize)
	{
		if (parentSize > 0)
			wOut = w_ / parentSize;
	}
	else if (wMode_ == parentSizeMinusAbsolute)
		wOut = parentSize - w_;
	else
		wOut = w_;

	if ((xMode_ & anchorAtRightOrBottom) != 0)
		x_ += w_;
	else if ((xMode_ & anchorAtCentre) != 0)
		x_ += w_ / 2;

	if ((xMode_ & proportionOfParentSize) != 0)
	{
		if (parentSize > 0)
			xOut = (x_ - parentPos) / parentSize;
	}
	else if ((xMode_ & absoluteFromParentBottomRight) != 0)
		xOut = (parentPos + parentSize) - x_;
	else if ((xMode_ & absoluteFromParentCentre) != 0)
		xOut = x_ - (parentPos + parentSize / 2);
	else
		xOut = x_ - parentPos;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PositionedRectangle.cpp ***/


/*** Start of inlined file: juce_RectangleList.cpp ***/
BEGIN_JUCE_NAMESPACE

RectangleList::RectangleList() throw()
{
}

RectangleList::RectangleList (const Rectangle<int>& rect) throw()
{
	if (! rect.isEmpty())
		rects.add (rect);
}

RectangleList::RectangleList (const RectangleList& other) throw()
	: rects (other.rects)
{
}

RectangleList& RectangleList::operator= (const RectangleList& other) throw()
{
	rects = other.rects;
	return *this;
}

RectangleList::~RectangleList() throw()
{
}

void RectangleList::clear() throw()
{
	rects.clearQuick();
}

const Rectangle<int> RectangleList::getRectangle (const int index) const throw()
{
	if (((unsigned int) index) < (unsigned int) rects.size())
		return rects.getReference (index);

	return Rectangle<int>();
}

bool RectangleList::isEmpty() const throw()
{
	return rects.size() == 0;
}

RectangleList::Iterator::Iterator (const RectangleList& list) throw()
	: current (0),
	  owner (list),
	  index (list.rects.size())
{
}

RectangleList::Iterator::~Iterator() throw()
{
}

bool RectangleList::Iterator::next() throw()
{
	if (--index >= 0)
	{
		current = & (owner.rects.getReference (index));
		return true;
	}

	return false;
}

void RectangleList::add (const Rectangle<int>& rect) throw()
{
	if (! rect.isEmpty())
	{
		if (rects.size() == 0)
		{
			rects.add (rect);
		}
		else
		{
			bool anyOverlaps = false;

			int i;
			for (i = rects.size(); --i >= 0;)
			{
				Rectangle<int>& ourRect = rects.getReference (i);

				if (rect.intersects (ourRect))
				{
					if (rect.contains (ourRect))
						rects.remove (i);
					else if (! ourRect.reduceIfPartlyContainedIn (rect))
						anyOverlaps = true;
				}
			}

			if (anyOverlaps && rects.size() > 0)
			{
				RectangleList r (rect);

				for (i = rects.size(); --i >= 0;)
				{
					const Rectangle<int>& ourRect = rects.getReference (i);

					if (rect.intersects (ourRect))
					{
						r.subtract (ourRect);

						if (r.rects.size() == 0)
							return;
					}
				}

				for (i = r.getNumRectangles(); --i >= 0;)
					rects.add (r.rects.getReference (i));
			}
			else
			{
				rects.add (rect);
			}
		}
	}
}

void RectangleList::addWithoutMerging (const Rectangle<int>& rect) throw()
{
	rects.add (rect);
}

void RectangleList::add (const int x, const int y, const int w, const int h) throw()
{
	if (rects.size() == 0)
	{
		if (w > 0 && h > 0)
			rects.add (Rectangle<int> (x, y, w, h));
	}
	else
	{
		add (Rectangle<int> (x, y, w, h));
	}
}

void RectangleList::add (const RectangleList& other) throw()
{
	for (int i = 0; i < other.rects.size(); ++i)
		add (other.rects.getReference (i));
}

void RectangleList::subtract (const Rectangle<int>& rect) throw()
{
	const int originalNumRects = rects.size();

	if (originalNumRects > 0)
	{
		const int x1 = rect.x;
		const int y1 = rect.y;
		const int x2 = x1 + rect.w;
		const int y2 = y1 + rect.h;

		for (int i = getNumRectangles(); --i >= 0;)
		{
			Rectangle<int>& r = rects.getReference (i);

			const int rx1 = r.x;
			const int ry1 = r.y;
			const int rx2 = rx1 + r.w;
			const int ry2 = ry1 + r.h;

			if (! (x2 <= rx1 || x1 >= rx2 || y2 <= ry1 || y1 >= ry2))
			{
				if (x1 > rx1 && x1 < rx2)
				{
					if (y1 <= ry1 && y2 >= ry2 && x2 >= rx2)
					{
						r.w = x1 - rx1;
					}
					else
					{
						r.x = x1;
						r.w = rx2 - x1;

						rects.insert (i + 1, Rectangle<int> (rx1, ry1, x1 - rx1,  ry2 - ry1));
						i += 2;
					}
				}
				else if (x2 > rx1 && x2 < rx2)
				{
					r.x = x2;
					r.w = rx2 - x2;

					if (y1 > ry1 || y2 < ry2 || x1 > rx1)
					{
						rects.insert (i + 1, Rectangle<int> (rx1, ry1, x2 - rx1,  ry2 - ry1));
						i += 2;
					}
				}
				else if (y1 > ry1 && y1 < ry2)
				{
					if (x1 <= rx1 && x2 >= rx2 && y2 >= ry2)
					{
						r.h = y1 - ry1;
					}
					else
					{
						r.y = y1;
						r.h = ry2 - y1;

						rects.insert (i + 1, Rectangle<int> (rx1, ry1, rx2 - rx1, y1 - ry1));
						i += 2;
					}
				}
				else if (y2 > ry1 && y2 < ry2)
				{
					r.y = y2;
					r.h = ry2 - y2;

					if (x1 > rx1 || x2 < rx2 || y1 > ry1)
					{
						rects.insert (i + 1, Rectangle<int> (rx1, ry1, rx2 - rx1, y2 - ry1));
						i += 2;
					}
				}
				else
				{
					rects.remove (i);
				}
			}
		}

		if (rects.size() > originalNumRects + 10)
			consolidate();
	}
}

void RectangleList::subtract (const RectangleList& otherList) throw()
{
	for (int i = otherList.rects.size(); --i >= 0;)
		subtract (otherList.rects.getReference (i));
}

bool RectangleList::clipTo (const Rectangle<int>& rect) throw()
{
	bool notEmpty = false;

	if (rect.isEmpty())
	{
		clear();
	}
	else
	{
		for (int i = rects.size(); --i >= 0;)
		{
			Rectangle<int>& r = rects.getReference (i);

			if (! rect.intersectRectangle (r.x, r.y, r.w, r.h))
				rects.remove (i);
			else
				notEmpty = true;
		}
	}

	return notEmpty;
}

bool RectangleList::clipTo (const RectangleList& other) throw()
{
	if (rects.size() == 0)
		return false;

	RectangleList result;

	for (int j = 0; j < rects.size(); ++j)
	{
		const Rectangle<int>& rect = rects.getReference (j);

		for (int i = other.rects.size(); --i >= 0;)
		{
			Rectangle<int> r (other.rects.getReference (i));

			if (rect.intersectRectangle (r.x, r.y, r.w, r.h))
				result.rects.add (r);
		}
	}

	swapWith (result);

	return ! isEmpty();
}

bool RectangleList::getIntersectionWith (const Rectangle<int>& rect, RectangleList& destRegion) const throw()
{
	destRegion.clear();

	if (! rect.isEmpty())
	{
		for (int i = rects.size(); --i >= 0;)
		{
			Rectangle<int> r (rects.getReference (i));

			if (rect.intersectRectangle (r.x, r.y, r.w, r.h))
				destRegion.rects.add (r);
		}
	}

	return destRegion.rects.size() > 0;
}

void RectangleList::swapWith (RectangleList& otherList) throw()
{
	rects.swapWithArray (otherList.rects);
}

void RectangleList::consolidate() throw()
{
	int i;
	for (i = 0; i < getNumRectangles() - 1; ++i)
	{
		Rectangle<int>& r = rects.getReference (i);
		const int rx1 = r.x;
		const int ry1 = r.y;
		const int rx2 = rx1 + r.w;
		const int ry2 = ry1 + r.h;

		for (int j = rects.size(); --j > i;)
		{
			Rectangle<int>& r2 = rects.getReference (j);
			const int jrx1 = r2.x;
			const int jry1 = r2.y;
			const int jrx2 = jrx1 + r2.w;
			const int jry2 = jry1 + r2.h;

			// if the vertical edges of any blocks are touching and their horizontals don't
			// line up, split them horizontally..
			if (jrx1 == rx2 || jrx2 == rx1)
			{
				if (jry1 > ry1 && jry1 < ry2)
				{
					r.h = jry1 - ry1;
					rects.add (Rectangle<int> (rx1, jry1, rx2 - rx1, ry2 - jry1));
					i = -1;
					break;
				}

				if (jry2 > ry1 && jry2 < ry2)
				{
					r.h = jry2 - ry1;
					rects.add (Rectangle<int> (rx1, jry2, rx2 - rx1, ry2 - jry2));
					i = -1;
					break;
				}
				else if (ry1 > jry1 && ry1 < jry2)
				{
					r2.h = ry1 - jry1;
					rects.add (Rectangle<int> (jrx1, ry1, jrx2 - jrx1, jry2 - ry1));
					i = -1;
					break;
				}
				else if (ry2 > jry1 && ry2 < jry2)
				{
					r2.h = ry2 - jry1;
					rects.add (Rectangle<int> (jrx1, ry2, jrx2 - jrx1, jry2 - ry2));
					i = -1;
					break;
				}
			}
		}
	}

	for (i = 0; i < rects.size() - 1; ++i)
	{
		Rectangle<int>& r = rects.getReference (i);

		for (int j = rects.size(); --j > i;)
		{
			if (r.enlargeIfAdjacent (rects.getReference (j)))
			{
				rects.remove (j);
				i = -1;
				break;
			}
		}
	}
}

bool RectangleList::containsPoint (const int x, const int y) const throw()
{
	for (int i = getNumRectangles(); --i >= 0;)
		if (rects.getReference (i).contains (x, y))
			return true;

	return false;
}

bool RectangleList::containsRectangle (const Rectangle<int>& rectangleToCheck) const throw()
{
	if (rects.size() > 1)
	{
		RectangleList r (rectangleToCheck);

		for (int i = rects.size(); --i >= 0;)
		{
			r.subtract (rects.getReference (i));

			if (r.rects.size() == 0)
				return true;
		}
	}
	else if (rects.size() > 0)
	{
		return rects.getReference (0).contains (rectangleToCheck);
	}

	return false;
}

bool RectangleList::intersectsRectangle (const Rectangle<int>& rectangleToCheck) const throw()
{
	for (int i = rects.size(); --i >= 0;)
		if (rects.getReference (i).intersects (rectangleToCheck))
			return true;

	return false;
}

bool RectangleList::intersects (const RectangleList& other) const throw()
{
	for (int i = rects.size(); --i >= 0;)
		if (other.intersectsRectangle (rects.getReference (i)))
			return true;

	return false;
}

const Rectangle<int> RectangleList::getBounds() const throw()
{
	if (rects.size() <= 1)
	{
		if (rects.size() == 0)
			return Rectangle<int>();
		else
			return rects.getReference (0);
	}
	else
	{
		const Rectangle<int>& r = rects.getReference (0);

		int minX = r.x;
		int minY = r.y;
		int maxX = minX + r.w;
		int maxY = minY + r.h;

		for (int i = rects.size(); --i > 0;)
		{
			const Rectangle<int>& r2 = rects.getReference (i);

			minX = jmin (minX, r2.x);
			minY = jmin (minY, r2.y);
			maxX = jmax (maxX, r2.getRight());
			maxY = jmax (maxY, r2.getBottom());
		}

		return Rectangle<int> (minX, minY, maxX - minX, maxY - minY);
	}
}

void RectangleList::offsetAll (const int dx, const int dy) throw()
{
	for (int i = rects.size(); --i >= 0;)
	{
		Rectangle<int>& r = rects.getReference (i);

		r.x += dx;
		r.y += dy;
	}
}

const Path RectangleList::toPath() const throw()
{
	Path p;

	for (int i = rects.size(); --i >= 0;)
	{
		const Rectangle<int>& r = rects.getReference (i);

		p.addRectangle ((float) r.x,
						(float) r.y,
						(float) r.w,
						(float) r.h);
	}

	return p;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_RectangleList.cpp ***/


/*** Start of inlined file: juce_Image.cpp ***/
BEGIN_JUCE_NAMESPACE

static const int fullAlphaThreshold = 253;

Image::Image (const PixelFormat format_,
			  const int imageWidth_,
			  const int imageHeight_)
  : format (format_),
	imageWidth (imageWidth_),
	imageHeight (imageHeight_),
	imageData (0)
{
	jassert (format_ == RGB || format_ == ARGB || format_ == SingleChannel);
	jassert (imageWidth_ > 0 && imageHeight_ > 0); // it's illegal to create a zero-sized image - the
												   //  actual image will be at least 1x1.
}

Image::Image (const PixelFormat format_,
			  const int imageWidth_,
			  const int imageHeight_,
			  const bool clearImage)
  : format (format_),
	imageWidth (imageWidth_),
	imageHeight (imageHeight_)
{
	jassert (format_ == RGB || format_ == ARGB || format_ == SingleChannel);
	jassert (imageWidth_ > 0 && imageHeight_ > 0); // it's illegal to create a zero-sized image - the
												   //  actual image will be at least 1x1.

	pixelStride = (format == RGB) ? 3 : ((format == ARGB) ? 4 : 1);
	lineStride = (pixelStride * jmax (1, imageWidth_) + 3) & ~3;

	imageDataAllocated.allocate (lineStride * jmax (1, imageHeight_), clearImage);
	imageData = imageDataAllocated;
}

Image::Image (const Image& other)
   : format (other.format),
	 imageWidth (other.imageWidth),
	 imageHeight (other.imageHeight)
{
	pixelStride = (format == RGB) ? 3 : ((format == ARGB) ? 4 : 1);
	lineStride = (pixelStride * jmax (1, imageWidth) + 3) & ~3;

	imageDataAllocated.malloc (lineStride * jmax (1, imageHeight));
	imageData = imageDataAllocated;

	BitmapData srcData (other, 0, 0, imageWidth, imageHeight);
	setPixelData (0, 0, imageWidth, imageHeight, srcData.data, srcData.lineStride);
}

Image::~Image()
{
}

LowLevelGraphicsContext* Image::createLowLevelContext()
{
	return new LowLevelGraphicsSoftwareRenderer (*this);
}

Image::BitmapData::BitmapData (Image& image, int x, int y, int w, int h, const bool /*makeWritable*/)
	: data (image.imageData + image.lineStride * y + image.pixelStride * x),
	  lineStride (image.lineStride),
	  pixelStride (image.pixelStride),
	  width (w),
	  height (h)
{
	jassert (x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= image.getWidth() && y + h <= image.getHeight());
}

Image::BitmapData::BitmapData (const Image& image, int x, int y, int w, int h)
	: data (image.imageData + image.lineStride * y + image.pixelStride * x),
	  lineStride (image.lineStride),
	  pixelStride (image.pixelStride),
	  width (w),
	  height (h)
{
	jassert (x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= image.getWidth() && y + h <= image.getHeight());
}

Image::BitmapData::~BitmapData()
{
}

void Image::setPixelData (int x, int y, int w, int h,
						  const uint8* sourcePixelData, int sourceLineStride)
{
	jassert (x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= imageWidth && y + h <= imageHeight);

	if (Rectangle<int>::intersectRectangles (x, y, w, h, 0, 0, imageWidth, imageHeight))
	{
		const BitmapData dest (*this, x, y, w, h, true);

		for (int i = 0; i < h; ++i)
		{
			memcpy (dest.getLinePointer(i),
					sourcePixelData + sourceLineStride * i,
					w * dest.pixelStride);
		}
	}
}

void Image::clear (int dx, int dy, int dw, int dh, const Colour& colourToClearTo)
{
	if (Rectangle<int>::intersectRectangles (dx, dy, dw, dh, 0, 0, imageWidth, imageHeight))
	{
		const PixelARGB col (colourToClearTo.getPixelARGB());

		const BitmapData destData (*this, dx, dy, dw, dh, true);
		uint8* dest = destData.data;

		while (--dh >= 0)
		{
			uint8* line = dest;
			dest += destData.lineStride;

			if (isARGB())
			{
				for (int x = dw; --x >= 0;)
				{
					((PixelARGB*) line)->set (col);
					line += destData.pixelStride;
				}
			}
			else if (isRGB())
			{
				for (int x = dw; --x >= 0;)
				{
					((PixelRGB*) line)->set (col);
					line += destData.pixelStride;
				}
			}
			else
			{
				for (int x = dw; --x >= 0;)
				{
					*line = col.getAlpha();
					line += destData.pixelStride;
				}
			}
		}
	}
}

Image* Image::createCopy (int newWidth, int newHeight,
						  const Graphics::ResamplingQuality quality) const
{
	if (newWidth < 0)
		newWidth = imageWidth;

	if (newHeight < 0)
		newHeight = imageHeight;

	Image* const newImage = Image::createNativeImage (format, newWidth, newHeight, true);

	Graphics g (*newImage);
	g.setImageResamplingQuality (quality);

	g.drawImage (this,
				 0, 0, newWidth, newHeight,
				 0, 0, imageWidth, imageHeight,
				 false);

	return newImage;
}

Image* Image::createCopyOfAlphaChannel() const
{
	jassert (format != SingleChannel);

	Image* const newImage = Image::createNativeImage (SingleChannel, imageWidth, imageHeight, false);

	if (! hasAlphaChannel())
	{
		newImage->clear (0, 0, imageWidth, imageHeight, Colours::black);
	}
	else
	{
		const BitmapData destData (*newImage, 0, 0, imageWidth, imageHeight, true);
		const BitmapData srcData (*this, 0, 0, imageWidth, imageHeight);

		for (int y = 0; y < imageHeight; ++y)
		{
			const PixelARGB* src = (const PixelARGB*) srcData.getLinePointer(y);
			uint8* dst = destData.getLinePointer (y);

			for (int x = imageWidth; --x >= 0;)
			{
				*dst++ = src->getAlpha();
				++src;
			}
		}
	}

	return newImage;
}

const Colour Image::getPixelAt (const int x, const int y) const
{
	Colour c;

	if (((unsigned int) x) < (unsigned int) imageWidth
		 && ((unsigned int) y) < (unsigned int) imageHeight)
	{
		const BitmapData srcData (*this, x, y, 1, 1);

		if (isARGB())
		{
			PixelARGB p (*(const PixelARGB*) srcData.data);
			p.unpremultiply();
			c = Colour (p.getARGB());
		}
		else if (isRGB())
			c = Colour (((const PixelRGB*) srcData.data)->getARGB());
		else
			c = Colour ((uint8) 0, (uint8) 0, (uint8) 0, *(srcData.data));
	}

	return c;
}

void Image::setPixelAt (const int x, const int y,
						const Colour& colour)
{
	if (((unsigned int) x) < (unsigned int) imageWidth
		 && ((unsigned int) y) < (unsigned int) imageHeight)
	{
		const BitmapData destData (*this, x, y, 1, 1, true);
		const PixelARGB col (colour.getPixelARGB());

		if (isARGB())
			((PixelARGB*) destData.data)->set (col);
		else if (isRGB())
			((PixelRGB*) destData.data)->set (col);
		else
			*(destData.data) = col.getAlpha();
	}
}

void Image::multiplyAlphaAt (const int x, const int y,
							 const float multiplier)
{
	if (((unsigned int) x) < (unsigned int) imageWidth
		 && ((unsigned int) y) < (unsigned int) imageHeight
		 && hasAlphaChannel())
	{
		const BitmapData destData (*this, x, y, 1, 1, true);

		if (isARGB())
			((PixelARGB*) destData.data)->multiplyAlpha (multiplier);
		else
			*(destData.data) = (uint8) (*(destData.data) * multiplier);
	}
}

void Image::multiplyAllAlphas (const float amountToMultiplyBy)
{
	if (hasAlphaChannel())
	{
		const BitmapData destData (*this, 0, 0, getWidth(), getHeight(), true);

		if (isARGB())
		{
			for (int y = 0; y < imageHeight; ++y)
			{
				uint8* p = destData.getLinePointer (y);

				for (int x = 0; x < imageWidth; ++x)
				{
					((PixelARGB*) p)->multiplyAlpha (amountToMultiplyBy);
					p += destData.pixelStride;
				}
			}
		}
		else
		{
			for (int y = 0; y < imageHeight; ++y)
			{
				uint8* p = destData.getLinePointer (y);

				for (int x = 0; x < imageWidth; ++x)
				{
					*p = (uint8) (*p * amountToMultiplyBy);
					p += destData.pixelStride;
				}
			}
		}
	}
	else
	{
		jassertfalse // can't do this without an alpha-channel!
	}
}

void Image::desaturate()
{
	if (isARGB() || isRGB())
	{
		const BitmapData destData (*this, 0, 0, getWidth(), getHeight(), true);

		if (isARGB())
		{
			for (int y = 0; y < imageHeight; ++y)
			{
				uint8* p = destData.getLinePointer (y);

				for (int x = 0; x < imageWidth; ++x)
				{
					((PixelARGB*) p)->desaturate();
					p += destData.pixelStride;
				}
			}
		}
		else
		{
			for (int y = 0; y < imageHeight; ++y)
			{
				uint8* p = destData.getLinePointer (y);

				for (int x = 0; x < imageWidth; ++x)
				{
					((PixelRGB*) p)->desaturate();
					p += destData.pixelStride;
				}
			}
		}
	}
}

void Image::createSolidAreaMask (RectangleList& result, const float alphaThreshold) const
{
	if (hasAlphaChannel())
	{
		const uint8 threshold = (uint8) jlimit (0, 255, roundToInt (alphaThreshold * 255.0f));
		SparseSet <int> pixelsOnRow;

		const BitmapData srcData (*this, 0, 0, getWidth(), getHeight());

		for (int y = 0; y < imageHeight; ++y)
		{
			pixelsOnRow.clear();
			const uint8* lineData = srcData.getLinePointer (y);

			if (isARGB())
			{
				for (int x = 0; x < imageWidth; ++x)
				{
					if (((const PixelARGB*) lineData)->getAlpha() >= threshold)
						pixelsOnRow.addRange (x, 1);

					lineData += srcData.pixelStride;
				}
			}
			else
			{
				for (int x = 0; x < imageWidth; ++x)
				{
					if (*lineData >= threshold)
						pixelsOnRow.addRange (x, 1);

					lineData += srcData.pixelStride;
				}
			}

			for (int i = 0; i < pixelsOnRow.getNumRanges(); ++i)
			{
				int x, w;

				if (pixelsOnRow.getRange (i, x, w))
					result.add (Rectangle<int> (x, y, w, 1));
			}

			result.consolidate();
		}
	}
	else
	{
		result.add (0, 0, imageWidth, imageHeight);
	}
}

void Image::moveImageSection (int dx, int dy,
							  int sx, int sy,
							  int w, int h)
{
	if (dx < 0)
	{
		w += dx;
		sx -= dx;
		dx = 0;
	}

	if (dy < 0)
	{
		h += dy;
		sy -= dy;
		dy = 0;
	}

	if (sx < 0)
	{
		w += sx;
		dx -= sx;
		sx = 0;
	}

	if (sy < 0)
	{
		h += sy;
		dy -= sy;
		sy = 0;
	}

	const int minX = jmin (dx, sx);
	const int minY = jmin (dy, sy);

	w = jmin (w, getWidth() - jmax (sx, dx));
	h = jmin (h, getHeight() - jmax (sy, dy));

	if (w > 0 && h > 0)
	{
		const int maxX = jmax (dx, sx) + w;
		const int maxY = jmax (dy, sy) + h;

		const BitmapData destData (*this, minX, minY, maxX - minX, maxY - minY, true);

		uint8* dst	   = destData.getPixelPointer (dx - minX, dy - minY);
		const uint8* src = destData.getPixelPointer (sx - minX, sy - minY);

		const int lineSize = destData.pixelStride * w;

		if (dy > sy)
		{
			while (--h >= 0)
			{
				const int offset = h * destData.lineStride;
				memmove (dst + offset, src + offset, lineSize);
			}
		}
		else if (dst != src)
		{
			while (--h >= 0)
			{
				memmove (dst, src, lineSize);
				dst += destData.lineStride;
				src += destData.lineStride;
			}
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_Image.cpp ***/


/*** Start of inlined file: juce_ImageCache.cpp ***/
BEGIN_JUCE_NAMESPACE

struct ImageCache::Item
{
	ScopedPointer <Image> image;
	int64 hashCode;
	int refCount;
	uint32 releaseTime;

	juce_UseDebuggingNewOperator
};

ImageCache* ImageCache::instance = 0;
int ImageCache::cacheTimeout = 5000;

ImageCache::ImageCache()
{
}

ImageCache::~ImageCache()
{
	jassert (instance == this);
	instance = 0;
}

Image* ImageCache::getFromHashCode (const int64 hashCode)
{
	if (instance != 0)
	{
		const ScopedLock sl (instance->lock);

		for (int i = instance->images.size(); --i >= 0;)
		{
			Item* const ci = instance->images.getUnchecked(i);

			if (ci->hashCode == hashCode)
			{
				ci->refCount++;
				return ci->image;
			}
		}
	}

	return 0;
}

void ImageCache::addImageToCache (Image* const image, const int64 hashCode)
{
	if (image != 0)
	{
		if (instance == 0)
			instance = new ImageCache();

		Item* const newC = new Item();
		newC->hashCode = hashCode;
		newC->image = image;
		newC->refCount = 1;
		newC->releaseTime = 0;

		const ScopedLock sl (instance->lock);
		instance->images.add (newC);
	}
}

void ImageCache::release (Image* const imageToRelease)
{
	if (imageToRelease != 0 && instance != 0)
	{
		const ScopedLock sl (instance->lock);

		for (int i = instance->images.size(); --i >= 0;)
		{
			Item* const ci = instance->images.getUnchecked(i);

			if (static_cast <Image*> (ci->image) == imageToRelease)
			{
				if (--(ci->refCount) == 0)
					ci->releaseTime = Time::getApproximateMillisecondCounter();

				if (! instance->isTimerRunning())
					instance->startTimer (999);

				break;
			}
		}
	}
}

void ImageCache::releaseOrDelete (Image* const imageToRelease)
{
	if (isImageInCache (imageToRelease))
		release (imageToRelease);
	else
		delete imageToRelease;
}

bool ImageCache::isImageInCache (Image* const imageToLookFor)
{
	if (instance != 0)
	{
		const ScopedLock sl (instance->lock);

		for (int i = instance->images.size(); --i >= 0;)
			if (static_cast <Image*> (instance->images.getUnchecked(i)->image) == imageToLookFor)
				return true;
	}

	return false;
}

void ImageCache::incReferenceCount (Image* const image)
{
	if (instance != 0)
	{
		const ScopedLock sl (instance->lock);

		for (int i = instance->images.size(); --i >= 0;)
		{
			Item* const ci = instance->images.getUnchecked(i);

			if (static_cast <Image*> (ci->image) == image)
			{
				ci->refCount++;
				return;
			}
		}
	}

	jassertfalse  // (trying to inc the ref count of an image that's not in the cache)
}

void ImageCache::timerCallback()
{
	int numberStillNeedingReleasing = 0;
	const uint32 now = Time::getApproximateMillisecondCounter();

	const ScopedLock sl (lock);

	for (int i = images.size(); --i >= 0;)
	{
		Item* const ci = images.getUnchecked(i);

		if (ci->refCount <= 0)
		{
			if (now > ci->releaseTime + cacheTimeout
				 || now < ci->releaseTime - 1000)
			{
				images.remove (i);
			}
			else
			{
				++numberStillNeedingReleasing;
			}
		}
	}

	if (numberStillNeedingReleasing == 0)
		stopTimer();
}

Image* ImageCache::getFromFile (const File& file)
{
	const int64 hashCode = file.hashCode64();
	Image* image = getFromHashCode (hashCode);

	if (image == 0)
	{
		image = ImageFileFormat::loadFrom (file);
		addImageToCache (image, hashCode);
	}

	return image;
}

Image* ImageCache::getFromMemory (const void* imageData, const int dataSize)
{
	const int64 hashCode = (int64) (pointer_sized_int) imageData;
	Image* image = getFromHashCode (hashCode);

	if (image == 0)
	{
		image = ImageFileFormat::loadFrom (imageData, dataSize);
		addImageToCache (image, hashCode);
	}

	return image;
}

void ImageCache::setCacheTimeout (const int millisecs)
{
	cacheTimeout = millisecs;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ImageCache.cpp ***/


/*** Start of inlined file: juce_ImageConvolutionKernel.cpp ***/
BEGIN_JUCE_NAMESPACE

ImageConvolutionKernel::ImageConvolutionKernel (const int size_)
	: values (size_ * size_),
	  size (size_)
{
	clear();
}

ImageConvolutionKernel::~ImageConvolutionKernel()
{
}

float ImageConvolutionKernel::getKernelValue (const int x, const int y) const throw()
{
	if (((unsigned int) x) < (unsigned int) size
		 && ((unsigned int) y) < (unsigned int) size)
	{
		return values [x + y * size];
	}
	else
	{
		jassertfalse;
		return 0;
	}
}

void ImageConvolutionKernel::setKernelValue (const int x, const int y, const float value) throw()
{
	if (((unsigned int) x) < (unsigned int) size
		 && ((unsigned int) y) < (unsigned int) size)
	{
		values [x + y * size] = value;
	}
	else
	{
		jassertfalse;
	}
}

void ImageConvolutionKernel::clear()
{
	for (int i = size * size; --i >= 0;)
		values[i] = 0;
}

void ImageConvolutionKernel::setOverallSum (const float desiredTotalSum)
{
	double currentTotal = 0.0;

	for (int i = size * size; --i >= 0;)
		currentTotal += values[i];

	rescaleAllValues ((float) (desiredTotalSum / currentTotal));
}

void ImageConvolutionKernel::rescaleAllValues (const float multiplier)
{
	for (int i = size * size; --i >= 0;)
		values[i] *= multiplier;
}

void ImageConvolutionKernel::createGaussianBlur (const float radius)
{
	const double radiusFactor = -1.0 / (radius * radius * 2);
	const int centre = size >> 1;

	for (int y = size; --y >= 0;)
	{
		for (int x = size; --x >= 0;)
		{
			const int cx = x - centre;
			const int cy = y - centre;

			values [x + y * size] = (float) exp (radiusFactor * (cx * cx + cy * cy));
		}
	}

	setOverallSum (1.0f);
}

void ImageConvolutionKernel::applyToImage (Image& destImage,
										   const Image* sourceImage,
										   const Rectangle<int>& destinationArea) const
{
	ScopedPointer <Image> imageCreated;

	if (sourceImage == 0)
	{
		sourceImage = imageCreated = destImage.createCopy();
	}
	else
	{
		jassert (sourceImage->getWidth() == destImage.getWidth()
				  && sourceImage->getHeight() == destImage.getHeight()
				  && sourceImage->getFormat() == destImage.getFormat());

		if (sourceImage->getWidth() != destImage.getWidth()
			 || sourceImage->getHeight() != destImage.getHeight()
			 || sourceImage->getFormat() != destImage.getFormat())
			return;
	}

	const Rectangle<int> area (destinationArea.getIntersection (destImage.getBounds()));

	if (area.isEmpty())
		return;

	const int right = area.getRight();
	const int bottom = area.getBottom();

	const Image::BitmapData destData (destImage, area.getX(), area.getY(), area.getWidth(), area.getHeight(), true);
	uint8* line = destData.data;

	const Image::BitmapData srcData (*sourceImage, 0, 0, sourceImage->getWidth(), sourceImage->getHeight());

	if (destData.pixelStride == 4)
	{
		for (int y = area.getY(); y < bottom; ++y)
		{
			uint8* dest = line;
			line += destData.lineStride;

			for (int x = area.getX(); x < right; ++x)
			{
				float c1 = 0;
				float c2 = 0;
				float c3 = 0;
				float c4 = 0;

				for (int yy = 0; yy < size; ++yy)
				{
					const int sy = y + yy - (size >> 1);

					if (sy >= srcData.height)
						break;

					if (sy >= 0)
					{
						int sx = x - (size >> 1);
						const uint8* src = srcData.getPixelPointer (sx, sy);

						for (int xx = 0; xx < size; ++xx)
						{
							if (sx >= srcData.width)
								break;

							if (sx >= 0)
							{
								const float kernelMult = values [xx + yy * size];
								c1 += kernelMult * *src++;
								c2 += kernelMult * *src++;
								c3 += kernelMult * *src++;
								c4 += kernelMult * *src++;
							}
							else
							{
								src += 4;
							}

							++sx;
						}
					}
				}

				*dest++ = (uint8) jmin (0xff, roundToInt (c1));
				*dest++ = (uint8) jmin (0xff, roundToInt (c2));
				*dest++ = (uint8) jmin (0xff, roundToInt (c3));
				*dest++ = (uint8) jmin (0xff, roundToInt (c4));
			}
		}
	}
	else if (destData.pixelStride == 3)
	{
		for (int y = area.getY(); y < bottom; ++y)
		{
			uint8* dest = line;
			line += destData.lineStride;

			for (int x = area.getX(); x < right; ++x)
			{
				float c1 = 0;
				float c2 = 0;
				float c3 = 0;

				for (int yy = 0; yy < size; ++yy)
				{
					const int sy = y + yy - (size >> 1);

					if (sy >= srcData.height)
						break;

					if (sy >= 0)
					{
						int sx = x - (size >> 1);
						const uint8* src = srcData.getPixelPointer (sx, sy);

						for (int xx = 0; xx < size; ++xx)
						{
							if (sx >= srcData.width)
								break;

							if (sx >= 0)
							{
								const float kernelMult = values [xx + yy * size];
								c1 += kernelMult * *src++;
								c2 += kernelMult * *src++;
								c3 += kernelMult * *src++;
							}
							else
							{
								src += 3;
							}

							++sx;
						}
					}
				}

				*dest++ = (uint8) roundToInt (c1);
				*dest++ = (uint8) roundToInt (c2);
				*dest++ = (uint8) roundToInt (c3);
			}
		}
	}
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ImageConvolutionKernel.cpp ***/


/*** Start of inlined file: juce_ImageFileFormat.cpp ***/
BEGIN_JUCE_NAMESPACE


/*** Start of inlined file: juce_GIFLoader.h ***/
#ifndef __JUCE_GIFLOADER_JUCEHEADER__
#define __JUCE_GIFLOADER_JUCEHEADER__

#ifndef DOXYGEN

class GIFLoader
{
public:
	GIFLoader (InputStream& in);
	~GIFLoader();

	Image* getImage() const		 { return image; }

private:
	Image* image;
	InputStream& input;
	uint8 buffer [300];
	uint8 palette [256][4];
	bool dataBlockIsZero, fresh, finished;
	int currentBit, lastBit, lastByteIndex;
	int codeSize, setCodeSize;
	int maxCode, maxCodeSize;
	int firstcode, oldcode;
	int clearCode, end_code;
	enum { maxGifCode = 1 << 12 };
	int table [2] [maxGifCode];
	int stack [2 * maxGifCode];
	int *sp;

	bool getSizeFromHeader (int& width, int& height);
	bool readPalette (const int numCols);
	int readDataBlock (unsigned char* dest);
	int processExtension (int type, int& transparent);
	int readLZWByte (bool initialise, int input_code_size);
	int getCode (int code_size, bool initialise);
	bool readImage (int width, int height, int interlace, int transparent);
	static inline int makeWord (const uint8 a, const uint8 b)	{ return (b << 8) | a; }

	GIFLoader (const GIFLoader&);
	GIFLoader& operator= (const GIFLoader&);
};

#endif   // DOXYGEN

#endif   // __JUCE_GIFLOADER_JUCEHEADER__
/*** End of inlined file: juce_GIFLoader.h ***/

Image* juce_loadPNGImageFromStream (InputStream& inputStream);
bool juce_writePNGImageToStream (const Image& image, OutputStream& out);

PNGImageFormat::PNGImageFormat()	{}
PNGImageFormat::~PNGImageFormat()   {}

const String PNGImageFormat::getFormatName()
{
	return "PNG";
}

bool PNGImageFormat::canUnderstand (InputStream& in)
{
	const int bytesNeeded = 4;
	char header [bytesNeeded];

	return in.read (header, bytesNeeded) == bytesNeeded
			&& header[1] == 'P'
			&& header[2] == 'N'
			&& header[3] == 'G';
}

Image* PNGImageFormat::decodeImage (InputStream& in)
{
	return juce_loadPNGImageFromStream (in);
}

bool PNGImageFormat::writeImageToStream (const Image& sourceImage,
										 OutputStream& destStream)
{
	return juce_writePNGImageToStream (sourceImage, destStream);
}

Image* juce_loadJPEGImageFromStream (InputStream& inputStream);
bool juce_writeJPEGImageToStream (const Image& image, OutputStream& out, float quality);

JPEGImageFormat::JPEGImageFormat()
	: quality (-1.0f)
{
}

JPEGImageFormat::~JPEGImageFormat()	 {}

void JPEGImageFormat::setQuality (const float newQuality)
{
	quality = newQuality;
}

const String JPEGImageFormat::getFormatName()
{
	return "JPEG";
}

bool JPEGImageFormat::canUnderstand (InputStream& in)
{
	const int bytesNeeded = 10;
	uint8 header [bytesNeeded];

	if (in.read (header, bytesNeeded) == bytesNeeded)
	{
		return header[0] == 0xff
			&& header[1] == 0xd8
			&& header[2] == 0xff
			&& (header[3] == 0xe0 || header[3] == 0xe1);
	}

	return false;
}

Image* JPEGImageFormat::decodeImage (InputStream& in)
{
	return juce_loadJPEGImageFromStream (in);
}

bool JPEGImageFormat::writeImageToStream (const Image& sourceImage,
										  OutputStream& destStream)
{
	return juce_writeJPEGImageToStream (sourceImage, destStream, quality);
}

class GIFImageFormat  : public ImageFileFormat
{
public:
	GIFImageFormat()	{}
	~GIFImageFormat()   {}

	const String getFormatName()
	{
		return "GIF";
	}

	bool canUnderstand (InputStream& in)
	{
		const int bytesNeeded = 4;
		char header [bytesNeeded];

		return (in.read (header, bytesNeeded) == bytesNeeded)
				&& header[0] == 'G'
				&& header[1] == 'I'
				&& header[2] == 'F';
	}

	Image* decodeImage (InputStream& in)
	{
		const ScopedPointer <GIFLoader> loader (new GIFLoader (in));
		return loader->getImage();
	}

	bool writeImageToStream (const Image& /*sourceImage*/, OutputStream& /*destStream*/)
	{
		return false;
	}
};

ImageFileFormat* ImageFileFormat::findImageFormatForStream (InputStream& input)
{
	static PNGImageFormat png;
	static JPEGImageFormat jpg;
	static GIFImageFormat gif;

	ImageFileFormat* formats[4];
	int numFormats = 0;

	formats [numFormats++] = &png;
	formats [numFormats++] = &jpg;
	formats [numFormats++] = &gif;

	const int64 streamPos = input.getPosition();

	for (int i = 0; i < numFormats; ++i)
	{
		const bool found = formats[i]->canUnderstand (input);
		input.setPosition (streamPos);

		if (found)
			return formats[i];
	}

	return 0;
}

Image* ImageFileFormat::loadFrom (InputStream& input)
{
	ImageFileFormat* const format = findImageFormatForStream (input);

	if (format != 0)
		return format->decodeImage (input);

	return 0;
}

Image* ImageFileFormat::loadFrom (const File& file)
{
	InputStream* const in = file.createInputStream();

	if (in != 0)
	{
		BufferedInputStream b (in, 8192, true);
		return loadFrom (b);
	}

	return 0;
}

Image* ImageFileFormat::loadFrom (const void* rawData, const int numBytes)
{
	if (rawData != 0 && numBytes > 4)
	{
		MemoryInputStream stream (rawData, numBytes, false);
		return loadFrom (stream);
	}

	return 0;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_ImageFileFormat.cpp ***/


/*** Start of inlined file: juce_GIFLoader.cpp ***/
BEGIN_JUCE_NAMESPACE

GIFLoader::GIFLoader (InputStream& in)
	: image (0),
	  input (in),
	  dataBlockIsZero (false),
	  fresh (false),
	  finished (false)
{
	currentBit = lastBit = lastByteIndex = 0;
	maxCode = maxCodeSize = codeSize = setCodeSize = 0;
	firstcode = oldcode = 0;
	clearCode = end_code = 0;

	int imageWidth, imageHeight;
	int transparent = -1;

	if (! getSizeFromHeader (imageWidth, imageHeight))
		return;

	if ((imageWidth <= 0) || (imageHeight <= 0))
		return;

	unsigned char buf [16];
	if (in.read (buf, 3) != 3)
		return;

	int numColours = 2 << (buf[0] & 7);

	if ((buf[0] & 0x80) != 0)
		readPalette (numColours);

	for (;;)
	{
		if (input.read (buf, 1) != 1)
			break;

		if (buf[0] == ';')
			break;

		if (buf[0] == '!')
		{
			if (input.read (buf, 1) != 1)
				break;

			if (processExtension (buf[0], transparent) < 0)
				break;

			continue;
		}

		if (buf[0] != ',')
			continue;

		if (input.read (buf, 9) != 9)
			break;

		imageWidth  = makeWord (buf[4], buf[5]);
		imageHeight = makeWord (buf[6], buf[7]);

		numColours = 2 << (buf[8] & 7);

		if ((buf[8] & 0x80) != 0)
			if (! readPalette (numColours))
				break;

		image = Image::createNativeImage ((transparent >= 0) ? Image::ARGB : Image::RGB,
										  imageWidth, imageHeight, (transparent >= 0));

		readImage (imageWidth, imageHeight,
				   (buf[8] & 0x40) != 0,
				   transparent);

		break;
	}
}

GIFLoader::~GIFLoader()
{
}

bool GIFLoader::getSizeFromHeader (int& w, int& h)
{
	char b[8];

	if (input.read (b, 6) == 6)
	{
		if ((strncmp ("GIF87a", b, 6) == 0)
			 || (strncmp ("GIF89a", b, 6) == 0))
		{
			if (input.read (b, 4) == 4)
			{
				w = makeWord (b[0], b[1]);
				h = makeWord (b[2], b[3]);
				return true;
			}
		}
	}

	return false;
}

bool GIFLoader::readPalette (const int numCols)
{
	unsigned char rgb[4];

	for (int i = 0; i < numCols; ++i)
	{
		input.read (rgb, 3);

		palette [i][0] = rgb[0];
		palette [i][1] = rgb[1];
		palette [i][2] = rgb[2];
		palette [i][3] = 0xff;
	}

	return true;
}

int GIFLoader::readDataBlock (unsigned char* const dest)
{
	unsigned char n;

	if (input.read (&n, 1) == 1)
	{
		dataBlockIsZero = (n == 0);

		if (dataBlockIsZero || (input.read (dest, n) == n))
			return n;
	}

	return -1;
}

int GIFLoader::processExtension (const int type, int& transparent)
{
	unsigned char b [300];
	int n = 0;

	if (type == 0xf9)
	{
		n = readDataBlock (b);
		if (n < 0)
			return 1;

		if ((b[0] & 0x1) != 0)
			transparent = b[3];
	}

	do
	{
		n = readDataBlock (b);
	}
	while (n > 0);

	return n;
}

int GIFLoader::getCode (const int codeSize_, const bool initialise)
{
	if (initialise)
	{
		currentBit = 0;
		lastBit = 0;
		finished = false;
		return 0;
	}

	if ((currentBit + codeSize_) >= lastBit)
	{
		if (finished)
			return -1;

		buffer[0] = buffer [lastByteIndex - 2];
		buffer[1] = buffer [lastByteIndex - 1];

		const int n = readDataBlock (&buffer[2]);

		if (n == 0)
			finished = true;

		lastByteIndex = 2 + n;
		currentBit = (currentBit - lastBit) + 16;
		lastBit = (2 + n) * 8 ;
	}

	int result = 0;
	int i = currentBit;

	for (int j = 0; j < codeSize_; ++j)
	{
		result |= ((buffer[i >> 3] & (1 << (i & 7))) != 0) << j;
		++i;
	}

	currentBit += codeSize_;

	return result;
}

int GIFLoader::readLZWByte (const bool initialise, const int inputCodeSize)
{
	int code, incode, i;

	if (initialise)
	{
		setCodeSize = inputCodeSize;
		codeSize = setCodeSize + 1;
		clearCode = 1 << setCodeSize;
		end_code = clearCode + 1;
		maxCodeSize = 2 * clearCode;
		maxCode = clearCode + 2;

		getCode (0, true);

		fresh = true;

		for (i = 0; i < clearCode; ++i)
		{
			table[0][i] = 0;
			table[1][i] = i;
		}

		for (; i < maxGifCode; ++i)
		{
			table[0][i] = 0;
			table[1][i] = 0;
		}

		sp = stack;

		return 0;
	}
	else if (fresh)
	{
		fresh = false;

		do
		{
			firstcode = oldcode
				= getCode (codeSize, false);
		}
		while (firstcode == clearCode);

		return firstcode;
	}

	if (sp > stack)
		return *--sp;

	while ((code = getCode (codeSize, false)) >= 0)
	{
		if (code == clearCode)
		{
			for (i = 0; i < clearCode; ++i)
			{
				table[0][i] = 0;
				table[1][i] = i;
			}

			for (; i < maxGifCode; ++i)
			{
				table[0][i] = 0;
				table[1][i] = 0;
			}

			codeSize = setCodeSize + 1;
			maxCodeSize = 2 * clearCode;
			maxCode = clearCode + 2;
			sp = stack;
			firstcode = oldcode = getCode (codeSize, false);
			return firstcode;

		}
		else if (code == end_code)
		{
			if (dataBlockIsZero)
				return -2;

			unsigned char buf [260];

			int n;
			while ((n = readDataBlock (buf)) > 0)
			{}

			if (n != 0)
				return -2;
		}

		incode = code;

		if (code >= maxCode)
		{
			*sp++ = firstcode;
			code = oldcode;
		}

		while (code >= clearCode)
		{
			*sp++ = table[1][code];
			if (code == table[0][code])
				return -2;

			code = table[0][code];
		}

		*sp++ = firstcode = table[1][code];

		if ((code = maxCode) < maxGifCode)
		{
			table[0][code] = oldcode;
			table[1][code] = firstcode;
			++maxCode;

			if ((maxCode >= maxCodeSize)
				&& (maxCodeSize < maxGifCode))
			{
				maxCodeSize <<= 1;
				++codeSize;
			}
		}

		oldcode = incode;

		if (sp > stack)
			return *--sp;
	}

	return code;
}

bool GIFLoader::readImage (const int width, const int height,
						   const int interlace, const int transparent)
{
	unsigned char c;

	if (input.read (&c, 1) != 1
		 || readLZWByte (true, c) < 0)
		return false;

	if (transparent >= 0)
	{
		palette [transparent][0] = 0;
		palette [transparent][1] = 0;
		palette [transparent][2] = 0;
		palette [transparent][3] = 0;
	}

	int index;
	int xpos = 0, ypos = 0, pass = 0;

	const Image::BitmapData destData (*image, 0, 0, width, height, true);
	uint8* p = destData.data;
	const bool hasAlpha = image->hasAlphaChannel();

	while ((index = readLZWByte (false, c)) >= 0)
	{
		const uint8* const paletteEntry = palette [index];

		if (hasAlpha)
		{
			((PixelARGB*) p)->setARGB (paletteEntry[3],
									   paletteEntry[0],
									   paletteEntry[1],
									   paletteEntry[2]);

			((PixelARGB*) p)->premultiply();
		}
		else
		{
			((PixelRGB*) p)->setARGB (0,
									  paletteEntry[0],
									  paletteEntry[1],
									  paletteEntry[2]);
		}

		p += destData.pixelStride;
		++xpos;

		if (xpos == width)
		{
			xpos = 0;

			if (interlace)
			{
				switch (pass)
				{
				case 0:
				case 1:
					ypos += 8;
					break;
				case 2:
					ypos += 4;
					break;
				case 3:
					ypos += 2;
					break;
				}

				while (ypos >= height)
				{
					++pass;

					switch (pass)
					{
					case 1:
						ypos = 4;
						break;
					case 2:
						ypos = 2;
						break;
					case 3:
						ypos = 1;
						break;
					default:
						return true;
					}
				}
			}
			else
			{
				++ypos;
			}

			p = destData.getPixelPointer (xpos, ypos);
		}

		if (ypos >= height)
			break;
	}

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_GIFLoader.cpp ***/

#endif

//==============================================================================
// some files include lots of library code, so leave them to the end to avoid cluttering
// up the build for the clean files.
#if JUCE_BUILD_CORE

/*** Start of inlined file: juce_GZIPCompressorOutputStream.cpp ***/
namespace zlibNamespace
{
#if JUCE_INCLUDE_ZLIB_CODE
  #undef OS_CODE
  #undef fdopen

/*** Start of inlined file: zlib.h ***/
#ifndef ZLIB_H
#define ZLIB_H


/*** Start of inlined file: zconf.h ***/
#ifndef ZCONF_H
#define ZCONF_H

// *** Just a few hacks here to make it compile nicely with Juce..
#define Z_PREFIX 1
#undef __MACTYPES__

#ifdef _MSC_VER
  #pragma warning (disable : 4131 4127 4244 4267)
#endif

#ifdef Z_PREFIX
#  define deflateInit_	  z_deflateInit_
#  define deflate		   z_deflate
#  define deflateEnd		z_deflateEnd
#  define inflateInit_	  z_inflateInit_
#  define inflate		   z_inflate
#  define inflateEnd		z_inflateEnd
#  define inflatePrime	  z_inflatePrime
#  define inflateGetHeader	  z_inflateGetHeader
#  define adler32_combine	   z_adler32_combine
#  define crc32_combine	 z_crc32_combine
#  define deflateInit2_	 z_deflateInit2_
#  define deflateSetDictionary  z_deflateSetDictionary
#  define deflateCopy	   z_deflateCopy
#  define deflateReset	  z_deflateReset
#  define deflateParams	 z_deflateParams
#  define deflateBound	  z_deflateBound
#  define deflatePrime	  z_deflatePrime
#  define inflateInit2_	 z_inflateInit2_
#  define inflateSetDictionary  z_inflateSetDictionary
#  define inflateSync	   z_inflateSync
#  define inflateSyncPoint	  z_inflateSyncPoint
#  define inflateCopy	   z_inflateCopy
#  define inflateReset	  z_inflateReset
#  define inflateBack	   z_inflateBack
#  define inflateBackEnd	z_inflateBackEnd
#  define compress		  z_compress
#  define compress2		 z_compress2
#  define compressBound	 z_compressBound
#  define uncompress		z_uncompress
#  define adler32		   z_adler32
#  define crc32		 z_crc32
#  define get_crc_table	 z_get_crc_table
#  define zError		z_zError

#  define alloc_func		z_alloc_func
#  define free_func		 z_free_func
#  define in_func		   z_in_func
#  define out_func		  z_out_func
#  define Byte		  z_Byte
#  define uInt		  z_uInt
#  define uLong		 z_uLong
#  define Bytef		 z_Bytef
#  define charf		 z_charf
#  define intf		  z_intf
#  define uIntf		 z_uIntf
#  define uLongf		z_uLongf
#  define voidpf		z_voidpf
#  define voidp		 z_voidp
#endif

#if defined(__MSDOS__) && !defined(MSDOS)
#  define MSDOS
#endif
#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
#  define OS2
#endif
#if defined(_WINDOWS) && !defined(WINDOWS)
#  define WINDOWS
#endif
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
#  ifndef WIN32
#	define WIN32
#  endif
#endif
#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
#  if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
#	ifndef SYS16BIT
#	  define SYS16BIT
#	endif
#  endif
#endif

#ifdef SYS16BIT
#  define MAXSEG_64K
#endif
#ifdef MSDOS
#  define UNALIGNED_OK
#endif

#ifdef __STDC_VERSION__
#  ifndef STDC
#	define STDC
#  endif
#  if __STDC_VERSION__ >= 199901L
#	ifndef STDC99
#	  define STDC99
#	endif
#  endif
#endif
#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
#  define STDC
#endif
#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
#  define STDC
#endif
#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
#  define STDC
#endif
#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
#  define STDC
#endif

#if defined(__OS400__) && !defined(STDC)	/* iSeries (formerly AS/400). */
#  define STDC
#endif

#ifndef STDC
#  ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
#	define const	   /* note: need a more gentle solution here */
#  endif
#endif

#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
#  define NO_DUMMY_DECL
#endif

#ifndef MAX_MEM_LEVEL
#  ifdef MAXSEG_64K
#	define MAX_MEM_LEVEL 8
#  else
#	define MAX_MEM_LEVEL 9
#  endif
#endif

#ifndef MAX_WBITS
#  define MAX_WBITS   15 /* 32K LZ77 window */
#endif

#ifndef OF /* function prototypes */
#  ifdef STDC
#	define OF(args)  args
#  else
#	define OF(args)  ()
#  endif
#endif

#ifdef SYS16BIT
#  if defined(M_I86SM) || defined(M_I86MM)
#	define SMALL_MEDIUM
#	ifdef _MSC_VER
#	  define FAR _far
#	else
#	  define FAR far
#	endif
#  endif
#  if (defined(__SMALL__) || defined(__MEDIUM__))
#	define SMALL_MEDIUM
#	ifdef __BORLANDC__
#	  define FAR _far
#	else
#	  define FAR far
#	endif
#  endif
#endif

#if defined(WINDOWS) || defined(WIN32)
#  ifdef ZLIB_DLL
#	if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
#	  ifdef ZLIB_INTERNAL
#	define ZEXTERN extern __declspec(dllexport)
#	  else
#	define ZEXTERN extern __declspec(dllimport)
#	  endif
#	endif
#  endif  /* ZLIB_DLL */
#  ifdef ZLIB_WINAPI
#	ifdef FAR
#	  undef FAR
#	endif
#	include <windows.h>
#	define ZEXPORT WINAPI
#	ifdef WIN32
#	  define ZEXPORTVA WINAPIV
#	else
#	  define ZEXPORTVA FAR CDECL
#	endif
#  endif
#endif

#if defined (__BEOS__)
#  ifdef ZLIB_DLL
#	ifdef ZLIB_INTERNAL
#	  define ZEXPORT   __declspec(dllexport)
#	  define ZEXPORTVA __declspec(dllexport)
#	else
#	  define ZEXPORT   __declspec(dllimport)
#	  define ZEXPORTVA __declspec(dllimport)
#	endif
#  endif
#endif

#ifndef ZEXTERN
#  define ZEXTERN extern
#endif
#ifndef ZEXPORT
#  define ZEXPORT
#endif
#ifndef ZEXPORTVA
#  define ZEXPORTVA
#endif

#ifndef FAR
#  define FAR
#endif

#if !defined(__MACTYPES__)
typedef unsigned char  Byte;  /* 8 bits */
#endif
typedef unsigned int   uInt;  /* 16 bits or more */
typedef unsigned long  uLong; /* 32 bits or more */

#ifdef SMALL_MEDIUM
#  define Bytef Byte FAR
#else
   typedef Byte  FAR Bytef;
#endif
typedef char  FAR charf;
typedef int   FAR intf;
typedef uInt  FAR uIntf;
typedef uLong FAR uLongf;

#ifdef STDC
   typedef void const *voidpc;
   typedef void FAR   *voidpf;
   typedef void	   *voidp;
#else
   typedef Byte const *voidpc;
   typedef Byte FAR   *voidpf;
   typedef Byte	   *voidp;
#endif

#if 0	   /* HAVE_UNISTD_H -- this line is updated by ./configure */
#  include <sys/types.h> /* for off_t */
#  include <unistd.h>	/* for SEEK_* and off_t */
#  ifdef VMS
#	include <unixio.h>   /* for off_t */
#  endif
#  define z_off_t off_t
#endif
#ifndef SEEK_SET
#  define SEEK_SET	0	   /* Seek from beginning of file.  */
#  define SEEK_CUR	1	   /* Seek from current position.  */
#  define SEEK_END        2       /* Set file pointer to EOF plus "offset" */
#endif
#ifndef z_off_t
#  define z_off_t long
#endif

#if defined(__OS400__)
#  define NO_vsnprintf
#endif

#if defined(__MVS__)
#  define NO_vsnprintf
#  ifdef FAR
#	undef FAR
#  endif
#endif

#if defined(__MVS__)
#   pragma map(deflateInit_,"DEIN")
#   pragma map(deflateInit2_,"DEIN2")
#   pragma map(deflateEnd,"DEEND")
#   pragma map(deflateBound,"DEBND")
#   pragma map(inflateInit_,"ININ")
#   pragma map(inflateInit2_,"ININ2")
#   pragma map(inflateEnd,"INEND")
#   pragma map(inflateSync,"INSY")
#   pragma map(inflateSetDictionary,"INSEDI")
#   pragma map(compressBound,"CMBND")
#   pragma map(inflate_table,"INTABL")
#   pragma map(inflate_fast,"INFA")
#   pragma map(inflate_copyright,"INCOPY")
#endif

#endif /* ZCONF_H */
/*** End of inlined file: zconf.h ***/

#ifdef __cplusplus
extern "C" {
#endif

#define ZLIB_VERSION "1.2.3"
#define ZLIB_VERNUM 0x1230

typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
typedef void   (*free_func)  OF((voidpf opaque, voidpf address));

struct internal_state;

typedef struct z_stream_s {
	Bytef	*next_in;  /* next input byte */
	uInt	 avail_in;  /* number of bytes available at next_in */
	uLong	total_in;  /* total nb of input bytes read so far */

	Bytef	*next_out; /* next output byte should be put there */
	uInt	 avail_out; /* remaining free space at next_out */
	uLong	total_out; /* total nb of bytes output so far */

	char	 *msg;	  /* last error message, NULL if no error */
	struct internal_state FAR *state; /* not visible by applications */

	alloc_func zalloc;  /* used to allocate the internal state */
	free_func  zfree;   /* used to free the internal state */
	voidpf	 opaque;  /* private data object passed to zalloc and zfree */

	int	 data_type;  /* best guess about the data type: binary or text */
	uLong   adler;	  /* adler32 value of the uncompressed data */
	uLong   reserved;   /* reserved for future use */
} z_stream;

typedef z_stream FAR *z_streamp;

typedef struct gz_header_s {
	int	 text;	   /* true if compressed data believed to be text */
	uLong   time;	   /* modification time */
	int	 xflags;	 /* extra flags (not used when writing a gzip file) */
	int	 os;	 /* operating system */
	Bytef   *extra;	 /* pointer to extra field or Z_NULL if none */
	uInt	extra_len;  /* extra field length (valid if extra != Z_NULL) */
	uInt	extra_max;  /* space at extra (only when reading header) */
	Bytef   *name;	  /* pointer to zero-terminated file name or Z_NULL */
	uInt	name_max;   /* space at name (only when reading header) */
	Bytef   *comment;   /* pointer to zero-terminated comment or Z_NULL */
	uInt	comm_max;   /* space at comment (only when reading header) */
	int	 hcrc;	   /* true if there was or will be a header crc */
	int	 done;	   /* true when done reading gzip header (not used
						   when writing a gzip file) */
} gz_header;

typedef gz_header FAR *gz_headerp;

#define Z_NO_FLUSH	  0
#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
#define Z_SYNC_FLUSH	2
#define Z_FULL_FLUSH	3
#define Z_FINISH	4
#define Z_BLOCK	 5

#define Z_OK		0
#define Z_STREAM_END	1
#define Z_NEED_DICT	 2
#define Z_ERRNO	(-1)
#define Z_STREAM_ERROR (-2)
#define Z_DATA_ERROR   (-3)
#define Z_MEM_ERROR	(-4)
#define Z_BUF_ERROR	(-5)
#define Z_VERSION_ERROR (-6)

#define Z_NO_COMPRESSION	 0
#define Z_BEST_SPEED		 1
#define Z_BEST_COMPRESSION	   9
#define Z_DEFAULT_COMPRESSION  (-1)

#define Z_FILTERED		1
#define Z_HUFFMAN_ONLY	2
#define Z_RLE		 3
#define Z_FIXED		   4
#define Z_DEFAULT_STRATEGY	0

#define Z_BINARY   0
#define Z_TEXT	 1
#define Z_ASCII	Z_TEXT   /* for compatibility with 1.2.2 and earlier */
#define Z_UNKNOWN  2

#define Z_DEFLATED   8

#define Z_NULL  0  /* for initializing zalloc, zfree, opaque */

#define zlib_version zlibVersion()

//ZEXTERN const char * ZEXPORT zlibVersion OF((void));

ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));

ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));

ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));

ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));

ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
											 const Bytef *dictionary,
											 uInt  dictLength));

ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
									z_streamp source));

ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));

ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
									  int level,
									  int strategy));

ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
									int good_length,
									int max_lazy,
									int nice_length,
									int max_chain));

ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
									   uLong sourceLen));

ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
									 int bits,
									 int value));

ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
										 gz_headerp head));

ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
											 const Bytef *dictionary,
											 uInt  dictLength));

ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));

ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
									z_streamp source));

ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));

ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
									 int bits,
									 int value));

ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
										 gz_headerp head));

typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));

ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
									in_func in, void FAR *in_desc,
									out_func out, void FAR *out_desc));

ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));

//ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));

ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
								 const Bytef *source, uLong sourceLen));

ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
								  const Bytef *source, uLong sourceLen,
								  int level));

ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));

ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
								   const Bytef *source, uLong sourceLen));

typedef voidp gzFile;

ZEXTERN gzFile ZEXPORT gzopen  OF((const char *path, const char *mode));

ZEXTERN gzFile ZEXPORT gzdopen  OF((int fd, const char *mode));

ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));

ZEXTERN int ZEXPORT	gzread  OF((gzFile file, voidp buf, unsigned len));

ZEXTERN int ZEXPORT	gzwrite OF((gzFile file,
								   voidpc buf, unsigned len));

ZEXTERN int ZEXPORTVA   gzprintf OF((gzFile file, const char *format, ...));

ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));

ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));

ZEXTERN int ZEXPORT	gzputc OF((gzFile file, int c));

ZEXTERN int ZEXPORT	gzgetc OF((gzFile file));

ZEXTERN int ZEXPORT	gzungetc OF((int c, gzFile file));

ZEXTERN int ZEXPORT	gzflush OF((gzFile file, int flush));

ZEXTERN z_off_t ZEXPORT	gzseek OF((gzFile file,
									  z_off_t offset, int whence));

ZEXTERN int ZEXPORT	gzrewind OF((gzFile file));

ZEXTERN z_off_t ZEXPORT	gztell OF((gzFile file));

ZEXTERN int ZEXPORT gzeof OF((gzFile file));

ZEXTERN int ZEXPORT gzdirect OF((gzFile file));

ZEXTERN int ZEXPORT	gzclose OF((gzFile file));

ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));

ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));

ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));

ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
										  z_off_t len2));

ZEXTERN uLong ZEXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));

ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));

ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
									 const char *version, int stream_size));
ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
									 const char *version, int stream_size));
ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int  level, int  method,
									  int windowBits, int memLevel,
									  int strategy, const char *version,
									  int stream_size));
ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int  windowBits,
									  const char *version, int stream_size));
ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
										 unsigned char FAR *window,
										 const char *version,
										 int stream_size));
#define deflateInit(strm, level) \
		deflateInit_((strm), (level),	   ZLIB_VERSION, sizeof(z_stream))
#define inflateInit(strm) \
		inflateInit_((strm),		ZLIB_VERSION, sizeof(z_stream))
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
		deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
					  (strategy),	   ZLIB_VERSION, sizeof(z_stream))
#define inflateInit2(strm, windowBits) \
		inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
#define inflateBackInit(strm, windowBits, window) \
		inflateBackInit_((strm), (windowBits), (window), \
		ZLIB_VERSION, sizeof(z_stream))

#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
	struct internal_state {int dummy;}; /* hack for buggy compilers */
#endif

ZEXTERN const char   * ZEXPORT zError	   OF((int));
ZEXTERN int		ZEXPORT inflateSyncPoint OF((z_streamp z));
ZEXTERN const uLongf * ZEXPORT get_crc_table	OF((void));

#ifdef __cplusplus
}
#endif

#endif /* ZLIB_H */
/*** End of inlined file: zlib.h ***/


  #undef OS_CODE
#else
  #include <zlib.h>
#endif
}

BEGIN_JUCE_NAMESPACE

// internal helper object that holds the zlib structures so they don't have to be
// included publicly.
class GZIPCompressorHelper
{
public:
	GZIPCompressorHelper (const int compressionLevel, const bool nowrap)
		: data (0),
		  dataSize (0),
		  compLevel (compressionLevel),
		  strategy (0),
		  setParams (true),
		  streamIsValid (false),
		  finished (false),
		  shouldFinish (false)
	{
		using namespace zlibNamespace;
		zerostruct (stream);

		streamIsValid = (deflateInit2 (&stream, compLevel, Z_DEFLATED,
									   nowrap ? -MAX_WBITS : MAX_WBITS,
									   8, strategy) == Z_OK);
	}

	~GZIPCompressorHelper()
	{
		using namespace zlibNamespace;
		if (streamIsValid)
			deflateEnd (&stream);
	}

	bool needsInput() const throw()
	{
		return dataSize <= 0;
	}

	void setInput (const uint8* const newData, const int size) throw()
	{
		data = newData;
		dataSize = size;
	}

	int doNextBlock (uint8* const dest, const int destSize) throw()
	{
		using namespace zlibNamespace;
		if (streamIsValid)
		{
			stream.next_in = const_cast <uint8*> (data);
			stream.next_out = dest;
			stream.avail_in = dataSize;
			stream.avail_out = destSize;

			const int result = setParams ? deflateParams (&stream, compLevel, strategy)
										 : deflate (&stream, shouldFinish ? Z_FINISH : Z_NO_FLUSH);

			setParams = false;

			switch (result)
			{
			case Z_STREAM_END:
				finished = true;
				// Deliberate fall-through..
			case Z_OK:
				data += dataSize - stream.avail_in;
				dataSize = stream.avail_in;

				return destSize - stream.avail_out;

			default:
				break;
			}
		}

		return 0;
	}

private:
	zlibNamespace::z_stream stream;
	const uint8* data;
	int dataSize, compLevel, strategy;
	bool setParams, streamIsValid;

public:
	bool finished, shouldFinish;
};

const int gzipCompBufferSize = 32768;

GZIPCompressorOutputStream::GZIPCompressorOutputStream (OutputStream* const destStream_,
														int compressionLevel,
														const bool deleteDestStream,
														const bool noWrap)
  : destStream (destStream_),
	streamToDelete (deleteDestStream ? destStream_ : 0),
	buffer (gzipCompBufferSize)
{
	if (compressionLevel < 1 || compressionLevel > 9)
		compressionLevel = -1;

	helper = new GZIPCompressorHelper (compressionLevel, noWrap);
}

GZIPCompressorOutputStream::~GZIPCompressorOutputStream()
{
	flush();
}

void GZIPCompressorOutputStream::flush()
{
	if (! helper->finished)
	{
		helper->shouldFinish = true;

		while (! helper->finished)
			doNextBlock();
	}

	destStream->flush();
}

bool GZIPCompressorOutputStream::write (const void* destBuffer, int howMany)
{
	if (! helper->finished)
	{
		helper->setInput (static_cast <const uint8*> (destBuffer), howMany);

		while (! helper->needsInput())
		{
			if (! doNextBlock())
				return false;
		}
	}

	return true;
}

bool GZIPCompressorOutputStream::doNextBlock()
{
	const int len = helper->doNextBlock (buffer, gzipCompBufferSize);

	if (len > 0)
		return destStream->write (buffer, len);
	else
		return true;
}

int64 GZIPCompressorOutputStream::getPosition()
{
	return destStream->getPosition();
}

bool GZIPCompressorOutputStream::setPosition (int64 /*newPosition*/)
{
	jassertfalse // can't do it!
	return false;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_GZIPCompressorOutputStream.cpp ***/



/*** Start of inlined file: juce_GZIPDecompressorInputStream.cpp ***/
#if JUCE_MSVC
  #pragma warning (push)
  #pragma warning (disable: 4309 4305)
#endif

namespace zlibNamespace
{
#if JUCE_INCLUDE_ZLIB_CODE
  extern "C"
  {
	#undef OS_CODE
	#undef fdopen
	#define ZLIB_INTERNAL
	#define NO_DUMMY_DECL


/*** Start of inlined file: adler32.c ***/
#define ZLIB_INTERNAL

#define BASE 65521UL	/* largest prime smaller than 65536 */
#define NMAX 5552

#define DO1(buf,i)  {adler += (buf)[i]; sum2 += adler;}
#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
#define DO16(buf)   DO8(buf,0); DO8(buf,8);

#ifdef NO_DIVIDE
#  define MOD(a) \
	do { \
		if (a >= (BASE << 16)) a -= (BASE << 16); \
		if (a >= (BASE << 15)) a -= (BASE << 15); \
		if (a >= (BASE << 14)) a -= (BASE << 14); \
		if (a >= (BASE << 13)) a -= (BASE << 13); \
		if (a >= (BASE << 12)) a -= (BASE << 12); \
		if (a >= (BASE << 11)) a -= (BASE << 11); \
		if (a >= (BASE << 10)) a -= (BASE << 10); \
		if (a >= (BASE << 9)) a -= (BASE << 9); \
		if (a >= (BASE << 8)) a -= (BASE << 8); \
		if (a >= (BASE << 7)) a -= (BASE << 7); \
		if (a >= (BASE << 6)) a -= (BASE << 6); \
		if (a >= (BASE << 5)) a -= (BASE << 5); \
		if (a >= (BASE << 4)) a -= (BASE << 4); \
		if (a >= (BASE << 3)) a -= (BASE << 3); \
		if (a >= (BASE << 2)) a -= (BASE << 2); \
		if (a >= (BASE << 1)) a -= (BASE << 1); \
		if (a >= BASE) a -= BASE; \
	} while (0)
#  define MOD4(a) \
	do { \
		if (a >= (BASE << 4)) a -= (BASE << 4); \
		if (a >= (BASE << 3)) a -= (BASE << 3); \
		if (a >= (BASE << 2)) a -= (BASE << 2); \
		if (a >= (BASE << 1)) a -= (BASE << 1); \
		if (a >= BASE) a -= BASE; \
	} while (0)
#else
#  define MOD(a) a %= BASE
#  define MOD4(a) a %= BASE
#endif

uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
{
	unsigned long sum2;
	unsigned n;

	sum2 = (adler >> 16) & 0xffff;
	adler &= 0xffff;

	if (len == 1) {
		adler += buf[0];
		if (adler >= BASE)
			adler -= BASE;
		sum2 += adler;
		if (sum2 >= BASE)
			sum2 -= BASE;
		return adler | (sum2 << 16);
	}

	if (buf == Z_NULL)
		return 1L;

	if (len < 16) {
		while (len--) {
			adler += *buf++;
			sum2 += adler;
		}
		if (adler >= BASE)
			adler -= BASE;
		MOD4(sum2);		 /* only added so many BASE's */
		return adler | (sum2 << 16);
	}

	while (len >= NMAX) {
		len -= NMAX;
		n = NMAX / 16;	  /* NMAX is divisible by 16 */
		do {
			DO16(buf);	  /* 16 sums unrolled */
			buf += 16;
		} while (--n);
		MOD(adler);
		MOD(sum2);
	}

	if (len) {		  /* avoid modulos if none remaining */
		while (len >= 16) {
			len -= 16;
			DO16(buf);
			buf += 16;
		}
		while (len--) {
			adler += *buf++;
			sum2 += adler;
		}
		MOD(adler);
		MOD(sum2);
	}

	return adler | (sum2 << 16);
}

uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2, z_off_t len2)
{
	unsigned long sum1;
	unsigned long sum2;
	unsigned rem;

	rem = (unsigned)(len2 % BASE);
	sum1 = adler1 & 0xffff;
	sum2 = rem * sum1;
	MOD(sum2);
	sum1 += (adler2 & 0xffff) + BASE - 1;
	sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
	if (sum1 > BASE) sum1 -= BASE;
	if (sum1 > BASE) sum1 -= BASE;
	if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
	if (sum2 > BASE) sum2 -= BASE;
	return sum1 | (sum2 << 16);
}
/*** End of inlined file: adler32.c ***/


/*** Start of inlined file: compress.c ***/
#define ZLIB_INTERNAL

int ZEXPORT compress2 (Bytef *dest, uLongf *destLen, const Bytef *source,
					   uLong sourceLen, int level)
{
	z_stream stream;
	int err;

	stream.next_in = (Bytef*)source;
	stream.avail_in = (uInt)sourceLen;
#ifdef MAXSEG_64K
	if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
#endif
	stream.next_out = dest;
	stream.avail_out = (uInt)*destLen;
	if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;

	stream.zalloc = (alloc_func)0;
	stream.zfree = (free_func)0;
	stream.opaque = (voidpf)0;

	err = deflateInit(&stream, level);
	if (err != Z_OK) return err;

	err = deflate(&stream, Z_FINISH);
	if (err != Z_STREAM_END) {
		deflateEnd(&stream);
		return err == Z_OK ? Z_BUF_ERROR : err;
	}
	*destLen = stream.total_out;

	err = deflateEnd(&stream);
	return err;
}

int ZEXPORT compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
{
	return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
}

uLong ZEXPORT compressBound (uLong sourceLen)
{
	return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
}
/*** End of inlined file: compress.c ***/

	#undef DO1
	#undef DO8

/*** Start of inlined file: crc32.c ***/
#ifdef MAKECRCH
#  include <stdio.h>
#  ifndef DYNAMIC_CRC_TABLE
#	define DYNAMIC_CRC_TABLE
#  endif /* !DYNAMIC_CRC_TABLE */
#endif /* MAKECRCH */


/*** Start of inlined file: zutil.h ***/
#ifndef ZUTIL_H
#define ZUTIL_H

#define ZLIB_INTERNAL

#ifdef STDC
#  ifndef _WIN32_WCE
#	include <stddef.h>
#  endif
#  include <string.h>
#  include <stdlib.h>
#endif
#ifdef NO_ERRNO_H
#   ifdef _WIN32_WCE
#	 define errno z_errno
#   endif
	extern int errno;
#else
#  ifndef _WIN32_WCE
#	include <errno.h>
#  endif
#endif

#ifndef local
#  define local static
#endif

typedef unsigned char  uch;
typedef uch FAR uchf;
typedef unsigned short ush;
typedef ush FAR ushf;
typedef unsigned long  ulg;

extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */

#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]

#define ERR_RETURN(strm,err) \
  return (strm->msg = (char*)ERR_MSG(err), (err))

#ifndef DEF_WBITS
#  define DEF_WBITS MAX_WBITS
#endif

#if MAX_MEM_LEVEL >= 8
#  define DEF_MEM_LEVEL 8
#else
#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
#endif

#define STORED_BLOCK 0
#define STATIC_TREES 1
#define DYN_TREES	2

#define MIN_MATCH  3
#define MAX_MATCH  258

#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */

#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
#  define OS_CODE  0x00
#  if defined(__TURBOC__) || defined(__BORLANDC__)
#	if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
	   void _Cdecl farfree( void *block );
	   void *_Cdecl farmalloc( unsigned long nbytes );
#	else
#	  include <alloc.h>
#	endif
#  else /* MSC or DJGPP */
#	include <malloc.h>
#  endif
#endif

#ifdef AMIGA
#  define OS_CODE  0x01
#endif

#if defined(VAXC) || defined(VMS)
#  define OS_CODE  0x02
#  define F_OPEN(name, mode) \
	 fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
#endif

#if defined(ATARI) || defined(atarist)
#  define OS_CODE  0x05
#endif

#ifdef OS2
#  define OS_CODE  0x06
#  ifdef M_I86
	 #include <malloc.h>
#  endif
#endif

#if defined(MACOS) || TARGET_OS_MAC
#  define OS_CODE  0x07
#  if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
#	include <unix.h> /* for fdopen */
#  else
#	ifndef fdopen
#	  define fdopen(fd,mode) NULL /* No fdopen() */
#	endif
#  endif
#endif

#ifdef TOPS20
#  define OS_CODE  0x0a
#endif

#ifdef WIN32
#  ifndef __CYGWIN__  /* Cygwin is Unix, not Win32 */
#	define OS_CODE  0x0b
#  endif
#endif

#ifdef __50SERIES /* Prime/PRIMOS */
#  define OS_CODE  0x0f
#endif

#if defined(_BEOS_) || defined(RISCOS)
#  define fdopen(fd,mode) NULL /* No fdopen() */
#endif

#if (defined(_MSC_VER) && (_MSC_VER > 600))
#  if defined(_WIN32_WCE)
#	define fdopen(fd,mode) NULL /* No fdopen() */
#	ifndef _PTRDIFF_T_DEFINED
	   typedef int ptrdiff_t;
#	  define _PTRDIFF_T_DEFINED
#	endif
#  else
#	define fdopen(fd,type)  _fdopen(fd,type)
#  endif
#endif

#ifndef OS_CODE
#  define OS_CODE  0x03  /* assume Unix */
#endif

#ifndef F_OPEN
#  define F_OPEN(name, mode) fopen((name), (mode))
#endif

#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
#  ifndef HAVE_VSNPRINTF
#	define HAVE_VSNPRINTF
#  endif
#endif
#if defined(__CYGWIN__)
#  ifndef HAVE_VSNPRINTF
#	define HAVE_VSNPRINTF
#  endif
#endif
#ifndef HAVE_VSNPRINTF
#  ifdef MSDOS
#	define NO_vsnprintf
#  endif
#  ifdef __TURBOC__
#	define NO_vsnprintf
#  endif
#  ifdef WIN32
#	if !defined(vsnprintf) && !defined(NO_vsnprintf)
#	  define vsnprintf _vsnprintf
#	endif
#  endif
#  ifdef __SASC
#	define NO_vsnprintf
#  endif
#endif
#ifdef VMS
#  define NO_vsnprintf
#endif

#if defined(pyr)
#  define NO_MEMCPY
#endif
#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
#  define NO_MEMCPY
#endif
#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
#  define HAVE_MEMCPY
#endif
#ifdef HAVE_MEMCPY
#  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
#	define zmemcpy _fmemcpy
#	define zmemcmp _fmemcmp
#	define zmemzero(dest, len) _fmemset(dest, 0, len)
#  else
#	define zmemcpy memcpy
#	define zmemcmp memcmp
#	define zmemzero(dest, len) memset(dest, 0, len)
#  endif
#else
   extern void zmemcpy  OF((Bytef* dest, const Bytef* source, uInt len));
   extern int  zmemcmp  OF((const Bytef* s1, const Bytef* s2, uInt len));
   extern void zmemzero OF((Bytef* dest, uInt len));
#endif

#ifdef DEBUG
#  include <stdio.h>
   extern int z_verbose;
   extern void z_error	OF((const char *m));
#  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
#  define Trace(x) {if (z_verbose>=0) fprintf x ;}
#  define Tracev(x) {if (z_verbose>0) fprintf x ;}
#  define Tracevv(x) {if (z_verbose>1) fprintf x ;}
#  define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
#  define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
#else
#  define Assert(cond,msg)
#  define Trace(x)
#  define Tracev(x)
#  define Tracevv(x)
#  define Tracec(c,x)
#  define Tracecv(c,x)
#endif

voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
void   zcfree  OF((voidpf opaque, voidpf ptr));

#define ZALLOC(strm, items, size) \
		   (*((strm)->zalloc))((strm)->opaque, (items), (size))
#define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}

#endif /* ZUTIL_H */
/*** End of inlined file: zutil.h ***/

	  /* for STDC and FAR definitions */

#define local static

#ifndef NOBYFOUR
#  ifdef STDC	   /* need ANSI C limits.h to determine sizes */
#	include <limits.h>
#	define BYFOUR
#	if (UINT_MAX == 0xffffffffUL)
	   typedef unsigned int u4;
#	else
#	  if (ULONG_MAX == 0xffffffffUL)
		 typedef unsigned long u4;
#	  else
#	if (USHRT_MAX == 0xffffffffUL)
		   typedef unsigned short u4;
#	else
#	  undef BYFOUR	 /* can't find a four-byte integer type! */
#	endif
#	  endif
#	endif
#  endif /* STDC */
#endif /* !NOBYFOUR */

#ifdef BYFOUR
#  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
				(((w)&0xff00)<<8)+(((w)&0xff)<<24))
   local unsigned long crc32_little OF((unsigned long,
						const unsigned char FAR *, unsigned));
   local unsigned long crc32_big OF((unsigned long,
						const unsigned char FAR *, unsigned));
#  define TBLS 8
#else
#  define TBLS 1
#endif /* BYFOUR */

local unsigned long gf2_matrix_times OF((unsigned long *mat,
										 unsigned long vec));
local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));

#ifdef DYNAMIC_CRC_TABLE

local volatile int crc_table_empty = 1;
local unsigned long FAR crc_table[TBLS][256];
local void make_crc_table OF((void));
#ifdef MAKECRCH
   local void write_table OF((FILE *, const unsigned long FAR *));
#endif /* MAKECRCH */
local void make_crc_table()
{
	unsigned long c;
	int n, k;
	unsigned long poly;		 /* polynomial exclusive-or pattern */
	static volatile int first = 1;	  /* flag to limit concurrent making */
	static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};

	if (first) {
		first = 0;

		poly = 0UL;
		for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
			poly |= 1UL << (31 - p[n]);

		for (n = 0; n < 256; n++) {
			c = (unsigned long)n;
			for (k = 0; k < 8; k++)
				c = c & 1 ? poly ^ (c >> 1) : c >> 1;
			crc_table[0][n] = c;
		}

#ifdef BYFOUR
		for (n = 0; n < 256; n++) {
			c = crc_table[0][n];
			crc_table[4][n] = REV(c);
			for (k = 1; k < 4; k++) {
				c = crc_table[0][c & 0xff] ^ (c >> 8);
				crc_table[k][n] = c;
				crc_table[k + 4][n] = REV(c);
			}
		}
#endif /* BYFOUR */

		crc_table_empty = 0;
	}
	else {	  /* not first */
		while (crc_table_empty)
			;
	}

#ifdef MAKECRCH
	{
		FILE *out;

		out = fopen("crc32.h", "w");
		if (out == NULL) return;
		fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
		fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
		fprintf(out, "local const unsigned long FAR ");
		fprintf(out, "crc_table[TBLS][256] =\n{\n  {\n");
		write_table(out, crc_table[0]);
#  ifdef BYFOUR
		fprintf(out, "#ifdef BYFOUR\n");
		for (k = 1; k < 8; k++) {
			fprintf(out, "  },\n  {\n");
			write_table(out, crc_table[k]);
		}
		fprintf(out, "#endif\n");
#  endif /* BYFOUR */
		fprintf(out, "  }\n};\n");
		fclose(out);
	}
#endif /* MAKECRCH */
}

#ifdef MAKECRCH
local void write_table(out, table)
	FILE *out;
	const unsigned long FAR *table;
{
	int n;

	for (n = 0; n < 256; n++)
		fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ", table[n],
				n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
}
#endif /* MAKECRCH */

#else /* !DYNAMIC_CRC_TABLE */

/*** Start of inlined file: crc32.h ***/
local const unsigned long FAR crc_table[TBLS][256] =
{
  {
	0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
	0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
	0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
	0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
	0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
	0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
	0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
	0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
	0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
	0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
	0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
	0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
	0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
	0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
	0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
	0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
	0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
	0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
	0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
	0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
	0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
	0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
	0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
	0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
	0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
	0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
	0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
	0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
	0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
	0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
	0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
	0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
	0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
	0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
	0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
	0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
	0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
	0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
	0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
	0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
	0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
	0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
	0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
	0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
	0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
	0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
	0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
	0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
	0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
	0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
	0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
	0x2d02ef8dUL
#ifdef BYFOUR
  },
  {
	0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
	0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
	0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
	0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
	0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
	0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
	0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
	0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
	0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
	0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
	0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
	0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
	0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
	0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
	0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
	0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
	0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
	0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
	0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
	0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
	0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
	0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
	0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
	0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
	0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
	0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
	0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
	0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
	0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
	0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
	0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
	0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
	0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
	0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
	0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
	0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
	0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
	0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
	0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
	0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
	0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
	0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
	0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
	0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
	0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
	0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
	0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
	0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
	0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
	0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
	0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
	0x9324fd72UL
  },
  {
	0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
	0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
	0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
	0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
	0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
	0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
	0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
	0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
	0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
	0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
	0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
	0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
	0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
	0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
	0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
	0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
	0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
	0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
	0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
	0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
	0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
	0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
	0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
	0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
	0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
	0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
	0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
	0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
	0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
	0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
	0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
	0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
	0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
	0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
	0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
	0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
	0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
	0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
	0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
	0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
	0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
	0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
	0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
	0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
	0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
	0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
	0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
	0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
	0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
	0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
	0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
	0xbe9834edUL
  },
  {
	0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
	0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
	0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
	0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
	0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
	0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
	0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
	0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
	0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
	0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
	0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
	0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
	0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
	0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
	0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
	0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
	0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
	0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
	0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
	0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
	0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
	0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
	0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
	0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
	0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
	0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
	0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
	0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
	0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
	0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
	0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
	0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
	0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
	0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
	0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
	0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
	0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
	0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
	0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
	0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
	0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
	0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
	0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
	0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
	0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
	0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
	0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
	0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
	0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
	0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
	0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
	0xde0506f1UL
  },
  {
	0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
	0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
	0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
	0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
	0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
	0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
	0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
	0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
	0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
	0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
	0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
	0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
	0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
	0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
	0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
	0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
	0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
	0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
	0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
	0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
	0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
	0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
	0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
	0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
	0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
	0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
	0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
	0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
	0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
	0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
	0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
	0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
	0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
	0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
	0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
	0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
	0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
	0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
	0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
	0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
	0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
	0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
	0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
	0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
	0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
	0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
	0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
	0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
	0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
	0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
	0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
	0x8def022dUL
  },
  {
	0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
	0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
	0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
	0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
	0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
	0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
	0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
	0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
	0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
	0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
	0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
	0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
	0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
	0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
	0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
	0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
	0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
	0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
	0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
	0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
	0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
	0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
	0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
	0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
	0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
	0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
	0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
	0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
	0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
	0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
	0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
	0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
	0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
	0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
	0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
	0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
	0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
	0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
	0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
	0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
	0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
	0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
	0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
	0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
	0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
	0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
	0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
	0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
	0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
	0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
	0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
	0x72fd2493UL
  },
  {
	0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
	0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
	0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
	0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
	0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
	0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
	0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
	0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
	0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
	0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
	0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
	0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
	0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
	0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
	0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
	0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
	0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
	0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
	0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
	0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
	0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
	0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
	0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
	0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
	0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
	0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
	0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
	0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
	0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
	0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
	0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
	0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
	0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
	0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
	0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
	0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
	0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
	0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
	0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
	0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
	0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
	0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
	0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
	0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
	0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
	0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
	0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
	0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
	0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
	0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
	0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
	0xed3498beUL
  },
  {
	0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
	0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
	0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
	0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
	0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
	0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
	0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
	0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
	0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
	0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
	0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
	0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
	0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
	0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
	0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
	0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
	0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
	0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
	0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
	0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
	0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
	0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
	0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
	0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
	0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
	0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
	0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
	0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
	0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
	0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
	0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
	0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
	0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
	0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
	0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
	0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
	0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
	0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
	0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
	0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
	0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
	0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
	0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
	0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
	0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
	0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
	0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
	0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
	0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
	0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
	0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
	0xf10605deUL
#endif
  }
};
/*** End of inlined file: crc32.h ***/


#endif /* DYNAMIC_CRC_TABLE */

const unsigned long FAR * ZEXPORT get_crc_table()
{
#ifdef DYNAMIC_CRC_TABLE
	if (crc_table_empty)
		make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */
	return (const unsigned long FAR *)crc_table;
}

#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1

unsigned long ZEXPORT crc32 (unsigned long crc, const unsigned char FAR *buf, unsigned len)
{
	if (buf == Z_NULL) return 0UL;

#ifdef DYNAMIC_CRC_TABLE
	if (crc_table_empty)
		make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */

#ifdef BYFOUR
	if (sizeof(void *) == sizeof(ptrdiff_t)) {
		u4 endian;

		endian = 1;
		if (*((unsigned char *)(&endian)))
			return crc32_little(crc, buf, len);
		else
			return crc32_big(crc, buf, len);
	}
#endif /* BYFOUR */
	crc = crc ^ 0xffffffffUL;
	while (len >= 8) {
		DO8;
		len -= 8;
	}
	if (len) do {
		DO1;
	} while (--len);
	return crc ^ 0xffffffffUL;
}

#ifdef BYFOUR

#define DOLIT4 c ^= *buf4++; \
		c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
			crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4

local unsigned long crc32_little(unsigned long crc, const unsigned char FAR *buf, unsigned len)
{
	register u4 c;
	register const u4 FAR *buf4;

	c = (u4)crc;
	c = ~c;
	while (len && ((ptrdiff_t)buf & 3)) {
		c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
		len--;
	}

	buf4 = (const u4 FAR *)(const void FAR *)buf;
	while (len >= 32) {
		DOLIT32;
		len -= 32;
	}
	while (len >= 4) {
		DOLIT4;
		len -= 4;
	}
	buf = (const unsigned char FAR *)buf4;

	if (len) do {
		c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
	} while (--len);
	c = ~c;
	return (unsigned long)c;
}

#define DOBIG4 c ^= *++buf4; \
		c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
			crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4

local unsigned long crc32_big (unsigned long crc, const unsigned char FAR *buf, unsigned len)
{
	register u4 c;
	register const u4 FAR *buf4;

	c = REV((u4)crc);
	c = ~c;
	while (len && ((ptrdiff_t)buf & 3)) {
		c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
		len--;
	}

	buf4 = (const u4 FAR *)(const void FAR *)buf;
	buf4--;
	while (len >= 32) {
		DOBIG32;
		len -= 32;
	}
	while (len >= 4) {
		DOBIG4;
		len -= 4;
	}
	buf4++;
	buf = (const unsigned char FAR *)buf4;

	if (len) do {
		c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
	} while (--len);
	c = ~c;
	return (unsigned long)(REV(c));
}

#endif /* BYFOUR */

#define GF2_DIM 32	  /* dimension of GF(2) vectors (length of CRC) */

local unsigned long gf2_matrix_times (unsigned long *mat, unsigned long vec)
{
	unsigned long sum;

	sum = 0;
	while (vec) {
		if (vec & 1)
			sum ^= *mat;
		vec >>= 1;
		mat++;
	}
	return sum;
}

local void gf2_matrix_square (unsigned long *square, unsigned long *mat)
{
	int n;

	for (n = 0; n < GF2_DIM; n++)
		square[n] = gf2_matrix_times(mat, mat[n]);
}

uLong ZEXPORT crc32_combine (uLong crc1, uLong crc2, z_off_t len2)
{
	int n;
	unsigned long row;
	unsigned long even[GF2_DIM];	/* even-power-of-two zeros operator */
	unsigned long odd[GF2_DIM];	 /* odd-power-of-two zeros operator */

	if (len2 == 0)
		return crc1;

	odd[0] = 0xedb88320L;	   /* CRC-32 polynomial */
	row = 1;
	for (n = 1; n < GF2_DIM; n++) {
		odd[n] = row;
		row <<= 1;
	}

	gf2_matrix_square(even, odd);

	gf2_matrix_square(odd, even);

	do {
		gf2_matrix_square(even, odd);
		if (len2 & 1)
			crc1 = gf2_matrix_times(even, crc1);
		len2 >>= 1;

		if (len2 == 0)
			break;

		gf2_matrix_square(odd, even);
		if (len2 & 1)
			crc1 = gf2_matrix_times(odd, crc1);
		len2 >>= 1;

	} while (len2 != 0);

	crc1 ^= crc2;
	return crc1;
}
/*** End of inlined file: crc32.c ***/



/*** Start of inlined file: deflate.c ***/

/*** Start of inlined file: deflate.h ***/
#ifndef DEFLATE_H
#define DEFLATE_H

#ifndef NO_GZIP
#  define GZIP
#endif

#define NO_DUMMY_DECL

#define LENGTH_CODES 29

#define LITERALS  256

#define L_CODES (LITERALS+1+LENGTH_CODES)

#define D_CODES   30

#define BL_CODES  19

#define HEAP_SIZE (2*L_CODES+1)

#define MAX_BITS 15

#define INIT_STATE	42
#define EXTRA_STATE   69
#define NAME_STATE	73
#define COMMENT_STATE 91
#define HCRC_STATE   103
#define BUSY_STATE   113
#define FINISH_STATE 666

typedef struct ct_data_s {
	union {
		ush  freq;	   /* frequency count */
		ush  code;	   /* bit string */
	} fc;
	union {
		ush  dad;	/* father node in Huffman tree */
		ush  len;	/* length of bit string */
	} dl;
} FAR ct_data;

#define Freq fc.freq
#define Code fc.code
#define Dad  dl.dad
#define Len  dl.len

typedef struct static_tree_desc_s  static_tree_desc;

typedef struct tree_desc_s {
	ct_data *dyn_tree;	   /* the dynamic tree */
	int	 max_code;		/* largest code with non zero frequency */
	static_tree_desc *stat_desc; /* the corresponding static tree */
} FAR tree_desc;

typedef ush Pos;
typedef Pos FAR Posf;
typedef unsigned IPos;

typedef struct internal_state {
	z_streamp strm;	  /* pointer back to this zlib stream */
	int   status;	/* as the name implies */
	Bytef *pending_buf;  /* output still pending */
	ulg   pending_buf_size; /* size of pending_buf */
	Bytef *pending_out;  /* next pending byte to output to the stream */
	uInt   pending;	  /* nb of bytes in the pending buffer */
	int   wrap;	  /* bit 0 true for zlib, bit 1 true for gzip */
	gz_headerp  gzhead;  /* gzip header information to write */
	uInt   gzindex;	  /* where in extra, name, or comment */
	Byte  method;	/* STORED (for zip only) or DEFLATED */
	int   last_flush;	/* value of flush param for previous deflate call */

	uInt  w_size;	/* LZ77 window size (32K by default) */
	uInt  w_bits;	/* log2(w_size)  (8..16) */
	uInt  w_mask;	/* w_size - 1 */

	Bytef *window;

	ulg window_size;

	Posf *prev;

	Posf *head; /* Heads of the hash chains or NIL. */

	uInt  ins_h;	  /* hash index of string to be inserted */
	uInt  hash_size;	  /* number of elements in hash table */
	uInt  hash_bits;	  /* log2(hash_size) */
	uInt  hash_mask;	  /* hash_size-1 */

	uInt  hash_shift;

	long block_start;

	uInt match_length;	   /* length of best match */
	IPos prev_match;		 /* previous match */
	int match_available;	 /* set if previous match exists */
	uInt strstart;		   /* start of string to insert */
	uInt match_start;		/* start of matching string */
	uInt lookahead;		  /* number of valid bytes ahead in window */

	uInt prev_length;

	uInt max_chain_length;

	uInt max_lazy_match;
#   define max_insert_length  max_lazy_match

	int level;	/* compression level (1..9) */
	int strategy; /* favor or force Huffman coding*/

	uInt good_match;

	int nice_match; /* Stop searching when current match exceeds this */

	struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
	struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
	struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */

	struct tree_desc_s l_desc;		   /* desc. for literal tree */
	struct tree_desc_s d_desc;		   /* desc. for distance tree */
	struct tree_desc_s bl_desc;		  /* desc. for bit length tree */

	ush bl_count[MAX_BITS+1];

	int heap[2*L_CODES+1];	  /* heap used to build the Huffman trees */
	int heap_len;		   /* number of elements in the heap */
	int heap_max;		   /* element of largest frequency */

	uch depth[2*L_CODES+1];

	uchf *l_buf;	  /* buffer for literals or lengths */

	uInt  lit_bufsize;

	uInt last_lit;	  /* running index in l_buf */

	ushf *d_buf;

	ulg opt_len;	/* bit length of current block with optimal trees */
	ulg static_len;	 /* bit length of current block with static trees */
	uInt matches;	   /* number of string matches in current block */
	int last_eob_len;   /* bit length of EOB code for last block */

#ifdef DEBUG
	ulg compressed_len; /* total bit length of compressed file mod 2^32 */
	ulg bits_sent;	  /* bit length of compressed data sent mod 2^32 */
#endif

	ush bi_buf;
	int bi_valid;

} FAR deflate_state;

#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}

#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)

#define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)

void _tr_init	 OF((deflate_state *s));
int  _tr_tally	OF((deflate_state *s, unsigned dist, unsigned lc));
void _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
						  int eof));
void _tr_align	OF((deflate_state *s));
void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
						  int eof));

#define d_code(dist) \
   ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])

#ifndef DEBUG

#if defined(GEN_TREES_H) || !defined(STDC)
  extern uch _length_code[];
  extern uch _dist_code[];
#else
  extern const uch _length_code[];
  extern const uch _dist_code[];
#endif

# define _tr_tally_lit(s, c, flush) \
  { uch cc = (c); \
	s->d_buf[s->last_lit] = 0; \
	s->l_buf[s->last_lit++] = cc; \
	s->dyn_ltree[cc].Freq++; \
	flush = (s->last_lit == s->lit_bufsize-1); \
   }
# define _tr_tally_dist(s, distance, length, flush) \
  { uch len = (length); \
	ush dist = (distance); \
	s->d_buf[s->last_lit] = dist; \
	s->l_buf[s->last_lit++] = len; \
	dist--; \
	s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
	s->dyn_dtree[d_code(dist)].Freq++; \
	flush = (s->last_lit == s->lit_bufsize-1); \
  }
#else
# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
# define _tr_tally_dist(s, distance, length, flush) \
			  flush = _tr_tally(s, distance, length)
#endif

#endif /* DEFLATE_H */
/*** End of inlined file: deflate.h ***/

const char deflate_copyright[] =
   " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";

typedef enum {
	need_more,	  /* block not completed, need more input or more output */
	block_done,	 /* block flush performed */
	finish_started, /* finish started, need only more output at next deflate */
	finish_done	 /* finish done, accept no more input or output */
} block_state;

typedef block_state (*compress_func) OF((deflate_state *s, int flush));

local void fill_window	OF((deflate_state *s));
local block_state deflate_stored OF((deflate_state *s, int flush));
local block_state deflate_fast   OF((deflate_state *s, int flush));
#ifndef FASTEST
local block_state deflate_slow   OF((deflate_state *s, int flush));
#endif
local void lm_init	OF((deflate_state *s));
local void putShortMSB	OF((deflate_state *s, uInt b));
local void flush_pending  OF((z_streamp strm));
local int read_buf	OF((z_streamp strm, Bytef *buf, unsigned size));
#ifndef FASTEST
#ifdef ASMV
	  void match_init OF((void)); /* asm code initialization */
	  uInt longest_match  OF((deflate_state *s, IPos cur_match));
#else
local uInt longest_match  OF((deflate_state *s, IPos cur_match));
#endif
#endif
local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));

#ifdef DEBUG
local  void check_match OF((deflate_state *s, IPos start, IPos match,
							int length));
#endif

#define NIL 0

#ifndef TOO_FAR
#  define TOO_FAR 4096
#endif

#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)

typedef struct config_s {
   ush good_length; /* reduce lazy search above this match length */
   ush max_lazy;	/* do not perform lazy search above this match length */
   ush nice_length; /* quit search above this match length */
   ush max_chain;
   compress_func func;
} config;

#ifdef FASTEST
local const config configuration_table[2] = {
 {0,	0,  0,	0, deflate_stored},  /* store only */
 {4,	4,  8,	4, deflate_fast}}; /* max speed, no lazy matches */
#else
local const config configuration_table[10] = {
 {0,	0,  0,	0, deflate_stored},  /* store only */
 {4,	4,  8,	4, deflate_fast}, /* max speed, no lazy matches */
 {4,	5, 16,	8, deflate_fast},
 {4,	6, 32,   32, deflate_fast},

 {4,	4, 16,   16, deflate_slow},  /* lazy matches */
 {8,   16, 32,   32, deflate_slow},
 {8,   16, 128, 128, deflate_slow},
 {8,   32, 128, 256, deflate_slow},
 {32, 128, 258, 1024, deflate_slow},
 {32, 258, 258, 4096, deflate_slow}}; /* max compression */
#endif

#define EQUAL 0

#ifndef NO_DUMMY_DECL
struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
#endif

#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)

#ifdef FASTEST
#define INSERT_STRING(s, str, match_head) \
   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
	match_head = s->head[s->ins_h], \
	s->head[s->ins_h] = (Pos)(str))
#else
#define INSERT_STRING(s, str, match_head) \
   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
	match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
	s->head[s->ins_h] = (Pos)(str))
#endif

#define CLEAR_HASH(s) \
	s->head[s->hash_size-1] = NIL; \
	zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));

int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
{
	return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
						 Z_DEFAULT_STRATEGY, version, stream_size);
}

int ZEXPORT deflateInit2_ (z_streamp strm, int  level, int  method, int  windowBits, int  memLevel, int  strategy, const char *version, int stream_size)
{
	deflate_state *s;
	int wrap = 1;
	static const char my_version[] = ZLIB_VERSION;

	ushf *overlay;

	if (version == Z_NULL || version[0] != my_version[0] ||
		stream_size != sizeof(z_stream)) {
		return Z_VERSION_ERROR;
	}
	if (strm == Z_NULL) return Z_STREAM_ERROR;

	strm->msg = Z_NULL;
	if (strm->zalloc == (alloc_func)0) {
		strm->zalloc = zcalloc;
		strm->opaque = (voidpf)0;
	}
	if (strm->zfree == (free_func)0) strm->zfree = zcfree;

#ifdef FASTEST
	if (level != 0) level = 1;
#else
	if (level == Z_DEFAULT_COMPRESSION) level = 6;
#endif

	if (windowBits < 0) { /* suppress zlib wrapper */
		wrap = 0;
		windowBits = -windowBits;
	}
#ifdef GZIP
	else if (windowBits > 15) {
		wrap = 2;	   /* write gzip wrapper instead */
		windowBits -= 16;
	}
#endif
	if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
		windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
		strategy < 0 || strategy > Z_FIXED) {
		return Z_STREAM_ERROR;
	}
	if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
	s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
	if (s == Z_NULL) return Z_MEM_ERROR;
	strm->state = (struct internal_state FAR *)s;
	s->strm = strm;

	s->wrap = wrap;
	s->gzhead = Z_NULL;
	s->w_bits = windowBits;
	s->w_size = 1 << s->w_bits;
	s->w_mask = s->w_size - 1;

	s->hash_bits = memLevel + 7;
	s->hash_size = 1 << s->hash_bits;
	s->hash_mask = s->hash_size - 1;
	s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);

	s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
	s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
	s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));

	s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */

	overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
	s->pending_buf = (uchf *) overlay;
	s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);

	if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
		s->pending_buf == Z_NULL) {
		s->status = FINISH_STATE;
		strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
		deflateEnd (strm);
		return Z_MEM_ERROR;
	}
	s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
	s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;

	s->level = level;
	s->strategy = strategy;
	s->method = (Byte)method;

	return deflateReset(strm);
}

int ZEXPORT deflateSetDictionary (z_streamp strm, const Bytef *dictionary, uInt  dictLength)
{
	deflate_state *s;
	uInt length = dictLength;
	uInt n;
	IPos hash_head = 0;

	if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
		strm->state->wrap == 2 ||
		(strm->state->wrap == 1 && strm->state->status != INIT_STATE))
		return Z_STREAM_ERROR;

	s = strm->state;
	if (s->wrap)
		strm->adler = adler32(strm->adler, dictionary, dictLength);

	if (length < MIN_MATCH) return Z_OK;
	if (length > MAX_DIST(s)) {
		length = MAX_DIST(s);
		dictionary += dictLength - length; /* use the tail of the dictionary */
	}
	zmemcpy(s->window, dictionary, length);
	s->strstart = length;
	s->block_start = (long)length;

	s->ins_h = s->window[0];
	UPDATE_HASH(s, s->ins_h, s->window[1]);
	for (n = 0; n <= length - MIN_MATCH; n++) {
		INSERT_STRING(s, n, hash_head);
	}
	if (hash_head) hash_head = 0;  /* to make compiler happy */
	return Z_OK;
}

int ZEXPORT deflateReset (z_streamp strm)
{
	deflate_state *s;

	if (strm == Z_NULL || strm->state == Z_NULL ||
		strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
		return Z_STREAM_ERROR;
	}

	strm->total_in = strm->total_out = 0;
	strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
	strm->data_type = Z_UNKNOWN;

	s = (deflate_state *)strm->state;
	s->pending = 0;
	s->pending_out = s->pending_buf;

	if (s->wrap < 0) {
		s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
	}
	s->status = s->wrap ? INIT_STATE : BUSY_STATE;
	strm->adler =
#ifdef GZIP
		s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
#endif
		adler32(0L, Z_NULL, 0);
	s->last_flush = Z_NO_FLUSH;

	_tr_init(s);
	lm_init(s);

	return Z_OK;
}

int ZEXPORT deflateSetHeader (z_streamp strm, gz_headerp head)
{
	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	if (strm->state->wrap != 2) return Z_STREAM_ERROR;
	strm->state->gzhead = head;
	return Z_OK;
}

int ZEXPORT deflatePrime (z_streamp strm, int bits, int value)
{
	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	strm->state->bi_valid = bits;
	strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
	return Z_OK;
}

int ZEXPORT deflateParams (z_streamp strm, int level, int strategy)
{
	deflate_state *s;
	compress_func func;
	int err = Z_OK;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	s = strm->state;

#ifdef FASTEST
	if (level != 0) level = 1;
#else
	if (level == Z_DEFAULT_COMPRESSION) level = 6;
#endif
	if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
		return Z_STREAM_ERROR;
	}
	func = configuration_table[s->level].func;

	if (func != configuration_table[level].func && strm->total_in != 0) {
		err = deflate(strm, Z_PARTIAL_FLUSH);
	}
	if (s->level != level) {
		s->level = level;
		s->max_lazy_match   = configuration_table[level].max_lazy;
		s->good_match	   = configuration_table[level].good_length;
		s->nice_match	   = configuration_table[level].nice_length;
		s->max_chain_length = configuration_table[level].max_chain;
	}
	s->strategy = strategy;
	return err;
}

int ZEXPORT deflateTune (z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
{
	deflate_state *s;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	s = strm->state;
	s->good_match = good_length;
	s->max_lazy_match = max_lazy;
	s->nice_match = nice_length;
	s->max_chain_length = max_chain;
	return Z_OK;
}

uLong ZEXPORT deflateBound (z_streamp strm, uLong sourceLen)
{
	deflate_state *s;
	uLong destLen;

	destLen = sourceLen +
			  ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;

	if (strm == Z_NULL || strm->state == Z_NULL)
		return destLen;

	s = strm->state;
	if (s->w_bits != 15 || s->hash_bits != 8 + 7)
		return destLen;

	return compressBound(sourceLen);
}

local void putShortMSB (deflate_state *s, uInt b)
{
	put_byte(s, (Byte)(b >> 8));
	put_byte(s, (Byte)(b & 0xff));
}

local void flush_pending (z_streamp strm)
{
	unsigned len = strm->state->pending;

	if (len > strm->avail_out) len = strm->avail_out;
	if (len == 0) return;

	zmemcpy(strm->next_out, strm->state->pending_out, len);
	strm->next_out  += len;
	strm->state->pending_out  += len;
	strm->total_out += len;
	strm->avail_out  -= len;
	strm->state->pending -= len;
	if (strm->state->pending == 0) {
		strm->state->pending_out = strm->state->pending_buf;
	}
}

int ZEXPORT deflate (z_streamp strm, int flush)
{
	int old_flush; /* value of flush param for previous deflate call */
	deflate_state *s;

	if (strm == Z_NULL || strm->state == Z_NULL ||
		flush > Z_FINISH || flush < 0) {
		return Z_STREAM_ERROR;
	}
	s = strm->state;

	if (strm->next_out == Z_NULL ||
		(strm->next_in == Z_NULL && strm->avail_in != 0) ||
		(s->status == FINISH_STATE && flush != Z_FINISH)) {
		ERR_RETURN(strm, Z_STREAM_ERROR);
	}
	if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);

	s->strm = strm; /* just in case */
	old_flush = s->last_flush;
	s->last_flush = flush;

	if (s->status == INIT_STATE) {
#ifdef GZIP
		if (s->wrap == 2) {
			strm->adler = crc32(0L, Z_NULL, 0);
			put_byte(s, 31);
			put_byte(s, 139);
			put_byte(s, 8);
			if (s->gzhead == NULL) {
				put_byte(s, 0);
				put_byte(s, 0);
				put_byte(s, 0);
				put_byte(s, 0);
				put_byte(s, 0);
				put_byte(s, s->level == 9 ? 2 :
							(s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
							 4 : 0));
				put_byte(s, OS_CODE);
				s->status = BUSY_STATE;
			}
			else {
				put_byte(s, (s->gzhead->text ? 1 : 0) +
							(s->gzhead->hcrc ? 2 : 0) +
							(s->gzhead->extra == Z_NULL ? 0 : 4) +
							(s->gzhead->name == Z_NULL ? 0 : 8) +
							(s->gzhead->comment == Z_NULL ? 0 : 16)
						);
				put_byte(s, (Byte)(s->gzhead->time & 0xff));
				put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
				put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
				put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
				put_byte(s, s->level == 9 ? 2 :
							(s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
							 4 : 0));
				put_byte(s, s->gzhead->os & 0xff);
				if (s->gzhead->extra != NULL) {
					put_byte(s, s->gzhead->extra_len & 0xff);
					put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
				}
				if (s->gzhead->hcrc)
					strm->adler = crc32(strm->adler, s->pending_buf,
										s->pending);
				s->gzindex = 0;
				s->status = EXTRA_STATE;
			}
		}
		else
#endif
		{
			uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
			uInt level_flags;

			if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
				level_flags = 0;
			else if (s->level < 6)
				level_flags = 1;
			else if (s->level == 6)
				level_flags = 2;
			else
				level_flags = 3;
			header |= (level_flags << 6);
			if (s->strstart != 0) header |= PRESET_DICT;
			header += 31 - (header % 31);

			s->status = BUSY_STATE;
			putShortMSB(s, header);

			if (s->strstart != 0) {
				putShortMSB(s, (uInt)(strm->adler >> 16));
				putShortMSB(s, (uInt)(strm->adler & 0xffff));
			}
			strm->adler = adler32(0L, Z_NULL, 0);
		}
	}
#ifdef GZIP
	if (s->status == EXTRA_STATE) {
		if (s->gzhead->extra != NULL) {
			uInt beg = s->pending;  /* start of bytes to update crc */

			while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
				if (s->pending == s->pending_buf_size) {
					if (s->gzhead->hcrc && s->pending > beg)
						strm->adler = crc32(strm->adler, s->pending_buf + beg,
											s->pending - beg);
					flush_pending(strm);
					beg = s->pending;
					if (s->pending == s->pending_buf_size)
						break;
				}
				put_byte(s, s->gzhead->extra[s->gzindex]);
				s->gzindex++;
			}
			if (s->gzhead->hcrc && s->pending > beg)
				strm->adler = crc32(strm->adler, s->pending_buf + beg,
									s->pending - beg);
			if (s->gzindex == s->gzhead->extra_len) {
				s->gzindex = 0;
				s->status = NAME_STATE;
			}
		}
		else
			s->status = NAME_STATE;
	}
	if (s->status == NAME_STATE) {
		if (s->gzhead->name != NULL) {
			uInt beg = s->pending;  /* start of bytes to update crc */
			int val;

			do {
				if (s->pending == s->pending_buf_size) {
					if (s->gzhead->hcrc && s->pending > beg)
						strm->adler = crc32(strm->adler, s->pending_buf + beg,
											s->pending - beg);
					flush_pending(strm);
					beg = s->pending;
					if (s->pending == s->pending_buf_size) {
						val = 1;
						break;
					}
				}
				val = s->gzhead->name[s->gzindex++];
				put_byte(s, val);
			} while (val != 0);
			if (s->gzhead->hcrc && s->pending > beg)
				strm->adler = crc32(strm->adler, s->pending_buf + beg,
									s->pending - beg);
			if (val == 0) {
				s->gzindex = 0;
				s->status = COMMENT_STATE;
			}
		}
		else
			s->status = COMMENT_STATE;
	}
	if (s->status == COMMENT_STATE) {
		if (s->gzhead->comment != NULL) {
			uInt beg = s->pending;  /* start of bytes to update crc */
			int val;

			do {
				if (s->pending == s->pending_buf_size) {
					if (s->gzhead->hcrc && s->pending > beg)
						strm->adler = crc32(strm->adler, s->pending_buf + beg,
											s->pending - beg);
					flush_pending(strm);
					beg = s->pending;
					if (s->pending == s->pending_buf_size) {
						val = 1;
						break;
					}
				}
				val = s->gzhead->comment[s->gzindex++];
				put_byte(s, val);
			} while (val != 0);
			if (s->gzhead->hcrc && s->pending > beg)
				strm->adler = crc32(strm->adler, s->pending_buf + beg,
									s->pending - beg);
			if (val == 0)
				s->status = HCRC_STATE;
		}
		else
			s->status = HCRC_STATE;
	}
	if (s->status == HCRC_STATE) {
		if (s->gzhead->hcrc) {
			if (s->pending + 2 > s->pending_buf_size)
				flush_pending(strm);
			if (s->pending + 2 <= s->pending_buf_size) {
				put_byte(s, (Byte)(strm->adler & 0xff));
				put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
				strm->adler = crc32(0L, Z_NULL, 0);
				s->status = BUSY_STATE;
			}
		}
		else
			s->status = BUSY_STATE;
	}
#endif

	if (s->pending != 0) {
		flush_pending(strm);
		if (strm->avail_out == 0) {
			s->last_flush = -1;
			return Z_OK;
		}

	} else if (strm->avail_in == 0 && flush <= old_flush &&
			   flush != Z_FINISH) {
		ERR_RETURN(strm, Z_BUF_ERROR);
	}

	if (s->status == FINISH_STATE && strm->avail_in != 0) {
		ERR_RETURN(strm, Z_BUF_ERROR);
	}

	if (strm->avail_in != 0 || s->lookahead != 0 ||
		(flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
		block_state bstate;

		bstate = (*(configuration_table[s->level].func))(s, flush);

		if (bstate == finish_started || bstate == finish_done) {
			s->status = FINISH_STATE;
		}
		if (bstate == need_more || bstate == finish_started) {
			if (strm->avail_out == 0) {
				s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
			}
			return Z_OK;
		}
		if (bstate == block_done) {
			if (flush == Z_PARTIAL_FLUSH) {
				_tr_align(s);
			} else { /* FULL_FLUSH or SYNC_FLUSH */
				_tr_stored_block(s, (char*)0, 0L, 0);
				if (flush == Z_FULL_FLUSH) {
					CLEAR_HASH(s);		 /* forget history */
				}
			}
			flush_pending(strm);
			if (strm->avail_out == 0) {
			  s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
			  return Z_OK;
			}
		}
	}
	Assert(strm->avail_out > 0, "bug2");

	if (flush != Z_FINISH) return Z_OK;
	if (s->wrap <= 0) return Z_STREAM_END;

#ifdef GZIP
	if (s->wrap == 2) {
		put_byte(s, (Byte)(strm->adler & 0xff));
		put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
		put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
		put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
		put_byte(s, (Byte)(strm->total_in & 0xff));
		put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
		put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
		put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
	}
	else
#endif
	{
		putShortMSB(s, (uInt)(strm->adler >> 16));
		putShortMSB(s, (uInt)(strm->adler & 0xffff));
	}
	flush_pending(strm);
	if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
	return s->pending != 0 ? Z_OK : Z_STREAM_END;
}

int ZEXPORT deflateEnd (z_streamp strm)
{
	int status;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;

	status = strm->state->status;
	if (status != INIT_STATE &&
		status != EXTRA_STATE &&
		status != NAME_STATE &&
		status != COMMENT_STATE &&
		status != HCRC_STATE &&
		status != BUSY_STATE &&
		status != FINISH_STATE) {
	  return Z_STREAM_ERROR;
	}

	TRY_FREE(strm, strm->state->pending_buf);
	TRY_FREE(strm, strm->state->head);
	TRY_FREE(strm, strm->state->prev);
	TRY_FREE(strm, strm->state->window);

	ZFREE(strm, strm->state);
	strm->state = Z_NULL;

	return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
}

int ZEXPORT deflateCopy (z_streamp dest, z_streamp source)
{
#ifdef MAXSEG_64K
	return Z_STREAM_ERROR;
#else
	deflate_state *ds;
	deflate_state *ss;
	ushf *overlay;

	if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
		return Z_STREAM_ERROR;
	}

	ss = source->state;

	zmemcpy(dest, source, sizeof(z_stream));

	ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
	if (ds == Z_NULL) return Z_MEM_ERROR;
	dest->state = (struct internal_state FAR *) ds;
	zmemcpy(ds, ss, sizeof(deflate_state));
	ds->strm = dest;

	ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
	ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
	ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
	overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
	ds->pending_buf = (uchf *) overlay;

	if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
		ds->pending_buf == Z_NULL) {
		deflateEnd (dest);
		return Z_MEM_ERROR;
	}
	zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
	zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
	zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
	zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);

	ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
	ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
	ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;

	ds->l_desc.dyn_tree = ds->dyn_ltree;
	ds->d_desc.dyn_tree = ds->dyn_dtree;
	ds->bl_desc.dyn_tree = ds->bl_tree;

	return Z_OK;
#endif /* MAXSEG_64K */
}

local int read_buf (z_streamp strm, Bytef *buf, unsigned size)
{
	unsigned len = strm->avail_in;

	if (len > size) len = size;
	if (len == 0) return 0;

	strm->avail_in  -= len;

	if (strm->state->wrap == 1) {
		strm->adler = adler32(strm->adler, strm->next_in, len);
	}
#ifdef GZIP
	else if (strm->state->wrap == 2) {
		strm->adler = crc32(strm->adler, strm->next_in, len);
	}
#endif
	zmemcpy(buf, strm->next_in, len);
	strm->next_in  += len;
	strm->total_in += len;

	return (int)len;
}

local void lm_init (deflate_state *s)
{
	s->window_size = (ulg)2L*s->w_size;

	CLEAR_HASH(s);

	s->max_lazy_match   = configuration_table[s->level].max_lazy;
	s->good_match	   = configuration_table[s->level].good_length;
	s->nice_match	   = configuration_table[s->level].nice_length;
	s->max_chain_length = configuration_table[s->level].max_chain;

	s->strstart = 0;
	s->block_start = 0L;
	s->lookahead = 0;
	s->match_length = s->prev_length = MIN_MATCH-1;
	s->match_available = 0;
	s->ins_h = 0;
#ifndef FASTEST
#ifdef ASMV
	match_init(); /* initialize the asm code */
#endif
#endif
}

#ifndef FASTEST
#ifndef ASMV
local uInt longest_match(deflate_state *s, IPos cur_match)
{
	unsigned chain_length = s->max_chain_length;/* max hash chain length */
	register Bytef *scan = s->window + s->strstart; /* current string */
	register Bytef *match;			   /* matched string */
	register int len;			   /* length of current match */
	int best_len = s->prev_length;		  /* best match length so far */
	int nice_match = s->nice_match;		 /* stop if match long enough */
	IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
		s->strstart - (IPos)MAX_DIST(s) : NIL;
	Posf *prev = s->prev;
	uInt wmask = s->w_mask;

#ifdef UNALIGNED_OK
	register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
	register ush scan_start = *(ushf*)scan;
	register ush scan_end   = *(ushf*)(scan+best_len-1);
#else
	register Bytef *strend = s->window + s->strstart + MAX_MATCH;
	register Byte scan_end1  = scan[best_len-1];
	register Byte scan_end   = scan[best_len];
#endif

	Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");

	if (s->prev_length >= s->good_match) {
		chain_length >>= 2;
	}
	if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;

	Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");

	do {
		Assert(cur_match < s->strstart, "no future");
		match = s->window + cur_match;

#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
		if (*(ushf*)(match+best_len-1) != scan_end ||
			*(ushf*)match != scan_start) continue;

		Assert(scan[2] == match[2], "scan[2]?");
		scan++, match++;
		do {
		} while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
				 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
				 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
				 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
				 scan < strend);

		Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
		if (*scan == *match) scan++;

		len = (MAX_MATCH - 1) - (int)(strend-scan);
		scan = strend - (MAX_MATCH-1);

#else /* UNALIGNED_OK */

		if (match[best_len]   != scan_end  ||
			match[best_len-1] != scan_end1 ||
			*match		!= *scan	 ||
			*++match	  != scan[1])	  continue;

		scan += 2, match++;
		Assert(*scan == *match, "match[2]?");

		do {
		} while (*++scan == *++match && *++scan == *++match &&
				 *++scan == *++match && *++scan == *++match &&
				 *++scan == *++match && *++scan == *++match &&
				 *++scan == *++match && *++scan == *++match &&
				 scan < strend);

		Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");

		len = MAX_MATCH - (int)(strend - scan);
		scan = strend - MAX_MATCH;

#endif /* UNALIGNED_OK */

		if (len > best_len) {
			s->match_start = cur_match;
			best_len = len;
			if (len >= nice_match) break;
#ifdef UNALIGNED_OK
			scan_end = *(ushf*)(scan+best_len-1);
#else
			scan_end1  = scan[best_len-1];
			scan_end   = scan[best_len];
#endif
		}
	} while ((cur_match = prev[cur_match & wmask]) > limit
			 && --chain_length != 0);

	if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
	return s->lookahead;
}
#endif /* ASMV */
#endif /* FASTEST */

local uInt longest_match_fast (deflate_state *s, IPos cur_match)
{
	register Bytef *scan = s->window + s->strstart; /* current string */
	register Bytef *match;			   /* matched string */
	register int len;			   /* length of current match */
	register Bytef *strend = s->window + s->strstart + MAX_MATCH;

	Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");

	Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");

	Assert(cur_match < s->strstart, "no future");

	match = s->window + cur_match;

	if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;

	scan += 2, match += 2;
	Assert(*scan == *match, "match[2]?");

	do {
	} while (*++scan == *++match && *++scan == *++match &&
			 *++scan == *++match && *++scan == *++match &&
			 *++scan == *++match && *++scan == *++match &&
			 *++scan == *++match && *++scan == *++match &&
			 scan < strend);

	Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");

	len = MAX_MATCH - (int)(strend - scan);

	if (len < MIN_MATCH) return MIN_MATCH - 1;

	s->match_start = cur_match;
	return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
}

#ifdef DEBUG
local void check_match(deflate_state *s, IPos start, IPos match, int length)
{
	if (zmemcmp(s->window + match,
				s->window + start, length) != EQUAL) {
		fprintf(stderr, " start %u, match %u, length %d\n",
				start, match, length);
		do {
			fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
		} while (--length != 0);
		z_error("invalid match");
	}
	if (z_verbose > 1) {
		fprintf(stderr,"\\[%d,%d]", start-match, length);
		do { putc(s->window[start++], stderr); } while (--length != 0);
	}
}
#else
#  define check_match(s, start, match, length)
#endif /* DEBUG */

local void fill_window (deflate_state *s)
{
	register unsigned n, m;
	register Posf *p;
	unsigned more;	/* Amount of free space at the end of the window. */
	uInt wsize = s->w_size;

	do {
		more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);

		if (sizeof(int) <= 2) {
			if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
				more = wsize;

			} else if (more == (unsigned)(-1)) {
				more--;
			}
		}

		if (s->strstart >= wsize+MAX_DIST(s)) {

			zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
			s->match_start -= wsize;
			s->strstart	-= wsize; /* we now have strstart >= MAX_DIST */
			s->block_start -= (long) wsize;

			n = s->hash_size;
			p = &s->head[n];
			do {
				m = *--p;
				*p = (Pos)(m >= wsize ? m-wsize : NIL);
			} while (--n);

			n = wsize;
#ifndef FASTEST
			p = &s->prev[n];
			do {
				m = *--p;
				*p = (Pos)(m >= wsize ? m-wsize : NIL);
			} while (--n);
#endif
			more += wsize;
		}
		if (s->strm->avail_in == 0) return;

		Assert(more >= 2, "more < 2");

		n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
		s->lookahead += n;

		if (s->lookahead >= MIN_MATCH) {
			s->ins_h = s->window[s->strstart];
			UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
#if MIN_MATCH != 3
			Call UPDATE_HASH() MIN_MATCH-3 more times
#endif
		}

	} while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
}

#define FLUSH_BLOCK_ONLY(s, eof) { \
   _tr_flush_block(s, (s->block_start >= 0L ? \
				   (charf *)&s->window[(unsigned)s->block_start] : \
				   (charf *)Z_NULL), \
				(ulg)((long)s->strstart - s->block_start), \
				(eof)); \
   s->block_start = s->strstart; \
   flush_pending(s->strm); \
   Tracev((stderr,"[FLUSH]")); \
}

#define FLUSH_BLOCK(s, eof) { \
   FLUSH_BLOCK_ONLY(s, eof); \
   if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
}

local block_state deflate_stored(deflate_state *s, int flush)
{
	ulg max_block_size = 0xffff;
	ulg max_start;

	if (max_block_size > s->pending_buf_size - 5) {
		max_block_size = s->pending_buf_size - 5;
	}

	for (;;) {
		if (s->lookahead <= 1) {

			Assert(s->strstart < s->w_size+MAX_DIST(s) ||
				   s->block_start >= (long)s->w_size, "slide too late");

			fill_window(s);
			if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;

			if (s->lookahead == 0) break; /* flush the current block */
		}
		Assert(s->block_start >= 0L, "block gone");

		s->strstart += s->lookahead;
		s->lookahead = 0;

		max_start = s->block_start + max_block_size;
		if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
			s->lookahead = (uInt)(s->strstart - max_start);
			s->strstart = (uInt)max_start;
			FLUSH_BLOCK(s, 0);
		}
		if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
			FLUSH_BLOCK(s, 0);
		}
	}
	FLUSH_BLOCK(s, flush == Z_FINISH);
	return flush == Z_FINISH ? finish_done : block_done;
}

local block_state deflate_fast(deflate_state *s, int flush)
{
	IPos hash_head = NIL; /* head of the hash chain */
	int bflush;	   /* set if current block must be flushed */

	for (;;) {
		if (s->lookahead < MIN_LOOKAHEAD) {
			fill_window(s);
			if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
				return need_more;
			}
			if (s->lookahead == 0) break; /* flush the current block */
		}

		if (s->lookahead >= MIN_MATCH) {
			INSERT_STRING(s, s->strstart, hash_head);
		}

		if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
#ifdef FASTEST
			if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
				(s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
				s->match_length = longest_match_fast (s, hash_head);
			}
#else
			if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
				s->match_length = longest_match (s, hash_head);
			} else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
				s->match_length = longest_match_fast (s, hash_head);
			}
#endif
		}
		if (s->match_length >= MIN_MATCH) {
			check_match(s, s->strstart, s->match_start, s->match_length);

			_tr_tally_dist(s, s->strstart - s->match_start,
						   s->match_length - MIN_MATCH, bflush);

			s->lookahead -= s->match_length;

#ifndef FASTEST
			if (s->match_length <= s->max_insert_length &&
				s->lookahead >= MIN_MATCH) {
				s->match_length--; /* string at strstart already in table */
				do {
					s->strstart++;
					INSERT_STRING(s, s->strstart, hash_head);
				} while (--s->match_length != 0);
				s->strstart++;
			} else
#endif
			{
				s->strstart += s->match_length;
				s->match_length = 0;
				s->ins_h = s->window[s->strstart];
				UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
#if MIN_MATCH != 3
				Call UPDATE_HASH() MIN_MATCH-3 more times
#endif
			}
		} else {
			Tracevv((stderr,"%c", s->window[s->strstart]));
			_tr_tally_lit (s, s->window[s->strstart], bflush);
			s->lookahead--;
			s->strstart++;
		}
		if (bflush) FLUSH_BLOCK(s, 0);
	}
	FLUSH_BLOCK(s, flush == Z_FINISH);
	return flush == Z_FINISH ? finish_done : block_done;
}

#ifndef FASTEST
local block_state deflate_slow(deflate_state *s, int flush)
{
	IPos hash_head = NIL;	/* head of hash chain */
	int bflush;		  /* set if current block must be flushed */

	for (;;) {
		if (s->lookahead < MIN_LOOKAHEAD) {
			fill_window(s);
			if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
				return need_more;
			}
			if (s->lookahead == 0) break; /* flush the current block */
		}

		if (s->lookahead >= MIN_MATCH) {
			INSERT_STRING(s, s->strstart, hash_head);
		}

		s->prev_length = s->match_length, s->prev_match = s->match_start;
		s->match_length = MIN_MATCH-1;

		if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
			s->strstart - hash_head <= MAX_DIST(s)) {
			if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
				s->match_length = longest_match (s, hash_head);
			} else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
				s->match_length = longest_match_fast (s, hash_head);
			}

			if (s->match_length <= 5 && (s->strategy == Z_FILTERED
#if TOO_FAR <= 32767
				|| (s->match_length == MIN_MATCH &&
					s->strstart - s->match_start > TOO_FAR)
#endif
				)) {

				s->match_length = MIN_MATCH-1;
			}
		}
		if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
			uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;

			check_match(s, s->strstart-1, s->prev_match, s->prev_length);

			_tr_tally_dist(s, s->strstart -1 - s->prev_match,
						   s->prev_length - MIN_MATCH, bflush);

			s->lookahead -= s->prev_length-1;
			s->prev_length -= 2;
			do {
				if (++s->strstart <= max_insert) {
					INSERT_STRING(s, s->strstart, hash_head);
				}
			} while (--s->prev_length != 0);
			s->match_available = 0;
			s->match_length = MIN_MATCH-1;
			s->strstart++;

			if (bflush) FLUSH_BLOCK(s, 0);

		} else if (s->match_available) {
			Tracevv((stderr,"%c", s->window[s->strstart-1]));
			_tr_tally_lit(s, s->window[s->strstart-1], bflush);
			if (bflush) {
				FLUSH_BLOCK_ONLY(s, 0);
			}
			s->strstart++;
			s->lookahead--;
			if (s->strm->avail_out == 0) return need_more;
		} else {
			s->match_available = 1;
			s->strstart++;
			s->lookahead--;
		}
	}
	Assert (flush != Z_NO_FLUSH, "no flush?");
	if (s->match_available) {
		Tracevv((stderr,"%c", s->window[s->strstart-1]));
		_tr_tally_lit(s, s->window[s->strstart-1], bflush);
		s->match_available = 0;
	}
	FLUSH_BLOCK(s, flush == Z_FINISH);
	return flush == Z_FINISH ? finish_done : block_done;
}
#endif /* FASTEST */

#if 0
local block_state deflate_rle(s, flush)
	deflate_state *s;
	int flush;
{
	int bflush;	 /* set if current block must be flushed */
	uInt run;	   /* length of run */
	uInt max;	   /* maximum length of run */
	uInt prev;	  /* byte at distance one to match */
	Bytef *scan;	/* scan for end of run */

	for (;;) {
		if (s->lookahead < MAX_MATCH) {
			fill_window(s);
			if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
				return need_more;
			}
			if (s->lookahead == 0) break; /* flush the current block */
		}

		run = 0;
		if (s->strstart > 0) {	  /* if there is a previous byte, that is */
			max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
			scan = s->window + s->strstart - 1;
			prev = *scan++;
			do {
				if (*scan++ != prev)
					break;
			} while (++run < max);
		}

		if (run >= MIN_MATCH) {
			check_match(s, s->strstart, s->strstart - 1, run);
			_tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
			s->lookahead -= run;
			s->strstart += run;
		} else {
			Tracevv((stderr,"%c", s->window[s->strstart]));
			_tr_tally_lit (s, s->window[s->strstart], bflush);
			s->lookahead--;
			s->strstart++;
		}
		if (bflush) FLUSH_BLOCK(s, 0);
	}
	FLUSH_BLOCK(s, flush == Z_FINISH);
	return flush == Z_FINISH ? finish_done : block_done;
}
#endif
/*** End of inlined file: deflate.c ***/


/*** Start of inlined file: inffast.c ***/

/*** Start of inlined file: inftrees.h ***/
#ifndef _INFTREES_H_
#define _INFTREES_H_

typedef struct {
	unsigned char op;	   /* operation, extra bits, table bits */
	unsigned char bits;	 /* bits in this part of the code */
	unsigned short val;	 /* offset in table or code value */
} code;

#define ENOUGH 2048
#define MAXD 592

typedef enum {
	CODES,
	LENS,
	DISTS
} codetype;

extern int inflate_table OF((codetype type, unsigned short FAR *lens,
							 unsigned codes, code FAR * FAR *table,
							 unsigned FAR *bits, unsigned short FAR *work));

#endif
/*** End of inlined file: inftrees.h ***/


/*** Start of inlined file: inflate.h ***/
#ifndef _INFLATE_H_
#define _INFLATE_H_

#ifndef NO_GZIP
#  define GUNZIP
#endif

typedef enum {
	HEAD,	   /* i: waiting for magic header */
	FLAGS,	  /* i: waiting for method and flags (gzip) */
	TIME,	   /* i: waiting for modification time (gzip) */
	OS,	 /* i: waiting for extra flags and operating system (gzip) */
	EXLEN,	  /* i: waiting for extra length (gzip) */
	EXTRA,	  /* i: waiting for extra bytes (gzip) */
	NAME,	   /* i: waiting for end of file name (gzip) */
	COMMENT,	/* i: waiting for end of comment (gzip) */
	HCRC,	   /* i: waiting for header crc (gzip) */
	DICTID,	 /* i: waiting for dictionary check value */
	DICT,	   /* waiting for inflateSetDictionary() call */
		TYPE,	   /* i: waiting for type bits, including last-flag bit */
		TYPEDO,	 /* i: same, but skip check to exit inflate on new block */
		STORED,	 /* i: waiting for stored size (length and complement) */
		COPY,	   /* i/o: waiting for input or output to copy stored block */
		TABLE,	  /* i: waiting for dynamic block table lengths */
		LENLENS,	/* i: waiting for code length code lengths */
		CODELENS,   /* i: waiting for length/lit and distance code lengths */
			LEN,	/* i: waiting for length/lit code */
			LENEXT,	 /* i: waiting for length extra bits */
			DIST,	   /* i: waiting for distance code */
			DISTEXT,	/* i: waiting for distance extra bits */
			MATCH,	  /* o: waiting for output space to copy string */
			LIT,	/* o: waiting for output space to write literal */
	CHECK,	  /* i: waiting for 32-bit check value */
	LENGTH,	 /* i: waiting for 32-bit length (gzip) */
	DONE,	   /* finished check, done -- remain here until reset */
	BAD,	/* got a data error -- remain here until reset */
	MEM,	/* got an inflate() memory error -- remain here until reset */
	SYNC	/* looking for synchronization bytes to restart inflate() */
} inflate_mode;

struct inflate_state {
	inflate_mode mode;	  /* current inflate mode */
	int last;		   /* true if processing last block */
	int wrap;		   /* bit 0 true for zlib, bit 1 true for gzip */
	int havedict;		   /* true if dictionary provided */
	int flags;		  /* gzip header method and flags (0 if zlib) */
	unsigned dmax;		  /* zlib header max distance (INFLATE_STRICT) */
	unsigned long check;	/* protected copy of check value */
	unsigned long total;	/* protected copy of output count */
	gz_headerp head;		/* where to save gzip header information */
	unsigned wbits;		 /* log base 2 of requested window size */
	unsigned wsize;		 /* window size or zero if not using window */
	unsigned whave;		 /* valid bytes in the window */
	unsigned write;		 /* window write index */
	unsigned char FAR *window;  /* allocated sliding window, if needed */
	unsigned long hold;	 /* input bit accumulator */
	unsigned bits;              /* number of bits in "in" */
	unsigned length;		/* literal or length of data to copy */
	unsigned offset;		/* distance back to copy string from */
	unsigned extra;		 /* extra bits needed */
	code const FAR *lencode;	/* starting table for length/literal codes */
	code const FAR *distcode;   /* starting table for distance codes */
	unsigned lenbits;	   /* index bits for lencode */
	unsigned distbits;	  /* index bits for distcode */
	unsigned ncode;		 /* number of code length code lengths */
	unsigned nlen;		  /* number of length code lengths */
	unsigned ndist;		 /* number of distance code lengths */
	unsigned have;		  /* number of code lengths in lens[] */
	code FAR *next;		 /* next available space in codes[] */
	unsigned short lens[320];   /* temporary storage for code lengths */
	unsigned short work[288];   /* work area for code table building */
	code codes[ENOUGH];	 /* space for code tables */
};

#endif
/*** End of inlined file: inflate.h ***/


/*** Start of inlined file: inffast.h ***/
void inflate_fast OF((z_streamp strm, unsigned start));
/*** End of inlined file: inffast.h ***/

#ifndef ASMINF

#ifdef POSTINC
#  define OFF 0
#  define PUP(a) *(a)++
#else
#  define OFF 1
#  define PUP(a) *++(a)
#endif

void inflate_fast (z_streamp strm, unsigned start)
{
	struct inflate_state FAR *state;
	unsigned char FAR *in;	  /* local strm->next_in */
	unsigned char FAR *last;	/* while in < last, enough input available */
	unsigned char FAR *out;	 /* local strm->next_out */
	unsigned char FAR *beg;	 /* inflate()'s initial strm->next_out */
	unsigned char FAR *end;	 /* while out < end, enough space available */
#ifdef INFLATE_STRICT
	unsigned dmax;		  /* maximum distance from zlib header */
#endif
	unsigned wsize;		 /* window size or zero if not using window */
	unsigned whave;		 /* valid bytes in the window */
	unsigned write;		 /* window write index */
	unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
	unsigned long hold;	 /* local strm->hold */
	unsigned bits;		  /* local strm->bits */
	code const FAR *lcode;	  /* local strm->lencode */
	code const FAR *dcode;	  /* local strm->distcode */
	unsigned lmask;		 /* mask for first level of length codes */
	unsigned dmask;		 /* mask for first level of distance codes */
	code thisx;		  /* retrieved table entry */
	unsigned op;		/* code bits, operation, extra bits, or */
	unsigned len;		   /* match length, unused bytes */
	unsigned dist;		  /* match distance */
	unsigned char FAR *from;	/* where to copy match from */

	state = (struct inflate_state FAR *)strm->state;
	in = strm->next_in - OFF;
	last = in + (strm->avail_in - 5);
	out = strm->next_out - OFF;
	beg = out - (start - strm->avail_out);
	end = out + (strm->avail_out - 257);
#ifdef INFLATE_STRICT
	dmax = state->dmax;
#endif
	wsize = state->wsize;
	whave = state->whave;
	write = state->write;
	window = state->window;
	hold = state->hold;
	bits = state->bits;
	lcode = state->lencode;
	dcode = state->distcode;
	lmask = (1U << state->lenbits) - 1;
	dmask = (1U << state->distbits) - 1;

	do {
		if (bits < 15) {
			hold += (unsigned long)(PUP(in)) << bits;
			bits += 8;
			hold += (unsigned long)(PUP(in)) << bits;
			bits += 8;
		}
		thisx = lcode[hold & lmask];
	  dolen:
		op = (unsigned)(thisx.bits);
		hold >>= op;
		bits -= op;
		op = (unsigned)(thisx.op);
		if (op == 0) {			  /* literal */
			Tracevv((stderr, thisx.val >= 0x20 && thisx.val < 0x7f ?
					"inflate:         literal '%c'\n" :
					"inflate:         literal 0x%02x\n", thisx.val));
			PUP(out) = (unsigned char)(thisx.val);
		}
		else if (op & 16) {			 /* length base */
			len = (unsigned)(thisx.val);
			op &= 15;			   /* number of extra bits */
			if (op) {
				if (bits < op) {
					hold += (unsigned long)(PUP(in)) << bits;
					bits += 8;
				}
				len += (unsigned)hold & ((1U << op) - 1);
				hold >>= op;
				bits -= op;
			}
			Tracevv((stderr, "inflate:         length %u\n", len));
			if (bits < 15) {
				hold += (unsigned long)(PUP(in)) << bits;
				bits += 8;
				hold += (unsigned long)(PUP(in)) << bits;
				bits += 8;
			}
			thisx = dcode[hold & dmask];
		  dodist:
			op = (unsigned)(thisx.bits);
			hold >>= op;
			bits -= op;
			op = (unsigned)(thisx.op);
			if (op & 16) {			  /* distance base */
				dist = (unsigned)(thisx.val);
				op &= 15;			   /* number of extra bits */
				if (bits < op) {
					hold += (unsigned long)(PUP(in)) << bits;
					bits += 8;
					if (bits < op) {
						hold += (unsigned long)(PUP(in)) << bits;
						bits += 8;
					}
				}
				dist += (unsigned)hold & ((1U << op) - 1);
#ifdef INFLATE_STRICT
				if (dist > dmax) {
					strm->msg = (char *)"invalid distance too far back";
					state->mode = BAD;
					break;
				}
#endif
				hold >>= op;
				bits -= op;
				Tracevv((stderr, "inflate:         distance %u\n", dist));
				op = (unsigned)(out - beg);	 /* max distance in output */
				if (dist > op) {		/* see if copy from window */
					op = dist - op;		 /* distance back in window */
					if (op > whave) {
						strm->msg = (char *)"invalid distance too far back";
						state->mode = BAD;
						break;
					}
					from = window - OFF;
					if (write == 0) {	   /* very common case */
						from += wsize - op;
						if (op < len) {	 /* some from window */
							len -= op;
							do {
								PUP(out) = PUP(from);
							} while (--op);
							from = out - dist;  /* rest from output */
						}
					}
					else if (write < op) {	  /* wrap around window */
						from += wsize + write - op;
						op -= write;
						if (op < len) {	 /* some from end of window */
							len -= op;
							do {
								PUP(out) = PUP(from);
							} while (--op);
							from = window - OFF;
							if (write < len) {  /* some from start of window */
								op = write;
								len -= op;
								do {
									PUP(out) = PUP(from);
								} while (--op);
								from = out - dist;	  /* rest from output */
							}
						}
					}
					else {			  /* contiguous in window */
						from += write - op;
						if (op < len) {	 /* some from window */
							len -= op;
							do {
								PUP(out) = PUP(from);
							} while (--op);
							from = out - dist;  /* rest from output */
						}
					}
					while (len > 2) {
						PUP(out) = PUP(from);
						PUP(out) = PUP(from);
						PUP(out) = PUP(from);
						len -= 3;
					}
					if (len) {
						PUP(out) = PUP(from);
						if (len > 1)
							PUP(out) = PUP(from);
					}
				}
				else {
					from = out - dist;	  /* copy direct from output */
					do {			/* minimum length is three */
						PUP(out) = PUP(from);
						PUP(out) = PUP(from);
						PUP(out) = PUP(from);
						len -= 3;
					} while (len > 2);
					if (len) {
						PUP(out) = PUP(from);
						if (len > 1)
							PUP(out) = PUP(from);
					}
				}
			}
			else if ((op & 64) == 0) {	  /* 2nd level distance code */
				thisx = dcode[thisx.val + (hold & ((1U << op) - 1))];
				goto dodist;
			}
			else {
				strm->msg = (char *)"invalid distance code";
				state->mode = BAD;
				break;
			}
		}
		else if ((op & 64) == 0) {		  /* 2nd level length code */
			thisx = lcode[thisx.val + (hold & ((1U << op) - 1))];
			goto dolen;
		}
		else if (op & 32) {			 /* end-of-block */
			Tracevv((stderr, "inflate:         end of block\n"));
			state->mode = TYPE;
			break;
		}
		else {
			strm->msg = (char *)"invalid literal/length code";
			state->mode = BAD;
			break;
		}
	} while (in < last && out < end);

	len = bits >> 3;
	in -= len;
	bits -= len << 3;
	hold &= (1U << bits) - 1;

	strm->next_in = in + OFF;
	strm->next_out = out + OFF;
	strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
	strm->avail_out = (unsigned)(out < end ?
								 257 + (end - out) : 257 - (out - end));
	state->hold = hold;
	state->bits = bits;
	return;
}

#endif /* !ASMINF */
/*** End of inlined file: inffast.c ***/

	#undef PULLBYTE
	#undef LOAD
	#undef RESTORE
	#undef INITBITS
	#undef NEEDBITS
	#undef DROPBITS
	#undef BYTEBITS

/*** Start of inlined file: inflate.c ***/

/*** Start of inlined file: inffast.h ***/
void inflate_fast OF((z_streamp strm, unsigned start));
/*** End of inlined file: inffast.h ***/

#ifdef MAKEFIXED
#  ifndef BUILDFIXED
#	define BUILDFIXED
#  endif
#endif

local void fixedtables OF((struct inflate_state FAR *state));
local int updatewindow OF((z_streamp strm, unsigned out));
#ifdef BUILDFIXED
   void makefixed OF((void));
#endif
local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
							  unsigned len));

int ZEXPORT inflateReset (z_streamp strm)
{
	struct inflate_state FAR *state;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	state = (struct inflate_state FAR *)strm->state;
	strm->total_in = strm->total_out = state->total = 0;
	strm->msg = Z_NULL;
	strm->adler = 1;	/* to support ill-conceived Java test suite */
	state->mode = HEAD;
	state->last = 0;
	state->havedict = 0;
	state->dmax = 32768U;
	state->head = Z_NULL;
	state->wsize = 0;
	state->whave = 0;
	state->write = 0;
	state->hold = 0;
	state->bits = 0;
	state->lencode = state->distcode = state->next = state->codes;
	Tracev((stderr, "inflate: reset\n"));
	return Z_OK;
}

int ZEXPORT inflatePrime (z_streamp strm, int bits, int value)
{
	struct inflate_state FAR *state;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	state = (struct inflate_state FAR *)strm->state;
	if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
	value &= (1L << bits) - 1;
	state->hold += value << state->bits;
	state->bits += bits;
	return Z_OK;
}

int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size)
{
	struct inflate_state FAR *state;

	if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
		stream_size != (int)(sizeof(z_stream)))
		return Z_VERSION_ERROR;
	if (strm == Z_NULL) return Z_STREAM_ERROR;
	strm->msg = Z_NULL;		 /* in case we return an error */
	if (strm->zalloc == (alloc_func)0) {
		strm->zalloc = zcalloc;
		strm->opaque = (voidpf)0;
	}
	if (strm->zfree == (free_func)0) strm->zfree = zcfree;
	state = (struct inflate_state FAR *)
			ZALLOC(strm, 1, sizeof(struct inflate_state));
	if (state == Z_NULL) return Z_MEM_ERROR;
	Tracev((stderr, "inflate: allocated\n"));
	strm->state = (struct internal_state FAR *)state;
	if (windowBits < 0) {
		state->wrap = 0;
		windowBits = -windowBits;
	}
	else {
		state->wrap = (windowBits >> 4) + 1;
#ifdef GUNZIP
		if (windowBits < 48) windowBits &= 15;
#endif
	}
	if (windowBits < 8 || windowBits > 15) {
		ZFREE(strm, state);
		strm->state = Z_NULL;
		return Z_STREAM_ERROR;
	}
	state->wbits = (unsigned)windowBits;
	state->window = Z_NULL;
	return inflateReset(strm);
}

int ZEXPORT inflateInit_ (z_streamp strm, const char *version, int stream_size)
{
	return inflateInit2_(strm, DEF_WBITS, version, stream_size);
}

local void fixedtables (struct inflate_state FAR *state)
{
#ifdef BUILDFIXED
	static int virgin = 1;
	static code *lenfix, *distfix;
	static code fixed[544];

	if (virgin) {
		unsigned sym, bits;
		static code *next;

		sym = 0;
		while (sym < 144) state->lens[sym++] = 8;
		while (sym < 256) state->lens[sym++] = 9;
		while (sym < 280) state->lens[sym++] = 7;
		while (sym < 288) state->lens[sym++] = 8;
		next = fixed;
		lenfix = next;
		bits = 9;
		inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);

		sym = 0;
		while (sym < 32) state->lens[sym++] = 5;
		distfix = next;
		bits = 5;
		inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);

		virgin = 0;
	}
#else /* !BUILDFIXED */

/*** Start of inlined file: inffixed.h ***/
	static const code lenfix[512] = {
		{96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
		{0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
		{0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
		{0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
		{0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
		{21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
		{0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
		{0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
		{18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
		{0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
		{0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
		{0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
		{20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
		{0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
		{0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
		{0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
		{16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
		{0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
		{0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
		{0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
		{0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
		{0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
		{0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
		{0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
		{17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
		{0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
		{0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
		{0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
		{19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
		{0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
		{0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
		{0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
		{16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
		{0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
		{0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
		{0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
		{0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
		{20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
		{0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
		{0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
		{17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
		{0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
		{0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
		{0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
		{20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
		{0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
		{0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
		{0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
		{16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
		{0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
		{0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
		{0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
		{0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
		{0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
		{0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
		{0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
		{16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
		{0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
		{0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
		{0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
		{19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
		{0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
		{0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
		{0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
		{16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
		{0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
		{0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
		{0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
		{0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
		{64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
		{0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
		{0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
		{18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
		{0,9,255}
	};

	static const code distfix[32] = {
		{16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
		{21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
		{18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
		{19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
		{16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
		{22,5,193},{64,5,0}
	};
/*** End of inlined file: inffixed.h ***/


#endif /* BUILDFIXED */
	state->lencode = lenfix;
	state->lenbits = 9;
	state->distcode = distfix;
	state->distbits = 5;
}

#ifdef MAKEFIXED
#include <stdio.h>

void makefixed()
{
	unsigned low, size;
	struct inflate_state state;

	fixedtables(&state);
	puts("    /* inffixed.h -- table for decoding fixed codes");
	puts("     * Generated automatically by makefixed().");
	puts("     */");
	puts("");
	puts("    /* WARNING: this file should *not* be used by applications.");
	puts("       It is part of the implementation of this library and is");
	puts("       subject to change. Applications should only use zlib.h.");
	puts("     */");
	puts("");
	size = 1U << 9;
	printf("    static const code lenfix[%u] = {", size);
	low = 0;
	for (;;) {
		if ((low % 7) == 0) printf("\n        ");
		printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
			   state.lencode[low].val);
		if (++low == size) break;
		putchar(',');
	}
	puts("\n    };");
	size = 1U << 5;
	printf("\n    static const code distfix[%u] = {", size);
	low = 0;
	for (;;) {
		if ((low % 6) == 0) printf("\n        ");
		printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
			   state.distcode[low].val);
		if (++low == size) break;
		putchar(',');
	}
	puts("\n    };");
}
#endif /* MAKEFIXED */

local int updatewindow (z_streamp strm, unsigned out)
{
	struct inflate_state FAR *state;
	unsigned copy, dist;

	state = (struct inflate_state FAR *)strm->state;

	if (state->window == Z_NULL) {
		state->window = (unsigned char FAR *)
						ZALLOC(strm, 1U << state->wbits,
							   sizeof(unsigned char));
		if (state->window == Z_NULL) return 1;
	}

	if (state->wsize == 0) {
		state->wsize = 1U << state->wbits;
		state->write = 0;
		state->whave = 0;
	}

	copy = out - strm->avail_out;
	if (copy >= state->wsize) {
		zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
		state->write = 0;
		state->whave = state->wsize;
	}
	else {
		dist = state->wsize - state->write;
		if (dist > copy) dist = copy;
		zmemcpy(state->window + state->write, strm->next_out - copy, dist);
		copy -= dist;
		if (copy) {
			zmemcpy(state->window, strm->next_out - copy, copy);
			state->write = copy;
			state->whave = state->wsize;
		}
		else {
			state->write += dist;
			if (state->write == state->wsize) state->write = 0;
			if (state->whave < state->wsize) state->whave += dist;
		}
	}
	return 0;
}

#ifdef GUNZIP
#  define UPDATE(check, buf, len) \
	(state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
#else
#  define UPDATE(check, buf, len) adler32(check, buf, len)
#endif

#ifdef GUNZIP
#  define CRC2(check, word) \
	do { \
		hbuf[0] = (unsigned char)(word); \
		hbuf[1] = (unsigned char)((word) >> 8); \
		check = crc32(check, hbuf, 2); \
	} while (0)

#  define CRC4(check, word) \
	do { \
		hbuf[0] = (unsigned char)(word); \
		hbuf[1] = (unsigned char)((word) >> 8); \
		hbuf[2] = (unsigned char)((word) >> 16); \
		hbuf[3] = (unsigned char)((word) >> 24); \
		check = crc32(check, hbuf, 4); \
	} while (0)
#endif

#define LOAD() \
	do { \
		put = strm->next_out; \
		left = strm->avail_out; \
		next = strm->next_in; \
		have = strm->avail_in; \
		hold = state->hold; \
		bits = state->bits; \
	} while (0)

#define RESTORE() \
	do { \
		strm->next_out = put; \
		strm->avail_out = left; \
		strm->next_in = next; \
		strm->avail_in = have; \
		state->hold = hold; \
		state->bits = bits; \
	} while (0)

#define INITBITS() \
	do { \
		hold = 0; \
		bits = 0; \
	} while (0)

#define PULLBYTE() \
	do { \
		if (have == 0) goto inf_leave; \
		have--; \
		hold += (unsigned long)(*next++) << bits; \
		bits += 8; \
	} while (0)

#define NEEDBITS(n) \
	do { \
		while (bits < (unsigned)(n)) \
			PULLBYTE(); \
	} while (0)

#define BITS(n) \
	((unsigned)hold & ((1U << (n)) - 1))

#define DROPBITS(n) \
	do { \
		hold >>= (n); \
		bits -= (unsigned)(n); \
	} while (0)

#define BYTEBITS() \
	do { \
		hold >>= bits & 7; \
		bits -= bits & 7; \
	} while (0)

#define REVERSE(q) \
	((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
	 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))

int ZEXPORT inflate (z_streamp strm, int flush)
{
	struct inflate_state FAR *state;
	unsigned char FAR *next;	/* next input */
	unsigned char FAR *put;	 /* next output */
	unsigned have, left;	/* available input and output */
	unsigned long hold;	 /* bit buffer */
	unsigned bits;		  /* bits in bit buffer */
	unsigned in, out;	   /* save starting available input and output */
	unsigned copy;		  /* number of stored or match bytes to copy */
	unsigned char FAR *from;	/* where to copy match bytes from */
	code thisx;		  /* current decoding table entry */
	code last;		  /* parent table entry */
	unsigned len;		   /* length to copy for repeats, bits to drop */
	int ret;			/* return code */
#ifdef GUNZIP
	unsigned char hbuf[4];	  /* buffer for gzip header crc calculation */
#endif
	static const unsigned short order[19] = /* permutation of code lengths */
		{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};

	if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
		(strm->next_in == Z_NULL && strm->avail_in != 0))
		return Z_STREAM_ERROR;

	state = (struct inflate_state FAR *)strm->state;
	if (state->mode == TYPE) state->mode = TYPEDO;	  /* skip check */
	LOAD();
	in = have;
	out = left;
	ret = Z_OK;
	for (;;)
		switch (state->mode) {
		case HEAD:
			if (state->wrap == 0) {
				state->mode = TYPEDO;
				break;
			}
			NEEDBITS(16);
#ifdef GUNZIP
			if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
				state->check = crc32(0L, Z_NULL, 0);
				CRC2(state->check, hold);
				INITBITS();
				state->mode = FLAGS;
				break;
			}
			state->flags = 0;	   /* expect zlib header */
			if (state->head != Z_NULL)
				state->head->done = -1;
			if (!(state->wrap & 1) ||   /* check if zlib header allowed */
#else
			if (
#endif
				((BITS(8) << 8) + (hold >> 8)) % 31) {
				strm->msg = (char *)"incorrect header check";
				state->mode = BAD;
				break;
			}
			if (BITS(4) != Z_DEFLATED) {
				strm->msg = (char *)"unknown compression method";
				state->mode = BAD;
				break;
			}
			DROPBITS(4);
			len = BITS(4) + 8;
			if (len > state->wbits) {
				strm->msg = (char *)"invalid window size";
				state->mode = BAD;
				break;
			}
			state->dmax = 1U << len;
			Tracev((stderr, "inflate:   zlib header ok\n"));
			strm->adler = state->check = adler32(0L, Z_NULL, 0);
			state->mode = hold & 0x200 ? DICTID : TYPE;
			INITBITS();
			break;
#ifdef GUNZIP
		case FLAGS:
			NEEDBITS(16);
			state->flags = (int)(hold);
			if ((state->flags & 0xff) != Z_DEFLATED) {
				strm->msg = (char *)"unknown compression method";
				state->mode = BAD;
				break;
			}
			if (state->flags & 0xe000) {
				strm->msg = (char *)"unknown header flags set";
				state->mode = BAD;
				break;
			}
			if (state->head != Z_NULL)
				state->head->text = (int)((hold >> 8) & 1);
			if (state->flags & 0x0200) CRC2(state->check, hold);
			INITBITS();
			state->mode = TIME;
		case TIME:
			NEEDBITS(32);
			if (state->head != Z_NULL)
				state->head->time = hold;
			if (state->flags & 0x0200) CRC4(state->check, hold);
			INITBITS();
			state->mode = OS;
		case OS:
			NEEDBITS(16);
			if (state->head != Z_NULL) {
				state->head->xflags = (int)(hold & 0xff);
				state->head->os = (int)(hold >> 8);
			}
			if (state->flags & 0x0200) CRC2(state->check, hold);
			INITBITS();
			state->mode = EXLEN;
		case EXLEN:
			if (state->flags & 0x0400) {
				NEEDBITS(16);
				state->length = (unsigned)(hold);
				if (state->head != Z_NULL)
					state->head->extra_len = (unsigned)hold;
				if (state->flags & 0x0200) CRC2(state->check, hold);
				INITBITS();
			}
			else if (state->head != Z_NULL)
				state->head->extra = Z_NULL;
			state->mode = EXTRA;
		case EXTRA:
			if (state->flags & 0x0400) {
				copy = state->length;
				if (copy > have) copy = have;
				if (copy) {
					if (state->head != Z_NULL &&
						state->head->extra != Z_NULL) {
						len = state->head->extra_len - state->length;
						zmemcpy(state->head->extra + len, next,
								len + copy > state->head->extra_max ?
								state->head->extra_max - len : copy);
					}
					if (state->flags & 0x0200)
						state->check = crc32(state->check, next, copy);
					have -= copy;
					next += copy;
					state->length -= copy;
				}
				if (state->length) goto inf_leave;
			}
			state->length = 0;
			state->mode = NAME;
		case NAME:
			if (state->flags & 0x0800) {
				if (have == 0) goto inf_leave;
				copy = 0;
				do {
					len = (unsigned)(next[copy++]);
					if (state->head != Z_NULL &&
							state->head->name != Z_NULL &&
							state->length < state->head->name_max)
						state->head->name[state->length++] = len;
				} while (len && copy < have);
				if (state->flags & 0x0200)
					state->check = crc32(state->check, next, copy);
				have -= copy;
				next += copy;
				if (len) goto inf_leave;
			}
			else if (state->head != Z_NULL)
				state->head->name = Z_NULL;
			state->length = 0;
			state->mode = COMMENT;
		case COMMENT:
			if (state->flags & 0x1000) {
				if (have == 0) goto inf_leave;
				copy = 0;
				do {
					len = (unsigned)(next[copy++]);
					if (state->head != Z_NULL &&
							state->head->comment != Z_NULL &&
							state->length < state->head->comm_max)
						state->head->comment[state->length++] = len;
				} while (len && copy < have);
				if (state->flags & 0x0200)
					state->check = crc32(state->check, next, copy);
				have -= copy;
				next += copy;
				if (len) goto inf_leave;
			}
			else if (state->head != Z_NULL)
				state->head->comment = Z_NULL;
			state->mode = HCRC;
		case HCRC:
			if (state->flags & 0x0200) {
				NEEDBITS(16);
				if (hold != (state->check & 0xffff)) {
					strm->msg = (char *)"header crc mismatch";
					state->mode = BAD;
					break;
				}
				INITBITS();
			}
			if (state->head != Z_NULL) {
				state->head->hcrc = (int)((state->flags >> 9) & 1);
				state->head->done = 1;
			}
			strm->adler = state->check = crc32(0L, Z_NULL, 0);
			state->mode = TYPE;
			break;
#endif
		case DICTID:
			NEEDBITS(32);
			strm->adler = state->check = REVERSE(hold);
			INITBITS();
			state->mode = DICT;
		case DICT:
			if (state->havedict == 0) {
				RESTORE();
				return Z_NEED_DICT;
			}
			strm->adler = state->check = adler32(0L, Z_NULL, 0);
			state->mode = TYPE;
		case TYPE:
			if (flush == Z_BLOCK) goto inf_leave;
		case TYPEDO:
			if (state->last) {
				BYTEBITS();
				state->mode = CHECK;
				break;
			}
			NEEDBITS(3);
			state->last = BITS(1);
			DROPBITS(1);
			switch (BITS(2)) {
			case 0:				 /* stored block */
				Tracev((stderr, "inflate:     stored block%s\n",
						state->last ? " (last)" : ""));
				state->mode = STORED;
				break;
			case 1:				 /* fixed block */
				fixedtables(state);
				Tracev((stderr, "inflate:     fixed codes block%s\n",
						state->last ? " (last)" : ""));
				state->mode = LEN;		  /* decode codes */
				break;
			case 2:				 /* dynamic block */
				Tracev((stderr, "inflate:     dynamic codes block%s\n",
						state->last ? " (last)" : ""));
				state->mode = TABLE;
				break;
			case 3:
				strm->msg = (char *)"invalid block type";
				state->mode = BAD;
			}
			DROPBITS(2);
			break;
		case STORED:
			BYTEBITS();			 /* go to byte boundary */
			NEEDBITS(32);
			if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
				strm->msg = (char *)"invalid stored block lengths";
				state->mode = BAD;
				break;
			}
			state->length = (unsigned)hold & 0xffff;
			Tracev((stderr, "inflate:       stored length %u\n",
					state->length));
			INITBITS();
			state->mode = COPY;
		case COPY:
			copy = state->length;
			if (copy) {
				if (copy > have) copy = have;
				if (copy > left) copy = left;
				if (copy == 0) goto inf_leave;
				zmemcpy(put, next, copy);
				have -= copy;
				next += copy;
				left -= copy;
				put += copy;
				state->length -= copy;
				break;
			}
			Tracev((stderr, "inflate:       stored end\n"));
			state->mode = TYPE;
			break;
		case TABLE:
			NEEDBITS(14);
			state->nlen = BITS(5) + 257;
			DROPBITS(5);
			state->ndist = BITS(5) + 1;
			DROPBITS(5);
			state->ncode = BITS(4) + 4;
			DROPBITS(4);
#ifndef PKZIP_BUG_WORKAROUND
			if (state->nlen > 286 || state->ndist > 30) {
				strm->msg = (char *)"too many length or distance symbols";
				state->mode = BAD;
				break;
			}
#endif
			Tracev((stderr, "inflate:       table sizes ok\n"));
			state->have = 0;
			state->mode = LENLENS;
		case LENLENS:
			while (state->have < state->ncode) {
				NEEDBITS(3);
				state->lens[order[state->have++]] = (unsigned short)BITS(3);
				DROPBITS(3);
			}
			while (state->have < 19)
				state->lens[order[state->have++]] = 0;
			state->next = state->codes;
			state->lencode = (code const FAR *)(state->next);
			state->lenbits = 7;
			ret = inflate_table(CODES, state->lens, 19, &(state->next),
								&(state->lenbits), state->work);
			if (ret) {
				strm->msg = (char *)"invalid code lengths set";
				state->mode = BAD;
				break;
			}
			Tracev((stderr, "inflate:       code lengths ok\n"));
			state->have = 0;
			state->mode = CODELENS;
		case CODELENS:
			while (state->have < state->nlen + state->ndist) {
				for (;;) {
					thisx = state->lencode[BITS(state->lenbits)];
					if ((unsigned)(thisx.bits) <= bits) break;
					PULLBYTE();
				}
				if (thisx.val < 16) {
					NEEDBITS(thisx.bits);
					DROPBITS(thisx.bits);
					state->lens[state->have++] = thisx.val;
				}
				else {
					if (thisx.val == 16) {
						NEEDBITS(thisx.bits + 2);
						DROPBITS(thisx.bits);
						if (state->have == 0) {
							strm->msg = (char *)"invalid bit length repeat";
							state->mode = BAD;
							break;
						}
						len = state->lens[state->have - 1];
						copy = 3 + BITS(2);
						DROPBITS(2);
					}
					else if (thisx.val == 17) {
						NEEDBITS(thisx.bits + 3);
						DROPBITS(thisx.bits);
						len = 0;
						copy = 3 + BITS(3);
						DROPBITS(3);
					}
					else {
						NEEDBITS(thisx.bits + 7);
						DROPBITS(thisx.bits);
						len = 0;
						copy = 11 + BITS(7);
						DROPBITS(7);
					}
					if (state->have + copy > state->nlen + state->ndist) {
						strm->msg = (char *)"invalid bit length repeat";
						state->mode = BAD;
						break;
					}
					while (copy--)
						state->lens[state->have++] = (unsigned short)len;
				}
			}

			if (state->mode == BAD) break;

			state->next = state->codes;
			state->lencode = (code const FAR *)(state->next);
			state->lenbits = 9;
			ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
								&(state->lenbits), state->work);
			if (ret) {
				strm->msg = (char *)"invalid literal/lengths set";
				state->mode = BAD;
				break;
			}
			state->distcode = (code const FAR *)(state->next);
			state->distbits = 6;
			ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
							&(state->next), &(state->distbits), state->work);
			if (ret) {
				strm->msg = (char *)"invalid distances set";
				state->mode = BAD;
				break;
			}
			Tracev((stderr, "inflate:       codes ok\n"));
			state->mode = LEN;
		case LEN:
			if (have >= 6 && left >= 258) {
				RESTORE();
				inflate_fast(strm, out);
				LOAD();
				break;
			}
			for (;;) {
				thisx = state->lencode[BITS(state->lenbits)];
				if ((unsigned)(thisx.bits) <= bits) break;
				PULLBYTE();
			}
			if (thisx.op && (thisx.op & 0xf0) == 0) {
				last = thisx;
				for (;;) {
					thisx = state->lencode[last.val +
							(BITS(last.bits + last.op) >> last.bits)];
					if ((unsigned)(last.bits + thisx.bits) <= bits) break;
					PULLBYTE();
				}
				DROPBITS(last.bits);
			}
			DROPBITS(thisx.bits);
			state->length = (unsigned)thisx.val;
			if ((int)(thisx.op) == 0) {
				Tracevv((stderr, thisx.val >= 0x20 && thisx.val < 0x7f ?
						"inflate:         literal '%c'\n" :
						"inflate:         literal 0x%02x\n", thisx.val));
				state->mode = LIT;
				break;
			}
			if (thisx.op & 32) {
				Tracevv((stderr, "inflate:         end of block\n"));
				state->mode = TYPE;
				break;
			}
			if (thisx.op & 64) {
				strm->msg = (char *)"invalid literal/length code";
				state->mode = BAD;
				break;
			}
			state->extra = (unsigned)(thisx.op) & 15;
			state->mode = LENEXT;
		case LENEXT:
			if (state->extra) {
				NEEDBITS(state->extra);
				state->length += BITS(state->extra);
				DROPBITS(state->extra);
			}
			Tracevv((stderr, "inflate:         length %u\n", state->length));
			state->mode = DIST;
		case DIST:
			for (;;) {
				thisx = state->distcode[BITS(state->distbits)];
				if ((unsigned)(thisx.bits) <= bits) break;
				PULLBYTE();
			}
			if ((thisx.op & 0xf0) == 0) {
				last = thisx;
				for (;;) {
					thisx = state->distcode[last.val +
							(BITS(last.bits + last.op) >> last.bits)];
					if ((unsigned)(last.bits + thisx.bits) <= bits) break;
					PULLBYTE();
				}
				DROPBITS(last.bits);
			}
			DROPBITS(thisx.bits);
			if (thisx.op & 64) {
				strm->msg = (char *)"invalid distance code";
				state->mode = BAD;
				break;
			}
			state->offset = (unsigned)thisx.val;
			state->extra = (unsigned)(thisx.op) & 15;
			state->mode = DISTEXT;
		case DISTEXT:
			if (state->extra) {
				NEEDBITS(state->extra);
				state->offset += BITS(state->extra);
				DROPBITS(state->extra);
			}
#ifdef INFLATE_STRICT
			if (state->offset > state->dmax) {
				strm->msg = (char *)"invalid distance too far back";
				state->mode = BAD;
				break;
			}
#endif
			if (state->offset > state->whave + out - left) {
				strm->msg = (char *)"invalid distance too far back";
				state->mode = BAD;
				break;
			}
			Tracevv((stderr, "inflate:         distance %u\n", state->offset));
			state->mode = MATCH;
		case MATCH:
			if (left == 0) goto inf_leave;
			copy = out - left;
			if (state->offset > copy) {	 /* copy from window */
				copy = state->offset - copy;
				if (copy > state->write) {
					copy -= state->write;
					from = state->window + (state->wsize - copy);
				}
				else
					from = state->window + (state->write - copy);
				if (copy > state->length) copy = state->length;
			}
			else {				  /* copy from output */
				from = put - state->offset;
				copy = state->length;
			}
			if (copy > left) copy = left;
			left -= copy;
			state->length -= copy;
			do {
				*put++ = *from++;
			} while (--copy);
			if (state->length == 0) state->mode = LEN;
			break;
		case LIT:
			if (left == 0) goto inf_leave;
			*put++ = (unsigned char)(state->length);
			left--;
			state->mode = LEN;
			break;
		case CHECK:
			if (state->wrap) {
				NEEDBITS(32);
				out -= left;
				strm->total_out += out;
				state->total += out;
				if (out)
					strm->adler = state->check =
						UPDATE(state->check, put - out, out);
				out = left;
				if ((
#ifdef GUNZIP
					 state->flags ? hold :
#endif
					 REVERSE(hold)) != state->check) {
					strm->msg = (char *)"incorrect data check";
					state->mode = BAD;
					break;
				}
				INITBITS();
				Tracev((stderr, "inflate:   check matches trailer\n"));
			}
#ifdef GUNZIP
			state->mode = LENGTH;
		case LENGTH:
			if (state->wrap && state->flags) {
				NEEDBITS(32);
				if (hold != (state->total & 0xffffffffUL)) {
					strm->msg = (char *)"incorrect length check";
					state->mode = BAD;
					break;
				}
				INITBITS();
				Tracev((stderr, "inflate:   length matches trailer\n"));
			}
#endif
			state->mode = DONE;
		case DONE:
			ret = Z_STREAM_END;
			goto inf_leave;
		case BAD:
			ret = Z_DATA_ERROR;
			goto inf_leave;
		case MEM:
			return Z_MEM_ERROR;
		case SYNC:
		default:
			return Z_STREAM_ERROR;
		}

  inf_leave:
	RESTORE();
	if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
		if (updatewindow(strm, out)) {
			state->mode = MEM;
			return Z_MEM_ERROR;
		}
	in -= strm->avail_in;
	out -= strm->avail_out;
	strm->total_in += in;
	strm->total_out += out;
	state->total += out;
	if (state->wrap && out)
		strm->adler = state->check =
			UPDATE(state->check, strm->next_out - out, out);
	strm->data_type = state->bits + (state->last ? 64 : 0) +
					  (state->mode == TYPE ? 128 : 0);
	if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
		ret = Z_BUF_ERROR;
	return ret;
}

int ZEXPORT inflateEnd (z_streamp strm)
{
	struct inflate_state FAR *state;
	if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
		return Z_STREAM_ERROR;
	state = (struct inflate_state FAR *)strm->state;
	if (state->window != Z_NULL) ZFREE(strm, state->window);
	ZFREE(strm, strm->state);
	strm->state = Z_NULL;
	Tracev((stderr, "inflate: end\n"));
	return Z_OK;
}

int ZEXPORT inflateSetDictionary (z_streamp strm, const Bytef *dictionary, uInt dictLength)
{
	struct inflate_state FAR *state;
	unsigned long id_;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	state = (struct inflate_state FAR *)strm->state;
	if (state->wrap != 0 && state->mode != DICT)
		return Z_STREAM_ERROR;

	if (state->mode == DICT) {
		id_ = adler32(0L, Z_NULL, 0);
		id_ = adler32(id_, dictionary, dictLength);
		if (id_ != state->check)
			return Z_DATA_ERROR;
	}

	if (updatewindow(strm, strm->avail_out)) {
		state->mode = MEM;
		return Z_MEM_ERROR;
	}
	if (dictLength > state->wsize) {
		zmemcpy(state->window, dictionary + dictLength - state->wsize,
				state->wsize);
		state->whave = state->wsize;
	}
	else {
		zmemcpy(state->window + state->wsize - dictLength, dictionary,
				dictLength);
		state->whave = dictLength;
	}
	state->havedict = 1;
	Tracev((stderr, "inflate:   dictionary set\n"));
	return Z_OK;
}

int ZEXPORT inflateGetHeader (z_streamp strm, gz_headerp head)
{
	struct inflate_state FAR *state;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	state = (struct inflate_state FAR *)strm->state;
	if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;

	state->head = head;
	head->done = 0;
	return Z_OK;
}

local unsigned syncsearch (unsigned FAR *have, unsigned char FAR *buf, unsigned len)
{
	unsigned got;
	unsigned next;

	got = *have;
	next = 0;
	while (next < len && got < 4) {
		if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
			got++;
		else if (buf[next])
			got = 0;
		else
			got = 4 - got;
		next++;
	}
	*have = got;
	return next;
}

int ZEXPORT inflateSync (z_streamp strm)
{
	unsigned len;		   /* number of bytes to look at or looked at */
	unsigned long in, out;	  /* temporary to save total_in and total_out */
	unsigned char buf[4];	   /* to restore bit buffer to byte string */
	struct inflate_state FAR *state;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	state = (struct inflate_state FAR *)strm->state;
	if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;

	if (state->mode != SYNC) {
		state->mode = SYNC;
		state->hold <<= state->bits & 7;
		state->bits -= state->bits & 7;
		len = 0;
		while (state->bits >= 8) {
			buf[len++] = (unsigned char)(state->hold);
			state->hold >>= 8;
			state->bits -= 8;
		}
		state->have = 0;
		syncsearch(&(state->have), buf, len);
	}

	len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
	strm->avail_in -= len;
	strm->next_in += len;
	strm->total_in += len;

	if (state->have != 4) return Z_DATA_ERROR;
	in = strm->total_in;  out = strm->total_out;
	inflateReset(strm);
	strm->total_in = in;  strm->total_out = out;
	state->mode = TYPE;
	return Z_OK;
}

int ZEXPORT inflateSyncPoint (z_streamp strm)
{
	struct inflate_state FAR *state;

	if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
	state = (struct inflate_state FAR *)strm->state;
	return state->mode == STORED && state->bits == 0;
}

int ZEXPORT inflateCopy(z_streamp dest, z_streamp source)
{
	struct inflate_state FAR *state;
	struct inflate_state FAR *copy;
	unsigned char FAR *window;
	unsigned wsize;

	if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
		source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
		return Z_STREAM_ERROR;
	state = (struct inflate_state FAR *)source->state;

	copy = (struct inflate_state FAR *)
		   ZALLOC(source, 1, sizeof(struct inflate_state));
	if (copy == Z_NULL) return Z_MEM_ERROR;
	window = Z_NULL;
	if (state->window != Z_NULL) {
		window = (unsigned char FAR *)
				 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
		if (window == Z_NULL) {
			ZFREE(source, copy);
			return Z_MEM_ERROR;
		}
	}

	zmemcpy(dest, source, sizeof(z_stream));
	zmemcpy(copy, state, sizeof(struct inflate_state));
	if (state->lencode >= state->codes &&
		state->lencode <= state->codes + ENOUGH - 1) {
		copy->lencode = copy->codes + (state->lencode - state->codes);
		copy->distcode = copy->codes + (state->distcode - state->codes);
	}
	copy->next = copy->codes + (state->next - state->codes);
	if (window != Z_NULL) {
		wsize = 1U << state->wbits;
		zmemcpy(window, state->window, wsize);
	}
	copy->window = window;
	dest->state = (struct internal_state FAR *)copy;
	return Z_OK;
}
/*** End of inlined file: inflate.c ***/



/*** Start of inlined file: inftrees.c ***/
#define MAXBITS 15

const char inflate_copyright[] =
   " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";

int inflate_table (codetype type,
				   unsigned short FAR *lens,
				   unsigned codes,
				   code FAR * FAR *table,
				   unsigned FAR *bits,
				   unsigned short FAR *work)
{
	unsigned len;		   /* a code's length in bits */
	unsigned sym;		   /* index of code symbols */
	unsigned min, max;	  /* minimum and maximum code lengths */
	unsigned root;		  /* number of index bits for root table */
	unsigned curr;		  /* number of index bits for current table */
	unsigned drop;		  /* code bits to drop for sub-table */
	int left;		   /* number of prefix codes available */
	unsigned used;		  /* code entries in table used */
	unsigned huff;		  /* Huffman code */
	unsigned incr;		  /* for incrementing code, index */
	unsigned fill;		  /* index for replicating entries */
	unsigned low;		   /* low bits for current root entry */
	unsigned mask;		  /* mask for low root bits */
	code thisx;		  /* table entry for duplication */
	code FAR *next;		 /* next available space in table */
	const unsigned short FAR *base;	 /* base value table to use */
	const unsigned short FAR *extra;	/* extra bits table to use */
	int end;			/* use base and extra for symbol > end */
	unsigned short count[MAXBITS+1];	/* number of codes of each length */
	unsigned short offs[MAXBITS+1];	 /* offsets in table for each length */
	static const unsigned short lbase[31] = { /* Length codes 257..285 base */
		3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
		35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
	static const unsigned short lext[31] = { /* Length codes 257..285 extra */
		16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
		19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
	static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
		1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
		257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
		8193, 12289, 16385, 24577, 0, 0};
	static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
		16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
		23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
		28, 28, 29, 29, 64, 64};

	for (len = 0; len <= MAXBITS; len++)
		count[len] = 0;
	for (sym = 0; sym < codes; sym++)
		count[lens[sym]]++;

	root = *bits;
	for (max = MAXBITS; max >= 1; max--)
		if (count[max] != 0) break;
	if (root > max) root = max;
	if (max == 0) {			 /* no symbols to code at all */
		thisx.op = (unsigned char)64;	/* invalid code marker */
		thisx.bits = (unsigned char)1;
		thisx.val = (unsigned short)0;
		*(*table)++ = thisx;		 /* make a table to force an error */
		*(*table)++ = thisx;
		*bits = 1;
		return 0;	 /* no symbols, but wait for decoding to report error */
	}
	for (min = 1; min <= MAXBITS; min++)
		if (count[min] != 0) break;
	if (root < min) root = min;

	left = 1;
	for (len = 1; len <= MAXBITS; len++) {
		left <<= 1;
		left -= count[len];
		if (left < 0) return -1;	/* over-subscribed */
	}
	if (left > 0 && (type == CODES || max != 1))
		return -1;			  /* incomplete set */

	offs[1] = 0;
	for (len = 1; len < MAXBITS; len++)
		offs[len + 1] = offs[len] + count[len];

	for (sym = 0; sym < codes; sym++)
		if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;

	switch (type) {
	case CODES:
		base = extra = work;	/* dummy value--not used */
		end = 19;
		break;
	case LENS:
		base = lbase;
		base -= 257;
		extra = lext;
		extra -= 257;
		end = 256;
		break;
	default:		/* DISTS */
		base = dbase;
		extra = dext;
		end = -1;
	}

	huff = 0;		   /* starting code */
	sym = 0;			/* starting code symbol */
	len = min;		  /* starting code length */
	next = *table;		  /* current table to fill in */
	curr = root;		/* current table index bits */
	drop = 0;		   /* current bits to drop from code for index */
	low = (unsigned)(-1);	   /* trigger new sub-table when len > root */
	used = 1U << root;	  /* use root table entries */
	mask = used - 1;		/* mask for comparing low */

	if (type == LENS && used >= ENOUGH - MAXD)
		return 1;

	for (;;) {
		thisx.bits = (unsigned char)(len - drop);
		if ((int)(work[sym]) < end) {
			thisx.op = (unsigned char)0;
			thisx.val = work[sym];
		}
		else if ((int)(work[sym]) > end) {
			thisx.op = (unsigned char)(extra[work[sym]]);
			thisx.val = base[work[sym]];
		}
		else {
			thisx.op = (unsigned char)(32 + 64);	 /* end of block */
			thisx.val = 0;
		}

		incr = 1U << (len - drop);
		fill = 1U << curr;
		min = fill;		 /* save offset to next table */
		do {
			fill -= incr;
			next[(huff >> drop) + fill] = thisx;
		} while (fill != 0);

		incr = 1U << (len - 1);
		while (huff & incr)
			incr >>= 1;
		if (incr != 0) {
			huff &= incr - 1;
			huff += incr;
		}
		else
			huff = 0;

		sym++;
		if (--(count[len]) == 0) {
			if (len == max) break;
			len = lens[work[sym]];
		}

		if (len > root && (huff & mask) != low) {
			if (drop == 0)
				drop = root;

			next += min;		/* here min is 1 << curr */

			curr = len - drop;
			left = (int)(1 << curr);
			while (curr + drop < max) {
				left -= count[curr + drop];
				if (left <= 0) break;
				curr++;
				left <<= 1;
			}

			used += 1U << curr;
			if (type == LENS && used >= ENOUGH - MAXD)
				return 1;

			low = huff & mask;
			(*table)[low].op = (unsigned char)curr;
			(*table)[low].bits = (unsigned char)root;
			(*table)[low].val = (unsigned short)(next - *table);
		}
	}

	thisx.op = (unsigned char)64;		/* invalid code marker */
	thisx.bits = (unsigned char)(len - drop);
	thisx.val = (unsigned short)0;
	while (huff != 0) {
		if (drop != 0 && (huff & mask) != low) {
			drop = 0;
			len = root;
			next = *table;
			thisx.bits = (unsigned char)len;
		}

		next[huff >> drop] = thisx;

		incr = 1U << (len - 1);
		while (huff & incr)
			incr >>= 1;
		if (incr != 0) {
			huff &= incr - 1;
			huff += incr;
		}
		else
			huff = 0;
	}

	*table += used;
	*bits = root;
	return 0;
}
/*** End of inlined file: inftrees.c ***/


/*** Start of inlined file: trees.c ***/
#ifdef DEBUG
#  include <ctype.h>
#endif

#define MAX_BL_BITS 7

#define END_BLOCK 256

#define REP_3_6	  16

#define REPZ_3_10	17

#define REPZ_11_138  18

local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
   = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};

local const int extra_dbits[D_CODES] /* extra bits for each distance code */
   = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};

local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
   = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};

local const uch bl_order[BL_CODES]
   = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};

#define Buf_size (8 * 2*sizeof(char))

#define DIST_CODE_LEN  512 /* see definition of array dist_code below */

#if defined(GEN_TREES_H) || !defined(STDC)

local ct_data static_ltree[L_CODES+2];

local ct_data static_dtree[D_CODES];

uch _dist_code[DIST_CODE_LEN];

uch _length_code[MAX_MATCH-MIN_MATCH+1];

local int base_length[LENGTH_CODES];

local int base_dist[D_CODES];

#else

/*** Start of inlined file: trees.h ***/
local const ct_data static_ltree[L_CODES+2] = {
{{ 12},{  8}}, {{140},{  8}}, {{ 76},{  8}}, {{204},{  8}}, {{ 44},{  8}},
{{172},{  8}}, {{108},{  8}}, {{236},{  8}}, {{ 28},{  8}}, {{156},{  8}},
{{ 92},{  8}}, {{220},{  8}}, {{ 60},{  8}}, {{188},{  8}}, {{124},{  8}},
{{252},{  8}}, {{  2},{  8}}, {{130},{  8}}, {{ 66},{  8}}, {{194},{  8}},
{{ 34},{  8}}, {{162},{  8}}, {{ 98},{  8}}, {{226},{  8}}, {{ 18},{  8}},
{{146},{  8}}, {{ 82},{  8}}, {{210},{  8}}, {{ 50},{  8}}, {{178},{  8}},
{{114},{  8}}, {{242},{  8}}, {{ 10},{  8}}, {{138},{  8}}, {{ 74},{  8}},
{{202},{  8}}, {{ 42},{  8}}, {{170},{  8}}, {{106},{  8}}, {{234},{  8}},
{{ 26},{  8}}, {{154},{  8}}, {{ 90},{  8}}, {{218},{  8}}, {{ 58},{  8}},
{{186},{  8}}, {{122},{  8}}, {{250},{  8}}, {{  6},{  8}}, {{134},{  8}},
{{ 70},{  8}}, {{198},{  8}}, {{ 38},{  8}}, {{166},{  8}}, {{102},{  8}},
{{230},{  8}}, {{ 22},{  8}}, {{150},{  8}}, {{ 86},{  8}}, {{214},{  8}},
{{ 54},{  8}}, {{182},{  8}}, {{118},{  8}}, {{246},{  8}}, {{ 14},{  8}},
{{142},{  8}}, {{ 78},{  8}}, {{206},{  8}}, {{ 46},{  8}}, {{174},{  8}},
{{110},{  8}}, {{238},{  8}}, {{ 30},{  8}}, {{158},{  8}}, {{ 94},{  8}},
{{222},{  8}}, {{ 62},{  8}}, {{190},{  8}}, {{126},{  8}}, {{254},{  8}},
{{  1},{  8}}, {{129},{  8}}, {{ 65},{  8}}, {{193},{  8}}, {{ 33},{  8}},
{{161},{  8}}, {{ 97},{  8}}, {{225},{  8}}, {{ 17},{  8}}, {{145},{  8}},
{{ 81},{  8}}, {{209},{  8}}, {{ 49},{  8}}, {{177},{  8}}, {{113},{  8}},
{{241},{  8}}, {{  9},{  8}}, {{137},{  8}}, {{ 73},{  8}}, {{201},{  8}},
{{ 41},{  8}}, {{169},{  8}}, {{105},{  8}}, {{233},{  8}}, {{ 25},{  8}},
{{153},{  8}}, {{ 89},{  8}}, {{217},{  8}}, {{ 57},{  8}}, {{185},{  8}},
{{121},{  8}}, {{249},{  8}}, {{  5},{  8}}, {{133},{  8}}, {{ 69},{  8}},
{{197},{  8}}, {{ 37},{  8}}, {{165},{  8}}, {{101},{  8}}, {{229},{  8}},
{{ 21},{  8}}, {{149},{  8}}, {{ 85},{  8}}, {{213},{  8}}, {{ 53},{  8}},
{{181},{  8}}, {{117},{  8}}, {{245},{  8}}, {{ 13},{  8}}, {{141},{  8}},
{{ 77},{  8}}, {{205},{  8}}, {{ 45},{  8}}, {{173},{  8}}, {{109},{  8}},
{{237},{  8}}, {{ 29},{  8}}, {{157},{  8}}, {{ 93},{  8}}, {{221},{  8}},
{{ 61},{  8}}, {{189},{  8}}, {{125},{  8}}, {{253},{  8}}, {{ 19},{  9}},
{{275},{  9}}, {{147},{  9}}, {{403},{  9}}, {{ 83},{  9}}, {{339},{  9}},
{{211},{  9}}, {{467},{  9}}, {{ 51},{  9}}, {{307},{  9}}, {{179},{  9}},
{{435},{  9}}, {{115},{  9}}, {{371},{  9}}, {{243},{  9}}, {{499},{  9}},
{{ 11},{  9}}, {{267},{  9}}, {{139},{  9}}, {{395},{  9}}, {{ 75},{  9}},
{{331},{  9}}, {{203},{  9}}, {{459},{  9}}, {{ 43},{  9}}, {{299},{  9}},
{{171},{  9}}, {{427},{  9}}, {{107},{  9}}, {{363},{  9}}, {{235},{  9}},
{{491},{  9}}, {{ 27},{  9}}, {{283},{  9}}, {{155},{  9}}, {{411},{  9}},
{{ 91},{  9}}, {{347},{  9}}, {{219},{  9}}, {{475},{  9}}, {{ 59},{  9}},
{{315},{  9}}, {{187},{  9}}, {{443},{  9}}, {{123},{  9}}, {{379},{  9}},
{{251},{  9}}, {{507},{  9}}, {{  7},{  9}}, {{263},{  9}}, {{135},{  9}},
{{391},{  9}}, {{ 71},{  9}}, {{327},{  9}}, {{199},{  9}}, {{455},{  9}},
{{ 39},{  9}}, {{295},{  9}}, {{167},{  9}}, {{423},{  9}}, {{103},{  9}},
{{359},{  9}}, {{231},{  9}}, {{487},{  9}}, {{ 23},{  9}}, {{279},{  9}},
{{151},{  9}}, {{407},{  9}}, {{ 87},{  9}}, {{343},{  9}}, {{215},{  9}},
{{471},{  9}}, {{ 55},{  9}}, {{311},{  9}}, {{183},{  9}}, {{439},{  9}},
{{119},{  9}}, {{375},{  9}}, {{247},{  9}}, {{503},{  9}}, {{ 15},{  9}},
{{271},{  9}}, {{143},{  9}}, {{399},{  9}}, {{ 79},{  9}}, {{335},{  9}},
{{207},{  9}}, {{463},{  9}}, {{ 47},{  9}}, {{303},{  9}}, {{175},{  9}},
{{431},{  9}}, {{111},{  9}}, {{367},{  9}}, {{239},{  9}}, {{495},{  9}},
{{ 31},{  9}}, {{287},{  9}}, {{159},{  9}}, {{415},{  9}}, {{ 95},{  9}},
{{351},{  9}}, {{223},{  9}}, {{479},{  9}}, {{ 63},{  9}}, {{319},{  9}},
{{191},{  9}}, {{447},{  9}}, {{127},{  9}}, {{383},{  9}}, {{255},{  9}},
{{511},{  9}}, {{  0},{  7}}, {{ 64},{  7}}, {{ 32},{  7}}, {{ 96},{  7}},
{{ 16},{  7}}, {{ 80},{  7}}, {{ 48},{  7}}, {{112},{  7}}, {{  8},{  7}},
{{ 72},{  7}}, {{ 40},{  7}}, {{104},{  7}}, {{ 24},{  7}}, {{ 88},{  7}},
{{ 56},{  7}}, {{120},{  7}}, {{  4},{  7}}, {{ 68},{  7}}, {{ 36},{  7}},
{{100},{  7}}, {{ 20},{  7}}, {{ 84},{  7}}, {{ 52},{  7}}, {{116},{  7}},
{{  3},{  8}}, {{131},{  8}}, {{ 67},{  8}}, {{195},{  8}}, {{ 35},{  8}},
{{163},{  8}}, {{ 99},{  8}}, {{227},{  8}}
};

local const ct_data static_dtree[D_CODES] = {
{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
};

const uch _dist_code[DIST_CODE_LEN] = {
 0,  1,  2,  3,  4,  4,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,
 8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,  0,  0, 16, 17,
18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
};

const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
 0,  1,  2,  3,  4,  5,  6,  7,  8,  8,  9,  9, 10, 10, 11, 11, 12, 12, 12, 12,
13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
};

local const int base_length[LENGTH_CODES] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
64, 80, 96, 112, 128, 160, 192, 224, 0
};

local const int base_dist[D_CODES] = {
	0,	 1,	 2,	 3,	 4,	 6,	 8,	12,	16,	24,
   32,	48,	64,	96,   128,   192,   256,   384,   512,   768,
 1024,  1536,  2048,  3072,  4096,  6144,  8192, 12288, 16384, 24576
};
/*** End of inlined file: trees.h ***/


#endif /* GEN_TREES_H */

struct static_tree_desc_s {
	const ct_data *static_tree;  /* static tree or NULL */
	const intf *extra_bits;	  /* extra bits for each code or NULL */
	int	 extra_base;	  /* base index for extra_bits */
	int	 elems;		   /* max number of elements in the tree */
	int	 max_length;	  /* max bit length for the codes */
};

local static_tree_desc  static_l_desc =
{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};

local static_tree_desc  static_d_desc =
{static_dtree, extra_dbits, 0,	  D_CODES, MAX_BITS};

local static_tree_desc  static_bl_desc =
{(const ct_data *)0, extra_blbits, 0,   BL_CODES, MAX_BL_BITS};

local void tr_static_init OF((void));
local void init_block	 OF((deflate_state *s));
local void pqdownheap	 OF((deflate_state *s, ct_data *tree, int k));
local void gen_bitlen	 OF((deflate_state *s, tree_desc *desc));
local void gen_codes	  OF((ct_data *tree, int max_code, ushf *bl_count));
local void build_tree	 OF((deflate_state *s, tree_desc *desc));
local void scan_tree	  OF((deflate_state *s, ct_data *tree, int max_code));
local void send_tree	  OF((deflate_state *s, ct_data *tree, int max_code));
local int  build_bl_tree  OF((deflate_state *s));
local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
							  int blcodes));
local void compress_block OF((deflate_state *s, ct_data *ltree,
							  ct_data *dtree));
local void set_data_type  OF((deflate_state *s));
local unsigned bi_reverse OF((unsigned value, int length));
local void bi_windup	  OF((deflate_state *s));
local void bi_flush	   OF((deflate_state *s));
local void copy_block	 OF((deflate_state *s, charf *buf, unsigned len,
							  int header));

#ifdef GEN_TREES_H
local void gen_trees_header OF((void));
#endif

#ifndef DEBUG
#  define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)

#else /* DEBUG */
#  define send_code(s, c, tree) \
	 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
	   send_bits(s, tree[c].Code, tree[c].Len); }
#endif

#define put_short(s, w) { \
	put_byte(s, (uch)((w) & 0xff)); \
	put_byte(s, (uch)((ush)(w) >> 8)); \
}

#ifdef DEBUG
local void send_bits	  OF((deflate_state *s, int value, int length));

local void send_bits (deflate_state *s, int value, int length)
{
	Tracevv((stderr," l %2d v %4x ", length, value));
	Assert(length > 0 && length <= 15, "invalid length");
	s->bits_sent += (ulg)length;

	if (s->bi_valid > (int)Buf_size - length) {
		s->bi_buf |= (value << s->bi_valid);
		put_short(s, s->bi_buf);
		s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
		s->bi_valid += length - Buf_size;
	} else {
		s->bi_buf |= value << s->bi_valid;
		s->bi_valid += length;
	}
}
#else /* !DEBUG */

#define send_bits(s, value, length) \
{ int len = length;\
  if (s->bi_valid > (int)Buf_size - len) {\
	int val = value;\
	s->bi_buf |= (val << s->bi_valid);\
	put_short(s, s->bi_buf);\
	s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
	s->bi_valid += len - Buf_size;\
  } else {\
	s->bi_buf |= (value) << s->bi_valid;\
	s->bi_valid += len;\
  }\
}
#endif /* DEBUG */

local void tr_static_init()
{
#if defined(GEN_TREES_H) || !defined(STDC)
	static int static_init_done = 0;
	int n;	/* iterates over tree elements */
	int bits;	 /* bit counter */
	int length;   /* length value */
	int code;	 /* code value */
	int dist;	 /* distance index */
	ush bl_count[MAX_BITS+1];

	if (static_init_done) return;

	static_l_desc.static_tree = static_ltree;
	static_l_desc.extra_bits = extra_lbits;
	static_d_desc.static_tree = static_dtree;
	static_d_desc.extra_bits = extra_dbits;
	static_bl_desc.extra_bits = extra_blbits;

	length = 0;
	for (code = 0; code < LENGTH_CODES-1; code++) {
		base_length[code] = length;
		for (n = 0; n < (1<<extra_lbits[code]); n++) {
			_length_code[length++] = (uch)code;
		}
	}
	Assert (length == 256, "tr_static_init: length != 256");
	_length_code[length-1] = (uch)code;

	dist = 0;
	for (code = 0 ; code < 16; code++) {
		base_dist[code] = dist;
		for (n = 0; n < (1<<extra_dbits[code]); n++) {
			_dist_code[dist++] = (uch)code;
		}
	}
	Assert (dist == 256, "tr_static_init: dist != 256");
	dist >>= 7; /* from now on, all distances are divided by 128 */
	for ( ; code < D_CODES; code++) {
		base_dist[code] = dist << 7;
		for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
			_dist_code[256 + dist++] = (uch)code;
		}
	}
	Assert (dist == 256, "tr_static_init: 256+dist != 512");

	for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
	n = 0;
	while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
	while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
	while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
	while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
	gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);

	for (n = 0; n < D_CODES; n++) {
		static_dtree[n].Len = 5;
		static_dtree[n].Code = bi_reverse((unsigned)n, 5);
	}
	static_init_done = 1;

#  ifdef GEN_TREES_H
	gen_trees_header();
#  endif
#endif /* defined(GEN_TREES_H) || !defined(STDC) */
}

#ifdef GEN_TREES_H
#  ifndef DEBUG
#	include <stdio.h>
#  endif

#  define SEPARATOR(i, last, width) \
	  ((i) == (last)? "\n};\n\n" :    \
	   ((i) % (width) == (width)-1 ? ",\n" : ", "))

void gen_trees_header()
{
	FILE *header = fopen("trees.h", "w");
	int i;

	Assert (header != NULL, "Can't open trees.h");
	fprintf(header,
			"/* header created automatically with -DGEN_TREES_H */\n\n");

	fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
	for (i = 0; i < L_CODES+2; i++) {
		fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
				static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
	}

	fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
	for (i = 0; i < D_CODES; i++) {
		fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
				static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
	}

	fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
	for (i = 0; i < DIST_CODE_LEN; i++) {
		fprintf(header, "%2u%s", _dist_code[i],
				SEPARATOR(i, DIST_CODE_LEN-1, 20));
	}

	fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
	for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
		fprintf(header, "%2u%s", _length_code[i],
				SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
	}

	fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
	for (i = 0; i < LENGTH_CODES; i++) {
		fprintf(header, "%1u%s", base_length[i],
				SEPARATOR(i, LENGTH_CODES-1, 20));
	}

	fprintf(header, "local const int base_dist[D_CODES] = {\n");
	for (i = 0; i < D_CODES; i++) {
		fprintf(header, "%5u%s", base_dist[i],
				SEPARATOR(i, D_CODES-1, 10));
	}

	fclose(header);
}
#endif /* GEN_TREES_H */

void _tr_init(deflate_state *s)
{
	tr_static_init();

	s->l_desc.dyn_tree = s->dyn_ltree;
	s->l_desc.stat_desc = &static_l_desc;

	s->d_desc.dyn_tree = s->dyn_dtree;
	s->d_desc.stat_desc = &static_d_desc;

	s->bl_desc.dyn_tree = s->bl_tree;
	s->bl_desc.stat_desc = &static_bl_desc;

	s->bi_buf = 0;
	s->bi_valid = 0;
	s->last_eob_len = 8; /* enough lookahead for inflate */
#ifdef DEBUG
	s->compressed_len = 0L;
	s->bits_sent = 0L;
#endif

	init_block(s);
}

local void init_block (deflate_state *s)
{
	int n; /* iterates over tree elements */

	for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
	for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
	for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;

	s->dyn_ltree[END_BLOCK].Freq = 1;
	s->opt_len = s->static_len = 0L;
	s->last_lit = s->matches = 0;
}

#define SMALLEST 1

#define pqremove(s, tree, top) \
{\
	top = s->heap[SMALLEST]; \
	s->heap[SMALLEST] = s->heap[s->heap_len--]; \
	pqdownheap(s, tree, SMALLEST); \
}

#define smaller(tree, n, m, depth) \
   (tree[n].Freq < tree[m].Freq || \
   (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))

local void pqdownheap (deflate_state *s,
					   ct_data *tree,  /* the tree to restore */
					   int k)		   /* node to move down */
{
	int v = s->heap[k];
	int j = k << 1;  /* left son of k */
	while (j <= s->heap_len) {
		if (j < s->heap_len &&
			smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
			j++;
		}
		if (smaller(tree, v, s->heap[j], s->depth)) break;

		s->heap[k] = s->heap[j];  k = j;

		j <<= 1;
	}
	s->heap[k] = v;
}

local void gen_bitlen (deflate_state *s, tree_desc *desc)
{
	ct_data *tree	= desc->dyn_tree;
	int max_code	 = desc->max_code;
	const ct_data *stree = desc->stat_desc->static_tree;
	const intf *extra	= desc->stat_desc->extra_bits;
	int base		 = desc->stat_desc->extra_base;
	int max_length	   = desc->stat_desc->max_length;
	int h;		  /* heap index */
	int n, m;	   /* iterate over the tree elements */
	int bits;	   /* bit length */
	int xbits;	  /* extra bits */
	ush f;		  /* frequency */
	int overflow = 0;   /* number of elements with bit length too large */

	for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;

	tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */

	for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
		n = s->heap[h];
		bits = tree[tree[n].Dad].Len + 1;
		if (bits > max_length) bits = max_length, overflow++;
		tree[n].Len = (ush)bits;

		if (n > max_code) continue; /* not a leaf node */

		s->bl_count[bits]++;
		xbits = 0;
		if (n >= base) xbits = extra[n-base];
		f = tree[n].Freq;
		s->opt_len += (ulg)f * (bits + xbits);
		if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
	}
	if (overflow == 0) return;

	Trace((stderr,"\nbit length overflow\n"));

	do {
		bits = max_length-1;
		while (s->bl_count[bits] == 0) bits--;
		s->bl_count[bits]--;	  /* move one leaf down the tree */
		s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
		s->bl_count[max_length]--;
		overflow -= 2;
	} while (overflow > 0);

	for (bits = max_length; bits != 0; bits--) {
		n = s->bl_count[bits];
		while (n != 0) {
			m = s->heap[--h];
			if (m > max_code) continue;
			if ((unsigned) tree[m].Len != (unsigned) bits) {
				Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
				s->opt_len += ((long)bits - (long)tree[m].Len)
							  *(long)tree[m].Freq;
				tree[m].Len = (ush)bits;
			}
			n--;
		}
	}
}

local void gen_codes (ct_data *tree,		 /* the tree to decorate */
					  int max_code,		  /* largest code with non zero frequency */
					  ushf *bl_count)		/* number of codes at each bit length */
{
	ush next_code[MAX_BITS+1]; /* next code value for each bit length */
	ush code = 0;		  /* running code value */
	int bits;		  /* bit index */
	int n;			 /* code index */

	for (bits = 1; bits <= MAX_BITS; bits++) {
		next_code[bits] = code = (code + bl_count[bits-1]) << 1;
	}
	Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
			"inconsistent bit counts");
	Tracev((stderr,"\ngen_codes: max_code %d ", max_code));

	for (n = 0;  n <= max_code; n++) {
		int len = tree[n].Len;
		if (len == 0) continue;
		tree[n].Code = bi_reverse(next_code[len]++, len);

		Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
			 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
	}
}

local void build_tree (deflate_state *s,
					   tree_desc *desc) /* the tree descriptor */
{
	ct_data *tree	 = desc->dyn_tree;
	const ct_data *stree  = desc->stat_desc->static_tree;
	int elems		 = desc->stat_desc->elems;
	int n, m;	  /* iterate over heap elements */
	int max_code = -1; /* largest code with non zero frequency */
	int node;	  /* new node being created */

	s->heap_len = 0, s->heap_max = HEAP_SIZE;

	for (n = 0; n < elems; n++) {
		if (tree[n].Freq != 0) {
			s->heap[++(s->heap_len)] = max_code = n;
			s->depth[n] = 0;
		} else {
			tree[n].Len = 0;
		}
	}

	while (s->heap_len < 2) {
		node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
		tree[node].Freq = 1;
		s->depth[node] = 0;
		s->opt_len--; if (stree) s->static_len -= stree[node].Len;
	}
	desc->max_code = max_code;

	for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);

	node = elems;		  /* next internal node of the tree */
	do {
		pqremove(s, tree, n);  /* n = node of least frequency */
		m = s->heap[SMALLEST]; /* m = node of next least frequency */

		s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
		s->heap[--(s->heap_max)] = m;

		tree[node].Freq = tree[n].Freq + tree[m].Freq;
		s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
								s->depth[n] : s->depth[m]) + 1);
		tree[n].Dad = tree[m].Dad = (ush)node;
#ifdef DUMP_BL_TREE
		if (tree == s->bl_tree) {
			fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
					node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
		}
#endif
		s->heap[SMALLEST] = node++;
		pqdownheap(s, tree, SMALLEST);

	} while (s->heap_len >= 2);

	s->heap[--(s->heap_max)] = s->heap[SMALLEST];

	gen_bitlen(s, (tree_desc *)desc);

	gen_codes ((ct_data *)tree, max_code, s->bl_count);
}

local void scan_tree (deflate_state *s,
					  ct_data *tree,   /* the tree to be scanned */
					  int max_code)	/* and its largest code of non zero frequency */
{
	int n;			 /* iterates over all tree elements */
	int prevlen = -1;	  /* last emitted length */
	int curlen;		/* length of current code */
	int nextlen = tree[0].Len; /* length of next code */
	int count = 0;		 /* repeat count of the current code */
	int max_count = 7;	 /* max repeat count */
	int min_count = 4;	 /* min repeat count */

	if (nextlen == 0) max_count = 138, min_count = 3;
	tree[max_code+1].Len = (ush)0xffff; /* guard */

	for (n = 0; n <= max_code; n++) {
		curlen = nextlen; nextlen = tree[n+1].Len;
		if (++count < max_count && curlen == nextlen) {
			continue;
		} else if (count < min_count) {
			s->bl_tree[curlen].Freq += count;
		} else if (curlen != 0) {
			if (curlen != prevlen) s->bl_tree[curlen].Freq++;
			s->bl_tree[REP_3_6].Freq++;
		} else if (count <= 10) {
			s->bl_tree[REPZ_3_10].Freq++;
		} else {
			s->bl_tree[REPZ_11_138].Freq++;
		}
		count = 0; prevlen = curlen;
		if (nextlen == 0) {
			max_count = 138, min_count = 3;
		} else if (curlen == nextlen) {
			max_count = 6, min_count = 3;
		} else {
			max_count = 7, min_count = 4;
		}
	}
}

local void send_tree (deflate_state *s,
					  ct_data *tree, /* the tree to be scanned */
					  int max_code)	   /* and its largest code of non zero frequency */
{
	int n;			 /* iterates over all tree elements */
	int prevlen = -1;	  /* last emitted length */
	int curlen;		/* length of current code */
	int nextlen = tree[0].Len; /* length of next code */
	int count = 0;		 /* repeat count of the current code */
	int max_count = 7;	 /* max repeat count */
	int min_count = 4;	 /* min repeat count */

  /* guard already set */
	if (nextlen == 0) max_count = 138, min_count = 3;

	for (n = 0; n <= max_code; n++) {
		curlen = nextlen; nextlen = tree[n+1].Len;
		if (++count < max_count && curlen == nextlen) {
			continue;
		} else if (count < min_count) {
			do { send_code(s, curlen, s->bl_tree); } while (--count != 0);

		} else if (curlen != 0) {
			if (curlen != prevlen) {
				send_code(s, curlen, s->bl_tree); count--;
			}
			Assert(count >= 3 && count <= 6, " 3_6?");
			send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);

		} else if (count <= 10) {
			send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);

		} else {
			send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
		}
		count = 0; prevlen = curlen;
		if (nextlen == 0) {
			max_count = 138, min_count = 3;
		} else if (curlen == nextlen) {
			max_count = 6, min_count = 3;
		} else {
			max_count = 7, min_count = 4;
		}
	}
}

local int build_bl_tree (deflate_state *s)
{
	int max_blindex;  /* index of last bit length code of non zero freq */

	scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
	scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);

	build_tree(s, (tree_desc *)(&(s->bl_desc)));

	for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
		if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
	}
	s->opt_len += 3*(max_blindex+1) + 5+5+4;
	Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
			s->opt_len, s->static_len));

	return max_blindex;
}

local void send_all_trees (deflate_state *s,
						   int lcodes, int dcodes, int blcodes) /* number of codes for each tree */
{
	int rank;			/* index in bl_order */

	Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
	Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
			"too many codes");
	Tracev((stderr, "\nbl counts: "));
	send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
	send_bits(s, dcodes-1,   5);
	send_bits(s, blcodes-4,  4); /* not -3 as stated in appnote.txt */
	for (rank = 0; rank < blcodes; rank++) {
		Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
		send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
	}
	Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));

	send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
	Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));

	send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
	Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
}

void _tr_stored_block (deflate_state *s, charf *buf, ulg stored_len, int eof)
{
	send_bits(s, (STORED_BLOCK<<1)+eof, 3);  /* send block type */
#ifdef DEBUG
	s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
	s->compressed_len += (stored_len + 4) << 3;
#endif
	copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
}

void _tr_align (deflate_state *s)
{
	send_bits(s, STATIC_TREES<<1, 3);
	send_code(s, END_BLOCK, static_ltree);
#ifdef DEBUG
	s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
#endif
	bi_flush(s);
	if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
		send_bits(s, STATIC_TREES<<1, 3);
		send_code(s, END_BLOCK, static_ltree);
#ifdef DEBUG
		s->compressed_len += 10L;
#endif
		bi_flush(s);
	}
	s->last_eob_len = 7;
}

void _tr_flush_block (deflate_state *s,
					  charf *buf,	   /* input block, or NULL if too old */
					  ulg stored_len,   /* length of input block */
					  int eof)	  /* true if this is the last block for a file */
{
	ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
	int max_blindex = 0;  /* index of last bit length code of non zero freq */

	if (s->level > 0) {

		if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
			set_data_type(s);

		build_tree(s, (tree_desc *)(&(s->l_desc)));
		Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
				s->static_len));

		build_tree(s, (tree_desc *)(&(s->d_desc)));
		Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
				s->static_len));

		max_blindex = build_bl_tree(s);

		opt_lenb = (s->opt_len+3+7)>>3;
		static_lenb = (s->static_len+3+7)>>3;

		Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
				opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
				s->last_lit));

		if (static_lenb <= opt_lenb) opt_lenb = static_lenb;

	} else {
		Assert(buf != (char*)0, "lost buf");
		opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
	}

#ifdef FORCE_STORED
	if (buf != (char*)0) { /* force stored block */
#else
	if (stored_len+4 <= opt_lenb && buf != (char*)0) {
#endif
		_tr_stored_block(s, buf, stored_len, eof);

#ifdef FORCE_STATIC
	} else if (static_lenb >= 0) { /* force static trees */
#else
	} else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
#endif
		send_bits(s, (STATIC_TREES<<1)+eof, 3);
		compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
#ifdef DEBUG
		s->compressed_len += 3 + s->static_len;
#endif
	} else {
		send_bits(s, (DYN_TREES<<1)+eof, 3);
		send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
					   max_blindex+1);
		compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
#ifdef DEBUG
		s->compressed_len += 3 + s->opt_len;
#endif
	}
	Assert (s->compressed_len == s->bits_sent, "bad compressed size");
	init_block(s);

	if (eof) {
		bi_windup(s);
#ifdef DEBUG
		s->compressed_len += 7;  /* align on byte boundary */
#endif
	}
	Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
		   s->compressed_len-7*eof));
}

int _tr_tally (deflate_state *s,
			   unsigned dist,  /* distance of matched string */
			   unsigned lc)	/* match length-MIN_MATCH or unmatched char (if dist==0) */
{
	s->d_buf[s->last_lit] = (ush)dist;
	s->l_buf[s->last_lit++] = (uch)lc;
	if (dist == 0) {
		s->dyn_ltree[lc].Freq++;
	} else {
		s->matches++;
		dist--;		 /* dist = match distance - 1 */
		Assert((ush)dist < (ush)MAX_DIST(s) &&
			   (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
			   (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");

		s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
		s->dyn_dtree[d_code(dist)].Freq++;
	}

#ifdef TRUNCATE_BLOCK
	if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
		ulg out_length = (ulg)s->last_lit*8L;
		ulg in_length = (ulg)((long)s->strstart - s->block_start);
		int dcode;
		for (dcode = 0; dcode < D_CODES; dcode++) {
			out_length += (ulg)s->dyn_dtree[dcode].Freq *
				(5L+extra_dbits[dcode]);
		}
		out_length >>= 3;
		Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
			   s->last_lit, in_length, out_length,
			   100L - out_length*100L/in_length));
		if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
	}
#endif
	return (s->last_lit == s->lit_bufsize-1);
}

local void compress_block (deflate_state *s,
						   ct_data *ltree, /* literal tree */
						   ct_data *dtree) /* distance tree */
{
	unsigned dist;	  /* distance of matched string */
	int lc;		 /* match length or unmatched char (if dist == 0) */
	unsigned lx = 0;	/* running index in l_buf */
	unsigned code;	  /* the code to send */
	int extra;	  /* number of extra bits to send */

	if (s->last_lit != 0) do {
		dist = s->d_buf[lx];
		lc = s->l_buf[lx++];
		if (dist == 0) {
			send_code(s, lc, ltree); /* send a literal byte */
			Tracecv(isgraph(lc), (stderr," '%c' ", lc));
		} else {
			code = _length_code[lc];
			send_code(s, code+LITERALS+1, ltree); /* send the length code */
			extra = extra_lbits[code];
			if (extra != 0) {
				lc -= base_length[code];
				send_bits(s, lc, extra);	   /* send the extra length bits */
			}
			dist--; /* dist is now the match distance - 1 */
			code = d_code(dist);
			Assert (code < D_CODES, "bad d_code");

			send_code(s, code, dtree);	   /* send the distance code */
			extra = extra_dbits[code];
			if (extra != 0) {
				dist -= base_dist[code];
				send_bits(s, dist, extra);   /* send the extra distance bits */
			}
		} /* literal or match pair ? */

		Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
			   "pendingBuf overflow");

	} while (lx < s->last_lit);

	send_code(s, END_BLOCK, ltree);
	s->last_eob_len = ltree[END_BLOCK].Len;
}

local void set_data_type (deflate_state *s)
{
	int n;

	for (n = 0; n < 9; n++)
		if (s->dyn_ltree[n].Freq != 0)
			break;
	if (n == 9)
		for (n = 14; n < 32; n++)
			if (s->dyn_ltree[n].Freq != 0)
				break;
	s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
}

local unsigned bi_reverse (unsigned code, int len)
{
	register unsigned res = 0;
	do {
		res |= code & 1;
		code >>= 1, res <<= 1;
	} while (--len > 0);
	return res >> 1;
}

local void bi_flush (deflate_state *s)
{
	if (s->bi_valid == 16) {
		put_short(s, s->bi_buf);
		s->bi_buf = 0;
		s->bi_valid = 0;
	} else if (s->bi_valid >= 8) {
		put_byte(s, (Byte)s->bi_buf);
		s->bi_buf >>= 8;
		s->bi_valid -= 8;
	}
}

local void bi_windup (deflate_state *s)
{
	if (s->bi_valid > 8) {
		put_short(s, s->bi_buf);
	} else if (s->bi_valid > 0) {
		put_byte(s, (Byte)s->bi_buf);
	}
	s->bi_buf = 0;
	s->bi_valid = 0;
#ifdef DEBUG
	s->bits_sent = (s->bits_sent+7) & ~7;
#endif
}

local void copy_block(deflate_state *s,
					  charf	*buf,	/* the input data */
					  unsigned len,	 /* its length */
					  int	  header)  /* true if block header must be written */
{
	bi_windup(s);	/* align on byte boundary */
	s->last_eob_len = 8; /* enough lookahead for inflate */

	if (header) {
		put_short(s, (ush)len);
		put_short(s, (ush)~len);
#ifdef DEBUG
		s->bits_sent += 2*16;
#endif
	}
#ifdef DEBUG
	s->bits_sent += (ulg)len<<3;
#endif
	while (len--) {
		put_byte(s, *buf++);
	}
}
/*** End of inlined file: trees.c ***/


/*** Start of inlined file: zutil.c ***/
#ifndef NO_DUMMY_DECL
struct internal_state	  {int dummy;}; /* for buggy compilers */
#endif

const char * const z_errmsg[10] = {
"need dictionary",     /* Z_NEED_DICT       2  */
"stream end",          /* Z_STREAM_END      1  */
"",                    /* Z_OK              0  */
"file error",          /* Z_ERRNO         (-1) */
"stream error",        /* Z_STREAM_ERROR  (-2) */
"data error",          /* Z_DATA_ERROR    (-3) */
"insufficient memory", /* Z_MEM_ERROR     (-4) */
"buffer error",        /* Z_BUF_ERROR     (-5) */
"incompatible version",/* Z_VERSION_ERROR (-6) */
""};

#ifdef DEBUG

#  ifndef verbose
#	define verbose 0
#  endif
int z_verbose = verbose;

void z_error (const char *m)
{
	fprintf(stderr, "%s\n", m);
	exit(1);
}
#endif

const char * ZEXPORT zError(int err)
{
	return ERR_MSG(err);
}

#if defined(_WIN32_WCE)
	int errno = 0;
#endif

#ifndef HAVE_MEMCPY

void zmemcpy(dest, source, len)
	Bytef* dest;
	const Bytef* source;
	uInt  len;
{
	if (len == 0) return;
	do {
		*dest++ = *source++; /* ??? to be unrolled */
	} while (--len != 0);
}

int zmemcmp(s1, s2, len)
	const Bytef* s1;
	const Bytef* s2;
	uInt  len;
{
	uInt j;

	for (j = 0; j < len; j++) {
		if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
	}
	return 0;
}

void zmemzero(dest, len)
	Bytef* dest;
	uInt  len;
{
	if (len == 0) return;
	do {
		*dest++ = 0;  /* ??? to be unrolled */
	} while (--len != 0);
}
#endif

#ifdef SYS16BIT

#ifdef __TURBOC__

#  define MY_ZCALLOC

#define MAX_PTR 10

local int next_ptr = 0;

typedef struct ptr_table_s {
	voidpf org_ptr;
	voidpf new_ptr;
} ptr_table;

local ptr_table table[MAX_PTR];

voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
	voidpf buf = opaque; /* just to make some compilers happy */
	ulg bsize = (ulg)items*size;

	if (bsize < 65520L) {
		buf = farmalloc(bsize);
		if (*(ush*)&buf != 0) return buf;
	} else {
		buf = farmalloc(bsize + 16L);
	}
	if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
	table[next_ptr].org_ptr = buf;

	*((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
	*(ush*)&buf = 0;
	table[next_ptr++].new_ptr = buf;
	return buf;
}

void  zcfree (voidpf opaque, voidpf ptr)
{
	int n;
	if (*(ush*)&ptr != 0) { /* object < 64K */
		farfree(ptr);
		return;
	}
	for (n = 0; n < next_ptr; n++) {
		if (ptr != table[n].new_ptr) continue;

		farfree(table[n].org_ptr);
		while (++n < next_ptr) {
			table[n-1] = table[n];
		}
		next_ptr--;
		return;
	}
	ptr = opaque; /* just to make some compilers happy */
	Assert(0, "zcfree: ptr not found");
}

#endif /* __TURBOC__ */

#ifdef M_I86

#  define MY_ZCALLOC

#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
#  define _halloc  halloc
#  define _hfree   hfree
#endif

voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
	if (opaque) opaque = 0; /* to make compiler happy */
	return _halloc((long)items, size);
}

void  zcfree (voidpf opaque, voidpf ptr)
{
	if (opaque) opaque = 0; /* to make compiler happy */
	_hfree(ptr);
}

#endif /* M_I86 */

#endif /* SYS16BIT */

#ifndef MY_ZCALLOC /* Any system without a special alloc function */

#ifndef STDC
extern voidp  malloc OF((uInt size));
extern voidp  calloc OF((uInt items, uInt size));
extern void   free   OF((voidpf ptr));
#endif

voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
	if (opaque) items += size - size; /* make compiler happy */
	return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
							  (voidpf)calloc(items, size);
}

void  zcfree (voidpf opaque, voidpf ptr)
{
	free(ptr);
	if (opaque) return; /* make compiler happy */
}

#endif /* MY_ZCALLOC */
/*** End of inlined file: zutil.c ***/

	#undef Byte
  }
#else
  #include <zlib.h>
#endif
}

#if JUCE_MSVC
  #pragma warning (pop)
#endif

BEGIN_JUCE_NAMESPACE

// internal helper object that holds the zlib structures so they don't have to be
// included publicly.
class GZIPDecompressHelper
{
public:
	GZIPDecompressHelper (const bool noWrap)
		: finished (true),
		  needsDictionary (false),
		  error (true),
		  streamIsValid (false),
		  data (0),
		  dataSize (0)
	{
		using namespace zlibNamespace;
		zerostruct (stream);
		streamIsValid = (inflateInit2 (&stream, noWrap ? -MAX_WBITS : MAX_WBITS) == Z_OK);
		finished = error = ! streamIsValid;
	}

	~GZIPDecompressHelper()
	{
		using namespace zlibNamespace;
		if (streamIsValid)
			inflateEnd (&stream);
	}

	bool needsInput() const throw()	 { return dataSize <= 0; }

	void setInput (uint8* const data_, const int size) throw()
	{
		data = data_;
		dataSize = size;
	}

	int doNextBlock (uint8* const dest, const int destSize)
	{
		using namespace zlibNamespace;
		if (streamIsValid && data != 0 && ! finished)
		{
			stream.next_in  = data;
			stream.next_out = dest;
			stream.avail_in  = dataSize;
			stream.avail_out = destSize;

			switch (inflate (&stream, Z_PARTIAL_FLUSH))
			{
			case Z_STREAM_END:
				finished = true;
				// deliberate fall-through
			case Z_OK:
				data += dataSize - stream.avail_in;
				dataSize = stream.avail_in;
				return destSize - stream.avail_out;

			case Z_NEED_DICT:
				needsDictionary = true;
				data += dataSize - stream.avail_in;
				dataSize = stream.avail_in;
				break;

			case Z_DATA_ERROR:
			case Z_MEM_ERROR:
				error = true;

			default:
				break;
			}
		}

		return 0;
	}

	bool finished, needsDictionary, error, streamIsValid;

private:
	zlibNamespace::z_stream stream;
	uint8* data;
	int dataSize;

	GZIPDecompressHelper (const GZIPDecompressHelper&);
	GZIPDecompressHelper& operator= (const GZIPDecompressHelper&);
};

const int gzipDecompBufferSize = 32768;

GZIPDecompressorInputStream::GZIPDecompressorInputStream (InputStream* const sourceStream_,
														  const bool deleteSourceWhenDestroyed,
														  const bool noWrap_,
														  const int64 uncompressedStreamLength_)
  : sourceStream (sourceStream_),
	streamToDelete (deleteSourceWhenDestroyed ? sourceStream_ : 0),
	uncompressedStreamLength (uncompressedStreamLength_),
	noWrap (noWrap_),
	isEof (false),
	activeBufferSize (0),
	originalSourcePos (sourceStream_->getPosition()),
	currentPos (0),
	buffer (gzipDecompBufferSize),
	helper (new GZIPDecompressHelper (noWrap_))
{
}

GZIPDecompressorInputStream::~GZIPDecompressorInputStream()
{
}

int64 GZIPDecompressorInputStream::getTotalLength()
{
	return uncompressedStreamLength;
}

int GZIPDecompressorInputStream::read (void* destBuffer, int howMany)
{
	if ((howMany > 0) && ! isEof)
	{
		jassert (destBuffer != 0);

		if (destBuffer != 0)
		{
			int numRead = 0;
			uint8* d = static_cast <uint8*> (destBuffer);

			while (! helper->error)
			{
				const int n = helper->doNextBlock (d, howMany);
				currentPos += n;

				if (n == 0)
				{
					if (helper->finished || helper->needsDictionary)
					{
						isEof = true;
						return numRead;
					}

					if (helper->needsInput())
					{
						activeBufferSize = sourceStream->read (buffer, gzipDecompBufferSize);

						if (activeBufferSize > 0)
						{
							helper->setInput (buffer, activeBufferSize);
						}
						else
						{
							isEof = true;
							return numRead;
						}
					}
				}
				else
				{
					numRead += n;
					howMany -= n;
					d += n;

					if (howMany <= 0)
						return numRead;
				}
			}
		}
	}

	return 0;
}

bool GZIPDecompressorInputStream::isExhausted()
{
	return helper->error || isEof;
}

int64 GZIPDecompressorInputStream::getPosition()
{
	return currentPos;
}

bool GZIPDecompressorInputStream::setPosition (int64 newPos)
{
	if (newPos < currentPos)
	{
		// to go backwards, reset the stream and start again..
		isEof = false;
		activeBufferSize = 0;
		currentPos = 0;
		helper = new GZIPDecompressHelper (noWrap);

		sourceStream->setPosition (originalSourcePos);
	}

	skipNextBytes (newPos - currentPos);
	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_GZIPDecompressorInputStream.cpp ***/

#endif

#if JUCE_BUILD_NATIVE && ! JUCE_ONLY_BUILD_CORE_LIBRARY

/*** Start of inlined file: juce_FlacAudioFormat.cpp ***/
#if JUCE_USE_FLAC

#if JUCE_WINDOWS
 #include <windows.h>
#endif

#ifdef _MSC_VER
  #pragma warning (disable : 4505)
  #pragma warning (push)
#endif

namespace FlacNamespace
{
#if JUCE_INCLUDE_FLAC_CODE
 #define FLAC__NO_DLL 1

 #if ! defined (SIZE_MAX)
  #define SIZE_MAX 0xffffffff
 #endif

 #define __STDC_LIMIT_MACROS 1

/*** Start of inlined file: all.h ***/
#ifndef FLAC__ALL_H
#define FLAC__ALL_H


/*** Start of inlined file: export.h ***/
#ifndef FLAC__EXPORT_H
#define FLAC__EXPORT_H

#if defined(FLAC__NO_DLL) || !defined(_MSC_VER)
#define FLAC_API

#else

#ifdef FLAC_API_EXPORTS
#define	FLAC_API	_declspec(dllexport)
#else
#define FLAC_API	_declspec(dllimport)

#endif
#endif

#define FLAC_API_VERSION_CURRENT 10
#define FLAC_API_VERSION_REVISION 0 /**< see above */
#define FLAC_API_VERSION_AGE 2 /**< see above */

#ifdef __cplusplus
extern "C" {
#endif

extern FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC;

#ifdef __cplusplus
}
#endif

#endif
/*** End of inlined file: export.h ***/


/*** Start of inlined file: assert.h ***/
#ifndef FLAC__ASSERT_H
#define FLAC__ASSERT_H

/* we need this since some compilers (like MSVC) leave assert()s on release code (and we don't want to use their ASSERT) */
#ifdef DEBUG
#include <assert.h>
#define FLAC__ASSERT(x) assert(x)
#define FLAC__ASSERT_DECLARATION(x) x
#else
#define FLAC__ASSERT(x)
#define FLAC__ASSERT_DECLARATION(x)
#endif

#endif
/*** End of inlined file: assert.h ***/


/*** Start of inlined file: callback.h ***/
#ifndef FLAC__CALLBACK_H
#define FLAC__CALLBACK_H


/*** Start of inlined file: ordinals.h ***/
#ifndef FLAC__ORDINALS_H
#define FLAC__ORDINALS_H

#if !(defined(_MSC_VER) || defined(__BORLANDC__) || defined(__EMX__))
#include <inttypes.h>
#endif

typedef signed char FLAC__int8;
typedef unsigned char FLAC__uint8;

#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef __int16 FLAC__int16;
typedef __int32 FLAC__int32;
typedef __int64 FLAC__int64;
typedef unsigned __int16 FLAC__uint16;
typedef unsigned __int32 FLAC__uint32;
typedef unsigned __int64 FLAC__uint64;
#elif defined(__EMX__)
typedef short FLAC__int16;
typedef long FLAC__int32;
typedef long long FLAC__int64;
typedef unsigned short FLAC__uint16;
typedef unsigned long FLAC__uint32;
typedef unsigned long long FLAC__uint64;
#else
typedef int16_t FLAC__int16;
typedef int32_t FLAC__int32;
typedef int64_t FLAC__int64;
typedef uint16_t FLAC__uint16;
typedef uint32_t FLAC__uint32;
typedef uint64_t FLAC__uint64;
#endif

typedef int FLAC__bool;

typedef FLAC__uint8 FLAC__byte;

#ifdef true
#undef true
#endif
#ifdef false
#undef false
#endif
#ifndef __cplusplus
#define true 1
#define false 0
#endif

#endif
/*** End of inlined file: ordinals.h ***/

#include <stdlib.h> /* for size_t */

#ifdef __cplusplus
extern "C" {
#endif

typedef void* FLAC__IOHandle;

typedef size_t (*FLAC__IOCallback_Read) (void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle);

typedef size_t (*FLAC__IOCallback_Write) (const void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle);

typedef int (*FLAC__IOCallback_Seek) (FLAC__IOHandle handle, FLAC__int64 offset, int whence);

typedef FLAC__int64 (*FLAC__IOCallback_Tell) (FLAC__IOHandle handle);

typedef int (*FLAC__IOCallback_Eof) (FLAC__IOHandle handle);

typedef int (*FLAC__IOCallback_Close) (FLAC__IOHandle handle);

typedef struct {
	FLAC__IOCallback_Read read;
	FLAC__IOCallback_Write write;
	FLAC__IOCallback_Seek seek;
	FLAC__IOCallback_Tell tell;
	FLAC__IOCallback_Eof eof;
	FLAC__IOCallback_Close close;
} FLAC__IOCallbacks;

#ifdef __cplusplus
}
#endif

#endif
/*** End of inlined file: callback.h ***/


/*** Start of inlined file: format.h ***/
#ifndef FLAC__FORMAT_H
#define FLAC__FORMAT_H

#ifdef __cplusplus
extern "C" {
#endif

#define FLAC__MAX_METADATA_TYPE_CODE (126u)

#define FLAC__MIN_BLOCK_SIZE (16u)

#define FLAC__MAX_BLOCK_SIZE (65535u)

#define FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ (4608u)

#define FLAC__MAX_CHANNELS (8u)

#define FLAC__MIN_BITS_PER_SAMPLE (4u)

#define FLAC__MAX_BITS_PER_SAMPLE (32u)

#define FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE (24u)

#define FLAC__MAX_SAMPLE_RATE (655350u)

#define FLAC__MAX_LPC_ORDER (32u)

#define FLAC__SUBSET_MAX_LPC_ORDER_48000HZ (12u)

#define FLAC__MIN_QLP_COEFF_PRECISION (5u)

#define FLAC__MAX_QLP_COEFF_PRECISION (15u)

#define FLAC__MAX_FIXED_ORDER (4u)

#define FLAC__MAX_RICE_PARTITION_ORDER (15u)

#define FLAC__SUBSET_MAX_RICE_PARTITION_ORDER (8u)

extern FLAC_API const char *FLAC__VERSION_STRING;

extern FLAC_API const char *FLAC__VENDOR_STRING;

extern FLAC_API const FLAC__byte FLAC__STREAM_SYNC_STRING[4]; /* = "fLaC" */

extern FLAC_API const unsigned FLAC__STREAM_SYNC; /* = 0x664C6143 */

extern FLAC_API const unsigned FLAC__STREAM_SYNC_LEN; /* = 32 bits */

#define FLAC__STREAM_SYNC_LENGTH (4u)

typedef enum {
	FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE = 0,

	FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 = 1
} FLAC__EntropyCodingMethodType;

extern FLAC_API const char * const FLAC__EntropyCodingMethodTypeString[];

typedef struct {

	unsigned *parameters;

	unsigned *raw_bits;

	unsigned capacity_by_order;
} FLAC__EntropyCodingMethod_PartitionedRiceContents;

typedef struct {

	unsigned order;

	const FLAC__EntropyCodingMethod_PartitionedRiceContents *contents;

} FLAC__EntropyCodingMethod_PartitionedRice;

extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; /**< == 4 (bits) */
extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; /**< == 4 (bits) */
extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN; /**< == 5 (bits) */
extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN; /**< == 5 (bits) */

extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER;

typedef struct {
	FLAC__EntropyCodingMethodType type;
	union {
		FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice;
	} data;
} FLAC__EntropyCodingMethod;

extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN; /**< == 2 (bits) */

typedef enum {
	FLAC__SUBFRAME_TYPE_CONSTANT = 0, /**< constant signal */
	FLAC__SUBFRAME_TYPE_VERBATIM = 1, /**< uncompressed signal */
	FLAC__SUBFRAME_TYPE_FIXED = 2, /**< fixed polynomial prediction */
	FLAC__SUBFRAME_TYPE_LPC = 3 /**< linear prediction */
} FLAC__SubframeType;

extern FLAC_API const char * const FLAC__SubframeTypeString[];

typedef struct {
	FLAC__int32 value; /**< The constant signal value. */
} FLAC__Subframe_Constant;

typedef struct {
	const FLAC__int32 *data; /**< A pointer to verbatim signal. */
} FLAC__Subframe_Verbatim;

typedef struct {
	FLAC__EntropyCodingMethod entropy_coding_method;

	unsigned order;

	FLAC__int32 warmup[FLAC__MAX_FIXED_ORDER];

	const FLAC__int32 *residual;
} FLAC__Subframe_Fixed;

typedef struct {
	FLAC__EntropyCodingMethod entropy_coding_method;

	unsigned order;

	unsigned qlp_coeff_precision;

	int quantization_level;

	FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];

	FLAC__int32 warmup[FLAC__MAX_LPC_ORDER];

	const FLAC__int32 *residual;
} FLAC__Subframe_LPC;

extern FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN; /**< == 4 (bits) */
extern FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN; /**< == 5 (bits) */

typedef struct {
	FLAC__SubframeType type;
	union {
		FLAC__Subframe_Constant constant;
		FLAC__Subframe_Fixed fixed;
		FLAC__Subframe_LPC lpc;
		FLAC__Subframe_Verbatim verbatim;
	} data;
	unsigned wasted_bits;
} FLAC__Subframe;

extern FLAC_API const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN;
extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LEN; /**< == 6 (bits) */
extern FLAC_API const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN; /**< == 1 (bit) */

extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK; /**< = 0x00 */
extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK; /**< = 0x02 */
extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK; /**< = 0x10 */
extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK; /**< = 0x40 */

typedef enum {
	FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT = 0, /**< independent channels */
	FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE = 1, /**< left+side stereo */
	FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE = 2, /**< right+side stereo */
	FLAC__CHANNEL_ASSIGNMENT_MID_SIDE = 3 /**< mid+side stereo */
} FLAC__ChannelAssignment;

extern FLAC_API const char * const FLAC__ChannelAssignmentString[];

typedef enum {
	FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER, /**< number contains the frame number */
	FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER /**< number contains the sample number of first sample in frame */
} FLAC__FrameNumberType;

extern FLAC_API const char * const FLAC__FrameNumberTypeString[];

typedef struct {
	unsigned blocksize;

	unsigned sample_rate;

	unsigned channels;

	FLAC__ChannelAssignment channel_assignment;

	unsigned bits_per_sample;

	FLAC__FrameNumberType number_type;

	union {
		FLAC__uint32 frame_number;
		FLAC__uint64 sample_number;
	} number;

	FLAC__uint8 crc;
} FLAC__FrameHeader;

extern FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC; /**< == 0x3ffe; the frame header sync code */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC_LEN; /**< == 14 (bits) */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_RESERVED_LEN; /**< == 1 (bits) */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN; /**< == 1 (bits) */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN; /**< == 4 (bits) */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN; /**< == 4 (bits) */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN; /**< == 4 (bits) */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN; /**< == 3 (bits) */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN; /**< == 1 (bit) */
extern FLAC_API const unsigned FLAC__FRAME_HEADER_CRC_LEN; /**< == 8 (bits) */

typedef struct {
	FLAC__uint16 crc;
} FLAC__FrameFooter;

extern FLAC_API const unsigned FLAC__FRAME_FOOTER_CRC_LEN; /**< == 16 (bits) */

typedef struct {
	FLAC__FrameHeader header;
	FLAC__Subframe subframes[FLAC__MAX_CHANNELS];
	FLAC__FrameFooter footer;
} FLAC__Frame;

typedef enum {

	FLAC__METADATA_TYPE_STREAMINFO = 0,

	FLAC__METADATA_TYPE_PADDING = 1,

	FLAC__METADATA_TYPE_APPLICATION = 2,

	FLAC__METADATA_TYPE_SEEKTABLE = 3,

	FLAC__METADATA_TYPE_VORBIS_COMMENT = 4,

	FLAC__METADATA_TYPE_CUESHEET = 5,

	FLAC__METADATA_TYPE_PICTURE = 6,

	FLAC__METADATA_TYPE_UNDEFINED = 7

} FLAC__MetadataType;

extern FLAC_API const char * const FLAC__MetadataTypeString[];

typedef struct {
	unsigned min_blocksize, max_blocksize;
	unsigned min_framesize, max_framesize;
	unsigned sample_rate;
	unsigned channels;
	unsigned bits_per_sample;
	FLAC__uint64 total_samples;
	FLAC__byte md5sum[16];
} FLAC__StreamMetadata_StreamInfo;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; /**< == 16 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; /**< == 16 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; /**< == 24 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; /**< == 24 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; /**< == 20 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; /**< == 3 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; /**< == 5 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; /**< == 36 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN; /**< == 128 (bits) */

#define FLAC__STREAM_METADATA_STREAMINFO_LENGTH (34u)

typedef struct {
	int dummy;
} FLAC__StreamMetadata_Padding;

typedef struct {
	FLAC__byte id[4];
	FLAC__byte *data;
} FLAC__StreamMetadata_Application;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN; /**< == 32 (bits) */

typedef struct {
	FLAC__uint64 sample_number;

	FLAC__uint64 stream_offset;

	unsigned frame_samples;
} FLAC__StreamMetadata_SeekPoint;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN; /**< == 64 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN; /**< == 64 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN; /**< == 16 (bits) */

#define FLAC__STREAM_METADATA_SEEKPOINT_LENGTH (18u)

extern FLAC_API const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;

typedef struct {
	unsigned num_points;
	FLAC__StreamMetadata_SeekPoint *points;
} FLAC__StreamMetadata_SeekTable;

typedef struct {
	FLAC__uint32 length;
	FLAC__byte *entry;
} FLAC__StreamMetadata_VorbisComment_Entry;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN; /**< == 32 (bits) */

typedef struct {
	FLAC__StreamMetadata_VorbisComment_Entry vendor_string;
	FLAC__uint32 num_comments;
	FLAC__StreamMetadata_VorbisComment_Entry *comments;
} FLAC__StreamMetadata_VorbisComment;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN; /**< == 32 (bits) */

typedef struct {
	FLAC__uint64 offset;

	FLAC__byte number;
} FLAC__StreamMetadata_CueSheet_Index;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN; /**< == 64 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN; /**< == 8 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN; /**< == 3*8 (bits) */

typedef struct {
	FLAC__uint64 offset;

	FLAC__byte number;

	char isrc[13];

	unsigned type:1;

	unsigned pre_emphasis:1;

	FLAC__byte num_indices;

	FLAC__StreamMetadata_CueSheet_Index *indices;

} FLAC__StreamMetadata_CueSheet_Track;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN; /**< == 64 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN; /**< == 8 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN; /**< == 12*8 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN; /**< == 1 (bit) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN; /**< == 1 (bit) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN; /**< == 6+13*8 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN; /**< == 8 (bits) */

typedef struct {
	char media_catalog_number[129];

	FLAC__uint64 lead_in;

	FLAC__bool is_cd;

	unsigned num_tracks;

	FLAC__StreamMetadata_CueSheet_Track *tracks;

} FLAC__StreamMetadata_CueSheet;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN; /**< == 128*8 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN; /**< == 64 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN; /**< == 1 (bit) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN; /**< == 7+258*8 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN; /**< == 8 (bits) */

typedef enum {
	FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER = 0, /**< Other */
	FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD = 1, /**< 32x32 pixels 'file icon' (PNG only) */
	FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON = 2, /**< Other file icon */
	FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER = 3, /**< Cover (front) */
	FLAC__STREAM_METADATA_PICTURE_TYPE_BACK_COVER = 4, /**< Cover (back) */
	FLAC__STREAM_METADATA_PICTURE_TYPE_LEAFLET_PAGE = 5, /**< Leaflet page */
	FLAC__STREAM_METADATA_PICTURE_TYPE_MEDIA = 6, /**< Media (e.g. label side of CD) */
	FLAC__STREAM_METADATA_PICTURE_TYPE_LEAD_ARTIST = 7, /**< Lead artist/lead performer/soloist */
	FLAC__STREAM_METADATA_PICTURE_TYPE_ARTIST = 8, /**< Artist/performer */
	FLAC__STREAM_METADATA_PICTURE_TYPE_CONDUCTOR = 9, /**< Conductor */
	FLAC__STREAM_METADATA_PICTURE_TYPE_BAND = 10, /**< Band/Orchestra */
	FLAC__STREAM_METADATA_PICTURE_TYPE_COMPOSER = 11, /**< Composer */
	FLAC__STREAM_METADATA_PICTURE_TYPE_LYRICIST = 12, /**< Lyricist/text writer */
	FLAC__STREAM_METADATA_PICTURE_TYPE_RECORDING_LOCATION = 13, /**< Recording Location */
	FLAC__STREAM_METADATA_PICTURE_TYPE_DURING_RECORDING = 14, /**< During recording */
	FLAC__STREAM_METADATA_PICTURE_TYPE_DURING_PERFORMANCE = 15, /**< During performance */
	FLAC__STREAM_METADATA_PICTURE_TYPE_VIDEO_SCREEN_CAPTURE = 16, /**< Movie/video screen capture */
	FLAC__STREAM_METADATA_PICTURE_TYPE_FISH = 17, /**< A bright coloured fish */
	FLAC__STREAM_METADATA_PICTURE_TYPE_ILLUSTRATION = 18, /**< Illustration */
	FLAC__STREAM_METADATA_PICTURE_TYPE_BAND_LOGOTYPE = 19, /**< Band/artist logotype */
	FLAC__STREAM_METADATA_PICTURE_TYPE_PUBLISHER_LOGOTYPE = 20, /**< Publisher/Studio logotype */
	FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED
} FLAC__StreamMetadata_Picture_Type;

extern FLAC_API const char * const FLAC__StreamMetadata_Picture_TypeString[];

typedef struct {
	FLAC__StreamMetadata_Picture_Type type;

	char *mime_type;

	FLAC__byte *description;

	FLAC__uint32 width;

	FLAC__uint32 height;

	FLAC__uint32 depth;

	FLAC__uint32 colors;

	FLAC__uint32 data_length;

	FLAC__byte *data;

} FLAC__StreamMetadata_Picture;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_TYPE_LEN; /**< == 32 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN; /**< == 32 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN; /**< == 32 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN; /**< == 32 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN; /**< == 32 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN; /**< == 32 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_COLORS_LEN; /**< == 32 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN; /**< == 32 (bits) */

typedef struct {
	FLAC__byte *data;
} FLAC__StreamMetadata_Unknown;

typedef struct {
	FLAC__MetadataType type;

	FLAC__bool is_last;

	unsigned length;

	union {
		FLAC__StreamMetadata_StreamInfo stream_info;
		FLAC__StreamMetadata_Padding padding;
		FLAC__StreamMetadata_Application application;
		FLAC__StreamMetadata_SeekTable seek_table;
		FLAC__StreamMetadata_VorbisComment vorbis_comment;
		FLAC__StreamMetadata_CueSheet cue_sheet;
		FLAC__StreamMetadata_Picture picture;
		FLAC__StreamMetadata_Unknown unknown;
	} data;
} FLAC__StreamMetadata;

extern FLAC_API const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN; /**< == 1 (bit) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_TYPE_LEN; /**< == 7 (bits) */
extern FLAC_API const unsigned FLAC__STREAM_METADATA_LENGTH_LEN; /**< == 24 (bits) */

#define FLAC__STREAM_METADATA_HEADER_LENGTH (4u)

FLAC_API FLAC__bool FLAC__format_sample_rate_is_valid(unsigned sample_rate);

FLAC_API FLAC__bool FLAC__format_sample_rate_is_subset(unsigned sample_rate);

FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_name_is_legal(const char *name);

FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_value_is_legal(const FLAC__byte *value, unsigned length);

FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_is_legal(const FLAC__byte *entry, unsigned length);

FLAC_API FLAC__bool FLAC__format_seektable_is_legal(const FLAC__StreamMetadata_SeekTable *seek_table);

FLAC_API unsigned FLAC__format_seektable_sort(FLAC__StreamMetadata_SeekTable *seek_table);

FLAC_API FLAC__bool FLAC__format_cuesheet_is_legal(const FLAC__StreamMetadata_CueSheet *cue_sheet, FLAC__bool check_cd_da_subset, const char **violation);

FLAC_API FLAC__bool FLAC__format_picture_is_legal(const FLAC__StreamMetadata_Picture *picture, const char **violation);

#ifdef __cplusplus
}
#endif

#endif
/*** End of inlined file: format.h ***/


/*** Start of inlined file: metadata.h ***/
#ifndef FLAC__METADATA_H
#define FLAC__METADATA_H

#include <sys/types.h> /* for off_t */

#ifdef __cplusplus
extern "C" {
#endif

FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo);

FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags);

FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet);

FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors);

struct FLAC__Metadata_SimpleIterator;
typedef struct FLAC__Metadata_SimpleIterator FLAC__Metadata_SimpleIterator;

typedef enum {

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK = 0,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR,

	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR

} FLAC__Metadata_SimpleIteratorStatus;

extern FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[];

FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void);

FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator);

FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator);

FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator);

FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator);

FLAC_API unsigned FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id);

FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding);

FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding);

struct FLAC__Metadata_Chain;
typedef struct FLAC__Metadata_Chain FLAC__Metadata_Chain;

struct FLAC__Metadata_Iterator;
typedef struct FLAC__Metadata_Iterator FLAC__Metadata_Iterator;

typedef enum {
	FLAC__METADATA_CHAIN_STATUS_OK = 0,

	FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT,

	FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE,

	FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE,

	FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE,

	FLAC__METADATA_CHAIN_STATUS_BAD_METADATA,

	FLAC__METADATA_CHAIN_STATUS_READ_ERROR,

	FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR,

	FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR,

	FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR,

	FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR,

	FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR,

	FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR,

	FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS,

	FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH,

	FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL

} FLAC__Metadata_ChainStatus;

extern FLAC_API const char * const FLAC__Metadata_ChainStatusString[];

FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void);

FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain);

FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain);

FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename);

FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename);

FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks);

FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks);

FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding);

FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats);

FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks);

FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__IOHandle temp_handle, FLAC__IOCallbacks temp_callbacks);

FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain);

FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain);

FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void);

FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator);

FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain);

FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator);

FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator);

FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator);

FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator);

FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block);

FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding);

FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block);

FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block);

FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type);

FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object);

FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object);

FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2);

FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points);

FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples);

FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, const char *field_value);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value);

FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length);

FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name);

FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name);

FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name);

FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void);

FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object);

FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num);

FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation);

FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object);

FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy);

FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation);

#ifdef __cplusplus
}
#endif

#endif
/*** End of inlined file: metadata.h ***/


/*** Start of inlined file: stream_decoder.h ***/
#ifndef FLAC__STREAM_DECODER_H
#define FLAC__STREAM_DECODER_H

#include <stdio.h> /* for FILE */

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {

	FLAC__STREAM_DECODER_SEARCH_FOR_METADATA = 0,

	FLAC__STREAM_DECODER_READ_METADATA,

	FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC,

	FLAC__STREAM_DECODER_READ_FRAME,

	FLAC__STREAM_DECODER_END_OF_STREAM,

	FLAC__STREAM_DECODER_OGG_ERROR,

	FLAC__STREAM_DECODER_SEEK_ERROR,

	FLAC__STREAM_DECODER_ABORTED,

	FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR,

	FLAC__STREAM_DECODER_UNINITIALIZED

} FLAC__StreamDecoderState;

extern FLAC_API const char * const FLAC__StreamDecoderStateString[];

typedef enum {

	FLAC__STREAM_DECODER_INIT_STATUS_OK = 0,

	FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER,

	FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS,

	FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR,

	FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE,

	FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED

} FLAC__StreamDecoderInitStatus;

extern FLAC_API const char * const FLAC__StreamDecoderInitStatusString[];

typedef enum {

	FLAC__STREAM_DECODER_READ_STATUS_CONTINUE,

	FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM,

	FLAC__STREAM_DECODER_READ_STATUS_ABORT

} FLAC__StreamDecoderReadStatus;

extern FLAC_API const char * const FLAC__StreamDecoderReadStatusString[];

typedef enum {

	FLAC__STREAM_DECODER_SEEK_STATUS_OK,

	FLAC__STREAM_DECODER_SEEK_STATUS_ERROR,

	FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED

} FLAC__StreamDecoderSeekStatus;

extern FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[];

typedef enum {

	FLAC__STREAM_DECODER_TELL_STATUS_OK,

	FLAC__STREAM_DECODER_TELL_STATUS_ERROR,

	FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED

} FLAC__StreamDecoderTellStatus;

extern FLAC_API const char * const FLAC__StreamDecoderTellStatusString[];

typedef enum {

	FLAC__STREAM_DECODER_LENGTH_STATUS_OK,

	FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR,

	FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED

} FLAC__StreamDecoderLengthStatus;

extern FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[];

typedef enum {

	FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE,

	FLAC__STREAM_DECODER_WRITE_STATUS_ABORT

} FLAC__StreamDecoderWriteStatus;

extern FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[];

typedef enum {

	FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC,

	FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER,

	FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH,

	FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM

} FLAC__StreamDecoderErrorStatus;

extern FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[];

struct FLAC__StreamDecoderProtected;
struct FLAC__StreamDecoderPrivate;
typedef struct {
	struct FLAC__StreamDecoderProtected *protected_; /* avoid the C++ keyword 'protected' */
	struct FLAC__StreamDecoderPrivate *private_; /* avoid the C++ keyword 'private' */
} FLAC__StreamDecoder;

typedef FLAC__StreamDecoderReadStatus (*FLAC__StreamDecoderReadCallback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);

typedef FLAC__StreamDecoderSeekStatus (*FLAC__StreamDecoderSeekCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);

typedef FLAC__StreamDecoderTellStatus (*FLAC__StreamDecoderTellCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);

typedef FLAC__StreamDecoderLengthStatus (*FLAC__StreamDecoderLengthCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);

typedef FLAC__bool (*FLAC__StreamDecoderEofCallback)(const FLAC__StreamDecoder *decoder, void *client_data);

typedef FLAC__StreamDecoderWriteStatus (*FLAC__StreamDecoderWriteCallback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);

typedef void (*FLAC__StreamDecoderMetadataCallback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);

typedef void (*FLAC__StreamDecoderErrorCallback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);

FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void);

FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long serial_number);

FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value);

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type);

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]);

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type);

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]);

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder);

FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder);

FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder);

FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder);

FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder);

FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder);

FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder);

FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position);

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
	FLAC__StreamDecoder *decoder,
	FLAC__StreamDecoderReadCallback read_callback,
	FLAC__StreamDecoderSeekCallback seek_callback,
	FLAC__StreamDecoderTellCallback tell_callback,
	FLAC__StreamDecoderLengthCallback length_callback,
	FLAC__StreamDecoderEofCallback eof_callback,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
);

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
	FLAC__StreamDecoder *decoder,
	FLAC__StreamDecoderReadCallback read_callback,
	FLAC__StreamDecoderSeekCallback seek_callback,
	FLAC__StreamDecoderTellCallback tell_callback,
	FLAC__StreamDecoderLengthCallback length_callback,
	FLAC__StreamDecoderEofCallback eof_callback,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
);

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
	FLAC__StreamDecoder *decoder,
	FILE *file,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
);

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
	FLAC__StreamDecoder *decoder,
	FILE *file,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
);

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
	FLAC__StreamDecoder *decoder,
	const char *filename,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
);

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
	FLAC__StreamDecoder *decoder,
	const char *filename,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
);

FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder);

FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample);

#ifdef __cplusplus
}
#endif

#endif
/*** End of inlined file: stream_decoder.h ***/


/*** Start of inlined file: stream_encoder.h ***/
#ifndef FLAC__STREAM_ENCODER_H
#define FLAC__STREAM_ENCODER_H

#include <stdio.h> /* for FILE */

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {

	FLAC__STREAM_ENCODER_OK = 0,

	FLAC__STREAM_ENCODER_UNINITIALIZED,

	FLAC__STREAM_ENCODER_OGG_ERROR,

	FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR,

	FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA,

	FLAC__STREAM_ENCODER_CLIENT_ERROR,

	FLAC__STREAM_ENCODER_IO_ERROR,

	FLAC__STREAM_ENCODER_FRAMING_ERROR,

	FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR

} FLAC__StreamEncoderState;

extern FLAC_API const char * const FLAC__StreamEncoderStateString[];

typedef enum {

	FLAC__STREAM_ENCODER_INIT_STATUS_OK = 0,

	FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR,

	FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER,

	FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS,

	FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS,

	FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE,

	FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE,

	FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE,

	FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER,

	FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION,

	FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER,

	FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE,

	FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA,

	FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED

} FLAC__StreamEncoderInitStatus;

extern FLAC_API const char * const FLAC__StreamEncoderInitStatusString[];

typedef enum {

	FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE,

	FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM,

	FLAC__STREAM_ENCODER_READ_STATUS_ABORT,

	FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED

} FLAC__StreamEncoderReadStatus;

extern FLAC_API const char * const FLAC__StreamEncoderReadStatusString[];

typedef enum {

	FLAC__STREAM_ENCODER_WRITE_STATUS_OK = 0,

	FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR

} FLAC__StreamEncoderWriteStatus;

extern FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[];

typedef enum {

	FLAC__STREAM_ENCODER_SEEK_STATUS_OK,

	FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR,

	FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED

} FLAC__StreamEncoderSeekStatus;

extern FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[];

typedef enum {

	FLAC__STREAM_ENCODER_TELL_STATUS_OK,

	FLAC__STREAM_ENCODER_TELL_STATUS_ERROR,

	FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED

} FLAC__StreamEncoderTellStatus;

extern FLAC_API const char * const FLAC__StreamEncoderTellStatusString[];

struct FLAC__StreamEncoderProtected;
struct FLAC__StreamEncoderPrivate;
typedef struct {
	struct FLAC__StreamEncoderProtected *protected_; /* avoid the C++ keyword 'protected' */
	struct FLAC__StreamEncoderPrivate *private_; /* avoid the C++ keyword 'private' */
} FLAC__StreamEncoder;

typedef FLAC__StreamEncoderReadStatus (*FLAC__StreamEncoderReadCallback)(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);

typedef FLAC__StreamEncoderWriteStatus (*FLAC__StreamEncoderWriteCallback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data);

typedef FLAC__StreamEncoderSeekStatus (*FLAC__StreamEncoderSeekCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);

typedef FLAC__StreamEncoderTellStatus (*FLAC__StreamEncoderTellCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);

typedef void (*FLAC__StreamEncoderMetadataCallback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);

typedef void (*FLAC__StreamEncoderProgressCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data);

FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void);

FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder);

FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long serial_number);

FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification);

FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value);

FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks);

FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder);

FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder);

FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got);

FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder);

FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder);

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data);

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderReadCallback read_callback, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data);

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data);

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data);

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data);

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data);

FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);

FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples);

FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples);

#ifdef __cplusplus
}
#endif

#endif
/*** End of inlined file: stream_encoder.h ***/

#ifdef _MSC_VER
static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
{
	x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
	return (x>>16) | (x<<16);
}
#endif

#if defined(_MSC_VER) && defined(_X86_)
static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len)
{
	__asm {
		mov edx, start
		mov ecx, len
		test ecx, ecx
loop1:
		jz done1
		mov eax, [edx]
		bswap eax
		mov [edx], eax
		add edx, 4
		dec ecx
		jmp short loop1
done1:
	}
}
#endif

#endif
/*** End of inlined file: all.h ***/



/*** Start of inlined file: bitmath.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif


/*** Start of inlined file: bitmath.h ***/
#ifndef FLAC__PRIVATE__BITMATH_H
#define FLAC__PRIVATE__BITMATH_H

unsigned FLAC__bitmath_ilog2(FLAC__uint32 v);
unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v);
unsigned FLAC__bitmath_silog2(int v);
unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v);

#endif
/*** End of inlined file: bitmath.h ***/

unsigned FLAC__bitmath_ilog2(FLAC__uint32 v)
{
	unsigned l = 0;
	FLAC__ASSERT(v > 0);
	while(v >>= 1)
		l++;
	return l;
}

unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v)
{
	unsigned l = 0;
	FLAC__ASSERT(v > 0);
	while(v >>= 1)
		l++;
	return l;
}

unsigned FLAC__bitmath_silog2(int v)
{
	while(1) {
		if(v == 0) {
			return 0;
		}
		else if(v > 0) {
			unsigned l = 0;
			while(v) {
				l++;
				v >>= 1;
			}
			return l+1;
		}
		else if(v == -1) {
			return 2;
		}
		else {
			v++;
			v = -v;
		}
	}
}

unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v)
{
	while(1) {
		if(v == 0) {
			return 0;
		}
		else if(v > 0) {
			unsigned l = 0;
			while(v) {
				l++;
				v >>= 1;
			}
			return l+1;
		}
		else if(v == -1) {
			return 2;
		}
		else {
			v++;
			v = -v;
		}
	}
}

#endif
/*** End of inlined file: bitmath.c ***/


/*** Start of inlined file: bitreader.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy(), memset() */
#ifdef _MSC_VER
#include <winsock.h> /* for ntohl() */
#elif defined FLAC__SYS_DARWIN
#include <machine/endian.h> /* for ntohl() */
#elif defined __MINGW32__
#include <winsock.h> /* for ntohl() */
#else
#include <netinet/in.h> /* for ntohl() */
#endif


/*** Start of inlined file: bitreader.h ***/
#ifndef FLAC__PRIVATE__BITREADER_H
#define FLAC__PRIVATE__BITREADER_H

#include <stdio.h> /* for FILE */


/*** Start of inlined file: cpu.h ***/
#ifndef FLAC__PRIVATE__CPU_H
#define FLAC__PRIVATE__CPU_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

typedef enum {
	FLAC__CPUINFO_TYPE_IA32,
	FLAC__CPUINFO_TYPE_PPC,
	FLAC__CPUINFO_TYPE_UNKNOWN
} FLAC__CPUInfo_Type;

typedef struct {
	FLAC__bool cpuid;
	FLAC__bool bswap;
	FLAC__bool cmov;
	FLAC__bool mmx;
	FLAC__bool fxsr;
	FLAC__bool sse;
	FLAC__bool sse2;
	FLAC__bool sse3;
	FLAC__bool ssse3;
	FLAC__bool _3dnow;
	FLAC__bool ext3dnow;
	FLAC__bool extmmx;
} FLAC__CPUInfo_IA32;

typedef struct {
	FLAC__bool altivec;
	FLAC__bool ppc64;
} FLAC__CPUInfo_PPC;

typedef struct {
	FLAC__bool use_asm;
	FLAC__CPUInfo_Type type;
	union {
		FLAC__CPUInfo_IA32 ia32;
		FLAC__CPUInfo_PPC ppc;
	} data;
} FLAC__CPUInfo;

void FLAC__cpu_info(FLAC__CPUInfo *info);

#ifndef FLAC__NO_ASM
#ifdef FLAC__CPU_IA32
#ifdef FLAC__HAS_NASM
FLAC__uint32 FLAC__cpu_have_cpuid_asm_ia32(void);
void	 FLAC__cpu_info_asm_ia32(FLAC__uint32 *flags_edx, FLAC__uint32 *flags_ecx);
FLAC__uint32 FLAC__cpu_info_extended_amd_asm_ia32(void);
#endif
#endif
#endif

#endif
/*** End of inlined file: cpu.h ***/

struct FLAC__BitReader;
typedef struct FLAC__BitReader FLAC__BitReader;

typedef FLAC__bool (*FLAC__BitReaderReadCallback)(FLAC__byte buffer[], size_t *bytes, void *client_data);

FLAC__BitReader *FLAC__bitreader_new(void);
void FLAC__bitreader_delete(FLAC__BitReader *br);
FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd);
void FLAC__bitreader_free(FLAC__BitReader *br); /* does not 'free(br)' */
FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br);
void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out);

void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed);
FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br);

FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br);
unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br);
unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br);

FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits);
FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits);
FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits);
FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val); /*only for bits=32*/
FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits); /* WATCHOUT: does not CRC the skipped data! */ /*@@@@ add to unit tests */
FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals); /* WATCHOUT: does not CRC the read data! */
FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals); /* WATCHOUT: does not CRC the read data! */
FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val);
FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter);
FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter);
#ifndef FLAC__NO_ASM
#  ifdef FLAC__CPU_IA32
#	ifdef FLAC__HAS_NASM
FLAC__bool FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter);
#	endif
#  endif
#endif
#if 0 /* UNUSED */
FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter);
FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter);
#endif
FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen);
FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen);

FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br);
#endif
/*** End of inlined file: bitreader.h ***/


/*** Start of inlined file: crc.h ***/
#ifndef FLAC__PRIVATE__CRC_H
#define FLAC__PRIVATE__CRC_H

extern FLAC__byte const FLAC__crc8_table[256];
#define FLAC__CRC8_UPDATE(data, crc) (crc) = FLAC__crc8_table[(crc) ^ (data)];
void FLAC__crc8_update(const FLAC__byte data, FLAC__uint8 *crc);
void FLAC__crc8_update_block(const FLAC__byte *data, unsigned len, FLAC__uint8 *crc);
FLAC__uint8 FLAC__crc8(const FLAC__byte *data, unsigned len);

extern unsigned FLAC__crc16_table[256];

#define FLAC__CRC16_UPDATE(data, crc) (((((crc)<<8) & 0xffff) ^ FLAC__crc16_table[((crc)>>8) ^ (data)]))
#if 0
#define FLAC__CRC16_UPDATE(data, crc) ((((crc)<<8) ^ FLAC__crc16_table[((crc)>>8) ^ (data)]) & 0xffff)
#endif

unsigned FLAC__crc16(const FLAC__byte *data, unsigned len);

#endif
/*** End of inlined file: crc.h ***/

typedef FLAC__uint32 brword;
#define FLAC__BYTES_PER_WORD 4
#define FLAC__BITS_PER_WORD 32
#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
#if WORDS_BIGENDIAN
#define SWAP_BE_WORD_TO_HOST(x) (x)
#else
#if defined (_MSC_VER) && defined (_X86_)
#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
#else
#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
#endif
#endif
#define COUNT_ZERO_MSBS(word) ( \
	(word) <= 0xffff ? \
		( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
		( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
)
#define COUNT_ZERO_MSBS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )

static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */

static const unsigned char byte_to_unary_table[] = {
	8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

#ifdef min
#undef min
#endif
#define min(x,y) ((x)<(y)?(x):(y))
#ifdef max
#undef max
#endif
#define max(x,y) ((x)>(y)?(x):(y))

#ifdef _MSC_VER
#define FLAC__U64L(x) x
#else
#define FLAC__U64L(x) x##LLU
#endif

#ifndef FLaC__INLINE
#define FLaC__INLINE
#endif

struct FLAC__BitReader {
	brword *buffer;
	unsigned capacity; /* in words */
	unsigned words; /* # of completed words in buffer */
	unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
	unsigned consumed_words; /* #words ... */
	unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
	unsigned read_crc16; /* the running frame CRC */
	unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
	FLAC__BitReaderReadCallback read_callback;
	void *client_data;
	FLAC__CPUInfo cpu_info;
};

static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
{
	register unsigned crc = br->read_crc16;
#if FLAC__BYTES_PER_WORD == 4
	switch(br->crc16_align) {
		case  0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
		case  8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
		case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
		case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
	}
#elif FLAC__BYTES_PER_WORD == 8
	switch(br->crc16_align) {
		case  0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
		case  8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
		case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
		case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
		case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
		case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
		case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
		case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
	}
#else
	for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
		crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
	br->read_crc16 = crc;
#endif
	br->crc16_align = 0;
}

FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
{
	unsigned start, end;
	size_t bytes;
	FLAC__byte *target;

	if(br->consumed_words > 0) {
		start = br->consumed_words;
		end = br->words + (br->bytes? 1:0);
		memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));

		br->words -= start;
		br->consumed_words = 0;
	}

	bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
	if(bytes == 0)
		return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY  */
	target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;

#if WORDS_BIGENDIAN
#else
	if(br->bytes)
		br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
#endif

	if(!br->read_callback(target, &bytes, br->client_data))
		return false;

#if WORDS_BIGENDIAN
#else
	end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
# if defined(_MSC_VER) && defined (_X86_) && (FLAC__BYTES_PER_WORD == 4)
	if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) {
		start = br->words;
		local_swap32_block_(br->buffer + start, end - start);
	}
	else
# endif
	for(start = br->words; start < end; start++)
		br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
#endif

	end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
	br->words = end / FLAC__BYTES_PER_WORD;
	br->bytes = end % FLAC__BYTES_PER_WORD;

	return true;
}

FLAC__BitReader *FLAC__bitreader_new(void)
{
	FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));

	return br;
}

void FLAC__bitreader_delete(FLAC__BitReader *br)
{
	FLAC__ASSERT(0 != br);

	FLAC__bitreader_free(br);
	free(br);
}

FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
{
	FLAC__ASSERT(0 != br);

	br->words = br->bytes = 0;
	br->consumed_words = br->consumed_bits = 0;
	br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
	br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
	if(br->buffer == 0)
		return false;
	br->read_callback = rcb;
	br->client_data = cd;
	br->cpu_info = cpu;

	return true;
}

void FLAC__bitreader_free(FLAC__BitReader *br)
{
	FLAC__ASSERT(0 != br);

	if(0 != br->buffer)
		free(br->buffer);
	br->buffer = 0;
	br->capacity = 0;
	br->words = br->bytes = 0;
	br->consumed_words = br->consumed_bits = 0;
	br->read_callback = 0;
	br->client_data = 0;
}

FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
{
	br->words = br->bytes = 0;
	br->consumed_words = br->consumed_bits = 0;
	return true;
}

void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
{
	unsigned i, j;
	if(br == 0) {
		fprintf(out, "bitreader is NULL\n");
	}
	else {
		fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);

		for(i = 0; i < br->words; i++) {
			fprintf(out, "%08X: ", i);
			for(j = 0; j < FLAC__BITS_PER_WORD; j++)
				if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
					fprintf(out, ".");
				else
					fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
			fprintf(out, "\n");
		}
		if(br->bytes > 0) {
			fprintf(out, "%08X: ", i);
			for(j = 0; j < br->bytes*8; j++)
				if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
					fprintf(out, ".");
				else
					fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
			fprintf(out, "\n");
		}
	}
}

void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
{
	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);
	FLAC__ASSERT((br->consumed_bits & 7) == 0);

	br->read_crc16 = (unsigned)seed;
	br->crc16_align = br->consumed_bits;
}

FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
{
	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);
	FLAC__ASSERT((br->consumed_bits & 7) == 0);
	FLAC__ASSERT(br->crc16_align <= br->consumed_bits);

	if(br->consumed_bits) {
		const brword tail = br->buffer[br->consumed_words];
		for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
			br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
	}
	return br->read_crc16;
}

FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
{
	return ((br->consumed_bits & 7) == 0);
}

FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
{
	return 8 - (br->consumed_bits & 7);
}

FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
{
	return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
}

FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
{
	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);

	FLAC__ASSERT(bits <= 32);
	FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
	FLAC__ASSERT(br->consumed_words <= br->words);

	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);

	if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
		*val = 0;
		return true;
	}

	while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
		if(!bitreader_read_from_client_(br))
			return false;
	}
	if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
		if(br->consumed_bits) {
			const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
			const brword word = br->buffer[br->consumed_words];
			if(bits < n) {
				*val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
				br->consumed_bits += bits;
				return true;
			}
			*val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
			bits -= n;
			crc16_update_word_(br, word);
			br->consumed_words++;
			br->consumed_bits = 0;
			if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
				*val <<= bits;
				*val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
				br->consumed_bits = bits;
			}
			return true;
		}
		else {
			const brword word = br->buffer[br->consumed_words];
			if(bits < FLAC__BITS_PER_WORD) {
				*val = word >> (FLAC__BITS_PER_WORD-bits);
				br->consumed_bits = bits;
				return true;
			}
			*val = word;
			crc16_update_word_(br, word);
			br->consumed_words++;
			return true;
		}
	}
	else {
		if(br->consumed_bits) {
			FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
			*val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
			br->consumed_bits += bits;
			return true;
		}
		else {
			*val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
			br->consumed_bits += bits;
			return true;
		}
	}
}

FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
{
	if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
		return false;
	*val <<= (32-bits);
	*val >>= (32-bits);
	return true;
}

FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
{
	FLAC__uint32 hi, lo;

	if(bits > 32) {
		if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
			return false;
		if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
			return false;
		*val = hi;
		*val <<= 32;
		*val |= lo;
	}
	else {
		if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
			return false;
		*val = lo;
	}
	return true;
}

FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
{
	FLAC__uint32 x8, x32 = 0;

	if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
		return false;

	if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
		return false;
	x32 |= (x8 << 8);

	if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
		return false;
	x32 |= (x8 << 16);

	if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
		return false;
	x32 |= (x8 << 24);

	*val = x32;
	return true;
}

FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
{
	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);

	if(bits > 0) {
		const unsigned n = br->consumed_bits & 7;
		unsigned m;
		FLAC__uint32 x;

		if(n != 0) {
			m = min(8-n, bits);
			if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
				return false;
			bits -= m;
		}
		m = bits / 8;
		if(m > 0) {
			if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
				return false;
			bits %= 8;
		}
		if(bits > 0) {
			if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
				return false;
		}
	}

	return true;
}

FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
{
	FLAC__uint32 x;

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);
	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));

	while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
			return false;
		nvals--;
	}
	if(0 == nvals)
		return true;
	while(nvals >= FLAC__BYTES_PER_WORD) {
		if(br->consumed_words < br->words) {
			br->consumed_words++;
			nvals -= FLAC__BYTES_PER_WORD;
		}
		else if(!bitreader_read_from_client_(br))
			return false;
	}
	while(nvals) {
		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
			return false;
		nvals--;
	}

	return true;
}

FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
{
	FLAC__uint32 x;

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);
	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));

	while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
			return false;
		*val++ = (FLAC__byte)x;
		nvals--;
	}
	if(0 == nvals)
		return true;
	while(nvals >= FLAC__BYTES_PER_WORD) {
		if(br->consumed_words < br->words) {
			const brword word = br->buffer[br->consumed_words++];
#if FLAC__BYTES_PER_WORD == 4
			val[0] = (FLAC__byte)(word >> 24);
			val[1] = (FLAC__byte)(word >> 16);
			val[2] = (FLAC__byte)(word >> 8);
			val[3] = (FLAC__byte)word;
#elif FLAC__BYTES_PER_WORD == 8
			val[0] = (FLAC__byte)(word >> 56);
			val[1] = (FLAC__byte)(word >> 48);
			val[2] = (FLAC__byte)(word >> 40);
			val[3] = (FLAC__byte)(word >> 32);
			val[4] = (FLAC__byte)(word >> 24);
			val[5] = (FLAC__byte)(word >> 16);
			val[6] = (FLAC__byte)(word >> 8);
			val[7] = (FLAC__byte)word;
#else
			for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
				val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
#endif
			val += FLAC__BYTES_PER_WORD;
			nvals -= FLAC__BYTES_PER_WORD;
		}
		else if(!bitreader_read_from_client_(br))
			return false;
	}
	while(nvals) {
		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
			return false;
		*val++ = (FLAC__byte)x;
		nvals--;
	}

	return true;
}

FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
#if 0 /* slow but readable version */
{
	unsigned bit;

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);

	*val = 0;
	while(1) {
		if(!FLAC__bitreader_read_bit(br, &bit))
			return false;
		if(bit)
			break;
		else
			*val++;
	}
	return true;
}
#else
{
	unsigned i;

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);

	*val = 0;
	while(1) {
		while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
			brword b = br->buffer[br->consumed_words] << br->consumed_bits;
			if(b) {
				i = COUNT_ZERO_MSBS(b);
				*val += i;
				i++;
				br->consumed_bits += i;
				if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
					crc16_update_word_(br, br->buffer[br->consumed_words]);
					br->consumed_words++;
					br->consumed_bits = 0;
				}
				return true;
			}
			else {
				*val += FLAC__BITS_PER_WORD - br->consumed_bits;
				crc16_update_word_(br, br->buffer[br->consumed_words]);
				br->consumed_words++;
				br->consumed_bits = 0;
			}
		}
		if(br->bytes) {
			const unsigned end = br->bytes * 8;
			brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
			if(b) {
				i = COUNT_ZERO_MSBS(b);
				*val += i;
				i++;
				br->consumed_bits += i;
				FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
				return true;
			}
			else {
				*val += end - br->consumed_bits;
				br->consumed_bits += end;
				FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
			}
		}
		if(!bitreader_read_from_client_(br))
			return false;
	}
}
#endif

FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
{
	FLAC__uint32 lsbs = 0, msbs = 0;
	unsigned uval;

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);
	FLAC__ASSERT(parameter <= 31);

	if(!FLAC__bitreader_read_unary_unsigned(br, (unsigned int*) &msbs))
		return false;

	if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
		return false;

	uval = (msbs << parameter) | lsbs;
	if(uval & 1)
		*val = -((int)(uval >> 1)) - 1;
	else
		*val = (int)(uval >> 1);

	return true;
}

FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
#ifdef _MSC_VER
{
	unsigned i;
	unsigned uval = 0;
	unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */

	register unsigned cwords;
	register unsigned cbits;

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);
	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
	FLAC__ASSERT(parameter < 32);

	if(nvals == 0)
		return true;

	cbits = br->consumed_bits;
	cwords = br->consumed_words;

	while(1) {

		while(1) {
			while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
				brword b = br->buffer[cwords] << cbits;
				if(b) {
#if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
					__asm {
						bsr eax, b
						not eax
						and eax, 31
						mov i, eax
					}
#else
					i = COUNT_ZERO_MSBS(b);
#endif
					uval += i;
					bits = parameter;
					i++;
					cbits += i;
					if(cbits == FLAC__BITS_PER_WORD) {
						crc16_update_word_(br, br->buffer[cwords]);
						cwords++;
						cbits = 0;
					}
					goto break1;
				}
				else {
					uval += FLAC__BITS_PER_WORD - cbits;
					crc16_update_word_(br, br->buffer[cwords]);
					cwords++;
					cbits = 0;
				}
			}
			if(br->bytes) {
				const unsigned end = br->bytes * 8;
				brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
				if(b) {
					i = COUNT_ZERO_MSBS(b);
					uval += i;
					bits = parameter;
					i++;
					cbits += i;
					FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
					goto break1;
				}
				else {
					uval += end - cbits;
					cbits += end;
					FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
				}
			}
			br->consumed_bits = cbits;
			br->consumed_words = cwords;
			if(!bitreader_read_from_client_(br))
				return false;
			cwords = br->consumed_words;
		}
break1:
		FLAC__ASSERT(cwords <= br->words);

		if(bits) {
			while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
				br->consumed_bits = cbits;
				br->consumed_words = cwords;
				if(!bitreader_read_from_client_(br))
					return false;
				cwords = br->consumed_words;
			}
			if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
				if(cbits) {
					const unsigned n = FLAC__BITS_PER_WORD - cbits;
					const brword word = br->buffer[cwords];
					if(bits < n) {
						uval <<= bits;
						uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
						cbits += bits;
						goto break2;
					}
					uval <<= n;
					uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
					bits -= n;
					crc16_update_word_(br, word);
					cwords++;
					cbits = 0;
					if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
						uval <<= bits;
						uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
						cbits = bits;
					}
					goto break2;
				}
				else {
					FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
					uval <<= bits;
					uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
					cbits = bits;
					goto break2;
				}
			}
			else {
				uval <<= bits;
				if(cbits) {
					FLAC__ASSERT(cbits + bits <= br->bytes*8);
					uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
					cbits += bits;
					goto break2;
				}
				else {
					uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
					cbits += bits;
					goto break2;
				}
			}
		}
break2:
		*vals = (int)(uval >> 1 ^ -(int)(uval & 1));

		--nvals;
		if(nvals == 0) {
			br->consumed_bits = cbits;
			br->consumed_words = cwords;
			return true;
		}

		uval = 0;
		++vals;

	}
}
#else
{
	unsigned i;
	unsigned uval = 0;

	register unsigned cwords;
	register unsigned cbits;
	unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);
	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
	FLAC__ASSERT(parameter < 32);

	if(nvals == 0)
		return true;

	cbits = br->consumed_bits;
	cwords = br->consumed_words;
	ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;

	while(1) {

		while(1) {
			while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
				brword b = br->buffer[cwords] << cbits;
				if(b) {
#if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
					asm volatile (
						"bsrl %1, %0;"
						"notl %0;"
						"andl $31, %0;"
						: "=r"(i)
						: "r"(b)
					);
#else
					i = COUNT_ZERO_MSBS(b);
#endif
					uval += i;
					cbits += i;
					cbits++; /* skip over stop bit */
					if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
						crc16_update_word_(br, br->buffer[cwords]);
						cwords++;
						cbits = 0;
					}
					goto break1;
				}
				else {
					uval += FLAC__BITS_PER_WORD - cbits;
					crc16_update_word_(br, br->buffer[cwords]);
					cwords++;
					cbits = 0;
				}
			}
			if(br->bytes) {
				const unsigned end = br->bytes * 8;
				brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
				if(b) {
					i = COUNT_ZERO_MSBS(b);
					uval += i;
					cbits += i;
					cbits++; /* skip over stop bit */
					FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
					goto break1;
				}
				else {
					uval += end - cbits;
					cbits += end;
					FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
				}
			}
			br->consumed_bits = cbits;
			br->consumed_words = cwords;
			if(!bitreader_read_from_client_(br))
				return false;
			cwords = br->consumed_words;
			ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
		}
break1:
		ucbits -= uval;
		ucbits--; /* account for stop bit */

		FLAC__ASSERT(cwords <= br->words);

		if(parameter) {
			while(ucbits < parameter) {
				br->consumed_bits = cbits;
				br->consumed_words = cwords;
				if(!bitreader_read_from_client_(br))
					return false;
				cwords = br->consumed_words;
				ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
			}
			if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
				if(cbits) {
					const unsigned n = FLAC__BITS_PER_WORD - cbits;
					const brword word = br->buffer[cwords];
					if(parameter < n) {
						uval <<= parameter;
						uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
						cbits += parameter;
					}
					else {
						uval <<= n;
						uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
						crc16_update_word_(br, word);
						cwords++;
						cbits = parameter - n;
						if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
							uval <<= cbits;
							uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
						}
					}
				}
				else {
					cbits = parameter;
					uval <<= parameter;
					uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
				}
			}
			else {
				uval <<= parameter;
				if(cbits) {
					FLAC__ASSERT(cbits + parameter <= br->bytes*8);
					uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
					cbits += parameter;
				}
				else {
					cbits = parameter;
					uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
				}
			}
		}

		ucbits -= parameter;

		*vals = (int)(uval >> 1 ^ -(int)(uval & 1));

		--nvals;
		if(nvals == 0) {
			br->consumed_bits = cbits;
			br->consumed_words = cwords;
			return true;
		}

		uval = 0;
		++vals;

	}
}
#endif

#if 0 /* UNUSED */
FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
{
	FLAC__uint32 lsbs = 0, msbs = 0;
	unsigned bit, uval, k;

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);

	k = FLAC__bitmath_ilog2(parameter);

	if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
		return false;

	if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
		return false;

	if(parameter == 1u<<k) {
		uval = (msbs << k) | lsbs;
	}
	else {
		unsigned d = (1 << (k+1)) - parameter;
		if(lsbs >= d) {
			if(!FLAC__bitreader_read_bit(br, &bit))
				return false;
			lsbs <<= 1;
			lsbs |= bit;
			lsbs -= d;
		}
		uval = msbs * parameter + lsbs;
	}

	if(uval & 1)
		*val = -((int)(uval >> 1)) - 1;
	else
		*val = (int)(uval >> 1);

	return true;
}

FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
{
	FLAC__uint32 lsbs, msbs = 0;
	unsigned bit, k;

	FLAC__ASSERT(0 != br);
	FLAC__ASSERT(0 != br->buffer);

	k = FLAC__bitmath_ilog2(parameter);

	if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
		return false;

	if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
		return false;

	if(parameter == 1u<<k) {
		*val = (msbs << k) | lsbs;
	}
	else {
		unsigned d = (1 << (k+1)) - parameter;
		if(lsbs >= d) {
			if(!FLAC__bitreader_read_bit(br, &bit))
				return false;
			lsbs <<= 1;
			lsbs |= bit;
			lsbs -= d;
		}
		*val = msbs * parameter + lsbs;
	}

	return true;
}
#endif /* UNUSED */

FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
{
	FLAC__uint32 v = 0;
	FLAC__uint32 x;
	unsigned i;

	if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
		return false;
	if(raw)
		raw[(*rawlen)++] = (FLAC__byte)x;
	if(!(x & 0x80)) { /* 0xxxxxxx */
		v = x;
		i = 0;
	}
	else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
		v = x & 0x1F;
		i = 1;
	}
	else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
		v = x & 0x0F;
		i = 2;
	}
	else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
		v = x & 0x07;
		i = 3;
	}
	else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
		v = x & 0x03;
		i = 4;
	}
	else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
		v = x & 0x01;
		i = 5;
	}
	else {
		*val = 0xffffffff;
		return true;
	}
	for( ; i; i--) {
		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
			return false;
		if(raw)
			raw[(*rawlen)++] = (FLAC__byte)x;
		if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
			*val = 0xffffffff;
			return true;
		}
		v <<= 6;
		v |= (x & 0x3F);
	}
	*val = v;
	return true;
}

FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
{
	FLAC__uint64 v = 0;
	FLAC__uint32 x;
	unsigned i;

	if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
		return false;
	if(raw)
		raw[(*rawlen)++] = (FLAC__byte)x;
	if(!(x & 0x80)) { /* 0xxxxxxx */
		v = x;
		i = 0;
	}
	else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
		v = x & 0x1F;
		i = 1;
	}
	else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
		v = x & 0x0F;
		i = 2;
	}
	else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
		v = x & 0x07;
		i = 3;
	}
	else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
		v = x & 0x03;
		i = 4;
	}
	else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
		v = x & 0x01;
		i = 5;
	}
	else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
		v = 0;
		i = 6;
	}
	else {
		*val = FLAC__U64L(0xffffffffffffffff);
		return true;
	}
	for( ; i; i--) {
		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
			return false;
		if(raw)
			raw[(*rawlen)++] = (FLAC__byte)x;
		if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
			*val = FLAC__U64L(0xffffffffffffffff);
			return true;
		}
		v <<= 6;
		v |= (x & 0x3F);
	}
	*val = v;
	return true;
}

#endif
/*** End of inlined file: bitreader.c ***/


/*** Start of inlined file: bitwriter.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy(), memset() */
#ifdef _MSC_VER
#include <winsock.h> /* for ntohl() */
#elif defined FLAC__SYS_DARWIN
#include <machine/endian.h> /* for ntohl() */
#elif defined __MINGW32__
#include <winsock.h> /* for ntohl() */
#else
#include <netinet/in.h> /* for ntohl() */
#endif
#if 0 /* UNUSED */

#endif

/*** Start of inlined file: bitwriter.h ***/
#ifndef FLAC__PRIVATE__BITWRITER_H
#define FLAC__PRIVATE__BITWRITER_H

#include <stdio.h> /* for FILE */

struct FLAC__BitWriter;
typedef struct FLAC__BitWriter FLAC__BitWriter;

FLAC__BitWriter *FLAC__bitwriter_new(void);
void FLAC__bitwriter_delete(FLAC__BitWriter *bw);
FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw);
void FLAC__bitwriter_free(FLAC__BitWriter *bw); /* does not 'free(buffer)' */
void FLAC__bitwriter_clear(FLAC__BitWriter *bw);
void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out);

FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc);
FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc);

FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw);
unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw); /* can be called anytime, returns total # of bits unconsumed */

FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes);
void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw);

FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits);
FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits);
FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits);
FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits);
FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val); /*only for bits=32*/
FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals);
FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val);
unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter);
#if 0 /* UNUSED */
unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter);
unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned val, unsigned parameter);
#endif
FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter);
FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter);
#if 0 /* UNUSED */
FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter);
FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned val, unsigned parameter);
#endif
FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val);
FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val);
FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw);

#endif
/*** End of inlined file: bitwriter.h ***/



/*** Start of inlined file: alloc.h ***/
#ifndef FLAC__SHARE__ALLOC_H
#define FLAC__SHARE__ALLOC_H

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <limits.h> /* for SIZE_MAX */
#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
#include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
#endif
#include <stdlib.h> /* for size_t, malloc(), etc */

#ifndef SIZE_MAX
# ifndef SIZE_T_MAX
#  ifdef _MSC_VER
#   define SIZE_T_MAX UINT_MAX
#  else
#   error
#  endif
# endif
# define SIZE_MAX SIZE_T_MAX
#endif

#ifndef FLaC__INLINE
#define FLaC__INLINE
#endif

static FLaC__INLINE void *safe_malloc_(size_t size)
{
	if(!size)
		size++;
	return malloc(size);
}

static FLaC__INLINE void *safe_calloc_(size_t nmemb, size_t size)
{
	if(!nmemb || !size)
		return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
	return calloc(nmemb, size);
}

static FLaC__INLINE void *safe_malloc_add_2op_(size_t size1, size_t size2)
{
	size2 += size1;
	if(size2 < size1)
		return 0;
	return safe_malloc_(size2);
}

static FLaC__INLINE void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
{
	size2 += size1;
	if(size2 < size1)
		return 0;
	size3 += size2;
	if(size3 < size2)
		return 0;
	return safe_malloc_(size3);
}

static FLaC__INLINE void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
{
	size2 += size1;
	if(size2 < size1)
		return 0;
	size3 += size2;
	if(size3 < size2)
		return 0;
	size4 += size3;
	if(size4 < size3)
		return 0;
	return safe_malloc_(size4);
}

static FLaC__INLINE void *safe_malloc_mul_2op_(size_t size1, size_t size2)
#if 0
needs support for cases where sizeof(size_t) != 4
{
	if(sizeof(size_t) == 4) {
		if ((double)size1 * (double)size2 < 4294967296.0)
			return malloc(size1*size2);
	}
	return 0;
}
#else
{
	if(!size1 || !size2)
		return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
	if(size1 > SIZE_MAX / size2)
		return 0;
	return malloc(size1*size2);
}
#endif

static FLaC__INLINE void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
{
	if(!size1 || !size2 || !size3)
		return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
	if(size1 > SIZE_MAX / size2)
		return 0;
	size1 *= size2;
	if(size1 > SIZE_MAX / size3)
		return 0;
	return malloc(size1*size3);
}

static FLaC__INLINE void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
{
	if(!size1 || !size2)
		return safe_malloc_(size3);
	if(size1 > SIZE_MAX / size2)
		return 0;
	return safe_malloc_add_2op_(size1*size2, size3);
}

static FLaC__INLINE void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
{
	if(!size1 || (!size2 && !size3))
		return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
	size2 += size3;
	if(size2 < size3)
		return 0;
	return safe_malloc_mul_2op_(size1, size2);
}

static FLaC__INLINE void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
{
	size2 += size1;
	if(size2 < size1)
		return 0;
	return realloc(ptr, size2);
}

static FLaC__INLINE void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
{
	size2 += size1;
	if(size2 < size1)
		return 0;
	size3 += size2;
	if(size3 < size2)
		return 0;
	return realloc(ptr, size3);
}

static FLaC__INLINE void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
{
	size2 += size1;
	if(size2 < size1)
		return 0;
	size3 += size2;
	if(size3 < size2)
		return 0;
	size4 += size3;
	if(size4 < size3)
		return 0;
	return realloc(ptr, size4);
}

static FLaC__INLINE void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
{
	if(!size1 || !size2)
		return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
	if(size1 > SIZE_MAX / size2)
		return 0;
	return realloc(ptr, size1*size2);
}

static FLaC__INLINE void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
{
	if(!size1 || (!size2 && !size3))
		return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
	size2 += size3;
	if(size2 < size3)
		return 0;
	return safe_realloc_mul_2op_(ptr, size1, size2);
}

#endif
/*** End of inlined file: alloc.h ***/

typedef FLAC__uint32 bwword;
#define FLAC__BYTES_PER_WORD 4
#define FLAC__BITS_PER_WORD 32
#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
#if WORDS_BIGENDIAN
#define SWAP_BE_WORD_TO_HOST(x) (x)
#else
#ifdef _MSC_VER
#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
#else
#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
#endif
#endif

static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */

#define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
#define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)

#ifdef min
#undef min
#endif
#define min(x,y) ((x)<(y)?(x):(y))

#ifdef _MSC_VER
#define FLAC__U64L(x) x
#else
#define FLAC__U64L(x) x##LLU
#endif

#ifndef FLaC__INLINE
#define FLaC__INLINE
#endif

struct FLAC__BitWriter {
	bwword *buffer;
	bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
	unsigned capacity; /* capacity of buffer in words */
	unsigned words; /* # of complete words in buffer */
	unsigned bits; /* # of used bits in accum */
};

static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
{
	unsigned new_capacity;
	bwword *new_buffer;

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);

	new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);

	if(bw->capacity >= new_capacity)
		return true;

	if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
		new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
	FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
	FLAC__ASSERT(new_capacity > bw->capacity);
	FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));

	new_buffer = (bwword*)safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
	if(new_buffer == 0)
		return false;
	bw->buffer = new_buffer;
	bw->capacity = new_capacity;
	return true;
}

FLAC__BitWriter *FLAC__bitwriter_new(void)
{
	FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
	return bw;
}

void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
{
	FLAC__ASSERT(0 != bw);

	FLAC__bitwriter_free(bw);
	free(bw);
}

FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
{
	FLAC__ASSERT(0 != bw);

	bw->words = bw->bits = 0;
	bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
	bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
	if(bw->buffer == 0)
		return false;

	return true;
}

void FLAC__bitwriter_free(FLAC__BitWriter *bw)
{
	FLAC__ASSERT(0 != bw);

	if(0 != bw->buffer)
		free(bw->buffer);
	bw->buffer = 0;
	bw->capacity = 0;
	bw->words = bw->bits = 0;
}

void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
{
	bw->words = bw->bits = 0;
}

void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
{
	unsigned i, j;
	if(bw == 0) {
		fprintf(out, "bitwriter is NULL\n");
	}
	else {
		fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));

		for(i = 0; i < bw->words; i++) {
			fprintf(out, "%08X: ", i);
			for(j = 0; j < FLAC__BITS_PER_WORD; j++)
				fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
			fprintf(out, "\n");
		}
		if(bw->bits > 0) {
			fprintf(out, "%08X: ", i);
			for(j = 0; j < bw->bits; j++)
				fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
			fprintf(out, "\n");
		}
	}
}

FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
{
	const FLAC__byte *buffer;
	size_t bytes;

	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */

	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
		return false;

	*crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
	FLAC__bitwriter_release_buffer(bw);
	return true;
}

FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
{
	const FLAC__byte *buffer;
	size_t bytes;

	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */

	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
		return false;

	*crc = FLAC__crc8(buffer, bytes);
	FLAC__bitwriter_release_buffer(bw);
	return true;
}

FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
{
	return ((bw->bits & 7) == 0);
}

unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
{
	return FLAC__TOTAL_BITS(bw);
}

FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
{
	FLAC__ASSERT((bw->bits & 7) == 0);
	if(bw->bits & 7)
		return false;
	if(bw->bits) {
		FLAC__ASSERT(bw->words <= bw->capacity);
		if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
			return false;
		bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
	}
	*buffer = (FLAC__byte*)bw->buffer;
	*bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
	return true;
}

void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
{
	(void)bw;
}

FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
{
	unsigned n;

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);

	if(bits == 0)
		return true;
	if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
		return false;
	if(bw->bits) {
		n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
		bw->accum <<= n;
		bits -= n;
		bw->bits += n;
		if(bw->bits == FLAC__BITS_PER_WORD) {
			bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
			bw->bits = 0;
		}
		else
			return true;
	}
	while(bits >= FLAC__BITS_PER_WORD) {
		bw->buffer[bw->words++] = 0;
		bits -= FLAC__BITS_PER_WORD;
	}
	if(bits > 0) {
		bw->accum = 0;
		bw->bits = bits;
	}
	return true;
}

FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
{
	register unsigned left;

	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);

	FLAC__ASSERT(bits <= 32);
	if(bits == 0)
		return true;

	if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
		return false;

	left = FLAC__BITS_PER_WORD - bw->bits;
	if(bits < left) {
		bw->accum <<= bits;
		bw->accum |= val;
		bw->bits += bits;
	}
	else if(bw->bits) { /* WATCHOUT: if bw->bits == 0, left==FLAC__BITS_PER_WORD and bw->accum<<=left is a NOP instead of setting to 0 */
		bw->accum <<= left;
		bw->accum |= val >> (bw->bits = bits - left);
		bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
		bw->accum = val;
	}
	else {
		bw->accum = val;
		bw->bits = 0;
		bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
	}

	return true;
}

FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
{
	if(bits < 32)
		val &= (~(0xffffffff << bits));

	return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
}

FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
{
	if(bits > 32) {
		return
			FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
			FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
	}
	else
		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
}

FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
{

	if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
		return false;
	if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
		return false;
	if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
		return false;
	if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
		return false;

	return true;
}

FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
{
	unsigned i;

	for(i = 0; i < nvals; i++) {
		if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
			return false;
	}

	return true;
}

FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
{
	if(val < 32)
		return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
	else
		return
			FLAC__bitwriter_write_zeroes(bw, val) &&
			FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
}

unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
{
	FLAC__uint32 uval;

	FLAC__ASSERT(parameter < sizeof(unsigned)*8);

	uval = (val<<1) ^ (val>>31);

	return 1 + parameter + (uval >> parameter);
}

#if 0 /* UNUSED */
unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
{
	unsigned bits, msbs, uval;
	unsigned k;

	FLAC__ASSERT(parameter > 0);

	if(val < 0)
		uval = (unsigned)(((-(++val)) << 1) + 1);
	else
		uval = (unsigned)(val << 1);

	k = FLAC__bitmath_ilog2(parameter);
	if(parameter == 1u<<k) {
		FLAC__ASSERT(k <= 30);

		msbs = uval >> k;
		bits = 1 + k + msbs;
	}
	else {
		unsigned q, r, d;

		d = (1 << (k+1)) - parameter;
		q = uval / parameter;
		r = uval - (q * parameter);

		bits = 1 + q + k;
		if(r >= d)
			bits++;
	}
	return bits;
}

unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
{
	unsigned bits, msbs;
	unsigned k;

	FLAC__ASSERT(parameter > 0);

	k = FLAC__bitmath_ilog2(parameter);
	if(parameter == 1u<<k) {
		FLAC__ASSERT(k <= 30);

		msbs = uval >> k;
		bits = 1 + k + msbs;
	}
	else {
		unsigned q, r, d;

		d = (1 << (k+1)) - parameter;
		q = uval / parameter;
		r = uval - (q * parameter);

		bits = 1 + q + k;
		if(r >= d)
			bits++;
	}
	return bits;
}
#endif /* UNUSED */

FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
{
	unsigned total_bits, interesting_bits, msbs;
	FLAC__uint32 uval, pattern;

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);
	FLAC__ASSERT(parameter < 8*sizeof(uval));

	uval = (val<<1) ^ (val>>31);

	msbs = uval >> parameter;
	interesting_bits = 1 + parameter;
	total_bits = interesting_bits + msbs;
	pattern = 1 << parameter; /* the unary end bit */
	pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */

	if(total_bits <= 32)
		return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
	else
		return
			FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
			FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
}

FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
{
	const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
	const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
	FLAC__uint32 uval;
	unsigned left;
	const unsigned lsbits = 1 + parameter;
	unsigned msbits;

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);
	FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);

	while(nvals) {
		uval = (*vals<<1) ^ (*vals>>31);

		msbits = uval >> parameter;

#if 0 /* OPT: can remove this special case if it doesn't make up for the extra compare (doesn't make a statistically significant difference with msvc or gcc/x86) */
		if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
			bw->bits = bw->bits + msbits + lsbits;
			uval |= mask1; /* set stop bit */
			uval &= mask2; /* mask off unused top bits */
			bw->accum <<= msbits;
			bw->accum <<= lsbits;
			bw->accum |= uval;
			if(bw->bits == FLAC__BITS_PER_WORD) {
				bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
				bw->bits = 0;
				if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
					FLAC__ASSERT(bw->capacity == bw->words);
					return false;
				}
			}
		}
		else {
#elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
		if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
			bw->bits = bw->bits + msbits + lsbits;
			uval |= mask1; /* set stop bit */
			uval &= mask2; /* mask off unused top bits */
			bw->accum <<= msbits + lsbits;
			bw->accum |= uval;
		}
		else {
#endif
			if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 bwword*/ && !bitwriter_grow_(bw, msbits+lsbits))
				return false;

			if(msbits) {
				if(bw->bits) {
					left = FLAC__BITS_PER_WORD - bw->bits;
					if(msbits < left) {
						bw->accum <<= msbits;
						bw->bits += msbits;
						goto break1;
					}
					else {
						bw->accum <<= left;
						msbits -= left;
						bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
						bw->bits = 0;
					}
				}
				while(msbits >= FLAC__BITS_PER_WORD) {
					bw->buffer[bw->words++] = 0;
					msbits -= FLAC__BITS_PER_WORD;
				}
				if(msbits > 0) {
					bw->accum = 0;
					bw->bits = msbits;
				}
			}
break1:
			uval |= mask1; /* set stop bit */
			uval &= mask2; /* mask off unused top bits */

			left = FLAC__BITS_PER_WORD - bw->bits;
			if(lsbits < left) {
				bw->accum <<= lsbits;
				bw->accum |= uval;
				bw->bits += lsbits;
			}
			else {
				FLAC__ASSERT(bw->bits);
				FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
				bw->accum <<= left;
				bw->accum |= uval >> (bw->bits = lsbits - left);
				bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
				bw->accum = uval;
			}
#if 1
		}
#endif
		vals++;
		nvals--;
	}
	return true;
}

#if 0 /* UNUSED */
FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
{
	unsigned total_bits, msbs, uval;
	unsigned k;

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);
	FLAC__ASSERT(parameter > 0);

	if(val < 0)
		uval = (unsigned)(((-(++val)) << 1) + 1);
	else
		uval = (unsigned)(val << 1);

	k = FLAC__bitmath_ilog2(parameter);
	if(parameter == 1u<<k) {
		unsigned pattern;

		FLAC__ASSERT(k <= 30);

		msbs = uval >> k;
		total_bits = 1 + k + msbs;
		pattern = 1 << k; /* the unary end bit */
		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */

		if(total_bits <= 32) {
			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
				return false;
		}
		else {
			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
				return false;
		}
	}
	else {
		unsigned q, r, d;

		d = (1 << (k+1)) - parameter;
		q = uval / parameter;
		r = uval - (q * parameter);
		if(!FLAC__bitwriter_write_zeroes(bw, q))
			return false;
		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
			return false;
		if(r >= d) {
			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
				return false;
		}
		else {
			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
				return false;
		}
	}
	return true;
}

FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
{
	unsigned total_bits, msbs;
	unsigned k;

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);
	FLAC__ASSERT(parameter > 0);

	k = FLAC__bitmath_ilog2(parameter);
	if(parameter == 1u<<k) {
		unsigned pattern;

		FLAC__ASSERT(k <= 30);

		msbs = uval >> k;
		total_bits = 1 + k + msbs;
		pattern = 1 << k; /* the unary end bit */
		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */

		if(total_bits <= 32) {
			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
				return false;
		}
		else {
			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
				return false;
		}
	}
	else {
		unsigned q, r, d;

		d = (1 << (k+1)) - parameter;
		q = uval / parameter;
		r = uval - (q * parameter);
		if(!FLAC__bitwriter_write_zeroes(bw, q))
			return false;
		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
			return false;
		if(r >= d) {
			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
				return false;
		}
		else {
			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
				return false;
		}
	}
	return true;
}
#endif /* UNUSED */

FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
{
	FLAC__bool ok = 1;

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);

	FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */

	if(val < 0x80) {
		return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
	}
	else if(val < 0x800) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
	}
	else if(val < 0x10000) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
	}
	else if(val < 0x200000) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
	}
	else if(val < 0x4000000) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
	}
	else {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
	}

	return ok;
}

FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
{
	FLAC__bool ok = 1;

	FLAC__ASSERT(0 != bw);
	FLAC__ASSERT(0 != bw->buffer);

	FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */

	if(val < 0x80) {
		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
	}
	else if(val < 0x800) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
	}
	else if(val < 0x10000) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
	}
	else if(val < 0x200000) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
	}
	else if(val < 0x4000000) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
	}
	else if(val < 0x80000000) {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
	}
	else {
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
	}

	return ok;
}

FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
{
	if(bw->bits & 7u)
		return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
	else
		return true;
}

#endif
/*** End of inlined file: bitwriter.c ***/


/*** Start of inlined file: cpu.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#if defined FLAC__CPU_IA32
# include <signal.h>
#elif defined FLAC__CPU_PPC
# if !defined FLAC__NO_ASM
#  if defined FLAC__SYS_DARWIN
#   include <sys/sysctl.h>
#   include <mach/mach.h>
#   include <mach/mach_host.h>
#   include <mach/host_info.h>
#   include <mach/machine.h>
#   ifndef CPU_SUBTYPE_POWERPC_970
#	define CPU_SUBTYPE_POWERPC_970 ((cpu_subtype_t) 100)
#   endif
#  else /* FLAC__SYS_DARWIN */

#   include <signal.h>
#   include <setjmp.h>

static sigjmp_buf jmpbuf;
static volatile sig_atomic_t canjump = 0;

static void sigill_handler (int sig)
{
	if (!canjump) {
		signal (sig, SIG_DFL);
		raise (sig);
	}
	canjump = 0;
	siglongjmp (jmpbuf, 1);
}
#  endif /* FLAC__SYS_DARWIN */
# endif /* FLAC__NO_ASM */
#endif /* FLAC__CPU_PPC */

#if defined (__NetBSD__) || defined(__OpenBSD__)
#include <sys/param.h>
#include <sys/sysctl.h>
#include <machine/cpu.h>
#endif

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif

#if defined(__APPLE__)
#endif

static const unsigned FLAC__CPUINFO_IA32_CPUID_CMOV = 0x00008000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_MMX = 0x00800000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_FXSR = 0x01000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE = 0x02000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE2 = 0x04000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE3 = 0x00000001;
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSSE3 = 0x00000200;
static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_3DNOW = 0x80000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXT3DNOW = 0x40000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXTMMX = 0x00400000;

#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM && !defined FLAC__NO_SSE_OS && !defined FLAC__SSE_OS
# if defined(__linux__)
#  undef USE_OBSOLETE_SIGCONTEXT_FLAVOR /* #define this to use the older signal handler method */
#  ifdef USE_OBSOLETE_SIGCONTEXT_FLAVOR
	static void sigill_handler_sse_os(int signal, struct sigcontext sc)
	{
		(void)signal;
		sc.eip += 3 + 3 + 6;
	}
#  else
#   include <sys/ucontext.h>
	static void sigill_handler_sse_os(int signal, siginfo_t *si, void *uc)
	{
		(void)signal, (void)si;
		((ucontext_t*)uc)->uc_mcontext.gregs[14/*REG_EIP*/] += 3 + 3 + 6;
	}
#  endif
# elif defined(_MSC_VER)
#  include <windows.h>
#  undef USE_TRY_CATCH_FLAVOR /* #define this to use the try/catch method for catching illegal opcode exception */
#  ifdef USE_TRY_CATCH_FLAVOR
#  else
	LONG CALLBACK sigill_handler_sse_os(EXCEPTION_POINTERS *ep)
	{
		if(ep->ExceptionRecord->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION) {
			ep->ContextRecord->Eip += 3 + 3 + 6;
			return EXCEPTION_CONTINUE_EXECUTION;
		}
		return EXCEPTION_CONTINUE_SEARCH;
	}
#  endif
# endif
#endif

void FLAC__cpu_info(FLAC__CPUInfo *info)
{
#ifdef FLAC__CPU_IA32
	info->type = FLAC__CPUINFO_TYPE_IA32;
#if !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
	info->use_asm = true; /* we assume a minimum of 80386 with FLAC__CPU_IA32 */
	info->data.ia32.cpuid = FLAC__cpu_have_cpuid_asm_ia32()? true : false;
	info->data.ia32.bswap = info->data.ia32.cpuid; /* CPUID => BSWAP since it came after */
	info->data.ia32.cmov = false;
	info->data.ia32.mmx = false;
	info->data.ia32.fxsr = false;
	info->data.ia32.sse = false;
	info->data.ia32.sse2 = false;
	info->data.ia32.sse3 = false;
	info->data.ia32.ssse3 = false;
	info->data.ia32._3dnow = false;
	info->data.ia32.ext3dnow = false;
	info->data.ia32.extmmx = false;
	if(info->data.ia32.cpuid) {
		FLAC__uint32 flags_edx, flags_ecx;
		FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx);
		info->data.ia32.cmov  = (flags_edx & FLAC__CPUINFO_IA32_CPUID_CMOV )? true : false;
		info->data.ia32.mmx   = (flags_edx & FLAC__CPUINFO_IA32_CPUID_MMX  )? true : false;
		info->data.ia32.fxsr  = (flags_edx & FLAC__CPUINFO_IA32_CPUID_FXSR )? true : false;
		info->data.ia32.sse   = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE  )? true : false;
		info->data.ia32.sse2  = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE2 )? true : false;
		info->data.ia32.sse3  = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 )? true : false;
		info->data.ia32.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3)? true : false;

#ifdef FLAC__USE_3DNOW
		flags_edx = FLAC__cpu_info_extended_amd_asm_ia32();
		info->data.ia32._3dnow   = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_3DNOW   )? true : false;
		info->data.ia32.ext3dnow = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXT3DNOW)? true : false;
		info->data.ia32.extmmx   = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXTMMX  )? true : false;
#else
		info->data.ia32._3dnow = info->data.ia32.ext3dnow = info->data.ia32.extmmx = false;
#endif

#ifdef DEBUG
		fprintf(stderr, "CPU info (IA-32):\n");
		fprintf(stderr, "  CPUID ...... %c\n", info->data.ia32.cpuid   ? 'Y' : 'n');
		fprintf(stderr, "  BSWAP ...... %c\n", info->data.ia32.bswap   ? 'Y' : 'n');
		fprintf(stderr, "  CMOV ....... %c\n", info->data.ia32.cmov    ? 'Y' : 'n');
		fprintf(stderr, "  MMX ........ %c\n", info->data.ia32.mmx     ? 'Y' : 'n');
		fprintf(stderr, "  FXSR ....... %c\n", info->data.ia32.fxsr    ? 'Y' : 'n');
		fprintf(stderr, "  SSE ........ %c\n", info->data.ia32.sse     ? 'Y' : 'n');
		fprintf(stderr, "  SSE2 ....... %c\n", info->data.ia32.sse2    ? 'Y' : 'n');
		fprintf(stderr, "  SSE3 ....... %c\n", info->data.ia32.sse3    ? 'Y' : 'n');
		fprintf(stderr, "  SSSE3 ...... %c\n", info->data.ia32.ssse3   ? 'Y' : 'n');
		fprintf(stderr, "  3DNow! ..... %c\n", info->data.ia32._3dnow  ? 'Y' : 'n');
		fprintf(stderr, "  3DNow!-ext . %c\n", info->data.ia32.ext3dnow? 'Y' : 'n');
		fprintf(stderr, "  3DNow!-MMX . %c\n", info->data.ia32.extmmx  ? 'Y' : 'n');
#endif

		if(info->data.ia32.fxsr || info->data.ia32.sse || info->data.ia32.sse2) {
#if defined FLAC__NO_SSE_OS
			info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
#elif defined FLAC__SSE_OS
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
			int sse = 0;
			size_t len;
			len = sizeof(sse); sse = sse || (sysctlbyname("hw.instruction_sse", &sse, &len, NULL, 0) == 0 && sse);
			len = sizeof(sse); sse = sse || (sysctlbyname("hw.optional.sse"   , &sse, &len, NULL, 0) == 0 && sse); /* __APPLE__ ? */
			if(!sse)
				info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
#elif defined(__NetBSD__) || defined (__OpenBSD__)
# if __NetBSD_Version__ >= 105250000 || (defined __OpenBSD__)
			int val = 0, mib[2] = { CTL_MACHDEP, CPU_SSE };
			size_t len = sizeof(val);
			if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val)
				info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
			else { /* double-check SSE2 */
				mib[1] = CPU_SSE2;
				len = sizeof(val);
				if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val)
					info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
			}
# else
			info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
# endif
#elif defined(__linux__)
			int sse = 0;
			struct sigaction sigill_save;
#ifdef USE_OBSOLETE_SIGCONTEXT_FLAVOR
			if(0 == sigaction(SIGILL, NULL, &sigill_save) && signal(SIGILL, (void (*)(int))sigill_handler_sse_os) != SIG_ERR)
#else
			struct sigaction sigill_sse;
			sigill_sse.sa_sigaction = sigill_handler_sse_os;
			__sigemptyset(&sigill_sse.sa_mask);
			sigill_sse.sa_flags = SA_SIGINFO | SA_RESETHAND; /* SA_RESETHAND just in case our SIGILL return jump breaks, so we don't get stuck in a loop */
			if(0 == sigaction(SIGILL, &sigill_sse, &sigill_save))
#endif
			{
				asm volatile (
					"xorl %0,%0\n\t"          /* for some reason, still need to do this to clear 'sse' var */
					"xorps %%xmm0,%%xmm0\n\t" /* will cause SIGILL if unsupported by OS */
					"incl %0\n\t"             /* SIGILL handler will jump over this */
					"nop\n\t" /* SIGILL jump lands here if "inc" is 9 bytes */
					"nop\n\t"
					"nop\n\t"
					"nop\n\t"
					"nop\n\t"
					"nop\n\t"
					"nop\n\t" /* SIGILL jump lands here if "inc" is 3 bytes (expected) */
					"nop\n\t"
					"nop"     /* SIGILL jump lands here if "inc" is 1 byte */
					: "=r"(sse)
					: "r"(sse)
				);

				sigaction(SIGILL, &sigill_save, NULL);
			}

			if(!sse)
				info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
#elif defined(_MSC_VER)
# ifdef USE_TRY_CATCH_FLAVOR
			_try {
				__asm {
#  if _MSC_VER <= 1200
					_emit 0x0F
					_emit 0x57
					_emit 0xC0
#  else
					xorps xmm0,xmm0
#  endif
				}
			}
			_except(EXCEPTION_EXECUTE_HANDLER) {
				if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION)
					info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
			}
# else
			int sse = 0;
			LPTOP_LEVEL_EXCEPTION_FILTER save = SetUnhandledExceptionFilter(sigill_handler_sse_os);
			__asm {
#  if _MSC_VER <= 1200
				_emit 0x0F
				_emit 0x57
				_emit 0xC0
#  else
				xorps xmm0,xmm0
#  endif
				inc sse
				nop
				nop
				nop
				nop
				nop
				nop
				nop
				nop
				nop
			}
			SetUnhandledExceptionFilter(save);
			if(!sse)
				info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
# endif
#else
			info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false;
#endif
#ifdef DEBUG
		fprintf(stderr, "  SSE OS sup . %c\n", info->data.ia32.sse     ? 'Y' : 'n');
#endif

		}
	}
#else
	info->use_asm = false;
#endif

#elif defined FLAC__CPU_PPC
	info->type = FLAC__CPUINFO_TYPE_PPC;
# if !defined FLAC__NO_ASM
	info->use_asm = true;
#  ifdef FLAC__USE_ALTIVEC
#   if defined FLAC__SYS_DARWIN
	{
		int val = 0, mib[2] = { CTL_HW, HW_VECTORUNIT };
		size_t len = sizeof(val);
		info->data.ppc.altivec = !(sysctl(mib, 2, &val, &len, NULL, 0) || !val);
	}
	{
		host_basic_info_data_t hostInfo;
		mach_msg_type_number_t infoCount;

		infoCount = HOST_BASIC_INFO_COUNT;
		host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);

		info->data.ppc.ppc64 = (hostInfo.cpu_type == CPU_TYPE_POWERPC) && (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_970);
	}
#   else /* FLAC__USE_ALTIVEC && !FLAC__SYS_DARWIN */
	{
		info->data.ppc.altivec = 0;
		info->data.ppc.ppc64 = 0;

		signal (SIGILL, sigill_handler);
		canjump = 0;
		if (!sigsetjmp (jmpbuf, 1)) {
			canjump = 1;

			asm volatile (
				"mtspr 256, %0\n\t"
				"vand %%v0, %%v0, %%v0"
				:
				: "r" (-1)
			);

			info->data.ppc.altivec = 1;
		}
		canjump = 0;
		if (!sigsetjmp (jmpbuf, 1)) {
			int x = 0;
			canjump = 1;

			asm volatile ("cntlzd %0, %1" : "=r" (x) : "r" (x) );

			info->data.ppc.ppc64 = 1;
		}
		signal (SIGILL, SIG_DFL); /*@@@@@@ should save and restore old signal */
	}
#   endif
#  else /* !FLAC__USE_ALTIVEC */
	info->data.ppc.altivec = 0;
	info->data.ppc.ppc64 = 0;
#  endif
# else
	info->use_asm = false;
# endif

#else
	info->type = FLAC__CPUINFO_TYPE_UNKNOWN;
	info->use_asm = false;
#endif
}

#endif
/*** End of inlined file: cpu.c ***/


/*** Start of inlined file: crc.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

FLAC__byte const FLAC__crc8_table[256] = {
	0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
	0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
	0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
	0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
	0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5,
	0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
	0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85,
	0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
	0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
	0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
	0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2,
	0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
	0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32,
	0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
	0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
	0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
	0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C,
	0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
	0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC,
	0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
	0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
	0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
	0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C,
	0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
	0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B,
	0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
	0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
	0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
	0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
	0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
	0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
	0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
};

unsigned FLAC__crc16_table[256] = {
	0x0000,  0x8005,  0x800f,  0x000a,  0x801b,  0x001e,  0x0014,  0x8011,
	0x8033,  0x0036,  0x003c,  0x8039,  0x0028,  0x802d,  0x8027,  0x0022,
	0x8063,  0x0066,  0x006c,  0x8069,  0x0078,  0x807d,  0x8077,  0x0072,
	0x0050,  0x8055,  0x805f,  0x005a,  0x804b,  0x004e,  0x0044,  0x8041,
	0x80c3,  0x00c6,  0x00cc,  0x80c9,  0x00d8,  0x80dd,  0x80d7,  0x00d2,
	0x00f0,  0x80f5,  0x80ff,  0x00fa,  0x80eb,  0x00ee,  0x00e4,  0x80e1,
	0x00a0,  0x80a5,  0x80af,  0x00aa,  0x80bb,  0x00be,  0x00b4,  0x80b1,
	0x8093,  0x0096,  0x009c,  0x8099,  0x0088,  0x808d,  0x8087,  0x0082,
	0x8183,  0x0186,  0x018c,  0x8189,  0x0198,  0x819d,  0x8197,  0x0192,
	0x01b0,  0x81b5,  0x81bf,  0x01ba,  0x81ab,  0x01ae,  0x01a4,  0x81a1,
	0x01e0,  0x81e5,  0x81ef,  0x01ea,  0x81fb,  0x01fe,  0x01f4,  0x81f1,
	0x81d3,  0x01d6,  0x01dc,  0x81d9,  0x01c8,  0x81cd,  0x81c7,  0x01c2,
	0x0140,  0x8145,  0x814f,  0x014a,  0x815b,  0x015e,  0x0154,  0x8151,
	0x8173,  0x0176,  0x017c,  0x8179,  0x0168,  0x816d,  0x8167,  0x0162,
	0x8123,  0x0126,  0x012c,  0x8129,  0x0138,  0x813d,  0x8137,  0x0132,
	0x0110,  0x8115,  0x811f,  0x011a,  0x810b,  0x010e,  0x0104,  0x8101,
	0x8303,  0x0306,  0x030c,  0x8309,  0x0318,  0x831d,  0x8317,  0x0312,
	0x0330,  0x8335,  0x833f,  0x033a,  0x832b,  0x032e,  0x0324,  0x8321,
	0x0360,  0x8365,  0x836f,  0x036a,  0x837b,  0x037e,  0x0374,  0x8371,
	0x8353,  0x0356,  0x035c,  0x8359,  0x0348,  0x834d,  0x8347,  0x0342,
	0x03c0,  0x83c5,  0x83cf,  0x03ca,  0x83db,  0x03de,  0x03d4,  0x83d1,
	0x83f3,  0x03f6,  0x03fc,  0x83f9,  0x03e8,  0x83ed,  0x83e7,  0x03e2,
	0x83a3,  0x03a6,  0x03ac,  0x83a9,  0x03b8,  0x83bd,  0x83b7,  0x03b2,
	0x0390,  0x8395,  0x839f,  0x039a,  0x838b,  0x038e,  0x0384,  0x8381,
	0x0280,  0x8285,  0x828f,  0x028a,  0x829b,  0x029e,  0x0294,  0x8291,
	0x82b3,  0x02b6,  0x02bc,  0x82b9,  0x02a8,  0x82ad,  0x82a7,  0x02a2,
	0x82e3,  0x02e6,  0x02ec,  0x82e9,  0x02f8,  0x82fd,  0x82f7,  0x02f2,
	0x02d0,  0x82d5,  0x82df,  0x02da,  0x82cb,  0x02ce,  0x02c4,  0x82c1,
	0x8243,  0x0246,  0x024c,  0x8249,  0x0258,  0x825d,  0x8257,  0x0252,
	0x0270,  0x8275,  0x827f,  0x027a,  0x826b,  0x026e,  0x0264,  0x8261,
	0x0220,  0x8225,  0x822f,  0x022a,  0x823b,  0x023e,  0x0234,  0x8231,
	0x8213,  0x0216,  0x021c,  0x8219,  0x0208,  0x820d,  0x8207,  0x0202
};

void FLAC__crc8_update(const FLAC__byte data, FLAC__uint8 *crc)
{
	*crc = FLAC__crc8_table[*crc ^ data];
}

void FLAC__crc8_update_block(const FLAC__byte *data, unsigned len, FLAC__uint8 *crc)
{
	while(len--)
		*crc = FLAC__crc8_table[*crc ^ *data++];
}

FLAC__uint8 FLAC__crc8(const FLAC__byte *data, unsigned len)
{
	FLAC__uint8 crc = 0;

	while(len--)
		crc = FLAC__crc8_table[crc ^ *data++];

	return crc;
}

unsigned FLAC__crc16(const FLAC__byte *data, unsigned len)
{
	unsigned crc = 0;

	while(len--)
		crc = ((crc<<8) ^ FLAC__crc16_table[(crc>>8) ^ *data++]) & 0xffff;

	return crc;
}

#endif
/*** End of inlined file: crc.c ***/


/*** Start of inlined file: fixed.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <math.h>
#include <string.h>


/*** Start of inlined file: fixed.h ***/
#ifndef FLAC__PRIVATE__FIXED_H
#define FLAC__PRIVATE__FIXED_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


/*** Start of inlined file: float.h ***/
#ifndef FLAC__PRIVATE__FLOAT_H
#define FLAC__PRIVATE__FLOAT_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifndef FLAC__INTEGER_ONLY_LIBRARY
typedef double FLAC__double;
typedef float FLAC__float;
typedef float FLAC__real;
#else
typedef FLAC__int32 FLAC__fixedpoint;
extern const FLAC__fixedpoint FLAC__FP_ZERO;
extern const FLAC__fixedpoint FLAC__FP_ONE_HALF;
extern const FLAC__fixedpoint FLAC__FP_ONE;
extern const FLAC__fixedpoint FLAC__FP_LN2;
extern const FLAC__fixedpoint FLAC__FP_E;

#define FLAC__fixedpoint_trunc(x) ((x)>>16)

#define FLAC__fixedpoint_mul(x, y) ( (FLAC__fixedpoint) ( ((FLAC__int64)(x)*(FLAC__int64)(y)) >> 16 ) )

#define FLAC__fixedpoint_div(x, y) ( (FLAC__fixedpoint) ( ( ((FLAC__int64)(x)<<32) / (FLAC__int64)(y) ) >> 16 ) )

FLAC__uint32 FLAC__fixedpoint_log2(FLAC__uint32 x, unsigned fracbits, unsigned precision);

#endif

#endif
/*** End of inlined file: float.h ***/


/*** Start of inlined file: format.h ***/
#ifndef FLAC__PRIVATE__FORMAT_H
#define FLAC__PRIVATE__FORMAT_H

unsigned FLAC__format_get_max_rice_partition_order(unsigned blocksize, unsigned predictor_order);
unsigned FLAC__format_get_max_rice_partition_order_from_blocksize(unsigned blocksize);
unsigned FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(unsigned limit, unsigned blocksize, unsigned predictor_order);
void FLAC__format_entropy_coding_method_partitioned_rice_contents_init(FLAC__EntropyCodingMethod_PartitionedRiceContents *object);
void FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(FLAC__EntropyCodingMethod_PartitionedRiceContents *object);
FLAC__bool FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(FLAC__EntropyCodingMethod_PartitionedRiceContents *object, unsigned max_partition_order);

#endif
/*** End of inlined file: format.h ***/

#ifndef FLAC__INTEGER_ONLY_LIBRARY
unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
# ifndef FLAC__NO_ASM
#  ifdef FLAC__CPU_IA32
#   ifdef FLAC__HAS_NASM
unsigned FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
#   endif
#  endif
# endif
unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
#else
unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
#endif

void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[]);

void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[]);

#endif
/*** End of inlined file: fixed.h ***/

#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif

#ifdef min
#undef min
#endif
#define min(x,y) ((x) < (y)? (x) : (y))

#ifdef local_abs
#undef local_abs
#endif
#define local_abs(x) ((unsigned)((x)<0? -(x) : (x)))

#ifdef FLAC__INTEGER_ONLY_LIBRARY
static FLAC__fixedpoint local__compute_rbps_integerized(FLAC__uint32 err, FLAC__uint32 n)
{
	FLAC__uint32 rbps;
	unsigned bits; /* the number of bits required to represent a number */
	int fracbits; /* the number of bits of rbps that comprise the fractional part */

	FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
	FLAC__ASSERT(err > 0);
	FLAC__ASSERT(n > 0);

	FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
	if(err <= n)
		return 0;

	fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2(err)+1);

	err <<= fracbits;
	err /= n;

	FLAC__ASSERT(err > 0);
	bits = FLAC__bitmath_ilog2(err)+1;
	if(bits > 16) {
		err >>= (bits-16);
		fracbits -= (bits-16);
	}
	rbps = (FLAC__uint32)err;

	rbps *= FLAC__FP_LN2;
	fracbits += 16;
	FLAC__ASSERT(fracbits >= 0);

	{
		const int f = fracbits & 3;
		if(f) {
			rbps >>= f;
			fracbits -= f;
		}
	}

	rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1));

	if(rbps == 0)
		return 0;

	FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
	FLAC__ASSERT(fracbits >= -3);

	if(fracbits < 16)
		return rbps << (16-fracbits);
	else if(fracbits > 16)
		return rbps >> (fracbits-16);
	else
		return rbps;
}

static FLAC__fixedpoint local__compute_rbps_wide_integerized(FLAC__uint64 err, FLAC__uint32 n)
{
	FLAC__uint32 rbps;
	unsigned bits; /* the number of bits required to represent a number */
	int fracbits; /* the number of bits of rbps that comprise the fractional part */

	FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
	FLAC__ASSERT(err > 0);
	FLAC__ASSERT(n > 0);

	FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
	if(err <= n)
		return 0;

	fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2_wide(err)+1);

	err <<= fracbits;
	err /= n;

	FLAC__ASSERT(err > 0);
	bits = FLAC__bitmath_ilog2_wide(err)+1;
	if(bits > 16) {
		err >>= (bits-16);
		fracbits -= (bits-16);
	}
	rbps = (FLAC__uint32)err;

	rbps *= FLAC__FP_LN2;
	fracbits += 16;
	FLAC__ASSERT(fracbits >= 0);

	{
		const int f = fracbits & 3;
		if(f) {
			rbps >>= f;
			fracbits -= f;
		}
	}

	rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1));

	if(rbps == 0)
		return 0;

	FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
	FLAC__ASSERT(fracbits >= -3);

	if(fracbits < 16)
		return rbps << (16-fracbits);
	else if(fracbits > 16)
		return rbps >> (fracbits-16);
	else
		return rbps;
}
#endif

#ifndef FLAC__INTEGER_ONLY_LIBRARY
unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
#else
unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
#endif
{
	FLAC__int32 last_error_0 = data[-1];
	FLAC__int32 last_error_1 = data[-1] - data[-2];
	FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
	FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
	FLAC__int32 error, save;
	FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
	unsigned i, order;

	for(i = 0; i < data_len; i++) {
		error  = data[i]	 ; total_error_0 += local_abs(error);			  save = error;
		error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
		error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
		error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
		error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
	}

	if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4))
		order = 0;
	else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4))
		order = 1;
	else if(total_error_2 < min(total_error_3, total_error_4))
		order = 2;
	else if(total_error_3 < total_error_4)
		order = 3;
	else
		order = 4;

	FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
	FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
	FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
	FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
	FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
#else
	residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_integerized(total_error_0, data_len) : 0;
	residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_integerized(total_error_1, data_len) : 0;
	residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_integerized(total_error_2, data_len) : 0;
	residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_integerized(total_error_3, data_len) : 0;
	residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_integerized(total_error_4, data_len) : 0;
#endif

	return order;
}

#ifndef FLAC__INTEGER_ONLY_LIBRARY
unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
#else
unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
#endif
{
	FLAC__int32 last_error_0 = data[-1];
	FLAC__int32 last_error_1 = data[-1] - data[-2];
	FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
	FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
	FLAC__int32 error, save;
	FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
	unsigned i, order;

	for(i = 0; i < data_len; i++) {
		error  = data[i]	 ; total_error_0 += local_abs(error);			  save = error;
		error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
		error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
		error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
		error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
	}

	if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4))
		order = 0;
	else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4))
		order = 1;
	else if(total_error_2 < min(total_error_3, total_error_4))
		order = 2;
	else if(total_error_3 < total_error_4)
		order = 3;
	else
		order = 4;

	FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
	FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
	FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
	FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
	FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
#ifndef FLAC__INTEGER_ONLY_LIBRARY
#if defined _MSC_VER || defined __MINGW32__
	residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
#else
	residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
	residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
#endif
#else
	residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_wide_integerized(total_error_0, data_len) : 0;
	residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_wide_integerized(total_error_1, data_len) : 0;
	residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_wide_integerized(total_error_2, data_len) : 0;
	residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_wide_integerized(total_error_3, data_len) : 0;
	residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_wide_integerized(total_error_4, data_len) : 0;
#endif

	return order;
}

void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
{
	const int idata_len = (int)data_len;
	int i;

	switch(order) {
		case 0:
			FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
			memcpy(residual, data, sizeof(residual[0])*data_len);
			break;
		case 1:
			for(i = 0; i < idata_len; i++)
				residual[i] = data[i] - data[i-1];
			break;
		case 2:
			for(i = 0; i < idata_len; i++)
#if 1 /* OPT: may be faster with some compilers on some systems */
				residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
#else
				residual[i] = data[i] - 2*data[i-1] + data[i-2];
#endif
			break;
		case 3:
			for(i = 0; i < idata_len; i++)
#if 1 /* OPT: may be faster with some compilers on some systems */
				residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
#else
				residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
#endif
			break;
		case 4:
			for(i = 0; i < idata_len; i++)
#if 1 /* OPT: may be faster with some compilers on some systems */
				residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
#else
				residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
#endif
			break;
		default:
			FLAC__ASSERT(0);
	}
}

void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[])
{
	int i, idata_len = (int)data_len;

	switch(order) {
		case 0:
			FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
			memcpy(data, residual, sizeof(residual[0])*data_len);
			break;
		case 1:
			for(i = 0; i < idata_len; i++)
				data[i] = residual[i] + data[i-1];
			break;
		case 2:
			for(i = 0; i < idata_len; i++)
#if 1 /* OPT: may be faster with some compilers on some systems */
				data[i] = residual[i] + (data[i-1]<<1) - data[i-2];
#else
				data[i] = residual[i] + 2*data[i-1] - data[i-2];
#endif
			break;
		case 3:
			for(i = 0; i < idata_len; i++)
#if 1 /* OPT: may be faster with some compilers on some systems */
				data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3];
#else
				data[i] = residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3];
#endif
			break;
		case 4:
			for(i = 0; i < idata_len; i++)
#if 1 /* OPT: may be faster with some compilers on some systems */
				data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4];
#else
				data[i] = residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4];
#endif
			break;
		default:
			FLAC__ASSERT(0);
	}
}

#endif
/*** End of inlined file: fixed.c ***/


/*** Start of inlined file: float.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#ifdef FLAC__INTEGER_ONLY_LIBRARY

#ifdef _MSC_VER
#define FLAC__U64L(x) x
#else
#define FLAC__U64L(x) x##LLU
#endif

const FLAC__fixedpoint FLAC__FP_ZERO = 0;
const FLAC__fixedpoint FLAC__FP_ONE_HALF = 0x00008000;
const FLAC__fixedpoint FLAC__FP_ONE = 0x00010000;
const FLAC__fixedpoint FLAC__FP_LN2 = 45426;
const FLAC__fixedpoint FLAC__FP_E = 178145;

#define LOG2_LOOKUP_PRECISION 16
static const FLAC__uint32 log2_lookup[][LOG2_LOOKUP_PRECISION] = {
	{
 0x00000000,
 0x00000001,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000
	},
	{
 0x00000000,
 0x00000010,
 0x00000007,
 0x00000003,
 0x00000001,
 0x00000001,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000
	},
	{
 0x00000000,
 0x00000100,
 0x0000006a,
 0x00000031,
 0x00000018,
 0x0000000c,
 0x00000006,
 0x00000003,
 0x00000001,
 0x00000001,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000,
 0x00000000
	},
	{
 0x00000000,
 0x00001000,
 0x000006a4,
 0x00000315,
 0x0000017d,
 0x000000bc,
 0x0000005d,
 0x0000002e,
 0x00000017,
 0x0000000c,
 0x00000006,
 0x00000003,
 0x00000001,
 0x00000001,
 0x00000000,
 0x00000000
	},
	{
 0x00000000,
 0x00010000,
 0x00006a40,
 0x00003151,
 0x000017d6,
 0x00000bba,
 0x000005d1,
 0x000002e6,
 0x00000172,
 0x000000b9,
 0x0000005c,
 0x0000002e,
 0x00000017,
 0x0000000c,
 0x00000006,
 0x00000003
	},
	{
 0x00000000,
 0x00100000,
 0x0006a3fe,
 0x00031513,
 0x00017d60,
 0x0000bb9d,
 0x00005d10,
 0x00002e59,
 0x00001721,
 0x00000b8e,
 0x000005c6,
 0x000002e3,
 0x00000171,
 0x000000b9,
 0x0000005c,
 0x0000002e
	},
	{
 0x00000000,
 0x01000000,
 0x006a3fe6,
 0x00315130,
 0x0017d605,
 0x000bb9ca,
 0x0005d0fc,
 0x0002e58f,
 0x0001720e,
 0x0000b8d8,
 0x00005c61,
 0x00002e2d,
 0x00001716,
 0x00000b8b,
 0x000005c5,
 0x000002e3
	},
	{
 0x00000000,
 0x10000000,
 0x06a3fe5c,
 0x03151301,
 0x017d6049,
 0x00bb9ca6,
 0x005d0fba,
 0x002e58f7,
 0x001720da,
 0x000b8d87,
 0x0005c60b,
 0x0002e2d7,
 0x00017160,
 0x0000b8ad,
 0x00005c56,
 0x00002e2b
	}
};

#if 0
static const FLAC__uint64 log2_lookup_wide[] = {
	{
 0x00000000,
 FLAC__U64L(0x100000000),
 FLAC__U64L(0x6a3fe5c6),
 FLAC__U64L(0x31513015),
 FLAC__U64L(0x17d60497),
 FLAC__U64L(0x0bb9ca65),
 FLAC__U64L(0x05d0fba2),
 FLAC__U64L(0x02e58f74),
 FLAC__U64L(0x01720d9c),
 FLAC__U64L(0x00b8d875),
 FLAC__U64L(0x005c60aa),
 FLAC__U64L(0x002e2d72),
 FLAC__U64L(0x00171600),
 FLAC__U64L(0x000b8ad2),
 FLAC__U64L(0x0005c55d),
 FLAC__U64L(0x0002e2ac)
	},
	{
 0x00000000,
 FLAC__U64L(0x1000000000000),
 FLAC__U64L(0x6a3fe5c60429),
 FLAC__U64L(0x315130157f7a),
 FLAC__U64L(0x17d60496cfbb),
 FLAC__U64L(0xbb9ca64ecac),
 FLAC__U64L(0x5d0fba187cd),
 FLAC__U64L(0x2e58f7441ee),
 FLAC__U64L(0x1720d9c06a8),
 FLAC__U64L(0xb8d8752173),
 FLAC__U64L(0x5c60aa252e),
 FLAC__U64L(0x2e2d71b0d8),
 FLAC__U64L(0x1716001719),
 FLAC__U64L(0xb8ad1de1b),
 FLAC__U64L(0x5c55d640d),
 FLAC__U64L(0x2e2abcf52)
	}
};
#endif

FLAC__uint32 FLAC__fixedpoint_log2(FLAC__uint32 x, unsigned fracbits, unsigned precision)
{
	const FLAC__uint32 ONE = (1u << fracbits);
	const FLAC__uint32 *table = log2_lookup[fracbits >> 2];

	FLAC__ASSERT(fracbits < 32);
	FLAC__ASSERT((fracbits & 0x3) == 0);

	if(x < ONE)
		return 0;

	if(precision > LOG2_LOOKUP_PRECISION)
		precision = LOG2_LOOKUP_PRECISION;

	{
		FLAC__uint32 y = 0;
		FLAC__uint32 z = x >> 1, k = 1;
		while (x > ONE && k < precision) {
			if (x - z >= ONE) {
				x -= z;
				z = x >> k;
				y += table[k];
			}
			else {
				z >>= 1;
				k++;
			}
		}
		return y;
	}
}

#endif /* defined FLAC__INTEGER_ONLY_LIBRARY */

#endif
/*** End of inlined file: float.c ***/


/*** Start of inlined file: format.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h> /* for qsort() */
#include <string.h> /* for memset() */

#ifndef FLaC__INLINE
#define FLaC__INLINE
#endif

#ifdef min
#undef min
#endif
#define min(a,b) ((a)<(b)?(a):(b))

#ifdef _MSC_VER
#define FLAC__U64L(x) x
#else
#define FLAC__U64L(x) x##LLU
#endif

FLAC_API const char *FLAC__VERSION_STRING = VERSION

;

#if defined _MSC_VER || defined __BORLANDC__ || defined __MINW32__
FLAC_API const char *FLAC__VENDOR_STRING = "reference libFLAC 1.2.1 20070917";
#else
FLAC_API const char *FLAC__VENDOR_STRING = "reference libFLAC " VERSION " 20070917";
#endif

FLAC_API const FLAC__byte FLAC__STREAM_SYNC_STRING[4] = { 'f','L','a','C' };
FLAC_API const unsigned FLAC__STREAM_SYNC = 0x664C6143;
FLAC_API const unsigned FLAC__STREAM_SYNC_LEN = 32; /* bits */

FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN = 16; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN = 16; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN = 24; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN = 24; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN = 20; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN = 3; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN = 5; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN = 36; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN = 128; /* bits */

FLAC_API const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN = 32; /* bits */

FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN = 64; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN = 64; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN = 16; /* bits */

FLAC_API const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER = FLAC__U64L(0xffffffffffffffff);

FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN = 32; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN = 32; /* bits */

FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN = 64; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN = 8; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN = 3*8; /* bits */

FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN = 64; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN = 8; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN = 12*8; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN = 1; /* bit */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN = 1; /* bit */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN = 6+13*8; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN = 8; /* bits */

FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN = 128*8; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN = 64; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN = 1; /* bit */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN = 7+258*8; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN = 8; /* bits */

FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_TYPE_LEN = 32; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN = 32; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN = 32; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN = 32; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN = 32; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN = 32; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_COLORS_LEN = 32; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN = 32; /* bits */

FLAC_API const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN = 1; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_TYPE_LEN = 7; /* bits */
FLAC_API const unsigned FLAC__STREAM_METADATA_LENGTH_LEN = 24; /* bits */

FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC = 0x3ffe;
FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC_LEN = 14; /* bits */
FLAC_API const unsigned FLAC__FRAME_HEADER_RESERVED_LEN = 1; /* bits */
FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN = 1; /* bits */
FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN = 4; /* bits */
FLAC_API const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN = 4; /* bits */
FLAC_API const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN = 4; /* bits */
FLAC_API const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN = 3; /* bits */
FLAC_API const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN = 1; /* bits */
FLAC_API const unsigned FLAC__FRAME_HEADER_CRC_LEN = 8; /* bits */

FLAC_API const unsigned FLAC__FRAME_FOOTER_CRC_LEN = 16; /* bits */

FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN = 2; /* bits */
FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN = 4; /* bits */
FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN = 4; /* bits */
FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN = 5; /* bits */
FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN = 5; /* bits */

FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER = 15; /* == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)-1 */
FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER = 31; /* == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN)-1 */

FLAC_API const char * const FLAC__EntropyCodingMethodTypeString[] = {
	"PARTITIONED_RICE",
	"PARTITIONED_RICE2"
};

FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN = 4; /* bits */
FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN = 5; /* bits */

FLAC_API const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN = 1; /* bits */
FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LEN = 6; /* bits */
FLAC_API const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN = 1; /* bits */

FLAC_API const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK = 0x00;
FLAC_API const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK = 0x02;
FLAC_API const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK = 0x10;
FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK = 0x40;

FLAC_API const char * const FLAC__SubframeTypeString[] = {
	"CONSTANT",
	"VERBATIM",
	"FIXED",
	"LPC"
};

FLAC_API const char * const FLAC__ChannelAssignmentString[] = {
	"INDEPENDENT",
	"LEFT_SIDE",
	"RIGHT_SIDE",
	"MID_SIDE"
};

FLAC_API const char * const FLAC__FrameNumberTypeString[] = {
	"FRAME_NUMBER_TYPE_FRAME_NUMBER",
	"FRAME_NUMBER_TYPE_SAMPLE_NUMBER"
};

FLAC_API const char * const FLAC__MetadataTypeString[] = {
	"STREAMINFO",
	"PADDING",
	"APPLICATION",
	"SEEKTABLE",
	"VORBIS_COMMENT",
	"CUESHEET",
	"PICTURE"
};

FLAC_API const char * const FLAC__StreamMetadata_Picture_TypeString[] = {
	"Other",
	"32x32 pixels 'file icon' (PNG only)",
	"Other file icon",
	"Cover (front)",
	"Cover (back)",
	"Leaflet page",
	"Media (e.g. label side of CD)",
	"Lead artist/lead performer/soloist",
	"Artist/performer",
	"Conductor",
	"Band/Orchestra",
	"Composer",
	"Lyricist/text writer",
	"Recording Location",
	"During recording",
	"During performance",
	"Movie/video screen capture",
	"A bright coloured fish",
	"Illustration",
	"Band/artist logotype",
	"Publisher/Studio logotype"
};

FLAC_API FLAC__bool FLAC__format_sample_rate_is_valid(unsigned sample_rate)
{
	if(sample_rate == 0 || sample_rate > FLAC__MAX_SAMPLE_RATE) {
		return false;
	}
	else
		return true;
}

FLAC_API FLAC__bool FLAC__format_sample_rate_is_subset(unsigned sample_rate)
{
	if(
		!FLAC__format_sample_rate_is_valid(sample_rate) ||
		(
			sample_rate >= (1u << 16) &&
			!(sample_rate % 1000 == 0 || sample_rate % 10 == 0)
		)
	) {
		return false;
	}
	else
		return true;
}

FLAC_API FLAC__bool FLAC__format_seektable_is_legal(const FLAC__StreamMetadata_SeekTable *seek_table)
{
	unsigned i;
	FLAC__uint64 prev_sample_number = 0;
	FLAC__bool got_prev = false;

	FLAC__ASSERT(0 != seek_table);

	for(i = 0; i < seek_table->num_points; i++) {
		if(got_prev) {
			if(
				seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
				seek_table->points[i].sample_number <= prev_sample_number
			)
				return false;
		}
		prev_sample_number = seek_table->points[i].sample_number;
		got_prev = true;
	}

	return true;
}

static int seekpoint_compare_(const FLAC__StreamMetadata_SeekPoint *l, const FLAC__StreamMetadata_SeekPoint *r)
{
	if(l->sample_number == r->sample_number)
		return 0;
	else if(l->sample_number < r->sample_number)
		return -1;
	else
		return 1;
}

FLAC_API unsigned FLAC__format_seektable_sort(FLAC__StreamMetadata_SeekTable *seek_table)
{
	unsigned i, j;
	FLAC__bool first;

	FLAC__ASSERT(0 != seek_table);

	qsort(seek_table->points, seek_table->num_points, sizeof(FLAC__StreamMetadata_SeekPoint), (int (*)(const void *, const void *))seekpoint_compare_);

	first = true;
	for(i = j = 0; i < seek_table->num_points; i++) {
		if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
			if(!first) {
				if(seek_table->points[i].sample_number == seek_table->points[j-1].sample_number)
					continue;
			}
		}
		first = false;
		seek_table->points[j++] = seek_table->points[i];
	}

	for(i = j; i < seek_table->num_points; i++) {
		seek_table->points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
		seek_table->points[i].stream_offset = 0;
		seek_table->points[i].frame_samples = 0;
	}

	return j;
}

static FLaC__INLINE unsigned utf8len_(const FLAC__byte *utf8)
{
	FLAC__ASSERT(0 != utf8);
	if ((utf8[0] & 0x80) == 0) {
		return 1;
	}
	else if ((utf8[0] & 0xE0) == 0xC0 && (utf8[1] & 0xC0) == 0x80) {
		if ((utf8[0] & 0xFE) == 0xC0) /* overlong sequence check */
			return 0;
		return 2;
	}
	else if ((utf8[0] & 0xF0) == 0xE0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80) {
		if (utf8[0] == 0xE0 && (utf8[1] & 0xE0) == 0x80) /* overlong sequence check */
			return 0;
		if (utf8[0] == 0xED && (utf8[1] & 0xE0) == 0xA0) /* D800-DFFF */
			return 0;
		if (utf8[0] == 0xEF && utf8[1] == 0xBF && (utf8[2] & 0xFE) == 0xBE) /* FFFE-FFFF */
			return 0;
		return 3;
	}
	else if ((utf8[0] & 0xF8) == 0xF0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80) {
		if (utf8[0] == 0xF0 && (utf8[1] & 0xF0) == 0x80) /* overlong sequence check */
			return 0;
		return 4;
	}
	else if ((utf8[0] & 0xFC) == 0xF8 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80 && (utf8[4] & 0xC0) == 0x80) {
		if (utf8[0] == 0xF8 && (utf8[1] & 0xF8) == 0x80) /* overlong sequence check */
			return 0;
		return 5;
	}
	else if ((utf8[0] & 0xFE) == 0xFC && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80 && (utf8[4] & 0xC0) == 0x80 && (utf8[5] & 0xC0) == 0x80) {
		if (utf8[0] == 0xFC && (utf8[1] & 0xFC) == 0x80) /* overlong sequence check */
			return 0;
		return 6;
	}
	else {
		return 0;
	}
}

FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_name_is_legal(const char *name)
{
	char c;
	for(c = *name; c; c = *(++name))
		if(c < 0x20 || c == 0x3d || c > 0x7d)
			return false;
	return true;
}

FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_value_is_legal(const FLAC__byte *value, unsigned length)
{
	if(length == (unsigned)(-1)) {
		while(*value) {
			unsigned n = utf8len_(value);
			if(n == 0)
				return false;
			value += n;
		}
	}
	else {
		const FLAC__byte *end = value + length;
		while(value < end) {
			unsigned n = utf8len_(value);
			if(n == 0)
				return false;
			value += n;
		}
		if(value != end)
			return false;
	}
	return true;
}

FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_is_legal(const FLAC__byte *entry, unsigned length)
{
	const FLAC__byte *s, *end;

	for(s = entry, end = s + length; s < end && *s != '='; s++) {
		if(*s < 0x20 || *s > 0x7D)
			return false;
	}
	if(s == end)
		return false;

	s++; /* skip '=' */

	while(s < end) {
		unsigned n = utf8len_(s);
		if(n == 0)
			return false;
		s += n;
	}
	if(s != end)
		return false;

	return true;
}

FLAC_API FLAC__bool FLAC__format_cuesheet_is_legal(const FLAC__StreamMetadata_CueSheet *cue_sheet, FLAC__bool check_cd_da_subset, const char **violation)
{
	unsigned i, j;

	if(check_cd_da_subset) {
		if(cue_sheet->lead_in < 2 * 44100) {
			if(violation) *violation = "CD-DA cue sheet must have a lead-in length of at least 2 seconds";
			return false;
		}
		if(cue_sheet->lead_in % 588 != 0) {
			if(violation) *violation = "CD-DA cue sheet lead-in length must be evenly divisible by 588 samples";
			return false;
		}
	}

	if(cue_sheet->num_tracks == 0) {
		if(violation) *violation = "cue sheet must have at least one track (the lead-out)";
		return false;
	}

	if(check_cd_da_subset && cue_sheet->tracks[cue_sheet->num_tracks-1].number != 170) {
		if(violation) *violation = "CD-DA cue sheet must have a lead-out track number 170 (0xAA)";
		return false;
	}

	for(i = 0; i < cue_sheet->num_tracks; i++) {
		if(cue_sheet->tracks[i].number == 0) {
			if(violation) *violation = "cue sheet may not have a track number 0";
			return false;
		}

		if(check_cd_da_subset) {
			if(!((cue_sheet->tracks[i].number >= 1 && cue_sheet->tracks[i].number <= 99) || cue_sheet->tracks[i].number == 170)) {
				if(violation) *violation = "CD-DA cue sheet track number must be 1-99 or 170";
				return false;
			}
		}

		if(check_cd_da_subset && cue_sheet->tracks[i].offset % 588 != 0) {
			if(violation) {
				if(i == cue_sheet->num_tracks-1) /* the lead-out track... */
					*violation = "CD-DA cue sheet lead-out offset must be evenly divisible by 588 samples";
				else
					*violation = "CD-DA cue sheet track offset must be evenly divisible by 588 samples";
			}
			return false;
		}

		if(i < cue_sheet->num_tracks - 1) {
			if(cue_sheet->tracks[i].num_indices == 0) {
				if(violation) *violation = "cue sheet track must have at least one index point";
				return false;
			}

			if(cue_sheet->tracks[i].indices[0].number > 1) {
				if(violation) *violation = "cue sheet track's first index number must be 0 or 1";
				return false;
			}
		}

		for(j = 0; j < cue_sheet->tracks[i].num_indices; j++) {
			if(check_cd_da_subset && cue_sheet->tracks[i].indices[j].offset % 588 != 0) {
				if(violation) *violation = "CD-DA cue sheet track index offset must be evenly divisible by 588 samples";
				return false;
			}

			if(j > 0) {
				if(cue_sheet->tracks[i].indices[j].number != cue_sheet->tracks[i].indices[j-1].number + 1) {
					if(violation) *violation = "cue sheet track index numbers must increase by 1";
					return false;
				}
			}
		}
	}

	return true;
}

FLAC_API FLAC__bool FLAC__format_picture_is_legal(const FLAC__StreamMetadata_Picture *picture, const char **violation)
{
	char *p;
	FLAC__byte *b;

	for(p = picture->mime_type; *p; p++) {
		if(*p < 0x20 || *p > 0x7e) {
			if(violation) *violation = "MIME type string must contain only printable ASCII characters (0x20-0x7e)";
			return false;
		}
	}

	for(b = picture->description; *b; ) {
		unsigned n = utf8len_(b);
		if(n == 0) {
			if(violation) *violation = "description string must be valid UTF-8";
			return false;
		}
		b += n;
	}

	return true;
}

unsigned FLAC__format_get_max_rice_partition_order(unsigned blocksize, unsigned predictor_order)
{
	return
		FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(
			FLAC__format_get_max_rice_partition_order_from_blocksize(blocksize),
			blocksize,
			predictor_order
		);
}

unsigned FLAC__format_get_max_rice_partition_order_from_blocksize(unsigned blocksize)
{
	unsigned max_rice_partition_order = 0;
	while(!(blocksize & 1)) {
		max_rice_partition_order++;
		blocksize >>= 1;
	}
	return min(FLAC__MAX_RICE_PARTITION_ORDER, max_rice_partition_order);
}

unsigned FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(unsigned limit, unsigned blocksize, unsigned predictor_order)
{
	unsigned max_rice_partition_order = limit;

	while(max_rice_partition_order > 0 && (blocksize >> max_rice_partition_order) <= predictor_order)
		max_rice_partition_order--;

	FLAC__ASSERT(
		(max_rice_partition_order == 0 && blocksize >= predictor_order) ||
		(max_rice_partition_order > 0 && blocksize >> max_rice_partition_order > predictor_order)
	);

	return max_rice_partition_order;
}

void FLAC__format_entropy_coding_method_partitioned_rice_contents_init(FLAC__EntropyCodingMethod_PartitionedRiceContents *object)
{
	FLAC__ASSERT(0 != object);

	object->parameters = 0;
	object->raw_bits = 0;
	object->capacity_by_order = 0;
}

void FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(FLAC__EntropyCodingMethod_PartitionedRiceContents *object)
{
	FLAC__ASSERT(0 != object);

	if(0 != object->parameters)
		free(object->parameters);
	if(0 != object->raw_bits)
		free(object->raw_bits);
	FLAC__format_entropy_coding_method_partitioned_rice_contents_init(object);
}

FLAC__bool FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(FLAC__EntropyCodingMethod_PartitionedRiceContents *object, unsigned max_partition_order)
{
	FLAC__ASSERT(0 != object);

	FLAC__ASSERT(object->capacity_by_order > 0 || (0 == object->parameters && 0 == object->raw_bits));

	if(object->capacity_by_order < max_partition_order) {
		if(0 == (object->parameters = (unsigned*)realloc(object->parameters, sizeof(unsigned)*(1 << max_partition_order))))
			return false;
		if(0 == (object->raw_bits = (unsigned*)realloc(object->raw_bits, sizeof(unsigned)*(1 << max_partition_order))))
			return false;
		memset(object->raw_bits, 0, sizeof(unsigned)*(1 << max_partition_order));
		object->capacity_by_order = max_partition_order;
	}

	return true;
}

#endif
/*** End of inlined file: format.c ***/


/*** Start of inlined file: lpc_flac.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <math.h>


/*** Start of inlined file: lpc.h ***/
#ifndef FLAC__PRIVATE__LPC_H
#define FLAC__PRIVATE__LPC_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifndef FLAC__INTEGER_ONLY_LIBRARY

void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len);

void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
#ifndef FLAC__NO_ASM
#  ifdef FLAC__CPU_IA32
#	ifdef FLAC__HAS_NASM
void FLAC__lpc_compute_autocorrelation_asm_ia32(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
void FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
#	endif
#  endif
#endif

void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[]);

int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift);

void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
#ifndef FLAC__NO_ASM
#  ifdef FLAC__CPU_IA32
#	ifdef FLAC__HAS_NASM
void FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
void FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
#	endif
#  endif
#endif

#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */

void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
#ifndef FLAC__NO_ASM
#  ifdef FLAC__CPU_IA32
#	ifdef FLAC__HAS_NASM
void FLAC__lpc_restore_signal_asm_ia32(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
void FLAC__lpc_restore_signal_asm_ia32_mmx(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
#	endif /* FLAC__HAS_NASM */
#  elif defined FLAC__CPU_PPC
void FLAC__lpc_restore_signal_asm_ppc_altivec_16(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
void FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
#  endif/* FLAC__CPU_IA32 || FLAC__CPU_PPC */
#endif /* FLAC__NO_ASM */

#ifndef FLAC__INTEGER_ONLY_LIBRARY

FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples);
FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale);

unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order);

#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */

#endif
/*** End of inlined file: lpc.h ***/

#if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
#include <stdio.h>
#endif

#ifndef FLAC__INTEGER_ONLY_LIBRARY

#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif

#define FLAC__LPC_UNROLLED_FILTER_LOOPS

void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len)
{
	unsigned i;
	for(i = 0; i < data_len; i++)
		out[i] = in[i] * window[i];
}

void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
{
#if 0
	FLAC__real d;
	unsigned i;

	FLAC__ASSERT(lag > 0);
	FLAC__ASSERT(lag <= data_len);

	while(lag--) {
		for(i = lag, d = 0.0; i < data_len; i++)
			d += data[i] * data[i - lag];
		autoc[lag] = d;
	}
#endif

	FLAC__real d;
	unsigned sample, coeff;
	const unsigned limit = data_len - lag;

	FLAC__ASSERT(lag > 0);
	FLAC__ASSERT(lag <= data_len);

	for(coeff = 0; coeff < lag; coeff++)
		autoc[coeff] = 0.0;
	for(sample = 0; sample <= limit; sample++) {
		d = data[sample];
		for(coeff = 0; coeff < lag; coeff++)
			autoc[coeff] += d * data[sample+coeff];
	}
	for(; sample < data_len; sample++) {
		d = data[sample];
		for(coeff = 0; coeff < data_len - sample; coeff++)
			autoc[coeff] += d * data[sample+coeff];
	}
}

void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
{
	unsigned i, j;
	FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];

	FLAC__ASSERT(0 != max_order);
	FLAC__ASSERT(0 < *max_order);
	FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER);
	FLAC__ASSERT(autoc[0] != 0.0);

	err = autoc[0];

	for(i = 0; i < *max_order; i++) {
		r = -autoc[i+1];
		for(j = 0; j < i; j++)
			r -= lpc[j] * autoc[i-j];
		ref[i] = (r/=err);

		lpc[i]=r;
		for(j = 0; j < (i>>1); j++) {
			FLAC__double tmp = lpc[j];
			lpc[j] += r * lpc[i-1-j];
			lpc[i-1-j] += r * tmp;
		}
		if(i & 1)
			lpc[j] += lpc[j] * r;

		err *= (1.0 - r * r);

		for(j = 0; j <= i; j++)
			lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
		error[i] = err;

		if(err == 0.0) {
			*max_order = i+1;
			return;
		}
	}
}

int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
{
	unsigned i;
	FLAC__double cmax;
	FLAC__int32 qmax, qmin;

	FLAC__ASSERT(precision > 0);
	FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);

	precision--;
	qmax = 1 << precision;
	qmin = -qmax;
	qmax--;

	cmax = 0.0;
	for(i = 0; i < order; i++) {
		const FLAC__double d = fabs(lp_coeff[i]);
		if(d > cmax)
			cmax = d;
	}

	if(cmax <= 0.0) {
		return 2;
	}
	else {
		const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
		const int min_shiftlimit = -max_shiftlimit - 1;
		int log2cmax;

		(void)frexp(cmax, &log2cmax);
		log2cmax--;
		*shift = (int)precision - log2cmax - 1;

		if(*shift > max_shiftlimit)
			*shift = max_shiftlimit;
		else if(*shift < min_shiftlimit)
			return 1;
	}

	if(*shift >= 0) {
		FLAC__double error = 0.0;
		FLAC__int32 q;
		for(i = 0; i < order; i++) {
			error += lp_coeff[i] * (1 << *shift);
#if 1 /* unfortunately lround() is C99 */
			if(error >= 0.0)
				q = (FLAC__int32)(error + 0.5);
			else
				q = (FLAC__int32)(error - 0.5);
#else
			q = lround(error);
#endif
#ifdef FLAC__OVERFLOW_DETECT
			if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
			else if(q < qmin)
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
#endif
			if(q > qmax)
				q = qmax;
			else if(q < qmin)
				q = qmin;
			error -= q;
			qlp_coeff[i] = q;
		}
	}
	else {
		const int nshift = -(*shift);
		FLAC__double error = 0.0;
		FLAC__int32 q;
#ifdef DEBUG
		fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax);
#endif
		for(i = 0; i < order; i++) {
			error += lp_coeff[i] / (1 << nshift);
#if 1 /* unfortunately lround() is C99 */
			if(error >= 0.0)
				q = (FLAC__int32)(error + 0.5);
			else
				q = (FLAC__int32)(error - 0.5);
#else
			q = lround(error);
#endif
#ifdef FLAC__OVERFLOW_DETECT
			if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
			else if(q < qmin)
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
#endif
			if(q > qmax)
				q = qmax;
			else if(q < qmin)
				q = qmin;
			error -= q;
			qlp_coeff[i] = q;
		}
		*shift = 0;
	}

	return 0;
}

void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
{
	FLAC__int64 sumo;
	unsigned i, j;
	FLAC__int32 sum;
	const FLAC__int32 *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
	fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
	for(i=0;i<order;i++)
		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
	fprintf(stderr,"\n");
#endif
	FLAC__ASSERT(order > 0);

	for(i = 0; i < data_len; i++) {
		sumo = 0;
		sum = 0;
		history = data;
		for(j = 0; j < order; j++) {
			sum += qlp_coeff[j] * (*(--history));
			sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
#if defined _MSC_VER
			if(sumo > 2147483647I64 || sumo < -2147483648I64)
				fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
#else
			if(sumo > 2147483647ll || sumo < -2147483648ll)
				fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
#endif
		}
		*(residual++) = *(data++) - (sum >> lp_quantization);
	}

}
#else /* fully unrolled version for normal use */
{
	int i;
	FLAC__int32 sum;

	FLAC__ASSERT(order > 0);
	FLAC__ASSERT(order <= 32);

	if(order <= 12) {
		if(order > 8) {
			if(order > 10) {
				if(order == 12) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[11] * data[i-12];
						sum += qlp_coeff[10] * data[i-11];
						sum += qlp_coeff[9] * data[i-10];
						sum += qlp_coeff[8] * data[i-9];
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
				else { /* order == 11 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[10] * data[i-11];
						sum += qlp_coeff[9] * data[i-10];
						sum += qlp_coeff[8] * data[i-9];
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 10) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[9] * data[i-10];
						sum += qlp_coeff[8] * data[i-9];
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
				else { /* order == 9 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[8] * data[i-9];
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
			}
		}
		else if(order > 4) {
			if(order > 6) {
				if(order == 8) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
				else { /* order == 7 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 6) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
				else { /* order == 5 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
			}
		}
		else {
			if(order > 2) {
				if(order == 4) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
				else { /* order == 3 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 2) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						residual[i] = data[i] - (sum >> lp_quantization);
					}
				}
				else { /* order == 1 */
					for(i = 0; i < (int)data_len; i++)
						residual[i] = data[i] - ((qlp_coeff[0] * data[i-1]) >> lp_quantization);
				}
			}
		}
	}
	else { /* order > 12 */
		for(i = 0; i < (int)data_len; i++) {
			sum = 0;
			switch(order) {
				case 32: sum += qlp_coeff[31] * data[i-32];
				case 31: sum += qlp_coeff[30] * data[i-31];
				case 30: sum += qlp_coeff[29] * data[i-30];
				case 29: sum += qlp_coeff[28] * data[i-29];
				case 28: sum += qlp_coeff[27] * data[i-28];
				case 27: sum += qlp_coeff[26] * data[i-27];
				case 26: sum += qlp_coeff[25] * data[i-26];
				case 25: sum += qlp_coeff[24] * data[i-25];
				case 24: sum += qlp_coeff[23] * data[i-24];
				case 23: sum += qlp_coeff[22] * data[i-23];
				case 22: sum += qlp_coeff[21] * data[i-22];
				case 21: sum += qlp_coeff[20] * data[i-21];
				case 20: sum += qlp_coeff[19] * data[i-20];
				case 19: sum += qlp_coeff[18] * data[i-19];
				case 18: sum += qlp_coeff[17] * data[i-18];
				case 17: sum += qlp_coeff[16] * data[i-17];
				case 16: sum += qlp_coeff[15] * data[i-16];
				case 15: sum += qlp_coeff[14] * data[i-15];
				case 14: sum += qlp_coeff[13] * data[i-14];
				case 13: sum += qlp_coeff[12] * data[i-13];
					 sum += qlp_coeff[11] * data[i-12];
					 sum += qlp_coeff[10] * data[i-11];
					 sum += qlp_coeff[ 9] * data[i-10];
					 sum += qlp_coeff[ 8] * data[i- 9];
					 sum += qlp_coeff[ 7] * data[i- 8];
					 sum += qlp_coeff[ 6] * data[i- 7];
					 sum += qlp_coeff[ 5] * data[i- 6];
					 sum += qlp_coeff[ 4] * data[i- 5];
					 sum += qlp_coeff[ 3] * data[i- 4];
					 sum += qlp_coeff[ 2] * data[i- 3];
					 sum += qlp_coeff[ 1] * data[i- 2];
					 sum += qlp_coeff[ 0] * data[i- 1];
			}
			residual[i] = data[i] - (sum >> lp_quantization);
		}
	}
}
#endif

void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
{
	unsigned i, j;
	FLAC__int64 sum;
	const FLAC__int32 *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
	fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
	for(i=0;i<order;i++)
		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
	fprintf(stderr,"\n");
#endif
	FLAC__ASSERT(order > 0);

	for(i = 0; i < data_len; i++) {
		sum = 0;
		history = data;
		for(j = 0; j < order; j++)
			sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
		if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
#if defined _MSC_VER
			fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization);
#else
			fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
#endif
			break;
		}
		if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
#if defined _MSC_VER
			fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%I64d, residual=%I64d\n", i, *data, sum >> lp_quantization, (FLAC__int64)(*data) - (sum >> lp_quantization));
#else
			fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*data) - (sum >> lp_quantization)));
#endif
			break;
		}
		*(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
	}
}
#else /* fully unrolled version for normal use */
{
	int i;
	FLAC__int64 sum;

	FLAC__ASSERT(order > 0);
	FLAC__ASSERT(order <= 32);

	if(order <= 12) {
		if(order > 8) {
			if(order > 10) {
				if(order == 12) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
						sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 11 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 10) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 9 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
		}
		else if(order > 4) {
			if(order > 6) {
				if(order == 8) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 7 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 6) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 5 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
		}
		else {
			if(order > 2) {
				if(order == 4) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 3 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 2) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 1 */
					for(i = 0; i < (int)data_len; i++)
						residual[i] = data[i] - (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization);
				}
			}
		}
	}
	else { /* order > 12 */
		for(i = 0; i < (int)data_len; i++) {
			sum = 0;
			switch(order) {
				case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32];
				case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31];
				case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30];
				case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29];
				case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28];
				case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27];
				case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26];
				case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25];
				case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24];
				case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23];
				case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22];
				case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21];
				case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20];
				case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19];
				case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18];
				case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17];
				case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16];
				case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15];
				case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14];
				case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13];
					 sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
					 sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
					 sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10];
					 sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9];
					 sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8];
					 sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7];
					 sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6];
					 sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5];
					 sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4];
					 sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3];
					 sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2];
					 sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1];
			}
			residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
		}
	}
}
#endif

#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */

void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
{
	FLAC__int64 sumo;
	unsigned i, j;
	FLAC__int32 sum;
	const FLAC__int32 *r = residual, *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
	fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
	for(i=0;i<order;i++)
		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
	fprintf(stderr,"\n");
#endif
	FLAC__ASSERT(order > 0);

	for(i = 0; i < data_len; i++) {
		sumo = 0;
		sum = 0;
		history = data;
		for(j = 0; j < order; j++) {
			sum += qlp_coeff[j] * (*(--history));
			sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
#if defined _MSC_VER
			if(sumo > 2147483647I64 || sumo < -2147483648I64)
				fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
#else
			if(sumo > 2147483647ll || sumo < -2147483648ll)
				fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
#endif
		}
		*(data++) = *(r++) + (sum >> lp_quantization);
	}

}
#else /* fully unrolled version for normal use */
{
	int i;
	FLAC__int32 sum;

	FLAC__ASSERT(order > 0);
	FLAC__ASSERT(order <= 32);

	if(order <= 12) {
		if(order > 8) {
			if(order > 10) {
				if(order == 12) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[11] * data[i-12];
						sum += qlp_coeff[10] * data[i-11];
						sum += qlp_coeff[9] * data[i-10];
						sum += qlp_coeff[8] * data[i-9];
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
				else { /* order == 11 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[10] * data[i-11];
						sum += qlp_coeff[9] * data[i-10];
						sum += qlp_coeff[8] * data[i-9];
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 10) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[9] * data[i-10];
						sum += qlp_coeff[8] * data[i-9];
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
				else { /* order == 9 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[8] * data[i-9];
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
			}
		}
		else if(order > 4) {
			if(order > 6) {
				if(order == 8) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[7] * data[i-8];
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
				else { /* order == 7 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[6] * data[i-7];
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 6) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[5] * data[i-6];
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
				else { /* order == 5 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[4] * data[i-5];
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
			}
		}
		else {
			if(order > 2) {
				if(order == 4) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[3] * data[i-4];
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
				else { /* order == 3 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[2] * data[i-3];
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 2) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[1] * data[i-2];
						sum += qlp_coeff[0] * data[i-1];
						data[i] = residual[i] + (sum >> lp_quantization);
					}
				}
				else { /* order == 1 */
					for(i = 0; i < (int)data_len; i++)
						data[i] = residual[i] + ((qlp_coeff[0] * data[i-1]) >> lp_quantization);
				}
			}
		}
	}
	else { /* order > 12 */
		for(i = 0; i < (int)data_len; i++) {
			sum = 0;
			switch(order) {
				case 32: sum += qlp_coeff[31] * data[i-32];
				case 31: sum += qlp_coeff[30] * data[i-31];
				case 30: sum += qlp_coeff[29] * data[i-30];
				case 29: sum += qlp_coeff[28] * data[i-29];
				case 28: sum += qlp_coeff[27] * data[i-28];
				case 27: sum += qlp_coeff[26] * data[i-27];
				case 26: sum += qlp_coeff[25] * data[i-26];
				case 25: sum += qlp_coeff[24] * data[i-25];
				case 24: sum += qlp_coeff[23] * data[i-24];
				case 23: sum += qlp_coeff[22] * data[i-23];
				case 22: sum += qlp_coeff[21] * data[i-22];
				case 21: sum += qlp_coeff[20] * data[i-21];
				case 20: sum += qlp_coeff[19] * data[i-20];
				case 19: sum += qlp_coeff[18] * data[i-19];
				case 18: sum += qlp_coeff[17] * data[i-18];
				case 17: sum += qlp_coeff[16] * data[i-17];
				case 16: sum += qlp_coeff[15] * data[i-16];
				case 15: sum += qlp_coeff[14] * data[i-15];
				case 14: sum += qlp_coeff[13] * data[i-14];
				case 13: sum += qlp_coeff[12] * data[i-13];
					 sum += qlp_coeff[11] * data[i-12];
					 sum += qlp_coeff[10] * data[i-11];
					 sum += qlp_coeff[ 9] * data[i-10];
					 sum += qlp_coeff[ 8] * data[i- 9];
					 sum += qlp_coeff[ 7] * data[i- 8];
					 sum += qlp_coeff[ 6] * data[i- 7];
					 sum += qlp_coeff[ 5] * data[i- 6];
					 sum += qlp_coeff[ 4] * data[i- 5];
					 sum += qlp_coeff[ 3] * data[i- 4];
					 sum += qlp_coeff[ 2] * data[i- 3];
					 sum += qlp_coeff[ 1] * data[i- 2];
					 sum += qlp_coeff[ 0] * data[i- 1];
			}
			data[i] = residual[i] + (sum >> lp_quantization);
		}
	}
}
#endif

void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
{
	unsigned i, j;
	FLAC__int64 sum;
	const FLAC__int32 *r = residual, *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
	fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
	for(i=0;i<order;i++)
		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
	fprintf(stderr,"\n");
#endif
	FLAC__ASSERT(order > 0);

	for(i = 0; i < data_len; i++) {
		sum = 0;
		history = data;
		for(j = 0; j < order; j++)
			sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
		if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
#ifdef _MSC_VER
			fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization);
#else
			fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
#endif
			break;
		}
		if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
#ifdef _MSC_VER
			fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%I64d, data=%I64d\n", i, *r, sum >> lp_quantization, (FLAC__int64)(*r) + (sum >> lp_quantization));
#else
			fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *r, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*r) + (sum >> lp_quantization)));
#endif
			break;
		}
		*(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization);
	}
}
#else /* fully unrolled version for normal use */
{
	int i;
	FLAC__int64 sum;

	FLAC__ASSERT(order > 0);
	FLAC__ASSERT(order <= 32);

	if(order <= 12) {
		if(order > 8) {
			if(order > 10) {
				if(order == 12) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
						sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 11 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 10) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 9 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
		}
		else if(order > 4) {
			if(order > 6) {
				if(order == 8) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 7 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 6) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 5 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
		}
		else {
			if(order > 2) {
				if(order == 4) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 3 */
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
			}
			else {
				if(order == 2) {
					for(i = 0; i < (int)data_len; i++) {
						sum = 0;
						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
						data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
					}
				}
				else { /* order == 1 */
					for(i = 0; i < (int)data_len; i++)
						data[i] = residual[i] + (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization);
				}
			}
		}
	}
	else { /* order > 12 */
		for(i = 0; i < (int)data_len; i++) {
			sum = 0;
			switch(order) {
				case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32];
				case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31];
				case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30];
				case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29];
				case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28];
				case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27];
				case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26];
				case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25];
				case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24];
				case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23];
				case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22];
				case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21];
				case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20];
				case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19];
				case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18];
				case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17];
				case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16];
				case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15];
				case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14];
				case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13];
					 sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
					 sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
					 sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10];
					 sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9];
					 sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8];
					 sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7];
					 sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6];
					 sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5];
					 sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4];
					 sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3];
					 sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2];
					 sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1];
			}
			data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
		}
	}
}
#endif

#ifndef FLAC__INTEGER_ONLY_LIBRARY

FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples)
{
	FLAC__double error_scale;

	FLAC__ASSERT(total_samples > 0);

	error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;

	return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
}

FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale)
{
	if(lpc_error > 0.0) {
		FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2;
		if(bps >= 0.0)
			return bps;
		else
			return 0.0;
	}
	else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */
		return 1e32;
	}
	else {
		return 0.0;
	}
}

unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order)
{
	unsigned order, index, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */
	FLAC__double bits, best_bits, error_scale;

	FLAC__ASSERT(max_order > 0);
	FLAC__ASSERT(total_samples > 0);

	error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;

	best_index = 0;
	best_bits = (unsigned)(-1);

	for(index = 0, order = 1; index < max_order; index++, order++) {
		bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[index], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order);
		if(bits < best_bits) {
			best_index = index;
			best_bits = bits;
		}
	}

	return best_index+1; /* +1 since index of lpc_error[] is order-1 */
}

#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */

#endif
/*** End of inlined file: lpc_flac.c ***/


/*** Start of inlined file: md5.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdlib.h>		/* for malloc() */
#include <string.h>		/* for memcpy() */


/*** Start of inlined file: md5.h ***/
#ifndef FLAC__PRIVATE__MD5_H
#define FLAC__PRIVATE__MD5_H

typedef struct {
	FLAC__uint32 in[16];
	FLAC__uint32 buf[4];
	FLAC__uint32 bytes[2];
	FLAC__byte *internal_buf;
	size_t capacity;
} FLAC__MD5Context;

void FLAC__MD5Init(FLAC__MD5Context *context);
void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *context);

FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample);

#endif
/*** End of inlined file: md5.h ***/

#ifndef FLaC__INLINE
#define FLaC__INLINE
#endif

#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))

#define MD5STEP(f,w,x,y,z,in,s) \
	 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)

static void FLAC__MD5Transform(FLAC__uint32 buf[4], FLAC__uint32 const in[16])
{
	register FLAC__uint32 a, b, c, d;

	a = buf[0];
	b = buf[1];
	c = buf[2];
	d = buf[3];

	MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
	MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
	MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
	MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
	MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
	MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
	MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
	MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
	MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
	MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
	MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
	MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
	MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
	MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
	MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
	MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);

	MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
	MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
	MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
	MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
	MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
	MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
	MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
	MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
	MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
	MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
	MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
	MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
	MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
	MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
	MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
	MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);

	MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
	MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
	MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
	MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
	MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
	MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
	MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
	MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
	MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
	MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
	MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
	MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
	MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
	MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
	MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
	MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);

	MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
	MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
	MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
	MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
	MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
	MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
	MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
	MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
	MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
	MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
	MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
	MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
	MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
	MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
	MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);

	buf[0] += a;
	buf[1] += b;
	buf[2] += c;
	buf[3] += d;
}

#if WORDS_BIGENDIAN
//@@@@@@ OPT: use bswap/intrinsics
static void byteSwap(FLAC__uint32 *buf, unsigned words)
{
	register FLAC__uint32 x;
	do {
		x = *buf;
		x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff);
		*buf++ = (x >> 16) | (x << 16);
	} while (--words);
}
static void byteSwapX16(FLAC__uint32 *buf)
{
	register FLAC__uint32 x;

	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
	x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf   = (x >> 16) | (x << 16);
}
#else
#define byteSwap(buf, words)
#define byteSwapX16(buf)
#endif

static void FLAC__MD5Update(FLAC__MD5Context *ctx, FLAC__byte const *buf, unsigned len)
{
	FLAC__uint32 t;

	t = ctx->bytes[0];
	if ((ctx->bytes[0] = t + len) < t)
		ctx->bytes[1]++;	/* Carry from low to high */

	t = 64 - (t & 0x3f);	/* Space available in ctx->in (at least 1) */
	if (t > len) {
		memcpy((FLAC__byte *)ctx->in + 64 - t, buf, len);
		return;
	}
	memcpy((FLAC__byte *)ctx->in + 64 - t, buf, t);
	byteSwapX16(ctx->in);
	FLAC__MD5Transform(ctx->buf, ctx->in);
	buf += t;
	len -= t;

	while (len >= 64) {
		memcpy(ctx->in, buf, 64);
		byteSwapX16(ctx->in);
		FLAC__MD5Transform(ctx->buf, ctx->in);
		buf += 64;
		len -= 64;
	}

	memcpy(ctx->in, buf, len);
}

void FLAC__MD5Init(FLAC__MD5Context *ctx)
{
	ctx->buf[0] = 0x67452301;
	ctx->buf[1] = 0xefcdab89;
	ctx->buf[2] = 0x98badcfe;
	ctx->buf[3] = 0x10325476;

	ctx->bytes[0] = 0;
	ctx->bytes[1] = 0;

	ctx->internal_buf = 0;
	ctx->capacity = 0;
}

void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *ctx)
{
	int count = ctx->bytes[0] & 0x3f;	/* Number of bytes in ctx->in */
	FLAC__byte *p = (FLAC__byte *)ctx->in + count;

	*p++ = 0x80;

	count = 56 - 1 - count;

	if (count < 0) {	/* Padding forces an extra block */
		memset(p, 0, count + 8);
		byteSwapX16(ctx->in);
		FLAC__MD5Transform(ctx->buf, ctx->in);
		p = (FLAC__byte *)ctx->in;
		count = 56;
	}
	memset(p, 0, count);
	byteSwap(ctx->in, 14);

	ctx->in[14] = ctx->bytes[0] << 3;
	ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
	FLAC__MD5Transform(ctx->buf, ctx->in);

	byteSwap(ctx->buf, 4);
	memcpy(digest, ctx->buf, 16);
	memset(ctx, 0, sizeof(ctx));	/* In case it's sensitive */
	if(0 != ctx->internal_buf) {
		free(ctx->internal_buf);
		ctx->internal_buf = 0;
		ctx->capacity = 0;
	}
}

static void format_input_(FLAC__byte *buf, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
{
	unsigned channel, sample;
	register FLAC__int32 a_word;
	register FLAC__byte *buf_ = buf;

#if WORDS_BIGENDIAN
#else
	if(channels == 2 && bytes_per_sample == 2) {
		FLAC__int16 *buf1_ = ((FLAC__int16*)buf_) + 1;
		memcpy(buf_, signal[0], sizeof(FLAC__int32) * samples);
		for(sample = 0; sample < samples; sample++, buf1_+=2)
			*buf1_ = (FLAC__int16)signal[1][sample];
	}
	else if(channels == 1 && bytes_per_sample == 2) {
		FLAC__int16 *buf1_ = (FLAC__int16*)buf_;
		for(sample = 0; sample < samples; sample++)
			*buf1_++ = (FLAC__int16)signal[0][sample];
	}
	else
#endif
	if(bytes_per_sample == 2) {
		if(channels == 2) {
			for(sample = 0; sample < samples; sample++) {
				a_word = signal[0][sample];
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word;
				a_word = signal[1][sample];
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word;
			}
		}
		else if(channels == 1) {
			for(sample = 0; sample < samples; sample++) {
				a_word = signal[0][sample];
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word;
			}
		}
		else {
			for(sample = 0; sample < samples; sample++) {
				for(channel = 0; channel < channels; channel++) {
					a_word = signal[channel][sample];
					*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
					*buf_++ = (FLAC__byte)a_word;
				}
			}
		}
	}
	else if(bytes_per_sample == 3) {
		if(channels == 2) {
			for(sample = 0; sample < samples; sample++) {
				a_word = signal[0][sample];
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word;
				a_word = signal[1][sample];
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word;
			}
		}
		else if(channels == 1) {
			for(sample = 0; sample < samples; sample++) {
				a_word = signal[0][sample];
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word;
			}
		}
		else {
			for(sample = 0; sample < samples; sample++) {
				for(channel = 0; channel < channels; channel++) {
					a_word = signal[channel][sample];
					*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
					*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
					*buf_++ = (FLAC__byte)a_word;
				}
			}
		}
	}
	else if(bytes_per_sample == 1) {
		if(channels == 2) {
			for(sample = 0; sample < samples; sample++) {
				a_word = signal[0][sample];
				*buf_++ = (FLAC__byte)a_word;
				a_word = signal[1][sample];
				*buf_++ = (FLAC__byte)a_word;
			}
		}
		else if(channels == 1) {
			for(sample = 0; sample < samples; sample++) {
				a_word = signal[0][sample];
				*buf_++ = (FLAC__byte)a_word;
			}
		}
		else {
			for(sample = 0; sample < samples; sample++) {
				for(channel = 0; channel < channels; channel++) {
					a_word = signal[channel][sample];
					*buf_++ = (FLAC__byte)a_word;
				}
			}
		}
	}
	else { /* bytes_per_sample == 4, maybe optimize more later */
		for(sample = 0; sample < samples; sample++) {
			for(channel = 0; channel < channels; channel++) {
				a_word = signal[channel][sample];
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word; a_word >>= 8;
				*buf_++ = (FLAC__byte)a_word;
			}
		}
	}
}

FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
{
	const size_t bytes_needed = (size_t)channels * (size_t)samples * (size_t)bytes_per_sample;

	if((size_t)channels > SIZE_MAX / (size_t)bytes_per_sample)
		return false;
	if((size_t)channels * (size_t)bytes_per_sample > SIZE_MAX / (size_t)samples)
		return false;

	if(ctx->capacity < bytes_needed) {
		FLAC__byte *tmp = (FLAC__byte*)realloc(ctx->internal_buf, bytes_needed);
		if(0 == tmp) {
			free(ctx->internal_buf);
			if(0 == (ctx->internal_buf = (FLAC__byte*)safe_malloc_(bytes_needed)))
				return false;
		}
		ctx->internal_buf = tmp;
		ctx->capacity = bytes_needed;
	}

	format_input_(ctx->internal_buf, signal, channels, samples, bytes_per_sample);

	FLAC__MD5Update(ctx, ctx->internal_buf, bytes_needed);

	return true;
}

#endif
/*** End of inlined file: md5.c ***/


/*** Start of inlined file: memory.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif


/*** Start of inlined file: memory.h ***/
#ifndef FLAC__PRIVATE__MEMORY_H
#define FLAC__PRIVATE__MEMORY_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h> /* for size_t */

void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address);
FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer);
FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer);
FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer);
FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer);
#ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer);
#endif

#endif
/*** End of inlined file: memory.h ***/

void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
{
	void *x;

	FLAC__ASSERT(0 != aligned_address);

#ifdef FLAC__ALIGN_MALLOC_DATA
	x = safe_malloc_add_2op_(bytes, /*+*/31);
#ifdef SIZEOF_VOIDP
#if SIZEOF_VOIDP == 4
		*aligned_address = (void*)(((unsigned)x + 31) & -32);
#elif SIZEOF_VOIDP == 8
		*aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
#else
# error  Unsupported sizeof(void*)
#endif
#else
	if(sizeof(void*) == sizeof(unsigned))
		*aligned_address = (void*)(((unsigned)x + 31) & -32);
	else if(sizeof(void*) == sizeof(FLAC__uint64))
		*aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
	else
		return 0;
#endif
#else
	x = safe_malloc_(bytes);
	*aligned_address = x;
#endif
	return x;
}

FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
{
	FLAC__int32 *pu; /* unaligned pointer */
	union { /* union needed to comply with C99 pointer aliasing rules */
		FLAC__int32 *pa; /* aligned pointer */
		void	*pv; /* aligned pointer alias */
	} u;

	FLAC__ASSERT(elements > 0);
	FLAC__ASSERT(0 != unaligned_pointer);
	FLAC__ASSERT(0 != aligned_pointer);
	FLAC__ASSERT(unaligned_pointer != aligned_pointer);

	pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(*pu) * (size_t)elements, &u.pv);
	if(0 == pu) {
		return false;
	}
	else {
		if(*unaligned_pointer != 0)
			free(*unaligned_pointer);
		*unaligned_pointer = pu;
		*aligned_pointer = u.pa;
		return true;
	}
}

FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
{
	FLAC__uint32 *pu; /* unaligned pointer */
	union { /* union needed to comply with C99 pointer aliasing rules */
		FLAC__uint32 *pa; /* aligned pointer */
		void	 *pv; /* aligned pointer alias */
	} u;

	FLAC__ASSERT(elements > 0);
	FLAC__ASSERT(0 != unaligned_pointer);
	FLAC__ASSERT(0 != aligned_pointer);
	FLAC__ASSERT(unaligned_pointer != aligned_pointer);

	pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
	if(0 == pu) {
		return false;
	}
	else {
		if(*unaligned_pointer != 0)
			free(*unaligned_pointer);
		*unaligned_pointer = pu;
		*aligned_pointer = u.pa;
		return true;
	}
}

FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
{
	FLAC__uint64 *pu; /* unaligned pointer */
	union { /* union needed to comply with C99 pointer aliasing rules */
		FLAC__uint64 *pa; /* aligned pointer */
		void	 *pv; /* aligned pointer alias */
	} u;

	FLAC__ASSERT(elements > 0);
	FLAC__ASSERT(0 != unaligned_pointer);
	FLAC__ASSERT(0 != aligned_pointer);
	FLAC__ASSERT(unaligned_pointer != aligned_pointer);

	pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
	if(0 == pu) {
		return false;
	}
	else {
		if(*unaligned_pointer != 0)
			free(*unaligned_pointer);
		*unaligned_pointer = pu;
		*aligned_pointer = u.pa;
		return true;
	}
}

FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
{
	unsigned *pu; /* unaligned pointer */
	union { /* union needed to comply with C99 pointer aliasing rules */
		unsigned *pa; /* aligned pointer */
		void	 *pv; /* aligned pointer alias */
	} u;

	FLAC__ASSERT(elements > 0);
	FLAC__ASSERT(0 != unaligned_pointer);
	FLAC__ASSERT(0 != aligned_pointer);
	FLAC__ASSERT(unaligned_pointer != aligned_pointer);

	pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
	if(0 == pu) {
		return false;
	}
	else {
		if(*unaligned_pointer != 0)
			free(*unaligned_pointer);
		*unaligned_pointer = pu;
		*aligned_pointer = u.pa;
		return true;
	}
}

#ifndef FLAC__INTEGER_ONLY_LIBRARY

FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
{
	FLAC__real *pu; /* unaligned pointer */
	union { /* union needed to comply with C99 pointer aliasing rules */
		FLAC__real *pa; /* aligned pointer */
		void	   *pv; /* aligned pointer alias */
	} u;

	FLAC__ASSERT(elements > 0);
	FLAC__ASSERT(0 != unaligned_pointer);
	FLAC__ASSERT(0 != aligned_pointer);
	FLAC__ASSERT(unaligned_pointer != aligned_pointer);

	pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
	if(0 == pu) {
		return false;
	}
	else {
		if(*unaligned_pointer != 0)
			free(*unaligned_pointer);
		*unaligned_pointer = pu;
		*aligned_pointer = u.pa;
		return true;
	}
}

#endif

#endif
/*** End of inlined file: memory.c ***/


/*** Start of inlined file: stream_decoder.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#if defined _MSC_VER || defined __MINGW32__
#include <io.h> /* for _setmode() */
#include <fcntl.h> /* for _O_BINARY */
#endif
#if defined __CYGWIN__ || defined __EMX__
#include <io.h> /* for setmode(), O_BINARY */
#include <fcntl.h> /* for _O_BINARY */
#endif
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memset/memcpy() */
#include <sys/stat.h> /* for stat() */
#include <sys/types.h> /* for off_t */
#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
#if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */
#define fseeko fseek
#define ftello ftell
#endif
#endif


/*** Start of inlined file: stream_decoder.h ***/
#ifndef FLAC__PROTECTED__STREAM_DECODER_H
#define FLAC__PROTECTED__STREAM_DECODER_H

#if FLAC__HAS_OGG
#include "include/private/ogg_decoder_aspect.h"
#endif

typedef struct FLAC__StreamDecoderProtected {
	FLAC__StreamDecoderState state;
	unsigned channels;
	FLAC__ChannelAssignment channel_assignment;
	unsigned bits_per_sample;
	unsigned sample_rate; /* in Hz */
	unsigned blocksize; /* in samples (per channel) */
	FLAC__bool md5_checking; /* if true, generate MD5 signature of decoded data and compare against signature in the STREAMINFO metadata block */
#if FLAC__HAS_OGG
	FLAC__OggDecoderAspect ogg_decoder_aspect;
#endif
} FLAC__StreamDecoderProtected;

unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder);

#endif
/*** End of inlined file: stream_decoder.h ***/

#ifdef max
#undef max
#endif
#define max(a,b) ((a)>(b)?(a):(b))

#ifdef _MSC_VER
#define FLAC__U64L(x) x
#else
#define FLAC__U64L(x) x##LLU
#endif

FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC =
#if FLAC__HAS_OGG
	1
#else
	0
#endif
;

static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };

static void set_defaults_dec(FLAC__StreamDecoder *decoder);
static FILE *get_binary_stdin_(void);
static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended);
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
#if FLAC__HAS_OGG
static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
#endif
static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
#if FLAC__HAS_OGG
static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
#endif
static FLAC__StreamDecoderReadStatus file_read_callback_dec (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
static FLAC__StreamDecoderSeekStatus file_seek_callback_dec (const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
static FLAC__StreamDecoderTellStatus file_tell_callback_dec (const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);

typedef struct FLAC__StreamDecoderPrivate {
#if FLAC__HAS_OGG
	FLAC__bool is_ogg;
#endif
	FLAC__StreamDecoderReadCallback read_callback;
	FLAC__StreamDecoderSeekCallback seek_callback;
	FLAC__StreamDecoderTellCallback tell_callback;
	FLAC__StreamDecoderLengthCallback length_callback;
	FLAC__StreamDecoderEofCallback eof_callback;
	FLAC__StreamDecoderWriteCallback write_callback;
	FLAC__StreamDecoderMetadataCallback metadata_callback;
	FLAC__StreamDecoderErrorCallback error_callback;
	void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
	void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
	void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
	void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
	FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int* vals, unsigned nvals, unsigned parameter);
	void *client_data;
	FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
	FLAC__BitReader *input;
	FLAC__int32 *output[FLAC__MAX_CHANNELS];
	FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
	unsigned output_capacity, output_channels;
	FLAC__uint32 fixed_block_size, next_fixed_block_size;
	FLAC__uint64 samples_decoded;
	FLAC__bool has_stream_info, has_seek_table;
	FLAC__StreamMetadata stream_info;
	FLAC__StreamMetadata seek_table;
	FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
	FLAC__byte *metadata_filter_ids;
	size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
	FLAC__Frame frame;
	FLAC__bool cached; /* true if there is a byte in lookahead */
	FLAC__CPUInfo cpuinfo;
	FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
	FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
	FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
	FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */
	FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
	FLAC__bool is_seeking;
	FLAC__MD5Context md5context;
	FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
	FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
	FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
	FLAC__uint64 target_sample;
	unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
#if FLAC__HAS_OGG
	FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
#endif
} FLAC__StreamDecoderPrivate;

FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
	"FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
	"FLAC__STREAM_DECODER_READ_METADATA",
	"FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
	"FLAC__STREAM_DECODER_READ_FRAME",
	"FLAC__STREAM_DECODER_END_OF_STREAM",
	"FLAC__STREAM_DECODER_OGG_ERROR",
	"FLAC__STREAM_DECODER_SEEK_ERROR",
	"FLAC__STREAM_DECODER_ABORTED",
	"FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
	"FLAC__STREAM_DECODER_UNINITIALIZED"
};

FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
	"FLAC__STREAM_DECODER_INIT_STATUS_OK",
	"FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
	"FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
	"FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
	"FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
	"FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
};

FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
	"FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
	"FLAC__STREAM_DECODER_READ_STATUS_ABORT"
};

FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
	"FLAC__STREAM_DECODER_SEEK_STATUS_OK",
	"FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
	"FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
};

FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
	"FLAC__STREAM_DECODER_TELL_STATUS_OK",
	"FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
	"FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
};

FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
	"FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
	"FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
	"FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
};

FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
	"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
};

FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
	"FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
	"FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
	"FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
	"FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
};

FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
{
	FLAC__StreamDecoder *decoder;
	unsigned i;

	FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */

	decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
	if(decoder == 0) {
		return 0;
	}

	decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
	if(decoder->protected_ == 0) {
		free(decoder);
		return 0;
	}

	decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
	if(decoder->private_ == 0) {
		free(decoder->protected_);
		free(decoder);
		return 0;
	}

	decoder->private_->input = FLAC__bitreader_new();
	if(decoder->private_->input == 0) {
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}

	decoder->private_->metadata_filter_ids_capacity = 16;
	if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
		FLAC__bitreader_delete(decoder->private_->input);
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}

	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		decoder->private_->output[i] = 0;
		decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
	}

	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
	decoder->private_->has_seek_table = false;

	for(i = 0; i < FLAC__MAX_CHANNELS; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);

	decoder->private_->file = 0;

	set_defaults_dec(decoder);

	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

	return decoder;
}

FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
{
	unsigned i;

	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->private_->input);

	(void)FLAC__stream_decoder_finish(decoder);

	if(0 != decoder->private_->metadata_filter_ids)
		free(decoder->private_->metadata_filter_ids);

	FLAC__bitreader_delete(decoder->private_->input);

	for(i = 0; i < FLAC__MAX_CHANNELS; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);

	free(decoder->private_);
	free(decoder->protected_);
	free(decoder);
}

static FLAC__StreamDecoderInitStatus init_stream_internal_dec(
	FLAC__StreamDecoder *decoder,
	FLAC__StreamDecoderReadCallback read_callback,
	FLAC__StreamDecoderSeekCallback seek_callback,
	FLAC__StreamDecoderTellCallback tell_callback,
	FLAC__StreamDecoderLengthCallback length_callback,
	FLAC__StreamDecoderEofCallback eof_callback,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data,
	FLAC__bool is_ogg
)
{
	FLAC__ASSERT(0 != decoder);

	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;

#if !FLAC__HAS_OGG
	if(is_ogg)
		return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
#endif

	if(
		0 == read_callback ||
		0 == write_callback ||
		0 == error_callback ||
		(seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
	)
		return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;

#if FLAC__HAS_OGG
	decoder->private_->is_ogg = is_ogg;
	if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
		return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
#endif

	FLAC__cpu_info(&decoder->private_->cpuinfo);
	decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
	decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
	decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
	decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
	decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block;
#ifndef FLAC__NO_ASM
	if(decoder->private_->cpuinfo.use_asm) {
#ifdef FLAC__CPU_IA32
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
#ifdef FLAC__HAS_NASM
#if 1 /*@@@@@@ OPT: not clearly faster, needs more testing */
		if(decoder->private_->cpuinfo.data.ia32.bswap)
			decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap;
#endif
		if(decoder->private_->cpuinfo.data.ia32.mmx) {
			decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
			decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
			decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
		}
		else {
			decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
			decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
			decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
		}
#endif
#elif defined FLAC__CPU_PPC
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
		if(decoder->private_->cpuinfo.data.ppc.altivec) {
			decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
			decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
		}
#endif
	}
#endif

	if(!FLAC__bitreader_init(decoder->private_->input, decoder->private_->cpuinfo, read_callback_, decoder)) {
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
	}

	decoder->private_->read_callback = read_callback;
	decoder->private_->seek_callback = seek_callback;
	decoder->private_->tell_callback = tell_callback;
	decoder->private_->length_callback = length_callback;
	decoder->private_->eof_callback = eof_callback;
	decoder->private_->write_callback = write_callback;
	decoder->private_->metadata_callback = metadata_callback;
	decoder->private_->error_callback = error_callback;
	decoder->private_->client_data = client_data;
	decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
	decoder->private_->samples_decoded = 0;
	decoder->private_->has_stream_info = false;
	decoder->private_->cached = false;

	decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
	decoder->private_->is_seeking = false;

	decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
	if(!FLAC__stream_decoder_reset(decoder)) {
		return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
	}

	return FLAC__STREAM_DECODER_INIT_STATUS_OK;
}

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
	FLAC__StreamDecoder *decoder,
	FLAC__StreamDecoderReadCallback read_callback,
	FLAC__StreamDecoderSeekCallback seek_callback,
	FLAC__StreamDecoderTellCallback tell_callback,
	FLAC__StreamDecoderLengthCallback length_callback,
	FLAC__StreamDecoderEofCallback eof_callback,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
)
{
	return init_stream_internal_dec(
		decoder,
		read_callback,
		seek_callback,
		tell_callback,
		length_callback,
		eof_callback,
		write_callback,
		metadata_callback,
		error_callback,
		client_data,
false
	);
}

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
	FLAC__StreamDecoder *decoder,
	FLAC__StreamDecoderReadCallback read_callback,
	FLAC__StreamDecoderSeekCallback seek_callback,
	FLAC__StreamDecoderTellCallback tell_callback,
	FLAC__StreamDecoderLengthCallback length_callback,
	FLAC__StreamDecoderEofCallback eof_callback,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
)
{
	return init_stream_internal_dec(
		decoder,
		read_callback,
		seek_callback,
		tell_callback,
		length_callback,
		eof_callback,
		write_callback,
		metadata_callback,
		error_callback,
		client_data,
true
	);
}

static FLAC__StreamDecoderInitStatus init_FILE_internal_(
	FLAC__StreamDecoder *decoder,
	FILE *file,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data,
	FLAC__bool is_ogg
)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != file);

	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return (FLAC__StreamDecoderInitStatus) (decoder->protected_->state = (FLAC__StreamDecoderState) FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED);

	if(0 == write_callback || 0 == error_callback)
		return (FLAC__StreamDecoderInitStatus) (decoder->protected_->state = (FLAC__StreamDecoderState) FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS);

	if(file == stdin)
		file = get_binary_stdin_(); /* just to be safe */

	decoder->private_->file = file;

	return init_stream_internal_dec(
		decoder,
		file_read_callback_dec,
		decoder->private_->file == stdin? 0: file_seek_callback_dec,
		decoder->private_->file == stdin? 0: file_tell_callback_dec,
		decoder->private_->file == stdin? 0: file_length_callback_,
		file_eof_callback_,
		write_callback,
		metadata_callback,
		error_callback,
		client_data,
		is_ogg
	);
}

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
	FLAC__StreamDecoder *decoder,
	FILE *file,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
)
{
	return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
}

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
	FLAC__StreamDecoder *decoder,
	FILE *file,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
)
{
	return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
}

static FLAC__StreamDecoderInitStatus init_file_internal_(
	FLAC__StreamDecoder *decoder,
	const char *filename,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data,
	FLAC__bool is_ogg
)
{
	FILE *file;

	FLAC__ASSERT(0 != decoder);

	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return (FLAC__StreamDecoderInitStatus) (decoder->protected_->state = (FLAC__StreamDecoderState) FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED);

	if(0 == write_callback || 0 == error_callback)
		return (FLAC__StreamDecoderInitStatus) (decoder->protected_->state = (FLAC__StreamDecoderState) FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS);

	file = filename? fopen(filename, "rb") : stdin;

	if(0 == file)
		return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;

	return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
}

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
	FLAC__StreamDecoder *decoder,
	const char *filename,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
)
{
	return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
}

FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
	FLAC__StreamDecoder *decoder,
	const char *filename,
	FLAC__StreamDecoderWriteCallback write_callback,
	FLAC__StreamDecoderMetadataCallback metadata_callback,
	FLAC__StreamDecoderErrorCallback error_callback,
	void *client_data
)
{
	return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
}

FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
{
	FLAC__bool md5_failed = false;
	unsigned i;

	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);

	if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
		return true;

	FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);

	if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
		free(decoder->private_->seek_table.data.seek_table.points);
		decoder->private_->seek_table.data.seek_table.points = 0;
		decoder->private_->has_seek_table = false;
	}
	FLAC__bitreader_free(decoder->private_->input);
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		if(0 != decoder->private_->output[i]) {
			free(decoder->private_->output[i]-4);
			decoder->private_->output[i] = 0;
		}
		if(0 != decoder->private_->residual_unaligned[i]) {
			free(decoder->private_->residual_unaligned[i]);
			decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
		}
	}
	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;

#if FLAC__HAS_OGG
	if(decoder->private_->is_ogg)
		FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
#endif

	if(0 != decoder->private_->file) {
		if(decoder->private_->file != stdin)
			fclose(decoder->private_->file);
		decoder->private_->file = 0;
	}

	if(decoder->private_->do_md5_checking) {
		if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
			md5_failed = true;
	}
	decoder->private_->is_seeking = false;

	set_defaults_dec(decoder);

	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

	return !md5_failed;
}

FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
#if FLAC__HAS_OGG
	FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
	return true;
#else
	(void)value;
	return false;
#endif
}

FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	decoder->protected_->md5_checking = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
	if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
		return false;
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	decoder->private_->metadata_filter[type] = true;
	if(type == FLAC__METADATA_TYPE_APPLICATION)
		decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;

	if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
		return true;

	FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);

	if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
		decoder->private_->metadata_filter_ids_capacity *= 2;
	}

	memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
	decoder->private_->metadata_filter_ids_count++;

	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
{
	unsigned i;
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
		decoder->private_->metadata_filter[i] = true;
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
	if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
		return false;
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	decoder->private_->metadata_filter[type] = false;
	if(type == FLAC__METADATA_TYPE_APPLICATION)
		decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;

	if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
		return true;

	FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);

	if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
		decoder->private_->metadata_filter_ids_capacity *= 2;
	}

	memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
	decoder->private_->metadata_filter_ids_count++;

	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	return decoder->protected_->state;
}

FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
{
	return FLAC__StreamDecoderStateString[decoder->protected_->state];
}

FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	return decoder->protected_->md5_checking;
}

FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
}

FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	return decoder->protected_->channels;
}

FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	return decoder->protected_->channel_assignment;
}

FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	return decoder->protected_->bits_per_sample;
}

FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	return decoder->protected_->sample_rate;
}

FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	return decoder->protected_->blocksize;
}

FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != position);

#if FLAC__HAS_OGG
	if(decoder->private_->is_ogg)
		return false;
#endif
	if(0 == decoder->private_->tell_callback)
		return false;
	if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
		return false;
	if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
		return false;
	FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
	*position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);

	decoder->private_->samples_decoded = 0;
	decoder->private_->do_md5_checking = false;

#if FLAC__HAS_OGG
	if(decoder->private_->is_ogg)
		FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
#endif

	if(!FLAC__bitreader_clear(decoder->private_->input)) {
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;

	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);

	if(!FLAC__stream_decoder_flush(decoder)) {
		return false;
	}

#if FLAC__HAS_OGG
	if(decoder->private_->is_ogg)
		FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
#endif

	if(!decoder->private_->internal_reset_hack) {
		if(decoder->private_->file == stdin)
			return false; /* can't rewind stdin, reset fails */
		if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
			return false; /* seekable and seek fails, reset fails */
	}
	else
		decoder->private_->internal_reset_hack = false;

	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;

	decoder->private_->has_stream_info = false;
	if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
		free(decoder->private_->seek_table.data.seek_table.points);
		decoder->private_->seek_table.data.seek_table.points = 0;
		decoder->private_->has_seek_table = false;
	}
	decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
	decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;

	FLAC__MD5Init(&decoder->private_->md5context);

	decoder->private_->first_frame_offset = 0;
	decoder->private_->unparseable_frame_count = 0;

	return true;
}

FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
{
	FLAC__bool got_a_frame;
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);

	while(1) {
		switch(decoder->protected_->state) {
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
				if(!find_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!read_metadata_(decoder))
					return false; /* above function sets the status for us */
				else
					return true;
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
				if(!frame_sync_(decoder))
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
				if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
					return false; /* above function sets the status for us */
				if(got_a_frame)
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
			case FLAC__STREAM_DECODER_ABORTED:
				return true;
			default:
				FLAC__ASSERT(0);
				return false;
		}
	}
}

FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);

	while(1) {
		switch(decoder->protected_->state) {
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
				if(!find_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!read_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
			case FLAC__STREAM_DECODER_READ_FRAME:
			case FLAC__STREAM_DECODER_END_OF_STREAM:
			case FLAC__STREAM_DECODER_ABORTED:
				return true;
			default:
				FLAC__ASSERT(0);
				return false;
		}
	}
}

FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
{
	FLAC__bool dummy;
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);

	while(1) {
		switch(decoder->protected_->state) {
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
				if(!find_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!read_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
				if(!frame_sync_(decoder))
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
				if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
			case FLAC__STREAM_DECODER_ABORTED:
				return true;
			default:
				FLAC__ASSERT(0);
				return false;
		}
	}
}

FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
{
	FLAC__bool got_a_frame;
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);

	while(1) {
		switch(decoder->protected_->state) {
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
			case FLAC__STREAM_DECODER_READ_METADATA:
				return false; /* above function sets the status for us */
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
				if(!frame_sync_(decoder))
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
				if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
					return false; /* above function sets the status for us */
				if(got_a_frame)
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
			case FLAC__STREAM_DECODER_ABORTED:
				return true;
			default:
				FLAC__ASSERT(0);
				return false;
		}
	}
}

FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
{
	FLAC__uint64 length;

	FLAC__ASSERT(0 != decoder);

	if(
		decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
		decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
		decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
		decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
		decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
	)
		return false;

	if(0 == decoder->private_->seek_callback)
		return false;

	FLAC__ASSERT(decoder->private_->seek_callback);
	FLAC__ASSERT(decoder->private_->tell_callback);
	FLAC__ASSERT(decoder->private_->length_callback);
	FLAC__ASSERT(decoder->private_->eof_callback);

	if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
		return false;

	decoder->private_->is_seeking = true;

	decoder->private_->do_md5_checking = false;

	if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
		decoder->private_->is_seeking = false;
		return false;
	}

	if(
		decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
		decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
	) {
		if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
			decoder->private_->is_seeking = false;
			return false;
		}
		if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
			decoder->private_->is_seeking = false;
			return false;
		}
	}

	{
		const FLAC__bool ok =
#if FLAC__HAS_OGG
			decoder->private_->is_ogg?
			seek_to_absolute_sample_ogg_(decoder, length, sample) :
#endif
			seek_to_absolute_sample_(decoder, length, sample)
		;
		decoder->private_->is_seeking = false;
		return ok;
	}
}

unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
{
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
	FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
	return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
}

void set_defaults_dec(FLAC__StreamDecoder *decoder)
{
#if FLAC__HAS_OGG
	decoder->private_->is_ogg = false;
#endif
	decoder->private_->read_callback = 0;
	decoder->private_->seek_callback = 0;
	decoder->private_->tell_callback = 0;
	decoder->private_->length_callback = 0;
	decoder->private_->eof_callback = 0;
	decoder->private_->write_callback = 0;
	decoder->private_->metadata_callback = 0;
	decoder->private_->error_callback = 0;
	decoder->private_->client_data = 0;

	memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
	decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
	decoder->private_->metadata_filter_ids_count = 0;

	decoder->protected_->md5_checking = false;

#if FLAC__HAS_OGG
	FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
#endif
}

FILE *get_binary_stdin_(void)
{
#if defined _MSC_VER || defined __MINGW32__
	_setmode(_fileno(stdin), _O_BINARY);
#elif defined __CYGWIN__
	setmode(_fileno(stdin), _O_BINARY);
#elif defined __EMX__
	setmode(fileno(stdin), O_BINARY);
#endif

	return stdin;
}

FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
{
	unsigned i;
	FLAC__int32 *tmp;

	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
		return true;

	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		if(0 != decoder->private_->output[i]) {
			free(decoder->private_->output[i]-4);
			decoder->private_->output[i] = 0;
		}
		if(0 != decoder->private_->residual_unaligned[i]) {
			free(decoder->private_->residual_unaligned[i]);
			decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
		}
	}

	for(i = 0; i < channels; i++) {
		tmp = (FLAC__int32*)safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/);
		if(tmp == 0) {
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
		memset(tmp, 0, sizeof(FLAC__int32)*4);
		decoder->private_->output[i] = tmp + 4;

		if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
	}

	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;

	return true;
}

FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
{
	size_t i;

	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);

	for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
		if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
			return true;

	return false;
}

FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
{
	FLAC__uint32 x;
	unsigned i, id_;
	FLAC__bool first = true;

	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));

	for(i = id_ = 0; i < 4; ) {
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
		}
		else {
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
				return false; /* read_callback_ sets the state for us */
		}
		if(x == FLAC__STREAM_SYNC_STRING[i]) {
			first = true;
			i++;
			id_ = 0;
			continue;
		}
		if(x == ID3V2_TAG_[id_]) {
			id_++;
			i = 0;
			if(id_ == 3) {
				if(!skip_id3v2_tag_(decoder))
					return false; /* skip_id3v2_tag_ sets the state for us */
			}
			continue;
		}
		id_ = 0;
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
			decoder->private_->header_warmup[0] = (FLAC__byte)x;
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
				return false; /* read_callback_ sets the state for us */

			if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
				return true;
			}
		}
		i = 0;
		if(first) {
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
			first = false;
		}
	}

	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
	return true;
}

FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
{
	FLAC__bool is_last;
	FLAC__uint32 i, x, type, length;

	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
		return false; /* read_callback_ sets the state for us */
	is_last = x? true : false;

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
		return false; /* read_callback_ sets the state for us */

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
		return false; /* read_callback_ sets the state for us */

	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
		if(!read_metadata_streaminfo_(decoder, is_last, length))
			return false;

		decoder->private_->has_stream_info = true;
		if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
			decoder->private_->do_md5_checking = false;
		if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
			decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
	}
	else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
		if(!read_metadata_seektable_(decoder, is_last, length))
			return false;

		decoder->private_->has_seek_table = true;
		if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
			decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
	}
	else {
		FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
		unsigned real_length = length;
		FLAC__StreamMetadata block;

		block.is_last = is_last;
		block.type = (FLAC__MetadataType)type;
		block.length = length;

		if(type == FLAC__METADATA_TYPE_APPLICATION) {
			if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
				return false; /* read_callback_ sets the state for us */

			if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */
				decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/
				return false;
			}

			real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;

			if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
				skip_it = !skip_it;
		}

		if(skip_it) {
			if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
				return false; /* read_callback_ sets the state for us */
		}
		else {
			switch(type) {
				case FLAC__METADATA_TYPE_PADDING:
					if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
						return false; /* read_callback_ sets the state for us */
					break;
				case FLAC__METADATA_TYPE_APPLICATION:
					if(real_length > 0) {
						if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
							decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
							return false;
						}
						if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
							return false; /* read_callback_ sets the state for us */
					}
					else
						block.data.application.data = 0;
					break;
				case FLAC__METADATA_TYPE_VORBIS_COMMENT:
					if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
						return false;
					break;
				case FLAC__METADATA_TYPE_CUESHEET:
					if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
						return false;
					break;
				case FLAC__METADATA_TYPE_PICTURE:
					if(!read_metadata_picture_(decoder, &block.data.picture))
						return false;
					break;
				case FLAC__METADATA_TYPE_STREAMINFO:
				case FLAC__METADATA_TYPE_SEEKTABLE:
					FLAC__ASSERT(0);
					break;
				default:
					if(real_length > 0) {
						if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
							decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
							return false;
						}
						if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
							return false; /* read_callback_ sets the state for us */
					}
					else
						block.data.unknown.data = 0;
					break;
			}
			if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
				decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);

			switch(type) {
				case FLAC__METADATA_TYPE_PADDING:
					break;
				case FLAC__METADATA_TYPE_APPLICATION:
					if(0 != block.data.application.data)
						free(block.data.application.data);
					break;
				case FLAC__METADATA_TYPE_VORBIS_COMMENT:
					if(0 != block.data.vorbis_comment.vendor_string.entry)
						free(block.data.vorbis_comment.vendor_string.entry);
					if(block.data.vorbis_comment.num_comments > 0)
						for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
							if(0 != block.data.vorbis_comment.comments[i].entry)
								free(block.data.vorbis_comment.comments[i].entry);
					if(0 != block.data.vorbis_comment.comments)
						free(block.data.vorbis_comment.comments);
					break;
				case FLAC__METADATA_TYPE_CUESHEET:
					if(block.data.cue_sheet.num_tracks > 0)
						for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
							if(0 != block.data.cue_sheet.tracks[i].indices)
								free(block.data.cue_sheet.tracks[i].indices);
					if(0 != block.data.cue_sheet.tracks)
						free(block.data.cue_sheet.tracks);
					break;
				case FLAC__METADATA_TYPE_PICTURE:
					if(0 != block.data.picture.mime_type)
						free(block.data.picture.mime_type);
					if(0 != block.data.picture.description)
						free(block.data.picture.description);
					if(0 != block.data.picture.data)
						free(block.data.picture.data);
					break;
				case FLAC__METADATA_TYPE_STREAMINFO:
				case FLAC__METADATA_TYPE_SEEKTABLE:
					FLAC__ASSERT(0);
				default:
					if(0 != block.data.unknown.data)
						free(block.data.unknown.data);
					break;
			}
		}
	}

	if(is_last) {
		if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
			decoder->private_->first_frame_offset = 0;
		decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
	}

	return true;
}

FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
{
	FLAC__uint32 x;
	unsigned bits, used_bits = 0;

	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));

	decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
	decoder->private_->stream_info.is_last = is_last;
	decoder->private_->stream_info.length = length;

	bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
		return false; /* read_callback_ sets the state for us */
	decoder->private_->stream_info.data.stream_info.min_blocksize = x;
	used_bits += bits;

	bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
		return false; /* read_callback_ sets the state for us */
	decoder->private_->stream_info.data.stream_info.max_blocksize = x;
	used_bits += bits;

	bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
		return false; /* read_callback_ sets the state for us */
	decoder->private_->stream_info.data.stream_info.min_framesize = x;
	used_bits += bits;

	bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
		return false; /* read_callback_ sets the state for us */
	decoder->private_->stream_info.data.stream_info.max_framesize = x;
	used_bits += bits;

	bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
		return false; /* read_callback_ sets the state for us */
	decoder->private_->stream_info.data.stream_info.sample_rate = x;
	used_bits += bits;

	bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
		return false; /* read_callback_ sets the state for us */
	decoder->private_->stream_info.data.stream_info.channels = x+1;
	used_bits += bits;

	bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
		return false; /* read_callback_ sets the state for us */
	decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
	used_bits += bits;

	bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
	if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
		return false; /* read_callback_ sets the state for us */
	used_bits += bits;

	if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
		return false; /* read_callback_ sets the state for us */
	used_bits += 16*8;

	FLAC__ASSERT(used_bits % 8 == 0);
	length -= (used_bits / 8);
	if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
		return false; /* read_callback_ sets the state for us */

	return true;
}

FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
{
	FLAC__uint32 i, x;
	FLAC__uint64 xx;

	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));

	decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
	decoder->private_->seek_table.is_last = is_last;
	decoder->private_->seek_table.length = length;

	decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;

	if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) {
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}
	for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
		if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
			return false; /* read_callback_ sets the state for us */
		decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;

		if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
			return false; /* read_callback_ sets the state for us */
		decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;

		if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
			return false; /* read_callback_ sets the state for us */
		decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
	}
	length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
	if(length > 0) {
		if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
			return false; /* read_callback_ sets the state for us */
	}

	return true;
}

FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
{
	FLAC__uint32 i;

	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));

	FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
	if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
		return false; /* read_callback_ sets the state for us */
	if(obj->vendor_string.length > 0) {
		if(0 == (obj->vendor_string.entry = (FLAC__byte*)safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) {
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
		if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
			return false; /* read_callback_ sets the state for us */
		obj->vendor_string.entry[obj->vendor_string.length] = '\0';
	}
	else
		obj->vendor_string.entry = 0;

	FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
	if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
		return false; /* read_callback_ sets the state for us */

	if(obj->num_comments > 0) {
		if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)safe_malloc_mul_2op_(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
		for(i = 0; i < obj->num_comments; i++) {
			FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
			if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
				return false; /* read_callback_ sets the state for us */
			if(obj->comments[i].length > 0) {
				if(0 == (obj->comments[i].entry = (FLAC__byte*)safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) {
					decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
					return false;
				}
				if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
					return false; /* read_callback_ sets the state for us */
				obj->comments[i].entry[obj->comments[i].length] = '\0';
			}
			else
				obj->comments[i].entry = 0;
		}
	}
	else {
		obj->comments = 0;
	}

	return true;
}

FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
{
	FLAC__uint32 i, j, x;

	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));

	memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));

	FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
	if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
		return false; /* read_callback_ sets the state for us */

	if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
		return false; /* read_callback_ sets the state for us */

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
		return false; /* read_callback_ sets the state for us */
	obj->is_cd = x? true : false;

	if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
		return false; /* read_callback_ sets the state for us */

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
		return false; /* read_callback_ sets the state for us */
	obj->num_tracks = x;

	if(obj->num_tracks > 0) {
		if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
		for(i = 0; i < obj->num_tracks; i++) {
			FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
			if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
				return false; /* read_callback_ sets the state for us */

			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
				return false; /* read_callback_ sets the state for us */
			track->number = (FLAC__byte)x;

			FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
			if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
				return false; /* read_callback_ sets the state for us */

			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
				return false; /* read_callback_ sets the state for us */
			track->type = x;

			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
				return false; /* read_callback_ sets the state for us */
			track->pre_emphasis = x;

			if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
				return false; /* read_callback_ sets the state for us */

			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
				return false; /* read_callback_ sets the state for us */
			track->num_indices = (FLAC__byte)x;

			if(track->num_indices > 0) {
				if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
					decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
					return false;
				}
				for(j = 0; j < track->num_indices; j++) {
					FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
					if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
						return false; /* read_callback_ sets the state for us */

					if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
						return false; /* read_callback_ sets the state for us */
					index->number = (FLAC__byte)x;

					if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
						return false; /* read_callback_ sets the state for us */
				}
			}
		}
	}

	return true;
}

FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
{
	FLAC__uint32 x;

	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
		return false; /* read_callback_ sets the state for us */
	obj->type = (FLAC__StreamMetadata_Picture_Type) x;

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
		return false; /* read_callback_ sets the state for us */
	if(0 == (obj->mime_type = (char*)safe_malloc_add_2op_(x, /*+*/1))) {
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}
	if(x > 0) {
		if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x))
			return false; /* read_callback_ sets the state for us */
	}
	obj->mime_type[x] = '\0';

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
		return false; /* read_callback_ sets the state for us */
	if(0 == (obj->description = (FLAC__byte*)safe_malloc_add_2op_(x, /*+*/1))) {
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}
	if(x > 0) {
		if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x))
			return false; /* read_callback_ sets the state for us */
	}
	obj->description[x] = '\0';

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
		return false; /* read_callback_ sets the state for us */

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
		return false; /* read_callback_ sets the state for us */

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
		return false; /* read_callback_ sets the state for us */

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
		return false; /* read_callback_ sets the state for us */

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
		return false; /* read_callback_ sets the state for us */
	if(0 == (obj->data = (FLAC__byte*)safe_malloc_(obj->data_length))) {
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}
	if(obj->data_length > 0) {
		if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
			return false; /* read_callback_ sets the state for us */
	}

	return true;
}

FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
{
	FLAC__uint32 x;
	unsigned i, skip;

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
		return false; /* read_callback_ sets the state for us */
	skip = 0;
	for(i = 0; i < 4; i++) {
		if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
			return false; /* read_callback_ sets the state for us */
		skip <<= 7;
		skip |= (x & 0x7f);
	}
	if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
		return false; /* read_callback_ sets the state for us */
	return true;
}

FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
{
	FLAC__uint32 x;
	FLAC__bool first = true;

	if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
		if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
			decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
			return true;
		}
	}

	if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
		if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
			return false; /* read_callback_ sets the state for us */
	}

	while(1) {
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
		}
		else {
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
				return false; /* read_callback_ sets the state for us */
		}
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
			decoder->private_->header_warmup[0] = (FLAC__byte)x;
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
				return false; /* read_callback_ sets the state for us */

			if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
				return true;
			}
		}
		if(first) {
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
			first = false;
		}
	}

	return true;
}

FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
{
	unsigned channel;
	unsigned i;
	FLAC__int32 mid, side;
	unsigned frame_crc; /* the one we calculate from the input stream */
	FLAC__uint32 x;

	*got_a_frame = false;

	frame_crc = 0;
	frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
	frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
	FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);

	if(!read_frame_header_(decoder))
		return false;
	if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
		return true;
	if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
		return false;
	for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
		unsigned bps = decoder->private_->frame.header.bits_per_sample;
		switch(decoder->private_->frame.header.channel_assignment) {
			case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
				break;
			case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
				FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
				if(channel == 1)
					bps++;
				break;
			case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
				FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
				if(channel == 0)
					bps++;
				break;
			case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
				FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
				if(channel == 1)
					bps++;
				break;
			default:
				FLAC__ASSERT(0);
		}
		if(!read_subframe_(decoder, channel, bps, do_full_decode))
			return false;
		if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
			return true;
	}
	if(!read_zero_padding_(decoder))
		return false;
	if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption (i.e. "zero bits" were not all zeroes) */
		return true;

	frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
		return false; /* read_callback_ sets the state for us */
	if(frame_crc == x) {
		if(do_full_decode) {
			switch(decoder->private_->frame.header.channel_assignment) {
				case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
					break;
				case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
					FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
					for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
						decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
					break;
				case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
					FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
					for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
						decoder->private_->output[0][i] += decoder->private_->output[1][i];
					break;
				case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
					FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
					for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
#if 1
						mid = decoder->private_->output[0][i];
						side = decoder->private_->output[1][i];
						mid <<= 1;
						mid |= (side & 1); /* i.e. if 'side' is odd... */
						decoder->private_->output[0][i] = (mid + side) >> 1;
						decoder->private_->output[1][i] = (mid - side) >> 1;
#else
						mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */
						decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1;
						decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1;
#endif
					}
					break;
				default:
					FLAC__ASSERT(0);
					break;
			}
		}
	}
	else {
		send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
		if(do_full_decode) {
			for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
				memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
			}
		}
	}

	*got_a_frame = true;

	if(decoder->private_->next_fixed_block_size)
		decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size;

	decoder->protected_->channels = decoder->private_->frame.header.channels;
	decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
	decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
	decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
	decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;

	FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
	decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;

	if(do_full_decode) {
		if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
			return false;
	}

	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
	return true;
}

FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
{
	FLAC__uint32 x;
	FLAC__uint64 xx;
	unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
	FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
	unsigned raw_header_len;
	FLAC__bool is_unparseable = false;

	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));

	raw_header[0] = decoder->private_->header_warmup[0];
	raw_header[1] = decoder->private_->header_warmup[1];
	raw_header_len = 2;

	if(raw_header[1] & 0x02) /* MAGIC NUMBER */
		is_unparseable = true;

	for(i = 0; i < 2; i++) {
		if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
			return false; /* read_callback_ sets the state for us */
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
			decoder->private_->lookahead = (FLAC__byte)x;
			decoder->private_->cached = true;
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
		}
		raw_header[raw_header_len++] = (FLAC__byte)x;
	}

	switch(x = raw_header[2] >> 4) {
		case 0:
			is_unparseable = true;
			break;
		case 1:
			decoder->private_->frame.header.blocksize = 192;
			break;
		case 2:
		case 3:
		case 4:
		case 5:
			decoder->private_->frame.header.blocksize = 576 << (x-2);
			break;
		case 6:
		case 7:
			blocksize_hint = x;
			break;
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
			decoder->private_->frame.header.blocksize = 256 << (x-8);
			break;
		default:
			FLAC__ASSERT(0);
			break;
	}

	switch(x = raw_header[2] & 0x0f) {
		case 0:
			if(decoder->private_->has_stream_info)
				decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
			else
				is_unparseable = true;
			break;
		case 1:
			decoder->private_->frame.header.sample_rate = 88200;
			break;
		case 2:
			decoder->private_->frame.header.sample_rate = 176400;
			break;
		case 3:
			decoder->private_->frame.header.sample_rate = 192000;
			break;
		case 4:
			decoder->private_->frame.header.sample_rate = 8000;
			break;
		case 5:
			decoder->private_->frame.header.sample_rate = 16000;
			break;
		case 6:
			decoder->private_->frame.header.sample_rate = 22050;
			break;
		case 7:
			decoder->private_->frame.header.sample_rate = 24000;
			break;
		case 8:
			decoder->private_->frame.header.sample_rate = 32000;
			break;
		case 9:
			decoder->private_->frame.header.sample_rate = 44100;
			break;
		case 10:
			decoder->private_->frame.header.sample_rate = 48000;
			break;
		case 11:
			decoder->private_->frame.header.sample_rate = 96000;
			break;
		case 12:
		case 13:
		case 14:
			sample_rate_hint = x;
			break;
		case 15:
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
		default:
			FLAC__ASSERT(0);
	}

	x = (unsigned)(raw_header[3] >> 4);
	if(x & 8) {
		decoder->private_->frame.header.channels = 2;
		switch(x & 7) {
			case 0:
				decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
				break;
			case 1:
				decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
				break;
			case 2:
				decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
				break;
			default:
				is_unparseable = true;
				break;
		}
	}
	else {
		decoder->private_->frame.header.channels = (unsigned)x + 1;
		decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
	}

	switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
		case 0:
			if(decoder->private_->has_stream_info)
				decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
			else
				is_unparseable = true;
			break;
		case 1:
			decoder->private_->frame.header.bits_per_sample = 8;
			break;
		case 2:
			decoder->private_->frame.header.bits_per_sample = 12;
			break;
		case 4:
			decoder->private_->frame.header.bits_per_sample = 16;
			break;
		case 5:
			decoder->private_->frame.header.bits_per_sample = 20;
			break;
		case 6:
			decoder->private_->frame.header.bits_per_sample = 24;
			break;
		case 3:
		case 7:
			is_unparseable = true;
			break;
		default:
			FLAC__ASSERT(0);
			break;
	}

	if(raw_header[3] & 0x01) /* MAGIC NUMBER */
		is_unparseable = true;

	if(
		raw_header[1] & 0x01 ||
		(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize)
	) { /* variable blocksize */
		if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
			return false; /* read_callback_ sets the state for us */
		if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
			decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
			decoder->private_->cached = true;
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
		}
		decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
		decoder->private_->frame.header.number.sample_number = xx;
	}
	else { /* fixed blocksize */
		if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
			return false; /* read_callback_ sets the state for us */
		if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
			decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
			decoder->private_->cached = true;
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
		}
		decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
		decoder->private_->frame.header.number.frame_number = x;
	}

	if(blocksize_hint) {
		if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
			return false; /* read_callback_ sets the state for us */
		raw_header[raw_header_len++] = (FLAC__byte)x;
		if(blocksize_hint == 7) {
			FLAC__uint32 _x;
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
				return false; /* read_callback_ sets the state for us */
			raw_header[raw_header_len++] = (FLAC__byte)_x;
			x = (x << 8) | _x;
		}
		decoder->private_->frame.header.blocksize = x+1;
	}

	if(sample_rate_hint) {
		if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
			return false; /* read_callback_ sets the state for us */
		raw_header[raw_header_len++] = (FLAC__byte)x;
		if(sample_rate_hint != 12) {
			FLAC__uint32 _x;
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
				return false; /* read_callback_ sets the state for us */
			raw_header[raw_header_len++] = (FLAC__byte)_x;
			x = (x << 8) | _x;
		}
		if(sample_rate_hint == 12)
			decoder->private_->frame.header.sample_rate = x*1000;
		else if(sample_rate_hint == 13)
			decoder->private_->frame.header.sample_rate = x;
		else
			decoder->private_->frame.header.sample_rate = x*10;
	}

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
		return false; /* read_callback_ sets the state for us */
	crc8 = (FLAC__byte)x;

	if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
		send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
		decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}

	decoder->private_->next_fixed_block_size = 0;
	if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
		x = decoder->private_->frame.header.number.frame_number;
		decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
		if(decoder->private_->fixed_block_size)
			decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x;
		else if(decoder->private_->has_stream_info) {
			if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) {
				decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
				decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize;
			}
			else
				is_unparseable = true;
		}
		else if(x == 0) {
			decoder->private_->frame.header.number.sample_number = 0;
			decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize;
		}
		else {
			decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
		}
	}

	if(is_unparseable) {
		send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
		decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}

	return true;
}

FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
{
	FLAC__uint32 x;
	FLAC__bool wasted_bits;
	unsigned i;

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
		return false; /* read_callback_ sets the state for us */

	wasted_bits = (x & 1);
	x &= 0xfe;

	if(wasted_bits) {
		unsigned u;
		if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
			return false; /* read_callback_ sets the state for us */
		decoder->private_->frame.subframes[channel].wasted_bits = u+1;
		bps -= decoder->private_->frame.subframes[channel].wasted_bits;
	}
	else
		decoder->private_->frame.subframes[channel].wasted_bits = 0;

	if(x & 0x80) {
		send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
		decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}
	else if(x == 0) {
		if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
			return false;
	}
	else if(x == 2) {
		if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
			return false;
	}
	else if(x < 16) {
		send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
		decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}
	else if(x <= 24) {
		if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
			return false;
		if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
			return true;
	}
	else if(x < 64) {
		send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
		decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}
	else {
		if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
			return false;
		if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
			return true;
	}

	if(wasted_bits && do_full_decode) {
		x = decoder->private_->frame.subframes[channel].wasted_bits;
		for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
			decoder->private_->output[channel][i] <<= x;
	}

	return true;
}

FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
{
	FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
	FLAC__int32 x;
	unsigned i;
	FLAC__int32 *output = decoder->private_->output[channel];

	decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;

	if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
		return false; /* read_callback_ sets the state for us */

	subframe->value = x;

	if(do_full_decode) {
		for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
			output[i] = x;
	}

	return true;
}

FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
{
	FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
	FLAC__int32 i32;
	FLAC__uint32 u32;
	unsigned u;

	decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;

	subframe->residual = decoder->private_->residual[channel];
	subframe->order = order;

	for(u = 0; u < order; u++) {
		if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
			return false; /* read_callback_ sets the state for us */
		subframe->warmup[u] = i32;
	}

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
		return false; /* read_callback_ sets the state for us */
	subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
	switch(subframe->entropy_coding_method.type) {
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
				return false; /* read_callback_ sets the state for us */
			subframe->entropy_coding_method.data.partitioned_rice.order = u32;
			subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
			break;
		default:
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
	}

	switch(subframe->entropy_coding_method.type) {
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
			if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2))
				return false;
			break;
		default:
			FLAC__ASSERT(0);
	}

	if(do_full_decode) {
		memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
		FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
	}

	return true;
}

FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
{
	FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
	FLAC__int32 i32;
	FLAC__uint32 u32;
	unsigned u;

	decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;

	subframe->residual = decoder->private_->residual[channel];
	subframe->order = order;

	for(u = 0; u < order; u++) {
		if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
			return false; /* read_callback_ sets the state for us */
		subframe->warmup[u] = i32;
	}

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
		return false; /* read_callback_ sets the state for us */
	if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
		send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
		decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}
	subframe->qlp_coeff_precision = u32+1;

	if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
		return false; /* read_callback_ sets the state for us */
	subframe->quantization_level = i32;

	for(u = 0; u < order; u++) {
		if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
			return false; /* read_callback_ sets the state for us */
		subframe->qlp_coeff[u] = i32;
	}

	if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
		return false; /* read_callback_ sets the state for us */
	subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
	switch(subframe->entropy_coding_method.type) {
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
				return false; /* read_callback_ sets the state for us */
			subframe->entropy_coding_method.data.partitioned_rice.order = u32;
			subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
			break;
		default:
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
	}

	switch(subframe->entropy_coding_method.type) {
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
			if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2))
				return false;
			break;
		default:
			FLAC__ASSERT(0);
	}

	if(do_full_decode) {
		memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
		if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
			if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
				if(order <= 8)
					decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
				else
					decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
			}
			else
				decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
		else
			decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
	}

	return true;
}

FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
{
	FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
	FLAC__int32 x, *residual = decoder->private_->residual[channel];
	unsigned i;

	decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;

	subframe->data = residual;

	for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
		if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
			return false; /* read_callback_ sets the state for us */
		residual[i] = x;
	}

	if(do_full_decode)
		memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);

	return true;
}

FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended)
{
	FLAC__uint32 rice_parameter;
	int i;
	unsigned partition, sample, u;
	const unsigned partitions = 1u << partition_order;
	const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
	const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
	const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;

	if(partition_order == 0) {
		if(decoder->private_->frame.header.blocksize < predictor_order) {
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
		}
	}
	else {
		if(partition_samples < predictor_order) {
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
		}
	}

	if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}

	sample = 0;
	for(partition = 0; partition < partitions; partition++) {
		if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen))
			return false; /* read_callback_ sets the state for us */
		partitioned_rice_contents->parameters[partition] = rice_parameter;
		if(rice_parameter < pesc) {
			partitioned_rice_contents->raw_bits[partition] = 0;
			u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
			if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, (int*) residual + sample, u, rice_parameter))
				return false; /* read_callback_ sets the state for us */
			sample += u;
		}
		else {
			if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
				return false; /* read_callback_ sets the state for us */
			partitioned_rice_contents->raw_bits[partition] = rice_parameter;
			for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
				if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, (FLAC__int32*) &i, rice_parameter))
					return false; /* read_callback_ sets the state for us */
				residual[sample] = i;
			}
		}
	}

	return true;
}

FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
{
	if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
		FLAC__uint32 zero = 0;
		if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
			return false; /* read_callback_ sets the state for us */
		if(zero != 0) {
			send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		}
	}
	return true;
}

FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
{
	FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;

	if(
#if FLAC__HAS_OGG
		!decoder->private_->is_ogg &&
#endif
		decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
	) {
		*bytes = 0;
		decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
		return false;
	}
	else if(*bytes > 0) {
		if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
			decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
			return false;
		}
		else {
			const FLAC__StreamDecoderReadStatus status =
#if FLAC__HAS_OGG
				decoder->private_->is_ogg?
				read_callback_ogg_aspect_(decoder, buffer, bytes) :
#endif
				decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
			;
			if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
				decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
				return false;
			}
			else if(*bytes == 0) {
				if(
					status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
					(
#if FLAC__HAS_OGG
						!decoder->private_->is_ogg &&
#endif
						decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
					)
				) {
					decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
					return false;
				}
				else
					return true;
			}
			else
				return true;
		}
	}
	else {
		decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
		return false;
	}
}

#if FLAC__HAS_OGG
FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
{
	switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
		case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
			return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
		case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
			return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
		case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
			return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
		case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
		case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
		case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
		case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
		case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
			return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
		default:
			FLAC__ASSERT(0);
			return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
	}
}

FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
	FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;

	switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
		case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
			return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
		case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
			return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
		case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
			return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
		default:
			FLAC__ASSERT(0);
			return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
	}
}
#endif

FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
{
	if(decoder->private_->is_seeking) {
		FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
		FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
		FLAC__uint64 target_sample = decoder->private_->target_sample;

		FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);

#if FLAC__HAS_OGG
		decoder->private_->got_a_frame = true;
#endif
		decoder->private_->last_frame = *frame; /* save the frame */
		if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
			unsigned delta = (unsigned)(target_sample - this_frame_sample);
			decoder->private_->is_seeking = false;
			if(delta > 0) {
				unsigned channel;
				const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
				for(channel = 0; channel < frame->header.channels; channel++)
					newbuffer[channel] = buffer[channel] + delta;
				decoder->private_->last_frame.header.blocksize -= delta;
				decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
				return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
			}
			else {
				return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
			}
		}

		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
	}

	if(!decoder->private_->has_stream_info)
		decoder->private_->do_md5_checking = false;
	if(decoder->private_->do_md5_checking) {
		if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
	}

	return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
}

void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
{
	if(!decoder->private_->is_seeking)
		decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
	else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
		decoder->private_->unparseable_frame_count++;
}

FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
{
	FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
	FLAC__int64 pos = -1;
	int i;
	unsigned approx_bytes_per_frame;
	FLAC__bool first_seek = true;
	const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
	const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
	const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
	const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
	const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
	unsigned channels = FLAC__stream_decoder_get_channels(decoder);
	unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
	const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;

	if(channels == 0)
		channels = decoder->private_->stream_info.data.stream_info.channels;
	if(bps == 0)
		bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;

	if(max_framesize > 0)
		approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
	else if(min_blocksize == max_blocksize && min_blocksize > 0) {
		approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
	}
	else
		approx_bytes_per_frame = 4096 * channels * bps/8 + 64;

	lower_bound = first_frame_offset;
	lower_bound_sample = 0;
	upper_bound = stream_length;
	upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/;

	if(seek_table) {
		FLAC__uint64 new_lower_bound = lower_bound;
		FLAC__uint64 new_upper_bound = upper_bound;
		FLAC__uint64 new_lower_bound_sample = lower_bound_sample;
		FLAC__uint64 new_upper_bound_sample = upper_bound_sample;

		for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
			if(
				seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
				seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
				(total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
				seek_table->points[i].sample_number <= target_sample
			)
				break;
		}
		if(i >= 0) { /* i.e. we found a suitable seek point... */
			new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
			new_lower_bound_sample = seek_table->points[i].sample_number;
		}

		for(i = 0; i < (int)seek_table->num_points; i++) {
			if(
				seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
				seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
				(total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
				seek_table->points[i].sample_number > target_sample
			)
				break;
		}
		if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
			new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
			new_upper_bound_sample = seek_table->points[i].sample_number;
		}
		if(new_upper_bound >= new_lower_bound) {
			lower_bound = new_lower_bound;
			upper_bound = new_upper_bound;
			lower_bound_sample = new_lower_bound_sample;
			upper_bound_sample = new_upper_bound_sample;
		}
	}

	FLAC__ASSERT(upper_bound_sample >= lower_bound_sample);
	if(upper_bound_sample == lower_bound_sample)
		upper_bound_sample++;

	decoder->private_->target_sample = target_sample;
	while(1) {
		if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
			decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
			return false;
		}
#ifndef FLAC__INTEGER_ONLY_LIBRARY
#if defined _MSC_VER || defined __MINGW32__
		pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(FLAC__int64)(target_sample - lower_bound_sample) / (FLAC__double)(FLAC__int64)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(FLAC__int64)(upper_bound - lower_bound)) - approx_bytes_per_frame;
#else
		pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(target_sample - lower_bound_sample) / (FLAC__double)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(upper_bound - lower_bound)) - approx_bytes_per_frame;
#endif
#else
		if(upper_bound - lower_bound < 0xffffffff)
			pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame;
		else /* @@@ WATCHOUT, ~2TB limit */
			pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8)) / ((upper_bound_sample - lower_bound_sample)>>16)) - approx_bytes_per_frame;
#endif
		if(pos >= (FLAC__int64)upper_bound)
			pos = (FLAC__int64)upper_bound - 1;
		if(pos < (FLAC__int64)lower_bound)
			pos = (FLAC__int64)lower_bound;
		if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
			decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
			return false;
		}
		if(!FLAC__stream_decoder_flush(decoder)) {
			return false;
		}
		decoder->private_->unparseable_frame_count = 0;
		if(!FLAC__stream_decoder_process_single(decoder)) {
			decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
			return false;
		}
#if 0
		if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
			break;
#endif
		if(!decoder->private_->is_seeking)
			break;

		FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
		this_frame_sample = decoder->private_->last_frame.header.number.sample_number;

		if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
			if (pos == (FLAC__int64)lower_bound) {
				decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
				return false;
			}
			approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16;
			continue;
		}
		first_seek = false;

		if (this_frame_sample < lower_bound_sample) {
			decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
			return false;
		}

		if(target_sample < this_frame_sample) {
			upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
			if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
				decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
				return false;
			}
			approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
		}
		else { /* target_sample >= this_frame_sample + this frame's blocksize */
			lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
			if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
				decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
				return false;
			}
			approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
		}
	}

	return true;
}

#if FLAC__HAS_OGG
FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
{
	FLAC__uint64 left_pos = 0, right_pos = stream_length;
	FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
	FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1;
	FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
	FLAC__bool did_a_seek;
	unsigned iteration = 0;

	unsigned BINARY_SEARCH_AFTER_ITERATION = 2;

	static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;

	if(right_sample == 0) {
		right_sample = (FLAC__uint64)(-1);
		BINARY_SEARCH_AFTER_ITERATION = 0;
	}

	decoder->private_->target_sample = target_sample;
	for( ; ; iteration++) {
		if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
			if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
				pos = (right_pos + left_pos) / 2;
			}
			else {
#ifndef FLAC__INTEGER_ONLY_LIBRARY
#if defined _MSC_VER || defined __MINGW32__
				pos = (FLAC__uint64)((FLAC__double)(FLAC__int64)(target_sample - left_sample) / (FLAC__double)(FLAC__int64)(right_sample - left_sample) * (FLAC__double)(FLAC__int64)(right_pos - left_pos));
#else
				pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
#endif
#else
				if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
					pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
				else /* @@@ WATCHOUT, ~2TB limit */
					pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
#endif
			}

			if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
				decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
				return false;
			}
			if(!FLAC__stream_decoder_flush(decoder)) {
				return false;
			}
			did_a_seek = true;
		}
		else
			did_a_seek = false;

		decoder->private_->got_a_frame = false;
		if(!FLAC__stream_decoder_process_single(decoder)) {
			decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
			return false;
		}
		if(!decoder->private_->got_a_frame) {
			if(did_a_seek) {
				right_pos = pos;
				BINARY_SEARCH_AFTER_ITERATION = 0;
			}
			else {
				decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
				return false;
			}
		}
		else if(!decoder->private_->is_seeking) {
			break;
		}
		else {
			this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
			FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);

			if (did_a_seek) {
				if (this_frame_sample <= target_sample) {
					FLAC__ASSERT(this_frame_sample != target_sample);

					left_sample = this_frame_sample;
					if (left_pos == pos) {
						decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
						return false;
					}
					left_pos = pos;
				}
				else if(this_frame_sample > target_sample) {
					right_sample = this_frame_sample;
					if (right_pos == pos) {
						decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
						return false;
					}
					right_pos = pos;
				}
			}
		}
	}

	return true;
}
#endif

FLAC__StreamDecoderReadStatus file_read_callback_dec(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
	(void)client_data;

	if(*bytes > 0) {
		*bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
		if(ferror(decoder->private_->file))
			return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
		else if(*bytes == 0)
			return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
		else
			return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
	}
	else
		return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
}

FLAC__StreamDecoderSeekStatus file_seek_callback_dec(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
{
	(void)client_data;

	if(decoder->private_->file == stdin)
		return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
	else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
		return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
	else
		return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}

FLAC__StreamDecoderTellStatus file_tell_callback_dec(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
{
	off_t pos;
	(void)client_data;

	if(decoder->private_->file == stdin)
		return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
	else if((pos = ftello(decoder->private_->file)) < 0)
		return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
	else {
		*absolute_byte_offset = (FLAC__uint64)pos;
		return FLAC__STREAM_DECODER_TELL_STATUS_OK;
	}
}

FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
{
	struct stat filestats;
	(void)client_data;

	if(decoder->private_->file == stdin)
		return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
	else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
		return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
	else {
		*stream_length = (FLAC__uint64)filestats.st_size;
		return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
	}
}

FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
{
	(void)client_data;

	return feof(decoder->private_->file)? true : false;
}

#endif
/*** End of inlined file: stream_decoder.c ***/


/*** Start of inlined file: stream_encoder.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#if defined _MSC_VER || defined __MINGW32__
#include <io.h> /* for _setmode() */
#include <fcntl.h> /* for _O_BINARY */
#endif
#if defined __CYGWIN__ || defined __EMX__
#include <io.h> /* for setmode(), O_BINARY */
#include <fcntl.h> /* for _O_BINARY */
#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy() */
#include <sys/types.h> /* for off_t */
#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
#if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */
#define fseeko fseek
#define ftello ftell
#endif
#endif


/*** Start of inlined file: stream_encoder.h ***/
#ifndef FLAC__PROTECTED__STREAM_ENCODER_H
#define FLAC__PROTECTED__STREAM_ENCODER_H

#if FLAC__HAS_OGG
#include "private/ogg_encoder_aspect.h"
#endif

#ifndef FLAC__INTEGER_ONLY_LIBRARY

#define FLAC__MAX_APODIZATION_FUNCTIONS 32

typedef enum {
	FLAC__APODIZATION_BARTLETT,
	FLAC__APODIZATION_BARTLETT_HANN,
	FLAC__APODIZATION_BLACKMAN,
	FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE,
	FLAC__APODIZATION_CONNES,
	FLAC__APODIZATION_FLATTOP,
	FLAC__APODIZATION_GAUSS,
	FLAC__APODIZATION_HAMMING,
	FLAC__APODIZATION_HANN,
	FLAC__APODIZATION_KAISER_BESSEL,
	FLAC__APODIZATION_NUTTALL,
	FLAC__APODIZATION_RECTANGLE,
	FLAC__APODIZATION_TRIANGLE,
	FLAC__APODIZATION_TUKEY,
	FLAC__APODIZATION_WELCH
} FLAC__ApodizationFunction;

typedef struct {
	FLAC__ApodizationFunction type;
	union {
		struct {
			FLAC__real stddev;
		} gauss;
		struct {
			FLAC__real p;
		} tukey;
	} parameters;
} FLAC__ApodizationSpecification;

#endif // #ifndef FLAC__INTEGER_ONLY_LIBRARY

typedef struct FLAC__StreamEncoderProtected {
	FLAC__StreamEncoderState state;
	FLAC__bool verify;
	FLAC__bool streamable_subset;
	FLAC__bool do_md5;
	FLAC__bool do_mid_side_stereo;
	FLAC__bool loose_mid_side_stereo;
	unsigned channels;
	unsigned bits_per_sample;
	unsigned sample_rate;
	unsigned blocksize;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	unsigned num_apodizations;
	FLAC__ApodizationSpecification apodizations[FLAC__MAX_APODIZATION_FUNCTIONS];
#endif
	unsigned max_lpc_order;
	unsigned qlp_coeff_precision;
	FLAC__bool do_qlp_coeff_prec_search;
	FLAC__bool do_exhaustive_model_search;
	FLAC__bool do_escape_coding;
	unsigned min_residual_partition_order;
	unsigned max_residual_partition_order;
	unsigned rice_parameter_search_dist;
	FLAC__uint64 total_samples_estimate;
	FLAC__StreamMetadata **metadata;
	unsigned num_metadata_blocks;
	FLAC__uint64 streaminfo_offset, seektable_offset, audio_offset;
#if FLAC__HAS_OGG
	FLAC__OggEncoderAspect ogg_encoder_aspect;
#endif
} FLAC__StreamEncoderProtected;

#endif
/*** End of inlined file: stream_encoder.h ***/

#if FLAC__HAS_OGG
#include "include/private/ogg_helper.h"
#include "include/private/ogg_mapping.h"
#endif

/*** Start of inlined file: stream_encoder_framing.h ***/
#ifndef FLAC__PRIVATE__STREAM_ENCODER_FRAMING_H
#define FLAC__PRIVATE__STREAM_ENCODER_FRAMING_H

FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitWriter *bw);
FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitWriter *bw);
FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw);
FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw);
FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw);
FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw);

#endif
/*** End of inlined file: stream_encoder_framing.h ***/



/*** Start of inlined file: window.h ***/
#ifndef FLAC__PRIVATE__WINDOW_H
#define FLAC__PRIVATE__WINDOW_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifndef FLAC__INTEGER_ONLY_LIBRARY

void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_connes(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_flattop(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_gauss(FLAC__real *window, const FLAC__int32 L, const FLAC__real stddev); /* 0.0 < stddev <= 0.5 */
void FLAC__window_hamming(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_hann(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_kaiser_bessel(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_nuttall(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_rectangle(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L);
void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p);
void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L);

#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */

#endif
/*** End of inlined file: window.h ***/

#ifndef FLaC__INLINE
#define FLaC__INLINE
#endif

#ifdef min
#undef min
#endif
#define min(x,y) ((x)<(y)?(x):(y))

#ifdef max
#undef max
#endif
#define max(x,y) ((x)>(y)?(x):(y))

#undef EXACT_RICE_BITS_CALCULATION
#undef ENABLE_RICE_PARAMETER_SEARCH

typedef struct {
	FLAC__int32 *data[FLAC__MAX_CHANNELS];
	unsigned size; /* of each data[] in samples */
	unsigned tail;
} verify_input_fifo;

typedef struct {
	const FLAC__byte *data;
	unsigned capacity;
	unsigned bytes;
} verify_output;

typedef enum {
	ENCODER_IN_MAGIC = 0,
	ENCODER_IN_METADATA = 1,
	ENCODER_IN_AUDIO = 2
} EncoderStateHint;

static struct CompressionLevels {
	FLAC__bool do_mid_side_stereo;
	FLAC__bool loose_mid_side_stereo;
	unsigned max_lpc_order;
	unsigned qlp_coeff_precision;
	FLAC__bool do_qlp_coeff_prec_search;
	FLAC__bool do_escape_coding;
	FLAC__bool do_exhaustive_model_search;
	unsigned min_residual_partition_order;
	unsigned max_residual_partition_order;
	unsigned rice_parameter_search_dist;
} compression_levels_[] = {
	{ false, false,  0, 0, false, false, false, 0, 3, 0 },
	{ true , true ,  0, 0, false, false, false, 0, 3, 0 },
	{ true , false,  0, 0, false, false, false, 0, 3, 0 },
	{ false, false,  6, 0, false, false, false, 0, 4, 0 },
	{ true , true ,  8, 0, false, false, false, 0, 4, 0 },
	{ true , false,  8, 0, false, false, false, 0, 5, 0 },
	{ true , false,  8, 0, false, false, false, 0, 6, 0 },
	{ true , false,  8, 0, false, false, true , 0, 6, 0 },
	{ true , false, 12, 0, false, false, true , 0, 6, 0 }
};

static void set_defaults_enc(FLAC__StreamEncoder *encoder);
static void free_(FLAC__StreamEncoder *encoder);
static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block);
static void update_metadata_(const FLAC__StreamEncoder *encoder);
#if FLAC__HAS_OGG
static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
#endif
static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);

static FLAC__bool process_subframe_(
	FLAC__StreamEncoder *encoder,
	unsigned min_partition_order,
	unsigned max_partition_order,
	const FLAC__FrameHeader *frame_header,
	unsigned subframe_bps,
	const FLAC__int32 integer_signal[],
	FLAC__Subframe *subframe[2],
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
	FLAC__int32 *residual[2],
	unsigned *best_subframe,
	unsigned *best_bits
);

static FLAC__bool add_subframe_(
	FLAC__StreamEncoder *encoder,
	unsigned blocksize,
	unsigned subframe_bps,
	const FLAC__Subframe *subframe,
	FLAC__BitWriter *frame
);

static unsigned evaluate_constant_subframe_(
	FLAC__StreamEncoder *encoder,
	const FLAC__int32 signal,
	unsigned blocksize,
	unsigned subframe_bps,
	FLAC__Subframe *subframe
);

static unsigned evaluate_fixed_subframe_(
	FLAC__StreamEncoder *encoder,
	const FLAC__int32 signal[],
	FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned raw_bits_per_partition[],
	unsigned blocksize,
	unsigned subframe_bps,
	unsigned order,
	unsigned rice_parameter,
	unsigned rice_parameter_limit,
	unsigned min_partition_order,
	unsigned max_partition_order,
	FLAC__bool do_escape_coding,
	unsigned rice_parameter_search_dist,
	FLAC__Subframe *subframe,
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
);

#ifndef FLAC__INTEGER_ONLY_LIBRARY
static unsigned evaluate_lpc_subframe_(
	FLAC__StreamEncoder *encoder,
	const FLAC__int32 signal[],
	FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned raw_bits_per_partition[],
	const FLAC__real lp_coeff[],
	unsigned blocksize,
	unsigned subframe_bps,
	unsigned order,
	unsigned qlp_coeff_precision,
	unsigned rice_parameter,
	unsigned rice_parameter_limit,
	unsigned min_partition_order,
	unsigned max_partition_order,
	FLAC__bool do_escape_coding,
	unsigned rice_parameter_search_dist,
	FLAC__Subframe *subframe,
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
);
#endif

static unsigned evaluate_verbatim_subframe_(
	FLAC__StreamEncoder *encoder,
	const FLAC__int32 signal[],
	unsigned blocksize,
	unsigned subframe_bps,
	FLAC__Subframe *subframe
);

static unsigned find_best_partition_order_(
	struct FLAC__StreamEncoderPrivate *private_,
	const FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned raw_bits_per_partition[],
	unsigned residual_samples,
	unsigned predictor_order,
	unsigned rice_parameter,
	unsigned rice_parameter_limit,
	unsigned min_partition_order,
	unsigned max_partition_order,
	unsigned bps,
	FLAC__bool do_escape_coding,
	unsigned rice_parameter_search_dist,
	FLAC__EntropyCodingMethod *best_ecm
);

static void precompute_partition_info_sums_(
	const FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned residual_samples,
	unsigned predictor_order,
	unsigned min_partition_order,
	unsigned max_partition_order,
	unsigned bps
);

static void precompute_partition_info_escapes_(
	const FLAC__int32 residual[],
	unsigned raw_bits_per_partition[],
	unsigned residual_samples,
	unsigned predictor_order,
	unsigned min_partition_order,
	unsigned max_partition_order
);

static FLAC__bool set_partitioned_rice_(
#ifdef EXACT_RICE_BITS_CALCULATION
	const FLAC__int32 residual[],
#endif
	const FLAC__uint64 abs_residual_partition_sums[],
	const unsigned raw_bits_per_partition[],
	const unsigned residual_samples,
	const unsigned predictor_order,
	const unsigned suggested_rice_parameter,
	const unsigned rice_parameter_limit,
	const unsigned rice_parameter_search_dist,
	const unsigned partition_order,
	const FLAC__bool search_for_escapes,
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
	unsigned *bits
);

static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);

static void append_to_verify_fifo_(
	verify_input_fifo *fifo,
	const FLAC__int32 * const input[],
	unsigned input_offset,
	unsigned channels,
	unsigned wide_samples
);

static void append_to_verify_fifo_interleaved_(
	verify_input_fifo *fifo,
	const FLAC__int32 input[],
	unsigned input_offset,
	unsigned channels,
	unsigned wide_samples
);

static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);

static FLAC__StreamEncoderReadStatus file_read_callback_enc(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
static FLAC__StreamEncoderSeekStatus file_seek_callback_enc(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
static FLAC__StreamEncoderTellStatus file_tell_callback_enc(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
static FILE *get_binary_stdout_(void);

typedef struct FLAC__StreamEncoderPrivate {
	unsigned input_capacity;			  /* current size (in samples) of the signal and residual buffers */
	FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
	FLAC__int32 *integer_signal_mid_side[2];	  /* the integer version of the mid-side input signal (stereo only) */
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	FLAC__real *real_signal[FLAC__MAX_CHANNELS];	  /* (@@@ currently unused) the floating-point version of the input signal */
	FLAC__real *real_signal_mid_side[2];		  /* (@@@ currently unused) the floating-point version of the mid-side input signal (stereo only) */
	FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
	FLAC__real *windowed_signal;			  /* the integer_signal[] * current window[] */
#endif
	unsigned subframe_bps[FLAC__MAX_CHANNELS];	/* the effective bits per sample of the input signal (stream bps - wasted bits) */
	unsigned subframe_bps_mid_side[2];		/* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
	FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
	FLAC__int32 *residual_workspace_mid_side[2][2];
	FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
	FLAC__Subframe subframe_workspace_mid_side[2][2];
	FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
	FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
	unsigned best_subframe[FLAC__MAX_CHANNELS];	   /* index (0 or 1) into 2nd dimension of the above workspaces */
	unsigned best_subframe_mid_side[2];
	unsigned best_subframe_bits[FLAC__MAX_CHANNELS];  /* size in bits of the best subframe for each channel */
	unsigned best_subframe_bits_mid_side[2];
	FLAC__uint64 *abs_residual_partition_sums;	/* workspace where the sum of abs(candidate residual) for each partition is stored */
	unsigned *raw_bits_per_partition;		 /* workspace where the sum of silog2(candidate residual) for each partition is stored */
	FLAC__BitWriter *frame;			   /* the current frame being worked on */
	unsigned loose_mid_side_stereo_frames;		/* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
	unsigned loose_mid_side_stereo_frame_count;	   /* number of frames using the current channel assignment */
	FLAC__ChannelAssignment last_channel_assignment;
	FLAC__StreamMetadata streaminfo;		  /* scratchpad for STREAMINFO as it is built */
	FLAC__StreamMetadata_SeekTable *seek_table;	   /* pointer into encoder->protected_->metadata_ where the seek table is */
	unsigned current_sample_number;
	unsigned current_frame_number;
	FLAC__MD5Context md5context;
	FLAC__CPUInfo cpuinfo;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
#else
	unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
#endif
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
	void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
	void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
	void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
#endif
	FLAC__bool use_wide_by_block;	  /* use slow 64-bit versions of some functions because of the block size */
	FLAC__bool use_wide_by_partition;	  /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
	FLAC__bool use_wide_by_order;	  /* use slow 64-bit versions of some functions because of the lpc order */
	FLAC__bool disable_constant_subframes;
	FLAC__bool disable_fixed_subframes;
	FLAC__bool disable_verbatim_subframes;
#if FLAC__HAS_OGG
	FLAC__bool is_ogg;
#endif
	FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
	FLAC__StreamEncoderSeekCallback seek_callback;
	FLAC__StreamEncoderTellCallback tell_callback;
	FLAC__StreamEncoderWriteCallback write_callback;
	FLAC__StreamEncoderMetadataCallback metadata_callback;
	FLAC__StreamEncoderProgressCallback progress_callback;
	void *client_data;
	unsigned first_seekpoint_to_check;
	FILE *file;				/* only used when encoding to a file */
	FLAC__uint64 bytes_written;
	FLAC__uint64 samples_written;
	unsigned frames_written;
	unsigned total_frames_estimate;
	FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
	FLAC__int32 *integer_signal_mid_side_unaligned[2];
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) */
	FLAC__real *real_signal_mid_side_unaligned[2]; /* (@@@ currently unused) */
	FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
	FLAC__real *windowed_signal_unaligned;
#endif
	FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
	FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
	FLAC__uint64 *abs_residual_partition_sums_unaligned;
	unsigned *raw_bits_per_partition_unaligned;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
#endif
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
	struct {
		FLAC__StreamDecoder *decoder;
		EncoderStateHint state_hint;
		FLAC__bool needs_magic_hack;
		verify_input_fifo input_fifo;
		verify_output output;
		struct {
			FLAC__uint64 absolute_sample;
			unsigned frame_number;
			unsigned channel;
			unsigned sample;
			FLAC__int32 expected;
			FLAC__int32 got;
		} error_stats;
	} verify;
	FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
} FLAC__StreamEncoderPrivate;

FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
	"FLAC__STREAM_ENCODER_OK",
	"FLAC__STREAM_ENCODER_UNINITIALIZED",
	"FLAC__STREAM_ENCODER_OGG_ERROR",
	"FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
	"FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
	"FLAC__STREAM_ENCODER_CLIENT_ERROR",
	"FLAC__STREAM_ENCODER_IO_ERROR",
	"FLAC__STREAM_ENCODER_FRAMING_ERROR",
	"FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
};

FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
	"FLAC__STREAM_ENCODER_INIT_STATUS_OK",
	"FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
	"FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
	"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
	"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
	"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
	"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
	"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
	"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
	"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
	"FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
	"FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
	"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
	"FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
};

FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
	"FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
	"FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
	"FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
	"FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
};

FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
	"FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
	"FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
};

FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
	"FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
	"FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
	"FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
};

FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
	"FLAC__STREAM_ENCODER_TELL_STATUS_OK",
	"FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
	"FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
};

static const unsigned OVERREAD_ = 1;

FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
{
	FLAC__StreamEncoder *encoder;
	unsigned i;

	FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */

	encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
	if(encoder == 0) {
		return 0;
	}

	encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
	if(encoder->protected_ == 0) {
		free(encoder);
		return 0;
	}

	encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
	if(encoder->private_ == 0) {
		free(encoder->protected_);
		free(encoder);
		return 0;
	}

	encoder->private_->frame = FLAC__bitwriter_new();
	if(encoder->private_->frame == 0) {
		free(encoder->private_);
		free(encoder->protected_);
		free(encoder);
		return 0;
	}

	encoder->private_->file = 0;

	set_defaults_enc(encoder);

	encoder->private_->is_being_deleted = false;

	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
		encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
	}
	for(i = 0; i < 2; i++) {
		encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
		encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
	}
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
		encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
	}
	for(i = 0; i < 2; i++) {
		encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
		encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
	}

	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
		FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
	}
	for(i = 0; i < 2; i++) {
		FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
		FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
	}
	for(i = 0; i < 2; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);

	encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;

	return encoder;
}

FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
{
	unsigned i;

	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->protected_);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->private_->frame);

	encoder->private_->is_being_deleted = true;

	(void)FLAC__stream_encoder_finish(encoder);

	if(0 != encoder->private_->verify.decoder)
		FLAC__stream_decoder_delete(encoder->private_->verify.decoder);

	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
	}
	for(i = 0; i < 2; i++) {
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
	}
	for(i = 0; i < 2; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);

	FLAC__bitwriter_delete(encoder->private_->frame);
	free(encoder->private_);
	free(encoder->protected_);
	free(encoder);
}

static FLAC__StreamEncoderInitStatus init_stream_internal_enc(
	FLAC__StreamEncoder *encoder,
	FLAC__StreamEncoderReadCallback read_callback,
	FLAC__StreamEncoderWriteCallback write_callback,
	FLAC__StreamEncoderSeekCallback seek_callback,
	FLAC__StreamEncoderTellCallback tell_callback,
	FLAC__StreamEncoderMetadataCallback metadata_callback,
	void *client_data,
	FLAC__bool is_ogg
)
{
	unsigned i;
	FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;

	FLAC__ASSERT(0 != encoder);

	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;

#if !FLAC__HAS_OGG
	if(is_ogg)
		return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
#endif

	if(0 == write_callback || (seek_callback && 0 == tell_callback))
		return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;

	if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
		return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;

	if(encoder->protected_->channels != 2) {
		encoder->protected_->do_mid_side_stereo = false;
		encoder->protected_->loose_mid_side_stereo = false;
	}
	else if(!encoder->protected_->do_mid_side_stereo)
		encoder->protected_->loose_mid_side_stereo = false;

	if(encoder->protected_->bits_per_sample >= 32)
		encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */

	if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
		return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;

	if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
		return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;

	if(encoder->protected_->blocksize == 0) {
		if(encoder->protected_->max_lpc_order == 0)
			encoder->protected_->blocksize = 1152;
		else
			encoder->protected_->blocksize = 4096;
	}

	if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
		return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;

	if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
		return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;

	if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
		return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;

	if(encoder->protected_->qlp_coeff_precision == 0) {
		if(encoder->protected_->bits_per_sample < 16) {
			encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
		}
		else if(encoder->protected_->bits_per_sample == 16) {
			if(encoder->protected_->blocksize <= 192)
				encoder->protected_->qlp_coeff_precision = 7;
			else if(encoder->protected_->blocksize <= 384)
				encoder->protected_->qlp_coeff_precision = 8;
			else if(encoder->protected_->blocksize <= 576)
				encoder->protected_->qlp_coeff_precision = 9;
			else if(encoder->protected_->blocksize <= 1152)
				encoder->protected_->qlp_coeff_precision = 10;
			else if(encoder->protected_->blocksize <= 2304)
				encoder->protected_->qlp_coeff_precision = 11;
			else if(encoder->protected_->blocksize <= 4608)
				encoder->protected_->qlp_coeff_precision = 12;
			else
				encoder->protected_->qlp_coeff_precision = 13;
		}
		else {
			if(encoder->protected_->blocksize <= 384)
				encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
			else if(encoder->protected_->blocksize <= 1152)
				encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
			else
				encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
		}
		FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
	}
	else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
		return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;

	if(encoder->protected_->streamable_subset) {
		if(
			encoder->protected_->blocksize != 192 &&
			encoder->protected_->blocksize != 576 &&
			encoder->protected_->blocksize != 1152 &&
			encoder->protected_->blocksize != 2304 &&
			encoder->protected_->blocksize != 4608 &&
			encoder->protected_->blocksize != 256 &&
			encoder->protected_->blocksize != 512 &&
			encoder->protected_->blocksize != 1024 &&
			encoder->protected_->blocksize != 2048 &&
			encoder->protected_->blocksize != 4096 &&
			encoder->protected_->blocksize != 8192 &&
			encoder->protected_->blocksize != 16384
		)
			return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
		if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate))
			return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
		if(
			encoder->protected_->bits_per_sample != 8 &&
			encoder->protected_->bits_per_sample != 12 &&
			encoder->protected_->bits_per_sample != 16 &&
			encoder->protected_->bits_per_sample != 20 &&
			encoder->protected_->bits_per_sample != 24
		)
			return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
		if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
			return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
		if(
			encoder->protected_->sample_rate <= 48000 &&
			(
				encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
				encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
			)
		) {
			return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
		}
	}

	if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
		encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
	if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
		encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;

#if FLAC__HAS_OGG
	if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
		unsigned i;
		for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
			if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
				FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
				for( ; i > 0; i--)
					encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
				encoder->protected_->metadata[0] = vc;
				break;
			}
		}
	}
#endif
	if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
		unsigned i;
		for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
			if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
				encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
				break; /* take only the first one */
			}
		}
	}

	if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
		return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
	metadata_has_seektable = false;
	metadata_has_vorbis_comment = false;
	metadata_picture_has_type1 = false;
	metadata_picture_has_type2 = false;
	for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
		const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
		if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
			return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
		else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
			if(metadata_has_seektable) /* only one is allowed */
				return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
			metadata_has_seektable = true;
			if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
				return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
		}
		else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
			if(metadata_has_vorbis_comment) /* only one is allowed */
				return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
			metadata_has_vorbis_comment = true;
		}
		else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
			if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
				return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
		}
		else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
			if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
				return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
			if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
				if(metadata_picture_has_type1) /* there should only be 1 per stream */
					return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
				metadata_picture_has_type1 = true;
				if(
					m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
					(
						(strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
						m->data.picture.width != 32 ||
						m->data.picture.height != 32
					)
				)
					return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
			}
			else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
				if(metadata_picture_has_type2) /* there should only be 1 per stream */
					return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
				metadata_picture_has_type2 = true;
			}
		}
	}

	encoder->private_->input_capacity = 0;
	for(i = 0; i < encoder->protected_->channels; i++) {
		encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
		encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
#endif
	}
	for(i = 0; i < 2; i++) {
		encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
		encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
#endif
	}
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	for(i = 0; i < encoder->protected_->num_apodizations; i++)
		encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
	encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
#endif
	for(i = 0; i < encoder->protected_->channels; i++) {
		encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
		encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
		encoder->private_->best_subframe[i] = 0;
	}
	for(i = 0; i < 2; i++) {
		encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
		encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
		encoder->private_->best_subframe_mid_side[i] = 0;
	}
	encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
	encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
#else
	FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
	FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
	FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
	FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
	encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF);
#endif
	if(encoder->private_->loose_mid_side_stereo_frames == 0)
		encoder->private_->loose_mid_side_stereo_frames = 1;
	encoder->private_->loose_mid_side_stereo_frame_count = 0;
	encoder->private_->current_sample_number = 0;
	encoder->private_->current_frame_number = 0;

	encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
	encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
	encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */

	FLAC__cpu_info(&encoder->private_->cpuinfo);
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
#endif
	encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
	encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
	encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
#endif
#ifndef FLAC__INTEGER_ONLY_LIBRARY
# ifndef FLAC__NO_ASM
	if(encoder->private_->cpuinfo.use_asm) {
#  ifdef FLAC__CPU_IA32
		FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
#   ifdef FLAC__HAS_NASM
		if(encoder->private_->cpuinfo.data.ia32.sse) {
			if(encoder->protected_->max_lpc_order < 4)
				encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
			else if(encoder->protected_->max_lpc_order < 8)
				encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
			else if(encoder->protected_->max_lpc_order < 12)
				encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
			else
				encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
		}
		else if(encoder->private_->cpuinfo.data.ia32._3dnow)
			encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
		else
			encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
		if(encoder->private_->cpuinfo.data.ia32.mmx) {
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
		}
		else {
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
		}
		if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
			encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
#   endif /* FLAC__HAS_NASM */
#  endif /* FLAC__CPU_IA32 */
	}
# endif /* !FLAC__NO_ASM */
#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
	if(encoder->private_->use_wide_by_block) {
		encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
	}

	encoder->protected_->state = FLAC__STREAM_ENCODER_OK;

#if FLAC__HAS_OGG
	encoder->private_->is_ogg = is_ogg;
	if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}
#endif

	encoder->private_->read_callback = read_callback;
	encoder->private_->write_callback = write_callback;
	encoder->private_->seek_callback = seek_callback;
	encoder->private_->tell_callback = tell_callback;
	encoder->private_->metadata_callback = metadata_callback;
	encoder->private_->client_data = client_data;

	if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}

	if(!FLAC__bitwriter_init(encoder->private_->frame)) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}

	if(encoder->protected_->verify) {
		encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
		for(i = 0; i < encoder->protected_->channels; i++) {
			if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)safe_malloc_mul_2op_(sizeof(FLAC__int32), /*times*/encoder->private_->verify.input_fifo.size))) {
				encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
				return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
			}
		}
		encoder->private_->verify.input_fifo.tail = 0;

		encoder->private_->verify.decoder = FLAC__stream_decoder_new();
		if(0 == encoder->private_->verify.decoder) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
			return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
		}

		if(FLAC__stream_decoder_init_stream(encoder->private_->verify.decoder, verify_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, verify_write_callback_, verify_metadata_callback_, verify_error_callback_, /*client_data=*/encoder) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
			return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
		}
	}
	encoder->private_->verify.error_stats.absolute_sample = 0;
	encoder->private_->verify.error_stats.frame_number = 0;
	encoder->private_->verify.error_stats.channel = 0;
	encoder->private_->verify.error_stats.sample = 0;
	encoder->private_->verify.error_stats.expected = 0;
	encoder->private_->verify.error_stats.got = 0;

	encoder->private_->first_seekpoint_to_check = 0;
	encoder->private_->samples_written = 0;
	encoder->protected_->streaminfo_offset = 0;
	encoder->protected_->seektable_offset = 0;
	encoder->protected_->audio_offset = 0;

	if(encoder->protected_->verify)
		encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
	if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}
	if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}

	if(encoder->protected_->verify)
		encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
	encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
	encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
	encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
	encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
	encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
	encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
	encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
	encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
	encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
	encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
	encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
	memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
	if(encoder->protected_->do_md5)
		FLAC__MD5Init(&encoder->private_->md5context);
	if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}
	if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}

	encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
	encoder->private_->streaminfo.data.stream_info.total_samples = 0;

	if(!metadata_has_vorbis_comment) {
		FLAC__StreamMetadata vorbis_comment;
		vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
		vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
		vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
		vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
		vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
		vorbis_comment.data.vorbis_comment.num_comments = 0;
		vorbis_comment.data.vorbis_comment.comments = 0;
		if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
			return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
		}
		if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
			return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
		}
	}

	for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
		encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
		if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
			return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
		}
		if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
			return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
		}
	}

	if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
		encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}

	if(encoder->protected_->verify)
		encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;

	return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
}

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
	FLAC__StreamEncoder *encoder,
	FLAC__StreamEncoderWriteCallback write_callback,
	FLAC__StreamEncoderSeekCallback seek_callback,
	FLAC__StreamEncoderTellCallback tell_callback,
	FLAC__StreamEncoderMetadataCallback metadata_callback,
	void *client_data
)
{
	return init_stream_internal_enc(
		encoder,
0,
		write_callback,
		seek_callback,
		tell_callback,
		metadata_callback,
		client_data,
false
	);
}

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
	FLAC__StreamEncoder *encoder,
	FLAC__StreamEncoderReadCallback read_callback,
	FLAC__StreamEncoderWriteCallback write_callback,
	FLAC__StreamEncoderSeekCallback seek_callback,
	FLAC__StreamEncoderTellCallback tell_callback,
	FLAC__StreamEncoderMetadataCallback metadata_callback,
	void *client_data
)
{
	return init_stream_internal_enc(
		encoder,
		read_callback,
		write_callback,
		seek_callback,
		tell_callback,
		metadata_callback,
		client_data,
true
	);
}

static FLAC__StreamEncoderInitStatus init_FILE_internal_enc(
	FLAC__StreamEncoder *encoder,
	FILE *file,
	FLAC__StreamEncoderProgressCallback progress_callback,
	void *client_data,
	FLAC__bool is_ogg
)
{
	FLAC__StreamEncoderInitStatus init_status;

	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != file);

	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;

	if(file == 0) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}

	if(file == stdout)
		file = get_binary_stdout_(); /* just to be safe */

	encoder->private_->file = file;

	encoder->private_->progress_callback = progress_callback;
	encoder->private_->bytes_written = 0;
	encoder->private_->samples_written = 0;
	encoder->private_->frames_written = 0;

	init_status = init_stream_internal_enc(
		encoder,
		encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_enc : 0,
		file_write_callback_,
		encoder->private_->file == stdout? 0 : file_seek_callback_enc,
		encoder->private_->file == stdout? 0 : file_tell_callback_enc,
0,
		client_data,
		is_ogg
	);
	if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
		return init_status;
	}

	{
		unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);

		FLAC__ASSERT(blocksize != 0);
		encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
	}

	return init_status;
}

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
	FLAC__StreamEncoder *encoder,
	FILE *file,
	FLAC__StreamEncoderProgressCallback progress_callback,
	void *client_data
)
{
	return init_FILE_internal_enc(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
}

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
	FLAC__StreamEncoder *encoder,
	FILE *file,
	FLAC__StreamEncoderProgressCallback progress_callback,
	void *client_data
)
{
	return init_FILE_internal_enc(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
}

static FLAC__StreamEncoderInitStatus init_file_internal_enc(
	FLAC__StreamEncoder *encoder,
	const char *filename,
	FLAC__StreamEncoderProgressCallback progress_callback,
	void *client_data,
	FLAC__bool is_ogg
)
{
	FILE *file;

	FLAC__ASSERT(0 != encoder);

	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;

	file = filename? fopen(filename, "w+b") : stdout;

	if(file == 0) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
		return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
	}

	return init_FILE_internal_enc(encoder, file, progress_callback, client_data, is_ogg);
}

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
	FLAC__StreamEncoder *encoder,
	const char *filename,
	FLAC__StreamEncoderProgressCallback progress_callback,
	void *client_data
)
{
	return init_file_internal_enc(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
}

FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
	FLAC__StreamEncoder *encoder,
	const char *filename,
	FLAC__StreamEncoderProgressCallback progress_callback,
	void *client_data
)
{
	return init_file_internal_enc(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
}

FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
{
	FLAC__bool error = false;

	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);

	if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
		return true;

	if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
		if(encoder->private_->current_sample_number != 0) {
			const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
			encoder->protected_->blocksize = encoder->private_->current_sample_number;
			if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
				error = true;
		}
	}

	if(encoder->protected_->do_md5)
		FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);

	if(!encoder->private_->is_being_deleted) {
		if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
			if(encoder->private_->seek_callback) {
#if FLAC__HAS_OGG
				if(encoder->private_->is_ogg)
					update_ogg_metadata_(encoder);
				else
#endif
				update_metadata_(encoder);

				if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
					error = true;
			}
			if(encoder->private_->metadata_callback)
				encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
		}

		if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
			if(!error)
				encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
			error = true;
		}
	}

	if(0 != encoder->private_->file) {
		if(encoder->private_->file != stdout)
			fclose(encoder->private_->file);
		encoder->private_->file = 0;
	}

#if FLAC__HAS_OGG
	if(encoder->private_->is_ogg)
		FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
#endif

	free_(encoder);
	set_defaults_enc(encoder);

	if(!error)
		encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;

	return !error;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
#if FLAC__HAS_OGG
	FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
	return true;
#else
	(void)value;
	return false;
#endif
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
	encoder->protected_->verify = value;
#endif
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->streamable_subset = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->do_md5 = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->channels = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->bits_per_sample = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->sample_rate = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__bool ok = true;
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
		value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
	ok &= FLAC__stream_encoder_set_do_mid_side_stereo	  (encoder, compression_levels_[value].do_mid_side_stereo);
	ok &= FLAC__stream_encoder_set_loose_mid_side_stereo	   (encoder, compression_levels_[value].loose_mid_side_stereo);
#ifndef FLAC__INTEGER_ONLY_LIBRARY
#if 0
	ok &= FLAC__stream_encoder_set_apodization		 (encoder, compression_levels_[value].apodization);
#else
	encoder->protected_->num_apodizations = 1;
	encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
	encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
#endif
#endif
	ok &= FLAC__stream_encoder_set_max_lpc_order		   (encoder, compression_levels_[value].max_lpc_order);
	ok &= FLAC__stream_encoder_set_qlp_coeff_precision	 (encoder, compression_levels_[value].qlp_coeff_precision);
	ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search	(encoder, compression_levels_[value].do_qlp_coeff_prec_search);
	ok &= FLAC__stream_encoder_set_do_escape_coding		(encoder, compression_levels_[value].do_escape_coding);
	ok &= FLAC__stream_encoder_set_do_exhaustive_model_search  (encoder, compression_levels_[value].do_exhaustive_model_search);
	ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
	ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
	ok &= FLAC__stream_encoder_set_rice_parameter_search_dist  (encoder, compression_levels_[value].rice_parameter_search_dist);
	return ok;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->blocksize = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->do_mid_side_stereo = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->loose_mid_side_stereo = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	FLAC__ASSERT(0 != specification);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
#ifdef FLAC__INTEGER_ONLY_LIBRARY
	(void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
#else
	encoder->protected_->num_apodizations = 0;
	while(1) {
		const char *s = strchr(specification, ';');
		const size_t n = s? (size_t)(s - specification) : strlen(specification);
		if     (n==8  && 0 == strncmp("bartlett"     , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
		else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
		else if(n==8  && 0 == strncmp("blackman"     , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
		else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
		else if(n==6  && 0 == strncmp("connes"       , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
		else if(n==7  && 0 == strncmp("flattop"      , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
		else if(n>7   && 0 == strncmp("gauss("       , specification, 6)) {
			FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
			if (stddev > 0.0 && stddev <= 0.5) {
				encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
				encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
			}
		}
		else if(n==7  && 0 == strncmp("hamming"      , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
		else if(n==4  && 0 == strncmp("hann"         , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
		else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
		else if(n==7  && 0 == strncmp("nuttall"      , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
		else if(n==9  && 0 == strncmp("rectangle"    , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
		else if(n==8  && 0 == strncmp("triangle"     , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
		else if(n>7   && 0 == strncmp("tukey("       , specification, 6)) {
			FLAC__real p = (FLAC__real)strtod(specification+6, 0);
			if (p >= 0.0 && p <= 1.0) {
				encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
				encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
			}
		}
		else if(n==5  && 0 == strncmp("welch"        , specification, n))
			encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
		if (encoder->protected_->num_apodizations == 32)
			break;
		if (s)
			specification = s+1;
		else
			break;
	}
	if(encoder->protected_->num_apodizations == 0) {
		encoder->protected_->num_apodizations = 1;
		encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
		encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
	}
#endif
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->max_lpc_order = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->qlp_coeff_precision = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->do_qlp_coeff_prec_search = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
#if 0
	encoder->protected_->do_escape_coding = value;
#else
	(void)value;
#endif
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->do_exhaustive_model_search = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->min_residual_partition_order = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->max_residual_partition_order = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
#if 0
	encoder->protected_->rice_parameter_search_dist = value;
#else
	(void)value;
#endif
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->protected_->total_samples_estimate = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	if(0 == metadata)
		num_blocks = 0;
	if(0 == num_blocks)
		metadata = 0;
	if(encoder->protected_->metadata) {
		free(encoder->protected_->metadata);
		encoder->protected_->metadata = 0;
		encoder->protected_->num_metadata_blocks = 0;
	}
	if(num_blocks) {
		FLAC__StreamMetadata **m;
		if(0 == (m = (FLAC__StreamMetadata**)safe_malloc_mul_2op_(sizeof(m[0]), /*times*/num_blocks)))
			return false;
		memcpy(m, metadata, sizeof(m[0]) * num_blocks);
		encoder->protected_->metadata = m;
		encoder->protected_->num_metadata_blocks = num_blocks;
	}
#if FLAC__HAS_OGG
	if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
		return false;
#endif
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->private_->disable_constant_subframes = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->private_->disable_fixed_subframes = value;
	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return false;
	encoder->private_->disable_verbatim_subframes = value;
	return true;
}

FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->state;
}

FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->verify)
		return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
	else
		return FLAC__STREAM_DECODER_UNINITIALIZED;
}

FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
		return FLAC__StreamEncoderStateString[encoder->protected_->state];
	else
		return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
}

FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	if(0 != absolute_sample)
		*absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
	if(0 != frame_number)
		*frame_number = encoder->private_->verify.error_stats.frame_number;
	if(0 != channel)
		*channel = encoder->private_->verify.error_stats.channel;
	if(0 != sample)
		*sample = encoder->private_->verify.error_stats.sample;
	if(0 != expected)
		*expected = encoder->private_->verify.error_stats.expected;
	if(0 != got)
		*got = encoder->private_->verify.error_stats.got;
}

FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->verify;
}

FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->streamable_subset;
}

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->do_md5;
}

FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->channels;
}

FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->bits_per_sample;
}

FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->sample_rate;
}

FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->blocksize;
}

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->do_mid_side_stereo;
}

FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->loose_mid_side_stereo;
}

FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->max_lpc_order;
}

FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->qlp_coeff_precision;
}

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->do_qlp_coeff_prec_search;
}

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->do_escape_coding;
}

FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->do_exhaustive_model_search;
}

FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->min_residual_partition_order;
}

FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->max_residual_partition_order;
}

FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->rice_parameter_search_dist;
}

FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	return encoder->protected_->total_samples_estimate;
}

FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
{
	unsigned i, j = 0, channel;
	const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;

	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);

	do {
		const unsigned n = min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j);

		if(encoder->protected_->verify)
			append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n);

		for(channel = 0; channel < channels; channel++)
			memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n);

		if(encoder->protected_->do_mid_side_stereo) {
			FLAC__ASSERT(channels == 2);
			for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
				encoder->private_->integer_signal_mid_side[1][i] = buffer[0][j] - buffer[1][j];
				encoder->private_->integer_signal_mid_side[0][i] = (buffer[0][j] + buffer[1][j]) >> 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
			}
		}
		else
			j += n;

		encoder->private_->current_sample_number += n;

		if(encoder->private_->current_sample_number > blocksize) {
			FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_);
			FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
			if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
				return false;
			for(channel = 0; channel < channels; channel++)
				encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
			if(encoder->protected_->do_mid_side_stereo) {
				encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
				encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
			}
			encoder->private_->current_sample_number = 1;
		}
	} while(j < samples);

	return true;
}

FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
{
	unsigned i, j, k, channel;
	FLAC__int32 x, mid, side;
	const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;

	FLAC__ASSERT(0 != encoder);
	FLAC__ASSERT(0 != encoder->private_);
	FLAC__ASSERT(0 != encoder->protected_);
	FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);

	j = k = 0;
	if(encoder->protected_->do_mid_side_stereo && channels == 2) {
		do {
			if(encoder->protected_->verify)
				append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));

			for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
				encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
				x = buffer[k++];
				encoder->private_->integer_signal[1][i] = x;
				mid += x;
				side -= x;
				mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
				encoder->private_->integer_signal_mid_side[1][i] = side;
				encoder->private_->integer_signal_mid_side[0][i] = mid;
			}
			encoder->private_->current_sample_number = i;
			if(i > blocksize) {
				if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
					return false;
				/* move unprocessed overread samples to beginnings of arrays */
				FLAC__ASSERT(i == blocksize+OVERREAD_);
				FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
				encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][blocksize];
				encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][blocksize];
				encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
				encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
				encoder->private_->current_sample_number = 1;
			}
		} while(j < samples);
	}
	else {
		do {
			if(encoder->protected_->verify)
				append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));

			for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
				for(channel = 0; channel < channels; channel++)
					encoder->private_->integer_signal[channel][i] = buffer[k++];
			}
			encoder->private_->current_sample_number = i;
			if(i > blocksize) {
				if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
					return false;
				/* move unprocessed overread samples to beginnings of arrays */
				FLAC__ASSERT(i == blocksize+OVERREAD_);
				FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
				for(channel = 0; channel < channels; channel++)
					encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
				encoder->private_->current_sample_number = 1;
			}
		} while(j < samples);
	}

	return true;
}

void set_defaults_enc(FLAC__StreamEncoder *encoder)
{
	FLAC__ASSERT(0 != encoder);

#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
	encoder->protected_->verify = true;
#else
	encoder->protected_->verify = false;
#endif
	encoder->protected_->streamable_subset = true;
	encoder->protected_->do_md5 = true;
	encoder->protected_->do_mid_side_stereo = false;
	encoder->protected_->loose_mid_side_stereo = false;
	encoder->protected_->channels = 2;
	encoder->protected_->bits_per_sample = 16;
	encoder->protected_->sample_rate = 44100;
	encoder->protected_->blocksize = 0;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	encoder->protected_->num_apodizations = 1;
	encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
	encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
#endif
	encoder->protected_->max_lpc_order = 0;
	encoder->protected_->qlp_coeff_precision = 0;
	encoder->protected_->do_qlp_coeff_prec_search = false;
	encoder->protected_->do_exhaustive_model_search = false;
	encoder->protected_->do_escape_coding = false;
	encoder->protected_->min_residual_partition_order = 0;
	encoder->protected_->max_residual_partition_order = 0;
	encoder->protected_->rice_parameter_search_dist = 0;
	encoder->protected_->total_samples_estimate = 0;
	encoder->protected_->metadata = 0;
	encoder->protected_->num_metadata_blocks = 0;

	encoder->private_->seek_table = 0;
	encoder->private_->disable_constant_subframes = false;
	encoder->private_->disable_fixed_subframes = false;
	encoder->private_->disable_verbatim_subframes = false;
#if FLAC__HAS_OGG
	encoder->private_->is_ogg = false;
#endif
	encoder->private_->read_callback = 0;
	encoder->private_->write_callback = 0;
	encoder->private_->seek_callback = 0;
	encoder->private_->tell_callback = 0;
	encoder->private_->metadata_callback = 0;
	encoder->private_->progress_callback = 0;
	encoder->private_->client_data = 0;

#if FLAC__HAS_OGG
	FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
#endif
}

void free_(FLAC__StreamEncoder *encoder)
{
	unsigned i, channel;

	FLAC__ASSERT(0 != encoder);
	if(encoder->protected_->metadata) {
		free(encoder->protected_->metadata);
		encoder->protected_->metadata = 0;
		encoder->protected_->num_metadata_blocks = 0;
	}
	for(i = 0; i < encoder->protected_->channels; i++) {
		if(0 != encoder->private_->integer_signal_unaligned[i]) {
			free(encoder->private_->integer_signal_unaligned[i]);
			encoder->private_->integer_signal_unaligned[i] = 0;
		}
#ifndef FLAC__INTEGER_ONLY_LIBRARY
		if(0 != encoder->private_->real_signal_unaligned[i]) {
			free(encoder->private_->real_signal_unaligned[i]);
			encoder->private_->real_signal_unaligned[i] = 0;
		}
#endif
	}
	for(i = 0; i < 2; i++) {
		if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
			free(encoder->private_->integer_signal_mid_side_unaligned[i]);
			encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
		}
#ifndef FLAC__INTEGER_ONLY_LIBRARY
		if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
			free(encoder->private_->real_signal_mid_side_unaligned[i]);
			encoder->private_->real_signal_mid_side_unaligned[i] = 0;
		}
#endif
	}
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	for(i = 0; i < encoder->protected_->num_apodizations; i++) {
		if(0 != encoder->private_->window_unaligned[i]) {
			free(encoder->private_->window_unaligned[i]);
			encoder->private_->window_unaligned[i] = 0;
		}
	}
	if(0 != encoder->private_->windowed_signal_unaligned) {
		free(encoder->private_->windowed_signal_unaligned);
		encoder->private_->windowed_signal_unaligned = 0;
	}
#endif
	for(channel = 0; channel < encoder->protected_->channels; channel++) {
		for(i = 0; i < 2; i++) {
			if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
				free(encoder->private_->residual_workspace_unaligned[channel][i]);
				encoder->private_->residual_workspace_unaligned[channel][i] = 0;
			}
		}
	}
	for(channel = 0; channel < 2; channel++) {
		for(i = 0; i < 2; i++) {
			if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
				free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
				encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
			}
		}
	}
	if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
		free(encoder->private_->abs_residual_partition_sums_unaligned);
		encoder->private_->abs_residual_partition_sums_unaligned = 0;
	}
	if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
		free(encoder->private_->raw_bits_per_partition_unaligned);
		encoder->private_->raw_bits_per_partition_unaligned = 0;
	}
	if(encoder->protected_->verify) {
		for(i = 0; i < encoder->protected_->channels; i++) {
			if(0 != encoder->private_->verify.input_fifo.data[i]) {
				free(encoder->private_->verify.input_fifo.data[i]);
				encoder->private_->verify.input_fifo.data[i] = 0;
			}
		}
	}
	FLAC__bitwriter_free(encoder->private_->frame);
}

FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
{
	FLAC__bool ok;
	unsigned i, channel;

	FLAC__ASSERT(new_blocksize > 0);
	FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
	FLAC__ASSERT(encoder->private_->current_sample_number == 0);

	if(new_blocksize <= encoder->private_->input_capacity)
		return true;

	ok = true;

	for(i = 0; ok && i < encoder->protected_->channels; i++) {
		ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
		memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
		encoder->private_->integer_signal[i] += 4;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
#if 0 /* @@@ currently unused */
		if(encoder->protected_->max_lpc_order > 0)
			ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
#endif
#endif
	}
	for(i = 0; ok && i < 2; i++) {
		ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
		memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
		encoder->private_->integer_signal_mid_side[i] += 4;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
#if 0 /* @@@ currently unused */
		if(encoder->protected_->max_lpc_order > 0)
			ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
#endif
#endif
	}
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	if(ok && encoder->protected_->max_lpc_order > 0) {
		for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
			ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
		ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
	}
#endif
	for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
		for(i = 0; ok && i < 2; i++) {
			ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
		}
	}
	for(channel = 0; ok && channel < 2; channel++) {
		for(i = 0; ok && i < 2; i++) {
			ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
		}
	}
	ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
	if(encoder->protected_->do_escape_coding)
		ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);

#ifndef FLAC__INTEGER_ONLY_LIBRARY
	if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
		for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
			switch(encoder->protected_->apodizations[i].type) {
				case FLAC__APODIZATION_BARTLETT:
					FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_BARTLETT_HANN:
					FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_BLACKMAN:
					FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
					FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_CONNES:
					FLAC__window_connes(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_FLATTOP:
					FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_GAUSS:
					FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
					break;
				case FLAC__APODIZATION_HAMMING:
					FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_HANN:
					FLAC__window_hann(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_KAISER_BESSEL:
					FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_NUTTALL:
					FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_RECTANGLE:
					FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_TRIANGLE:
					FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
					break;
				case FLAC__APODIZATION_TUKEY:
					FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
					break;
				case FLAC__APODIZATION_WELCH:
					FLAC__window_welch(encoder->private_->window[i], new_blocksize);
					break;
				default:
					FLAC__ASSERT(0);
					FLAC__window_hann(encoder->private_->window[i], new_blocksize);
					break;
			}
		}
	}
#endif

	if(ok)
		encoder->private_->input_capacity = new_blocksize;
	else
		encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;

	return ok;
}

FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
{
	const FLAC__byte *buffer;
	size_t bytes;

	FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));

	if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}

	if(encoder->protected_->verify) {
		encoder->private_->verify.output.data = buffer;
		encoder->private_->verify.output.bytes = bytes;
		if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
			encoder->private_->verify.needs_magic_hack = true;
		}
		else {
			if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
				FLAC__bitwriter_release_buffer(encoder->private_->frame);
				FLAC__bitwriter_clear(encoder->private_->frame);
				if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
					encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
				return false;
			}
		}
	}

	if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
		FLAC__bitwriter_release_buffer(encoder->private_->frame);
		FLAC__bitwriter_clear(encoder->private_->frame);
		encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
		return false;
	}

	FLAC__bitwriter_release_buffer(encoder->private_->frame);
	FLAC__bitwriter_clear(encoder->private_->frame);

	if(samples > 0) {
		encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
		encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
	}

	return true;
}

FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
{
	FLAC__StreamEncoderWriteStatus status;
	FLAC__uint64 output_position = 0;

	if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
		return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
	}

	if(samples == 0) {
		FLAC__MetadataType type = (FLAC__MetadataType) (buffer[0] & 0x7f);
		if(type == FLAC__METADATA_TYPE_STREAMINFO)
			encoder->protected_->streaminfo_offset = output_position;
		else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
			encoder->protected_->seektable_offset = output_position;
	}

	if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
		const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
		const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
		const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
		FLAC__uint64 test_sample;
		unsigned i;
		for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
			test_sample = encoder->private_->seek_table->points[i].sample_number;
			if(test_sample > frame_last_sample) {
				break;
			}
			else if(test_sample >= frame_first_sample) {
				encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
				encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
				encoder->private_->seek_table->points[i].frame_samples = blocksize;
				encoder->private_->first_seekpoint_to_check++;
			}
			else {
				encoder->private_->first_seekpoint_to_check++;
			}
		}
	}

#if FLAC__HAS_OGG
	if(encoder->private_->is_ogg) {
		status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
			&encoder->protected_->ogg_encoder_aspect,
			buffer,
			bytes,
			samples,
			encoder->private_->current_frame_number,
			is_last_block,
			(FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
			encoder,
			encoder->private_->client_data
		);
	}
	else
#endif
	status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);

	if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
		encoder->private_->bytes_written += bytes;
		encoder->private_->samples_written += samples;
		encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
	}
	else
		encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;

	return status;
}

void update_metadata_(const FLAC__StreamEncoder *encoder)
{
	FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
	const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
	const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
	const unsigned min_framesize = metadata->data.stream_info.min_framesize;
	const unsigned max_framesize = metadata->data.stream_info.max_framesize;
	const unsigned bps = metadata->data.stream_info.bits_per_sample;
	FLAC__StreamEncoderSeekStatus seek_status;

	FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);

	{
		const unsigned md5_offset =
			FLAC__STREAM_METADATA_HEADER_LENGTH +
			(
				FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
			) / 8;

		if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
			if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
				encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
			return;
		}
		if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
			return;
		}
	}

	{
		const unsigned total_samples_byte_offset =
			FLAC__STREAM_METADATA_HEADER_LENGTH +
			(
				FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
				- 4
			) / 8;

		b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
		b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
		b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
		b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
		b[4] = (FLAC__byte)(samples & 0xFF);
		if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + total_samples_byte_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
			if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
				encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
			return;
		}
		if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
			return;
		}
	}

	{
		const unsigned min_framesize_offset =
			FLAC__STREAM_METADATA_HEADER_LENGTH +
			(
				FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
			) / 8;

		b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
		b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
		b[2] = (FLAC__byte)(min_framesize & 0xFF);
		b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
		b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
		b[5] = (FLAC__byte)(max_framesize & 0xFF);
		if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + min_framesize_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
			if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
				encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
			return;
		}
		if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
			return;
		}
	}

	if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
		unsigned i;

		FLAC__format_seektable_sort(encoder->private_->seek_table);

		FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));

		if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
			if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
				encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
			return;
		}

		for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
			FLAC__uint64 xx;
			unsigned x;
			xx = encoder->private_->seek_table->points[i].sample_number;
			b[7] = (FLAC__byte)xx; xx >>= 8;
			b[6] = (FLAC__byte)xx; xx >>= 8;
			b[5] = (FLAC__byte)xx; xx >>= 8;
			b[4] = (FLAC__byte)xx; xx >>= 8;
			b[3] = (FLAC__byte)xx; xx >>= 8;
			b[2] = (FLAC__byte)xx; xx >>= 8;
			b[1] = (FLAC__byte)xx; xx >>= 8;
			b[0] = (FLAC__byte)xx; xx >>= 8;
			xx = encoder->private_->seek_table->points[i].stream_offset;
			b[15] = (FLAC__byte)xx; xx >>= 8;
			b[14] = (FLAC__byte)xx; xx >>= 8;
			b[13] = (FLAC__byte)xx; xx >>= 8;
			b[12] = (FLAC__byte)xx; xx >>= 8;
			b[11] = (FLAC__byte)xx; xx >>= 8;
			b[10] = (FLAC__byte)xx; xx >>= 8;
			b[9] = (FLAC__byte)xx; xx >>= 8;
			b[8] = (FLAC__byte)xx; xx >>= 8;
			x = encoder->private_->seek_table->points[i].frame_samples;
			b[17] = (FLAC__byte)x; x >>= 8;
			b[16] = (FLAC__byte)x; x >>= 8;
			if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
				encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
				return;
			}
		}
	}
}

#if FLAC__HAS_OGG
void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
{
	static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
		FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
		FLAC__OGG_MAPPING_MAGIC_LENGTH +
		FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
		FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
		FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
		FLAC__STREAM_SYNC_LENGTH
	;
	FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
	const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
	const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
	const unsigned min_framesize = metadata->data.stream_info.min_framesize;
	const unsigned max_framesize = metadata->data.stream_info.max_framesize;
	ogg_page page;

	FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
	FLAC__ASSERT(0 != encoder->private_->seek_callback);

	if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
		return;

	simple_ogg_page__init(&page);
	if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
		simple_ogg_page__clear(&page);
		return; /* state already set */
	}

	{
		const unsigned md5_offset =
			FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
			FLAC__STREAM_METADATA_HEADER_LENGTH +
			(
				FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
			) / 8;

		if(md5_offset + 16 > (unsigned)page.body_len) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
			simple_ogg_page__clear(&page);
			return;
		}
		memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
	}

	{
		const unsigned total_samples_byte_offset =
			FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
			FLAC__STREAM_METADATA_HEADER_LENGTH +
			(
				FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
				- 4
			) / 8;

		if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
			simple_ogg_page__clear(&page);
			return;
		}
		b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
		b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
		b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
		b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
		b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
		b[4] = (FLAC__byte)(samples & 0xFF);
		memcpy(page.body + total_samples_byte_offset, b, 5);
	}

	{
		const unsigned min_framesize_offset =
			FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
			FLAC__STREAM_METADATA_HEADER_LENGTH +
			(
				FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
				FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
			) / 8;

		if(min_framesize_offset + 6 > (unsigned)page.body_len) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
			simple_ogg_page__clear(&page);
			return;
		}
		b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
		b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
		b[2] = (FLAC__byte)(min_framesize & 0xFF);
		b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
		b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
		b[5] = (FLAC__byte)(max_framesize & 0xFF);
		memcpy(page.body + min_framesize_offset, b, 6);
	}
	if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
		simple_ogg_page__clear(&page);
		return; /* state already set */
	}
	simple_ogg_page__clear(&page);

	if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
		unsigned i;
		FLAC__byte *p;

		FLAC__format_seektable_sort(encoder->private_->seek_table);

		FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));

		simple_ogg_page__init(&page);
		if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
			simple_ogg_page__clear(&page);
			return; /* state already set */
		}

		if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
			simple_ogg_page__clear(&page);
			return;
		}

		for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
			FLAC__uint64 xx;
			unsigned x;
			xx = encoder->private_->seek_table->points[i].sample_number;
			b[7] = (FLAC__byte)xx; xx >>= 8;
			b[6] = (FLAC__byte)xx; xx >>= 8;
			b[5] = (FLAC__byte)xx; xx >>= 8;
			b[4] = (FLAC__byte)xx; xx >>= 8;
			b[3] = (FLAC__byte)xx; xx >>= 8;
			b[2] = (FLAC__byte)xx; xx >>= 8;
			b[1] = (FLAC__byte)xx; xx >>= 8;
			b[0] = (FLAC__byte)xx; xx >>= 8;
			xx = encoder->private_->seek_table->points[i].stream_offset;
			b[15] = (FLAC__byte)xx; xx >>= 8;
			b[14] = (FLAC__byte)xx; xx >>= 8;
			b[13] = (FLAC__byte)xx; xx >>= 8;
			b[12] = (FLAC__byte)xx; xx >>= 8;
			b[11] = (FLAC__byte)xx; xx >>= 8;
			b[10] = (FLAC__byte)xx; xx >>= 8;
			b[9] = (FLAC__byte)xx; xx >>= 8;
			b[8] = (FLAC__byte)xx; xx >>= 8;
			x = encoder->private_->seek_table->points[i].frame_samples;
			b[17] = (FLAC__byte)x; x >>= 8;
			b[16] = (FLAC__byte)x; x >>= 8;
			memcpy(p, b, 18);
		}

		if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
			simple_ogg_page__clear(&page);
			return; /* state already set */
		}
		simple_ogg_page__clear(&page);
	}
}
#endif

FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
{
	FLAC__uint16 crc;
	FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);

	if(encoder->protected_->do_md5 && !FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}

	if(!process_subframes_(encoder, is_fractional_block)) {
		return false;
	}

	if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}

	FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
	if(
		!FLAC__bitwriter_get_write_crc16(encoder->private_->frame, &crc) ||
		!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN)
	) {
		encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}

	if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
		return false;
	}

	encoder->private_->current_sample_number = 0;
	encoder->private_->current_frame_number++;
	encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;

	return true;
}

FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
{
	FLAC__FrameHeader frame_header;
	unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
	FLAC__bool do_independent, do_mid_side;

	if(is_fractional_block) {
		max_partition_order = 0;
	}
	else {
		max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
		max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
	}
	min_partition_order = min(min_partition_order, max_partition_order);

	frame_header.blocksize = encoder->protected_->blocksize;
	frame_header.sample_rate = encoder->protected_->sample_rate;
	frame_header.channels = encoder->protected_->channels;
	frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
	frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
	frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
	frame_header.number.frame_number = encoder->private_->current_frame_number;

	if(encoder->protected_->do_mid_side_stereo) {
		if(encoder->protected_->loose_mid_side_stereo) {
			if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
				do_independent = true;
				do_mid_side = true;
			}
			else {
				do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
				do_mid_side = !do_independent;
			}
		}
		else {
			do_independent = true;
			do_mid_side = true;
		}
	}
	else {
		do_independent = true;
		do_mid_side = false;
	}

	FLAC__ASSERT(do_independent || do_mid_side);

	if(do_independent) {
		for(channel = 0; channel < encoder->protected_->channels; channel++) {
			const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
			encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
			encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
		}
	}
	if(do_mid_side) {
		FLAC__ASSERT(encoder->protected_->channels == 2);
		for(channel = 0; channel < 2; channel++) {
			const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
			encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
			encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
		}
	}

	if(do_independent) {
		for(channel = 0; channel < encoder->protected_->channels; channel++) {
			if(!
				process_subframe_(
					encoder,
					min_partition_order,
					max_partition_order,
					&frame_header,
					encoder->private_->subframe_bps[channel],
					encoder->private_->integer_signal[channel],
					encoder->private_->subframe_workspace_ptr[channel],
					encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
					encoder->private_->residual_workspace[channel],
					encoder->private_->best_subframe+channel,
					encoder->private_->best_subframe_bits+channel
				)
			)
				return false;
		}
	}

	if(do_mid_side) {
		FLAC__ASSERT(encoder->protected_->channels == 2);

		for(channel = 0; channel < 2; channel++) {
			if(!
				process_subframe_(
					encoder,
					min_partition_order,
					max_partition_order,
					&frame_header,
					encoder->private_->subframe_bps_mid_side[channel],
					encoder->private_->integer_signal_mid_side[channel],
					encoder->private_->subframe_workspace_ptr_mid_side[channel],
					encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
					encoder->private_->residual_workspace_mid_side[channel],
					encoder->private_->best_subframe_mid_side+channel,
					encoder->private_->best_subframe_bits_mid_side+channel
				)
			)
				return false;
		}
	}

	if(do_mid_side) {
		unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
		FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
		FLAC__ChannelAssignment channel_assignment;

		FLAC__ASSERT(encoder->protected_->channels == 2);

		if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
			channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
		}
		else {
			unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
			unsigned min_bits;
			int ca;

			FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0);
			FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE   == 1);
			FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE  == 2);
			FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE	== 3);
			FLAC__ASSERT(do_independent && do_mid_side);

			bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits	 [0] + encoder->private_->best_subframe_bits	 [1];
			bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->private_->best_subframe_bits	 [0] + encoder->private_->best_subframe_bits_mid_side[1];
			bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits	 [1] + encoder->private_->best_subframe_bits_mid_side[1];
			bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];

			channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
			min_bits = bits[channel_assignment];
			for(ca = 1; ca <= 3; ca++) {
				if(bits[ca] < min_bits) {
					min_bits = bits[ca];
					channel_assignment = (FLAC__ChannelAssignment)ca;
				}
			}
		}

		frame_header.channel_assignment = channel_assignment;

		if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
			return false;
		}

		switch(channel_assignment) {
			case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
				left_subframe  = &encoder->private_->subframe_workspace	 [0][encoder->private_->best_subframe	 [0]];
				right_subframe = &encoder->private_->subframe_workspace	 [1][encoder->private_->best_subframe	 [1]];
				break;
			case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
				left_subframe  = &encoder->private_->subframe_workspace	 [0][encoder->private_->best_subframe	 [0]];
				right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
				break;
			case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
				left_subframe  = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
				right_subframe = &encoder->private_->subframe_workspace	 [1][encoder->private_->best_subframe	 [1]];
				break;
			case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
				left_subframe  = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
				right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
				break;
			default:
				FLAC__ASSERT(0);
		}

		switch(channel_assignment) {
			case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
				left_bps  = encoder->private_->subframe_bps	 [0];
				right_bps = encoder->private_->subframe_bps	 [1];
				break;
			case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
				left_bps  = encoder->private_->subframe_bps	 [0];
				right_bps = encoder->private_->subframe_bps_mid_side[1];
				break;
			case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
				left_bps  = encoder->private_->subframe_bps_mid_side[1];
				right_bps = encoder->private_->subframe_bps	 [1];
				break;
			case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
				left_bps  = encoder->private_->subframe_bps_mid_side[0];
				right_bps = encoder->private_->subframe_bps_mid_side[1];
				break;
			default:
				FLAC__ASSERT(0);
		}

		if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame))
			return false;
		if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame))
			return false;
	}
	else {
		if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
			encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
			return false;
		}

		for(channel = 0; channel < encoder->protected_->channels; channel++) {
			if(!add_subframe_(encoder, frame_header.blocksize, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
				return false;
			}
		}
	}

	if(encoder->protected_->loose_mid_side_stereo) {
		encoder->private_->loose_mid_side_stereo_frame_count++;
		if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
			encoder->private_->loose_mid_side_stereo_frame_count = 0;
	}

	encoder->private_->last_channel_assignment = frame_header.channel_assignment;

	return true;
}

FLAC__bool process_subframe_(
	FLAC__StreamEncoder *encoder,
	unsigned min_partition_order,
	unsigned max_partition_order,
	const FLAC__FrameHeader *frame_header,
	unsigned subframe_bps,
	const FLAC__int32 integer_signal[],
	FLAC__Subframe *subframe[2],
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
	FLAC__int32 *residual[2],
	unsigned *best_subframe,
	unsigned *best_bits
)
{
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
#else
	FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
#endif
#ifndef FLAC__INTEGER_ONLY_LIBRARY
	FLAC__double lpc_residual_bits_per_sample;
	FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected_->max_lpc_order might be less; some asm routines need all the space */
	FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
	unsigned min_lpc_order, max_lpc_order, lpc_order;
	unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
#endif
	unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
	unsigned rice_parameter;
	unsigned _candidate_bits, _best_bits;
	unsigned _best_subframe;
	const unsigned rice_parameter_limit = FLAC__stream_encoder_get_bits_per_sample(encoder) > 16? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;

	FLAC__ASSERT(frame_header->blocksize > 0);

	_best_subframe = 0;
	if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
		_best_bits = UINT_MAX;
	else
		_best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);

	if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
		unsigned signal_is_constant = false;
		guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
		if(
			!encoder->private_->disable_constant_subframes &&
#ifndef FLAC__INTEGER_ONLY_LIBRARY
			fixed_residual_bits_per_sample[1] == 0.0
#else
			fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
#endif
		) {
			unsigned i;
			signal_is_constant = true;
			for(i = 1; i < frame_header->blocksize; i++) {
				if(integer_signal[0] != integer_signal[i]) {
					signal_is_constant = false;
					break;
				}
			}
		}
		if(signal_is_constant) {
			_candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]);
			if(_candidate_bits < _best_bits) {
				_best_subframe = !_best_subframe;
				_best_bits = _candidate_bits;
			}
		}
		else {
			if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
				if(encoder->protected_->do_exhaustive_model_search) {
					min_fixed_order = 0;
					max_fixed_order = FLAC__MAX_FIXED_ORDER;
				}
				else {
					min_fixed_order = max_fixed_order = guess_fixed_order;
				}
				if(max_fixed_order >= frame_header->blocksize)
					max_fixed_order = frame_header->blocksize - 1;
				for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
#ifndef FLAC__INTEGER_ONLY_LIBRARY
					if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
						continue; /* don't even try */
					rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */
#else
					if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
						continue; /* don't even try */
					rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (unsigned)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */
#endif
					rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
					if(rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
						fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, rice_parameter_limit - 1);
#endif
						rice_parameter = rice_parameter_limit - 1;
					}
					_candidate_bits =
						evaluate_fixed_subframe_(
							encoder,
							integer_signal,
							residual[!_best_subframe],
							encoder->private_->abs_residual_partition_sums,
							encoder->private_->raw_bits_per_partition,
							frame_header->blocksize,
							subframe_bps,
							fixed_order,
							rice_parameter,
							rice_parameter_limit,
							min_partition_order,
							max_partition_order,
							encoder->protected_->do_escape_coding,
							encoder->protected_->rice_parameter_search_dist,
							subframe[!_best_subframe],
							partitioned_rice_contents[!_best_subframe]
						);
					if(_candidate_bits < _best_bits) {
						_best_subframe = !_best_subframe;
						_best_bits = _candidate_bits;
					}
				}
			}

#ifndef FLAC__INTEGER_ONLY_LIBRARY
			if(encoder->protected_->max_lpc_order > 0) {
				if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
					max_lpc_order = frame_header->blocksize-1;
				else
					max_lpc_order = encoder->protected_->max_lpc_order;
				if(max_lpc_order > 0) {
					unsigned a;
					for (a = 0; a < encoder->protected_->num_apodizations; a++) {
						FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
						encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
						if(autoc[0] != 0.0) {
							FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
							if(encoder->protected_->do_exhaustive_model_search) {
								min_lpc_order = 1;
							}
							else {
								const unsigned guess_lpc_order =
									FLAC__lpc_compute_best_order(
										lpc_error,
										max_lpc_order,
										frame_header->blocksize,
										subframe_bps + (
											encoder->protected_->do_qlp_coeff_prec_search?
												FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
												encoder->protected_->qlp_coeff_precision
										)
									);
								min_lpc_order = max_lpc_order = guess_lpc_order;
							}
							if(max_lpc_order >= frame_header->blocksize)
								max_lpc_order = frame_header->blocksize - 1;
							for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
								lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
								if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
									continue; /* don't even try */
								rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
								rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
								if(rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
									fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1);
#endif
									rice_parameter = rice_parameter_limit - 1;
								}
								if(encoder->protected_->do_qlp_coeff_prec_search) {
									min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
									if(subframe_bps <= 17) {
										max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
										max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
									}
									else
										max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
								}
								else {
									min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
								}
								for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
									_candidate_bits =
										evaluate_lpc_subframe_(
											encoder,
											integer_signal,
											residual[!_best_subframe],
											encoder->private_->abs_residual_partition_sums,
											encoder->private_->raw_bits_per_partition,
											encoder->private_->lp_coeff[lpc_order-1],
											frame_header->blocksize,
											subframe_bps,
											lpc_order,
											qlp_coeff_precision,
											rice_parameter,
											rice_parameter_limit,
											min_partition_order,
											max_partition_order,
											encoder->protected_->do_escape_coding,
											encoder->protected_->rice_parameter_search_dist,
											subframe[!_best_subframe],
											partitioned_rice_contents[!_best_subframe]
										);
									if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
										if(_candidate_bits < _best_bits) {
											_best_subframe = !_best_subframe;
											_best_bits = _candidate_bits;
										}
									}
								}
							}
						}
					}
				}
			}
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
		}
	}

	if(_best_bits == UINT_MAX) {
		FLAC__ASSERT(_best_subframe == 0);
		_best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
	}

	*best_subframe = _best_subframe;
	*best_bits = _best_bits;

	return true;
}

FLAC__bool add_subframe_(
	FLAC__StreamEncoder *encoder,
	unsigned blocksize,
	unsigned subframe_bps,
	const FLAC__Subframe *subframe,
	FLAC__BitWriter *frame
)
{
	switch(subframe->type) {
		case FLAC__SUBFRAME_TYPE_CONSTANT:
			if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
				encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
				return false;
			}
			break;
		case FLAC__SUBFRAME_TYPE_FIXED:
			if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
				encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
				return false;
			}
			break;
		case FLAC__SUBFRAME_TYPE_LPC:
			if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
				encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
				return false;
			}
			break;
		case FLAC__SUBFRAME_TYPE_VERBATIM:
			if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) {
				encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
				return false;
			}
			break;
		default:
			FLAC__ASSERT(0);
	}

	return true;
}

#define SPOTCHECK_ESTIMATE 0
#if SPOTCHECK_ESTIMATE
static void spotcheck_subframe_estimate_(
	FLAC__StreamEncoder *encoder,
	unsigned blocksize,
	unsigned subframe_bps,
	const FLAC__Subframe *subframe,
	unsigned estimate
)
{
	FLAC__bool ret;
	FLAC__BitWriter *frame = FLAC__bitwriter_new();
	if(frame == 0) {
		fprintf(stderr, "EST: can't allocate frame\n");
		return;
	}
	if(!FLAC__bitwriter_init(frame)) {
		fprintf(stderr, "EST: can't init frame\n");
		return;
	}
	ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
	FLAC__ASSERT(ret);
	{
		const unsigned actual = FLAC__bitwriter_get_input_bits_unconsumed(frame);
		if(estimate != actual)
			fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
	}
	FLAC__bitwriter_delete(frame);
}
#endif

unsigned evaluate_constant_subframe_(
	FLAC__StreamEncoder *encoder,
	const FLAC__int32 signal,
	unsigned blocksize,
	unsigned subframe_bps,
	FLAC__Subframe *subframe
)
{
	unsigned estimate;
	subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
	subframe->data.constant.value = signal;

	estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps;

#if SPOTCHECK_ESTIMATE
	spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
#else
	(void)encoder, (void)blocksize;
#endif

	return estimate;
}

unsigned evaluate_fixed_subframe_(
	FLAC__StreamEncoder *encoder,
	const FLAC__int32 signal[],
	FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned raw_bits_per_partition[],
	unsigned blocksize,
	unsigned subframe_bps,
	unsigned order,
	unsigned rice_parameter,
	unsigned rice_parameter_limit,
	unsigned min_partition_order,
	unsigned max_partition_order,
	FLAC__bool do_escape_coding,
	unsigned rice_parameter_search_dist,
	FLAC__Subframe *subframe,
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
)
{
	unsigned i, residual_bits, estimate;
	const unsigned residual_samples = blocksize - order;

	FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);

	subframe->type = FLAC__SUBFRAME_TYPE_FIXED;

	subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
	subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
	subframe->data.fixed.residual = residual;

	residual_bits =
		find_best_partition_order_(
			encoder->private_,
			residual,
			abs_residual_partition_sums,
			raw_bits_per_partition,
			residual_samples,
			order,
			rice_parameter,
			rice_parameter_limit,
			min_partition_order,
			max_partition_order,
			subframe_bps,
			do_escape_coding,
			rice_parameter_search_dist,
			&subframe->data.fixed.entropy_coding_method
		);

	subframe->data.fixed.order = order;
	for(i = 0; i < order; i++)
		subframe->data.fixed.warmup[i] = signal[i];

	estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps) + residual_bits;

#if SPOTCHECK_ESTIMATE
	spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
#endif

	return estimate;
}

#ifndef FLAC__INTEGER_ONLY_LIBRARY
unsigned evaluate_lpc_subframe_(
	FLAC__StreamEncoder *encoder,
	const FLAC__int32 signal[],
	FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned raw_bits_per_partition[],
	const FLAC__real lp_coeff[],
	unsigned blocksize,
	unsigned subframe_bps,
	unsigned order,
	unsigned qlp_coeff_precision,
	unsigned rice_parameter,
	unsigned rice_parameter_limit,
	unsigned min_partition_order,
	unsigned max_partition_order,
	FLAC__bool do_escape_coding,
	unsigned rice_parameter_search_dist,
	FLAC__Subframe *subframe,
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
)
{
	FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
	unsigned i, residual_bits, estimate;
	int quantization, ret;
	const unsigned residual_samples = blocksize - order;

	if(subframe_bps <= 16) {
		FLAC__ASSERT(order > 0);
		FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
		qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
	}

	ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
	if(ret != 0)
		return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */

	if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
		if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
		else
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
	else
		encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);

	subframe->type = FLAC__SUBFRAME_TYPE_LPC;

	subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
	subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
	subframe->data.lpc.residual = residual;

	residual_bits =
		find_best_partition_order_(
			encoder->private_,
			residual,
			abs_residual_partition_sums,
			raw_bits_per_partition,
			residual_samples,
			order,
			rice_parameter,
			rice_parameter_limit,
			min_partition_order,
			max_partition_order,
			subframe_bps,
			do_escape_coding,
			rice_parameter_search_dist,
			&subframe->data.lpc.entropy_coding_method
		);

	subframe->data.lpc.order = order;
	subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
	subframe->data.lpc.quantization_level = quantization;
	memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
	for(i = 0; i < order; i++)
		subframe->data.lpc.warmup[i] = signal[i];

	estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;

#if SPOTCHECK_ESTIMATE
	spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
#endif

	return estimate;
}
#endif

unsigned evaluate_verbatim_subframe_(
	FLAC__StreamEncoder *encoder,
	const FLAC__int32 signal[],
	unsigned blocksize,
	unsigned subframe_bps,
	FLAC__Subframe *subframe
)
{
	unsigned estimate;

	subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;

	subframe->data.verbatim.data = signal;

	estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);

#if SPOTCHECK_ESTIMATE
	spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
#else
	(void)encoder;
#endif

	return estimate;
}

unsigned find_best_partition_order_(
	FLAC__StreamEncoderPrivate *private_,
	const FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned raw_bits_per_partition[],
	unsigned residual_samples,
	unsigned predictor_order,
	unsigned rice_parameter,
	unsigned rice_parameter_limit,
	unsigned min_partition_order,
	unsigned max_partition_order,
	unsigned bps,
	FLAC__bool do_escape_coding,
	unsigned rice_parameter_search_dist,
	FLAC__EntropyCodingMethod *best_ecm
)
{
	unsigned residual_bits, best_residual_bits = 0;
	unsigned best_parameters_index = 0;
	unsigned best_partition_order = 0;
	const unsigned blocksize = residual_samples + predictor_order;

	max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
	min_partition_order = min(min_partition_order, max_partition_order);

	precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps);

	if(do_escape_coding)
		precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);

	{
		int partition_order;
		unsigned sum;

		for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
			if(!
				set_partitioned_rice_(
#ifdef EXACT_RICE_BITS_CALCULATION
					residual,
#endif
					abs_residual_partition_sums+sum,
					raw_bits_per_partition+sum,
					residual_samples,
					predictor_order,
					rice_parameter,
					rice_parameter_limit,
					rice_parameter_search_dist,
					(unsigned)partition_order,
					do_escape_coding,
					&private_->partitioned_rice_contents_extra[!best_parameters_index],
					&residual_bits
				)
			)
			{
				FLAC__ASSERT(best_residual_bits != 0);
				break;
			}
			sum += 1u << partition_order;
			if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
				best_residual_bits = residual_bits;
				best_parameters_index = !best_parameters_index;
				best_partition_order = partition_order;
			}
		}
	}

	best_ecm->data.partitioned_rice.order = best_partition_order;

	{
		FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents;
		unsigned partition;

		FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, max(6, best_partition_order));
		memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partition_order)));
		if(do_escape_coding)
			memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partition_order)));
		for(partition = 0; partition < (1u<<best_partition_order); partition++) {
			if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
				best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2;
				break;
			}
		}
	}

	return best_residual_bits;
}

#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
extern void precompute_partition_info_sums_32bit_asm_ia32_(
	const FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned blocksize,
	unsigned predictor_order,
	unsigned min_partition_order,
	unsigned max_partition_order
);
#endif

void precompute_partition_info_sums_(
	const FLAC__int32 residual[],
	FLAC__uint64 abs_residual_partition_sums[],
	unsigned residual_samples,
	unsigned predictor_order,
	unsigned min_partition_order,
	unsigned max_partition_order,
	unsigned bps
)
{
	const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
	unsigned partitions = 1u << max_partition_order;

	FLAC__ASSERT(default_partition_samples > predictor_order);

#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
	if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
		precompute_partition_info_sums_32bit_asm_ia32_(residual, abs_residual_partition_sums, residual_samples + predictor_order, predictor_order, min_partition_order, max_partition_order);
		return;
	}
#endif

	{
		unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order);
		if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
			FLAC__uint32 abs_residual_partition_sum;

			for(partition = residual_sample = 0; partition < partitions; partition++) {
				end += default_partition_samples;
				abs_residual_partition_sum = 0;
				for( ; residual_sample < end; residual_sample++)
					abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
				abs_residual_partition_sums[partition] = abs_residual_partition_sum;
			}
		}
		else { /* have to pessimistically use 64 bits for accumulator */
			FLAC__uint64 abs_residual_partition_sum;

			for(partition = residual_sample = 0; partition < partitions; partition++) {
				end += default_partition_samples;
				abs_residual_partition_sum = 0;
				for( ; residual_sample < end; residual_sample++)
					abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
				abs_residual_partition_sums[partition] = abs_residual_partition_sum;
			}
		}
	}

	{
		unsigned from_partition = 0, to_partition = partitions;
		int partition_order;
		for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
			unsigned i;
			partitions >>= 1;
			for(i = 0; i < partitions; i++) {
				abs_residual_partition_sums[to_partition++] =
					abs_residual_partition_sums[from_partition  ] +
					abs_residual_partition_sums[from_partition+1];
				from_partition += 2;
			}
		}
	}
}

void precompute_partition_info_escapes_(
	const FLAC__int32 residual[],
	unsigned raw_bits_per_partition[],
	unsigned residual_samples,
	unsigned predictor_order,
	unsigned min_partition_order,
	unsigned max_partition_order
)
{
	int partition_order;
	unsigned from_partition, to_partition = 0;
	const unsigned blocksize = residual_samples + predictor_order;

	for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
		FLAC__int32 r;
		FLAC__uint32 rmax;
		unsigned partition, partition_sample, partition_samples, residual_sample;
		const unsigned partitions = 1u << partition_order;
		const unsigned default_partition_samples = blocksize >> partition_order;

		FLAC__ASSERT(default_partition_samples > predictor_order);

		for(partition = residual_sample = 0; partition < partitions; partition++) {
			partition_samples = default_partition_samples;
			if(partition == 0)
				partition_samples -= predictor_order;
			rmax = 0;
			for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
				r = residual[residual_sample++];
				if(r < 0)
					rmax |= ~r;
				else
					rmax |= r;
			}
			raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1;
		}
		to_partition = partitions;
		break; /*@@@ yuck, should remove the 'for' loop instead */
	}

	for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
		unsigned m;
		unsigned i;
		const unsigned partitions = 1u << partition_order;
		for(i = 0; i < partitions; i++) {
			m = raw_bits_per_partition[from_partition];
			from_partition++;
			raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
			from_partition++;
			to_partition++;
		}
	}
}

#ifdef EXACT_RICE_BITS_CALCULATION
static FLaC__INLINE unsigned count_rice_bits_in_partition_(
	const unsigned rice_parameter,
	const unsigned partition_samples,
	const FLAC__int32 *residual
)
{
	unsigned i, partition_bits =
		FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
		(1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
	;
	for(i = 0; i < partition_samples; i++)
		partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
	return partition_bits;
}
#else
static FLaC__INLINE unsigned count_rice_bits_in_partition_(
	const unsigned rice_parameter,
	const unsigned partition_samples,
	const FLAC__uint64 abs_residual_partition_sum
)
{
	return
		FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
		(1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
		(
			rice_parameter?
				(unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
				: (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
		)
		- (partition_samples >> 1)
	;
}
#endif

FLAC__bool set_partitioned_rice_(
#ifdef EXACT_RICE_BITS_CALCULATION
	const FLAC__int32 residual[],
#endif
	const FLAC__uint64 abs_residual_partition_sums[],
	const unsigned raw_bits_per_partition[],
	const unsigned residual_samples,
	const unsigned predictor_order,
	const unsigned suggested_rice_parameter,
	const unsigned rice_parameter_limit,
	const unsigned rice_parameter_search_dist,
	const unsigned partition_order,
	const FLAC__bool search_for_escapes,
	FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
	unsigned *bits
)
{
	unsigned rice_parameter, partition_bits;
	unsigned best_partition_bits, best_rice_parameter = 0;
	unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
	unsigned *parameters, *raw_bits;
#ifdef ENABLE_RICE_PARAMETER_SEARCH
	unsigned min_rice_parameter, max_rice_parameter;
#else
	(void)rice_parameter_search_dist;
#endif

	FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
	FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);

	FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
	parameters = partitioned_rice_contents->parameters;
	raw_bits = partitioned_rice_contents->raw_bits;

	if(partition_order == 0) {
		best_partition_bits = (unsigned)(-1);
#ifdef ENABLE_RICE_PARAMETER_SEARCH
		if(rice_parameter_search_dist) {
			if(suggested_rice_parameter < rice_parameter_search_dist)
				min_rice_parameter = 0;
			else
				min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
			max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
			if(max_rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
				fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1);
#endif
				max_rice_parameter = rice_parameter_limit - 1;
			}
		}
		else
			min_rice_parameter = max_rice_parameter = suggested_rice_parameter;

		for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
#else
			rice_parameter = suggested_rice_parameter;
#endif
#ifdef EXACT_RICE_BITS_CALCULATION
			partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual);
#else
			partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]);
#endif
			if(partition_bits < best_partition_bits) {
				best_rice_parameter = rice_parameter;
				best_partition_bits = partition_bits;
			}
#ifdef ENABLE_RICE_PARAMETER_SEARCH
		}
#endif
		if(search_for_escapes) {
			partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
			if(partition_bits <= best_partition_bits) {
				raw_bits[0] = raw_bits_per_partition[0];
				best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
				best_partition_bits = partition_bits;
			}
			else
				raw_bits[0] = 0;
		}
		parameters[0] = best_rice_parameter;
		bits_ += best_partition_bits;
	}
	else {
		unsigned partition, residual_sample;
		unsigned partition_samples;
		FLAC__uint64 mean, k;
		const unsigned partitions = 1u << partition_order;
		for(partition = residual_sample = 0; partition < partitions; partition++) {
			partition_samples = (residual_samples+predictor_order) >> partition_order;
			if(partition == 0) {
				if(partition_samples <= predictor_order)
					return false;
				else
					partition_samples -= predictor_order;
			}
			mean = abs_residual_partition_sums[partition];
			for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
				;
			if(rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
				fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
#endif
				rice_parameter = rice_parameter_limit - 1;
			}

			best_partition_bits = (unsigned)(-1);
#ifdef ENABLE_RICE_PARAMETER_SEARCH
			if(rice_parameter_search_dist) {
				if(rice_parameter < rice_parameter_search_dist)
					min_rice_parameter = 0;
				else
					min_rice_parameter = rice_parameter - rice_parameter_search_dist;
				max_rice_parameter = rice_parameter + rice_parameter_search_dist;
				if(max_rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
					fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
#endif
					max_rice_parameter = rice_parameter_limit - 1;
				}
			}
			else
				min_rice_parameter = max_rice_parameter = rice_parameter;

			for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
#endif
#ifdef EXACT_RICE_BITS_CALCULATION
				partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample);
#else
				partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]);
#endif
				if(partition_bits < best_partition_bits) {
					best_rice_parameter = rice_parameter;
					best_partition_bits = partition_bits;
				}
#ifdef ENABLE_RICE_PARAMETER_SEARCH
			}
#endif
			if(search_for_escapes) {
				partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
				if(partition_bits <= best_partition_bits) {
					raw_bits[partition] = raw_bits_per_partition[partition];
					best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
					best_partition_bits = partition_bits;
				}
				else
					raw_bits[partition] = 0;
			}
			parameters[partition] = best_rice_parameter;
			bits_ += best_partition_bits;
			residual_sample += partition_samples;
		}
	}

	*bits = bits_;
	return true;
}

unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
{
	unsigned i, shift;
	FLAC__int32 x = 0;

	for(i = 0; i < samples && !(x&1); i++)
		x |= signal[i];

	if(x == 0) {
		shift = 0;
	}
	else {
		for(shift = 0; !(x&1); shift++)
			x >>= 1;
	}

	if(shift > 0) {
		for(i = 0; i < samples; i++)
			 signal[i] >>= shift;
	}

	return shift;
}

void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
{
	unsigned channel;

	for(channel = 0; channel < channels; channel++)
		memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);

	fifo->tail += wide_samples;

	FLAC__ASSERT(fifo->tail <= fifo->size);
}

void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
{
	unsigned channel;
	unsigned sample, wide_sample;
	unsigned tail = fifo->tail;

	sample = input_offset * channels;
	for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
		for(channel = 0; channel < channels; channel++)
			fifo->data[channel][tail] = input[sample++];
		tail++;
	}
	fifo->tail = tail;

	FLAC__ASSERT(fifo->tail <= fifo->size);
}

FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
	FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
	const size_t encoded_bytes = encoder->private_->verify.output.bytes;
	(void)decoder;

	if(encoder->private_->verify.needs_magic_hack) {
		FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
		*bytes = FLAC__STREAM_SYNC_LENGTH;
		memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
		encoder->private_->verify.needs_magic_hack = false;
	}
	else {
		if(encoded_bytes == 0) {
			FLAC__ASSERT(0);
			return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
		}
		else if(encoded_bytes < *bytes)
			*bytes = encoded_bytes;
		memcpy(buffer, encoder->private_->verify.output.data, *bytes);
		encoder->private_->verify.output.data += *bytes;
		encoder->private_->verify.output.bytes -= *bytes;
	}

	return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}

FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
	FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
	unsigned channel;
	const unsigned channels = frame->header.channels;
	const unsigned blocksize = frame->header.blocksize;
	const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;

	(void)decoder;

	for(channel = 0; channel < channels; channel++) {
		if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
			unsigned i, sample = 0;
			FLAC__int32 expect = 0, got = 0;

			for(i = 0; i < blocksize; i++) {
				if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
					sample = i;
					expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
					got = (FLAC__int32)buffer[channel][i];
					break;
				}
			}
			FLAC__ASSERT(i < blocksize);
			FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
			encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
			encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
			encoder->private_->verify.error_stats.channel = channel;
			encoder->private_->verify.error_stats.sample = sample;
			encoder->private_->verify.error_stats.expected = expect;
			encoder->private_->verify.error_stats.got = got;
			encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
		}
	}
	encoder->private_->verify.input_fifo.tail -= blocksize;
	FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
	for(channel = 0; channel < channels; channel++)
		memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail * sizeof(encoder->private_->verify.input_fifo.data[0][0]));
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
	(void)decoder, (void)metadata, (void)client_data;
}

void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
	FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
	(void)decoder, (void)status;
	encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
}

FLAC__StreamEncoderReadStatus file_read_callback_enc(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
	(void)client_data;

	*bytes = fread(buffer, 1, *bytes, encoder->private_->file);
	if (*bytes == 0) {
		if (feof(encoder->private_->file))
			return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
		else if (ferror(encoder->private_->file))
			return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
	}
	return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
}

FLAC__StreamEncoderSeekStatus file_seek_callback_enc(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
{
	(void)client_data;

	if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
		return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
	else
		return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
}

FLAC__StreamEncoderTellStatus file_tell_callback_enc(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
{
	off_t offset;

	(void)client_data;

	offset = ftello(encoder->private_->file);

	if(offset < 0) {
		return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
	}
	else {
		*absolute_byte_offset = (FLAC__uint64)offset;
		return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
	}
}

#ifdef FLAC__VALGRIND_TESTING
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
	size_t ret = fwrite(ptr, size, nmemb, stream);
	if(!ferror(stream))
		fflush(stream);
	return ret;
}
#else
#define local__fwrite fwrite
#endif

FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
{
	(void)client_data, (void)current_frame;

	if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
		FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
#if FLAC__HAS_OGG
			encoder->private_->is_ogg? true :
#endif
			samples > 0
		);
		if(call_it) {
			encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data);
		}
		return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
	}
	else
		return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
}

FILE *get_binary_stdout_(void)
{
#if defined _MSC_VER || defined __MINGW32__
	_setmode(_fileno(stdout), _O_BINARY);
#elif defined __CYGWIN__
	setmode(_fileno(stdout), _O_BINARY);
#elif defined __EMX__
	setmode(fileno(stdout), O_BINARY);
#endif

	return stdout;
}

#endif
/*** End of inlined file: stream_encoder.c ***/


/*** Start of inlined file: stream_encoder_framing.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <string.h> /* for strlen() */

#ifdef max
#undef max
#endif
#define max(x,y) ((x)>(y)?(x):(y))

static FLAC__bool add_entropy_coding_method_(FLAC__BitWriter *bw, const FLAC__EntropyCodingMethod *method);
static FLAC__bool add_residual_partitioned_rice_(FLAC__BitWriter *bw, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order, const FLAC__bool is_extended);

FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitWriter *bw)
{
	unsigned i, j;
	const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);

	if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
		return false;

	if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
		return false;

	i = metadata->length;
	if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
		FLAC__ASSERT(metadata->data.vorbis_comment.vendor_string.length == 0 || 0 != metadata->data.vorbis_comment.vendor_string.entry);
		i -= metadata->data.vorbis_comment.vendor_string.length;
		i += vendor_string_length;
	}
	FLAC__ASSERT(i < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
	if(!FLAC__bitwriter_write_raw_uint32(bw, i, FLAC__STREAM_METADATA_LENGTH_LEN))
		return false;

	switch(metadata->type) {
		case FLAC__METADATA_TYPE_STREAMINFO:
			FLAC__ASSERT(metadata->data.stream_info.min_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.max_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.min_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_framesize, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.max_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_framesize, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
				return false;
			FLAC__ASSERT(FLAC__format_sample_rate_is_valid(metadata->data.stream_info.sample_rate));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.sample_rate, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.channels > 0);
			FLAC__ASSERT(metadata->data.stream_info.channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.channels-1, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.bits_per_sample > 0);
			FLAC__ASSERT(metadata->data.stream_info.bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.bits_per_sample-1, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
				return false;
			if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
				return false;
			if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.stream_info.md5sum, 16))
				return false;
			break;
		case FLAC__METADATA_TYPE_PADDING:
			if(!FLAC__bitwriter_write_zeroes(bw, metadata->length * 8))
				return false;
			break;
		case FLAC__METADATA_TYPE_APPLICATION:
			if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8))
				return false;
			if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.data, metadata->length - (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)))
				return false;
			break;
		case FLAC__METADATA_TYPE_SEEKTABLE:
			for(i = 0; i < metadata->data.seek_table.num_points; i++) {
				if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].sample_number, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
					return false;
			}
			break;
		case FLAC__METADATA_TYPE_VORBIS_COMMENT:
			if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, vendor_string_length))
				return false;
			if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)FLAC__VENDOR_STRING, vendor_string_length))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.num_comments))
				return false;
			for(i = 0; i < metadata->data.vorbis_comment.num_comments; i++) {
				if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.comments[i].length))
					return false;
				if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.vorbis_comment.comments[i].entry, metadata->data.vorbis_comment.comments[i].length))
					return false;
			}
			break;
		case FLAC__METADATA_TYPE_CUESHEET:
			FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
			if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.cue_sheet.media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
				return false;
			if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.cue_sheet.lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.is_cd? 1 : 0, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
				return false;
			if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.num_tracks, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
				return false;
			for(i = 0; i < metadata->data.cue_sheet.num_tracks; i++) {
				const FLAC__StreamMetadata_CueSheet_Track *track = metadata->data.cue_sheet.tracks + i;

				if(!FLAC__bitwriter_write_raw_uint64(bw, track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, track->number, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
					return false;
				FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
				if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, track->type, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, track->pre_emphasis, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
					return false;
				if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, track->num_indices, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
					return false;
				for(j = 0; j < track->num_indices; j++) {
					const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j;

					if(!FLAC__bitwriter_write_raw_uint64(bw, index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
						return false;
					if(!FLAC__bitwriter_write_raw_uint32(bw, index->number, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
						return false;
					if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
						return false;
				}
			}
			break;
		case FLAC__METADATA_TYPE_PICTURE:
			{
				size_t len;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
					return false;
				len = strlen(metadata->data.picture.mime_type);
				if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.picture.mime_type, len))
					return false;
				len = strlen((const char *)metadata->data.picture.description);
				if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.description, len))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.data_length, FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.data, metadata->data.picture.data_length))
					return false;
			}
			break;
		default:
			if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.unknown.data, metadata->length))
				return false;
			break;
	}

	FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));
	return true;
}

FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitWriter *bw)
{
	unsigned u, blocksize_hint, sample_rate_hint;
	FLAC__byte crc;

	FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));

	if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
		return false;

	if(!FLAC__bitwriter_write_raw_uint32(bw, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
		return false;

	if(!FLAC__bitwriter_write_raw_uint32(bw, (header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER)? 0 : 1, FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN))
		return false;

	FLAC__ASSERT(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
	FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535u);
	blocksize_hint = 0;
	switch(header->blocksize) {
		case   192: u = 1; break;
		case   576: u = 2; break;
		case  1152: u = 3; break;
		case  2304: u = 4; break;
		case  4608: u = 5; break;
		case   256: u = 8; break;
		case   512: u = 9; break;
		case  1024: u = 10; break;
		case  2048: u = 11; break;
		case  4096: u = 12; break;
		case  8192: u = 13; break;
		case 16384: u = 14; break;
		case 32768: u = 15; break;
		default:
			if(header->blocksize <= 0x100)
				blocksize_hint = u = 6;
			else
				blocksize_hint = u = 7;
			break;
	}
	if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
		return false;

	FLAC__ASSERT(FLAC__format_sample_rate_is_valid(header->sample_rate));
	sample_rate_hint = 0;
	switch(header->sample_rate) {
		case  88200: u = 1; break;
		case 176400: u = 2; break;
		case 192000: u = 3; break;
		case   8000: u = 4; break;
		case  16000: u = 5; break;
		case  22050: u = 6; break;
		case  24000: u = 7; break;
		case  32000: u = 8; break;
		case  44100: u = 9; break;
		case  48000: u = 10; break;
		case  96000: u = 11; break;
		default:
			if(header->sample_rate <= 255000 && header->sample_rate % 1000 == 0)
				sample_rate_hint = u = 12;
			else if(header->sample_rate % 10 == 0)
				sample_rate_hint = u = 14;
			else if(header->sample_rate <= 0xffff)
				sample_rate_hint = u = 13;
			else
				u = 0;
			break;
	}
	if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
		return false;

	FLAC__ASSERT(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
	switch(header->channel_assignment) {
		case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
			u = header->channels - 1;
			break;
		case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
			FLAC__ASSERT(header->channels == 2);
			u = 8;
			break;
		case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
			FLAC__ASSERT(header->channels == 2);
			u = 9;
			break;
		case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
			FLAC__ASSERT(header->channels == 2);
			u = 10;
			break;
		default:
			FLAC__ASSERT(0);
	}
	if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
		return false;

	FLAC__ASSERT(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
	switch(header->bits_per_sample) {
		case 8 : u = 1; break;
		case 12: u = 2; break;
		case 16: u = 4; break;
		case 20: u = 5; break;
		case 24: u = 6; break;
		default: u = 0; break;
	}
	if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
		return false;

	if(!FLAC__bitwriter_write_raw_uint32(bw, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
		return false;

	if(header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
		if(!FLAC__bitwriter_write_utf8_uint32(bw, header->number.frame_number))
			return false;
	}
	else {
		if(!FLAC__bitwriter_write_utf8_uint64(bw, header->number.sample_number))
			return false;
	}

	if(blocksize_hint)
		if(!FLAC__bitwriter_write_raw_uint32(bw, header->blocksize-1, (blocksize_hint==6)? 8:16))
			return false;

	switch(sample_rate_hint) {
		case 12:
			if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate / 1000, 8))
				return false;
			break;
		case 13:
			if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate, 16))
				return false;
			break;
		case 14:
			if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate / 10, 16))
				return false;
			break;
	}

	if(!FLAC__bitwriter_get_write_crc8(bw, &crc))
		return false;
	if(!FLAC__bitwriter_write_raw_uint32(bw, crc, FLAC__FRAME_HEADER_CRC_LEN))
		return false;

	return true;
}

FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
{
	FLAC__bool ok;

	ok =
		FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN) &&
		(wasted_bits? FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1) : true) &&
		FLAC__bitwriter_write_raw_int32(bw, subframe->value, subframe_bps)
	;

	return ok;
}

FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
{
	unsigned i;

	if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK | (subframe->order<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
		return false;
	if(wasted_bits)
		if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
			return false;

	for(i = 0; i < subframe->order; i++)
		if(!FLAC__bitwriter_write_raw_int32(bw, subframe->warmup[i], subframe_bps))
			return false;

	if(!add_entropy_coding_method_(bw, &subframe->entropy_coding_method))
		return false;
	switch(subframe->entropy_coding_method.type) {
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
			if(!add_residual_partitioned_rice_(
				bw,
				subframe->residual,
				residual_samples,
				subframe->order,
				subframe->entropy_coding_method.data.partitioned_rice.contents->parameters,
				subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits,
				subframe->entropy_coding_method.data.partitioned_rice.order,
subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2
			))
				return false;
			break;
		default:
			FLAC__ASSERT(0);
	}

	return true;
}

FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
{
	unsigned i;

	if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK | ((subframe->order-1)<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
		return false;
	if(wasted_bits)
		if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
			return false;

	for(i = 0; i < subframe->order; i++)
		if(!FLAC__bitwriter_write_raw_int32(bw, subframe->warmup[i], subframe_bps))
			return false;

	if(!FLAC__bitwriter_write_raw_uint32(bw, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
		return false;
	if(!FLAC__bitwriter_write_raw_int32(bw, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
		return false;
	for(i = 0; i < subframe->order; i++)
		if(!FLAC__bitwriter_write_raw_int32(bw, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
			return false;

	if(!add_entropy_coding_method_(bw, &subframe->entropy_coding_method))
		return false;
	switch(subframe->entropy_coding_method.type) {
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
			if(!add_residual_partitioned_rice_(
				bw,
				subframe->residual,
				residual_samples,
				subframe->order,
				subframe->entropy_coding_method.data.partitioned_rice.contents->parameters,
				subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits,
				subframe->entropy_coding_method.data.partitioned_rice.order,
subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2
			))
				return false;
			break;
		default:
			FLAC__ASSERT(0);
	}

	return true;
}

FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
{
	unsigned i;
	const FLAC__int32 *signal = subframe->data;

	if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
		return false;
	if(wasted_bits)
		if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
			return false;

	for(i = 0; i < samples; i++)
		if(!FLAC__bitwriter_write_raw_int32(bw, signal[i], subframe_bps))
			return false;

	return true;
}

FLAC__bool add_entropy_coding_method_(FLAC__BitWriter *bw, const FLAC__EntropyCodingMethod *method)
{
	if(!FLAC__bitwriter_write_raw_uint32(bw, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
		return false;
	switch(method->type) {
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
			if(!FLAC__bitwriter_write_raw_uint32(bw, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
				return false;
			break;
		default:
			FLAC__ASSERT(0);
	}
	return true;
}

FLAC__bool add_residual_partitioned_rice_(FLAC__BitWriter *bw, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order, const FLAC__bool is_extended)
{
	const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
	const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;

	if(partition_order == 0) {
		unsigned i;

		if(raw_bits[0] == 0) {
			if(!FLAC__bitwriter_write_raw_uint32(bw, rice_parameters[0], plen))
				return false;
			if(!FLAC__bitwriter_write_rice_signed_block(bw, residual, residual_samples, rice_parameters[0]))
				return false;
		}
		else {
			FLAC__ASSERT(rice_parameters[0] == 0);
			if(!FLAC__bitwriter_write_raw_uint32(bw, pesc, plen))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32(bw, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
				return false;
			for(i = 0; i < residual_samples; i++) {
				if(!FLAC__bitwriter_write_raw_int32(bw, residual[i], raw_bits[0]))
					return false;
			}
		}
		return true;
	}
	else {
		unsigned i, j, k = 0, k_last = 0;
		unsigned partition_samples;
		const unsigned default_partition_samples = (residual_samples+predictor_order) >> partition_order;
		for(i = 0; i < (1u<<partition_order); i++) {
			partition_samples = default_partition_samples;
			if(i == 0)
				partition_samples -= predictor_order;
			k += partition_samples;
			if(raw_bits[i] == 0) {
				if(!FLAC__bitwriter_write_raw_uint32(bw, rice_parameters[i], plen))
					return false;
				if(!FLAC__bitwriter_write_rice_signed_block(bw, residual+k_last, k-k_last, rice_parameters[i]))
					return false;
			}
			else {
				if(!FLAC__bitwriter_write_raw_uint32(bw, pesc, plen))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
					return false;
				for(j = k_last; j < k; j++) {
					if(!FLAC__bitwriter_write_raw_int32(bw, residual[j], raw_bits[i]))
						return false;
				}
			}
			k_last = k;
		}
		return true;
	}
}

#endif
/*** End of inlined file: stream_encoder_framing.c ***/


/*** Start of inlined file: window_flac.c ***/

/*** Start of inlined file: juce_FlacHeader.h ***/
// This file is included at the start of each FLAC .c file, just to do a few housekeeping
// tasks..

#define VERSION "1.2.1"

#define FLAC__NO_DLL 1

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4312)
#endif

#if JUCE_MAC
 #define FLAC__SYS_DARWIN 1
#endif
/*** End of inlined file: juce_FlacHeader.h ***/

#if JUCE_USE_FLAC

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <math.h>

#ifndef FLAC__INTEGER_ONLY_LIBRARY

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	if (L & 1) {
		for (n = 0; n <= N/2; n++)
			window[n] = 2.0f * n / (float)N;
		for (; n <= N; n++)
			window[n] = 2.0f - 2.0f * n / (float)N;
	}
	else {
		for (n = 0; n <= L/2-1; n++)
			window[n] = 2.0f * n / (float)N;
		for (; n <= N; n++)
			window[n] = 2.0f - 2.0f * (N-n) / (float)N;
	}
}

void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	for (n = 0; n < L; n++)
		window[n] = (FLAC__real)(0.62f - 0.48f * fabs((float)n/(float)N+0.5f) + 0.38f * cos(2.0f * M_PI * ((float)n/(float)N+0.5f)));
}

void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	for (n = 0; n < L; n++)
		window[n] = (FLAC__real)(0.42f - 0.5f * cos(2.0f * M_PI * n / N) + 0.08f * cos(4.0f * M_PI * n / N));
}

void FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	for (n = 0; n <= N; n++)
		window[n] = (FLAC__real)(0.35875f - 0.48829f * cos(2.0f * M_PI * n / N) + 0.14128f * cos(4.0f * M_PI * n / N) - 0.01168f * cos(6.0f * M_PI * n / N));
}

void FLAC__window_connes(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	const double N2 = (double)N / 2.;
	FLAC__int32 n;

	for (n = 0; n <= N; n++) {
		double k = ((double)n - N2) / N2;
		k = 1.0f - k * k;
		window[n] = (FLAC__real)(k * k);
	}
}

void FLAC__window_flattop(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	for (n = 0; n < L; n++)
		window[n] = (FLAC__real)(1.0f - 1.93f * cos(2.0f * M_PI * n / N) + 1.29f * cos(4.0f * M_PI * n / N) - 0.388f * cos(6.0f * M_PI * n / N) + 0.0322f * cos(8.0f * M_PI * n / N));
}

void FLAC__window_gauss(FLAC__real *window, const FLAC__int32 L, const FLAC__real stddev)
{
	const FLAC__int32 N = L - 1;
	const double N2 = (double)N / 2.;
	FLAC__int32 n;

	for (n = 0; n <= N; n++) {
		const double k = ((double)n - N2) / (stddev * N2);
		window[n] = (FLAC__real)exp(-0.5f * k * k);
	}
}

void FLAC__window_hamming(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	for (n = 0; n < L; n++)
		window[n] = (FLAC__real)(0.54f - 0.46f * cos(2.0f * M_PI * n / N));
}

void FLAC__window_hann(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	for (n = 0; n < L; n++)
		window[n] = (FLAC__real)(0.5f - 0.5f * cos(2.0f * M_PI * n / N));
}

void FLAC__window_kaiser_bessel(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	for (n = 0; n < L; n++)
		window[n] = (FLAC__real)(0.402f - 0.498f * cos(2.0f * M_PI * n / N) + 0.098f * cos(4.0f * M_PI * n / N) - 0.001f * cos(6.0f * M_PI * n / N));
}

void FLAC__window_nuttall(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	FLAC__int32 n;

	for (n = 0; n < L; n++)
		window[n] = (FLAC__real)(0.3635819f - 0.4891775f*cos(2.0f*M_PI*n/N) + 0.1365995f*cos(4.0f*M_PI*n/N) - 0.0106411f*cos(6.0f*M_PI*n/N));
}

void FLAC__window_rectangle(FLAC__real *window, const FLAC__int32 L)
{
	FLAC__int32 n;

	for (n = 0; n < L; n++)
		window[n] = 1.0f;
}

void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L)
{
	FLAC__int32 n;

	if (L & 1) {
		for (n = 1; n <= L+1/2; n++)
			window[n-1] = 2.0f * n / ((float)L + 1.0f);
		for (; n <= L; n++)
			window[n-1] = - (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
	}
	else {
		for (n = 1; n <= L/2; n++)
			window[n-1] = 2.0f * n / (float)L;
		for (; n <= L; n++)
			window[n-1] = ((float)(2 * (L - n)) + 1.0f) / (float)L;
	}
}

void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p)
{
	if (p <= 0.0)
		FLAC__window_rectangle(window, L);
	else if (p >= 1.0)
		FLAC__window_hann(window, L);
	else {
		const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1;
		FLAC__int32 n;
		FLAC__window_rectangle(window, L);
		if (Np > 0) {
			for (n = 0; n <= Np; n++) {
				window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * n / Np));
				window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * (n+Np) / Np));
			}
		}
	}
}

void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L)
{
	const FLAC__int32 N = L - 1;
	const double N2 = (double)N / 2.;
	FLAC__int32 n;

	for (n = 0; n <= N; n++) {
		const double k = ((double)n - N2) / N2;
		window[n] = (FLAC__real)(1.0f - k * k);
	}
}

#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */

#endif
/*** End of inlined file: window_flac.c ***/

#else
 #include <FLAC/all.h>
#endif
}

#undef max
#undef min

#ifdef _MSC_VER
  #pragma warning (pop)
#endif

BEGIN_JUCE_NAMESPACE

static const char* const flacFormatName = "FLAC file";
static const juce_wchar* const flacExtensions[] = { T(".flac"), 0 };

class FlacReader  : public AudioFormatReader
{
public:

	FlacReader (InputStream* const in)
		: AudioFormatReader (in, TRANS (flacFormatName)),
		  reservoir (2, 0),
		  reservoirStart (0),
		  samplesInReservoir (0),
		  scanningForLength (false)
	{
		using namespace FlacNamespace;
		lengthInSamples = 0;

		decoder = FLAC__stream_decoder_new();

		ok = FLAC__stream_decoder_init_stream (decoder,
											   readCallback_, seekCallback_, tellCallback_, lengthCallback_,
											   eofCallback_, writeCallback_, metadataCallback_, errorCallback_,
											   this) == FLAC__STREAM_DECODER_INIT_STATUS_OK;

		if (ok)
		{
			FLAC__stream_decoder_process_until_end_of_metadata (decoder);

			if (lengthInSamples == 0 && sampleRate > 0)
			{
				// the length hasn't been stored in the metadata, so we'll need to
				// work it out the length the hard way, by scanning the whole file..
				scanningForLength = true;
				FLAC__stream_decoder_process_until_end_of_stream (decoder);
				scanningForLength = false;
				const int64 tempLength = lengthInSamples;

				FLAC__stream_decoder_reset (decoder);
				FLAC__stream_decoder_process_until_end_of_metadata (decoder);
				lengthInSamples = tempLength;
			}
		}
	}

	~FlacReader()
	{
		FlacNamespace::FLAC__stream_decoder_delete (decoder);
	}

	void useMetadata (const FlacNamespace::FLAC__StreamMetadata_StreamInfo& info)
	{
		sampleRate = info.sample_rate;
		bitsPerSample = info.bits_per_sample;
		lengthInSamples = (unsigned int) info.total_samples;
		numChannels = info.channels;

		reservoir.setSize (numChannels, 2 * info.max_blocksize, false, false, true);
	}

	// returns the number of samples read
	bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
					  int64 startSampleInFile, int numSamples)
	{
		using namespace FlacNamespace;

		if (! ok)
			return false;

		while (numSamples > 0)
		{
			if (startSampleInFile >= reservoirStart
				 && startSampleInFile < reservoirStart + samplesInReservoir)
			{
				const int num = (int) jmin ((int64) numSamples,
											reservoirStart + samplesInReservoir - startSampleInFile);

				jassert (num > 0);

				for (int i = jmin (numDestChannels, reservoir.getNumChannels()); --i >= 0;)
					if (destSamples[i] != 0)
						memcpy (destSamples[i] + startOffsetInDestBuffer,
								reservoir.getSampleData (i, (int) (startSampleInFile - reservoirStart)),
								sizeof (int) * num);

				startOffsetInDestBuffer += num;
				startSampleInFile += num;
				numSamples -= num;
			}
			else
			{
				if (startSampleInFile >= (int) lengthInSamples)
				{
					samplesInReservoir = 0;
				}
				else if (startSampleInFile < reservoirStart
						  || startSampleInFile > reservoirStart + jmax (samplesInReservoir, 511))
				{
					// had some problems with flac crashing if the read pos is aligned more
					// accurately than this. Probably fixed in newer versions of the library, though.
					reservoirStart = (int) (startSampleInFile & ~511);
					samplesInReservoir = 0;
					FLAC__stream_decoder_seek_absolute (decoder, (FLAC__uint64) reservoirStart);
				}
				else
				{
					reservoirStart += samplesInReservoir;
					samplesInReservoir = 0;
					FLAC__stream_decoder_process_single (decoder);
				}

				if (samplesInReservoir == 0)
					break;
			}
		}

		if (numSamples > 0)
		{
			for (int i = numDestChannels; --i >= 0;)
				if (destSamples[i] != 0)
					zeromem (destSamples[i] + startOffsetInDestBuffer,
							 sizeof (int) * numSamples);
		}

		return true;
	}

	void useSamples (const FlacNamespace::FLAC__int32* const buffer[], int numSamples)
	{
		if (scanningForLength)
		{
			lengthInSamples += numSamples;
		}
		else
		{
			if (numSamples > reservoir.getNumSamples())
				reservoir.setSize (numChannels, numSamples, false, false, true);

			const int bitsToShift = 32 - bitsPerSample;

			for (int i = 0; i < (int) numChannels; ++i)
			{
				const FlacNamespace::FLAC__int32* src = buffer[i];

				int n = i;
				while (src == 0 && n > 0)
					src = buffer [--n];

				if (src != 0)
				{
					int* dest = (int*) reservoir.getSampleData(i);

					for (int j = 0; j < numSamples; ++j)
						dest[j] = src[j] << bitsToShift;
				}
			}

			samplesInReservoir = numSamples;
		}
	}

	static FlacNamespace::FLAC__StreamDecoderReadStatus readCallback_ (const FlacNamespace::FLAC__StreamDecoder*, FlacNamespace::FLAC__byte buffer[], size_t* bytes, void* client_data)
	{
		using namespace FlacNamespace;
		*bytes = (size_t) static_cast <const FlacReader*> (client_data)->input->read (buffer, (int) *bytes);
		return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
	}

	static FlacNamespace::FLAC__StreamDecoderSeekStatus seekCallback_ (const FlacNamespace::FLAC__StreamDecoder*, FlacNamespace::FLAC__uint64 absolute_byte_offset, void* client_data)
	{
		using namespace FlacNamespace;
		static_cast <const FlacReader*> (client_data)->input->setPosition ((int) absolute_byte_offset);
		return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
	}

	static FlacNamespace::FLAC__StreamDecoderTellStatus tellCallback_ (const FlacNamespace::FLAC__StreamDecoder*, FlacNamespace::FLAC__uint64* absolute_byte_offset, void* client_data)
	{
		using namespace FlacNamespace;
		*absolute_byte_offset = static_cast <const FlacReader*> (client_data)->input->getPosition();
		return FLAC__STREAM_DECODER_TELL_STATUS_OK;
	}

	static FlacNamespace::FLAC__StreamDecoderLengthStatus lengthCallback_ (const FlacNamespace::FLAC__StreamDecoder*, FlacNamespace::FLAC__uint64* stream_length, void* client_data)
	{
		using namespace FlacNamespace;
		*stream_length = static_cast <const FlacReader*> (client_data)->input->getTotalLength();
		return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
	}

	static FlacNamespace::FLAC__bool eofCallback_ (const FlacNamespace::FLAC__StreamDecoder*, void* client_data)
	{
		return static_cast <const FlacReader*> (client_data)->input->isExhausted();
	}

	static FlacNamespace::FLAC__StreamDecoderWriteStatus writeCallback_ (const FlacNamespace::FLAC__StreamDecoder*,
																		 const FlacNamespace::FLAC__Frame* frame,
																		 const FlacNamespace::FLAC__int32* const buffer[],
																		 void* client_data)
	{
		using namespace FlacNamespace;
		static_cast <FlacReader*> (client_data)->useSamples (buffer, frame->header.blocksize);
		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
	}

	static void metadataCallback_ (const FlacNamespace::FLAC__StreamDecoder*,
								   const FlacNamespace::FLAC__StreamMetadata* metadata,
								   void* client_data)
	{
		static_cast <FlacReader*> (client_data)->useMetadata (metadata->data.stream_info);
	}

	static void errorCallback_ (const FlacNamespace::FLAC__StreamDecoder*, FlacNamespace::FLAC__StreamDecoderErrorStatus, void*)
	{
	}

	juce_UseDebuggingNewOperator

private:
	FlacNamespace::FLAC__StreamDecoder* decoder;
	AudioSampleBuffer reservoir;
	int reservoirStart, samplesInReservoir;
	bool ok, scanningForLength;

	FlacReader (const FlacReader&);
	FlacReader& operator= (const FlacReader&);
};

class FlacWriter  : public AudioFormatWriter
{
public:

	FlacWriter (OutputStream* const out,
				const double sampleRate_,
				const int numChannels_,
				const int bitsPerSample_)
		: AudioFormatWriter (out, TRANS (flacFormatName),
							 sampleRate_,
							 numChannels_,
							 bitsPerSample_)
	{
		using namespace FlacNamespace;
		encoder = FLAC__stream_encoder_new();

		FLAC__stream_encoder_set_do_mid_side_stereo (encoder, numChannels == 2);
		FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, numChannels == 2);
		FLAC__stream_encoder_set_channels (encoder, numChannels);
		FLAC__stream_encoder_set_bits_per_sample (encoder, jmin ((unsigned int) 24, bitsPerSample));
		FLAC__stream_encoder_set_sample_rate (encoder, (unsigned int) sampleRate);
		FLAC__stream_encoder_set_blocksize (encoder, 2048);
		FLAC__stream_encoder_set_do_escape_coding (encoder, true);

		ok = FLAC__stream_encoder_init_stream (encoder,
											   encodeWriteCallback, encodeSeekCallback,
											   encodeTellCallback, encodeMetadataCallback,
											   this) == FLAC__STREAM_ENCODER_INIT_STATUS_OK;
	}

	~FlacWriter()
	{
		if (ok)
		{
			FlacNamespace::FLAC__stream_encoder_finish (encoder);
			output->flush();
		}
		else
		{
			output = 0; // to stop the base class deleting this, as it needs to be returned
						// to the caller of createWriter()
		}

		FlacNamespace::FLAC__stream_encoder_delete (encoder);
	}

	bool write (const int** samplesToWrite, int numSamples)
	{
		using namespace FlacNamespace;
		if (! ok)
			return false;

		int* buf[3];
		const int bitsToShift = 32 - bitsPerSample;

		if (bitsToShift > 0)
		{
			const int numChannelsToWrite = (samplesToWrite[1] == 0) ? 1 : 2;
			temp.setSize (sizeof (int) * numSamples * numChannelsToWrite);

			buf[0] = (int*) temp.getData();
			buf[1] = buf[0] + numSamples;
			buf[2] = 0;

			for (int i = numChannelsToWrite; --i >= 0;)
			{
				if (samplesToWrite[i] != 0)
				{
					for (int j = 0; j < numSamples; ++j)
						buf [i][j] = (samplesToWrite [i][j] >> bitsToShift);
				}
			}

			samplesToWrite = (const int**) buf;
		}

		return FLAC__stream_encoder_process (encoder,
											 (const FLAC__int32**) samplesToWrite,
											 numSamples) != 0;
	}

	bool writeData (const void* const data, const int size) const
	{
		return output->write (data, size);
	}

	static void packUint32 (FlacNamespace::FLAC__uint32 val, FlacNamespace::FLAC__byte* b, const int bytes)
	{
		using namespace FlacNamespace;
		b += bytes;

		for (int i = 0; i < bytes; ++i)
		{
			*(--b) = (FLAC__byte) (val & 0xff);
			val >>= 8;
		}
	}

	void writeMetaData (const FlacNamespace::FLAC__StreamMetadata* metadata)
	{
		using namespace FlacNamespace;
		const FLAC__StreamMetadata_StreamInfo& info = metadata->data.stream_info;

		unsigned char buffer [FLAC__STREAM_METADATA_STREAMINFO_LENGTH];
		const unsigned int channelsMinus1 = info.channels - 1;
		const unsigned int bitsMinus1 = info.bits_per_sample - 1;

		packUint32 (info.min_blocksize, buffer, 2);
		packUint32 (info.max_blocksize, buffer + 2, 2);
		packUint32 (info.min_framesize, buffer + 4, 3);
		packUint32 (info.max_framesize, buffer + 7, 3);
		buffer[10] = (uint8) ((info.sample_rate >> 12) & 0xff);
		buffer[11] = (uint8) ((info.sample_rate >> 4) & 0xff);
		buffer[12] = (uint8) (((info.sample_rate & 0x0f) << 4) | (channelsMinus1 << 1) | (bitsMinus1 >> 4));
		buffer[13] = (FLAC__byte) (((bitsMinus1 & 0x0f) << 4) | (unsigned int) ((info.total_samples >> 32) & 0x0f));
		packUint32 ((FLAC__uint32) info.total_samples, buffer + 14, 4);
		memcpy (buffer + 18, info.md5sum, 16);

		const bool seekOk = output->setPosition (4);
		(void) seekOk;

		// if this fails, you've given it an output stream that can't seek! It needs
		// to be able to seek back to write the header
		jassert (seekOk);

		output->writeIntBigEndian (FLAC__STREAM_METADATA_STREAMINFO_LENGTH);
		output->write (buffer, FLAC__STREAM_METADATA_STREAMINFO_LENGTH);
	}

	static FlacNamespace::FLAC__StreamEncoderWriteStatus encodeWriteCallback (const FlacNamespace::FLAC__StreamEncoder*,
																			  const FlacNamespace::FLAC__byte buffer[],
																			  size_t bytes,
																			  unsigned int /*samples*/,
																			  unsigned int /*current_frame*/,
																			  void* client_data)
	{
		using namespace FlacNamespace;
		return static_cast <FlacWriter*> (client_data)->writeData (buffer, (int) bytes)
				? FLAC__STREAM_ENCODER_WRITE_STATUS_OK
				: FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
	}

	static FlacNamespace::FLAC__StreamEncoderSeekStatus encodeSeekCallback (const FlacNamespace::FLAC__StreamEncoder*, FlacNamespace::FLAC__uint64, void*)
	{
		using namespace FlacNamespace;
		return FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
	}

	static FlacNamespace::FLAC__StreamEncoderTellStatus encodeTellCallback (const FlacNamespace::FLAC__StreamEncoder*, FlacNamespace::FLAC__uint64* absolute_byte_offset, void* client_data)
	{
		using namespace FlacNamespace;
		if (client_data == 0)
			return FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;

		*absolute_byte_offset = (FLAC__uint64) static_cast <FlacWriter*> (client_data)->output->getPosition();
		return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
	}

	static void encodeMetadataCallback (const FlacNamespace::FLAC__StreamEncoder*, const FlacNamespace::FLAC__StreamMetadata* metadata, void* client_data)
	{
		static_cast <FlacWriter*> (client_data)->writeMetaData (metadata);
	}

	juce_UseDebuggingNewOperator

	bool ok;

private:
	FlacNamespace::FLAC__StreamEncoder* encoder;
	MemoryBlock temp;

	FlacWriter (const FlacWriter&);
	FlacWriter& operator= (const FlacWriter&);
};

FlacAudioFormat::FlacAudioFormat()
	: AudioFormat (TRANS (flacFormatName), (const juce_wchar**) flacExtensions)
{
}

FlacAudioFormat::~FlacAudioFormat()
{
}

const Array <int> FlacAudioFormat::getPossibleSampleRates()
{
	const int rates[] = { 22050, 32000, 44100, 48000, 88200, 96000, 0 };
	return Array <int> (rates);
}

const Array <int> FlacAudioFormat::getPossibleBitDepths()
{
	const int depths[] = { 16, 24, 0 };
	return Array <int> (depths);
}

bool FlacAudioFormat::canDoStereo()
{
	return true;
}

bool FlacAudioFormat::canDoMono()
{
	return true;
}

bool FlacAudioFormat::isCompressed()
{
	return true;
}

AudioFormatReader* FlacAudioFormat::createReaderFor (InputStream* in,
													 const bool deleteStreamIfOpeningFails)
{
	ScopedPointer<FlacReader> r (new FlacReader (in));

	if (r->sampleRate != 0)
		return r.release();

	if (! deleteStreamIfOpeningFails)
		r->input = 0;

	return 0;
}

AudioFormatWriter* FlacAudioFormat::createWriterFor (OutputStream* out,
													 double sampleRate,
													 unsigned int numberOfChannels,
													 int bitsPerSample,
													 const StringPairArray& /*metadataValues*/,
													 int /*qualityOptionIndex*/)
{
	if (getPossibleBitDepths().contains (bitsPerSample))
	{
		ScopedPointer<FlacWriter> w (new FlacWriter (out, sampleRate, numberOfChannels, bitsPerSample));

		if (w->ok)
			return w.release();
	}

	return 0;
}

END_JUCE_NAMESPACE

#endif
/*** End of inlined file: juce_FlacAudioFormat.cpp ***/



/*** Start of inlined file: juce_OggVorbisAudioFormat.cpp ***/
#if JUCE_USE_OGGVORBIS

#if JUCE_MAC
  #define __MACOSX__ 1
#endif

namespace OggVorbisNamespace
{
#if JUCE_INCLUDE_OGGVORBIS_CODE

/*** Start of inlined file: vorbisenc.h ***/
#ifndef _OV_ENC_H_
#define _OV_ENC_H_

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */


/*** Start of inlined file: codec.h ***/
#ifndef _vorbis_codec_h_
#define _vorbis_codec_h_

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */


/*** Start of inlined file: ogg.h ***/
#ifndef _OGG_H
#define _OGG_H

#ifdef __cplusplus
extern "C" {
#endif


/*** Start of inlined file: os_types.h ***/
#ifndef _OS_TYPES_H
#define _OS_TYPES_H

#define _ogg_malloc  malloc
#define _ogg_calloc  calloc
#define _ogg_realloc realloc
#define _ogg_free	free

#if defined(_WIN32)

#  if defined(__CYGWIN__)
#	include <_G_config.h>
	 typedef _G_int64_t ogg_int64_t;
	 typedef _G_int32_t ogg_int32_t;
	 typedef _G_uint32_t ogg_uint32_t;
	 typedef _G_int16_t ogg_int16_t;
	 typedef _G_uint16_t ogg_uint16_t;
#  elif defined(__MINGW32__)
	 typedef short ogg_int16_t;
	 typedef unsigned short ogg_uint16_t;
	 typedef int ogg_int32_t;
	 typedef unsigned int ogg_uint32_t;
	 typedef long long ogg_int64_t;
	 typedef unsigned long long ogg_uint64_t;
#  elif defined(__MWERKS__)
	 typedef long long ogg_int64_t;
	 typedef int ogg_int32_t;
	 typedef unsigned int ogg_uint32_t;
	 typedef short ogg_int16_t;
	 typedef unsigned short ogg_uint16_t;
#  else
	 typedef __int64 ogg_int64_t;
	 typedef __int32 ogg_int32_t;
	 typedef unsigned __int32 ogg_uint32_t;
	 typedef __int16 ogg_int16_t;
	 typedef unsigned __int16 ogg_uint16_t;
#  endif

#elif defined(__MACOS__)

#  include <sys/types.h>
   typedef SInt16 ogg_int16_t;
   typedef UInt16 ogg_uint16_t;
   typedef SInt32 ogg_int32_t;
   typedef UInt32 ogg_uint32_t;
   typedef SInt64 ogg_int64_t;

#elif defined(__MACOSX__) /* MacOS X Framework build */

#  include <sys/types.h>
   typedef int16_t ogg_int16_t;
   typedef u_int16_t ogg_uint16_t;
   typedef int32_t ogg_int32_t;
   typedef u_int32_t ogg_uint32_t;
   typedef int64_t ogg_int64_t;

#elif defined(__BEOS__)

#  include <inttypes.h>
   typedef int16_t ogg_int16_t;
   typedef u_int16_t ogg_uint16_t;
   typedef int32_t ogg_int32_t;
   typedef u_int32_t ogg_uint32_t;
   typedef int64_t ogg_int64_t;

#elif defined (__EMX__)

   typedef short ogg_int16_t;
   typedef unsigned short ogg_uint16_t;
   typedef int ogg_int32_t;
   typedef unsigned int ogg_uint32_t;
   typedef long long ogg_int64_t;

#elif defined (DJGPP)

   typedef short ogg_int16_t;
   typedef int ogg_int32_t;
   typedef unsigned int ogg_uint32_t;
   typedef long long ogg_int64_t;

#elif defined(R5900)

   typedef long ogg_int64_t;
   typedef int ogg_int32_t;
   typedef unsigned ogg_uint32_t;
   typedef short ogg_int16_t;

#elif defined(__SYMBIAN32__)

   typedef signed short ogg_int16_t;
   typedef unsigned short ogg_uint16_t;
   typedef signed int ogg_int32_t;
   typedef unsigned int ogg_uint32_t;
   typedef long long int ogg_int64_t;

#else

#  include <sys/types.h>

/*** Start of inlined file: config_types.h ***/
#ifndef __CONFIG_TYPES_H__
#define __CONFIG_TYPES_H__

typedef int16_t ogg_int16_t;
typedef unsigned short ogg_uint16_t;
typedef int32_t ogg_int32_t;
typedef unsigned int ogg_uint32_t;
typedef int64_t ogg_int64_t;

#endif
/*** End of inlined file: config_types.h ***/


#endif

#endif  /* _OS_TYPES_H */
/*** End of inlined file: os_types.h ***/

typedef struct {
  long endbyte;
  int  endbit;

  unsigned char *buffer;
  unsigned char *ptr;
  long storage;
} oggpack_buffer;

typedef struct {
  unsigned char *header;
  long header_len;
  unsigned char *body;
  long body_len;
} ogg_page;

ogg_uint32_t ogg_bitreverse(ogg_uint32_t x){
  x=	((x>>16)&0x0000ffffUL) | ((x<<16)&0xffff0000UL);
  x=	((x>> 8)&0x00ff00ffUL) | ((x<< 8)&0xff00ff00UL);
  x=	((x>> 4)&0x0f0f0f0fUL) | ((x<< 4)&0xf0f0f0f0UL);
  x=	((x>> 2)&0x33333333UL) | ((x<< 2)&0xccccccccUL);
  return((x>> 1)&0x55555555UL) | ((x<< 1)&0xaaaaaaaaUL);
}

typedef struct {
  unsigned char   *body_data;	/* bytes from packet bodies */
  long	body_storage;	  /* storage elements allocated */
  long	body_fill;		 /* elements stored; fill mark */
  long	body_returned;	 /* elements of fill returned */

  int	 *lacing_vals;	  /* The values that will go to the segment table */
  ogg_int64_t *granule_vals; /* granulepos values for headers. Not compact
				this way, but it is simple coupled to the
				lacing fifo */
  long	lacing_storage;
  long	lacing_fill;
  long	lacing_packet;
  long	lacing_returned;

  unsigned char	header[282];	  /* working space for header encode */
  int		  header_fill;

  int	 e_o_s;	  /* set when we have buffered the last packet in the
							 logical bitstream */
  int	 b_o_s;	  /* set after we've written the initial page
							 of a logical bitstream */
  long	serialno;
  long	pageno;
  ogg_int64_t  packetno;	  /* sequence number for decode; the framing
							 knows where there's a hole in the data,
							 but we need coupling so that the codec
							 (which is in a seperate abstraction
							 layer) also knows about the gap */
  ogg_int64_t   granulepos;

} ogg_stream_state;

typedef struct {
  unsigned char *packet;
  long  bytes;
  long  b_o_s;
  long  e_o_s;

  ogg_int64_t  granulepos;

  ogg_int64_t  packetno;	 /* sequence number for decode; the framing
				knows where there's a hole in the data,
				but we need coupling so that the codec
				(which is in a seperate abstraction
				layer) also knows about the gap */
} ogg_packet;

typedef struct {
  unsigned char *data;
  int storage;
  int fill;
  int returned;

  int unsynced;
  int headerbytes;
  int bodybytes;
} ogg_sync_state;

extern void  oggpack_writeinit(oggpack_buffer *b);
extern void  oggpack_writetrunc(oggpack_buffer *b,long bits);
extern void  oggpack_writealign(oggpack_buffer *b);
extern void  oggpack_writecopy(oggpack_buffer *b,void *source,long bits);
extern void  oggpack_reset(oggpack_buffer *b);
extern void  oggpack_writeclear(oggpack_buffer *b);
extern void  oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes);
extern void  oggpack_write(oggpack_buffer *b,unsigned long value,int bits);
extern long  oggpack_look(oggpack_buffer *b,int bits);
extern long  oggpack_look1(oggpack_buffer *b);
extern void  oggpack_adv(oggpack_buffer *b,int bits);
extern void  oggpack_adv1(oggpack_buffer *b);
extern long  oggpack_read(oggpack_buffer *b,int bits);
extern long  oggpack_read1(oggpack_buffer *b);
extern long  oggpack_bytes(oggpack_buffer *b);
extern long  oggpack_bits(oggpack_buffer *b);
extern unsigned char *oggpack_get_buffer(oggpack_buffer *b);

extern void  oggpackB_writeinit(oggpack_buffer *b);
extern void  oggpackB_writetrunc(oggpack_buffer *b,long bits);
extern void  oggpackB_writealign(oggpack_buffer *b);
extern void  oggpackB_writecopy(oggpack_buffer *b,void *source,long bits);
extern void  oggpackB_reset(oggpack_buffer *b);
extern void  oggpackB_writeclear(oggpack_buffer *b);
extern void  oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes);
extern void  oggpackB_write(oggpack_buffer *b,unsigned long value,int bits);
extern long  oggpackB_look(oggpack_buffer *b,int bits);
extern long  oggpackB_look1(oggpack_buffer *b);
extern void  oggpackB_adv(oggpack_buffer *b,int bits);
extern void  oggpackB_adv1(oggpack_buffer *b);
extern long  oggpackB_read(oggpack_buffer *b,int bits);
extern long  oggpackB_read1(oggpack_buffer *b);
extern long  oggpackB_bytes(oggpack_buffer *b);
extern long  oggpackB_bits(oggpack_buffer *b);
extern unsigned char *oggpackB_get_buffer(oggpack_buffer *b);

extern int	  ogg_stream_packetin(ogg_stream_state *os, ogg_packet *op);
extern int	  ogg_stream_pageout(ogg_stream_state *os, ogg_page *og);
extern int	  ogg_stream_flush(ogg_stream_state *os, ogg_page *og);

extern int	  ogg_sync_init(ogg_sync_state *oy);
extern int	  ogg_sync_clear(ogg_sync_state *oy);
extern int	  ogg_sync_reset(ogg_sync_state *oy);
extern int	ogg_sync_destroy(ogg_sync_state *oy);

extern char	*ogg_sync_buffer(ogg_sync_state *oy, long size);
extern int	  ogg_sync_wrote(ogg_sync_state *oy, long bytes);
extern long	 ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og);
extern int	  ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og);
extern int	  ogg_stream_pagein(ogg_stream_state *os, ogg_page *og);
extern int	  ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op);
extern int	  ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op);

extern int	  ogg_stream_init(ogg_stream_state *os,int serialno);
extern int	  ogg_stream_clear(ogg_stream_state *os);
extern int	  ogg_stream_reset(ogg_stream_state *os);
extern int	  ogg_stream_reset_serialno(ogg_stream_state *os,int serialno);
extern int	  ogg_stream_destroy(ogg_stream_state *os);
extern int	  ogg_stream_eos(ogg_stream_state *os);

extern void	 ogg_page_checksum_set(ogg_page *og);

extern int	  ogg_page_version(ogg_page *og);
extern int	  ogg_page_continued(ogg_page *og);
extern int	  ogg_page_bos(ogg_page *og);
extern int	  ogg_page_eos(ogg_page *og);
extern ogg_int64_t  ogg_page_granulepos(ogg_page *og);
extern int	  ogg_page_serialno(ogg_page *og);
extern long	 ogg_page_pageno(ogg_page *og);
extern int	  ogg_page_packets(ogg_page *og);

extern void	 ogg_packet_clear(ogg_packet *op);

#ifdef __cplusplus
}
#endif

#endif  /* _OGG_H */
/*** End of inlined file: ogg.h ***/

typedef struct vorbis_info{
  int version;
  int channels;
  long rate;

  long bitrate_upper;
  long bitrate_nominal;
  long bitrate_lower;
  long bitrate_window;

  void *codec_setup;
} vorbis_info;

typedef struct vorbis_dsp_state{
  int analysisp;
  vorbis_info *vi;

  float **pcm;
  float **pcmret;
  int	  pcm_storage;
  int	  pcm_current;
  int	  pcm_returned;

  int  preextrapolate;
  int  eofflag;

  long lW;
  long W;
  long nW;
  long centerW;

  ogg_int64_t granulepos;
  ogg_int64_t sequence;

  ogg_int64_t glue_bits;
  ogg_int64_t time_bits;
  ogg_int64_t floor_bits;
  ogg_int64_t res_bits;

  void	   *backend_state;
} vorbis_dsp_state;

typedef struct vorbis_block{
  float  **pcm;	   /* this is a pointer into local storage */
  oggpack_buffer opb;

  long  lW;
  long  W;
  long  nW;
  int   pcmend;
  int   mode;

  int	 eofflag;
  ogg_int64_t granulepos;
  ogg_int64_t sequence;
  vorbis_dsp_state *vd; /* For read-only access of configuration */

  void		   *localstore;
  long		localtop;
  long		localalloc;
  long		totaluse;
  struct alloc_chain *reap;

  long glue_bits;
  long time_bits;
  long floor_bits;
  long res_bits;

  void *internal;

} vorbis_block;

struct alloc_chain{
  void *ptr;
  struct alloc_chain *next;
};

typedef struct vorbis_comment{
  char **user_comments;
  int   *comment_lengths;
  int	comments;
  char  *vendor;

} vorbis_comment;

extern void	 vorbis_info_init(vorbis_info *vi);
extern void	 vorbis_info_clear(vorbis_info *vi);
extern int	  vorbis_info_blocksize(vorbis_info *vi,int zo);
extern void	 vorbis_comment_init(vorbis_comment *vc);
extern void	 vorbis_comment_add(vorbis_comment *vc, char *comment);
extern void	 vorbis_comment_add_tag(vorbis_comment *vc,
					   const char *tag, char *contents);
extern char	*vorbis_comment_query(vorbis_comment *vc, char *tag, int count);
extern int	  vorbis_comment_query_count(vorbis_comment *vc, char *tag);
extern void	 vorbis_comment_clear(vorbis_comment *vc);

extern int	  vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb);
extern int	  vorbis_block_clear(vorbis_block *vb);
extern void	 vorbis_dsp_clear(vorbis_dsp_state *v);
extern double   vorbis_granule_time(vorbis_dsp_state *v,
					ogg_int64_t granulepos);

extern int	  vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi);
extern int	  vorbis_commentheader_out(vorbis_comment *vc, ogg_packet *op);
extern int	  vorbis_analysis_headerout(vorbis_dsp_state *v,
					  vorbis_comment *vc,
					  ogg_packet *op,
					  ogg_packet *op_comm,
					  ogg_packet *op_code);
extern float  **vorbis_analysis_buffer(vorbis_dsp_state *v,int vals);
extern int	  vorbis_analysis_wrote(vorbis_dsp_state *v,int vals);
extern int	  vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb);
extern int	  vorbis_analysis(vorbis_block *vb,ogg_packet *op);

extern int	  vorbis_bitrate_addblock(vorbis_block *vb);
extern int	  vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,
					   ogg_packet *op);

extern int	  vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,
					  ogg_packet *op);

extern int	  vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi);
extern int	  vorbis_synthesis_restart(vorbis_dsp_state *v);
extern int	  vorbis_synthesis(vorbis_block *vb,ogg_packet *op);
extern int	  vorbis_synthesis_trackonly(vorbis_block *vb,ogg_packet *op);
extern int	  vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb);
extern int	  vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm);
extern int	  vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm);
extern int	  vorbis_synthesis_read(vorbis_dsp_state *v,int samples);
extern long	 vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op);

extern int	  vorbis_synthesis_halfrate(vorbis_info *v,int flag);
extern int	  vorbis_synthesis_halfrate_p(vorbis_info *v);

#define OV_FALSE	  -1
#define OV_EOF	-2
#define OV_HOLE	   -3

#define OV_EREAD	  -128
#define OV_EFAULT	 -129
#define OV_EIMPL	  -130
#define OV_EINVAL	 -131
#define OV_ENOTVORBIS -132
#define OV_EBADHEADER -133
#define OV_EVERSION   -134
#define OV_ENOTAUDIO  -135
#define OV_EBADPACKET -136
#define OV_EBADLINK   -137
#define OV_ENOSEEK	-138

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
/*** End of inlined file: codec.h ***/

extern int vorbis_encode_init(vorbis_info *vi,
				  long channels,
				  long rate,

				  long max_bitrate,
				  long nominal_bitrate,
				  long min_bitrate);

extern int vorbis_encode_setup_managed(vorbis_info *vi,
					   long channels,
					   long rate,

					   long max_bitrate,
					   long nominal_bitrate,
					   long min_bitrate);

extern int vorbis_encode_setup_vbr(vorbis_info *vi,
				  long channels,
				  long rate,

				  float quality /* quality level from 0. (lo) to 1. (hi) */
				  );

extern int vorbis_encode_init_vbr(vorbis_info *vi,
				  long channels,
				  long rate,

				  float base_quality /* quality level from 0. (lo) to 1. (hi) */
				  );

extern int vorbis_encode_setup_init(vorbis_info *vi);

extern int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg);

#define OV_ECTL_RATEMANAGE_GET	   0x10
#define OV_ECTL_RATEMANAGE_SET	   0x11
#define OV_ECTL_RATEMANAGE_AVG	   0x12
#define OV_ECTL_RATEMANAGE_HARD	  0x13

struct ovectl_ratemanage_arg {
  int	management_active;

  long   bitrate_hard_min;
  long   bitrate_hard_max;
  double bitrate_hard_window;

  long   bitrate_av_lo;
  long   bitrate_av_hi;
  double bitrate_av_window;
  double bitrate_av_window_center;
};

#define OV_ECTL_RATEMANAGE2_GET	  0x14
#define OV_ECTL_RATEMANAGE2_SET	  0x15

struct ovectl_ratemanage2_arg {
  int	management_active;

  long   bitrate_limit_min_kbps;
  long   bitrate_limit_max_kbps;
  long   bitrate_limit_reservoir_bits;
  double bitrate_limit_reservoir_bias;

  long   bitrate_average_kbps;
  double bitrate_average_damping;
};

#define OV_ECTL_LOWPASS_GET	  0x20
#define OV_ECTL_LOWPASS_SET	  0x21

#define OV_ECTL_IBLOCK_GET	   0x30
#define OV_ECTL_IBLOCK_SET	   0x31

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
/*** End of inlined file: vorbisenc.h ***/



/*** Start of inlined file: vorbisfile.h ***/
#ifndef _OV_FILE_H_
#define _OV_FILE_H_

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

#include <stdio.h>

typedef struct {
  size_t (*read_func)  (void *ptr, size_t size, size_t nmemb, void *datasource);
  int	(*seek_func)  (void *datasource, ogg_int64_t offset, int whence);
  int	(*close_func) (void *datasource);
  long   (*tell_func)  (void *datasource);
} ov_callbacks;

#define  NOTOPEN   0
#define  PARTOPEN  1
#define  OPENED	2
#define  STREAMSET 3
#define  INITSET   4

typedef struct OggVorbis_File {
  void		*datasource; /* Pointer to a FILE *, etc. */
  int		  seekable;
  ogg_int64_t	  offset;
  ogg_int64_t	  end;
  ogg_sync_state   oy;

  int		  links;
  ogg_int64_t	 *offsets;
  ogg_int64_t	 *dataoffsets;
  long		*serialnos;
  ogg_int64_t	 *pcmlengths; /* overloaded to maintain binary
				  compatability; x2 size, stores both
				  beginning and end values */
  vorbis_info	 *vi;
  vorbis_comment  *vc;

  ogg_int64_t	  pcm_offset;
  int		  ready_state;
  long		 current_serialno;
  int		  current_link;

  double	   bittrack;
  double	   samptrack;

  ogg_stream_state os; /* take physical pages, weld into a logical
						  stream of packets */
  vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
  vorbis_block	 vb; /* local working space for packet->PCM decode */

  ov_callbacks callbacks;

} OggVorbis_File;

extern int ov_clear(OggVorbis_File *vf);
extern int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes);
extern int ov_open_callbacks(void *datasource, OggVorbis_File *vf,
		char *initial, long ibytes, ov_callbacks callbacks);

extern int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes);
extern int ov_test_callbacks(void *datasource, OggVorbis_File *vf,
		char *initial, long ibytes, ov_callbacks callbacks);
extern int ov_test_open(OggVorbis_File *vf);

extern long ov_bitrate(OggVorbis_File *vf,int i);
extern long ov_bitrate_instant(OggVorbis_File *vf);
extern long ov_streams(OggVorbis_File *vf);
extern long ov_seekable(OggVorbis_File *vf);
extern long ov_serialnumber(OggVorbis_File *vf,int i);

extern ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i);
extern ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i);
extern double ov_time_total(OggVorbis_File *vf,int i);

extern int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos);
extern int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos);
extern int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos);
extern int ov_time_seek(OggVorbis_File *vf,double pos);
extern int ov_time_seek_page(OggVorbis_File *vf,double pos);

extern int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos);
extern int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos);
extern int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos);
extern int ov_time_seek_lap(OggVorbis_File *vf,double pos);
extern int ov_time_seek_page_lap(OggVorbis_File *vf,double pos);

extern ogg_int64_t ov_raw_tell(OggVorbis_File *vf);
extern ogg_int64_t ov_pcm_tell(OggVorbis_File *vf);
extern double ov_time_tell(OggVorbis_File *vf);

extern vorbis_info *ov_info(OggVorbis_File *vf,int link);
extern vorbis_comment *ov_comment(OggVorbis_File *vf,int link);

extern long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int samples,
			  int *bitstream);
extern long ov_read(OggVorbis_File *vf,char *buffer,int length,
			int bigendianp,int word,int sgned,int *bitstream);
extern int ov_crosslap(OggVorbis_File *vf1,OggVorbis_File *vf2);

extern int ov_halfrate(OggVorbis_File *vf,int flag);
extern int ov_halfrate_p(OggVorbis_File *vf);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
/*** End of inlined file: vorbisfile.h ***/


/*** Start of inlined file: bitwise.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <string.h>
#include <stdlib.h>

#define BUFFER_INCREMENT 256

static const unsigned long mask[]=
{0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
 0x3fffffff,0x7fffffff,0xffffffff };

static const unsigned int mask8B[]=
{0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};

void oggpack_writeinit(oggpack_buffer *b){
  memset(b,0,sizeof(*b));
  b->ptr=b->buffer=(unsigned char*) _ogg_malloc(BUFFER_INCREMENT);
  b->buffer[0]='\0';
  b->storage=BUFFER_INCREMENT;
}

void oggpackB_writeinit(oggpack_buffer *b){
  oggpack_writeinit(b);
}

void oggpack_writetrunc(oggpack_buffer *b,long bits){
  long bytes=bits>>3;
  bits-=bytes*8;
  b->ptr=b->buffer+bytes;
  b->endbit=bits;
  b->endbyte=bytes;
  *b->ptr&=mask[bits];
}

void oggpackB_writetrunc(oggpack_buffer *b,long bits){
  long bytes=bits>>3;
  bits-=bytes*8;
  b->ptr=b->buffer+bytes;
  b->endbit=bits;
  b->endbyte=bytes;
  *b->ptr&=mask8B[bits];
}

void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){
  if(b->endbyte+4>=b->storage){
	b->buffer=(unsigned char*) _ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
	b->storage+=BUFFER_INCREMENT;
	b->ptr=b->buffer+b->endbyte;
  }

  value&=mask[bits];
  bits+=b->endbit;

  b->ptr[0]|=value<<b->endbit;

  if(bits>=8){
	b->ptr[1]=(unsigned char)(value>>(8-b->endbit));
	if(bits>=16){
	  b->ptr[2]=(unsigned char)(value>>(16-b->endbit));
	  if(bits>=24){
	b->ptr[3]=(unsigned char)(value>>(24-b->endbit));
	if(bits>=32){
	  if(b->endbit)
		b->ptr[4]=(unsigned char)(value>>(32-b->endbit));
	  else
		b->ptr[4]=0;
	}
	  }
	}
  }

  b->endbyte+=bits/8;
  b->ptr+=bits/8;
  b->endbit=bits&7;
}

void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){
  if(b->endbyte+4>=b->storage){
	b->buffer=(unsigned char*) _ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
	b->storage+=BUFFER_INCREMENT;
	b->ptr=b->buffer+b->endbyte;
  }

  value=(value&mask[bits])<<(32-bits);
  bits+=b->endbit;

  b->ptr[0]|=value>>(24+b->endbit);

  if(bits>=8){
	b->ptr[1]=(unsigned char)(value>>(16+b->endbit));
	if(bits>=16){
	  b->ptr[2]=(unsigned char)(value>>(8+b->endbit));
	  if(bits>=24){
	b->ptr[3]=(unsigned char)(value>>(b->endbit));
	if(bits>=32){
	  if(b->endbit)
		b->ptr[4]=(unsigned char)(value<<(8-b->endbit));
	  else
		b->ptr[4]=0;
	}
	  }
	}
  }

  b->endbyte+=bits/8;
  b->ptr+=bits/8;
  b->endbit=bits&7;
}

void oggpack_writealign(oggpack_buffer *b){
  int bits=8-b->endbit;
  if(bits<8)
	oggpack_write(b,0,bits);
}

void oggpackB_writealign(oggpack_buffer *b){
  int bits=8-b->endbit;
  if(bits<8)
	oggpackB_write(b,0,bits);
}

static void oggpack_writecopy_helper(oggpack_buffer *b,
					 void *source,
					 long bits,
					 void (*w)(oggpack_buffer *,
						   unsigned long,
						   int),
					 int msb){
  unsigned char *ptr=(unsigned char *)source;

  long bytes=bits/8;
  bits-=bytes*8;

  if(b->endbit){
	int i;
	for(i=0;i<bytes;i++)
	  w(b,(unsigned long)(ptr[i]),8);
  }else{
	if(b->endbyte+bytes+1>=b->storage){
	  b->storage=b->endbyte+bytes+BUFFER_INCREMENT;
	  b->buffer=(unsigned char*) _ogg_realloc(b->buffer,b->storage);
	  b->ptr=b->buffer+b->endbyte;
	}

	memmove(b->ptr,source,bytes);
	b->ptr+=bytes;
	b->endbyte+=bytes;
	*b->ptr=0;

  }
  if(bits){
	if(msb)
	  w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits);
	else
	  w(b,(unsigned long)(ptr[bytes]),bits);
  }
}

void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){
  oggpack_writecopy_helper(b,source,bits,oggpack_write,0);
}

void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){
  oggpack_writecopy_helper(b,source,bits,oggpackB_write,1);
}

void oggpack_reset(oggpack_buffer *b){
  b->ptr=b->buffer;
  b->buffer[0]=0;
  b->endbit=b->endbyte=0;
}

void oggpackB_reset(oggpack_buffer *b){
  oggpack_reset(b);
}

void oggpack_writeclear(oggpack_buffer *b){
  _ogg_free(b->buffer);
  memset(b,0,sizeof(*b));
}

void oggpackB_writeclear(oggpack_buffer *b){
  oggpack_writeclear(b);
}

void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
  memset(b,0,sizeof(*b));
  b->buffer=b->ptr=buf;
  b->storage=bytes;
}

void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
  oggpack_readinit(b,buf,bytes);
}

long oggpack_look(oggpack_buffer *b,int bits){
  unsigned long ret;
  unsigned long m=mask[bits];

  bits+=b->endbit;

  if(b->endbyte+4>=b->storage){
	if(b->endbyte*8+bits>b->storage*8)return(-1);
  }

  ret=b->ptr[0]>>b->endbit;
  if(bits>8){
	ret|=b->ptr[1]<<(8-b->endbit);
	if(bits>16){
	  ret|=b->ptr[2]<<(16-b->endbit);
	  if(bits>24){
	ret|=b->ptr[3]<<(24-b->endbit);
	if(bits>32 && b->endbit)
	  ret|=b->ptr[4]<<(32-b->endbit);
	  }
	}
  }
  return(m&ret);
}

long oggpackB_look(oggpack_buffer *b,int bits){
  unsigned long ret;
  int m=32-bits;

  bits+=b->endbit;

  if(b->endbyte+4>=b->storage){
	if(b->endbyte*8+bits>b->storage*8)return(-1);
  }

  ret=b->ptr[0]<<(24+b->endbit);
  if(bits>8){
	ret|=b->ptr[1]<<(16+b->endbit);
	if(bits>16){
	  ret|=b->ptr[2]<<(8+b->endbit);
	  if(bits>24){
	ret|=b->ptr[3]<<(b->endbit);
	if(bits>32 && b->endbit)
	  ret|=b->ptr[4]>>(8-b->endbit);
	  }
	}
  }
  return ((ret&0xffffffff)>>(m>>1))>>((m+1)>>1);
}

long oggpack_look1(oggpack_buffer *b){
  if(b->endbyte>=b->storage)return(-1);
  return((b->ptr[0]>>b->endbit)&1);
}

long oggpackB_look1(oggpack_buffer *b){
  if(b->endbyte>=b->storage)return(-1);
  return((b->ptr[0]>>(7-b->endbit))&1);
}

void oggpack_adv(oggpack_buffer *b,int bits){
  bits+=b->endbit;
  b->ptr+=bits/8;
  b->endbyte+=bits/8;
  b->endbit=bits&7;
}

void oggpackB_adv(oggpack_buffer *b,int bits){
  oggpack_adv(b,bits);
}

void oggpack_adv1(oggpack_buffer *b){
  if(++(b->endbit)>7){
	b->endbit=0;
	b->ptr++;
	b->endbyte++;
  }
}

void oggpackB_adv1(oggpack_buffer *b){
  oggpack_adv1(b);
}

long oggpack_read(oggpack_buffer *b,int bits){
  long ret;
  unsigned long m=mask[bits];

  bits+=b->endbit;

  if(b->endbyte+4>=b->storage){
	ret=-1L;
	if(b->endbyte*8+bits>b->storage*8)goto overflow;
  }

  ret=b->ptr[0]>>b->endbit;
  if(bits>8){
	ret|=b->ptr[1]<<(8-b->endbit);
	if(bits>16){
	  ret|=b->ptr[2]<<(16-b->endbit);
	  if(bits>24){
	ret|=b->ptr[3]<<(24-b->endbit);
	if(bits>32 && b->endbit){
	  ret|=b->ptr[4]<<(32-b->endbit);
	}
	  }
	}
  }
  ret&=m;

 overflow:

  b->ptr+=bits/8;
  b->endbyte+=bits/8;
  b->endbit=bits&7;
  return(ret);
}

long oggpackB_read(oggpack_buffer *b,int bits){
  long ret;
  long m=32-bits;

  bits+=b->endbit;

  if(b->endbyte+4>=b->storage){
	ret=-1L;
	if(b->endbyte*8+bits>b->storage*8)goto overflow;
  }

  ret=b->ptr[0]<<(24+b->endbit);
  if(bits>8){
	ret|=b->ptr[1]<<(16+b->endbit);
	if(bits>16){
	  ret|=b->ptr[2]<<(8+b->endbit);
	  if(bits>24){
	ret|=b->ptr[3]<<(b->endbit);
	if(bits>32 && b->endbit)
	  ret|=b->ptr[4]>>(8-b->endbit);
	  }
	}
  }
  ret=((ret&0xffffffffUL)>>(m>>1))>>((m+1)>>1);

 overflow:

  b->ptr+=bits/8;
  b->endbyte+=bits/8;
  b->endbit=bits&7;
  return(ret);
}

long oggpack_read1(oggpack_buffer *b){
  long ret;

  if(b->endbyte>=b->storage){
	ret=-1L;
	goto overflow;
  }

  ret=(b->ptr[0]>>b->endbit)&1;

 overflow:

  b->endbit++;
  if(b->endbit>7){
	b->endbit=0;
	b->ptr++;
	b->endbyte++;
  }
  return(ret);
}

long oggpackB_read1(oggpack_buffer *b){
  long ret;

  if(b->endbyte>=b->storage){
	ret=-1L;
	goto overflow;
  }

  ret=(b->ptr[0]>>(7-b->endbit))&1;

 overflow:

  b->endbit++;
  if(b->endbit>7){
	b->endbit=0;
	b->ptr++;
	b->endbyte++;
  }
  return(ret);
}

long oggpack_bytes(oggpack_buffer *b){
  return(b->endbyte+(b->endbit+7)/8);
}

long oggpack_bits(oggpack_buffer *b){
  return(b->endbyte*8+b->endbit);
}

long oggpackB_bytes(oggpack_buffer *b){
  return oggpack_bytes(b);
}

long oggpackB_bits(oggpack_buffer *b){
  return oggpack_bits(b);
}

unsigned char *oggpack_get_buffer(oggpack_buffer *b){
  return(b->buffer);
}

unsigned char *oggpackB_get_buffer(oggpack_buffer *b){
  return oggpack_get_buffer(b);
}

#ifdef _V_SELFTEST
#include <stdio.h>

static int ilog(unsigned int v){
  int ret=0;
  while(v){
	ret++;
	v>>=1;
  }
  return(ret);
}

oggpack_buffer o;
oggpack_buffer r;

void report(char *in){
  fprintf(stderr,"%s",in);
  exit(1);
}

void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){
  long bytes,i;
  unsigned char *buffer;

  oggpack_reset(&o);
  for(i=0;i<vals;i++)
	oggpack_write(&o,b[i],bits?bits:ilog(b[i]));
  buffer=oggpack_get_buffer(&o);
  bytes=oggpack_bytes(&o);
  if(bytes!=compsize)report("wrong number of bytes!\n");
  for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
	for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
	report("wrote incorrect value!\n");
  }
  oggpack_readinit(&r,buffer,bytes);
  for(i=0;i<vals;i++){
	int tbit=bits?bits:ilog(b[i]);
	if(oggpack_look(&r,tbit)==-1)
	  report("out of data!\n");
	if(oggpack_look(&r,tbit)!=(b[i]&mask[tbit]))
	  report("looked at incorrect value!\n");
	if(tbit==1)
	  if(oggpack_look1(&r)!=(b[i]&mask[tbit]))
	report("looked at single bit incorrect value!\n");
	if(tbit==1){
	  if(oggpack_read1(&r)!=(b[i]&mask[tbit]))
	report("read incorrect single bit value!\n");
	}else{
	if(oggpack_read(&r,tbit)!=(b[i]&mask[tbit]))
	  report("read incorrect value!\n");
	}
  }
  if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
}

void cliptestB(unsigned long *b,int vals,int bits,int *comp,int compsize){
  long bytes,i;
  unsigned char *buffer;

  oggpackB_reset(&o);
  for(i=0;i<vals;i++)
	oggpackB_write(&o,b[i],bits?bits:ilog(b[i]));
  buffer=oggpackB_get_buffer(&o);
  bytes=oggpackB_bytes(&o);
  if(bytes!=compsize)report("wrong number of bytes!\n");
  for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
	for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
	report("wrote incorrect value!\n");
  }
  oggpackB_readinit(&r,buffer,bytes);
  for(i=0;i<vals;i++){
	int tbit=bits?bits:ilog(b[i]);
	if(oggpackB_look(&r,tbit)==-1)
	  report("out of data!\n");
	if(oggpackB_look(&r,tbit)!=(b[i]&mask[tbit]))
	  report("looked at incorrect value!\n");
	if(tbit==1)
	  if(oggpackB_look1(&r)!=(b[i]&mask[tbit]))
	report("looked at single bit incorrect value!\n");
	if(tbit==1){
	  if(oggpackB_read1(&r)!=(b[i]&mask[tbit]))
	report("read incorrect single bit value!\n");
	}else{
	if(oggpackB_read(&r,tbit)!=(b[i]&mask[tbit]))
	  report("read incorrect value!\n");
	}
  }
  if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
}

int main(void){
  unsigned char *buffer;
  long bytes,i;
  static unsigned long testbuffer1[]=
	{18,12,103948,4325,543,76,432,52,3,65,4,56,32,42,34,21,1,23,32,546,456,7,
	   567,56,8,8,55,3,52,342,341,4,265,7,67,86,2199,21,7,1,5,1,4};
  int test1size=43;

  static unsigned long testbuffer2[]=
	{216531625L,1237861823,56732452,131,3212421,12325343,34547562,12313212,
	   1233432,534,5,346435231,14436467,7869299,76326614,167548585,
	   85525151,0,12321,1,349528352};
  int test2size=21;

  static unsigned long testbuffer3[]=
	{1,0,14,0,1,0,12,0,1,0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1,0,0,1,
	   0,1,30,1,1,1,0,0,1,0,0,0,12,0,11,0,1,0,0,1};
  int test3size=56;

  static unsigned long large[]=
	{2136531625L,2137861823,56732452,131,3212421,12325343,34547562,12313212,
	   1233432,534,5,2146435231,14436467,7869299,76326614,167548585,
	   85525151,0,12321,1,2146528352};

  int onesize=33;
  static int one[33]={146,25,44,151,195,15,153,176,233,131,196,65,85,172,47,40,
					34,242,223,136,35,222,211,86,171,50,225,135,214,75,172,
					223,4};
  static int oneB[33]={150,101,131,33,203,15,204,216,105,193,156,65,84,85,222,
			   8,139,145,227,126,34,55,244,171,85,100,39,195,173,18,
			   245,251,128};

  int twosize=6;
  static int two[6]={61,255,255,251,231,29};
  static int twoB[6]={247,63,255,253,249,120};

  int threesize=54;
  static int three[54]={169,2,232,252,91,132,156,36,89,13,123,176,144,32,254,
					  142,224,85,59,121,144,79,124,23,67,90,90,216,79,23,83,
					  58,135,196,61,55,129,183,54,101,100,170,37,127,126,10,
					  100,52,4,14,18,86,77,1};
  static int threeB[54]={206,128,42,153,57,8,183,251,13,89,36,30,32,144,183,
			 130,59,240,121,59,85,223,19,228,180,134,33,107,74,98,
			 233,253,196,135,63,2,110,114,50,155,90,127,37,170,104,
			 200,20,254,4,58,106,176,144,0};

  int foursize=38;
  static int four[38]={18,6,163,252,97,194,104,131,32,1,7,82,137,42,129,11,72,
					 132,60,220,112,8,196,109,64,179,86,9,137,195,208,122,169,
					 28,2,133,0,1};
  static int fourB[38]={36,48,102,83,243,24,52,7,4,35,132,10,145,21,2,93,2,41,
			1,219,184,16,33,184,54,149,170,132,18,30,29,98,229,67,
			129,10,4,32};

  int fivesize=45;
  static int five[45]={169,2,126,139,144,172,30,4,80,72,240,59,130,218,73,62,
					 241,24,210,44,4,20,0,248,116,49,135,100,110,130,181,169,
					 84,75,159,2,1,0,132,192,8,0,0,18,22};
  static int fiveB[45]={1,84,145,111,245,100,128,8,56,36,40,71,126,78,213,226,
			124,105,12,0,133,128,0,162,233,242,67,152,77,205,77,
			172,150,169,129,79,128,0,6,4,32,0,27,9,0};

  int sixsize=7;
  static int six[7]={17,177,170,242,169,19,148};
  static int sixB[7]={136,141,85,79,149,200,41};

  oggpack_writeinit(&o);

  fprintf(stderr,"\nSmall preclipped packing (LSb): ");
  cliptest(testbuffer1,test1size,0,one,onesize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nNull bit call (LSb): ");
  cliptest(testbuffer3,test3size,0,two,twosize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nLarge preclipped packing (LSb): ");
  cliptest(testbuffer2,test2size,0,three,threesize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\n32 bit preclipped packing (LSb): ");
  oggpack_reset(&o);
  for(i=0;i<test2size;i++)
	oggpack_write(&o,large[i],32);
  buffer=oggpack_get_buffer(&o);
  bytes=oggpack_bytes(&o);
  oggpack_readinit(&r,buffer,bytes);
  for(i=0;i<test2size;i++){
	if(oggpack_look(&r,32)==-1)report("out of data. failed!");
	if(oggpack_look(&r,32)!=large[i]){
	  fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpack_look(&r,32),large[i],
		  oggpack_look(&r,32),large[i]);
	  report("read incorrect value!\n");
	}
	oggpack_adv(&r,32);
  }
  if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nSmall unclipped packing (LSb): ");
  cliptest(testbuffer1,test1size,7,four,foursize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nLarge unclipped packing (LSb): ");
  cliptest(testbuffer2,test2size,17,five,fivesize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nSingle bit unclipped packing (LSb): ");
  cliptest(testbuffer3,test3size,1,six,sixsize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nTesting read past end (LSb): ");
  oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8);
  for(i=0;i<64;i++){
	if(oggpack_read(&r,1)!=0){
	  fprintf(stderr,"failed; got -1 prematurely.\n");
	  exit(1);
	}
  }
  if(oggpack_look(&r,1)!=-1 ||
	 oggpack_read(&r,1)!=-1){
	  fprintf(stderr,"failed; read past end without -1.\n");
	  exit(1);
  }
  oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8);
  if(oggpack_read(&r,30)!=0 || oggpack_read(&r,16)!=0){
	  fprintf(stderr,"failed 2; got -1 prematurely.\n");
	  exit(1);
  }

  if(oggpack_look(&r,18)!=0 ||
	 oggpack_look(&r,18)!=0){
	fprintf(stderr,"failed 3; got -1 prematurely.\n");
	  exit(1);
  }
  if(oggpack_look(&r,19)!=-1 ||
	 oggpack_look(&r,19)!=-1){
	fprintf(stderr,"failed; read past end without -1.\n");
	  exit(1);
  }
  if(oggpack_look(&r,32)!=-1 ||
	 oggpack_look(&r,32)!=-1){
	fprintf(stderr,"failed; read past end without -1.\n");
	  exit(1);
  }
  oggpack_writeclear(&o);
  fprintf(stderr,"ok.\n");

  oggpackB_writeinit(&o);

  fprintf(stderr,"\nSmall preclipped packing (MSb): ");
  cliptestB(testbuffer1,test1size,0,oneB,onesize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nNull bit call (MSb): ");
  cliptestB(testbuffer3,test3size,0,twoB,twosize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nLarge preclipped packing (MSb): ");
  cliptestB(testbuffer2,test2size,0,threeB,threesize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\n32 bit preclipped packing (MSb): ");
  oggpackB_reset(&o);
  for(i=0;i<test2size;i++)
	oggpackB_write(&o,large[i],32);
  buffer=oggpackB_get_buffer(&o);
  bytes=oggpackB_bytes(&o);
  oggpackB_readinit(&r,buffer,bytes);
  for(i=0;i<test2size;i++){
	if(oggpackB_look(&r,32)==-1)report("out of data. failed!");
	if(oggpackB_look(&r,32)!=large[i]){
	  fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpackB_look(&r,32),large[i],
		  oggpackB_look(&r,32),large[i]);
	  report("read incorrect value!\n");
	}
	oggpackB_adv(&r,32);
  }
  if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nSmall unclipped packing (MSb): ");
  cliptestB(testbuffer1,test1size,7,fourB,foursize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nLarge unclipped packing (MSb): ");
  cliptestB(testbuffer2,test2size,17,fiveB,fivesize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nSingle bit unclipped packing (MSb): ");
  cliptestB(testbuffer3,test3size,1,sixB,sixsize);
  fprintf(stderr,"ok.");

  fprintf(stderr,"\nTesting read past end (MSb): ");
  oggpackB_readinit(&r,"\0\0\0\0\0\0\0\0",8);
  for(i=0;i<64;i++){
	if(oggpackB_read(&r,1)!=0){
	  fprintf(stderr,"failed; got -1 prematurely.\n");
	  exit(1);
	}
  }
  if(oggpackB_look(&r,1)!=-1 ||
	 oggpackB_read(&r,1)!=-1){
	  fprintf(stderr,"failed; read past end without -1.\n");
	  exit(1);
  }
  oggpackB_readinit(&r,"\0\0\0\0\0\0\0\0",8);
  if(oggpackB_read(&r,30)!=0 || oggpackB_read(&r,16)!=0){
	  fprintf(stderr,"failed 2; got -1 prematurely.\n");
	  exit(1);
  }

  if(oggpackB_look(&r,18)!=0 ||
	 oggpackB_look(&r,18)!=0){
	fprintf(stderr,"failed 3; got -1 prematurely.\n");
	  exit(1);
  }
  if(oggpackB_look(&r,19)!=-1 ||
	 oggpackB_look(&r,19)!=-1){
	fprintf(stderr,"failed; read past end without -1.\n");
	  exit(1);
  }
  if(oggpackB_look(&r,32)!=-1 ||
	 oggpackB_look(&r,32)!=-1){
	fprintf(stderr,"failed; read past end without -1.\n");
	  exit(1);
  }
  oggpackB_writeclear(&o);
  fprintf(stderr,"ok.\n\n");

  return(0);
}
#endif  /* _V_SELFTEST */

#undef BUFFER_INCREMENT

#endif
/*** End of inlined file: bitwise.c ***/


/*** Start of inlined file: framing.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>

int ogg_page_version(ogg_page *og){
  return((int)(og->header[4]));
}

int ogg_page_continued(ogg_page *og){
  return((int)(og->header[5]&0x01));
}

int ogg_page_bos(ogg_page *og){
  return((int)(og->header[5]&0x02));
}

int ogg_page_eos(ogg_page *og){
  return((int)(og->header[5]&0x04));
}

ogg_int64_t ogg_page_granulepos(ogg_page *og){
  unsigned char *page=og->header;
  ogg_int64_t granulepos=page[13]&(0xff);
  granulepos= (granulepos<<8)|(page[12]&0xff);
  granulepos= (granulepos<<8)|(page[11]&0xff);
  granulepos= (granulepos<<8)|(page[10]&0xff);
  granulepos= (granulepos<<8)|(page[9]&0xff);
  granulepos= (granulepos<<8)|(page[8]&0xff);
  granulepos= (granulepos<<8)|(page[7]&0xff);
  granulepos= (granulepos<<8)|(page[6]&0xff);
  return(granulepos);
}

int ogg_page_serialno(ogg_page *og){
  return(og->header[14] |
	 (og->header[15]<<8) |
	 (og->header[16]<<16) |
	 (og->header[17]<<24));
}

long ogg_page_pageno(ogg_page *og){
  return(og->header[18] |
	 (og->header[19]<<8) |
	 (og->header[20]<<16) |
	 (og->header[21]<<24));
}

int ogg_page_packets(ogg_page *og){
  int i,n=og->header[26],count=0;
  for(i=0;i<n;i++)
	if(og->header[27+i]<255)count++;
  return(count);
}

#if 0

static ogg_uint32_t _ogg_crc_entry(unsigned long index){
  int	   i;
  unsigned long r;

  r = index << 24;
  for (i=0; i<8; i++)
	if (r & 0x80000000UL)
	  r = (r << 1) ^ 0x04c11db7; /* The same as the ethernet generator
					polynomial, although we use an
					unreflected alg and an init/final
					of 0, not 0xffffffff */
	else
	   r<<=1;
 return (r & 0xffffffffUL);
}
#endif

static const ogg_uint32_t crc_lookup[256]={
  0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,
  0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005,
  0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,
  0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd,
  0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,
  0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75,
  0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,
  0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd,
  0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,
  0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5,
  0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,
  0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d,
  0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,
  0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95,
  0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,
  0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d,
  0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,
  0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072,
  0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,
  0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca,
  0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,
  0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02,
  0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,
  0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba,
  0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,
  0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692,
  0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,
  0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a,
  0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,
  0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2,
  0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,
  0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a,
  0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,
  0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb,
  0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,
  0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53,
  0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,
  0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b,
  0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,
  0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623,
  0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,
  0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b,
  0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,
  0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3,
  0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,
  0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b,
  0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,
  0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3,
  0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,
  0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c,
  0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,
  0x68860bfd,0x6c47164a,0x61043093,0x65c52d24,
  0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,
  0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec,
  0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,
  0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654,
  0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,
  0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c,
  0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,
  0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4,
  0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,
  0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c,
  0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,
  0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4};

int ogg_stream_init(ogg_stream_state *os,int serialno){
  if(os){
	memset(os,0,sizeof(*os));
	os->body_storage=16*1024;
	os->body_data=(unsigned char*) _ogg_malloc(os->body_storage*sizeof(*os->body_data));

	os->lacing_storage=1024;
	os->lacing_vals=(int*) _ogg_malloc(os->lacing_storage*sizeof(*os->lacing_vals));
	os->granule_vals=(ogg_int64_t*) _ogg_malloc(os->lacing_storage*sizeof(*os->granule_vals));

	os->serialno=serialno;

	return(0);
  }
  return(-1);
}

int ogg_stream_clear(ogg_stream_state *os){
  if(os){
	if(os->body_data)_ogg_free(os->body_data);
	if(os->lacing_vals)_ogg_free(os->lacing_vals);
	if(os->granule_vals)_ogg_free(os->granule_vals);

	memset(os,0,sizeof(*os));
  }
  return(0);
}

int ogg_stream_destroy(ogg_stream_state *os){
  if(os){
	ogg_stream_clear(os);
	_ogg_free(os);
  }
  return(0);
}

static void _os_body_expand(ogg_stream_state *os,int needed){
  if(os->body_storage<=os->body_fill+needed){
	os->body_storage+=(needed+1024);
	os->body_data=(unsigned char*) _ogg_realloc(os->body_data,os->body_storage*sizeof(*os->body_data));
  }
}

static void _os_lacing_expand(ogg_stream_state *os,int needed){
  if(os->lacing_storage<=os->lacing_fill+needed){
	os->lacing_storage+=(needed+32);
	os->lacing_vals=(int*)_ogg_realloc(os->lacing_vals,os->lacing_storage*sizeof(*os->lacing_vals));
	os->granule_vals=(ogg_int64_t*)_ogg_realloc(os->granule_vals,os->lacing_storage*sizeof(*os->granule_vals));
  }
}

void ogg_page_checksum_set(ogg_page *og){
  if(og){
	ogg_uint32_t crc_reg=0;
	int i;

	og->header[22]=0;
	og->header[23]=0;
	og->header[24]=0;
	og->header[25]=0;

	for(i=0;i<og->header_len;i++)
	  crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->header[i]];
	for(i=0;i<og->body_len;i++)
	  crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->body[i]];

	og->header[22]=(unsigned char)(crc_reg&0xff);
	og->header[23]=(unsigned char)((crc_reg>>8)&0xff);
	og->header[24]=(unsigned char)((crc_reg>>16)&0xff);
	og->header[25]=(unsigned char)((crc_reg>>24)&0xff);
  }
}

int ogg_stream_packetin(ogg_stream_state *os,ogg_packet *op){
  int lacing_vals=op->bytes/255+1,i;

  if(os->body_returned){

	os->body_fill-=os->body_returned;
	if(os->body_fill)
	  memmove(os->body_data,os->body_data+os->body_returned,
		  os->body_fill);
	os->body_returned=0;
  }

  _os_body_expand(os,op->bytes);
  _os_lacing_expand(os,lacing_vals);

  memcpy(os->body_data+os->body_fill,op->packet,op->bytes);
  os->body_fill+=op->bytes;

  for(i=0;i<lacing_vals-1;i++){
	os->lacing_vals[os->lacing_fill+i]=255;
	os->granule_vals[os->lacing_fill+i]=os->granulepos;
  }
  os->lacing_vals[os->lacing_fill+i]=(op->bytes)%255;
  os->granulepos=os->granule_vals[os->lacing_fill+i]=op->granulepos;

  os->lacing_vals[os->lacing_fill]|= 0x100;

  os->lacing_fill+=lacing_vals;

  os->packetno++;

  if(op->e_o_s)os->e_o_s=1;

  return(0);
}

int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){
  int i;
  int vals=0;
  int maxvals=(os->lacing_fill>255?255:os->lacing_fill);
  int bytes=0;
  long acc=0;
  ogg_int64_t granule_pos=-1;

  if(maxvals==0)return(0);

  if(os->b_o_s==0){  /* 'initial header page' case */
	granule_pos=0;
	for(vals=0;vals<maxvals;vals++){
	  if((os->lacing_vals[vals]&0x0ff)<255){
	vals++;
	break;
	  }
	}
  }else{
	for(vals=0;vals<maxvals;vals++){
	  if(acc>4096)break;
	  acc+=os->lacing_vals[vals]&0x0ff;
	  if((os->lacing_vals[vals]&0xff)<255)
		granule_pos=os->granule_vals[vals];
	}
  }

  memcpy(os->header,"OggS",4);

  os->header[4]=0x00;

  os->header[5]=0x00;
  if((os->lacing_vals[0]&0x100)==0)os->header[5]|=0x01;
  if(os->b_o_s==0)os->header[5]|=0x02;
  if(os->e_o_s && os->lacing_fill==vals)os->header[5]|=0x04;
  os->b_o_s=1;

  for(i=6;i<14;i++){
	os->header[i]=(unsigned char)(granule_pos&0xff);
	granule_pos>>=8;
  }

  {
	long serialno=os->serialno;
	for(i=14;i<18;i++){
	  os->header[i]=(unsigned char)(serialno&0xff);
	  serialno>>=8;
	}
  }

  if(os->pageno==-1)os->pageno=0; /* because someone called
					 stream_reset; this would be a
					 strange thing to do in an
					 encode stream, but it has
					 plausible uses */
  {
	long pageno=os->pageno++;
	for(i=18;i<22;i++){
	  os->header[i]=(unsigned char)(pageno&0xff);
	  pageno>>=8;
	}
  }

  os->header[22]=0;
  os->header[23]=0;
  os->header[24]=0;
  os->header[25]=0;

  os->header[26]=(unsigned char)(vals&0xff);
  for(i=0;i<vals;i++)
	bytes+=os->header[i+27]=(unsigned char)(os->lacing_vals[i]&0xff);

  og->header=os->header;
  og->header_len=os->header_fill=vals+27;
  og->body=os->body_data+os->body_returned;
  og->body_len=bytes;

  os->lacing_fill-=vals;
  memmove(os->lacing_vals,os->lacing_vals+vals,os->lacing_fill*sizeof(*os->lacing_vals));
  memmove(os->granule_vals,os->granule_vals+vals,os->lacing_fill*sizeof(*os->granule_vals));
  os->body_returned+=bytes;

  ogg_page_checksum_set(og);

  return(1);
}

int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og){

  if((os->e_o_s&&os->lacing_fill) ||	  /* 'were done, now flush' case */
	 os->body_fill-os->body_returned > 4096 ||/* 'page nominal size' case */
	 os->lacing_fill>=255 ||		  /* 'segment table full' case */
	 (os->lacing_fill&&!os->b_o_s)){	  /* 'initial header page' case */

	return(ogg_stream_flush(os,og));
  }

  return(0);
}

int ogg_stream_eos(ogg_stream_state *os){
  return os->e_o_s;
}

int ogg_sync_init(ogg_sync_state *oy){
  if(oy){
	memset(oy,0,sizeof(*oy));
  }
  return(0);
}

int ogg_sync_clear(ogg_sync_state *oy){
  if(oy){
	if(oy->data)_ogg_free(oy->data);
	ogg_sync_init(oy);
  }
  return(0);
}

int ogg_sync_destroy(ogg_sync_state *oy){
  if(oy){
	ogg_sync_clear(oy);
	_ogg_free(oy);
  }
  return(0);
}

char *ogg_sync_buffer(ogg_sync_state *oy, long size){

  if(oy->returned){
	oy->fill-=oy->returned;
	if(oy->fill>0)
	  memmove(oy->data,oy->data+oy->returned,oy->fill);
	oy->returned=0;
  }

  if(size>oy->storage-oy->fill){
	long newsize=size+oy->fill+4096; /* an extra page to be nice */

	if(oy->data)
	  oy->data=(unsigned char*) _ogg_realloc(oy->data,newsize);
	else
	  oy->data=(unsigned char*) _ogg_malloc(newsize);
	oy->storage=newsize;
  }

  return((char *)oy->data+oy->fill);
}

int ogg_sync_wrote(ogg_sync_state *oy, long bytes){
  if(oy->fill+bytes>oy->storage)return(-1);
  oy->fill+=bytes;
  return(0);
}

long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){
  unsigned char *page=oy->data+oy->returned;
  unsigned char *next;
  long bytes=oy->fill-oy->returned;

  if(oy->headerbytes==0){
	int headerbytes,i;
	if(bytes<27)return(0); /* not enough for a header */

	if(memcmp(page,"OggS",4))goto sync_fail;

	headerbytes=page[26]+27;
	if(bytes<headerbytes)return(0); /* not enough for header + seg table */

	for(i=0;i<page[26];i++)
	  oy->bodybytes+=page[27+i];
	oy->headerbytes=headerbytes;
  }

  if(oy->bodybytes+oy->headerbytes>bytes)return(0);

  {
	char chksum[4];
	ogg_page log;

	memcpy(chksum,page+22,4);
	memset(page+22,0,4);

	log.header=page;
	log.header_len=oy->headerbytes;
	log.body=page+oy->headerbytes;
	log.body_len=oy->bodybytes;
	ogg_page_checksum_set(&log);

	if(memcmp(chksum,page+22,4)){
	  memcpy(page+22,chksum,4);

	  goto sync_fail;
	}
  }

  {
	unsigned char *page=oy->data+oy->returned;
	long bytes;

	if(og){
	  og->header=page;
	  og->header_len=oy->headerbytes;
	  og->body=page+oy->headerbytes;
	  og->body_len=oy->bodybytes;
	}

	oy->unsynced=0;
	oy->returned+=(bytes=oy->headerbytes+oy->bodybytes);
	oy->headerbytes=0;
	oy->bodybytes=0;
	return(bytes);
  }

 sync_fail:

  oy->headerbytes=0;
  oy->bodybytes=0;

  next=(unsigned char*)memchr(page+1,'O',bytes-1);
  if(!next)
	next=oy->data+oy->fill;

  oy->returned=next-oy->data;
  return(-(next-page));
}

int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og){

  for(;;){
	long ret=ogg_sync_pageseek(oy,og);
	if(ret>0){
	  return(1);
	}
	if(ret==0){
	  return(0);
	}

	if(!oy->unsynced){
	  oy->unsynced=1;
	  return(-1);
	}

  }
}

int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){
  unsigned char *header=og->header;
  unsigned char *body=og->body;
  long	   bodysize=og->body_len;
  int		segptr=0;

  int version=ogg_page_version(og);
  int continued=ogg_page_continued(og);
  int bos=ogg_page_bos(og);
  int eos=ogg_page_eos(og);
  ogg_int64_t granulepos=ogg_page_granulepos(og);
  int serialno=ogg_page_serialno(og);
  long pageno=ogg_page_pageno(og);
  int segments=header[26];

  {
	long lr=os->lacing_returned;
	long br=os->body_returned;

	if(br){
	  os->body_fill-=br;
	  if(os->body_fill)
	memmove(os->body_data,os->body_data+br,os->body_fill);
	  os->body_returned=0;
	}

	if(lr){
	  if(os->lacing_fill-lr){
	memmove(os->lacing_vals,os->lacing_vals+lr,
		(os->lacing_fill-lr)*sizeof(*os->lacing_vals));
	memmove(os->granule_vals,os->granule_vals+lr,
		(os->lacing_fill-lr)*sizeof(*os->granule_vals));
	  }
	  os->lacing_fill-=lr;
	  os->lacing_packet-=lr;
	  os->lacing_returned=0;
	}
  }

  if(serialno!=os->serialno)return(-1);
  if(version>0)return(-1);

  _os_lacing_expand(os,segments+1);

  if(pageno!=os->pageno){
	int i;

	for(i=os->lacing_packet;i<os->lacing_fill;i++)
	  os->body_fill-=os->lacing_vals[i]&0xff;
	os->lacing_fill=os->lacing_packet;

	if(os->pageno!=-1){
	  os->lacing_vals[os->lacing_fill++]=0x400;
	  os->lacing_packet++;
	}
  }

  if(continued){
	if(os->lacing_fill<1 ||
	   os->lacing_vals[os->lacing_fill-1]==0x400){
	  bos=0;
	  for(;segptr<segments;segptr++){
	int val=header[27+segptr];
	body+=val;
	bodysize-=val;
	if(val<255){
	  segptr++;
	  break;
	}
	  }
	}
  }

  if(bodysize){
	_os_body_expand(os,bodysize);
	memcpy(os->body_data+os->body_fill,body,bodysize);
	os->body_fill+=bodysize;
  }

  {
	int saved=-1;
	while(segptr<segments){
	  int val=header[27+segptr];
	  os->lacing_vals[os->lacing_fill]=val;
	  os->granule_vals[os->lacing_fill]=-1;

	  if(bos){
	os->lacing_vals[os->lacing_fill]|=0x100;
	bos=0;
	  }

	  if(val<255)saved=os->lacing_fill;

	  os->lacing_fill++;
	  segptr++;

	  if(val<255)os->lacing_packet=os->lacing_fill;
	}

	if(saved!=-1){
	  os->granule_vals[saved]=granulepos;
	}

  }

  if(eos){
	os->e_o_s=1;
	if(os->lacing_fill>0)
	  os->lacing_vals[os->lacing_fill-1]|=0x200;
  }

  os->pageno=pageno+1;

  return(0);
}

int ogg_sync_reset(ogg_sync_state *oy){
  oy->fill=0;
  oy->returned=0;
  oy->unsynced=0;
  oy->headerbytes=0;
  oy->bodybytes=0;
  return(0);
}

int ogg_stream_reset(ogg_stream_state *os){
  os->body_fill=0;
  os->body_returned=0;

  os->lacing_fill=0;
  os->lacing_packet=0;
  os->lacing_returned=0;

  os->header_fill=0;

  os->e_o_s=0;
  os->b_o_s=0;
  os->pageno=-1;
  os->packetno=0;
  os->granulepos=0;

  return(0);
}

int ogg_stream_reset_serialno(ogg_stream_state *os,int serialno){
  ogg_stream_reset(os);
  os->serialno=serialno;
  return(0);
}

static int _packetout(ogg_stream_state *os,ogg_packet *op,int adv){

  int ptr=os->lacing_returned;

  if(os->lacing_packet<=ptr)return(0);

  if(os->lacing_vals[ptr]&0x400){
	os->lacing_returned++;
	os->packetno++;
	return(-1);
  }

  if(!op && !adv)return(1); /* just using peek as an inexpensive way
							   to ask if there's a whole packet
							   waiting */

  {
	int size=os->lacing_vals[ptr]&0xff;
	int bytes=size;
	int eos=os->lacing_vals[ptr]&0x200; /* last packet of the stream? */
	int bos=os->lacing_vals[ptr]&0x100; /* first packet of the stream? */

	while(size==255){
	  int val=os->lacing_vals[++ptr];
	  size=val&0xff;
	  if(val&0x200)eos=0x200;
	  bytes+=size;
	}

	if(op){
	  op->e_o_s=eos;
	  op->b_o_s=bos;
	  op->packet=os->body_data+os->body_returned;
	  op->packetno=os->packetno;
	  op->granulepos=os->granule_vals[ptr];
	  op->bytes=bytes;
	}

	if(adv){
	  os->body_returned+=bytes;
	  os->lacing_returned=ptr+1;
	  os->packetno++;
	}
  }
  return(1);
}

int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op){
  return _packetout(os,op,1);
}

int ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op){
  return _packetout(os,op,0);
}

void ogg_packet_clear(ogg_packet *op) {
  _ogg_free(op->packet);
  memset(op, 0, sizeof(*op));
}

#ifdef _V_SELFTEST
#include <stdio.h>

ogg_stream_state os_en, os_de;
ogg_sync_state oy;

void checkpacket(ogg_packet *op,int len, int no, int pos){
  long j;
  static int sequence=0;
  static int lastno=0;

  if(op->bytes!=len){
	fprintf(stderr,"incorrect packet length!\n");
	exit(1);
  }
  if(op->granulepos!=pos){
	fprintf(stderr,"incorrect packet position!\n");
	exit(1);
  }

  if(no==0){
	sequence=0;
  }else{
	sequence++;
	if(no>lastno+1)
	  sequence++;
  }
  lastno=no;
  if(op->packetno!=sequence){
	fprintf(stderr,"incorrect packet sequence %ld != %d\n",
		(long)(op->packetno),sequence);
	exit(1);
  }

  for(j=0;j<op->bytes;j++)
	if(op->packet[j]!=((j+no)&0xff)){
	  fprintf(stderr,"body data mismatch (1) at pos %ld: %x!=%lx!\n\n",
		  j,op->packet[j],(j+no)&0xff);
	  exit(1);
	}
}

void check_page(unsigned char *data,const int *header,ogg_page *og){
  long j;
  for(j=0;j<og->body_len;j++)
	if(og->body[j]!=data[j]){
	  fprintf(stderr,"body data mismatch (2) at pos %ld: %x!=%x!\n\n",
		  j,data[j],og->body[j]);
	  exit(1);
	}

  for(j=0;j<og->header_len;j++){
	if(og->header[j]!=header[j]){
	  fprintf(stderr,"header content mismatch at pos %ld:\n",j);
	  for(j=0;j<header[26]+27;j++)
	fprintf(stderr," (%ld)%02x:%02x",j,header[j],og->header[j]);
	  fprintf(stderr,"\n");
	  exit(1);
	}
  }
  if(og->header_len!=header[26]+27){
	fprintf(stderr,"header length incorrect! (%ld!=%d)\n",
		og->header_len,header[26]+27);
	exit(1);
  }
}

void print_header(ogg_page *og){
  int j;
  fprintf(stderr,"\nHEADER:\n");
  fprintf(stderr,"  capture: %c %c %c %c  version: %d  flags: %x\n",
	  og->header[0],og->header[1],og->header[2],og->header[3],
	  (int)og->header[4],(int)og->header[5]);

  fprintf(stderr,"  granulepos: %d  serialno: %d  pageno: %ld\n",
	  (og->header[9]<<24)|(og->header[8]<<16)|
	  (og->header[7]<<8)|og->header[6],
	  (og->header[17]<<24)|(og->header[16]<<16)|
	  (og->header[15]<<8)|og->header[14],
	  ((long)(og->header[21])<<24)|(og->header[20]<<16)|
	  (og->header[19]<<8)|og->header[18]);

  fprintf(stderr,"  checksum: %02x:%02x:%02x:%02x\n  segments: %d (",
	  (int)og->header[22],(int)og->header[23],
	  (int)og->header[24],(int)og->header[25],
	  (int)og->header[26]);

  for(j=27;j<og->header_len;j++)
	fprintf(stderr,"%d ",(int)og->header[j]);
  fprintf(stderr,")\n\n");
}

void copy_page(ogg_page *og){
  unsigned char *temp=_ogg_malloc(og->header_len);
  memcpy(temp,og->header,og->header_len);
  og->header=temp;

  temp=_ogg_malloc(og->body_len);
  memcpy(temp,og->body,og->body_len);
  og->body=temp;
}

void free_page(ogg_page *og){
  _ogg_free (og->header);
  _ogg_free (og->body);
}

void error(void){
  fprintf(stderr,"error!\n");
  exit(1);
}

const int head1_0[] = {0x4f,0x67,0x67,0x53,0,0x06,
			   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,0,0,0,0,
			   0x15,0xed,0xec,0x91,
			   1,
			   17};

const int head1_1[] = {0x4f,0x67,0x67,0x53,0,0x02,
			   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,0,0,0,0,
			   0x59,0x10,0x6c,0x2c,
			   1,
			   17};
const int head2_1[] = {0x4f,0x67,0x67,0x53,0,0x04,
			   0x07,0x18,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,1,0,0,0,
			   0x89,0x33,0x85,0xce,
			   13,
			   254,255,0,255,1,255,245,255,255,0,
			   255,255,90};

const int head1_2[] = {0x4f,0x67,0x67,0x53,0,0x02,
			   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,0,0,0,0,
			   0xff,0x7b,0x23,0x17,
			   1,
			   0};
const int head2_2[] = {0x4f,0x67,0x67,0x53,0,0x04,
			   0x07,0x28,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,1,0,0,0,
			   0x5c,0x3f,0x66,0xcb,
			   17,
			   17,254,255,0,0,255,1,0,255,245,255,255,0,
			   255,255,90,0};

const int head1_3[] = {0x4f,0x67,0x67,0x53,0,0x02,
			   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,0,0,0,0,
			   0x01,0x27,0x31,0xaa,
			   18,
			   255,255,255,255,255,255,255,255,
			   255,255,255,255,255,255,255,255,255,10};

const int head2_3[] = {0x4f,0x67,0x67,0x53,0,0x04,
			   0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,1,0,0,0,
			   0x7f,0x4e,0x8a,0xd2,
			   4,
			   255,4,255,0};

const int head1_4[] = {0x4f,0x67,0x67,0x53,0,0x02,
			   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,0,0,0,0,
			   0xff,0x7b,0x23,0x17,
			   1,
			   0};

const int head2_4[] = {0x4f,0x67,0x67,0x53,0,0x00,
			   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
			   0x01,0x02,0x03,0x04,1,0,0,0,
			   0x54,0x05,0x51,0xc8,
			   17,
			   255,255,255,255,255,255,255,255,
			   255,255,255,255,255,255,255,255,255};

const int head3_4[] = {0x4f,0x67,0x67,0x53,0,0x05,
			   0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,2,0,0,0,
			   0xc8,0xc3,0xcb,0xed,
			   5,
			   10,255,4,255,0};

const int head1_5[] = {0x4f,0x67,0x67,0x53,0,0x02,
			   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,0,0,0,0,
			   0xff,0x7b,0x23,0x17,
			   1,
			   0};

const int head2_5[] = {0x4f,0x67,0x67,0x53,0,0x00,
			   0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,1,0,0,0,
			   0xed,0x2a,0x2e,0xa7,
			   255,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10,10,
			   10,10,10,10,10,10,10};

const int head3_5[] = {0x4f,0x67,0x67,0x53,0,0x04,
			   0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,2,0,0,0,
			   0x6c,0x3b,0x82,0x3d,
			   1,
			   50};

const int head1_6[] = {0x4f,0x67,0x67,0x53,0,0x02,
			   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,0,0,0,0,
			   0xff,0x7b,0x23,0x17,
			   1,
			   0};

const int head2_6[] = {0x4f,0x67,0x67,0x53,0,0x00,
			   0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,1,0,0,0,
			   0x3c,0xd9,0x4d,0x3f,
			   17,
			   100,255,255,255,255,255,255,255,255,
			   255,255,255,255,255,255,255,255};

const int head3_6[] = {0x4f,0x67,0x67,0x53,0,0x01,
			   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
			   0x01,0x02,0x03,0x04,2,0,0,0,
			   0x01,0xd2,0xe5,0xe5,
			   17,
			   255,255,255,255,255,255,255,255,
			   255,255,255,255,255,255,255,255,255};

const int head4_6[] = {0x4f,0x67,0x67,0x53,0,0x05,
			   0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,3,0,0,0,
			   0xef,0xdd,0x88,0xde,
			   7,
			   255,255,75,255,4,255,0};

const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02,
			   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,0,0,0,0,
			   0xff,0x7b,0x23,0x17,
			   1,
			   0};

const int head2_7[] = {0x4f,0x67,0x67,0x53,0,0x00,
			   0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,1,0,0,0,
			   0x3c,0xd9,0x4d,0x3f,
			   17,
			   100,255,255,255,255,255,255,255,255,
			   255,255,255,255,255,255,255,255};

const int head3_7[] = {0x4f,0x67,0x67,0x53,0,0x05,
			   0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
			   0x01,0x02,0x03,0x04,2,0,0,0,
			   0xd4,0xe0,0x60,0xe5,
			   1,0};

void test_pack(const int *pl, const int **headers, int byteskip,
		   int pageskip, int packetskip){
  unsigned char *data=_ogg_malloc(1024*1024); /* for scripted test cases only */
  long inptr=0;
  long outptr=0;
  long deptr=0;
  long depacket=0;
  long granule_pos=7,pageno=0;
  int i,j,packets,pageout=pageskip;
  int eosflag=0;
  int bosflag=0;

  int byteskipcount=0;

  ogg_stream_reset(&os_en);
  ogg_stream_reset(&os_de);
  ogg_sync_reset(&oy);

  for(packets=0;packets<packetskip;packets++)
	depacket+=pl[packets];

  for(packets=0;;packets++)if(pl[packets]==-1)break;

  for(i=0;i<packets;i++){
	ogg_packet op;
	int len=pl[i];

	op.packet=data+inptr;
	op.bytes=len;
	op.e_o_s=(pl[i+1]<0?1:0);
	op.granulepos=granule_pos;

	granule_pos+=1024;

	for(j=0;j<len;j++)data[inptr++]=i+j;

	ogg_stream_packetin(&os_en,&op);

	{
	  ogg_page og;

	  while(ogg_stream_pageout(&os_en,&og)){

	fprintf(stderr,"%ld, ",pageno);

	if(headers[pageno]==NULL){
	  fprintf(stderr,"coded too many pages!\n");
	  exit(1);
	}

	check_page(data+outptr,headers[pageno],&og);

	outptr+=og.body_len;
	pageno++;
	if(pageskip){
	  bosflag=1;
	  pageskip--;
	  deptr+=og.body_len;
	}

	{
	  ogg_page og_de;
	  ogg_packet op_de,op_de2;
	  char *buf=ogg_sync_buffer(&oy,og.header_len+og.body_len);
	  char *next=buf;
	  byteskipcount+=og.header_len;
	  if(byteskipcount>byteskip){
		memcpy(next,og.header,byteskipcount-byteskip);
		next+=byteskipcount-byteskip;
		byteskipcount=byteskip;
	  }

	  byteskipcount+=og.body_len;
	  if(byteskipcount>byteskip){
		memcpy(next,og.body,byteskipcount-byteskip);
		next+=byteskipcount-byteskip;
		byteskipcount=byteskip;
	  }

	  ogg_sync_wrote(&oy,next-buf);

	  while(1){
		int ret=ogg_sync_pageout(&oy,&og_de);
		if(ret==0)break;
		if(ret<0)continue;

	    fprintf(stderr,"(%ld), ",pageout);

		check_page(data+deptr,headers[pageout],&og_de);
		deptr+=og_de.body_len;
		pageout++;

		ogg_stream_pagein(&os_de,&og_de);

		while(ogg_stream_packetpeek(&os_de,&op_de2)>0){
		  ogg_stream_packetpeek(&os_de,NULL);
		  ogg_stream_packetout(&os_de,&op_de); /* just catching them all */

		  if(memcmp(&op_de,&op_de2,sizeof(op_de))){
		fprintf(stderr,"packetout != packetpeek! pos=%ld\n",
			depacket);
		exit(1);
		  }

		  if(memcmp(data+depacket,op_de.packet,op_de.bytes)){
		fprintf(stderr,"packet data mismatch in decode! pos=%ld\n",
			depacket);
		exit(1);
		  }
		  if(bosflag==0 && op_de.b_o_s==0){
		fprintf(stderr,"b_o_s flag not set on packet!\n");
		exit(1);
		  }
		  if(bosflag && op_de.b_o_s){
		fprintf(stderr,"b_o_s flag incorrectly set on packet!\n");
		exit(1);
		  }
		  bosflag=1;
		  depacket+=op_de.bytes;

		  if(eosflag){
		fprintf(stderr,"Multiple decoded packets with eos flag!\n");
		exit(1);
		  }

		  if(op_de.e_o_s)eosflag=1;

		  if(op_de.granulepos!=-1){
		fprintf(stderr," granule:%ld ",(long)op_de.granulepos);
		  }
		}
	  }
	}
	  }
	}
  }
  _ogg_free(data);
  if(headers[pageno]!=NULL){
	fprintf(stderr,"did not write last page!\n");
	exit(1);
  }
  if(headers[pageout]!=NULL){
	fprintf(stderr,"did not decode last page!\n");
	exit(1);
  }
  if(inptr!=outptr){
	fprintf(stderr,"encoded page data incomplete!\n");
	exit(1);
  }
  if(inptr!=deptr){
	fprintf(stderr,"decoded page data incomplete!\n");
	exit(1);
  }
  if(inptr!=depacket){
	fprintf(stderr,"decoded packet data incomplete!\n");
	exit(1);
  }
  if(!eosflag){
	fprintf(stderr,"Never got a packet with EOS set!\n");
	exit(1);
  }
  fprintf(stderr,"ok.\n");
}

int main(void){

  ogg_stream_init(&os_en,0x04030201);
  ogg_stream_init(&os_de,0x04030201);
  ogg_sync_init(&oy);

  {
	const int packets[]={17, -1};
	const int *headret[]={head1_0,NULL};

	fprintf(stderr,"testing single page encoding... ");
	test_pack(packets,headret,0,0,0);
  }

  {
	const int packets[]={17, 254, 255, 256, 500, 510, 600, -1};
	const int *headret[]={head1_1,head2_1,NULL};

	fprintf(stderr,"testing basic page encoding... ");
	test_pack(packets,headret,0,0,0);
  }

  {
	const int packets[]={0,17, 254, 255, 0, 256, 0, 500, 510, 600, 0, -1};
	const int *headret[]={head1_2,head2_2,NULL};

	fprintf(stderr,"testing basic nil packets... ");
	test_pack(packets,headret,0,0,0);
  }

  {
	const int packets[]={4345,259,255,-1};
	const int *headret[]={head1_3,head2_3,NULL};

	fprintf(stderr,"testing initial-packet lacing > 4k... ");
	test_pack(packets,headret,0,0,0);
  }

  {
	const int packets[]={0,4345,259,255,-1};
	const int *headret[]={head1_4,head2_4,head3_4,NULL};

	fprintf(stderr,"testing single packet page span... ");
	test_pack(packets,headret,0,0,0);
  }

  {

	const int packets[]={0,10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,10,
		   10,10,10,10,10,10,10,50,-1};
	const int *headret[]={head1_5,head2_5,head3_5,NULL};

	fprintf(stderr,"testing max packet segments... ");
	test_pack(packets,headret,0,0,0);
  }

  {
	const int packets[]={0,100,9000,259,255,-1};
	const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL};

	fprintf(stderr,"testing very large packets... ");
	test_pack(packets,headret,0,0,0);
  }

  {
	const int packets[]={0,100,9000,259,255,-1};
	const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL};

	fprintf(stderr,"testing continuation resync in very large packets... ");
	test_pack(packets,headret,100,2,3);
  }

  {
	const int packets[]={0,100,4080,-1};
	const int *headret[]={head1_7,head2_7,head3_7,NULL};

	fprintf(stderr,"testing zero data page (1 nil packet)... ");
	test_pack(packets,headret,0,0,0);
  }

  {
	unsigned char *data=_ogg_malloc(1024*1024);
	int pl[]={0,100,4079,2956,2057,76,34,912,0,234,1000,1000,1000,300,-1};
	int inptr=0,i,j;
	ogg_page og[5];

	ogg_stream_reset(&os_en);

	for(i=0;pl[i]!=-1;i++){
	  ogg_packet op;
	  int len=pl[i];

	  op.packet=data+inptr;
	  op.bytes=len;
	  op.e_o_s=(pl[i+1]<0?1:0);
	  op.granulepos=(i+1)*1000;

	  for(j=0;j<len;j++)data[inptr++]=i+j;
	  ogg_stream_packetin(&os_en,&op);
	}

	_ogg_free(data);

	for(i=0;i<5;i++){
	  if(ogg_stream_pageout(&os_en,&og[i])==0){
	fprintf(stderr,"Too few pages output building sync tests!\n");
	exit(1);
	  }
	  copy_page(&og[i]);
	}

	{
	  ogg_page temp;
	  ogg_packet test;

	  fprintf(stderr,"Testing loss of pages... ");

	  ogg_sync_reset(&oy);
	  ogg_stream_reset(&os_de);
	  for(i=0;i<5;i++){
	memcpy(ogg_sync_buffer(&oy,og[i].header_len),og[i].header,
		   og[i].header_len);
	ogg_sync_wrote(&oy,og[i].header_len);
	memcpy(ogg_sync_buffer(&oy,og[i].body_len),og[i].body,og[i].body_len);
	ogg_sync_wrote(&oy,og[i].body_len);
	  }

	  ogg_sync_pageout(&oy,&temp);
	  ogg_stream_pagein(&os_de,&temp);
	  ogg_sync_pageout(&oy,&temp);
	  ogg_stream_pagein(&os_de,&temp);
	  ogg_sync_pageout(&oy,&temp);
	  ogg_sync_pageout(&oy,&temp);
	  ogg_stream_pagein(&os_de,&temp);

	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,0,0,0);
	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,100,1,-1);
	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,4079,2,3000);
	  if(ogg_stream_packetout(&os_de,&test)!=-1){
	fprintf(stderr,"Error: loss of page did not return error\n");
	exit(1);
	  }
	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,76,5,-1);
	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,34,6,-1);
	  fprintf(stderr,"ok.\n");
	}

	{
	  ogg_page temp;
	  ogg_packet test;

	  fprintf(stderr,"Testing loss of pages (rollback required)... ");

	  ogg_sync_reset(&oy);
	  ogg_stream_reset(&os_de);
	  for(i=0;i<5;i++){
	memcpy(ogg_sync_buffer(&oy,og[i].header_len),og[i].header,
		   og[i].header_len);
	ogg_sync_wrote(&oy,og[i].header_len);
	memcpy(ogg_sync_buffer(&oy,og[i].body_len),og[i].body,og[i].body_len);
	ogg_sync_wrote(&oy,og[i].body_len);
	  }

	  ogg_sync_pageout(&oy,&temp);
	  ogg_stream_pagein(&os_de,&temp);
	  ogg_sync_pageout(&oy,&temp);
	  ogg_stream_pagein(&os_de,&temp);
	  ogg_sync_pageout(&oy,&temp);
	  ogg_stream_pagein(&os_de,&temp);
	  ogg_sync_pageout(&oy,&temp);
	  ogg_sync_pageout(&oy,&temp);
	  ogg_stream_pagein(&os_de,&temp);

	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,0,0,0);
	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,100,1,-1);
	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,4079,2,3000);
	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,2956,3,4000);
	  if(ogg_stream_packetout(&os_de,&test)!=-1){
	fprintf(stderr,"Error: loss of page did not return error\n");
	exit(1);
	  }
	  if(ogg_stream_packetout(&os_de,&test)!=1)error();
	  checkpacket(&test,300,13,14000);
	  fprintf(stderr,"ok.\n");
	}

	{
	  ogg_page og_de;
	  fprintf(stderr,"Testing sync on partial inputs... ");
	  ogg_sync_reset(&oy);
	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
		 3);
	  ogg_sync_wrote(&oy,3);
	  if(ogg_sync_pageout(&oy,&og_de)>0)error();

	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+3,
		 20);
	  ogg_sync_wrote(&oy,20);
	  if(ogg_sync_pageout(&oy,&og_de)>0)error();

	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+23,
		 5);
	  ogg_sync_wrote(&oy,5);
	  if(ogg_sync_pageout(&oy,&og_de)>0)error();

	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+28,
		 og[1].header_len-28);
	  ogg_sync_wrote(&oy,og[1].header_len-28);
	  if(ogg_sync_pageout(&oy,&og_de)>0)error();

	  memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,1000);
	  ogg_sync_wrote(&oy,1000);
	  if(ogg_sync_pageout(&oy,&og_de)>0)error();

	  memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body+1000,
		 og[1].body_len-1000);
	  ogg_sync_wrote(&oy,og[1].body_len-1000);
	  if(ogg_sync_pageout(&oy,&og_de)<=0)error();

	  fprintf(stderr,"ok.\n");
	}

	{
	  ogg_page og_de;
	  fprintf(stderr,"Testing sync on 1+partial inputs... ");
	  ogg_sync_reset(&oy);

	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
		 og[1].header_len);
	  ogg_sync_wrote(&oy,og[1].header_len);

	  memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
		 og[1].body_len);
	  ogg_sync_wrote(&oy,og[1].body_len);

	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
		 20);
	  ogg_sync_wrote(&oy,20);
	  if(ogg_sync_pageout(&oy,&og_de)<=0)error();
	  if(ogg_sync_pageout(&oy,&og_de)>0)error();

	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+20,
		 og[1].header_len-20);
	  ogg_sync_wrote(&oy,og[1].header_len-20);
	  memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
		 og[1].body_len);
	  ogg_sync_wrote(&oy,og[1].body_len);
	  if(ogg_sync_pageout(&oy,&og_de)<=0)error();

	  fprintf(stderr,"ok.\n");
	}

	{
	  ogg_page og_de;
	  fprintf(stderr,"Testing search for capture... ");
	  ogg_sync_reset(&oy);

	  memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
		 og[1].body_len);
	  ogg_sync_wrote(&oy,og[1].body_len);

	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
		 og[1].header_len);
	  ogg_sync_wrote(&oy,og[1].header_len);

	  memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
		 og[1].body_len);
	  ogg_sync_wrote(&oy,og[1].body_len);

	  memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
		 20);
	  ogg_sync_wrote(&oy,20);
	  if(ogg_sync_pageout(&oy,&og_de)>0)error();
	  if(ogg_sync_pageout(&oy,&og_de)<=0)error();
	  if(ogg_sync_pageout(&oy,&og_de)>0)error();

	  memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header+20,
		 og[2].header_len-20);
	  ogg_sync_wrote(&oy,og[2].header_len-20);
	  memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body,
		 og[2].body_len);
	  ogg_sync_wrote(&oy,og[2].body_len);
	  if(ogg_sync_pageout(&oy,&og_de)<=0)error();

	  fprintf(stderr,"ok.\n");
	}

	{
	  ogg_page og_de;
	  fprintf(stderr,"Testing recapture... ");
	  ogg_sync_reset(&oy);

	  memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
		 og[1].header_len);
	  ogg_sync_wrote(&oy,og[1].header_len);

	  memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
		 og[1].body_len);
	  ogg_sync_wrote(&oy,og[1].body_len);

	  memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
		 og[2].header_len);
	  ogg_sync_wrote(&oy,og[2].header_len);

	  memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
		 og[2].header_len);
	  ogg_sync_wrote(&oy,og[2].header_len);

	  if(ogg_sync_pageout(&oy,&og_de)<=0)error();

	  memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body,
		 og[2].body_len-5);
	  ogg_sync_wrote(&oy,og[2].body_len-5);

	  memcpy(ogg_sync_buffer(&oy,og[3].header_len),og[3].header,
		 og[3].header_len);
	  ogg_sync_wrote(&oy,og[3].header_len);

	  memcpy(ogg_sync_buffer(&oy,og[3].body_len),og[3].body,
		 og[3].body_len);
	  ogg_sync_wrote(&oy,og[3].body_len);

	  if(ogg_sync_pageout(&oy,&og_de)>0)error();
	  if(ogg_sync_pageout(&oy,&og_de)<=0)error();

	  fprintf(stderr,"ok.\n");
	}

	{
	  for(i=0;i<5;i++){
	free_page(&og[i]);
	  }
	}
  }

  return(0);
}

#endif

#endif
/*** End of inlined file: framing.c ***/


/*** Start of inlined file: analysis.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdio.h>
#include <string.h>
#include <math.h>


/*** Start of inlined file: codec_internal.h ***/
#ifndef _V_CODECI_H_
#define _V_CODECI_H_


/*** Start of inlined file: envelope.h ***/
#ifndef _V_ENVELOPE_
#define _V_ENVELOPE_


/*** Start of inlined file: mdct.h ***/
#ifndef _OGG_mdct_H_
#define _OGG_mdct_H_

#ifdef MDCT_INTEGERIZED

#define DATA_TYPE int
#define REG_TYPE  register int
#define TRIGBITS 14
#define cPI3_8 6270
#define cPI2_8 11585
#define cPI1_8 15137

#define FLOAT_CONV(x) ((int)((x)*(1<<TRIGBITS)+.5))
#define MULT_NORM(x) ((x)>>TRIGBITS)
#define HALVE(x) ((x)>>1)

#else

#define DATA_TYPE float
#define REG_TYPE  float
#define cPI3_8 .38268343236508977175F
#define cPI2_8 .70710678118654752441F
#define cPI1_8 .92387953251128675613F

#define FLOAT_CONV(x) (x)
#define MULT_NORM(x) (x)
#define HALVE(x) ((x)*.5f)

#endif

typedef struct {
  int n;
  int log2n;

  DATA_TYPE *trig;
  int	   *bitrev;

  DATA_TYPE scale;
} mdct_lookup;

extern void mdct_init(mdct_lookup *lookup,int n);
extern void mdct_clear(mdct_lookup *l);
extern void mdct_forward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out);
extern void mdct_backward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out);

#endif
/*** End of inlined file: mdct.h ***/

#define VE_PRE	16
#define VE_WIN	4
#define VE_POST   2
#define VE_AMP	(VE_PRE+VE_POST-1)

#define VE_BANDS  7
#define VE_NEARDC 15

#define VE_MINSTRETCH 2   /* a bit less than short block */
#define VE_MAXSTRETCH 12  /* one-third full block */

typedef struct {
  float ampbuf[VE_AMP];
  int   ampptr;

  float nearDC[VE_NEARDC];
  float nearDC_acc;
  float nearDC_partialacc;
  int   nearptr;

} envelope_filter_state;

typedef struct {
  int begin;
  int end;
  float *window;
  float total;
} envelope_band;

typedef struct {
  int ch;
  int winlength;
  int searchstep;
  float minenergy;

  mdct_lookup  mdct;
  float	   *mdct_win;

  envelope_band	  band[VE_BANDS];
  envelope_filter_state *filter;
  int   stretch;

  int		   *mark;

  long storage;
  long current;
  long curmark;
  long cursor;
} envelope_lookup;

extern void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi);
extern void _ve_envelope_clear(envelope_lookup *e);
extern long _ve_envelope_search(vorbis_dsp_state *v);
extern void _ve_envelope_shift(envelope_lookup *e,long shift);
extern int  _ve_envelope_mark(vorbis_dsp_state *v);

#endif
/*** End of inlined file: envelope.h ***/


/*** Start of inlined file: codebook.h ***/
#ifndef _V_CODEBOOK_H_
#define _V_CODEBOOK_H_

typedef struct static_codebook{
  long   dim;		/* codebook dimensions (elements per vector) */
  long   entries;	/* codebook entries */
  long  *lengthlist;	 /* codeword lengths in bits */

  int	maptype;	/* 0=none
				1=implicitly populated values from map column
				2=listed arbitrary values */

  long	 q_min;	   /* packed 32 bit float; quant value 0 maps to minval */
  long	 q_delta;	 /* packed 32 bit float; val 1 - val 0 == delta */
  int	  q_quant;	 /* bits: 0 < quant <= 16 */
  int	  q_sequencep; /* bitflag */

  long	 *quantlist;  /* map == 1: (int)(entries^(1/dim)) element column map
			   map == 2: list of dim*entries quantized entry vals
			*/

  struct encode_aux_nearestmatch *nearest_tree;
  struct encode_aux_threshmatch  *thresh_tree;
  struct encode_aux_pigeonhole  *pigeon_tree;

  int allocedp;
} static_codebook;

typedef struct encode_aux_nearestmatch{
  long   *ptr0;
  long   *ptr1;

  long   *p;	 /* decision points (each is an entry) */
  long   *q;	 /* decision points (each is an entry) */
  long   aux;	/* number of tree entries */
  long   alloc;
} encode_aux_nearestmatch;

typedef struct encode_aux_threshmatch{
  float *quantthresh;
  long   *quantmap;
  int	 quantvals;
  int	 threshvals;
} encode_aux_threshmatch;

typedef struct encode_aux_pigeonhole{
  float min;
  float del;

  int  mapentries;
  int  quantvals;
  long *pigeonmap;

  long fittotal;
  long *fitlist;
  long *fitmap;
  long *fitlength;
} encode_aux_pigeonhole;

typedef struct codebook{
  long dim;	   /* codebook dimensions (elements per vector) */
  long entries;	   /* codebook entries */
  long used_entries;  /* populated codebook entries */
  const static_codebook *c;

  float	*valuelist;  /* list of dim*entries actual entry values */
  ogg_uint32_t *codelist;   /* list of bitstream codewords for each entry */

  int	  *dec_index;  /* only used if sparseness collapsed */
  char	 *dec_codelengths;
  ogg_uint32_t *dec_firsttable;
  int	   dec_firsttablen;
  int	   dec_maxlength;

} codebook;

extern void vorbis_staticbook_clear(static_codebook *b);
extern void vorbis_staticbook_destroy(static_codebook *b);
extern int vorbis_book_init_encode(codebook *dest,const static_codebook *source);
extern int vorbis_book_init_decode(codebook *dest,const static_codebook *source);
extern void vorbis_book_clear(codebook *b);

extern float *_book_unquantize(const static_codebook *b,int n,int *map);
extern float *_book_logdist(const static_codebook *b,float *vals);
extern float _float32_unpack(long val);
extern long   _float32_pack(float val);
extern int  _best(codebook *book, float *a, int step);
extern int _ilog(unsigned int v);
extern long _book_maptype1_quantvals(const static_codebook *b);

extern int vorbis_book_besterror(codebook *book,float *a,int step,int addmul);
extern long vorbis_book_codeword(codebook *book,int entry);
extern long vorbis_book_codelen(codebook *book,int entry);

extern int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *b);
extern int vorbis_staticbook_unpack(oggpack_buffer *b,static_codebook *c);

extern int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b);
extern int vorbis_book_errorv(codebook *book, float *a);
extern int vorbis_book_encodev(codebook *book, int best,float *a,
				   oggpack_buffer *b);

extern long vorbis_book_decode(codebook *book, oggpack_buffer *b);
extern long vorbis_book_decodevs_add(codebook *book, float *a,
					 oggpack_buffer *b,int n);
extern long vorbis_book_decodev_set(codebook *book, float *a,
					oggpack_buffer *b,int n);
extern long vorbis_book_decodev_add(codebook *book, float *a,
					oggpack_buffer *b,int n);
extern long vorbis_book_decodevv_add(codebook *book, float **a,
					 long off,int ch,
					oggpack_buffer *b,int n);

#endif
/*** End of inlined file: codebook.h ***/

#define BLOCKTYPE_IMPULSE	0
#define BLOCKTYPE_PADDING	1
#define BLOCKTYPE_TRANSITION 0
#define BLOCKTYPE_LONG	   1

#define PACKETBLOBS 15

typedef struct vorbis_block_internal{
  float  **pcmdelay;  /* this is a pointer into local storage */
  float  ampmax;
  int	blocktype;

  oggpack_buffer *packetblob[PACKETBLOBS]; /* initialized, must be freed;
						  blob [PACKETBLOBS/2] points to
						  the oggpack_buffer in the
						  main vorbis_block */
} vorbis_block_internal;

typedef void vorbis_look_floor;
typedef void vorbis_look_residue;
typedef void vorbis_look_transform;

typedef struct {
  int blockflag;
  int windowtype;
  int transformtype;
  int mapping;
} vorbis_info_mode;

typedef void vorbis_info_floor;
typedef void vorbis_info_residue;
typedef void vorbis_info_mapping;


/*** Start of inlined file: psy.h ***/
#ifndef _V_PSY_H_
#define _V_PSY_H_

/*** Start of inlined file: smallft.h ***/
#ifndef _V_SMFT_H_
#define _V_SMFT_H_

typedef struct {
  int n;
  float *trigcache;
  int *splitcache;
} drft_lookup;

extern void drft_forward(drft_lookup *l,float *data);
extern void drft_backward(drft_lookup *l,float *data);
extern void drft_init(drft_lookup *l,int n);
extern void drft_clear(drft_lookup *l);

#endif
/*** End of inlined file: smallft.h ***/



/*** Start of inlined file: backends.h ***/
#ifndef _vorbis_backend_h_
#define _vorbis_backend_h_

typedef struct{
  void		   (*pack)  (vorbis_info_floor *,oggpack_buffer *);
  vorbis_info_floor	 *(*unpack)(vorbis_info *,oggpack_buffer *);
  vorbis_look_floor	 *(*look)  (vorbis_dsp_state *,vorbis_info_floor *);
  void (*free_info) (vorbis_info_floor *);
  void (*free_look) (vorbis_look_floor *);
  void *(*inverse1)  (struct vorbis_block *,vorbis_look_floor *);
  int   (*inverse2)  (struct vorbis_block *,vorbis_look_floor *,
			 void *buffer,float *);
} vorbis_func_floor;

typedef struct{
  int   order;
  long  rate;
  long  barkmap;

  int   ampbits;
  int   ampdB;

  int   numbooks; /* <= 16 */
  int   books[16];

  float lessthan;	 /* encode-only config setting hacks for libvorbis */
  float greaterthan;  /* encode-only config setting hacks for libvorbis */

} vorbis_info_floor0;

#define VIF_POSIT 63
#define VIF_CLASS 16
#define VIF_PARTS 31
typedef struct{
  int   partitions;		/* 0 to 31 */
  int   partitionclass[VIF_PARTS]; /* 0 to 15 */

  int   class_dim[VIF_CLASS];	/* 1 to 8 */
  int   class_subs[VIF_CLASS];	   /* 0,1,2,3 (bits: 1<<n poss) */
  int   class_book[VIF_CLASS];	   /* subs ^ dim entries */
  int   class_subbook[VIF_CLASS][8]; /* [VIF_CLASS][subs] */

  int   mult;			  /* 1 2 3 or 4 */
  int   postlist[VIF_POSIT+2];	/* first two implicit */

  float maxover;
  float maxunder;
  float maxerr;

  float twofitweight;
  float twofitatten;

  int   n;

} vorbis_info_floor1;

typedef struct{
  void		 (*pack)  (vorbis_info_residue *,oggpack_buffer *);
  vorbis_info_residue *(*unpack)(vorbis_info *,oggpack_buffer *);
  vorbis_look_residue *(*look)  (vorbis_dsp_state *,
				 vorbis_info_residue *);
  void (*free_info)	(vorbis_info_residue *);
  void (*free_look)	(vorbis_look_residue *);
  long **(*classx)	  (struct vorbis_block *,vorbis_look_residue *,
			float **,int *,int);
  int  (*forward)	  (oggpack_buffer *,struct vorbis_block *,
			vorbis_look_residue *,
			float **,float **,int *,int,long **);
  int  (*inverse)	  (struct vorbis_block *,vorbis_look_residue *,
			float **,int *,int);
} vorbis_func_residue;

typedef struct vorbis_info_residue0{
  long  begin;
  long  end;

  int	grouping;	 /* group n vectors per partition */
  int	partitions;	   /* possible codebooks for a partition */
  int	groupbook;	/* huffbook for partitioning */
  int	secondstages[64]; /* expanded out to pointers in lookup */
  int	booklist[256];	/* list of second stage books */

  float  classmetric1[64];
  float  classmetric2[64];

} vorbis_info_residue0;

typedef struct{
  void		 (*pack)  (vorbis_info *,vorbis_info_mapping *,
				 oggpack_buffer *);
  vorbis_info_mapping *(*unpack)(vorbis_info *,oggpack_buffer *);
  void (*free_info)	(vorbis_info_mapping *);
  int  (*forward)	  (struct vorbis_block *vb);
  int  (*inverse)	  (struct vorbis_block *vb,vorbis_info_mapping *);
} vorbis_func_mapping;

typedef struct vorbis_info_mapping0{
  int   submaps;  /* <= 16 */
  int   chmuxlist[256];   /* up to 256 channels in a Vorbis stream */

  int   floorsubmap[16];   /* [mux] submap to floors */
  int   residuesubmap[16]; /* [mux] submap to residue */

  int   coupling_steps;
  int   coupling_mag[256];
  int   coupling_ang[256];

} vorbis_info_mapping0;

#endif
/*** End of inlined file: backends.h ***/

#ifndef EHMER_MAX
#define EHMER_MAX 56
#endif

#define P_BANDS 17	  /* 62Hz to 16kHz */
#define P_LEVELS 8	  /* 30dB to 100dB */
#define P_LEVEL_0 30.	/* 30 dB */
#define P_NOISECURVES 3

#define NOISE_COMPAND_LEVELS 40
typedef struct vorbis_info_psy{
  int   blockflag;

  float ath_adjatt;
  float ath_maxatt;

  float tone_masteratt[P_NOISECURVES];
  float tone_centerboost;
  float tone_decay;
  float tone_abs_limit;
  float toneatt[P_BANDS];

  int noisemaskp;
  float noisemaxsupp;
  float noisewindowlo;
  float noisewindowhi;
  int   noisewindowlomin;
  int   noisewindowhimin;
  int   noisewindowfixed;
  float noiseoff[P_NOISECURVES][P_BANDS];
  float noisecompand[NOISE_COMPAND_LEVELS];

  float max_curve_dB;

  int normal_channel_p;
  int normal_point_p;
  int normal_start;
  int normal_partition;
  double normal_thresh;
} vorbis_info_psy;

typedef struct{
  int   eighth_octave_lines;

  float preecho_thresh[VE_BANDS];
  float postecho_thresh[VE_BANDS];
  float stretch_penalty;
  float preecho_minenergy;

  float ampmax_att_per_sec;

  int   coupling_pkHz[PACKETBLOBS];
  int   coupling_pointlimit[2][PACKETBLOBS];
  int   coupling_prepointamp[PACKETBLOBS];
  int   coupling_postpointamp[PACKETBLOBS];
  int   sliding_lowpass[2][PACKETBLOBS];

} vorbis_info_psy_global;

typedef struct {
  float ampmax;
  int   channels;

  vorbis_info_psy_global *gi;
  int   coupling_pointlimit[2][P_NOISECURVES];
} vorbis_look_psy_global;

typedef struct {
  int n;
  struct vorbis_info_psy *vi;

  float ***tonecurves;
  float **noiseoffset;

  float *ath;
  long  *octave;		 /* in n.ocshift format */
  long  *bark;

  long  firstoc;
  long  shiftoc;
  int   eighth_octave_lines; /* power of two, please */
  int   total_octave_lines;
  long  rate; /* cache it */

  float m_val; /* Masking compensation value */

} vorbis_look_psy;

extern void   _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,
			   vorbis_info_psy_global *gi,int n,long rate);
extern void   _vp_psy_clear(vorbis_look_psy *p);
extern void  *_vi_psy_dup(void *source);

extern void   _vi_psy_free(vorbis_info_psy *i);
extern vorbis_info_psy *_vi_psy_copy(vorbis_info_psy *i);

extern void _vp_remove_floor(vorbis_look_psy *p,
				 float *mdct,
				 int *icodedflr,
				 float *residue,
				 int sliding_lowpass);

extern void _vp_noisemask(vorbis_look_psy *p,
			  float *logmdct,
			  float *logmask);

extern void _vp_tonemask(vorbis_look_psy *p,
			 float *logfft,
			 float *logmask,
			 float global_specmax,
			 float local_specmax);

extern void _vp_offset_and_mix(vorbis_look_psy *p,
				   float *noise,
				   float *tone,
				   int offset_select,
				   float *logmask,
				   float *mdct,
				   float *logmdct);

extern float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd);

extern float **_vp_quantize_couple_memo(vorbis_block *vb,
					vorbis_info_psy_global *g,
					vorbis_look_psy *p,
					vorbis_info_mapping0 *vi,
					float **mdct);

extern void _vp_couple(int blobno,
			   vorbis_info_psy_global *g,
			   vorbis_look_psy *p,
			   vorbis_info_mapping0 *vi,
			   float **res,
			   float **mag_memo,
			   int   **mag_sort,
			   int   **ifloor,
			   int   *nonzero,
			   int   sliding_lowpass);

extern void _vp_noise_normalize(vorbis_look_psy *p,
				float *in,float *out,int *sortedindex);

extern void _vp_noise_normalize_sort(vorbis_look_psy *p,
					 float *magnitudes,int *sortedindex);

extern int **_vp_quantize_couple_sort(vorbis_block *vb,
					  vorbis_look_psy *p,
					  vorbis_info_mapping0 *vi,
					  float **mags);

extern void hf_reduction(vorbis_info_psy_global *g,
			 vorbis_look_psy *p,
			 vorbis_info_mapping0 *vi,
			 float **mdct);

#endif
/*** End of inlined file: psy.h ***/


/*** Start of inlined file: bitrate.h ***/
#ifndef _V_BITRATE_H_
#define _V_BITRATE_H_


/*** Start of inlined file: os.h ***/
#ifndef _OS_H
#define _OS_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>


/*** Start of inlined file: misc.h ***/
#ifndef _V_RANDOM_H_
#define _V_RANDOM_H_

extern int analysis_noisy;

extern void *_vorbis_block_alloc(vorbis_block *vb,long bytes);
extern void _vorbis_block_ripcord(vorbis_block *vb);
extern void _analysis_output(char *base,int i,float *v,int n,int bark,int dB,
				 ogg_int64_t off);

#ifdef DEBUG_MALLOC

#define _VDBG_GRAPHFILE "malloc.m"
extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line);
extern void _VDBG_free(void *ptr,char *file,long line);

#ifndef MISC_C
#undef _ogg_malloc
#undef _ogg_calloc
#undef _ogg_realloc
#undef _ogg_free

#define _ogg_malloc(x) _VDBG_malloc(NULL,(x),__FILE__,__LINE__)
#define _ogg_calloc(x,y) _VDBG_malloc(NULL,(x)*(y),__FILE__,__LINE__)
#define _ogg_realloc(x,y) _VDBG_malloc((x),(y),__FILE__,__LINE__)
#define _ogg_free(x) _VDBG_free((x),__FILE__,__LINE__)
#endif
#endif

#endif
/*** End of inlined file: misc.h ***/

#ifndef _V_IFDEFJAIL_H_
#  define _V_IFDEFJAIL_H_

#  ifdef __GNUC__
#	define STIN static __inline__
#  elif _WIN32
#	define STIN static __inline
#  else
#	define STIN static
#  endif

#ifdef DJGPP
#  define rint(x)   (floor((x)+0.5f))
#endif

#ifndef M_PI
#  define M_PI (3.1415926536f)
#endif

#if defined(_WIN32) && !defined(__SYMBIAN32__)
#  include <malloc.h>
#  define rint(x)   (floor((x)+0.5f))
#  define NO_FLOAT_MATH_LIB
#  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
#endif

#if defined(__SYMBIAN32__) && defined(__WINS__)
void *_alloca(size_t size);
#  define alloca _alloca
#endif

#ifndef FAST_HYPOT
#  define FAST_HYPOT hypot
#endif

#endif

#ifdef HAVE_ALLOCA_H
#  include <alloca.h>
#endif

#ifdef USE_MEMORY_H
#  include <memory.h>
#endif

#ifndef min
#  define min(x,y)  ((x)>(y)?(y):(x))
#endif

#ifndef max
#  define max(x,y)  ((x)<(y)?(y):(x))
#endif

#if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
#  define VORBIS_FPU_CONTROL

typedef ogg_int16_t vorbis_fpu_control;

static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  ogg_int16_t ret;
  ogg_int16_t temp;
  __asm__ __volatile__("fnstcw %0\n\t"
	  "movw %0,%%dx\n\t"
	  "orw $62463,%%dx\n\t"
	  "movw %%dx,%1\n\t"
	  "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
  *fpu=ret;
}

static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  __asm__ __volatile__("fldcw %0":: "m"(fpu));
}

static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
											 we get extra fst/fld to
											 truncate precision */
  int i;
  __asm__("fistl %0": "=m"(i) : "t"(f));
  return(i);
}
#endif

#if defined(_WIN32) && defined(_X86_) && !defined(__GNUC__) && !defined(__BORLANDC__)
#  define VORBIS_FPU_CONTROL

typedef ogg_int16_t vorbis_fpu_control;

static __inline int vorbis_ftoi(double f){
	int i;
	__asm{
		fld f
		fistp i
	}
	return i;
}

static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
}

static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
}

#endif

#ifndef VORBIS_FPU_CONTROL

typedef int vorbis_fpu_control;

static int vorbis_ftoi(double f){
  return (int)(f+.5);
}

#  define vorbis_fpu_setround(vorbis_fpu_control) {}
#  define vorbis_fpu_restore(vorbis_fpu_control) {}

#endif

#endif /* _OS_H */
/*** End of inlined file: os.h ***/

typedef struct bitrate_manager_state {
  int		managed;

  long	   avg_reservoir;
  long	   minmax_reservoir;
  long	   avg_bitsper;
  long	   min_bitsper;
  long	   max_bitsper;

  long	   short_per_long;
  double	 avgfloat;

  vorbis_block  *vb;
  int		choice;
} bitrate_manager_state;

typedef struct bitrate_manager_info{
  long	   avg_rate;
  long	   min_rate;
  long	   max_rate;
  long	   reservoir_bits;
  double	 reservoir_bias;

  double	 slew_damp;

} bitrate_manager_info;

extern void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bs);
extern void vorbis_bitrate_clear(bitrate_manager_state *bs);
extern int vorbis_bitrate_managed(vorbis_block *vb);
extern int vorbis_bitrate_addblock(vorbis_block *vb);
extern int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd, ogg_packet *op);

#endif
/*** End of inlined file: bitrate.h ***/

static int ilog(unsigned int v){
  int ret=0;
  while(v){
	ret++;
	v>>=1;
  }
  return(ret);
}

static int ilog2(unsigned int v){
  int ret=0;
  if(v)--v;
  while(v){
	ret++;
	v>>=1;
  }
  return(ret);
}

typedef struct private_state {
  envelope_lookup	*ve; /* envelope lookup */
  int			 window[2];
  vorbis_look_transform **transform[2];	/* block, type */
  drft_lookup		 fft_look[2];

  int			 modebits;
  vorbis_look_floor	 **flr;
  vorbis_look_residue   **residue;
  vorbis_look_psy	*psy;
  vorbis_look_psy_global *psy_g_look;

  unsigned char *header;
  unsigned char *header1;
  unsigned char *header2;

  bitrate_manager_state bms;

  ogg_int64_t sample_count;
} private_state;


/*** Start of inlined file: highlevel.h ***/
typedef struct highlevel_byblocktype {
  double tone_mask_setting;
  double tone_peaklimit_setting;
  double noise_bias_setting;
  double noise_compand_setting;
} highlevel_byblocktype;

typedef struct highlevel_encode_setup {
  void *setup;
  int   set_in_stone;

  double base_setting;
  double long_setting;
  double short_setting;
  double impulse_noisetune;

  int	managed;
  long   bitrate_min;
  long   bitrate_av;
  double bitrate_av_damp;
  long   bitrate_max;
  long   bitrate_reservoir;
  double bitrate_reservoir_bias;

  int impulse_block_p;
  int noise_normalize_p;

  double stereo_point_setting;
  double lowpass_kHz;

  double ath_floating_dB;
  double ath_absolute_dB;

  double amplitude_track_dBpersec;
  double trigger_setting;

  highlevel_byblocktype block[4]; /* padding, impulse, transition, long */

} highlevel_encode_setup;
/*** End of inlined file: highlevel.h ***/

typedef struct codec_setup_info {

  long blocksizes[2];

  int	modes;
  int	maps;
  int	floors;
  int	residues;
  int	books;
  int	psys;	 /* encode only */

  vorbis_info_mode	   *mode_param[64];
  int			 map_type[64];
  vorbis_info_mapping	*map_param[64];
  int			 floor_type[64];
  vorbis_info_floor	  *floor_param[64];
  int			 residue_type[64];
  vorbis_info_residue	*residue_param[64];
  static_codebook	*book_param[256];
  codebook		   *fullbooks;

  vorbis_info_psy	*psy_param[4]; /* encode only */
  vorbis_info_psy_global psy_g_param;

  bitrate_manager_info   bi;
  highlevel_encode_setup hi; /* used only by vorbisenc.c.  It's a
								highly redundant structure, but
								improves clarity of program flow. */
  int	 halfrate_flag; /* painless downsample for decode */
} codec_setup_info;

extern vorbis_look_psy_global *_vp_global_look(vorbis_info *vi);
extern void _vp_global_free(vorbis_look_psy_global *look);

#endif
/*** End of inlined file: codec_internal.h ***/


/*** Start of inlined file: registry.h ***/
#ifndef _V_REG_H_
#define _V_REG_H_

#define VI_TRANSFORMB 1
#define VI_WINDOWB 1
#define VI_TIMEB 1
#define VI_FLOORB 2
#define VI_RESB 3
#define VI_MAPB 1

extern vorbis_func_floor	 *_floor_P[];
extern vorbis_func_residue   *_residue_P[];
extern vorbis_func_mapping   *_mapping_P[];

#endif
/*** End of inlined file: registry.h ***/


/*** Start of inlined file: scales.h ***/
#ifndef _V_SCALES_H_
#define _V_SCALES_H_

#include <math.h>

#define VORBIS_IEEE_FLOAT32 1
#ifdef VORBIS_IEEE_FLOAT32

static float unitnorm(float x){
  union {
	ogg_uint32_t i;
	float f;
  } ix;
  ix.f = x;
  ix.i = (ix.i & 0x80000000U) | (0x3f800000U);
  return ix.f;
}

static float todB(const float *x){
  union {
	ogg_uint32_t i;
	float f;
  } ix;
  ix.f = *x;
  ix.i = ix.i&0x7fffffff;
  return (float)(ix.i * 7.17711438e-7f -764.6161886f);
}

#define todB_nn(x) todB(x)

#else

static float unitnorm(float x){
  if(x<0)return(-1.f);
  return(1.f);
}

#define todB(x)   (*(x)==0?-400.f:log(*(x)**(x))*4.34294480f)
#define todB_nn(x)   (*(x)==0.f?-400.f:log(*(x))*8.6858896f)

#endif

#define fromdB(x) (exp((x)*.11512925f))

#define toBARK(n)   (13.1f*atan(.00074f*(n))+2.24f*atan((n)*(n)*1.85e-8f)+1e-4f*(n))
#define fromBARK(z) (102.f*(z)-2.f*pow(z,2.f)+.4f*pow(z,3.f)+pow(1.46f,z)-1.f)
#define toMEL(n)	(log(1.f+(n)*.001f)*1442.695f)
#define fromMEL(m)  (1000.f*exp((m)/1442.695f)-1000.f)

#define toOC(n)	 (log(n)*1.442695f-5.965784f)
#define fromOC(o)   (exp(((o)+5.965784f)*.693147f))

#endif
/*** End of inlined file: scales.h ***/

int analysis_noisy=1;

int vorbis_analysis(vorbis_block *vb, ogg_packet *op){
  int ret,i;
  vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;

  vb->glue_bits=0;
  vb->time_bits=0;
  vb->floor_bits=0;
  vb->res_bits=0;

  for(i=0;i<PACKETBLOBS;i++)
	oggpack_reset(vbi->packetblob[i]);

  if((ret=_mapping_P[0]->forward(vb)))
	return(ret);

  if(op){
	if(vorbis_bitrate_managed(vb))
	  return(OV_EINVAL);

	op->packet=oggpack_get_buffer(&vb->opb);
	op->bytes=oggpack_bytes(&vb->opb);
	op->b_o_s=0;
	op->e_o_s=vb->eofflag;
	op->granulepos=vb->granulepos;
	op->packetno=vb->sequence; /* for sake of completeness */
  }
  return(0);
}

void _analysis_output_always(const char *base,int i,float *v,int n,int bark,int dB,ogg_int64_t off){
  int j;
  FILE *of;
  char buffer[80];

	sprintf(buffer,"%s_%d.m",base,i);
	of=fopen(buffer,"w");

	if(!of)perror("failed to open data dump file");

	for(j=0;j<n;j++){
	  if(bark){
	float b=toBARK((4000.f*j/n)+.25);
	fprintf(of,"%f ",b);
	  }else
	if(off!=0)
	  fprintf(of,"%f ",(double)(j+off)/8000.);
	else
	  fprintf(of,"%f ",(double)j);

	  if(dB){
	float val;
	if(v[j]==0.)
	  val=-140.;
	else
	  val=todB(v+j);
	fprintf(of,"%f\n",val);
	  }else{
	fprintf(of,"%f\n",v[j]);
	  }
	}
	fclose(of);
}

void _analysis_output(char *base,int i,float *v,int n,int bark,int dB,
			  ogg_int64_t off){
  if(analysis_noisy)_analysis_output_always(base,i,v,n,bark,dB,off);
}

#endif
/*** End of inlined file: analysis.c ***/


/*** Start of inlined file: bitrate.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <math.h>

void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){
  codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  bitrate_manager_info *bi=&ci->bi;

  memset(bm,0,sizeof(*bm));

  if(bi && (bi->reservoir_bits>0)){
	long ratesamples=vi->rate;
	int  halfsamples=ci->blocksizes[0]>>1;

	bm->short_per_long=ci->blocksizes[1]/ci->blocksizes[0];
	bm->managed=1;

	bm->avg_bitsper= rint(1.*bi->avg_rate*halfsamples/ratesamples);
	bm->min_bitsper= rint(1.*bi->min_rate*halfsamples/ratesamples);
	bm->max_bitsper= rint(1.*bi->max_rate*halfsamples/ratesamples);

	bm->avgfloat=PACKETBLOBS/2;

	{
	  long desired_fill=bi->reservoir_bits*bi->reservoir_bias;
	  bm->minmax_reservoir=desired_fill;
	  bm->avg_reservoir=desired_fill;
	}

  }
}

void vorbis_bitrate_clear(bitrate_manager_state *bm){
  memset(bm,0,sizeof(*bm));
  return;
}

int vorbis_bitrate_managed(vorbis_block *vb){
  vorbis_dsp_state	  *vd=vb->vd;
  private_state	 *b=(private_state*)vd->backend_state;
  bitrate_manager_state *bm=&b->bms;

  if(bm && bm->managed)return(1);
  return(0);
}

int vorbis_bitrate_addblock(vorbis_block *vb){
  vorbis_block_internal *vbi=(vorbis_block_internal*)vb->internal;
  vorbis_dsp_state	  *vd=vb->vd;
  private_state	 *b=(private_state*)vd->backend_state;
  bitrate_manager_state *bm=&b->bms;
  vorbis_info	   *vi=vd->vi;
  codec_setup_info	  *ci=(codec_setup_info*)vi->codec_setup;
  bitrate_manager_info  *bi=&ci->bi;

  int  choice=rint(bm->avgfloat);
  long this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  long min_target_bits=(vb->W?bm->min_bitsper*bm->short_per_long:bm->min_bitsper);
  long max_target_bits=(vb->W?bm->max_bitsper*bm->short_per_long:bm->max_bitsper);
  int  samples=ci->blocksizes[vb->W]>>1;
  long desired_fill=bi->reservoir_bits*bi->reservoir_bias;
  if(!bm->managed){

	if(bm->vb)return(-1); /* one has been submitted without
				 being claimed */
	bm->vb=vb;
	return(0);
  }

  bm->vb=vb;

  if(bm->avg_bitsper>0){
	double slew=0.;
	long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper);
	double slewlimit= 15./bi->slew_damp;

	if(bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){
	  while(choice>0 && this_bits>avg_target_bits &&
		bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){
	choice--;
	this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
	  }
	}else if(bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){
	  while(choice+1<PACKETBLOBS && this_bits<avg_target_bits &&
		bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){
	choice++;
	this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
	  }
	}

	slew=rint(choice-bm->avgfloat)/samples*vi->rate;
	if(slew<-slewlimit)slew=-slewlimit;
	if(slew>slewlimit)slew=slewlimit;
	choice=rint(bm->avgfloat+= slew/vi->rate*samples);
	this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  }

  if(bm->min_bitsper>0){
	if(this_bits<min_target_bits){
	  while(bm->minmax_reservoir-(min_target_bits-this_bits)<0){
	choice++;
	if(choice>=PACKETBLOBS)break;
	this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
	  }
	}
  }

  if(bm->max_bitsper>0){
	if(this_bits>max_target_bits){
	  while(bm->minmax_reservoir+(this_bits-max_target_bits)>bi->reservoir_bits){
	choice--;
	if(choice<0)break;
	this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
	  }
	}
  }

  if(choice<0){
	long maxsize=(max_target_bits+(bi->reservoir_bits-bm->minmax_reservoir))/8;
	bm->choice=choice=0;

	if(oggpack_bytes(vbi->packetblob[choice])>maxsize){

	  oggpack_writetrunc(vbi->packetblob[choice],maxsize*8);
	  this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
	}
  }else{
	long minsize=(min_target_bits-bm->minmax_reservoir+7)/8;
	if(choice>=PACKETBLOBS)
	  choice=PACKETBLOBS-1;

	bm->choice=choice;

	minsize-=oggpack_bytes(vbi->packetblob[choice]);
	while(minsize-->0)oggpack_write(vbi->packetblob[choice],0,8);
	this_bits=oggpack_bytes(vbi->packetblob[choice])*8;

  }

  if(bm->min_bitsper>0 || bm->max_bitsper>0){

	if(max_target_bits>0 && this_bits>max_target_bits){
	  bm->minmax_reservoir+=(this_bits-max_target_bits);
	}else if(min_target_bits>0 && this_bits<min_target_bits){
	  bm->minmax_reservoir+=(this_bits-min_target_bits);
	}else{
	  if(bm->minmax_reservoir>desired_fill){
	if(max_target_bits>0){ /* logical bulletproofing against initialization state */
	  bm->minmax_reservoir+=(this_bits-max_target_bits);
	  if(bm->minmax_reservoir<desired_fill)bm->minmax_reservoir=desired_fill;
	}else{
	  bm->minmax_reservoir=desired_fill;
	}
	  }else{
	if(min_target_bits>0){ /* logical bulletproofing against initialization state */
	  bm->minmax_reservoir+=(this_bits-min_target_bits);
	  if(bm->minmax_reservoir>desired_fill)bm->minmax_reservoir=desired_fill;
	}else{
	  bm->minmax_reservoir=desired_fill;
	}
	  }
	}
  }

  if(bm->avg_bitsper>0){
	long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper);
	bm->avg_reservoir+=this_bits-avg_target_bits;
  }

  return(0);
}

int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){
  private_state	 *b=(private_state*)vd->backend_state;
  bitrate_manager_state *bm=&b->bms;
  vorbis_block	  *vb=bm->vb;
  int			choice=PACKETBLOBS/2;
  if(!vb)return 0;

  if(op){
	vorbis_block_internal *vbi=(vorbis_block_internal*)vb->internal;

	if(vorbis_bitrate_managed(vb))
	  choice=bm->choice;

	op->packet=oggpack_get_buffer(vbi->packetblob[choice]);
	op->bytes=oggpack_bytes(vbi->packetblob[choice]);
	op->b_o_s=0;
	op->e_o_s=vb->eofflag;
	op->granulepos=vb->granulepos;
	op->packetno=vb->sequence; /* for sake of completeness */
  }

  bm->vb=0;
  return(1);
}

#endif
/*** End of inlined file: bitrate.c ***/


/*** Start of inlined file: block.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/*** Start of inlined file: window.h ***/
#ifndef _V_WINDOW_
#define _V_WINDOW_

extern float *_vorbis_window_get(int n);
extern void _vorbis_apply_window(float *d,int *winno,long *blocksizes,
						  int lW,int W,int nW);

#endif
/*** End of inlined file: window.h ***/


/*** Start of inlined file: lpc.h ***/
#ifndef _V_LPC_H_
#define _V_LPC_H_

extern float vorbis_lpc_from_data(float *data,float *lpc,int n,int m);

extern void vorbis_lpc_predict(float *coeff,float *prime,int m,
				   float *data,long n);

#endif
/*** End of inlined file: lpc.h ***/

#ifndef WORD_ALIGN
#define WORD_ALIGN 8
#endif

int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
  int i;
  memset(vb,0,sizeof(*vb));
  vb->vd=v;
  vb->localalloc=0;
  vb->localstore=NULL;
  if(v->analysisp){
	vorbis_block_internal *vbi=(vorbis_block_internal*)
	  (vb->internal=(vorbis_block_internal*)_ogg_calloc(1,sizeof(vorbis_block_internal)));
	vbi->ampmax=-9999;

	for(i=0;i<PACKETBLOBS;i++){
	  if(i==PACKETBLOBS/2){
	vbi->packetblob[i]=&vb->opb;
	  }else{
	vbi->packetblob[i]=
	  (oggpack_buffer*) _ogg_calloc(1,sizeof(oggpack_buffer));
	  }
	  oggpack_writeinit(vbi->packetblob[i]);
	}
  }

  return(0);
}

void *_vorbis_block_alloc(vorbis_block *vb,long bytes){
  bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
  if(bytes+vb->localtop>vb->localalloc){
	if(vb->localstore){
	  struct alloc_chain *link=(struct alloc_chain*)_ogg_malloc(sizeof(*link));
	  vb->totaluse+=vb->localtop;
	  link->next=vb->reap;
	  link->ptr=vb->localstore;
	  vb->reap=link;
	}
	vb->localalloc=bytes;
	vb->localstore=_ogg_malloc(vb->localalloc);
	vb->localtop=0;
  }
  {
	void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
	vb->localtop+=bytes;
	return ret;
  }
}

void _vorbis_block_ripcord(vorbis_block *vb){
  struct alloc_chain *reap=vb->reap;
  while(reap){
	struct alloc_chain *next=reap->next;
	_ogg_free(reap->ptr);
	memset(reap,0,sizeof(*reap));
	_ogg_free(reap);
	reap=next;
  }
  if(vb->totaluse){
	vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc);
	vb->localalloc+=vb->totaluse;
	vb->totaluse=0;
  }

  vb->localtop=0;
  vb->reap=NULL;
}

int vorbis_block_clear(vorbis_block *vb){
  int i;
  vorbis_block_internal *vbi=(vorbis_block_internal*)vb->internal;

  _vorbis_block_ripcord(vb);
  if(vb->localstore)_ogg_free(vb->localstore);

  if(vbi){
	for(i=0;i<PACKETBLOBS;i++){
	  oggpack_writeclear(vbi->packetblob[i]);
	  if(i!=PACKETBLOBS/2)_ogg_free(vbi->packetblob[i]);
	}
	_ogg_free(vbi);
  }
  memset(vb,0,sizeof(*vb));
  return(0);
}

static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
  int i;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  private_state *b=NULL;
  int hs;

  if(ci==NULL) return 1;
  hs=ci->halfrate_flag;

  memset(v,0,sizeof(*v));
  b=(private_state*) (v->backend_state=(private_state*)_ogg_calloc(1,sizeof(*b)));

  v->vi=vi;
  b->modebits=ilog2(ci->modes);

  b->transform[0]=(vorbis_look_transform**)_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0]));
  b->transform[1]=(vorbis_look_transform**)_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1]));

  b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup));
  b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup));
  mdct_init((mdct_lookup*)b->transform[0][0],ci->blocksizes[0]>>hs);
  mdct_init((mdct_lookup*)b->transform[1][0],ci->blocksizes[1]>>hs);

  b->window[0]=ilog2(ci->blocksizes[0])-6;
  b->window[1]=ilog2(ci->blocksizes[1])-6;

  if(encp){ /* encode/decode differ here */

	drft_init(&b->fft_look[0],ci->blocksizes[0]);
	drft_init(&b->fft_look[1],ci->blocksizes[1]);

	if(!ci->fullbooks){
	  ci->fullbooks=(codebook*) _ogg_calloc(ci->books,sizeof(*ci->fullbooks));
	  for(i=0;i<ci->books;i++)
	vorbis_book_init_encode(ci->fullbooks+i,ci->book_param[i]);
	}

	b->psy=(vorbis_look_psy*)_ogg_calloc(ci->psys,sizeof(*b->psy));
	for(i=0;i<ci->psys;i++){
	  _vp_psy_init(b->psy+i,
		   ci->psy_param[i],
		   &ci->psy_g_param,
		   ci->blocksizes[ci->psy_param[i]->blockflag]/2,
		   vi->rate);
	}

	v->analysisp=1;
  }else{
	if(!ci->fullbooks){
	  ci->fullbooks=(codebook*) _ogg_calloc(ci->books,sizeof(*ci->fullbooks));
	  for(i=0;i<ci->books;i++){
	vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i]);
	vorbis_staticbook_destroy(ci->book_param[i]);
	ci->book_param[i]=NULL;
	  }
	}
  }

  v->pcm_storage=ci->blocksizes[1];
  v->pcm=(float**)_ogg_malloc(vi->channels*sizeof(*v->pcm));
  v->pcmret=(float**)_ogg_malloc(vi->channels*sizeof(*v->pcmret));
  {
	int i;
	for(i=0;i<vi->channels;i++)
	  v->pcm[i]=(float*)_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i]));
  }

  v->lW=0; /* previous window size */
  v->W=0;  /* current window size */

  v->centerW=ci->blocksizes[1]/2;

  v->pcm_current=v->centerW;

  b->flr=(vorbis_look_floor**)_ogg_calloc(ci->floors,sizeof(*b->flr));
  b->residue=(vorbis_look_residue**)_ogg_calloc(ci->residues,sizeof(*b->residue));

  for(i=0;i<ci->floors;i++)
	b->flr[i]=_floor_P[ci->floor_type[i]]->
	  look(v,ci->floor_param[i]);

  for(i=0;i<ci->residues;i++)
	b->residue[i]=_residue_P[ci->residue_type[i]]->
	  look(v,ci->residue_param[i]);

  return 0;
}

int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
  private_state *b=NULL;

  if(_vds_shared_init(v,vi,1))return 1;
  b=(private_state*)v->backend_state;
  b->psy_g_look=_vp_global_look(vi);

  b->ve=(envelope_lookup*)_ogg_calloc(1,sizeof(*b->ve));
  _ve_envelope_init(b->ve,vi);

  vorbis_bitrate_init(vi,&b->bms);

  v->sequence=3;

  return(0);
}

void vorbis_dsp_clear(vorbis_dsp_state *v){
  int i;
  if(v){
	vorbis_info *vi=v->vi;
	codec_setup_info *ci=(codec_setup_info*)(vi?vi->codec_setup:NULL);
	private_state *b=(private_state*)v->backend_state;

	if(b){

	  if(b->ve){
	_ve_envelope_clear(b->ve);
	_ogg_free(b->ve);
	  }

	  if(b->transform[0]){
	mdct_clear((mdct_lookup*) b->transform[0][0]);
	_ogg_free(b->transform[0][0]);
	_ogg_free(b->transform[0]);
	  }
	  if(b->transform[1]){
	mdct_clear((mdct_lookup*) b->transform[1][0]);
	_ogg_free(b->transform[1][0]);
	_ogg_free(b->transform[1]);
	  }

	  if(b->flr){
	for(i=0;i<ci->floors;i++)
	  _floor_P[ci->floor_type[i]]->
		free_look(b->flr[i]);
	_ogg_free(b->flr);
	  }
	  if(b->residue){
	for(i=0;i<ci->residues;i++)
	  _residue_P[ci->residue_type[i]]->
		free_look(b->residue[i]);
	_ogg_free(b->residue);
	  }
	  if(b->psy){
	for(i=0;i<ci->psys;i++)
	  _vp_psy_clear(b->psy+i);
	_ogg_free(b->psy);
	  }

	  if(b->psy_g_look)_vp_global_free(b->psy_g_look);
	  vorbis_bitrate_clear(&b->bms);

	  drft_clear(&b->fft_look[0]);
	  drft_clear(&b->fft_look[1]);

	}

	if(v->pcm){
	  for(i=0;i<vi->channels;i++)
	if(v->pcm[i])_ogg_free(v->pcm[i]);
	  _ogg_free(v->pcm);
	  if(v->pcmret)_ogg_free(v->pcmret);
	}

	if(b){
	  if(b->header)_ogg_free(b->header);
	  if(b->header1)_ogg_free(b->header1);
	  if(b->header2)_ogg_free(b->header2);
	  _ogg_free(b);
	}

	memset(v,0,sizeof(*v));
  }
}

float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
  int i;
  vorbis_info *vi=v->vi;
  private_state *b=(private_state*)v->backend_state;

  if(b->header)_ogg_free(b->header);b->header=NULL;
  if(b->header1)_ogg_free(b->header1);b->header1=NULL;
  if(b->header2)_ogg_free(b->header2);b->header2=NULL;

  if(v->pcm_current+vals>=v->pcm_storage){
	v->pcm_storage=v->pcm_current+vals*2;

	for(i=0;i<vi->channels;i++){
	  v->pcm[i]=(float*)_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i]));
	}
  }

  for(i=0;i<vi->channels;i++)
	v->pcmret[i]=v->pcm[i]+v->pcm_current;

  return(v->pcmret);
}

static void _preextrapolate_helper(vorbis_dsp_state *v){
  int i;
  int order=32;
  float *lpc=(float*)alloca(order*sizeof(*lpc));
  float *work=(float*)alloca(v->pcm_current*sizeof(*work));
  long j;
  v->preextrapolate=1;

  if(v->pcm_current-v->centerW>order*2){ /* safety */
	for(i=0;i<v->vi->channels;i++){
	  for(j=0;j<v->pcm_current;j++)
	work[j]=v->pcm[i][v->pcm_current-j-1];

	  vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);

	  vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
			 order,
			 work+v->pcm_current-v->centerW,
			 v->centerW);

	  for(j=0;j<v->pcm_current;j++)
	v->pcm[i][v->pcm_current-j-1]=work[j];

	}
  }
}

int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;

  if(vals<=0){
	int order=32;
	int i;
	float *lpc=(float*) alloca(order*sizeof(*lpc));

	if(!v->preextrapolate)
	  _preextrapolate_helper(v);

	vorbis_analysis_buffer(v,ci->blocksizes[1]*3);
	v->eofflag=v->pcm_current;
	v->pcm_current+=ci->blocksizes[1]*3;

	for(i=0;i<vi->channels;i++){
	  if(v->eofflag>order*2){
	long n;

	n=v->eofflag;
	if(n>ci->blocksizes[1])n=ci->blocksizes[1];
	vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order);

	vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order,
			   v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag);
	  }else{
	memset(v->pcm[i]+v->eofflag,0,
		   (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i]));

	  }
	}
  }else{

	if(v->pcm_current+vals>v->pcm_storage)
	  return(OV_EINVAL);

	v->pcm_current+=vals;

	if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1])
	  _preextrapolate_helper(v);

  }
  return(0);
}

int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
  int i;
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  private_state *b=(private_state*)v->backend_state;
  vorbis_look_psy_global *g=b->psy_g_look;
  long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext;
  vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;

  if(!v->preextrapolate)return(0);

  if(v->eofflag==-1)return(0);

  {
	long bp=_ve_envelope_search(v);
	if(bp==-1){

	  if(v->eofflag==0)return(0); /* not enough data currently to search for a
					 full long block */
	  v->nW=0;
	}else{

	  if(ci->blocksizes[0]==ci->blocksizes[1])
	v->nW=0;
	  else
	v->nW=bp;
	}
  }

  centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4;

  {

	long blockbound=centerNext+ci->blocksizes[v->nW]/2;
	if(v->pcm_current<blockbound)return(0); /* not enough data yet;
											   although this check is
											   less strict that the
											   _ve_envelope_search,
											   the search is not run
											   if we only use one
											   block size */

  }

  _vorbis_block_ripcord(vb);
  vb->lW=v->lW;
  vb->W=v->W;
  vb->nW=v->nW;

  if(v->W){
	if(!v->lW || !v->nW){
	  vbi->blocktype=BLOCKTYPE_TRANSITION;
	}else{
	  vbi->blocktype=BLOCKTYPE_LONG;
	}
  }else{
	if(_ve_envelope_mark(v)){
	  vbi->blocktype=BLOCKTYPE_IMPULSE;

	}else{
	  vbi->blocktype=BLOCKTYPE_PADDING;

	}
  }

  vb->vd=v;
  vb->sequence=v->sequence++;
  vb->granulepos=v->granulepos;
  vb->pcmend=ci->blocksizes[v->W];

  if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax;
  g->ampmax=_vp_ampmax_decay(g->ampmax,v);
  vbi->ampmax=g->ampmax;

  vb->pcm=(float**)_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
  vbi->pcmdelay=(float**)_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels);
  for(i=0;i<vi->channels;i++){
	vbi->pcmdelay[i]=
	  (float*) _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
	memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
	vb->pcm[i]=vbi->pcmdelay[i]+beginW;

  }

  if(v->eofflag){
	if(v->centerW>=v->eofflag){
	  v->eofflag=-1;
	  vb->eofflag=1;
	  return(1);
	}
  }

  {
	int new_centerNext=ci->blocksizes[1]/2;
	int movementW=centerNext-new_centerNext;

	if(movementW>0){

	  _ve_envelope_shift(b->ve,movementW);
	  v->pcm_current-=movementW;

	  for(i=0;i<vi->channels;i++)
	memmove(v->pcm[i],v->pcm[i]+movementW,
		v->pcm_current*sizeof(*v->pcm[i]));

	  v->lW=v->W;
	  v->W=v->nW;
	  v->centerW=new_centerNext;

	  if(v->eofflag){
	v->eofflag-=movementW;
	if(v->eofflag<=0)v->eofflag=-1;
	if(v->centerW>=v->eofflag){
	  v->granulepos+=movementW-(v->centerW-v->eofflag);
	}else{
	  v->granulepos+=movementW;
	}
	  }else{
	v->granulepos+=movementW;
	  }
	}
  }

  return(1);
}

int vorbis_synthesis_restart(vorbis_dsp_state *v){
  vorbis_info *vi=v->vi;
  codec_setup_info *ci;
  int hs;

  if(!v->backend_state)return -1;
  if(!vi)return -1;
  ci=(codec_setup_info*) vi->codec_setup;
  if(!ci)return -1;
  hs=ci->halfrate_flag;

  v->centerW=ci->blocksizes[1]>>(hs+1);
  v->pcm_current=v->centerW>>hs;

  v->pcm_returned=-1;
  v->granulepos=-1;
  v->sequence=-1;
  v->eofflag=0;
  ((private_state *)(v->backend_state))->sample_count=-1;

  return(0);
}

int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
  if(_vds_shared_init(v,vi,0)) return 1;
  vorbis_synthesis_restart(v);

  return 0;
}

int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  private_state *b=(private_state*)v->backend_state;
  int hs=ci->halfrate_flag;
  int i,j;

  if(!vb)return(OV_EINVAL);
  if(v->pcm_current>v->pcm_returned  && v->pcm_returned!=-1)return(OV_EINVAL);

  v->lW=v->W;
  v->W=vb->W;
  v->nW=-1;

  if((v->sequence==-1)||
	 (v->sequence+1 != vb->sequence)){
	v->granulepos=-1; /* out of sequence; lose count */
	b->sample_count=-1;
  }

  v->sequence=vb->sequence;

  if(vb->pcm){  /* no pcm to process if vorbis_synthesis_trackonly
		   was called on block */
	int n=ci->blocksizes[v->W]>>(hs+1);
	int n0=ci->blocksizes[0]>>(hs+1);
	int n1=ci->blocksizes[1]>>(hs+1);

	int thisCenter;
	int prevCenter;

	v->glue_bits+=vb->glue_bits;
	v->time_bits+=vb->time_bits;
	v->floor_bits+=vb->floor_bits;
	v->res_bits+=vb->res_bits;

	if(v->centerW){
	  thisCenter=n1;
	  prevCenter=0;
	}else{
	  thisCenter=0;
	  prevCenter=n1;
	}

	for(j=0;j<vi->channels;j++){
	  if(v->lW){
	if(v->W){
	  float *w=_vorbis_window_get(b->window[1]-hs);
	  float *pcm=v->pcm[j]+prevCenter;
	  float *p=vb->pcm[j];
	  for(i=0;i<n1;i++)
		pcm[i]=pcm[i]*w[n1-i-1] + p[i]*w[i];
	}else{
	  float *w=_vorbis_window_get(b->window[0]-hs);
	  float *pcm=v->pcm[j]+prevCenter+n1/2-n0/2;
	  float *p=vb->pcm[j];
	  for(i=0;i<n0;i++)
		pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
	}
	  }else{
	if(v->W){
	  float *w=_vorbis_window_get(b->window[0]-hs);
	  float *pcm=v->pcm[j]+prevCenter;
	  float *p=vb->pcm[j]+n1/2-n0/2;
	  for(i=0;i<n0;i++)
		pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
	  for(;i<n1/2+n0/2;i++)
		pcm[i]=p[i];
	}else{
	  float *w=_vorbis_window_get(b->window[0]-hs);
	  float *pcm=v->pcm[j]+prevCenter;
	  float *p=vb->pcm[j];
	  for(i=0;i<n0;i++)
		pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
	}
	  }

	  {
	float *pcm=v->pcm[j]+thisCenter;
	float *p=vb->pcm[j]+n;
	for(i=0;i<n;i++)
	  pcm[i]=p[i];
	  }
	}

	if(v->centerW)
	  v->centerW=0;
	else
	  v->centerW=n1;

	if(v->pcm_returned==-1){
	  v->pcm_returned=thisCenter;
	  v->pcm_current=thisCenter;
	}else{
	  v->pcm_returned=prevCenter;
	  v->pcm_current=prevCenter+
	((ci->blocksizes[v->lW]/4+
	ci->blocksizes[v->W]/4)>>hs);
	}

  }

  if(b->sample_count==-1){
	b->sample_count=0;
  }else{
	b->sample_count+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
  }

  if(v->granulepos==-1){
	if(vb->granulepos!=-1){ /* only set if we have a position to set to */

	  v->granulepos=vb->granulepos;

	  if(b->sample_count>v->granulepos){
	if(vb->eofflag){

	  v->pcm_current-=(b->sample_count-v->granulepos)>>hs;
	}else{
	  v->pcm_returned+=(b->sample_count-v->granulepos)>>hs;
	  if(v->pcm_returned>v->pcm_current)
		v->pcm_returned=v->pcm_current;
	}

	  }

	}
  }else{
	v->granulepos+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
	if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){

	  if(v->granulepos>vb->granulepos){
	long extra=v->granulepos-vb->granulepos;

	if(extra)
	  if(vb->eofflag){
		v->pcm_current-=extra>>hs;
	  } /* else {Shouldn't happen *unless* the bitstream is out of
		   spec.  Either way, believe the bitstream } */
	  } /* else {Shouldn't happen *unless* the bitstream is out of
	   spec.  Either way, believe the bitstream } */
	  v->granulepos=vb->granulepos;
	}
  }

  if(vb->eofflag)v->eofflag=1;
  return(0);

}

int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){
  vorbis_info *vi=v->vi;

  if(v->pcm_returned>-1 && v->pcm_returned<v->pcm_current){
	if(pcm){
	  int i;
	  for(i=0;i<vi->channels;i++)
	v->pcmret[i]=v->pcm[i]+v->pcm_returned;
	  *pcm=v->pcmret;
	}
	return(v->pcm_current-v->pcm_returned);
  }
  return(0);
}

int vorbis_synthesis_read(vorbis_dsp_state *v,int n){
  if(n && v->pcm_returned+n>v->pcm_current)return(OV_EINVAL);
  v->pcm_returned+=n;
  return(0);
}

int vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm){
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  int hs=ci->halfrate_flag;

  int n=ci->blocksizes[v->W]>>(hs+1);
  int n0=ci->blocksizes[0]>>(hs+1);
  int n1=ci->blocksizes[1]>>(hs+1);
  int i,j;

  if(v->pcm_returned<0)return 0;

  if(v->centerW==n1){
	for(j=0;j<vi->channels;j++){
	  float *p=v->pcm[j];
	  for(i=0;i<n1;i++){
	float temp=p[i];
	p[i]=p[i+n1];
	p[i+n1]=temp;
	  }
	}

	v->pcm_current-=n1;
	v->pcm_returned-=n1;
	v->centerW=0;
  }

  if((v->lW^v->W)==1){
	for(j=0;j<vi->channels;j++){
	  float *s=v->pcm[j];
	  float *d=v->pcm[j]+(n1-n0)/2;
	  for(i=(n1+n0)/2-1;i>=0;--i)
	d[i]=s[i];
	}
	v->pcm_returned+=(n1-n0)/2;
	v->pcm_current+=(n1-n0)/2;
  }else{
	if(v->lW==0){
	  for(j=0;j<vi->channels;j++){
	float *s=v->pcm[j];
	float *d=v->pcm[j]+n1-n0;
	for(i=n0-1;i>=0;--i)
	  d[i]=s[i];
	  }
	  v->pcm_returned+=n1-n0;
	  v->pcm_current+=n1-n0;
	}
  }

  if(pcm){
	int i;
	for(i=0;i<vi->channels;i++)
	  v->pcmret[i]=v->pcm[i]+v->pcm_returned;
	*pcm=v->pcmret;
  }

  return(n1+n-v->pcm_returned);

}

float *vorbis_window(vorbis_dsp_state *v,int W){
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=(codec_setup_info*) vi->codec_setup;
  int hs=ci->halfrate_flag;
  private_state *b=(private_state*)v->backend_state;

  if(b->window[W]-1<0)return NULL;
  return _vorbis_window_get(b->window[W]-hs);
}

#endif
/*** End of inlined file: block.c ***/


/*** Start of inlined file: codebook.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <math.h>

int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){
  long i,j;
  int ordered=0;

  oggpack_write(opb,0x564342,24);
  oggpack_write(opb,c->dim,16);
  oggpack_write(opb,c->entries,24);

  for(i=1;i<c->entries;i++)
	if(c->lengthlist[i-1]==0 || c->lengthlist[i]<c->lengthlist[i-1])break;
  if(i==c->entries)ordered=1;

  if(ordered){

	long count=0;
	oggpack_write(opb,1,1);  /* ordered */
	oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */

	for(i=1;i<c->entries;i++){
	  long thisx=c->lengthlist[i];
	  long last=c->lengthlist[i-1];
	  if(thisx>last){
	for(j=last;j<thisx;j++){
	  oggpack_write(opb,i-count,_ilog(c->entries-count));
	  count=i;
	}
	  }
	}
	oggpack_write(opb,i-count,_ilog(c->entries-count));

  }else{
	oggpack_write(opb,0,1);   /* unordered */

	for(i=0;i<c->entries;i++)
	  if(c->lengthlist[i]==0)break;

	if(i==c->entries){
	  oggpack_write(opb,0,1); /* no unused entries */
	  for(i=0;i<c->entries;i++)
	oggpack_write(opb,c->lengthlist[i]-1,5);
	}else{
	  oggpack_write(opb,1,1); /* we have unused entries; thus we tag */
	  for(i=0;i<c->entries;i++){
	if(c->lengthlist[i]==0){
	  oggpack_write(opb,0,1);
	}else{
	  oggpack_write(opb,1,1);
	  oggpack_write(opb,c->lengthlist[i]-1,5);
	}
	  }
	}
  }

  oggpack_write(opb,c->maptype,4);
  switch(c->maptype){
  case 0:
	break;
  case 1:case 2:

	if(!c->quantlist){
	  return(-1);
	}

	oggpack_write(opb,c->q_min,32);
	oggpack_write(opb,c->q_delta,32);
	oggpack_write(opb,c->q_quant-1,4);
	oggpack_write(opb,c->q_sequencep,1);

	{
	  int quantvals;
	  switch(c->maptype){
	  case 1:
	quantvals=_book_maptype1_quantvals(c);
	break;
	  case 2:
	quantvals=c->entries*c->dim;
	break;
	  default: /* NOT_REACHABLE */
	quantvals=-1;
	  }

	  for(i=0;i<quantvals;i++)
	oggpack_write(opb,labs(c->quantlist[i]),c->q_quant);

	}
	break;
  default:
	return(-1);
  }

  return(0);
}

int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){
  long i,j;
  memset(s,0,sizeof(*s));
  s->allocedp=1;

  if(oggpack_read(opb,24)!=0x564342)goto _eofout;

  s->dim=oggpack_read(opb,16);
  s->entries=oggpack_read(opb,24);
  if(s->entries==-1)goto _eofout;

  switch((int)oggpack_read(opb,1)){
  case 0:
	s->lengthlist=(long*)_ogg_malloc(sizeof(*s->lengthlist)*s->entries);

	if(oggpack_read(opb,1)){

	  for(i=0;i<s->entries;i++){
	if(oggpack_read(opb,1)){
	  long num=oggpack_read(opb,5);
	  if(num==-1)goto _eofout;
	  s->lengthlist[i]=num+1;
	}else
	  s->lengthlist[i]=0;
	  }
	}else{
	  for(i=0;i<s->entries;i++){
	long num=oggpack_read(opb,5);
	if(num==-1)goto _eofout;
	s->lengthlist[i]=num+1;
	  }
	}

	break;
  case 1:
	{
	  long length=oggpack_read(opb,5)+1;
	  s->lengthlist=(long*)_ogg_malloc(sizeof(*s->lengthlist)*s->entries);

	  for(i=0;i<s->entries;){
	long num=oggpack_read(opb,_ilog(s->entries-i));
	if(num==-1)goto _eofout;
	for(j=0;j<num && i<s->entries;j++,i++)
	  s->lengthlist[i]=length;
	length++;
	  }
	}
	break;
  default:
	return(-1);
  }

  switch((s->maptype=oggpack_read(opb,4))){
  case 0:
	break;
  case 1: case 2:

	s->q_min=oggpack_read(opb,32);
	s->q_delta=oggpack_read(opb,32);
	s->q_quant=oggpack_read(opb,4)+1;
	s->q_sequencep=oggpack_read(opb,1);

	{
	  int quantvals=0;
	  switch(s->maptype){
	  case 1:
	quantvals=_book_maptype1_quantvals(s);
	break;
	  case 2:
	quantvals=s->entries*s->dim;
	break;
	  }

	  s->quantlist=(long*)_ogg_malloc(sizeof(*s->quantlist)*quantvals);
	  for(i=0;i<quantvals;i++)
	s->quantlist[i]=oggpack_read(opb,s->q_quant);

	  if(quantvals&&s->quantlist[quantvals-1]==-1)goto _eofout;
	}
	break;
  default:
	goto _errout;
  }

  return(0);

 _errout:
 _eofout:
  vorbis_staticbook_clear(s);
  return(-1);
}

int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
  oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
  return(book->c->lengthlist[a]);
}

int vorbis_book_errorv(codebook *book,float *a){
  int dim=book->dim,k;
  int best=_best(book,a,1);
  for(k=0;k<dim;k++)
	a[k]=(book->valuelist+best*dim)[k];
  return(best);
}

int vorbis_book_encodev(codebook *book,int best,float *a,oggpack_buffer *b){
  int k,dim=book->dim;
  for(k=0;k<dim;k++)
	a[k]=(book->valuelist+best*dim)[k];
  return(vorbis_book_encode(book,best,b));
}

STIN long decode_packed_entry_number(codebook *book, oggpack_buffer *b){
  int  read=book->dec_maxlength;
  long lo,hi;
  long lok = oggpack_look(b,book->dec_firsttablen);

  if (lok >= 0) {
	long entry = book->dec_firsttable[lok];
	if(entry&0x80000000UL){
	  lo=(entry>>15)&0x7fff;
	  hi=book->used_entries-(entry&0x7fff);
	}else{
	  oggpack_adv(b, book->dec_codelengths[entry-1]);
	  return(entry-1);
	}
  }else{
	lo=0;
	hi=book->used_entries;
  }

  lok = oggpack_look(b, read);

  while(lok<0 && read>1)
	lok = oggpack_look(b, --read);
  if(lok<0)return -1;

  {
	ogg_uint32_t testword=ogg_bitreverse((ogg_uint32_t)lok);

	while(hi-lo>1){
	  long p=(hi-lo)>>1;
	  long test=book->codelist[lo+p]>testword;
	  lo+=p&(test-1);
	  hi-=p&(-test);
	}

	if(book->dec_codelengths[lo]<=read){
	  oggpack_adv(b, book->dec_codelengths[lo]);
	  return(lo);
	}
  }

  oggpack_adv(b, read);
  return(-1);
}

long vorbis_book_decode(codebook *book, oggpack_buffer *b){
  long packed_entry=decode_packed_entry_number(book,b);
  if(packed_entry>=0)
	return(book->dec_index[packed_entry]);

  return(packed_entry);
}

long vorbis_book_decodevs_add(codebook *book,float *a,oggpack_buffer *b,int n){
  int step=n/book->dim;
  long *entry = (long*)alloca(sizeof(*entry)*step);
  float **t = (float**)alloca(sizeof(*t)*step);
  int i,j,o;

  for (i = 0; i < step; i++) {
	entry[i]=decode_packed_entry_number(book,b);
	if(entry[i]==-1)return(-1);
	t[i] = book->valuelist+entry[i]*book->dim;
  }
  for(i=0,o=0;i<book->dim;i++,o+=step)
	for (j=0;j<step;j++)
	  a[o+j]+=t[j][i];
  return(0);
}

long vorbis_book_decodev_add(codebook *book,float *a,oggpack_buffer *b,int n){
  int i,j,entry;
  float *t;

  if(book->dim>8){
	for(i=0;i<n;){
	  entry = decode_packed_entry_number(book,b);
	  if(entry==-1)return(-1);
	  t	 = book->valuelist+entry*book->dim;
	  for (j=0;j<book->dim;)
	a[i++]+=t[j++];
	}
  }else{
	for(i=0;i<n;){
	  entry = decode_packed_entry_number(book,b);
	  if(entry==-1)return(-1);
	  t	 = book->valuelist+entry*book->dim;
	  j=0;
	  switch((int)book->dim){
	  case 8:
	a[i++]+=t[j++];
	  case 7:
	a[i++]+=t[j++];
	  case 6:
	a[i++]+=t[j++];
	  case 5:
	a[i++]+=t[j++];
	  case 4:
	a[i++]+=t[j++];
	  case 3:
	a[i++]+=t[j++];
	  case 2:
	a[i++]+=t[j++];
	  case 1:
	a[i++]+=t[j++];
	  case 0:
	break;
	  }
	}
  }
  return(0);
}

long vorbis_book_decodev_set(codebook *book,float *a,oggpack_buffer *b,int n){
  int i,j,entry;
  float *t;

  for(i=0;i<n;){
	entry = decode_packed_entry_number(book,b);
	if(entry==-1)return(-1);
	t	 = book->valuelist+entry*book->dim;
	for (j=0;j<book->dim;)
	  a[i++]=t[j++];
  }
  return(0);
}

long vorbis_book_decodevv_add(codebook *book,float **a,long offset,int ch,
				  oggpack_buffer *b,int n){
  long i,j,entry;
  int chptr=0;

  for(i=offset/ch;i<(offset+n)/ch;){
	entry = decode_packed_entry_number(book,b);
	if(entry==-1)return(-1);
	{
	  const float *t = book->valuelist+entry*book->dim;
	  for (j=0;j<book->dim;j++){
	a[chptr++][i]+=t[j];
	if(chptr==ch){
	  chptr=0;
	  i++;
	}
	  }
	}
  }
  return(0);
}

#ifdef _V_SELFTEST

#include <stdio.h>

#include "vorbis/book/lsp20_0.vqh"
#include "vorbis/book/res0a_13.vqh"
#define TESTSIZE 40

float test1[TESTSIZE]={
  0.105939f,
  0.215373f,
  0.429117f,
  0.587974f,

  0.181173f,
  0.296583f,
  0.515707f,
  0.715261f,

  0.162327f,
  0.263834f,
  0.342876f,
  0.406025f,

  0.103571f,
  0.223561f,
  0.368513f,
  0.540313f,

  0.136672f,
  0.395882f,
  0.587183f,
  0.652476f,

  0.114338f,
  0.417300f,
  0.525486f,
  0.698679f,

  0.147492f,
  0.324481f,
  0.643089f,
  0.757582f,

  0.139556f,
  0.215795f,
  0.324559f,
  0.399387f,

  0.120236f,
  0.267420f,
  0.446940f,
  0.608760f,

  0.115587f,
  0.287234f,
  0.571081f,
  0.708603f,
};

float test3[TESTSIZE]={
  0,1,-2,3,4,-5,6,7,8,9,
  8,-2,7,-1,4,6,8,3,1,-9,
  10,11,12,13,14,15,26,17,18,19,
  30,-25,-30,-1,-5,-32,4,3,-2,0};

static_codebook *testlist[]={&_vq_book_lsp20_0,
				 &_vq_book_res0a_13,NULL};
float   *testvec[]={test1,test3};

int main(){
  oggpack_buffer write;
  oggpack_buffer read;
  long ptr=0,i;
  oggpack_writeinit(&write);

  fprintf(stderr,"Testing codebook abstraction...:\n");

  while(testlist[ptr]){
	codebook c;
	static_codebook s;
	float *qv=alloca(sizeof(*qv)*TESTSIZE);
	float *iv=alloca(sizeof(*iv)*TESTSIZE);
	memcpy(qv,testvec[ptr],sizeof(*qv)*TESTSIZE);
	memset(iv,0,sizeof(*iv)*TESTSIZE);

	fprintf(stderr,"\tpacking/coding %ld... ",ptr);

	oggpack_reset(&write);
	vorbis_book_init_encode(&c,testlist[ptr]); /* get it into memory
												  we can write */
	vorbis_staticbook_pack(testlist[ptr],&write);
	fprintf(stderr,"Codebook size %ld bytes... ",oggpack_bytes(&write));
	for(i=0;i<TESTSIZE;i+=c.dim){
	  int best=_best(&c,qv+i,1);
	  vorbis_book_encodev(&c,best,qv+i,&write);
	}
	vorbis_book_clear(&c);

	fprintf(stderr,"OK.\n");
	fprintf(stderr,"\tunpacking/decoding %ld... ",ptr);

	oggpack_readinit(&read,oggpack_get_buffer(&write),oggpack_bytes(&write));
	if(vorbis_staticbook_unpack(&read,&s)){
	  fprintf(stderr,"Error unpacking codebook.\n");
	  exit(1);
	}
	if(vorbis_book_init_decode(&c,&s)){
	  fprintf(stderr,"Error initializing codebook.\n");
	  exit(1);
	}

	for(i=0;i<TESTSIZE;i+=c.dim)
	  if(vorbis_book_decodev_set(&c,iv+i,&read,c.dim)==-1){
	fprintf(stderr,"Error reading codebook test data (EOP).\n");
	exit(1);
	  }
	for(i=0;i<TESTSIZE;i++)
	  if(fabs(qv[i]-iv[i])>.000001){
	fprintf(stderr,"read (%g) != written (%g) at position (%ld)\n",
		iv[i],qv[i],i);
	exit(1);
	  }

	fprintf(stderr,"OK\n");
	ptr++;
  }

  exit(0);
}

#endif

#endif
/*** End of inlined file: codebook.c ***/


/*** Start of inlined file: envelope.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy_global *gi=&ci->psy_g_param;
  int ch=vi->channels;
  int i,j;
  int n=e->winlength=128;
  e->searchstep=64; /* not random */

  e->minenergy=gi->preecho_minenergy;
  e->ch=ch;
  e->storage=128;
  e->cursor=ci->blocksizes[1]/2;
  e->mdct_win=(float*)_ogg_calloc(n,sizeof(*e->mdct_win));
  mdct_init(&e->mdct,n);

  for(i=0;i<n;i++){
	e->mdct_win[i]=sin(i/(n-1.)*M_PI);
	e->mdct_win[i]*=e->mdct_win[i];
  }

  e->band[0].begin=2;  e->band[0].end=4;
  e->band[1].begin=4;  e->band[1].end=5;
  e->band[2].begin=6;  e->band[2].end=6;
  e->band[3].begin=9;  e->band[3].end=8;
  e->band[4].begin=13;  e->band[4].end=8;
  e->band[5].begin=17;  e->band[5].end=8;
  e->band[6].begin=22;  e->band[6].end=8;

  for(j=0;j<VE_BANDS;j++){
	n=e->band[j].end;
	e->band[j].window=(float*)_ogg_malloc(n*sizeof(*e->band[0].window));
	for(i=0;i<n;i++){
	  e->band[j].window[i]=sin((i+.5)/n*M_PI);
	  e->band[j].total+=e->band[j].window[i];
	}
	e->band[j].total=1./e->band[j].total;
  }

  e->filter=(envelope_filter_state*)_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
  e->mark=(int*)_ogg_calloc(e->storage,sizeof(*e->mark));

}

void _ve_envelope_clear(envelope_lookup *e){
  int i;
  mdct_clear(&e->mdct);
  for(i=0;i<VE_BANDS;i++)
	_ogg_free(e->band[i].window);
  _ogg_free(e->mdct_win);
  _ogg_free(e->filter);
  _ogg_free(e->mark);
  memset(e,0,sizeof(*e));
}

static int _ve_amp(envelope_lookup *ve,
		   vorbis_info_psy_global *gi,
		   float *data,
		   envelope_band *bands,
		   envelope_filter_state *filters,
		   long pos){
  long n=ve->winlength;
  int ret=0;
  long i,j;
  float decay;

  float minV=ve->minenergy;
  float *vec=(float*) alloca(n*sizeof(*vec));

  int stretch=max(VE_MINSTRETCH,ve->stretch/2);
  float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH);
  if(penalty<0.f)penalty=0.f;
  if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty;

  for(i=0;i<n;i++)
	vec[i]=data[i]*ve->mdct_win[i];
  mdct_forward(&ve->mdct,vec,vec);

  {
	float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
	int ptr=filters->nearptr;

	if(ptr==0){
	  decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
	  filters->nearDC_partialacc=temp;
	}else{
	  decay=filters->nearDC_acc+=temp;
	  filters->nearDC_partialacc+=temp;
	}
	filters->nearDC_acc-=filters->nearDC[ptr];
	filters->nearDC[ptr]=temp;

	decay*=(1./(VE_NEARDC+1));
	filters->nearptr++;
	if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
	decay=todB(&decay)*.5-15.f;
  }

  for(i=0;i<n/2;i+=2){
	float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
	val=todB(&val)*.5f;
	if(val<decay)val=decay;
	if(val<minV)val=minV;
	vec[i>>1]=val;
	decay-=8.;
  }

  for(j=0;j<VE_BANDS;j++){
	float acc=0.;
	float valmax,valmin;

	for(i=0;i<bands[j].end;i++)
	  acc+=vec[i+bands[j].begin]*bands[j].window[i];

	acc*=bands[j].total;

	{
	  int p,thisx=filters[j].ampptr;
	  float postmax,postmin,premax=-99999.f,premin=99999.f;

	  p=thisx;
	  p--;
	  if(p<0)p+=VE_AMP;
	  postmax=max(acc,filters[j].ampbuf[p]);
	  postmin=min(acc,filters[j].ampbuf[p]);

	  for(i=0;i<stretch;i++){
	p--;
	if(p<0)p+=VE_AMP;
	premax=max(premax,filters[j].ampbuf[p]);
	premin=min(premin,filters[j].ampbuf[p]);
	  }

	  valmin=postmin-premin;
	  valmax=postmax-premax;

	  filters[j].ampbuf[thisx]=acc;
	  filters[j].ampptr++;
	  if(filters[j].ampptr>=VE_AMP)filters[j].ampptr=0;
	}

	if(valmax>gi->preecho_thresh[j]+penalty){
	  ret|=1;
	  ret|=4;
	}
	if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
  }

  return(ret);
}

#if 0
static int seq=0;
static ogg_int64_t totalshift=-1024;
#endif

long _ve_envelope_search(vorbis_dsp_state *v){
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  vorbis_info_psy_global *gi=&ci->psy_g_param;
  envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
  long i,j;

  int first=ve->current/ve->searchstep;
  int last=v->pcm_current/ve->searchstep-VE_WIN;
  if(first<0)first=0;

  if(last+VE_WIN+VE_POST>ve->storage){
	ve->storage=last+VE_WIN+VE_POST; /* be sure */
	ve->mark=(int*)_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
  }

  for(j=first;j<last;j++){
	int ret=0;

	ve->stretch++;
	if(ve->stretch>VE_MAXSTRETCH*2)
	  ve->stretch=VE_MAXSTRETCH*2;

	for(i=0;i<ve->ch;i++){
	  float *pcm=v->pcm[i]+ve->searchstep*(j);
	  ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS,j);
	}

	ve->mark[j+VE_POST]=0;
	if(ret&1){
	  ve->mark[j]=1;
	  ve->mark[j+1]=1;
	}

	if(ret&2){
	  ve->mark[j]=1;
	  if(j>0)ve->mark[j-1]=1;
	}

	if(ret&4)ve->stretch=-1;
  }

  ve->current=last*ve->searchstep;

  {
	long centerW=v->centerW;
	long testW=
	  centerW+
	  ci->blocksizes[v->W]/4+
	  ci->blocksizes[1]/2+
	  ci->blocksizes[0]/4;

	j=ve->cursor;

	while(j<ve->current-(ve->searchstep)){/* account for postecho
											 working back one window */
	  if(j>=testW)return(1);

	  ve->cursor=j;

	  if(ve->mark[j/ve->searchstep]){
	if(j>centerW){

#if 0
	  if(j>ve->curmark){
		float *marker=alloca(v->pcm_current*sizeof(*marker));
		int l,m;
		memset(marker,0,sizeof(*marker)*v->pcm_current);
	    fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
			seq,
			(totalshift+ve->cursor)/44100.,
			(totalshift+j)/44100.);
	    _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
	    _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);

	    _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
	    _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);

		for(m=0;m<VE_BANDS;m++){
		  char buf[80];
	      sprintf(buf,"delL%d",m);
		  for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
		  _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
		}

		for(m=0;m<VE_BANDS;m++){
		  char buf[80];
	      sprintf(buf,"delR%d",m);
		  for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
		  _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
		}

		for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->mark[l]*.4;
	    _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift);

		seq++;

	  }
#endif

	  ve->curmark=j;
	  if(j>=testW)return(1);
	  return(0);
	}
	  }
	  j+=ve->searchstep;
	}
  }

  return(-1);
}

int _ve_envelope_mark(vorbis_dsp_state *v){
  envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
  vorbis_info *vi=v->vi;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  long centerW=v->centerW;
  long beginW=centerW-ci->blocksizes[v->W]/4;
  long endW=centerW+ci->blocksizes[v->W]/4;
  if(v->W){
	beginW-=ci->blocksizes[v->lW]/4;
	endW+=ci->blocksizes[v->nW]/4;
  }else{
	beginW-=ci->blocksizes[0]/4;
	endW+=ci->blocksizes[0]/4;
  }

  if(ve->curmark>=beginW && ve->curmark<endW)return(1);
  {
	long first=beginW/ve->searchstep;
	long last=endW/ve->searchstep;
	long i;
	for(i=first;i<last;i++)
	  if(ve->mark[i])return(1);
  }
  return(0);
}

void _ve_envelope_shift(envelope_lookup *e,long shift){
  int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks
							 ahead of ve->current */
  int smallshift=shift/e->searchstep;

  memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));

#if 0
  for(i=0;i<VE_BANDS*e->ch;i++)
	memmove(e->filter[i].markers,
		e->filter[i].markers+smallshift,
		(1024-smallshift)*sizeof(*(*e->filter).markers));
  totalshift+=shift;
#endif

  e->current-=shift;
  if(e->curmark>=0)
	e->curmark-=shift;
  e->cursor-=shift;
}

#endif
/*** End of inlined file: envelope.c ***/


/*** Start of inlined file: floor0.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <math.h>


/*** Start of inlined file: lsp.h ***/
#ifndef _V_LSP_H_
#define _V_LSP_H_

extern int vorbis_lpc_to_lsp(float *lpc,float *lsp,int m);

extern void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,
				float *lsp,int m,
				float amp,float ampoffset);

#endif
/*** End of inlined file: lsp.h ***/

#include <stdio.h>

typedef struct {
  int ln;
  int  m;
  int **linearmap;
  int  n[2];

  vorbis_info_floor0 *vi;

  long bits;
  long frames;
} vorbis_look_floor0;

static void floor0_free_info(vorbis_info_floor *i){
  vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  if(info){
	memset(info,0,sizeof(*info));
	_ogg_free(info);
  }
}

static void floor0_free_look(vorbis_look_floor *i){
  vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  if(look){

	if(look->linearmap){

	  if(look->linearmap[0])_ogg_free(look->linearmap[0]);
	  if(look->linearmap[1])_ogg_free(look->linearmap[1]);

	  _ogg_free(look->linearmap);
	}
	memset(look,0,sizeof(*look));
	_ogg_free(look);
  }
}

static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  int j;

  vorbis_info_floor0 *info=(vorbis_info_floor0*)_ogg_malloc(sizeof(*info));
  info->order=oggpack_read(opb,8);
  info->rate=oggpack_read(opb,16);
  info->barkmap=oggpack_read(opb,16);
  info->ampbits=oggpack_read(opb,6);
  info->ampdB=oggpack_read(opb,8);
  info->numbooks=oggpack_read(opb,4)+1;

  if(info->order<1)goto err_out;
  if(info->rate<1)goto err_out;
  if(info->barkmap<1)goto err_out;
  if(info->numbooks<1)goto err_out;

  for(j=0;j<info->numbooks;j++){
	info->books[j]=oggpack_read(opb,8);
	if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out;
  }
  return(info);

 err_out:
  floor0_free_info(info);
  return(NULL);
}

static void floor0_map_lazy_init(vorbis_block	  *vb,
				 vorbis_info_floor *infoX,
				 vorbis_look_floor0 *look){
  if(!look->linearmap[vb->W]){
	vorbis_dsp_state   *vd=vb->vd;
	vorbis_info	*vi=vd->vi;
	codec_setup_info   *ci=(codec_setup_info*)vi->codec_setup;
	vorbis_info_floor0 *info=(vorbis_info_floor0 *)infoX;
	int W=vb->W;
	int n=ci->blocksizes[W]/2,j;

	float scale=look->ln/toBARK(info->rate/2.f);

	look->linearmap[W]=(int*)_ogg_malloc((n+1)*sizeof(**look->linearmap));
	for(j=0;j<n;j++){
	  int val=floor( toBARK((info->rate/2.f)/n*j)
			 *scale); /* bark numbers represent band edges */
	  if(val>=look->ln)val=look->ln-1; /* guard against the approximation */
	  look->linearmap[W][j]=val;
	}
	look->linearmap[W][j]=-1;
	look->n[W]=n;
  }
}

static vorbis_look_floor *floor0_look(vorbis_dsp_state *vd,
					  vorbis_info_floor *i){
  vorbis_info_floor0 *info=(vorbis_info_floor0*)i;
  vorbis_look_floor0 *look=(vorbis_look_floor0*)_ogg_calloc(1,sizeof(*look));
  look->m=info->order;
  look->ln=info->barkmap;
  look->vi=info;

  look->linearmap=(int**)_ogg_calloc(2,sizeof(*look->linearmap));

  return look;
}

static void *floor0_inverse1(vorbis_block *vb,vorbis_look_floor *i){
  vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  vorbis_info_floor0 *info=look->vi;
  int j,k;

  int ampraw=oggpack_read(&vb->opb,info->ampbits);
  if(ampraw>0){ /* also handles the -1 out of data case */
	long maxval=(1<<info->ampbits)-1;
	float amp=(float)ampraw/maxval*info->ampdB;
	int booknum=oggpack_read(&vb->opb,_ilog(info->numbooks));

	if(booknum!=-1 && booknum<info->numbooks){ /* be paranoid */
	  codec_setup_info  *ci=(codec_setup_info *)vb->vd->vi->codec_setup;
	  codebook *b=ci->fullbooks+info->books[booknum];
	  float last=0.f;

	  float *lsp=(float*)_vorbis_block_alloc(vb,sizeof(*lsp)*(look->m+b->dim+1));

	  for(j=0;j<look->m;j+=b->dim)
	if(vorbis_book_decodev_set(b,lsp+j,&vb->opb,b->dim)==-1)goto eop;
	  for(j=0;j<look->m;){
	for(k=0;k<b->dim;k++,j++)lsp[j]+=last;
	last=lsp[j-1];
	  }

	  lsp[look->m]=amp;
	  return(lsp);
	}
  }
 eop:
  return(NULL);
}

static int floor0_inverse2(vorbis_block *vb,vorbis_look_floor *i,
			   void *memo,float *out){
  vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  vorbis_info_floor0 *info=look->vi;

  floor0_map_lazy_init(vb,info,look);

  if(memo){
	float *lsp=(float *)memo;
	float amp=lsp[look->m];

	vorbis_lsp_to_curve(out,
			look->linearmap[vb->W],
			look->n[vb->W],
			look->ln,
			lsp,look->m,amp,(float)info->ampdB);
	return(1);
  }
  memset(out,0,sizeof(*out)*look->n[vb->W]);
  return(0);
}

vorbis_func_floor floor0_exportbundle={
  NULL,&floor0_unpack,&floor0_look,&floor0_free_info,
  &floor0_free_look,&floor0_inverse1,&floor0_inverse2
};

#endif
/*** End of inlined file: floor0.c ***/


/*** Start of inlined file: floor1.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <stdio.h>

#define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */

typedef struct {
  int sorted_index[VIF_POSIT+2];
  int forward_index[VIF_POSIT+2];
  int reverse_index[VIF_POSIT+2];

  int hineighbor[VIF_POSIT];
  int loneighbor[VIF_POSIT];
  int posts;

  int n;
  int quant_q;
  vorbis_info_floor1 *vi;

  long phrasebits;
  long postbits;
  long frames;
} vorbis_look_floor1;

typedef struct lsfit_acc{
  long x0;
  long x1;

  long xa;
  long ya;
  long x2a;
  long y2a;
  long xya;
  long an;
} lsfit_acc;

static void floor1_free_info(vorbis_info_floor *i){
  vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  if(info){
	memset(info,0,sizeof(*info));
	_ogg_free(info);
  }
}

static void floor1_free_look(vorbis_look_floor *i){
  vorbis_look_floor1 *look=(vorbis_look_floor1 *)i;
  if(look){

	memset(look,0,sizeof(*look));
	_ogg_free(look);
  }
}

static void floor1_pack (vorbis_info_floor *i,oggpack_buffer *opb){
  vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  int j,k;
  int count=0;
  int rangebits;
  int maxposit=info->postlist[1];
  int maxclass=-1;

  oggpack_write(opb,info->partitions,5); /* only 0 to 31 legal */
  for(j=0;j<info->partitions;j++){
	oggpack_write(opb,info->partitionclass[j],4); /* only 0 to 15 legal */
	if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
  }

  for(j=0;j<maxclass+1;j++){
	oggpack_write(opb,info->class_dim[j]-1,3); /* 1 to 8 */
	oggpack_write(opb,info->class_subs[j],2); /* 0 to 3 */
	if(info->class_subs[j])oggpack_write(opb,info->class_book[j],8);
	for(k=0;k<(1<<info->class_subs[j]);k++)
	  oggpack_write(opb,info->class_subbook[j][k]+1,8);
  }

  oggpack_write(opb,info->mult-1,2);	 /* only 1,2,3,4 legal now */
  oggpack_write(opb,ilog2(maxposit),4);
  rangebits=ilog2(maxposit);

  for(j=0,k=0;j<info->partitions;j++){
	count+=info->class_dim[info->partitionclass[j]];
	for(;k<count;k++)
	  oggpack_write(opb,info->postlist[k+2],rangebits);
  }
}

static vorbis_info_floor *floor1_unpack (vorbis_info *vi,oggpack_buffer *opb){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  int j,k,count=0,maxclass=-1,rangebits;

  vorbis_info_floor1 *info=(vorbis_info_floor1*)_ogg_calloc(1,sizeof(*info));
  info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */
  for(j=0;j<info->partitions;j++){
	info->partitionclass[j]=oggpack_read(opb,4); /* only 0 to 15 legal */
	if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
  }

  for(j=0;j<maxclass+1;j++){
	info->class_dim[j]=oggpack_read(opb,3)+1; /* 1 to 8 */
	info->class_subs[j]=oggpack_read(opb,2); /* 0,1,2,3 bits */
	if(info->class_subs[j]<0)
	  goto err_out;
	if(info->class_subs[j])info->class_book[j]=oggpack_read(opb,8);
	if(info->class_book[j]<0 || info->class_book[j]>=ci->books)
	  goto err_out;
	for(k=0;k<(1<<info->class_subs[j]);k++){
	  info->class_subbook[j][k]=oggpack_read(opb,8)-1;
	  if(info->class_subbook[j][k]<-1 || info->class_subbook[j][k]>=ci->books)
	goto err_out;
	}
  }

  info->mult=oggpack_read(opb,2)+1;	 /* only 1,2,3,4 legal now */
  rangebits=oggpack_read(opb,4);

  for(j=0,k=0;j<info->partitions;j++){
	count+=info->class_dim[info->partitionclass[j]];
	for(;k<count;k++){
	  int t=info->postlist[k+2]=oggpack_read(opb,rangebits);
	  if(t<0 || t>=(1<<rangebits))
	goto err_out;
	}
  }
  info->postlist[0]=0;
  info->postlist[1]=1<<rangebits;

  return(info);

 err_out:
  floor1_free_info(info);
  return(NULL);
}

static int icomp(const void *a,const void *b){
  return(**(int **)a-**(int **)b);
}

static vorbis_look_floor *floor1_look(vorbis_dsp_state *vd,
					  vorbis_info_floor *in){

  int *sortpointer[VIF_POSIT+2];
  vorbis_info_floor1 *info=(vorbis_info_floor1*)in;
  vorbis_look_floor1 *look=(vorbis_look_floor1*)_ogg_calloc(1,sizeof(*look));
  int i,j,n=0;

  look->vi=info;
  look->n=info->postlist[1];

  for(i=0;i<info->partitions;i++)n+=info->class_dim[info->partitionclass[i]];
  n+=2;
  look->posts=n;

  for(i=0;i<n;i++)sortpointer[i]=info->postlist+i;
  qsort(sortpointer,n,sizeof(*sortpointer),icomp);

  for(i=0;i<n;i++)look->forward_index[i]=sortpointer[i]-info->postlist;
  for(i=0;i<n;i++)look->reverse_index[look->forward_index[i]]=i;
  for(i=0;i<n;i++)look->sorted_index[i]=info->postlist[look->forward_index[i]];

  switch(info->mult){
  case 1: /* 1024 -> 256 */
	look->quant_q=256;
	break;
  case 2: /* 1024 -> 128 */
	look->quant_q=128;
	break;
  case 3: /* 1024 -> 86 */
	look->quant_q=86;
	break;
  case 4: /* 1024 -> 64 */
	look->quant_q=64;
	break;
  }

  for(i=0;i<n-2;i++){
	int lo=0;
	int hi=1;
	int lx=0;
	int hx=look->n;
	int currentx=info->postlist[i+2];
	for(j=0;j<i+2;j++){
	  int x=info->postlist[j];
	  if(x>lx && x<currentx){
	lo=j;
	lx=x;
	  }
	  if(x<hx && x>currentx){
	hi=j;
	hx=x;
	  }
	}
	look->loneighbor[i]=lo;
	look->hineighbor[i]=hi;
  }

  return(look);
}

static int render_point(int x0,int x1,int y0,int y1,int x){
  y0&=0x7fff; /* mask off flag */
  y1&=0x7fff;

  {
	int dy=y1-y0;
	int adx=x1-x0;
	int ady=abs(dy);
	int err=ady*(x-x0);

	int off=err/adx;
	if(dy<0)return(y0-off);
	return(y0+off);
  }
}

static int vorbis_dBquant(const float *x){
  int i= *x*7.3142857f+1023.5f;
  if(i>1023)return(1023);
  if(i<0)return(0);
  return i;
}

static float FLOOR1_fromdB_LOOKUP[256]={
  1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
  1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
  1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
  2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
  2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
  3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
  4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
  6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
  7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
  1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
  1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
  1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
  2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
  2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
  3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
  4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
  5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
  7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
  9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
  1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
  1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
  2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
  2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
  3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
  4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
  5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
  7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
  9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
  0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
  0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
  0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
  0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
  0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
  0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
  0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
  0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
  0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
  0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
  0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
  0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
  0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
  0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
  0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
  0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
  0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
  0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
  0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
  0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
  0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
  0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
  0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
  0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
  0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
  0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
  0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
  0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
  0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
  0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
  0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
  0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
  0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
  0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
  0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
  0.82788260F, 0.88168307F, 0.9389798F, 1.F,
};

static void render_line(int x0,int x1,int y0,int y1,float *d){
  int dy=y1-y0;
  int adx=x1-x0;
  int ady=abs(dy);
  int base=dy/adx;
  int sy=(dy<0?base-1:base+1);
  int x=x0;
  int y=y0;
  int err=0;

  ady-=abs(base*adx);

  d[x]*=FLOOR1_fromdB_LOOKUP[y];
  while(++x<x1){
	err=err+ady;
	if(err>=adx){
	  err-=adx;
	  y+=sy;
	}else{
	  y+=base;
	}
	d[x]*=FLOOR1_fromdB_LOOKUP[y];
  }
}

static void render_line0(int x0,int x1,int y0,int y1,int *d){
  int dy=y1-y0;
  int adx=x1-x0;
  int ady=abs(dy);
  int base=dy/adx;
  int sy=(dy<0?base-1:base+1);
  int x=x0;
  int y=y0;
  int err=0;

  ady-=abs(base*adx);

  d[x]=y;
  while(++x<x1){
	err=err+ady;
	if(err>=adx){
	  err-=adx;
	  y+=sy;
	}else{
	  y+=base;
	}
	d[x]=y;
  }
}

static int accumulate_fit(const float *flr,const float *mdct,
			  int x0, int x1,lsfit_acc *a,
			  int n,vorbis_info_floor1 *info){
  long i;
  long xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0;

  memset(a,0,sizeof(*a));
  a->x0=x0;
  a->x1=x1;
  if(x1>=n)x1=n-1;

  for(i=x0;i<=x1;i++){
	int quantized=vorbis_dBquant(flr+i);
	if(quantized){
	  if(mdct[i]+info->twofitatten>=flr[i]){
	xa  += i;
	ya  += quantized;
	x2a += i*i;
	y2a += quantized*quantized;
	xya += i*quantized;
	na++;
	  }else{
	xb  += i;
	yb  += quantized;
	x2b += i*i;
	y2b += quantized*quantized;
	xyb += i*quantized;
	nb++;
	  }
	}
  }

  xb+=xa;
  yb+=ya;
  x2b+=x2a;
  y2b+=y2a;
  xyb+=xya;
  nb+=na;

  {
	int weight=nb*info->twofitweight/(na+1);

	a->xa=xa*weight+xb;
	a->ya=ya*weight+yb;
	a->x2a=x2a*weight+x2b;
	a->y2a=y2a*weight+y2b;
	a->xya=xya*weight+xyb;
	a->an=na*weight+nb;
  }

  return(na);
}

static void fit_line(lsfit_acc *a,int fits,int *y0,int *y1){
  long x=0,y=0,x2=0,y2=0,xy=0,an=0,i;
  long x0=a[0].x0;
  long x1=a[fits-1].x1;

  for(i=0;i<fits;i++){
	x+=a[i].xa;
	y+=a[i].ya;
	x2+=a[i].x2a;
	y2+=a[i].y2a;
	xy+=a[i].xya;
	an+=a[i].an;
  }

  if(*y0>=0){
	x+=   x0;
	y+=  *y0;
	x2+=  x0 *  x0;
	y2+= *y0 * *y0;
	xy+= *y0 *  x0;
	an++;
  }

  if(*y1>=0){
	x+=   x1;
	y+=  *y1;
	x2+=  x1 *  x1;
	y2+= *y1 * *y1;
	xy+= *y1 *  x1;
	an++;
  }

  if(an){
	double fx=x;
	double fy=y;
	double fx2=x2;
	double fxy=xy;
	double denom=1./(an*fx2-fx*fx);
	double a=(fy*fx2-fxy*fx)*denom;
	double b=(an*fxy-fx*fy)*denom;
	*y0=rint(a+b*x0);
	*y1=rint(a+b*x1);

	if(*y0>1023)*y0=1023;
	if(*y1>1023)*y1=1023;
	if(*y0<0)*y0=0;
	if(*y1<0)*y1=0;

  }else{
	*y0=0;
	*y1=0;
  }
}

static int inspect_error(int x0,int x1,int y0,int y1,const float *mask,
			 const float *mdct,
			 vorbis_info_floor1 *info){
  int dy=y1-y0;
  int adx=x1-x0;
  int ady=abs(dy);
  int base=dy/adx;
  int sy=(dy<0?base-1:base+1);
  int x=x0;
  int y=y0;
  int err=0;
  int val=vorbis_dBquant(mask+x);
  int mse=0;
  int n=0;

  ady-=abs(base*adx);

  mse=(y-val);
  mse*=mse;
  n++;
  if(mdct[x]+info->twofitatten>=mask[x]){
	if(y+info->maxover<val)return(1);
	if(y-info->maxunder>val)return(1);
  }

  while(++x<x1){
	err=err+ady;
	if(err>=adx){
	  err-=adx;
	  y+=sy;
	}else{
	  y+=base;
	}

	val=vorbis_dBquant(mask+x);
	mse+=((y-val)*(y-val));
	n++;
	if(mdct[x]+info->twofitatten>=mask[x]){
	  if(val){
	if(y+info->maxover<val)return(1);
	if(y-info->maxunder>val)return(1);
	  }
	}
  }

  if(info->maxover*info->maxover/n>info->maxerr)return(0);
  if(info->maxunder*info->maxunder/n>info->maxerr)return(0);
  if(mse/n>info->maxerr)return(1);
  return(0);
}

static int post_Y(int *A,int *B,int pos){
  if(A[pos]<0)
	return B[pos];
  if(B[pos]<0)
	return A[pos];

  return (A[pos]+B[pos])>>1;
}

int *floor1_fit(vorbis_block *vb,void *look_,
			  const float *logmdct,   /* in */
			  const float *logmask){
  long i,j;
  vorbis_look_floor1 *look = (vorbis_look_floor1*) look_;
  vorbis_info_floor1 *info=look->vi;
  long n=look->n;
  long posts=look->posts;
  long nonzero=0;
  lsfit_acc fits[VIF_POSIT+1];
  int fit_valueA[VIF_POSIT+2]; /* index by range list position */
  int fit_valueB[VIF_POSIT+2]; /* index by range list position */

  int loneighbor[VIF_POSIT+2]; /* sorted index of range list position (+2) */
  int hineighbor[VIF_POSIT+2];
  int *output=NULL;
  int memo[VIF_POSIT+2];

  for(i=0;i<posts;i++)fit_valueA[i]=-200; /* mark all unused */
  for(i=0;i<posts;i++)fit_valueB[i]=-200; /* mark all unused */
  for(i=0;i<posts;i++)loneighbor[i]=0; /* 0 for the implicit 0 post */
  for(i=0;i<posts;i++)hineighbor[i]=1; /* 1 for the implicit post at n */
  for(i=0;i<posts;i++)memo[i]=-1;	  /* no neighbor yet */

  if(posts==0){
	nonzero+=accumulate_fit(logmask,logmdct,0,n,fits,n,info);
  }else{
	for(i=0;i<posts-1;i++)
	  nonzero+=accumulate_fit(logmask,logmdct,look->sorted_index[i],
				  look->sorted_index[i+1],fits+i,
				  n,info);
  }

  if(nonzero){
	int y0=-200;
	int y1=-200;
	fit_line(fits,posts-1,&y0,&y1);

	fit_valueA[0]=y0;
	fit_valueB[0]=y0;
	fit_valueB[1]=y1;
	fit_valueA[1]=y1;

	for(i=2;i<posts;i++){
	  int sortpos=look->reverse_index[i];
	  int ln=loneighbor[sortpos];
	  int hn=hineighbor[sortpos];

	  if(memo[ln]!=hn){
	int lsortpos=look->reverse_index[ln];
	int hsortpos=look->reverse_index[hn];
	memo[ln]=hn;

	{
	  int lx=info->postlist[ln];
	  int hx=info->postlist[hn];
	  int ly=post_Y(fit_valueA,fit_valueB,ln);
	  int hy=post_Y(fit_valueA,fit_valueB,hn);

	  if(ly==-1 || hy==-1){
		exit(1);
	  }

	  if(inspect_error(lx,hx,ly,hy,logmask,logmdct,info)){
		int ly0=-200;
		int ly1=-200;
		int hy0=-200;
		int hy1=-200;
		fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1);
		fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1);

		fit_valueB[ln]=ly0;
		if(ln==0)fit_valueA[ln]=ly0;
		fit_valueA[i]=ly1;
		fit_valueB[i]=hy0;
		fit_valueA[hn]=hy1;
		if(hn==1)fit_valueB[hn]=hy1;

		if(ly1>=0 || hy0>=0){
		  for(j=sortpos-1;j>=0;j--)
		if(hineighbor[j]==hn)
		  hineighbor[j]=i;
		else
		  break;
		  for(j=sortpos+1;j<posts;j++)
		if(loneighbor[j]==ln)
		  loneighbor[j]=i;
		else
		  break;

		}
	  }else{

		fit_valueA[i]=-200;
		fit_valueB[i]=-200;
	  }
	}
	  }
	}

	output=(int*)_vorbis_block_alloc(vb,sizeof(*output)*posts);

	output[0]=post_Y(fit_valueA,fit_valueB,0);
	output[1]=post_Y(fit_valueA,fit_valueB,1);

	for(i=2;i<posts;i++){
	  int ln=look->loneighbor[i-2];
	  int hn=look->hineighbor[i-2];
	  int x0=info->postlist[ln];
	  int x1=info->postlist[hn];
	  int y0=output[ln];
	  int y1=output[hn];

	  int predicted=render_point(x0,x1,y0,y1,info->postlist[i]);
	  int vx=post_Y(fit_valueA,fit_valueB,i);

	  if(vx>=0 && predicted!=vx){
	output[i]=vx;
	  }else{
	output[i]= predicted|0x8000;
	  }
	}
  }

  return(output);

}

int *floor1_interpolate_fit(vorbis_block *vb,void *look_,
			  int *A,int *B,
			  int del){

  long i;
  vorbis_look_floor1* look = (vorbis_look_floor1*) look_;
  long posts=look->posts;
  int *output=NULL;

  if(A && B){
	output=(int*)_vorbis_block_alloc(vb,sizeof(*output)*posts);

	for(i=0;i<posts;i++){
	  output[i]=((65536-del)*(A[i]&0x7fff)+del*(B[i]&0x7fff)+32768)>>16;
	  if(A[i]&0x8000 && B[i]&0x8000)output[i]|=0x8000;
	}
  }

  return(output);
}

int floor1_encode(oggpack_buffer *opb,vorbis_block *vb,
		  void*look_,
		  int *post,int *ilogmask){

  long i,j;
  vorbis_look_floor1 *look = (vorbis_look_floor1 *) look_;
  vorbis_info_floor1 *info=look->vi;
  long posts=look->posts;
  codec_setup_info *ci=(codec_setup_info*)vb->vd->vi->codec_setup;
  int out[VIF_POSIT+2];
  static_codebook **sbooks=ci->book_param;
  codebook *books=ci->fullbooks;
  static long seq=0;

  if(post){
	for(i=0;i<posts;i++){
	  int val=post[i]&0x7fff;
	  switch(info->mult){
	  case 1: /* 1024 -> 256 */
	val>>=2;
	break;
	  case 2: /* 1024 -> 128 */
	val>>=3;
	break;
	  case 3: /* 1024 -> 86 */
	val/=12;
	break;
	  case 4: /* 1024 -> 64 */
	val>>=4;
	break;
	  }
	  post[i]=val | (post[i]&0x8000);
	}

	out[0]=post[0];
	out[1]=post[1];

	for(i=2;i<posts;i++){
	  int ln=look->loneighbor[i-2];
	  int hn=look->hineighbor[i-2];
	  int x0=info->postlist[ln];
	  int x1=info->postlist[hn];
	  int y0=post[ln];
	  int y1=post[hn];

	  int predicted=render_point(x0,x1,y0,y1,info->postlist[i]);

	  if((post[i]&0x8000) || (predicted==post[i])){
	post[i]=predicted|0x8000; /* in case there was roundoff jitter
					 in interpolation */
	out[i]=0;
	  }else{
	int headroom=(look->quant_q-predicted<predicted?
			  look->quant_q-predicted:predicted);

	int val=post[i]-predicted;

	if(val<0)
	  if(val<-headroom)
		val=headroom-val-1;
	  else
		val=-1-(val<<1);
	else
	  if(val>=headroom)
		val= val+headroom;
	  else
		val<<=1;

	out[i]=val;
	post[ln]&=0x7fff;
	post[hn]&=0x7fff;
	  }
	}

	oggpack_write(opb,1,1);

	look->frames++;
	look->postbits+=ilog(look->quant_q-1)*2;
	oggpack_write(opb,out[0],ilog(look->quant_q-1));
	oggpack_write(opb,out[1],ilog(look->quant_q-1));

	for(i=0,j=2;i<info->partitions;i++){
	  int classx=info->partitionclass[i];
	  int cdim=info->class_dim[classx];
	  int csubbits=info->class_subs[classx];
	  int csub=1<<csubbits;
	  int bookas[8]={0,0,0,0,0,0,0,0};
	  int cval=0;
	  int cshift=0;
	  int k,l;

	  if(csubbits){
	int maxval[8];
	for(k=0;k<csub;k++){
	  int booknum=info->class_subbook[classx][k];
	  if(booknum<0){
		maxval[k]=1;
	  }else{
		maxval[k]=sbooks[info->class_subbook[classx][k]]->entries;
	  }
	}
	for(k=0;k<cdim;k++){
	  for(l=0;l<csub;l++){
		int val=out[j+k];
		if(val<maxval[l]){
		  bookas[k]=l;
		  break;
		}
	  }
	  cval|= bookas[k]<<cshift;
	  cshift+=csubbits;
	}
	look->phrasebits+=
	  vorbis_book_encode(books+info->class_book[classx],cval,opb);

#ifdef TRAIN_FLOOR1
	{
	  FILE *of;
	  char buffer[80];
	  sprintf(buffer,"line_%dx%ld_class%d.vqd",
		  vb->pcmend/2,posts-2,class);
	  of=fopen(buffer,"a");
	  fprintf(of,"%d\n",cval);
	  fclose(of);
	}
#endif
	  }

	  for(k=0;k<cdim;k++){
	int book=info->class_subbook[classx][bookas[k]];
	if(book>=0){
	  if(out[j+k]<(books+book)->entries)
		look->postbits+=vorbis_book_encode(books+book,
						   out[j+k],opb);

#ifdef TRAIN_FLOOR1
	  {
		FILE *of;
		char buffer[80];
	    sprintf(buffer,"line_%dx%ld_%dsub%d.vqd",
			vb->pcmend/2,posts-2,class,bookas[k]);
	    of=fopen(buffer,"a");
	    fprintf(of,"%d\n",out[j+k]);
		fclose(of);
	  }
#endif
	}
	  }
	  j+=cdim;
	}

	{
	  int hx=0;
	  int lx=0;
	  int ly=post[0]*info->mult;
	  for(j=1;j<look->posts;j++){
	int current=look->forward_index[j];
	int hy=post[current]&0x7fff;
	if(hy==post[current]){

	  hy*=info->mult;
	  hx=info->postlist[current];

	  render_line0(lx,hx,ly,hy,ilogmask);

	  lx=hx;
	  ly=hy;
	}
	  }
	  for(j=hx;j<vb->pcmend/2;j++)ilogmask[j]=ly; /* be certain */
	  seq++;
	  return(1);
	}
  }else{
	oggpack_write(opb,0,1);
	memset(ilogmask,0,vb->pcmend/2*sizeof(*ilogmask));
	seq++;
	return(0);
  }
}

static void *floor1_inverse1(vorbis_block *vb,vorbis_look_floor *in){
  vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
  vorbis_info_floor1 *info=look->vi;
  codec_setup_info   *ci=(codec_setup_info*)vb->vd->vi->codec_setup;

  int i,j,k;
  codebook *books=ci->fullbooks;

  if(oggpack_read(&vb->opb,1)==1){
	int *fit_value=(int*)_vorbis_block_alloc(vb,(look->posts)*sizeof(*fit_value));

	fit_value[0]=oggpack_read(&vb->opb,ilog(look->quant_q-1));
	fit_value[1]=oggpack_read(&vb->opb,ilog(look->quant_q-1));

	for(i=0,j=2;i<info->partitions;i++){
	  int classx=info->partitionclass[i];
	  int cdim=info->class_dim[classx];
	  int csubbits=info->class_subs[classx];
	  int csub=1<<csubbits;
	  int cval=0;

	  if(csubbits){
	cval=vorbis_book_decode(books+info->class_book[classx],&vb->opb);

	if(cval==-1)goto eop;
	  }

	  for(k=0;k<cdim;k++){
	int book=info->class_subbook[classx][cval&(csub-1)];
	cval>>=csubbits;
	if(book>=0){
	  if((fit_value[j+k]=vorbis_book_decode(books+book,&vb->opb))==-1)
		goto eop;
	}else{
	  fit_value[j+k]=0;
	}
	  }
	  j+=cdim;
	}

	for(i=2;i<look->posts;i++){
	  int predicted=render_point(info->postlist[look->loneighbor[i-2]],
				 info->postlist[look->hineighbor[i-2]],
				 fit_value[look->loneighbor[i-2]],
				 fit_value[look->hineighbor[i-2]],
				 info->postlist[i]);
	  int hiroom=look->quant_q-predicted;
	  int loroom=predicted;
	  int room=(hiroom<loroom?hiroom:loroom)<<1;
	  int val=fit_value[i];

	  if(val){
	if(val>=room){
	  if(hiroom>loroom){
		val = val-loroom;
	  }else{
		val = -1-(val-hiroom);
	  }
	}else{
	  if(val&1){
		val= -((val+1)>>1);
	  }else{
		val>>=1;
	  }
	}

	fit_value[i]=val+predicted;
	fit_value[look->loneighbor[i-2]]&=0x7fff;
	fit_value[look->hineighbor[i-2]]&=0x7fff;

	  }else{
	fit_value[i]=predicted|0x8000;
	  }

	}

	return(fit_value);
  }
 eop:
  return(NULL);
}

static int floor1_inverse2(vorbis_block *vb,vorbis_look_floor *in,void *memo,
			  float *out){
  vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
  vorbis_info_floor1 *info=look->vi;

  codec_setup_info   *ci=(codec_setup_info*)vb->vd->vi->codec_setup;
  int		  n=ci->blocksizes[vb->W]/2;
  int j;

  if(memo){
	int *fit_value=(int *)memo;
	int hx=0;
	int lx=0;
	int ly=fit_value[0]*info->mult;
	for(j=1;j<look->posts;j++){
	  int current=look->forward_index[j];
	  int hy=fit_value[current]&0x7fff;
	  if(hy==fit_value[current]){

	hy*=info->mult;
	hx=info->postlist[current];

	render_line(lx,hx,ly,hy,out);

	lx=hx;
	ly=hy;
	  }
	}
	for(j=hx;j<n;j++)out[j]*=FLOOR1_fromdB_LOOKUP[ly]; /* be certain */
	return(1);
  }
  memset(out,0,sizeof(*out)*n);
  return(0);
}

vorbis_func_floor floor1_exportbundle={
  &floor1_pack,&floor1_unpack,&floor1_look,&floor1_free_info,
  &floor1_free_look,&floor1_inverse1,&floor1_inverse2
};

#endif
/*** End of inlined file: floor1.c ***/


/*** Start of inlined file: info.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static void _v_writestring(oggpack_buffer *o, const char *s, int bytes){

  while(bytes--){
	oggpack_write(o,*s++,8);
  }
}

static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
  while(bytes--){
	*buf++=oggpack_read(o,8);
  }
}

void vorbis_comment_init(vorbis_comment *vc){
  memset(vc,0,sizeof(*vc));
}

void vorbis_comment_add(vorbis_comment *vc,char *comment){
  vc->user_comments=(char**)_ogg_realloc(vc->user_comments,
				(vc->comments+2)*sizeof(*vc->user_comments));
  vc->comment_lengths=(int*)_ogg_realloc(vc->comment_lengths,
	  				(vc->comments+2)*sizeof(*vc->comment_lengths));
  vc->comment_lengths[vc->comments]=strlen(comment);
  vc->user_comments[vc->comments]=(char*)_ogg_malloc(vc->comment_lengths[vc->comments]+1);
  strcpy(vc->user_comments[vc->comments], comment);
  vc->comments++;
  vc->user_comments[vc->comments]=NULL;
}

void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, char *contents){
  char *comment=(char*)alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
  strcpy(comment, tag);
  strcat(comment, "=");
  strcat(comment, contents);
  vorbis_comment_add(vc, comment);
}

static int tagcompare(const char *s1, const char *s2, int n){
  int c=0;
  while(c < n){
	if(toupper(s1[c]) != toupper(s2[c]))
	  return !0;
	c++;
  }
  return 0;
}

char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
  long i;
  int found = 0;
  int taglen = strlen(tag)+1; /* +1 for the = we append */
  char *fulltag = (char*)alloca(taglen+ 1);

  strcpy(fulltag, tag);
  strcat(fulltag, "=");

  for(i=0;i<vc->comments;i++){
	if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
	  if(count == found)
	  	return vc->user_comments[i] + taglen;
	  else
	found++;
	}
  }
  return NULL; /* didn't find anything */
}

int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
  int i,count=0;
  int taglen = strlen(tag)+1; /* +1 for the = we append */
  char *fulltag = (char*)alloca(taglen+1);
  strcpy(fulltag,tag);
  strcat(fulltag, "=");

  for(i=0;i<vc->comments;i++){
	if(!tagcompare(vc->user_comments[i], fulltag, taglen))
	  count++;
  }

  return count;
}

void vorbis_comment_clear(vorbis_comment *vc){
  if(vc){
	long i;
	for(i=0;i<vc->comments;i++)
	  if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
	if(vc->user_comments)_ogg_free(vc->user_comments);
	if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
	if(vc->vendor)_ogg_free(vc->vendor);
  }
  memset(vc,0,sizeof(*vc));
}

int vorbis_info_blocksize(vorbis_info *vi,int zo){
  codec_setup_info *ci = (codec_setup_info*)vi->codec_setup;
  return ci ? ci->blocksizes[zo] : -1;
}

void vorbis_info_init(vorbis_info *vi){
  memset(vi,0,sizeof(*vi));
  vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
}

void vorbis_info_clear(vorbis_info *vi){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  int i;

  if(ci){

	for(i=0;i<ci->modes;i++)
	  if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);

	for(i=0;i<ci->maps;i++) /* unpack does the range checking */
	  _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);

	for(i=0;i<ci->floors;i++) /* unpack does the range checking */
	  _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);

	for(i=0;i<ci->residues;i++) /* unpack does the range checking */
	  _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);

	for(i=0;i<ci->books;i++){
	  if(ci->book_param[i]){
	vorbis_staticbook_destroy(ci->book_param[i]);
	  }
	  if(ci->fullbooks)
	vorbis_book_clear(ci->fullbooks+i);
	}
	if(ci->fullbooks)
	_ogg_free(ci->fullbooks);

	for(i=0;i<ci->psys;i++)
	  _vi_psy_free(ci->psy_param[i]);

	_ogg_free(ci);
  }

  memset(vi,0,sizeof(*vi));
}

static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  if(!ci)return(OV_EFAULT);

  vi->version=oggpack_read(opb,32);
  if(vi->version!=0)return(OV_EVERSION);

  vi->channels=oggpack_read(opb,8);
  vi->rate=oggpack_read(opb,32);

  vi->bitrate_upper=oggpack_read(opb,32);
  vi->bitrate_nominal=oggpack_read(opb,32);
  vi->bitrate_lower=oggpack_read(opb,32);

  ci->blocksizes[0]=1<<oggpack_read(opb,4);
  ci->blocksizes[1]=1<<oggpack_read(opb,4);

  if(vi->rate<1)goto err_out;
  if(vi->channels<1)goto err_out;
  if(ci->blocksizes[0]<8)goto err_out;
  if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;

  if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */

  return(0);
 err_out:
  vorbis_info_clear(vi);
  return(OV_EBADHEADER);
}

static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
  int i;
  int vendorlen=oggpack_read(opb,32);
  if(vendorlen<0)goto err_out;
  vc->vendor=(char*)_ogg_calloc(vendorlen+1,1);
  _v_readstring(opb,vc->vendor,vendorlen);
  vc->comments=oggpack_read(opb,32);
  if(vc->comments<0)goto err_out;
  vc->user_comments=(char**)_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
  vc->comment_lengths=(int*)_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));

  for(i=0;i<vc->comments;i++){
	int len=oggpack_read(opb,32);
	if(len<0)goto err_out;
	vc->comment_lengths[i]=len;
	vc->user_comments[i]=(char*)_ogg_calloc(len+1,1);
	_v_readstring(opb,vc->user_comments[i],len);
  }
  if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */

  return(0);
 err_out:
  vorbis_comment_clear(vc);
  return(OV_EBADHEADER);
}

static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  int i;
  if(!ci)return(OV_EFAULT);

  ci->books=oggpack_read(opb,8)+1;
  for(i=0;i<ci->books;i++){
	ci->book_param[i]=(static_codebook*)_ogg_calloc(1,sizeof(*ci->book_param[i]));
	if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
  }

  {
	int times=oggpack_read(opb,6)+1;
	for(i=0;i<times;i++){
	  int test=oggpack_read(opb,16);
	  if(test<0 || test>=VI_TIMEB)goto err_out;
	}
  }

  ci->floors=oggpack_read(opb,6)+1;
  for(i=0;i<ci->floors;i++){
	ci->floor_type[i]=oggpack_read(opb,16);
	if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
	ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
	if(!ci->floor_param[i])goto err_out;
  }

  ci->residues=oggpack_read(opb,6)+1;
  for(i=0;i<ci->residues;i++){
	ci->residue_type[i]=oggpack_read(opb,16);
	if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
	ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
	if(!ci->residue_param[i])goto err_out;
  }

  ci->maps=oggpack_read(opb,6)+1;
  for(i=0;i<ci->maps;i++){
	ci->map_type[i]=oggpack_read(opb,16);
	if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
	ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
	if(!ci->map_param[i])goto err_out;
  }

  ci->modes=oggpack_read(opb,6)+1;
  for(i=0;i<ci->modes;i++){
	ci->mode_param[i]=(vorbis_info_mode*)_ogg_calloc(1,sizeof(*ci->mode_param[i]));
	ci->mode_param[i]->blockflag=oggpack_read(opb,1);
	ci->mode_param[i]->windowtype=oggpack_read(opb,16);
	ci->mode_param[i]->transformtype=oggpack_read(opb,16);
	ci->mode_param[i]->mapping=oggpack_read(opb,8);

	if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
	if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
	if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
  }

  if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */

  return(0);
 err_out:
  vorbis_info_clear(vi);
  return(OV_EBADHEADER);
}

int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
  oggpack_buffer opb;

  if(op){
	oggpack_readinit(&opb,op->packet,op->bytes);

	{
	  char buffer[6];
	  int packtype=oggpack_read(&opb,8);
	  memset(buffer,0,6);
	  _v_readstring(&opb,buffer,6);
	  if(memcmp(buffer,"vorbis",6)){
	return(OV_ENOTVORBIS);
	  }
	  switch(packtype){
	  case 0x01: /* least significant *bit* is read first */
	if(!op->b_o_s){
	  return(OV_EBADHEADER);
	}
	if(vi->rate!=0){
	  return(OV_EBADHEADER);
	}

	return(_vorbis_unpack_info(vi,&opb));

	  case 0x03: /* least significant *bit* is read first */
	if(vi->rate==0){
	  return(OV_EBADHEADER);
	}

	return(_vorbis_unpack_comment(vc,&opb));

	  case 0x05: /* least significant *bit* is read first */
	if(vi->rate==0 || vc->vendor==NULL){
	  return(OV_EBADHEADER);
	}

	return(_vorbis_unpack_books(vi,&opb));

	  default:
	return(OV_EBADHEADER);
	break;
	  }
	}
  }
  return(OV_EBADHEADER);
}

static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  if(!ci)return(OV_EFAULT);

  oggpack_write(opb,0x01,8);
  _v_writestring(opb,"vorbis", 6);

  oggpack_write(opb,0x00,32);
  oggpack_write(opb,vi->channels,8);
  oggpack_write(opb,vi->rate,32);

  oggpack_write(opb,vi->bitrate_upper,32);
  oggpack_write(opb,vi->bitrate_nominal,32);
  oggpack_write(opb,vi->bitrate_lower,32);

  oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
  oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
  oggpack_write(opb,1,1);

  return(0);
}

static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
  char temp[]="Xiph.Org libVorbis I 20050304";
  int bytes = strlen(temp);

  oggpack_write(opb,0x03,8);
  _v_writestring(opb,"vorbis", 6);

  oggpack_write(opb,bytes,32);
  _v_writestring(opb,temp, bytes);

  oggpack_write(opb,vc->comments,32);
  if(vc->comments){
	int i;
	for(i=0;i<vc->comments;i++){
	  if(vc->user_comments[i]){
	oggpack_write(opb,vc->comment_lengths[i],32);
	_v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
	  }else{
	oggpack_write(opb,0,32);
	  }
	}
  }
  oggpack_write(opb,1,1);

  return(0);
}

static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  int i;
  if(!ci)return(OV_EFAULT);

  oggpack_write(opb,0x05,8);
  _v_writestring(opb,"vorbis", 6);

  oggpack_write(opb,ci->books-1,8);
  for(i=0;i<ci->books;i++)
	if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;

  oggpack_write(opb,0,6);
  oggpack_write(opb,0,16);

  oggpack_write(opb,ci->floors-1,6);
  for(i=0;i<ci->floors;i++){
	oggpack_write(opb,ci->floor_type[i],16);
	if(_floor_P[ci->floor_type[i]]->pack)
	  _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
	else
	  goto err_out;
  }

  oggpack_write(opb,ci->residues-1,6);
  for(i=0;i<ci->residues;i++){
	oggpack_write(opb,ci->residue_type[i],16);
	_residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
  }

  oggpack_write(opb,ci->maps-1,6);
  for(i=0;i<ci->maps;i++){
	oggpack_write(opb,ci->map_type[i],16);
	_mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
  }

  oggpack_write(opb,ci->modes-1,6);
  for(i=0;i<ci->modes;i++){
	oggpack_write(opb,ci->mode_param[i]->blockflag,1);
	oggpack_write(opb,ci->mode_param[i]->windowtype,16);
	oggpack_write(opb,ci->mode_param[i]->transformtype,16);
	oggpack_write(opb,ci->mode_param[i]->mapping,8);
  }
  oggpack_write(opb,1,1);

  return(0);
err_out:
  return(-1);
}

int vorbis_commentheader_out(vorbis_comment *vc,
						  ogg_packet *op){

  oggpack_buffer opb;

  oggpack_writeinit(&opb);
  if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;

  op->packet = (unsigned char*) _ogg_malloc(oggpack_bytes(&opb));
  memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));

  op->bytes=oggpack_bytes(&opb);
  op->b_o_s=0;
  op->e_o_s=0;
  op->granulepos=0;
  op->packetno=1;

  return 0;
}

int vorbis_analysis_headerout(vorbis_dsp_state *v,
				  vorbis_comment *vc,
				  ogg_packet *op,
				  ogg_packet *op_comm,
				  ogg_packet *op_code){
  int ret=OV_EIMPL;
  vorbis_info *vi=v->vi;
  oggpack_buffer opb;
  private_state *b=(private_state*)v->backend_state;

  if(!b){
	ret=OV_EFAULT;
	goto err_out;
  }

  oggpack_writeinit(&opb);
  if(_vorbis_pack_info(&opb,vi))goto err_out;

  if(b->header)_ogg_free(b->header);
  b->header=(unsigned char*) _ogg_malloc(oggpack_bytes(&opb));
  memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
  op->packet=b->header;
  op->bytes=oggpack_bytes(&opb);
  op->b_o_s=1;
  op->e_o_s=0;
  op->granulepos=0;
  op->packetno=0;

  oggpack_reset(&opb);
  if(_vorbis_pack_comment(&opb,vc))goto err_out;

  if(b->header1)_ogg_free(b->header1);
  b->header1=(unsigned char*) _ogg_malloc(oggpack_bytes(&opb));
  memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
  op_comm->packet=b->header1;
  op_comm->bytes=oggpack_bytes(&opb);
  op_comm->b_o_s=0;
  op_comm->e_o_s=0;
  op_comm->granulepos=0;
  op_comm->packetno=1;

  oggpack_reset(&opb);
  if(_vorbis_pack_books(&opb,vi))goto err_out;

  if(b->header2)_ogg_free(b->header2);
  b->header2=(unsigned char*) _ogg_malloc(oggpack_bytes(&opb));
  memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
  op_code->packet=b->header2;
  op_code->bytes=oggpack_bytes(&opb);
  op_code->b_o_s=0;
  op_code->e_o_s=0;
  op_code->granulepos=0;
  op_code->packetno=2;

  oggpack_writeclear(&opb);
  return(0);
 err_out:
  oggpack_writeclear(&opb);
  memset(op,0,sizeof(*op));
  memset(op_comm,0,sizeof(*op_comm));
  memset(op_code,0,sizeof(*op_code));

  if(b->header)_ogg_free(b->header);
  if(b->header1)_ogg_free(b->header1);
  if(b->header2)_ogg_free(b->header2);
  b->header=NULL;
  b->header1=NULL;
  b->header2=NULL;
  return(ret);
}

double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
  if(granulepos>=0)
	return((double)granulepos/v->vi->rate);
  return(-1);
}

#endif
/*** End of inlined file: info.c ***/


/*** Start of inlined file: lpc.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <math.h>

float vorbis_lpc_from_data(float *data,float *lpci,int n,int m){
  double *aut=(double*)alloca(sizeof(*aut)*(m+1));
  double *lpc=(double*)alloca(sizeof(*lpc)*(m));
  double error;
  int i,j;

  j=m+1;
  while(j--){
	double d=0; /* double needed for accumulator depth */
	for(i=j;i<n;i++)d+=(double)data[i]*data[i-j];
	aut[j]=d;
  }

  error=aut[0];

  for(i=0;i<m;i++){
	double r= -aut[i+1];

	if(error==0){
	  memset(lpci,0,m*sizeof(*lpci));
	  return 0;
	}

	for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
	r/=error;

	lpc[i]=r;
	for(j=0;j<i/2;j++){
	  double tmp=lpc[j];

	  lpc[j]+=r*lpc[i-1-j];
	  lpc[i-1-j]+=r*tmp;
	}
	if(i%2)lpc[j]+=lpc[j]*r;

	error*=1.f-r*r;
  }

  for(j=0;j<m;j++)lpci[j]=(float)lpc[j];

  return error;
}

void vorbis_lpc_predict(float *coeff,float *prime,int m,
					 float *data,long n){

  long i,j,o,p;
  float y;
  float *work=(float*)alloca(sizeof(*work)*(m+n));

  if(!prime)
	for(i=0;i<m;i++)
	  work[i]=0.f;
  else
	for(i=0;i<m;i++)
	  work[i]=prime[i];

  for(i=0;i<n;i++){
	y=0;
	o=i;
	p=m;
	for(j=0;j<m;j++)
	  y-=work[o++]*coeff[--p];

	data[i]=work[o]=y;
  }
}

#endif
/*** End of inlined file: lpc.c ***/


/*** Start of inlined file: lsp.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <math.h>
#include <string.h>
#include <stdlib.h>


/*** Start of inlined file: lookup.h ***/
#ifndef _V_LOOKUP_H_

#ifdef FLOAT_LOOKUP
extern float vorbis_coslook(float a);
extern float vorbis_invsqlook(float a);
extern float vorbis_invsq2explook(int a);
extern float vorbis_fromdBlook(float a);
#endif
#ifdef INT_LOOKUP
extern long vorbis_invsqlook_i(long a,long e);
extern long vorbis_coslook_i(long a);
extern float vorbis_fromdBlook_i(long a);
#endif

#endif
/*** End of inlined file: lookup.h ***/

#define   FLOAT_LOOKUP
#undef	INT_LOOKUP

#ifdef FLOAT_LOOKUP

/*** Start of inlined file: lookup.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <math.h>

/*** Start of inlined file: lookup.h ***/
#ifndef _V_LOOKUP_H_

#ifdef FLOAT_LOOKUP
extern float vorbis_coslook(float a);
extern float vorbis_invsqlook(float a);
extern float vorbis_invsq2explook(int a);
extern float vorbis_fromdBlook(float a);
#endif
#ifdef INT_LOOKUP
extern long vorbis_invsqlook_i(long a,long e);
extern long vorbis_coslook_i(long a);
extern float vorbis_fromdBlook_i(long a);
#endif

#endif
/*** End of inlined file: lookup.h ***/



/*** Start of inlined file: lookup_data.h ***/
#ifndef _V_LOOKUP_DATA_H_

#ifdef FLOAT_LOOKUP
#define COS_LOOKUP_SZ 128
static float COS_LOOKUP[COS_LOOKUP_SZ+1]={
	+1.0000000000000f,+0.9996988186962f,+0.9987954562052f,+0.9972904566787f,
	+0.9951847266722f,+0.9924795345987f,+0.9891765099648f,+0.9852776423889f,
	+0.9807852804032f,+0.9757021300385f,+0.9700312531945f,+0.9637760657954f,
	+0.9569403357322f,+0.9495281805930f,+0.9415440651830f,+0.9329927988347f,
	+0.9238795325113f,+0.9142097557035f,+0.9039892931234f,+0.8932243011955f,
	+0.8819212643484f,+0.8700869911087f,+0.8577286100003f,+0.8448535652497f,
	+0.8314696123025f,+0.8175848131516f,+0.8032075314806f,+0.7883464276266f,
	+0.7730104533627f,+0.7572088465065f,+0.7409511253550f,+0.7242470829515f,
	+0.7071067811865f,+0.6895405447371f,+0.6715589548470f,+0.6531728429538f,
	+0.6343932841636f,+0.6152315905806f,+0.5956993044924f,+0.5758081914178f,
	+0.5555702330196f,+0.5349976198871f,+0.5141027441932f,+0.4928981922298f,
	+0.4713967368260f,+0.4496113296546f,+0.4275550934303f,+0.4052413140050f,
	+0.3826834323651f,+0.3598950365350f,+0.3368898533922f,+0.3136817403989f,
	+0.2902846772545f,+0.2667127574749f,+0.2429801799033f,+0.2191012401569f,
	+0.1950903220161f,+0.1709618887603f,+0.1467304744554f,+0.1224106751992f,
	+0.0980171403296f,+0.0735645635997f,+0.0490676743274f,+0.0245412285229f,
	+0.0000000000000f,-0.0245412285229f,-0.0490676743274f,-0.0735645635997f,
	-0.0980171403296f,-0.1224106751992f,-0.1467304744554f,-0.1709618887603f,
	-0.1950903220161f,-0.2191012401569f,-0.2429801799033f,-0.2667127574749f,
	-0.2902846772545f,-0.3136817403989f,-0.3368898533922f,-0.3598950365350f,
	-0.3826834323651f,-0.4052413140050f,-0.4275550934303f,-0.4496113296546f,
	-0.4713967368260f,-0.4928981922298f,-0.5141027441932f,-0.5349976198871f,
	-0.5555702330196f,-0.5758081914178f,-0.5956993044924f,-0.6152315905806f,
	-0.6343932841636f,-0.6531728429538f,-0.6715589548470f,-0.6895405447371f,
	-0.7071067811865f,-0.7242470829515f,-0.7409511253550f,-0.7572088465065f,
	-0.7730104533627f,-0.7883464276266f,-0.8032075314806f,-0.8175848131516f,
	-0.8314696123025f,-0.8448535652497f,-0.8577286100003f,-0.8700869911087f,
	-0.8819212643484f,-0.8932243011955f,-0.9039892931234f,-0.9142097557035f,
	-0.9238795325113f,-0.9329927988347f,-0.9415440651830f,-0.9495281805930f,
	-0.9569403357322f,-0.9637760657954f,-0.9700312531945f,-0.9757021300385f,
	-0.9807852804032f,-0.9852776423889f,-0.9891765099648f,-0.9924795345987f,
	-0.9951847266722f,-0.9972904566787f,-0.9987954562052f,-0.9996988186962f,
	-1.0000000000000f,
};

#define INVSQ_LOOKUP_SZ 32
static float INVSQ_LOOKUP[INVSQ_LOOKUP_SZ+1]={
	1.414213562373f,1.392621247646f,1.371988681140f,1.352246807566f,
	1.333333333333f,1.315191898443f,1.297771369046f,1.281025230441f,
	1.264911064067f,1.249390095109f,1.234426799697f,1.219988562661f,
	1.206045378311f,1.192569588000f,1.179535649239f,1.166919931983f,
	1.154700538379f,1.142857142857f,1.131370849898f,1.120224067222f,
	1.109400392450f,1.098884511590f,1.088662107904f,1.078719779941f,
	1.069044967650f,1.059625885652f,1.050451462878f,1.041511287847f,
	1.032795558989f,1.024295039463f,1.016001016002f,1.007905261358f,
	1.000000000000f,
};

#define INVSQ2EXP_LOOKUP_MIN (-32)
#define INVSQ2EXP_LOOKUP_MAX 32
static float INVSQ2EXP_LOOKUP[INVSQ2EXP_LOOKUP_MAX-\
							  INVSQ2EXP_LOOKUP_MIN+1]={
		 65536.f,	46340.95001f,	 32768.f,	23170.47501f,
		 16384.f,	 11585.2375f,	  8192.f,	5792.618751f,
		  4096.f,	2896.309376f,	  2048.f,	1448.154688f,
		  1024.f,	724.0773439f,	   512.f,	 362.038672f,
		   256.f,	 181.019336f,	   128.f,	90.50966799f,
			64.f,	  45.254834f,		32.f,	  22.627417f,
			16.f,	 11.3137085f,		 8.f,	5.656854249f,
			 4.f,	2.828427125f,		 2.f,	1.414213562f,
			 1.f,   0.7071067812f,		0.5f,   0.3535533906f,
		   0.25f,   0.1767766953f,	  0.125f,  0.08838834765f,
		 0.0625f,  0.04419417382f,	0.03125f,  0.02209708691f,
		   0.015625f,  0.01104854346f,	  0.0078125f, 0.005524271728f,
		 0.00390625f, 0.002762135864f,	0.001953125f, 0.001381067932f,
	   0.0009765625f, 0.000690533966f,  0.00048828125f, 0.000345266983f,
	 0.000244140625f,0.0001726334915f,0.0001220703125f,8.631674575e-05f,
	6.103515625e-05f,4.315837288e-05f,3.051757812e-05f,2.157918644e-05f,
	1.525878906e-05f,
};

#endif

#define FROMdB_LOOKUP_SZ 35
#define FROMdB2_LOOKUP_SZ 32
#define FROMdB_SHIFT 5
#define FROMdB2_SHIFT 3
#define FROMdB2_MASK 31
static float FROMdB_LOOKUP[FROMdB_LOOKUP_SZ]={
			 1.f,   0.6309573445f,   0.3981071706f,   0.2511886432f,
	   0.1584893192f,		0.1f,  0.06309573445f,  0.03981071706f,
	  0.02511886432f,  0.01584893192f,	   0.01f, 0.006309573445f,
	 0.003981071706f, 0.002511886432f, 0.001584893192f,	  0.001f,
	0.0006309573445f,0.0003981071706f,0.0002511886432f,0.0001584893192f,
		 0.0001f,6.309573445e-05f,3.981071706e-05f,2.511886432e-05f,
	1.584893192e-05f,	  1e-05f,6.309573445e-06f,3.981071706e-06f,
	2.511886432e-06f,1.584893192e-06f,	  1e-06f,6.309573445e-07f,
	3.981071706e-07f,2.511886432e-07f,1.584893192e-07f,
};

static float FROMdB2_LOOKUP[FROMdB2_LOOKUP_SZ]={
	   0.9928302478f,   0.9786445908f,   0.9646616199f,   0.9508784391f,
	   0.9372921937f,	 0.92390007f,   0.9106992942f,   0.8976871324f,
	   0.8848608897f,   0.8722179097f,   0.8597555737f,   0.8474713009f,
		0.835362547f,   0.8234268041f,   0.8116616003f,   0.8000644989f,
	   0.7886330981f,   0.7773650302f,   0.7662579617f,	0.755309592f,
	   0.7445176537f,   0.7338799116f,   0.7233941627f,   0.7130582353f,
	   0.7028699885f,   0.6928273125f,   0.6829281272f,   0.6731703824f,
	   0.6635520573f,   0.6540711597f,   0.6447257262f,   0.6355138211f,
};

#ifdef INT_LOOKUP

#define INVSQ_LOOKUP_I_SHIFT 10
#define INVSQ_LOOKUP_I_MASK 1023
static long INVSQ_LOOKUP_I[64+1]={
	   92682l,   91966l,   91267l,   90583l,
	   89915l,   89261l,   88621l,   87995l,
	   87381l,   86781l,   86192l,   85616l,
	   85051l,   84497l,   83953l,   83420l,
	   82897l,   82384l,   81880l,   81385l,
	   80899l,   80422l,   79953l,   79492l,
	   79039l,   78594l,   78156l,   77726l,
	   77302l,   76885l,   76475l,   76072l,
	   75674l,   75283l,   74898l,   74519l,
	   74146l,   73778l,   73415l,   73058l,
	   72706l,   72359l,   72016l,   71679l,
	   71347l,   71019l,   70695l,   70376l,
	   70061l,   69750l,   69444l,   69141l,
	   68842l,   68548l,   68256l,   67969l,
	   67685l,   67405l,   67128l,   66855l,
	   66585l,   66318l,   66054l,   65794l,
	   65536l,
};

#define COS_LOOKUP_I_SHIFT 9
#define COS_LOOKUP_I_MASK 511
#define COS_LOOKUP_I_SZ 128
static long COS_LOOKUP_I[COS_LOOKUP_I_SZ+1]={
	   16384l,   16379l,   16364l,   16340l,
	   16305l,   16261l,   16207l,   16143l,
	   16069l,   15986l,   15893l,   15791l,
	   15679l,   15557l,   15426l,   15286l,
	   15137l,   14978l,   14811l,   14635l,
	   14449l,   14256l,   14053l,   13842l,
	   13623l,   13395l,   13160l,   12916l,
	   12665l,   12406l,   12140l,   11866l,
	   11585l,   11297l,   11003l,   10702l,
	   10394l,   10080l,	9760l,	9434l,
		9102l,	8765l,	8423l,	8076l,
		7723l,	7366l,	7005l,	6639l,
		6270l,	5897l,	5520l,	5139l,
		4756l,	4370l,	3981l,	3590l,
		3196l,	2801l,	2404l,	2006l,
		1606l,	1205l,	 804l,	 402l,
		   0l,	-401l,	-803l,   -1204l,
	   -1605l,   -2005l,   -2403l,   -2800l,
	   -3195l,   -3589l,   -3980l,   -4369l,
	   -4755l,   -5138l,   -5519l,   -5896l,
	   -6269l,   -6638l,   -7004l,   -7365l,
	   -7722l,   -8075l,   -8422l,   -8764l,
	   -9101l,   -9433l,   -9759l,  -10079l,
	  -10393l,  -10701l,  -11002l,  -11296l,
	  -11584l,  -11865l,  -12139l,  -12405l,
	  -12664l,  -12915l,  -13159l,  -13394l,
	  -13622l,  -13841l,  -14052l,  -14255l,
	  -14448l,  -14634l,  -14810l,  -14977l,
	  -15136l,  -15285l,  -15425l,  -15556l,
	  -15678l,  -15790l,  -15892l,  -15985l,
	  -16068l,  -16142l,  -16206l,  -16260l,
	  -16304l,  -16339l,  -16363l,  -16378l,
	  -16383l,
};

#endif

#endif
/*** End of inlined file: lookup_data.h ***/

#ifdef FLOAT_LOOKUP

float vorbis_coslook(float a){
  double d=a*(.31830989*(float)COS_LOOKUP_SZ);
  int i=vorbis_ftoi(d-.5);

  return COS_LOOKUP[i]+ (d-i)*(COS_LOOKUP[i+1]-COS_LOOKUP[i]);
}

float vorbis_invsqlook(float a){
  double d=a*(2.f*(float)INVSQ_LOOKUP_SZ)-(float)INVSQ_LOOKUP_SZ;
  int i=vorbis_ftoi(d-.5f);
  return INVSQ_LOOKUP[i]+ (d-i)*(INVSQ_LOOKUP[i+1]-INVSQ_LOOKUP[i]);
}

float vorbis_invsq2explook(int a){
  return INVSQ2EXP_LOOKUP[a-INVSQ2EXP_LOOKUP_MIN];
}

#include <stdio.h>
float vorbis_fromdBlook(float a){
  int i=vorbis_ftoi(a*((float)(-(1<<FROMdB2_SHIFT)))-.5f);
  return (i<0)?1.f:
	((i>=(FROMdB_LOOKUP_SZ<<FROMdB_SHIFT))?0.f:
	 FROMdB_LOOKUP[i>>FROMdB_SHIFT]*FROMdB2_LOOKUP[i&FROMdB2_MASK]);
}

#endif

#ifdef INT_LOOKUP
long vorbis_invsqlook_i(long a,long e){
  long i=(a&0x7fff)>>(INVSQ_LOOKUP_I_SHIFT-1);
  long d=(a&INVSQ_LOOKUP_I_MASK)<<(16-INVSQ_LOOKUP_I_SHIFT); /*  0.16 */
  long val=INVSQ_LOOKUP_I[i]-				/*  1.16 */
	(((INVSQ_LOOKUP_I[i]-INVSQ_LOOKUP_I[i+1])*		   /*  0.16 */
	  d)>>16);						   /* result 1.16 */

  e+=32;
  if(e&1)val=(val*5792)>>13; /* multiply val by 1/sqrt(2) */
  e=(e>>1)-8;

  return(val>>e);
}

float vorbis_fromdBlook_i(long a){
  int i=(-a)>>(12-FROMdB2_SHIFT);
  return (i<0)?1.f:
	((i>=(FROMdB_LOOKUP_SZ<<FROMdB_SHIFT))?0.f:
	 FROMdB_LOOKUP[i>>FROMdB_SHIFT]*FROMdB2_LOOKUP[i&FROMdB2_MASK]);
}

long vorbis_coslook_i(long a){
  int i=a>>COS_LOOKUP_I_SHIFT;
  int d=a&COS_LOOKUP_I_MASK;
  return COS_LOOKUP_I[i]- ((d*(COS_LOOKUP_I[i]-COS_LOOKUP_I[i+1]))>>
			   COS_LOOKUP_I_SHIFT);
}

#endif

#endif
/*** End of inlined file: lookup.c ***/

 /* catch this in the build system; we #include for
					   compilers (like gcc) that can't inline across
					   modules */

void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
				float amp,float ampoffset){
  int i;
  float wdel=M_PI/ln;
  vorbis_fpu_control fpu;
  (void) fpu; // to avoid an unused variable warning

  vorbis_fpu_setround(&fpu);
  for(i=0;i<m;i++)lsp[i]=vorbis_coslook(lsp[i]);

  i=0;
  while(i<n){
	int k=map[i];
	int qexp;
	float p=.7071067812f;
	float q=.7071067812f;
	float w=vorbis_coslook(wdel*k);
	float *ftmp=lsp;
	int c=m>>1;

	do{
	  q*=ftmp[0]-w;
	  p*=ftmp[1]-w;
	  ftmp+=2;
	}while(--c);

	if(m&1){
	  q*=ftmp[0]-w;
	  q*=q;
	  p*=p*(1.f-w*w);
	}else{
	  q*=q*(1.f+w);
	  p*=p*(1.f-w);
	}

	q=frexp(p+q,&qexp);
	q=vorbis_fromdBlook(amp*
			vorbis_invsqlook(q)*
			vorbis_invsq2explook(qexp+m)-
			ampoffset);

	do{
	  curve[i++]*=q;
	}while(map[i]==k);
  }
  vorbis_fpu_restore(fpu);
}

#else

#ifdef INT_LOOKUP

/*** Start of inlined file: lookup.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <math.h>

/*** Start of inlined file: lookup.h ***/
#ifndef _V_LOOKUP_H_

#ifdef FLOAT_LOOKUP
extern float vorbis_coslook(float a);
extern float vorbis_invsqlook(float a);
extern float vorbis_invsq2explook(int a);
extern float vorbis_fromdBlook(float a);
#endif
#ifdef INT_LOOKUP
extern long vorbis_invsqlook_i(long a,long e);
extern long vorbis_coslook_i(long a);
extern float vorbis_fromdBlook_i(long a);
#endif

#endif
/*** End of inlined file: lookup.h ***/



/*** Start of inlined file: lookup_data.h ***/
#ifndef _V_LOOKUP_DATA_H_

#ifdef FLOAT_LOOKUP
#define COS_LOOKUP_SZ 128
static float COS_LOOKUP[COS_LOOKUP_SZ+1]={
	+1.0000000000000f,+0.9996988186962f,+0.9987954562052f,+0.9972904566787f,
	+0.9951847266722f,+0.9924795345987f,+0.9891765099648f,+0.9852776423889f,
	+0.9807852804032f,+0.9757021300385f,+0.9700312531945f,+0.9637760657954f,
	+0.9569403357322f,+0.9495281805930f,+0.9415440651830f,+0.9329927988347f,
	+0.9238795325113f,+0.9142097557035f,+0.9039892931234f,+0.8932243011955f,
	+0.8819212643484f,+0.8700869911087f,+0.8577286100003f,+0.8448535652497f,
	+0.8314696123025f,+0.8175848131516f,+0.8032075314806f,+0.7883464276266f,
	+0.7730104533627f,+0.7572088465065f,+0.7409511253550f,+0.7242470829515f,
	+0.7071067811865f,+0.6895405447371f,+0.6715589548470f,+0.6531728429538f,
	+0.6343932841636f,+0.6152315905806f,+0.5956993044924f,+0.5758081914178f,
	+0.5555702330196f,+0.5349976198871f,+0.5141027441932f,+0.4928981922298f,
	+0.4713967368260f,+0.4496113296546f,+0.4275550934303f,+0.4052413140050f,
	+0.3826834323651f,+0.3598950365350f,+0.3368898533922f,+0.3136817403989f,
	+0.2902846772545f,+0.2667127574749f,+0.2429801799033f,+0.2191012401569f,
	+0.1950903220161f,+0.1709618887603f,+0.1467304744554f,+0.1224106751992f,
	+0.0980171403296f,+0.0735645635997f,+0.0490676743274f,+0.0245412285229f,
	+0.0000000000000f,-0.0245412285229f,-0.0490676743274f,-0.0735645635997f,
	-0.0980171403296f,-0.1224106751992f,-0.1467304744554f,-0.1709618887603f,
	-0.1950903220161f,-0.2191012401569f,-0.2429801799033f,-0.2667127574749f,
	-0.2902846772545f,-0.3136817403989f,-0.3368898533922f,-0.3598950365350f,
	-0.3826834323651f,-0.4052413140050f,-0.4275550934303f,-0.4496113296546f,
	-0.4713967368260f,-0.4928981922298f,-0.5141027441932f,-0.5349976198871f,
	-0.5555702330196f,-0.5758081914178f,-0.5956993044924f,-0.6152315905806f,
	-0.6343932841636f,-0.6531728429538f,-0.6715589548470f,-0.6895405447371f,
	-0.7071067811865f,-0.7242470829515f,-0.7409511253550f,-0.7572088465065f,
	-0.7730104533627f,-0.7883464276266f,-0.8032075314806f,-0.8175848131516f,
	-0.8314696123025f,-0.8448535652497f,-0.8577286100003f,-0.8700869911087f,
	-0.8819212643484f,-0.8932243011955f,-0.9039892931234f,-0.9142097557035f,
	-0.9238795325113f,-0.9329927988347f,-0.9415440651830f,-0.9495281805930f,
	-0.9569403357322f,-0.9637760657954f,-0.9700312531945f,-0.9757021300385f,
	-0.9807852804032f,-0.9852776423889f,-0.9891765099648f,-0.9924795345987f,
	-0.9951847266722f,-0.9972904566787f,-0.9987954562052f,-0.9996988186962f,
	-1.0000000000000f,
};

#define INVSQ_LOOKUP_SZ 32
static float INVSQ_LOOKUP[INVSQ_LOOKUP_SZ+1]={
	1.414213562373f,1.392621247646f,1.371988681140f,1.352246807566f,
	1.333333333333f,1.315191898443f,1.297771369046f,1.281025230441f,
	1.264911064067f,1.249390095109f,1.234426799697f,1.219988562661f,
	1.206045378311f,1.192569588000f,1.179535649239f,1.166919931983f,
	1.154700538379f,1.142857142857f,1.131370849898f,1.120224067222f,
	1.109400392450f,1.098884511590f,1.088662107904f,1.078719779941f,
	1.069044967650f,1.059625885652f,1.050451462878f,1.041511287847f,
	1.032795558989f,1.024295039463f,1.016001016002f,1.007905261358f,
	1.000000000000f,
};

#define INVSQ2EXP_LOOKUP_MIN (-32)
#define INVSQ2EXP_LOOKUP_MAX 32
static float INVSQ2EXP_LOOKUP[INVSQ2EXP_LOOKUP_MAX-\
							  INVSQ2EXP_LOOKUP_MIN+1]={
		 65536.f,	46340.95001f,	 32768.f,	23170.47501f,
		 16384.f,	 11585.2375f,	  8192.f,	5792.618751f,
		  4096.f,	2896.309376f,	  2048.f,	1448.154688f,
		  1024.f,	724.0773439f,	   512.f,	 362.038672f,
		   256.f,	 181.019336f,	   128.f,	90.50966799f,
			64.f,	  45.254834f,		32.f,	  22.627417f,
			16.f,	 11.3137085f,		 8.f,	5.656854249f,
			 4.f,	2.828427125f,		 2.f,	1.414213562f,
			 1.f,   0.7071067812f,		0.5f,   0.3535533906f,
		   0.25f,   0.1767766953f,	  0.125f,  0.08838834765f,
		 0.0625f,  0.04419417382f,	0.03125f,  0.02209708691f,
		   0.015625f,  0.01104854346f,	  0.0078125f, 0.005524271728f,
		 0.00390625f, 0.002762135864f,	0.001953125f, 0.001381067932f,
	   0.0009765625f, 0.000690533966f,  0.00048828125f, 0.000345266983f,
	 0.000244140625f,0.0001726334915f,0.0001220703125f,8.631674575e-05f,
	6.103515625e-05f,4.315837288e-05f,3.051757812e-05f,2.157918644e-05f,
	1.525878906e-05f,
};

#endif

#define FROMdB_LOOKUP_SZ 35
#define FROMdB2_LOOKUP_SZ 32
#define FROMdB_SHIFT 5
#define FROMdB2_SHIFT 3
#define FROMdB2_MASK 31
static float FROMdB_LOOKUP[FROMdB_LOOKUP_SZ]={
			 1.f,   0.6309573445f,   0.3981071706f,   0.2511886432f,
	   0.1584893192f,		0.1f,  0.06309573445f,  0.03981071706f,
	  0.02511886432f,  0.01584893192f,	   0.01f, 0.006309573445f,
	 0.003981071706f, 0.002511886432f, 0.001584893192f,	  0.001f,
	0.0006309573445f,0.0003981071706f,0.0002511886432f,0.0001584893192f,
		 0.0001f,6.309573445e-05f,3.981071706e-05f,2.511886432e-05f,
	1.584893192e-05f,	  1e-05f,6.309573445e-06f,3.981071706e-06f,
	2.511886432e-06f,1.584893192e-06f,	  1e-06f,6.309573445e-07f,
	3.981071706e-07f,2.511886432e-07f,1.584893192e-07f,
};

static float FROMdB2_LOOKUP[FROMdB2_LOOKUP_SZ]={
	   0.9928302478f,   0.9786445908f,   0.9646616199f,   0.9508784391f,
	   0.9372921937f,	 0.92390007f,   0.9106992942f,   0.8976871324f,
	   0.8848608897f,   0.8722179097f,   0.8597555737f,   0.8474713009f,
		0.835362547f,   0.8234268041f,   0.8116616003f,   0.8000644989f,
	   0.7886330981f,   0.7773650302f,   0.7662579617f,	0.755309592f,
	   0.7445176537f,   0.7338799116f,   0.7233941627f,   0.7130582353f,
	   0.7028699885f,   0.6928273125f,   0.6829281272f,   0.6731703824f,
	   0.6635520573f,   0.6540711597f,   0.6447257262f,   0.6355138211f,
};

#ifdef INT_LOOKUP

#define INVSQ_LOOKUP_I_SHIFT 10
#define INVSQ_LOOKUP_I_MASK 1023
static long INVSQ_LOOKUP_I[64+1]={
	   92682l,   91966l,   91267l,   90583l,
	   89915l,   89261l,   88621l,   87995l,
	   87381l,   86781l,   86192l,   85616l,
	   85051l,   84497l,   83953l,   83420l,
	   82897l,   82384l,   81880l,   81385l,
	   80899l,   80422l,   79953l,   79492l,
	   79039l,   78594l,   78156l,   77726l,
	   77302l,   76885l,   76475l,   76072l,
	   75674l,   75283l,   74898l,   74519l,
	   74146l,   73778l,   73415l,   73058l,
	   72706l,   72359l,   72016l,   71679l,
	   71347l,   71019l,   70695l,   70376l,
	   70061l,   69750l,   69444l,   69141l,
	   68842l,   68548l,   68256l,   67969l,
	   67685l,   67405l,   67128l,   66855l,
	   66585l,   66318l,   66054l,   65794l,
	   65536l,
};

#define COS_LOOKUP_I_SHIFT 9
#define COS_LOOKUP_I_MASK 511
#define COS_LOOKUP_I_SZ 128
static long COS_LOOKUP_I[COS_LOOKUP_I_SZ+1]={
	   16384l,   16379l,   16364l,   16340l,
	   16305l,   16261l,   16207l,   16143l,
	   16069l,   15986l,   15893l,   15791l,
	   15679l,   15557l,   15426l,   15286l,
	   15137l,   14978l,   14811l,   14635l,
	   14449l,   14256l,   14053l,   13842l,
	   13623l,   13395l,   13160l,   12916l,
	   12665l,   12406l,   12140l,   11866l,
	   11585l,   11297l,   11003l,   10702l,
	   10394l,   10080l,	9760l,	9434l,
		9102l,	8765l,	8423l,	8076l,
		7723l,	7366l,	7005l,	6639l,
		6270l,	5897l,	5520l,	5139l,
		4756l,	4370l,	3981l,	3590l,
		3196l,	2801l,	2404l,	2006l,
		1606l,	1205l,	 804l,	 402l,
		   0l,	-401l,	-803l,   -1204l,
	   -1605l,   -2005l,   -2403l,   -2800l,
	   -3195l,   -3589l,   -3980l,   -4369l,
	   -4755l,   -5138l,   -5519l,   -5896l,
	   -6269l,   -6638l,   -7004l,   -7365l,
	   -7722l,   -8075l,   -8422l,   -8764l,
	   -9101l,   -9433l,   -9759l,  -10079l,
	  -10393l,  -10701l,  -11002l,  -11296l,
	  -11584l,  -11865l,  -12139l,  -12405l,
	  -12664l,  -12915l,  -13159l,  -13394l,
	  -13622l,  -13841l,  -14052l,  -14255l,
	  -14448l,  -14634l,  -14810l,  -14977l,
	  -15136l,  -15285l,  -15425l,  -15556l,
	  -15678l,  -15790l,  -15892l,  -15985l,
	  -16068l,  -16142l,  -16206l,  -16260l,
	  -16304l,  -16339l,  -16363l,  -16378l,
	  -16383l,
};

#endif

#endif
/*** End of inlined file: lookup_data.h ***/

#ifdef FLOAT_LOOKUP

float vorbis_coslook(float a){
  double d=a*(.31830989*(float)COS_LOOKUP_SZ);
  int i=vorbis_ftoi(d-.5);

  return COS_LOOKUP[i]+ (d-i)*(COS_LOOKUP[i+1]-COS_LOOKUP[i]);
}

float vorbis_invsqlook(float a){
  double d=a*(2.f*(float)INVSQ_LOOKUP_SZ)-(float)INVSQ_LOOKUP_SZ;
  int i=vorbis_ftoi(d-.5f);
  return INVSQ_LOOKUP[i]+ (d-i)*(INVSQ_LOOKUP[i+1]-INVSQ_LOOKUP[i]);
}

float vorbis_invsq2explook(int a){
  return INVSQ2EXP_LOOKUP[a-INVSQ2EXP_LOOKUP_MIN];
}

#include <stdio.h>
float vorbis_fromdBlook(float a){
  int i=vorbis_ftoi(a*((float)(-(1<<FROMdB2_SHIFT)))-.5f);
  return (i<0)?1.f:
	((i>=(FROMdB_LOOKUP_SZ<<FROMdB_SHIFT))?0.f:
	 FROMdB_LOOKUP[i>>FROMdB_SHIFT]*FROMdB2_LOOKUP[i&FROMdB2_MASK]);
}

#endif

#ifdef INT_LOOKUP
long vorbis_invsqlook_i(long a,long e){
  long i=(a&0x7fff)>>(INVSQ_LOOKUP_I_SHIFT-1);
  long d=(a&INVSQ_LOOKUP_I_MASK)<<(16-INVSQ_LOOKUP_I_SHIFT); /*  0.16 */
  long val=INVSQ_LOOKUP_I[i]-				/*  1.16 */
	(((INVSQ_LOOKUP_I[i]-INVSQ_LOOKUP_I[i+1])*		   /*  0.16 */
	  d)>>16);						   /* result 1.16 */

  e+=32;
  if(e&1)val=(val*5792)>>13; /* multiply val by 1/sqrt(2) */
  e=(e>>1)-8;

  return(val>>e);
}

float vorbis_fromdBlook_i(long a){
  int i=(-a)>>(12-FROMdB2_SHIFT);
  return (i<0)?1.f:
	((i>=(FROMdB_LOOKUP_SZ<<FROMdB_SHIFT))?0.f:
	 FROMdB_LOOKUP[i>>FROMdB_SHIFT]*FROMdB2_LOOKUP[i&FROMdB2_MASK]);
}

long vorbis_coslook_i(long a){
  int i=a>>COS_LOOKUP_I_SHIFT;
  int d=a&COS_LOOKUP_I_MASK;
  return COS_LOOKUP_I[i]- ((d*(COS_LOOKUP_I[i]-COS_LOOKUP_I[i+1]))>>
			   COS_LOOKUP_I_SHIFT);
}

#endif

#endif
/*** End of inlined file: lookup.c ***/

 /* catch this in the build system; we #include for
					   compilers (like gcc) that can't inline across
					   modules */

static int MLOOP_1[64]={
   0,10,11,11, 12,12,12,12, 13,13,13,13, 13,13,13,13,
  14,14,14,14, 14,14,14,14, 14,14,14,14, 14,14,14,14,
  15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
};

static int MLOOP_2[64]={
  0,4,5,5, 6,6,6,6, 7,7,7,7, 7,7,7,7,
  8,8,8,8, 8,8,8,8, 8,8,8,8, 8,8,8,8,
  9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
  9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
};

static int MLOOP_3[8]={0,1,2,2,3,3,3,3};

void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
				float amp,float ampoffset){

  int i;
  int ampoffseti=rint(ampoffset*4096.f);
  int ampi=rint(amp*16.f);
  long *ilsp=alloca(m*sizeof(*ilsp));
  for(i=0;i<m;i++)ilsp[i]=vorbis_coslook_i(lsp[i]/M_PI*65536.f+.5f);

  i=0;
  while(i<n){
	int j,k=map[i];
	unsigned long pi=46341; /* 2**-.5 in 0.16 */
	unsigned long qi=46341;
	int qexp=0,shift;
	long wi=vorbis_coslook_i(k*65536/ln);

	qi*=labs(ilsp[0]-wi);
	pi*=labs(ilsp[1]-wi);

	for(j=3;j<m;j+=2){
	  if(!(shift=MLOOP_1[(pi|qi)>>25]))
	if(!(shift=MLOOP_2[(pi|qi)>>19]))
	  shift=MLOOP_3[(pi|qi)>>16];
	  qi=(qi>>shift)*labs(ilsp[j-1]-wi);
	  pi=(pi>>shift)*labs(ilsp[j]-wi);
	  qexp+=shift;
	}
	if(!(shift=MLOOP_1[(pi|qi)>>25]))
	  if(!(shift=MLOOP_2[(pi|qi)>>19]))
	shift=MLOOP_3[(pi|qi)>>16];

	if(m&1){
	  qi=(qi>>shift)*labs(ilsp[j-1]-wi);
	  pi=(pi>>shift)<<14;
	  qexp+=shift;

	  if(!(shift=MLOOP_1[(pi|qi)>>25]))
	if(!(shift=MLOOP_2[(pi|qi)>>19]))
	  shift=MLOOP_3[(pi|qi)>>16];

	  pi>>=shift;
	  qi>>=shift;
	  qexp+=shift-14*((m+1)>>1);

	  pi=((pi*pi)>>16);
	  qi=((qi*qi)>>16);
	  qexp=qexp*2+m;

	  pi*=(1<<14)-((wi*wi)>>14);
	  qi+=pi>>14;

	}else{

	  pi>>=shift;
	  qi>>=shift;
	  qexp+=shift-7*m;

	  pi=((pi*pi)>>16);
	  qi=((qi*qi)>>16);
	  qexp=qexp*2+m;

	  pi*=(1<<14)-wi;
	  qi*=(1<<14)+wi;
	  qi=(qi+pi)>>14;

	}

	if(qi&0xffff0000){ /* checks for 1.xxxxxxxxxxxxxxxx */
	  qi>>=1; qexp++;
	}else
	  while(qi && !(qi&0x8000)){ /* checks for 0.0xxxxxxxxxxxxxxx or less*/
	qi<<=1; qexp--;
	  }

	amp=vorbis_fromdBlook_i(ampi*			 /*  n.4	 */
				vorbis_invsqlook_i(qi,qexp)-
				ampoffseti);		  /*  8.12[0]	 */

	curve[i]*=amp;
	while(map[++i]==k)curve[i]*=amp;
  }
}

#else

void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
				float amp,float ampoffset){
  int i;
  float wdel=M_PI/ln;
  for(i=0;i<m;i++)lsp[i]=2.f*cos(lsp[i]);

  i=0;
  while(i<n){
	int j,k=map[i];
	float p=.5f;
	float q=.5f;
	float w=2.f*cos(wdel*k);
	for(j=1;j<m;j+=2){
	  q *= w-lsp[j-1];
	  p *= w-lsp[j];
	}
	if(j==m){
	  q*=w-lsp[j-1];
	  p*=p*(4.f-w*w);
	  q*=q;
	}else{
	  p*=p*(2.f-w);
	  q*=q*(2.f+w);
	}

	q=fromdB(amp/sqrt(p+q)-ampoffset);

	curve[i]*=q;
	while(map[++i]==k)curve[i]*=q;
  }
}

#endif
#endif

static void cheby(float *g, int ord) {
  int i, j;

  g[0] *= .5f;
  for(i=2; i<= ord; i++) {
	for(j=ord; j >= i; j--) {
	  g[j-2] -= g[j];
	  g[j] += g[j];
	}
  }
}

static int comp(const void *a,const void *b){
  return (*(float *)a<*(float *)b)-(*(float *)a>*(float *)b);
}

#define EPSILON 10e-7
static int Laguerre_With_Deflation(float *a,int ord,float *r){
  int i,m;
  double lastdelta=0.f;
  double *defl=(double*)alloca(sizeof(*defl)*(ord+1));
  for(i=0;i<=ord;i++)defl[i]=a[i];

  for(m=ord;m>0;m--){
	double newx=0.f,delta;

	while(1){
	  double p=defl[m],pp=0.f,ppp=0.f,denom;

	  for(i=m;i>0;i--){
	ppp = newx*ppp + pp;
	pp  = newx*pp  + p;
	p   = newx*p   + defl[i-1];
	  }

	  denom=(m-1) * ((m-1)*pp*pp - m*p*ppp);
	  if(denom<0)
	return(-1);  /* complex root!  The LPC generator handed us a bad filter */

	  if(pp>0){
	denom = pp + sqrt(denom);
	if(denom<EPSILON)denom=EPSILON;
	  }else{
	denom = pp - sqrt(denom);
	if(denom>-(EPSILON))denom=-(EPSILON);
	  }

	  delta  = m*p/denom;
	  newx   -= delta;

	  if(delta<0.f)delta*=-1;

	  if(fabs(delta/newx)<10e-12)break;
	  lastdelta=delta;
	}

	r[m-1]=newx;

	for(i=m;i>0;i--)
	  defl[i-1]+=newx*defl[i];
	defl++;

  }
  return(0);
}

static int Newton_Raphson(float *a,int ord,float *r){
  int i, k, count=0;
  double error=1.f;
  double *root=(double*)alloca(ord*sizeof(*root));

  for(i=0; i<ord;i++) root[i] = r[i];

  while(error>1e-20){
	error=0;

	for(i=0; i<ord; i++) { /* Update each point. */
	  double pp=0.,delta;
	  double rooti=root[i];
	  double p=a[ord];
	  for(k=ord-1; k>= 0; k--) {

	pp= pp* rooti + p;
	p = p * rooti + a[k];
	  }

	  delta = p/pp;
	  root[i] -= delta;
	  error+= delta*delta;
	}

	if(count>40)return(-1);

	count++;
  }

  for(i=0; i<ord;i++) r[i] = root[i];
  return(0);
}

int vorbis_lpc_to_lsp(float *lpc,float *lsp,int m){
  int order2=(m+1)>>1;
  int g1_order,g2_order;
  float *g1=(float*)alloca(sizeof(*g1)*(order2+1));
  float *g2=(float*)alloca(sizeof(*g2)*(order2+1));
  float *g1r=(float*)alloca(sizeof(*g1r)*(order2+1));
  float *g2r=(float*)alloca(sizeof(*g2r)*(order2+1));
  int i;

  g1_order=(m+1)>>1;
  g2_order=(m)  >>1;

  g1[g1_order] = 1.f;
  for(i=1;i<=g1_order;i++) g1[g1_order-i] = lpc[i-1]+lpc[m-i];
  g2[g2_order] = 1.f;
  for(i=1;i<=g2_order;i++) g2[g2_order-i] = lpc[i-1]-lpc[m-i];

  if(g1_order>g2_order){
	for(i=2; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+2];
  }else{
	for(i=1; i<=g1_order;i++) g1[g1_order-i] -= g1[g1_order-i+1];
	for(i=1; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+1];
  }

  cheby(g1,g1_order);
  cheby(g2,g2_order);

  if(Laguerre_With_Deflation(g1,g1_order,g1r) ||
	 Laguerre_With_Deflation(g2,g2_order,g2r))
	return(-1);

  Newton_Raphson(g1,g1_order,g1r); /* if it fails, it leaves g1r alone */
  Newton_Raphson(g2,g2_order,g2r); /* if it fails, it leaves g2r alone */

  qsort(g1r,g1_order,sizeof(*g1r),comp);
  qsort(g2r,g2_order,sizeof(*g2r),comp);

  for(i=0;i<g1_order;i++)
	lsp[i*2] = acos(g1r[i]);

  for(i=0;i<g2_order;i++)
	lsp[i*2+1] = acos(g2r[i]);
  return(0);
}

#endif
/*** End of inlined file: lsp.c ***/


/*** Start of inlined file: mapping0.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

static void mapping0_free_info(vorbis_info_mapping *i){
  vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
  if(info){
	memset(info,0,sizeof(*info));
	_ogg_free(info);
  }
}

static int ilog3(unsigned int v){
  int ret=0;
  if(v)--v;
  while(v){
	ret++;
	v>>=1;
  }
  return(ret);
}

static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,
			  oggpack_buffer *opb){
  int i;
  vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;

  if(info->submaps>1){
	oggpack_write(opb,1,1);
	oggpack_write(opb,info->submaps-1,4);
  }else
	oggpack_write(opb,0,1);

  if(info->coupling_steps>0){
	oggpack_write(opb,1,1);
	oggpack_write(opb,info->coupling_steps-1,8);

	for(i=0;i<info->coupling_steps;i++){
	  oggpack_write(opb,info->coupling_mag[i],ilog3(vi->channels));
	  oggpack_write(opb,info->coupling_ang[i],ilog3(vi->channels));
	}
  }else
	oggpack_write(opb,0,1);

  oggpack_write(opb,0,2); /* 2,3:reserved */

  if(info->submaps>1){
	for(i=0;i<vi->channels;i++)
	  oggpack_write(opb,info->chmuxlist[i],4);
  }
  for(i=0;i<info->submaps;i++){
	oggpack_write(opb,0,8); /* time submap unused */
	oggpack_write(opb,info->floorsubmap[i],8);
	oggpack_write(opb,info->residuesubmap[i],8);
  }
}

static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
  int i;
  vorbis_info_mapping0 *info=(vorbis_info_mapping0*)_ogg_calloc(1,sizeof(*info));
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  memset(info,0,sizeof(*info));

  if(oggpack_read(opb,1))
	info->submaps=oggpack_read(opb,4)+1;
  else
	info->submaps=1;

  if(oggpack_read(opb,1)){
	info->coupling_steps=oggpack_read(opb,8)+1;

	for(i=0;i<info->coupling_steps;i++){
	  int testM=info->coupling_mag[i]=oggpack_read(opb,ilog3(vi->channels));
	  int testA=info->coupling_ang[i]=oggpack_read(opb,ilog3(vi->channels));

	  if(testM<0 ||
	 testA<0 ||
	 testM==testA ||
	 testM>=vi->channels ||
	 testA>=vi->channels) goto err_out;
	}

  }

  if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */

  if(info->submaps>1){
	for(i=0;i<vi->channels;i++){
	  info->chmuxlist[i]=oggpack_read(opb,4);
	  if(info->chmuxlist[i]>=info->submaps)goto err_out;
	}
  }
  for(i=0;i<info->submaps;i++){
	oggpack_read(opb,8); /* time submap unused */
	info->floorsubmap[i]=oggpack_read(opb,8);
	if(info->floorsubmap[i]>=ci->floors)goto err_out;
	info->residuesubmap[i]=oggpack_read(opb,8);
	if(info->residuesubmap[i]>=ci->residues)goto err_out;
  }

  return info;

 err_out:
  mapping0_free_info(info);
  return(NULL);
}

#if 0
static long seq=0;
static ogg_int64_t total=0;
static float FLOOR1_fromdB_LOOKUP[256]={
  1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
  1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
  1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
  2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
  2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
  3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
  4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
  6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
  7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
  1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
  1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
  1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
  2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
  2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
  3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
  4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
  5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
  7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
  9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
  1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
  1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
  2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
  2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
  3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
  4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
  5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
  7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
  9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
  0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
  0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
  0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
  0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
  0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
  0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
  0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
  0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
  0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
  0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
  0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
  0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
  0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
  0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
  0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
  0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
  0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
  0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
  0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
  0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
  0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
  0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
  0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
  0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
  0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
  0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
  0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
  0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
  0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
  0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
  0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
  0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
  0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
  0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
  0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
  0.82788260F, 0.88168307F, 0.9389798F, 1.F,
};

#endif

extern int *floor1_fit(vorbis_block *vb,void *look,
			   const float *logmdct,   /* in */
			   const float *logmask);
extern int *floor1_interpolate_fit(vorbis_block *vb,void *look,
				   int *A,int *B,
				   int del);
extern int floor1_encode(oggpack_buffer *opb,vorbis_block *vb,
			 void*look,
			 int *post,int *ilogmask);

static int mapping0_forward(vorbis_block *vb){
  vorbis_dsp_state	  *vd=vb->vd;
  vorbis_info	   *vi=vd->vi;
  codec_setup_info	  *ci=(codec_setup_info*)vi->codec_setup;
  private_state	 *b=(private_state*)vb->vd->backend_state;
  vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
  int			n=vb->pcmend;
  int i,j,k;

  int	*nonzero	= (int*) alloca(sizeof(*nonzero)*vi->channels);
  float  **gmdct	 = (float**) _vorbis_block_alloc(vb,vi->channels*sizeof(*gmdct));
  int	**ilogmaskch= (int**) _vorbis_block_alloc(vb,vi->channels*sizeof(*ilogmaskch));
  int ***floor_posts = (int***) _vorbis_block_alloc(vb,vi->channels*sizeof(*floor_posts));

  float global_ampmax=vbi->ampmax;
  float *local_ampmax=(float*)alloca(sizeof(*local_ampmax)*vi->channels);
  int blocktype=vbi->blocktype;

  int modenumber=vb->W;
  vorbis_info_mapping0 *info=(vorbis_info_mapping0*)ci->map_param[modenumber];
  vorbis_look_psy *psy_look=
	b->psy+blocktype+(vb->W?2:0);

  vb->mode=modenumber;

  for(i=0;i<vi->channels;i++){
	float scale=4.f/n;
	float scale_dB;

	float *pcm	 =vb->pcm[i];
	float *logfft  =pcm;

	gmdct[i]=(float*)_vorbis_block_alloc(vb,n/2*sizeof(**gmdct));

	scale_dB=todB(&scale) + .345; /* + .345 is a hack; the original
									 todB estimation used on IEEE 754
									 compliant machines had a bug that
									 returned dB values about a third
									 of a decibel too high.  The bug
									 was harmless because tunings
									 implicitly took that into
									 account.  However, fixing the bug
									 in the estimator requires
									 changing all the tunings as well.
									 For now, it's easier to sync
									 things back up here, and
									 recalibrate the tunings in the
									 next major model upgrade. */

#if 0
	if(vi->channels==2)
	  if(i==0)
	_analysis_output("pcmL",seq,pcm,n,0,0,total-n/2);
	  else
	_analysis_output("pcmR",seq,pcm,n,0,0,total-n/2);
#endif

	_vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);

#if 0
	if(vi->channels==2)
	  if(i==0)
	_analysis_output("windowedL",seq,pcm,n,0,0,total-n/2);
	  else
	_analysis_output("windowedR",seq,pcm,n,0,0,total-n/2);
#endif

	mdct_forward((mdct_lookup*) b->transform[vb->W][0],pcm,gmdct[i]);

	drft_forward(&b->fft_look[vb->W],pcm);
	logfft[0]=scale_dB+todB(pcm)  + .345; /* + .345 is a hack; the
									 original todB estimation used on
									 IEEE 754 compliant machines had a
									 bug that returned dB values about
									 a third of a decibel too high.
									 The bug was harmless because
									 tunings implicitly took that into
									 account.  However, fixing the bug
									 in the estimator requires
									 changing all the tunings as well.
									 For now, it's easier to sync
									 things back up here, and
									 recalibrate the tunings in the
									 next major model upgrade. */
	local_ampmax[i]=logfft[0];
	for(j=1;j<n-1;j+=2){
	  float temp=pcm[j]*pcm[j]+pcm[j+1]*pcm[j+1];
	  temp=logfft[(j+1)>>1]=scale_dB+.5f*todB(&temp)  + .345; /* +
									 .345 is a hack; the original todB
									 estimation used on IEEE 754
									 compliant machines had a bug that
									 returned dB values about a third
									 of a decibel too high.  The bug
									 was harmless because tunings
									 implicitly took that into
									 account.  However, fixing the bug
									 in the estimator requires
									 changing all the tunings as well.
									 For now, it's easier to sync
									 things back up here, and
									 recalibrate the tunings in the
									 next major model upgrade. */
	  if(temp>local_ampmax[i])local_ampmax[i]=temp;
	}

	if(local_ampmax[i]>0.f)local_ampmax[i]=0.f;
	if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];

#if 0
	if(vi->channels==2){
	  if(i==0){
	_analysis_output("fftL",seq,logfft,n/2,1,0,0);
	  }else{
	_analysis_output("fftR",seq,logfft,n/2,1,0,0);
	  }
	}
#endif

  }

  {
	float   *noise	= (float*) _vorbis_block_alloc(vb,n/2*sizeof(*noise));
	float   *tone	 = (float*) _vorbis_block_alloc(vb,n/2*sizeof(*tone));

	for(i=0;i<vi->channels;i++){

	  int submap=info->chmuxlist[i];

	  float *mdct	=gmdct[i];
	  float *logfft  =vb->pcm[i];

	  float *logmdct =logfft+n/2;
	  float *logmask =logfft;

	  vb->mode=modenumber;

	  floor_posts[i]=(int**) _vorbis_block_alloc(vb,PACKETBLOBS*sizeof(**floor_posts));
	  memset(floor_posts[i],0,sizeof(**floor_posts)*PACKETBLOBS);

	  for(j=0;j<n/2;j++)
	logmdct[j]=todB(mdct+j)  + .345; /* + .345 is a hack; the original
									 todB estimation used on IEEE 754
									 compliant machines had a bug that
									 returned dB values about a third
									 of a decibel too high.  The bug
									 was harmless because tunings
									 implicitly took that into
									 account.  However, fixing the bug
									 in the estimator requires
									 changing all the tunings as well.
									 For now, it's easier to sync
									 things back up here, and
									 recalibrate the tunings in the
									 next major model upgrade. */

#if 0
	  if(vi->channels==2){
	if(i==0)
	  _analysis_output("mdctL",seq,logmdct,n/2,1,0,0);
	else
	  _analysis_output("mdctR",seq,logmdct,n/2,1,0,0);
	  }else{
	_analysis_output("mdct",seq,logmdct,n/2,1,0,0);
	  }
#endif

	  _vp_noisemask(psy_look,
			logmdct,
			noise); /* noise does not have by-frequency offset
							   bias applied yet */
#if 0
	  if(vi->channels==2){
	if(i==0)
	  _analysis_output("noiseL",seq,noise,n/2,1,0,0);
	else
	  _analysis_output("noiseR",seq,noise,n/2,1,0,0);
	  }
#endif

	  _vp_tonemask(psy_look,
		   logfft,
		   tone,
		   global_ampmax,
		   local_ampmax[i]);

#if 0
	  if(vi->channels==2){
	if(i==0)
	  _analysis_output("toneL",seq,tone,n/2,1,0,0);
	else
	  _analysis_output("toneR",seq,tone,n/2,1,0,0);
	  }
#endif

#if 0
	  {
	  float aotuv[psy_look->n];
#endif

	_vp_offset_and_mix(psy_look,
			   noise,
			   tone,
			   1,
			   logmask,
			   mdct,
			   logmdct);

#if 0
	if(vi->channels==2){
	  if(i==0)
	    _analysis_output("aotuvM1_L",seq,aotuv,psy_look->n,1,1,0);
	  else
	    _analysis_output("aotuvM1_R",seq,aotuv,psy_look->n,1,1,0);
	}
	  }
#endif

#if 0
	  if(vi->channels==2){
	if(i==0)
	  _analysis_output("mask1L",seq,logmask,n/2,1,0,0);
	else
	  _analysis_output("mask1R",seq,logmask,n/2,1,0,0);
	  }
#endif

	  if(ci->floor_type[info->floorsubmap[submap]]!=1)return(-1);

	  floor_posts[i][PACKETBLOBS/2]=
	floor1_fit(vb,b->flr[info->floorsubmap[submap]],
		   logmdct,
		   logmask);

	  if(vorbis_bitrate_managed(vb) && floor_posts[i][PACKETBLOBS/2]){

	_vp_offset_and_mix(psy_look,
			   noise,
			   tone,
			   2,
			   logmask,
			   mdct,
			   logmdct);

#if 0
	if(vi->channels==2){
	  if(i==0)
	    _analysis_output("mask2L",seq,logmask,n/2,1,0,0);
	  else
	    _analysis_output("mask2R",seq,logmask,n/2,1,0,0);
	}
#endif

	floor_posts[i][PACKETBLOBS-1]=
	  floor1_fit(vb,b->flr[info->floorsubmap[submap]],
			 logmdct,
			 logmask);

	_vp_offset_and_mix(psy_look,
			   noise,
			   tone,
			   0,
			   logmask,
			   mdct,
			   logmdct);

#if 0
	if(vi->channels==2)
	  if(i==0)
	    _analysis_output("mask0L",seq,logmask,n/2,1,0,0);
	  else
	    _analysis_output("mask0R",seq,logmask,n/2,1,0,0);
#endif

	floor_posts[i][0]=
	  floor1_fit(vb,b->flr[info->floorsubmap[submap]],
			 logmdct,
			 logmask);

	for(k=1;k<PACKETBLOBS/2;k++)
	  floor_posts[i][k]=
		floor1_interpolate_fit(vb,b->flr[info->floorsubmap[submap]],
				   floor_posts[i][0],
				   floor_posts[i][PACKETBLOBS/2],
				   k*65536/(PACKETBLOBS/2));
	for(k=PACKETBLOBS/2+1;k<PACKETBLOBS-1;k++)
	  floor_posts[i][k]=
		floor1_interpolate_fit(vb,b->flr[info->floorsubmap[submap]],
				   floor_posts[i][PACKETBLOBS/2],
				   floor_posts[i][PACKETBLOBS-1],
				   (k-PACKETBLOBS/2)*65536/(PACKETBLOBS/2));
	  }
	}
  }
  vbi->ampmax=global_ampmax;

  {
	float **res_bundle=(float**) alloca(sizeof(*res_bundle)*vi->channels);
	float **couple_bundle=(float**) alloca(sizeof(*couple_bundle)*vi->channels);
	int *zerobundle=(int*) alloca(sizeof(*zerobundle)*vi->channels);
	int **sortindex=(int**) alloca(sizeof(*sortindex)*vi->channels);
	float **mag_memo;
	int **mag_sort;

	if(info->coupling_steps){
	  mag_memo=_vp_quantize_couple_memo(vb,
					&ci->psy_g_param,
					psy_look,
					info,
					gmdct);

	  mag_sort=_vp_quantize_couple_sort(vb,
					psy_look,
					info,
					mag_memo);

	  hf_reduction(&ci->psy_g_param,
		   psy_look,
		   info,
		   mag_memo);
	}

	memset(sortindex,0,sizeof(*sortindex)*vi->channels);
	if(psy_look->vi->normal_channel_p){
	  for(i=0;i<vi->channels;i++){
	float *mdct	=gmdct[i];
	sortindex[i]=(int*) alloca(sizeof(**sortindex)*n/2);
	_vp_noise_normalize_sort(psy_look,mdct,sortindex[i]);
	  }
	}

	for(k=(vorbis_bitrate_managed(vb)?0:PACKETBLOBS/2);
	k<=(vorbis_bitrate_managed(vb)?PACKETBLOBS-1:PACKETBLOBS/2);
	k++){
	  oggpack_buffer *opb=vbi->packetblob[k];

	  oggpack_write(opb,0,1);
	  oggpack_write(opb,modenumber,b->modebits);
	  if(vb->W){
	oggpack_write(opb,vb->lW,1);
	oggpack_write(opb,vb->nW,1);
	  }

	  for(i=0;i<vi->channels;i++){
	int submap=info->chmuxlist[i];
	float *mdct	=gmdct[i];
	float *res	 =vb->pcm[i];
	int   *ilogmask=ilogmaskch[i]=
	  (int*) _vorbis_block_alloc(vb,n/2*sizeof(**gmdct));

	nonzero[i]=floor1_encode(opb,vb,b->flr[info->floorsubmap[submap]],
				 floor_posts[i][k],
				 ilogmask);
#if 0
	{
	  char buf[80];
	  sprintf(buf,"maskI%c%d",i?'R':'L',k);
	  float work[n/2];
	  for(j=0;j<n/2;j++)
		work[j]=FLOOR1_fromdB_LOOKUP[ilogmask[j]];
	  _analysis_output(buf,seq,work,n/2,1,1,0);
	}
#endif
	_vp_remove_floor(psy_look,
			 mdct,
			 ilogmask,
			 res,
			 ci->psy_g_param.sliding_lowpass[vb->W][k]);

	_vp_noise_normalize(psy_look,res,res+n/2,sortindex[i]);

#if 0
	{
	  char buf[80];
	  float work[n/2];
	  for(j=0;j<n/2;j++)
		work[j]=FLOOR1_fromdB_LOOKUP[ilogmask[j]]*(res+n/2)[j];
	  sprintf(buf,"resI%c%d",i?'R':'L',k);
	  _analysis_output(buf,seq,work,n/2,1,1,0);

	}
#endif
	  }

	  if(info->coupling_steps){
	_vp_couple(k,
		   &ci->psy_g_param,
		   psy_look,
		   info,
		   vb->pcm,
		   mag_memo,
		   mag_sort,
		   ilogmaskch,
		   nonzero,
		   ci->psy_g_param.sliding_lowpass[vb->W][k]);
	  }

	  for(i=0;i<info->submaps;i++){
	int ch_in_bundle=0;
	long **classifications;
	int resnum=info->residuesubmap[i];

	for(j=0;j<vi->channels;j++){
	  if(info->chmuxlist[j]==i){
		zerobundle[ch_in_bundle]=0;
		if(nonzero[j])zerobundle[ch_in_bundle]=1;
		res_bundle[ch_in_bundle]=vb->pcm[j];
		couple_bundle[ch_in_bundle++]=vb->pcm[j]+n/2;
	  }
	}

	classifications=_residue_P[ci->residue_type[resnum]]->
	  classx(vb,b->residue[resnum],couple_bundle,zerobundle,ch_in_bundle);

	_residue_P[ci->residue_type[resnum]]->
	  forward(opb,vb,b->residue[resnum],
		  couple_bundle,NULL,zerobundle,ch_in_bundle,classifications);
	  }

	}

  }

#if 0
  seq++;
  total+=ci->blocksizes[vb->W]/4+ci->blocksizes[vb->nW]/4;
#endif
  return(0);
}

static int mapping0_inverse(vorbis_block *vb,vorbis_info_mapping *l){
  vorbis_dsp_state	 *vd=vb->vd;
  vorbis_info	  *vi=vd->vi;
  codec_setup_info	 *ci=(codec_setup_info*) vi->codec_setup;
  private_state	*b=(private_state*)vd->backend_state;
  vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)l;

  int		   i,j;
  long		  n=vb->pcmend=ci->blocksizes[vb->W];

  float **pcmbundle=(float**) alloca(sizeof(*pcmbundle)*vi->channels);
  int	*zerobundle=(int*) alloca(sizeof(*zerobundle)*vi->channels);

  int   *nonzero  =(int*) alloca(sizeof(*nonzero)*vi->channels);
  void **floormemo=(void**) alloca(sizeof(*floormemo)*vi->channels);

  for(i=0;i<vi->channels;i++){
	int submap=info->chmuxlist[i];
	floormemo[i]=_floor_P[ci->floor_type[info->floorsubmap[submap]]]->
	  inverse1(vb,b->flr[info->floorsubmap[submap]]);
	if(floormemo[i])
	  nonzero[i]=1;
	else
	  nonzero[i]=0;
	memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
  }

  for(i=0;i<info->coupling_steps;i++){
	if(nonzero[info->coupling_mag[i]] ||
	   nonzero[info->coupling_ang[i]]){
	  nonzero[info->coupling_mag[i]]=1;
	  nonzero[info->coupling_ang[i]]=1;
	}
  }

  for(i=0;i<info->submaps;i++){
	int ch_in_bundle=0;
	for(j=0;j<vi->channels;j++){
	  if(info->chmuxlist[j]==i){
	if(nonzero[j])
	  zerobundle[ch_in_bundle]=1;
	else
	  zerobundle[ch_in_bundle]=0;
	pcmbundle[ch_in_bundle++]=vb->pcm[j];
	  }
	}

	_residue_P[ci->residue_type[info->residuesubmap[i]]]->
	  inverse(vb,b->residue[info->residuesubmap[i]],
		  pcmbundle,zerobundle,ch_in_bundle);
  }

  for(i=info->coupling_steps-1;i>=0;i--){
	float *pcmM=vb->pcm[info->coupling_mag[i]];
	float *pcmA=vb->pcm[info->coupling_ang[i]];

	for(j=0;j<n/2;j++){
	  float mag=pcmM[j];
	  float ang=pcmA[j];

	  if(mag>0)
	if(ang>0){
	  pcmM[j]=mag;
	  pcmA[j]=mag-ang;
	}else{
	  pcmA[j]=mag;
	  pcmM[j]=mag+ang;
	}
	  else
	if(ang>0){
	  pcmM[j]=mag;
	  pcmA[j]=mag+ang;
	}else{
	  pcmA[j]=mag;
	  pcmM[j]=mag-ang;
	}
	}
  }

  for(i=0;i<vi->channels;i++){
	float *pcm=vb->pcm[i];
	int submap=info->chmuxlist[i];
	_floor_P[ci->floor_type[info->floorsubmap[submap]]]->
	  inverse2(vb,b->flr[info->floorsubmap[submap]],
		   floormemo[i],pcm);
  }

  for(i=0;i<vi->channels;i++){
	float *pcm=vb->pcm[i];
	mdct_backward((mdct_lookup*) b->transform[vb->W][0],pcm,pcm);
  }

  return(0);
}

vorbis_func_mapping mapping0_exportbundle={
  &mapping0_pack,
  &mapping0_unpack,
  &mapping0_free_info,
  &mapping0_forward,
  &mapping0_inverse
};

#endif
/*** End of inlined file: mapping0.c ***/


/*** Start of inlined file: mdct.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void mdct_init(mdct_lookup *lookup,int n){
  int   *bitrev=(int*) _ogg_malloc(sizeof(*bitrev)*(n/4));
  DATA_TYPE *T=(DATA_TYPE*) _ogg_malloc(sizeof(*T)*(n+n/4));

  int i;
  int n2=n>>1;
  int log2n=lookup->log2n=rint(log((float)n)/log(2.f));
  lookup->n=n;
  lookup->trig=T;
  lookup->bitrev=bitrev;

  for(i=0;i<n/4;i++){
	T[i*2]=FLOAT_CONV(cos((M_PI/n)*(4*i)));
	T[i*2+1]=FLOAT_CONV(-sin((M_PI/n)*(4*i)));
	T[n2+i*2]=FLOAT_CONV(cos((M_PI/(2*n))*(2*i+1)));
	T[n2+i*2+1]=FLOAT_CONV(sin((M_PI/(2*n))*(2*i+1)));
  }
  for(i=0;i<n/8;i++){
	T[n+i*2]=FLOAT_CONV(cos((M_PI/n)*(4*i+2))*.5);
	T[n+i*2+1]=FLOAT_CONV(-sin((M_PI/n)*(4*i+2))*.5);
  }

  {
	int mask=(1<<(log2n-1))-1,i,j;
	int msb=1<<(log2n-2);
	for(i=0;i<n/8;i++){
	  int acc=0;
	  for(j=0;msb>>j;j++)
	if((msb>>j)&i)acc|=1<<j;
	  bitrev[i*2]=((~acc)&mask)-1;
	  bitrev[i*2+1]=acc;

	}
  }
  lookup->scale=FLOAT_CONV(4.f/n);
}

STIN void mdct_butterfly_8(DATA_TYPE *x){
  REG_TYPE r0   = x[6] + x[2];
  REG_TYPE r1   = x[6] - x[2];
  REG_TYPE r2   = x[4] + x[0];
  REG_TYPE r3   = x[4] - x[0];

	   x[6] = r0   + r2;
	   x[4] = r0   - r2;

	   r0   = x[5] - x[1];
	   r2   = x[7] - x[3];
	   x[0] = r1   + r0;
	   x[2] = r1   - r0;

	   r0   = x[5] + x[1];
	   r1   = x[7] + x[3];
	   x[3] = r2   + r3;
	   x[1] = r2   - r3;
	   x[7] = r1   + r0;
	   x[5] = r1   - r0;

}

STIN void mdct_butterfly_16(DATA_TYPE *x){
  REG_TYPE r0	 = x[1]  - x[9];
  REG_TYPE r1	 = x[0]  - x[8];

		   x[8]  += x[0];
		   x[9]  += x[1];
		   x[0]   = MULT_NORM((r0   + r1) * cPI2_8);
		   x[1]   = MULT_NORM((r0   - r1) * cPI2_8);

		   r0	 = x[3]  - x[11];
		   r1	 = x[10] - x[2];
		   x[10] += x[2];
		   x[11] += x[3];
		   x[2]   = r0;
		   x[3]   = r1;

		   r0	 = x[12] - x[4];
		   r1	 = x[13] - x[5];
		   x[12] += x[4];
		   x[13] += x[5];
		   x[4]   = MULT_NORM((r0   - r1) * cPI2_8);
		   x[5]   = MULT_NORM((r0   + r1) * cPI2_8);

		   r0	 = x[14] - x[6];
		   r1	 = x[15] - x[7];
		   x[14] += x[6];
		   x[15] += x[7];
		   x[6]  = r0;
		   x[7]  = r1;

	   mdct_butterfly_8(x);
	   mdct_butterfly_8(x+8);
}

STIN void mdct_butterfly_32(DATA_TYPE *x){
  REG_TYPE r0	 = x[30] - x[14];
  REG_TYPE r1	 = x[31] - x[15];

		   x[30] +=	 x[14];
	   x[31] +=	 x[15];
		   x[14]  =	 r0;
	   x[15]  =	 r1;

		   r0	 = x[28] - x[12];
	   r1	 = x[29] - x[13];
		   x[28] +=	 x[12];
	   x[29] +=	 x[13];
		   x[12]  = MULT_NORM( r0 * cPI1_8  -  r1 * cPI3_8 );
	   x[13]  = MULT_NORM( r0 * cPI3_8  +  r1 * cPI1_8 );

		   r0	 = x[26] - x[10];
	   r1	 = x[27] - x[11];
	   x[26] +=	 x[10];
	   x[27] +=	 x[11];
	   x[10]  = MULT_NORM(( r0  - r1 ) * cPI2_8);
	   x[11]  = MULT_NORM(( r0  + r1 ) * cPI2_8);

	   r0	 = x[24] - x[8];
	   r1	 = x[25] - x[9];
	   x[24] += x[8];
	   x[25] += x[9];
	   x[8]   = MULT_NORM( r0 * cPI3_8  -  r1 * cPI1_8 );
	   x[9]   = MULT_NORM( r1 * cPI3_8  +  r0 * cPI1_8 );

	   r0	 = x[22] - x[6];
	   r1	 = x[7]  - x[23];
	   x[22] += x[6];
	   x[23] += x[7];
	   x[6]   = r1;
	   x[7]   = r0;

	   r0	 = x[4]  - x[20];
	   r1	 = x[5]  - x[21];
	   x[20] += x[4];
	   x[21] += x[5];
	   x[4]   = MULT_NORM( r1 * cPI1_8  +  r0 * cPI3_8 );
	   x[5]   = MULT_NORM( r1 * cPI3_8  -  r0 * cPI1_8 );

	   r0	 = x[2]  - x[18];
	   r1	 = x[3]  - x[19];
	   x[18] += x[2];
	   x[19] += x[3];
	   x[2]   = MULT_NORM(( r1  + r0 ) * cPI2_8);
	   x[3]   = MULT_NORM(( r1  - r0 ) * cPI2_8);

	   r0	 = x[0]  - x[16];
	   r1	 = x[1]  - x[17];
	   x[16] += x[0];
	   x[17] += x[1];
	   x[0]   = MULT_NORM( r1 * cPI3_8  +  r0 * cPI1_8 );
	   x[1]   = MULT_NORM( r1 * cPI1_8  -  r0 * cPI3_8 );

	   mdct_butterfly_16(x);
	   mdct_butterfly_16(x+16);

}

STIN void mdct_butterfly_first(DATA_TYPE *T,
					DATA_TYPE *x,
					int points){

  DATA_TYPE *x1	= x	  + points	  - 8;
  DATA_TYPE *x2	= x	  + (points>>1) - 8;
  REG_TYPE   r0;
  REG_TYPE   r1;

  do{

			   r0	  = x1[6]	  -  x2[6];
		   r1	  = x1[7]	  -  x2[7];
		   x1[6]  += x2[6];
		   x1[7]  += x2[7];
		   x2[6]   = MULT_NORM(r1 * T[1]  +  r0 * T[0]);
		   x2[7]   = MULT_NORM(r1 * T[0]  -  r0 * T[1]);

		   r0	  = x1[4]	  -  x2[4];
		   r1	  = x1[5]	  -  x2[5];
		   x1[4]  += x2[4];
		   x1[5]  += x2[5];
		   x2[4]   = MULT_NORM(r1 * T[5]  +  r0 * T[4]);
		   x2[5]   = MULT_NORM(r1 * T[4]  -  r0 * T[5]);

		   r0	  = x1[2]	  -  x2[2];
		   r1	  = x1[3]	  -  x2[3];
		   x1[2]  += x2[2];
		   x1[3]  += x2[3];
		   x2[2]   = MULT_NORM(r1 * T[9]  +  r0 * T[8]);
		   x2[3]   = MULT_NORM(r1 * T[8]  -  r0 * T[9]);

		   r0	  = x1[0]	  -  x2[0];
		   r1	  = x1[1]	  -  x2[1];
		   x1[0]  += x2[0];
		   x1[1]  += x2[1];
		   x2[0]   = MULT_NORM(r1 * T[13] +  r0 * T[12]);
		   x2[1]   = MULT_NORM(r1 * T[12] -  r0 * T[13]);

	x1-=8;
	x2-=8;
	T+=16;

  }while(x2>=x);
}

STIN void mdct_butterfly_generic(DATA_TYPE *T,
					  DATA_TYPE *x,
					  int points,
					  int trigint){

  DATA_TYPE *x1	= x	  + points	  - 8;
  DATA_TYPE *x2	= x	  + (points>>1) - 8;
  REG_TYPE   r0;
  REG_TYPE   r1;

  do{

			   r0	  = x1[6]	  -  x2[6];
		   r1	  = x1[7]	  -  x2[7];
		   x1[6]  += x2[6];
		   x1[7]  += x2[7];
		   x2[6]   = MULT_NORM(r1 * T[1]  +  r0 * T[0]);
		   x2[7]   = MULT_NORM(r1 * T[0]  -  r0 * T[1]);

		   T+=trigint;

		   r0	  = x1[4]	  -  x2[4];
		   r1	  = x1[5]	  -  x2[5];
		   x1[4]  += x2[4];
		   x1[5]  += x2[5];
		   x2[4]   = MULT_NORM(r1 * T[1]  +  r0 * T[0]);
		   x2[5]   = MULT_NORM(r1 * T[0]  -  r0 * T[1]);

		   T+=trigint;

		   r0	  = x1[2]	  -  x2[2];
		   r1	  = x1[3]	  -  x2[3];
		   x1[2]  += x2[2];
		   x1[3]  += x2[3];
		   x2[2]   = MULT_NORM(r1 * T[1]  +  r0 * T[0]);
		   x2[3]   = MULT_NORM(r1 * T[0]  -  r0 * T[1]);

		   T+=trigint;

		   r0	  = x1[0]	  -  x2[0];
		   r1	  = x1[1]	  -  x2[1];
		   x1[0]  += x2[0];
		   x1[1]  += x2[1];
		   x2[0]   = MULT_NORM(r1 * T[1]  +  r0 * T[0]);
		   x2[1]   = MULT_NORM(r1 * T[0]  -  r0 * T[1]);

		   T+=trigint;
	x1-=8;
	x2-=8;

  }while(x2>=x);
}

STIN void mdct_butterflies(mdct_lookup *init,
				 DATA_TYPE *x,
				 int points){

  DATA_TYPE *T=init->trig;
  int stages=init->log2n-5;
  int i,j;

  if(--stages>0){
	mdct_butterfly_first(T,x,points);
  }

  for(i=1;--stages>0;i++){
	for(j=0;j<(1<<i);j++)
	  mdct_butterfly_generic(T,x+(points>>i)*j,points>>i,4<<i);
  }

  for(j=0;j<points;j+=32)
	mdct_butterfly_32(x+j);

}

void mdct_clear(mdct_lookup *l){
  if(l){
	if(l->trig)_ogg_free(l->trig);
	if(l->bitrev)_ogg_free(l->bitrev);
	memset(l,0,sizeof(*l));
  }
}

STIN void mdct_bitreverse(mdct_lookup *init,
				DATA_TYPE *x){
  int	n	   = init->n;
  int	   *bit	 = init->bitrev;
  DATA_TYPE *w0	  = x;
  DATA_TYPE *w1	  = x = w0+(n>>1);
  DATA_TYPE *T	   = init->trig+n;

  do{
	DATA_TYPE *x0	= x+bit[0];
	DATA_TYPE *x1	= x+bit[1];

	REG_TYPE  r0	 = x0[1]  - x1[1];
	REG_TYPE  r1	 = x0[0]  + x1[0];
	REG_TYPE  r2	 = MULT_NORM(r1	 * T[0]   + r0 * T[1]);
	REG_TYPE  r3	 = MULT_NORM(r1	 * T[1]   - r0 * T[0]);

		  w1	-= 4;

			  r0	 = HALVE(x0[1] + x1[1]);
			  r1	 = HALVE(x0[0] - x1[0]);

		  w0[0]  = r0	 + r2;
		  w1[2]  = r0	 - r2;
		  w0[1]  = r1	 + r3;
		  w1[3]  = r3	 - r1;

			  x0	 = x+bit[2];
			  x1	 = x+bit[3];

			  r0	 = x0[1]  - x1[1];
			  r1	 = x0[0]  + x1[0];
			  r2	 = MULT_NORM(r1	 * T[2]   + r0 * T[3]);
			  r3	 = MULT_NORM(r1	 * T[3]   - r0 * T[2]);

			  r0	 = HALVE(x0[1] + x1[1]);
			  r1	 = HALVE(x0[0] - x1[0]);

		  w0[2]  = r0	 + r2;
		  w1[0]  = r0	 - r2;
		  w0[3]  = r1	 + r3;
		  w1[1]  = r3	 - r1;

		  T	 += 4;
		  bit   += 4;
		  w0	+= 4;

  }while(w0<w1);
}

void mdct_backward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out){
  int n=init->n;
  int n2=n>>1;
  int n4=n>>2;

  DATA_TYPE *iX = in+n2-7;
  DATA_TYPE *oX = out+n2+n4;
  DATA_TYPE *T  = init->trig+n4;

  do{
	oX	 -= 4;
	oX[0]	   = MULT_NORM(-iX[2] * T[3] - iX[0]  * T[2]);
	oX[1]	   = MULT_NORM (iX[0] * T[3] - iX[2]  * T[2]);
	oX[2]	   = MULT_NORM(-iX[6] * T[1] - iX[4]  * T[0]);
	oX[3]	   = MULT_NORM (iX[4] * T[1] - iX[6]  * T[0]);
	iX	 -= 8;
	T	  += 4;
  }while(iX>=in);

  iX		= in+n2-8;
  oX		= out+n2+n4;
  T		 = init->trig+n4;

  do{
	T	  -= 4;
	oX[0]	   =  MULT_NORM (iX[4] * T[3] + iX[6] * T[2]);
	oX[1]	   =  MULT_NORM (iX[4] * T[2] - iX[6] * T[3]);
	oX[2]	   =  MULT_NORM (iX[0] * T[1] + iX[2] * T[0]);
	oX[3]	   =  MULT_NORM (iX[0] * T[0] - iX[2] * T[1]);
	iX	 -= 8;
	oX	 += 4;
  }while(iX>=in);

  mdct_butterflies(init,out+n2,n2);
  mdct_bitreverse(init,out);

  {
	DATA_TYPE *oX1=out+n2+n4;
	DATA_TYPE *oX2=out+n2+n4;
	DATA_TYPE *iX =out;
	T		 =init->trig+n2;

	do{
	  oX1-=4;

	  oX1[3]  =  MULT_NORM (iX[0] * T[1] - iX[1] * T[0]);
	  oX2[0]  = -MULT_NORM (iX[0] * T[0] + iX[1] * T[1]);

	  oX1[2]  =  MULT_NORM (iX[2] * T[3] - iX[3] * T[2]);
	  oX2[1]  = -MULT_NORM (iX[2] * T[2] + iX[3] * T[3]);

	  oX1[1]  =  MULT_NORM (iX[4] * T[5] - iX[5] * T[4]);
	  oX2[2]  = -MULT_NORM (iX[4] * T[4] + iX[5] * T[5]);

	  oX1[0]  =  MULT_NORM (iX[6] * T[7] - iX[7] * T[6]);
	  oX2[3]  = -MULT_NORM (iX[6] * T[6] + iX[7] * T[7]);

	  oX2+=4;
	  iX	+=   8;
	  T	 +=   8;
	}while(iX<oX1);

	iX=out+n2+n4;
	oX1=out+n4;
	oX2=oX1;

	do{
	  oX1-=4;
	  iX-=4;

	  oX2[0] = -(oX1[3] = iX[3]);
	  oX2[1] = -(oX1[2] = iX[2]);
	  oX2[2] = -(oX1[1] = iX[1]);
	  oX2[3] = -(oX1[0] = iX[0]);

	  oX2+=4;
	}while(oX2<iX);

	iX=out+n2+n4;
	oX1=out+n2+n4;
	oX2=out+n2;
	do{
	  oX1-=4;
	  oX1[0]= iX[3];
	  oX1[1]= iX[2];
	  oX1[2]= iX[1];
	  oX1[3]= iX[0];
	  iX+=4;
	}while(oX1>oX2);
  }
}

void mdct_forward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out){
  int n=init->n;
  int n2=n>>1;
  int n4=n>>2;
  int n8=n>>3;
  DATA_TYPE *w=(DATA_TYPE*) alloca(n*sizeof(*w)); /* forward needs working space */
  DATA_TYPE *w2=w+n2;

  REG_TYPE r0;
  REG_TYPE r1;
  DATA_TYPE *x0=in+n2+n4;
  DATA_TYPE *x1=x0+1;
  DATA_TYPE *T=init->trig+n2;

  int i=0;

  for(i=0;i<n8;i+=2){
	x0 -=4;
	T-=2;
	r0= x0[2] + x1[0];
	r1= x0[0] + x1[2];
	w2[i]=   MULT_NORM(r1*T[1] + r0*T[0]);
	w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
	x1 +=4;
  }

  x1=in+1;

  for(;i<n2-n8;i+=2){
	T-=2;
	x0 -=4;
	r0= x0[2] - x1[0];
	r1= x0[0] - x1[2];
	w2[i]=   MULT_NORM(r1*T[1] + r0*T[0]);
	w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
	x1 +=4;
  }

  x0=in+n;

  for(;i<n2;i+=2){
	T-=2;
	x0 -=4;
	r0= -x0[2] - x1[0];
	r1= -x0[0] - x1[2];
	w2[i]=   MULT_NORM(r1*T[1] + r0*T[0]);
	w2[i+1]= MULT_NORM(r1*T[0] - r0*T[1]);
	x1 +=4;
  }

  mdct_butterflies(init,w+n2,n2);
  mdct_bitreverse(init,w);

  T=init->trig+n2;
  x0=out+n2;

  for(i=0;i<n4;i++){
	x0--;
	out[i] =MULT_NORM((w[0]*T[0]+w[1]*T[1])*init->scale);
	x0[0]  =MULT_NORM((w[0]*T[1]-w[1]*T[0])*init->scale);
	w+=2;
	T+=2;
  }
}

#endif
/*** End of inlined file: mdct.c ***/


/*** Start of inlined file: psy.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <math.h>
#include <string.h>


/*** Start of inlined file: masking.h ***/
#ifndef _V_MASKING_H_
#define _V_MASKING_H_

#define MAX_ATH 88
static float ATH[]={
  -51, -52, -53, -54, -55, -56, -57, -58,
  -59, -60, -61, -62, -63, -64, -65, -66,
  -67, -68, -69, -70, -71, -72, -73, -74,
 -75, -76, -77, -78, -80, -81, -82, -83,
 -84, -85, -86, -87, -88, -88, -89, -89,
 -90, -91, -91, -92, -93, -94, -95, -96,
  -96, -97, -98, -98, -99, -99,-100,-100,
 -101,-102,-103,-104,-106,-107,-107,-107,
 -107,-105,-103,-102,-101, -99, -98, -96,
  -95, -95, -96, -97, -96, -95, -93, -90,
 -80, -70, -50, -40, -30, -30, -30, -30
};

#define EHMER_OFFSET 16
#define EHMER_MAX 56

static float tonemasks[P_BANDS][6][EHMER_MAX]={
  {{ -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
	 -60,  -60,  -60,  -60,  -62,  -62,  -65,  -73,
	 -69,  -68,  -68,  -67,  -70,  -70,  -72,  -74,
	 -75,  -79,  -79,  -80,  -83,  -88,  -93, -100,
	 -110, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
	 -48,  -48,  -48,  -48,  -48,  -53,  -61,  -66,
	 -66,  -68,  -67,  -70,  -76,  -76,  -72,  -73,
	 -75,  -76,  -78,  -79,  -83,  -88,  -93, -100,
	 -110, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
	 -38,  -40,  -42,  -46,  -48,  -53,  -55,  -62,
	 -65,  -58,  -56,  -56,  -61,  -60,  -65,  -67,
	 -69,  -71,  -77,  -77,  -78,  -80,  -82,  -84,
	 -88,  -93,  -98, -106, -112, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
	 -25,  -26,  -27,  -29,  -32,  -38,  -48,  -52,
	 -52,  -50,  -48,  -48,  -51,  -52,  -54,  -60,
	 -67,  -67,  -66,  -68,  -69,  -73,  -73,  -76,
	 -80,  -81,  -81,  -85,  -85,  -86,  -88,  -93,
	 -100, -110, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
	 -17,  -19,  -20,  -22,  -26,  -28,  -31,  -40,
	 -47,  -39,  -39,  -40,  -42,  -43,  -47,  -51,
	 -57,  -52,  -55,  -55,  -60,  -58,  -62,  -63,
	 -70,  -67,  -69,  -72,  -73,  -77,  -80,  -82,
	 -83,  -87,  -90,  -94,  -98, -104, -115, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   {  -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
	  -8,   -8,  -10,  -11,  -15,  -19,  -25,  -30,
	  -34,  -31,  -30,  -31,  -29,  -32,  -35,  -42,
	  -48,  -42,  -44,  -46,  -50,  -50,  -51,  -52,
	  -59,  -54,  -55,  -55,  -58,  -62,  -63,  -66,
	  -72,  -73,  -76,  -75,  -78,  -80,  -80,  -81,
	  -84,  -88,  -90,  -94,  -98, -101, -106, -110}},
  {{ -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
	 -66,  -66,  -66,  -66,  -66,  -67,  -67,  -67,
	 -76,  -72,  -71,  -74,  -76,  -76,  -75,  -78,
	 -79,  -79,  -81,  -83,  -86,  -89,  -93,  -97,
	 -100, -105, -110, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
	 -47,  -47,  -47,  -48,  -51,  -55,  -59,  -66,
	 -66,  -66,  -67,  -66,  -68,  -69,  -70,  -74,
	 -79,  -77,  -77,  -78,  -80,  -81,  -82,  -84,
	 -86,  -88,  -91,  -95, -100, -108, -116, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
	 -36,  -37,  -37,  -41,  -44,  -48,  -51,  -58,
	 -62,  -60,  -57,  -59,  -59,  -60,  -63,  -65,
	 -72,  -71,  -70,  -72,  -74,  -77,  -76,  -78,
	 -81,  -81,  -80,  -83,  -86,  -91,  -96, -100,
	 -105, -110, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
	 -28,  -30,  -32,  -32,  -33,  -35,  -41,  -49,
	 -50,  -49,  -47,  -48,  -48,  -52,  -51,  -57,
	 -65,  -61,  -59,  -61,  -64,  -69,  -70,  -74,
	 -77,  -77,  -78,  -81,  -84,  -85,  -87,  -90,
	 -92,  -96, -100, -107, -112, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
	 -20,  -21,  -23,  -27,  -30,  -35,  -36,  -41,
	 -46,  -44,  -42,  -40,  -41,  -41,  -43,  -48,
	 -55,  -53,  -52,  -53,  -56,  -59,  -58,  -60,
	 -67,  -66,  -69,  -71,  -72,  -75,  -79,  -81,
	 -84,  -87,  -90,  -93,  -97, -101, -107, -114,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   {  -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
	  -11,  -12,  -12,  -15,  -16,  -20,  -23,  -30,
	  -37,  -34,  -33,  -34,  -31,  -32,  -32,  -38,
	  -47,  -44,  -41,  -40,  -47,  -49,  -46,  -46,
	  -58,  -50,  -50,  -54,  -58,  -62,  -64,  -67,
	  -67,  -70,  -72,  -76,  -79,  -83,  -87,  -91,
	  -96, -100, -104, -110, -999, -999, -999, -999}},
  {{ -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
	 -62,  -62,  -63,  -64,  -66,  -67,  -66,  -68,
	 -75,  -72,  -76,  -75,  -76,  -78,  -79,  -82,
	 -84,  -85,  -90,  -94, -101, -110, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
	 -59,  -59,  -59,  -60,  -60,  -61,  -63,  -66,
	 -71,  -68,  -70,  -70,  -71,  -72,  -72,  -75,
	 -81,  -78,  -79,  -82,  -83,  -86,  -90,  -97,
	 -103, -113, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
	 -53,  -54,  -55,  -57,  -56,  -57,  -55,  -61,
	 -65,  -60,  -60,  -62,  -63,  -63,  -66,  -68,
	 -74,  -73,  -75,  -75,  -78,  -80,  -80,  -82,
	 -85,  -90,  -96, -101, -108, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
	 -46,  -46,  -47,  -47,  -47,  -47,  -48,  -51,
	 -57,  -51,  -49,  -50,  -51,  -53,  -54,  -59,
	 -66,  -60,  -62,  -67,  -67,  -70,  -72,  -75,
	 -76,  -78,  -81,  -85,  -88,  -94,  -97, -104,
	 -112, -999, -999, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
	 -39,  -41,  -42,  -42,  -39,  -38,  -41,  -43,
	 -52,  -44,  -40,  -39,  -37,  -37,  -40,  -47,
	 -54,  -50,  -48,  -50,  -55,  -61,  -59,  -62,
	 -66,  -66,  -66,  -69,  -69,  -73,  -74,  -74,
	 -75,  -77,  -79,  -82,  -87,  -91,  -95, -100,
	 -108, -115, -999, -999, -999, -999, -999, -999},
   { -28,  -26,  -24,  -22,  -20,  -20,  -23,  -29,
	 -30,  -31,  -28,  -27,  -28,  -28,  -28,  -35,
	 -40,  -33,  -32,  -29,  -30,  -30,  -30,  -37,
	 -45,  -41,  -37,  -38,  -45,  -47,  -47,  -48,
	 -53,  -49,  -48,  -50,  -49,  -49,  -51,  -52,
	 -58,  -56,  -57,  -56,  -60,  -61,  -62,  -70,
	 -72,  -74,  -78,  -83,  -88,  -93, -100, -106}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -110, -105, -100,  -95,  -91,  -87,  -83,
	-80,  -78,  -76,  -78,  -78,  -81,  -83,  -85,
	-86,  -85,  -86,  -87,  -90,  -97, -107, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -110, -105, -100,  -95,  -90,
	-85,  -81,  -77,  -73,  -70,  -67,  -67,  -68,
	-75,  -73,  -70,  -69,  -70,  -72,  -75,  -79,
	-84,  -83,  -84,  -86,  -88,  -89,  -89,  -93,
	-98, -105, -112, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-105, -100,  -95,  -90,  -85,  -80,  -76,  -71,
	-68,  -68,  -65,  -63,  -63,  -62,  -62,  -64,
	-65,  -64,  -61,  -62,  -63,  -64,  -66,  -68,
	-73,  -73,  -74,  -75,  -76,  -81,  -83,  -85,
	-88,  -89,  -92,  -95, -100, -108, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   { -80,  -75,  -71,  -68,  -65,  -63,  -62,  -61,
	 -61,  -61,  -61,  -59,  -56,  -57,  -53,  -50,
	 -58,  -52,  -50,  -50,  -52,  -53,  -54,  -58,
	 -67,  -63,  -67,  -68,  -72,  -75,  -78,  -80,
	 -81,  -81,  -82,  -85,  -89,  -90,  -93,  -97,
	 -101, -107, -114, -999, -999, -999, -999, -999,
	 -999, -999, -999, -999, -999, -999, -999, -999},
   { -65,  -61,  -59,  -57,  -56,  -55,  -55,  -56,
	 -56,  -57,  -55,  -53,  -52,  -47,  -44,  -44,
	 -50,  -44,  -41,  -39,  -39,  -42,  -40,  -46,
	 -51,  -49,  -50,  -53,  -54,  -63,  -60,  -61,
	 -62,  -66,  -66,  -66,  -70,  -73,  -74,  -75,
	 -76,  -75,  -79,  -85,  -89,  -91,  -96, -102,
	 -110, -999, -999, -999, -999, -999, -999, -999},
   { -52,  -50,  -49,  -49,  -48,  -48,  -48,  -49,
	 -50,  -50,  -49,  -46,  -43,  -39,  -35,  -33,
	 -38,  -36,  -32,  -29,  -32,  -32,  -32,  -35,
	 -44,  -39,  -38,  -38,  -46,  -50,  -45,  -46,
	 -53,  -50,  -50,  -50,  -54,  -54,  -53,  -53,
	 -56,  -57,  -59,  -66,  -70,  -72,  -74,  -79,
	 -83,  -85,  -90, -97, -114, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -110, -105,
	-100,  -95,  -90,  -86,  -80,  -75,  -75,  -79,
	-80,  -79,  -80,  -81,  -82,  -88,  -95, -103,
	-110, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -108, -103,  -98,  -93,
	-88,  -83,  -79,  -78,  -75,  -71,  -67,  -68,
	-73,  -73,  -72,  -73,  -75,  -77,  -80,  -82,
	-88,  -93, -100, -107, -114, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -110, -105, -101,  -96,  -90,
	-86,  -81,  -77,  -73,  -69,  -66,  -61,  -62,
	-66,  -64,  -62,  -65,  -66,  -70,  -72,  -76,
	-81,  -80,  -84,  -90,  -95, -102, -110, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -107, -103,  -97,  -92,  -88,
	-83,  -79,  -74,  -70,  -66,  -59,  -53,  -58,
	-62,  -55,  -54,  -54,  -54,  -58,  -61,  -62,
	-72,  -70,  -72,  -75,  -78,  -80,  -81,  -80,
	-83,  -83,  -88,  -93, -100, -107, -115, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -105, -100,  -95,  -90,  -85,
	-80,  -75,  -70,  -66,  -62,  -56,  -48,  -44,
	-48,  -46,  -46,  -43,  -46,  -48,  -48,  -51,
	-58,  -58,  -59,  -60,  -62,  -62,  -61,  -61,
	-65,  -64,  -65,  -68,  -70,  -74,  -75,  -78,
	-81,  -86,  -95, -110, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999,  -999, -105, -100,  -95,  -90,  -85,  -80,
	-75,  -70,  -65,  -61,  -55,  -49,  -39,  -33,
	-40,  -35,  -32,  -38,  -40,  -33,  -35,  -37,
	-46,  -41,  -45,  -44,  -46,  -42,  -45,  -46,
	-52,  -50,  -50,  -50,  -54,  -54,  -55,  -57,
	-62,  -64,  -66,  -68,  -70,  -76,  -81,  -90,
	-100, -110, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-105,  -98,  -90,  -85,  -82,  -83,  -80,  -78,
	-84,  -79,  -80,  -83,  -87,  -89,  -91,  -93,
	-99, -106, -117, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-105,  -98,  -90,  -85,  -80,  -75,  -70,  -68,
	-74,  -72,  -74,  -77,  -80,  -82,  -85,  -87,
	-92,  -89,  -91,  -95, -100, -106, -112, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-105,  -98,  -90,  -83,  -75,  -71,  -63,  -64,
	-67,  -62,  -64,  -67,  -70,  -73,  -77,  -81,
	-84,  -83,  -85,  -89,  -90,  -93,  -98, -104,
	-109, -114, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-103,  -96,  -88,  -81,  -75,  -68,  -58,  -54,
	-56,  -54,  -56,  -56,  -58,  -60,  -63,  -66,
	-74,  -69,  -72,  -72,  -75,  -74,  -77,  -81,
	-81,  -82,  -84,  -87,  -93,  -96,  -99, -104,
	-110, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -108, -102,  -96,
	-91,  -85,  -80,  -74,  -68,  -60,  -51,  -46,
	-48,  -46,  -43,  -45,  -47,  -47,  -49,  -48,
	-56,  -53,  -55,  -58,  -57,  -63,  -58,  -60,
	-66,  -64,  -67,  -70,  -70,  -74,  -77,  -84,
	-86,  -89,  -91,  -93,  -94, -101, -109, -118,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -108, -103,  -98,  -93,  -88,
	-83,  -78,  -73,  -68,  -60,  -53,  -44,  -35,
	-38,  -38,  -34,  -34,  -36,  -40,  -41,  -44,
	-51,  -45,  -46,  -47,  -46,  -54,  -50,  -49,
	-50,  -50,  -50,  -51,  -54,  -57,  -58,  -60,
	-66,  -66,  -66,  -64,  -65,  -68,  -77,  -82,
	-87,  -95, -110, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-107, -102,  -97,  -92,  -87,  -83,  -78,  -75,
	-82,  -79,  -83,  -85,  -89,  -92,  -95,  -98,
	-101, -105, -109, -113, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -106,
	-100,  -95,  -90,  -86,  -81,  -78,  -74,  -69,
	-74,  -74,  -76,  -79,  -83,  -84,  -86,  -89,
	-92,  -97,  -93, -100, -103, -107, -110, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -106, -100,
	-95, -90, -87, -83, -80, -75, -69, -60,
	-66, -66, -68, -70, -74, -78, -79, -81,
	-81, -83, -84, -87, -93, -96, -99, -103,
	-107, -110, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -108, -103, -98,
	-93, -89, -85, -82, -78, -71, -62, -55,
	-58, -58, -54, -54, -55, -59, -61, -62,
	-70, -66, -66, -67, -70, -72, -75, -78,
	-84, -84, -84, -88, -91, -90, -95, -98,
	-102, -103, -106, -110, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -108, -103,  -98,  -94,
	-90,  -87,  -82,  -79,  -73,  -67,  -58,  -47,
	-50,  -45,  -41,  -45,  -48,  -44,  -44,  -49,
	-54,  -51,  -48,  -47,  -49,  -50,  -51,  -57,
	-58,  -60,  -63,  -69,  -70,  -69,  -71,  -74,
	-78,  -82,  -90,  -95, -101, -105, -110, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -105, -101, -97, -93, -90,
	-85, -80, -77, -72, -65, -56, -48, -37,
	-40, -36, -34, -40, -50, -47, -38, -41,
	-47, -38, -35, -39, -38, -43, -40, -45,
	-50, -45, -44, -47, -50, -55, -48, -48,
	-52, -66, -70, -76, -82, -90, -97, -105,
	-110, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -108, -103,  -98,  -93,  -86,  -79,  -76,
	-83,  -81,  -85,  -87,  -89,  -93,  -98, -102,
	-107, -112, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -108, -103,  -98,  -93,  -86,  -79,  -71,
	-77,  -74,  -77,  -79,  -81,  -84,  -85,  -90,
	-92,  -93,  -92,  -98, -101, -108, -112, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-108, -103,  -98,  -93,  -87,  -78,  -68,  -65,
	-66,  -62,  -65,  -67,  -70,  -73,  -75,  -78,
	-82,  -82,  -83,  -84,  -91,  -93,  -98, -102,
	-106, -110, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-105, -100, -95, -90, -82, -74, -62, -57,
	-58, -56, -51, -52, -52, -54, -54, -58,
	-66, -59, -60, -63, -66, -69, -73, -79,
	-83, -84, -80, -81, -81, -82, -88, -92,
	-98, -105, -113, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -107,
	-102,  -97,  -92,  -84,  -79,  -69,  -57,  -47,
	-52,  -47,  -44,  -45,  -50,  -52,  -42,  -42,
	-53,  -43,  -43,  -48,  -51,  -56,  -55,  -52,
	-57,  -59,  -61,  -62,  -67,  -71,  -78,  -83,
	-86,  -94,  -98, -103, -110, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -105, -100,
	-95,  -90,  -84,  -78,  -70,  -61,  -51,  -41,
	-40,  -38,  -40,  -46,  -52,  -51,  -41,  -40,
	-46,  -40,  -38,  -38,  -41,  -46,  -41,  -46,
	-47,  -43,  -43,  -45,  -41,  -45,  -56,  -67,
	-68,  -83,  -87,  -90,  -95, -102, -107, -113,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -109, -105, -101,  -96,  -91,  -84,  -77,
	-82,  -82,  -85,  -89,  -94, -100, -106, -110,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -106, -103,  -98,  -92,  -85,  -80,  -71,
	-75,  -72,  -76,  -80,  -84,  -86,  -89,  -93,
	-100, -107, -113, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -107,
	-104, -101,  -97,  -92,  -88,  -84,  -80,  -64,
	-66,  -63,  -64,  -66,  -69,  -73,  -77,  -83,
	-83,  -86,  -91,  -98, -104, -111, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -107,
	-104, -101,  -97,  -92,  -90,  -84,  -74,  -57,
	-58,  -52,  -55,  -54,  -50,  -52,  -50,  -52,
	-63,  -62,  -69,  -76,  -77,  -78,  -78,  -79,
	-82,  -88,  -94, -100, -106, -111, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -106, -102,
	-98,  -95,  -90,  -85,  -83,  -78,  -70,  -50,
	-50,  -41,  -44,  -49,  -47,  -50,  -50,  -44,
	-55,  -46,  -47,  -48,  -48,  -54,  -49,  -49,
	-58,  -62,  -71,  -81,  -87,  -92,  -97, -102,
	-108, -114, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -106, -102,
	-98,  -95,  -90,  -85,  -83,  -78,  -70,  -45,
	-43,  -41,  -47,  -50,  -51,  -50,  -49,  -45,
	-47,  -41,  -44,  -41,  -39,  -43,  -38,  -37,
	-40,  -41,  -44,  -50,  -58,  -65,  -73,  -79,
	-85,  -92,  -97, -101, -105, -109, -113, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -107, -100,  -95,  -87,  -81,
	-85,  -83,  -88,  -93, -100, -107, -114, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -107, -101,  -95,  -88,  -83,  -76,
	-73,  -72,  -79,  -84,  -90,  -95, -100, -105,
	-110, -115, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -104,  -98,  -92,  -87,  -81,  -70,
	-65,  -62,  -67,  -71,  -74,  -80,  -85,  -91,
	-95,  -99, -103, -108, -111, -114, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -103,  -97,  -90,  -85,  -76,  -60,
	-56,  -54,  -60,  -62,  -61,  -56,  -63,  -65,
	-73,  -74,  -77,  -75,  -78,  -81,  -86,  -87,
	-88,  -91,  -94,  -98, -103, -110, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -105,
	-100,  -97,  -92,  -86,  -81,  -79,  -70,  -57,
	-51,  -47,  -51,  -58,  -60,  -56,  -53,  -50,
	-58,  -52,  -50,  -50,  -53,  -55,  -64,  -69,
	-71,  -85,  -82,  -78,  -81,  -85,  -95, -102,
	-112, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -105,
	-100,  -97,  -92,  -85,  -83,  -79,  -72,  -49,
	-40,  -43,  -43,  -54,  -56,  -51,  -50,  -40,
	-43,  -38,  -36,  -35,  -37,  -38,  -37,  -44,
	-54,  -60,  -57,  -60,  -70,  -75,  -84,  -92,
	-103, -112, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -110, -102,  -95,  -89,  -82,
	-83,  -84,  -90,  -92,  -99, -107, -113, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -107, -101,  -95,  -89,  -83,  -72,
	-74,  -78,  -85,  -88,  -88,  -90,  -92,  -98,
	-105, -111, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -109, -103, -97, -93, -87, -81, -70,
	-70, -67, -75, -73, -76, -79, -81, -83,
	-88, -89, -97, -103, -110, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -107, -100,  -94,  -88,  -83,  -75,  -63,
	-59,  -59,  -63,  -66,  -60,  -62,  -67,  -67,
	-77,  -76,  -81,  -88,  -86,  -92,  -96, -102,
	-109, -116, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -105,  -98,  -92,  -86,  -81,  -73,  -56,
	-52,  -47,  -55,  -60,  -58,  -52,  -51,  -45,
	-49,  -50,  -53,  -54,  -61,  -71,  -70,  -69,
	-78,  -79,  -87,  -90,  -96, -104, -112, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -103,  -96,  -90,  -86,  -78,  -70,  -51,
	-42,  -47,  -48,  -55,  -54,  -54,  -53,  -42,
	-35,  -28,  -33,  -38,  -37,  -44,  -47,  -49,
	-54,  -63,  -68,  -78,  -82,  -89,  -94,  -99,
	-104, -109, -114, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -110, -100,  -90,  -79,
	-85,  -81,  -82,  -82,  -89,  -94,  -99, -103,
	-109, -115, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -105,  -97,  -85,  -72,
	-74,  -70,  -70,  -70,  -76,  -85,  -91,  -93,
	-97, -103, -109, -115, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -112,  -93,  -81,  -68,
	-62,  -60,  -60,  -57,  -63,  -70,  -77,  -82,
	-90,  -93,  -98, -104, -109, -113, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -113, -100,  -93,  -84,  -63,
	-58,  -48,  -53,  -54,  -52,  -52,  -57,  -64,
	-66,  -76,  -83,  -81,  -85,  -85,  -90,  -95,
	-98, -101, -103, -106, -108, -111, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -105,  -95,  -86,  -74,  -53,
	-50,  -38,  -43,  -49,  -43,  -42,  -39,  -39,
	-46,  -52,  -57,  -56,  -72,  -69,  -74,  -81,
	-87,  -92,  -94,  -97,  -99, -102, -105, -108,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -108,  -99,  -90,  -76,  -66,  -45,
	-43,  -41,  -44,  -47,  -43,  -47,  -40,  -30,
	-31,  -31,  -39,  -33,  -40,  -41,  -43,  -53,
	-59,  -70,  -73,  -77,  -79,  -82,  -84,  -87,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -110,  -91,  -76,
	-75,  -85,  -93,  -98, -104, -110, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -110,  -91,  -70,
	-70,  -75,  -86,  -89,  -94,  -98, -101, -106,
	-110, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -110,  -95,  -80,  -60,
	-65,  -64,  -74,  -83,  -88,  -91,  -95,  -99,
	-103, -107, -110, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -110,  -95,  -80,  -58,
	-55,  -49,  -66,  -68,  -71,  -78,  -78,  -80,
	-88,  -85,  -89,  -97, -100, -105, -110, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -110,  -95,  -80,  -53,
	-52,  -41,  -59,  -59,  -49,  -58,  -56,  -63,
	-86,  -79,  -90,  -93,  -98, -103, -107, -112,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -110,  -97,  -91,  -73,  -45,
	-40,  -33,  -53,  -61,  -49,  -54,  -50,  -50,
	-60,  -52,  -67,  -74,  -81,  -92,  -96, -100,
	-105, -110, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -113, -106,  -99,  -92,  -77,
	-80,  -88,  -97, -106, -115, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -116, -109, -102,  -95,  -89,  -74,
	-72,  -88,  -87,  -95, -102, -109, -116, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -116, -109, -102,  -95,  -89,  -75,
	-66,  -74,  -77,  -78,  -86,  -87,  -90,  -96,
	-105, -115, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -115, -108, -101,  -94,  -88,  -66,
	-56,  -61,  -70,  -65,  -78,  -72,  -83,  -84,
	-93,  -98, -105, -110, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -110, -105,  -95,  -89,  -82,  -57,
	-52,  -52,  -59,  -56,  -59,  -58,  -69,  -67,
	-88,  -82,  -82,  -89,  -94, -100, -108, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -110, -101,  -96,  -90,  -83,  -77,  -54,
	-43,  -38,  -50,  -48,  -52,  -48,  -42,  -42,
	-51,  -52,  -53,  -59,  -65,  -71,  -78,  -85,
	-95, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -120, -105,  -86,  -68,
	-78,  -79,  -90, -100, -110, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -120, -105,  -86,  -66,
	-73,  -77,  -88,  -96, -105, -115, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -120, -105,  -92,  -80,  -61,
	-64,  -68,  -80,  -87,  -92, -100, -110, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -120, -104,  -91,  -79,  -52,
	-60,  -54,  -64,  -69,  -77,  -80,  -82,  -84,
	-85,  -87,  -88,  -90, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -118, -100,  -87,  -77,  -49,
	-50,  -44,  -58,  -61,  -61,  -67,  -65,  -62,
	-62,  -62,  -65,  -68, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -115,  -98,  -84,  -62,  -49,
	-44,  -38,  -46,  -49,  -49,  -46,  -39,  -37,
	-39,  -40,  -42,  -43, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -110,  -88,  -74,
	-77,  -82,  -82,  -85,  -90,  -94,  -99, -104,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -110,  -88,  -66,
	-70,  -81,  -80,  -81,  -84,  -88,  -91,  -93,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -110,  -88,  -61,
	-63,  -70,  -71,  -74,  -77,  -80,  -83,  -85,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -110, -86, -62,
	-63,  -62,  -62,  -58,  -52,  -50,  -50,  -52,
	-54, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -118, -108,  -84,  -53,
	-50,  -50,  -50,  -55,  -47,  -45,  -40,  -40,
	-40, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -118, -100,  -73,  -43,
	-37,  -42,  -43,  -53,  -38,  -37,  -35,  -35,
	-38, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}},
  {{-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -110, -100,  -91,  -84,  -74,
	-80,  -80,  -80,  -80,  -80, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -110, -100,  -91,  -84,  -74,
	-68,  -68,  -68,  -68,  -68, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -110, -100,  -86,  -78,  -70,
	-60,  -45,  -30,  -21, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -110, -100,  -87,  -78,  -67,
	-48,  -38,  -29,  -21, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -110, -100,  -86,  -69,  -56,
	-45,  -35,  -33,  -29, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999},
   {-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -110, -100,  -83,  -71,  -48,
	-27,  -38,  -37,  -34, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999,
	-999, -999, -999, -999, -999, -999, -999, -999}}
};

#endif
/*** End of inlined file: masking.h ***/

#define NEGINF -9999.f
static double stereo_threshholds[]={0.0, .5, 1.0, 1.5, 2.5, 4.5, 8.5, 16.5, 9e10};
static double stereo_threshholds_limited[]={0.0, .5, 1.0, 1.5, 2.0, 2.5, 4.5, 8.5, 9e10};

vorbis_look_psy_global *_vp_global_look(vorbis_info *vi){
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy_global *gi=&ci->psy_g_param;
  vorbis_look_psy_global *look=(vorbis_look_psy_global*)_ogg_calloc(1,sizeof(*look));

  look->channels=vi->channels;

  look->ampmax=-9999.;
  look->gi=gi;
  return(look);
}

void _vp_global_free(vorbis_look_psy_global *look){
  if(look){
	memset(look,0,sizeof(*look));
	_ogg_free(look);
  }
}

void _vi_gpsy_free(vorbis_info_psy_global *i){
  if(i){
	memset(i,0,sizeof(*i));
	_ogg_free(i);
  }
}

void _vi_psy_free(vorbis_info_psy *i){
  if(i){
	memset(i,0,sizeof(*i));
	_ogg_free(i);
  }
}

static void min_curve(float *c,
			   float *c2){
  int i;
  for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
}
static void max_curve(float *c,
			   float *c2){
  int i;
  for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
}

static void attenuate_curve(float *c,float att){
  int i;
  for(i=0;i<EHMER_MAX;i++)
	c[i]+=att;
}

static float ***setup_tone_curves(float curveatt_dB[P_BANDS],float binHz,int n,
				  float center_boost, float center_decay_rate){
  int i,j,k,m;
  float ath[EHMER_MAX];
  float workc[P_BANDS][P_LEVELS][EHMER_MAX];
  float athc[P_LEVELS][EHMER_MAX];
  float *brute_buffer=(float*) alloca(n*sizeof(*brute_buffer));

  float ***ret=(float***) _ogg_malloc(sizeof(*ret)*P_BANDS);

  memset(workc,0,sizeof(workc));

  for(i=0;i<P_BANDS;i++){

	int ath_offset=i*4;
	for(j=0;j<EHMER_MAX;j++){
	  float min=999.;
	  for(k=0;k<4;k++)
	if(j+k+ath_offset<MAX_ATH){
	  if(min>ATH[j+k+ath_offset])min=ATH[j+k+ath_offset];
	}else{
	  if(min>ATH[MAX_ATH-1])min=ATH[MAX_ATH-1];
	}
	  ath[j]=min;
	}

	for(j=0;j<6;j++)
	  memcpy(workc[i][j+2],tonemasks[i][j],EHMER_MAX*sizeof(*tonemasks[i][j]));
	memcpy(workc[i][0],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
	memcpy(workc[i][1],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));

	for(j=0;j<P_LEVELS;j++){
	  for(k=0;k<EHMER_MAX;k++){
	float adj=center_boost+abs(EHMER_OFFSET-k)*center_decay_rate;
	if(adj<0. && center_boost>0)adj=0.;
	if(adj>0. && center_boost<0)adj=0.;
	workc[i][j][k]+=adj;
	  }
	}

	for(j=0;j<P_LEVELS;j++){
	  attenuate_curve(workc[i][j],curveatt_dB[i]+100.-(j<2?2:j)*10.-P_LEVEL_0);
	  memcpy(athc[j],ath,EHMER_MAX*sizeof(**athc));
	  attenuate_curve(athc[j],+100.-j*10.f-P_LEVEL_0);
	  max_curve(athc[j],workc[i][j]);
	}

	for(j=1;j<P_LEVELS;j++){
	  min_curve(athc[j],athc[j-1]);
	  min_curve(workc[i][j],athc[j]);
	}
  }

  for(i=0;i<P_BANDS;i++){
	int hi_curve,lo_curve,bin;
	ret[i]=(float**)_ogg_malloc(sizeof(**ret)*P_LEVELS);

	bin=floor(fromOC(i*.5)/binHz);
	lo_curve=  ceil(toOC(bin*binHz+1)*2);
	hi_curve=  floor(toOC((bin+1)*binHz)*2);
	if(lo_curve>i)lo_curve=i;
	if(lo_curve<0)lo_curve=0;
	if(hi_curve>=P_BANDS)hi_curve=P_BANDS-1;

	for(m=0;m<P_LEVELS;m++){
	  ret[i][m]=(float*)_ogg_malloc(sizeof(***ret)*(EHMER_MAX+2));

	  for(j=0;j<n;j++)brute_buffer[j]=999.;

	  for(k=lo_curve;k<=hi_curve;k++){
	int l=0;

	for(j=0;j<EHMER_MAX;j++){
	  int lo_bin= fromOC(j*.125+k*.5-2.0625)/binHz;
	  int hi_bin= fromOC(j*.125+k*.5-1.9375)/binHz+1;

	  if(lo_bin<0)lo_bin=0;
	  if(lo_bin>n)lo_bin=n;
	  if(lo_bin<l)l=lo_bin;
	  if(hi_bin<0)hi_bin=0;
	  if(hi_bin>n)hi_bin=n;

	  for(;l<hi_bin && l<n;l++)
		if(brute_buffer[l]>workc[k][m][j])
		  brute_buffer[l]=workc[k][m][j];
	}

	for(;l<n;l++)
	  if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
		brute_buffer[l]=workc[k][m][EHMER_MAX-1];

	  }

	  if(i+1<P_BANDS){
	int l=0;
	k=i+1;
	for(j=0;j<EHMER_MAX;j++){
	  int lo_bin= fromOC(j*.125+i*.5-2.0625)/binHz;
	  int hi_bin= fromOC(j*.125+i*.5-1.9375)/binHz+1;

	  if(lo_bin<0)lo_bin=0;
	  if(lo_bin>n)lo_bin=n;
	  if(lo_bin<l)l=lo_bin;
	  if(hi_bin<0)hi_bin=0;
	  if(hi_bin>n)hi_bin=n;

	  for(;l<hi_bin && l<n;l++)
		if(brute_buffer[l]>workc[k][m][j])
		  brute_buffer[l]=workc[k][m][j];
	}

	for(;l<n;l++)
	  if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
		brute_buffer[l]=workc[k][m][EHMER_MAX-1];

	  }

	  for(j=0;j<EHMER_MAX;j++){
	int bin=fromOC(j*.125+i*.5-2.)/binHz;
	if(bin<0){
	  ret[i][m][j+2]=-999.;
	}else{
	  if(bin>=n){
		ret[i][m][j+2]=-999.;
	  }else{
		ret[i][m][j+2]=brute_buffer[bin];
	  }
	}
	  }

	  for(j=0;j<EHMER_OFFSET;j++)
	if(ret[i][m][j+2]>-200.f)break;
	  ret[i][m][0]=j;

	  for(j=EHMER_MAX-1;j>EHMER_OFFSET+1;j--)
	if(ret[i][m][j+2]>-200.f)
	  break;
	  ret[i][m][1]=j;

	}
  }

  return(ret);
}

void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,
		  vorbis_info_psy_global *gi,int n,long rate){
  long i,j,lo=-99,hi=1;
  long maxoc;
  memset(p,0,sizeof(*p));

  p->eighth_octave_lines=gi->eighth_octave_lines;
  p->shiftoc=rint(log(gi->eighth_octave_lines*8.f)/log(2.f))-1;

  p->firstoc=toOC(.25f*rate*.5/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines;
  maxoc=toOC((n+.25f)*rate*.5/n)*(1<<(p->shiftoc+1))+.5f;
  p->total_octave_lines=maxoc-p->firstoc+1;
  p->ath=(float*)_ogg_malloc(n*sizeof(*p->ath));

  p->octave=(long*)_ogg_malloc(n*sizeof(*p->octave));
  p->bark=(long*)_ogg_malloc(n*sizeof(*p->bark));
  p->vi=vi;
  p->n=n;
  p->rate=rate;

  p->m_val = 1.;
  if(rate < 26000) p->m_val = 0;
  else if(rate < 38000) p->m_val = .94;   /* 32kHz */
  else if(rate > 46000) p->m_val = 1.275; /* 48kHz */

  for(i=0,j=0;i<MAX_ATH-1;i++){
	int endpos=rint(fromOC((i+1)*.125-2.)*2*n/rate);
	float base=ATH[i];
	if(j<endpos){
	  float delta=(ATH[i+1]-base)/(endpos-j);
	  for(;j<endpos && j<n;j++){
		p->ath[j]=base+100.;
		base+=delta;
	  }
	}
  }

  for(i=0;i<n;i++){
	float bark=toBARK(rate/(2*n)*i);

	for(;lo+vi->noisewindowlomin<i &&
	  toBARK(rate/(2*n)*lo)<(bark-vi->noisewindowlo);lo++);

	for(;hi<=n && (hi<i+vi->noisewindowhimin ||
	  toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++);

	p->bark[i]=((lo-1)<<16)+(hi-1);

  }

  for(i=0;i<n;i++)
	p->octave[i]=toOC((i+.25f)*.5*rate/n)*(1<<(p->shiftoc+1))+.5f;

  p->tonecurves=setup_tone_curves(vi->toneatt,rate*.5/n,n,
				  vi->tone_centerboost,vi->tone_decay);

  p->noiseoffset=(float**)_ogg_malloc(P_NOISECURVES*sizeof(*p->noiseoffset));
  for(i=0;i<P_NOISECURVES;i++)
	p->noiseoffset[i]=(float*)_ogg_malloc(n*sizeof(**p->noiseoffset));

  for(i=0;i<n;i++){
	float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
	int inthalfoc;
	float del;

	if(halfoc<0)halfoc=0;
	if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
	inthalfoc=(int)halfoc;
	del=halfoc-inthalfoc;

	for(j=0;j<P_NOISECURVES;j++)
	  p->noiseoffset[j][i]=
	p->vi->noiseoff[j][inthalfoc]*(1.-del) +
	p->vi->noiseoff[j][inthalfoc+1]*del;

  }
#if 0
  {
	static int ls=0;
	_analysis_output_always("noiseoff0",ls,p->noiseoffset[0],n,1,0,0);
	_analysis_output_always("noiseoff1",ls,p->noiseoffset[1],n,1,0,0);
	_analysis_output_always("noiseoff2",ls++,p->noiseoffset[2],n,1,0,0);
  }
#endif
}

void _vp_psy_clear(vorbis_look_psy *p){
  int i,j;
  if(p){
	if(p->ath)_ogg_free(p->ath);
	if(p->octave)_ogg_free(p->octave);
	if(p->bark)_ogg_free(p->bark);
	if(p->tonecurves){
	  for(i=0;i<P_BANDS;i++){
	for(j=0;j<P_LEVELS;j++){
	  _ogg_free(p->tonecurves[i][j]);
	}
	_ogg_free(p->tonecurves[i]);
	  }
	  _ogg_free(p->tonecurves);
	}
	if(p->noiseoffset){
	  for(i=0;i<P_NOISECURVES;i++){
		_ogg_free(p->noiseoffset[i]);
	  }
	  _ogg_free(p->noiseoffset);
	}
	memset(p,0,sizeof(*p));
  }
}

static void seed_curve(float *seed,
			   const float **curves,
			   float amp,
			   int oc, int n,
			   int linesper,float dBoffset){
  int i,post1;
  int seedptr;
  const float *posts,*curve;

  int choice=(int)((amp+dBoffset-P_LEVEL_0)*.1f);
  choice=max(choice,0);
  choice=min(choice,P_LEVELS-1);
  posts=curves[choice];
  curve=posts+2;
  post1=(int)posts[1];
  seedptr=oc+(posts[0]-EHMER_OFFSET)*linesper-(linesper>>1);

  for(i=posts[0];i<post1;i++){
	if(seedptr>0){
	  float lin=amp+curve[i];
	  if(seed[seedptr]<lin)seed[seedptr]=lin;
	}
	seedptr+=linesper;
	if(seedptr>=n)break;
  }
}

static void seed_loop(vorbis_look_psy *p,
			  const float ***curves,
			  const float *f,
			  const float *flr,
			  float *seed,
			  float specmax){
  vorbis_info_psy *vi=p->vi;
  long n=p->n,i;
  float dBoffset=vi->max_curve_dB-specmax;

  for(i=0;i<n;i++){
	float max=f[i];
	long oc=p->octave[i];
	while(i+1<n && p->octave[i+1]==oc){
	  i++;
	  if(f[i]>max)max=f[i];
	}

	if(max+6.f>flr[i]){
	  oc=oc>>p->shiftoc;

	  if(oc>=P_BANDS)oc=P_BANDS-1;
	  if(oc<0)oc=0;

	  seed_curve(seed,
		 curves[oc],
		 max,
		 p->octave[i]-p->firstoc,
		 p->total_octave_lines,
		 p->eighth_octave_lines,
		 dBoffset);
	}
  }
}

static void seed_chase(float *seeds, int linesper, long n){
  long  *posstack=(long*)alloca(n*sizeof(*posstack));
  float *ampstack=(float*)alloca(n*sizeof(*ampstack));
  long   stack=0;
  long   pos=0;
  long   i;

  for(i=0;i<n;i++){
	if(stack<2){
	  posstack[stack]=i;
	  ampstack[stack++]=seeds[i];
	}else{
	  while(1){
	if(seeds[i]<ampstack[stack-1]){
	  posstack[stack]=i;
	  ampstack[stack++]=seeds[i];
	  break;
	}else{
	  if(i<posstack[stack-1]+linesper){
		if(stack>1 && ampstack[stack-1]<=ampstack[stack-2] &&
		   i<posstack[stack-2]+linesper){
		  stack--;
		  continue;
		}
	  }
	  posstack[stack]=i;
	  ampstack[stack++]=seeds[i];
	  break;

	}
	  }
	}
  }

  for(i=0;i<stack;i++){
	long endpos;
	if(i<stack-1 && ampstack[i+1]>ampstack[i]){
	  endpos=posstack[i+1];
	}else{
	  endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is
					discarded in short frames */
	}
	if(endpos>n)endpos=n;
	for(;pos<endpos;pos++)
	  seeds[pos]=ampstack[i];
  }

}

#include<stdio.h>
static void max_seeds(vorbis_look_psy *p,
			  float *seed,
			  float *flr){
  long   n=p->total_octave_lines;
  int	linesper=p->eighth_octave_lines;
  long   linpos=0;
  long   pos;

  seed_chase(seed,linesper,n); /* for masking */

  pos=p->octave[0]-p->firstoc-(linesper>>1);

  while(linpos+1<p->n){
	float minV=seed[pos];
	long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc;
	if(minV>p->vi->tone_abs_limit)minV=p->vi->tone_abs_limit;
	while(pos+1<=end){
	  pos++;
	  if((seed[pos]>NEGINF && seed[pos]<minV) || minV==NEGINF)
	minV=seed[pos];
	}

	end=pos+p->firstoc;
	for(;linpos<p->n && p->octave[linpos]<=end;linpos++)
	  if(flr[linpos]<minV)flr[linpos]=minV;
  }

  {
	float minV=seed[p->total_octave_lines-1];
	for(;linpos<p->n;linpos++)
	  if(flr[linpos]<minV)flr[linpos]=minV;
  }

}

static void bark_noise_hybridmp(int n,const long *b,
								const float *f,
								float *noise,
								const float offset,
								const int fixed){

  float *N=(float*) alloca(n*sizeof(*N));
  float *X=(float*) alloca(n*sizeof(*N));
  float *XX=(float*) alloca(n*sizeof(*N));
  float *Y=(float*) alloca(n*sizeof(*N));
  float *XY=(float*) alloca(n*sizeof(*N));

  float tN, tX, tXX, tY, tXY;
  int i;

  int lo, hi;
  float R, A, B, D;
  float w, x, y;

  tN = tX = tXX = tY = tXY = 0.f;

  y = f[0] + offset;
  if (y < 1.f) y = 1.f;

  w = y * y * .5;

  tN += w;
  tX += w;
  tY += w * y;

  N[0] = tN;
  X[0] = tX;
  XX[0] = tXX;
  Y[0] = tY;
  XY[0] = tXY;

  for (i = 1, x = 1.f; i < n; i++, x += 1.f) {

	y = f[i] + offset;
	if (y < 1.f) y = 1.f;

	w = y * y;

	tN += w;
	tX += w * x;
	tXX += w * x * x;
	tY += w * y;
	tXY += w * x * y;

	N[i] = tN;
	X[i] = tX;
	XX[i] = tXX;
	Y[i] = tY;
	XY[i] = tXY;
  }

  for (i = 0, x = 0.f;; i++, x += 1.f) {

	lo = b[i] >> 16;
	if( lo>=0 ) break;
	hi = b[i] & 0xffff;

	tN = N[hi] + N[-lo];
	tX = X[hi] - X[-lo];
	tXX = XX[hi] + XX[-lo];
	tY = Y[hi] + Y[-lo];
	tXY = XY[hi] - XY[-lo];

	A = tY * tXX - tX * tXY;
	B = tN * tXY - tX * tY;
	D = tN * tXX - tX * tX;
	R = (A + x * B) / D;
	if (R < 0.f)
	  R = 0.f;

	noise[i] = R - offset;
  }

  for ( ;; i++, x += 1.f) {

	lo = b[i] >> 16;
	hi = b[i] & 0xffff;
	if(hi>=n)break;

	tN = N[hi] - N[lo];
	tX = X[hi] - X[lo];
	tXX = XX[hi] - XX[lo];
	tY = Y[hi] - Y[lo];
	tXY = XY[hi] - XY[lo];

	A = tY * tXX - tX * tXY;
	B = tN * tXY - tX * tY;
	D = tN * tXX - tX * tX;
	R = (A + x * B) / D;
	if (R < 0.f) R = 0.f;

	noise[i] = R - offset;
  }
  for ( ; i < n; i++, x += 1.f) {

	R = (A + x * B) / D;
	if (R < 0.f) R = 0.f;

	noise[i] = R - offset;
  }

  if (fixed <= 0) return;

  for (i = 0, x = 0.f;; i++, x += 1.f) {
	hi = i + fixed / 2;
	lo = hi - fixed;
	if(lo>=0)break;

	tN = N[hi] + N[-lo];
	tX = X[hi] - X[-lo];
	tXX = XX[hi] + XX[-lo];
	tY = Y[hi] + Y[-lo];
	tXY = XY[hi] - XY[-lo];

	A = tY * tXX - tX * tXY;
	B = tN * tXY - tX * tY;
	D = tN * tXX - tX * tX;
	R = (A + x * B) / D;

	if (R - offset < noise[i]) noise[i] = R - offset;
  }
  for ( ;; i++, x += 1.f) {

	hi = i + fixed / 2;
	lo = hi - fixed;
	if(hi>=n)break;

	tN = N[hi] - N[lo];
	tX = X[hi] - X[lo];
	tXX = XX[hi] - XX[lo];
	tY = Y[hi] - Y[lo];
	tXY = XY[hi] - XY[lo];

	A = tY * tXX - tX * tXY;
	B = tN * tXY - tX * tY;
	D = tN * tXX - tX * tX;
	R = (A + x * B) / D;

	if (R - offset < noise[i]) noise[i] = R - offset;
  }
  for ( ; i < n; i++, x += 1.f) {
	R = (A + x * B) / D;
	if (R - offset < noise[i]) noise[i] = R - offset;
  }
}

static float FLOOR1_fromdB_INV_LOOKUP[256]={
  0.F, 8.81683e+06F, 8.27882e+06F, 7.77365e+06F,
  7.29930e+06F, 6.85389e+06F, 6.43567e+06F, 6.04296e+06F,
  5.67422e+06F, 5.32798e+06F, 5.00286e+06F, 4.69759e+06F,
  4.41094e+06F, 4.14178e+06F, 3.88905e+06F, 3.65174e+06F,
  3.42891e+06F, 3.21968e+06F, 3.02321e+06F, 2.83873e+06F,
  2.66551e+06F, 2.50286e+06F, 2.35014e+06F, 2.20673e+06F,
  2.07208e+06F, 1.94564e+06F, 1.82692e+06F, 1.71544e+06F,
  1.61076e+06F, 1.51247e+06F, 1.42018e+06F, 1.33352e+06F,
  1.25215e+06F, 1.17574e+06F, 1.10400e+06F, 1.03663e+06F,
  973377.F, 913981.F, 858210.F, 805842.F,
  756669.F, 710497.F, 667142.F, 626433.F,
  588208.F, 552316.F, 518613.F, 486967.F,
  457252.F, 429351.F, 403152.F, 378551.F,
  355452.F, 333762.F, 313396.F, 294273.F,
  276316.F, 259455.F, 243623.F, 228757.F,
  214798.F, 201691.F, 189384.F, 177828.F,
  166977.F, 156788.F, 147221.F, 138237.F,
  129802.F, 121881.F, 114444.F, 107461.F,
  100903.F, 94746.3F, 88964.9F, 83536.2F,
  78438.8F, 73652.5F, 69158.2F, 64938.1F,
  60975.6F, 57254.9F, 53761.2F, 50480.6F,
  47400.3F, 44507.9F, 41792.0F, 39241.9F,
  36847.3F, 34598.9F, 32487.7F, 30505.3F,
  28643.8F, 26896.0F, 25254.8F, 23713.7F,
  22266.7F, 20908.0F, 19632.2F, 18434.2F,
  17309.4F, 16253.1F, 15261.4F, 14330.1F,
  13455.7F, 12634.6F, 11863.7F, 11139.7F,
  10460.0F, 9821.72F, 9222.39F, 8659.64F,
  8131.23F, 7635.06F, 7169.17F, 6731.70F,
  6320.93F, 5935.23F, 5573.06F, 5232.99F,
  4913.67F, 4613.84F, 4332.30F, 4067.94F,
  3819.72F, 3586.64F, 3367.78F, 3162.28F,
  2969.31F, 2788.13F, 2617.99F, 2458.24F,
  2308.24F, 2167.39F, 2035.14F, 1910.95F,
  1794.35F, 1684.85F, 1582.04F, 1485.51F,
  1394.86F, 1309.75F, 1229.83F, 1154.78F,
  1084.32F, 1018.15F, 956.024F, 897.687F,
  842.910F, 791.475F, 743.179F, 697.830F,
  655.249F, 615.265F, 577.722F, 542.469F,
  509.367F, 478.286F, 449.101F, 421.696F,
  395.964F, 371.803F, 349.115F, 327.812F,
  307.809F, 289.026F, 271.390F, 254.830F,
  239.280F, 224.679F, 210.969F, 198.096F,
  186.008F, 174.658F, 164.000F, 153.993F,
  144.596F, 135.773F, 127.488F, 119.708F,
  112.404F, 105.545F, 99.1046F, 93.0572F,
  87.3788F, 82.0469F, 77.0404F, 72.3394F,
  67.9252F, 63.7804F, 59.8885F, 56.2341F,
  52.8027F, 49.5807F, 46.5553F, 43.7144F,
  41.0470F, 38.5423F, 36.1904F, 33.9821F,
  31.9085F, 29.9614F, 28.1332F, 26.4165F,
  24.8045F, 23.2910F, 21.8697F, 20.5352F,
  19.2822F, 18.1056F, 17.0008F, 15.9634F,
  14.9893F, 14.0746F, 13.2158F, 12.4094F,
  11.6522F, 10.9411F, 10.2735F, 9.64662F,
  9.05798F, 8.50526F, 7.98626F, 7.49894F,
  7.04135F, 6.61169F, 6.20824F, 5.82941F,
  5.47370F, 5.13970F, 4.82607F, 4.53158F,
  4.25507F, 3.99542F, 3.75162F, 3.52269F,
  3.30774F, 3.10590F, 2.91638F, 2.73842F,
  2.57132F, 2.41442F, 2.26709F, 2.12875F,
  1.99885F, 1.87688F, 1.76236F, 1.65482F,
  1.55384F, 1.45902F, 1.36999F, 1.28640F,
  1.20790F, 1.13419F, 1.06499F, 1.F
};

void _vp_remove_floor(vorbis_look_psy *p,
			  float *mdct,
			  int *codedflr,
			  float *residue,
			  int sliding_lowpass){

  int i,n=p->n;

  if(sliding_lowpass>n)sliding_lowpass=n;

  for(i=0;i<sliding_lowpass;i++){
	residue[i]=
	  mdct[i]*FLOOR1_fromdB_INV_LOOKUP[codedflr[i]];
  }

  for(;i<n;i++)
	residue[i]=0.;
}

void _vp_noisemask(vorbis_look_psy *p,
		   float *logmdct,
		   float *logmask){

  int i,n=p->n;
  float *work=(float*) alloca(n*sizeof(*work));

  bark_noise_hybridmp(n,p->bark,logmdct,logmask,
			  140.,-1);

  for(i=0;i<n;i++)work[i]=logmdct[i]-logmask[i];

  bark_noise_hybridmp(n,p->bark,work,logmask,0.,
			  p->vi->noisewindowfixed);

  for(i=0;i<n;i++)work[i]=logmdct[i]-work[i];

#if 0
  {
	static int seq=0;

	float work2[n];
	for(i=0;i<n;i++){
	  work2[i]=logmask[i]+work[i];
	}

	if(seq&1)
	  _analysis_output("median2R",seq/2,work,n,1,0,0);
	else
	  _analysis_output("median2L",seq/2,work,n,1,0,0);

	if(seq&1)
	  _analysis_output("envelope2R",seq/2,work2,n,1,0,0);
	else
	  _analysis_output("envelope2L",seq/2,work2,n,1,0,0);
	seq++;
  }
#endif

  for(i=0;i<n;i++){
	int dB=logmask[i]+.5;
	if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
	if(dB<0)dB=0;
	logmask[i]= work[i]+p->vi->noisecompand[dB];
  }

}

void _vp_tonemask(vorbis_look_psy *p,
		  float *logfft,
		  float *logmask,
		  float global_specmax,
		  float local_specmax){

  int i,n=p->n;

  float *seed=(float*) alloca(sizeof(*seed)*p->total_octave_lines);
  float att=local_specmax+p->vi->ath_adjatt;
  for(i=0;i<p->total_octave_lines;i++)seed[i]=NEGINF;

  if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;

  for(i=0;i<n;i++)
	logmask[i]=p->ath[i]+att;

  seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax);
  max_seeds(p,seed,logmask);

}

void _vp_offset_and_mix(vorbis_look_psy *p,
			float *noise,
			float *tone,
			int offset_select,
			float *logmask,
			float *mdct,
			float *logmdct){
  int i,n=p->n;
  float de, coeffi, cx;/* AoTuV */
  float toneatt=p->vi->tone_masteratt[offset_select];

  cx = p->m_val;

  for(i=0;i<n;i++){
	float val= noise[i]+p->noiseoffset[offset_select][i];
	if(val>p->vi->noisemaxsupp)val=p->vi->noisemaxsupp;
	logmask[i]=max(val,tone[i]+toneatt);

	if(offset_select == 1) {
	  coeffi = -17.2;	   /* coeffi is a -17.2dB threshold */
	  val = val - logmdct[i];  /* val == mdct line value relative to floor in dB */

	  if(val > coeffi){

	de = 1.0-((val-coeffi)*0.005*cx);

	if(de < 0) de = 0.0001;
	  }else

	de = 1.0-((val-coeffi)*0.0003*cx);

	  mdct[i] *= de;

	}
  }
}

float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){
  vorbis_info *vi=vd->vi;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy_global *gi=&ci->psy_g_param;

  int n=ci->blocksizes[vd->W]/2;
  float secs=(float)n/vi->rate;

  amp+=secs*gi->ampmax_att_per_sec;
  if(amp<-9999)amp=-9999;
  return(amp);
}

static void couple_lossless(float A, float B,
				float *qA, float *qB){
  int test1=fabs(*qA)>fabs(*qB);
  test1-= fabs(*qA)<fabs(*qB);

  if(!test1)test1=((fabs(A)>fabs(B))<<1)-1;
  if(test1==1){
	*qB=(*qA>0.f?*qA-*qB:*qB-*qA);
  }else{
	float temp=*qB;
	*qB=(*qB>0.f?*qA-*qB:*qB-*qA);
	*qA=temp;
  }

  if(*qB>fabs(*qA)*1.9999f){
	*qB= -fabs(*qA)*2.f;
	*qA= -*qA;
  }
}

static float hypot_lookup[32]={
  -0.009935, -0.011245, -0.012726, -0.014397,
  -0.016282, -0.018407, -0.020800, -0.023494,
  -0.026522, -0.029923, -0.033737, -0.038010,
  -0.042787, -0.048121, -0.054064, -0.060671,
  -0.068000, -0.076109, -0.085054, -0.094892,
  -0.105675, -0.117451, -0.130260, -0.144134,
  -0.159093, -0.175146, -0.192286, -0.210490,
  -0.229718, -0.249913, -0.271001, -0.292893};

static void precomputed_couple_point(float premag,
					 int floorA,int floorB,
					 float *mag, float *ang){

  int test=(floorA>floorB)-1;
  int offset=31-abs(floorA-floorB);
  float floormag=hypot_lookup[((offset<0)-1)&offset]+1.f;

  floormag*=FLOOR1_fromdB_INV_LOOKUP[(floorB&test)|(floorA&(~test))];

  *mag=premag*floormag;
  *ang=0.f;
}

static float dipole_hypot(float a, float b){
  if(a>0.){
	if(b>0.)return sqrt(a*a+b*b);
	if(a>-b)return sqrt(a*a-b*b);
	return -sqrt(b*b-a*a);
  }
  if(b<0.)return -sqrt(a*a+b*b);
  if(-a>b)return -sqrt(a*a-b*b);
  return sqrt(b*b-a*a);
}
static float round_hypot(float a, float b){
  if(a>0.){
	if(b>0.)return sqrt(a*a+b*b);
	if(a>-b)return sqrt(a*a+b*b);
	return -sqrt(b*b+a*a);
  }
  if(b<0.)return -sqrt(a*a+b*b);
  if(-a>b)return -sqrt(a*a+b*b);
  return sqrt(b*b+a*a);
}

float **_vp_quantize_couple_memo(vorbis_block *vb,
				 vorbis_info_psy_global *g,
				 vorbis_look_psy *p,
				 vorbis_info_mapping0 *vi,
				 float **mdct){

  int i,j,n=p->n;
  float **ret=(float**) _vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret));
  int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2];

  for(i=0;i<vi->coupling_steps;i++){
	float *mdctM=mdct[vi->coupling_mag[i]];
	float *mdctA=mdct[vi->coupling_ang[i]];
	ret[i]=(float*) _vorbis_block_alloc(vb,n*sizeof(**ret));
	for(j=0;j<limit;j++)
	  ret[i][j]=dipole_hypot(mdctM[j],mdctA[j]);
	for(;j<n;j++)
	  ret[i][j]=round_hypot(mdctM[j],mdctA[j]);
  }

  return(ret);
}

static int apsort(const void *a, const void *b){
  float f1=fabs(**(float**)a);
  float f2=fabs(**(float**)b);
  return (f1<f2)-(f1>f2);
}

int **_vp_quantize_couple_sort(vorbis_block *vb,
				   vorbis_look_psy *p,
				   vorbis_info_mapping0 *vi,
				   float **mags){

  if(p->vi->normal_point_p){
	int i,j,k,n=p->n;
	int **ret=(int**) _vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret));
	int partition=p->vi->normal_partition;
	float **work=(float**) alloca(sizeof(*work)*partition);

	for(i=0;i<vi->coupling_steps;i++){
	  ret[i]=(int*) _vorbis_block_alloc(vb,n*sizeof(**ret));

	  for(j=0;j<n;j+=partition){
	for(k=0;k<partition;k++)work[k]=mags[i]+k+j;
	qsort(work,partition,sizeof(*work),apsort);
	for(k=0;k<partition;k++)ret[i][k+j]=work[k]-mags[i];
	  }
	}
	return(ret);
  }
  return(NULL);
}

void _vp_noise_normalize_sort(vorbis_look_psy *p,
				  float *magnitudes,int *sortedindex){
  int i,j,n=p->n;
  vorbis_info_psy *vi=p->vi;
  int partition=vi->normal_partition;
  float **work=(float**) alloca(sizeof(*work)*partition);
  int start=vi->normal_start;

  for(j=start;j<n;j+=partition){
	if(j+partition>n)partition=n-j;
	for(i=0;i<partition;i++)work[i]=magnitudes+i+j;
	qsort(work,partition,sizeof(*work),apsort);
	for(i=0;i<partition;i++){
	  sortedindex[i+j-start]=work[i]-magnitudes;
	}
  }
}

void _vp_noise_normalize(vorbis_look_psy *p,
			 float *in,float *out,int *sortedindex){
  int flag=0,i,j=0,n=p->n;
  vorbis_info_psy *vi=p->vi;
  int partition=vi->normal_partition;
  int start=vi->normal_start;

  if(start>n)start=n;

  if(vi->normal_channel_p){
	for(;j<start;j++)
	  out[j]=rint(in[j]);

	for(;j+partition<=n;j+=partition){
	  float acc=0.;
	  int k;

	  for(i=j;i<j+partition;i++)
	acc+=in[i]*in[i];

	  for(i=0;i<partition;i++){
	k=sortedindex[i+j-start];

	if(in[k]*in[k]>=.25f){
	  out[k]=rint(in[k]);
	  acc-=in[k]*in[k];
	  flag=1;
	}else{
	  if(acc<vi->normal_thresh)break;
	  out[k]=unitnorm(in[k]);
	  acc-=1.;
	}
	  }

	  for(;i<partition;i++){
	k=sortedindex[i+j-start];
	out[k]=0.;
	  }
	}
  }

  for(;j<n;j++)
	out[j]=rint(in[j]);

}

void _vp_couple(int blobno,
		vorbis_info_psy_global *g,
		vorbis_look_psy *p,
		vorbis_info_mapping0 *vi,
		float **res,
		float **mag_memo,
		int   **mag_sort,
		int   **ifloor,
		int   *nonzero,
		int  sliding_lowpass){

  int i,j,k,n=p->n;

  for(i=0;i<vi->coupling_steps;i++){

	if(nonzero[vi->coupling_mag[i]] ||
	   nonzero[vi->coupling_ang[i]]){

	  float *rM=res[vi->coupling_mag[i]];
	  float *rA=res[vi->coupling_ang[i]];
	  float *qM=rM+n;
	  float *qA=rA+n;
	  int *floorM=ifloor[vi->coupling_mag[i]];
	  int *floorA=ifloor[vi->coupling_ang[i]];
	  float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]];
	  float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]];
	  int partition=(p->vi->normal_point_p?p->vi->normal_partition:p->n);
	  int limit=g->coupling_pointlimit[p->vi->blockflag][blobno];
	  int pointlimit=limit;

	  nonzero[vi->coupling_mag[i]]=1;
	  nonzero[vi->coupling_ang[i]]=1;

	   if(n > 1000)
		 postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]];

	  for(j=0;j<p->n;j+=partition){
	float acc=0.f;

	for(k=0;k<partition;k++){
	  int l=k+j;

	  if(l<sliding_lowpass){
		if((l>=limit && fabs(rM[l])<postpoint && fabs(rA[l])<postpoint) ||
		   (fabs(rM[l])<prepoint && fabs(rA[l])<prepoint)){

		  precomputed_couple_point(mag_memo[i][l],
					   floorM[l],floorA[l],
					   qM+l,qA+l);

		  if(rint(qM[l])==0.f)acc+=qM[l]*qM[l];
		}else{
		  couple_lossless(rM[l],rA[l],qM+l,qA+l);
		}
	  }else{
		qM[l]=0.;
		qA[l]=0.;
	  }
	}

	if(p->vi->normal_point_p){
	  for(k=0;k<partition && acc>=p->vi->normal_thresh;k++){
		int l=mag_sort[i][j+k];
		if(l<sliding_lowpass && l>=pointlimit && rint(qM[l])==0.f){
		  qM[l]=unitnorm(qM[l]);
		  acc-=1.f;
		}
	  }
	}
	  }
	}
  }
}

void hf_reduction(vorbis_info_psy_global *g,
					  vorbis_look_psy *p,
					  vorbis_info_mapping0 *vi,
					  float **mdct){

  int i,j,n=p->n, de=0.3*p->m_val;
  int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2];

  for(i=0; i<vi->coupling_steps; i++){
	for(j=limit; j<n; j++)
	  mdct[i][j] *= (1.0 - de*((float)(j-limit) / (float)(n-limit)));
  }
}

#endif
/*** End of inlined file: psy.c ***/


/*** Start of inlined file: registry.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

extern vorbis_func_floor	 floor0_exportbundle;
extern vorbis_func_floor	 floor1_exportbundle;
extern vorbis_func_residue   residue0_exportbundle;
extern vorbis_func_residue   residue1_exportbundle;
extern vorbis_func_residue   residue2_exportbundle;
extern vorbis_func_mapping   mapping0_exportbundle;

vorbis_func_floor	 *_floor_P[]={
  &floor0_exportbundle,
  &floor1_exportbundle,
};

vorbis_func_residue   *_residue_P[]={
  &residue0_exportbundle,
  &residue1_exportbundle,
  &residue2_exportbundle,
};

vorbis_func_mapping   *_mapping_P[]={
  &mapping0_exportbundle,
};

#endif
/*** End of inlined file: registry.c ***/


/*** Start of inlined file: res0.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <math.h>

#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
#include <stdio.h>
#endif

typedef struct {
  vorbis_info_residue0 *info;

  int	 parts;
  int	 stages;
  codebook   *fullbooks;
  codebook   *phrasebook;
  codebook ***partbooks;

  int	 partvals;
  int	   **decodemap;

  long	  postbits;
  long	  phrasebits;
  long	  frames;

#if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
  int	train_seq;
  long	  *training_data[8][64];
  float	  training_max[8][64];
  float	  training_min[8][64];
  float	 tmin;
  float	 tmax;
#endif

} vorbis_look_residue0;

void res0_free_info(vorbis_info_residue *i){
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
  if(info){
	memset(info,0,sizeof(*info));
	_ogg_free(info);
  }
}

void res0_free_look(vorbis_look_residue *i){
  int j;
  if(i){

	vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;

#ifdef TRAIN_RES
	{
	  int j,k,l;
	  for(j=0;j<look->parts;j++){
	for(k=0;k<8;k++)
	  if(look->training_data[k][j]){
		char buffer[80];
		FILE *of;
		codebook *statebook=look->partbooks[j][k];

	    sprintf(buffer,"res_part%d_pass%d.vqd",j,k);
	    of=fopen(buffer,"a");

		for(l=0;l<statebook->entries;l++)
	      fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);

		fclose(of);

		_ogg_free(look->training_data[k][j]);
		look->training_data[k][j]=NULL;
	  }
	  }
	}
	fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);

#endif

	for(j=0;j<look->parts;j++)
	  if(look->partbooks[j])_ogg_free(look->partbooks[j]);
	_ogg_free(look->partbooks);
	for(j=0;j<look->partvals;j++)
	  _ogg_free(look->decodemap[j]);
	_ogg_free(look->decodemap);

	memset(look,0,sizeof(*look));
	_ogg_free(look);
  }
}

static int icount(unsigned int v){
  int ret=0;
  while(v){
	ret+=v&1;
	v>>=1;
  }
  return(ret);
}

void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  int j,acc=0;
  oggpack_write(opb,info->begin,24);
  oggpack_write(opb,info->end,24);

  oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and
						 code with a partitioned book */
  oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
  oggpack_write(opb,info->groupbook,8);  /* group huffman book */

  for(j=0;j<info->partitions;j++){
	if(ilog(info->secondstages[j])>3){
	  oggpack_write(opb,info->secondstages[j],3);
	  oggpack_write(opb,1,1);
	  oggpack_write(opb,info->secondstages[j]>>3,5);
	}else
	  oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
	acc+=icount(info->secondstages[j]);
  }
  for(j=0;j<acc;j++)
	oggpack_write(opb,info->booklist[j],8);

}

vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
  int j,acc=0;
  vorbis_info_residue0 *info=(vorbis_info_residue0*) _ogg_calloc(1,sizeof(*info));
  codec_setup_info	 *ci=(codec_setup_info*) vi->codec_setup;

  info->begin=oggpack_read(opb,24);
  info->end=oggpack_read(opb,24);
  info->grouping=oggpack_read(opb,24)+1;
  info->partitions=oggpack_read(opb,6)+1;
  info->groupbook=oggpack_read(opb,8);

  for(j=0;j<info->partitions;j++){
	int cascade=oggpack_read(opb,3);
	if(oggpack_read(opb,1))
	  cascade|=(oggpack_read(opb,5)<<3);
	info->secondstages[j]=cascade;

	acc+=icount(cascade);
  }
  for(j=0;j<acc;j++)
	info->booklist[j]=oggpack_read(opb,8);

  if(info->groupbook>=ci->books)goto errout;
  for(j=0;j<acc;j++)
	if(info->booklist[j]>=ci->books)goto errout;

  return(info);
 errout:
  res0_free_info(info);
  return(NULL);
}

vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
				   vorbis_info_residue *vr){
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)_ogg_calloc(1,sizeof(*look));
  codec_setup_info	 *ci=(codec_setup_info*)vd->vi->codec_setup;

  int j,k,acc=0;
  int dim;
  int maxstage=0;
  look->info=info;

  look->parts=info->partitions;
  look->fullbooks=ci->fullbooks;
  look->phrasebook=ci->fullbooks+info->groupbook;
  dim=look->phrasebook->dim;

  look->partbooks=(codebook***)_ogg_calloc(look->parts,sizeof(*look->partbooks));

  for(j=0;j<look->parts;j++){
	int stages=ilog(info->secondstages[j]);
	if(stages){
	  if(stages>maxstage)maxstage=stages;
	  look->partbooks[j]=(codebook**) _ogg_calloc(stages,sizeof(*look->partbooks[j]));
	  for(k=0;k<stages;k++)
	if(info->secondstages[j]&(1<<k)){
	  look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
#ifdef TRAIN_RES
	  look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
					   sizeof(***look->training_data));
#endif
	}
	}
  }

  look->partvals=rint(pow((float)look->parts,(float)dim));
  look->stages=maxstage;
  look->decodemap=(int**)_ogg_malloc(look->partvals*sizeof(*look->decodemap));
  for(j=0;j<look->partvals;j++){
	long val=j;
	long mult=look->partvals/look->parts;
	look->decodemap[j]=(int*)_ogg_malloc(dim*sizeof(*look->decodemap[j]));
	for(k=0;k<dim;k++){
	  long deco=val/mult;
	  val-=deco*mult;
	  mult/=look->parts;
	  look->decodemap[j][k]=deco;
	}
  }
#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
  {
	static int train_seq=0;
	look->train_seq=train_seq++;
  }
#endif
  return(look);
}

static int local_book_besterror(codebook *book,float *a){
  int dim=book->dim,i,k,o;
  int best=0;
  encode_aux_threshmatch *tt=book->c->thresh_tree;

  for(k=0,o=dim;k<dim;++k){
	float val=a[--o];
	i=tt->threshvals>>1;

	if(val<tt->quantthresh[i]){
	  if(val<tt->quantthresh[i-1]){
	for(--i;i>0;--i)
	  if(val>=tt->quantthresh[i-1])
		break;
	  }
	}else{

	  for(++i;i<tt->threshvals-1;++i)
	if(val<tt->quantthresh[i])break;

	}

	best=(best*tt->quantvals)+tt->quantmap[i];
  }

  if(book->c->lengthlist[best]<=0){
	const static_codebook *c=book->c;
	int i,j;
	float bestf=0.f;
	float *e=book->valuelist;
	best=-1;
	for(i=0;i<book->entries;i++){
	  if(c->lengthlist[i]>0){
	float thisx=0.f;
	for(j=0;j<dim;j++){
	  float val=(e[j]-a[j]);
	  thisx+=val*val;
	}
	if(best==-1 || thisx<bestf){
	  bestf=thisx;
	  best=i;
	}
	  }
	  e+=dim;
	}
  }

  {
	float *ptr=book->valuelist+best*dim;
	for(i=0;i<dim;i++)
	  *a++ -= *ptr++;
  }

  return(best);
}

static int _encodepart(oggpack_buffer *opb,float *vec, int n,
			   codebook *book,long *acc){
  int i,bits=0;
  int dim=book->dim;
  int step=n/dim;

  for(i=0;i<step;i++){
	int entry=local_book_besterror(book,vec+i*dim);

#ifdef TRAIN_RES
	acc[entry]++;
#endif

	bits+=vorbis_book_encode(book,entry,opb);
  }

  return(bits);
}

static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
			   float **in,int ch){
  long i,j,k;
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  int samples_per_partition=info->grouping;
  int possible_partitions=info->partitions;
  int n=info->end-info->begin;

  int partvals=n/samples_per_partition;
  long **partword=(long**)_vorbis_block_alloc(vb,ch*sizeof(*partword));
  float scale=100./samples_per_partition;

  for(i=0;i<ch;i++){
	partword[i]=(long*)_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
	memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
  }

  for(i=0;i<partvals;i++){
	int offset=i*samples_per_partition+info->begin;
	for(j=0;j<ch;j++){
	  float max=0.;
	  float ent=0.;
	  for(k=0;k<samples_per_partition;k++){
	if(fabs(in[j][offset+k])>max)max=fabs(in[j][offset+k]);
	ent+=fabs(rint(in[j][offset+k]));
	  }
	  ent*=scale;

	  for(k=0;k<possible_partitions-1;k++)
	if(max<=info->classmetric1[k] &&
	   (info->classmetric2[k]<0 || (int)ent<info->classmetric2[k]))
	  break;

	  partword[j][i]=k;
	}
  }

#ifdef TRAIN_RESAUX
  {
	FILE *of;
	char buffer[80];

	for(i=0;i<ch;i++){
	  sprintf(buffer,"resaux_%d.vqd",look->train_seq);
	  of=fopen(buffer,"a");
	  for(j=0;j<partvals;j++)
	fprintf(of,"%ld, ",partword[i][j]);
	  fprintf(of,"\n");
	  fclose(of);
	}
  }
#endif
  look->frames++;

  return(partword);
}

static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in,
			  int ch){
  long i,j,k,l;
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  int samples_per_partition=info->grouping;
  int possible_partitions=info->partitions;
  int n=info->end-info->begin;

  int partvals=n/samples_per_partition;
  long **partword=(long**)_vorbis_block_alloc(vb,sizeof(*partword));

#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
  FILE *of;
  char buffer[80];
#endif

  partword[0]=(long*)_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
  memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));

  for(i=0,l=info->begin/ch;i<partvals;i++){
	float magmax=0.f;
	float angmax=0.f;
	for(j=0;j<samples_per_partition;j+=ch){
	  if(fabs(in[0][l])>magmax)magmax=fabs(in[0][l]);
	  for(k=1;k<ch;k++)
	if(fabs(in[k][l])>angmax)angmax=fabs(in[k][l]);
	  l++;
	}

	for(j=0;j<possible_partitions-1;j++)
	  if(magmax<=info->classmetric1[j] &&
	 angmax<=info->classmetric2[j])
	break;

	partword[0][i]=j;

  }

#ifdef TRAIN_RESAUX
  sprintf(buffer,"resaux_%d.vqd",look->train_seq);
  of=fopen(buffer,"a");
  for(i=0;i<partvals;i++)
	fprintf(of,"%ld, ",partword[0][i]);
  fprintf(of,"\n");
  fclose(of);
#endif

  look->frames++;

  return(partword);
}

static int _01forward(oggpack_buffer *opb,
			  vorbis_block *vb,vorbis_look_residue *vl,
			  float **in,int ch,
			  long **partword,
			  int (*encode)(oggpack_buffer *,float *,int,
					codebook *,long *)){
  long i,j,k,s;
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  int samples_per_partition=info->grouping;
  int possible_partitions=info->partitions;
  int partitions_per_word=look->phrasebook->dim;
  int n=info->end-info->begin;

  int partvals=n/samples_per_partition;
  long resbits[128];
  long resvals[128];

#ifdef TRAIN_RES
  for(i=0;i<ch;i++)
	for(j=info->begin;j<info->end;j++){
	  if(in[i][j]>look->tmax)look->tmax=in[i][j];
	  if(in[i][j]<look->tmin)look->tmin=in[i][j];
	}
#endif

  memset(resbits,0,sizeof(resbits));
  memset(resvals,0,sizeof(resvals));

  for(s=0;s<look->stages;s++){

	for(i=0;i<partvals;){

	  if(s==0){
	for(j=0;j<ch;j++){
	  long val=partword[j][i];
	  for(k=1;k<partitions_per_word;k++){
		val*=possible_partitions;
		if(i+k<partvals)
		  val+=partword[j][i+k];
	  }

	  if(val<look->phrasebook->entries)
		look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
#if 0 /*def TRAIN_RES*/
	  else
	    fprintf(stderr,"!");
#endif

	}
	  }

	  for(k=0;k<partitions_per_word && i<partvals;k++,i++){
	long offset=i*samples_per_partition+info->begin;

	for(j=0;j<ch;j++){
	  if(s==0)resvals[partword[j][i]]+=samples_per_partition;
	  if(info->secondstages[partword[j][i]]&(1<<s)){
		codebook *statebook=look->partbooks[partword[j][i]][s];
		if(statebook){
		  int ret;
		  long *accumulator=NULL;

#ifdef TRAIN_RES
		  accumulator=look->training_data[s][partword[j][i]];
		  {
		int l;
		float *samples=in[j]+offset;
		for(l=0;l<samples_per_partition;l++){
		  if(samples[l]<look->training_min[s][partword[j][i]])
			look->training_min[s][partword[j][i]]=samples[l];
		  if(samples[l]>look->training_max[s][partword[j][i]])
			look->training_max[s][partword[j][i]]=samples[l];
		}
		  }
#endif

		  ret=encode(opb,in[j]+offset,samples_per_partition,
			 statebook,accumulator);

		  look->postbits+=ret;
		  resbits[partword[j][i]]+=ret;
		}
	  }
	}
	  }
	}
  }

  return(0);
}

static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
			  float **in,int ch,
			  long (*decodepart)(codebook *, float *,
					 oggpack_buffer *,int)){

  long i,j,k,l,s;
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  int samples_per_partition=info->grouping;
  int partitions_per_word=look->phrasebook->dim;
  int n=info->end-info->begin;

  int partvals=n/samples_per_partition;
  int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  int ***partword=(int***)alloca(ch*sizeof(*partword));

  for(j=0;j<ch;j++)
	partword[j]=(int**)_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));

  for(s=0;s<look->stages;s++){

	for(i=0,l=0;i<partvals;l++){
	  if(s==0){
	for(j=0;j<ch;j++){
	  int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
	  if(temp==-1)goto eopbreak;
	  partword[j][l]=look->decodemap[temp];
	  if(partword[j][l]==NULL)goto errout;
	}
	  }

	  for(k=0;k<partitions_per_word && i<partvals;k++,i++)
	for(j=0;j<ch;j++){
	  long offset=info->begin+i*samples_per_partition;
	  if(info->secondstages[partword[j][l][k]]&(1<<s)){
		codebook *stagebook=look->partbooks[partword[j][l][k]][s];
		if(stagebook){
		  if(decodepart(stagebook,in[j]+offset,&vb->opb,
				samples_per_partition)==-1)goto eopbreak;
		}
	  }
	}
	}
  }

 errout:
 eopbreak:
  return(0);
}

#if 0
long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
		  float **in,int *nonzero,int ch){
  int i,used=0;
  for(i=0;i<ch;i++)
	if(nonzero[i])
	  in[used++]=in[i];
  if(used)
	return(_01class(vb,vl,in,used));
  else
	return(0);
}

int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
		 float **in,float **out,int *nonzero,int ch,
		 long **partword){
  int i,j,used=0,n=vb->pcmend/2;
  for(i=0;i<ch;i++)
	if(nonzero[i]){
	  if(out)
	for(j=0;j<n;j++)
	  out[i][j]+=in[i][j];
	  in[used++]=in[i];
	}
  if(used){
	int ret=_01forward(vb,vl,in,used,partword,
			  _interleaved_encodepart);
	if(out){
	  used=0;
	  for(i=0;i<ch;i++)
	if(nonzero[i]){
	  for(j=0;j<n;j++)
		out[i][j]-=in[used][j];
	  used++;
	}
	}
	return(ret);
  }else{
	return(0);
  }
}
#endif

int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
		 float **in,int *nonzero,int ch){
  int i,used=0;
  for(i=0;i<ch;i++)
	if(nonzero[i])
	  in[used++]=in[i];
  if(used)
	return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
  else
	return(0);
}

int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
		 float **in,float **out,int *nonzero,int ch,
		 long **partword){
  int i,j,used=0,n=vb->pcmend/2;
  for(i=0;i<ch;i++)
	if(nonzero[i]){
	  if(out)
	for(j=0;j<n;j++)
	  out[i][j]+=in[i][j];
	  in[used++]=in[i];
	}

  if(used){
	int ret=_01forward(opb,vb,vl,in,used,partword,_encodepart);
	if(out){
	  used=0;
	  for(i=0;i<ch;i++)
	if(nonzero[i]){
	  for(j=0;j<n;j++)
		out[i][j]-=in[used][j];
	  used++;
	}
	}
	return(ret);
  }else{
	return(0);
  }
}

long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
		  float **in,int *nonzero,int ch){
  int i,used=0;
  for(i=0;i<ch;i++)
	if(nonzero[i])
	  in[used++]=in[i];
  if(used)
	return(_01class(vb,vl,in,used));
  else
	return(0);
}

int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
		 float **in,int *nonzero,int ch){
  int i,used=0;
  for(i=0;i<ch;i++)
	if(nonzero[i])
	  in[used++]=in[i];
  if(used)
	return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
  else
	return(0);
}

long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
		  float **in,int *nonzero,int ch){
  int i,used=0;
  for(i=0;i<ch;i++)
	if(nonzero[i])used++;
  if(used)
	return(_2class(vb,vl,in,ch));
  else
	return(0);
}

int res2_forward(oggpack_buffer *opb,
		 vorbis_block *vb,vorbis_look_residue *vl,
		 float **in,float **out,int *nonzero,int ch,
		 long **partword){
  long i,j,k,n=vb->pcmend/2,used=0;

  float *work=(float*)_vorbis_block_alloc(vb,ch*n*sizeof(*work));
  for(i=0;i<ch;i++){
	float *pcm=in[i];
	if(nonzero[i])used++;
	for(j=0,k=i;j<n;j++,k+=ch)
	  work[k]=pcm[j];
  }

  if(used){
	int ret=_01forward(opb,vb,vl,&work,1,partword,_encodepart);
	if(out){
	  for(i=0;i<ch;i++){
	float *pcm=in[i];
	float *sofar=out[i];
	for(j=0,k=i;j<n;j++,k+=ch)
	  sofar[j]+=pcm[j]-work[k];

	  }
	}
	return(ret);
  }else{
	return(0);
  }
}

int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
		 float **in,int *nonzero,int ch){
  long i,k,l,s;
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  int samples_per_partition=info->grouping;
  int partitions_per_word=look->phrasebook->dim;
  int n=info->end-info->begin;

  int partvals=n/samples_per_partition;
  int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  int **partword=(int**)_vorbis_block_alloc(vb,partwords*sizeof(*partword));

  for(i=0;i<ch;i++)if(nonzero[i])break;
  if(i==ch)return(0); /* no nonzero vectors */

  for(s=0;s<look->stages;s++){
	for(i=0,l=0;i<partvals;l++){

	  if(s==0){
	int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
	if(temp==-1)goto eopbreak;
	partword[l]=look->decodemap[temp];
	if(partword[l]==NULL)goto errout;
	  }

	  for(k=0;k<partitions_per_word && i<partvals;k++,i++)
	if(info->secondstages[partword[l][k]]&(1<<s)){
	  codebook *stagebook=look->partbooks[partword[l][k]][s];

	  if(stagebook){
		if(vorbis_book_decodevv_add(stagebook,in,
					i*samples_per_partition+info->begin,ch,
					&vb->opb,samples_per_partition)==-1)
		  goto eopbreak;
	  }
	}
	}
  }

 errout:
 eopbreak:
  return(0);
}

vorbis_func_residue residue0_exportbundle={
  NULL,
  &res0_unpack,
  &res0_look,
  &res0_free_info,
  &res0_free_look,
  NULL,
  NULL,
  &res0_inverse
};

vorbis_func_residue residue1_exportbundle={
  &res0_pack,
  &res0_unpack,
  &res0_look,
  &res0_free_info,
  &res0_free_look,
  &res1_class,
  &res1_forward,
  &res1_inverse
};

vorbis_func_residue residue2_exportbundle={
  &res0_pack,
  &res0_unpack,
  &res0_look,
  &res0_free_info,
  &res0_free_look,
  &res2_class,
  &res2_forward,
  &res2_inverse
};

#endif
/*** End of inlined file: res0.c ***/


/*** Start of inlined file: sharedbook.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <math.h>
#include <string.h>

int _ilog(unsigned int v){
  int ret=0;
  while(v){
	ret++;
	v>>=1;
  }
  return(ret);
}

#define VQ_FEXP 10
#define VQ_FMAN 21
#define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */

long _float32_pack(float val){
  int sign=0;
  long exp;
  long mant;
  if(val<0){
	sign=0x80000000;
	val= -val;
  }
  exp= floor(log(val)/log(2.f));
  mant=rint(ldexp(val,(VQ_FMAN-1)-exp));
  exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN;

  return(sign|exp|mant);
}

float _float32_unpack(long val){
  double mant=val&0x1fffff;
  int	sign=val&0x80000000;
  long   exp =(val&0x7fe00000L)>>VQ_FMAN;
  if(sign)mant= -mant;
  return(ldexp(mant,exp-(VQ_FMAN-1)-VQ_FEXP_BIAS));
}

ogg_uint32_t *_make_words(long *l,long n,long sparsecount){
  long i,j,count=0;
  ogg_uint32_t marker[33];
  ogg_uint32_t *r=(ogg_uint32_t*)_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r));
  memset(marker,0,sizeof(marker));

  for(i=0;i<n;i++){
	long length=l[i];
	if(length>0){
	  ogg_uint32_t entry=marker[length];

	  if(length<32 && (entry>>length)){
	_ogg_free(r);
	return(NULL);
	  }
	  r[count++]=entry;

	  {
	for(j=length;j>0;j--){

	  if(marker[j]&1){
		if(j==1)
		  marker[1]++;
		else
		  marker[j]=marker[j-1]<<1;
		break; /* invariant says next upper marker would already
			  have been moved if it was on the same path */
	  }
	  marker[j]++;
	}
	  }

	  for(j=length+1;j<33;j++)
	if((marker[j]>>1) == entry){
	  entry=marker[j];
	  marker[j]=marker[j-1]<<1;
	}else
	  break;
	}else
	  if(sparsecount==0)count++;
  }

  for(i=0,count=0;i<n;i++){
	ogg_uint32_t temp=0;
	for(j=0;j<l[i];j++){
	  temp<<=1;
	  temp|=(r[count]>>j)&1;
	}

	if(sparsecount){
	  if(l[i])
	r[count++]=temp;
	}else
	  r[count++]=temp;
  }

  return(r);
}

long _book_maptype1_quantvals(const static_codebook *b){
  long vals=floor(pow((float)b->entries,1.f/b->dim));

  while(1){
	long acc=1;
	long acc1=1;
	int i;
	for(i=0;i<b->dim;i++){
	  acc*=vals;
	  acc1*=vals+1;
	}
	if(acc<=b->entries && acc1>b->entries){
	  return(vals);
	}else{
	  if(acc>b->entries){
	vals--;
	  }else{
	vals++;
	  }
	}
  }
}

float *_book_unquantize(const static_codebook *b,int n,int *sparsemap){
  long j,k,count=0;
  if(b->maptype==1 || b->maptype==2){
	int quantvals;
	float mindel=_float32_unpack(b->q_min);
	float delta=_float32_unpack(b->q_delta);
	float *r=(float*)_ogg_calloc(n*b->dim,sizeof(*r));

	switch(b->maptype){
	case 1:
	  quantvals=_book_maptype1_quantvals(b);
	  for(j=0;j<b->entries;j++){
	if((sparsemap && b->lengthlist[j]) || !sparsemap){
	  float last=0.f;
	  int indexdiv=1;
	  for(k=0;k<b->dim;k++){
		int index= (j/indexdiv)%quantvals;
		float val=b->quantlist[index];
		val=fabs(val)*delta+mindel+last;
		if(b->q_sequencep)last=val;
		if(sparsemap)
		  r[sparsemap[count]*b->dim+k]=val;
		else
		  r[count*b->dim+k]=val;
		indexdiv*=quantvals;
	  }
	  count++;
	}

	  }
	  break;
	case 2:
	  for(j=0;j<b->entries;j++){
	if((sparsemap && b->lengthlist[j]) || !sparsemap){
	  float last=0.f;

	  for(k=0;k<b->dim;k++){
		float val=b->quantlist[j*b->dim+k];
		val=fabs(val)*delta+mindel+last;
		if(b->q_sequencep)last=val;
		if(sparsemap)
		  r[sparsemap[count]*b->dim+k]=val;
		else
		  r[count*b->dim+k]=val;
	  }
	  count++;
	}
	  }
	  break;
	}

	return(r);
  }
  return(NULL);
}

void vorbis_staticbook_clear(static_codebook *b){
  if(b->allocedp){
	if(b->quantlist)_ogg_free(b->quantlist);
	if(b->lengthlist)_ogg_free(b->lengthlist);
	if(b->nearest_tree){
	  _ogg_free(b->nearest_tree->ptr0);
	  _ogg_free(b->nearest_tree->ptr1);
	  _ogg_free(b->nearest_tree->p);
	  _ogg_free(b->nearest_tree->q);
	  memset(b->nearest_tree,0,sizeof(*b->nearest_tree));
	  _ogg_free(b->nearest_tree);
	}
	if(b->thresh_tree){
	  _ogg_free(b->thresh_tree->quantthresh);
	  _ogg_free(b->thresh_tree->quantmap);
	  memset(b->thresh_tree,0,sizeof(*b->thresh_tree));
	  _ogg_free(b->thresh_tree);
	}

	memset(b,0,sizeof(*b));
  }
}

void vorbis_staticbook_destroy(static_codebook *b){
  if(b->allocedp){
	vorbis_staticbook_clear(b);
	_ogg_free(b);
  }
}

void vorbis_book_clear(codebook *b){
  if(b->valuelist)_ogg_free(b->valuelist);
  if(b->codelist)_ogg_free(b->codelist);

  if(b->dec_index)_ogg_free(b->dec_index);
  if(b->dec_codelengths)_ogg_free(b->dec_codelengths);
  if(b->dec_firsttable)_ogg_free(b->dec_firsttable);

  memset(b,0,sizeof(*b));
}

int vorbis_book_init_encode(codebook *c,const static_codebook *s){

  memset(c,0,sizeof(*c));
  c->c=s;
  c->entries=s->entries;
  c->used_entries=s->entries;
  c->dim=s->dim;
  c->codelist=_make_words(s->lengthlist,s->entries,0);
  c->valuelist=_book_unquantize(s,s->entries,NULL);

  return(0);
}

static int sort32a(const void *a,const void *b){
  return ( **(ogg_uint32_t **)a>**(ogg_uint32_t **)b)-
	( **(ogg_uint32_t **)a<**(ogg_uint32_t **)b);
}

int vorbis_book_init_decode(codebook *c,const static_codebook *s){
  int i,j,n=0,tabn;
  int *sortindex;
  memset(c,0,sizeof(*c));

  for(i=0;i<s->entries;i++)
	if(s->lengthlist[i]>0)
	  n++;

  c->entries=s->entries;
  c->used_entries=n;
  c->dim=s->dim;

  {
	ogg_uint32_t *codes=_make_words(s->lengthlist,s->entries,c->used_entries);
	ogg_uint32_t **codep=(ogg_uint32_t**)alloca(sizeof(*codep)*n);

	if(codes==NULL)goto err_out;

	for(i=0;i<n;i++){
	  codes[i]=ogg_bitreverse(codes[i]);
	  codep[i]=codes+i;
	}

	qsort(codep,n,sizeof(*codep),sort32a);

	sortindex=(int*)alloca(n*sizeof(*sortindex));
	c->codelist=(ogg_uint32_t*)_ogg_malloc(n*sizeof(*c->codelist));
	for(i=0;i<n;i++){
	  int position=codep[i]-codes;
	  sortindex[position]=i;
	}

	for(i=0;i<n;i++)
	  c->codelist[sortindex[i]]=codes[i];
	_ogg_free(codes);
  }

  c->valuelist=_book_unquantize(s,n,sortindex);
  c->dec_index=(int*)_ogg_malloc(n*sizeof(*c->dec_index));

  for(n=0,i=0;i<s->entries;i++)
	if(s->lengthlist[i]>0)
	  c->dec_index[sortindex[n++]]=i;

  c->dec_codelengths=(char*)_ogg_malloc(n*sizeof(*c->dec_codelengths));
  for(n=0,i=0;i<s->entries;i++)
	if(s->lengthlist[i]>0)
	  c->dec_codelengths[sortindex[n++]]=s->lengthlist[i];

  c->dec_firsttablen=_ilog(c->used_entries)-4; /* this is magic */
  if(c->dec_firsttablen<5)c->dec_firsttablen=5;
  if(c->dec_firsttablen>8)c->dec_firsttablen=8;

  tabn=1<<c->dec_firsttablen;
  c->dec_firsttable=(ogg_uint32_t*)_ogg_calloc(tabn,sizeof(*c->dec_firsttable));
  c->dec_maxlength=0;

  for(i=0;i<n;i++){
	if(c->dec_maxlength<c->dec_codelengths[i])
	  c->dec_maxlength=c->dec_codelengths[i];
	if(c->dec_codelengths[i]<=c->dec_firsttablen){
	  ogg_uint32_t orig=ogg_bitreverse(c->codelist[i]);
	  for(j=0;j<(1<<(c->dec_firsttablen-c->dec_codelengths[i]));j++)
	c->dec_firsttable[orig|(j<<c->dec_codelengths[i])]=i+1;
	}
  }

  {
	ogg_uint32_t mask=0xfffffffeUL<<(31-c->dec_firsttablen);
	long lo=0,hi=0;

	for(i=0;i<tabn;i++){
	  ogg_uint32_t word=i<<(32-c->dec_firsttablen);
	  if(c->dec_firsttable[ogg_bitreverse(word)]==0){
	while((lo+1)<n && c->codelist[lo+1]<=word)lo++;
	while(	hi<n && word>=(c->codelist[hi]&mask))hi++;

	{
	  unsigned long loval=lo;
	  unsigned long hival=n-hi;

	  if(loval>0x7fff)loval=0x7fff;
	  if(hival>0x7fff)hival=0x7fff;
	  c->dec_firsttable[ogg_bitreverse(word)]=
		0x80000000UL | (loval<<15) | hival;
	}
	  }
	}
  }

  return(0);
 err_out:
  vorbis_book_clear(c);
  return(-1);
}

static float _dist(int el,float *ref, float *b,int step){
  int i;
  float acc=0.f;
  for(i=0;i<el;i++){
	float val=(ref[i]-b[i*step]);
	acc+=val*val;
  }
  return(acc);
}

int _best(codebook *book, float *a, int step){
  encode_aux_threshmatch *tt=book->c->thresh_tree;

#if 0
  encode_aux_nearestmatch *nt=book->c->nearest_tree;
  encode_aux_pigeonhole *pt=book->c->pigeon_tree;
#endif
  int dim=book->dim;
  int k,o;

  if(tt){
	int index=0,i;
	for(k=0,o=step*(dim-1);k<dim;k++,o-=step){

	  i=tt->threshvals>>1;
	  if(a[o]<tt->quantthresh[i]){

	for(;i>0;i--)
	  if(a[o]>=tt->quantthresh[i-1])
		break;

	  }else{

	for(i++;i<tt->threshvals-1;i++)
	  if(a[o]<tt->quantthresh[i])break;

	  }

	  index=(index*tt->quantvals)+tt->quantmap[i];
	}
	if(book->c->lengthlist[index]>0) /* is this unused?  If so, we'll
					use a decision tree after all
					and fall through*/
	  return(index);
  }

#if 0
  if(pt){
	const static_codebook *c=book->c;
	int i,besti=-1;
	float best=0.f;
	int entry=0;

	if(c->q_sequencep){
	  int pv;
	  long mul=1;
	  float qlast=0;
	  for(k=0,o=0;k<dim;k++,o+=step){
	pv=(int)((a[o]-qlast-pt->min)/pt->del);
	if(pv<0 || pv>=pt->mapentries)break;
	entry+=pt->pigeonmap[pv]*mul;
	mul*=pt->quantvals;
	qlast+=pv*pt->del+pt->min;
	  }
	}else{
	  for(k=0,o=step*(dim-1);k<dim;k++,o-=step){
	int pv=(int)((a[o]-pt->min)/pt->del);
	if(pv<0 || pv>=pt->mapentries)break;
	entry=entry*pt->quantvals+pt->pigeonmap[pv];
	  }
	}

	if(k==dim && pt->fitlength[entry]){
	  long *list=pt->fitlist+pt->fitmap[entry];
	  for(i=0;i<pt->fitlength[entry];i++){
	float this=_dist(dim,book->valuelist+list[i]*dim,a,step);
	if(besti==-1 || this<best){
	  best=this;
	  besti=list[i];
	}
	  }

	  return(besti);
	}
  }

  if(nt){
	while(1){
	  float c=0.f;
	  float *p=book->valuelist+nt->p[ptr];
	  float *q=book->valuelist+nt->q[ptr];

	  for(k=0,o=0;k<dim;k++,o+=step)
	c+=(p[k]-q[k])*(a[o]-(p[k]+q[k])*.5);

	  if(c>0.f) /* in A */
	ptr= -nt->ptr0[ptr];
	  else	 /* in B */
	ptr= -nt->ptr1[ptr];
	  if(ptr<=0)break;
	}
	return(-ptr);
  }
#endif

  {
	const static_codebook *c=book->c;
	int i,besti=-1;
	float best=0.f;
	float *e=book->valuelist;
	for(i=0;i<book->entries;i++){
	  if(c->lengthlist[i]>0){
	float thisx=_dist(dim,e,a,step);
	if(besti==-1 || thisx<best){
	  best=thisx;
	  besti=i;
	}
	  }
	  e+=dim;
	}

	return(besti);
  }
}

long vorbis_book_codeword(codebook *book,int entry){
  if(book->c) /* only use with encode; decode optimizations are
				 allowed to break this */
	return book->codelist[entry];
  return -1;
}

long vorbis_book_codelen(codebook *book,int entry){
  if(book->c) /* only use with encode; decode optimizations are
				 allowed to break this */
	return book->c->lengthlist[entry];
  return -1;
}

#ifdef _V_SELFTEST

#include <stdio.h>

static long full_quantlist1[]={0,1,2,3,	4,5,6,7, 8,3,6,1};
static long partial_quantlist1[]={0,7,2};

static_codebook test1={
  4,16,
  NULL,
  0,
  0,0,0,0,
  NULL,
  NULL,NULL
};
static float *test1_result=NULL;

static_codebook test2={
  4,3,
  NULL,
  2,
  -533200896,1611661312,4,0,
  full_quantlist1,
  NULL,NULL
};
static float test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2};

static_codebook test3={
  4,3,
  NULL,
  2,
  -533200896,1611661312,4,1,
  full_quantlist1,
  NULL,NULL
};
static float test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6};

static_codebook test4={
  3,27,
  NULL,
  1,
  -533200896,1611661312,4,0,
  partial_quantlist1,
  NULL,NULL
};
static float test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3,
				  -3, 4,-3, 4, 4,-3, -1, 4,-3,
				  -3,-1,-3, 4,-1,-3, -1,-1,-3,
				  -3,-3, 4, 4,-3, 4, -1,-3, 4,
				  -3, 4, 4, 4, 4, 4, -1, 4, 4,
				  -3,-1, 4, 4,-1, 4, -1,-1, 4,
				  -3,-3,-1, 4,-3,-1, -1,-3,-1,
				  -3, 4,-1, 4, 4,-1, -1, 4,-1,
				  -3,-1,-1, 4,-1,-1, -1,-1,-1};

static_codebook test5={
  3,27,
  NULL,
  1,
  -533200896,1611661312,4,1,
  partial_quantlist1,
  NULL,NULL
};
static float test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7,
				  -3, 1,-2, 4, 8, 5, -1, 3, 0,
				  -3,-4,-7, 4, 3, 0, -1,-2,-5,
				  -3,-6,-2, 4, 1, 5, -1,-4, 0,
				  -3, 1, 5, 4, 8,12, -1, 3, 7,
				  -3,-4, 0, 4, 3, 7, -1,-2, 2,
				  -3,-6,-7, 4, 1, 0, -1,-4,-5,
				  -3, 1, 0, 4, 8, 7, -1, 3, 2,
				  -3,-4,-5, 4, 3, 2, -1,-2,-3};

void run_test(static_codebook *b,float *comp){
  float *out=_book_unquantize(b,b->entries,NULL);
  int i;

  if(comp){
	if(!out){
	  fprintf(stderr,"_book_unquantize incorrectly returned NULL\n");
	  exit(1);
	}

	for(i=0;i<b->entries*b->dim;i++)
	  if(fabs(out[i]-comp[i])>.0001){
	fprintf(stderr,"disagreement in unquantized and reference data:\n"
		"position %d, %g != %g\n",i,out[i],comp[i]);
	exit(1);
	  }

  }else{
	if(out){
	  fprintf(stderr,"_book_unquantize returned a value array: \n"
	      " correct result should have been NULL\n");
	  exit(1);
	}
  }
}

int main(){
  fprintf(stderr,"Dequant test 1... ");
  run_test(&test1,test1_result);
  fprintf(stderr,"OK\nDequant test 2... ");
  run_test(&test2,test2_result);
  fprintf(stderr,"OK\nDequant test 3... ");
  run_test(&test3,test3_result);
  fprintf(stderr,"OK\nDequant test 4... ");
  run_test(&test4,test4_result);
  fprintf(stderr,"OK\nDequant test 5... ");
  run_test(&test5,test5_result);
  fprintf(stderr,"OK\n\n");

  return(0);
}

#endif

#endif
/*** End of inlined file: sharedbook.c ***/


/*** Start of inlined file: smallft.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <math.h>

static void drfti1(int n, float *wa, int *ifac){
  static int ntryh[4] = { 4,2,3,5 };
  static float tpi = 6.28318530717958648f;
  float arg,argh,argld,fi;
  int ntry=0,i,j=-1;
  int k1, l1, l2, ib;
  int ld, ii, ip, is, nq, nr;
  int ido, ipm, nfm1;
  int nl=n;
  int nf=0;

 L101:
  j++;
  if (j < 4)
	ntry=ntryh[j];
  else
	ntry+=2;

 L104:
  nq=nl/ntry;
  nr=nl-ntry*nq;
  if (nr!=0) goto L101;

  nf++;
  ifac[nf+1]=ntry;
  nl=nq;
  if(ntry!=2)goto L107;
  if(nf==1)goto L107;

  for (i=1;i<nf;i++){
	ib=nf-i+1;
	ifac[ib+1]=ifac[ib];
  }
  ifac[2] = 2;

 L107:
  if(nl!=1)goto L104;
  ifac[0]=n;
  ifac[1]=nf;
  argh=tpi/n;
  is=0;
  nfm1=nf-1;
  l1=1;

  if(nfm1==0)return;

  for (k1=0;k1<nfm1;k1++){
	ip=ifac[k1+2];
	ld=0;
	l2=l1*ip;
	ido=n/l2;
	ipm=ip-1;

	for (j=0;j<ipm;j++){
	  ld+=l1;
	  i=is;
	  argld=(float)ld*argh;
	  fi=0.f;
	  for (ii=2;ii<ido;ii+=2){
	fi+=1.f;
	arg=fi*argld;
	wa[i++]=cos(arg);
	wa[i++]=sin(arg);
	  }
	  is+=ido;
	}
	l1=l2;
  }
}

static void fdrffti(int n, float *wsave, int *ifac){

  if (n == 1) return;
  drfti1(n, wsave+n, ifac);
}

static void dradf2(int ido,int l1,float *cc,float *ch,float *wa1){
  int i,k;
  float ti2,tr2;
  int t0,t1,t2,t3,t4,t5,t6;

  t1=0;
  t0=(t2=l1*ido);
  t3=ido<<1;
  for(k=0;k<l1;k++){
	ch[t1<<1]=cc[t1]+cc[t2];
	ch[(t1<<1)+t3-1]=cc[t1]-cc[t2];
	t1+=ido;
	t2+=ido;
  }

  if(ido<2)return;
  if(ido==2)goto L105;

  t1=0;
  t2=t0;
  for(k=0;k<l1;k++){
	t3=t2;
	t4=(t1<<1)+(ido<<1);
	t5=t1;
	t6=t1+t1;
	for(i=2;i<ido;i+=2){
	  t3+=2;
	  t4-=2;
	  t5+=2;
	  t6+=2;
	  tr2=wa1[i-2]*cc[t3-1]+wa1[i-1]*cc[t3];
	  ti2=wa1[i-2]*cc[t3]-wa1[i-1]*cc[t3-1];
	  ch[t6]=cc[t5]+ti2;
	  ch[t4]=ti2-cc[t5];
	  ch[t6-1]=cc[t5-1]+tr2;
	  ch[t4-1]=cc[t5-1]-tr2;
	}
	t1+=ido;
	t2+=ido;
  }

  if(ido%2==1)return;

 L105:
  t3=(t2=(t1=ido)-1);
  t2+=t0;
  for(k=0;k<l1;k++){
	ch[t1]=-cc[t2];
	ch[t1-1]=cc[t3];
	t1+=ido<<1;
	t2+=ido;
	t3+=ido;
  }
}

static void dradf4(int ido,int l1,float *cc,float *ch,float *wa1,
		float *wa2,float *wa3){
  static float hsqt2 = .70710678118654752f;
  int i,k,t0,t1,t2,t3,t4,t5,t6;
  float ci2,ci3,ci4,cr2,cr3,cr4,ti1,ti2,ti3,ti4,tr1,tr2,tr3,tr4;
  t0=l1*ido;

  t1=t0;
  t4=t1<<1;
  t2=t1+(t1<<1);
  t3=0;

  for(k=0;k<l1;k++){
	tr1=cc[t1]+cc[t2];
	tr2=cc[t3]+cc[t4];

	ch[t5=t3<<2]=tr1+tr2;
	ch[(ido<<2)+t5-1]=tr2-tr1;
	ch[(t5+=(ido<<1))-1]=cc[t3]-cc[t4];
	ch[t5]=cc[t2]-cc[t1];

	t1+=ido;
	t2+=ido;
	t3+=ido;
	t4+=ido;
  }

  if(ido<2)return;
  if(ido==2)goto L105;

  t1=0;
  for(k=0;k<l1;k++){
	t2=t1;
	t4=t1<<2;
	t5=(t6=ido<<1)+t4;
	for(i=2;i<ido;i+=2){
	  t3=(t2+=2);
	  t4+=2;
	  t5-=2;

	  t3+=t0;
	  cr2=wa1[i-2]*cc[t3-1]+wa1[i-1]*cc[t3];
	  ci2=wa1[i-2]*cc[t3]-wa1[i-1]*cc[t3-1];
	  t3+=t0;
	  cr3=wa2[i-2]*cc[t3-1]+wa2[i-1]*cc[t3];
	  ci3=wa2[i-2]*cc[t3]-wa2[i-1]*cc[t3-1];
	  t3+=t0;
	  cr4=wa3[i-2]*cc[t3-1]+wa3[i-1]*cc[t3];
	  ci4=wa3[i-2]*cc[t3]-wa3[i-1]*cc[t3-1];

	  tr1=cr2+cr4;
	  tr4=cr4-cr2;
	  ti1=ci2+ci4;
	  ti4=ci2-ci4;

	  ti2=cc[t2]+ci3;
	  ti3=cc[t2]-ci3;
	  tr2=cc[t2-1]+cr3;
	  tr3=cc[t2-1]-cr3;

	  ch[t4-1]=tr1+tr2;
	  ch[t4]=ti1+ti2;

	  ch[t5-1]=tr3-ti4;
	  ch[t5]=tr4-ti3;

	  ch[t4+t6-1]=ti4+tr3;
	  ch[t4+t6]=tr4+ti3;

	  ch[t5+t6-1]=tr2-tr1;
	  ch[t5+t6]=ti1-ti2;
	}
	t1+=ido;
  }
  if(ido&1)return;

 L105:

  t2=(t1=t0+ido-1)+(t0<<1);
  t3=ido<<2;
  t4=ido;
  t5=ido<<1;
  t6=ido;

  for(k=0;k<l1;k++){
	ti1=-hsqt2*(cc[t1]+cc[t2]);
	tr1=hsqt2*(cc[t1]-cc[t2]);

	ch[t4-1]=tr1+cc[t6-1];
	ch[t4+t5-1]=cc[t6-1]-tr1;

	ch[t4]=ti1-cc[t1+t0];
	ch[t4+t5]=ti1+cc[t1+t0];

	t1+=ido;
	t2+=ido;
	t4+=t3;
	t6+=ido;
  }
}

static void dradfg(int ido,int ip,int l1,int idl1,float *cc,float *c1,
						  float *c2,float *ch,float *ch2,float *wa){

  static float tpi=6.283185307179586f;
  int idij,ipph,i,j,k,l,ic,ik,is;
  int t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
  float dc2,ai1,ai2,ar1,ar2,ds2;
  int nbd;
  float dcp,arg,dsp,ar1h,ar2h;
  int idp2,ipp2;

  arg=tpi/(float)ip;
  dcp=cos(arg);
  dsp=sin(arg);
  ipph=(ip+1)>>1;
  ipp2=ip;
  idp2=ido;
  nbd=(ido-1)>>1;
  t0=l1*ido;
  t10=ip*ido;

  if(ido==1)goto L119;
  for(ik=0;ik<idl1;ik++)ch2[ik]=c2[ik];

  t1=0;
  for(j=1;j<ip;j++){
	t1+=t0;
	t2=t1;
	for(k=0;k<l1;k++){
	  ch[t2]=c1[t2];
	  t2+=ido;
	}
  }

  is=-ido;
  t1=0;
  if(nbd>l1){
	for(j=1;j<ip;j++){
	  t1+=t0;
	  is+=ido;
	  t2= -ido+t1;
	  for(k=0;k<l1;k++){
		idij=is-1;
		t2+=ido;
		t3=t2;
		for(i=2;i<ido;i+=2){
		  idij+=2;
		  t3+=2;
		  ch[t3-1]=wa[idij-1]*c1[t3-1]+wa[idij]*c1[t3];
		  ch[t3]=wa[idij-1]*c1[t3]-wa[idij]*c1[t3-1];
		}
	  }
	}
  }else{

	for(j=1;j<ip;j++){
	  is+=ido;
	  idij=is-1;
	  t1+=t0;
	  t2=t1;
	  for(i=2;i<ido;i+=2){
		idij+=2;
		t2+=2;
		t3=t2;
		for(k=0;k<l1;k++){
		  ch[t3-1]=wa[idij-1]*c1[t3-1]+wa[idij]*c1[t3];
		  ch[t3]=wa[idij-1]*c1[t3]-wa[idij]*c1[t3-1];
		  t3+=ido;
		}
	  }
	}
  }

  t1=0;
  t2=ipp2*t0;
  if(nbd<l1){
	for(j=1;j<ipph;j++){
	  t1+=t0;
	  t2-=t0;
	  t3=t1;
	  t4=t2;
	  for(i=2;i<ido;i+=2){
		t3+=2;
		t4+=2;
		t5=t3-ido;
		t6=t4-ido;
		for(k=0;k<l1;k++){
		  t5+=ido;
		  t6+=ido;
		  c1[t5-1]=ch[t5-1]+ch[t6-1];
		  c1[t6-1]=ch[t5]-ch[t6];
		  c1[t5]=ch[t5]+ch[t6];
		  c1[t6]=ch[t6-1]-ch[t5-1];
		}
	  }
	}
  }else{
	for(j=1;j<ipph;j++){
	  t1+=t0;
	  t2-=t0;
	  t3=t1;
	  t4=t2;
	  for(k=0;k<l1;k++){
		t5=t3;
		t6=t4;
		for(i=2;i<ido;i+=2){
		  t5+=2;
		  t6+=2;
		  c1[t5-1]=ch[t5-1]+ch[t6-1];
		  c1[t6-1]=ch[t5]-ch[t6];
		  c1[t5]=ch[t5]+ch[t6];
		  c1[t6]=ch[t6-1]-ch[t5-1];
		}
		t3+=ido;
		t4+=ido;
	  }
	}
  }

L119:
  for(ik=0;ik<idl1;ik++)c2[ik]=ch2[ik];

  t1=0;
  t2=ipp2*idl1;
  for(j=1;j<ipph;j++){
	t1+=t0;
	t2-=t0;
	t3=t1-ido;
	t4=t2-ido;
	for(k=0;k<l1;k++){
	  t3+=ido;
	  t4+=ido;
	  c1[t3]=ch[t3]+ch[t4];
	  c1[t4]=ch[t4]-ch[t3];
	}
  }

  ar1=1.f;
  ai1=0.f;
  t1=0;
  t2=ipp2*idl1;
  t3=(ip-1)*idl1;
  for(l=1;l<ipph;l++){
	t1+=idl1;
	t2-=idl1;
	ar1h=dcp*ar1-dsp*ai1;
	ai1=dcp*ai1+dsp*ar1;
	ar1=ar1h;
	t4=t1;
	t5=t2;
	t6=t3;
	t7=idl1;

	for(ik=0;ik<idl1;ik++){
	  ch2[t4++]=c2[ik]+ar1*c2[t7++];
	  ch2[t5++]=ai1*c2[t6++];
	}

	dc2=ar1;
	ds2=ai1;
	ar2=ar1;
	ai2=ai1;

	t4=idl1;
	t5=(ipp2-1)*idl1;
	for(j=2;j<ipph;j++){
	  t4+=idl1;
	  t5-=idl1;

	  ar2h=dc2*ar2-ds2*ai2;
	  ai2=dc2*ai2+ds2*ar2;
	  ar2=ar2h;

	  t6=t1;
	  t7=t2;
	  t8=t4;
	  t9=t5;
	  for(ik=0;ik<idl1;ik++){
		ch2[t6++]+=ar2*c2[t8++];
		ch2[t7++]+=ai2*c2[t9++];
	  }
	}
  }

  t1=0;
  for(j=1;j<ipph;j++){
	t1+=idl1;
	t2=t1;
	for(ik=0;ik<idl1;ik++)ch2[ik]+=c2[t2++];
  }

  if(ido<l1)goto L132;

  t1=0;
  t2=0;
  for(k=0;k<l1;k++){
	t3=t1;
	t4=t2;
	for(i=0;i<ido;i++)cc[t4++]=ch[t3++];
	t1+=ido;
	t2+=t10;
  }

  goto L135;

 L132:
  for(i=0;i<ido;i++){
	t1=i;
	t2=i;
	for(k=0;k<l1;k++){
	  cc[t2]=ch[t1];
	  t1+=ido;
	  t2+=t10;
	}
  }

 L135:
  t1=0;
  t2=ido<<1;
  t3=0;
  t4=ipp2*t0;
  for(j=1;j<ipph;j++){

	t1+=t2;
	t3+=t0;
	t4-=t0;

	t5=t1;
	t6=t3;
	t7=t4;

	for(k=0;k<l1;k++){
	  cc[t5-1]=ch[t6];
	  cc[t5]=ch[t7];
	  t5+=t10;
	  t6+=ido;
	  t7+=ido;
	}
  }

  if(ido==1)return;
  if(nbd<l1)goto L141;

  t1=-ido;
  t3=0;
  t4=0;
  t5=ipp2*t0;
  for(j=1;j<ipph;j++){
	t1+=t2;
	t3+=t2;
	t4+=t0;
	t5-=t0;
	t6=t1;
	t7=t3;
	t8=t4;
	t9=t5;
	for(k=0;k<l1;k++){
	  for(i=2;i<ido;i+=2){
		ic=idp2-i;
		cc[i+t7-1]=ch[i+t8-1]+ch[i+t9-1];
		cc[ic+t6-1]=ch[i+t8-1]-ch[i+t9-1];
		cc[i+t7]=ch[i+t8]+ch[i+t9];
		cc[ic+t6]=ch[i+t9]-ch[i+t8];
	  }
	  t6+=t10;
	  t7+=t10;
	  t8+=ido;
	  t9+=ido;
	}
  }
  return;

 L141:

  t1=-ido;
  t3=0;
  t4=0;
  t5=ipp2*t0;
  for(j=1;j<ipph;j++){
	t1+=t2;
	t3+=t2;
	t4+=t0;
	t5-=t0;
	for(i=2;i<ido;i+=2){
	  t6=idp2+t1-i;
	  t7=i+t3;
	  t8=i+t4;
	  t9=i+t5;
	  for(k=0;k<l1;k++){
		cc[t7-1]=ch[t8-1]+ch[t9-1];
		cc[t6-1]=ch[t8-1]-ch[t9-1];
		cc[t7]=ch[t8]+ch[t9];
		cc[t6]=ch[t9]-ch[t8];
		t6+=t10;
		t7+=t10;
		t8+=ido;
		t9+=ido;
	  }
	}
  }
}

static void drftf1(int n,float *c,float *ch,float *wa,int *ifac){
  int i,k1,l1,l2;
  int na,kh,nf;
  int ip,iw,ido,idl1,ix2,ix3;

  nf=ifac[1];
  na=1;
  l2=n;
  iw=n;

  for(k1=0;k1<nf;k1++){
	kh=nf-k1;
	ip=ifac[kh+1];
	l1=l2/ip;
	ido=n/l2;
	idl1=ido*l1;
	iw-=(ip-1)*ido;
	na=1-na;

	if(ip!=4)goto L102;

	ix2=iw+ido;
	ix3=ix2+ido;
	if(na!=0)
	  dradf4(ido,l1,ch,c,wa+iw-1,wa+ix2-1,wa+ix3-1);
	else
	  dradf4(ido,l1,c,ch,wa+iw-1,wa+ix2-1,wa+ix3-1);
	goto L110;

 L102:
	if(ip!=2)goto L104;
	if(na!=0)goto L103;

	dradf2(ido,l1,c,ch,wa+iw-1);
	goto L110;

  L103:
	dradf2(ido,l1,ch,c,wa+iw-1);
	goto L110;

  L104:
	if(ido==1)na=1-na;
	if(na!=0)goto L109;

	dradfg(ido,ip,l1,idl1,c,c,c,ch,ch,wa+iw-1);
	na=1;
	goto L110;

  L109:
	dradfg(ido,ip,l1,idl1,ch,ch,ch,c,c,wa+iw-1);
	na=0;

  L110:
	l2=l1;
  }

  if(na==1)return;

  for(i=0;i<n;i++)c[i]=ch[i];
}

static void dradb2(int ido,int l1,float *cc,float *ch,float *wa1){
  int i,k,t0,t1,t2,t3,t4,t5,t6;
  float ti2,tr2;

  t0=l1*ido;

  t1=0;
  t2=0;
  t3=(ido<<1)-1;
  for(k=0;k<l1;k++){
	ch[t1]=cc[t2]+cc[t3+t2];
	ch[t1+t0]=cc[t2]-cc[t3+t2];
	t2=(t1+=ido)<<1;
  }

  if(ido<2)return;
  if(ido==2)goto L105;

  t1=0;
  t2=0;
  for(k=0;k<l1;k++){
	t3=t1;
	t5=(t4=t2)+(ido<<1);
	t6=t0+t1;
	for(i=2;i<ido;i+=2){
	  t3+=2;
	  t4+=2;
	  t5-=2;
	  t6+=2;
	  ch[t3-1]=cc[t4-1]+cc[t5-1];
	  tr2=cc[t4-1]-cc[t5-1];
	  ch[t3]=cc[t4]-cc[t5];
	  ti2=cc[t4]+cc[t5];
	  ch[t6-1]=wa1[i-2]*tr2-wa1[i-1]*ti2;
	  ch[t6]=wa1[i-2]*ti2+wa1[i-1]*tr2;
	}
	t2=(t1+=ido)<<1;
  }

  if(ido%2==1)return;

L105:
  t1=ido-1;
  t2=ido-1;
  for(k=0;k<l1;k++){
	ch[t1]=cc[t2]+cc[t2];
	ch[t1+t0]=-(cc[t2+1]+cc[t2+1]);
	t1+=ido;
	t2+=ido<<1;
  }
}

static void dradb3(int ido,int l1,float *cc,float *ch,float *wa1,
						  float *wa2){
  static float taur = -.5f;
  static float taui = .8660254037844386f;
  int i,k,t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
  float ci2,ci3,di2,di3,cr2,cr3,dr2,dr3,ti2,tr2;
  t0=l1*ido;

  t1=0;
  t2=t0<<1;
  t3=ido<<1;
  t4=ido+(ido<<1);
  t5=0;
  for(k=0;k<l1;k++){
	tr2=cc[t3-1]+cc[t3-1];
	cr2=cc[t5]+(taur*tr2);
	ch[t1]=cc[t5]+tr2;
	ci3=taui*(cc[t3]+cc[t3]);
	ch[t1+t0]=cr2-ci3;
	ch[t1+t2]=cr2+ci3;
	t1+=ido;
	t3+=t4;
	t5+=t4;
  }

  if(ido==1)return;

  t1=0;
  t3=ido<<1;
  for(k=0;k<l1;k++){
	t7=t1+(t1<<1);
	t6=(t5=t7+t3);
	t8=t1;
	t10=(t9=t1+t0)+t0;

	for(i=2;i<ido;i+=2){
	  t5+=2;
	  t6-=2;
	  t7+=2;
	  t8+=2;
	  t9+=2;
	  t10+=2;
	  tr2=cc[t5-1]+cc[t6-1];
	  cr2=cc[t7-1]+(taur*tr2);
	  ch[t8-1]=cc[t7-1]+tr2;
	  ti2=cc[t5]-cc[t6];
	  ci2=cc[t7]+(taur*ti2);
	  ch[t8]=cc[t7]+ti2;
	  cr3=taui*(cc[t5-1]-cc[t6-1]);
	  ci3=taui*(cc[t5]+cc[t6]);
	  dr2=cr2-ci3;
	  dr3=cr2+ci3;
	  di2=ci2+cr3;
	  di3=ci2-cr3;
	  ch[t9-1]=wa1[i-2]*dr2-wa1[i-1]*di2;
	  ch[t9]=wa1[i-2]*di2+wa1[i-1]*dr2;
	  ch[t10-1]=wa2[i-2]*dr3-wa2[i-1]*di3;
	  ch[t10]=wa2[i-2]*di3+wa2[i-1]*dr3;
	}
	t1+=ido;
  }
}

static void dradb4(int ido,int l1,float *cc,float *ch,float *wa1,
			  float *wa2,float *wa3){
  static float sqrt2=1.414213562373095f;
  int i,k,t0,t1,t2,t3,t4,t5,t6,t7,t8;
  float ci2,ci3,ci4,cr2,cr3,cr4,ti1,ti2,ti3,ti4,tr1,tr2,tr3,tr4;
  t0=l1*ido;

  t1=0;
  t2=ido<<2;
  t3=0;
  t6=ido<<1;
  for(k=0;k<l1;k++){
	t4=t3+t6;
	t5=t1;
	tr3=cc[t4-1]+cc[t4-1];
	tr4=cc[t4]+cc[t4];
	tr1=cc[t3]-cc[(t4+=t6)-1];
	tr2=cc[t3]+cc[t4-1];
	ch[t5]=tr2+tr3;
	ch[t5+=t0]=tr1-tr4;
	ch[t5+=t0]=tr2-tr3;
	ch[t5+=t0]=tr1+tr4;
	t1+=ido;
	t3+=t2;
  }

  if(ido<2)return;
  if(ido==2)goto L105;

  t1=0;
  for(k=0;k<l1;k++){
	t5=(t4=(t3=(t2=t1<<2)+t6))+t6;
	t7=t1;
	for(i=2;i<ido;i+=2){
	  t2+=2;
	  t3+=2;
	  t4-=2;
	  t5-=2;
	  t7+=2;
	  ti1=cc[t2]+cc[t5];
	  ti2=cc[t2]-cc[t5];
	  ti3=cc[t3]-cc[t4];
	  tr4=cc[t3]+cc[t4];
	  tr1=cc[t2-1]-cc[t5-1];
	  tr2=cc[t2-1]+cc[t5-1];
	  ti4=cc[t3-1]-cc[t4-1];
	  tr3=cc[t3-1]+cc[t4-1];
	  ch[t7-1]=tr2+tr3;
	  cr3=tr2-tr3;
	  ch[t7]=ti2+ti3;
	  ci3=ti2-ti3;
	  cr2=tr1-tr4;
	  cr4=tr1+tr4;
	  ci2=ti1+ti4;
	  ci4=ti1-ti4;

	  ch[(t8=t7+t0)-1]=wa1[i-2]*cr2-wa1[i-1]*ci2;
	  ch[t8]=wa1[i-2]*ci2+wa1[i-1]*cr2;
	  ch[(t8+=t0)-1]=wa2[i-2]*cr3-wa2[i-1]*ci3;
	  ch[t8]=wa2[i-2]*ci3+wa2[i-1]*cr3;
	  ch[(t8+=t0)-1]=wa3[i-2]*cr4-wa3[i-1]*ci4;
	  ch[t8]=wa3[i-2]*ci4+wa3[i-1]*cr4;
	}
	t1+=ido;
  }

  if(ido%2 == 1)return;

 L105:

  t1=ido;
  t2=ido<<2;
  t3=ido-1;
  t4=ido+(ido<<1);
  for(k=0;k<l1;k++){
	t5=t3;
	ti1=cc[t1]+cc[t4];
	ti2=cc[t4]-cc[t1];
	tr1=cc[t1-1]-cc[t4-1];
	tr2=cc[t1-1]+cc[t4-1];
	ch[t5]=tr2+tr2;
	ch[t5+=t0]=sqrt2*(tr1-ti1);
	ch[t5+=t0]=ti2+ti2;
	ch[t5+=t0]=-sqrt2*(tr1+ti1);

	t3+=ido;
	t1+=t2;
	t4+=t2;
  }
}

static void dradbg(int ido,int ip,int l1,int idl1,float *cc,float *c1,
			float *c2,float *ch,float *ch2,float *wa){
  static float tpi=6.283185307179586f;
  int idij,ipph,i,j,k,l,ik,is,t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,
	  t11,t12;
  float dc2,ai1,ai2,ar1,ar2,ds2;
  int nbd;
  float dcp,arg,dsp,ar1h,ar2h;
  int ipp2;

  t10=ip*ido;
  t0=l1*ido;
  arg=tpi/(float)ip;
  dcp=cos(arg);
  dsp=sin(arg);
  nbd=(ido-1)>>1;
  ipp2=ip;
  ipph=(ip+1)>>1;
  if(ido<l1)goto L103;

  t1=0;
  t2=0;
  for(k=0;k<l1;k++){
	t3=t1;
	t4=t2;
	for(i=0;i<ido;i++){
	  ch[t3]=cc[t4];
	  t3++;
	  t4++;
	}
	t1+=ido;
	t2+=t10;
  }
  goto L106;

 L103:
  t1=0;
  for(i=0;i<ido;i++){
	t2=t1;
	t3=t1;
	for(k=0;k<l1;k++){
	  ch[t2]=cc[t3];
	  t2+=ido;
	  t3+=t10;
	}
	t1++;
  }

 L106:
  t1=0;
  t2=ipp2*t0;
  t7=(t5=ido<<1);
  for(j=1;j<ipph;j++){
	t1+=t0;
	t2-=t0;
	t3=t1;
	t4=t2;
	t6=t5;
	for(k=0;k<l1;k++){
	  ch[t3]=cc[t6-1]+cc[t6-1];
	  ch[t4]=cc[t6]+cc[t6];
	  t3+=ido;
	  t4+=ido;
	  t6+=t10;
	}
	t5+=t7;
  }

  if (ido == 1)goto L116;
  if(nbd<l1)goto L112;

  t1=0;
  t2=ipp2*t0;
  t7=0;
  for(j=1;j<ipph;j++){
	t1+=t0;
	t2-=t0;
	t3=t1;
	t4=t2;

	t7+=(ido<<1);
	t8=t7;
	for(k=0;k<l1;k++){
	  t5=t3;
	  t6=t4;
	  t9=t8;
	  t11=t8;
	  for(i=2;i<ido;i+=2){
		t5+=2;
		t6+=2;
		t9+=2;
		t11-=2;
		ch[t5-1]=cc[t9-1]+cc[t11-1];
		ch[t6-1]=cc[t9-1]-cc[t11-1];
		ch[t5]=cc[t9]-cc[t11];
		ch[t6]=cc[t9]+cc[t11];
	  }
	  t3+=ido;
	  t4+=ido;
	  t8+=t10;
	}
  }
  goto L116;

 L112:
  t1=0;
  t2=ipp2*t0;
  t7=0;
  for(j=1;j<ipph;j++){
	t1+=t0;
	t2-=t0;
	t3=t1;
	t4=t2;
	t7+=(ido<<1);
	t8=t7;
	t9=t7;
	for(i=2;i<ido;i+=2){
	  t3+=2;
	  t4+=2;
	  t8+=2;
	  t9-=2;
	  t5=t3;
	  t6=t4;
	  t11=t8;
	  t12=t9;
	  for(k=0;k<l1;k++){
		ch[t5-1]=cc[t11-1]+cc[t12-1];
		ch[t6-1]=cc[t11-1]-cc[t12-1];
		ch[t5]=cc[t11]-cc[t12];
		ch[t6]=cc[t11]+cc[t12];
		t5+=ido;
		t6+=ido;
		t11+=t10;
		t12+=t10;
	  }
	}
  }

L116:
  ar1=1.f;
  ai1=0.f;
  t1=0;
  t9=(t2=ipp2*idl1);
  t3=(ip-1)*idl1;
  for(l=1;l<ipph;l++){
	t1+=idl1;
	t2-=idl1;

	ar1h=dcp*ar1-dsp*ai1;
	ai1=dcp*ai1+dsp*ar1;
	ar1=ar1h;
	t4=t1;
	t5=t2;
	t6=0;
	t7=idl1;
	t8=t3;
	for(ik=0;ik<idl1;ik++){
	  c2[t4++]=ch2[t6++]+ar1*ch2[t7++];
	  c2[t5++]=ai1*ch2[t8++];
	}
	dc2=ar1;
	ds2=ai1;
	ar2=ar1;
	ai2=ai1;

	t6=idl1;
	t7=t9-idl1;
	for(j=2;j<ipph;j++){
	  t6+=idl1;
	  t7-=idl1;
	  ar2h=dc2*ar2-ds2*ai2;
	  ai2=dc2*ai2+ds2*ar2;
	  ar2=ar2h;
	  t4=t1;
	  t5=t2;
	  t11=t6;
	  t12=t7;
	  for(ik=0;ik<idl1;ik++){
		c2[t4++]+=ar2*ch2[t11++];
		c2[t5++]+=ai2*ch2[t12++];
	  }
	}
  }

  t1=0;
  for(j=1;j<ipph;j++){
	t1+=idl1;
	t2=t1;
	for(ik=0;ik<idl1;ik++)ch2[ik]+=ch2[t2++];
  }

  t1=0;
  t2=ipp2*t0;
  for(j=1;j<ipph;j++){
	t1+=t0;
	t2-=t0;
	t3=t1;
	t4=t2;
	for(k=0;k<l1;k++){
	  ch[t3]=c1[t3]-c1[t4];
	  ch[t4]=c1[t3]+c1[t4];
	  t3+=ido;
	  t4+=ido;
	}
  }

  if(ido==1)goto L132;
  if(nbd<l1)goto L128;

  t1=0;
  t2=ipp2*t0;
  for(j=1;j<ipph;j++){
	t1+=t0;
	t2-=t0;
	t3=t1;
	t4=t2;
	for(k=0;k<l1;k++){
	  t5=t3;
	  t6=t4;
	  for(i=2;i<ido;i+=2){
		t5+=2;
		t6+=2;
		ch[t5-1]=c1[t5-1]-c1[t6];
		ch[t6-1]=c1[t5-1]+c1[t6];
		ch[t5]=c1[t5]+c1[t6-1];
		ch[t6]=c1[t5]-c1[t6-1];
	  }
	  t3+=ido;
	  t4+=ido;
	}
  }
  goto L132;

 L128:
  t1=0;
  t2=ipp2*t0;
  for(j=1;j<ipph;j++){
	t1+=t0;
	t2-=t0;
	t3=t1;
	t4=t2;
	for(i=2;i<ido;i+=2){
	  t3+=2;
	  t4+=2;
	  t5=t3;
	  t6=t4;
	  for(k=0;k<l1;k++){
		ch[t5-1]=c1[t5-1]-c1[t6];
		ch[t6-1]=c1[t5-1]+c1[t6];
		ch[t5]=c1[t5]+c1[t6-1];
		ch[t6]=c1[t5]-c1[t6-1];
		t5+=ido;
		t6+=ido;
	  }
	}
  }

L132:
  if(ido==1)return;

  for(ik=0;ik<idl1;ik++)c2[ik]=ch2[ik];

  t1=0;
  for(j=1;j<ip;j++){
	t2=(t1+=t0);
	for(k=0;k<l1;k++){
	  c1[t2]=ch[t2];
	  t2+=ido;
	}
  }

  if(nbd>l1)goto L139;

  is= -ido-1;
  t1=0;
  for(j=1;j<ip;j++){
	is+=ido;
	t1+=t0;
	idij=is;
	t2=t1;
	for(i=2;i<ido;i+=2){
	  t2+=2;
	  idij+=2;
	  t3=t2;
	  for(k=0;k<l1;k++){
		c1[t3-1]=wa[idij-1]*ch[t3-1]-wa[idij]*ch[t3];
		c1[t3]=wa[idij-1]*ch[t3]+wa[idij]*ch[t3-1];
		t3+=ido;
	  }
	}
  }
  return;

 L139:
  is= -ido-1;
  t1=0;
  for(j=1;j<ip;j++){
	is+=ido;
	t1+=t0;
	t2=t1;
	for(k=0;k<l1;k++){
	  idij=is;
	  t3=t2;
	  for(i=2;i<ido;i+=2){
		idij+=2;
		t3+=2;
		c1[t3-1]=wa[idij-1]*ch[t3-1]-wa[idij]*ch[t3];
		c1[t3]=wa[idij-1]*ch[t3]+wa[idij]*ch[t3-1];
	  }
	  t2+=ido;
	}
  }
}

static void drftb1(int n, float *c, float *ch, float *wa, int *ifac){
  int i,k1,l1,l2;
  int na;
  int nf,ip,iw,ix2,ix3,ido,idl1;

  nf=ifac[1];
  na=0;
  l1=1;
  iw=1;

  for(k1=0;k1<nf;k1++){
	ip=ifac[k1 + 2];
	l2=ip*l1;
	ido=n/l2;
	idl1=ido*l1;
	if(ip!=4)goto L103;
	ix2=iw+ido;
	ix3=ix2+ido;

	if(na!=0)
	  dradb4(ido,l1,ch,c,wa+iw-1,wa+ix2-1,wa+ix3-1);
	else
	  dradb4(ido,l1,c,ch,wa+iw-1,wa+ix2-1,wa+ix3-1);
	na=1-na;
	goto L115;

  L103:
	if(ip!=2)goto L106;

	if(na!=0)
	  dradb2(ido,l1,ch,c,wa+iw-1);
	else
	  dradb2(ido,l1,c,ch,wa+iw-1);
	na=1-na;
	goto L115;

  L106:
	if(ip!=3)goto L109;

	ix2=iw+ido;
	if(na!=0)
	  dradb3(ido,l1,ch,c,wa+iw-1,wa+ix2-1);
	else
	  dradb3(ido,l1,c,ch,wa+iw-1,wa+ix2-1);
	na=1-na;
	goto L115;

  L109:
	if(na!=0)
	  dradbg(ido,ip,l1,idl1,ch,ch,ch,c,c,wa+iw-1);
	else
	  dradbg(ido,ip,l1,idl1,c,c,c,ch,ch,wa+iw-1);
	if(ido==1)na=1-na;

  L115:
	l1=l2;
	iw+=(ip-1)*ido;
  }

  if(na==0)return;

  for(i=0;i<n;i++)c[i]=ch[i];
}

void drft_forward(drft_lookup *l,float *data){
  if(l->n==1)return;
  drftf1(l->n,data,l->trigcache,l->trigcache+l->n,l->splitcache);
}

void drft_backward(drft_lookup *l,float *data){
  if (l->n==1)return;
  drftb1(l->n,data,l->trigcache,l->trigcache+l->n,l->splitcache);
}

void drft_init(drft_lookup *l,int n){
  l->n=n;
  l->trigcache=(float*)_ogg_calloc(3*n,sizeof(*l->trigcache));
  l->splitcache=(int*)_ogg_calloc(32,sizeof(*l->splitcache));
  fdrffti(n, l->trigcache, l->splitcache);
}

void drft_clear(drft_lookup *l){
  if(l){
	if(l->trigcache)_ogg_free(l->trigcache);
	if(l->splitcache)_ogg_free(l->splitcache);
	memset(l,0,sizeof(*l));
  }
}

#endif
/*** End of inlined file: smallft.c ***/


/*** Start of inlined file: synthesis.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdio.h>

int vorbis_synthesis(vorbis_block *vb,ogg_packet *op){
  vorbis_dsp_state	 *vd=vb->vd;
  private_state	*b=(private_state*)vd->backend_state;
  vorbis_info	  *vi=vd->vi;
  codec_setup_info	 *ci=(codec_setup_info*) vi->codec_setup;
  oggpack_buffer	   *opb=&vb->opb;
  int		   type,mode,i;

  _vorbis_block_ripcord(vb);
  oggpack_readinit(opb,op->packet,op->bytes);

  if(oggpack_read(opb,1)!=0){
	return(OV_ENOTAUDIO);
  }

  mode=oggpack_read(opb,b->modebits);
  if(mode==-1)return(OV_EBADPACKET);

  vb->mode=mode;
  vb->W=ci->mode_param[mode]->blockflag;
  if(vb->W){

	vb->lW=oggpack_read(opb,1);
	vb->nW=oggpack_read(opb,1);
	if(vb->nW==-1)   return(OV_EBADPACKET);
  }else{
	vb->lW=0;
	vb->nW=0;
  }

  vb->granulepos=op->granulepos;
  vb->sequence=op->packetno;
  vb->eofflag=op->e_o_s;

  vb->pcmend=ci->blocksizes[vb->W];
  vb->pcm=(float**)_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
  for(i=0;i<vi->channels;i++)
	vb->pcm[i]=(float*)_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i]));

  type=ci->map_type[ci->mode_param[mode]->mapping];

  return(_mapping_P[type]->inverse(vb,ci->map_param[ci->mode_param[mode]->
						   mapping]));
}

int vorbis_synthesis_trackonly(vorbis_block *vb,ogg_packet *op){
  vorbis_dsp_state	 *vd=vb->vd;
  private_state	*b=(private_state*)vd->backend_state;
  vorbis_info	  *vi=vd->vi;
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  oggpack_buffer	   *opb=&vb->opb;
  int		   mode;

  _vorbis_block_ripcord(vb);
  oggpack_readinit(opb,op->packet,op->bytes);

  if(oggpack_read(opb,1)!=0){
	return(OV_ENOTAUDIO);
  }

  mode=oggpack_read(opb,b->modebits);
  if(mode==-1)return(OV_EBADPACKET);

  vb->mode=mode;
  vb->W=ci->mode_param[mode]->blockflag;
  if(vb->W){
	vb->lW=oggpack_read(opb,1);
	vb->nW=oggpack_read(opb,1);
	if(vb->nW==-1)   return(OV_EBADPACKET);
  }else{
	vb->lW=0;
	vb->nW=0;
  }

  vb->granulepos=op->granulepos;
  vb->sequence=op->packetno;
  vb->eofflag=op->e_o_s;

  vb->pcmend=0;
  vb->pcm=NULL;

  return(0);
}

long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  oggpack_buffer	   opb;
  int		  mode;

  oggpack_readinit(&opb,op->packet,op->bytes);

  if(oggpack_read(&opb,1)!=0){
	return(OV_ENOTAUDIO);
  }

  {
	int modebits=0;
	int v=ci->modes;
	while(v>1){
	  modebits++;
	  v>>=1;
	}

	mode=oggpack_read(&opb,modebits);
  }
  if(mode==-1)return(OV_EBADPACKET);
  return(ci->blocksizes[ci->mode_param[mode]->blockflag]);
}

int vorbis_synthesis_halfrate(vorbis_info *vi,int flag){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;

  if(ci->blocksizes[0]<=64 && flag)return -1;
  ci->halfrate_flag=(flag?1:0);
  return 0;
}

int vorbis_synthesis_halfrate_p(vorbis_info *vi){
  codec_setup_info	 *ci=(codec_setup_info*)vi->codec_setup;
  return ci->halfrate_flag;
}

#endif
/*** End of inlined file: synthesis.c ***/


/*** Start of inlined file: vorbisenc.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct {
  static_codebook *books[12][3];
} static_bookblock;

typedef struct {
  int res_type;
  int limit_type; /* 0 lowpass limited, 1 point stereo limited */
  vorbis_info_residue0 *res;
  static_codebook  *book_aux;
  static_codebook  *book_aux_managed;
  static_bookblock *books_base;
  static_bookblock *books_base_managed;
} vorbis_residue_template;

typedef struct {
  vorbis_info_mapping0	*map;
  vorbis_residue_template *res;
} vorbis_mapping_template;

typedef struct vp_adjblock{
  int block[P_BANDS];
} vp_adjblock;

typedef struct {
  int data[NOISE_COMPAND_LEVELS];
} compandblock;

typedef struct {
  int att[P_NOISECURVES];
  float boost;
  float decay;
} att3;
typedef struct { int data[P_NOISECURVES]; } adj3;

typedef struct {
  int   pre[PACKETBLOBS];
  int   post[PACKETBLOBS];
  float kHz[PACKETBLOBS];
  float lowpasskHz[PACKETBLOBS];
} adj_stereo;

typedef struct {
  int lo;
  int hi;
  int fixed;
} noiseguard;
typedef struct {
  int data[P_NOISECURVES][17];
} noise3;

typedef struct {
  int	  mappings;
  double  *rate_mapping;
  double  *quality_mapping;
  int	  coupling_restriction;
  long	 samplerate_min_restriction;
  long	 samplerate_max_restriction;

  int	 *blocksize_short;
  int	 *blocksize_long;

  att3	*psy_tone_masteratt;
  int	 *psy_tone_0dB;
  int	 *psy_tone_dBsuppress;

  vp_adjblock *psy_tone_adj_impulse;
  vp_adjblock *psy_tone_adj_long;
  vp_adjblock *psy_tone_adj_other;

  noiseguard  *psy_noiseguards;
  noise3	  *psy_noise_bias_impulse;
  noise3	  *psy_noise_bias_padding;
  noise3	  *psy_noise_bias_trans;
  noise3	  *psy_noise_bias_long;
  int	 *psy_noise_dBsuppress;

  compandblock  *psy_noise_compand;
  double	*psy_noise_compand_short_mapping;
  double	*psy_noise_compand_long_mapping;

  int	  *psy_noise_normal_start[2];
  int	  *psy_noise_normal_partition[2];
  double   *psy_noise_normal_thresh;

  int	  *psy_ath_float;
  int	  *psy_ath_abs;

  double   *psy_lowpass;

  vorbis_info_psy_global *global_params;
  double	 *global_mapping;
  adj_stereo *stereo_modes;

  static_codebook ***floor_books;
  vorbis_info_floor1 *floor_params;
  int *floor_short_mapping;
  int *floor_long_mapping;

  vorbis_mapping_template *maps;
} ve_setup_data_template;

static vorbis_info_mode _mode_template[2]={
  {0,0,0,0},
  {1,0,0,1}
};

static vorbis_info_mapping0 _map_nominal[2]={
  {1, {0,0}, {0}, {0}, 1,{0},{1}},
  {1, {0,0}, {1}, {1}, 1,{0},{1}}
};


/*** Start of inlined file: setup_44.h ***/

/*** Start of inlined file: floor_all.h ***/

/*** Start of inlined file: floor_books.h ***/
static long _huff_lengthlist_line_256x7_0sub1[] = {
	 0, 2, 3, 3, 3, 3, 4, 3, 4,
};

static static_codebook _huff_book_line_256x7_0sub1 = {
	1, 9,
	_huff_lengthlist_line_256x7_0sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x7_0sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 3, 4, 3, 5, 3,
	 6, 3, 6, 4, 6, 4, 7, 5, 7,
};

static static_codebook _huff_book_line_256x7_0sub2 = {
	1, 25,
	_huff_lengthlist_line_256x7_0sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x7_0sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 2, 5, 3, 5, 3,
	 6, 3, 6, 4, 7, 6, 7, 8, 7, 9, 8, 9, 9, 9,10, 9,
	11,13,11,13,10,10,13,13,13,13,13,13,12,12,12,12,
};

static static_codebook _huff_book_line_256x7_0sub3 = {
	1, 64,
	_huff_lengthlist_line_256x7_0sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x7_1sub1[] = {
	 0, 3, 3, 3, 3, 2, 4, 3, 4,
};

static static_codebook _huff_book_line_256x7_1sub1 = {
	1, 9,
	_huff_lengthlist_line_256x7_1sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x7_1sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 3, 4, 3, 4, 4,
	 5, 4, 6, 5, 6, 7, 6, 8, 8,
};

static static_codebook _huff_book_line_256x7_1sub2 = {
	1, 25,
	_huff_lengthlist_line_256x7_1sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x7_1sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 4, 3, 6, 3, 7,
	 3, 8, 5, 8, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7,
};

static static_codebook _huff_book_line_256x7_1sub3 = {
	1, 64,
	_huff_lengthlist_line_256x7_1sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x7_class0[] = {
	 7, 5, 5, 9, 9, 6, 6, 9,12, 8, 7, 8,11, 8, 9,15,
	 6, 3, 3, 7, 7, 4, 3, 6, 9, 6, 5, 6, 8, 6, 8,15,
	 8, 5, 5, 9, 8, 5, 4, 6,10, 7, 5, 5,11, 8, 7,15,
	14,15,13,13,13,13, 8,11,15,10, 7, 6,11, 9,10,15,
};

static static_codebook _huff_book_line_256x7_class0 = {
	1, 64,
	_huff_lengthlist_line_256x7_class0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x7_class1[] = {
	 5, 6, 8,15, 6, 9,10,15,10,11,12,15,15,15,15,15,
	 4, 6, 7,15, 6, 7, 8,15, 9, 8, 9,15,15,15,15,15,
	 6, 8, 9,15, 7, 7, 8,15,10, 9,10,15,15,15,15,15,
	15,13,15,15,15,10,11,15,15,13,13,15,15,15,15,15,
	 4, 6, 7,15, 6, 8, 9,15,10,10,12,15,15,15,15,15,
	 2, 5, 6,15, 5, 6, 7,15, 8, 6, 7,15,15,15,15,15,
	 5, 6, 8,15, 5, 6, 7,15, 9, 6, 7,15,15,15,15,15,
	14,12,13,15,12,10,11,15,15,15,15,15,15,15,15,15,
	 7, 8, 9,15, 9,10,10,15,15,14,14,15,15,15,15,15,
	 5, 6, 7,15, 7, 8, 9,15,12, 9,10,15,15,15,15,15,
	 7, 7, 9,15, 7, 7, 8,15,12, 8, 9,15,15,15,15,15,
	13,13,14,15,12,11,12,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	13,13,13,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,12,13,15,15,12,13,15,15,14,15,15,15,15,15,15,
	15,15,15,15,15,15,13,15,15,15,15,15,15,15,15,15,
};

static static_codebook _huff_book_line_256x7_class1 = {
	1, 256,
	_huff_lengthlist_line_256x7_class1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_0sub0[] = {
	 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
	 5, 6, 5, 6, 6, 6, 6, 5, 6, 6, 7, 6, 7, 6, 7, 6,
	 7, 6, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 9, 7, 9, 7,
	 9, 7, 9, 8, 9, 8,10, 8,10, 8,10, 7,10, 6,10, 8,
	10, 8,11, 7,10, 7,11, 8,11,11,12,12,11,11,12,11,
	13,11,13,11,13,12,15,12,13,13,14,14,14,14,14,15,
	15,15,16,14,17,19,19,18,18,18,18,18,18,18,18,18,
	18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,
};

static static_codebook _huff_book_line_512x17_0sub0 = {
	1, 128,
	_huff_lengthlist_line_512x17_0sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_1sub0[] = {
	 2, 4, 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 5, 5, 6, 5,
	 6, 5, 6, 6, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 8, 7,
};

static static_codebook _huff_book_line_512x17_1sub0 = {
	1, 32,
	_huff_lengthlist_line_512x17_1sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_1sub1[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 4, 3, 5, 3, 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 6, 5,
	 6, 5, 7, 5, 8, 6, 8, 6, 8, 6, 8, 6, 8, 7, 9, 7,
	 9, 7,11, 9,11,11,12,11,14,12,14,16,14,16,13,16,
	14,16,12,15,13,16,14,16,13,14,12,15,13,15,13,13,
	13,15,12,14,14,15,13,15,12,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
};

static static_codebook _huff_book_line_512x17_1sub1 = {
	1, 128,
	_huff_lengthlist_line_512x17_1sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_2sub1[] = {
	 0, 4, 5, 4, 4, 4, 5, 4, 4, 4, 5, 4, 5, 4, 5, 3,
	 5, 3,
};

static static_codebook _huff_book_line_512x17_2sub1 = {
	1, 18,
	_huff_lengthlist_line_512x17_2sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_2sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 6, 4, 6, 5,
	 6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 7, 8, 7, 9, 7,
	 9, 8,
};

static static_codebook _huff_book_line_512x17_2sub2 = {
	1, 50,
	_huff_lengthlist_line_512x17_2sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_2sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 3, 3, 3, 4, 3, 4, 4, 5, 5, 6, 6, 7, 7,
	 7, 8, 8,11, 8, 9, 9, 9,10,11,11,11, 9,10,10,11,
	11,11,11,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
};

static static_codebook _huff_book_line_512x17_2sub3 = {
	1, 128,
	_huff_lengthlist_line_512x17_2sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_3sub1[] = {
	 0, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 5, 4, 5,
	 5, 5,
};

static static_codebook _huff_book_line_512x17_3sub1 = {
	1, 18,
	_huff_lengthlist_line_512x17_3sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_3sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 2, 3, 3, 4, 3, 5, 4, 6, 4, 6, 5, 7, 6, 7,
	 6, 8, 6, 8, 7, 9, 8,10, 8,12, 9,13,10,15,10,15,
	11,14,
};

static static_codebook _huff_book_line_512x17_3sub2 = {
	1, 50,
	_huff_lengthlist_line_512x17_3sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_3sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 8, 4, 8, 4, 8, 4, 8, 5, 8, 5, 8, 6, 8,
	 4, 8, 4, 8, 5, 8, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};

static static_codebook _huff_book_line_512x17_3sub3 = {
	1, 128,
	_huff_lengthlist_line_512x17_3sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_class1[] = {
	 1, 2, 3, 6, 5, 4, 7, 7,
};

static static_codebook _huff_book_line_512x17_class1 = {
	1, 8,
	_huff_lengthlist_line_512x17_class1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_class2[] = {
	 3, 3, 3,14, 5, 4, 4,11, 8, 6, 6,10,17,12,11,17,
	 6, 5, 5,15, 5, 3, 4,11, 8, 5, 5, 8,16, 9,10,14,
	10, 8, 9,17, 8, 6, 6,13,10, 7, 7,10,16,11,13,14,
	17,17,17,17,17,16,16,16,16,15,16,16,16,16,16,16,
};

static static_codebook _huff_book_line_512x17_class2 = {
	1, 64,
	_huff_lengthlist_line_512x17_class2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_512x17_class3[] = {
	 2, 4, 6,17, 4, 5, 7,17, 8, 7,10,17,17,17,17,17,
	 3, 4, 6,15, 3, 3, 6,15, 7, 6, 9,17,17,17,17,17,
	 6, 8,10,17, 6, 6, 8,16, 9, 8,10,17,17,15,16,17,
	17,17,17,17,12,15,15,16,12,15,15,16,16,16,16,16,
};

static static_codebook _huff_book_line_512x17_class3 = {
	1, 64,
	_huff_lengthlist_line_512x17_class3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x4_class0[] = {
	 7, 7, 7,11, 6, 6, 7,11, 7, 6, 6,10,12,10,10,13,
	 7, 7, 8,11, 7, 7, 7,11, 7, 6, 7,10,11,10,10,13,
	10,10, 9,12, 9, 9, 9,11, 8, 8, 8,11,13,11,10,14,
	15,15,14,15,15,14,13,14,15,12,12,17,17,17,17,17,
	 7, 7, 6, 9, 6, 6, 6, 9, 7, 6, 6, 8,11,11,10,12,
	 7, 7, 7, 9, 7, 6, 6, 9, 7, 6, 6, 9,13,10,10,11,
	10, 9, 8,10, 9, 8, 8,10, 8, 8, 7, 9,13,12,10,11,
	17,14,14,13,15,14,12,13,17,13,12,15,17,17,14,17,
	 7, 6, 6, 7, 6, 6, 5, 7, 6, 6, 6, 6,11, 9, 9, 9,
	 7, 7, 6, 7, 7, 6, 6, 7, 6, 6, 6, 6,10, 9, 8, 9,
	10, 9, 8, 8, 9, 8, 7, 8, 8, 7, 6, 8,11,10, 9,10,
	17,17,12,15,15,15,12,14,14,14,10,12,15,13,12,13,
	11,10, 8,10,11,10, 8, 8,10, 9, 7, 7,10, 9, 9,11,
	11,11, 9,10,11,10, 8, 9,10, 8, 6, 8,10, 9, 9,11,
	14,13,10,12,12,11,10,10, 8, 7, 8,10,10,11,11,12,
	17,17,15,17,17,17,17,17,17,13,12,17,17,17,14,17,
};

static static_codebook _huff_book_line_128x4_class0 = {
	1, 256,
	_huff_lengthlist_line_128x4_class0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x4_0sub0[] = {
	 2, 2, 2, 2,
};

static static_codebook _huff_book_line_128x4_0sub0 = {
	1, 4,
	_huff_lengthlist_line_128x4_0sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x4_0sub1[] = {
	 0, 0, 0, 0, 3, 2, 3, 2, 3, 3,
};

static static_codebook _huff_book_line_128x4_0sub1 = {
	1, 10,
	_huff_lengthlist_line_128x4_0sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x4_0sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 4, 3, 4, 3,
	 4, 4, 5, 4, 5, 4, 6, 5, 6,
};

static static_codebook _huff_book_line_128x4_0sub2 = {
	1, 25,
	_huff_lengthlist_line_128x4_0sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x4_0sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 3, 5, 3, 5, 3,
	 5, 4, 6, 5, 6, 5, 7, 6, 6, 7, 7, 9, 9,11,11,16,
	11,14,10,11,11,13,16,15,15,15,15,15,15,15,15,15,
};

static static_codebook _huff_book_line_128x4_0sub3 = {
	1, 64,
	_huff_lengthlist_line_128x4_0sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4_class0[] = {
	 6, 7, 7,12, 6, 6, 7,12, 7, 6, 6,10,15,12,11,13,
	 7, 7, 8,13, 7, 7, 8,12, 7, 7, 7,11,12,12,11,13,
	10, 9, 9,11, 9, 9, 9,10,10, 8, 8,12,14,12,12,14,
	11,11,12,14,11,12,11,15,15,12,13,15,15,15,15,15,
	 6, 6, 7,10, 6, 6, 6,11, 7, 6, 6, 9,14,12,11,13,
	 7, 7, 7,10, 6, 6, 7, 9, 7, 7, 6,10,13,12,10,12,
	 9, 9, 9,11, 9, 9, 8, 9, 9, 8, 8,10,13,12,10,12,
	12,12,11,13,12,12,11,12,15,13,12,15,15,15,14,14,
	 6, 6, 6, 8, 6, 6, 5, 6, 7, 7, 6, 5,11,10, 9, 8,
	 7, 6, 6, 7, 6, 6, 5, 6, 7, 7, 6, 6,11,10, 9, 8,
	 8, 8, 8, 9, 8, 8, 7, 8, 8, 8, 6, 7,11,10, 9, 9,
	14,11,10,14,14,11,10,15,13,11, 9,11,15,12,12,11,
	11, 9, 8, 8,10, 9, 8, 9,11,10, 9, 8,12,11,12,11,
	13,10, 8, 9,11,10, 8, 9,10, 9, 8, 9,10, 8,12,12,
	15,11,10,10,13,11,10,10, 8, 8, 7,12,10, 9,11,12,
	15,12,11,15,13,11,11,15,12,14,11,13,15,15,13,13,
};

static static_codebook _huff_book_line_256x4_class0 = {
	1, 256,
	_huff_lengthlist_line_256x4_class0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4_0sub0[] = {
	 2, 2, 2, 2,
};

static static_codebook _huff_book_line_256x4_0sub0 = {
	1, 4,
	_huff_lengthlist_line_256x4_0sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4_0sub1[] = {
	 0, 0, 0, 0, 2, 2, 3, 3, 3, 3,
};

static static_codebook _huff_book_line_256x4_0sub1 = {
	1, 10,
	_huff_lengthlist_line_256x4_0sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4_0sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 3, 4, 3, 4, 3,
	 5, 3, 5, 4, 5, 4, 6, 4, 6,
};

static static_codebook _huff_book_line_256x4_0sub2 = {
	1, 25,
	_huff_lengthlist_line_256x4_0sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4_0sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 3, 5, 3, 5, 3,
	 6, 4, 7, 4, 7, 5, 7, 6, 7, 6, 7, 8,10,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,12,12,12,12,12,
};

static static_codebook _huff_book_line_256x4_0sub3 = {
	1, 64,
	_huff_lengthlist_line_256x4_0sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x7_class0[] = {
	10, 7, 8,13, 9, 6, 7,11,10, 8, 8,12,17,17,17,17,
	 7, 5, 5, 9, 6, 4, 4, 8, 8, 5, 5, 8,16,14,13,16,
	 7, 5, 5, 7, 6, 3, 3, 5, 8, 5, 4, 7,14,12,12,15,
	10, 7, 8, 9, 7, 5, 5, 6, 9, 6, 5, 5,15,12, 9,10,
};

static static_codebook _huff_book_line_128x7_class0 = {
	1, 64,
	_huff_lengthlist_line_128x7_class0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x7_class1[] = {
	 8,13,17,17, 8,11,17,17,11,13,17,17,17,17,17,17,
	 6,10,16,17, 6,10,15,17, 8,10,16,17,17,17,17,17,
	 9,13,15,17, 8,11,17,17,10,12,17,17,17,17,17,17,
	17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
	 6,11,15,17, 7,10,15,17, 8,10,17,17,17,15,17,17,
	 4, 8,13,17, 4, 7,13,17, 6, 8,15,17,16,15,17,17,
	 6,11,15,17, 6, 9,13,17, 8,10,17,17,15,17,17,17,
	16,17,17,17,12,14,15,17,13,14,15,17,17,17,17,17,
	 5,10,14,17, 5, 9,14,17, 7, 9,15,17,15,15,17,17,
	 3, 7,12,17, 3, 6,11,17, 5, 7,13,17,12,12,17,17,
	 5, 9,14,17, 3, 7,11,17, 5, 8,13,17,13,11,16,17,
	12,17,17,17, 9,14,15,17,10,11,14,17,16,14,17,17,
	 8,12,17,17, 8,12,17,17,10,12,17,17,17,17,17,17,
	 5,10,17,17, 5, 9,15,17, 7, 9,17,17,13,13,17,17,
	 7,11,17,17, 6,10,15,17, 7, 9,15,17,12,11,17,17,
	12,15,17,17,11,14,17,17,11,10,15,17,17,16,17,17,
};

static static_codebook _huff_book_line_128x7_class1 = {
	1, 256,
	_huff_lengthlist_line_128x7_class1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x7_0sub1[] = {
	 0, 3, 3, 3, 3, 3, 3, 3, 3,
};

static static_codebook _huff_book_line_128x7_0sub1 = {
	1, 9,
	_huff_lengthlist_line_128x7_0sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x7_0sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 4, 4, 4, 4,
	 5, 4, 5, 4, 5, 4, 6, 4, 6,
};

static static_codebook _huff_book_line_128x7_0sub2 = {
	1, 25,
	_huff_lengthlist_line_128x7_0sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x7_0sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 3, 5, 3, 5, 4,
	 5, 4, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
	 7, 8, 9,11,13,13,13,13,13,13,13,13,13,13,13,13,
};

static static_codebook _huff_book_line_128x7_0sub3 = {
	1, 64,
	_huff_lengthlist_line_128x7_0sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x7_1sub1[] = {
	 0, 3, 3, 2, 3, 3, 4, 3, 4,
};

static static_codebook _huff_book_line_128x7_1sub1 = {
	1, 9,
	_huff_lengthlist_line_128x7_1sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x7_1sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 3, 6, 3, 6, 3,
	 6, 3, 7, 3, 8, 4, 9, 4, 9,
};

static static_codebook _huff_book_line_128x7_1sub2 = {
	1, 25,
	_huff_lengthlist_line_128x7_1sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x7_1sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 2, 7, 3, 8, 4,
	 9, 5, 9, 8,10,11,11,12,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,13,13,13,13,
};

static static_codebook _huff_book_line_128x7_1sub3 = {
	1, 64,
	_huff_lengthlist_line_128x7_1sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_class1[] = {
	 1, 6, 3, 7, 2, 4, 5, 7,
};

static static_codebook _huff_book_line_128x11_class1 = {
	1, 8,
	_huff_lengthlist_line_128x11_class1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_class2[] = {
	 1, 6,12,16, 4,12,15,16, 9,15,16,16,16,16,16,16,
	 2, 5,11,16, 5,11,13,16, 9,13,16,16,16,16,16,16,
	 4, 8,12,16, 5, 9,12,16, 9,13,15,16,16,16,16,16,
	15,16,16,16,11,14,13,16,12,15,16,16,16,16,16,15,
};

static static_codebook _huff_book_line_128x11_class2 = {
	1, 64,
	_huff_lengthlist_line_128x11_class2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_class3[] = {
	 7, 6, 9,17, 7, 6, 8,17,12, 9,11,16,16,16,16,16,
	 5, 4, 7,16, 5, 3, 6,14, 9, 6, 8,15,16,16,16,16,
	 5, 4, 6,13, 3, 2, 4,11, 7, 4, 6,13,16,11,10,14,
	12,12,12,16, 9, 7,10,15,12, 9,11,16,16,15,15,16,
};

static static_codebook _huff_book_line_128x11_class3 = {
	1, 64,
	_huff_lengthlist_line_128x11_class3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_0sub0[] = {
	 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
	 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 6, 6, 7, 6,
	 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 8, 6, 8, 6, 8, 7,
	 8, 7, 8, 7, 8, 7, 9, 7, 9, 8, 9, 8, 9, 8,10, 8,
	10, 9,10, 9,10, 9,11, 9,11, 9,10,10,11,10,11,10,
	11,11,11,11,11,11,12,13,14,14,14,15,15,16,16,16,
	17,15,16,15,16,16,17,17,16,17,17,17,17,17,17,17,
	17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
};

static static_codebook _huff_book_line_128x11_0sub0 = {
	1, 128,
	_huff_lengthlist_line_128x11_0sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_1sub0[] = {
	 2, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5,
	 6, 5, 6, 5, 6, 5, 7, 6, 7, 6, 7, 6, 8, 6, 8, 6,
};

static static_codebook _huff_book_line_128x11_1sub0 = {
	1, 32,
	_huff_lengthlist_line_128x11_1sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_1sub1[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 5, 3, 5, 3, 6, 4, 6, 4, 7, 4, 7, 4, 7, 4, 8, 4,
	 8, 4, 9, 5, 9, 5, 9, 5, 9, 6,10, 6,10, 6,11, 7,
	10, 7,10, 8,11, 9,11, 9,11,10,11,11,12,11,11,12,
	15,15,12,14,11,14,12,14,11,14,13,14,12,14,11,14,
	11,14,12,14,11,14,11,14,13,13,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
};

static static_codebook _huff_book_line_128x11_1sub1 = {
	1, 128,
	_huff_lengthlist_line_128x11_1sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_2sub1[] = {
	 0, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4,
	 5, 5,
};

static static_codebook _huff_book_line_128x11_2sub1 = {
	1, 18,
	_huff_lengthlist_line_128x11_2sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_2sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 3, 3, 4, 4, 4, 4, 5, 4, 5, 4, 6, 5, 7,
	 5, 7, 6, 8, 6, 8, 6, 9, 7, 9, 7,10, 7, 9, 8,11,
	 8,11,
};

static static_codebook _huff_book_line_128x11_2sub2 = {
	1, 50,
	_huff_lengthlist_line_128x11_2sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_2sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 8, 3, 8, 4, 8, 4, 8, 6, 8, 5, 8, 4, 8,
	 4, 8, 6, 8, 7, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};

static static_codebook _huff_book_line_128x11_2sub3 = {
	1, 128,
	_huff_lengthlist_line_128x11_2sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_3sub1[] = {
	 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4,
	 5, 4,
};

static static_codebook _huff_book_line_128x11_3sub1 = {
	1, 18,
	_huff_lengthlist_line_128x11_3sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_3sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 3, 5, 4, 6, 4, 6, 4, 7, 4, 7, 4, 8, 4,
	 8, 4, 9, 4, 9, 4,10, 4,10, 5,10, 5,11, 5,12, 6,
	12, 6,
};

static static_codebook _huff_book_line_128x11_3sub2 = {
	1, 50,
	_huff_lengthlist_line_128x11_3sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x11_3sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 7, 1, 6, 3, 7, 3, 8, 4, 8, 5, 8, 8, 8, 9,
	 7, 8, 8, 7, 7, 7, 8, 9,10, 9, 9,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9, 9,
};

static static_codebook _huff_book_line_128x11_3sub3 = {
	1, 128,
	_huff_lengthlist_line_128x11_3sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_class1[] = {
	 1, 3, 4, 7, 2, 5, 6, 7,
};

static static_codebook _huff_book_line_128x17_class1 = {
	1, 8,
	_huff_lengthlist_line_128x17_class1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_class2[] = {
	 1, 4,10,19, 3, 8,13,19, 7,12,19,19,19,19,19,19,
	 2, 6,11,19, 8,13,19,19, 9,11,19,19,19,19,19,19,
	 6, 7,13,19, 9,13,19,19,10,13,18,18,18,18,18,18,
	18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,
};

static static_codebook _huff_book_line_128x17_class2 = {
	1, 64,
	_huff_lengthlist_line_128x17_class2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_class3[] = {
	 3, 6,10,17, 4, 8,11,20, 8,10,11,20,20,20,20,20,
	 2, 4, 8,18, 4, 6, 8,17, 7, 8,10,20,20,17,20,20,
	 3, 5, 8,17, 3, 4, 6,17, 8, 8,10,17,17,12,16,20,
	13,13,15,20,10,10,12,20,15,14,15,20,20,20,19,19,
};

static static_codebook _huff_book_line_128x17_class3 = {
	1, 64,
	_huff_lengthlist_line_128x17_class3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_0sub0[] = {
	 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
	 7, 5, 7, 5, 7, 5, 7, 5, 7, 5, 7, 5, 8, 5, 8, 5,
	 8, 5, 8, 5, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, 9, 6,
	 9, 6, 9, 7, 9, 7, 9, 7, 9, 7,10, 7,10, 8,10, 8,
	10, 8,10, 8,10, 8,11, 8,11, 8,11, 8,11, 8,11, 9,
	12, 9,12, 9,12, 9,12, 9,12,10,12,10,13,11,13,11,
	14,12,14,13,15,14,16,14,17,15,18,16,20,20,20,20,
	20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
};

static static_codebook _huff_book_line_128x17_0sub0 = {
	1, 128,
	_huff_lengthlist_line_128x17_0sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_1sub0[] = {
	 2, 5, 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 5, 5, 6, 5,
	 6, 5, 6, 5, 7, 6, 7, 6, 7, 6, 8, 6, 9, 7, 9, 7,
};

static static_codebook _huff_book_line_128x17_1sub0 = {
	1, 32,
	_huff_lengthlist_line_128x17_1sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_1sub1[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 4, 3, 5, 3, 5, 3, 6, 3, 6, 4, 6, 4, 7, 4, 7, 5,
	 8, 5, 8, 6, 9, 7, 9, 7, 9, 8,10, 9,10, 9,11,10,
	11,11,11,11,11,11,12,12,12,13,12,13,12,14,12,15,
	12,14,12,16,13,17,13,17,14,17,14,16,13,17,14,17,
	14,17,15,17,15,15,16,17,17,17,17,17,17,17,17,17,
	17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16,
};

static static_codebook _huff_book_line_128x17_1sub1 = {
	1, 128,
	_huff_lengthlist_line_128x17_1sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_2sub1[] = {
	 0, 4, 5, 4, 6, 4, 8, 3, 9, 3, 9, 2, 9, 3, 8, 4,
	 9, 4,
};

static static_codebook _huff_book_line_128x17_2sub1 = {
	1, 18,
	_huff_lengthlist_line_128x17_2sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_2sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 1, 5, 3, 5, 3, 5, 4, 7, 5,10, 7,10, 7,
	12,10,14,10,14, 9,14,11,14,14,14,13,13,13,13,13,
	13,13,
};

static static_codebook _huff_book_line_128x17_2sub2 = {
	1, 50,
	_huff_lengthlist_line_128x17_2sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_2sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6,
	 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
	 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
	 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
};

static static_codebook _huff_book_line_128x17_2sub3 = {
	1, 128,
	_huff_lengthlist_line_128x17_2sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_3sub1[] = {
	 0, 4, 4, 4, 4, 4, 4, 4, 5, 3, 5, 3, 5, 4, 6, 4,
	 6, 4,
};

static static_codebook _huff_book_line_128x17_3sub1 = {
	1, 18,
	_huff_lengthlist_line_128x17_3sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_3sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 3, 6, 3, 6, 4, 7, 4, 7, 4, 7, 4, 8, 4,
	 8, 4, 8, 4, 8, 4, 9, 4, 9, 5,10, 5,10, 7,10, 8,
	10, 8,
};

static static_codebook _huff_book_line_128x17_3sub2 = {
	1, 50,
	_huff_lengthlist_line_128x17_3sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_128x17_3sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 2, 4, 3, 4, 4, 4, 5, 4, 7, 5, 8, 5,11,
	 6,10, 6,12, 7,12, 7,12, 8,12, 8,12,10,12,12,12,
	12,12,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
};

static static_codebook _huff_book_line_128x17_3sub3 = {
	1, 128,
	_huff_lengthlist_line_128x17_3sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_class1[] = {
	 2,10, 8,14, 7,12,11,14, 1, 5, 3, 7, 4, 9, 7,13,
};

static static_codebook _huff_book_line_1024x27_class1 = {
	1, 16,
	_huff_lengthlist_line_1024x27_class1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_class2[] = {
	 1, 4, 2, 6, 3, 7, 5, 7,
};

static static_codebook _huff_book_line_1024x27_class2 = {
	1, 8,
	_huff_lengthlist_line_1024x27_class2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_class3[] = {
	 1, 5, 7,21, 5, 8, 9,21,10, 9,12,20,20,16,20,20,
	 4, 8, 9,20, 6, 8, 9,20,11,11,13,20,20,15,17,20,
	 9,11,14,20, 8,10,15,20,11,13,15,20,20,20,20,20,
	20,20,20,20,13,20,20,20,18,18,20,20,20,20,20,20,
	 3, 6, 8,20, 6, 7, 9,20,10, 9,12,20,20,20,20,20,
	 5, 7, 9,20, 6, 6, 9,20,10, 9,12,20,20,20,20,20,
	 8,10,13,20, 8, 9,12,20,11,10,12,20,20,20,20,20,
	18,20,20,20,15,17,18,20,18,17,18,20,20,20,20,20,
	 7,10,12,20, 8, 9,11,20,14,13,14,20,20,20,20,20,
	 6, 9,12,20, 7, 8,11,20,12,11,13,20,20,20,20,20,
	 9,11,15,20, 8,10,14,20,12,11,14,20,20,20,20,20,
	20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
	11,16,18,20,15,15,17,20,20,17,20,20,20,20,20,20,
	 9,14,16,20,12,12,15,20,17,15,18,20,20,20,20,20,
	16,19,18,20,15,16,20,20,17,17,20,20,20,20,20,20,
	20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
};

static static_codebook _huff_book_line_1024x27_class3 = {
	1, 256,
	_huff_lengthlist_line_1024x27_class3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_class4[] = {
	 2, 3, 7,13, 4, 4, 7,15, 8, 6, 9,17,21,16,15,21,
	 2, 5, 7,11, 5, 5, 7,14, 9, 7,10,16,17,15,16,21,
	 4, 7,10,17, 7, 7, 9,15,11, 9,11,16,21,18,15,21,
	18,21,21,21,15,17,17,19,21,19,18,20,21,21,21,20,
};

static static_codebook _huff_book_line_1024x27_class4 = {
	1, 64,
	_huff_lengthlist_line_1024x27_class4,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_0sub0[] = {
	 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
	 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 5, 7, 5, 7, 5,
	 8, 6, 8, 6, 8, 6, 9, 6, 9, 6,10, 6,10, 6,11, 6,
	11, 7,11, 7,12, 7,12, 7,12, 7,12, 7,12, 7,12, 7,
	12, 7,12, 8,13, 8,12, 8,12, 8,13, 8,13, 9,13, 9,
	13, 9,13, 9,12,10,12,10,13,10,14,11,14,12,14,13,
	14,13,14,14,15,16,15,15,15,14,15,17,21,22,22,21,
	22,22,22,22,22,22,21,21,21,21,21,21,21,21,21,21,
};

static static_codebook _huff_book_line_1024x27_0sub0 = {
	1, 128,
	_huff_lengthlist_line_1024x27_0sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_1sub0[] = {
	 2, 5, 5, 4, 5, 4, 5, 4, 5, 4, 6, 5, 6, 5, 6, 5,
	 6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6,
};

static static_codebook _huff_book_line_1024x27_1sub0 = {
	1, 32,
	_huff_lengthlist_line_1024x27_1sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_1sub1[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 8, 5, 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4,
	 9, 4, 9, 4, 9, 4, 8, 4, 8, 4, 9, 5, 9, 5, 9, 5,
	 9, 5, 9, 6,10, 6,10, 7,10, 8,11, 9,11,11,12,13,
	12,14,13,15,13,15,14,16,14,17,15,17,15,15,16,16,
	15,16,16,16,15,18,16,15,17,17,19,19,19,19,19,19,
	19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,
};

static static_codebook _huff_book_line_1024x27_1sub1 = {
	1, 128,
	_huff_lengthlist_line_1024x27_1sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_2sub0[] = {
	 1, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5,
	 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 9, 8,10, 9,10, 9,
};

static static_codebook _huff_book_line_1024x27_2sub0 = {
	1, 32,
	_huff_lengthlist_line_1024x27_2sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_2sub1[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 5, 5, 6, 5, 6, 5,
	 7, 5, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 9, 8, 9, 9,
	 9, 9,10,10,10,11, 9,12, 9,12, 9,15,10,14, 9,13,
	10,13,10,12,10,12,10,13,10,12,11,13,11,14,12,13,
	13,14,14,13,14,15,14,16,13,13,14,16,16,16,16,16,
	16,16,16,16,16,16,16,16,16,16,16,16,16,16,15,15,
};

static static_codebook _huff_book_line_1024x27_2sub1 = {
	1, 128,
	_huff_lengthlist_line_1024x27_2sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_3sub1[] = {
	 0, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5,
	 5, 5,
};

static static_codebook _huff_book_line_1024x27_3sub1 = {
	1, 18,
	_huff_lengthlist_line_1024x27_3sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_3sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 3, 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6,
	 5, 7, 5, 8, 6, 8, 6, 9, 7,10, 7,10, 8,10, 8,11,
	 9,11,
};

static static_codebook _huff_book_line_1024x27_3sub2 = {
	1, 50,
	_huff_lengthlist_line_1024x27_3sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_3sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 7, 3, 8, 3,10, 3, 8, 3, 9, 3, 8, 4, 9,
	 4, 9, 5, 9, 6,10, 6, 9, 7,11, 7,12, 9,13,10,13,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
};

static static_codebook _huff_book_line_1024x27_3sub3 = {
	1, 128,
	_huff_lengthlist_line_1024x27_3sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_4sub1[] = {
	 0, 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4,
	 5, 4,
};

static static_codebook _huff_book_line_1024x27_4sub1 = {
	1, 18,
	_huff_lengthlist_line_1024x27_4sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_4sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 2, 4, 2, 5, 3, 5, 4, 6, 6, 6, 7, 7, 8,
	 7, 8, 7, 8, 7, 9, 8, 9, 8, 9, 8,10, 8,11, 9,12,
	 9,12,
};

static static_codebook _huff_book_line_1024x27_4sub2 = {
	1, 50,
	_huff_lengthlist_line_1024x27_4sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_1024x27_4sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 2, 5, 2, 6, 3, 6, 4, 7, 4, 7, 5, 9, 5,11,
	 6,11, 6,11, 7,11, 6,11, 6,11, 9,11, 8,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,
};

static static_codebook _huff_book_line_1024x27_4sub3 = {
	1, 128,
	_huff_lengthlist_line_1024x27_4sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_class1[] = {
	 2, 6, 8, 9, 7,11,13,13, 1, 3, 5, 5, 6, 6,12,10,
};

static static_codebook _huff_book_line_2048x27_class1 = {
	1, 16,
	_huff_lengthlist_line_2048x27_class1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_class2[] = {
	 1, 2, 3, 6, 4, 7, 5, 7,
};

static static_codebook _huff_book_line_2048x27_class2 = {
	1, 8,
	_huff_lengthlist_line_2048x27_class2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_class3[] = {
	 3, 3, 6,16, 5, 5, 7,16, 9, 8,11,16,16,16,16,16,
	 5, 5, 8,16, 5, 5, 7,16, 8, 7, 9,16,16,16,16,16,
	 9, 9,12,16, 6, 8,11,16, 9,10,11,16,16,16,16,16,
	16,16,16,16,13,16,16,16,15,16,16,16,16,16,16,16,
	 5, 4, 7,16, 6, 5, 8,16, 9, 8,10,16,16,16,16,16,
	 5, 5, 7,15, 5, 4, 6,15, 7, 6, 8,16,16,16,16,16,
	 9, 9,11,15, 7, 7, 9,16, 8, 8, 9,16,16,16,16,16,
	16,16,16,16,15,15,15,16,15,15,14,16,16,16,16,16,
	 8, 8,11,16, 8, 9,10,16,11,10,14,16,16,16,16,16,
	 6, 8,10,16, 6, 7,10,16, 8, 8,11,16,14,16,16,16,
	10,11,14,16, 9, 9,11,16,10,10,11,16,16,16,16,16,
	16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
	16,16,16,16,15,16,16,16,16,16,16,16,16,16,16,16,
	12,16,15,16,12,14,16,16,16,16,16,16,16,16,16,16,
	16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
	16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
};

static static_codebook _huff_book_line_2048x27_class3 = {
	1, 256,
	_huff_lengthlist_line_2048x27_class3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_class4[] = {
	 2, 4, 7,13, 4, 5, 7,15, 8, 7,10,16,16,14,16,16,
	 2, 4, 7,16, 3, 4, 7,14, 8, 8,10,16,16,16,15,16,
	 6, 8,11,16, 7, 7, 9,16,11, 9,13,16,16,16,15,16,
	16,16,16,16,14,16,16,16,16,16,16,16,16,16,16,16,
};

static static_codebook _huff_book_line_2048x27_class4 = {
	1, 64,
	_huff_lengthlist_line_2048x27_class4,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_0sub0[] = {
	 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
	 6, 5, 7, 5, 7, 5, 7, 5, 8, 5, 8, 5, 8, 5, 9, 5,
	 9, 6,10, 6,10, 6,11, 6,11, 6,11, 6,11, 6,11, 6,
	11, 6,11, 6,12, 7,11, 7,11, 7,11, 7,11, 7,10, 7,
	11, 7,11, 7,12, 7,11, 8,11, 8,11, 8,11, 8,13, 8,
	12, 9,11, 9,11, 9,11,10,12,10,12, 9,12,10,12,11,
	14,12,16,12,12,11,14,16,17,17,17,17,17,17,17,17,
	17,17,17,17,17,17,17,17,17,17,17,17,16,16,16,16,
};

static static_codebook _huff_book_line_2048x27_0sub0 = {
	1, 128,
	_huff_lengthlist_line_2048x27_0sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_1sub0[] = {
	 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
	 5, 5, 6, 6, 6, 6, 6, 6, 7, 6, 7, 6, 7, 6, 7, 6,
};

static static_codebook _huff_book_line_2048x27_1sub0 = {
	1, 32,
	_huff_lengthlist_line_2048x27_1sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_1sub1[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 6, 5, 7, 5, 7, 4, 7, 4, 8, 4, 8, 4, 8, 4, 8, 3,
	 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 5, 9, 5, 9, 6,
	 9, 7, 9, 8, 9, 9, 9,10, 9,11, 9,14, 9,15,10,15,
	10,15,10,15,10,15,11,15,10,14,12,14,11,14,13,14,
	13,15,15,15,12,15,15,15,13,15,13,15,13,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,14,
};

static static_codebook _huff_book_line_2048x27_1sub1 = {
	1, 128,
	_huff_lengthlist_line_2048x27_1sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_2sub0[] = {
	 2, 4, 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 5, 5, 6, 5,
	 6, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
};

static static_codebook _huff_book_line_2048x27_2sub0 = {
	1, 32,
	_huff_lengthlist_line_2048x27_2sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_2sub1[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 3, 4, 3, 4, 3, 4, 4, 5, 4, 5, 5, 5, 6, 6, 6, 7,
	 6, 8, 6, 8, 6, 9, 7,10, 7,10, 7,10, 7,12, 7,12,
	 7,12, 9,12,11,12,10,12,10,12,11,12,12,12,10,12,
	10,12,10,12, 9,12,11,12,12,12,12,12,11,12,11,12,
	12,12,12,12,12,12,12,12,10,10,12,12,12,12,12,10,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
};

static static_codebook _huff_book_line_2048x27_2sub1 = {
	1, 128,
	_huff_lengthlist_line_2048x27_2sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_3sub1[] = {
	 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
	 5, 5,
};

static static_codebook _huff_book_line_2048x27_3sub1 = {
	1, 18,
	_huff_lengthlist_line_2048x27_3sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_3sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6,
	 6, 7, 6, 7, 6, 8, 6, 9, 7, 9, 7, 9, 9,11, 9,12,
	10,12,
};

static static_codebook _huff_book_line_2048x27_3sub2 = {
	1, 50,
	_huff_lengthlist_line_2048x27_3sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_3sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 6, 3, 7, 3, 7, 5, 7, 7, 7, 7, 7, 6, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};

static static_codebook _huff_book_line_2048x27_3sub3 = {
	1, 128,
	_huff_lengthlist_line_2048x27_3sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_4sub1[] = {
	 0, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 5, 4, 5, 4,
	 4, 5,
};

static static_codebook _huff_book_line_2048x27_4sub1 = {
	1, 18,
	_huff_lengthlist_line_2048x27_4sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_4sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 2, 4, 3, 4, 4, 4, 5, 5, 6, 5, 6, 5, 7,
	 6, 6, 6, 7, 7, 7, 8, 9, 9, 9,12,10,11,10,10,12,
	10,10,
};

static static_codebook _huff_book_line_2048x27_4sub2 = {
	1, 50,
	_huff_lengthlist_line_2048x27_4sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_2048x27_4sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 6, 5, 7, 5, 7, 7, 7, 7, 7, 5, 7, 5, 7,
	 5, 7, 5, 7, 7, 7, 7, 7, 4, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,
};

static static_codebook _huff_book_line_2048x27_4sub3 = {
	1, 128,
	_huff_lengthlist_line_2048x27_4sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4low_class0[] = {
	 4, 5, 6,11, 5, 5, 6,10, 7, 7, 6, 6,14,13, 9, 9,
	 6, 6, 6,10, 6, 6, 6, 9, 8, 7, 7, 9,14,12, 8,11,
	 8, 7, 7,11, 8, 8, 7,11, 9, 9, 7, 9,13,11, 9,13,
	19,19,18,19,15,16,16,19,11,11,10,13,10,10, 9,15,
	 5, 5, 6,13, 6, 6, 6,11, 8, 7, 6, 7,14,11,10,11,
	 6, 6, 6,12, 7, 6, 6,11, 8, 7, 7,11,13,11, 9,11,
	 9, 7, 6,12, 8, 7, 6,12, 9, 8, 8,11,13,10, 7,13,
	19,19,17,19,17,14,14,19,12,10, 8,12,13,10, 9,16,
	 7, 8, 7,12, 7, 7, 7,11, 8, 7, 7, 8,12,12,11,11,
	 8, 8, 7,12, 8, 7, 6,11, 8, 7, 7,10,10,11,10,11,
	 9, 8, 8,13, 9, 8, 7,12,10, 9, 7,11, 9, 8, 7,11,
	18,18,15,18,18,16,17,18,15,11,10,18,11, 9, 9,18,
	16,16,13,16,12,11,10,16,12,11, 9, 6,15,12,11,13,
	16,16,14,14,13,11,12,16,12, 9, 9,13,13,10,10,12,
	17,18,17,17,14,15,14,16,14,12,14,15,12,10,11,12,
	18,18,18,18,18,18,18,18,18,12,13,18,16,11, 9,18,
};

static static_codebook _huff_book_line_256x4low_class0 = {
	1, 256,
	_huff_lengthlist_line_256x4low_class0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4low_0sub0[] = {
	 1, 3, 2, 3,
};

static static_codebook _huff_book_line_256x4low_0sub0 = {
	1, 4,
	_huff_lengthlist_line_256x4low_0sub0,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4low_0sub1[] = {
	 0, 0, 0, 0, 2, 3, 2, 3, 3, 3,
};

static static_codebook _huff_book_line_256x4low_0sub1 = {
	1, 10,
	_huff_lengthlist_line_256x4low_0sub1,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4low_0sub2[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 4, 3, 4,
	 4, 4, 4, 4, 5, 5, 5, 6, 6,
};

static static_codebook _huff_book_line_256x4low_0sub2 = {
	1, 25,
	_huff_lengthlist_line_256x4low_0sub2,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist_line_256x4low_0sub3[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 2, 4, 3, 5, 4,
	 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 8, 6, 9,
	 7,12,11,16,13,16,12,15,13,15,12,14,12,15,15,15,
};

static static_codebook _huff_book_line_256x4low_0sub3 = {
	1, 64,
	_huff_lengthlist_line_256x4low_0sub3,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};
/*** End of inlined file: floor_books.h ***/

static static_codebook *_floor_128x4_books[]={
  &_huff_book_line_128x4_class0,
  &_huff_book_line_128x4_0sub0,
  &_huff_book_line_128x4_0sub1,
  &_huff_book_line_128x4_0sub2,
  &_huff_book_line_128x4_0sub3,
};
static static_codebook *_floor_256x4_books[]={
  &_huff_book_line_256x4_class0,
  &_huff_book_line_256x4_0sub0,
  &_huff_book_line_256x4_0sub1,
  &_huff_book_line_256x4_0sub2,
  &_huff_book_line_256x4_0sub3,
};
static static_codebook *_floor_128x7_books[]={
  &_huff_book_line_128x7_class0,
  &_huff_book_line_128x7_class1,

  &_huff_book_line_128x7_0sub1,
  &_huff_book_line_128x7_0sub2,
  &_huff_book_line_128x7_0sub3,
  &_huff_book_line_128x7_1sub1,
  &_huff_book_line_128x7_1sub2,
  &_huff_book_line_128x7_1sub3,
};
static static_codebook *_floor_256x7_books[]={
  &_huff_book_line_256x7_class0,
  &_huff_book_line_256x7_class1,

  &_huff_book_line_256x7_0sub1,
  &_huff_book_line_256x7_0sub2,
  &_huff_book_line_256x7_0sub3,
  &_huff_book_line_256x7_1sub1,
  &_huff_book_line_256x7_1sub2,
  &_huff_book_line_256x7_1sub3,
};
static static_codebook *_floor_128x11_books[]={
  &_huff_book_line_128x11_class1,
  &_huff_book_line_128x11_class2,
  &_huff_book_line_128x11_class3,

  &_huff_book_line_128x11_0sub0,
  &_huff_book_line_128x11_1sub0,
  &_huff_book_line_128x11_1sub1,
  &_huff_book_line_128x11_2sub1,
  &_huff_book_line_128x11_2sub2,
  &_huff_book_line_128x11_2sub3,
  &_huff_book_line_128x11_3sub1,
  &_huff_book_line_128x11_3sub2,
  &_huff_book_line_128x11_3sub3,
};
static static_codebook *_floor_128x17_books[]={
  &_huff_book_line_128x17_class1,
  &_huff_book_line_128x17_class2,
  &_huff_book_line_128x17_class3,

  &_huff_book_line_128x17_0sub0,
  &_huff_book_line_128x17_1sub0,
  &_huff_book_line_128x17_1sub1,
  &_huff_book_line_128x17_2sub1,
  &_huff_book_line_128x17_2sub2,
  &_huff_book_line_128x17_2sub3,
  &_huff_book_line_128x17_3sub1,
  &_huff_book_line_128x17_3sub2,
  &_huff_book_line_128x17_3sub3,
};
static static_codebook *_floor_256x4low_books[]={
  &_huff_book_line_256x4low_class0,
  &_huff_book_line_256x4low_0sub0,
  &_huff_book_line_256x4low_0sub1,
  &_huff_book_line_256x4low_0sub2,
  &_huff_book_line_256x4low_0sub3,
};
static static_codebook *_floor_1024x27_books[]={
  &_huff_book_line_1024x27_class1,
  &_huff_book_line_1024x27_class2,
  &_huff_book_line_1024x27_class3,
  &_huff_book_line_1024x27_class4,

  &_huff_book_line_1024x27_0sub0,
  &_huff_book_line_1024x27_1sub0,
  &_huff_book_line_1024x27_1sub1,
  &_huff_book_line_1024x27_2sub0,
  &_huff_book_line_1024x27_2sub1,
  &_huff_book_line_1024x27_3sub1,
  &_huff_book_line_1024x27_3sub2,
  &_huff_book_line_1024x27_3sub3,
  &_huff_book_line_1024x27_4sub1,
  &_huff_book_line_1024x27_4sub2,
  &_huff_book_line_1024x27_4sub3,
};
static static_codebook *_floor_2048x27_books[]={
  &_huff_book_line_2048x27_class1,
  &_huff_book_line_2048x27_class2,
  &_huff_book_line_2048x27_class3,
  &_huff_book_line_2048x27_class4,

  &_huff_book_line_2048x27_0sub0,
  &_huff_book_line_2048x27_1sub0,
  &_huff_book_line_2048x27_1sub1,
  &_huff_book_line_2048x27_2sub0,
  &_huff_book_line_2048x27_2sub1,
  &_huff_book_line_2048x27_3sub1,
  &_huff_book_line_2048x27_3sub2,
  &_huff_book_line_2048x27_3sub3,
  &_huff_book_line_2048x27_4sub1,
  &_huff_book_line_2048x27_4sub2,
  &_huff_book_line_2048x27_4sub3,
};

static static_codebook *_floor_512x17_books[]={
  &_huff_book_line_512x17_class1,
  &_huff_book_line_512x17_class2,
  &_huff_book_line_512x17_class3,

  &_huff_book_line_512x17_0sub0,
  &_huff_book_line_512x17_1sub0,
  &_huff_book_line_512x17_1sub1,
  &_huff_book_line_512x17_2sub1,
  &_huff_book_line_512x17_2sub2,
  &_huff_book_line_512x17_2sub3,
  &_huff_book_line_512x17_3sub1,
  &_huff_book_line_512x17_3sub2,
  &_huff_book_line_512x17_3sub3,
};

static static_codebook **_floor_books[10]={
  _floor_128x4_books,
  _floor_256x4_books,
  _floor_128x7_books,
  _floor_256x7_books,
  _floor_128x11_books,
  _floor_128x17_books,
  _floor_256x4low_books,
  _floor_1024x27_books,
  _floor_2048x27_books,
  _floor_512x17_books,
};

static vorbis_info_floor1 _floor[10]={
  {
	1,{0},{4},{2},{0},
	{{1,2,3,4}},
	4,{0,128, 33,8,16,70},

	60,30,500,   1.,18.,  -1
  },
  {
	1,{0},{4},{2},{0},
	{{1,2,3,4}},
	4,{0,256, 66,16,32,140},

	60,30,500,   1.,18.,  -1
  },
  {
	2,{0,1},{3,4},{2,2},{0,1},
	{{-1,2,3,4},{-1,5,6,7}},
	4,{0,128, 14,4,58, 2,8,28,90},

	60,30,500,   1.,18.,  -1
  },
  {
	2,{0,1},{3,4},{2,2},{0,1},
	{{-1,2,3,4},{-1,5,6,7}},
	4,{0,256, 28,8,116, 4,16,56,180},

	60,30,500,   1.,18.,  -1
  },
  {
	4,{0,1,2,3},{2,3,3,3},{0,1,2,2},{-1,0,1,2},
	{{3},{4,5},{-1,6,7,8},{-1,9,10,11}},

	2,{0,128,  8,33,  4,16,70,  2,6,12,  23,46,90},

	 60,30,500,   1,18.,  -1
  },
  {
	6,{0,1,1,2,3,3},{2,3,3,3},{0,1,2,2},{-1,0,1,2},
	{{3},{4,5},{-1,6,7,8},{-1,9,10,11}},
	2,{0,128,  12,46,  4,8,16,  23,33,70,  2,6,10,  14,19,28,  39,58,90},

	60,30,500,	1,18.,  -1
  },
  {
	1,{0},{4},{2},{0},
	{{1,2,3,4}},
	4,{0,256, 66,16,32,140},

	60,30,500,   1.,18.,  -1
  },
  {
	8,{0,1,2,2,3,3,4,4},{3,4,3,4,3},{0,1,1,2,2},{-1,0,1,2,3},
	{{4},{5,6},{7,8},{-1,9,10,11},{-1,12,13,14}},
	2,{0,1024,   93,23,372, 6,46,186,750,  14,33,65, 130,260,556,
	   3,10,18,28,  39,55,79,111,  158,220,312,  464,650,850},

	60,30,500,	3,18.,  -1 /* lowpass */
  },
  {
	8,{0,1,2,2,3,3,4,4},{3,4,3,4,3},{0,1,1,2,2},{-1,0,1,2,3},
	{{4},{5,6},{7,8},{-1,9,10,11},{-1,12,13,14}},
	2,{0,2048,   186,46,744, 12,92,372,1500,  28,66,130, 260,520,1112,
	   6,20,36,56,  78,110,158,222,  316,440,624,  928,1300,1700},

	60,30,500,	3,18.,  -1 /* lowpass */
  },
  {
	6,{0,1,1,2,3,3},{2,3,3,3},{0,1,2,2},{-1,0,1,2},
	{{3},{4,5},{-1,6,7,8},{-1,9,10,11}},
	2,{0,512,  46,186,  16,33,65,  93,130,278,
	   7,23,39,  55,79,110,  156,232,360},

	60,30,500,	1,18.,  -1 /* lowpass! */
  },

};
/*** End of inlined file: floor_all.h ***/


/*** Start of inlined file: residue_44.h ***/

/*** Start of inlined file: res_books_stereo.h ***/
static long _vq_quantlist__16c0_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16c0_s_p1_0[] = {
	 1, 4, 4, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9,10, 0, 0, 0,
	 0, 0, 0, 7, 9,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 8, 0, 0, 0, 0,
	 0, 0, 8,10,10, 0, 0, 0, 0, 0, 0, 8,10,10, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,10,10, 0, 0, 0,
	 0, 0, 0, 9, 9,12, 0, 0, 0, 0, 0, 0,10,12,11, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,10,10, 0, 0,
	 0, 0, 0, 0, 9,12,10, 0, 0, 0, 0, 0, 0,10,11,12,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 8,10,10, 0, 0,
	 0, 0, 0, 0, 8,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7,10,10, 0, 0, 0, 0, 0, 0,10,12,11, 0,
	 0, 0, 0, 0, 0, 9,10,12, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7,10,10, 0, 0, 0, 0, 0, 0,10,11,12,
	 0, 0, 0, 0, 0, 0, 9,12, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c0_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__16c0_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p1_0 = {
	_vq_quantthresh__16c0_s_p1_0,
	_vq_quantmap__16c0_s_p1_0,
	3,
	3
};

static static_codebook _16c0_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__16c0_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__16c0_s_p1_0,
	NULL,
	&_vq_auxt__16c0_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16c0_s_p2_0[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c0_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16c0_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p2_0 = {
	_vq_quantthresh__16c0_s_p2_0,
	_vq_quantmap__16c0_s_p2_0,
	5,
	5
};

static static_codebook _16c0_s_p2_0 = {
	4, 625,
	_vq_lengthlist__16c0_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16c0_s_p2_0,
	NULL,
	&_vq_auxt__16c0_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16c0_s_p3_0[] = {
	 1, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 6, 7, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 6, 6, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c0_s_p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16c0_s_p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p3_0 = {
	_vq_quantthresh__16c0_s_p3_0,
	_vq_quantmap__16c0_s_p3_0,
	5,
	5
};

static static_codebook _16c0_s_p3_0 = {
	4, 625,
	_vq_lengthlist__16c0_s_p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16c0_s_p3_0,
	NULL,
	&_vq_auxt__16c0_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16c0_s_p4_0[] = {
	 1, 3, 2, 7, 8, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 8, 8, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0,
	 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c0_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16c0_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p4_0 = {
	_vq_quantthresh__16c0_s_p4_0,
	_vq_quantmap__16c0_s_p4_0,
	9,
	9
};

static static_codebook _16c0_s_p4_0 = {
	2, 81,
	_vq_lengthlist__16c0_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16c0_s_p4_0,
	NULL,
	&_vq_auxt__16c0_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16c0_s_p5_0[] = {
	 1, 3, 3, 6, 6, 6, 6, 8, 8, 0, 0, 0, 7, 7, 7, 7,
	 8, 8, 0, 0, 0, 7, 7, 7, 7, 8, 8, 0, 0, 0, 7, 7,
	 8, 8, 9, 9, 0, 0, 0, 7, 7, 8, 8, 9, 9, 0, 0, 0,
	 8, 9, 8, 8,10,10, 0, 0, 0, 8, 8, 8, 8,10,10, 0,
	 0, 0,10,10, 9, 9,10,10, 0, 0, 0, 0, 0, 9, 9,10,
	10,
};

static float _vq_quantthresh__16c0_s_p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16c0_s_p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p5_0 = {
	_vq_quantthresh__16c0_s_p5_0,
	_vq_quantmap__16c0_s_p5_0,
	9,
	9
};

static static_codebook _16c0_s_p5_0 = {
	2, 81,
	_vq_lengthlist__16c0_s_p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16c0_s_p5_0,
	NULL,
	&_vq_auxt__16c0_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p6_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__16c0_s_p6_0[] = {
	 1, 3, 4, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,11,
	11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,11,
	11,11, 0, 0, 0, 6, 6, 8, 8, 9, 9, 9, 9,10,10,11,
	11,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,10,
	11,11,12,12,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,
	10,11,11,12,12,12,13, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,10,10,10,
	10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,
	10,10,11,11,12,12,13,13,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,11,11,12,12,13,13,13,14, 0, 0, 0, 0, 0,
	10,10,10,11,11,11,12,12,13,13,13,14, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,12,12,13,13,14,14, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,12,13,13,13,13,14,14, 0, 0,
	 0, 0, 0, 0, 0,11,11,12,12,12,13,13,14,15,14, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,13,14,14,15,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,13,13,14,13,14,
	14,
};

static float _vq_quantthresh__16c0_s_p6_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__16c0_s_p6_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p6_0 = {
	_vq_quantthresh__16c0_s_p6_0,
	_vq_quantmap__16c0_s_p6_0,
	17,
	17
};

static static_codebook _16c0_s_p6_0 = {
	2, 289,
	_vq_lengthlist__16c0_s_p6_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__16c0_s_p6_0,
	NULL,
	&_vq_auxt__16c0_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16c0_s_p7_0[] = {
	 1, 4, 4, 6, 6, 6, 7, 6, 6, 4, 7, 7,11,10,10,11,
	11,10, 4, 7, 7,10,10,10,11,10,10, 6,10,10,11,11,
	11,11,11,10, 6, 9, 9,11,12,12,11, 9, 9, 6, 9,10,
	11,12,12,11, 9,10, 7,11,11,11,11,11,12,13,12, 6,
	 9,10,11,10,10,12,13,13, 6,10, 9,11,10,10,11,12,
	13,
};

static float _vq_quantthresh__16c0_s_p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__16c0_s_p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p7_0 = {
	_vq_quantthresh__16c0_s_p7_0,
	_vq_quantmap__16c0_s_p7_0,
	3,
	3
};

static static_codebook _16c0_s_p7_0 = {
	4, 81,
	_vq_lengthlist__16c0_s_p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__16c0_s_p7_0,
	NULL,
	&_vq_auxt__16c0_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16c0_s_p7_1[] = {
	 1, 3, 4, 6, 6, 7, 7, 8, 8, 8, 8,10,10,10, 7, 7,
	 8, 8, 8, 9, 9, 9,10,10,10, 6, 7, 8, 8, 8, 8, 9,
	 8,10,10,10, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10, 7,
	 7, 8, 8, 9, 9, 8, 9,10,10,10, 8, 8, 9, 9, 9, 9,
	 9, 9,11,11,11, 8, 8, 9, 9, 9, 9, 9,10,10,11,11,
	 9, 9, 9, 9, 9, 9, 9,10,11,11,11,10,11, 9, 9, 9,
	 9,10, 9,11,11,11,10,11,10,10, 9, 9,10,10,11,11,
	11,11,11, 9, 9, 9, 9,10,10,
};

static float _vq_quantthresh__16c0_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__16c0_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p7_1 = {
	_vq_quantthresh__16c0_s_p7_1,
	_vq_quantmap__16c0_s_p7_1,
	11,
	11
};

static static_codebook _16c0_s_p7_1 = {
	2, 121,
	_vq_lengthlist__16c0_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__16c0_s_p7_1,
	NULL,
	&_vq_auxt__16c0_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16c0_s_p8_0[] = {
	 1, 4, 4, 7, 7, 7, 7, 7, 6, 8, 8,10,10, 6, 5, 6,
	 8, 8, 8, 8, 8, 8, 8, 9,10,10, 7, 6, 6, 8, 8, 8,
	 8, 8, 8, 8, 8,10,10, 0, 8, 8, 8, 8, 9, 8, 9, 9,
	 9,10,10,10, 0, 9, 8, 8, 8, 9, 9, 8, 8, 9, 9,10,
	10, 0,12,11, 8, 8, 9, 9, 9, 9,10,10,11,10, 0,12,
	13, 8, 8, 9,10, 9, 9,11,11,11,12, 0, 0, 0, 8, 8,
	 8, 8,10, 9,12,13,12,14, 0, 0, 0, 8, 8, 8, 9,10,
	10,12,12,13,14, 0, 0, 0,13,13, 9, 9,11,11, 0, 0,
	14, 0, 0, 0, 0,14,14,10,10,12,11,12,14,14,14, 0,
	 0, 0, 0, 0,11,11,13,13,14,13,14,14, 0, 0, 0, 0,
	 0,12,13,13,12,13,14,14,14,
};

static float _vq_quantthresh__16c0_s_p8_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__16c0_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p8_0 = {
	_vq_quantthresh__16c0_s_p8_0,
	_vq_quantmap__16c0_s_p8_0,
	13,
	13
};

static static_codebook _16c0_s_p8_0 = {
	2, 169,
	_vq_lengthlist__16c0_s_p8_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__16c0_s_p8_0,
	NULL,
	&_vq_auxt__16c0_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p8_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16c0_s_p8_1[] = {
	 1, 4, 3, 5, 5, 7, 7, 7, 6, 6, 7, 7, 7, 5, 5, 7,
	 7, 7, 6, 6, 7, 7, 7, 6, 6,
};

static float _vq_quantthresh__16c0_s_p8_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16c0_s_p8_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p8_1 = {
	_vq_quantthresh__16c0_s_p8_1,
	_vq_quantmap__16c0_s_p8_1,
	5,
	5
};

static static_codebook _16c0_s_p8_1 = {
	2, 25,
	_vq_lengthlist__16c0_s_p8_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16c0_s_p8_1,
	NULL,
	&_vq_auxt__16c0_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p9_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16c0_s_p9_0[] = {
	 1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__16c0_s_p9_0[] = {
	-157.5, 157.5,
};

static long _vq_quantmap__16c0_s_p9_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p9_0 = {
	_vq_quantthresh__16c0_s_p9_0,
	_vq_quantmap__16c0_s_p9_0,
	3,
	3
};

static static_codebook _16c0_s_p9_0 = {
	4, 81,
	_vq_lengthlist__16c0_s_p9_0,
	1, -518803456, 1628680192, 2, 0,
	_vq_quantlist__16c0_s_p9_0,
	NULL,
	&_vq_auxt__16c0_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__16c0_s_p9_1[] = {
	 1, 5, 5, 5, 5, 9,11,11,10,10,10,10,10,10,10, 7,
	 6, 6, 6, 6,10,10,10,10,10,10,10,10,10,10, 7, 6,
	 6, 6, 6,10, 9,10,10,10,10,10,10,10,10,10, 7, 7,
	 8, 9,10,10,10,10,10,10,10,10,10,10,10, 8, 7,10,
	10,10, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__16c0_s_p9_1[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__16c0_s_p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p9_1 = {
	_vq_quantthresh__16c0_s_p9_1,
	_vq_quantmap__16c0_s_p9_1,
	15,
	15
};

static static_codebook _16c0_s_p9_1 = {
	2, 225,
	_vq_lengthlist__16c0_s_p9_1,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__16c0_s_p9_1,
	NULL,
	&_vq_auxt__16c0_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__16c0_s_p9_2[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__16c0_s_p9_2[] = {
	 1, 5, 5, 7, 8, 8, 7, 9, 9, 9,12,12,11,12,12,10,
	10,11,12,12,12,11,12,12, 8, 9, 8, 7, 9,10,10,11,
	11,10,11,12,10,12,10,12,12,12,11,12,11, 9, 8, 8,
	 9,10, 9, 8, 9,10,12,12,11,11,12,11,10,11,12,11,
	12,12, 8, 9, 9, 9,10,11,12,11,12,11,11,11,11,12,
	12,11,11,12,12,11,11, 9, 9, 8, 9, 9,11, 9, 9,10,
	 9,11,11,11,11,12,11,11,10,12,12,12, 9,12,11,10,
	11,11,11,11,12,12,12,11,11,11,12,10,12,12,12,10,
	10, 9,10, 9,10,10, 9, 9, 9,10,10,12,10,11,11, 9,
	11,11,10,11,11,11,10,10,10, 9, 9,10,10, 9, 9,10,
	11,11,10,11,10,11,10,11,11,10,11,11,11,10, 9,10,
	10, 9,10, 9, 9,11, 9, 9,11,10,10,11,11,10,10,11,
	10,11, 8, 9,11,11,10, 9,10,11,11,10,11,11,10,10,
	10,11,10, 9,10,10,11, 9,10,10, 9,11,10,10,10,10,
	11,10,11,11, 9,11,10,11,10,10,11,11,10,10,10, 9,
	10,10,11,11,11, 9,10,10,10,10,10,11,10,10,10, 9,
	10,10,11,10,10,10,10,10, 9,10,11,10,10,10,10,11,
	11,11,10,10,10,10,10,11,10,11,10,11,10,10,10, 9,
	11,11,10,10,10,11,11,10,10,10,10,10,10,10,10,11,
	11, 9,10,10,10,11,10,11,10,10,10,11, 9,10,11,10,
	11,10,10, 9,10,10,10,11,10,11,10,10,10,10,10,11,
	11,10,11,11,10,10,11,11,10, 9, 9,10,10,10,10,10,
	 9,11, 9,10,10,10,11,11,10,10,10,10,11,11,11,10,
	 9, 9,10,10,11,10,10,10,10,10,11,11,11,10,10,10,
	11,11,11, 9,10,10,10,10, 9,10, 9,10,11,10,11,10,
	10,11,11,10,11,11,11,11,11,10,11,10,10,10, 9,11,
	11,10,11,11,11,11,11,11,11,11,11,10,11,10,10,10,
	10,11,10,10,11, 9,10,10,10,
};

static float _vq_quantthresh__16c0_s_p9_2[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__16c0_s_p9_2[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__16c0_s_p9_2 = {
	_vq_quantthresh__16c0_s_p9_2,
	_vq_quantmap__16c0_s_p9_2,
	21,
	21
};

static static_codebook _16c0_s_p9_2 = {
	2, 441,
	_vq_lengthlist__16c0_s_p9_2,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__16c0_s_p9_2,
	NULL,
	&_vq_auxt__16c0_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__16c0_s_single[] = {
	 3, 4,19, 7, 9, 7, 8,11, 9,12, 4, 1,19, 6, 7, 7,
	 8,10,11,13,18,18,18,18,18,18,18,18,18,18, 8, 6,
	18, 8, 9, 9,11,12,14,18, 9, 6,18, 9, 7, 8, 9,11,
	12,18, 7, 6,18, 8, 7, 7, 7, 9,11,17, 8, 8,18, 9,
	 7, 6, 6, 8,11,17,10,10,18,12, 9, 8, 7, 9,12,18,
	13,15,18,15,13,11,10,11,15,18,14,18,18,18,18,18,
	16,16,18,18,
};

static static_codebook _huff_book__16c0_s_single = {
	2, 100,
	_huff_lengthlist__16c0_s_single,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__16c1_s_long[] = {
	 2, 5,20, 7,10, 7, 8,10,11,11, 4, 2,20, 5, 8, 6,
	 7, 9,10,10,20,20,20,20,19,19,19,19,19,19, 7, 5,
	19, 6,10, 7, 9,11,13,17,11, 8,19,10, 7, 7, 8,10,
	11,15, 7, 5,19, 7, 7, 5, 6, 9,11,16, 7, 6,19, 8,
	 7, 6, 6, 7, 9,13, 9, 9,19,11, 9, 8, 6, 7, 8,13,
	12,14,19,16,13,10, 9, 8, 9,13,14,17,19,18,18,17,
	12,11,11,13,
};

static static_codebook _huff_book__16c1_s_long = {
	2, 100,
	_huff_lengthlist__16c1_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16c1_s_p1_0[] = {
	 1, 5, 5, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 7, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 8, 0, 0, 0, 0, 0, 0, 7, 9, 8, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 7, 0, 0, 0, 0,
	 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 9, 9,11, 0, 0, 0, 0, 0, 0, 9,11,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8,11, 9, 0, 0, 0, 0, 0, 0, 9,10,11,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 8, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,11,10, 0,
	 0, 0, 0, 0, 0, 8, 9,11, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,11,
	 0, 0, 0, 0, 0, 0, 9,11, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c1_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__16c1_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p1_0 = {
	_vq_quantthresh__16c1_s_p1_0,
	_vq_quantmap__16c1_s_p1_0,
	3,
	3
};

static static_codebook _16c1_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__16c1_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__16c1_s_p1_0,
	NULL,
	&_vq_auxt__16c1_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16c1_s_p2_0[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c1_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16c1_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p2_0 = {
	_vq_quantthresh__16c1_s_p2_0,
	_vq_quantmap__16c1_s_p2_0,
	5,
	5
};

static static_codebook _16c1_s_p2_0 = {
	4, 625,
	_vq_lengthlist__16c1_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16c1_s_p2_0,
	NULL,
	&_vq_auxt__16c1_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16c1_s_p3_0[] = {
	 1, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 7, 7, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 5, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 7, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c1_s_p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16c1_s_p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p3_0 = {
	_vq_quantthresh__16c1_s_p3_0,
	_vq_quantmap__16c1_s_p3_0,
	5,
	5
};

static static_codebook _16c1_s_p3_0 = {
	4, 625,
	_vq_lengthlist__16c1_s_p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16c1_s_p3_0,
	NULL,
	&_vq_auxt__16c1_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16c1_s_p4_0[] = {
	 1, 2, 3, 7, 7, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 8, 8, 0, 0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0,
	 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c1_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16c1_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p4_0 = {
	_vq_quantthresh__16c1_s_p4_0,
	_vq_quantmap__16c1_s_p4_0,
	9,
	9
};

static static_codebook _16c1_s_p4_0 = {
	2, 81,
	_vq_lengthlist__16c1_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16c1_s_p4_0,
	NULL,
	&_vq_auxt__16c1_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16c1_s_p5_0[] = {
	 1, 3, 3, 5, 5, 6, 6, 8, 8, 0, 0, 0, 7, 7, 7, 7,
	 9, 9, 0, 0, 0, 7, 7, 7, 7, 9, 9, 0, 0, 0, 8, 8,
	 8, 8, 9, 9, 0, 0, 0, 8, 8, 8, 8,10,10, 0, 0, 0,
	 9, 9, 8, 8,10,10, 0, 0, 0, 9, 9, 8, 8,10,10, 0,
	 0, 0,10,10, 9, 9,10,10, 0, 0, 0, 0, 0, 9, 9,10,
	10,
};

static float _vq_quantthresh__16c1_s_p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16c1_s_p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p5_0 = {
	_vq_quantthresh__16c1_s_p5_0,
	_vq_quantmap__16c1_s_p5_0,
	9,
	9
};

static static_codebook _16c1_s_p5_0 = {
	2, 81,
	_vq_lengthlist__16c1_s_p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16c1_s_p5_0,
	NULL,
	&_vq_auxt__16c1_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p6_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__16c1_s_p6_0[] = {
	 1, 3, 3, 6, 6, 8, 8, 9, 9, 9, 9,10,10,11,11,12,
	12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,11,
	12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,
	11,12,12, 0, 0, 0, 8, 8, 8, 9,10, 9,10,10,10,10,
	11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,10,11,
	11,11,12,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,10,
	11,11,12,12,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,
	10,11,11,12,12,13,13, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,
	10,10,11,11,12,12,12,12,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,11,11,12,12,12,12,13,13, 0, 0, 0, 0, 0,
	10,10,11,10,11,11,12,12,13,13,13,13, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,12,12,13,13,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,12,12,12,13,13,14,14, 0, 0,
	 0, 0, 0, 0, 0,11,11,12,12,12,12,13,13,14,14, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,13,13,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,13,13,13,13,14,
	14,
};

static float _vq_quantthresh__16c1_s_p6_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__16c1_s_p6_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p6_0 = {
	_vq_quantthresh__16c1_s_p6_0,
	_vq_quantmap__16c1_s_p6_0,
	17,
	17
};

static static_codebook _16c1_s_p6_0 = {
	2, 289,
	_vq_lengthlist__16c1_s_p6_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__16c1_s_p6_0,
	NULL,
	&_vq_auxt__16c1_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16c1_s_p7_0[] = {
	 1, 4, 4, 6, 6, 6, 7, 6, 6, 4, 7, 7,10, 9,10,10,
	10, 9, 4, 7, 7,10,10,10,11,10,10, 6,10,10,11,11,
	11,11,10,10, 6,10, 9,11,11,11,11,10,10, 6,10,10,
	11,11,11,11,10,10, 7,11,11,11,11,11,12,12,11, 6,
	10,10,11,10,10,11,11,11, 6,10,10,10,11,10,11,11,
	11,
};

static float _vq_quantthresh__16c1_s_p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__16c1_s_p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p7_0 = {
	_vq_quantthresh__16c1_s_p7_0,
	_vq_quantmap__16c1_s_p7_0,
	3,
	3
};

static static_codebook _16c1_s_p7_0 = {
	4, 81,
	_vq_lengthlist__16c1_s_p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__16c1_s_p7_0,
	NULL,
	&_vq_auxt__16c1_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16c1_s_p7_1[] = {
	 2, 3, 3, 5, 6, 7, 7, 7, 7, 8, 8,10,10,10, 6, 6,
	 7, 7, 8, 8, 8, 8,10,10,10, 6, 6, 7, 7, 8, 8, 8,
	 8,10,10,10, 7, 7, 7, 7, 8, 8, 8, 8,10,10,10, 7,
	 7, 7, 7, 8, 8, 8, 8,10,10,10, 7, 7, 8, 8, 8, 8,
	 8, 8,10,10,10, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 9, 9,10,10,10,10,10, 8, 8, 8,
	 8, 9, 9,10,10,10,10,10, 9, 9, 8, 8, 9, 9,10,10,
	10,10,10, 8, 8, 8, 8, 9, 9,
};

static float _vq_quantthresh__16c1_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__16c1_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p7_1 = {
	_vq_quantthresh__16c1_s_p7_1,
	_vq_quantmap__16c1_s_p7_1,
	11,
	11
};

static static_codebook _16c1_s_p7_1 = {
	2, 121,
	_vq_lengthlist__16c1_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__16c1_s_p7_1,
	NULL,
	&_vq_auxt__16c1_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16c1_s_p8_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 6, 5, 5,
	 7, 8, 8, 9, 8, 8, 9, 9,10,11, 6, 5, 5, 8, 8, 9,
	 9, 8, 8, 9,10,10,11, 0, 8, 8, 8, 9, 9, 9, 9, 9,
	10,10,11,11, 0, 9, 9, 9, 8, 9, 9, 9, 9,10,10,11,
	11, 0,13,13, 9, 9,10,10,10,10,11,11,12,12, 0,14,
	13, 9, 9,10,10,10,10,11,11,12,12, 0, 0, 0,10,10,
	 9, 9,11,11,12,12,13,12, 0, 0, 0,10,10, 9, 9,10,
	10,12,12,13,13, 0, 0, 0,13,14,11,10,11,11,12,12,
	13,14, 0, 0, 0,14,14,10,10,11,11,12,12,13,13, 0,
	 0, 0, 0, 0,12,12,12,12,13,13,14,15, 0, 0, 0, 0,
	 0,12,12,12,12,13,13,14,15,
};

static float _vq_quantthresh__16c1_s_p8_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__16c1_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p8_0 = {
	_vq_quantthresh__16c1_s_p8_0,
	_vq_quantmap__16c1_s_p8_0,
	13,
	13
};

static static_codebook _16c1_s_p8_0 = {
	2, 169,
	_vq_lengthlist__16c1_s_p8_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__16c1_s_p8_0,
	NULL,
	&_vq_auxt__16c1_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p8_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16c1_s_p8_1[] = {
	 2, 3, 3, 5, 5, 6, 6, 6, 5, 5, 6, 6, 6, 5, 5, 6,
	 6, 6, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__16c1_s_p8_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16c1_s_p8_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p8_1 = {
	_vq_quantthresh__16c1_s_p8_1,
	_vq_quantmap__16c1_s_p8_1,
	5,
	5
};

static static_codebook _16c1_s_p8_1 = {
	2, 25,
	_vq_lengthlist__16c1_s_p8_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16c1_s_p8_1,
	NULL,
	&_vq_auxt__16c1_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16c1_s_p9_0[] = {
	 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__16c1_s_p9_0[] = {
	-1732.5, -1417.5, -1102.5, -787.5, -472.5, -157.5, 157.5, 472.5,
	787.5, 1102.5, 1417.5, 1732.5,
};

static long _vq_quantmap__16c1_s_p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p9_0 = {
	_vq_quantthresh__16c1_s_p9_0,
	_vq_quantmap__16c1_s_p9_0,
	13,
	13
};

static static_codebook _16c1_s_p9_0 = {
	2, 169,
	_vq_lengthlist__16c1_s_p9_0,
	1, -513964032, 1628680192, 4, 0,
	_vq_quantlist__16c1_s_p9_0,
	NULL,
	&_vq_auxt__16c1_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__16c1_s_p9_1[] = {
	 1, 4, 4, 4, 4, 8, 8,12,13,14,14,14,14,14,14, 6,
	 6, 6, 6, 6,10, 9,14,14,14,14,14,14,14,14, 7, 6,
	 5, 6, 6,10, 9,12,13,13,13,13,13,13,13,13, 7, 7,
	 9, 9,11,11,12,13,13,13,13,13,13,13,13, 7, 7, 8,
	 8,11,12,13,13,13,13,13,13,13,13,13,12,12,10,10,
	13,12,13,13,13,13,13,13,13,13,13,12,12,10,10,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,12,13,12,
	13,13,13,13,13,13,13,13,13,13,13,13,12,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,12,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,12,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,
};

static float _vq_quantthresh__16c1_s_p9_1[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__16c1_s_p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p9_1 = {
	_vq_quantthresh__16c1_s_p9_1,
	_vq_quantmap__16c1_s_p9_1,
	15,
	15
};

static static_codebook _16c1_s_p9_1 = {
	2, 225,
	_vq_lengthlist__16c1_s_p9_1,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__16c1_s_p9_1,
	NULL,
	&_vq_auxt__16c1_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__16c1_s_p9_2[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__16c1_s_p9_2[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 7, 8, 8, 9, 9, 9, 9,10,
	10,10, 9,10,10,11,12,12, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,10,10,10,11,11,10,12,11,11,13,11, 7, 7, 8,
	 8, 8, 8, 9, 9, 9,10,10,10,10, 9,10,10,11,11,12,
	11,11, 8, 8, 8, 8, 9, 9,10,10,10,10,11,11,11,11,
	11,11,11,12,11,12,12, 8, 8, 9, 9, 9, 9, 9,10,10,
	10,10,10,10,11,11,11,11,11,11,12,11, 9, 9, 9, 9,
	10,10,10,10,11,10,11,11,11,11,11,11,12,12,12,12,
	11, 9, 9, 9, 9,10,10,10,10,11,11,11,11,11,11,11,
	11,11,12,12,12,13, 9,10,10, 9,11,10,10,10,10,11,
	11,11,11,11,10,11,12,11,12,12,11,12,11,10, 9,10,
	10,11,10,11,11,11,11,11,11,11,11,11,12,12,11,12,
	12,12,10,10,10,11,10,11,11,11,11,11,11,11,11,11,
	11,11,12,13,12,12,11, 9,10,10,11,11,10,11,11,11,
	12,11,11,11,11,11,12,12,13,13,12,13,10,10,12,10,
	11,11,11,11,11,11,11,11,11,12,12,11,13,12,12,12,
	12,13,12,11,11,11,11,11,11,12,11,12,11,11,11,11,
	12,12,13,12,11,12,12,11,11,11,11,11,12,11,11,11,
	11,12,11,11,12,11,12,13,13,12,12,12,12,11,11,11,
	11,11,12,11,11,12,11,12,11,11,11,11,13,12,12,12,
	12,13,11,11,11,12,12,11,11,11,12,11,12,12,12,11,
	12,13,12,11,11,12,12,11,12,11,11,11,12,12,11,12,
	11,11,11,12,12,12,12,13,12,13,12,12,12,12,11,11,
	12,11,11,11,11,11,11,12,12,12,13,12,11,13,13,12,
	12,11,12,10,11,11,11,11,12,11,12,12,11,12,12,13,
	12,12,13,12,12,12,12,12,11,12,12,12,11,12,11,11,
	11,12,13,12,13,13,13,13,13,12,13,13,12,12,13,11,
	11,11,11,11,12,11,11,12,11,
};

static float _vq_quantthresh__16c1_s_p9_2[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__16c1_s_p9_2[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__16c1_s_p9_2 = {
	_vq_quantthresh__16c1_s_p9_2,
	_vq_quantmap__16c1_s_p9_2,
	21,
	21
};

static static_codebook _16c1_s_p9_2 = {
	2, 441,
	_vq_lengthlist__16c1_s_p9_2,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__16c1_s_p9_2,
	NULL,
	&_vq_auxt__16c1_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__16c1_s_short[] = {
	 5, 6,17, 8,12, 9,10,10,12,13, 5, 2,17, 4, 9, 5,
	 7, 8,11,13,16,16,16,16,16,16,16,16,16,16, 6, 4,
	16, 5,10, 5, 7,10,14,16,13, 9,16,11, 8, 7, 8, 9,
	13,16, 7, 4,16, 5, 7, 4, 6, 8,11,13, 8, 6,16, 7,
	 8, 5, 5, 7, 9,13, 9, 8,16, 9, 8, 6, 6, 7, 9,13,
	11,11,16,10,10, 7, 7, 7, 9,13,13,13,16,13,13, 9,
	 9, 9,10,13,
};

static static_codebook _huff_book__16c1_s_short = {
	2, 100,
	_huff_lengthlist__16c1_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__16c2_s_long[] = {
	 4, 7, 9, 9, 9, 8, 9,10,15,19, 5, 4, 5, 6, 7, 7,
	 8, 9,14,16, 6, 5, 4, 5, 6, 7, 8,10,12,19, 7, 6,
	 5, 4, 5, 6, 7, 9,11,18, 8, 7, 6, 5, 5, 5, 7, 9,
	10,17, 8, 7, 7, 5, 5, 5, 6, 7,12,18, 8, 8, 8, 7,
	 7, 5, 5, 7,12,18, 8, 9,10, 9, 9, 7, 6, 7,12,17,
	14,18,16,16,15,12,11,10,12,18,15,17,18,18,18,15,
	14,14,16,18,
};

static static_codebook _huff_book__16c2_s_long = {
	2, 100,
	_huff_lengthlist__16c2_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16c2_s_p1_0[] = {
	 1, 3, 3, 0, 0, 0, 0, 0, 0, 4, 5, 5, 0, 0, 0, 0,
	 0, 0, 4, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c2_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__16c2_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p1_0 = {
	_vq_quantthresh__16c2_s_p1_0,
	_vq_quantmap__16c2_s_p1_0,
	3,
	3
};

static static_codebook _16c2_s_p1_0 = {
	4, 81,
	_vq_lengthlist__16c2_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__16c2_s_p1_0,
	NULL,
	&_vq_auxt__16c2_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16c2_s_p2_0[] = {
	 2, 4, 3, 7, 7, 0, 0, 0, 7, 8, 0, 0, 0, 8, 8, 0,
	 0, 0, 8, 8, 0, 0, 0, 8, 8, 4, 5, 4, 8, 8, 0, 0,
	 0, 8, 8, 0, 0, 0, 8, 8, 0, 0, 0, 9, 9, 0, 0, 0,
	 9, 9, 4, 4, 5, 8, 8, 0, 0, 0, 8, 8, 0, 0, 0, 8,
	 8, 0, 0, 0, 9, 9, 0, 0, 0, 9, 9, 7, 8, 8,10,10,
	 0, 0, 0,12,11, 0, 0, 0,11,11, 0, 0, 0,14,13, 0,
	 0, 0,14,13, 7, 8, 8, 9,10, 0, 0, 0,11,12, 0, 0,
	 0,11,11, 0, 0, 0,14,14, 0, 0, 0,13,14, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8,11,11, 0, 0, 0,
	11,11, 0, 0, 0,12,11, 0, 0, 0,12,12, 0, 0, 0,13,
	13, 8, 8, 8,11,11, 0, 0, 0,11,11, 0, 0, 0,11,12,
	 0, 0, 0,12,13, 0, 0, 0,13,13, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 8, 8, 8,12,11, 0, 0, 0,12,11, 0,
	 0, 0,11,11, 0, 0, 0,13,13, 0, 0, 0,13,12, 8, 8,
	 8,11,12, 0, 0, 0,11,12, 0, 0, 0,11,11, 0, 0, 0,
	13,13, 0, 0, 0,12,13, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 8, 9, 9,14,13, 0, 0, 0,13,12, 0, 0, 0,13,
	13, 0, 0, 0,13,12, 0, 0, 0,13,13, 8, 9, 9,13,14,
	 0, 0, 0,12,13, 0, 0, 0,13,13, 0, 0, 0,12,13, 0,
	 0, 0,13,13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8,
	 9, 9,14,13, 0, 0, 0,13,13, 0, 0, 0,13,12, 0, 0,
	 0,13,13, 0, 0, 0,13,12, 8, 9, 9,14,14, 0, 0, 0,
	13,13, 0, 0, 0,12,13, 0, 0, 0,13,13, 0, 0, 0,12,
	13,
};

static float _vq_quantthresh__16c2_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16c2_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p2_0 = {
	_vq_quantthresh__16c2_s_p2_0,
	_vq_quantmap__16c2_s_p2_0,
	5,
	5
};

static static_codebook _16c2_s_p2_0 = {
	4, 625,
	_vq_lengthlist__16c2_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16c2_s_p2_0,
	NULL,
	&_vq_auxt__16c2_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16c2_s_p3_0[] = {
	 1, 3, 3, 6, 6, 7, 7, 8, 8, 0, 0, 0, 6, 6, 7, 7,
	 9, 9, 0, 0, 0, 6, 6, 7, 7, 9, 9, 0, 0, 0, 7, 7,
	 8, 8,10,10, 0, 0, 0, 7, 7, 8, 8,10,10, 0, 0, 0,
	 7, 7, 9, 9,10,10, 0, 0, 0, 7, 7, 9, 9,10,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c2_s_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16c2_s_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p3_0 = {
	_vq_quantthresh__16c2_s_p3_0,
	_vq_quantmap__16c2_s_p3_0,
	9,
	9
};

static static_codebook _16c2_s_p3_0 = {
	2, 81,
	_vq_lengthlist__16c2_s_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16c2_s_p3_0,
	NULL,
	&_vq_auxt__16c2_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p4_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__16c2_s_p4_0[] = {
	 2, 3, 3, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9,10,
	10, 0, 0, 0, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,
	11,11, 0, 0, 0, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,
	10,10,11, 0, 0, 0, 6, 6, 8, 8, 8, 8, 9, 9,10,10,
	10,11,11,11, 0, 0, 0, 6, 6, 8, 8, 9, 9, 9, 9,10,
	10,11,11,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,
	10,10,11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9,
	 9,10,10,11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,
	10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 8, 8, 9,
	 9,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__16c2_s_p4_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__16c2_s_p4_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p4_0 = {
	_vq_quantthresh__16c2_s_p4_0,
	_vq_quantmap__16c2_s_p4_0,
	17,
	17
};

static static_codebook _16c2_s_p4_0 = {
	2, 289,
	_vq_lengthlist__16c2_s_p4_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__16c2_s_p4_0,
	NULL,
	&_vq_auxt__16c2_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p5_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16c2_s_p5_0[] = {
	 1, 4, 4, 5, 7, 7, 6, 7, 7, 4, 6, 6,10,10,10,10,
	10,10, 4, 7, 6,10,10,10,10,10,10, 5, 9, 9, 9,12,
	11,10,11,12, 7,10,10,12,12,12,12,12,12, 7,10,10,
	11,12,12,12,12,13, 6,10,10,10,12,12,10,12,12, 7,
	10,10,11,13,12,12,12,12, 7,10,10,11,12,12,12,12,
	12,
};

static float _vq_quantthresh__16c2_s_p5_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__16c2_s_p5_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p5_0 = {
	_vq_quantthresh__16c2_s_p5_0,
	_vq_quantmap__16c2_s_p5_0,
	3,
	3
};

static static_codebook _16c2_s_p5_0 = {
	4, 81,
	_vq_lengthlist__16c2_s_p5_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__16c2_s_p5_0,
	NULL,
	&_vq_auxt__16c2_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p5_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16c2_s_p5_1[] = {
	 2, 3, 3, 6, 6, 7, 7, 7, 7, 8, 8,11,11,11, 6, 6,
	 7, 7, 8, 8, 8, 8,11,11,11, 6, 6, 7, 7, 8, 8, 8,
	 8,11,11,11, 6, 6, 8, 8, 8, 8, 9, 9,11,11,11, 6,
	 6, 8, 8, 8, 8, 9, 9,11,11,11, 7, 7, 8, 8, 8, 8,
	 8, 8,11,11,11, 7, 7, 8, 8, 8, 8, 8, 9,11,11,11,
	 8, 8, 8, 8, 8, 8, 8, 8,11,11,11,11,11, 8, 8, 8,
	 8, 8, 8,11,11,11,11,11, 8, 8, 8, 8, 8, 8,11,11,
	11,11,11, 7, 7, 8, 8, 8, 8,
};

static float _vq_quantthresh__16c2_s_p5_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__16c2_s_p5_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p5_1 = {
	_vq_quantthresh__16c2_s_p5_1,
	_vq_quantmap__16c2_s_p5_1,
	11,
	11
};

static static_codebook _16c2_s_p5_1 = {
	2, 121,
	_vq_lengthlist__16c2_s_p5_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__16c2_s_p5_1,
	NULL,
	&_vq_auxt__16c2_s_p5_1,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16c2_s_p6_0[] = {
	 1, 4, 4, 7, 6, 8, 8, 9, 9,10,10,11,11, 5, 5, 5,
	 7, 7, 9, 9, 9, 9,11,11,12,12, 6, 5, 5, 7, 7, 9,
	 9,10,10,11,11,12,12, 0, 6, 6, 7, 7, 9, 9,10,10,
	11,11,12,12, 0, 7, 7, 7, 7, 9, 9,10,10,11,12,12,
	12, 0,11,11, 8, 8,10,10,11,11,12,12,13,13, 0,11,
	12, 8, 8,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

static float _vq_quantthresh__16c2_s_p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__16c2_s_p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p6_0 = {
	_vq_quantthresh__16c2_s_p6_0,
	_vq_quantmap__16c2_s_p6_0,
	13,
	13
};

static static_codebook _16c2_s_p6_0 = {
	2, 169,
	_vq_lengthlist__16c2_s_p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__16c2_s_p6_0,
	NULL,
	&_vq_auxt__16c2_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16c2_s_p6_1[] = {
	 2, 3, 3, 5, 5, 6, 6, 6, 5, 5, 6, 6, 6, 5, 5, 6,
	 6, 6, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__16c2_s_p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16c2_s_p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p6_1 = {
	_vq_quantthresh__16c2_s_p6_1,
	_vq_quantmap__16c2_s_p6_1,
	5,
	5
};

static static_codebook _16c2_s_p6_1 = {
	2, 25,
	_vq_lengthlist__16c2_s_p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16c2_s_p6_1,
	NULL,
	&_vq_auxt__16c2_s_p6_1,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16c2_s_p7_0[] = {
	 1, 4, 4, 7, 7, 8, 8, 9, 9,10,10,11,11, 5, 5, 5,
	 8, 8, 9, 9,10,10,11,11,12,12, 6, 5, 5, 8, 8, 9,
	 9,10,10,11,11,12,13,18, 6, 6, 7, 7, 9, 9,10,10,
	12,12,13,13,18, 6, 6, 7, 7, 9, 9,10,10,12,12,13,
	13,18,11,10, 8, 8,10,10,11,11,12,12,13,13,18,11,
	11, 8, 8,10,10,11,11,12,13,13,13,18,18,18,10,11,
	11,11,12,12,13,13,14,14,18,18,18,11,11,11,11,12,
	12,13,13,14,14,18,18,18,14,14,12,12,12,12,14,14,
	15,14,18,18,18,15,15,11,12,12,12,13,13,15,15,18,
	18,18,18,18,13,13,13,13,13,14,17,16,18,18,18,18,
	18,13,14,13,13,14,13,15,14,
};

static float _vq_quantthresh__16c2_s_p7_0[] = {
	-60.5, -49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5,
	27.5, 38.5, 49.5, 60.5,
};

static long _vq_quantmap__16c2_s_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p7_0 = {
	_vq_quantthresh__16c2_s_p7_0,
	_vq_quantmap__16c2_s_p7_0,
	13,
	13
};

static static_codebook _16c2_s_p7_0 = {
	2, 169,
	_vq_lengthlist__16c2_s_p7_0,
	1, -523206656, 1618345984, 4, 0,
	_vq_quantlist__16c2_s_p7_0,
	NULL,
	&_vq_auxt__16c2_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16c2_s_p7_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 7, 7, 9, 9, 9, 6, 6,
	 7, 7, 8, 8, 8, 8, 9, 9, 9, 6, 6, 7, 7, 8, 8, 8,
	 8, 9, 9, 9, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 7,
	 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 7, 7, 7, 7, 8, 8,
	 8, 8, 9, 9, 9, 7, 7, 7, 7, 7, 7, 8, 8, 9, 9, 9,
	 7, 7, 8, 8, 7, 7, 8, 8, 9, 9, 9, 9, 9, 7, 7, 7,
	 7, 8, 8, 9, 9, 9, 9, 9, 8, 8, 7, 7, 8, 8, 9, 9,
	 9, 9, 9, 7, 7, 7, 7, 8, 8,
};

static float _vq_quantthresh__16c2_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__16c2_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p7_1 = {
	_vq_quantthresh__16c2_s_p7_1,
	_vq_quantmap__16c2_s_p7_1,
	11,
	11
};

static static_codebook _16c2_s_p7_1 = {
	2, 121,
	_vq_lengthlist__16c2_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__16c2_s_p7_1,
	NULL,
	&_vq_auxt__16c2_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p8_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__16c2_s_p8_0[] = {
	 1, 4, 4, 7, 6, 7, 7, 6, 6, 8, 8, 9, 9,10,10, 6,
	 6, 6, 8, 8, 9, 8, 8, 8, 9, 9,11,10,11,11, 7, 6,
	 6, 8, 8, 9, 8, 7, 7, 9, 9,10,10,12,11,14, 8, 8,
	 8, 9, 9, 9, 9, 9,10, 9,10,10,11,13,14, 8, 8, 8,
	 8, 9, 9, 8, 8, 9, 9,10,10,11,12,14,13,11, 9, 9,
	 9, 9, 9, 9, 9,10,11,10,13,12,14,11,13, 8, 9, 9,
	 9, 9, 9,10,10,11,10,13,12,14,14,14, 8, 9, 9, 9,
	11,11,11,11,11,12,13,13,14,14,14, 9, 8, 9, 9,10,
	10,12,10,11,12,12,14,14,14,14,11,12,10,10,12,12,
	12,12,13,14,12,12,14,14,14,12,12, 9,10,11,11,12,
	14,12,14,14,14,14,14,14,14,14,11,11,12,11,12,14,
	14,14,14,14,14,14,14,14,14,12,11,11,11,11,14,14,
	14,14,14,14,14,14,14,14,14,14,13,12,14,14,14,14,
	14,14,14,14,14,14,14,14,14,12,12,12,13,14,14,13,
	13,
};

static float _vq_quantthresh__16c2_s_p8_0[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__16c2_s_p8_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p8_0 = {
	_vq_quantthresh__16c2_s_p8_0,
	_vq_quantmap__16c2_s_p8_0,
	15,
	15
};

static static_codebook _16c2_s_p8_0 = {
	2, 225,
	_vq_lengthlist__16c2_s_p8_0,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__16c2_s_p8_0,
	NULL,
	&_vq_auxt__16c2_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p8_1[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__16c2_s_p8_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8,11,12,11, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9, 9,10, 9, 9,11,11,10, 7, 7, 8,
	 8, 8, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,
	11,11, 8, 7, 8, 8, 9, 9, 9, 9, 9, 9,10,10, 9,10,
	10, 9,10,10,11,11,12, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9,10, 9,10,10,10,10,11,11,11, 8, 8, 9, 9,
	 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,11,11,
	11, 8, 8, 9, 8, 9, 9, 9, 9,10, 9, 9, 9,10,10,10,
	10, 9,10,11,11,11, 9, 9, 9, 9,10, 9, 9, 9,10,10,
	 9,10, 9,10,10,10,10,10,11,12,11,11,11, 9, 9, 9,
	 9, 9,10,10, 9,10,10,10,10,10,10,10,10,12,11,13,
	13,11, 9, 9, 9, 9,10,10, 9,10,10,10,10,11,10,10,
	10,10,11,12,11,12,11, 9, 9, 9,10,10, 9,10,10,10,
	10,10,10,10,10,10,10,11,11,11,12,11, 9,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,11,12,12,12,
	11,11,11,10, 9,10,10,10,10,10,10,10,10,11,10,10,
	10,11,11,11,11,11,11,11,10,10,10,11,10,10,10,10,
	10,10,10,10,10,10,11,11,11,11,12,12,11,10,10,10,
	10,10,10,10,10,11,10,10,10,11,10,12,11,11,12,11,
	11,11,10,10,10,10,10,11,10,10,10,10,10,11,10,10,
	11,11,11,12,11,12,11,11,12,10,10,10,10,10,10,10,
	11,10,10,11,10,12,11,11,11,12,11,11,11,11,10,10,
	10,10,10,10,10,11,11,11,10,11,12,11,11,11,12,11,
	12,11,12,10,11,10,10,10,10,11,10,10,10,10,10,10,
	12,11,11,11,11,11,12,12,10,10,10,10,10,11,10,10,
	11,10,11,11,11,11,11,11,11,11,11,11,11,11,12,11,
	10,11,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__16c2_s_p8_1[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__16c2_s_p8_1[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p8_1 = {
	_vq_quantthresh__16c2_s_p8_1,
	_vq_quantmap__16c2_s_p8_1,
	21,
	21
};

static static_codebook _16c2_s_p8_1 = {
	2, 441,
	_vq_lengthlist__16c2_s_p8_1,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__16c2_s_p8_1,
	NULL,
	&_vq_auxt__16c2_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16c2_s_p9_0[] = {
	 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__16c2_s_p9_0[] = {
	-5120.5, -4189.5, -3258.5, -2327.5, -1396.5, -465.5, 465.5, 1396.5,
	2327.5, 3258.5, 4189.5, 5120.5,
};

static long _vq_quantmap__16c2_s_p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p9_0 = {
	_vq_quantthresh__16c2_s_p9_0,
	_vq_quantmap__16c2_s_p9_0,
	13,
	13
};

static static_codebook _16c2_s_p9_0 = {
	2, 169,
	_vq_lengthlist__16c2_s_p9_0,
	1, -510275072, 1631393792, 4, 0,
	_vq_quantlist__16c2_s_p9_0,
	NULL,
	&_vq_auxt__16c2_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p9_1[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__16c2_s_p9_1[] = {
	 1, 5, 5, 9, 8, 7, 7, 7, 6,10,11,11,11,11,11,11,
	11, 8, 7, 6, 8, 8,10, 9,10,10,10, 9,11,10,10,10,
	10,10, 8, 6, 6, 8, 8, 9, 8, 9, 8, 9,10,10,10,10,
	10,10,10,10, 8,10, 9, 9, 9, 9,10,10,10,10,10,10,
	10,10,10,10,10, 8, 9, 9, 9,10,10, 9,10,10,10,10,
	10,10,10,10,10,10,10,10, 9, 8, 9, 9,10,10,10,10,
	10,10,10,10,10,10,10,10, 9, 8, 8, 9, 9,10,10,10,
	10,10,10,10,10,10,10,10,10,10, 9,10, 9, 9,10,10,
	10,10,10,10,10,10,10,10,10,10,10, 9, 8, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	 8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10, 9,10, 9,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10, 9,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__16c2_s_p9_1[] = {
	-367.5, -318.5, -269.5, -220.5, -171.5, -122.5, -73.5, -24.5,
	24.5, 73.5, 122.5, 171.5, 220.5, 269.5, 318.5, 367.5,
};

static long _vq_quantmap__16c2_s_p9_1[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p9_1 = {
	_vq_quantthresh__16c2_s_p9_1,
	_vq_quantmap__16c2_s_p9_1,
	17,
	17
};

static static_codebook _16c2_s_p9_1 = {
	2, 289,
	_vq_lengthlist__16c2_s_p9_1,
	1, -518488064, 1622704128, 5, 0,
	_vq_quantlist__16c2_s_p9_1,
	NULL,
	&_vq_auxt__16c2_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__16c2_s_p9_2[] = {
	13,
	12,
	14,
	11,
	15,
	10,
	16,
	9,
	17,
	8,
	18,
	7,
	19,
	6,
	20,
	5,
	21,
	4,
	22,
	3,
	23,
	2,
	24,
	1,
	25,
	0,
	26,
};

static long _vq_lengthlist__16c2_s_p9_2[] = {
	 1, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 8, 7, 8, 7, 7, 4, 4,
};

static float _vq_quantthresh__16c2_s_p9_2[] = {
	-12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5,
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5,
	11.5, 12.5,
};

static long _vq_quantmap__16c2_s_p9_2[] = {
	   25,   23,   21,   19,   17,   15,   13,   11,
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,   12,   14,   16,   18,   20,
	   22,   24,   26,
};

static encode_aux_threshmatch _vq_auxt__16c2_s_p9_2 = {
	_vq_quantthresh__16c2_s_p9_2,
	_vq_quantmap__16c2_s_p9_2,
	27,
	27
};

static static_codebook _16c2_s_p9_2 = {
	1, 27,
	_vq_lengthlist__16c2_s_p9_2,
	1, -528875520, 1611661312, 5, 0,
	_vq_quantlist__16c2_s_p9_2,
	NULL,
	&_vq_auxt__16c2_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__16c2_s_short[] = {
	 7,10,11,11,11,14,15,15,17,14, 8, 6, 7, 7, 8, 9,
	11,11,14,17, 9, 6, 6, 6, 7, 7,10,11,15,16, 9, 6,
	 6, 4, 4, 5, 8, 9,12,16,10, 6, 6, 4, 4, 4, 6, 9,
	13,16,10, 7, 6, 5, 4, 3, 5, 7,13,16,11, 9, 8, 7,
	 6, 5, 5, 6,12,15,10,10,10, 9, 7, 6, 6, 7,11,15,
	13,13,13,13,11,10,10, 9,12,16,16,16,16,14,16,15,
	15,12,14,14,
};

static static_codebook _huff_book__16c2_s_short = {
	2, 100,
	_huff_lengthlist__16c2_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8c0_s_p1_0[] = {
	 1, 5, 4, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 7, 8, 9, 0, 0, 0,
	 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 7, 9, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 8, 0, 0, 0, 0,
	 0, 0, 8,10,10, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,10, 9, 0, 0, 0,
	 0, 0, 0, 8, 9,11, 0, 0, 0, 0, 0, 0, 9,11,11, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9,10, 0, 0,
	 0, 0, 0, 0, 9,11,10, 0, 0, 0, 0, 0, 0, 9,11,11,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 8, 9,10, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,11,11, 0,
	 0, 0, 0, 0, 0, 9,10,11, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 9,10, 0, 0, 0, 0, 0, 0, 9,11,11,
	 0, 0, 0, 0, 0, 0, 8,11, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__8c0_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__8c0_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p1_0 = {
	_vq_quantthresh__8c0_s_p1_0,
	_vq_quantmap__8c0_s_p1_0,
	3,
	3
};

static static_codebook _8c0_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__8c0_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__8c0_s_p1_0,
	NULL,
	&_vq_auxt__8c0_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8c0_s_p2_0[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__8c0_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8c0_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p2_0 = {
	_vq_quantthresh__8c0_s_p2_0,
	_vq_quantmap__8c0_s_p2_0,
	5,
	5
};

static static_codebook _8c0_s_p2_0 = {
	4, 625,
	_vq_lengthlist__8c0_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8c0_s_p2_0,
	NULL,
	&_vq_auxt__8c0_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8c0_s_p3_0[] = {
	 1, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 7, 7, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 5, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 7, 8, 8,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 7, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__8c0_s_p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8c0_s_p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p3_0 = {
	_vq_quantthresh__8c0_s_p3_0,
	_vq_quantmap__8c0_s_p3_0,
	5,
	5
};

static static_codebook _8c0_s_p3_0 = {
	4, 625,
	_vq_lengthlist__8c0_s_p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8c0_s_p3_0,
	NULL,
	&_vq_auxt__8c0_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__8c0_s_p4_0[] = {
	 1, 2, 3, 7, 7, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 8, 8, 0, 0, 0, 0, 0, 0, 0, 9, 8, 0, 0, 0, 0, 0,
	 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__8c0_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__8c0_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p4_0 = {
	_vq_quantthresh__8c0_s_p4_0,
	_vq_quantmap__8c0_s_p4_0,
	9,
	9
};

static static_codebook _8c0_s_p4_0 = {
	2, 81,
	_vq_lengthlist__8c0_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__8c0_s_p4_0,
	NULL,
	&_vq_auxt__8c0_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__8c0_s_p5_0[] = {
	 1, 3, 3, 5, 5, 7, 6, 8, 8, 0, 0, 0, 7, 7, 7, 7,
	 8, 8, 0, 0, 0, 7, 7, 7, 7, 8, 9, 0, 0, 0, 8, 8,
	 8, 8, 9, 9, 0, 0, 0, 8, 8, 8, 8, 9, 9, 0, 0, 0,
	 9, 9, 8, 8,10,10, 0, 0, 0, 9, 9, 8, 8,10,10, 0,
	 0, 0,10,10, 9, 9,10,10, 0, 0, 0, 0, 0, 9, 9,10,
	10,
};

static float _vq_quantthresh__8c0_s_p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__8c0_s_p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p5_0 = {
	_vq_quantthresh__8c0_s_p5_0,
	_vq_quantmap__8c0_s_p5_0,
	9,
	9
};

static static_codebook _8c0_s_p5_0 = {
	2, 81,
	_vq_lengthlist__8c0_s_p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__8c0_s_p5_0,
	NULL,
	&_vq_auxt__8c0_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p6_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__8c0_s_p6_0[] = {
	 1, 3, 3, 6, 6, 8, 8, 9, 9, 8, 8,10, 9,10,10,11,
	11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,11,
	11,12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,
	11,12,11, 0, 0, 0, 8, 8, 9, 9,10,10, 9, 9,10,10,
	11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10, 9, 9,11,
	10,11,11,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,10,10,
	11,11,11,12,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,10,
	10,11,11,12,12,13,13, 0, 0, 0,10,10,10,10,11,11,
	10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,10, 9,10,
	11,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,
	10, 9,10,11,12,12,13,13,14,13, 0, 0, 0, 0, 0, 9,
	 9, 9,10,10,10,11,11,13,12,13,13, 0, 0, 0, 0, 0,
	10,10,10,10,11,11,12,12,13,13,14,14, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,12,12,13,13,13,14, 0, 0, 0,
	 0, 0, 0, 0,11,11,11,11,12,12,13,14,14,14, 0, 0,
	 0, 0, 0, 0, 0,11,11,11,11,12,12,13,13,14,13, 0,
	 0, 0, 0, 0, 0, 0,11,11,12,12,13,13,14,14,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,14,
	14,
};

static float _vq_quantthresh__8c0_s_p6_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__8c0_s_p6_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p6_0 = {
	_vq_quantthresh__8c0_s_p6_0,
	_vq_quantmap__8c0_s_p6_0,
	17,
	17
};

static static_codebook _8c0_s_p6_0 = {
	2, 289,
	_vq_lengthlist__8c0_s_p6_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__8c0_s_p6_0,
	NULL,
	&_vq_auxt__8c0_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8c0_s_p7_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,11, 9,10,12,
	 9,10, 4, 7, 7,10,10,10,11, 9, 9, 6,11,10,11,11,
	12,11,11,11, 6,10,10,11,11,12,11,10,10, 6, 9,10,
	11,11,11,11,10,10, 7,10,11,12,11,11,12,11,12, 6,
	 9, 9,10, 9, 9,11,10,10, 6, 9, 9,10,10,10,11,10,
	10,
};

static float _vq_quantthresh__8c0_s_p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__8c0_s_p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p7_0 = {
	_vq_quantthresh__8c0_s_p7_0,
	_vq_quantmap__8c0_s_p7_0,
	3,
	3
};

static static_codebook _8c0_s_p7_0 = {
	4, 81,
	_vq_lengthlist__8c0_s_p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__8c0_s_p7_0,
	NULL,
	&_vq_auxt__8c0_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__8c0_s_p7_1[] = {
	 1, 3, 3, 6, 6, 8, 8, 9, 9, 9, 9,10,10,10, 7, 7,
	 8, 8, 9, 9, 9, 9,10,10, 9, 7, 7, 8, 8, 9, 9, 9,
	 9,10,10,10, 8, 8, 9, 9, 9, 9, 9, 9,10,10,10, 8,
	 8, 9, 9, 9, 9, 8, 9,10,10,10, 8, 8, 9, 9, 9,10,
	10,10,10,10,10, 9, 9, 9, 9, 9, 9,10,10,11,10,11,
	 9, 9, 9, 9,10,10,10,10,11,11,11,10,10, 9, 9,10,
	10,10, 9,11,10,10,10,10,10,10, 9, 9,10,10,11,11,
	10,10,10, 9, 9, 9,10,10,10,
};

static float _vq_quantthresh__8c0_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__8c0_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p7_1 = {
	_vq_quantthresh__8c0_s_p7_1,
	_vq_quantmap__8c0_s_p7_1,
	11,
	11
};

static static_codebook _8c0_s_p7_1 = {
	2, 121,
	_vq_lengthlist__8c0_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__8c0_s_p7_1,
	NULL,
	&_vq_auxt__8c0_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__8c0_s_p8_0[] = {
	 1, 4, 4, 7, 6, 7, 7, 7, 7, 8, 8, 9, 9, 7, 6, 6,
	 7, 7, 8, 8, 7, 7, 8, 9,10,10, 7, 6, 6, 7, 7, 8,
	 7, 7, 7, 9, 9,10,12, 0, 8, 8, 8, 8, 8, 9, 8, 8,
	 9, 9,10,10, 0, 8, 8, 8, 8, 8, 9, 8, 9, 9, 9,11,
	10, 0, 0,13, 9, 8, 9, 9, 9, 9,10,10,11,11, 0,13,
	 0, 9, 9, 9, 9, 9, 9,11,10,11,11, 0, 0, 0, 8, 9,
	10, 9,10,10,13,11,12,12, 0, 0, 0, 8, 9, 9, 9,10,
	10,13,12,12,13, 0, 0, 0,12, 0,10,10,12,11,10,11,
	12,12, 0, 0, 0,13,13,10,10,10,11,12, 0,13, 0, 0,
	 0, 0, 0, 0,13,11, 0,12,12,12,13,12, 0, 0, 0, 0,
	 0, 0,13,13,11,13,13,11,12,
};

static float _vq_quantthresh__8c0_s_p8_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__8c0_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p8_0 = {
	_vq_quantthresh__8c0_s_p8_0,
	_vq_quantmap__8c0_s_p8_0,
	13,
	13
};

static static_codebook _8c0_s_p8_0 = {
	2, 169,
	_vq_lengthlist__8c0_s_p8_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__8c0_s_p8_0,
	NULL,
	&_vq_auxt__8c0_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p8_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8c0_s_p8_1[] = {
	 1, 3, 4, 5, 5, 7, 6, 6, 6, 5, 7, 7, 7, 6, 6, 7,
	 7, 7, 6, 6, 7, 7, 7, 6, 6,
};

static float _vq_quantthresh__8c0_s_p8_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8c0_s_p8_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p8_1 = {
	_vq_quantthresh__8c0_s_p8_1,
	_vq_quantmap__8c0_s_p8_1,
	5,
	5
};

static static_codebook _8c0_s_p8_1 = {
	2, 25,
	_vq_lengthlist__8c0_s_p8_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8c0_s_p8_1,
	NULL,
	&_vq_auxt__8c0_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p9_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8c0_s_p9_0[] = {
	 1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__8c0_s_p9_0[] = {
	-157.5, 157.5,
};

static long _vq_quantmap__8c0_s_p9_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p9_0 = {
	_vq_quantthresh__8c0_s_p9_0,
	_vq_quantmap__8c0_s_p9_0,
	3,
	3
};

static static_codebook _8c0_s_p9_0 = {
	4, 81,
	_vq_lengthlist__8c0_s_p9_0,
	1, -518803456, 1628680192, 2, 0,
	_vq_quantlist__8c0_s_p9_0,
	NULL,
	&_vq_auxt__8c0_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__8c0_s_p9_1[] = {
	 1, 4, 4, 5, 5,10, 8,11,11,11,11,11,11,11,11, 6,
	 6, 6, 7, 6,11,10,11,11,11,11,11,11,11,11, 7, 5,
	 6, 6, 6, 8, 7,11,11,11,11,11,11,11,11,11, 7, 8,
	 8, 8, 9, 9,11,11,11,11,11,11,11,11,11, 9, 8, 7,
	 8, 9,11,11,11,11,11,11,11,11,11,11,11,10,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,10,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,
};

static float _vq_quantthresh__8c0_s_p9_1[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__8c0_s_p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p9_1 = {
	_vq_quantthresh__8c0_s_p9_1,
	_vq_quantmap__8c0_s_p9_1,
	15,
	15
};

static static_codebook _8c0_s_p9_1 = {
	2, 225,
	_vq_lengthlist__8c0_s_p9_1,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__8c0_s_p9_1,
	NULL,
	&_vq_auxt__8c0_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__8c0_s_p9_2[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__8c0_s_p9_2[] = {
	 1, 5, 5, 7, 7, 8, 7, 8, 8,10,10, 9, 9,10,10,10,
	11,11,10,12,11,12,12,12, 9, 8, 8, 8, 8, 8, 9,10,
	10,10,10,11,11,11,10,11,11,12,12,11,12, 8, 8, 7,
	 7, 8, 9,10,10,10, 9,10,10, 9,10,10,11,11,11,11,
	11,11, 9, 9, 9, 9, 8, 9,10,10,11,10,10,11,11,12,
	10,10,12,12,11,11,10, 9, 9,10, 8, 9,10,10,10, 9,
	10,10,11,11,10,11,10,10,10,12,12,12, 9,10, 9,10,
	 9, 9,10,10,11,11,11,11,10,10,10,11,12,11,12,11,
	12,10,11,10,11, 9,10, 9,10, 9,10,10, 9,10,10,11,
	10,11,11,11,11,12,11, 9,10,10,10,10,11,11,11,11,
	11,10,11,11,11,11,10,12,10,12,12,11,12,10,10,11,
	10, 9,11,10,11, 9,10,11,10,10,10,11,11,11,11,12,
	12,10, 9, 9,11,10, 9,12,11,10,12,12,11,11,11,11,
	10,11,11,12,11,10,12, 9,11,10,11,10,10,11,10,11,
	 9,10,10,10,11,12,11,11,12,11,10,10,11,11, 9,10,
	10,12,10,11,10,10,10, 9,10,10,10,10, 9,10,10,11,
	11,11,11,12,11,10,10,10,10,11,11,10,11,11, 9,11,
	10,12,10,12,11,10,11,10,10,10,11,10,10,11,11,10,
	11,10,10,10,10,11,11,12,10,10,10,11,10,11,12,11,
	10,11,10,10,11,11,10,12,10, 9,10,10,11,11,11,10,
	12,10,10,11,11,11,10,10,11,10,10,10,11,10,11,10,
	12,11,11,10,10,10,12,10,10,11, 9,10,11,11,11,10,
	10,11,10,10, 9,11,11,12,12,11,12,11,11,11,11,11,
	11, 9,10,11,10,12,10,10,10,10,11,10,10,11,10,10,
	12,10,10,10,10,10, 9,12,10,10,10,10,12, 9,11,10,
	10,11,10,12,12,10,12,12,12,10,10,10,10, 9,10,11,
	10,10,12,10,10,12,11,10,11,10,10,12,11,10,12,10,
	10,11, 9,11,10, 9,10, 9,10,
};

static float _vq_quantthresh__8c0_s_p9_2[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__8c0_s_p9_2[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__8c0_s_p9_2 = {
	_vq_quantthresh__8c0_s_p9_2,
	_vq_quantmap__8c0_s_p9_2,
	21,
	21
};

static static_codebook _8c0_s_p9_2 = {
	2, 441,
	_vq_lengthlist__8c0_s_p9_2,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__8c0_s_p9_2,
	NULL,
	&_vq_auxt__8c0_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__8c0_s_single[] = {
	 4, 5,18, 7,10, 6, 7, 8, 9,10, 5, 2,18, 5, 7, 5,
	 6, 7, 8,11,17,17,17,17,17,17,17,17,17,17, 7, 4,
	17, 6, 9, 6, 8,10,12,15,11, 7,17, 9, 6, 6, 7, 9,
	11,15, 6, 4,17, 6, 6, 4, 5, 8,11,16, 6, 6,17, 8,
	 6, 5, 6, 9,13,16, 8, 9,17,11, 9, 8, 8,11,13,17,
	 9,12,17,15,14,13,12,13,14,17,12,15,17,17,17,17,
	17,16,17,17,
};

static static_codebook _huff_book__8c0_s_single = {
	2, 100,
	_huff_lengthlist__8c0_s_single,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8c1_s_p1_0[] = {
	 1, 5, 5, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 7, 0, 0, 0, 0, 0, 0, 7, 8, 9, 0, 0, 0,
	 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 8, 0, 0, 0, 0, 0, 0, 7, 9, 8, 0, 0,
	 0, 0, 0, 0, 7, 9, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 8, 0, 0, 0, 0,
	 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 8, 8,10, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8,10, 9, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 8, 9,10, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 8,10, 8, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__8c1_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__8c1_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p1_0 = {
	_vq_quantthresh__8c1_s_p1_0,
	_vq_quantmap__8c1_s_p1_0,
	3,
	3
};

static static_codebook _8c1_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__8c1_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__8c1_s_p1_0,
	NULL,
	&_vq_auxt__8c1_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8c1_s_p2_0[] = {
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__8c1_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8c1_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p2_0 = {
	_vq_quantthresh__8c1_s_p2_0,
	_vq_quantmap__8c1_s_p2_0,
	5,
	5
};

static static_codebook _8c1_s_p2_0 = {
	4, 625,
	_vq_lengthlist__8c1_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8c1_s_p2_0,
	NULL,
	&_vq_auxt__8c1_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8c1_s_p3_0[] = {
	 2, 4, 4, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 6, 6, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__8c1_s_p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8c1_s_p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p3_0 = {
	_vq_quantthresh__8c1_s_p3_0,
	_vq_quantmap__8c1_s_p3_0,
	5,
	5
};

static static_codebook _8c1_s_p3_0 = {
	4, 625,
	_vq_lengthlist__8c1_s_p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8c1_s_p3_0,
	NULL,
	&_vq_auxt__8c1_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__8c1_s_p4_0[] = {
	 1, 2, 3, 7, 7, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 8, 8, 0, 0, 0, 0, 0, 0, 0, 9, 8, 0, 0, 0, 0, 0,
	 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__8c1_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__8c1_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p4_0 = {
	_vq_quantthresh__8c1_s_p4_0,
	_vq_quantmap__8c1_s_p4_0,
	9,
	9
};

static static_codebook _8c1_s_p4_0 = {
	2, 81,
	_vq_lengthlist__8c1_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__8c1_s_p4_0,
	NULL,
	&_vq_auxt__8c1_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__8c1_s_p5_0[] = {
	 1, 3, 3, 4, 5, 6, 6, 8, 8, 0, 0, 0, 8, 8, 7, 7,
	 9, 9, 0, 0, 0, 8, 8, 7, 7, 9, 9, 0, 0, 0, 9,10,
	 8, 8, 9, 9, 0, 0, 0,10,10, 8, 8, 9, 9, 0, 0, 0,
	11,10, 8, 8,10,10, 0, 0, 0,11,11, 8, 8,10,10, 0,
	 0, 0,12,12, 9, 9,10,10, 0, 0, 0, 0, 0, 9, 9,10,
	10,
};

static float _vq_quantthresh__8c1_s_p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__8c1_s_p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p5_0 = {
	_vq_quantthresh__8c1_s_p5_0,
	_vq_quantmap__8c1_s_p5_0,
	9,
	9
};

static static_codebook _8c1_s_p5_0 = {
	2, 81,
	_vq_lengthlist__8c1_s_p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__8c1_s_p5_0,
	NULL,
	&_vq_auxt__8c1_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p6_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__8c1_s_p6_0[] = {
	 1, 3, 3, 5, 5, 8, 8, 8, 8, 9, 9,10,10,11,11,11,
	11, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,11,
	12,12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11,12,12, 0, 0, 0, 9, 9, 8, 8,10,10,10,10,11,11,
	12,12,12,12, 0, 0, 0, 9, 9, 8, 8,10,10,10,10,11,
	11,12,12,12,12, 0, 0, 0,10,10, 9, 9,10,10,10,10,
	11,11,12,12,13,13, 0, 0, 0,10,10, 9, 9,10,10,10,
	10,11,11,12,12,13,13, 0, 0, 0,11,11, 9, 9,10,10,
	10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,
	10,10,11,11,12,12,12,12,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,11,11,12,11,12,12,13,13, 0, 0, 0, 0, 0,
	10,10,11,11,11,11,12,12,13,12,13,13, 0, 0, 0, 0,
	 0, 0, 0,11,10,11,11,12,12,13,13,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,12,12,12,13,13,13,14, 0, 0,
	 0, 0, 0, 0, 0,11,11,12,12,12,12,13,13,14,13, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,13,13,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,13,13,13,13,14,
	14,
};

static float _vq_quantthresh__8c1_s_p6_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__8c1_s_p6_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p6_0 = {
	_vq_quantthresh__8c1_s_p6_0,
	_vq_quantmap__8c1_s_p6_0,
	17,
	17
};

static static_codebook _8c1_s_p6_0 = {
	2, 289,
	_vq_lengthlist__8c1_s_p6_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__8c1_s_p6_0,
	NULL,
	&_vq_auxt__8c1_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8c1_s_p7_0[] = {
	 1, 4, 4, 6, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,10,
	 9, 9, 5, 7, 7,10, 9, 9,10, 9, 9, 6,10,10,10,10,
	10,11,10,10, 6, 9, 9,10, 9,10,11,10,10, 6, 9, 9,
	10, 9, 9,11, 9,10, 7,10,10,11,11,11,11,10,10, 6,
	 9, 9,10,10,10,11, 9, 9, 6, 9, 9,10,10,10,10, 9,
	 9,
};

static float _vq_quantthresh__8c1_s_p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__8c1_s_p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p7_0 = {
	_vq_quantthresh__8c1_s_p7_0,
	_vq_quantmap__8c1_s_p7_0,
	3,
	3
};

static static_codebook _8c1_s_p7_0 = {
	4, 81,
	_vq_lengthlist__8c1_s_p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__8c1_s_p7_0,
	NULL,
	&_vq_auxt__8c1_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__8c1_s_p7_1[] = {
	 2, 3, 3, 5, 5, 7, 7, 7, 7, 7, 7,10,10, 9, 7, 7,
	 7, 7, 8, 8, 8, 8, 9, 9, 9, 7, 7, 7, 7, 8, 8, 8,
	 8,10,10,10, 7, 7, 7, 7, 8, 8, 8, 8,10,10,10, 7,
	 7, 7, 7, 8, 8, 8, 8,10,10,10, 8, 8, 8, 8, 8, 8,
	 8, 8,10,10,10, 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 8, 8, 8, 8, 8, 8,10,10,
	10,10,10, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__8c1_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__8c1_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p7_1 = {
	_vq_quantthresh__8c1_s_p7_1,
	_vq_quantmap__8c1_s_p7_1,
	11,
	11
};

static static_codebook _8c1_s_p7_1 = {
	2, 121,
	_vq_lengthlist__8c1_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__8c1_s_p7_1,
	NULL,
	&_vq_auxt__8c1_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__8c1_s_p8_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 8, 8, 9, 9,10,10, 7, 5, 5,
	 7, 7, 8, 8, 8, 8, 9,10,11,11, 7, 5, 5, 7, 7, 8,
	 8, 9, 9,10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	 9,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9, 9, 9,10, 9,10,11,11,11, 0,13,
	12, 9, 8, 9, 9,10,10,11,11,12,11, 0, 0, 0, 9, 9,
	 9, 9,10,10,11,11,12,12, 0, 0, 0,10,10, 9, 9,10,
	10,11,11,12,12, 0, 0, 0,13,13,10,10,11,11,12,11,
	13,12, 0, 0, 0,14,14,10,10,11,10,11,11,12,12, 0,
	 0, 0, 0, 0,12,12,11,11,12,12,13,13, 0, 0, 0, 0,
	 0,12,12,11,10,12,11,13,12,
};

static float _vq_quantthresh__8c1_s_p8_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__8c1_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p8_0 = {
	_vq_quantthresh__8c1_s_p8_0,
	_vq_quantmap__8c1_s_p8_0,
	13,
	13
};

static static_codebook _8c1_s_p8_0 = {
	2, 169,
	_vq_lengthlist__8c1_s_p8_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__8c1_s_p8_0,
	NULL,
	&_vq_auxt__8c1_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p8_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8c1_s_p8_1[] = {
	 2, 3, 3, 5, 5, 6, 6, 6, 5, 5, 6, 6, 6, 5, 5, 6,
	 6, 6, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__8c1_s_p8_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8c1_s_p8_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p8_1 = {
	_vq_quantthresh__8c1_s_p8_1,
	_vq_quantmap__8c1_s_p8_1,
	5,
	5
};

static static_codebook _8c1_s_p8_1 = {
	2, 25,
	_vq_lengthlist__8c1_s_p8_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8c1_s_p8_1,
	NULL,
	&_vq_auxt__8c1_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__8c1_s_p9_0[] = {
	 1, 3, 3,10,10,10,10,10,10,10,10,10,10, 5, 6, 6,
	10,10,10,10,10,10,10,10,10,10, 6, 7, 8,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10, 9, 9, 9, 9,
};

static float _vq_quantthresh__8c1_s_p9_0[] = {
	-1732.5, -1417.5, -1102.5, -787.5, -472.5, -157.5, 157.5, 472.5,
	787.5, 1102.5, 1417.5, 1732.5,
};

static long _vq_quantmap__8c1_s_p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p9_0 = {
	_vq_quantthresh__8c1_s_p9_0,
	_vq_quantmap__8c1_s_p9_0,
	13,
	13
};

static static_codebook _8c1_s_p9_0 = {
	2, 169,
	_vq_lengthlist__8c1_s_p9_0,
	1, -513964032, 1628680192, 4, 0,
	_vq_quantlist__8c1_s_p9_0,
	NULL,
	&_vq_auxt__8c1_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__8c1_s_p9_1[] = {
	 1, 4, 4, 5, 5, 7, 7, 9, 9,11,11,12,12,13,13, 6,
	 5, 5, 6, 6, 9, 9,10,10,12,12,12,13,15,14, 6, 5,
	 5, 7, 7, 9, 9,10,10,12,12,12,13,14,13,17, 7, 7,
	 8, 8,10,10,11,11,12,13,13,13,13,13,17, 7, 7, 8,
	 8,10,10,11,11,13,13,13,13,14,14,17,11,11, 9, 9,
	11,11,12,12,12,13,13,14,15,13,17,12,12, 9, 9,11,
	11,12,12,13,13,13,13,14,16,17,17,17,11,12,12,12,
	13,13,13,14,15,14,15,15,17,17,17,12,12,11,11,13,
	13,14,14,15,14,15,15,17,17,17,15,15,13,13,14,14,
	15,14,15,15,16,15,17,17,17,15,15,13,13,13,14,14,
	15,15,15,15,16,17,17,17,17,16,14,15,14,14,15,14,
	14,15,15,15,17,17,17,17,17,14,14,16,14,15,15,15,
	15,15,15,17,17,17,17,17,17,16,16,15,17,15,15,14,
	17,15,17,16,17,17,17,17,16,15,14,15,15,15,15,15,
	15,
};

static float _vq_quantthresh__8c1_s_p9_1[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__8c1_s_p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p9_1 = {
	_vq_quantthresh__8c1_s_p9_1,
	_vq_quantmap__8c1_s_p9_1,
	15,
	15
};

static static_codebook _8c1_s_p9_1 = {
	2, 225,
	_vq_lengthlist__8c1_s_p9_1,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__8c1_s_p9_1,
	NULL,
	&_vq_auxt__8c1_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__8c1_s_p9_2[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__8c1_s_p9_2[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8, 9, 8, 9, 9, 9,
	 9, 9, 9, 9, 9,11,11,12, 7, 7, 7, 7, 8, 8, 9, 9,
	 9, 9,10,10,10,10,10,10,10,10,11,11,11, 7, 7, 7,
	 7, 8, 8, 9, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,11,
	11,12, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9,10,10,10,10,
	10,10,10,10,11,11,11, 7, 7, 8, 8, 8, 8, 9, 9, 9,
	 9,10,10,10,10,10,10,10,10,11,11,11, 8, 8, 8, 8,
	 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,11,11,
	11, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,10,
	10,10,10,11,12,11, 9, 9, 8, 9, 9, 9, 9, 9,10,10,
	10,10,10,10,10,10,10,10,11,11,11,11,11, 8, 8, 9,
	 9, 9, 9,10,10,10,10,10,10,10,10,10,10,11,12,11,
	12,11, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,
	10,10,11,11,11,11,11, 9, 9, 9, 9,10,10,10,10,10,
	10,10,10,10,10,10,10,12,11,12,11,11, 9, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10,10,10,12,11,11,11,
	11,11,11,10,10,10,10,10,10,10,10,10,10,10,10,10,
	11,11,11,12,11,11,12,11,10,10,10,10,10,10,10,10,
	10,10,10,10,11,10,11,11,11,11,11,11,11,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,11,11,12,11,12,
	11,11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	11,11,12,11,12,11,11,11,11,10,10,10,10,10,10,10,
	10,10,10,10,10,11,11,12,11,11,12,11,11,12,10,10,
	11,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,
	11,11,11,10,10,10,10,10,10,10,10,10,10,10,10,12,
	12,11,12,11,11,12,12,12,11,11,10,10,10,10,10,10,
	10,10,10,11,12,12,11,12,12,11,12,11,11,11,11,10,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__8c1_s_p9_2[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__8c1_s_p9_2[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__8c1_s_p9_2 = {
	_vq_quantthresh__8c1_s_p9_2,
	_vq_quantmap__8c1_s_p9_2,
	21,
	21
};

static static_codebook _8c1_s_p9_2 = {
	2, 441,
	_vq_lengthlist__8c1_s_p9_2,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__8c1_s_p9_2,
	NULL,
	&_vq_auxt__8c1_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__8c1_s_single[] = {
	 4, 6,18, 8,11, 8, 8, 9, 9,10, 4, 4,18, 5, 9, 5,
	 6, 7, 8,10,18,18,18,18,17,17,17,17,17,17, 7, 5,
	17, 6,11, 6, 7, 8, 9,12,12, 9,17,12, 8, 8, 9,10,
	10,13, 7, 5,17, 6, 8, 4, 5, 6, 8,10, 6, 5,17, 6,
	 8, 5, 4, 5, 7, 9, 7, 7,17, 8, 9, 6, 5, 5, 6, 8,
	 8, 8,17, 9,11, 8, 6, 6, 6, 7, 9,10,17,12,12,10,
	 9, 7, 7, 8,
};

static static_codebook _huff_book__8c1_s_single = {
	2, 100,
	_huff_lengthlist__8c1_s_single,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c2_s_long[] = {
	 6, 6,12,10,10,10, 9,10,12,12, 6, 1,10, 5, 6, 6,
	 7, 9,11,14,12, 9, 8,11, 7, 8, 9,11,13,15,10, 5,
	12, 7, 8, 7, 9,12,14,15,10, 6, 7, 8, 5, 6, 7, 9,
	12,14, 9, 6, 8, 7, 6, 6, 7, 9,12,12, 9, 7, 9, 9,
	 7, 6, 6, 7,10,10,10, 9,10,11, 8, 7, 6, 6, 8,10,
	12,11,13,13,11,10, 8, 8, 8,10,11,13,15,15,14,13,
	10, 8, 8, 9,
};

static static_codebook _huff_book__44c2_s_long = {
	2, 100,
	_huff_lengthlist__44c2_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c2_s_p1_0[] = {
	 2, 4, 4, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 0,
	 0, 0, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,
	 0, 0, 0, 6, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 6, 8, 7, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,
	 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8, 8, 0, 0,
	 0, 0, 0, 0, 8, 9, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 6, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0,
	 0, 0, 0, 0, 0, 8, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9,
	 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c2_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c2_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p1_0 = {
	_vq_quantthresh__44c2_s_p1_0,
	_vq_quantmap__44c2_s_p1_0,
	3,
	3
};

static static_codebook _44c2_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__44c2_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c2_s_p1_0,
	NULL,
	&_vq_auxt__44c2_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c2_s_p2_0[] = {
	 1, 4, 4, 0, 0, 0, 7, 7, 0, 0, 0, 7, 7, 0, 0, 0,
	 8, 8, 0, 0, 0, 0, 0, 0, 0, 4, 6, 6, 0, 0, 0, 8,
	 8, 0, 0, 0, 8, 8, 0, 0, 0, 9, 9, 0, 0, 0, 0, 0,
	 0, 0, 4, 6, 6, 0, 0, 0, 8, 8, 0, 0, 0, 8, 8, 0,
	 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,11,11, 0, 0,
	 0,11,11, 0, 0, 0,12,11, 0, 0, 0, 0, 0, 0, 0, 7,
	 8, 8, 0, 0, 0,10,11, 0, 0, 0,11,11, 0, 0, 0,11,
	12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 6, 8, 8, 0, 0, 0,11,11, 0, 0, 0,11,11,
	 0, 0, 0,12,12, 0, 0, 0, 0, 0, 0, 0, 6, 8, 8, 0,
	 0, 0,10,11, 0, 0, 0,10,11, 0, 0, 0,11,11, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 8, 9, 9, 0, 0, 0,11,12, 0, 0, 0,11,12, 0, 0, 0,
	12,11, 0, 0, 0, 0, 0, 0, 0, 8,10, 9, 0, 0, 0,12,
	11, 0, 0, 0,12,11, 0, 0, 0,11,12, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c2_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c2_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p2_0 = {
	_vq_quantthresh__44c2_s_p2_0,
	_vq_quantmap__44c2_s_p2_0,
	5,
	5
};

static static_codebook _44c2_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c2_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c2_s_p2_0,
	NULL,
	&_vq_auxt__44c2_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c2_s_p3_0[] = {
	 2, 4, 3, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 6, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c2_s_p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c2_s_p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p3_0 = {
	_vq_quantthresh__44c2_s_p3_0,
	_vq_quantmap__44c2_s_p3_0,
	5,
	5
};

static static_codebook _44c2_s_p3_0 = {
	4, 625,
	_vq_lengthlist__44c2_s_p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c2_s_p3_0,
	NULL,
	&_vq_auxt__44c2_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c2_s_p4_0[] = {
	 1, 3, 3, 6, 6, 0, 0, 0, 0, 0, 6, 6, 6, 6, 0, 0,
	 0, 0, 0, 6, 6, 6, 6, 0, 0, 0, 0, 0, 7, 7, 6, 6,
	 0, 0, 0, 0, 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0,
	 7, 8, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0,
	 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c2_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c2_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p4_0 = {
	_vq_quantthresh__44c2_s_p4_0,
	_vq_quantmap__44c2_s_p4_0,
	9,
	9
};

static static_codebook _44c2_s_p4_0 = {
	2, 81,
	_vq_lengthlist__44c2_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c2_s_p4_0,
	NULL,
	&_vq_auxt__44c2_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c2_s_p5_0[] = {
	 1, 3, 3, 6, 6, 7, 7, 9, 9, 0, 7, 7, 7, 7, 7, 7,
	 9, 9, 0, 7, 7, 7, 7, 7, 7, 9, 9, 0, 8, 8, 7, 7,
	 8, 8,10,10, 0, 0, 0, 7, 7, 8, 8,10,10, 0, 0, 0,
	 9, 9, 8, 8,10,10, 0, 0, 0, 9, 9, 8, 8,10,10, 0,
	 0, 0,10,10, 9, 9,11,11, 0, 0, 0, 0, 0, 9, 9,11,
	11,
};

static float _vq_quantthresh__44c2_s_p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c2_s_p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p5_0 = {
	_vq_quantthresh__44c2_s_p5_0,
	_vq_quantmap__44c2_s_p5_0,
	9,
	9
};

static static_codebook _44c2_s_p5_0 = {
	2, 81,
	_vq_lengthlist__44c2_s_p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c2_s_p5_0,
	NULL,
	&_vq_auxt__44c2_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p6_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c2_s_p6_0[] = {
	 1, 4, 3, 6, 6, 8, 8, 9, 9, 9, 9, 9, 9,10,10,11,
	11, 0, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,11,
	12,11, 0, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,
	11,11,12, 0, 8, 8, 7, 7, 9, 9,10,10, 9, 9,10,10,
	11,11,12,12, 0, 0, 0, 7, 7, 9, 9,10,10,10, 9,10,
	10,11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,10,
	11,11,11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,
	10,11,11,12,12,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,
	10,10,11,11,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,11,11,11,11,12,12,13,13, 0, 0, 0, 0, 0,
	10,10,10,10,11,11,12,12,13,12,13,13, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,12,12,13,13,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,12,12,12,13,13,13,14, 0, 0,
	 0, 0, 0, 0, 0,11,11,12,12,12,12,13,13,13,14, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,13,13,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,13,13,13,13,14,
	14,
};

static float _vq_quantthresh__44c2_s_p6_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c2_s_p6_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p6_0 = {
	_vq_quantthresh__44c2_s_p6_0,
	_vq_quantmap__44c2_s_p6_0,
	17,
	17
};

static static_codebook _44c2_s_p6_0 = {
	2, 289,
	_vq_lengthlist__44c2_s_p6_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c2_s_p6_0,
	NULL,
	&_vq_auxt__44c2_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c2_s_p7_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,11,
	 9, 9, 4, 7, 7,10, 9, 9,10, 9, 9, 7,10,10,11,10,
	11,11,10,11, 6, 9, 9,11,10,10,11,10,10, 6, 9, 9,
	11,10,11,11,10,10, 7,11,10,11,11,11,12,11,11, 6,
	 9, 9,11,10,10,11,11,10, 6, 9, 9,11,10,10,12,10,
	11,
};

static float _vq_quantthresh__44c2_s_p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c2_s_p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p7_0 = {
	_vq_quantthresh__44c2_s_p7_0,
	_vq_quantmap__44c2_s_p7_0,
	3,
	3
};

static static_codebook _44c2_s_p7_0 = {
	4, 81,
	_vq_lengthlist__44c2_s_p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c2_s_p7_0,
	NULL,
	&_vq_auxt__44c2_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c2_s_p7_1[] = {
	 2, 3, 4, 6, 6, 7, 7, 7, 7, 7, 7, 9, 7, 7, 6, 6,
	 7, 7, 8, 8, 8, 8, 9, 6, 6, 6, 6, 7, 7, 8, 8, 8,
	 8,10, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,10,10,10, 7,
	 7, 7, 7, 8, 8, 8, 8,10,10,10, 7, 7, 8, 8, 8, 8,
	 8, 8,10,10,10, 7, 8, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 9, 9, 8, 8, 8, 8,10,10,
	10,10,10, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c2_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c2_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p7_1 = {
	_vq_quantthresh__44c2_s_p7_1,
	_vq_quantmap__44c2_s_p7_1,
	11,
	11
};

static static_codebook _44c2_s_p7_1 = {
	2, 121,
	_vq_lengthlist__44c2_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c2_s_p7_1,
	NULL,
	&_vq_auxt__44c2_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c2_s_p8_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 6, 5, 5,
	 7, 7, 8, 8, 8, 8, 9, 9,10,10, 7, 6, 5, 7, 7, 8,
	 8, 8, 8, 9, 9,10,10, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9,10,10,10,10,11,11,11,11, 0,13,
	13, 9, 9,10,10,10,10,11,11,12,12, 0, 0, 0,10,10,
	10,10,11,11,12,12,12,13, 0, 0, 0,10,10,10,10,11,
	11,12,12,12,12, 0, 0, 0,14,14,10,11,11,11,12,12,
	13,13, 0, 0, 0,14,14,11,10,11,11,13,12,13,13, 0,
	 0, 0, 0, 0,12,12,11,12,13,12,14,14, 0, 0, 0, 0,
	 0,12,12,12,12,13,12,14,14,
};

static float _vq_quantthresh__44c2_s_p8_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c2_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p8_0 = {
	_vq_quantthresh__44c2_s_p8_0,
	_vq_quantmap__44c2_s_p8_0,
	13,
	13
};

static static_codebook _44c2_s_p8_0 = {
	2, 169,
	_vq_lengthlist__44c2_s_p8_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c2_s_p8_0,
	NULL,
	&_vq_auxt__44c2_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p8_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c2_s_p8_1[] = {
	 2, 4, 4, 5, 4, 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c2_s_p8_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c2_s_p8_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p8_1 = {
	_vq_quantthresh__44c2_s_p8_1,
	_vq_quantmap__44c2_s_p8_1,
	5,
	5
};

static static_codebook _44c2_s_p8_1 = {
	2, 25,
	_vq_lengthlist__44c2_s_p8_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c2_s_p8_1,
	NULL,
	&_vq_auxt__44c2_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c2_s_p9_0[] = {
	 1, 5, 4,12,12,12,12,12,12,12,12,12,12, 4, 9, 8,
	11,11,11,11,11,11,11,11,11,11, 2, 8, 7,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,10,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,
};

static float _vq_quantthresh__44c2_s_p9_0[] = {
	-1215.5, -994.5, -773.5, -552.5, -331.5, -110.5, 110.5, 331.5,
	552.5, 773.5, 994.5, 1215.5,
};

static long _vq_quantmap__44c2_s_p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p9_0 = {
	_vq_quantthresh__44c2_s_p9_0,
	_vq_quantmap__44c2_s_p9_0,
	13,
	13
};

static static_codebook _44c2_s_p9_0 = {
	2, 169,
	_vq_lengthlist__44c2_s_p9_0,
	1, -514541568, 1627103232, 4, 0,
	_vq_quantlist__44c2_s_p9_0,
	NULL,
	&_vq_auxt__44c2_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p9_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c2_s_p9_1[] = {
	 1, 4, 4, 6, 6, 7, 6, 8, 8,10, 9,10,10, 6, 5, 5,
	 7, 7, 8, 7,10, 9,11,11,12,13, 6, 5, 5, 7, 7, 8,
	 8,10,10,11,11,13,13,18, 8, 8, 8, 8, 9, 9,10,10,
	12,12,12,13,18, 8, 8, 8, 8, 9, 9,10,10,12,12,13,
	13,18,11,11, 8, 8,10,10,11,11,12,11,13,12,18,11,
	11, 9, 7,10,10,11,11,11,12,12,13,17,17,17,10,10,
	11,11,12,12,12,10,12,12,17,17,17,11,10,11,10,13,
	12,11,12,12,12,17,17,17,15,14,11,11,12,11,13,10,
	13,12,17,17,17,14,14,12,10,11,11,13,13,13,13,17,
	17,16,17,16,13,13,12,10,13,10,14,13,17,16,17,16,
	17,13,12,12,10,13,11,14,14,
};

static float _vq_quantthresh__44c2_s_p9_1[] = {
	-93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5, 25.5,
	42.5, 59.5, 76.5, 93.5,
};

static long _vq_quantmap__44c2_s_p9_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p9_1 = {
	_vq_quantthresh__44c2_s_p9_1,
	_vq_quantmap__44c2_s_p9_1,
	13,
	13
};

static static_codebook _44c2_s_p9_1 = {
	2, 169,
	_vq_lengthlist__44c2_s_p9_1,
	1, -522616832, 1620115456, 4, 0,
	_vq_quantlist__44c2_s_p9_1,
	NULL,
	&_vq_auxt__44c2_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44c2_s_p9_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c2_s_p9_2[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
	 8,10, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9,10, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9,10, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 8, 7, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 9, 9,10,11,11, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9,10, 9, 9, 9,10,11,10, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 9,10,10,10,10,11,10, 8, 8, 9, 9, 9, 9,
	 9, 9,10, 9, 9,10, 9,10,11,10,11,11,11, 8, 8, 9,
	 9, 9, 9, 9, 9, 9, 9,10,10,11,11,11,11,11, 9, 9,
	 9, 9, 9, 9,10, 9, 9, 9,10,10,11,11,11,11,11, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10, 9,10,11,11,11,11,11,
	 9, 9, 9, 9,10,10, 9, 9, 9,10,10,10,11,11,11,11,
	11,11,11, 9, 9, 9,10, 9, 9,10,10,10,10,11,11,10,
	11,11,11,11,10, 9,10,10, 9, 9, 9, 9,10,10,11,10,
	11,11,11,11,11, 9, 9, 9, 9,10, 9,10,10,10,10,11,
	10,11,11,11,11,11,10,10, 9, 9,10, 9,10,10,10,10,
	10,10,10,11,11,11,11,11,11, 9, 9,10, 9,10, 9,10,
	10,
};

static float _vq_quantthresh__44c2_s_p9_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c2_s_p9_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c2_s_p9_2 = {
	_vq_quantthresh__44c2_s_p9_2,
	_vq_quantmap__44c2_s_p9_2,
	17,
	17
};

static static_codebook _44c2_s_p9_2 = {
	2, 289,
	_vq_lengthlist__44c2_s_p9_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c2_s_p9_2,
	NULL,
	&_vq_auxt__44c2_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44c2_s_short[] = {
	11, 9,13,12,12,11,12,12,13,15, 8, 2,11, 4, 8, 5,
	 7,10,12,15,13, 7,10, 9, 8, 8,10,13,17,17,11, 4,
	12, 5, 9, 5, 8,11,14,16,12, 6, 8, 7, 6, 6, 8,11,
	13,16,11, 4, 9, 5, 6, 4, 6,10,13,16,11, 6,11, 7,
	 7, 6, 7,10,13,15,13, 9,12, 9, 8, 6, 8,10,12,14,
	14,10,10, 8, 6, 5, 6, 9,11,13,15,11,11, 9, 6, 5,
	 6, 8, 9,12,
};

static static_codebook _huff_book__44c2_s_short = {
	2, 100,
	_huff_lengthlist__44c2_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c3_s_long[] = {
	 5, 6,11,11,11,11,10,10,12,11, 5, 2,11, 5, 6, 6,
	 7, 9,11,13,13,10, 7,11, 6, 7, 8, 9,10,12,11, 5,
	11, 6, 8, 7, 9,11,14,15,11, 6, 6, 8, 4, 5, 7, 8,
	10,13,10, 5, 7, 7, 5, 5, 6, 8,10,11,10, 7, 7, 8,
	 6, 5, 5, 7, 9, 9,11, 8, 8,11, 8, 7, 6, 6, 7, 9,
	12,11,10,13, 9, 9, 7, 7, 7, 9,11,13,12,15,12,11,
	 9, 8, 8, 8,
};

static static_codebook _huff_book__44c3_s_long = {
	2, 100,
	_huff_lengthlist__44c3_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c3_s_p1_0[] = {
	 2, 4, 4, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 0,
	 0, 0, 5, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,
	 0, 0, 0, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 6, 8, 7, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,
	 0, 0, 0, 8, 8, 9, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8, 8, 0, 0,
	 0, 0, 0, 0, 7, 9, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 6, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0,
	 0, 0, 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9,
	 0, 0, 0, 0, 0, 0, 8, 9, 8, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c3_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c3_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p1_0 = {
	_vq_quantthresh__44c3_s_p1_0,
	_vq_quantmap__44c3_s_p1_0,
	3,
	3
};

static static_codebook _44c3_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__44c3_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c3_s_p1_0,
	NULL,
	&_vq_auxt__44c3_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c3_s_p2_0[] = {
	 2, 5, 5, 0, 0, 0, 5, 5, 0, 0, 0, 5, 5, 0, 0, 0,
	 7, 8, 0, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 7,
	 7, 0, 0, 0, 7, 7, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
	 0, 0, 5, 6, 6, 0, 0, 0, 7, 7, 0, 0, 0, 7, 7, 0,
	 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 7, 7, 0, 0,
	 0, 7, 7, 0, 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 5,
	 7, 7, 0, 0, 0, 7, 7, 0, 0, 0, 7, 7, 0, 0, 0, 9,
	 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 5, 7, 7, 0, 0, 0, 7, 7, 0, 0, 0, 7, 7,
	 0, 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0,
	 0, 0, 7, 7, 0, 0, 0, 7, 7, 0, 0, 0, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 8,10,10, 0, 0, 0, 9, 9, 0, 0, 0, 9, 9, 0, 0, 0,
	10,10, 0, 0, 0, 0, 0, 0, 0, 8,10,10, 0, 0, 0, 9,
	 9, 0, 0, 0, 9, 9, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c3_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c3_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p2_0 = {
	_vq_quantthresh__44c3_s_p2_0,
	_vq_quantmap__44c3_s_p2_0,
	5,
	5
};

static static_codebook _44c3_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c3_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c3_s_p2_0,
	NULL,
	&_vq_auxt__44c3_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c3_s_p3_0[] = {
	 2, 4, 3, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 6, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c3_s_p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c3_s_p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p3_0 = {
	_vq_quantthresh__44c3_s_p3_0,
	_vq_quantmap__44c3_s_p3_0,
	5,
	5
};

static static_codebook _44c3_s_p3_0 = {
	4, 625,
	_vq_lengthlist__44c3_s_p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c3_s_p3_0,
	NULL,
	&_vq_auxt__44c3_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c3_s_p4_0[] = {
	 2, 3, 3, 6, 6, 0, 0, 0, 0, 0, 4, 4, 6, 6, 0, 0,
	 0, 0, 0, 4, 4, 6, 6, 0, 0, 0, 0, 0, 5, 5, 6, 6,
	 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0,
	 7, 8, 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0,
	 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c3_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c3_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p4_0 = {
	_vq_quantthresh__44c3_s_p4_0,
	_vq_quantmap__44c3_s_p4_0,
	9,
	9
};

static static_codebook _44c3_s_p4_0 = {
	2, 81,
	_vq_lengthlist__44c3_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c3_s_p4_0,
	NULL,
	&_vq_auxt__44c3_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c3_s_p5_0[] = {
	 1, 3, 4, 6, 6, 7, 7, 9, 9, 0, 5, 5, 7, 7, 7, 8,
	 9, 9, 0, 5, 5, 7, 7, 8, 8, 9, 9, 0, 7, 7, 8, 8,
	 8, 8,10,10, 0, 0, 0, 8, 8, 8, 8,10,10, 0, 0, 0,
	 9, 9, 9, 9,10,10, 0, 0, 0, 9, 9, 9, 9,10,10, 0,
	 0, 0,10,10,10,10,11,11, 0, 0, 0, 0, 0,10,10,11,
	11,
};

static float _vq_quantthresh__44c3_s_p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c3_s_p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p5_0 = {
	_vq_quantthresh__44c3_s_p5_0,
	_vq_quantmap__44c3_s_p5_0,
	9,
	9
};

static static_codebook _44c3_s_p5_0 = {
	2, 81,
	_vq_lengthlist__44c3_s_p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c3_s_p5_0,
	NULL,
	&_vq_auxt__44c3_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p6_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c3_s_p6_0[] = {
	 2, 3, 3, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,11,
	10, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,10,
	11,11, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,
	10,11,11, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,11,12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11,12,12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9,
	 9,10,10,11,11,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,10,11,11,12,12, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,11,11,12,12, 0, 0, 0, 0, 0, 9, 8,
	 9, 9,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 8,
	 8, 9, 9,10,10,11,11,12,11,12,12, 0, 0, 0, 0, 0,
	 9,10,10,10,11,11,11,11,12,12,13,13, 0, 0, 0, 0,
	 0, 0, 0,10,10,10,10,11,11,12,12,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,11,11,12,12,12,12,13,13, 0, 0,
	 0, 0, 0, 0, 0,11,11,11,11,12,12,12,12,13,13, 0,
	 0, 0, 0, 0, 0, 0,11,11,12,12,12,12,13,13,13,13,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,13,
	13,
};

static float _vq_quantthresh__44c3_s_p6_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c3_s_p6_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p6_0 = {
	_vq_quantthresh__44c3_s_p6_0,
	_vq_quantmap__44c3_s_p6_0,
	17,
	17
};

static static_codebook _44c3_s_p6_0 = {
	2, 289,
	_vq_lengthlist__44c3_s_p6_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c3_s_p6_0,
	NULL,
	&_vq_auxt__44c3_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c3_s_p7_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,11,
	 9, 9, 4, 7, 7,10, 9, 9,11, 9, 9, 7,10,10,11,11,
	10,12,11,11, 6, 9, 9,11,10,10,11,10,10, 6, 9, 9,
	11,10,10,11,10,10, 7,11,11,11,11,11,12,11,11, 6,
	 9, 9,11,10,10,11,10,10, 6, 9, 9,11,10,10,11,10,
	10,
};

static float _vq_quantthresh__44c3_s_p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c3_s_p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p7_0 = {
	_vq_quantthresh__44c3_s_p7_0,
	_vq_quantmap__44c3_s_p7_0,
	3,
	3
};

static static_codebook _44c3_s_p7_0 = {
	4, 81,
	_vq_lengthlist__44c3_s_p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c3_s_p7_0,
	NULL,
	&_vq_auxt__44c3_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c3_s_p7_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8,10, 5, 5, 6, 6,
	 7, 7, 8, 8, 8, 8,10, 5, 5, 6, 6, 7, 7, 8, 8, 8,
	 8,10, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10, 7,
	 7, 8, 7, 8, 8, 8, 8,10,10,10, 8, 8, 8, 8, 8, 8,
	 8, 8,10,10,10, 7, 8, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 9, 9, 8, 8, 9, 8,10,10,
	10,10,10, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c3_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c3_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p7_1 = {
	_vq_quantthresh__44c3_s_p7_1,
	_vq_quantmap__44c3_s_p7_1,
	11,
	11
};

static static_codebook _44c3_s_p7_1 = {
	2, 121,
	_vq_lengthlist__44c3_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c3_s_p7_1,
	NULL,
	&_vq_auxt__44c3_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c3_s_p8_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 6, 5, 5,
	 7, 7, 8, 8, 8, 8, 9, 9,10,10, 7, 5, 5, 7, 7, 8,
	 8, 8, 8, 9, 9,11,10, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9,10,10,10,10,11,11,11,12, 0,13,
	13, 9, 9,10,10,10,10,11,11,12,12, 0, 0, 0,10,10,
	10,10,11,11,12,12,12,12, 0, 0, 0,10,10,10,10,11,
	11,12,12,12,12, 0, 0, 0,14,14,11,11,11,11,12,12,
	13,13, 0, 0, 0,14,14,11,11,11,11,12,12,13,13, 0,
	 0, 0, 0, 0,12,12,12,12,13,13,14,13, 0, 0, 0, 0,
	 0,13,13,12,12,13,12,14,13,
};

static float _vq_quantthresh__44c3_s_p8_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c3_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p8_0 = {
	_vq_quantthresh__44c3_s_p8_0,
	_vq_quantmap__44c3_s_p8_0,
	13,
	13
};

static static_codebook _44c3_s_p8_0 = {
	2, 169,
	_vq_lengthlist__44c3_s_p8_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c3_s_p8_0,
	NULL,
	&_vq_auxt__44c3_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p8_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c3_s_p8_1[] = {
	 2, 4, 4, 5, 5, 6, 5, 5, 5, 5, 6, 4, 5, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c3_s_p8_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c3_s_p8_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p8_1 = {
	_vq_quantthresh__44c3_s_p8_1,
	_vq_quantmap__44c3_s_p8_1,
	5,
	5
};

static static_codebook _44c3_s_p8_1 = {
	2, 25,
	_vq_lengthlist__44c3_s_p8_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c3_s_p8_1,
	NULL,
	&_vq_auxt__44c3_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c3_s_p9_0[] = {
	 1, 4, 4,12,12,12,12,12,12,12,12,12,12, 4, 9, 8,
	12,12,12,12,12,12,12,12,12,12, 2, 9, 7,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,
};

static float _vq_quantthresh__44c3_s_p9_0[] = {
	-1402.5, -1147.5, -892.5, -637.5, -382.5, -127.5, 127.5, 382.5,
	637.5, 892.5, 1147.5, 1402.5,
};

static long _vq_quantmap__44c3_s_p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p9_0 = {
	_vq_quantthresh__44c3_s_p9_0,
	_vq_quantmap__44c3_s_p9_0,
	13,
	13
};

static static_codebook _44c3_s_p9_0 = {
	2, 169,
	_vq_lengthlist__44c3_s_p9_0,
	1, -514332672, 1627381760, 4, 0,
	_vq_quantlist__44c3_s_p9_0,
	NULL,
	&_vq_auxt__44c3_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44c3_s_p9_1[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 7, 9, 9,10,10,10,10, 6,
	 5, 5, 7, 7, 8, 8,10, 8,11,10,12,12,13,13, 6, 5,
	 5, 7, 7, 8, 8,10, 9,11,11,12,12,13,12,18, 8, 8,
	 8, 8, 9, 9,10, 9,11,10,12,12,13,13,18, 8, 8, 8,
	 8, 9, 9,10,10,11,11,13,12,14,13,18,11,11, 9, 9,
	10,10,11,11,11,12,13,12,13,14,18,11,11, 9, 8,11,
	10,11,11,11,11,12,12,14,13,18,18,18,10,11,10,11,
	12,12,12,12,13,12,14,13,18,18,18,10,11,11, 9,12,
	11,12,12,12,13,13,13,18,18,17,14,14,11,11,12,12,
	13,12,14,12,14,13,18,18,18,14,14,11,10,12, 9,12,
	13,13,13,13,13,18,18,17,16,18,13,13,12,12,13,11,
	14,12,14,14,17,18,18,17,18,13,12,13,10,12,11,14,
	14,14,14,17,18,18,18,18,15,16,12,12,13,10,14,12,
	14,15,18,18,18,16,17,16,14,12,11,13,10,13,13,14,
	15,
};

static float _vq_quantthresh__44c3_s_p9_1[] = {
	-110.5, -93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5,
	25.5, 42.5, 59.5, 76.5, 93.5, 110.5,
};

static long _vq_quantmap__44c3_s_p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p9_1 = {
	_vq_quantthresh__44c3_s_p9_1,
	_vq_quantmap__44c3_s_p9_1,
	15,
	15
};

static static_codebook _44c3_s_p9_1 = {
	2, 225,
	_vq_lengthlist__44c3_s_p9_1,
	1, -522338304, 1620115456, 4, 0,
	_vq_quantlist__44c3_s_p9_1,
	NULL,
	&_vq_auxt__44c3_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44c3_s_p9_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c3_s_p9_2[] = {
	 2, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
	 8,10, 6, 6, 7, 7, 8, 7, 8, 8, 8, 8, 8, 9, 9, 9,
	 9, 9,10, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9,10, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 7, 7, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9, 9,11,11,11, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10,10, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9,10,10,10, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9,10, 9,10,10,10,11,11, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,11,10,11,11,11, 9, 9,
	 9, 9, 9, 9,10,10, 9, 9,10, 9,11,10,11,11,11, 9,
	 9, 9, 9, 9, 9, 9, 9,10,10,10, 9,11,11,11,11,11,
	 9, 9, 9, 9,10,10, 9, 9, 9, 9,10, 9,11,11,11,11,
	11,11,11, 9, 9, 9, 9, 9, 9,10,10,10,10,11,11,11,
	11,11,11,11,10, 9,10,10, 9,10, 9, 9,10, 9,11,10,
	10,11,11,11,11, 9,10, 9, 9, 9, 9,10,10,10,10,11,
	11,11,11,11,11,10,10,10, 9, 9,10, 9,10, 9,10,10,
	10,10,11,11,11,11,11,11,11, 9, 9, 9, 9, 9,10,10,
	10,
};

static float _vq_quantthresh__44c3_s_p9_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c3_s_p9_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c3_s_p9_2 = {
	_vq_quantthresh__44c3_s_p9_2,
	_vq_quantmap__44c3_s_p9_2,
	17,
	17
};

static static_codebook _44c3_s_p9_2 = {
	2, 289,
	_vq_lengthlist__44c3_s_p9_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c3_s_p9_2,
	NULL,
	&_vq_auxt__44c3_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44c3_s_short[] = {
	10, 9,13,11,14,10,12,13,13,14, 7, 2,12, 5,10, 5,
	 7,10,12,14,12, 6, 9, 8, 7, 7, 9,11,13,16,10, 4,
	12, 5,10, 6, 8,12,14,16,12, 6, 8, 7, 6, 5, 7,11,
	12,16,10, 4, 8, 5, 6, 4, 6, 9,13,16,10, 6,10, 7,
	 7, 6, 7, 9,13,15,12, 9,11, 9, 8, 6, 7,10,12,14,
	14,11,10, 9, 6, 5, 6, 9,11,13,15,13,11,10, 6, 5,
	 6, 8, 9,11,
};

static static_codebook _huff_book__44c3_s_short = {
	2, 100,
	_huff_lengthlist__44c3_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c4_s_long[] = {
	 4, 7,11,11,11,11,10,11,12,11, 5, 2,11, 5, 6, 6,
	 7, 9,11,12,11, 9, 6,10, 6, 7, 8, 9,10,11,11, 5,
	11, 7, 8, 8, 9,11,13,14,11, 6, 5, 8, 4, 5, 7, 8,
	10,11,10, 6, 7, 7, 5, 5, 6, 8, 9,11,10, 7, 8, 9,
	 6, 6, 6, 7, 8, 9,11, 9, 9,11, 7, 7, 6, 6, 7, 9,
	12,12,10,13, 9, 8, 7, 7, 7, 8,11,13,11,14,11,10,
	 9, 8, 7, 7,
};

static static_codebook _huff_book__44c4_s_long = {
	2, 100,
	_huff_lengthlist__44c4_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c4_s_p1_0[] = {
	 2, 4, 4, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 0,
	 0, 0, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,
	 0, 0, 0, 6, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 6, 8, 7, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,
	 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8, 8, 0, 0,
	 0, 0, 0, 0, 8, 9, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 6, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0,
	 0, 0, 0, 0, 0, 8, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9,
	 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c4_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c4_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p1_0 = {
	_vq_quantthresh__44c4_s_p1_0,
	_vq_quantmap__44c4_s_p1_0,
	3,
	3
};

static static_codebook _44c4_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__44c4_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c4_s_p1_0,
	NULL,
	&_vq_auxt__44c4_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c4_s_p2_0[] = {
	 2, 5, 5, 0, 0, 0, 5, 5, 0, 0, 0, 5, 5, 0, 0, 0,
	 7, 7, 0, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 7,
	 7, 0, 0, 0, 7, 7, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
	 0, 0, 5, 6, 6, 0, 0, 0, 7, 7, 0, 0, 0, 7, 7, 0,
	 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 5, 8, 7, 0, 0, 0, 7, 7, 0, 0,
	 0, 7, 7, 0, 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 5,
	 7, 8, 0, 0, 0, 7, 7, 0, 0, 0, 7, 7, 0, 0, 0, 9,
	 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 5, 7, 7, 0, 0, 0, 7, 7, 0, 0, 0, 7, 7,
	 0, 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0,
	 0, 0, 7, 7, 0, 0, 0, 7, 7, 0, 0, 0, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 7,10,10, 0, 0, 0, 9, 9, 0, 0, 0, 9, 9, 0, 0, 0,
	10,10, 0, 0, 0, 0, 0, 0, 0, 8,10,10, 0, 0, 0, 9,
	 9, 0, 0, 0, 9, 9, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c4_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c4_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p2_0 = {
	_vq_quantthresh__44c4_s_p2_0,
	_vq_quantmap__44c4_s_p2_0,
	5,
	5
};

static static_codebook _44c4_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c4_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c4_s_p2_0,
	NULL,
	&_vq_auxt__44c4_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c4_s_p3_0[] = {
	 2, 3, 3, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 4, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 4, 5, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 6, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c4_s_p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c4_s_p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p3_0 = {
	_vq_quantthresh__44c4_s_p3_0,
	_vq_quantmap__44c4_s_p3_0,
	5,
	5
};

static static_codebook _44c4_s_p3_0 = {
	4, 625,
	_vq_lengthlist__44c4_s_p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c4_s_p3_0,
	NULL,
	&_vq_auxt__44c4_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c4_s_p4_0[] = {
	 2, 3, 3, 6, 6, 0, 0, 0, 0, 0, 4, 4, 6, 6, 0, 0,
	 0, 0, 0, 4, 4, 6, 6, 0, 0, 0, 0, 0, 5, 5, 6, 6,
	 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0,
	 7, 8, 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0,
	 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c4_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c4_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p4_0 = {
	_vq_quantthresh__44c4_s_p4_0,
	_vq_quantmap__44c4_s_p4_0,
	9,
	9
};

static static_codebook _44c4_s_p4_0 = {
	2, 81,
	_vq_lengthlist__44c4_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c4_s_p4_0,
	NULL,
	&_vq_auxt__44c4_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c4_s_p5_0[] = {
	 2, 3, 3, 6, 6, 7, 7, 9, 9, 0, 4, 4, 6, 6, 7, 7,
	 9, 9, 0, 4, 5, 6, 6, 7, 7, 9, 9, 0, 6, 6, 7, 7,
	 8, 8,10,10, 0, 0, 0, 7, 7, 8, 8,10, 9, 0, 0, 0,
	 9, 8, 8, 8,10,10, 0, 0, 0, 8, 8, 8, 8,10,10, 0,
	 0, 0,10,10, 9, 9,11,11, 0, 0, 0, 0, 0, 9, 9,10,
	10,
};

static float _vq_quantthresh__44c4_s_p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c4_s_p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p5_0 = {
	_vq_quantthresh__44c4_s_p5_0,
	_vq_quantmap__44c4_s_p5_0,
	9,
	9
};

static static_codebook _44c4_s_p5_0 = {
	2, 81,
	_vq_lengthlist__44c4_s_p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c4_s_p5_0,
	NULL,
	&_vq_auxt__44c4_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p6_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c4_s_p6_0[] = {
	 2, 4, 4, 6, 6, 8, 8, 9, 9, 8, 8, 9, 9,10,10,11,
	11, 0, 4, 4, 6, 6, 8, 8, 9, 9, 9, 9,10,10,11,11,
	11,11, 0, 4, 4, 7, 6, 8, 8, 9, 9, 9, 9,10,10,11,
	11,11,11, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,11,12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11,12,12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9,
	 9,10,10,11,11,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,11,11,12,12, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,11,11,12,12, 0, 0, 0, 0, 0, 9, 9,
	 9,10,10,10,11,11,11,11,12,12, 0, 0, 0, 0, 0, 9,
	 9, 9, 9,10,10,11,11,11,12,12,12, 0, 0, 0, 0, 0,
	10,10,10,10,11,11,11,11,12,12,13,12, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,11,11,12,12,12,12, 0, 0, 0,
	 0, 0, 0, 0,11,11,11,11,12,12,12,12,13,13, 0, 0,
	 0, 0, 0, 0, 0,11,11,11,11,12,12,12,12,13,13, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,12,12,13,13,13,13,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,12,12,12,13,13,
	13,
};

static float _vq_quantthresh__44c4_s_p6_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c4_s_p6_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p6_0 = {
	_vq_quantthresh__44c4_s_p6_0,
	_vq_quantmap__44c4_s_p6_0,
	17,
	17
};

static static_codebook _44c4_s_p6_0 = {
	2, 289,
	_vq_lengthlist__44c4_s_p6_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c4_s_p6_0,
	NULL,
	&_vq_auxt__44c4_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c4_s_p7_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,11,
	 9, 9, 4, 7, 7,10, 9, 9,11, 9, 9, 7,10,10,11,11,
	10,11,11,11, 6, 9, 9,11,10,10,11,10,10, 6, 9, 9,
	11,10,10,11,10,10, 7,11,11,12,11,11,12,11,11, 6,
	 9, 9,11,10,10,11,10,10, 6, 9, 9,11,10,10,11,10,
	10,
};

static float _vq_quantthresh__44c4_s_p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c4_s_p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p7_0 = {
	_vq_quantthresh__44c4_s_p7_0,
	_vq_quantmap__44c4_s_p7_0,
	3,
	3
};

static static_codebook _44c4_s_p7_0 = {
	4, 81,
	_vq_lengthlist__44c4_s_p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c4_s_p7_0,
	NULL,
	&_vq_auxt__44c4_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c4_s_p7_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8,10, 5, 5, 6, 6,
	 7, 7, 8, 8, 8, 8,10, 5, 5, 6, 6, 7, 7, 8, 8, 8,
	 8,10, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10, 7,
	 7, 8, 8, 8, 8, 8, 8,10,10,10, 8, 7, 8, 8, 8, 8,
	 8, 8,10,10,10, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 9, 9, 8, 8, 9, 8,10,10,
	10,10,10, 8, 8, 8, 8, 9, 9,
};

static float _vq_quantthresh__44c4_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c4_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p7_1 = {
	_vq_quantthresh__44c4_s_p7_1,
	_vq_quantmap__44c4_s_p7_1,
	11,
	11
};

static static_codebook _44c4_s_p7_1 = {
	2, 121,
	_vq_lengthlist__44c4_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c4_s_p7_1,
	NULL,
	&_vq_auxt__44c4_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c4_s_p8_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 6, 5, 5,
	 7, 7, 8, 8, 8, 8, 9,10,11,11, 7, 5, 5, 7, 7, 8,
	 8, 9, 9,10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9, 9, 9,10,10,10,10,11,11, 0,13,
	13, 9, 9,10, 9,10,10,11,11,11,12, 0, 0, 0,10,10,
	10,10,10,10,11,11,12,12, 0, 0, 0,10,10,10,10,10,
	10,11,11,12,12, 0, 0, 0,14,14,11,11,11,11,12,12,
	12,12, 0, 0, 0,14,14,11,11,11,11,12,12,12,13, 0,
	 0, 0, 0, 0,12,12,12,12,12,12,13,13, 0, 0, 0, 0,
	 0,13,12,12,12,12,12,13,13,
};

static float _vq_quantthresh__44c4_s_p8_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c4_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p8_0 = {
	_vq_quantthresh__44c4_s_p8_0,
	_vq_quantmap__44c4_s_p8_0,
	13,
	13
};

static static_codebook _44c4_s_p8_0 = {
	2, 169,
	_vq_lengthlist__44c4_s_p8_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c4_s_p8_0,
	NULL,
	&_vq_auxt__44c4_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p8_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c4_s_p8_1[] = {
	 2, 4, 4, 5, 5, 6, 5, 5, 5, 5, 6, 5, 4, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c4_s_p8_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c4_s_p8_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p8_1 = {
	_vq_quantthresh__44c4_s_p8_1,
	_vq_quantmap__44c4_s_p8_1,
	5,
	5
};

static static_codebook _44c4_s_p8_1 = {
	2, 25,
	_vq_lengthlist__44c4_s_p8_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c4_s_p8_1,
	NULL,
	&_vq_auxt__44c4_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c4_s_p9_0[] = {
	 1, 3, 3,12,12,12,12,12,12,12,12,12,12, 4, 7, 7,
	12,12,12,12,12,12,12,12,12,12, 3, 8, 8,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,
};

static float _vq_quantthresh__44c4_s_p9_0[] = {
	-1732.5, -1417.5, -1102.5, -787.5, -472.5, -157.5, 157.5, 472.5,
	787.5, 1102.5, 1417.5, 1732.5,
};

static long _vq_quantmap__44c4_s_p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p9_0 = {
	_vq_quantthresh__44c4_s_p9_0,
	_vq_quantmap__44c4_s_p9_0,
	13,
	13
};

static static_codebook _44c4_s_p9_0 = {
	2, 169,
	_vq_lengthlist__44c4_s_p9_0,
	1, -513964032, 1628680192, 4, 0,
	_vq_quantlist__44c4_s_p9_0,
	NULL,
	&_vq_auxt__44c4_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44c4_s_p9_1[] = {
	 1, 4, 4, 5, 5, 7, 7, 9, 8,10, 9,10,10,10,10, 6,
	 5, 5, 7, 7, 9, 8,10, 9,11,10,12,12,13,13, 6, 5,
	 5, 7, 7, 9, 9,10,10,11,11,12,12,12,13,19, 8, 8,
	 8, 8, 9, 9,10,10,12,11,12,12,13,13,19, 8, 8, 8,
	 8, 9, 9,11,11,12,12,13,13,13,13,19,12,12, 9, 9,
	11,11,11,11,12,11,13,12,13,13,18,12,12, 9, 9,11,
	10,11,11,12,12,12,13,13,14,19,18,18,11,11,11,11,
	12,12,13,12,13,13,14,14,16,18,18,11,11,11,10,12,
	11,13,13,13,13,13,14,17,18,18,14,15,11,12,12,13,
	13,13,13,14,14,14,18,18,18,15,15,12,10,13,10,13,
	13,13,13,13,14,18,17,18,17,18,12,13,12,13,13,13,
	14,14,16,14,18,17,18,18,17,13,12,13,10,12,12,14,
	14,14,14,17,18,18,18,18,14,15,12,12,13,12,14,14,
	15,15,18,18,18,17,18,15,14,12,11,12,12,14,14,14,
	15,
};

static float _vq_quantthresh__44c4_s_p9_1[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__44c4_s_p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p9_1 = {
	_vq_quantthresh__44c4_s_p9_1,
	_vq_quantmap__44c4_s_p9_1,
	15,
	15
};

static static_codebook _44c4_s_p9_1 = {
	2, 225,
	_vq_lengthlist__44c4_s_p9_1,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__44c4_s_p9_1,
	NULL,
	&_vq_auxt__44c4_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44c4_s_p9_2[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__44c4_s_p9_2[] = {
	 2, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8,
	 8, 9, 9, 9, 9,11, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10,10,10,11, 6, 6, 7, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 9, 9,10, 9,10,10,10,10,11,
	 7, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9,10,10,10,
	10,10,10,10,12,11,11, 7, 7, 8, 8, 9, 9, 9, 9, 9,
	 9,10,10,10,10,10,10,10,10,12,11,12, 8, 8, 8, 8,
	 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,11,11,
	11, 8, 8, 8, 8, 9, 9, 9, 9,10,10,10,10,10,10,10,
	10,10,10,11,11,12, 9, 9, 9, 9, 9, 9,10, 9,10,10,
	10,10,10,10,10,10,10,10,11,11,11,11,11, 9, 9, 9,
	 9,10,10,10,10,10,10,10,10,10,10,10,10,11,12,11,
	11,11, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,
	10,10,11,11,11,11,11, 9, 9, 9, 9,10,10,10,10,10,
	10,10,10,10,10,10,10,11,11,11,12,12,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,11,12,11,12,
	11,11,11, 9,10,10,10,10,10,10,10,10,10,10,10,10,
	10,11,12,11,11,11,11,11,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,11,11,11,12,11,11,11,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,12,11,11,12,11,
	11,11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,
	10,10,10,10,10,11,11,11,11,12,12,11,11,11,11,11,
	11,11,10,10,10,10,10,10,10,10,12,12,12,11,11,11,
	12,11,11,11,10,10,10,10,10,10,10,10,10,10,10,12,
	11,12,12,12,12,12,11,12,11,11,10,10,10,10,10,10,
	10,10,10,10,12,12,12,12,11,11,11,11,11,11,11,10,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__44c4_s_p9_2[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__44c4_s_p9_2[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__44c4_s_p9_2 = {
	_vq_quantthresh__44c4_s_p9_2,
	_vq_quantmap__44c4_s_p9_2,
	21,
	21
};

static static_codebook _44c4_s_p9_2 = {
	2, 441,
	_vq_lengthlist__44c4_s_p9_2,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__44c4_s_p9_2,
	NULL,
	&_vq_auxt__44c4_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44c4_s_short[] = {
	 4, 7,14,10,15,10,12,15,16,15, 4, 2,11, 5,10, 6,
	 8,11,14,14,14,10, 7,11, 6, 8,10,11,13,15, 9, 4,
	11, 5, 9, 6, 9,12,14,15,14, 9, 6, 9, 4, 5, 7,10,
	12,13, 9, 5, 7, 6, 5, 5, 7,10,13,13,10, 8, 9, 8,
	 7, 6, 8,10,14,14,13,11,10,10, 7, 7, 8,11,14,15,
	13,12, 9, 9, 6, 5, 7,10,14,17,15,13,11,10, 6, 6,
	 7, 9,12,17,
};

static static_codebook _huff_book__44c4_s_short = {
	2, 100,
	_huff_lengthlist__44c4_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c5_s_long[] = {
	 3, 8, 9,13,10,12,12,12,12,12, 6, 4, 6, 8, 6, 8,
	10,10,11,12, 8, 5, 4,10, 4, 7, 8, 9,10,11,13, 8,
	10, 8, 9, 9,11,12,13,14,10, 6, 4, 9, 3, 5, 6, 8,
	10,11,11, 8, 6, 9, 5, 5, 6, 7, 9,11,12, 9, 7,11,
	 6, 6, 6, 7, 8,10,12,11, 9,12, 7, 7, 6, 6, 7, 9,
	13,12,10,13, 9, 8, 7, 7, 7, 8,11,15,11,15,11,10,
	 9, 8, 7, 7,
};

static static_codebook _huff_book__44c5_s_long = {
	2, 100,
	_huff_lengthlist__44c5_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c5_s_p1_0[] = {
	 2, 4, 4, 0, 0, 0, 0, 0, 0, 4, 7, 7, 0, 0, 0, 0,
	 0, 0, 4, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 7, 7, 0, 0, 0, 0, 0, 0, 7, 9, 8, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 7, 0, 0, 0, 0,
	 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 9,10,11, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8,10, 9, 0, 0, 0, 0, 0, 0, 9,10,11,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 7, 7, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,11,10, 0,
	 0, 0, 0, 0, 0, 8, 9,10, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 9,11,10, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c5_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c5_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p1_0 = {
	_vq_quantthresh__44c5_s_p1_0,
	_vq_quantmap__44c5_s_p1_0,
	3,
	3
};

static static_codebook _44c5_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__44c5_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c5_s_p1_0,
	NULL,
	&_vq_auxt__44c5_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c5_s_p2_0[] = {
	 2, 4, 4, 0, 0, 0, 5, 5, 0, 0, 0, 5, 5, 0, 0, 0,
	 8, 7, 0, 0, 0, 0, 0, 0, 0, 4, 6, 6, 0, 0, 0, 8,
	 8, 0, 0, 0, 8, 7, 0, 0, 0,10,10, 0, 0, 0, 0, 0,
	 0, 0, 4, 6, 6, 0, 0, 0, 8, 8, 0, 0, 0, 7, 8, 0,
	 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 5, 8, 7, 0, 0, 0, 8, 8, 0, 0,
	 0, 8, 8, 0, 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 5,
	 7, 8, 0, 0, 0, 8, 8, 0, 0, 0, 8, 8, 0, 0, 0,10,
	10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 5, 8, 8, 0, 0, 0, 8, 8, 0, 0, 0, 8, 8,
	 0, 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 5, 8, 8, 0,
	 0, 0, 8, 8, 0, 0, 0, 8, 8, 0, 0, 0,10,10, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 8,10,10, 0, 0, 0,10,10, 0, 0, 0, 9,10, 0, 0, 0,
	11,10, 0, 0, 0, 0, 0, 0, 0, 8,10,10, 0, 0, 0,10,
	10, 0, 0, 0,10,10, 0, 0, 0,10,11, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c5_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c5_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p2_0 = {
	_vq_quantthresh__44c5_s_p2_0,
	_vq_quantmap__44c5_s_p2_0,
	5,
	5
};

static static_codebook _44c5_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c5_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c5_s_p2_0,
	NULL,
	&_vq_auxt__44c5_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c5_s_p3_0[] = {
	 2, 4, 3, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 3, 5, 5, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 8, 8,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 5, 6, 6, 8, 8, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c5_s_p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c5_s_p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p3_0 = {
	_vq_quantthresh__44c5_s_p3_0,
	_vq_quantmap__44c5_s_p3_0,
	5,
	5
};

static static_codebook _44c5_s_p3_0 = {
	4, 625,
	_vq_lengthlist__44c5_s_p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c5_s_p3_0,
	NULL,
	&_vq_auxt__44c5_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c5_s_p4_0[] = {
	 2, 3, 3, 6, 6, 0, 0, 0, 0, 0, 4, 4, 6, 6, 0, 0,
	 0, 0, 0, 4, 4, 6, 6, 0, 0, 0, 0, 0, 5, 5, 6, 6,
	 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0,
	 7, 7, 0, 0, 0, 0, 0, 0, 0, 8, 7, 0, 0, 0, 0, 0,
	 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c5_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c5_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p4_0 = {
	_vq_quantthresh__44c5_s_p4_0,
	_vq_quantmap__44c5_s_p4_0,
	9,
	9
};

static static_codebook _44c5_s_p4_0 = {
	2, 81,
	_vq_lengthlist__44c5_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c5_s_p4_0,
	NULL,
	&_vq_auxt__44c5_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c5_s_p5_0[] = {
	 2, 4, 3, 6, 6, 7, 7, 9, 9, 0, 4, 4, 6, 6, 7, 7,
	 9, 9, 0, 4, 4, 6, 6, 7, 7, 9, 9, 0, 6, 6, 7, 7,
	 7, 7, 9, 9, 0, 0, 0, 7, 6, 7, 7, 9, 9, 0, 0, 0,
	 8, 8, 8, 8,10,10, 0, 0, 0, 8, 8, 8, 8,10,10, 0,
	 0, 0, 9, 9, 9, 9,10,10, 0, 0, 0, 0, 0, 9, 9,10,
	10,
};

static float _vq_quantthresh__44c5_s_p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c5_s_p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p5_0 = {
	_vq_quantthresh__44c5_s_p5_0,
	_vq_quantmap__44c5_s_p5_0,
	9,
	9
};

static static_codebook _44c5_s_p5_0 = {
	2, 81,
	_vq_lengthlist__44c5_s_p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c5_s_p5_0,
	NULL,
	&_vq_auxt__44c5_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p6_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c5_s_p6_0[] = {
	 2, 4, 4, 6, 6, 8, 8, 9, 9, 9, 9,10,10,10,10,11,
	11, 0, 4, 4, 6, 6, 8, 8, 9, 9, 9, 9,10,10,11,11,
	12,12, 0, 4, 4, 6, 6, 8, 8, 9, 9, 9, 9,10,10,11,
	11,12,12, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,12, 0, 0, 0, 7, 7, 9, 9,10,10,10,10,
	11,11,11,11,12,12, 0, 0, 0, 7, 7, 8, 9,10,10,10,
	10,11,11,11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,
	10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 9, 9,
	10,10,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 9,
	 9, 9,10,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0,
	10,10,10,10,11,11,11,12,12,12,13,13, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,11,11,12,12,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,11,11,12,12,12,13,13,13, 0, 0,
	 0, 0, 0, 0, 0,11,11,11,11,12,12,12,12,13,13, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,13,12,13,13,13,13,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,13,
	13,
};

static float _vq_quantthresh__44c5_s_p6_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c5_s_p6_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p6_0 = {
	_vq_quantthresh__44c5_s_p6_0,
	_vq_quantmap__44c5_s_p6_0,
	17,
	17
};

static static_codebook _44c5_s_p6_0 = {
	2, 289,
	_vq_lengthlist__44c5_s_p6_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c5_s_p6_0,
	NULL,
	&_vq_auxt__44c5_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c5_s_p7_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,11,
	 9, 9, 4, 7, 7,10, 9, 9,11, 9, 9, 7,10,10,11,11,
	10,11,11,11, 6, 9, 9,11,10,10,11,10,10, 6, 9, 9,
	11,10,10,11,10,10, 7,11,11,12,11,11,12,11,11, 6,
	 9, 9,11,10,10,11,10,10, 6, 9, 9,11,10,10,11,10,
	10,
};

static float _vq_quantthresh__44c5_s_p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c5_s_p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p7_0 = {
	_vq_quantthresh__44c5_s_p7_0,
	_vq_quantmap__44c5_s_p7_0,
	3,
	3
};

static static_codebook _44c5_s_p7_0 = {
	4, 81,
	_vq_lengthlist__44c5_s_p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c5_s_p7_0,
	NULL,
	&_vq_auxt__44c5_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c5_s_p7_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8,10, 5, 5, 6, 6,
	 7, 7, 8, 8, 8, 8,10, 5, 5, 6, 6, 7, 7, 8, 8, 8,
	 8,10, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10, 7,
	 7, 8, 8, 8, 8, 8, 8,10,10,10, 7, 7, 8, 8, 8, 8,
	 8, 8,10,10,10, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 8, 9,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 9, 9, 8, 8, 8, 8,10,10,
	10,10,10, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c5_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c5_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p7_1 = {
	_vq_quantthresh__44c5_s_p7_1,
	_vq_quantmap__44c5_s_p7_1,
	11,
	11
};

static static_codebook _44c5_s_p7_1 = {
	2, 121,
	_vq_lengthlist__44c5_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c5_s_p7_1,
	NULL,
	&_vq_auxt__44c5_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c5_s_p8_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 6, 5, 5,
	 7, 7, 8, 8, 8, 9,10,10,10,10, 7, 5, 5, 7, 7, 8,
	 8, 9, 9,10,10,10,10, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9, 9,10,10,10,10,10,11,11, 0,13,
	13, 9, 9, 9, 9,10,10,11,11,11,11, 0, 0, 0,10,10,
	10,10,10,10,11,11,11,11, 0, 0, 0,10,10,10,10,10,
	10,11,11,12,12, 0, 0, 0,14,14,11,11,11,11,12,12,
	12,12, 0, 0, 0,14,14,11,11,11,11,12,12,12,12, 0,
	 0, 0, 0, 0,12,12,12,12,12,12,13,13, 0, 0, 0, 0,
	 0,12,12,12,12,12,12,13,13,
};

static float _vq_quantthresh__44c5_s_p8_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c5_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p8_0 = {
	_vq_quantthresh__44c5_s_p8_0,
	_vq_quantmap__44c5_s_p8_0,
	13,
	13
};

static static_codebook _44c5_s_p8_0 = {
	2, 169,
	_vq_lengthlist__44c5_s_p8_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c5_s_p8_0,
	NULL,
	&_vq_auxt__44c5_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p8_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c5_s_p8_1[] = {
	 2, 4, 4, 5, 5, 6, 5, 5, 5, 5, 6, 4, 5, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c5_s_p8_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c5_s_p8_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p8_1 = {
	_vq_quantthresh__44c5_s_p8_1,
	_vq_quantmap__44c5_s_p8_1,
	5,
	5
};

static static_codebook _44c5_s_p8_1 = {
	2, 25,
	_vq_lengthlist__44c5_s_p8_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c5_s_p8_1,
	NULL,
	&_vq_auxt__44c5_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p9_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44c5_s_p9_0[] = {
	 1, 3, 3,13,13,13,13,13,13,13,13,13,13,13,13, 4,
	 7, 7,13,13,13,13,13,13,13,13,13,13,13,13, 3, 8,
	 6,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,12,12,12,12,12,12,12,
	12,
};

static float _vq_quantthresh__44c5_s_p9_0[] = {
	-2320.5, -1963.5, -1606.5, -1249.5, -892.5, -535.5, -178.5, 178.5,
	535.5, 892.5, 1249.5, 1606.5, 1963.5, 2320.5,
};

static long _vq_quantmap__44c5_s_p9_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p9_0 = {
	_vq_quantthresh__44c5_s_p9_0,
	_vq_quantmap__44c5_s_p9_0,
	15,
	15
};

static static_codebook _44c5_s_p9_0 = {
	2, 225,
	_vq_lengthlist__44c5_s_p9_0,
	1, -512522752, 1628852224, 4, 0,
	_vq_quantlist__44c5_s_p9_0,
	NULL,
	&_vq_auxt__44c5_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p9_1[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c5_s_p9_1[] = {
	 1, 4, 4, 5, 5, 7, 7, 9, 8,10, 9,10,10,11,10,11,
	11, 6, 5, 5, 7, 7, 8, 9,10,10,11,10,12,11,12,11,
	13,12, 6, 5, 5, 7, 7, 9, 9,10,10,11,11,12,12,13,
	12,13,13,18, 8, 8, 8, 8, 9, 9,10,11,11,11,12,11,
	13,11,13,12,18, 8, 8, 8, 8,10,10,11,11,12,12,13,
	13,13,13,13,14,18,12,12, 9, 9,11,11,11,11,12,12,
	13,12,13,12,13,13,20,13,12, 9, 9,11,11,11,11,12,
	12,13,13,13,14,14,13,20,18,19,11,12,11,11,12,12,
	13,13,13,13,13,13,14,13,18,19,19,12,11,11,11,12,
	12,13,12,13,13,13,14,14,13,18,17,19,14,15,12,12,
	12,13,13,13,14,14,14,14,14,14,19,19,19,16,15,12,
	11,13,12,14,14,14,13,13,14,14,14,19,18,19,18,19,
	13,13,13,13,14,14,14,13,14,14,14,14,18,17,19,19,
	19,13,13,13,11,13,11,13,14,14,14,14,14,19,17,17,
	18,18,16,16,13,13,13,13,14,13,15,15,14,14,19,19,
	17,17,18,16,16,13,11,14,10,13,12,14,14,14,14,19,
	19,19,19,19,18,17,13,14,13,11,14,13,14,14,15,15,
	19,19,19,17,19,18,18,14,13,12,11,14,11,15,15,15,
	15,
};

static float _vq_quantthresh__44c5_s_p9_1[] = {
	-157.5, -136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5,
	10.5, 31.5, 52.5, 73.5, 94.5, 115.5, 136.5, 157.5,
};

static long _vq_quantmap__44c5_s_p9_1[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p9_1 = {
	_vq_quantthresh__44c5_s_p9_1,
	_vq_quantmap__44c5_s_p9_1,
	17,
	17
};

static static_codebook _44c5_s_p9_1 = {
	2, 289,
	_vq_lengthlist__44c5_s_p9_1,
	1, -520814592, 1620377600, 5, 0,
	_vq_quantlist__44c5_s_p9_1,
	NULL,
	&_vq_auxt__44c5_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44c5_s_p9_2[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__44c5_s_p9_2[] = {
	 3, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 9,11, 5, 6, 7, 7, 8, 7, 8, 8, 8, 8,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11, 5, 5, 7, 7, 7,
	 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,
	 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
	 9,10, 9,10,11,11,11, 7, 7, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9,10,10,10,10,10,10,11,11,11, 8, 8, 8, 8,
	 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,11,11,
	11, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,10,
	10,10,10,11,11,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	10,10,10,10,10,10,10,10,11,11,11,11,11, 9, 9, 9,
	 9, 9, 9,10, 9,10,10,10,10,10,10,10,10,11,11,11,
	11,11, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,
	10,10,11,11,11,11,11, 9, 9, 9, 9, 9, 9,10,10,10,
	10,10,10,10,10,10,10,11,11,11,11,11, 9, 9,10, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,
	11,11,11, 9, 9,10,10,10,10,10,10,10,10,10,10,10,
	10,11,11,11,11,11,11,11,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,11,11,11,11,11,11,11,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,
	11,11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,
	10,10,10,10,10,11,11,11,11,11,11,11,11,11,10,10,
	10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,
	11,11,11,10,10,10,10,10,10,10,10,10,10,10,10,11,
	11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,10,
	10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,10,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__44c5_s_p9_2[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__44c5_s_p9_2[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__44c5_s_p9_2 = {
	_vq_quantthresh__44c5_s_p9_2,
	_vq_quantmap__44c5_s_p9_2,
	21,
	21
};

static static_codebook _44c5_s_p9_2 = {
	2, 441,
	_vq_lengthlist__44c5_s_p9_2,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__44c5_s_p9_2,
	NULL,
	&_vq_auxt__44c5_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44c5_s_short[] = {
	 5, 8,10,14,11,11,12,16,15,17, 5, 5, 7, 9, 7, 8,
	10,13,17,17, 7, 5, 5,10, 5, 7, 8,11,13,15,10, 8,
	10, 8, 8, 8,11,15,18,18, 8, 5, 5, 8, 3, 4, 6,10,
	14,16, 9, 7, 6, 7, 4, 3, 5, 9,14,18,10, 9, 8,10,
	 6, 5, 6, 9,14,18,12,12,11,12, 8, 7, 8,11,14,18,
	14,13,12,10, 7, 5, 6, 9,14,18,14,14,13,10, 6, 5,
	 6, 8,11,16,
};

static static_codebook _huff_book__44c5_s_short = {
	2, 100,
	_huff_lengthlist__44c5_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c6_s_long[] = {
	 3, 8,11,13,14,14,13,13,16,14, 6, 3, 4, 7, 9, 9,
	10,11,14,13,10, 4, 3, 5, 7, 7, 9,10,13,15,12, 7,
	 4, 4, 6, 6, 8,10,13,15,12, 8, 6, 6, 6, 6, 8,10,
	13,14,11, 9, 7, 6, 6, 6, 7, 8,12,11,13,10, 9, 8,
	 7, 6, 6, 7,11,11,13,11,10, 9, 9, 7, 7, 6,10,11,
	13,13,13,13,13,11, 9, 8,10,12,12,15,15,16,15,12,
	11,10,10,12,
};

static static_codebook _huff_book__44c6_s_long = {
	2, 100,
	_huff_lengthlist__44c6_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c6_s_p1_0[] = {
	 1, 5, 5, 0, 5, 5, 0, 5, 5, 5, 8, 7, 0, 9, 9, 0,
	 9, 8, 5, 7, 8, 0, 9, 9, 0, 8, 9, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 5, 9, 8, 0, 8, 8, 0, 8, 8, 5, 8, 9,
	 0, 8, 8, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
	 9, 9, 0, 8, 8, 0, 8, 8, 5, 9, 9, 0, 8, 8, 0, 8,
	 8,
};

static float _vq_quantthresh__44c6_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c6_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p1_0 = {
	_vq_quantthresh__44c6_s_p1_0,
	_vq_quantmap__44c6_s_p1_0,
	3,
	3
};

static static_codebook _44c6_s_p1_0 = {
	4, 81,
	_vq_lengthlist__44c6_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c6_s_p1_0,
	NULL,
	&_vq_auxt__44c6_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c6_s_p2_0[] = {
	 3, 5, 5, 8, 8, 0, 5, 5, 8, 8, 0, 5, 5, 8, 8, 0,
	 7, 7, 9, 9, 0, 0, 0, 9, 9, 5, 7, 7, 9, 9, 0, 8,
	 8,10,10, 0, 8, 7,10, 9, 0,10,10,11,11, 0, 0, 0,
	11,11, 5, 7, 7, 9, 9, 0, 8, 8,10,10, 0, 7, 8, 9,
	10, 0,10,10,11,11, 0, 0, 0,11,11, 8, 9, 9,11,11,
	 0,11,11,12,12, 0,11,10,12,12, 0,13,14,14,14, 0,
	 0, 0,14,13, 8, 9, 9,11,11, 0,11,11,12,12, 0,10,
	11,12,12, 0,14,13,14,14, 0, 0, 0,13,14, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 5, 8, 7,11,10, 0, 7, 7,10,10,
	 0, 7, 7,10,10, 0, 9, 9,11,10, 0, 0, 0,11,11, 5,
	 7, 8,10,11, 0, 7, 7,10,10, 0, 7, 7,10,10, 0, 9,
	 9,10,11, 0, 0, 0,11,11, 8,10, 9,12,12, 0,10,10,
	12,12, 0,10,10,12,12, 0,12,12,13,13, 0, 0, 0,13,
	13, 8, 9,10,12,12, 0,10,10,11,12, 0,10,10,12,12,
	 0,12,12,13,13, 0, 0, 0,13,13, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 5, 8, 8,11,11, 0, 7, 7,10,10, 0, 7, 7,
	10,10, 0, 9, 9,10,11, 0, 0, 0,11,10, 5, 8, 8,11,
	11, 0, 7, 7,10,10, 0, 7, 7,10,10, 0, 9, 9,11,11,
	 0, 0, 0,10,11, 8,10,10,12,12, 0,10,10,12,12, 0,
	10,10,12,12, 0,12,13,13,13, 0, 0, 0,14,13, 8,10,
	10,12,12, 0,10,10,12,12, 0,10,10,12,12, 0,13,12,
	13,13, 0, 0, 0,13,13, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 7,10,10,14,13, 0, 9, 9,13,12, 0, 9, 9,12,12, 0,
	10,10,12,12, 0, 0, 0,12,12, 7,10,10,13,14, 0, 9,
	 9,12,13, 0, 9, 9,12,12, 0,10,10,12,12, 0, 0, 0,
	12,12, 9,11,11,14,13, 0,11,10,14,13, 0,11,11,13,
	13, 0,12,12,13,13, 0, 0, 0,13,13, 9,11,11,13,14,
	 0,10,11,13,14, 0,11,11,13,13, 0,12,12,13,13, 0,
	 0, 0,13,13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
	11,11,14,14, 0,11,11,13,13, 0,11,10,13,13, 0,12,
	12,13,13, 0, 0, 0,13,13, 9,11,11,14,14, 0,11,11,
	13,13, 0,10,11,13,13, 0,12,12,14,13, 0, 0, 0,13,
	13,
};

static float _vq_quantthresh__44c6_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c6_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p2_0 = {
	_vq_quantthresh__44c6_s_p2_0,
	_vq_quantmap__44c6_s_p2_0,
	5,
	5
};

static static_codebook _44c6_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c6_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c6_s_p2_0,
	NULL,
	&_vq_auxt__44c6_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c6_s_p3_0[] = {
	 2, 3, 4, 6, 6, 7, 7, 9, 9, 0, 4, 4, 6, 6, 7, 7,
	 9,10, 0, 4, 4, 6, 6, 7, 7,10, 9, 0, 5, 5, 7, 7,
	 8, 8,10,10, 0, 0, 0, 7, 6, 8, 8,10,10, 0, 0, 0,
	 7, 7, 9, 9,11,11, 0, 0, 0, 7, 7, 9, 9,11,11, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c6_s_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c6_s_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p3_0 = {
	_vq_quantthresh__44c6_s_p3_0,
	_vq_quantmap__44c6_s_p3_0,
	9,
	9
};

static static_codebook _44c6_s_p3_0 = {
	2, 81,
	_vq_lengthlist__44c6_s_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c6_s_p3_0,
	NULL,
	&_vq_auxt__44c6_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p4_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c6_s_p4_0[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9,10,10,
	10, 0, 4, 4, 6, 6, 8, 8, 9, 9, 9, 9,10,10,10,10,
	11,11, 0, 4, 4, 6, 6, 8, 8, 9, 9, 9, 9,10,10,10,
	10,11,11, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,11,11, 0, 0, 0, 7, 7, 9, 9,10,10,10,10,
	11,11,11,11,12,12, 0, 0, 0, 7, 7, 9, 9,10,10,10,
	10,11,11,11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,
	10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 8, 8, 9,
	 9,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c6_s_p4_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c6_s_p4_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p4_0 = {
	_vq_quantthresh__44c6_s_p4_0,
	_vq_quantmap__44c6_s_p4_0,
	17,
	17
};

static static_codebook _44c6_s_p4_0 = {
	2, 289,
	_vq_lengthlist__44c6_s_p4_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c6_s_p4_0,
	NULL,
	&_vq_auxt__44c6_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p5_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c6_s_p5_0[] = {
	 1, 4, 4, 5, 7, 7, 6, 7, 7, 4, 6, 6, 9, 9,10,10,
	10, 9, 4, 6, 6, 9,10, 9,10, 9,10, 6, 9, 9,10,12,
	11,10,11,11, 7,10, 9,11,12,12,12,12,12, 7,10,10,
	11,12,12,12,12,12, 6,10,10,10,12,12,11,12,12, 7,
	 9,10,11,12,12,12,12,12, 7,10, 9,12,12,12,12,12,
	12,
};

static float _vq_quantthresh__44c6_s_p5_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c6_s_p5_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p5_0 = {
	_vq_quantthresh__44c6_s_p5_0,
	_vq_quantmap__44c6_s_p5_0,
	3,
	3
};

static static_codebook _44c6_s_p5_0 = {
	4, 81,
	_vq_lengthlist__44c6_s_p5_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c6_s_p5_0,
	NULL,
	&_vq_auxt__44c6_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p5_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c6_s_p5_1[] = {
	 3, 5, 4, 6, 6, 7, 7, 8, 8, 8, 8,11, 4, 4, 6, 6,
	 7, 7, 8, 8, 8, 8,11, 4, 4, 6, 6, 7, 7, 8, 8, 8,
	 8,11, 6, 6, 6, 6, 8, 8, 8, 8, 9, 9,11,11,11, 6,
	 6, 7, 8, 8, 8, 8, 9,11,11,11, 7, 7, 8, 8, 8, 8,
	 8, 8,11,11,11, 7, 7, 8, 8, 8, 8, 8, 8,11,11,11,
	 8, 8, 8, 8, 8, 8, 8, 8,11,11,11,10,10, 8, 8, 8,
	 8, 8, 8,11,11,11,10,10, 8, 8, 8, 8, 8, 8,11,11,
	11,10,10, 7, 7, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c6_s_p5_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c6_s_p5_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p5_1 = {
	_vq_quantthresh__44c6_s_p5_1,
	_vq_quantmap__44c6_s_p5_1,
	11,
	11
};

static static_codebook _44c6_s_p5_1 = {
	2, 121,
	_vq_lengthlist__44c6_s_p5_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c6_s_p5_1,
	NULL,
	&_vq_auxt__44c6_s_p5_1,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c6_s_p6_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 8, 8,10, 9,10,10, 6, 5, 5,
	 7, 7, 9, 9, 9, 9,10,10,11,11, 6, 5, 5, 7, 7, 9,
	 9,10, 9,11,10,11,11, 0, 6, 6, 7, 7, 9, 9,10,10,
	11,11,12,12, 0, 7, 7, 7, 7, 9, 9,10,10,11,11,12,
	12, 0,11,11, 8, 8,10,10,11,11,12,12,12,12, 0,11,
	12, 9, 8,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

static float _vq_quantthresh__44c6_s_p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c6_s_p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p6_0 = {
	_vq_quantthresh__44c6_s_p6_0,
	_vq_quantmap__44c6_s_p6_0,
	13,
	13
};

static static_codebook _44c6_s_p6_0 = {
	2, 169,
	_vq_lengthlist__44c6_s_p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c6_s_p6_0,
	NULL,
	&_vq_auxt__44c6_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c6_s_p6_1[] = {
	 3, 4, 4, 5, 5, 5, 4, 4, 5, 5, 5, 4, 4, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c6_s_p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c6_s_p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p6_1 = {
	_vq_quantthresh__44c6_s_p6_1,
	_vq_quantmap__44c6_s_p6_1,
	5,
	5
};

static static_codebook _44c6_s_p6_1 = {
	2, 25,
	_vq_lengthlist__44c6_s_p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c6_s_p6_1,
	NULL,
	&_vq_auxt__44c6_s_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c6_s_p7_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 8, 8,10,10,11,10, 6, 5, 5,
	 7, 7, 8, 8, 9, 9,10,10,12,11, 6, 5, 5, 7, 7, 8,
	 8, 9, 9,10,10,12,11,21, 7, 7, 7, 7, 9, 9,10,10,
	11,11,12,12,21, 7, 7, 7, 7, 9, 9,10,10,11,11,12,
	12,21,12,12, 9, 9,10,10,11,11,11,11,12,12,21,12,
	12, 9, 9,10,10,11,11,12,12,12,12,21,21,21,11,11,
	10,10,11,12,12,12,13,13,21,21,21,11,11,10,10,12,
	12,12,12,13,13,21,21,21,15,15,11,11,12,12,13,13,
	13,13,21,21,21,15,16,11,11,12,12,13,13,14,14,21,
	21,21,21,20,13,13,13,13,13,13,14,14,20,20,20,20,
	20,13,13,13,13,13,13,14,14,
};

static float _vq_quantthresh__44c6_s_p7_0[] = {
	-60.5, -49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5,
	27.5, 38.5, 49.5, 60.5,
};

static long _vq_quantmap__44c6_s_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p7_0 = {
	_vq_quantthresh__44c6_s_p7_0,
	_vq_quantmap__44c6_s_p7_0,
	13,
	13
};

static static_codebook _44c6_s_p7_0 = {
	2, 169,
	_vq_lengthlist__44c6_s_p7_0,
	1, -523206656, 1618345984, 4, 0,
	_vq_quantlist__44c6_s_p7_0,
	NULL,
	&_vq_auxt__44c6_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c6_s_p7_1[] = {
	 3, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 9, 5, 5, 6, 6,
	 7, 7, 7, 7, 8, 7, 8, 5, 5, 6, 6, 7, 7, 7, 7, 7,
	 7, 9, 6, 6, 7, 7, 7, 7, 8, 7, 7, 8, 9, 9, 9, 7,
	 7, 7, 7, 7, 7, 7, 8, 9, 9, 9, 7, 7, 7, 7, 8, 8,
	 8, 8, 9, 9, 9, 7, 7, 7, 7, 7, 7, 8, 8, 9, 9, 9,
	 8, 8, 8, 8, 7, 7, 8, 8, 9, 9, 9, 9, 8, 8, 8, 7,
	 7, 8, 8, 9, 9, 9, 8, 8, 8, 8, 7, 7, 8, 8, 9, 9,
	 9, 8, 8, 7, 7, 7, 7, 8, 8,
};

static float _vq_quantthresh__44c6_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c6_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p7_1 = {
	_vq_quantthresh__44c6_s_p7_1,
	_vq_quantmap__44c6_s_p7_1,
	11,
	11
};

static static_codebook _44c6_s_p7_1 = {
	2, 121,
	_vq_lengthlist__44c6_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c6_s_p7_1,
	NULL,
	&_vq_auxt__44c6_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p8_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44c6_s_p8_0[] = {
	 1, 4, 4, 7, 7, 8, 8, 7, 7, 8, 7, 9, 8,10, 9, 6,
	 5, 5, 8, 8, 9, 9, 8, 8, 9, 9,11,10,11,10, 6, 5,
	 5, 8, 8, 9, 9, 8, 8, 9, 9,10,10,11,11,18, 8, 8,
	 9, 8,10,10, 9, 9,10,10,10,10,11,10,18, 8, 8, 9,
	 9,10,10, 9, 9,10,10,11,11,12,12,18,12,13, 9,10,
	10,10, 9,10,10,10,11,11,12,11,18,13,13, 9, 9,10,
	10,10,10,10,10,11,11,12,12,18,18,18,10,10, 9, 9,
	11,11,11,11,11,12,12,12,18,18,18,10, 9,10, 9,11,
	10,11,11,11,11,13,12,18,18,18,14,13,10,10,11,11,
	12,12,12,12,12,12,18,18,18,14,13,10,10,11,10,12,
	12,12,12,12,12,18,18,18,18,18,12,12,11,11,12,12,
	13,13,13,14,18,18,18,18,18,12,12,11,11,12,11,13,
	13,14,13,18,18,18,18,18,16,16,11,12,12,13,13,13,
	14,13,18,18,18,18,18,16,15,12,11,12,11,13,11,15,
	14,
};

static float _vq_quantthresh__44c6_s_p8_0[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__44c6_s_p8_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p8_0 = {
	_vq_quantthresh__44c6_s_p8_0,
	_vq_quantmap__44c6_s_p8_0,
	15,
	15
};

static static_codebook _44c6_s_p8_0 = {
	2, 225,
	_vq_lengthlist__44c6_s_p8_0,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__44c6_s_p8_0,
	NULL,
	&_vq_auxt__44c6_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p8_1[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__44c6_s_p8_1[] = {
	 3, 5, 5, 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8,10, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 6, 6, 7, 7, 8,
	 8, 8, 8, 8, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9,10,
	 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9,10,11,11, 8, 7, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,11,11,11, 8, 8, 8, 8,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,11,
	11, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9,11,11,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,11,11,11,11,11, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10, 9,11,11,11,
	11,11, 9, 9, 9, 9, 9, 9,10, 9, 9,10, 9,10, 9, 9,
	10, 9,11,11,11,11,11, 9, 9, 9, 9, 9, 9, 9,10,10,
	10,10, 9,10,10, 9,10,11,11,11,11,11, 9, 9, 9, 9,
	10,10,10, 9,10,10,10,10, 9,10,10, 9,11,11,11,11,
	11,11,11, 9, 9, 9, 9,10,10,10,10, 9,10,10,10,10,
	10,11,11,11,11,11,11,11,10, 9,10,10,10,10,10,10,
	10, 9,10, 9,10,10,11,11,11,11,11,11,11,10, 9,10,
	 9,10,10, 9,10,10,10,10,10,10,10,11,11,11,11,11,
	11,11,10,10,10,10,10,10,10, 9,10,10,10,10,10, 9,
	11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,
	10,10,10,10,10,11,11,11,11,11,11,11,11,11,10,10,
	10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,
	11,11,11,10,10,10,10,10,10,10,10,10, 9,10,10,11,
	11,11,11,11,11,11,11,11,10,10,10, 9,10,10,10,10,
	10,10,10,10,10,11,11,11,11,11,11,11,11,10,11, 9,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__44c6_s_p8_1[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__44c6_s_p8_1[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p8_1 = {
	_vq_quantthresh__44c6_s_p8_1,
	_vq_quantmap__44c6_s_p8_1,
	21,
	21
};

static static_codebook _44c6_s_p8_1 = {
	2, 441,
	_vq_lengthlist__44c6_s_p8_1,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__44c6_s_p8_1,
	NULL,
	&_vq_auxt__44c6_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c6_s_p9_0[] = {
	 1, 3, 3,11,11,11,11,11,11,11,11,11,11, 4, 7, 7,
	11,11,11,11,11,11,11,11,11,11, 5, 8, 9,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__44c6_s_p9_0[] = {
	-3503.5, -2866.5, -2229.5, -1592.5, -955.5, -318.5, 318.5, 955.5,
	1592.5, 2229.5, 2866.5, 3503.5,
};

static long _vq_quantmap__44c6_s_p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p9_0 = {
	_vq_quantthresh__44c6_s_p9_0,
	_vq_quantmap__44c6_s_p9_0,
	13,
	13
};

static static_codebook _44c6_s_p9_0 = {
	2, 169,
	_vq_lengthlist__44c6_s_p9_0,
	1, -511845376, 1630791680, 4, 0,
	_vq_quantlist__44c6_s_p9_0,
	NULL,
	&_vq_auxt__44c6_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p9_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c6_s_p9_1[] = {
	 1, 4, 4, 7, 7, 7, 7, 7, 6, 8, 8, 8, 8, 6, 6, 6,
	 8, 8, 8, 8, 8, 7, 9, 8,10,10, 5, 6, 6, 8, 8, 9,
	 9, 8, 8,10,10,10,10,16, 9, 9, 9, 9, 9, 9, 9, 8,
	10, 9,11,11,16, 8, 9, 9, 9, 9, 9, 9, 9,10,10,11,
	11,16,13,13, 9, 9,10, 9, 9,10,11,11,11,12,16,13,
	14, 9, 8,10, 8, 9, 9,10,10,12,11,16,14,16, 9, 9,
	 9, 9,11,11,12,11,12,11,16,16,16, 9, 7, 9, 6,11,
	11,11,10,11,11,16,16,16,11,12, 9,10,11,11,12,11,
	13,13,16,16,16,12,11,10, 7,12,10,12,12,12,12,16,
	16,15,16,16,10,11,10,11,13,13,14,12,16,16,16,15,
	15,12,10,11,11,13,11,12,13,
};

static float _vq_quantthresh__44c6_s_p9_1[] = {
	-269.5, -220.5, -171.5, -122.5, -73.5, -24.5, 24.5, 73.5,
	122.5, 171.5, 220.5, 269.5,
};

static long _vq_quantmap__44c6_s_p9_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p9_1 = {
	_vq_quantthresh__44c6_s_p9_1,
	_vq_quantmap__44c6_s_p9_1,
	13,
	13
};

static static_codebook _44c6_s_p9_1 = {
	2, 169,
	_vq_lengthlist__44c6_s_p9_1,
	1, -518889472, 1622704128, 4, 0,
	_vq_quantlist__44c6_s_p9_1,
	NULL,
	&_vq_auxt__44c6_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44c6_s_p9_2[] = {
	24,
	23,
	25,
	22,
	26,
	21,
	27,
	20,
	28,
	19,
	29,
	18,
	30,
	17,
	31,
	16,
	32,
	15,
	33,
	14,
	34,
	13,
	35,
	12,
	36,
	11,
	37,
	10,
	38,
	9,
	39,
	8,
	40,
	7,
	41,
	6,
	42,
	5,
	43,
	4,
	44,
	3,
	45,
	2,
	46,
	1,
	47,
	0,
	48,
};

static long _vq_lengthlist__44c6_s_p9_2[] = {
	 2, 4, 3, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6,
	 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__44c6_s_p9_2[] = {
	-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5,
	-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
	8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
	16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5,
};

static long _vq_quantmap__44c6_s_p9_2[] = {
	   47,   45,   43,   41,   39,   37,   35,   33,
	   31,   29,   27,   25,   23,   21,   19,   17,
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,   18,   20,   22,   24,   26,   28,   30,
	   32,   34,   36,   38,   40,   42,   44,   46,
	   48,
};

static encode_aux_threshmatch _vq_auxt__44c6_s_p9_2 = {
	_vq_quantthresh__44c6_s_p9_2,
	_vq_quantmap__44c6_s_p9_2,
	49,
	49
};

static static_codebook _44c6_s_p9_2 = {
	1, 49,
	_vq_lengthlist__44c6_s_p9_2,
	1, -526909440, 1611661312, 6, 0,
	_vq_quantlist__44c6_s_p9_2,
	NULL,
	&_vq_auxt__44c6_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44c6_s_short[] = {
	 3, 9,11,11,13,14,19,17,17,19, 5, 4, 5, 8,10,10,
	13,16,18,19, 7, 4, 4, 5, 8, 9,12,14,17,19, 8, 6,
	 5, 5, 7, 7,10,13,16,18,10, 8, 7, 6, 5, 5, 8,11,
	17,19,11, 9, 7, 7, 5, 4, 5, 8,17,19,13,11, 8, 7,
	 7, 5, 5, 7,16,18,14,13, 8, 6, 6, 5, 5, 7,16,18,
	18,16,10, 8, 8, 7, 7, 9,16,18,18,18,12,10,10, 9,
	 9,10,17,18,
};

static static_codebook _huff_book__44c6_s_short = {
	2, 100,
	_huff_lengthlist__44c6_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c7_s_long[] = {
	 3, 8,11,13,15,14,14,13,15,14, 6, 4, 5, 7, 9,10,
	11,11,14,13,10, 4, 3, 5, 7, 8, 9,10,13,13,12, 7,
	 4, 4, 5, 6, 8, 9,12,14,13, 9, 6, 5, 5, 6, 8, 9,
	12,14,12, 9, 7, 6, 5, 5, 6, 8,11,11,12,11, 9, 8,
	 7, 6, 6, 7,10,11,13,11,10, 9, 8, 7, 6, 6, 9,11,
	13,13,12,12,12,10, 9, 8, 9,11,12,14,15,15,14,12,
	11,10,10,12,
};

static static_codebook _huff_book__44c7_s_long = {
	2, 100,
	_huff_lengthlist__44c7_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c7_s_p1_0[] = {
	 1, 5, 5, 0, 5, 5, 0, 5, 5, 5, 8, 7, 0, 9, 9, 0,
	 9, 8, 5, 7, 8, 0, 9, 9, 0, 8, 9, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 5, 9, 9, 0, 8, 8, 0, 8, 8, 5, 8, 9,
	 0, 8, 8, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
	 9, 9, 0, 8, 8, 0, 8, 8, 5, 8, 9, 0, 8, 8, 0, 8,
	 8,
};

static float _vq_quantthresh__44c7_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c7_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p1_0 = {
	_vq_quantthresh__44c7_s_p1_0,
	_vq_quantmap__44c7_s_p1_0,
	3,
	3
};

static static_codebook _44c7_s_p1_0 = {
	4, 81,
	_vq_lengthlist__44c7_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c7_s_p1_0,
	NULL,
	&_vq_auxt__44c7_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c7_s_p2_0[] = {
	 3, 5, 5, 8, 8, 0, 5, 5, 8, 8, 0, 5, 5, 8, 8, 0,
	 7, 7, 9, 9, 0, 0, 0, 9, 9, 5, 7, 7, 9, 9, 0, 8,
	 8,10,10, 0, 8, 7,10, 9, 0,10,10,11,11, 0, 0, 0,
	11,11, 5, 7, 7, 9, 9, 0, 8, 8,10,10, 0, 7, 8, 9,
	10, 0,10,10,11,11, 0, 0, 0,11,11, 8, 9, 9,11,10,
	 0,11,11,12,12, 0,11,10,12,12, 0,13,14,14,14, 0,
	 0, 0,14,13, 8, 9, 9,10,11, 0,11,11,12,12, 0,10,
	11,12,12, 0,13,13,14,14, 0, 0, 0,13,14, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 5, 8, 7,11,10, 0, 7, 7,10,10,
	 0, 7, 7,10,10, 0, 9, 9,11,10, 0, 0, 0,11,11, 5,
	 7, 8,10,11, 0, 7, 7,10,10, 0, 7, 7,10,10, 0, 9,
	 9,10,11, 0, 0, 0,11,11, 8,10, 9,12,12, 0,10,10,
	12,12, 0,10,10,12,12, 0,12,12,13,13, 0, 0, 0,13,
	13, 8, 9,10,12,12, 0,10,10,12,12, 0,10,10,11,12,
	 0,12,12,13,13, 0, 0, 0,13,13, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 5, 8, 8,11,11, 0, 7, 7,10,10, 0, 7, 7,
	10,10, 0, 9, 9,10,11, 0, 0, 0,11,10, 5, 8, 8,10,
	11, 0, 7, 7,10,10, 0, 7, 7,10,10, 0, 9, 9,11,10,
	 0, 0, 0,10,11, 9,10,10,12,12, 0,10,10,12,12, 0,
	10,10,12,12, 0,12,13,13,13, 0, 0, 0,13,12, 9,10,
	10,12,12, 0,10,10,12,12, 0,10,10,12,12, 0,13,12,
	13,13, 0, 0, 0,12,13, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 7,10,10,14,13, 0, 9, 9,12,12, 0, 9, 9,12,12, 0,
	10,10,12,12, 0, 0, 0,12,12, 7,10,10,13,14, 0, 9,
	 9,12,13, 0, 9, 9,12,12, 0,10,10,12,12, 0, 0, 0,
	12,12, 9,11,11,14,13, 0,11,10,13,12, 0,11,11,13,
	13, 0,12,12,13,13, 0, 0, 0,13,13, 9,11,11,13,14,
	 0,10,11,12,13, 0,11,11,13,13, 0,12,12,13,13, 0,
	 0, 0,13,13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
	11,11,14,14, 0,10,11,13,13, 0,11,10,13,13, 0,12,
	12,13,13, 0, 0, 0,13,12, 9,11,11,14,14, 0,11,10,
	13,13, 0,10,11,13,13, 0,12,12,14,13, 0, 0, 0,13,
	13,
};

static float _vq_quantthresh__44c7_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c7_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p2_0 = {
	_vq_quantthresh__44c7_s_p2_0,
	_vq_quantmap__44c7_s_p2_0,
	5,
	5
};

static static_codebook _44c7_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c7_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c7_s_p2_0,
	NULL,
	&_vq_auxt__44c7_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c7_s_p3_0[] = {
	 2, 4, 4, 5, 5, 7, 7, 9, 9, 0, 4, 4, 6, 6, 7, 7,
	 9, 9, 0, 4, 4, 6, 6, 7, 7, 9, 9, 0, 5, 5, 6, 6,
	 8, 8,10,10, 0, 0, 0, 6, 6, 8, 8,10,10, 0, 0, 0,
	 7, 7, 9, 9,10,10, 0, 0, 0, 7, 7, 8, 8,10,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c7_s_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c7_s_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p3_0 = {
	_vq_quantthresh__44c7_s_p3_0,
	_vq_quantmap__44c7_s_p3_0,
	9,
	9
};

static static_codebook _44c7_s_p3_0 = {
	2, 81,
	_vq_lengthlist__44c7_s_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c7_s_p3_0,
	NULL,
	&_vq_auxt__44c7_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p4_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c7_s_p4_0[] = {
	 3, 4, 4, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9,10,10,11,
	11, 0, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,11,
	12,12, 0, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,
	11,12,12, 0, 5, 5, 6, 6, 8, 8, 9, 9, 9, 9,10,10,
	11,12,12,12, 0, 0, 0, 6, 6, 8, 7, 9, 9, 9, 9,10,
	10,11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,10,10,
	11,11,12,12,13,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,10,
	10,11,11,12,12,12,13, 0, 0, 0, 7, 7, 8, 8, 9, 9,
	10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 8, 8, 9,
	 9,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c7_s_p4_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c7_s_p4_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p4_0 = {
	_vq_quantthresh__44c7_s_p4_0,
	_vq_quantmap__44c7_s_p4_0,
	17,
	17
};

static static_codebook _44c7_s_p4_0 = {
	2, 289,
	_vq_lengthlist__44c7_s_p4_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c7_s_p4_0,
	NULL,
	&_vq_auxt__44c7_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p5_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c7_s_p5_0[] = {
	 1, 4, 4, 5, 7, 7, 6, 7, 7, 4, 6, 7,10,10,10,10,
	10, 9, 4, 6, 6,10,10,10,10, 9,10, 5,10,10, 9,11,
	12,10,11,12, 7,10,10,11,12,12,12,12,12, 7,10,10,
	11,12,12,12,12,12, 6,10,10,10,12,12,11,12,12, 7,
	10,10,12,12,12,12,11,12, 7,10,10,11,12,12,12,12,
	12,
};

static float _vq_quantthresh__44c7_s_p5_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c7_s_p5_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p5_0 = {
	_vq_quantthresh__44c7_s_p5_0,
	_vq_quantmap__44c7_s_p5_0,
	3,
	3
};

static static_codebook _44c7_s_p5_0 = {
	4, 81,
	_vq_lengthlist__44c7_s_p5_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c7_s_p5_0,
	NULL,
	&_vq_auxt__44c7_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p5_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c7_s_p5_1[] = {
	 3, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8,11, 4, 4, 6, 6,
	 7, 7, 8, 8, 9, 9,11, 4, 4, 6, 6, 7, 7, 8, 8, 9,
	 9,12, 5, 5, 6, 6, 7, 7, 9, 9, 9, 9,12,12,12, 6,
	 6, 7, 7, 9, 9, 9, 9,11,11,11, 7, 7, 7, 7, 8, 8,
	 9, 9,11,11,11, 7, 7, 7, 7, 8, 8, 9, 9,11,11,11,
	 7, 7, 8, 8, 8, 8, 9, 9,11,11,11,11,11, 8, 8, 8,
	 8, 8, 9,11,11,11,11,11, 8, 8, 8, 8, 8, 8,11,11,
	11,11,11, 7, 7, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c7_s_p5_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c7_s_p5_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p5_1 = {
	_vq_quantthresh__44c7_s_p5_1,
	_vq_quantmap__44c7_s_p5_1,
	11,
	11
};

static static_codebook _44c7_s_p5_1 = {
	2, 121,
	_vq_lengthlist__44c7_s_p5_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c7_s_p5_1,
	NULL,
	&_vq_auxt__44c7_s_p5_1,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c7_s_p6_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 7, 9, 8,10,10, 6, 5, 5,
	 7, 7, 8, 8, 9, 9, 9,10,11,11, 7, 5, 5, 7, 7, 8,
	 8, 9, 9,10,10,11,11, 0, 7, 7, 7, 7, 9, 8, 9, 9,
	10,10,11,11, 0, 8, 8, 7, 7, 8, 9, 9, 9,10,10,11,
	11, 0,11,11, 9, 9,10,10,11,10,11,11,12,12, 0,12,
	12, 9, 9,10,10,11,11,11,11,12,12, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

static float _vq_quantthresh__44c7_s_p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c7_s_p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p6_0 = {
	_vq_quantthresh__44c7_s_p6_0,
	_vq_quantmap__44c7_s_p6_0,
	13,
	13
};

static static_codebook _44c7_s_p6_0 = {
	2, 169,
	_vq_lengthlist__44c7_s_p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c7_s_p6_0,
	NULL,
	&_vq_auxt__44c7_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c7_s_p6_1[] = {
	 3, 4, 4, 5, 5, 5, 4, 4, 5, 5, 5, 4, 4, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c7_s_p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c7_s_p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p6_1 = {
	_vq_quantthresh__44c7_s_p6_1,
	_vq_quantmap__44c7_s_p6_1,
	5,
	5
};

static static_codebook _44c7_s_p6_1 = {
	2, 25,
	_vq_lengthlist__44c7_s_p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c7_s_p6_1,
	NULL,
	&_vq_auxt__44c7_s_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c7_s_p7_0[] = {
	 1, 4, 4, 6, 6, 7, 8, 9, 9,10,10,12,11, 6, 5, 5,
	 7, 7, 8, 8, 9,10,11,11,12,12, 7, 5, 5, 7, 7, 8,
	 8,10,10,11,11,12,12,20, 7, 7, 7, 7, 8, 9,10,10,
	11,11,12,13,20, 7, 7, 7, 7, 9, 9,10,10,11,12,13,
	13,20,11,11, 8, 8, 9, 9,11,11,12,12,13,13,20,11,
	11, 8, 8, 9, 9,11,11,12,12,13,13,20,20,20,10,10,
	10,10,12,12,13,13,13,13,20,20,20,10,10,10,10,12,
	12,13,13,13,14,20,20,20,14,14,11,11,12,12,13,13,
	14,14,20,20,20,14,14,11,11,12,12,13,13,14,14,20,
	20,20,20,19,13,13,13,13,14,14,15,14,19,19,19,19,
	19,13,13,13,13,14,14,15,15,
};

static float _vq_quantthresh__44c7_s_p7_0[] = {
	-60.5, -49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5,
	27.5, 38.5, 49.5, 60.5,
};

static long _vq_quantmap__44c7_s_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p7_0 = {
	_vq_quantthresh__44c7_s_p7_0,
	_vq_quantmap__44c7_s_p7_0,
	13,
	13
};

static static_codebook _44c7_s_p7_0 = {
	2, 169,
	_vq_lengthlist__44c7_s_p7_0,
	1, -523206656, 1618345984, 4, 0,
	_vq_quantlist__44c7_s_p7_0,
	NULL,
	&_vq_auxt__44c7_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c7_s_p7_1[] = {
	 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 8, 6, 6, 7, 7,
	 7, 7, 7, 7, 7, 7, 8, 6, 6, 6, 7, 7, 7, 7, 7, 7,
	 7, 8, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 7,
	 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 7, 7, 7, 7, 7, 7,
	 7, 7, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8,
	 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7,
	 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 8, 8,
	 8, 8, 8, 7, 7, 7, 7, 7, 7,
};

static float _vq_quantthresh__44c7_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c7_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p7_1 = {
	_vq_quantthresh__44c7_s_p7_1,
	_vq_quantmap__44c7_s_p7_1,
	11,
	11
};

static static_codebook _44c7_s_p7_1 = {
	2, 121,
	_vq_lengthlist__44c7_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c7_s_p7_1,
	NULL,
	&_vq_auxt__44c7_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p8_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44c7_s_p8_0[] = {
	 1, 4, 4, 7, 7, 8, 8, 8, 7, 9, 8, 9, 9,10,10, 6,
	 5, 5, 7, 7, 9, 9, 8, 8,10, 9,11,10,12,11, 6, 5,
	 5, 8, 7, 9, 9, 8, 8,10,10,11,11,12,11,19, 8, 8,
	 8, 8,10,10, 9, 9,10,10,11,11,12,11,19, 8, 8, 8,
	 8,10,10, 9, 9,10,10,11,11,12,12,19,12,12, 9, 9,
	10,10, 9,10,10,10,11,11,12,12,19,12,12, 9, 9,10,
	10,10,10,10,10,12,12,12,12,19,19,19, 9, 9, 9, 9,
	11,10,11,11,12,11,13,13,19,19,19, 9, 9, 9, 9,11,
	10,11,11,11,12,13,13,19,19,19,13,13,10,10,11,11,
	12,12,12,12,13,12,19,19,19,14,13,10,10,11,11,12,
	12,12,13,13,13,19,19,19,19,19,12,12,12,11,12,13,
	14,13,13,13,19,19,19,19,19,12,12,12,11,12,12,13,
	14,13,14,19,19,19,19,19,16,16,12,13,12,13,13,14,
	15,14,19,18,18,18,18,16,15,12,11,12,11,14,12,14,
	14,
};

static float _vq_quantthresh__44c7_s_p8_0[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__44c7_s_p8_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p8_0 = {
	_vq_quantthresh__44c7_s_p8_0,
	_vq_quantmap__44c7_s_p8_0,
	15,
	15
};

static static_codebook _44c7_s_p8_0 = {
	2, 225,
	_vq_lengthlist__44c7_s_p8_0,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__44c7_s_p8_0,
	NULL,
	&_vq_auxt__44c7_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p8_1[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__44c7_s_p8_1[] = {
	 3, 5, 5, 7, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8,10, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 6, 6, 7, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,
	 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10, 8, 8, 8, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,
	10, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,11,10,10,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9,10, 9, 9,10, 9, 9,10,11,10,
	11,10, 9, 9, 9, 9, 9, 9, 9,10,10,10, 9,10, 9, 9,
	 9, 9,11,10,11,10,10, 9, 9, 9, 9, 9, 9,10, 9, 9,
	10, 9, 9,10, 9, 9,10,11,10,10,11,10, 9, 9, 9, 9,
	 9,10,10, 9,10,10,10,10, 9,10,10,10,10,10,10,11,
	11,11,10, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,
	10,10,10,11,11,10,10,10,10,10,10,10,10,10,10,10,
	10, 9,10,10, 9,10,11,11,10,11,10,11,10, 9,10,10,
	 9,10,10,10,10,10,10,10,10,10,10,11,11,11,11,10,
	11,11,10,10,10,10,10,10, 9,10, 9,10,10, 9,10, 9,
	10,10,10,11,10,11,10,11,11,10,10,10,10,10,10, 9,
	10,10,10,10,10,10,10,11,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,11,10,11,
	11,10,10,10,10, 9, 9,10,10, 9, 9,10, 9,10,10,10,
	10,11,11,10,10,10,10,10,10,10, 9, 9,10,10,10, 9,
	 9,10,10,10,10,10,11,10,11,10,10,10,10,10,10, 9,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__44c7_s_p8_1[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__44c7_s_p8_1[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p8_1 = {
	_vq_quantthresh__44c7_s_p8_1,
	_vq_quantmap__44c7_s_p8_1,
	21,
	21
};

static static_codebook _44c7_s_p8_1 = {
	2, 441,
	_vq_lengthlist__44c7_s_p8_1,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__44c7_s_p8_1,
	NULL,
	&_vq_auxt__44c7_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c7_s_p9_0[] = {
	 1, 3, 3,11,11,11,11,11,11,11,11,11,11, 4, 6, 6,
	11,11,11,11,11,11,11,11,11,11, 4, 7, 7,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,
};

static float _vq_quantthresh__44c7_s_p9_0[] = {
	-3503.5, -2866.5, -2229.5, -1592.5, -955.5, -318.5, 318.5, 955.5,
	1592.5, 2229.5, 2866.5, 3503.5,
};

static long _vq_quantmap__44c7_s_p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p9_0 = {
	_vq_quantthresh__44c7_s_p9_0,
	_vq_quantmap__44c7_s_p9_0,
	13,
	13
};

static static_codebook _44c7_s_p9_0 = {
	2, 169,
	_vq_lengthlist__44c7_s_p9_0,
	1, -511845376, 1630791680, 4, 0,
	_vq_quantlist__44c7_s_p9_0,
	NULL,
	&_vq_auxt__44c7_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p9_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c7_s_p9_1[] = {
	 1, 4, 4, 7, 7, 7, 7, 7, 6, 8, 8, 8, 8, 6, 6, 6,
	 8, 8, 9, 8, 8, 7, 9, 8,11,10, 5, 6, 6, 8, 8, 9,
	 8, 8, 8,10, 9,11,11,16, 8, 8, 9, 8, 9, 9, 9, 8,
	10, 9,11,10,16, 8, 8, 9, 9,10,10, 9, 9,10,10,11,
	11,16,13,13, 9, 9,10,10, 9,10,11,11,12,11,16,13,
	13, 9, 8,10, 9,10,10,10,10,11,11,16,14,16, 8, 9,
	 9, 9,11,10,11,11,12,11,16,16,16, 9, 7,10, 7,11,
	10,11,11,12,11,16,16,16,12,12, 9,10,11,11,12,11,
	12,12,16,16,16,12,10,10, 7,11, 8,12,11,12,12,16,
	16,15,16,16,11,12,10,10,12,11,12,12,16,16,16,15,
	15,11,11,10,10,12,12,12,12,
};

static float _vq_quantthresh__44c7_s_p9_1[] = {
	-269.5, -220.5, -171.5, -122.5, -73.5, -24.5, 24.5, 73.5,
	122.5, 171.5, 220.5, 269.5,
};

static long _vq_quantmap__44c7_s_p9_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p9_1 = {
	_vq_quantthresh__44c7_s_p9_1,
	_vq_quantmap__44c7_s_p9_1,
	13,
	13
};

static static_codebook _44c7_s_p9_1 = {
	2, 169,
	_vq_lengthlist__44c7_s_p9_1,
	1, -518889472, 1622704128, 4, 0,
	_vq_quantlist__44c7_s_p9_1,
	NULL,
	&_vq_auxt__44c7_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44c7_s_p9_2[] = {
	24,
	23,
	25,
	22,
	26,
	21,
	27,
	20,
	28,
	19,
	29,
	18,
	30,
	17,
	31,
	16,
	32,
	15,
	33,
	14,
	34,
	13,
	35,
	12,
	36,
	11,
	37,
	10,
	38,
	9,
	39,
	8,
	40,
	7,
	41,
	6,
	42,
	5,
	43,
	4,
	44,
	3,
	45,
	2,
	46,
	1,
	47,
	0,
	48,
};

static long _vq_lengthlist__44c7_s_p9_2[] = {
	 2, 4, 3, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6,
	 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__44c7_s_p9_2[] = {
	-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5,
	-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
	8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
	16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5,
};

static long _vq_quantmap__44c7_s_p9_2[] = {
	   47,   45,   43,   41,   39,   37,   35,   33,
	   31,   29,   27,   25,   23,   21,   19,   17,
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,   18,   20,   22,   24,   26,   28,   30,
	   32,   34,   36,   38,   40,   42,   44,   46,
	   48,
};

static encode_aux_threshmatch _vq_auxt__44c7_s_p9_2 = {
	_vq_quantthresh__44c7_s_p9_2,
	_vq_quantmap__44c7_s_p9_2,
	49,
	49
};

static static_codebook _44c7_s_p9_2 = {
	1, 49,
	_vq_lengthlist__44c7_s_p9_2,
	1, -526909440, 1611661312, 6, 0,
	_vq_quantlist__44c7_s_p9_2,
	NULL,
	&_vq_auxt__44c7_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44c7_s_short[] = {
	 4,11,12,14,15,15,17,17,18,18, 5, 6, 6, 8, 9,10,
	13,17,18,19, 7, 5, 4, 6, 8, 9,11,15,19,19, 8, 6,
	 5, 5, 6, 7,11,14,16,17, 9, 7, 7, 6, 7, 7,10,13,
	15,19,10, 8, 7, 6, 7, 6, 7, 9,14,16,12,10, 9, 7,
	 7, 6, 4, 5,10,15,14,13,11, 7, 6, 6, 4, 2, 7,13,
	16,16,15, 9, 8, 8, 8, 6, 9,13,19,19,17,12,11,10,
	10, 9,11,14,
};

static static_codebook _huff_book__44c7_s_short = {
	2, 100,
	_huff_lengthlist__44c7_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c8_s_long[] = {
	 3, 8,12,13,14,14,14,13,14,14, 6, 4, 5, 8,10,10,
	11,11,14,13, 9, 5, 4, 5, 7, 8, 9,10,13,13,12, 7,
	 5, 4, 5, 6, 8, 9,12,13,13, 9, 6, 5, 5, 5, 7, 9,
	11,14,12,10, 7, 6, 5, 4, 6, 7,10,11,12,11, 9, 8,
	 7, 5, 5, 6,10,10,13,12,10, 9, 8, 6, 6, 5, 8,10,
	14,13,12,12,11,10, 9, 7, 8,10,12,13,14,14,13,12,
	11, 9, 9,10,
};

static static_codebook _huff_book__44c8_s_long = {
	2, 100,
	_huff_lengthlist__44c8_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c8_s_p1_0[] = {
	 1, 5, 5, 0, 5, 5, 0, 5, 5, 5, 7, 7, 0, 9, 8, 0,
	 9, 8, 6, 7, 7, 0, 8, 9, 0, 8, 9, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 5, 9, 8, 0, 8, 8, 0, 8, 8, 5, 8, 9,
	 0, 8, 8, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
	 9, 8, 0, 8, 8, 0, 8, 8, 5, 8, 9, 0, 8, 8, 0, 8,
	 8,
};

static float _vq_quantthresh__44c8_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c8_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p1_0 = {
	_vq_quantthresh__44c8_s_p1_0,
	_vq_quantmap__44c8_s_p1_0,
	3,
	3
};

static static_codebook _44c8_s_p1_0 = {
	4, 81,
	_vq_lengthlist__44c8_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c8_s_p1_0,
	NULL,
	&_vq_auxt__44c8_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c8_s_p2_0[] = {
	 3, 5, 5, 8, 8, 0, 5, 5, 8, 8, 0, 5, 5, 8, 8, 0,
	 7, 7, 9, 9, 0, 0, 0, 9, 9, 5, 7, 7, 9, 9, 0, 8,
	 7,10, 9, 0, 8, 7,10, 9, 0,10,10,11,11, 0, 0, 0,
	11,11, 5, 7, 7, 9, 9, 0, 7, 8, 9,10, 0, 7, 8, 9,
	10, 0,10,10,11,11, 0, 0, 0,11,11, 8, 9, 9,11,10,
	 0,11,10,12,11, 0,11,10,12,12, 0,13,13,14,14, 0,
	 0, 0,14,13, 8, 9, 9,10,11, 0,10,11,12,12, 0,10,
	11,12,12, 0,13,13,14,14, 0, 0, 0,13,14, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 5, 8, 7,11,10, 0, 7, 7,10,10,
	 0, 7, 7,10,10, 0, 9, 9,10,10, 0, 0, 0,11,10, 5,
	 7, 8,10,11, 0, 7, 7,10,10, 0, 7, 7,10,10, 0, 9,
	 9,10,10, 0, 0, 0,10,10, 8,10, 9,12,12, 0,10,10,
	12,11, 0,10,10,12,12, 0,12,12,13,12, 0, 0, 0,13,
	12, 8, 9,10,12,12, 0,10,10,11,12, 0,10,10,11,12,
	 0,12,12,13,13, 0, 0, 0,12,13, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 6, 8, 7,11,10, 0, 7, 7,10,10, 0, 7, 7,
	10,10, 0, 9, 9,10,11, 0, 0, 0,10,10, 6, 7, 8,10,
	11, 0, 7, 7,10,10, 0, 7, 7,10,10, 0, 9, 9,10,10,
	 0, 0, 0,10,10, 9,10, 9,12,12, 0,10,10,12,12, 0,
	10,10,12,11, 0,12,12,13,13, 0, 0, 0,13,12, 8, 9,
	10,12,12, 0,10,10,12,12, 0,10,10,11,12, 0,12,12,
	13,13, 0, 0, 0,12,13, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 7,10,10,13,13, 0, 9, 9,12,12, 0, 9, 9,12,12, 0,
	10,10,12,12, 0, 0, 0,12,12, 7,10,10,13,13, 0, 9,
	 9,12,12, 0, 9, 9,12,12, 0,10,10,12,12, 0, 0, 0,
	12,12, 9,11,11,14,13, 0,10,10,13,12, 0,11,10,13,
	12, 0,12,12,13,12, 0, 0, 0,13,13, 9,11,11,13,14,
	 0,10,11,12,13, 0,10,11,13,13, 0,12,12,12,13, 0,
	 0, 0,13,13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
	11,11,14,14, 0,10,11,13,13, 0,11,10,13,13, 0,11,
	12,13,13, 0, 0, 0,13,12, 9,11,11,14,14, 0,11,10,
	13,13, 0,10,11,13,13, 0,12,12,13,13, 0, 0, 0,12,
	13,
};

static float _vq_quantthresh__44c8_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c8_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p2_0 = {
	_vq_quantthresh__44c8_s_p2_0,
	_vq_quantmap__44c8_s_p2_0,
	5,
	5
};

static static_codebook _44c8_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c8_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c8_s_p2_0,
	NULL,
	&_vq_auxt__44c8_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c8_s_p3_0[] = {
	 2, 4, 4, 5, 5, 7, 7, 9, 9, 0, 4, 4, 6, 6, 7, 7,
	 9, 9, 0, 4, 4, 6, 6, 7, 7, 9, 9, 0, 5, 5, 6, 6,
	 8, 8,10,10, 0, 0, 0, 6, 6, 8, 8,10,10, 0, 0, 0,
	 7, 7, 9, 9,10,10, 0, 0, 0, 7, 7, 8, 8,10,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c8_s_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c8_s_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p3_0 = {
	_vq_quantthresh__44c8_s_p3_0,
	_vq_quantmap__44c8_s_p3_0,
	9,
	9
};

static static_codebook _44c8_s_p3_0 = {
	2, 81,
	_vq_lengthlist__44c8_s_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c8_s_p3_0,
	NULL,
	&_vq_auxt__44c8_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p4_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c8_s_p4_0[] = {
	 3, 4, 4, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9,10,10,11,
	11, 0, 4, 4, 6, 6, 7, 7, 8, 8, 9, 8,10,10,11,11,
	11,11, 0, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,
	11,11,11, 0, 6, 5, 6, 6, 7, 7, 9, 9, 9, 9,10,10,
	11,11,12,12, 0, 0, 0, 6, 6, 7, 7, 9, 9, 9, 9,10,
	10,11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,10,10,
	11,11,11,12,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,10,
	10,11,11,11,12,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,
	10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 8, 8, 9,
	 9,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c8_s_p4_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c8_s_p4_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p4_0 = {
	_vq_quantthresh__44c8_s_p4_0,
	_vq_quantmap__44c8_s_p4_0,
	17,
	17
};

static static_codebook _44c8_s_p4_0 = {
	2, 289,
	_vq_lengthlist__44c8_s_p4_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c8_s_p4_0,
	NULL,
	&_vq_auxt__44c8_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p5_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c8_s_p5_0[] = {
	 1, 4, 4, 5, 7, 7, 6, 7, 7, 4, 7, 6,10,10,10,10,
	10,10, 4, 6, 6,10,10,10,10, 9,10, 5,10,10, 9,11,
	11,10,11,11, 7,10,10,11,12,12,12,12,12, 7,10,10,
	11,12,12,12,12,12, 6,10,10,10,12,12,10,12,12, 7,
	10,10,11,12,12,12,12,12, 7,10,10,11,12,12,12,12,
	12,
};

static float _vq_quantthresh__44c8_s_p5_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c8_s_p5_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p5_0 = {
	_vq_quantthresh__44c8_s_p5_0,
	_vq_quantmap__44c8_s_p5_0,
	3,
	3
};

static static_codebook _44c8_s_p5_0 = {
	4, 81,
	_vq_lengthlist__44c8_s_p5_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c8_s_p5_0,
	NULL,
	&_vq_auxt__44c8_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p5_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c8_s_p5_1[] = {
	 3, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8,11, 4, 5, 6, 6,
	 7, 7, 8, 8, 8, 8,11, 5, 5, 6, 6, 7, 7, 8, 8, 8,
	 9,12, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,12,12,12, 6,
	 6, 7, 7, 8, 8, 9, 9,11,11,11, 6, 6, 7, 7, 8, 8,
	 8, 8,11,11,11, 6, 6, 7, 7, 8, 8, 8, 8,11,11,11,
	 7, 7, 7, 8, 8, 8, 8, 8,11,11,11,11,11, 7, 7, 8,
	 8, 8, 8,11,11,11,11,11, 7, 7, 7, 7, 8, 8,11,11,
	11,11,11, 7, 7, 7, 7, 8, 8,
};

static float _vq_quantthresh__44c8_s_p5_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c8_s_p5_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p5_1 = {
	_vq_quantthresh__44c8_s_p5_1,
	_vq_quantmap__44c8_s_p5_1,
	11,
	11
};

static static_codebook _44c8_s_p5_1 = {
	2, 121,
	_vq_lengthlist__44c8_s_p5_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c8_s_p5_1,
	NULL,
	&_vq_auxt__44c8_s_p5_1,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c8_s_p6_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 6, 5, 5,
	 7, 7, 8, 8, 9, 9,10,10,11,11, 6, 5, 5, 7, 7, 8,
	 8, 9, 9,10,10,11,11, 0, 7, 7, 7, 7, 9, 9,10,10,
	10,10,11,11, 0, 7, 7, 7, 7, 9, 9,10,10,10,10,11,
	11, 0,11,11, 9, 9,10,10,11,11,11,11,12,12, 0,12,
	12, 9, 9,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

static float _vq_quantthresh__44c8_s_p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c8_s_p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p6_0 = {
	_vq_quantthresh__44c8_s_p6_0,
	_vq_quantmap__44c8_s_p6_0,
	13,
	13
};

static static_codebook _44c8_s_p6_0 = {
	2, 169,
	_vq_lengthlist__44c8_s_p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c8_s_p6_0,
	NULL,
	&_vq_auxt__44c8_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c8_s_p6_1[] = {
	 3, 4, 4, 5, 5, 5, 4, 4, 5, 5, 5, 4, 4, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c8_s_p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c8_s_p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p6_1 = {
	_vq_quantthresh__44c8_s_p6_1,
	_vq_quantmap__44c8_s_p6_1,
	5,
	5
};

static static_codebook _44c8_s_p6_1 = {
	2, 25,
	_vq_lengthlist__44c8_s_p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c8_s_p6_1,
	NULL,
	&_vq_auxt__44c8_s_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c8_s_p7_0[] = {
	 1, 4, 4, 6, 6, 8, 7, 9, 9,10,10,12,12, 6, 5, 5,
	 7, 7, 8, 8,10,10,11,11,12,12, 7, 5, 5, 7, 7, 8,
	 8,10,10,11,11,12,12,21, 7, 7, 7, 7, 8, 9,10,10,
	11,11,12,12,21, 7, 7, 7, 7, 9, 9,10,10,12,12,13,
	13,21,11,11, 8, 8, 9, 9,11,11,12,12,13,13,21,11,
	11, 8, 8, 9, 9,11,11,12,12,13,13,21,21,21,10,10,
	10,10,11,11,12,13,13,13,21,21,21,10,10,10,10,11,
	11,13,13,14,13,21,21,21,13,13,11,11,12,12,13,13,
	14,14,21,21,21,14,14,11,11,12,12,13,13,14,14,21,
	21,21,21,20,13,13,13,12,14,14,16,15,20,20,20,20,
	20,13,13,13,13,14,13,15,15,
};

static float _vq_quantthresh__44c8_s_p7_0[] = {
	-60.5, -49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5,
	27.5, 38.5, 49.5, 60.5,
};

static long _vq_quantmap__44c8_s_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p7_0 = {
	_vq_quantthresh__44c8_s_p7_0,
	_vq_quantmap__44c8_s_p7_0,
	13,
	13
};

static static_codebook _44c8_s_p7_0 = {
	2, 169,
	_vq_lengthlist__44c8_s_p7_0,
	1, -523206656, 1618345984, 4, 0,
	_vq_quantlist__44c8_s_p7_0,
	NULL,
	&_vq_auxt__44c8_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c8_s_p7_1[] = {
	 4, 5, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 6, 6, 6, 7,
	 7, 7, 7, 7, 7, 7, 8, 6, 6, 6, 6, 7, 7, 7, 7, 7,
	 7, 8, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 7,
	 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 7, 7, 7, 7, 7, 7,
	 7, 7, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8,
	 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7,
	 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 8, 8,
	 8, 8, 8, 7, 7, 7, 7, 7, 7,
};

static float _vq_quantthresh__44c8_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c8_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p7_1 = {
	_vq_quantthresh__44c8_s_p7_1,
	_vq_quantmap__44c8_s_p7_1,
	11,
	11
};

static static_codebook _44c8_s_p7_1 = {
	2, 121,
	_vq_lengthlist__44c8_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c8_s_p7_1,
	NULL,
	&_vq_auxt__44c8_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p8_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44c8_s_p8_0[] = {
	 1, 4, 4, 7, 6, 8, 8, 8, 7, 9, 8,10,10,11,10, 6,
	 5, 5, 7, 7, 9, 9, 8, 8,10,10,11,11,12,11, 6, 5,
	 5, 7, 7, 9, 9, 9, 9,10,10,11,11,12,12,20, 8, 8,
	 8, 8, 9, 9, 9, 9,10,10,11,11,12,12,20, 8, 8, 8,
	 8,10, 9, 9, 9,10,10,11,11,12,12,20,12,12, 9, 9,
	10,10,10,10,10,11,12,12,12,12,20,12,12, 9, 9,10,
	10,10,10,11,11,12,12,13,13,20,20,20, 9, 9, 9, 9,
	11,10,11,11,12,12,12,13,20,19,19, 9, 9, 9, 9,11,
	11,11,12,12,12,13,13,19,19,19,13,13,10,10,11,11,
	12,12,13,13,13,13,19,19,19,14,13,11,10,11,11,12,
	12,12,13,13,13,19,19,19,19,19,12,12,12,12,13,13,
	13,13,14,13,19,19,19,19,19,12,12,12,11,12,12,13,
	14,14,14,19,19,19,19,19,16,15,13,12,13,13,13,14,
	14,14,19,19,19,19,19,17,17,13,12,13,11,14,13,15,
	15,
};

static float _vq_quantthresh__44c8_s_p8_0[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__44c8_s_p8_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p8_0 = {
	_vq_quantthresh__44c8_s_p8_0,
	_vq_quantmap__44c8_s_p8_0,
	15,
	15
};

static static_codebook _44c8_s_p8_0 = {
	2, 225,
	_vq_lengthlist__44c8_s_p8_0,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__44c8_s_p8_0,
	NULL,
	&_vq_auxt__44c8_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p8_1[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__44c8_s_p8_1[] = {
	 4, 5, 5, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8,10, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 6, 6, 7, 7, 8,
	 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,
	 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10, 8, 8, 8, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,
	10, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,
	10,10, 9, 9, 9, 9, 9, 9, 9, 9,10, 9, 9, 9, 9, 9,
	 9, 9,10,10,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 9, 9, 9, 9, 9,10,10,10,10,
	10,10,10, 9, 9, 9, 9, 9,10,10,10, 9, 9, 9, 9, 9,
	 9,10,10,10,10,10,10,10, 9,10,10, 9,10,10,10,10,
	 9,10, 9,10,10, 9,10,10,10,10,10,10,10, 9,10,10,
	10,10,10,10, 9, 9,10,10, 9,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10, 9, 9, 9,10, 9, 9, 9, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9, 9,
	10, 9,10, 9,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10, 9, 9,10, 9, 9, 9,10,10,10,10,10,10,
	10,10,10,10,10, 9, 9, 9, 9, 9, 9,10, 9, 9,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10, 9,10, 9,
	 9,10, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,
	10, 9, 9,10,10, 9,10, 9, 9,
};

static float _vq_quantthresh__44c8_s_p8_1[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__44c8_s_p8_1[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p8_1 = {
	_vq_quantthresh__44c8_s_p8_1,
	_vq_quantmap__44c8_s_p8_1,
	21,
	21
};

static static_codebook _44c8_s_p8_1 = {
	2, 441,
	_vq_lengthlist__44c8_s_p8_1,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__44c8_s_p8_1,
	NULL,
	&_vq_auxt__44c8_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p9_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c8_s_p9_0[] = {
	 1, 4, 3,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11, 4, 7, 7,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11, 4, 8,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__44c8_s_p9_0[] = {
	-6982.5, -6051.5, -5120.5, -4189.5, -3258.5, -2327.5, -1396.5, -465.5,
	465.5, 1396.5, 2327.5, 3258.5, 4189.5, 5120.5, 6051.5, 6982.5,
};

static long _vq_quantmap__44c8_s_p9_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p9_0 = {
	_vq_quantthresh__44c8_s_p9_0,
	_vq_quantmap__44c8_s_p9_0,
	17,
	17
};

static static_codebook _44c8_s_p9_0 = {
	2, 289,
	_vq_lengthlist__44c8_s_p9_0,
	1, -509798400, 1631393792, 5, 0,
	_vq_quantlist__44c8_s_p9_0,
	NULL,
	&_vq_auxt__44c8_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p9_1[] = {
	9,
	8,
	10,
	7,
	11,
	6,
	12,
	5,
	13,
	4,
	14,
	3,
	15,
	2,
	16,
	1,
	17,
	0,
	18,
};

static long _vq_lengthlist__44c8_s_p9_1[] = {
	 1, 4, 4, 7, 6, 7, 7, 7, 7, 8, 8, 9, 9,10,10,10,
	10,11,11, 6, 6, 6, 8, 8, 9, 8, 8, 7,10, 8,11,10,
	12,11,12,12,13,13, 5, 5, 6, 8, 8, 9, 9, 8, 8,10,
	 9,11,11,12,12,13,13,13,13,17, 8, 8, 9, 9, 9, 9,
	 9, 9,10, 9,12,10,12,12,13,12,13,13,17, 9, 8, 9,
	 9, 9, 9, 9, 9,10,10,12,12,12,12,13,13,13,13,17,
	13,13, 9, 9,10,10,10,10,11,11,12,11,13,12,13,13,
	14,15,17,13,13, 9, 8,10, 9,10,10,11,11,12,12,14,
	13,15,13,14,15,17,17,17, 9,10, 9,10,11,11,12,12,
	12,12,13,13,14,14,15,15,17,17,17, 9, 8, 9, 8,11,
	11,12,12,12,12,14,13,14,14,14,15,17,17,17,12,14,
	 9,10,11,11,12,12,14,13,13,14,15,13,15,15,17,17,
	17,13,11,10, 8,11, 9,13,12,13,13,13,13,13,14,14,
	14,17,17,17,17,17,11,12,11,11,13,13,14,13,15,14,
	13,15,16,15,17,17,17,17,17,11,11,12, 8,13,12,14,
	13,17,14,15,14,15,14,17,17,17,17,17,15,15,12,12,
	12,12,13,14,14,14,15,14,17,14,17,17,17,17,17,16,
	17,12,12,13,12,13,13,14,14,14,14,14,14,17,17,17,
	17,17,17,17,14,14,13,12,13,13,15,15,14,13,15,17,
	17,17,17,17,17,17,17,13,14,13,13,13,13,14,15,15,
	15,14,15,17,17,17,17,17,17,17,16,15,13,14,13,13,
	14,14,15,14,14,16,17,17,17,17,17,17,17,16,16,13,
	14,13,13,14,14,15,14,15,14,
};

static float _vq_quantthresh__44c8_s_p9_1[] = {
	-416.5, -367.5, -318.5, -269.5, -220.5, -171.5, -122.5, -73.5,
	-24.5, 24.5, 73.5, 122.5, 171.5, 220.5, 269.5, 318.5,
	367.5, 416.5,
};

static long _vq_quantmap__44c8_s_p9_1[] = {
	   17,   15,   13,   11,	9,	7,	5,	3,
		1,	0,	2,	4,	6,	8,   10,   12,
	   14,   16,   18,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p9_1 = {
	_vq_quantthresh__44c8_s_p9_1,
	_vq_quantmap__44c8_s_p9_1,
	19,
	19
};

static static_codebook _44c8_s_p9_1 = {
	2, 361,
	_vq_lengthlist__44c8_s_p9_1,
	1, -518287360, 1622704128, 5, 0,
	_vq_quantlist__44c8_s_p9_1,
	NULL,
	&_vq_auxt__44c8_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44c8_s_p9_2[] = {
	24,
	23,
	25,
	22,
	26,
	21,
	27,
	20,
	28,
	19,
	29,
	18,
	30,
	17,
	31,
	16,
	32,
	15,
	33,
	14,
	34,
	13,
	35,
	12,
	36,
	11,
	37,
	10,
	38,
	9,
	39,
	8,
	40,
	7,
	41,
	6,
	42,
	5,
	43,
	4,
	44,
	3,
	45,
	2,
	46,
	1,
	47,
	0,
	48,
};

static long _vq_lengthlist__44c8_s_p9_2[] = {
	 2, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
	 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__44c8_s_p9_2[] = {
	-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5,
	-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
	8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
	16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5,
};

static long _vq_quantmap__44c8_s_p9_2[] = {
	   47,   45,   43,   41,   39,   37,   35,   33,
	   31,   29,   27,   25,   23,   21,   19,   17,
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,   18,   20,   22,   24,   26,   28,   30,
	   32,   34,   36,   38,   40,   42,   44,   46,
	   48,
};

static encode_aux_threshmatch _vq_auxt__44c8_s_p9_2 = {
	_vq_quantthresh__44c8_s_p9_2,
	_vq_quantmap__44c8_s_p9_2,
	49,
	49
};

static static_codebook _44c8_s_p9_2 = {
	1, 49,
	_vq_lengthlist__44c8_s_p9_2,
	1, -526909440, 1611661312, 6, 0,
	_vq_quantlist__44c8_s_p9_2,
	NULL,
	&_vq_auxt__44c8_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44c8_s_short[] = {
	 4,11,13,14,15,15,18,17,19,17, 5, 6, 8, 9,10,10,
	12,15,19,19, 6, 6, 6, 6, 8, 8,11,14,18,19, 8, 6,
	 5, 4, 6, 7,10,13,16,17, 9, 7, 6, 5, 6, 7, 9,12,
	15,19,10, 8, 7, 6, 6, 6, 7, 9,13,15,12,10, 9, 8,
	 7, 6, 4, 5,10,15,13,13,11, 8, 6, 6, 4, 2, 7,12,
	17,15,16,10, 8, 8, 7, 6, 9,12,19,18,17,13,11,10,
	10, 9,11,14,
};

static static_codebook _huff_book__44c8_s_short = {
	2, 100,
	_huff_lengthlist__44c8_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c9_s_long[] = {
	 3, 8,12,14,15,15,15,13,15,15, 6, 5, 8,10,12,12,
	13,12,14,13,10, 6, 5, 6, 8, 9,11,11,13,13,13, 8,
	 5, 4, 5, 6, 8,10,11,13,14,10, 7, 5, 4, 5, 7, 9,
	11,12,13,11, 8, 6, 5, 4, 5, 7, 9,11,12,11,10, 8,
	 7, 5, 4, 5, 9,10,13,13,11,10, 8, 6, 5, 4, 7, 9,
	15,14,13,12,10, 9, 8, 7, 8, 9,12,12,14,13,12,11,
	10, 9, 8, 9,
};

static static_codebook _huff_book__44c9_s_long = {
	2, 100,
	_huff_lengthlist__44c9_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c9_s_p1_0[] = {
	 1, 5, 5, 0, 5, 5, 0, 5, 5, 6, 8, 8, 0, 9, 8, 0,
	 9, 8, 6, 8, 8, 0, 8, 9, 0, 8, 9, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 5, 8, 8, 0, 7, 7, 0, 8, 8, 5, 8, 8,
	 0, 7, 8, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
	 9, 8, 0, 8, 8, 0, 7, 7, 5, 8, 9, 0, 8, 8, 0, 7,
	 7,
};

static float _vq_quantthresh__44c9_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c9_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p1_0 = {
	_vq_quantthresh__44c9_s_p1_0,
	_vq_quantmap__44c9_s_p1_0,
	3,
	3
};

static static_codebook _44c9_s_p1_0 = {
	4, 81,
	_vq_lengthlist__44c9_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c9_s_p1_0,
	NULL,
	&_vq_auxt__44c9_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c9_s_p2_0[] = {
	 3, 5, 5, 8, 8, 0, 5, 5, 8, 8, 0, 5, 5, 8, 8, 0,
	 7, 7, 9, 9, 0, 0, 0, 9, 9, 6, 7, 7, 9, 8, 0, 8,
	 8, 9, 9, 0, 8, 7, 9, 9, 0, 9,10,10,10, 0, 0, 0,
	11,10, 6, 7, 7, 8, 9, 0, 8, 8, 9, 9, 0, 7, 8, 9,
	 9, 0,10, 9,11,10, 0, 0, 0,10,10, 8, 9, 8,10,10,
	 0,10,10,12,11, 0,10,10,11,11, 0,12,13,13,13, 0,
	 0, 0,13,12, 8, 8, 9,10,10, 0,10,10,11,12, 0,10,
	10,11,11, 0,13,12,13,13, 0, 0, 0,13,13, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 6, 8, 7,10,10, 0, 7, 7,10, 9,
	 0, 7, 7,10,10, 0, 9, 9,10,10, 0, 0, 0,10,10, 6,
	 7, 8,10,10, 0, 7, 7, 9,10, 0, 7, 7,10,10, 0, 9,
	 9,10,10, 0, 0, 0,10,10, 8, 9, 9,11,11, 0,10,10,
	11,11, 0,10,10,11,11, 0,12,12,12,12, 0, 0, 0,12,
	12, 8, 9,10,11,11, 0, 9,10,11,11, 0,10,10,11,11,
	 0,12,12,12,12, 0, 0, 0,12,12, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 5, 8, 7,10,10, 0, 7, 7,10,10, 0, 7, 7,
	10, 9, 0, 9, 9,10,10, 0, 0, 0,10,10, 6, 7, 8,10,
	10, 0, 7, 7,10,10, 0, 7, 7, 9,10, 0, 9, 9,10,10,
	 0, 0, 0,10,10, 8,10, 9,12,11, 0,10,10,12,11, 0,
	10, 9,11,11, 0,11,12,12,12, 0, 0, 0,12,12, 8, 9,
	10,11,12, 0,10,10,11,11, 0, 9,10,11,11, 0,12,11,
	12,12, 0, 0, 0,12,12, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 7,10, 9,12,12, 0, 9, 9,12,11, 0, 9, 9,11,11, 0,
	10,10,12,11, 0, 0, 0,11,12, 7, 9,10,12,12, 0, 9,
	 9,11,12, 0, 9, 9,11,11, 0,10,10,11,12, 0, 0, 0,
	11,11, 9,11,10,13,12, 0,10,10,12,12, 0,10,10,12,
	12, 0,11,11,12,12, 0, 0, 0,13,12, 9,10,11,12,13,
	 0,10,10,12,12, 0,10,10,12,12, 0,11,12,12,12, 0,
	 0, 0,12,13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
	11,10,13,13, 0,10,10,12,12, 0,10,10,12,12, 0,11,
	12,12,12, 0, 0, 0,12,12, 9,10,11,13,13, 0,10,10,
	12,12, 0,10,10,12,12, 0,12,11,13,12, 0, 0, 0,12,
	12,
};

static float _vq_quantthresh__44c9_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c9_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p2_0 = {
	_vq_quantthresh__44c9_s_p2_0,
	_vq_quantmap__44c9_s_p2_0,
	5,
	5
};

static static_codebook _44c9_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c9_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c9_s_p2_0,
	NULL,
	&_vq_auxt__44c9_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c9_s_p3_0[] = {
	 3, 4, 4, 5, 5, 6, 6, 8, 8, 0, 4, 4, 5, 5, 6, 7,
	 8, 8, 0, 4, 4, 5, 5, 7, 7, 8, 8, 0, 5, 5, 6, 6,
	 7, 7, 9, 9, 0, 0, 0, 6, 6, 7, 7, 9, 9, 0, 0, 0,
	 7, 7, 8, 8, 9, 9, 0, 0, 0, 7, 7, 8, 8, 9, 9, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c9_s_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c9_s_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p3_0 = {
	_vq_quantthresh__44c9_s_p3_0,
	_vq_quantmap__44c9_s_p3_0,
	9,
	9
};

static static_codebook _44c9_s_p3_0 = {
	2, 81,
	_vq_lengthlist__44c9_s_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c9_s_p3_0,
	NULL,
	&_vq_auxt__44c9_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p4_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c9_s_p4_0[] = {
	 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10,
	10, 0, 5, 4, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9,10,10,
	11,11, 0, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,
	10,11,11, 0, 6, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,
	11,11,11,12, 0, 0, 0, 6, 6, 7, 7, 8, 8, 9, 9,10,
	10,11,11,12,12, 0, 0, 0, 7, 7, 7, 7, 9, 9, 9, 9,
	10,10,11,11,12,12, 0, 0, 0, 7, 7, 7, 8, 9, 9, 9,
	 9,10,10,11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,
	10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 8, 8, 9,
	 9,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c9_s_p4_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c9_s_p4_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p4_0 = {
	_vq_quantthresh__44c9_s_p4_0,
	_vq_quantmap__44c9_s_p4_0,
	17,
	17
};

static static_codebook _44c9_s_p4_0 = {
	2, 289,
	_vq_lengthlist__44c9_s_p4_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c9_s_p4_0,
	NULL,
	&_vq_auxt__44c9_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p5_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c9_s_p5_0[] = {
	 1, 4, 4, 5, 7, 7, 6, 7, 7, 4, 7, 6, 9,10,10,10,
	10, 9, 4, 6, 7, 9,10,10,10, 9,10, 5, 9, 9, 9,11,
	11,10,11,11, 7,10, 9,11,12,11,12,12,12, 7, 9,10,
	11,11,12,12,12,12, 6,10,10,10,12,12,10,12,11, 7,
	10,10,11,12,12,11,12,12, 7,10,10,11,12,12,12,12,
	12,
};

static float _vq_quantthresh__44c9_s_p5_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c9_s_p5_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p5_0 = {
	_vq_quantthresh__44c9_s_p5_0,
	_vq_quantmap__44c9_s_p5_0,
	3,
	3
};

static static_codebook _44c9_s_p5_0 = {
	4, 81,
	_vq_lengthlist__44c9_s_p5_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c9_s_p5_0,
	NULL,
	&_vq_auxt__44c9_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p5_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c9_s_p5_1[] = {
	 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7,11, 5, 5, 6, 6,
	 7, 7, 7, 7, 8, 8,11, 5, 5, 6, 6, 7, 7, 7, 7, 8,
	 8,11, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8,11,11,11, 6,
	 6, 7, 7, 7, 8, 8, 8,11,11,11, 6, 6, 7, 7, 7, 8,
	 8, 8,11,11,11, 6, 6, 7, 7, 7, 7, 8, 8,11,11,11,
	 7, 7, 7, 7, 7, 7, 8, 8,11,11,11,10,10, 7, 7, 7,
	 7, 8, 8,11,11,11,11,11, 7, 7, 7, 7, 7, 7,11,11,
	11,11,11, 7, 7, 7, 7, 7, 7,
};

static float _vq_quantthresh__44c9_s_p5_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c9_s_p5_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p5_1 = {
	_vq_quantthresh__44c9_s_p5_1,
	_vq_quantmap__44c9_s_p5_1,
	11,
	11
};

static static_codebook _44c9_s_p5_1 = {
	2, 121,
	_vq_lengthlist__44c9_s_p5_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c9_s_p5_1,
	NULL,
	&_vq_auxt__44c9_s_p5_1,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c9_s_p6_0[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 5, 4, 4,
	 6, 6, 8, 8, 9, 9, 9, 9,10,10, 6, 4, 4, 6, 6, 8,
	 8, 9, 9, 9, 9,10,10, 0, 6, 6, 7, 7, 8, 8, 9, 9,
	10,10,11,11, 0, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,
	11, 0,10,10, 8, 8, 9, 9,10,10,11,11,12,12, 0,11,
	11, 8, 8, 9, 9,10,10,11,11,12,12, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

static float _vq_quantthresh__44c9_s_p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c9_s_p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p6_0 = {
	_vq_quantthresh__44c9_s_p6_0,
	_vq_quantmap__44c9_s_p6_0,
	13,
	13
};

static static_codebook _44c9_s_p6_0 = {
	2, 169,
	_vq_lengthlist__44c9_s_p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c9_s_p6_0,
	NULL,
	&_vq_auxt__44c9_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c9_s_p6_1[] = {
	 4, 4, 4, 5, 5, 5, 4, 4, 5, 5, 5, 4, 4, 5, 5, 5,
	 5, 5, 5, 5, 5, 5, 5, 5, 5,
};

static float _vq_quantthresh__44c9_s_p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c9_s_p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p6_1 = {
	_vq_quantthresh__44c9_s_p6_1,
	_vq_quantmap__44c9_s_p6_1,
	5,
	5
};

static static_codebook _44c9_s_p6_1 = {
	2, 25,
	_vq_lengthlist__44c9_s_p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c9_s_p6_1,
	NULL,
	&_vq_auxt__44c9_s_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c9_s_p7_0[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8,10,10,11,11, 6, 4, 4,
	 6, 6, 8, 8, 9, 9,10,10,12,12, 6, 4, 5, 6, 6, 8,
	 8, 9, 9,10,10,12,12,20, 6, 6, 6, 6, 8, 8, 9,10,
	11,11,12,12,20, 6, 6, 6, 6, 8, 8,10,10,11,11,12,
	12,20,10,10, 7, 7, 9, 9,10,10,11,11,12,12,20,11,
	11, 7, 7, 9, 9,10,10,11,11,12,12,20,20,20, 9, 9,
	 9, 9,11,11,12,12,13,13,20,20,20, 9, 9, 9, 9,11,
	11,12,12,13,13,20,20,20,13,13,10,10,11,11,12,13,
	13,13,20,20,20,13,13,10,10,11,11,12,13,13,13,20,
	20,20,20,19,12,12,12,12,13,13,14,15,19,19,19,19,
	19,12,12,12,12,13,13,14,14,
};

static float _vq_quantthresh__44c9_s_p7_0[] = {
	-60.5, -49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5,
	27.5, 38.5, 49.5, 60.5,
};

static long _vq_quantmap__44c9_s_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p7_0 = {
	_vq_quantthresh__44c9_s_p7_0,
	_vq_quantmap__44c9_s_p7_0,
	13,
	13
};

static static_codebook _44c9_s_p7_0 = {
	2, 169,
	_vq_lengthlist__44c9_s_p7_0,
	1, -523206656, 1618345984, 4, 0,
	_vq_quantlist__44c9_s_p7_0,
	NULL,
	&_vq_auxt__44c9_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c9_s_p7_1[] = {
	 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6,
	 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 7, 7, 7, 7, 7,
	 7, 8, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 6,
	 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 7, 7, 7, 7, 7, 7,
	 7, 7, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8,
	 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7,
	 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 8, 8,
	 8, 8, 8, 7, 7, 7, 7, 7, 7,
};

static float _vq_quantthresh__44c9_s_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c9_s_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p7_1 = {
	_vq_quantthresh__44c9_s_p7_1,
	_vq_quantmap__44c9_s_p7_1,
	11,
	11
};

static static_codebook _44c9_s_p7_1 = {
	2, 121,
	_vq_lengthlist__44c9_s_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c9_s_p7_1,
	NULL,
	&_vq_auxt__44c9_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p8_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44c9_s_p8_0[] = {
	 1, 4, 4, 7, 6, 8, 8, 8, 8, 9, 9,10,10,11,10, 6,
	 5, 5, 7, 7, 9, 9, 8, 9,10,10,11,11,12,12, 6, 5,
	 5, 7, 7, 9, 9, 9, 9,10,10,11,11,12,12,21, 7, 8,
	 8, 8, 9, 9, 9, 9,10,10,11,11,12,12,21, 8, 8, 8,
	 8, 9, 9, 9, 9,10,10,11,11,12,12,21,11,12, 9, 9,
	10,10,10,10,10,11,11,12,12,12,21,12,12, 9, 8,10,
	10,10,10,11,11,12,12,13,13,21,21,21, 9, 9, 9, 9,
	11,11,11,11,12,12,12,13,21,20,20, 9, 9, 9, 9,10,
	11,11,11,12,12,13,13,20,20,20,13,13,10,10,11,11,
	12,12,13,13,13,13,20,20,20,13,13,10,10,11,11,12,
	12,13,13,13,13,20,20,20,20,20,12,12,12,12,12,12,
	13,13,14,14,20,20,20,20,20,12,12,12,11,13,12,13,
	13,14,14,20,20,20,20,20,15,16,13,12,13,13,14,13,
	14,14,20,20,20,20,20,16,15,12,12,13,12,14,13,14,
	14,
};

static float _vq_quantthresh__44c9_s_p8_0[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__44c9_s_p8_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p8_0 = {
	_vq_quantthresh__44c9_s_p8_0,
	_vq_quantmap__44c9_s_p8_0,
	15,
	15
};

static static_codebook _44c9_s_p8_0 = {
	2, 225,
	_vq_lengthlist__44c9_s_p8_0,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__44c9_s_p8_0,
	NULL,
	&_vq_auxt__44c9_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p8_1[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__44c9_s_p8_1[] = {
	 4, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8,10, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 6, 6, 7, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,
	 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10, 8, 8, 8, 8,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,
	10, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,
	10,10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9,10,10,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10, 9, 9, 9,10,10,10,10,
	10,10,10, 9, 9, 9, 9, 9, 9,10, 9, 9, 9, 9, 9, 9,
	 9,10,10,10,10,10,10,10, 9, 9, 9,10,10,10,10,10,
	 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10, 9, 9,10,
	 9,10, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,
	10,10,10,10, 9, 9,10,10, 9, 9, 9, 9, 9, 9, 9, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9,
	 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,
	10,10, 9, 9,10, 9, 9, 9, 9, 9,10,10,10,10,10,10,
	10,10,10,10,10, 9, 9,10,10, 9, 9,10, 9, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10, 9, 9,10, 9, 9, 9,
	 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10, 9,
	 9, 9, 9,10, 9, 9, 9, 9, 9,
};

static float _vq_quantthresh__44c9_s_p8_1[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__44c9_s_p8_1[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p8_1 = {
	_vq_quantthresh__44c9_s_p8_1,
	_vq_quantmap__44c9_s_p8_1,
	21,
	21
};

static static_codebook _44c9_s_p8_1 = {
	2, 441,
	_vq_lengthlist__44c9_s_p8_1,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__44c9_s_p8_1,
	NULL,
	&_vq_auxt__44c9_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p9_0[] = {
	9,
	8,
	10,
	7,
	11,
	6,
	12,
	5,
	13,
	4,
	14,
	3,
	15,
	2,
	16,
	1,
	17,
	0,
	18,
};

static long _vq_lengthlist__44c9_s_p9_0[] = {
	 1, 4, 3,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12, 4, 5, 6,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12, 4, 6, 6,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,
};

static float _vq_quantthresh__44c9_s_p9_0[] = {
	-7913.5, -6982.5, -6051.5, -5120.5, -4189.5, -3258.5, -2327.5, -1396.5,
	-465.5, 465.5, 1396.5, 2327.5, 3258.5, 4189.5, 5120.5, 6051.5,
	6982.5, 7913.5,
};

static long _vq_quantmap__44c9_s_p9_0[] = {
	   17,   15,   13,   11,	9,	7,	5,	3,
		1,	0,	2,	4,	6,	8,   10,   12,
	   14,   16,   18,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p9_0 = {
	_vq_quantthresh__44c9_s_p9_0,
	_vq_quantmap__44c9_s_p9_0,
	19,
	19
};

static static_codebook _44c9_s_p9_0 = {
	2, 361,
	_vq_lengthlist__44c9_s_p9_0,
	1, -508535424, 1631393792, 5, 0,
	_vq_quantlist__44c9_s_p9_0,
	NULL,
	&_vq_auxt__44c9_s_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p9_1[] = {
	9,
	8,
	10,
	7,
	11,
	6,
	12,
	5,
	13,
	4,
	14,
	3,
	15,
	2,
	16,
	1,
	17,
	0,
	18,
};

static long _vq_lengthlist__44c9_s_p9_1[] = {
	 1, 4, 4, 7, 7, 7, 7, 8, 7, 9, 8, 9, 9,10,10,11,
	11,11,11, 6, 5, 5, 8, 8, 9, 9, 9, 8,10, 9,11,10,
	12,12,13,12,13,13, 5, 5, 5, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,12,13,12,13,13,17, 8, 8, 9, 9, 9, 9,
	 9, 9,10,10,12,11,13,12,13,13,13,13,18, 8, 8, 9,
	 9, 9, 9, 9, 9,11,11,12,12,13,13,13,13,13,13,17,
	13,12, 9, 9,10,10,10,10,11,11,12,12,12,13,13,13,
	14,14,18,13,12, 9, 9,10,10,10,10,11,11,12,12,13,
	13,13,14,14,14,17,18,18,10,10,10,10,11,11,11,12,
	12,12,14,13,14,13,13,14,18,18,18,10, 9,10, 9,11,
	11,12,12,12,12,13,13,15,14,14,14,18,18,16,13,14,
	10,11,11,11,12,13,13,13,13,14,13,13,14,14,18,18,
	18,14,12,11, 9,11,10,13,12,13,13,13,14,14,14,13,
	14,18,18,17,18,18,11,12,12,12,13,13,14,13,14,14,
	13,14,14,14,18,18,18,18,17,12,10,12, 9,13,11,13,
	14,14,14,14,14,15,14,18,18,17,17,18,14,15,12,13,
	13,13,14,13,14,14,15,14,15,14,18,17,18,18,18,15,
	15,12,10,14,10,14,14,13,13,14,14,14,14,18,16,18,
	18,18,18,17,14,14,13,14,14,13,13,14,14,14,15,15,
	18,18,18,18,17,17,17,14,14,14,12,14,13,14,14,15,
	14,15,14,18,18,18,18,18,18,18,17,16,13,13,13,14,
	14,14,14,15,16,15,18,18,18,18,18,18,18,17,17,13,
	13,13,13,14,13,14,15,15,15,
};

static float _vq_quantthresh__44c9_s_p9_1[] = {
	-416.5, -367.5, -318.5, -269.5, -220.5, -171.5, -122.5, -73.5,
	-24.5, 24.5, 73.5, 122.5, 171.5, 220.5, 269.5, 318.5,
	367.5, 416.5,
};

static long _vq_quantmap__44c9_s_p9_1[] = {
	   17,   15,   13,   11,	9,	7,	5,	3,
		1,	0,	2,	4,	6,	8,   10,   12,
	   14,   16,   18,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p9_1 = {
	_vq_quantthresh__44c9_s_p9_1,
	_vq_quantmap__44c9_s_p9_1,
	19,
	19
};

static static_codebook _44c9_s_p9_1 = {
	2, 361,
	_vq_lengthlist__44c9_s_p9_1,
	1, -518287360, 1622704128, 5, 0,
	_vq_quantlist__44c9_s_p9_1,
	NULL,
	&_vq_auxt__44c9_s_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44c9_s_p9_2[] = {
	24,
	23,
	25,
	22,
	26,
	21,
	27,
	20,
	28,
	19,
	29,
	18,
	30,
	17,
	31,
	16,
	32,
	15,
	33,
	14,
	34,
	13,
	35,
	12,
	36,
	11,
	37,
	10,
	38,
	9,
	39,
	8,
	40,
	7,
	41,
	6,
	42,
	5,
	43,
	4,
	44,
	3,
	45,
	2,
	46,
	1,
	47,
	0,
	48,
};

static long _vq_lengthlist__44c9_s_p9_2[] = {
	 2, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
	 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__44c9_s_p9_2[] = {
	-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5,
	-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
	8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
	16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5,
};

static long _vq_quantmap__44c9_s_p9_2[] = {
	   47,   45,   43,   41,   39,   37,   35,   33,
	   31,   29,   27,   25,   23,   21,   19,   17,
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,   18,   20,   22,   24,   26,   28,   30,
	   32,   34,   36,   38,   40,   42,   44,   46,
	   48,
};

static encode_aux_threshmatch _vq_auxt__44c9_s_p9_2 = {
	_vq_quantthresh__44c9_s_p9_2,
	_vq_quantmap__44c9_s_p9_2,
	49,
	49
};

static static_codebook _44c9_s_p9_2 = {
	1, 49,
	_vq_lengthlist__44c9_s_p9_2,
	1, -526909440, 1611661312, 6, 0,
	_vq_quantlist__44c9_s_p9_2,
	NULL,
	&_vq_auxt__44c9_s_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44c9_s_short[] = {
	 5,13,18,16,17,17,19,18,19,19, 5, 7,10,11,12,12,
	13,16,17,18, 6, 6, 7, 7, 9, 9,10,14,17,19, 8, 7,
	 6, 5, 6, 7, 9,12,19,17, 8, 7, 7, 6, 5, 6, 8,11,
	15,19, 9, 8, 7, 6, 5, 5, 6, 8,13,15,11,10, 8, 8,
	 7, 5, 4, 4,10,14,12,13,11, 9, 7, 6, 4, 2, 6,12,
	18,16,16,13, 8, 7, 7, 5, 8,13,16,17,18,15,11, 9,
	 9, 8,10,13,
};

static static_codebook _huff_book__44c9_s_short = {
	2, 100,
	_huff_lengthlist__44c9_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c0_s_long[] = {
	 5, 4, 8, 9, 8, 9,10,12,15, 4, 1, 5, 5, 6, 8,11,
	12,12, 8, 5, 8, 9, 9,11,13,12,12, 9, 5, 8, 5, 7,
	 9,12,13,13, 8, 6, 8, 7, 7, 9,11,11,11, 9, 7, 9,
	 7, 7, 7, 7,10,12,10,10,11, 9, 8, 7, 7, 9,11,11,
	12,13,12,11, 9, 8, 9,11,13,16,16,15,15,12,10,11,
	12,
};

static static_codebook _huff_book__44c0_s_long = {
	2, 81,
	_huff_lengthlist__44c0_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c0_s_p1_0[] = {
	 1, 5, 5, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 7, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 8,10, 9, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 9,10,11, 0, 0, 0, 0, 0, 0, 9,11,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 9,11, 9, 0, 0, 0, 0, 0, 0, 9,10,11,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,11,10, 0,
	 0, 0, 0, 0, 0, 9, 9,11, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 9,10, 0, 0, 0, 0, 0, 0, 9,10,11,
	 0, 0, 0, 0, 0, 0, 9,11,10, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c0_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c0_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p1_0 = {
	_vq_quantthresh__44c0_s_p1_0,
	_vq_quantmap__44c0_s_p1_0,
	3,
	3
};

static static_codebook _44c0_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__44c0_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c0_s_p1_0,
	NULL,
	&_vq_auxt__44c0_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c0_s_p2_0[] = {
	 1, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 7, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 5, 6, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 7, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 7, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c0_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c0_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p2_0 = {
	_vq_quantthresh__44c0_s_p2_0,
	_vq_quantmap__44c0_s_p2_0,
	5,
	5
};

static static_codebook _44c0_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c0_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c0_s_p2_0,
	NULL,
	&_vq_auxt__44c0_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c0_s_p3_0[] = {
	 1, 3, 2, 8, 7, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 8, 8, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0,
	 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c0_s_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c0_s_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p3_0 = {
	_vq_quantthresh__44c0_s_p3_0,
	_vq_quantmap__44c0_s_p3_0,
	9,
	9
};

static static_codebook _44c0_s_p3_0 = {
	2, 81,
	_vq_lengthlist__44c0_s_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c0_s_p3_0,
	NULL,
	&_vq_auxt__44c0_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c0_s_p4_0[] = {
	 1, 3, 3, 6, 6, 6, 6, 8, 8, 0, 0, 0, 7, 7, 7, 7,
	 9, 9, 0, 0, 0, 7, 7, 7, 7, 9, 9, 0, 0, 0, 7, 7,
	 7, 8, 9, 9, 0, 0, 0, 7, 7, 7, 7, 9, 9, 0, 0, 0,
	 9, 9, 8, 8,10,10, 0, 0, 0, 8, 9, 8, 8,10,10, 0,
	 0, 0,10,10, 9, 9,10,10, 0, 0, 0, 0, 0, 9, 9,10,
	10,
};

static float _vq_quantthresh__44c0_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c0_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p4_0 = {
	_vq_quantthresh__44c0_s_p4_0,
	_vq_quantmap__44c0_s_p4_0,
	9,
	9
};

static static_codebook _44c0_s_p4_0 = {
	2, 81,
	_vq_lengthlist__44c0_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c0_s_p4_0,
	NULL,
	&_vq_auxt__44c0_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p5_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c0_s_p5_0[] = {
	 1, 4, 3, 6, 6, 8, 7, 8, 8, 8, 8, 9, 9,10,10,11,
	11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9, 9,10,10,10,
	11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,
	10,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,11,11, 0, 0, 0, 8, 8, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12, 0, 0, 0, 8, 8, 9, 9, 9, 9,10,
	10,10,10,11,11,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,11,12,12,12, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,11,11,12,12, 0, 0, 0, 0, 0, 9, 9,
	10,10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,
	10,10,11,11,11,11,11,12,12,12,13,13, 0, 0, 0, 0,
	 0, 0, 0,11,10,11,11,11,11,12,12,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,11,12,12,12,12,13,13, 0, 0,
	 0, 0, 0, 0, 0,11,11,11,12,12,12,12,13,13,13, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,12,13,13,13,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,14,
	14,
};

static float _vq_quantthresh__44c0_s_p5_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c0_s_p5_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p5_0 = {
	_vq_quantthresh__44c0_s_p5_0,
	_vq_quantmap__44c0_s_p5_0,
	17,
	17
};

static static_codebook _44c0_s_p5_0 = {
	2, 289,
	_vq_lengthlist__44c0_s_p5_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c0_s_p5_0,
	NULL,
	&_vq_auxt__44c0_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p6_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c0_s_p6_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,10,
	 9, 9, 4, 6, 7,10, 9, 9,11, 9, 9, 7,10,10,11,11,
	11,12,10,11, 6, 9, 9,11,10,11,11,10,10, 6, 9, 9,
	11,10,11,11,10,10, 7,11,10,12,11,11,11,11,11, 7,
	 9, 9,10,10,10,11,11,10, 6, 9, 9,11,10,10,11,10,
	10,
};

static float _vq_quantthresh__44c0_s_p6_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c0_s_p6_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p6_0 = {
	_vq_quantthresh__44c0_s_p6_0,
	_vq_quantmap__44c0_s_p6_0,
	3,
	3
};

static static_codebook _44c0_s_p6_0 = {
	4, 81,
	_vq_lengthlist__44c0_s_p6_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c0_s_p6_0,
	NULL,
	&_vq_auxt__44c0_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p6_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c0_s_p6_1[] = {
	 2, 3, 3, 6, 6, 7, 7, 7, 7, 7, 8,10,10,10, 6, 6,
	 7, 7, 8, 8, 8, 8,10,10,10, 6, 6, 7, 7, 8, 8, 8,
	 8,10,10,10, 7, 7, 7, 7, 8, 8, 8, 8,10,10,10, 7,
	 7, 7, 7, 8, 8, 8, 8,10,10,10, 8, 7, 8, 8, 8, 8,
	 8, 8,10,10,10, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 9, 9, 8, 8, 8, 8,10,10,
	10,10,10, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c0_s_p6_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c0_s_p6_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p6_1 = {
	_vq_quantthresh__44c0_s_p6_1,
	_vq_quantmap__44c0_s_p6_1,
	11,
	11
};

static static_codebook _44c0_s_p6_1 = {
	2, 121,
	_vq_lengthlist__44c0_s_p6_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c0_s_p6_1,
	NULL,
	&_vq_auxt__44c0_s_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c0_s_p7_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 7, 5, 5,
	 7, 7, 8, 8, 8, 8, 9, 9,10,10, 7, 5, 6, 7, 7, 8,
	 8, 8, 8, 9, 9,10,10, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9,10,10,10,10,11,11,11,11, 0,13,
	13, 9, 9, 9, 9,10,10,11,11,11,12, 0, 0, 0,10,10,
	10,10,11,11,11,11,12,12, 0, 0, 0,10,10, 9, 9,11,
	11,11,12,12,12, 0, 0, 0,13,13,10,10,11,11,12,12,
	13,13, 0, 0, 0,14,14,10,10,11,11,12,12,13,13, 0,
	 0, 0, 0, 0,11,11,11,11,13,12,13,13, 0, 0, 0, 0,
	 0,12,12,11,11,12,12,13,13,
};

static float _vq_quantthresh__44c0_s_p7_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c0_s_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p7_0 = {
	_vq_quantthresh__44c0_s_p7_0,
	_vq_quantmap__44c0_s_p7_0,
	13,
	13
};

static static_codebook _44c0_s_p7_0 = {
	2, 169,
	_vq_lengthlist__44c0_s_p7_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c0_s_p7_0,
	NULL,
	&_vq_auxt__44c0_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p7_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c0_s_p7_1[] = {
	 2, 3, 3, 5, 5, 6, 6, 6, 5, 5, 6, 6, 6, 5, 5, 6,
	 6, 6, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c0_s_p7_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c0_s_p7_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p7_1 = {
	_vq_quantthresh__44c0_s_p7_1,
	_vq_quantmap__44c0_s_p7_1,
	5,
	5
};

static static_codebook _44c0_s_p7_1 = {
	2, 25,
	_vq_lengthlist__44c0_s_p7_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c0_s_p7_1,
	NULL,
	&_vq_auxt__44c0_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p8_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c0_s_p8_0[] = {
	 1, 5, 5,10,10, 6, 9, 8,10,10, 6,10, 9,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10, 8,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,10,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,
};

static float _vq_quantthresh__44c0_s_p8_0[] = {
	-331.5, -110.5, 110.5, 331.5,
};

static long _vq_quantmap__44c0_s_p8_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p8_0 = {
	_vq_quantthresh__44c0_s_p8_0,
	_vq_quantmap__44c0_s_p8_0,
	5,
	5
};

static static_codebook _44c0_s_p8_0 = {
	4, 625,
	_vq_lengthlist__44c0_s_p8_0,
	1, -518283264, 1627103232, 3, 0,
	_vq_quantlist__44c0_s_p8_0,
	NULL,
	&_vq_auxt__44c0_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p8_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c0_s_p8_1[] = {
	 1, 4, 4, 6, 6, 7, 7, 9, 9,11,12,13,12, 6, 5, 5,
	 7, 7, 8, 8,10, 9,12,12,12,12, 6, 5, 5, 7, 7, 8,
	 8,10, 9,12,11,11,13,16, 7, 7, 8, 8, 9, 9,10,10,
	12,12,13,12,16, 7, 7, 8, 7, 9, 9,10,10,11,12,12,
	13,16,10,10, 8, 8,10,10,11,12,12,12,13,13,16,11,
	10, 8, 7,11,10,11,11,12,11,13,13,16,16,16,10,10,
	10,10,11,11,13,12,13,13,16,16,16,11, 9,11, 9,15,
	13,12,13,13,13,16,16,16,15,13,11,11,12,13,12,12,
	14,13,16,16,16,14,13,11,11,13,12,14,13,13,13,16,
	16,16,16,16,13,13,13,12,14,13,14,14,16,16,16,16,
	16,13,13,12,12,14,14,15,13,
};

static float _vq_quantthresh__44c0_s_p8_1[] = {
	-93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5, 25.5,
	42.5, 59.5, 76.5, 93.5,
};

static long _vq_quantmap__44c0_s_p8_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p8_1 = {
	_vq_quantthresh__44c0_s_p8_1,
	_vq_quantmap__44c0_s_p8_1,
	13,
	13
};

static static_codebook _44c0_s_p8_1 = {
	2, 169,
	_vq_lengthlist__44c0_s_p8_1,
	1, -522616832, 1620115456, 4, 0,
	_vq_quantlist__44c0_s_p8_1,
	NULL,
	&_vq_auxt__44c0_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c0_s_p8_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c0_s_p8_2[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8,
	 8,10,10,10, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9,10,10,10, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9,10,10,10, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,
	 9,10, 9, 9,10,10,10, 7, 7, 8, 8, 9, 8, 9, 9, 9,
	 9,10, 9, 9,10,10,10,10, 8, 8, 8, 8, 9, 8, 9, 9,
	 9, 9, 9,10, 9,10,10,10,10, 7, 7, 8, 8, 9, 9, 9,
	 9, 9, 9,10, 9,10,10,10,10,10, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 9,11,10,10,10,10, 8, 8, 9,
	 9, 9, 9, 9,10, 9, 9, 9,10,10,10,10,11,11, 9, 9,
	 9, 9, 9, 9, 9, 9,10, 9, 9,10,11,10,10,11,11, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,11,11,10,11,11,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,11,10,10,11,
	11,11,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,
	11,11,11,11, 9,10, 9,10, 9, 9, 9, 9,10, 9,10,11,
	10,11,10,10,10,10,10, 9, 9, 9,10, 9, 9, 9,10,11,
	11,10,11,11,10,11,10,10,10, 9, 9, 9, 9,10, 9, 9,
	10,11,10,11,11,11,11,10,11,10,10, 9,10, 9, 9, 9,
	10,
};

static float _vq_quantthresh__44c0_s_p8_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c0_s_p8_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c0_s_p8_2 = {
	_vq_quantthresh__44c0_s_p8_2,
	_vq_quantmap__44c0_s_p8_2,
	17,
	17
};

static static_codebook _44c0_s_p8_2 = {
	2, 289,
	_vq_lengthlist__44c0_s_p8_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c0_s_p8_2,
	NULL,
	&_vq_auxt__44c0_s_p8_2,
	NULL,
	0
};

static long _huff_lengthlist__44c0_s_short[] = {
	 9, 8,12,11,12,13,14,14,16, 6, 1, 5, 6, 6, 9,12,
	14,17, 9, 4, 5, 9, 7, 9,13,15,16, 8, 5, 8, 6, 8,
	10,13,17,17, 9, 6, 7, 7, 8, 9,13,15,17,11, 8, 9,
	 9, 9,10,12,16,16,13, 7, 8, 7, 7, 9,12,14,15,13,
	 6, 7, 5, 5, 7,10,13,13,14, 7, 8, 5, 6, 7, 9,10,
	12,
};

static static_codebook _huff_book__44c0_s_short = {
	2, 81,
	_huff_lengthlist__44c0_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c0_sm_long[] = {
	 5, 4, 9,10, 9,10,11,12,13, 4, 1, 5, 7, 7, 9,11,
	12,14, 8, 5, 7, 9, 8,10,13,13,13,10, 7, 9, 4, 6,
	 7,10,12,14, 9, 6, 7, 6, 6, 7,10,12,12, 9, 8, 9,
	 7, 6, 7, 8,11,12,11,11,11, 9, 8, 7, 8,10,12,12,
	13,14,12,11, 9, 9, 9,12,12,17,17,15,16,12,10,11,
	13,
};

static static_codebook _huff_book__44c0_sm_long = {
	2, 81,
	_huff_lengthlist__44c0_sm_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c0_sm_p1_0[] = {
	 1, 5, 5, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 7, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 9, 8, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 7, 0, 0, 0, 0,
	 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 9,10,10, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8,10, 9, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 8, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 9, 9,10, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 9,10,10, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c0_sm_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c0_sm_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p1_0 = {
	_vq_quantthresh__44c0_sm_p1_0,
	_vq_quantmap__44c0_sm_p1_0,
	3,
	3
};

static static_codebook _44c0_sm_p1_0 = {
	8, 6561,
	_vq_lengthlist__44c0_sm_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c0_sm_p1_0,
	NULL,
	&_vq_auxt__44c0_sm_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c0_sm_p2_0[] = {
	 1, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 7, 7, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 5, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 7, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 7, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c0_sm_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c0_sm_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p2_0 = {
	_vq_quantthresh__44c0_sm_p2_0,
	_vq_quantmap__44c0_sm_p2_0,
	5,
	5
};

static static_codebook _44c0_sm_p2_0 = {
	4, 625,
	_vq_lengthlist__44c0_sm_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c0_sm_p2_0,
	NULL,
	&_vq_auxt__44c0_sm_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c0_sm_p3_0[] = {
	 1, 3, 3, 7, 7, 0, 0, 0, 0, 0, 5, 4, 7, 7, 0, 0,
	 0, 0, 0, 5, 5, 7, 7, 0, 0, 0, 0, 0, 6, 7, 8, 8,
	 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0,
	 9,10, 0, 0, 0, 0, 0, 0, 0, 9, 9, 0, 0, 0, 0, 0,
	 0, 0,11,11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c0_sm_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c0_sm_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p3_0 = {
	_vq_quantthresh__44c0_sm_p3_0,
	_vq_quantmap__44c0_sm_p3_0,
	9,
	9
};

static static_codebook _44c0_sm_p3_0 = {
	2, 81,
	_vq_lengthlist__44c0_sm_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c0_sm_p3_0,
	NULL,
	&_vq_auxt__44c0_sm_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c0_sm_p4_0[] = {
	 1, 4, 3, 6, 6, 7, 7, 9, 9, 0, 5, 5, 7, 7, 8, 7,
	 9, 9, 0, 5, 5, 7, 7, 8, 8, 9, 9, 0, 7, 7, 8, 8,
	 8, 8,10,10, 0, 0, 0, 8, 8, 8, 8,10,10, 0, 0, 0,
	 9, 9, 9, 9,11,11, 0, 0, 0, 9, 9, 9, 9,11,11, 0,
	 0, 0,10,10,10,10,11,11, 0, 0, 0, 0, 0, 9, 9,11,
	11,
};

static float _vq_quantthresh__44c0_sm_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c0_sm_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p4_0 = {
	_vq_quantthresh__44c0_sm_p4_0,
	_vq_quantmap__44c0_sm_p4_0,
	9,
	9
};

static static_codebook _44c0_sm_p4_0 = {
	2, 81,
	_vq_lengthlist__44c0_sm_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c0_sm_p4_0,
	NULL,
	&_vq_auxt__44c0_sm_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p5_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c0_sm_p5_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 9, 9,10,10,11,
	11, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,11,
	11,11, 0, 5, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,
	11,11,11, 0, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,10,10,
	11,11,12,12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,10,
	11,11,11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,
	10,11,11,11,11,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12,12,13, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,
	10,10,11,11,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,11,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,
	10,10,10,10,11,11,12,12,12,13,13,13, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,12,12,12,13,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,12,12,12,13,13,14,14, 0, 0,
	 0, 0, 0, 0, 0,11,11,12,11,12,12,13,13,13,13, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,13,13,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,14,
	14,
};

static float _vq_quantthresh__44c0_sm_p5_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c0_sm_p5_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p5_0 = {
	_vq_quantthresh__44c0_sm_p5_0,
	_vq_quantmap__44c0_sm_p5_0,
	17,
	17
};

static static_codebook _44c0_sm_p5_0 = {
	2, 289,
	_vq_lengthlist__44c0_sm_p5_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c0_sm_p5_0,
	NULL,
	&_vq_auxt__44c0_sm_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p6_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c0_sm_p6_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,11,
	 9, 9, 4, 7, 7,10, 9, 9,11, 9, 9, 7,10,10,10,11,
	11,11,10,10, 6, 9, 9,11,11,10,11,10,10, 6, 9, 9,
	11,10,11,11,10,10, 7,11,10,11,11,11,11,11,11, 6,
	 9, 9,11,10,10,11,11,10, 6, 9, 9,11,10,10,11,10,
	11,
};

static float _vq_quantthresh__44c0_sm_p6_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c0_sm_p6_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p6_0 = {
	_vq_quantthresh__44c0_sm_p6_0,
	_vq_quantmap__44c0_sm_p6_0,
	3,
	3
};

static static_codebook _44c0_sm_p6_0 = {
	4, 81,
	_vq_lengthlist__44c0_sm_p6_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c0_sm_p6_0,
	NULL,
	&_vq_auxt__44c0_sm_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p6_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c0_sm_p6_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 7, 8, 9, 5, 5, 6, 6,
	 7, 7, 8, 8, 8, 8, 9, 5, 5, 6, 6, 7, 7, 8, 8, 8,
	 8,10, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,10,10,10, 7,
	 7, 7, 7, 8, 8, 8, 8,10,10,10, 8, 8, 8, 8, 8, 8,
	 8, 8,10,10,10, 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 9, 9, 8, 8, 8, 8,10,10,
	10,10,10, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c0_sm_p6_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c0_sm_p6_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p6_1 = {
	_vq_quantthresh__44c0_sm_p6_1,
	_vq_quantmap__44c0_sm_p6_1,
	11,
	11
};

static static_codebook _44c0_sm_p6_1 = {
	2, 121,
	_vq_lengthlist__44c0_sm_p6_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c0_sm_p6_1,
	NULL,
	&_vq_auxt__44c0_sm_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c0_sm_p7_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 7, 5, 5,
	 7, 7, 8, 8, 8, 8, 9, 9,10,10, 7, 6, 5, 7, 7, 8,
	 8, 8, 8, 9, 9,10,10, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9,10,10,10,10,11,11,11,11, 0,13,
	13, 9, 9, 9, 9,10,10,11,11,11,12, 0, 0, 0, 9,10,
	10,10,11,11,12,11,12,12, 0, 0, 0,10,10, 9, 9,11,
	11,12,12,12,12, 0, 0, 0,13,13,10,10,11,11,12,12,
	13,13, 0, 0, 0,14,14,10,10,11,11,12,12,13,13, 0,
	 0, 0, 0, 0,11,12,11,11,13,12,13,13, 0, 0, 0, 0,
	 0,12,12,11,11,13,12,14,14,
};

static float _vq_quantthresh__44c0_sm_p7_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c0_sm_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p7_0 = {
	_vq_quantthresh__44c0_sm_p7_0,
	_vq_quantmap__44c0_sm_p7_0,
	13,
	13
};

static static_codebook _44c0_sm_p7_0 = {
	2, 169,
	_vq_lengthlist__44c0_sm_p7_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c0_sm_p7_0,
	NULL,
	&_vq_auxt__44c0_sm_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p7_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c0_sm_p7_1[] = {
	 2, 4, 4, 4, 4, 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6,
	 6, 6, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c0_sm_p7_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c0_sm_p7_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p7_1 = {
	_vq_quantthresh__44c0_sm_p7_1,
	_vq_quantmap__44c0_sm_p7_1,
	5,
	5
};

static static_codebook _44c0_sm_p7_1 = {
	2, 25,
	_vq_lengthlist__44c0_sm_p7_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c0_sm_p7_1,
	NULL,
	&_vq_auxt__44c0_sm_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p8_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c0_sm_p8_0[] = {
	 1, 3, 3,11,11,11,11,11,11, 3, 7, 6,11,11,11,11,
	11,11, 4, 8, 7,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,
};

static float _vq_quantthresh__44c0_sm_p8_0[] = {
	-773.5, -552.5, -331.5, -110.5, 110.5, 331.5, 552.5, 773.5,
};

static long _vq_quantmap__44c0_sm_p8_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p8_0 = {
	_vq_quantthresh__44c0_sm_p8_0,
	_vq_quantmap__44c0_sm_p8_0,
	9,
	9
};

static static_codebook _44c0_sm_p8_0 = {
	2, 81,
	_vq_lengthlist__44c0_sm_p8_0,
	1, -516186112, 1627103232, 4, 0,
	_vq_quantlist__44c0_sm_p8_0,
	NULL,
	&_vq_auxt__44c0_sm_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p8_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c0_sm_p8_1[] = {
	 1, 4, 4, 6, 6, 7, 7, 9, 9,10,11,12,12, 6, 5, 5,
	 7, 7, 8, 8,10,10,12,11,12,12, 6, 5, 5, 7, 7, 8,
	 8,10,10,12,11,12,12,17, 7, 7, 8, 8, 9, 9,10,10,
	12,12,13,13,18, 7, 7, 8, 7, 9, 9,10,10,12,12,12,
	13,19,10,10, 8, 8,10,10,11,11,12,12,13,14,19,11,
	10, 8, 7,10,10,11,11,12,12,13,12,19,19,19,10,10,
	10,10,11,11,12,12,13,13,19,19,19,11, 9,11, 9,14,
	12,13,12,13,13,19,20,18,13,14,11,11,12,12,13,13,
	14,13,20,20,20,15,13,11,10,13,11,13,13,14,13,20,
	20,20,20,20,13,14,12,12,13,13,13,13,20,20,20,20,
	20,13,13,12,12,16,13,15,13,
};

static float _vq_quantthresh__44c0_sm_p8_1[] = {
	-93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5, 25.5,
	42.5, 59.5, 76.5, 93.5,
};

static long _vq_quantmap__44c0_sm_p8_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p8_1 = {
	_vq_quantthresh__44c0_sm_p8_1,
	_vq_quantmap__44c0_sm_p8_1,
	13,
	13
};

static static_codebook _44c0_sm_p8_1 = {
	2, 169,
	_vq_lengthlist__44c0_sm_p8_1,
	1, -522616832, 1620115456, 4, 0,
	_vq_quantlist__44c0_sm_p8_1,
	NULL,
	&_vq_auxt__44c0_sm_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c0_sm_p8_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c0_sm_p8_2[] = {
	 2, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8,
	 8,10, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9,10, 6, 6, 7, 7, 8, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9,10, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 7, 7, 8, 8, 9, 8, 9, 9, 9,
	 9,10, 9, 9,10,10,10,11, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 9,10, 9,10,10,10,10, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 9, 9,10,10,11,10,10, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10,10,10,10,11,11, 8, 8, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,11,11,11,11,11, 9, 9,
	 9, 9, 9, 9, 9, 9,10, 9,10, 9,11,11,10,11,11, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,11,11,10,11,11,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,11,10,11,11,
	11,11,11, 9, 9,10, 9, 9, 9, 9, 9, 9, 9,10,11,10,
	11,11,11,11,10,10,10,10, 9, 9, 9, 9, 9, 9,10,11,
	11,11,11,11,11, 9,10, 9, 9, 9, 9, 9, 9, 9, 9,11,
	11,10,11,11,11,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9,
	10,11,10,11,11,11,11,11,11, 9, 9, 9, 9, 9, 9, 9,
	 9,
};

static float _vq_quantthresh__44c0_sm_p8_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c0_sm_p8_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c0_sm_p8_2 = {
	_vq_quantthresh__44c0_sm_p8_2,
	_vq_quantmap__44c0_sm_p8_2,
	17,
	17
};

static static_codebook _44c0_sm_p8_2 = {
	2, 289,
	_vq_lengthlist__44c0_sm_p8_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c0_sm_p8_2,
	NULL,
	&_vq_auxt__44c0_sm_p8_2,
	NULL,
	0
};

static long _huff_lengthlist__44c0_sm_short[] = {
	 6, 6,12,13,13,14,16,17,17, 4, 2, 5, 8, 7, 9,12,
	15,15, 9, 4, 5, 9, 7, 9,12,16,18,11, 6, 7, 4, 6,
	 8,11,14,18,10, 5, 6, 5, 5, 7,10,14,17,10, 5, 7,
	 7, 6, 7,10,13,16,11, 5, 7, 7, 7, 8,10,12,15,13,
	 6, 7, 5, 5, 7, 9,12,13,16, 8, 9, 6, 6, 7, 9,10,
	12,
};

static static_codebook _huff_book__44c0_sm_short = {
	2, 81,
	_huff_lengthlist__44c0_sm_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c1_s_long[] = {
	 5, 5, 9,10, 9, 9,10,11,12, 5, 1, 5, 6, 6, 7,10,
	12,14, 9, 5, 6, 8, 8,10,12,14,14,10, 5, 8, 5, 6,
	 8,11,13,14, 9, 5, 7, 6, 6, 8,10,12,11, 9, 7, 9,
	 7, 6, 6, 7,10,10,10, 9,12, 9, 8, 7, 7,10,12,11,
	11,13,12,10, 9, 8, 9,11,11,14,15,15,13,11, 9, 9,
	11,
};

static static_codebook _huff_book__44c1_s_long = {
	2, 81,
	_huff_lengthlist__44c1_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c1_s_p1_0[] = {
	 2, 4, 4, 0, 0, 0, 0, 0, 0, 5, 7, 6, 0, 0, 0, 0,
	 0, 0, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,
	 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 7, 0, 0, 0, 0,
	 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0,
	 0, 0, 0, 8, 9,10, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8, 8, 0, 0,
	 0, 0, 0, 0, 8, 9, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0,
	 0, 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 6, 8, 8, 0, 0, 0, 0, 0, 0, 8,10, 9, 0,
	 0, 0, 0, 0, 0, 8, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9,
	 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c1_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c1_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p1_0 = {
	_vq_quantthresh__44c1_s_p1_0,
	_vq_quantmap__44c1_s_p1_0,
	3,
	3
};

static static_codebook _44c1_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__44c1_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c1_s_p1_0,
	NULL,
	&_vq_auxt__44c1_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c1_s_p2_0[] = {
	 2, 3, 4, 6, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 4, 5, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 8, 8,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 6, 6, 8, 8, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c1_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c1_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p2_0 = {
	_vq_quantthresh__44c1_s_p2_0,
	_vq_quantmap__44c1_s_p2_0,
	5,
	5
};

static static_codebook _44c1_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44c1_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c1_s_p2_0,
	NULL,
	&_vq_auxt__44c1_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c1_s_p3_0[] = {
	 1, 3, 2, 7, 7, 0, 0, 0, 0, 0,13,13, 6, 6, 0, 0,
	 0, 0, 0,12, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 8, 9, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0,
	 0, 0,11,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c1_s_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c1_s_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p3_0 = {
	_vq_quantthresh__44c1_s_p3_0,
	_vq_quantmap__44c1_s_p3_0,
	9,
	9
};

static static_codebook _44c1_s_p3_0 = {
	2, 81,
	_vq_lengthlist__44c1_s_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c1_s_p3_0,
	NULL,
	&_vq_auxt__44c1_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c1_s_p4_0[] = {
	 1, 3, 3, 6, 5, 6, 6, 8, 8, 0, 0, 0, 7, 7, 7, 7,
	 9, 9, 0, 0, 0, 7, 7, 7, 7, 9, 9, 0, 0, 0, 7, 7,
	 8, 8,10,10, 0, 0, 0, 7, 7, 8, 8,10,10, 0, 0, 0,
	 9, 9, 8, 8,10,10, 0, 0, 0, 8, 8, 8, 8,10,10, 0,
	 0, 0,10,10, 9, 9,11,11, 0, 0, 0, 0, 0, 9, 9,11,
	11,
};

static float _vq_quantthresh__44c1_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c1_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p4_0 = {
	_vq_quantthresh__44c1_s_p4_0,
	_vq_quantmap__44c1_s_p4_0,
	9,
	9
};

static static_codebook _44c1_s_p4_0 = {
	2, 81,
	_vq_lengthlist__44c1_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c1_s_p4_0,
	NULL,
	&_vq_auxt__44c1_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p5_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c1_s_p5_0[] = {
	 1, 4, 3, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,11,
	11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,10,
	11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,
	10,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,11, 0, 0, 0, 8, 8, 9, 9, 9,10,10,10,
	10,10,11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10, 9,10,
	10,10,10,11,11,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 9, 9,
	10,10,10,11,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,
	10,10,10,10,11,11,12,12,12,12,13,13, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,12,12,12,12,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,12,12,12,13,13,13,13, 0, 0,
	 0, 0, 0, 0, 0,11,11,11,11,12,12,13,13,13,13, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,12,12,12,13,13,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,14,
	14,
};

static float _vq_quantthresh__44c1_s_p5_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c1_s_p5_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p5_0 = {
	_vq_quantthresh__44c1_s_p5_0,
	_vq_quantmap__44c1_s_p5_0,
	17,
	17
};

static static_codebook _44c1_s_p5_0 = {
	2, 289,
	_vq_lengthlist__44c1_s_p5_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c1_s_p5_0,
	NULL,
	&_vq_auxt__44c1_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p6_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c1_s_p6_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,11,
	 9, 9, 4, 7, 7,10, 9, 9,11, 9, 9, 6,10,10,11,11,
	11,11,10,10, 6, 9, 9,11,10,10,11,10,10, 6, 9, 9,
	11,10,11,11,10,10, 7,11,10,11,11,11,12,11,11, 7,
	 9, 9,11,10,10,11,11,10, 6, 9, 9,10,10,10,12,10,
	11,
};

static float _vq_quantthresh__44c1_s_p6_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c1_s_p6_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p6_0 = {
	_vq_quantthresh__44c1_s_p6_0,
	_vq_quantmap__44c1_s_p6_0,
	3,
	3
};

static static_codebook _44c1_s_p6_0 = {
	4, 81,
	_vq_lengthlist__44c1_s_p6_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c1_s_p6_0,
	NULL,
	&_vq_auxt__44c1_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p6_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c1_s_p6_1[] = {
	 2, 3, 3, 6, 6, 7, 7, 7, 7, 8, 8,10,10,10, 6, 6,
	 7, 7, 8, 8, 8, 8,10,10,10, 6, 6, 7, 7, 8, 8, 8,
	 8,10,10,10, 7, 7, 7, 7, 8, 8, 8, 8,10,10,10, 7,
	 7, 7, 7, 8, 8, 8, 8,10,10,10, 7, 7, 8, 8, 8, 8,
	 8, 8,10,10,10, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 9, 9, 8, 8, 8, 8,10,10,
	10,10,10, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c1_s_p6_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c1_s_p6_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p6_1 = {
	_vq_quantthresh__44c1_s_p6_1,
	_vq_quantmap__44c1_s_p6_1,
	11,
	11
};

static static_codebook _44c1_s_p6_1 = {
	2, 121,
	_vq_lengthlist__44c1_s_p6_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c1_s_p6_1,
	NULL,
	&_vq_auxt__44c1_s_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c1_s_p7_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8,10, 9, 7, 5, 6,
	 7, 7, 8, 8, 8, 8, 9, 9,10,10, 7, 5, 5, 7, 7, 8,
	 8, 8, 8, 9, 9,10,10, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,10, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9, 9,10,10,10,11,11,11,11, 0,13,
	13, 9, 9, 9, 9,10,10,11,11,11,11, 0, 0, 0,10,10,
	10,10,11,11,12,11,12,12, 0, 0, 0,10,10,10, 9,11,
	11,12,11,13,12, 0, 0, 0,13,13,10,10,11,11,12,12,
	13,13, 0, 0, 0,14,14,10,10,11,11,12,12,13,13, 0,
	 0, 0, 0, 0,11,12,11,11,12,12,14,13, 0, 0, 0, 0,
	 0,12,11,11,11,13,10,14,13,
};

static float _vq_quantthresh__44c1_s_p7_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c1_s_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p7_0 = {
	_vq_quantthresh__44c1_s_p7_0,
	_vq_quantmap__44c1_s_p7_0,
	13,
	13
};

static static_codebook _44c1_s_p7_0 = {
	2, 169,
	_vq_lengthlist__44c1_s_p7_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c1_s_p7_0,
	NULL,
	&_vq_auxt__44c1_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p7_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c1_s_p7_1[] = {
	 2, 3, 3, 5, 5, 6, 6, 6, 5, 5, 6, 6, 6, 5, 5, 6,
	 6, 6, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c1_s_p7_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c1_s_p7_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p7_1 = {
	_vq_quantthresh__44c1_s_p7_1,
	_vq_quantmap__44c1_s_p7_1,
	5,
	5
};

static static_codebook _44c1_s_p7_1 = {
	2, 25,
	_vq_lengthlist__44c1_s_p7_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c1_s_p7_1,
	NULL,
	&_vq_auxt__44c1_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c1_s_p8_0[] = {
	 1, 4, 3,10,10,10,10,10,10,10,10,10,10, 4, 8, 6,
	10,10,10,10,10,10,10,10,10,10, 4, 8, 7,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__44c1_s_p8_0[] = {
	-1215.5, -994.5, -773.5, -552.5, -331.5, -110.5, 110.5, 331.5,
	552.5, 773.5, 994.5, 1215.5,
};

static long _vq_quantmap__44c1_s_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p8_0 = {
	_vq_quantthresh__44c1_s_p8_0,
	_vq_quantmap__44c1_s_p8_0,
	13,
	13
};

static static_codebook _44c1_s_p8_0 = {
	2, 169,
	_vq_lengthlist__44c1_s_p8_0,
	1, -514541568, 1627103232, 4, 0,
	_vq_quantlist__44c1_s_p8_0,
	NULL,
	&_vq_auxt__44c1_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p8_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c1_s_p8_1[] = {
	 1, 4, 4, 6, 5, 7, 7, 9, 9,10,10,12,12, 6, 5, 5,
	 7, 7, 8, 8,10,10,12,11,12,12, 6, 5, 5, 7, 7, 8,
	 8,10,10,11,11,12,12,15, 7, 7, 8, 8, 9, 9,11,11,
	12,12,13,12,15, 8, 8, 8, 7, 9, 9,10,10,12,12,13,
	13,16,11,10, 8, 8,10,10,11,11,12,12,13,13,16,11,
	11, 9, 8,11,10,11,11,12,12,13,12,16,16,16,10,11,
	10,11,12,12,12,12,13,13,16,16,16,11, 9,11, 9,14,
	12,12,12,13,13,16,16,16,12,14,11,12,12,12,13,13,
	14,13,16,16,16,15,13,12,10,13,10,13,14,13,13,16,
	16,16,16,16,13,14,12,13,13,12,13,13,16,16,16,16,
	16,13,12,12,11,14,12,15,13,
};

static float _vq_quantthresh__44c1_s_p8_1[] = {
	-93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5, 25.5,
	42.5, 59.5, 76.5, 93.5,
};

static long _vq_quantmap__44c1_s_p8_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p8_1 = {
	_vq_quantthresh__44c1_s_p8_1,
	_vq_quantmap__44c1_s_p8_1,
	13,
	13
};

static static_codebook _44c1_s_p8_1 = {
	2, 169,
	_vq_lengthlist__44c1_s_p8_1,
	1, -522616832, 1620115456, 4, 0,
	_vq_quantlist__44c1_s_p8_1,
	NULL,
	&_vq_auxt__44c1_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c1_s_p8_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c1_s_p8_2[] = {
	 2, 4, 4, 6, 6, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8,
	 8,10,10,10, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9,10,10,10, 7, 7, 8, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9,10,10,10, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9,
	 9,10, 9, 9,10,10,10, 7, 7, 8, 8, 9, 8, 9, 9, 9,
	 9,10, 9, 9,10,10,11,11, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9,10, 9, 9,10,10,10,10, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 9, 9,10,10,11,11,11, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10,10,10,11,11,11, 8, 8, 9,
	 9, 9, 9,10, 9, 9, 9, 9, 9,11,11,11,11,11, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,10,10,11,11, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,11,10,11,11,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10, 9,10,10,11,11,
	11,11,11, 9, 9, 9,10, 9, 9, 9, 9, 9, 9,10,11,11,
	11,11,11,11,10,10,10,10, 9, 9, 9, 9, 9, 9,10,11,
	11,11,11,11,11, 9,10, 9, 9, 9, 9,10, 9, 9, 9,11,
	11,11,11,11,11,11,10,10, 9, 9, 9, 9, 9, 9,10, 9,
	11,11,10,11,11,11,11,10,11, 9, 9, 9, 9, 9, 9, 9,
	 9,
};

static float _vq_quantthresh__44c1_s_p8_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c1_s_p8_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c1_s_p8_2 = {
	_vq_quantthresh__44c1_s_p8_2,
	_vq_quantmap__44c1_s_p8_2,
	17,
	17
};

static static_codebook _44c1_s_p8_2 = {
	2, 289,
	_vq_lengthlist__44c1_s_p8_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c1_s_p8_2,
	NULL,
	&_vq_auxt__44c1_s_p8_2,
	NULL,
	0
};

static long _huff_lengthlist__44c1_s_short[] = {
	 6, 8,13,12,13,14,15,16,16, 4, 2, 4, 7, 6, 8,11,
	13,15,10, 4, 4, 8, 6, 8,11,14,17,11, 5, 6, 5, 6,
	 8,12,14,17,11, 5, 5, 6, 5, 7,10,13,16,12, 6, 7,
	 8, 7, 8,10,13,15,13, 8, 8, 7, 7, 8,10,12,15,15,
	 7, 7, 5, 5, 7, 9,12,14,15, 8, 8, 6, 6, 7, 8,10,
	11,
};

static static_codebook _huff_book__44c1_s_short = {
	2, 81,
	_huff_lengthlist__44c1_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44c1_sm_long[] = {
	 5, 4, 8,10, 9, 9,10,11,12, 4, 2, 5, 6, 6, 8,10,
	11,13, 8, 4, 6, 8, 7, 9,12,12,14,10, 6, 8, 4, 5,
	 6, 9,11,12, 9, 5, 6, 5, 5, 6, 9,11,11, 9, 7, 9,
	 6, 5, 5, 7,10,10,10, 9,11, 8, 7, 6, 7, 9,11,11,
	12,13,10,10, 9, 8, 9,11,11,15,15,12,13,11, 9,10,
	11,
};

static static_codebook _huff_book__44c1_sm_long = {
	2, 81,
	_huff_lengthlist__44c1_sm_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c1_sm_p1_0[] = {
	 1, 5, 5, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 7, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 9, 8, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 7, 0, 0, 0, 0,
	 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0, 0,
	 0, 0, 0, 9, 9,10, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8,10, 9, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 7, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 8, 9,10, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 9,10, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c1_sm_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44c1_sm_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p1_0 = {
	_vq_quantthresh__44c1_sm_p1_0,
	_vq_quantmap__44c1_sm_p1_0,
	3,
	3
};

static static_codebook _44c1_sm_p1_0 = {
	8, 6561,
	_vq_lengthlist__44c1_sm_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44c1_sm_p1_0,
	NULL,
	&_vq_auxt__44c1_sm_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c1_sm_p2_0[] = {
	 2, 3, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 6, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c1_sm_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c1_sm_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p2_0 = {
	_vq_quantthresh__44c1_sm_p2_0,
	_vq_quantmap__44c1_sm_p2_0,
	5,
	5
};

static static_codebook _44c1_sm_p2_0 = {
	4, 625,
	_vq_lengthlist__44c1_sm_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c1_sm_p2_0,
	NULL,
	&_vq_auxt__44c1_sm_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c1_sm_p3_0[] = {
	 1, 3, 3, 7, 7, 0, 0, 0, 0, 0, 5, 5, 6, 6, 0, 0,
	 0, 0, 0, 5, 5, 7, 7, 0, 0, 0, 0, 0, 7, 7, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 8, 9, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0,
	 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44c1_sm_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c1_sm_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p3_0 = {
	_vq_quantthresh__44c1_sm_p3_0,
	_vq_quantmap__44c1_sm_p3_0,
	9,
	9
};

static static_codebook _44c1_sm_p3_0 = {
	2, 81,
	_vq_lengthlist__44c1_sm_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c1_sm_p3_0,
	NULL,
	&_vq_auxt__44c1_sm_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44c1_sm_p4_0[] = {
	 1, 3, 3, 6, 6, 7, 7, 9, 9, 0, 6, 6, 7, 7, 8, 8,
	 9, 9, 0, 6, 6, 7, 7, 8, 8, 9, 9, 0, 7, 7, 8, 8,
	 8, 8,10,10, 0, 0, 0, 8, 8, 8, 8,10,10, 0, 0, 0,
	 8, 8, 9, 9,11,11, 0, 0, 0, 9, 9, 9, 9,11,11, 0,
	 0, 0,10,10,10,10,11,11, 0, 0, 0, 0, 0, 9, 9,11,
	11,
};

static float _vq_quantthresh__44c1_sm_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44c1_sm_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p4_0 = {
	_vq_quantthresh__44c1_sm_p4_0,
	_vq_quantmap__44c1_sm_p4_0,
	9,
	9
};

static static_codebook _44c1_sm_p4_0 = {
	2, 81,
	_vq_lengthlist__44c1_sm_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44c1_sm_p4_0,
	NULL,
	&_vq_auxt__44c1_sm_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p5_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c1_sm_p5_0[] = {
	 2, 3, 3, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,11,
	11, 0, 5, 5, 6, 6, 8, 8, 9, 9, 9, 9,10,10,10,10,
	11,11, 0, 5, 5, 6, 6, 8, 8, 9, 9, 9, 9,10,10,10,
	10,11,11, 0, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,12, 0, 0, 0, 8, 8, 8, 8, 9, 9,10,10,
	10,11,11,11,12,12, 0, 0, 0, 8, 8, 8, 8, 9, 9,10,
	10,10,10,11,11,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,
	 9, 9,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9,
	 9, 9, 9,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,
	 9, 9,10,10,11,11,12,12,12,12,13,13, 0, 0, 0, 0,
	 0, 0, 0,10,10,11,11,12,12,12,12,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,11,11,12,12,13,13,13,13, 0, 0,
	 0, 0, 0, 0, 0,11,11,11,11,12,12,13,13,13,13, 0,
	 0, 0, 0, 0, 0, 0,11,11,12,12,12,12,13,13,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,12,12,13,13,14,
	14,
};

static float _vq_quantthresh__44c1_sm_p5_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c1_sm_p5_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p5_0 = {
	_vq_quantthresh__44c1_sm_p5_0,
	_vq_quantmap__44c1_sm_p5_0,
	17,
	17
};

static static_codebook _44c1_sm_p5_0 = {
	2, 289,
	_vq_lengthlist__44c1_sm_p5_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c1_sm_p5_0,
	NULL,
	&_vq_auxt__44c1_sm_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p6_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44c1_sm_p6_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7,10, 9, 9,11,
	 9, 9, 4, 7, 7,10, 9, 9,11, 9, 9, 7,10,10,10,11,
	11,11,10,10, 6, 9, 9,11,11,10,11,10,10, 6, 9, 9,
	11,10,11,11,10,10, 7,11,11,11,11,11,11,11,11, 6,
	 9, 9,11,10,10,11,11,10, 6, 9, 9,10,10,10,11,10,
	11,
};

static float _vq_quantthresh__44c1_sm_p6_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44c1_sm_p6_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p6_0 = {
	_vq_quantthresh__44c1_sm_p6_0,
	_vq_quantmap__44c1_sm_p6_0,
	3,
	3
};

static static_codebook _44c1_sm_p6_0 = {
	4, 81,
	_vq_lengthlist__44c1_sm_p6_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44c1_sm_p6_0,
	NULL,
	&_vq_auxt__44c1_sm_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p6_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44c1_sm_p6_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8,10, 5, 5, 6, 6,
	 7, 7, 8, 8, 8, 8,10, 5, 5, 6, 6, 7, 7, 8, 8, 8,
	 8,10, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10, 7,
	 7, 7, 7, 8, 8, 8, 8,10,10,10, 7, 7, 8, 8, 8, 8,
	 8, 8,10,10,10, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 9, 8,10,10,10,10,10, 8, 8, 8,
	 8, 8, 8,10,10,10,10,10, 9, 9, 8, 8, 8, 8,10,10,
	10,10,10, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44c1_sm_p6_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44c1_sm_p6_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p6_1 = {
	_vq_quantthresh__44c1_sm_p6_1,
	_vq_quantmap__44c1_sm_p6_1,
	11,
	11
};

static static_codebook _44c1_sm_p6_1 = {
	2, 121,
	_vq_lengthlist__44c1_sm_p6_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44c1_sm_p6_1,
	NULL,
	&_vq_auxt__44c1_sm_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c1_sm_p7_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 7, 5, 5,
	 7, 7, 8, 8, 8, 8, 9, 9,10,10, 7, 5, 6, 7, 7, 8,
	 8, 8, 8, 9, 9,11,10, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9,10,10,10,10,11,11,11,11, 0,13,
	13, 9, 9, 9, 9,10,10,11,11,12,12, 0, 0, 0, 9,10,
	 9,10,11,11,12,11,13,12, 0, 0, 0,10,10, 9, 9,11,
	11,12,12,13,12, 0, 0, 0,13,13,10,10,11,11,12,12,
	13,13, 0, 0, 0,14,14,10,10,11,11,12,12,13,13, 0,
	 0, 0, 0, 0,11,12,11,11,12,13,14,13, 0, 0, 0, 0,
	 0,12,12,11,11,13,12,14,13,
};

static float _vq_quantthresh__44c1_sm_p7_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44c1_sm_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p7_0 = {
	_vq_quantthresh__44c1_sm_p7_0,
	_vq_quantmap__44c1_sm_p7_0,
	13,
	13
};

static static_codebook _44c1_sm_p7_0 = {
	2, 169,
	_vq_lengthlist__44c1_sm_p7_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44c1_sm_p7_0,
	NULL,
	&_vq_auxt__44c1_sm_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p7_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44c1_sm_p7_1[] = {
	 2, 4, 4, 4, 5, 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44c1_sm_p7_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44c1_sm_p7_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p7_1 = {
	_vq_quantthresh__44c1_sm_p7_1,
	_vq_quantmap__44c1_sm_p7_1,
	5,
	5
};

static static_codebook _44c1_sm_p7_1 = {
	2, 25,
	_vq_lengthlist__44c1_sm_p7_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44c1_sm_p7_1,
	NULL,
	&_vq_auxt__44c1_sm_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p8_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c1_sm_p8_0[] = {
	 1, 3, 3,13,13,13,13,13,13,13,13,13,13, 3, 6, 6,
	13,13,13,13,13,13,13,13,13,13, 4, 8, 7,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,
};

static float _vq_quantthresh__44c1_sm_p8_0[] = {
	-1215.5, -994.5, -773.5, -552.5, -331.5, -110.5, 110.5, 331.5,
	552.5, 773.5, 994.5, 1215.5,
};

static long _vq_quantmap__44c1_sm_p8_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p8_0 = {
	_vq_quantthresh__44c1_sm_p8_0,
	_vq_quantmap__44c1_sm_p8_0,
	13,
	13
};

static static_codebook _44c1_sm_p8_0 = {
	2, 169,
	_vq_lengthlist__44c1_sm_p8_0,
	1, -514541568, 1627103232, 4, 0,
	_vq_quantlist__44c1_sm_p8_0,
	NULL,
	&_vq_auxt__44c1_sm_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p8_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44c1_sm_p8_1[] = {
	 1, 4, 4, 6, 6, 7, 7, 9, 9,10,11,12,12, 6, 5, 5,
	 7, 7, 8, 7,10,10,11,11,12,12, 6, 5, 5, 7, 7, 8,
	 8,10,10,11,11,12,12,16, 7, 7, 8, 8, 9, 9,11,11,
	12,12,13,13,17, 7, 7, 8, 7, 9, 9,11,10,12,12,13,
	13,19,11,10, 8, 8,10,10,11,11,12,12,13,13,19,11,
	11, 9, 7,11,10,11,11,12,12,13,12,19,19,19,10,10,
	10,10,11,12,12,12,13,14,18,19,19,11, 9,11, 9,13,
	12,12,12,13,13,19,20,19,13,15,11,11,12,12,13,13,
	14,13,18,19,20,15,13,12,10,13,10,13,13,13,14,20,
	20,20,20,20,13,14,12,12,13,12,13,13,20,20,20,20,
	20,13,12,12,12,14,12,14,13,
};

static float _vq_quantthresh__44c1_sm_p8_1[] = {
	-93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5, 25.5,
	42.5, 59.5, 76.5, 93.5,
};

static long _vq_quantmap__44c1_sm_p8_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p8_1 = {
	_vq_quantthresh__44c1_sm_p8_1,
	_vq_quantmap__44c1_sm_p8_1,
	13,
	13
};

static static_codebook _44c1_sm_p8_1 = {
	2, 169,
	_vq_lengthlist__44c1_sm_p8_1,
	1, -522616832, 1620115456, 4, 0,
	_vq_quantlist__44c1_sm_p8_1,
	NULL,
	&_vq_auxt__44c1_sm_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44c1_sm_p8_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44c1_sm_p8_2[] = {
	 2, 5, 5, 6, 6, 7, 6, 7, 7, 8, 8, 8, 8, 8, 8, 8,
	 8,10, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9,10, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9,10, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 7, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9,10,11,11, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9,10,10, 9,10,10,10,10, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 9, 9,10,10,11,10,10, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9,10, 9,10,10,10,11,11, 8, 8, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,11,11,11,11,11, 9, 9,
	 9, 9, 9, 9, 9, 9,10, 9,10, 9,11,11,11,11,11, 9,
	 8, 9, 9, 9, 9, 9, 9, 9,10,10, 9,11,11,10,11,11,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10, 9,11,11,11,11,
	11,11,11, 9, 9,10, 9, 9, 9, 9,10, 9,10,10,11,10,
	11,11,11,11, 9,10,10,10, 9, 9, 9, 9, 9, 9,10,11,
	11,11,11,11,11, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,11,
	11,10,11,11,11,11,10,10, 9, 9, 9, 9, 9, 9,10, 9,
	10,11,10,11,11,11,11,11,11, 9, 9,10, 9, 9, 9, 9,
	 9,
};

static float _vq_quantthresh__44c1_sm_p8_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44c1_sm_p8_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44c1_sm_p8_2 = {
	_vq_quantthresh__44c1_sm_p8_2,
	_vq_quantmap__44c1_sm_p8_2,
	17,
	17
};

static static_codebook _44c1_sm_p8_2 = {
	2, 289,
	_vq_lengthlist__44c1_sm_p8_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44c1_sm_p8_2,
	NULL,
	&_vq_auxt__44c1_sm_p8_2,
	NULL,
	0
};

static long _huff_lengthlist__44c1_sm_short[] = {
	 4, 7,13,14,14,15,16,18,18, 4, 2, 5, 8, 7, 9,12,
	15,15,10, 4, 5,10, 6, 8,11,15,17,12, 5, 7, 5, 6,
	 8,11,14,17,11, 5, 6, 6, 5, 6, 9,13,17,12, 6, 7,
	 6, 5, 6, 8,12,14,14, 7, 8, 6, 6, 7, 9,11,14,14,
	 8, 9, 6, 5, 6, 9,11,13,16,10,10, 7, 6, 7, 8,10,
	11,
};

static static_codebook _huff_book__44c1_sm_short = {
	2, 81,
	_huff_lengthlist__44c1_sm_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44cn1_s_long[] = {
	 4, 4, 7, 8, 7, 8,10,12,17, 3, 1, 6, 6, 7, 8,10,
	12,15, 7, 6, 9, 9, 9,11,12,14,17, 8, 6, 9, 6, 7,
	 9,11,13,17, 7, 6, 9, 7, 7, 8, 9,12,15, 8, 8,10,
	 8, 7, 7, 7,10,14, 9,10,12,10, 8, 8, 8,10,14,11,
	13,15,13,12,11,11,12,16,17,18,18,19,20,18,16,16,
	20,
};

static static_codebook _huff_book__44cn1_s_long = {
	2, 81,
	_huff_lengthlist__44cn1_s_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44cn1_s_p1_0[] = {
	 1, 4, 4, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0, 0,
	 0, 0, 0, 7, 9,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 7,10, 9, 0, 0,
	 0, 0, 0, 0, 8,10, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 8, 0, 0, 0, 0,
	 0, 0, 8,10,10, 0, 0, 0, 0, 0, 0, 8, 9,10, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,10,10, 0, 0, 0,
	 0, 0, 0, 9, 9,11, 0, 0, 0, 0, 0, 0,10,11,11, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,10,10, 0, 0,
	 0, 0, 0, 0, 9,11, 9, 0, 0, 0, 0, 0, 0,10,11,11,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 8,10,10, 0, 0,
	 0, 0, 0, 0, 8,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7,10,10, 0, 0, 0, 0, 0, 0,10,11,11, 0,
	 0, 0, 0, 0, 0, 9, 9,11, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7,10,10, 0, 0, 0, 0, 0, 0,10,11,11,
	 0, 0, 0, 0, 0, 0, 9,11, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44cn1_s_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44cn1_s_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p1_0 = {
	_vq_quantthresh__44cn1_s_p1_0,
	_vq_quantmap__44cn1_s_p1_0,
	3,
	3
};

static static_codebook _44cn1_s_p1_0 = {
	8, 6561,
	_vq_lengthlist__44cn1_s_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44cn1_s_p1_0,
	NULL,
	&_vq_auxt__44cn1_s_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44cn1_s_p2_0[] = {
	 1, 4, 4, 7, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 7, 7, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 5, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 7, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 6, 7, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44cn1_s_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44cn1_s_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p2_0 = {
	_vq_quantthresh__44cn1_s_p2_0,
	_vq_quantmap__44cn1_s_p2_0,
	5,
	5
};

static static_codebook _44cn1_s_p2_0 = {
	4, 625,
	_vq_lengthlist__44cn1_s_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44cn1_s_p2_0,
	NULL,
	&_vq_auxt__44cn1_s_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44cn1_s_p3_0[] = {
	 1, 2, 3, 7, 7, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0,
	 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 7, 7,
	 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
	 9, 8, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0,
	 0, 0,10,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44cn1_s_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44cn1_s_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p3_0 = {
	_vq_quantthresh__44cn1_s_p3_0,
	_vq_quantmap__44cn1_s_p3_0,
	9,
	9
};

static static_codebook _44cn1_s_p3_0 = {
	2, 81,
	_vq_lengthlist__44cn1_s_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44cn1_s_p3_0,
	NULL,
	&_vq_auxt__44cn1_s_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44cn1_s_p4_0[] = {
	 1, 3, 3, 6, 6, 6, 6, 8, 8, 0, 0, 0, 6, 6, 7, 7,
	 9, 9, 0, 0, 0, 6, 6, 7, 7, 9, 9, 0, 0, 0, 7, 7,
	 8, 8,10,10, 0, 0, 0, 7, 7, 8, 8,10,10, 0, 0, 0,
	 9, 9, 9, 9,10,10, 0, 0, 0, 9, 9, 9, 9,10,10, 0,
	 0, 0,10,10,10,10,11,11, 0, 0, 0, 0, 0,10,10,11,
	11,
};

static float _vq_quantthresh__44cn1_s_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44cn1_s_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p4_0 = {
	_vq_quantthresh__44cn1_s_p4_0,
	_vq_quantmap__44cn1_s_p4_0,
	9,
	9
};

static static_codebook _44cn1_s_p4_0 = {
	2, 81,
	_vq_lengthlist__44cn1_s_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44cn1_s_p4_0,
	NULL,
	&_vq_auxt__44cn1_s_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p5_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44cn1_s_p5_0[] = {
	 1, 4, 3, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,10,
	10, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,10,
	11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,
	10,11,11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,11,12, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,11,11, 0, 0, 0, 8, 8, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12, 0, 0, 0, 8, 8, 9, 9, 9, 9,10,
	10,10,11,11,11,12,12, 0, 0, 0, 9, 9,10, 9,10,10,
	10,10,11,11,11,11,12,12, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,12,12, 0, 0, 0, 0, 0, 9, 9,
	10,10,10,11,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0,
	10,10,11,10,11,11,11,12,13,12,13,13, 0, 0, 0, 0,
	 0, 0, 0,11,10,11,11,12,12,12,12,13,13, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,12,12,12,13,13,13,14, 0, 0,
	 0, 0, 0, 0, 0,11,11,12,12,12,12,13,13,13,14, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,13,13,13,13,13,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,13,12,13,13,14,
	14,
};

static float _vq_quantthresh__44cn1_s_p5_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44cn1_s_p5_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p5_0 = {
	_vq_quantthresh__44cn1_s_p5_0,
	_vq_quantmap__44cn1_s_p5_0,
	17,
	17
};

static static_codebook _44cn1_s_p5_0 = {
	2, 289,
	_vq_lengthlist__44cn1_s_p5_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44cn1_s_p5_0,
	NULL,
	&_vq_auxt__44cn1_s_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p6_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44cn1_s_p6_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 6, 6,10, 9, 9,11,
	 9, 9, 4, 6, 6,10, 9, 9,10, 9, 9, 7,10,10,11,11,
	11,12,11,11, 7, 9, 9,11,11,10,11,10,10, 7, 9, 9,
	11,10,11,11,10,10, 7,10,10,11,11,11,12,11,11, 7,
	 9, 9,11,10,10,11,10,10, 7, 9, 9,11,10,10,11,10,
	10,
};

static float _vq_quantthresh__44cn1_s_p6_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44cn1_s_p6_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p6_0 = {
	_vq_quantthresh__44cn1_s_p6_0,
	_vq_quantmap__44cn1_s_p6_0,
	3,
	3
};

static static_codebook _44cn1_s_p6_0 = {
	4, 81,
	_vq_lengthlist__44cn1_s_p6_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44cn1_s_p6_0,
	NULL,
	&_vq_auxt__44cn1_s_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p6_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44cn1_s_p6_1[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8,10,10,10, 7, 6,
	 8, 8, 8, 8, 8, 8,10,10,10, 7, 6, 7, 7, 8, 8, 8,
	 8,10,10,10, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10, 7,
	 7, 8, 8, 8, 8, 8, 8,10,10,10, 8, 8, 8, 8, 9, 9,
	 9, 9,10,10,10, 8, 8, 8, 8, 9, 9, 9, 9,10,10,10,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10, 9, 9, 9,
	 9, 9, 9,10,10,10,10,10, 9, 9, 9, 9, 9, 9,10,10,
	10,10,10, 9, 9, 9, 9, 9, 9,
};

static float _vq_quantthresh__44cn1_s_p6_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44cn1_s_p6_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p6_1 = {
	_vq_quantthresh__44cn1_s_p6_1,
	_vq_quantmap__44cn1_s_p6_1,
	11,
	11
};

static static_codebook _44cn1_s_p6_1 = {
	2, 121,
	_vq_lengthlist__44cn1_s_p6_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44cn1_s_p6_1,
	NULL,
	&_vq_auxt__44cn1_s_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44cn1_s_p7_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 6, 5, 5,
	 7, 7, 8, 8, 8, 8, 9, 9,11,11, 7, 5, 5, 7, 7, 8,
	 8, 8, 8, 9,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9, 9,10,10,10,11,11,11,12, 0,13,
	13, 9, 9, 9, 9,10,10,11,11,11,12, 0, 0, 0,10,10,
	10,10,11,11,12,12,12,13, 0, 0, 0,10,10,10,10,11,
	11,12,12,13,12, 0, 0, 0,14,14,11,10,11,12,12,13,
	13,14, 0, 0, 0,15,15,11,11,12,11,12,12,14,13, 0,
	 0, 0, 0, 0,12,12,12,12,13,13,14,14, 0, 0, 0, 0,
	 0,13,13,12,12,13,13,13,14,
};

static float _vq_quantthresh__44cn1_s_p7_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44cn1_s_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p7_0 = {
	_vq_quantthresh__44cn1_s_p7_0,
	_vq_quantmap__44cn1_s_p7_0,
	13,
	13
};

static static_codebook _44cn1_s_p7_0 = {
	2, 169,
	_vq_lengthlist__44cn1_s_p7_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44cn1_s_p7_0,
	NULL,
	&_vq_auxt__44cn1_s_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p7_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44cn1_s_p7_1[] = {
	 2, 3, 3, 5, 5, 6, 6, 6, 5, 5, 6, 6, 6, 5, 5, 6,
	 6, 6, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44cn1_s_p7_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44cn1_s_p7_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p7_1 = {
	_vq_quantthresh__44cn1_s_p7_1,
	_vq_quantmap__44cn1_s_p7_1,
	5,
	5
};

static static_codebook _44cn1_s_p7_1 = {
	2, 25,
	_vq_lengthlist__44cn1_s_p7_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44cn1_s_p7_1,
	NULL,
	&_vq_auxt__44cn1_s_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p8_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44cn1_s_p8_0[] = {
	 1, 7, 7,11,11, 8,11,11,11,11, 4,11, 3,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,10,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,10,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11, 7,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,10,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,10,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11, 8,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,
};

static float _vq_quantthresh__44cn1_s_p8_0[] = {
	-331.5, -110.5, 110.5, 331.5,
};

static long _vq_quantmap__44cn1_s_p8_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p8_0 = {
	_vq_quantthresh__44cn1_s_p8_0,
	_vq_quantmap__44cn1_s_p8_0,
	5,
	5
};

static static_codebook _44cn1_s_p8_0 = {
	4, 625,
	_vq_lengthlist__44cn1_s_p8_0,
	1, -518283264, 1627103232, 3, 0,
	_vq_quantlist__44cn1_s_p8_0,
	NULL,
	&_vq_auxt__44cn1_s_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p8_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44cn1_s_p8_1[] = {
	 1, 4, 4, 6, 6, 8, 8, 9,10,10,11,11,11, 6, 5, 5,
	 7, 7, 8, 8, 9,10, 9,11,11,12, 5, 5, 5, 7, 7, 8,
	 9,10,10,12,12,14,13,15, 7, 7, 8, 8, 9,10,11,11,
	10,12,10,11,15, 7, 8, 8, 8, 9, 9,11,11,13,12,12,
	13,15,10,10, 8, 8,10,10,12,12,11,14,10,10,15,11,
	11, 8, 8,10,10,12,13,13,14,15,13,15,15,15,10,10,
	10,10,12,12,13,12,13,10,15,15,15,10,10,11,10,13,
	11,13,13,15,13,15,15,15,13,13,10,11,11,11,12,10,
	14,11,15,15,14,14,13,10,10,12,11,13,13,14,14,15,
	15,15,15,15,11,11,11,11,12,11,15,12,15,15,15,15,
	15,12,12,11,11,14,12,13,14,
};

static float _vq_quantthresh__44cn1_s_p8_1[] = {
	-93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5, 25.5,
	42.5, 59.5, 76.5, 93.5,
};

static long _vq_quantmap__44cn1_s_p8_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p8_1 = {
	_vq_quantthresh__44cn1_s_p8_1,
	_vq_quantmap__44cn1_s_p8_1,
	13,
	13
};

static static_codebook _44cn1_s_p8_1 = {
	2, 169,
	_vq_lengthlist__44cn1_s_p8_1,
	1, -522616832, 1620115456, 4, 0,
	_vq_quantlist__44cn1_s_p8_1,
	NULL,
	&_vq_auxt__44cn1_s_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44cn1_s_p8_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44cn1_s_p8_2[] = {
	 3, 4, 3, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9,
	 9,10,11,11, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9,10,10,10, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9,10,10,10, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9,
	 9, 9,10, 9,10,11,10, 7, 6, 7, 7, 8, 8, 9, 9, 9,
	 9, 9, 9, 9,10,10,10,11, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10,10, 7, 7, 8, 8, 8, 8, 9,
	 9, 9, 9, 9, 9, 9,10,11,11,11, 8, 8, 8, 8, 8, 8,
	 9, 9, 9, 9, 9, 9, 9, 9,11,10,10,11,11, 8, 8, 8,
	 9, 9, 9, 9, 9, 9,10, 9,10,10,10,10,11,11, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,11,10,11,11, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,11,10,11,11,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,10,10,11,
	11,11,11, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,11,11,
	10,11,11,11, 9,10,10, 9, 9, 9, 9, 9, 9, 9,10,11,
	11,11,11,11,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,
	11,11,11,11,11,11,10,10, 9, 9, 9, 9, 9, 9, 9, 9,
	11,11,11,10,11,11,11,11,11, 9, 9, 9,10, 9, 9, 9,
	 9,
};

static float _vq_quantthresh__44cn1_s_p8_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44cn1_s_p8_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44cn1_s_p8_2 = {
	_vq_quantthresh__44cn1_s_p8_2,
	_vq_quantmap__44cn1_s_p8_2,
	17,
	17
};

static static_codebook _44cn1_s_p8_2 = {
	2, 289,
	_vq_lengthlist__44cn1_s_p8_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44cn1_s_p8_2,
	NULL,
	&_vq_auxt__44cn1_s_p8_2,
	NULL,
	0
};

static long _huff_lengthlist__44cn1_s_short[] = {
	10, 9,12,15,12,13,16,14,16, 7, 1, 5,14, 7,10,13,
	16,16, 9, 4, 6,16, 8,11,16,16,16,14, 4, 7,16, 9,
	12,14,16,16,10, 5, 7,14, 9,12,14,15,15,13, 8, 9,
	14,10,12,13,14,15,13, 9, 9, 7, 6, 8,11,12,12,14,
	 8, 8, 5, 4, 5, 8,11,12,16,10,10, 6, 5, 6, 8, 9,
	10,
};

static static_codebook _huff_book__44cn1_s_short = {
	2, 81,
	_huff_lengthlist__44cn1_s_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44cn1_sm_long[] = {
	 3, 3, 8, 8, 8, 8,10,12,14, 3, 2, 6, 7, 7, 8,10,
	12,16, 7, 6, 7, 9, 8,10,12,14,16, 8, 6, 8, 4, 5,
	 7, 9,11,13, 7, 6, 8, 5, 6, 7, 9,11,14, 8, 8,10,
	 7, 7, 6, 8,10,13, 9,11,12, 9, 9, 7, 8,10,12,10,
	13,15,11,11,10, 9,10,13,13,16,17,14,15,14,13,14,
	17,
};

static static_codebook _huff_book__44cn1_sm_long = {
	2, 81,
	_huff_lengthlist__44cn1_sm_long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44cn1_sm_p1_0[] = {
	 1, 4, 5, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0,
	 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0, 0,
	 0, 0, 0, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 7, 9, 8, 0, 0,
	 0, 0, 0, 0, 8, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 8, 0, 0, 0, 0,
	 0, 0, 8,10, 9, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,10, 9, 0, 0, 0,
	 0, 0, 0, 9, 9,10, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 0, 0,
	 0, 0, 0, 0, 8,10, 9, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 5, 8, 8, 0, 0, 0, 0, 0, 0, 8, 9, 9, 0, 0,
	 0, 0, 0, 0, 8, 9,10, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 7, 9, 9, 0, 0, 0, 0, 0, 0, 9,10,10, 0,
	 0, 0, 0, 0, 0, 8, 9,10, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 9,10, 0, 0, 0, 0, 0, 0, 9,10,10,
	 0, 0, 0, 0, 0, 0, 9,10, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44cn1_sm_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44cn1_sm_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p1_0 = {
	_vq_quantthresh__44cn1_sm_p1_0,
	_vq_quantmap__44cn1_sm_p1_0,
	3,
	3
};

static static_codebook _44cn1_sm_p1_0 = {
	8, 6561,
	_vq_lengthlist__44cn1_sm_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44cn1_sm_p1_0,
	NULL,
	&_vq_auxt__44cn1_sm_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44cn1_sm_p2_0[] = {
	 1, 4, 4, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 7, 7, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 4, 5, 5, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 7, 9, 9,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 7, 7, 7, 9, 9, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44cn1_sm_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44cn1_sm_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p2_0 = {
	_vq_quantthresh__44cn1_sm_p2_0,
	_vq_quantmap__44cn1_sm_p2_0,
	5,
	5
};

static static_codebook _44cn1_sm_p2_0 = {
	4, 625,
	_vq_lengthlist__44cn1_sm_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44cn1_sm_p2_0,
	NULL,
	&_vq_auxt__44cn1_sm_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44cn1_sm_p3_0[] = {
	 1, 3, 4, 7, 7, 0, 0, 0, 0, 0, 4, 4, 7, 7, 0, 0,
	 0, 0, 0, 4, 5, 7, 7, 0, 0, 0, 0, 0, 6, 7, 8, 8,
	 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0,
	 9, 9, 0, 0, 0, 0, 0, 0, 0,10, 9, 0, 0, 0, 0, 0,
	 0, 0,11,11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	 0,
};

static float _vq_quantthresh__44cn1_sm_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44cn1_sm_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p3_0 = {
	_vq_quantthresh__44cn1_sm_p3_0,
	_vq_quantmap__44cn1_sm_p3_0,
	9,
	9
};

static static_codebook _44cn1_sm_p3_0 = {
	2, 81,
	_vq_lengthlist__44cn1_sm_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44cn1_sm_p3_0,
	NULL,
	&_vq_auxt__44cn1_sm_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p4_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44cn1_sm_p4_0[] = {
	 1, 4, 3, 6, 6, 7, 7, 9, 9, 0, 5, 5, 7, 7, 8, 7,
	 9, 9, 0, 5, 5, 7, 7, 8, 8, 9, 9, 0, 7, 7, 8, 8,
	 8, 8,10,10, 0, 0, 0, 8, 8, 8, 8,10,10, 0, 0, 0,
	 9, 9, 9, 9,10,10, 0, 0, 0, 9, 9, 9, 9,10,10, 0,
	 0, 0,10,10,10,10,11,11, 0, 0, 0, 0, 0,10,10,11,
	11,
};

static float _vq_quantthresh__44cn1_sm_p4_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44cn1_sm_p4_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p4_0 = {
	_vq_quantthresh__44cn1_sm_p4_0,
	_vq_quantmap__44cn1_sm_p4_0,
	9,
	9
};

static static_codebook _44cn1_sm_p4_0 = {
	2, 81,
	_vq_lengthlist__44cn1_sm_p4_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44cn1_sm_p4_0,
	NULL,
	&_vq_auxt__44cn1_sm_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p5_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44cn1_sm_p5_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 9, 9, 8, 8, 9, 9,10,10,11,
	11, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,11,
	12,12, 0, 6, 5, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,
	11,12,12, 0, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,12,12, 0, 0, 0, 7, 7, 8, 8, 9, 9,10,10,11,
	11,11,11,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,10,
	11,11,12,12,12,12, 0, 0, 0, 8, 8, 9, 9,10,10,10,
	10,11,11,12,12,12,12, 0, 0, 0, 9, 9, 9, 9,10,10,
	10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,10,
	10,10,10,11,11,12,12,13,13, 0, 0, 0, 0, 0, 9, 9,
	10,10,11,11,12,12,13,13,13,13, 0, 0, 0, 0, 0, 9,
	 9,10,10,11,11,12,12,12,13,13,13, 0, 0, 0, 0, 0,
	10,10,11,11,11,11,12,12,13,13,14,14, 0, 0, 0, 0,
	 0, 0, 0,11,11,11,11,12,12,13,13,14,14, 0, 0, 0,
	 0, 0, 0, 0,11,11,12,12,13,13,13,13,14,14, 0, 0,
	 0, 0, 0, 0, 0,11,11,12,12,13,13,13,13,14,14, 0,
	 0, 0, 0, 0, 0, 0,12,12,12,13,13,13,14,14,14,14,
	 0, 0, 0, 0, 0, 0, 0, 0, 0,12,12,13,13,14,14,14,
	14,
};

static float _vq_quantthresh__44cn1_sm_p5_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44cn1_sm_p5_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p5_0 = {
	_vq_quantthresh__44cn1_sm_p5_0,
	_vq_quantmap__44cn1_sm_p5_0,
	17,
	17
};

static static_codebook _44cn1_sm_p5_0 = {
	2, 289,
	_vq_lengthlist__44cn1_sm_p5_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44cn1_sm_p5_0,
	NULL,
	&_vq_auxt__44cn1_sm_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p6_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44cn1_sm_p6_0[] = {
	 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 6,10, 9, 9,11,
	 9, 9, 4, 6, 7,10, 9, 9,11, 9, 9, 7,10,10,10,11,
	11,11,11,10, 6, 9, 9,11,10,10,11,10,10, 6, 9, 9,
	11,10,11,11,10,10, 7,11,11,11,11,11,12,11,11, 7,
	 9, 9,11,10,10,12,10,10, 7, 9, 9,11,10,10,11,10,
	10,
};

static float _vq_quantthresh__44cn1_sm_p6_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44cn1_sm_p6_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p6_0 = {
	_vq_quantthresh__44cn1_sm_p6_0,
	_vq_quantmap__44cn1_sm_p6_0,
	3,
	3
};

static static_codebook _44cn1_sm_p6_0 = {
	4, 81,
	_vq_lengthlist__44cn1_sm_p6_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44cn1_sm_p6_0,
	NULL,
	&_vq_auxt__44cn1_sm_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p6_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44cn1_sm_p6_1[] = {
	 2, 4, 4, 5, 5, 7, 7, 7, 7, 8, 8,10, 5, 5, 6, 6,
	 7, 7, 8, 8, 8, 8,10, 5, 5, 6, 6, 7, 7, 8, 8, 8,
	 8,10, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8,10,10,10, 7,
	 7, 7, 7, 8, 8, 8, 8,10,10,10, 8, 8, 8, 8, 8, 8,
	 8, 8,10,10,10, 8, 8, 8, 8, 8, 8, 8, 8,10,10,10,
	 8, 8, 8, 8, 8, 8, 9, 9,10,10,10,10,10, 8, 8, 8,
	 8, 9, 9,10,10,10,10,10, 9, 9, 9, 9, 8, 9,10,10,
	10,10,10, 8, 9, 8, 8, 9, 8,
};

static float _vq_quantthresh__44cn1_sm_p6_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44cn1_sm_p6_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p6_1 = {
	_vq_quantthresh__44cn1_sm_p6_1,
	_vq_quantmap__44cn1_sm_p6_1,
	11,
	11
};

static static_codebook _44cn1_sm_p6_1 = {
	2, 121,
	_vq_lengthlist__44cn1_sm_p6_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44cn1_sm_p6_1,
	NULL,
	&_vq_auxt__44cn1_sm_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44cn1_sm_p7_0[] = {
	 1, 4, 4, 6, 6, 7, 7, 7, 7, 9, 9,10,10, 7, 5, 5,
	 7, 7, 8, 8, 8, 8,10, 9,11,10, 7, 5, 5, 7, 7, 8,
	 8, 8, 8, 9,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,11, 0, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,
	11, 0,12,12, 9, 9, 9,10,10,10,11,11,12,12, 0,13,
	13, 9, 9, 9, 9,10,10,11,11,12,12, 0, 0, 0,10,10,
	10,10,11,11,12,12,12,13, 0, 0, 0,10,10,10,10,11,
	11,12,12,12,12, 0, 0, 0,14,14,11,11,11,11,12,13,
	13,13, 0, 0, 0,14,14,11,10,11,11,12,12,13,13, 0,
	 0, 0, 0, 0,12,12,12,12,13,13,13,14, 0, 0, 0, 0,
	 0,13,12,12,12,13,13,13,14,
};

static float _vq_quantthresh__44cn1_sm_p7_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44cn1_sm_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p7_0 = {
	_vq_quantthresh__44cn1_sm_p7_0,
	_vq_quantmap__44cn1_sm_p7_0,
	13,
	13
};

static static_codebook _44cn1_sm_p7_0 = {
	2, 169,
	_vq_lengthlist__44cn1_sm_p7_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44cn1_sm_p7_0,
	NULL,
	&_vq_auxt__44cn1_sm_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p7_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44cn1_sm_p7_1[] = {
	 2, 4, 4, 4, 5, 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6,
	 5, 5, 5, 5, 6, 6, 6, 5, 5,
};

static float _vq_quantthresh__44cn1_sm_p7_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44cn1_sm_p7_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p7_1 = {
	_vq_quantthresh__44cn1_sm_p7_1,
	_vq_quantmap__44cn1_sm_p7_1,
	5,
	5
};

static static_codebook _44cn1_sm_p7_1 = {
	2, 25,
	_vq_lengthlist__44cn1_sm_p7_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44cn1_sm_p7_1,
	NULL,
	&_vq_auxt__44cn1_sm_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p8_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44cn1_sm_p8_0[] = {
	 1, 4, 4,12,11,13,13,14,14, 4, 7, 7,11,13,14,14,
	14,14, 3, 8, 3,14,14,14,14,14,14,14,10,12,14,14,
	14,14,14,14,14,14, 5,14, 8,14,14,14,14,14,12,14,
	13,14,14,14,14,14,14,14,13,14,10,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,
};

static float _vq_quantthresh__44cn1_sm_p8_0[] = {
	-773.5, -552.5, -331.5, -110.5, 110.5, 331.5, 552.5, 773.5,
};

static long _vq_quantmap__44cn1_sm_p8_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p8_0 = {
	_vq_quantthresh__44cn1_sm_p8_0,
	_vq_quantmap__44cn1_sm_p8_0,
	9,
	9
};

static static_codebook _44cn1_sm_p8_0 = {
	2, 81,
	_vq_lengthlist__44cn1_sm_p8_0,
	1, -516186112, 1627103232, 4, 0,
	_vq_quantlist__44cn1_sm_p8_0,
	NULL,
	&_vq_auxt__44cn1_sm_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p8_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44cn1_sm_p8_1[] = {
	 1, 4, 4, 6, 6, 8, 8, 9, 9,10,11,11,11, 6, 5, 5,
	 7, 7, 8, 8,10,10,10,11,11,11, 6, 5, 5, 7, 7, 8,
	 8,10,10,11,12,12,12,14, 7, 7, 7, 8, 9, 9,11,11,
	11,12,11,12,17, 7, 7, 8, 7, 9, 9,11,11,12,12,12,
	12,14,11,11, 8, 8,10,10,11,12,12,13,11,12,14,11,
	11, 8, 8,10,10,11,12,12,13,13,12,14,15,14,10,10,
	10,10,11,12,12,12,12,11,14,13,16,10,10,10, 9,12,
	11,12,12,13,14,14,15,14,14,13,10,10,11,11,12,11,
	13,11,14,12,15,13,14,11,10,12,10,12,12,13,13,13,
	13,14,15,15,12,12,11,11,12,11,13,12,14,14,14,14,
	17,12,12,11,10,13,11,13,13,
};

static float _vq_quantthresh__44cn1_sm_p8_1[] = {
	-93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5, 25.5,
	42.5, 59.5, 76.5, 93.5,
};

static long _vq_quantmap__44cn1_sm_p8_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p8_1 = {
	_vq_quantthresh__44cn1_sm_p8_1,
	_vq_quantmap__44cn1_sm_p8_1,
	13,
	13
};

static static_codebook _44cn1_sm_p8_1 = {
	2, 169,
	_vq_lengthlist__44cn1_sm_p8_1,
	1, -522616832, 1620115456, 4, 0,
	_vq_quantlist__44cn1_sm_p8_1,
	NULL,
	&_vq_auxt__44cn1_sm_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44cn1_sm_p8_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44cn1_sm_p8_2[] = {
	 3, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9,10, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9,10, 6, 6, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9,10, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10,10, 7, 7, 7, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 9, 9,10,10,10, 8, 8, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10,10, 8, 8, 8, 8, 8, 8, 9,
	 9, 9, 9, 9, 9, 9, 9,11,10,11, 8, 8, 8, 8, 8, 8,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,11,11, 8, 8, 8,
	 8, 9, 9, 9, 9, 9, 9, 9, 9,11,10,11,11,11, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,11,10,11,11, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,11,11,10,11,11,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,10,11,11,
	11,11,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,11,11,
	11,11,11,11, 9,10,10,10, 9, 9, 9, 9, 9, 9,11,10,
	11,11,11,11,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,11,
	11,11,11,11,11,11,10,10, 9, 9, 9, 9, 9, 9, 9, 9,
	10,11,11,11,11,11,11,11,11, 9, 9, 9, 9, 9, 9, 9,
	 9,
};

static float _vq_quantthresh__44cn1_sm_p8_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44cn1_sm_p8_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44cn1_sm_p8_2 = {
	_vq_quantthresh__44cn1_sm_p8_2,
	_vq_quantmap__44cn1_sm_p8_2,
	17,
	17
};

static static_codebook _44cn1_sm_p8_2 = {
	2, 289,
	_vq_lengthlist__44cn1_sm_p8_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44cn1_sm_p8_2,
	NULL,
	&_vq_auxt__44cn1_sm_p8_2,
	NULL,
	0
};

static long _huff_lengthlist__44cn1_sm_short[] = {
	 5, 6,12,14,12,14,16,17,18, 4, 2, 5,11, 7,10,12,
	14,15, 9, 4, 5,11, 7,10,13,15,18,15, 6, 7, 5, 6,
	 8,11,13,16,11, 5, 6, 5, 5, 6, 9,13,15,12, 5, 7,
	 6, 5, 6, 9,12,14,12, 6, 7, 8, 6, 7, 9,12,13,14,
	 8, 8, 7, 5, 5, 8,10,12,16, 9, 9, 8, 6, 6, 7, 9,
	 9,
};

static static_codebook _huff_book__44cn1_sm_short = {
	2, 81,
	_huff_lengthlist__44cn1_sm_short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};
/*** End of inlined file: res_books_stereo.h ***/

static vorbis_info_residue0 _residue_44_low={
  0,-1, -1, 9,-1,
  {0},
  {-1},
  {  .5,  1.5,  2.5,  2.5,  4.5,  8.5,  16.5, 32.5},
  {  .5,   .5,   .5,  999., 4.5,  8.5,  16.5, 32.5},
};

static vorbis_info_residue0 _residue_44_mid={
  0,-1, -1, 10,-1,
  {0},
  {-1},
  {  .5,  1.5,  1.5,  2.5,  2.5,  4.5,  8.5,  16.5, 32.5},
  {  .5,   .5, 999.,   .5,  999., 4.5,  8.5,  16.5, 32.5},
};

static vorbis_info_residue0 _residue_44_high={
  0,-1, -1, 10,-1,
  {0},
  {-1},
  {  .5,  1.5,  2.5,  4.5,  8.5, 16.5, 32.5, 71.5,157.5},
  {  .5,  1.5,  2.5,  3.5,  4.5,  8.5, 16.5, 71.5,157.5},
};

static static_bookblock _resbook_44s_n1={
  {
	{0},{0,0,&_44cn1_s_p1_0},{0,0,&_44cn1_s_p2_0},
	{0,0,&_44cn1_s_p3_0},{0,0,&_44cn1_s_p4_0},{0,0,&_44cn1_s_p5_0},
	{&_44cn1_s_p6_0,&_44cn1_s_p6_1},{&_44cn1_s_p7_0,&_44cn1_s_p7_1},
	{&_44cn1_s_p8_0,&_44cn1_s_p8_1,&_44cn1_s_p8_2}
   }
};
static static_bookblock _resbook_44sm_n1={
  {
	{0},{0,0,&_44cn1_sm_p1_0},{0,0,&_44cn1_sm_p2_0},
	{0,0,&_44cn1_sm_p3_0},{0,0,&_44cn1_sm_p4_0},{0,0,&_44cn1_sm_p5_0},
	{&_44cn1_sm_p6_0,&_44cn1_sm_p6_1},{&_44cn1_sm_p7_0,&_44cn1_sm_p7_1},
	{&_44cn1_sm_p8_0,&_44cn1_sm_p8_1,&_44cn1_sm_p8_2}
   }
};

static static_bookblock _resbook_44s_0={
  {
	{0},{0,0,&_44c0_s_p1_0},{0,0,&_44c0_s_p2_0},
	{0,0,&_44c0_s_p3_0},{0,0,&_44c0_s_p4_0},{0,0,&_44c0_s_p5_0},
	{&_44c0_s_p6_0,&_44c0_s_p6_1},{&_44c0_s_p7_0,&_44c0_s_p7_1},
	{&_44c0_s_p8_0,&_44c0_s_p8_1,&_44c0_s_p8_2}
   }
};
static static_bookblock _resbook_44sm_0={
  {
	{0},{0,0,&_44c0_sm_p1_0},{0,0,&_44c0_sm_p2_0},
	{0,0,&_44c0_sm_p3_0},{0,0,&_44c0_sm_p4_0},{0,0,&_44c0_sm_p5_0},
	{&_44c0_sm_p6_0,&_44c0_sm_p6_1},{&_44c0_sm_p7_0,&_44c0_sm_p7_1},
	{&_44c0_sm_p8_0,&_44c0_sm_p8_1,&_44c0_sm_p8_2}
   }
};

static static_bookblock _resbook_44s_1={
  {
	{0},{0,0,&_44c1_s_p1_0},{0,0,&_44c1_s_p2_0},
	{0,0,&_44c1_s_p3_0},{0,0,&_44c1_s_p4_0},{0,0,&_44c1_s_p5_0},
	{&_44c1_s_p6_0,&_44c1_s_p6_1},{&_44c1_s_p7_0,&_44c1_s_p7_1},
	{&_44c1_s_p8_0,&_44c1_s_p8_1,&_44c1_s_p8_2}
   }
};
static static_bookblock _resbook_44sm_1={
  {
	{0},{0,0,&_44c1_sm_p1_0},{0,0,&_44c1_sm_p2_0},
	{0,0,&_44c1_sm_p3_0},{0,0,&_44c1_sm_p4_0},{0,0,&_44c1_sm_p5_0},
	{&_44c1_sm_p6_0,&_44c1_sm_p6_1},{&_44c1_sm_p7_0,&_44c1_sm_p7_1},
	{&_44c1_sm_p8_0,&_44c1_sm_p8_1,&_44c1_sm_p8_2}
   }
};

static static_bookblock _resbook_44s_2={
  {
	{0},{0,0,&_44c2_s_p1_0},{0,0,&_44c2_s_p2_0},{0,0,&_44c2_s_p3_0},
	{0,0,&_44c2_s_p4_0},{0,0,&_44c2_s_p5_0},{0,0,&_44c2_s_p6_0},
	{&_44c2_s_p7_0,&_44c2_s_p7_1},{&_44c2_s_p8_0,&_44c2_s_p8_1},
	{&_44c2_s_p9_0,&_44c2_s_p9_1,&_44c2_s_p9_2}
   }
};
static static_bookblock _resbook_44s_3={
  {
	{0},{0,0,&_44c3_s_p1_0},{0,0,&_44c3_s_p2_0},{0,0,&_44c3_s_p3_0},
	{0,0,&_44c3_s_p4_0},{0,0,&_44c3_s_p5_0},{0,0,&_44c3_s_p6_0},
	{&_44c3_s_p7_0,&_44c3_s_p7_1},{&_44c3_s_p8_0,&_44c3_s_p8_1},
	{&_44c3_s_p9_0,&_44c3_s_p9_1,&_44c3_s_p9_2}
   }
};
static static_bookblock _resbook_44s_4={
  {
	{0},{0,0,&_44c4_s_p1_0},{0,0,&_44c4_s_p2_0},{0,0,&_44c4_s_p3_0},
	{0,0,&_44c4_s_p4_0},{0,0,&_44c4_s_p5_0},{0,0,&_44c4_s_p6_0},
	{&_44c4_s_p7_0,&_44c4_s_p7_1},{&_44c4_s_p8_0,&_44c4_s_p8_1},
	{&_44c4_s_p9_0,&_44c4_s_p9_1,&_44c4_s_p9_2}
   }
};
static static_bookblock _resbook_44s_5={
  {
	{0},{0,0,&_44c5_s_p1_0},{0,0,&_44c5_s_p2_0},{0,0,&_44c5_s_p3_0},
	{0,0,&_44c5_s_p4_0},{0,0,&_44c5_s_p5_0},{0,0,&_44c5_s_p6_0},
	{&_44c5_s_p7_0,&_44c5_s_p7_1},{&_44c5_s_p8_0,&_44c5_s_p8_1},
	{&_44c5_s_p9_0,&_44c5_s_p9_1,&_44c5_s_p9_2}
   }
};
static static_bookblock _resbook_44s_6={
  {
	{0},{0,0,&_44c6_s_p1_0},{0,0,&_44c6_s_p2_0},{0,0,&_44c6_s_p3_0},
	{0,0,&_44c6_s_p4_0},
	{&_44c6_s_p5_0,&_44c6_s_p5_1},
	{&_44c6_s_p6_0,&_44c6_s_p6_1},
	{&_44c6_s_p7_0,&_44c6_s_p7_1},
	{&_44c6_s_p8_0,&_44c6_s_p8_1},
	{&_44c6_s_p9_0,&_44c6_s_p9_1,&_44c6_s_p9_2}
   }
};
static static_bookblock _resbook_44s_7={
  {
	{0},{0,0,&_44c7_s_p1_0},{0,0,&_44c7_s_p2_0},{0,0,&_44c7_s_p3_0},
	{0,0,&_44c7_s_p4_0},
	{&_44c7_s_p5_0,&_44c7_s_p5_1},
	{&_44c7_s_p6_0,&_44c7_s_p6_1},
	{&_44c7_s_p7_0,&_44c7_s_p7_1},
	{&_44c7_s_p8_0,&_44c7_s_p8_1},
	{&_44c7_s_p9_0,&_44c7_s_p9_1,&_44c7_s_p9_2}
   }
};
static static_bookblock _resbook_44s_8={
  {
	{0},{0,0,&_44c8_s_p1_0},{0,0,&_44c8_s_p2_0},{0,0,&_44c8_s_p3_0},
	{0,0,&_44c8_s_p4_0},
	{&_44c8_s_p5_0,&_44c8_s_p5_1},
	{&_44c8_s_p6_0,&_44c8_s_p6_1},
	{&_44c8_s_p7_0,&_44c8_s_p7_1},
	{&_44c8_s_p8_0,&_44c8_s_p8_1},
	{&_44c8_s_p9_0,&_44c8_s_p9_1,&_44c8_s_p9_2}
   }
};
static static_bookblock _resbook_44s_9={
  {
	{0},{0,0,&_44c9_s_p1_0},{0,0,&_44c9_s_p2_0},{0,0,&_44c9_s_p3_0},
	{0,0,&_44c9_s_p4_0},
	{&_44c9_s_p5_0,&_44c9_s_p5_1},
	{&_44c9_s_p6_0,&_44c9_s_p6_1},
	{&_44c9_s_p7_0,&_44c9_s_p7_1},
	{&_44c9_s_p8_0,&_44c9_s_p8_1},
	{&_44c9_s_p9_0,&_44c9_s_p9_1,&_44c9_s_p9_2}
   }
};

static vorbis_residue_template _res_44s_n1[]={
  {2,0,  &_residue_44_low,
   &_huff_book__44cn1_s_short,&_huff_book__44cn1_sm_short,
   &_resbook_44s_n1,&_resbook_44sm_n1},

  {2,0,  &_residue_44_low,
   &_huff_book__44cn1_s_long,&_huff_book__44cn1_sm_long,
   &_resbook_44s_n1,&_resbook_44sm_n1}
};
static vorbis_residue_template _res_44s_0[]={
  {2,0,  &_residue_44_low,
   &_huff_book__44c0_s_short,&_huff_book__44c0_sm_short,
   &_resbook_44s_0,&_resbook_44sm_0},

  {2,0,  &_residue_44_low,
   &_huff_book__44c0_s_long,&_huff_book__44c0_sm_long,
   &_resbook_44s_0,&_resbook_44sm_0}
};
static vorbis_residue_template _res_44s_1[]={
  {2,0,  &_residue_44_low,
   &_huff_book__44c1_s_short,&_huff_book__44c1_sm_short,
   &_resbook_44s_1,&_resbook_44sm_1},

  {2,0,  &_residue_44_low,
   &_huff_book__44c1_s_long,&_huff_book__44c1_sm_long,
   &_resbook_44s_1,&_resbook_44sm_1}
};

static vorbis_residue_template _res_44s_2[]={
  {2,0,  &_residue_44_mid,
   &_huff_book__44c2_s_short,&_huff_book__44c2_s_short,
   &_resbook_44s_2,&_resbook_44s_2},

  {2,0,  &_residue_44_mid,
   &_huff_book__44c2_s_long,&_huff_book__44c2_s_long,
   &_resbook_44s_2,&_resbook_44s_2}
};
static vorbis_residue_template _res_44s_3[]={
  {2,0,  &_residue_44_mid,
   &_huff_book__44c3_s_short,&_huff_book__44c3_s_short,
   &_resbook_44s_3,&_resbook_44s_3},

  {2,0,  &_residue_44_mid,
   &_huff_book__44c3_s_long,&_huff_book__44c3_s_long,
   &_resbook_44s_3,&_resbook_44s_3}
};
static vorbis_residue_template _res_44s_4[]={
  {2,0,  &_residue_44_mid,
   &_huff_book__44c4_s_short,&_huff_book__44c4_s_short,
   &_resbook_44s_4,&_resbook_44s_4},

  {2,0,  &_residue_44_mid,
   &_huff_book__44c4_s_long,&_huff_book__44c4_s_long,
   &_resbook_44s_4,&_resbook_44s_4}
};
static vorbis_residue_template _res_44s_5[]={
  {2,0,  &_residue_44_mid,
   &_huff_book__44c5_s_short,&_huff_book__44c5_s_short,
   &_resbook_44s_5,&_resbook_44s_5},

  {2,0,  &_residue_44_mid,
   &_huff_book__44c5_s_long,&_huff_book__44c5_s_long,
   &_resbook_44s_5,&_resbook_44s_5}
};
static vorbis_residue_template _res_44s_6[]={
  {2,0,  &_residue_44_high,
   &_huff_book__44c6_s_short,&_huff_book__44c6_s_short,
   &_resbook_44s_6,&_resbook_44s_6},

  {2,0,  &_residue_44_high,
   &_huff_book__44c6_s_long,&_huff_book__44c6_s_long,
   &_resbook_44s_6,&_resbook_44s_6}
};
static vorbis_residue_template _res_44s_7[]={
  {2,0,  &_residue_44_high,
   &_huff_book__44c7_s_short,&_huff_book__44c7_s_short,
   &_resbook_44s_7,&_resbook_44s_7},

  {2,0,  &_residue_44_high,
   &_huff_book__44c7_s_long,&_huff_book__44c7_s_long,
   &_resbook_44s_7,&_resbook_44s_7}
};
static vorbis_residue_template _res_44s_8[]={
  {2,0,  &_residue_44_high,
   &_huff_book__44c8_s_short,&_huff_book__44c8_s_short,
   &_resbook_44s_8,&_resbook_44s_8},

  {2,0,  &_residue_44_high,
   &_huff_book__44c8_s_long,&_huff_book__44c8_s_long,
   &_resbook_44s_8,&_resbook_44s_8}
};
static vorbis_residue_template _res_44s_9[]={
  {2,0,  &_residue_44_high,
   &_huff_book__44c9_s_short,&_huff_book__44c9_s_short,
   &_resbook_44s_9,&_resbook_44s_9},

  {2,0,  &_residue_44_high,
   &_huff_book__44c9_s_long,&_huff_book__44c9_s_long,
   &_resbook_44s_9,&_resbook_44s_9}
};

static vorbis_mapping_template _mapres_template_44_stereo[]={
  { _map_nominal, _res_44s_n1 }, /* -1 */
  { _map_nominal, _res_44s_0 }, /* 0 */
  { _map_nominal, _res_44s_1 }, /* 1 */
  { _map_nominal, _res_44s_2 }, /* 2 */
  { _map_nominal, _res_44s_3 }, /* 3 */
  { _map_nominal, _res_44s_4 }, /* 4 */
  { _map_nominal, _res_44s_5 }, /* 5 */
  { _map_nominal, _res_44s_6 }, /* 6 */
  { _map_nominal, _res_44s_7 }, /* 7 */
  { _map_nominal, _res_44s_8 }, /* 8 */
  { _map_nominal, _res_44s_9 }, /* 9 */
};
/*** End of inlined file: residue_44.h ***/


/*** Start of inlined file: psych_44.h ***/
static vorbis_info_psy_global _psy_global_44[5]={

  {8,   /* lines per eighth octave */
   {20.f,14.f,12.f,12.f,12.f,12.f,12.f},
   {-60.f,-30.f,-40.f,-40.f,-40.f,-40.f,-40.f}, 2,-75.f,
   -6.f,
   {99.},{{99.},{99.}},{0},{0},{{0.},{0.}}
  },
  {8,   /* lines per eighth octave */
   {14.f,10.f,10.f,10.f,10.f,10.f,10.f},
   {-40.f,-30.f,-25.f,-25.f,-25.f,-25.f,-25.f}, 2,-80.f,
   -6.f,
   {99.},{{99.},{99.}},{0},{0},{{0.},{0.}}
  },
  {8,   /* lines per eighth octave */
   {12.f,10.f,10.f,10.f,10.f,10.f,10.f},
   {-20.f,-20.f,-15.f,-15.f,-15.f,-15.f,-15.f}, 0,-80.f,
   -6.f,
   {99.},{{99.},{99.}},{0},{0},{{0.},{0.}}
  },
  {8,   /* lines per eighth octave */
   {10.f,8.f,8.f,8.f,8.f,8.f,8.f},
   {-20.f,-15.f,-12.f,-12.f,-12.f,-12.f,-12.f}, 0,-80.f,
   -6.f,
   {99.},{{99.},{99.}},{0},{0},{{0.},{0.}}
  },
  {8,   /* lines per eighth octave */
   {10.f,6.f,6.f,6.f,6.f,6.f,6.f},
   {-15.f,-15.f,-12.f,-12.f,-12.f,-12.f,-12.f}, 0,-85.f,
   -6.f,
   {99.},{{99.},{99.}},{0},{0},{{0.},{0.}}
  },
};

static compandblock _psy_compand_44[6]={
  {{
	0, 1, 2, 3, 4, 5, 6,  7,	 /* 7dB */
	8, 9,10,11,12,13,14, 15,	 /* 15dB */
	16,17,18,19,20,21,22, 23,	 /* 23dB */
	24,25,26,27,28,29,30, 31,	 /* 31dB */
	32,33,34,35,36,37,38, 39,	 /* 39dB */
  }},
  {{
	 0, 1, 2, 3, 4, 5, 6,  6,	 /* 7dB */
	 7, 7, 7, 7, 6, 6, 6,  7,	 /* 15dB */
	 7, 8, 9,10,11,12,13, 14,	 /* 23dB */
	15,16,17,17,17,18,18, 19,	 /* 31dB */
	19,19,20,21,22,23,24, 25,	 /* 39dB */
  }},
  {{
	0, 1, 2, 3, 4, 5, 5,  5,	 /* 7dB */
	6, 6, 6, 5, 4, 4, 4,  4,	 /* 15dB */
	4, 4, 5, 5, 5, 6, 6,  6,	 /* 23dB */
	7, 7, 7, 8, 8, 8, 9, 10,	 /* 31dB */
	11,12,13,14,15,16,17, 18,	 /* 39dB */
  }},
  {{
	 0, 1, 2, 3, 4, 5, 6,  7,	 /* 7dB */
	 8, 9,10,11,12,13,14, 15,	 /* 15dB */
	16,17,18,19,20,21,22, 23,	 /* 23dB */
	24,25,26,27,28,29,30, 31,	 /* 31dB */
	32,33,34,35,36,37,38, 39,	 /* 39dB */
  }},
  {{
	0, 1, 2, 3, 4, 5, 6,  7,	 /* 7dB */
	8, 9,10,11,12,12,13, 13,	 /* 15dB */
	13,14,14,14,15,15,15, 15,	 /* 23dB */
	16,16,17,17,17,18,18, 19,	 /* 31dB */
	19,19,20,21,22,23,24, 25,	 /* 39dB */
  }},
  {{
	0, 1, 2, 3, 4, 5, 6,  7,	 /* 7dB */
	8, 8, 7, 6, 5, 4, 4,  4,	 /* 15dB */
	4, 4, 5, 5, 5, 6, 6,  6,	 /* 23dB */
	7, 7, 7, 8, 8, 8, 9, 10,	 /* 31dB */
	11,12,13,14,15,16,17, 18,	 /* 39dB */
  }}
};

static vp_adjblock _vp_tonemask_adj_longblock[12]={

   {{ -3, -8,-13,-15,-10,-10,-10,-10,-10,-10,-10,  0,  0,  0,  0,  0,  0}}, /* -1 */

   {{ -4,-10,-14,-16,-15,-14,-13,-12,-12,-12,-11, -1, -1, -1, -1, -1,  0}}, /* 0 */

   {{ -6,-12,-14,-16,-15,-15,-14,-13,-13,-12,-12, -2, -2, -1, -1, -1,  0}}, /* 1 */

   {{-12,-13,-14,-16,-16,-16,-15,-14,-13,-12,-12, -6, -3, -1, -1, -1,  0}}, /* 2 */

   {{-15,-15,-15,-16,-16,-16,-16,-14,-13,-13,-13,-10, -4, -2, -1, -1,  0}}, /* 3 */

/* 4 */
   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-13,-11, -7  -3, -1, -1 , 0}}, /* 4 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-13,-11, -7  -3, -1, -1 , 0}}, /* 5 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -8, -4, -2, -2,  0}}, /* 6 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -9, -4, -2, -2,  0}}, /* 7 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -9, -4, -2, -2,  0}}, /* 8 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -9, -4, -2, -2,  0}}, /* 9 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -9, -4, -2, -2,  0}}, /* 10 */
};

static vp_adjblock _vp_tonemask_adj_otherblock[12]={

   {{ -3, -8,-13,-15,-10,-10, -9, -9, -9, -9, -9,  1,  1,  1,  1,  1,  1}}, /* -1 */

   {{ -4,-10,-14,-16,-14,-13,-12,-12,-11,-11,-10,  0,  0,  0,  0,  0,  0}}, /* 0 */

   {{ -6,-12,-14,-16,-15,-15,-14,-13,-13,-12,-12, -2, -2, -1,  0,  0,  0}}, /* 1 */

   {{-12,-13,-14,-16,-16,-16,-15,-14,-13,-12,-12, -5, -2, -1,  0,  0,  0}}, /* 2 */

   {{-15,-15,-15,-16,-16,-16,-16,-14,-13,-13,-13,-10, -4, -2,  0,  0,  0}}, /* 3 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-13,-11, -7  -3, -1, -1 , 0}}, /* 4 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-13,-11, -7  -3, -1, -1 , 0}}, /* 5 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -8, -4, -2, -2,  0}}, /* 6 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -9, -4, -2, -2,  0}}, /* 7 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -9, -4, -2, -2,  0}}, /* 8 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -9, -4, -2, -2,  0}}, /* 9 */

   {{-16,-16,-16,-16,-16,-16,-16,-15,-14,-14,-14,-12, -9, -4, -2, -2,  0}}, /* 10 */
};

static noise3 _psy_noisebias_trans[12]={
  {{{-10,-10,-10,-10,-10, -4,  0,  0,  4,  8,  8,  8,  8, 10, 12, 14, 20},
	{-30,-30,-30,-30,-26,-20,-16, -8, -6, -6, -2,  2,  2,  3,  6,  6, 15},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, -6, -4, -2}}},
  {{{-15,-15,-15,-15,-15,-12, -6, -4,  0,  2,  4,  4,  5,  5,  5,  8,  10},
	{-30,-30,-30,-30,-26,-22,-20,-14, -8, -4,  0,  0,  0,  0,  2,  3,   6},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -6, -6, -6, -4, -4, -4,  -2}}},
  {{{-15,-15,-15,-15,-15,-12,-10, -8,  0,  2,  4,  4,  5,  5,  5,  8,  10},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -4, -2, -2, -2, -2,  0,  1,   4},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -8, -8, -8, -8, -6, -6, -6,  -4}}},
  {{{-15,-15,-15,-15,-15,-12,-10, -8,  0,  2,  2,  2,  4,  4,  5,  6,  10},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -4, -3, -3, -3, -2, -1,  0,  3},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10,-10,-10,-10,-10, -8, -8, -7, -4}}},
  {{{-15,-15,-15,-15,-15,-12,-10, -8,  0,  2,  2,  2,  4,  4,  4,  5,  8},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -4, -3, -3, -3, -3, -2,  0,  2},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10,-10,-10,-10,-10, -8, -8, -8, -4}}},
  {{{-20,-20,-20,-20,-20,-18,-14, -8, -1,  1,  1,  1,  2,  3,  3,  4,  7},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -4, -3, -3, -3, -3, -2, -1,  1},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10,-10,-10,-10,-10, -8, -8, -8, -4}}},
  {{{-24,-24,-24,-24,-20,-18,-14, -8, -1,  1,  1,  1,  2,  3,  3,  4,  7},
	{-32,-32,-32,-32,-28,-24,-22,-16,-12, -6, -4, -4, -4, -4, -3, -1,  0},
	{-34,-34,-34,-34,-30,-24,-24,-18,-14,-12,-12,-12,-12,-10,-10, -9, -5}}},
  {{{-24,-24,-24,-24,-20,-18,-14, -8, -1,  1,  1,  1,  2,  3,  3,  4,  7},
	{-32,-32,-32,-32,-28,-24,-24,-18,-14, -8, -6, -6, -6, -6, -5, -2,  0},
	{-34,-34,-34,-34,-30,-26,-26,-24,-22,-19,-19,-19,-19,-18,-17,-16,-12}}},
  {{{-24,-24,-24,-24,-20,-18,-14, -8, -1,  1,  1,  1,  2,  3,  3,  4,  7},
	{-32,-32,-32,-32,-28,-24,-24,-24,-18,-14,-12,-10,-10,-10, -8, -6, -2},
	{-34,-34,-34,-34,-30,-26,-26,-26,-24,-24,-24,-24,-24,-24,-24,-20,-16}}},
  {{{-24,-24,-24,-24,-22,-20,-15,-10, -8, -2,  0,  0,  0,  1,  2,  3,  7},
	{-36,-36,-36,-36,-30,-30,-30,-24,-20,-16,-16,-16,-16,-14,-12,-10, -7},
	{-36,-36,-36,-36,-34,-30,-28,-26,-24,-30,-30,-30,-30,-30,-30,-24,-20}}},
  {{{-28,-28,-28,-28,-28,-28,-28,-20,-14, -8, -4, -4, -4, -4, -4, -2,  2},
	{-38,-38,-38,-38,-36,-34,-34,-30,-24,-20,-20,-20,-20,-18,-16,-12,-10},
	{-40,-40,-40,-40,-40,-40,-40,-38,-35,-35,-35,-35,-35,-35,-35,-35,-30}}},
  {{{-30,-30,-30,-30,-30,-30,-30,-28,-20,-14,-14,-14,-14,-14,-14,-12,-10},
	{-40,-40,-40,-40,-40,-40,-40,-40,-35,-30,-30,-30,-30,-30,-30,-30,-20},
	{-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40}}},
};

static noise3 _psy_noisebias_long[12]={
  {{{-10,-10,-10,-10,-10, -4,  0,  0,  0,  6,  6,  6,  6, 10, 10, 12,  20},
	{-20,-20,-20,-20,-20,-20,-10, -2,  0,  0,  0,  0,  0,  2,  4,  6,  15},
	{-20,-20,-20,-20,-20,-20,-20,-10, -6, -6, -6, -6, -6, -4, -4, -4, -2}}},

  {{{-10,-10,-10,-10,-10,-10, -8,  2,  2,  2,  4,  4,  5,  5,  5,  8,  10},
	{-20,-20,-20,-20,-20,-20,-20,-14, -6,  0,  0,  0,  0,  0,  2,  3,  6},
	{-20,-20,-20,-20,-20,-20,-20,-14, -8, -6, -6, -6, -6, -4, -4, -4, -2}}},
  {{{-10,-10,-10,-10,-10,-10, -8, -4,  0,  2,  4,  4,  5,  5,  5,  8,  10},
	{-20,-20,-20,-20,-20,-20,-20,-14,-10, -4, -2, -2, -2, -2,  0,  1,  4},
	{-20,-20,-20,-20,-20,-20,-20,-14,-10, -8, -8, -8, -8, -6, -6, -6, -4}}},
  {{{-10,-10,-10,-10,-10,-10,-10, -8,  0,  2,  2,  2,  4,  4,  5,  6,  10},
	{-20,-20,-20,-20,-20,-20,-20,-14,-10, -4, -3, -3, -3, -2, -1,  0,  3},
	{-20,-20,-20,-20,-20,-20,-20,-14,-10,-10,-10,-10,-10, -8, -8, -8, -4}}},
  {{{-10,-10,-10,-10,-10,-10,-10, -8,  0,  2,  2,  2,  4,  4,  4,  5,  8},
	{-20,-20,-20,-20,-20,-20,-20,-14,-10, -4, -3, -3, -3, -3, -2,  0,  2},
	{-20,-20,-20,-20,-20,-20,-20,-14,-10,-10,-10,-10,-10, -8, -8, -8, -5}}},
  {{{-15,-15,-15,-15,-15,-15,-15,-10, -4,  1,  1,  1,  2,  3,  3,  4,  7},
	{-20,-20,-20,-20,-20,-20,-20,-14,-10, -4, -3, -3, -3, -3, -2, -1,  1},
	{-20,-20,-20,-20,-20,-20,-20,-14,-10,-10,-10,-10,-10, -8, -8, -8, -7}}},
  {{{-15,-15,-15,-15,-15,-15,-15,-10, -4,  1,  1,  1,  2,  3,  3,  4,  7},
	{-22,-22,-22,-22,-22,-22,-22,-16,-12, -6, -4, -4, -4, -4, -3, -1,  0},
	{-24,-24,-24,-24,-24,-24,-24,-18,-14,-12,-12,-12,-12,-10,-10, -9, -8}}},
  {{{-15,-15,-15,-15,-15,-15,-15,-10, -4,  1,  1,  1,  2,  3,  3,  4,  7},
	{-24,-24,-24,-24,-24,-24,-24,-18,-14, -8, -6, -6, -6, -6, -5, -2,  0},
	{-26,-26,-26,-26,-26,-26,-26,-18,-16,-15,-15,-15,-15,-13,-13,-12,-10}}},
  {{{-15,-15,-15,-15,-15,-15,-15,-10, -4,  1,  1,  1,  2,  3,  3,  4,  7},
	{-24,-24,-24,-24,-24,-24,-24,-18,-14,-10, -8, -8, -8, -8, -6, -4,  0},
	{-26,-26,-26,-26,-26,-26,-26,-22,-20,-19,-19,-19,-19,-18,-17,-16,-12}}},
  {{{-15,-15,-15,-15,-15,-15,-15,-10, -4,  0,  0,  0,  0,  1,  2,  3,  7},
	{-26,-26,-26,-26,-26,-26,-26,-20,-16,-12,-10,-10,-10,-10, -8, -6, -2},
	{-28,-28,-28,-28,-28,-28,-28,-26,-24,-24,-24,-24,-24,-24,-24,-20,-16}}},
  {{{-22,-22,-22,-22,-22,-22,-22,-18,-14, -8, -4, -4, -4, -4, -4, -2,  2},
	{-26,-26,-26,-26,-26,-26,-26,-22,-18,-16,-16,-16,-16,-14,-12,-10, -7},
	{-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-24,-20}}},
  {{{-24,-24,-24,-24,-24,-24,-24,-24,-24,-18,-14,-14,-14,-14,-14,-12,-10},
	{-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-20},
	{-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40}}},
};

static noise3 _psy_noisebias_impulse[12]={
  {{{-10,-10,-10,-10,-10, -4,  0,  0,  4,  8,  8,  8,  8, 10, 12, 14, 20},
	{-30,-30,-30,-30,-26,-20,-16, -8, -6, -6, -2,  2,  2,  3,  6,  6, 15},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, -6, -4, -2}}},

  {{{-10,-10,-10,-10,-10, -4,  0,  0,  4,  4,  8,  8,  8, 10, 12, 14, 20},
	{-30,-30,-30,-30,-26,-22,-20,-14, -6, -2,  0,  0,  0,  0,  2,  3,  6},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, -6, -4, -2}}},
  {{{-12,-12,-12,-12,-12, -8, -6, -4,  0,  4,  4,  4,  4, 10, 12, 14, 20},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -4, -4, -2, -2, -2, -2,  2},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -8,-10,-10, -8, -8, -8, -6, -4}}},
  {{{-14,-14,-14,-14,-14,-10, -8, -6, -2,  2,  2,  2,  2,  8, 10, 10, 16},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -6, -6, -4, -4, -4, -2,  0},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10,-10,-10,-10,-10,-10,-10, -8, -4}}},
  {{{-14,-14,-14,-14,-14,-10, -8, -6, -2,  2,  2,  2,  2,  6,  8,  8, 14},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -6, -6, -4, -4, -4, -2,  0},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10,-10,-10,-10,-10,-10,-10, -8, -4}}},
  {{{-16,-16,-16,-16,-16,-12,-10, -6, -2,  0,  0,  0,  0,  4,  6,  6, 12},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -6, -6, -4, -4, -4, -2,  0},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10,-10,-10,-10,-10,-10,-10, -8, -4}}},
  {{{-20,-20,-20,-20,-20,-18,-14,-10, -4,  0,  0,  0,  0,  4,  4,  6, 11},
	{-32,-32,-32,-32,-28,-24,-22,-16,-10, -6, -8, -8, -6, -6, -6, -4, -2},
	{-34,-34,-34,-34,-30,-26,-24,-18,-14,-12,-12,-12,-12,-12,-10, -9, -5}}},
  {{{-20,-20,-20,-20,-20,-18,-14,-10, -4,  0,  0,  0,  0,  4,  4,  6, 11},
	{-34,-34,-34,-34,-30,-30,-30,-24,-16,-16,-16,-16,-16,-16,-14,-14,-12},
	{-36,-36,-36,-36,-36,-34,-28,-24,-20,-20,-20,-20,-20,-20,-20,-18,-16}}},
  {{{-22,-22,-22,-22,-22,-20,-14,-10, -6,  0,  0,  0,  0,  4,  4,  6, 11},
	{-34,-34,-34,-34,-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-24,-22},
	{-40,-40,-40,-40,-40,-40,-40,-32,-30,-30,-30,-30,-30,-30,-30,-30,-24}}},
  {{{-24,-24,-24,-24,-24,-22,-14,-10, -6, -1, -1, -1, -1,  3,  3,  5, 10},
	{-34,-34,-34,-34,-34,-32,-32,-30,-26,-26,-26,-26,-26,-26,-26,-26,-24},
	{-40,-40,-40,-40,-40,-40,-40,-32,-30,-30,-30,-30,-30,-30,-30,-30,-24}}},
  {{{-28,-28,-28,-28,-28,-28,-28,-20,-14, -8, -4, -4, -4, -4, -4, -2,  2},
	{-36,-36,-36,-36,-34,-32,-32,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26},
	{-40,-40,-40,-40,-40,-40,-40,-32,-30,-30,-30,-30,-30,-30,-30,-24,-20}}},
  {{{-30,-30,-30,-30,-30,-26,-24,-24,-24,-20,-16,-16,-16,-16,-16,-14,-12},
	{-40,-40,-40,-40,-40,-40,-40,-40,-35,-30,-30,-30,-30,-30,-30,-30,-26},
	{-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40}}},
};

static noise3 _psy_noisebias_padding[12]={

  {{{-10,-10,-10,-10,-10, -4,  0,  0,  4,  8,  8,  8,  8, 10, 12, 14, 20},
	{-30,-30,-30,-30,-26,-20,-16, -8, -6, -6, -2,  2,  2,  3,  6,  6, 15},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, -6, -4, -2}}},

  {{{-10,-10,-10,-10,-10, -4,  0,  0,  4,  8,  8,  8,  8, 10, 12, 14, 20},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -4, -2,  2,  3,  6,  6,  8, 10},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -4, -4, -4, -4, -4, -2,  0,  2}}},
  {{{-12,-12,-12,-12,-12, -8, -6, -4,  0,  4,  4,  4,  4, 10, 12, 14, 20},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -4,  0,  0,  0,  2,  2,  4,  8},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -6, -6, -6, -6, -4, -2,  0}}},
  {{{-14,-14,-14,-14,-14,-10, -8, -6, -2,  2,  2,  2,  2,  8, 10, 10, 16},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -1, -1, -1,  0,  0,  2,  6},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -8, -8, -8, -8, -8, -6, -4, -2}}},
  {{{-14,-14,-14,-14,-14,-10, -8, -6, -2,  2,  2,  2,  2,  6,  8,  8, 14},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -1, -1, -1,  0,  0,  2,  6},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -8, -8, -8, -8, -8, -6, -4, -2}}},
  {{{-16,-16,-16,-16,-16,-12,-10, -6, -2,  0,  0,  0,  0,  4,  6,  6, 12},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -6, -1, -1, -1, -1,  0,  2,  6},
	{-30,-30,-30,-30,-26,-22,-20,-14,-10, -8, -8, -8, -8, -8, -6, -4, -2}}},
  {{{-20,-20,-20,-20,-20,-18,-14,-10, -4,  0,  0,  0,  0,  4,  6,  6, 12},
	{-32,-32,-32,-32,-28,-24,-22,-16,-12, -6, -3, -3, -3, -3, -2,  0,  4},
	{-34,-34,-34,-34,-30,-26,-24,-18,-14,-10,-10,-10,-10,-10, -8, -5, -3}}},
  {{{-20,-20,-20,-20,-20,-18,-14,-10, -4,  0,  0,  0,  0,  4,  6,  6, 12},
	{-34,-34,-34,-34,-30,-30,-24,-20,-14, -8, -4, -4, -4, -4, -3, -1,  4},
	{-34,-34,-34,-34,-34,-30,-26,-20,-16,-13,-13,-13,-13,-13,-11, -8, -6}}},
  {{{-20,-20,-20,-20,-20,-18,-14,-10, -4,  0,  0,  0,  0,  4,  6,  6, 12},
	{-34,-34,-34,-34,-30,-30,-30,-24,-16,-10, -8, -6, -6, -6, -5, -3,  1},
	{-34,-34,-34,-34,-32,-32,-28,-22,-18,-16,-16,-16,-16,-16,-14,-12,-10}}},
  {{{-22,-22,-22,-22,-22,-20,-14,-10, -4,  0,  0,  0,  0,  3,  5,  5, 11},
	{-34,-34,-34,-34,-30,-30,-30,-24,-16,-12,-10, -8, -8, -8, -7, -5, -2},
	{-36,-36,-36,-36,-36,-34,-28,-22,-20,-20,-20,-20,-20,-20,-20,-16,-14}}},
  {{{-28,-28,-28,-28,-28,-28,-28,-20,-14, -8, -2, -2, -2, -2,  0,  2,  6},
	{-36,-36,-36,-36,-34,-32,-32,-24,-16,-12,-12,-12,-12,-12,-10, -8, -5},
	{-40,-40,-40,-40,-40,-40,-40,-32,-26,-24,-24,-24,-24,-24,-24,-20,-18}}},
  {{{-30,-30,-30,-30,-30,-26,-24,-24,-24,-20,-12,-12,-12,-12,-12,-10, -8},
	{-40,-40,-40,-40,-40,-40,-40,-40,-35,-30,-25,-25,-25,-25,-25,-25,-15},
	{-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40,-40}}},
};

static noiseguard _psy_noiseguards_44[4]={
  {3,3,15},
  {3,3,15},
  {10,10,100},
  {10,10,100},
};

static int _psy_tone_suppress[12]={
  -20,-20,-20,-20,-20,-24,-30,-40,-40,-45,-45,-45,
};
static int _psy_tone_0dB[12]={
  90,90,95,95,95,95,105,105,105,105,105,105,
};
static int _psy_noise_suppress[12]={
  -20,-20,-24,-24,-24,-24,-30,-40,-40,-45,-45,-45,
};

static vorbis_info_psy _psy_info_template={
  -1,
  -140.,-140.,
  {0.f,0.f,0.f},	 0.,0.,	-40.f, {0.},

  1,	  -0.f,	   .5f, .5f,	 0,0,0,
  {{-1},{-1},{-1}},{-1},105.f,
  0,0,-1,-1,0.,
};

static int _psy_ath_floater[12]={
  -100,-100,-100,-100,-100,-100,-105,-105,-105,-105,-110,-120,
};
static int _psy_ath_abs[12]={
  -130,-130,-130,-130,-140,-140,-140,-140,-140,-140,-140,-150,
};

static adj_stereo _psy_stereo_modes_44[12]={
  {{  4,  4,  4,  4,  4,  4,  4,  3,  2,  2,  1,  0,  0,  0,  0},
   {  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  5,  4,  3},
   {  1,  2,  3,  4,  4,  4,  4,  4,  4,  5,  6,  7,  8,  8,  8},
   { 12,12.5, 13,13.5, 14,14.5, 15, 99, 99, 99, 99, 99, 99, 99, 99}},

  {{  4,  4,  4,  4,  4,  4,  4,  3,  2,  1,  0,  0,  0,  0,  0},
   {  8,  8,  8,  8,  6,  6,  5,  5,  5,  5,  5,  5,  5,  4,  3},
   {  1,  2,  3,  4,  4,  5,  6,  6,  6,  6,  6,  8,  8,  8,  8},
   { 12,12.5, 13,13.5, 14,14.5, 15, 99, 99, 99, 99, 99, 99, 99, 99}},

  {{  3,  3,  3,  3,  3,  3,  3,  3,  2,  1,  0,  0,  0,  0,  0},
   {  8,  8,  8,  8,  6,  6,  5,  5,  5,  5,  5,  5,  5,  4,  3},
   {  1,  2,  3,  4,  4,  5,  6,  6,  6,  6,  6,  8,  8,  8,  8},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},

  {{  3,  3,  3,  3,  3,  3,  3,  2,  1,  1,  0,  0,  0,  0,  0},
   {  8,  8,  6,  6,  5,  5,  4,  4,  4,  4,  4,  4,  3,  2,  1},
   {  3,  4,  4,  5,  5,  6,  6,  6,  6,  6,  6,  8,  8,  8,  8},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  2,  2,  2,  2,  2,  1,  1,  1,  1,  0,  0,  0,  0,  0,  0},
   {  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  3,  2,  1},
   {  4,  4,  5,  6,  6,  6,  6,  6,  8,  8, 10, 10, 10, 10, 10},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  2,  2,  2,  1,  1,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  3,  3,  2,  1,  0},
   {  6,  6,  6,  8,  8,  8,  8,  8,  8,  8, 10, 10, 10, 10, 10},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  2,  2,  2,  1,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  2,  0,  0,  0,  0},
   {  6,  7,  8,  8,  8, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  3,  3,  3,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  8,  8,  8, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  3,  3,  3,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  8,  8, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  8, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
};

static att3 _psy_tone_masteratt_44[12]={
  {{ 35,  21,   9},  0,	0}, /* -1 */
  {{ 30,  20,   8}, -2, 1.25}, /* 0 */
/* 1 */
  {{ 25,  12,   2},  0,	0}, /* 1 */
/* 2 */
  {{ 20,   9,  -3},  0,	0}, /* 2 */
  {{ 20,   9,  -4},  0,	0}, /* 3 */
  {{ 20,   9,  -4},  0,	0}, /* 4 */
  {{ 20,   6,  -6},  0,	0}, /* 5 */
  {{ 20,   3, -10},  0,	0}, /* 6 */
  {{ 18,   1, -14},  0,	0}, /* 7 */
  {{ 18,   0, -16},  0,	0}, /* 8 */
  {{ 18,  -2, -16},  0,	0}, /* 9 */
  {{ 12,  -2, -20},  0,	0}, /* 10 */
};

static double _psy_lowpass_44[12]={
  13.9,15.1,15.8,16.5,17.2,18.9,20.1,48.,999.,999.,999.,999.
};

static int _noise_start_short_44[11]={
  32,16,16,16,32,9999,9999,9999,9999,9999,9999
};
static int _noise_start_long_44[11]={
  256,128,128,256,512,9999,9999,9999,9999,9999,9999
};

static int _noise_part_short_44[11]={
	8,8,8,8,8,8,8,8,8,8,8
};
static int _noise_part_long_44[11]={
	32,32,32,32,32,32,32,32,32,32,32
};

static double _noise_thresh_44[11]={
   .2,.2,.2,.4,.6,9999.,9999.,9999.,9999.,9999.,9999.,
};

static double _noise_thresh_5only[2]={
 .5,.5,
};
/*** End of inlined file: psych_44.h ***/

static double rate_mapping_44_stereo[12]={
  22500.,32000.,40000.,48000.,56000.,64000.,
  80000.,96000.,112000.,128000.,160000.,250001.
};

static double quality_mapping_44[12]={
  -.1,.0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1.0
};

static int blocksize_short_44[11]={
  512,256,256,256,256,256,256,256,256,256,256
};
static int blocksize_long_44[11]={
  4096,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048
};

static double _psy_compand_short_mapping[12]={
  0.5, 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.
};
static double _psy_compand_long_mapping[12]={
  3.5, 4., 4., 4.3, 4.6, 5., 5., 5., 5., 5., 5., 5.
};

static double _global_mapping_44[12]={
 0., 1., 1., 1.5, 2., 2., 2.5, 2.7, 3.0, 3.7, 4., 4.
};

static int _floor_short_mapping_44[11]={
  1,0,0,2,2,4,5,5,5,5,5
};
static int _floor_long_mapping_44[11]={
  8,7,7,7,7,7,7,7,7,7,7
};

ve_setup_data_template ve_setup_44_stereo={
  11,
  rate_mapping_44_stereo,
  quality_mapping_44,
  2,
  40000,
  50000,

  blocksize_short_44,
  blocksize_long_44,

  _psy_tone_masteratt_44,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_otherblock,
  _vp_tonemask_adj_longblock,
  _vp_tonemask_adj_otherblock,

  _psy_noiseguards_44,
  _psy_noisebias_impulse,
  _psy_noisebias_padding,
  _psy_noisebias_trans,
  _psy_noisebias_long,
  _psy_noise_suppress,

  _psy_compand_44,
  _psy_compand_short_mapping,
  _psy_compand_long_mapping,

  {_noise_start_short_44,_noise_start_long_44},
  {_noise_part_short_44,_noise_part_long_44},
  _noise_thresh_44,

  _psy_ath_floater,
  _psy_ath_abs,

  _psy_lowpass_44,

  _psy_global_44,
  _global_mapping_44,
  _psy_stereo_modes_44,

  _floor_books,
  _floor,
  _floor_short_mapping_44,
  _floor_long_mapping_44,

  _mapres_template_44_stereo
};
/*** End of inlined file: setup_44.h ***/


/*** Start of inlined file: setup_44u.h ***/

/*** Start of inlined file: residue_44u.h ***/

/*** Start of inlined file: res_books_uncoupled.h ***/
static long _vq_quantlist__16u0__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16u0__p1_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 8, 5, 8, 8, 8,10,10, 8,
	10,11, 5, 8, 8, 8,10,10, 8,10,10, 4, 9, 9, 9,12,
	11, 8,11,11, 8,12,11,10,12,14,10,13,13, 7,11,11,
	10,14,12,11,14,14, 4, 9, 9, 8,11,11, 9,11,12, 7,
	11,11,10,13,14,10,12,14, 8,11,12,10,14,14,10,13,
	12,
};

static float _vq_quantthresh__16u0__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__16u0__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16u0__p1_0 = {
	_vq_quantthresh__16u0__p1_0,
	_vq_quantmap__16u0__p1_0,
	3,
	3
};

static static_codebook _16u0__p1_0 = {
	4, 81,
	_vq_lengthlist__16u0__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__16u0__p1_0,
	NULL,
	&_vq_auxt__16u0__p1_0,
	NULL,
	0
};

static long _vq_quantlist__16u0__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16u0__p2_0[] = {
	 2, 4, 4, 5, 6, 6, 5, 6, 6, 5, 7, 7, 7, 8, 9, 7,
	 8, 9, 5, 7, 7, 7, 9, 8, 7, 9, 7, 4, 7, 7, 7, 9,
	 9, 7, 8, 8, 6, 9, 8, 7, 8,11, 9,11,10, 6, 8, 9,
	 8,11, 8, 9,10,11, 4, 7, 7, 7, 8, 8, 7, 9, 9, 6,
	 9, 8, 9,11,10, 8, 8,11, 6, 8, 9, 9,10,11, 8,11,
	 8,
};

static float _vq_quantthresh__16u0__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__16u0__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16u0__p2_0 = {
	_vq_quantthresh__16u0__p2_0,
	_vq_quantmap__16u0__p2_0,
	3,
	3
};

static static_codebook _16u0__p2_0 = {
	4, 81,
	_vq_lengthlist__16u0__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__16u0__p2_0,
	NULL,
	&_vq_auxt__16u0__p2_0,
	NULL,
	0
};

static long _vq_quantlist__16u0__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16u0__p3_0[] = {
	 1, 5, 5, 7, 7, 6, 7, 7, 8, 8, 6, 7, 8, 8, 8, 8,
	 9, 9,11,11, 8, 9, 9,11,11, 6, 9, 8,10,10, 8,10,
	10,11,11, 8,10,10,11,11,10,11,10,13,12, 9,11,10,
	13,13, 6, 8, 9,10,10, 8,10,10,11,11, 8,10,10,11,
	11, 9,10,11,13,12,10,10,11,12,12, 8,11,11,14,13,
	10,12,11,15,13, 9,12,11,15,14,12,14,13,16,14,12,
	13,13,17,14, 8,11,11,13,14, 9,11,12,14,15,10,11,
	12,13,15,11,13,13,14,16,12,13,14,14,16, 5, 9, 9,
	11,11, 9,11,11,12,12, 8,11,11,12,12,11,12,12,15,
	14,10,12,12,15,15, 8,11,11,13,12,10,12,12,13,13,
	10,12,12,14,13,12,12,13,14,15,11,13,13,17,16, 7,
	11,11,13,13,10,12,12,14,13,10,12,12,13,14,12,13,
	12,15,14,11,13,13,15,14, 9,12,12,16,15,11,13,13,
	17,16,10,13,13,16,16,13,14,15,15,16,13,15,14,19,
	17, 9,12,12,14,16,11,13,13,15,16,10,13,13,17,16,
	13,14,13,17,15,12,15,15,16,17, 5, 9, 9,11,11, 8,
	11,11,13,12, 9,11,11,12,12,10,12,12,14,15,11,12,
	12,14,14, 7,11,10,13,12,10,12,12,14,13,10,11,12,
	13,13,11,13,13,15,16,12,12,13,15,15, 7,11,11,13,
	13,10,13,13,14,14,10,12,12,13,13,11,13,13,16,15,
	12,13,13,15,14, 9,12,12,15,15,10,13,13,17,16,11,
	12,13,15,15,12,15,14,18,18,13,14,14,16,17, 9,12,
	12,15,16,10,13,13,15,16,11,13,13,15,16,13,15,15,
	17,17,13,15,14,16,15, 7,11,11,15,16,10,13,12,16,
	17,10,12,13,15,17,15,16,16,18,17,13,15,15,17,18,
	 8,12,12,16,16,11,13,14,17,18,11,13,13,18,16,15,
	17,16,17,19,14,15,15,17,16, 8,12,12,16,15,11,14,
	13,18,17,11,13,14,18,17,15,16,16,18,17,13,16,16,
	18,18,11,15,14,18,17,13,14,15,18, 0,12,15,15, 0,
	17,17,16,17,17,18,14,16,18,18, 0,11,14,14,17, 0,
	12,15,14,17,19,12,15,14,18, 0,15,18,16, 0,17,14,
	18,16,18, 0, 7,11,11,16,15,10,12,12,18,16,10,13,
	13,16,15,13,15,14,17,17,14,16,16,19,18, 8,12,12,
	16,16,11,13,13,18,16,11,13,14,17,16,14,15,15,19,
	18,15,16,16, 0,19, 8,12,12,16,17,11,13,13,17,17,
	11,14,13,17,17,13,15,15,17,19,15,17,17,19, 0,11,
	14,15,19,17,12,15,16,18,18,12,14,15,19,17,14,16,
	17, 0,18,16,16,19,17, 0,11,14,14,18,19,12,15,14,
	17,17,13,16,14,17,16,14,17,16,18,18,15,18,15, 0,
	18,
};

static float _vq_quantthresh__16u0__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16u0__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16u0__p3_0 = {
	_vq_quantthresh__16u0__p3_0,
	_vq_quantmap__16u0__p3_0,
	5,
	5
};

static static_codebook _16u0__p3_0 = {
	4, 625,
	_vq_lengthlist__16u0__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16u0__p3_0,
	NULL,
	&_vq_auxt__16u0__p3_0,
	NULL,
	0
};

static long _vq_quantlist__16u0__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16u0__p4_0[] = {
	 3, 5, 5, 8, 8, 6, 6, 6, 9, 9, 6, 6, 6, 9, 9, 9,
	10, 9,11,11, 9, 9, 9,11,11, 6, 7, 7,10,10, 7, 7,
	 8,10,10, 7, 7, 8,10,10,10,10,10,11,12, 9,10,10,
	11,12, 6, 7, 7,10,10, 7, 8, 7,10,10, 7, 8, 7,10,
	10,10,11,10,12,11,10,10,10,13,10, 9,10,10,12,12,
	10,11,10,14,12, 9,11,11,13,13,11,12,13,13,13,11,
	12,12,15,13, 9,10,10,12,13, 9,11,10,12,13,10,10,
	11,12,13,11,12,12,12,13,11,12,12,13,13, 5, 7, 7,
	10,10, 7, 8, 8,10,10, 7, 8, 8,10,10,10,11,10,12,
	13,10,10,11,12,12, 6, 8, 8,11,10, 7, 8, 9,10,12,
	 8, 9, 9,11,11,11,10,11,11,12,10,11,11,13,12, 7,
	 8, 8,10,11, 8, 9, 8,11,10, 8, 9, 9,11,11,10,12,
	10,13,11,10,11,11,13,13,10,11,10,14,13,10,10,11,
	13,13,10,12,11,14,13,12,11,13,12,13,13,12,13,14,
	14,10,11,11,13,13,10,11,10,12,13,10,12,12,12,14,
	12,12,12,14,12,12,13,12,17,15, 5, 7, 7,10,10, 7,
	 8, 8,10,10, 7, 8, 8,11,10,10,10,11,12,12,10,11,
	11,12,13, 6, 8, 8,11,10, 8, 9, 9,11,11, 7, 8, 9,
	10,11,11,11,11,12,12,10,10,11,12,13, 6, 8, 8,10,
	11, 8, 9, 9,11,11, 7, 9, 7,11,10,10,12,12,13,13,
	11,11,10,13,11, 9,11,10,14,13,11,11,11,15,13,10,
	10,11,13,13,12,13,13,14,14,12,11,12,12,13,10,11,
	11,12,13,10,11,12,13,13,10,11,10,13,12,12,12,13,
	14, 0,12,13,11,13,11, 8,10,10,13,13,10,11,11,14,
	13,10,11,11,13,12,13,14,14,14,15,12,12,12,15,14,
	 9,11,10,13,12,10,10,11,13,14,11,11,11,15,12,13,
	12,14,15,16,13,13,13,14,13, 9,11,11,12,12,10,12,
	11,13,13,10,11,11,13,14,13,13,13,15,15,13,13,14,
	17,15,11,12,12,14,14,10,11,12,13,15,12,13,13, 0,
	15,13,11,14,12,16,14,16,14, 0,15,11,12,12,14,16,
	11,13,12,16,15,12,13,13,14,15,12,14,12,15,13,15,
	14,14,16,16, 8,10,10,13,13,10,11,10,13,14,10,11,
	11,13,13,13,13,12,14,14,14,13,13,16,17, 9,10,10,
	12,14,10,12,11,14,13,10,11,12,13,14,12,12,12,15,
	15,13,13,13,14,14, 9,10,10,13,13,10,11,12,12,14,
	10,11,10,13,13,13,13,13,14,16,13,13,13,14,14,11,
	12,13,15,13,12,14,13,14,16,12,12,13,13,14,13,14,
	14,17,15,13,12,17,13,16,11,12,13,14,15,12,13,14,
	14,17,11,12,11,14,14,13,16,14,16, 0,14,15,11,15,
	11,
};

static float _vq_quantthresh__16u0__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16u0__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16u0__p4_0 = {
	_vq_quantthresh__16u0__p4_0,
	_vq_quantmap__16u0__p4_0,
	5,
	5
};

static static_codebook _16u0__p4_0 = {
	4, 625,
	_vq_lengthlist__16u0__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16u0__p4_0,
	NULL,
	&_vq_auxt__16u0__p4_0,
	NULL,
	0
};

static long _vq_quantlist__16u0__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16u0__p5_0[] = {
	 1, 4, 4, 7, 7, 7, 7, 9, 9, 4, 6, 6, 8, 8, 8, 8,
	 9, 9, 4, 6, 6, 8, 8, 8, 8, 9, 9, 7, 8, 8, 9, 9,
	 9, 9,11,10, 7, 8, 8, 9, 9, 9, 9,10,11, 7, 8, 8,
	 9, 9,10,10,11,11, 7, 8, 8, 9, 9,10,10,11,11, 9,
	 9, 9,10,10,11,11,12,12, 9, 9, 9,10,10,11,11,12,
	12,
};

static float _vq_quantthresh__16u0__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16u0__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16u0__p5_0 = {
	_vq_quantthresh__16u0__p5_0,
	_vq_quantmap__16u0__p5_0,
	9,
	9
};

static static_codebook _16u0__p5_0 = {
	2, 81,
	_vq_lengthlist__16u0__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16u0__p5_0,
	NULL,
	&_vq_auxt__16u0__p5_0,
	NULL,
	0
};

static long _vq_quantlist__16u0__p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16u0__p6_0[] = {
	 1, 4, 4, 7, 7,10,10,12,12,13,13,18,17, 3, 6, 6,
	 9, 9,11,11,13,13,14,14,18,17, 3, 6, 6, 9, 9,11,
	11,13,13,14,14,17,18, 7, 9, 9,11,11,13,13,14,14,
	15,15, 0, 0, 7, 9, 9,11,11,13,13,14,14,15,16,19,
	18,10,11,11,13,13,14,14,16,15,17,18, 0, 0,10,11,
	11,13,13,14,14,15,15,16,18, 0, 0,11,13,13,14,14,
	15,15,17,17, 0,19, 0, 0,11,13,13,14,14,14,15,16,
	18, 0,19, 0, 0,13,14,14,15,15,18,17,18,18, 0,19,
	 0, 0,13,14,14,15,16,16,16,18,18,19, 0, 0, 0,16,
	17,17, 0,17,19,19, 0,19, 0, 0, 0, 0,16,19,16,17,
	18, 0,19, 0, 0, 0, 0, 0, 0,
};

static float _vq_quantthresh__16u0__p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__16u0__p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16u0__p6_0 = {
	_vq_quantthresh__16u0__p6_0,
	_vq_quantmap__16u0__p6_0,
	13,
	13
};

static static_codebook _16u0__p6_0 = {
	2, 169,
	_vq_lengthlist__16u0__p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__16u0__p6_0,
	NULL,
	&_vq_auxt__16u0__p6_0,
	NULL,
	0
};

static long _vq_quantlist__16u0__p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16u0__p6_1[] = {
	 1, 4, 5, 6, 6, 4, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6,
	 6, 6, 7, 7, 6, 6, 6, 7, 7,
};

static float _vq_quantthresh__16u0__p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16u0__p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16u0__p6_1 = {
	_vq_quantthresh__16u0__p6_1,
	_vq_quantmap__16u0__p6_1,
	5,
	5
};

static static_codebook _16u0__p6_1 = {
	2, 25,
	_vq_lengthlist__16u0__p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16u0__p6_1,
	NULL,
	&_vq_auxt__16u0__p6_1,
	NULL,
	0
};

static long _vq_quantlist__16u0__p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16u0__p7_0[] = {
	 1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__16u0__p7_0[] = {
	-157.5, 157.5,
};

static long _vq_quantmap__16u0__p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16u0__p7_0 = {
	_vq_quantthresh__16u0__p7_0,
	_vq_quantmap__16u0__p7_0,
	3,
	3
};

static static_codebook _16u0__p7_0 = {
	4, 81,
	_vq_lengthlist__16u0__p7_0,
	1, -518803456, 1628680192, 2, 0,
	_vq_quantlist__16u0__p7_0,
	NULL,
	&_vq_auxt__16u0__p7_0,
	NULL,
	0
};

static long _vq_quantlist__16u0__p7_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__16u0__p7_1[] = {
	 1, 5, 5, 6, 5, 9,10,11,11,10,10,10,10,10,10, 5,
	 8, 8, 8,10,10,10,10,10,10,10,10,10,10,10, 5, 8,
	 9, 9, 9,10,10,10,10,10,10,10,10,10,10, 5,10, 8,
	10,10,10,10,10,10,10,10,10,10,10,10, 4, 8, 9,10,
	10,10,10,10,10,10,10,10,10,10,10, 9,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10, 9,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__16u0__p7_1[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__16u0__p7_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__16u0__p7_1 = {
	_vq_quantthresh__16u0__p7_1,
	_vq_quantmap__16u0__p7_1,
	15,
	15
};

static static_codebook _16u0__p7_1 = {
	2, 225,
	_vq_lengthlist__16u0__p7_1,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__16u0__p7_1,
	NULL,
	&_vq_auxt__16u0__p7_1,
	NULL,
	0
};

static long _vq_quantlist__16u0__p7_2[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__16u0__p7_2[] = {
	 1, 6, 6, 7, 8, 7, 7,10, 9,10, 9,11,10, 9,11,10,
	 9, 9, 9, 9,10, 6, 8, 7, 9, 9, 8, 8,10,10, 9,11,
	11,12,12,10, 9,11, 9,12,10, 9, 6, 9, 8, 9,12, 8,
	 8,11, 9,11,11,12,11,12,12,10,11,11,10,10,11, 7,
	10, 9, 9, 9, 9, 9,10, 9,10, 9,10,10,12,10,10,10,
	11,12,10,10, 7, 9, 9, 9,10, 9, 9,10,10, 9, 9, 9,
	11,11,10,10,10,10, 9, 9,12, 7, 9,10, 9,11, 9,10,
	 9,10,11,11,11,10,11,12, 9,12,11,10,10,10, 7, 9,
	 9, 9, 9,10,12,10, 9,11,12,10,11,12,12,11, 9,10,
	11,10,11, 7, 9,10,10,11,10, 9,10,11,11,11,10,12,
	12,12,11,11,10,11,11,12, 8, 9,10,12,11,10,10,12,
	12,12,12,12,10,11,11, 9,11,10,12,11,11, 8, 9,10,
	10,11,12,11,11,10,10,10,12,12,12, 9,10,12,12,12,
	12,12, 8,10,11,10,10,12, 9,11,12,12,11,12,12,12,
	12,10,12,10,10,10,10, 8,12,11,11,11,10,10,11,12,
	12,12,12,11,12,12,12,11,11,11,12,10, 9,10,10,12,
	10,12,10,12,12,10,10,10,11,12,12,12,11,12,12,12,
	11,10,11,12,12,12,11,12,12,11,12,12,11,12,12,12,
	12,11,12,12,10,10,10,10,11,11,12,11,12,12,12,12,
	12,12,12,11,12,11,10,11,11,12,11,11, 9,10,10,10,
	12,10,10,11, 9,11,12,11,12,11,12,12,10,11,10,12,
	 9, 9, 9,12,11,10,11,10,12,10,12,10,12,12,12,11,
	11,11,11,11,10, 9,10,10,11,10,11,11,12,11,10,11,
	12,12,12,11,11, 9,12,10,12, 9,10,12,10,10,11,10,
	11,11,12,11,10,11,10,11,11,11,11,12,11,11,10, 9,
	10,10,10, 9,11,11,10, 9,12,10,11,12,11,12,12,11,
	12,11,12,11,10,11,10,12,11,12,11,12,11,12,10,11,
	10,10,12,11,10,11,11,11,10,
};

static float _vq_quantthresh__16u0__p7_2[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__16u0__p7_2[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__16u0__p7_2 = {
	_vq_quantthresh__16u0__p7_2,
	_vq_quantmap__16u0__p7_2,
	21,
	21
};

static static_codebook _16u0__p7_2 = {
	2, 441,
	_vq_lengthlist__16u0__p7_2,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__16u0__p7_2,
	NULL,
	&_vq_auxt__16u0__p7_2,
	NULL,
	0
};

static long _huff_lengthlist__16u0__single[] = {
	 3, 5, 8, 7,14, 8, 9,19, 5, 2, 5, 5, 9, 6, 9,19,
	 8, 4, 5, 7, 8, 9,13,19, 7, 4, 6, 5, 9, 6, 9,19,
	12, 8, 7, 9,10,11,13,19, 8, 5, 8, 6, 9, 6, 7,19,
	 8, 8,10, 7, 7, 4, 5,19,12,17,19,15,18,13,11,18,
};

static static_codebook _huff_book__16u0__single = {
	2, 64,
	_huff_lengthlist__16u0__single,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__16u1__long[] = {
	 3, 6,10, 8,12, 8,14, 8,14,19, 5, 3, 5, 5, 7, 6,
	11, 7,16,19, 7, 5, 6, 7, 7, 9,11,12,19,19, 6, 4,
	 7, 5, 7, 6,10, 7,18,18, 8, 6, 7, 7, 7, 7, 8, 9,
	18,18, 7, 5, 8, 5, 7, 5, 8, 6,18,18,12, 9,10, 9,
	 9, 9, 8, 9,18,18, 8, 7,10, 6, 8, 5, 6, 4,11,18,
	11,15,16,12,11, 8, 8, 6, 9,18,14,18,18,18,16,16,
	16,13,16,18,
};

static static_codebook _huff_book__16u1__long = {
	2, 100,
	_huff_lengthlist__16u1__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__16u1__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16u1__p1_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 8, 7, 7,10,10, 7,
	 9,10, 5, 7, 8, 7,10, 9, 7,10,10, 5, 8, 8, 8,10,
	10, 8,10,10, 7,10,10,10,11,12,10,12,13, 7,10,10,
	 9,13,11,10,12,13, 5, 8, 8, 8,10,10, 8,10,10, 7,
	10,10,10,12,12, 9,11,12, 7,10,11,10,12,12,10,13,
	11,
};

static float _vq_quantthresh__16u1__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__16u1__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16u1__p1_0 = {
	_vq_quantthresh__16u1__p1_0,
	_vq_quantmap__16u1__p1_0,
	3,
	3
};

static static_codebook _16u1__p1_0 = {
	4, 81,
	_vq_lengthlist__16u1__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__16u1__p1_0,
	NULL,
	&_vq_auxt__16u1__p1_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16u1__p2_0[] = {
	 3, 4, 4, 5, 6, 6, 5, 6, 6, 5, 6, 6, 6, 7, 8, 6,
	 7, 8, 5, 6, 6, 6, 8, 7, 6, 8, 7, 5, 6, 6, 6, 8,
	 8, 6, 8, 8, 6, 8, 8, 7, 7,10, 8, 9, 9, 6, 8, 8,
	 7, 9, 8, 8, 9,10, 5, 6, 6, 6, 8, 8, 7, 8, 8, 6,
	 8, 8, 8,10, 9, 7, 8, 9, 6, 8, 8, 8, 9, 9, 7,10,
	 8,
};

static float _vq_quantthresh__16u1__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__16u1__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16u1__p2_0 = {
	_vq_quantthresh__16u1__p2_0,
	_vq_quantmap__16u1__p2_0,
	3,
	3
};

static static_codebook _16u1__p2_0 = {
	4, 81,
	_vq_lengthlist__16u1__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__16u1__p2_0,
	NULL,
	&_vq_auxt__16u1__p2_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16u1__p3_0[] = {
	 1, 5, 5, 8, 8, 6, 7, 7, 9, 9, 5, 7, 7, 9, 9, 9,
	10, 9,11,11, 9, 9,10,11,11, 6, 8, 8,10,10, 8, 9,
	10,11,11, 8, 9,10,11,11,10,11,11,12,13,10,11,11,
	13,13, 6, 8, 8,10,10, 8,10, 9,11,11, 8,10, 9,11,
	11,10,11,11,13,13,10,11,11,13,12, 9,11,11,14,13,
	10,12,12,15,14,10,12,11,14,13,12,13,13,15,15,12,
	13,13,16,14, 9,11,11,13,14,10,11,12,14,14,10,12,
	12,14,15,12,13,13,14,15,12,13,14,15,16, 5, 8, 8,
	11,11, 8,10,10,12,12, 8,10,10,12,12,11,12,12,14,
	14,11,12,12,14,14, 8,10,10,12,12, 9,11,12,12,13,
	10,12,12,13,13,12,12,13,14,15,11,13,13,15,15, 7,
	10,10,12,12, 9,12,11,13,12,10,11,12,13,13,12,13,
	12,15,14,11,12,13,15,15,10,12,12,15,14,11,13,13,
	16,15,11,13,13,16,15,14,13,14,15,16,13,15,15,17,
	17,10,12,12,14,15,11,12,12,15,15,11,13,13,15,16,
	13,15,13,16,15,13,15,15,16,17, 5, 8, 8,11,11, 8,
	10,10,12,12, 8,10,10,12,12,11,12,12,14,14,11,12,
	12,14,14, 7,10,10,12,12,10,12,12,14,13, 9,11,12,
	12,13,12,13,13,15,15,12,12,13,13,15, 7,10,10,12,
	13,10,11,12,13,13,10,12,11,13,13,11,13,13,15,15,
	12,13,12,15,14, 9,12,12,15,14,11,13,13,15,15,11,
	12,13,15,15,13,14,14,17,19,13,13,14,16,16,10,12,
	12,14,15,11,13,13,15,16,11,13,12,16,15,13,15,15,
	17,18,14,15,13,16,15, 8,11,11,15,14,10,12,12,16,
	15,10,12,12,16,16,14,15,15,18,17,13,14,15,16,18,
	 9,12,12,15,15,11,12,14,16,17,11,13,13,16,15,15,
	15,15,17,18,14,15,16,17,17, 9,12,12,15,15,11,14,
	13,16,16,11,13,13,16,16,15,16,15,17,18,14,16,15,
	17,16,12,14,14,17,16,12,14,15,18,17,13,15,15,17,
	17,15,15,18,16,20,15,16,17,18,18,11,14,14,16,17,
	13,15,14,18,17,13,15,15,17,17,15,17,15,18,17,15,
	17,16,19,18, 8,11,11,14,15,10,12,12,15,15,10,12,
	12,16,16,13,14,14,17,16,14,15,15,17,17, 9,12,12,
	15,16,11,13,13,16,16,11,12,13,16,16,14,16,15,20,
	17,14,16,16,17,17, 9,12,12,15,16,11,13,13,16,17,
	11,13,13,17,16,14,15,15,17,18,15,15,15,18,18,11,
	14,14,17,16,13,15,15,17,17,13,14,14,18,17,15,16,
	16,18,19,15,15,17,17,19,11,14,14,16,17,13,15,14,
	17,19,13,15,14,18,17,15,17,16,18,18,15,17,15,18,
	16,
};

static float _vq_quantthresh__16u1__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16u1__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16u1__p3_0 = {
	_vq_quantthresh__16u1__p3_0,
	_vq_quantmap__16u1__p3_0,
	5,
	5
};

static static_codebook _16u1__p3_0 = {
	4, 625,
	_vq_lengthlist__16u1__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16u1__p3_0,
	NULL,
	&_vq_auxt__16u1__p3_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16u1__p4_0[] = {
	 4, 5, 5, 8, 8, 6, 6, 7, 9, 9, 6, 6, 6, 9, 9, 9,
	10, 9,11,11, 9, 9,10,11,11, 6, 7, 7,10, 9, 7, 7,
	 8, 9,10, 7, 7, 8,10,10,10,10,10,10,12, 9, 9,10,
	11,12, 6, 7, 7, 9, 9, 7, 8, 7,10,10, 7, 8, 7,10,
	10, 9,10, 9,12,11,10,10, 9,12,10, 9,10,10,12,11,
	10,10,10,12,12, 9,10,10,12,12,12,11,12,13,13,11,
	11,12,12,13, 9,10,10,11,12, 9,10,10,12,12,10,10,
	10,12,12,11,12,11,14,13,11,12,12,14,13, 5, 7, 7,
	10,10, 7, 8, 8,10,10, 7, 8, 7,10,10,10,10,10,12,
	12,10,10,10,12,12, 6, 8, 7,10,10, 7, 7, 9,10,11,
	 8, 9, 9,11,10,10,10,11,11,13,10,10,11,12,13, 6,
	 8, 8,10,10, 7, 9, 8,11,10, 8, 9, 9,10,11,10,11,
	10,13,11,10,11,10,12,12,10,11,10,12,11,10,10,10,
	12,13,10,11,11,13,12,11,11,13,11,14,12,12,13,14,
	14, 9,10,10,12,13,10,11,10,13,12,10,11,11,12,13,
	11,12,11,14,12,12,13,13,15,14, 5, 7, 7,10,10, 7,
	 7, 8,10,10, 7, 8, 8,10,10,10,10,10,11,12,10,10,
	10,12,12, 7, 8, 8,10,10, 8, 9, 8,11,10, 7, 8, 9,
	10,11,10,11,11,12,12,10,10,11,11,13, 7, 7, 8,10,
	10, 8, 8, 9,10,11, 7, 9, 7,11,10,10,11,11,13,12,
	11,11,10,13,11, 9,10,10,12,12,10,11,11,13,12,10,
	10,11,12,12,12,13,13,14,14,11,11,12,12,14,10,10,
	11,12,12,10,11,11,12,13,10,10,10,13,12,12,13,13,
	15,14,12,13,10,14,11, 8,10,10,12,12,10,11,10,13,
	13, 9,10,10,12,12,12,13,13,15,14,11,12,12,13,13,
	 9,10,10,13,12,10,10,11,13,13,10,11,10,13,12,12,
	12,13,14,15,12,13,12,15,13, 9,10,10,12,13,10,11,
	10,13,12,10,10,11,12,13,12,14,12,15,13,12,12,13,
	14,15,11,12,11,14,13,11,11,12,14,15,12,13,12,15,
	14,13,11,15,11,16,13,14,14,16,15,11,12,12,14,14,
	11,12,11,14,13,12,12,13,14,15,13,14,12,16,12,14,
	14,14,15,15, 8,10,10,12,12, 9,10,10,12,12,10,10,
	11,13,13,11,12,12,13,13,12,13,13,14,15, 9,10,10,
	13,12,10,11,11,13,12,10,10,11,13,13,12,13,12,15,
	14,12,12,13,13,16, 9, 9,10,12,13,10,10,11,12,13,
	10,11,10,13,13,12,12,13,13,15,13,13,12,15,13,11,
	12,12,14,14,12,13,12,15,14,11,11,12,13,14,14,14,
	14,16,15,13,12,15,12,16,11,11,12,13,14,12,13,13,
	14,15,10,12,11,14,13,14,15,14,16,16,13,14,11,15,
	11,
};

static float _vq_quantthresh__16u1__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16u1__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16u1__p4_0 = {
	_vq_quantthresh__16u1__p4_0,
	_vq_quantmap__16u1__p4_0,
	5,
	5
};

static static_codebook _16u1__p4_0 = {
	4, 625,
	_vq_lengthlist__16u1__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16u1__p4_0,
	NULL,
	&_vq_auxt__16u1__p4_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16u1__p5_0[] = {
	 1, 4, 4, 7, 7, 7, 7, 9, 9, 4, 6, 6, 8, 8, 8, 8,
	10,10, 4, 5, 6, 8, 8, 8, 8,10,10, 7, 8, 8, 9, 9,
	 9, 9,11,11, 7, 8, 8, 9, 9, 9, 9,11,11, 7, 8, 8,
	10, 9,11,11,12,11, 7, 8, 8, 9, 9,11,11,12,12, 9,
	10,10,11,11,12,12,13,12, 9,10,10,11,11,12,12,12,
	13,
};

static float _vq_quantthresh__16u1__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16u1__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16u1__p5_0 = {
	_vq_quantthresh__16u1__p5_0,
	_vq_quantmap__16u1__p5_0,
	9,
	9
};

static static_codebook _16u1__p5_0 = {
	2, 81,
	_vq_lengthlist__16u1__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16u1__p5_0,
	NULL,
	&_vq_auxt__16u1__p5_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p6_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16u1__p6_0[] = {
	 3, 4, 4, 6, 6, 7, 7, 9, 9, 4, 4, 4, 6, 6, 8, 8,
	 9, 9, 4, 4, 4, 6, 6, 7, 7, 9, 9, 6, 6, 6, 7, 7,
	 8, 8,10, 9, 6, 6, 6, 7, 7, 8, 8, 9,10, 7, 8, 7,
	 8, 8, 9, 9,10,10, 7, 8, 8, 8, 8, 9, 9,10,10, 9,
	 9, 9,10,10,10,10,11,11, 9, 9, 9,10,10,10,10,11,
	11,
};

static float _vq_quantthresh__16u1__p6_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16u1__p6_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16u1__p6_0 = {
	_vq_quantthresh__16u1__p6_0,
	_vq_quantmap__16u1__p6_0,
	9,
	9
};

static static_codebook _16u1__p6_0 = {
	2, 81,
	_vq_lengthlist__16u1__p6_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16u1__p6_0,
	NULL,
	&_vq_auxt__16u1__p6_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16u1__p7_0[] = {
	 1, 4, 4, 4, 8, 8, 4, 8, 8, 5,11, 9, 8,12,11, 8,
	12,11, 5,10,11, 8,11,12, 8,11,12, 4,11,11,11,14,
	13,10,13,13, 8,14,13,12,14,16,12,16,15, 8,14,14,
	13,16,14,12,15,16, 4,11,11,10,14,13,11,14,14, 8,
	15,14,12,15,15,12,14,16, 8,14,14,11,16,15,12,15,
	13,
};

static float _vq_quantthresh__16u1__p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__16u1__p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16u1__p7_0 = {
	_vq_quantthresh__16u1__p7_0,
	_vq_quantmap__16u1__p7_0,
	3,
	3
};

static static_codebook _16u1__p7_0 = {
	4, 81,
	_vq_lengthlist__16u1__p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__16u1__p7_0,
	NULL,
	&_vq_auxt__16u1__p7_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16u1__p7_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8, 4, 6, 5, 7, 7,
	 8, 8, 8, 8, 8, 8, 4, 5, 6, 7, 7, 8, 8, 8, 8, 8,
	 8, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 6, 7, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 7, 8, 8, 8, 8, 9, 9, 9,10,
	 9,10, 7, 8, 8, 8, 8, 9, 9, 9, 9,10, 9, 8, 8, 8,
	 9, 9,10,10,10,10,10,10, 8, 8, 8, 9, 9, 9, 9,10,
	10,10,10, 8, 8, 8, 9, 9, 9,10,10,10,10,10, 8, 8,
	 8, 9, 9,10,10,10,10,10,10,
};

static float _vq_quantthresh__16u1__p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__16u1__p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16u1__p7_1 = {
	_vq_quantthresh__16u1__p7_1,
	_vq_quantmap__16u1__p7_1,
	11,
	11
};

static static_codebook _16u1__p7_1 = {
	2, 121,
	_vq_lengthlist__16u1__p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__16u1__p7_1,
	NULL,
	&_vq_auxt__16u1__p7_1,
	NULL,
	0
};

static long _vq_quantlist__16u1__p8_0[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16u1__p8_0[] = {
	 1, 4, 4, 5, 5, 8, 8,10,10,12,12, 4, 7, 7, 8, 8,
	 9, 9,12,11,14,13, 4, 7, 7, 7, 8, 9,10,11,11,13,
	12, 5, 8, 8, 9, 9,11,11,12,13,15,14, 5, 7, 8, 9,
	 9,11,11,13,13,17,15, 8, 9,10,11,11,12,13,17,14,
	17,16, 8,10, 9,11,11,12,12,13,15,15,17,10,11,11,
	12,13,14,15,15,16,16,17, 9,11,11,12,12,14,15,17,
	15,15,16,11,14,12,14,15,16,15,16,16,16,15,11,13,
	13,14,14,15,15,16,16,15,16,
};

static float _vq_quantthresh__16u1__p8_0[] = {
	-49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5, 27.5,
	38.5, 49.5,
};

static long _vq_quantmap__16u1__p8_0[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16u1__p8_0 = {
	_vq_quantthresh__16u1__p8_0,
	_vq_quantmap__16u1__p8_0,
	11,
	11
};

static static_codebook _16u1__p8_0 = {
	2, 121,
	_vq_lengthlist__16u1__p8_0,
	1, -524582912, 1618345984, 4, 0,
	_vq_quantlist__16u1__p8_0,
	NULL,
	&_vq_auxt__16u1__p8_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p8_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16u1__p8_1[] = {
	 2, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 4, 6, 6, 7, 7,
	 8, 7, 8, 8, 8, 8, 4, 6, 6, 7, 7, 7, 7, 8, 8, 8,
	 8, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 6, 7, 7, 7,
	 7, 8, 8, 8, 8, 9, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 8, 8, 8,
	 8, 8, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 8, 8, 8, 9, 8, 9, 9, 9, 9, 9, 9, 8, 8,
	 8, 9, 9, 9, 9, 9, 9, 9, 9,
};

static float _vq_quantthresh__16u1__p8_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__16u1__p8_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16u1__p8_1 = {
	_vq_quantthresh__16u1__p8_1,
	_vq_quantmap__16u1__p8_1,
	11,
	11
};

static static_codebook _16u1__p8_1 = {
	2, 121,
	_vq_lengthlist__16u1__p8_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__16u1__p8_1,
	NULL,
	&_vq_auxt__16u1__p8_1,
	NULL,
	0
};

static long _vq_quantlist__16u1__p9_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__16u1__p9_0[] = {
	 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8,
};

static float _vq_quantthresh__16u1__p9_0[] = {
	-1657.5, -1402.5, -1147.5, -892.5, -637.5, -382.5, -127.5, 127.5,
	382.5, 637.5, 892.5, 1147.5, 1402.5, 1657.5,
};

static long _vq_quantmap__16u1__p9_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__16u1__p9_0 = {
	_vq_quantthresh__16u1__p9_0,
	_vq_quantmap__16u1__p9_0,
	15,
	15
};

static static_codebook _16u1__p9_0 = {
	2, 225,
	_vq_lengthlist__16u1__p9_0,
	1, -514071552, 1627381760, 4, 0,
	_vq_quantlist__16u1__p9_0,
	NULL,
	&_vq_auxt__16u1__p9_0,
	NULL,
	0
};

static long _vq_quantlist__16u1__p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__16u1__p9_1[] = {
	 1, 6, 5, 9, 9,10,10, 6, 7, 9, 9,10,10,10,10, 5,
	10, 8,10, 8,10,10, 8, 8,10, 9,10,10,10,10, 5, 8,
	 9,10,10,10,10, 8,10,10,10,10,10,10,10, 9,10,10,
	10,10,10,10, 9, 9,10,10,10,10,10,10, 9, 9, 8, 9,
	10,10,10, 9,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10, 8,10,10,10,10,
	10,10,10,10,10,10,10,10,10, 6, 8, 8,10,10,10, 8,
	10,10,10,10,10,10,10,10, 5, 8, 8,10,10,10, 9, 9,
	10,10,10,10,10,10,10,10, 9,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9,
};

static float _vq_quantthresh__16u1__p9_1[] = {
	-110.5, -93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5,
	25.5, 42.5, 59.5, 76.5, 93.5, 110.5,
};

static long _vq_quantmap__16u1__p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__16u1__p9_1 = {
	_vq_quantthresh__16u1__p9_1,
	_vq_quantmap__16u1__p9_1,
	15,
	15
};

static static_codebook _16u1__p9_1 = {
	2, 225,
	_vq_lengthlist__16u1__p9_1,
	1, -522338304, 1620115456, 4, 0,
	_vq_quantlist__16u1__p9_1,
	NULL,
	&_vq_auxt__16u1__p9_1,
	NULL,
	0
};

static long _vq_quantlist__16u1__p9_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__16u1__p9_2[] = {
	 1, 6, 6, 7, 8, 8,11,10, 9, 9,11, 9,10, 9,11,11,
	 9, 6, 7, 6,11, 8,11, 9,10,10,11, 9,11,10,10,10,
	11, 9, 5, 7, 7, 8, 8,10,11, 8, 8,11, 9, 9,10,11,
	 9,10,11, 8, 9, 6, 8, 8, 9, 9,10,10,11,11,11, 9,
	11,10, 9,11, 8, 8, 8, 9, 8, 9,10,11, 9, 9,11,11,
	10, 9, 9,11,10, 8,11, 8, 9, 8,11, 9,10, 9,10,11,
	11,10,10, 9,10,10, 8, 8, 9,10,10,10, 9,11, 9,10,
	11,11,11,11,10, 9,11, 9, 9,11,11,10, 8,11,11,11,
	 9,10,10,11,10,11,11, 9,11,10, 9,11,10,10,10,10,
	 9,11,10,11,10, 9, 9,10,11, 9, 8,10,11,11,10,10,
	11, 9,11,10,11,11,10,11, 9, 9, 8,10, 8, 9,11, 9,
	 8,10,10, 9,11,10,11,10,11, 9,11, 8,10,11,11,11,
	11,10,10,11,11,11,11,10,11,11,10, 9, 8,10,10, 9,
	11,10,11,11,11, 9, 9, 9,11,11,11,10,10, 9, 9,10,
	 9,11,11,11,11, 8,10,11,10,11,11,10,11,11, 9, 9,
	 9,10, 9,11, 9,11,11,11,11,11,10,11,11,10,11,10,
	11,11, 9,11,10,11,10, 9,10, 9,10,10,11,11,11,11,
	 9,10, 9,10,11,11,10,11,11,11,11,11,11,10,11,11,
	10,
};

static float _vq_quantthresh__16u1__p9_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__16u1__p9_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__16u1__p9_2 = {
	_vq_quantthresh__16u1__p9_2,
	_vq_quantmap__16u1__p9_2,
	17,
	17
};

static static_codebook _16u1__p9_2 = {
	2, 289,
	_vq_lengthlist__16u1__p9_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__16u1__p9_2,
	NULL,
	&_vq_auxt__16u1__p9_2,
	NULL,
	0
};

static long _huff_lengthlist__16u1__short[] = {
	 5, 7,10, 9,11,10,15,11,13,16, 6, 4, 6, 6, 7, 7,
	10, 9,12,16,10, 6, 5, 6, 6, 7,10,11,16,16, 9, 6,
	 7, 6, 7, 7,10, 8,14,16,11, 6, 5, 4, 5, 6, 8, 9,
	15,16, 9, 6, 6, 5, 6, 6, 9, 8,14,16,12, 7, 6, 6,
	 5, 6, 6, 7,13,16, 8, 6, 7, 6, 5, 5, 4, 4,11,16,
	 9, 8, 9, 9, 7, 7, 6, 5,13,16,14,14,16,15,16,15,
	16,16,16,16,
};

static static_codebook _huff_book__16u1__short = {
	2, 100,
	_huff_lengthlist__16u1__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__16u2__long[] = {
	 5, 7,10,10,10,11,11,13,18,19, 6, 5, 5, 6, 7, 8,
	 9,12,19,19, 8, 5, 4, 4, 6, 7, 9,13,19,19, 8, 5,
	 4, 4, 5, 6, 8,12,17,19, 7, 5, 5, 4, 4, 5, 7,12,
	18,18, 8, 7, 7, 6, 5, 5, 6,10,18,18, 9, 9, 9, 8,
	 6, 5, 6, 9,18,18,11,13,13,13, 8, 7, 7, 9,16,18,
	13,17,18,16,11, 9, 9, 9,17,18,15,18,18,18,15,13,
	13,14,18,18,
};

static static_codebook _huff_book__16u2__long = {
	2, 100,
	_huff_lengthlist__16u2__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__16u2__short[] = {
	 8,11,12,12,14,15,16,16,16,16, 9, 7, 7, 8, 9,11,
	13,14,16,16,13, 7, 6, 6, 7, 9,12,13,15,16,15, 7,
	 6, 5, 4, 6,10,11,14,16,12, 8, 7, 4, 2, 4, 7,10,
	14,16,11, 9, 7, 5, 3, 4, 6, 9,14,16,11,10, 9, 7,
	 5, 5, 6, 9,16,16,10,10, 9, 8, 6, 6, 7,10,16,16,
	11,11,11,10,10,10,11,14,16,16,16,14,14,13,14,16,
	16,16,16,16,
};

static static_codebook _huff_book__16u2__short = {
	2, 100,
	_huff_lengthlist__16u2__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__16u2_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16u2_p1_0[] = {
	 1, 5, 5, 5, 7, 7, 5, 7, 7, 5, 7, 7, 7, 9, 9, 7,
	 9, 9, 5, 7, 7, 7, 9, 9, 7, 9, 9, 5, 7, 7, 8, 9,
	 9, 7, 9, 9, 7, 9, 9, 9,10,10, 9,10,10, 7, 9, 9,
	 9,10,10, 9,10,11, 5, 7, 8, 8, 9, 9, 8, 9, 9, 7,
	 9, 9, 9,10,10, 9, 9,10, 7, 9, 9, 9,10,10, 9,11,
	10,
};

static float _vq_quantthresh__16u2_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__16u2_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16u2_p1_0 = {
	_vq_quantthresh__16u2_p1_0,
	_vq_quantmap__16u2_p1_0,
	3,
	3
};

static static_codebook _16u2_p1_0 = {
	4, 81,
	_vq_lengthlist__16u2_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__16u2_p1_0,
	NULL,
	&_vq_auxt__16u2_p1_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16u2_p2_0[] = {
	 3, 5, 5, 8, 8, 5, 7, 7, 9, 9, 5, 7, 7, 9, 9, 9,
	10, 9,11,11, 9, 9, 9,11,11, 5, 7, 7, 9, 9, 7, 8,
	 8,10,10, 7, 8, 8,10,10,10,10,10,12,12, 9,10,10,
	11,12, 5, 7, 7, 9, 9, 7, 8, 8,10,10, 7, 8, 8,10,
	10, 9,10,10,12,11,10,10,10,12,12, 9,10,10,12,12,
	10,11,10,13,12, 9,10,10,12,12,12,12,12,14,14,11,
	12,12,13,14, 9,10,10,12,12, 9,10,10,12,12,10,10,
	10,12,12,11,12,12,14,13,12,13,12,14,14, 5, 7, 7,
	 9, 9, 7, 8, 8,10,10, 7, 8, 8,10,10,10,11,10,12,
	12,10,10,11,12,12, 7, 8, 8,10,10, 8, 9, 9,11,11,
	 8, 9, 9,11,11,11,11,11,12,13,10,11,11,12,13, 7,
	 8, 8,10,10, 8, 9, 8,11,10, 8, 9, 9,11,11,10,11,
	10,13,12,10,11,11,13,13, 9,11,10,13,13,10,11,11,
	13,13,10,11,11,13,13,12,12,13,13,15,12,12,13,14,
	15, 9,10,10,12,12,10,11,10,13,12,10,11,11,13,13,
	11,13,11,14,13,12,13,13,15,15, 5, 7, 7, 9, 9, 7,
	 8, 8,10,10, 7, 8, 8,10,10,10,10,10,12,12,10,10,
	11,12,12, 7, 8, 8,10,10, 8, 9, 9,11,11, 8, 8, 9,
	10,11,10,11,11,13,13,10,10,11,12,13, 7, 8, 8,10,
	11, 8, 9, 9,11,11, 8, 9, 9,11,11,10,11,11,13,12,
	11,11,11,13,12, 9,10,10,12,12,10,11,11,13,13,10,
	10,11,12,13,12,13,13,15,14,11,11,13,12,14,10,10,
	11,13,13,10,11,11,13,13,10,11,11,13,13,12,13,13,
	14,14,12,13,12,14,13, 8,10, 9,12,12, 9,11,10,13,
	13, 9,10,10,12,13,12,13,13,14,14,12,12,13,14,14,
	 9,11,10,13,13,10,11,11,13,13,10,11,11,13,13,12,
	13,13,15,15,13,13,13,14,15, 9,10,10,12,13,10,11,
	10,13,12,10,11,11,13,13,12,13,12,15,14,13,13,13,
	14,15,11,12,12,15,14,12,12,13,15,15,12,13,13,15,
	14,14,13,15,14,16,13,14,15,16,16,11,12,12,14,14,
	11,12,12,15,14,12,13,13,15,15,13,14,13,16,14,14,
	14,14,16,16, 8, 9, 9,12,12, 9,10,10,13,12, 9,10,
	10,13,13,12,12,12,14,14,12,12,13,15,15, 9,10,10,
	13,12,10,11,11,13,13,10,10,11,13,14,12,13,13,15,
	15,12,12,13,14,15, 9,10,10,13,13,10,11,11,13,13,
	10,11,11,13,13,12,13,13,14,14,13,14,13,15,14,11,
	12,12,14,14,12,13,13,15,14,11,12,12,14,15,14,14,
	14,16,15,13,12,14,14,16,11,12,13,14,15,12,13,13,
	14,16,12,13,12,15,14,13,15,14,16,16,14,15,13,16,
	13,
};

static float _vq_quantthresh__16u2_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16u2_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16u2_p2_0 = {
	_vq_quantthresh__16u2_p2_0,
	_vq_quantmap__16u2_p2_0,
	5,
	5
};

static static_codebook _16u2_p2_0 = {
	4, 625,
	_vq_lengthlist__16u2_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16u2_p2_0,
	NULL,
	&_vq_auxt__16u2_p2_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__16u2_p3_0[] = {
	 2, 4, 4, 6, 6, 7, 7, 9, 9, 4, 5, 5, 6, 6, 8, 7,
	 9, 9, 4, 5, 5, 6, 6, 7, 8, 9, 9, 6, 6, 6, 7, 7,
	 8, 8,10,10, 6, 6, 6, 7, 7, 8, 8, 9,10, 7, 8, 7,
	 8, 8, 9, 9,10,10, 7, 8, 8, 8, 8, 9, 9,10,10, 9,
	 9, 9,10, 9,10,10,11,11, 9, 9, 9,10,10,10,10,11,
	11,
};

static float _vq_quantthresh__16u2_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__16u2_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__16u2_p3_0 = {
	_vq_quantthresh__16u2_p3_0,
	_vq_quantmap__16u2_p3_0,
	9,
	9
};

static static_codebook _16u2_p3_0 = {
	2, 81,
	_vq_lengthlist__16u2_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__16u2_p3_0,
	NULL,
	&_vq_auxt__16u2_p3_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p4_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__16u2_p4_0[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,11,11,
	11, 5, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,11,
	12,11, 5, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9,10,10,11,
	11,12,12, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,12,12, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,12, 7, 8, 8, 8, 8, 9, 9, 9, 9,10,10,
	11,11,12,12,12,12, 7, 8, 8, 8, 8, 9, 9, 9, 9,10,
	10,11,11,11,12,12,12, 9, 9, 9, 9, 9, 9,10,10,10,
	10,10,11,11,12,12,13,13, 8, 9, 9, 9, 9,10, 9,10,
	10,10,10,11,11,12,12,13,13, 9, 9, 9, 9, 9,10,10,
	10,10,11,11,11,12,12,12,13,13, 9, 9, 9, 9, 9,10,
	10,10,10,11,11,12,11,12,12,13,13,10,10,10,10,10,
	11,11,11,11,11,12,12,12,12,13,13,14,10,10,10,10,
	10,11,11,11,11,12,11,12,12,13,12,13,13,11,11,11,
	11,11,12,12,12,12,12,12,13,13,13,13,14,14,11,11,
	11,11,11,12,12,12,12,12,12,13,12,13,13,14,14,11,
	12,12,12,12,12,12,13,13,13,13,13,13,14,14,14,14,
	11,12,12,12,12,12,12,13,13,13,13,14,13,14,14,14,
	14,
};

static float _vq_quantthresh__16u2_p4_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__16u2_p4_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__16u2_p4_0 = {
	_vq_quantthresh__16u2_p4_0,
	_vq_quantmap__16u2_p4_0,
	17,
	17
};

static static_codebook _16u2_p4_0 = {
	2, 289,
	_vq_lengthlist__16u2_p4_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__16u2_p4_0,
	NULL,
	&_vq_auxt__16u2_p4_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p5_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__16u2_p5_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 9, 8, 7,10, 9, 7,
	10, 9, 5, 8, 9, 7, 9,10, 7, 9,10, 4, 9, 9, 9,11,
	11, 8,11,11, 7,11,11,10,10,13,10,14,13, 7,11,11,
	10,13,11,10,13,14, 5, 9, 9, 8,11,11, 9,11,11, 7,
	11,11,10,14,13,10,12,14, 7,11,11,10,13,13,10,13,
	10,
};

static float _vq_quantthresh__16u2_p5_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__16u2_p5_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__16u2_p5_0 = {
	_vq_quantthresh__16u2_p5_0,
	_vq_quantmap__16u2_p5_0,
	3,
	3
};

static static_codebook _16u2_p5_0 = {
	4, 81,
	_vq_lengthlist__16u2_p5_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__16u2_p5_0,
	NULL,
	&_vq_auxt__16u2_p5_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p5_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16u2_p5_1[] = {
	 2, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 5, 5, 5, 7, 7,
	 7, 7, 8, 8, 8, 8, 5, 5, 6, 7, 7, 7, 7, 8, 8, 8,
	 8, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 6, 7, 7, 7,
	 7, 8, 8, 8, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 8, 8, 8,
	 8, 8, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 8, 8,
	 8, 8, 8, 9, 9, 9, 9, 9, 9,
};

static float _vq_quantthresh__16u2_p5_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__16u2_p5_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16u2_p5_1 = {
	_vq_quantthresh__16u2_p5_1,
	_vq_quantmap__16u2_p5_1,
	11,
	11
};

static static_codebook _16u2_p5_1 = {
	2, 121,
	_vq_lengthlist__16u2_p5_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__16u2_p5_1,
	NULL,
	&_vq_auxt__16u2_p5_1,
	NULL,
	0
};

static long _vq_quantlist__16u2_p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16u2_p6_0[] = {
	 1, 4, 4, 7, 7, 8, 8, 8, 8, 9, 9,10,10, 4, 6, 6,
	 8, 8, 9, 9, 9, 9,10,10,12,11, 4, 6, 6, 8, 8, 9,
	 9, 9, 9,10,10,11,12, 7, 8, 8, 9, 9,10,10,10,10,
	12,12,13,12, 7, 8, 8, 9, 9,10,10,10,10,11,12,12,
	12, 8, 9, 9,10,10,11,11,11,11,12,12,13,13, 8, 9,
	 9,10,10,11,11,11,11,12,13,13,13, 8, 9, 9,10,10,
	11,11,12,12,13,13,14,14, 8, 9, 9,10,10,11,11,12,
	12,13,13,14,14, 9,10,10,11,12,13,12,13,14,14,14,
	14,14, 9,10,10,11,12,12,13,13,13,14,14,14,14,10,
	11,11,12,12,13,13,14,14,15,15,15,15,10,11,11,12,
	12,13,13,14,14,14,14,15,15,
};

static float _vq_quantthresh__16u2_p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__16u2_p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16u2_p6_0 = {
	_vq_quantthresh__16u2_p6_0,
	_vq_quantmap__16u2_p6_0,
	13,
	13
};

static static_codebook _16u2_p6_0 = {
	2, 169,
	_vq_lengthlist__16u2_p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__16u2_p6_0,
	NULL,
	&_vq_auxt__16u2_p6_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__16u2_p6_1[] = {
	 2, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
	 5, 5, 6, 6, 5, 5, 5, 6, 6,
};

static float _vq_quantthresh__16u2_p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__16u2_p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__16u2_p6_1 = {
	_vq_quantthresh__16u2_p6_1,
	_vq_quantmap__16u2_p6_1,
	5,
	5
};

static static_codebook _16u2_p6_1 = {
	2, 25,
	_vq_lengthlist__16u2_p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__16u2_p6_1,
	NULL,
	&_vq_auxt__16u2_p6_1,
	NULL,
	0
};

static long _vq_quantlist__16u2_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__16u2_p7_0[] = {
	 1, 4, 4, 7, 7, 7, 7, 8, 8, 9, 9,10,10, 4, 6, 6,
	 9, 9, 9, 9, 9, 9,10,10,11,11, 4, 6, 6, 8, 9, 9,
	 9, 9, 9,10,11,12,11, 7, 8, 9,10,10,10,10,11,10,
	11,12,12,13, 7, 9, 9,10,10,10,10,10,10,11,12,13,
	13, 7, 9, 8,10,10,11,11,11,12,12,13,13,14, 7, 9,
	 9,10,10,11,11,11,12,13,13,13,13, 8, 9, 9,10,11,
	11,12,12,12,13,13,13,13, 8, 9, 9,10,11,11,11,12,
	12,13,13,14,14, 9,10,10,12,11,12,13,13,13,14,13,
	13,13, 9,10,10,11,11,12,12,13,14,13,13,14,13,10,
	11,11,12,13,14,14,14,15,14,14,14,14,10,11,11,12,
	12,13,13,13,14,14,14,15,14,
};

static float _vq_quantthresh__16u2_p7_0[] = {
	-60.5, -49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5,
	27.5, 38.5, 49.5, 60.5,
};

static long _vq_quantmap__16u2_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16u2_p7_0 = {
	_vq_quantthresh__16u2_p7_0,
	_vq_quantmap__16u2_p7_0,
	13,
	13
};

static static_codebook _16u2_p7_0 = {
	2, 169,
	_vq_lengthlist__16u2_p7_0,
	1, -523206656, 1618345984, 4, 0,
	_vq_quantlist__16u2_p7_0,
	NULL,
	&_vq_auxt__16u2_p7_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__16u2_p7_1[] = {
	 3, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 5, 6, 6, 7, 7,
	 7, 7, 7, 7, 8, 8, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8,
	 8, 6, 6, 7, 7, 7, 8, 7, 8, 8, 8, 8, 6, 7, 7, 7,
	 7, 7, 7, 8, 8, 8, 8, 7, 7, 7, 7, 7, 8, 8, 8, 8,
	 8, 8, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7,
	 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 8,
	 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__16u2_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__16u2_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__16u2_p7_1 = {
	_vq_quantthresh__16u2_p7_1,
	_vq_quantmap__16u2_p7_1,
	11,
	11
};

static static_codebook _16u2_p7_1 = {
	2, 121,
	_vq_lengthlist__16u2_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__16u2_p7_1,
	NULL,
	&_vq_auxt__16u2_p7_1,
	NULL,
	0
};

static long _vq_quantlist__16u2_p8_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__16u2_p8_0[] = {
	 1, 5, 5, 7, 7, 8, 8, 7, 7, 8, 8,10, 9,11,11, 4,
	 6, 6, 8, 8,10, 9, 9, 8, 9, 9,10,10,12,14, 4, 6,
	 7, 8, 9, 9,10, 9, 8, 9, 9,10,12,12,11, 7, 8, 8,
	10,10,10,10, 9, 9,10,10,11,13,13,12, 7, 8, 8, 9,
	11,11,10, 9, 9,11,10,12,11,11,14, 8, 9, 9,11,10,
	11,11,10,10,11,11,13,12,14,12, 8, 9, 9,11,12,11,
	11,10,10,12,11,12,12,12,14, 7, 8, 8, 9, 9,10,10,
	10,11,12,11,13,13,14,12, 7, 8, 9, 9, 9,10,10,11,
	11,11,12,12,14,14,14, 8,10, 9,10,11,11,11,11,14,
	12,12,13,14,14,13, 9, 9, 9,10,11,11,11,12,12,12,
	14,12,14,13,14,10,10,10,12,11,12,11,14,13,14,13,
	14,14,13,14, 9,10,10,11,12,11,13,12,13,13,14,14,
	14,13,14,10,13,13,12,12,11,12,14,13,14,13,14,12,
	14,13,10,11,11,12,11,12,12,14,14,14,13,14,14,14,
	14,
};

static float _vq_quantthresh__16u2_p8_0[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__16u2_p8_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__16u2_p8_0 = {
	_vq_quantthresh__16u2_p8_0,
	_vq_quantmap__16u2_p8_0,
	15,
	15
};

static static_codebook _16u2_p8_0 = {
	2, 225,
	_vq_lengthlist__16u2_p8_0,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__16u2_p8_0,
	NULL,
	&_vq_auxt__16u2_p8_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p8_1[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__16u2_p8_1[] = {
	 2, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9,10, 9,10, 9, 9,
	 9,10,10,10,10, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,
	10, 9,10,10,10,10,10,10,11,10, 5, 6, 6, 7, 7, 8,
	 8, 8, 9, 9,10,10,10,10,10,10,10,10,10,10,10, 7,
	 7, 7, 8, 8, 9, 8, 9, 9,10, 9,10,10,10,10,10,10,
	11,10,11,10, 7, 7, 7, 8, 8, 8, 9, 9, 9,10, 9,10,
	10,10,10,10,10,10,10,10,10, 8, 8, 8, 9, 9, 9, 9,
	10, 9,10,10,10,10,10,10,10,11,10,10,11,10, 8, 8,
	 8, 8, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,11,
	11,10,10, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,
	11,10,11,10,11,10,11,10, 8, 9, 9, 9, 9, 9,10,10,
	10,10,10,10,10,10,10,10,11,11,10,10,10, 9,10, 9,
	 9,10,10,10,11,10,10,10,10,10,10,10,10,11,11,11,
	11,11, 9, 9, 9,10, 9,10,10,10,10,10,10,11,10,11,
	10,11,11,11,11,10,10, 9,10, 9,10,10,10,10,11,10,
	10,10,10,10,11,10,11,10,11,10,10,11, 9,10,10,10,
	10,10,10,10,10,10,11,10,10,11,11,10,11,11,11,11,
	11, 9, 9,10,10,10,10,10,11,10,10,11,10,10,11,10,
	10,11,11,11,11,11, 9,10,10,10,10,10,10,10,11,10,
	11,10,11,10,11,11,11,11,11,10,11,10,10,10,10,10,
	10,10,10,10,11,11,11,11,11,11,11,11,11,10,11,11,
	10,10,10,10,10,11,10,10,10,11,10,11,11,11,11,10,
	12,11,11,11,10,10,10,10,10,10,11,10,10,10,11,11,
	12,11,11,11,11,11,11,11,11,11,10,10,10,11,10,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,10,
	10,10,11,10,11,10,10,11,11,11,11,11,11,11,11,11,
	11,11,11,10,10,10,10,10,10,10,11,11,10,11,11,10,
	11,11,10,11,11,11,10,11,11,
};

static float _vq_quantthresh__16u2_p8_1[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__16u2_p8_1[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__16u2_p8_1 = {
	_vq_quantthresh__16u2_p8_1,
	_vq_quantmap__16u2_p8_1,
	21,
	21
};

static static_codebook _16u2_p8_1 = {
	2, 441,
	_vq_lengthlist__16u2_p8_1,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__16u2_p8_1,
	NULL,
	&_vq_auxt__16u2_p8_1,
	NULL,
	0
};

static long _vq_quantlist__16u2_p9_0[] = {
	5586,
	4655,
	6517,
	3724,
	7448,
	2793,
	8379,
	1862,
	9310,
	931,
	10241,
	0,
	11172,
	5521,
	5651,
};

static long _vq_lengthlist__16u2_p9_0[] = {
	 1,10,10,10,10,10,10,10,10,10,10,10,10, 5, 4,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10, 4,10,10,10,10,10,10,10,10,10,10,10,10,
	 6, 6, 5,10,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9, 5,
	 5,
};

static float _vq_quantthresh__16u2_p9_0[] = {
	-5120.5, -4189.5, -3258.5, -2327.5, -1396.5, -498, -32.5, 32.5,
	498, 1396.5, 2327.5, 3258.5, 4189.5, 5120.5,
};

static long _vq_quantmap__16u2_p9_0[] = {
	   11,	9,	7,	5,	3,	1,   13,	0,
	   14,	2,	4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__16u2_p9_0 = {
	_vq_quantthresh__16u2_p9_0,
	_vq_quantmap__16u2_p9_0,
	15,
	15
};

static static_codebook _16u2_p9_0 = {
	2, 225,
	_vq_lengthlist__16u2_p9_0,
	1, -510275072, 1611661312, 14, 0,
	_vq_quantlist__16u2_p9_0,
	NULL,
	&_vq_auxt__16u2_p9_0,
	NULL,
	0
};

static long _vq_quantlist__16u2_p9_1[] = {
	392,
	343,
	441,
	294,
	490,
	245,
	539,
	196,
	588,
	147,
	637,
	98,
	686,
	49,
	735,
	0,
	784,
	388,
	396,
};

static long _vq_lengthlist__16u2_p9_1[] = {
	 1,12,10,12,10,12,10,12,11,12,12,12,12,12,12,12,
	12, 5, 5, 9,10,12,11,11,12,12,12,12,12,12,12,12,
	12,12,12,12,10, 9, 9,11, 9,11,11,12,11,12,12,12,
	12,12,12,12,12,12,12, 8, 8,10,11, 9,12,11,12,12,
	12,12,12,12,12,12,12,12,12,12, 9, 8,10,11,12,11,
	12,11,12,12,12,12,12,12,12,12,12,12,12, 8, 9,11,
	11,10,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	 9,10,11,12,11,12,11,12,12,12,12,12,12,12,12,12,
	12,12,12, 9, 9,11,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11, 5, 8, 9, 9, 8,11, 9,11,11,11,11,11,11,
	11,11,11,11, 5, 5, 4, 8, 8, 8, 8,10, 9,10,10,11,
	11,11,11,11,11,11,11, 5, 4,
};

static float _vq_quantthresh__16u2_p9_1[] = {
	-367.5, -318.5, -269.5, -220.5, -171.5, -122.5, -73.5, -26.5,
	-2, 2, 26.5, 73.5, 122.5, 171.5, 220.5, 269.5,
	318.5, 367.5,
};

static long _vq_quantmap__16u2_p9_1[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
	   17,	0,   18,	2,	4,	6,	8,   10,
	   12,   14,   16,
};

static encode_aux_threshmatch _vq_auxt__16u2_p9_1 = {
	_vq_quantthresh__16u2_p9_1,
	_vq_quantmap__16u2_p9_1,
	19,
	19
};

static static_codebook _16u2_p9_1 = {
	2, 361,
	_vq_lengthlist__16u2_p9_1,
	1, -518488064, 1611661312, 10, 0,
	_vq_quantlist__16u2_p9_1,
	NULL,
	&_vq_auxt__16u2_p9_1,
	NULL,
	0
};

static long _vq_quantlist__16u2_p9_2[] = {
	24,
	23,
	25,
	22,
	26,
	21,
	27,
	20,
	28,
	19,
	29,
	18,
	30,
	17,
	31,
	16,
	32,
	15,
	33,
	14,
	34,
	13,
	35,
	12,
	36,
	11,
	37,
	10,
	38,
	9,
	39,
	8,
	40,
	7,
	41,
	6,
	42,
	5,
	43,
	4,
	44,
	3,
	45,
	2,
	46,
	1,
	47,
	0,
	48,
};

static long _vq_lengthlist__16u2_p9_2[] = {
	 1, 3, 3, 4, 7, 7, 7, 8, 7, 7, 7, 7, 8, 8, 8, 8,
	 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 9, 9, 8, 9, 9,
	 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,12,12,10,
	11,
};

static float _vq_quantthresh__16u2_p9_2[] = {
	-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5,
	-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
	8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
	16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5,
};

static long _vq_quantmap__16u2_p9_2[] = {
	   47,   45,   43,   41,   39,   37,   35,   33,
	   31,   29,   27,   25,   23,   21,   19,   17,
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,   18,   20,   22,   24,   26,   28,   30,
	   32,   34,   36,   38,   40,   42,   44,   46,
	   48,
};

static encode_aux_threshmatch _vq_auxt__16u2_p9_2 = {
	_vq_quantthresh__16u2_p9_2,
	_vq_quantmap__16u2_p9_2,
	49,
	49
};

static static_codebook _16u2_p9_2 = {
	1, 49,
	_vq_lengthlist__16u2_p9_2,
	1, -526909440, 1611661312, 6, 0,
	_vq_quantlist__16u2_p9_2,
	NULL,
	&_vq_auxt__16u2_p9_2,
	NULL,
	0
};

static long _vq_quantlist__8u0__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8u0__p1_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 8, 8, 8,10,10, 7,
	10,10, 5, 8, 8, 7,10,10, 8,10,10, 4, 9, 8, 8,11,
	11, 8,11,11, 7,11,11,10,11,13,10,13,13, 7,11,11,
	10,13,12,10,13,13, 5, 9, 8, 8,11,11, 8,11,11, 7,
	11,11, 9,13,13,10,12,13, 7,11,11,10,13,13,10,13,
	11,
};

static float _vq_quantthresh__8u0__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__8u0__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8u0__p1_0 = {
	_vq_quantthresh__8u0__p1_0,
	_vq_quantmap__8u0__p1_0,
	3,
	3
};

static static_codebook _8u0__p1_0 = {
	4, 81,
	_vq_lengthlist__8u0__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__8u0__p1_0,
	NULL,
	&_vq_auxt__8u0__p1_0,
	NULL,
	0
};

static long _vq_quantlist__8u0__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8u0__p2_0[] = {
	 2, 4, 4, 5, 6, 6, 5, 6, 6, 5, 7, 7, 6, 7, 8, 6,
	 7, 8, 5, 7, 7, 6, 8, 8, 7, 9, 7, 5, 7, 7, 7, 9,
	 9, 7, 8, 8, 6, 9, 8, 7, 7,10, 8,10,10, 6, 8, 8,
	 8,10, 8, 8,10,10, 5, 7, 7, 7, 8, 8, 7, 8, 9, 6,
	 8, 8, 8,10,10, 8, 8,10, 6, 8, 9, 8,10,10, 7,10,
	 8,
};

static float _vq_quantthresh__8u0__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__8u0__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8u0__p2_0 = {
	_vq_quantthresh__8u0__p2_0,
	_vq_quantmap__8u0__p2_0,
	3,
	3
};

static static_codebook _8u0__p2_0 = {
	4, 81,
	_vq_lengthlist__8u0__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__8u0__p2_0,
	NULL,
	&_vq_auxt__8u0__p2_0,
	NULL,
	0
};

static long _vq_quantlist__8u0__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8u0__p3_0[] = {
	 1, 5, 5, 7, 7, 6, 7, 7, 9, 9, 6, 7, 7, 9, 9, 8,
	10, 9,11,11, 8, 9, 9,11,11, 6, 8, 8,10,10, 8,10,
	10,11,11, 8,10,10,11,11,10,11,11,12,12,10,11,11,
	12,13, 6, 8, 8,10,10, 8,10,10,11,11, 8,10,10,11,
	11, 9,10,11,12,12,10,11,11,12,12, 8,11,11,14,13,
	10,12,11,15,13,10,12,11,14,14,12,13,12,16,14,12,
	14,12,16,15, 8,11,11,13,14,10,11,12,13,15,10,11,
	12,13,15,11,12,13,14,15,12,12,14,14,16, 5, 8, 8,
	11,11, 9,11,11,12,12, 8,10,11,12,12,11,12,12,15,
	14,11,12,12,14,14, 7,11,10,13,12,10,11,12,13,14,
	10,12,12,14,13,12,13,13,14,15,12,13,13,15,15, 7,
	10,11,12,13,10,12,11,14,13,10,12,13,13,15,12,13,
	12,14,14,11,13,13,15,16, 9,12,12,15,14,11,13,13,
	15,16,11,13,13,16,16,13,14,15,15,15,12,14,15,17,
	16, 9,12,12,14,15,11,13,13,15,16,11,13,13,16,18,
	13,14,14,17,16,13,15,15,17,18, 5, 8, 9,11,11, 8,
	11,11,12,12, 8,10,11,12,12,11,12,12,14,14,11,12,
	12,14,15, 7,11,10,12,13,10,12,12,14,13,10,11,12,
	13,14,11,13,13,15,14,12,13,13,14,15, 7,10,11,13,
	13,10,12,12,13,14,10,12,12,13,13,11,13,13,16,16,
	12,13,13,15,14, 9,12,12,16,15,10,13,13,15,15,11,
	13,13,17,15,12,15,15,18,17,13,14,14,15,16, 9,12,
	12,15,15,11,13,13,15,16,11,13,13,15,15,12,15,15,
	16,16,13,15,14,17,15, 7,11,11,15,15,10,13,13,16,
	15,10,13,13,15,16,14,15,15,17,19,13,15,14,15,18,
	 9,12,12,16,16,11,13,14,17,16,11,13,13,17,16,15,
	15,16,17,19,13,15,16, 0,18, 9,12,12,16,15,11,14,
	13,17,17,11,13,14,16,16,15,16,16,19,18,13,15,15,
	17,19,11,14,14,19,16,12,14,15, 0,18,12,16,15,18,
	17,15,15,18,16,19,14,15,17,19,19,11,14,14,18,19,
	13,15,14,19,19,12,16,15,18,17,15,17,15, 0,16,14,
	17,16,19, 0, 7,11,11,14,14,10,12,12,15,15,10,13,
	13,16,15,13,15,15,17, 0,14,15,15,16,19, 9,12,12,
	16,16,11,14,14,16,16,11,13,13,16,16,14,17,16,19,
	 0,14,18,17,17,19, 9,12,12,15,16,11,13,13,15,17,
	12,14,13,19,16,13,15,15,17,19,15,17,16,17,19,11,
	14,14,19,16,12,15,15,19,17,13,14,15,17,19,14,16,
	17,19,19,16,15,16,17,19,11,15,14,16,16,12,15,15,
	19, 0,12,14,15,19,19,14,16,16, 0,18,15,19,14,18,
	16,
};

static float _vq_quantthresh__8u0__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8u0__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8u0__p3_0 = {
	_vq_quantthresh__8u0__p3_0,
	_vq_quantmap__8u0__p3_0,
	5,
	5
};

static static_codebook _8u0__p3_0 = {
	4, 625,
	_vq_lengthlist__8u0__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8u0__p3_0,
	NULL,
	&_vq_auxt__8u0__p3_0,
	NULL,
	0
};

static long _vq_quantlist__8u0__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8u0__p4_0[] = {
	 3, 5, 5, 8, 8, 5, 6, 7, 9, 9, 6, 7, 6, 9, 9, 9,
	 9, 9,10,11, 9, 9, 9,11,10, 6, 7, 7,10,10, 7, 7,
	 8,10,10, 7, 8, 8,10,10,10,10,10,10,11, 9,10,10,
	11,12, 6, 7, 7,10,10, 7, 8, 8,10,10, 7, 8, 7,10,
	10, 9,10,10,12,11,10,10,10,11,10, 9,10,10,12,11,
	10,10,10,13,11, 9,10,10,12,12,11,11,12,12,13,11,
	11,11,12,13, 9,10,10,12,12,10,10,11,12,12,10,10,
	11,12,12,11,11,11,13,13,11,12,12,13,13, 5, 7, 7,
	10,10, 7, 8, 8,10,10, 7, 8, 8,10,10,10,11,11,12,
	12,10,11,10,12,12, 7, 8, 8,11,11, 7, 8, 9,10,11,
	 8, 9, 9,11,11,11,10,11,10,12,10,11,11,12,13, 7,
	 8, 8,10,11, 8, 9, 8,12,10, 8, 9, 9,11,12,10,11,
	10,13,11,10,11,11,13,12, 9,11,10,13,12,10,10,11,
	12,12,10,11,11,13,13,12,10,13,11,14,11,12,12,15,
	13, 9,11,11,13,13,10,11,11,13,12,10,11,11,12,14,
	12,13,11,14,12,12,12,12,14,14, 5, 7, 7,10,10, 7,
	 8, 8,10,10, 7, 8, 8,11,10,10,11,11,12,12,10,11,
	10,12,12, 7, 8, 8,10,11, 8, 9, 9,12,11, 8, 8, 9,
	10,11,10,11,11,12,13,11,10,11,11,13, 6, 8, 8,10,
	11, 8, 9, 9,11,11, 7, 9, 7,11,10,10,11,11,12,12,
	10,11,10,13,10, 9,11,10,13,12,10,12,11,13,13,10,
	10,11,12,13,11,12,13,15,14,11,11,13,12,13, 9,10,
	11,12,13,10,11,11,12,13,10,11,10,13,12,12,13,13,
	13,14,12,12,11,14,11, 8,10,10,12,13,10,11,11,13,
	13,10,11,10,13,13,12,13,14,15,14,12,12,12,14,13,
	 9,10,10,13,12,10,10,12,13,13,10,11,11,15,12,12,
	12,13,15,14,12,13,13,15,13, 9,10,11,12,13,10,12,
	10,13,12,10,11,11,12,13,12,14,12,15,13,12,12,12,
	15,14,11,12,11,14,13,11,11,12,14,14,12,13,13,14,
	13,13,11,15,11,15,14,14,14,16,15,11,12,12,13,14,
	11,13,11,14,14,12,12,13,14,15,12,14,12,15,12,13,
	15,14,16,15, 8,10,10,12,12,10,10,10,12,13,10,11,
	11,13,13,12,12,12,13,14,13,13,13,15,15, 9,10,10,
	12,12,10,11,11,13,12,10,10,11,13,13,12,12,12,14,
	14,12,12,13,15,14, 9,10,10,13,12,10,10,12,12,13,
	10,11,10,13,13,12,13,13,14,14,12,13,12,14,13,11,
	12,12,14,13,12,13,12,14,14,10,12,12,14,14,14,14,
	14,16,14,13,12,14,12,15,10,12,12,14,15,12,13,13,
	14,16,11,12,11,15,14,13,14,14,14,15,13,14,11,14,
	12,
};

static float _vq_quantthresh__8u0__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8u0__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8u0__p4_0 = {
	_vq_quantthresh__8u0__p4_0,
	_vq_quantmap__8u0__p4_0,
	5,
	5
};

static static_codebook _8u0__p4_0 = {
	4, 625,
	_vq_lengthlist__8u0__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8u0__p4_0,
	NULL,
	&_vq_auxt__8u0__p4_0,
	NULL,
	0
};

static long _vq_quantlist__8u0__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__8u0__p5_0[] = {
	 1, 4, 4, 7, 7, 7, 7, 9, 9, 4, 6, 6, 8, 7, 8, 8,
	10,10, 4, 6, 6, 8, 8, 8, 8,10,10, 6, 8, 8, 9, 9,
	 9, 9,11,11, 7, 8, 8, 9, 9, 9, 9,11,11, 7, 8, 8,
	 9, 9,10,10,12,11, 7, 8, 8, 9, 9,10,10,11,11, 9,
	10,10,11,11,11,12,12,12, 9,10,10,11,11,12,12,12,
	12,
};

static float _vq_quantthresh__8u0__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__8u0__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__8u0__p5_0 = {
	_vq_quantthresh__8u0__p5_0,
	_vq_quantmap__8u0__p5_0,
	9,
	9
};

static static_codebook _8u0__p5_0 = {
	2, 81,
	_vq_lengthlist__8u0__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__8u0__p5_0,
	NULL,
	&_vq_auxt__8u0__p5_0,
	NULL,
	0
};

static long _vq_quantlist__8u0__p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__8u0__p6_0[] = {
	 1, 4, 4, 7, 7, 9, 9,11,11,12,12,16,16, 3, 6, 6,
	 9, 9,11,11,12,12,13,14,18,16, 3, 6, 7, 9, 9,11,
	11,13,12,14,14,17,16, 7, 9, 9,11,11,12,12,14,14,
	14,14,17,16, 7, 9, 9,11,11,13,12,13,13,14,14,17,
	 0, 9,11,11,12,13,14,14,14,13,15,14,17,17, 9,11,
	11,12,12,14,14,13,14,14,15, 0, 0,11,12,12,15,14,
	15,14,15,14,15,16,17, 0,11,12,13,13,13,14,14,15,
	14,15,15, 0, 0,12,14,14,15,15,14,16,15,15,17,16,
	 0,18,13,14,14,15,14,15,14,15,16,17,16, 0, 0,17,
	17,18, 0,16,18,16, 0, 0, 0,17, 0, 0,16, 0, 0,16,
	16, 0,15, 0,17, 0, 0, 0, 0,
};

static float _vq_quantthresh__8u0__p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__8u0__p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__8u0__p6_0 = {
	_vq_quantthresh__8u0__p6_0,
	_vq_quantmap__8u0__p6_0,
	13,
	13
};

static static_codebook _8u0__p6_0 = {
	2, 169,
	_vq_lengthlist__8u0__p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__8u0__p6_0,
	NULL,
	&_vq_auxt__8u0__p6_0,
	NULL,
	0
};

static long _vq_quantlist__8u0__p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8u0__p6_1[] = {
	 1, 4, 4, 6, 6, 4, 6, 5, 7, 7, 4, 5, 6, 7, 7, 6,
	 7, 7, 7, 7, 6, 7, 7, 7, 7,
};

static float _vq_quantthresh__8u0__p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8u0__p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8u0__p6_1 = {
	_vq_quantthresh__8u0__p6_1,
	_vq_quantmap__8u0__p6_1,
	5,
	5
};

static static_codebook _8u0__p6_1 = {
	2, 25,
	_vq_lengthlist__8u0__p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8u0__p6_1,
	NULL,
	&_vq_auxt__8u0__p6_1,
	NULL,
	0
};

static long _vq_quantlist__8u0__p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8u0__p7_0[] = {
	 1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__8u0__p7_0[] = {
	-157.5, 157.5,
};

static long _vq_quantmap__8u0__p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8u0__p7_0 = {
	_vq_quantthresh__8u0__p7_0,
	_vq_quantmap__8u0__p7_0,
	3,
	3
};

static static_codebook _8u0__p7_0 = {
	4, 81,
	_vq_lengthlist__8u0__p7_0,
	1, -518803456, 1628680192, 2, 0,
	_vq_quantlist__8u0__p7_0,
	NULL,
	&_vq_auxt__8u0__p7_0,
	NULL,
	0
};

static long _vq_quantlist__8u0__p7_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__8u0__p7_1[] = {
	 1, 5, 5, 5, 5,10,10,11,11,11,11,11,11,11,11, 5,
	 7, 6, 8, 8, 9,10,11,11,11,11,11,11,11,11, 6, 6,
	 7, 9, 7,11,10,11,11,11,11,11,11,11,11, 5, 6, 6,
	11, 8,11,11,11,11,11,11,11,11,11,11, 5, 6, 6, 9,
	10,11,10,11,11,11,11,11,11,11,11, 7,10,10,11,11,
	11,11,11,11,11,11,11,11,11,11, 7,11, 8,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__8u0__p7_1[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__8u0__p7_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__8u0__p7_1 = {
	_vq_quantthresh__8u0__p7_1,
	_vq_quantmap__8u0__p7_1,
	15,
	15
};

static static_codebook _8u0__p7_1 = {
	2, 225,
	_vq_lengthlist__8u0__p7_1,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__8u0__p7_1,
	NULL,
	&_vq_auxt__8u0__p7_1,
	NULL,
	0
};

static long _vq_quantlist__8u0__p7_2[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__8u0__p7_2[] = {
	 1, 6, 5, 7, 7, 9, 9, 9, 9,10,12,12,10,11,11,10,
	11,11,11,10,11, 6, 8, 8, 9, 9,10,10, 9,10,11,11,
	10,11,11,11,11,10,11,11,11,11, 6, 7, 8, 9, 9, 9,
	10,11,10,11,12,11,10,11,11,11,11,11,11,12,10, 8,
	 9, 9,10, 9,10,10, 9,10,10,10,10,10, 9,10,10,10,
	10, 9,10,10, 9, 9, 9, 9,10,10, 9, 9,10,10,11,10,
	 9,12,10,11,10, 9,10,10,10, 8, 9, 9,10, 9,10, 9,
	 9,10,10, 9,10, 9,11,10,10,10,10,10, 9,10, 8, 8,
	 9, 9,10, 9,11, 9, 8, 9, 9,10,11,10,10,10,11,12,
	 9, 9,11, 8, 9, 8,11,10,11,10,10, 9,11,10,10,10,
	10,10,10,10,11,11,11,11, 8, 9, 9, 9,10,10,10,11,
	11,12,11,12,11,10,10,10,12,11,11,11,10, 8,10, 9,
	11,10,10,11,12,10,11,12,11,11,12,11,12,12,10,11,
	11,10, 9, 9,10,11,12,10,10,10,11,10,11,11,10,12,
	12,10,11,10,11,12,10, 9,10,10,11,10,11,11,11,11,
	11,12,11,11,11, 9,11,10,11,10,11,10, 9, 9,10,11,
	11,11,10,10,11,12,12,11,12,11,11,11,12,12,12,12,
	11, 9,11,11,12,10,11,11,11,11,11,11,12,11,11,12,
	11,11,11,10,11,11, 9,11,10,11,11,11,10,10,10,11,
	11,11,12,10,11,10,11,11,11,11,12, 9,11,10,11,11,
	10,10,11,11, 9,11,11,12,10,10,10,10,10,11,11,10,
	 9,10,11,11,12,11,10,10,12,11,11,12,11,12,11,11,
	10,10,11,11,10,12,11,10,11,10,11,10,10,10,11,11,
	10,10,11,11,11,11,10,10,10,12,11,11,11,11,10, 9,
	10,11,11,11,12,11,11,11,12,10,11,11,11, 9,10,11,
	11,11,11,11,11,10,10,11,11,12,11,10,11,12,11,10,
	10,11, 9,10,11,11,11,11,11,10,11,11,10,12,11,11,
	11,12,11,11,11,10,10,11,11,
};

static float _vq_quantthresh__8u0__p7_2[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__8u0__p7_2[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__8u0__p7_2 = {
	_vq_quantthresh__8u0__p7_2,
	_vq_quantmap__8u0__p7_2,
	21,
	21
};

static static_codebook _8u0__p7_2 = {
	2, 441,
	_vq_lengthlist__8u0__p7_2,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__8u0__p7_2,
	NULL,
	&_vq_auxt__8u0__p7_2,
	NULL,
	0
};

static long _huff_lengthlist__8u0__single[] = {
	 4, 7,11, 9,12, 8, 7,10, 6, 4, 5, 5, 7, 5, 6,16,
	 9, 5, 5, 6, 7, 7, 9,16, 7, 4, 6, 5, 7, 5, 7,17,
	10, 7, 7, 8, 7, 7, 8,18, 7, 5, 6, 4, 5, 4, 5,15,
	 7, 6, 7, 5, 6, 4, 5,15,12,13,18,12,17,11, 9,17,
};

static static_codebook _huff_book__8u0__single = {
	2, 64,
	_huff_lengthlist__8u0__single,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__8u1__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8u1__p1_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 8, 8, 7, 9,10, 7,
	 9, 9, 5, 8, 8, 7,10, 9, 7, 9, 9, 5, 8, 8, 8,10,
	10, 8,10,10, 7,10,10, 9,10,12,10,12,12, 7,10,10,
	 9,12,11,10,12,12, 5, 8, 8, 8,10,10, 8,10,10, 7,
	10,10,10,12,12, 9,11,12, 7,10,10,10,12,12, 9,12,
	10,
};

static float _vq_quantthresh__8u1__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__8u1__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8u1__p1_0 = {
	_vq_quantthresh__8u1__p1_0,
	_vq_quantmap__8u1__p1_0,
	3,
	3
};

static static_codebook _8u1__p1_0 = {
	4, 81,
	_vq_lengthlist__8u1__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__8u1__p1_0,
	NULL,
	&_vq_auxt__8u1__p1_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8u1__p2_0[] = {
	 3, 4, 5, 5, 6, 6, 5, 6, 6, 5, 7, 6, 6, 7, 8, 6,
	 7, 8, 5, 6, 6, 6, 8, 7, 6, 8, 7, 5, 6, 6, 7, 8,
	 8, 6, 7, 7, 6, 8, 7, 7, 7, 9, 8, 9, 9, 6, 7, 8,
	 7, 9, 7, 8, 9, 9, 5, 6, 6, 6, 7, 7, 7, 8, 8, 6,
	 8, 7, 8, 9, 9, 7, 7, 9, 6, 7, 8, 8, 9, 9, 7, 9,
	 7,
};

static float _vq_quantthresh__8u1__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__8u1__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8u1__p2_0 = {
	_vq_quantthresh__8u1__p2_0,
	_vq_quantmap__8u1__p2_0,
	3,
	3
};

static static_codebook _8u1__p2_0 = {
	4, 81,
	_vq_lengthlist__8u1__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__8u1__p2_0,
	NULL,
	&_vq_auxt__8u1__p2_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8u1__p3_0[] = {
	 1, 5, 5, 7, 7, 6, 7, 7, 9, 9, 6, 7, 7, 9, 9, 8,
	10, 9,11,11, 9, 9, 9,11,11, 6, 8, 8,10,10, 8,10,
	10,11,11, 8, 9,10,11,11,10,11,11,12,12,10,11,11,
	12,13, 6, 8, 8,10,10, 8,10, 9,11,11, 8,10, 9,11,
	11,10,11,11,12,12,10,11,11,12,12, 9,11,11,14,13,
	10,12,11,14,14,10,12,11,14,13,12,13,13,15,14,12,
	13,13,15,14, 8,11,11,13,14,10,11,12,13,15,10,11,
	12,14,14,12,13,13,14,15,12,13,13,14,15, 5, 8, 8,
	11,11, 8,10,10,12,12, 8,10,10,12,12,11,12,12,14,
	13,11,12,12,13,14, 8,10,10,12,12, 9,11,12,13,14,
	10,12,12,13,13,12,12,13,14,14,11,13,13,15,15, 7,
	10,10,12,12, 9,12,11,14,12,10,11,12,13,14,12,13,
	12,14,14,12,13,13,15,16,10,12,12,15,14,11,12,13,
	15,15,11,13,13,15,16,14,14,15,15,16,13,14,15,17,
	15, 9,12,12,14,15,11,13,12,15,15,11,13,13,15,15,
	13,14,13,15,14,13,14,14,17, 0, 5, 8, 8,11,11, 8,
	10,10,12,12, 8,10,10,12,12,11,12,12,14,14,11,12,
	12,14,14, 7,10,10,12,12,10,12,12,13,13, 9,11,12,
	12,13,11,12,13,15,15,11,12,13,14,15, 8,10,10,12,
	12,10,12,11,13,13,10,12,11,13,13,11,13,13,15,14,
	12,13,12,15,13, 9,12,12,14,14,11,13,13,16,15,11,
	12,13,16,15,13,14,15,16,16,13,13,15,15,16,10,12,
	12,15,14,11,13,13,14,16,11,13,13,15,16,13,15,15,
	16,17,13,15,14,16,15, 8,11,11,14,15,10,12,12,15,
	15,10,12,12,15,16,14,15,15,16,17,13,14,14,16,16,
	 9,12,12,15,15,11,13,14,15,17,11,13,13,15,16,14,
	15,16,19,17,13,15,15, 0,17, 9,12,12,15,15,11,14,
	13,16,15,11,13,13,15,16,15,15,15,18,17,13,15,15,
	17,17,11,15,14,18,16,12,14,15,17,17,12,15,15,18,
	18,15,15,16,15,19,14,16,16, 0, 0,11,14,14,16,17,
	12,15,14,18,17,12,15,15,18,18,15,17,15,18,16,14,
	16,16,18,18, 7,11,11,14,14,10,12,12,15,15,10,12,
	13,15,15,13,14,15,16,16,14,15,15,18,18, 9,12,12,
	15,15,11,13,13,16,15,11,12,13,16,16,14,15,15,17,
	16,15,16,16,17,17, 9,12,12,15,15,11,13,13,15,17,
	11,14,13,16,15,13,15,15,17,17,15,15,15,18,17,11,
	14,14,17,15,12,14,15,17,18,13,13,15,17,17,14,16,
	16,19,18,16,15,17,17, 0,11,14,14,17,17,12,15,15,
	18, 0,12,15,14,18,16,14,17,17,19, 0,16,18,15, 0,
	16,
};

static float _vq_quantthresh__8u1__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8u1__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8u1__p3_0 = {
	_vq_quantthresh__8u1__p3_0,
	_vq_quantmap__8u1__p3_0,
	5,
	5
};

static static_codebook _8u1__p3_0 = {
	4, 625,
	_vq_lengthlist__8u1__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8u1__p3_0,
	NULL,
	&_vq_auxt__8u1__p3_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__8u1__p4_0[] = {
	 4, 5, 5, 9, 9, 6, 7, 7, 9, 9, 6, 7, 7, 9, 9, 9,
	 9, 9,11,11, 9, 9, 9,11,11, 6, 7, 7, 9, 9, 7, 7,
	 8, 9,10, 7, 7, 8, 9,10, 9, 9,10,10,11, 9, 9,10,
	10,12, 6, 7, 7, 9, 9, 7, 8, 7,10, 9, 7, 8, 7,10,
	 9, 9,10, 9,12,11,10,10, 9,12,10, 9,10,10,12,11,
	 9,10,10,12,11, 9,10,10,12,12,11,11,12,12,13,11,
	11,12,12,13, 9, 9,10,12,11, 9,10,10,12,12,10,10,
	10,12,12,11,12,11,13,12,11,12,11,13,12, 6, 7, 7,
	 9, 9, 7, 8, 8,10,10, 7, 8, 7,10, 9,10,10,10,12,
	12,10,10,10,12,11, 7, 8, 7,10,10, 7, 7, 9,10,11,
	 8, 9, 9,11,10,10,10,11,10,12,10,10,11,12,12, 7,
	 8, 8,10,10, 7, 9, 8,11,10, 8, 8, 9,11,11,10,11,
	10,12,11,10,11,11,12,12, 9,10,10,12,12, 9,10,10,
	12,12,10,11,11,13,12,11,10,12,10,14,12,12,12,13,
	14, 9,10,10,12,12, 9,11,10,12,12,10,11,11,12,12,
	11,12,11,14,12,12,12,12,14,14, 5, 7, 7, 9, 9, 7,
	 7, 7, 9,10, 7, 8, 8,10,10,10,10,10,11,11,10,10,
	10,12,12, 7, 8, 8,10,10, 8, 9, 8,11,10, 7, 8, 9,
	10,11,10,10,10,11,12,10,10,11,11,13, 6, 7, 8,10,
	10, 8, 9, 9,10,10, 7, 9, 7,11,10,10,11,10,12,12,
	10,11,10,12,10, 9,10,10,12,12,10,11,11,13,12, 9,
	10,10,12,12,12,12,12,14,13,11,11,12,11,14, 9,10,
	10,11,12,10,11,11,12,13, 9,10,10,12,12,12,12,12,
	14,13,11,12,10,14,11, 9, 9,10,11,12, 9,10,10,12,
	12, 9,10,10,12,12,12,12,12,14,14,11,12,12,13,12,
	 9,10, 9,12,12, 9,10,11,12,13,10,11,10,13,11,12,
	12,13,13,14,12,12,12,13,13, 9,10,10,12,12,10,11,
	10,13,12,10,10,11,12,13,12,13,12,14,13,12,12,12,
	13,14,11,12,11,14,13,10,10,11,13,13,12,12,12,14,
	13,12,10,14,10,15,13,14,14,14,14,11,11,12,13,14,
	10,12,11,13,13,12,12,12,13,15,12,13,11,15,12,13,
	13,14,14,14, 9,10, 9,12,12, 9,10,10,12,12,10,10,
	10,12,12,11,11,12,12,13,12,12,12,14,14, 9,10,10,
	12,12,10,11,10,13,12,10,10,11,12,13,12,12,12,14,
	13,12,12,13,13,14, 9,10,10,12,13,10,10,11,11,12,
	 9,11,10,13,12,12,12,12,13,14,12,13,12,14,13,11,
	12,11,13,13,12,13,12,14,13,10,11,12,13,13,13,13,
	13,14,15,12,11,14,12,14,11,11,12,12,13,12,12,12,
	13,14,10,12,10,14,13,13,13,13,14,15,12,14,11,15,
	10,
};

static float _vq_quantthresh__8u1__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__8u1__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__8u1__p4_0 = {
	_vq_quantthresh__8u1__p4_0,
	_vq_quantmap__8u1__p4_0,
	5,
	5
};

static static_codebook _8u1__p4_0 = {
	4, 625,
	_vq_lengthlist__8u1__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__8u1__p4_0,
	NULL,
	&_vq_auxt__8u1__p4_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__8u1__p5_0[] = {
	 1, 4, 4, 7, 7, 7, 7, 9, 9, 4, 6, 5, 8, 7, 8, 8,
	10,10, 4, 6, 6, 8, 8, 8, 8,10,10, 7, 8, 8, 9, 9,
	 9, 9,11,11, 7, 8, 8, 9, 9, 9, 9,11,11, 8, 8, 8,
	 9, 9,10,10,12,11, 8, 8, 8, 9, 9,10,10,11,11, 9,
	10,10,11,11,11,11,13,12, 9,10,10,11,11,12,12,12,
	13,
};

static float _vq_quantthresh__8u1__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__8u1__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__8u1__p5_0 = {
	_vq_quantthresh__8u1__p5_0,
	_vq_quantmap__8u1__p5_0,
	9,
	9
};

static static_codebook _8u1__p5_0 = {
	2, 81,
	_vq_lengthlist__8u1__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__8u1__p5_0,
	NULL,
	&_vq_auxt__8u1__p5_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p6_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__8u1__p6_0[] = {
	 3, 4, 4, 6, 6, 7, 7, 9, 9, 4, 4, 5, 6, 6, 7, 7,
	 9, 9, 4, 4, 4, 6, 6, 7, 7, 9, 9, 6, 6, 6, 7, 7,
	 8, 8, 9, 9, 6, 6, 6, 7, 7, 8, 8, 9, 9, 7, 7, 7,
	 8, 8, 8, 9,10,10, 7, 7, 7, 8, 8, 9, 8,10,10, 9,
	 9, 9, 9, 9,10,10,10,10, 9, 9, 9, 9, 9,10,10,10,
	10,
};

static float _vq_quantthresh__8u1__p6_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__8u1__p6_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__8u1__p6_0 = {
	_vq_quantthresh__8u1__p6_0,
	_vq_quantmap__8u1__p6_0,
	9,
	9
};

static static_codebook _8u1__p6_0 = {
	2, 81,
	_vq_lengthlist__8u1__p6_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__8u1__p6_0,
	NULL,
	&_vq_auxt__8u1__p6_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__8u1__p7_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 9, 9, 8,10,10, 8,
	10,10, 5, 9, 9, 7,10,10, 8,10,10, 4,10,10, 9,12,
	12, 9,11,11, 7,12,11,10,11,13,10,13,13, 7,12,12,
	10,13,12,10,13,13, 4,10,10, 9,12,12, 9,12,12, 7,
	12,12,10,13,13,10,12,13, 7,11,12,10,13,13,10,13,
	11,
};

static float _vq_quantthresh__8u1__p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__8u1__p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__8u1__p7_0 = {
	_vq_quantthresh__8u1__p7_0,
	_vq_quantmap__8u1__p7_0,
	3,
	3
};

static static_codebook _8u1__p7_0 = {
	4, 81,
	_vq_lengthlist__8u1__p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__8u1__p7_0,
	NULL,
	&_vq_auxt__8u1__p7_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__8u1__p7_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8, 4, 5, 5, 7, 7,
	 8, 8, 9, 9, 9, 9, 4, 5, 5, 7, 7, 8, 8, 9, 9, 9,
	 9, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 6, 7, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 8, 9, 9,
	 9, 9, 9, 9,10,10,10,10, 8, 9, 9, 9, 9, 9, 9,10,
	10,10,10, 8, 9, 9, 9, 9, 9, 9,10,10,10,10, 8, 9,
	 9, 9, 9, 9, 9,10,10,10,10,
};

static float _vq_quantthresh__8u1__p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__8u1__p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__8u1__p7_1 = {
	_vq_quantthresh__8u1__p7_1,
	_vq_quantmap__8u1__p7_1,
	11,
	11
};

static static_codebook _8u1__p7_1 = {
	2, 121,
	_vq_lengthlist__8u1__p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__8u1__p7_1,
	NULL,
	&_vq_auxt__8u1__p7_1,
	NULL,
	0
};

static long _vq_quantlist__8u1__p8_0[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__8u1__p8_0[] = {
	 1, 4, 4, 6, 6, 8, 8,10,10,11,11, 4, 6, 6, 7, 7,
	 9, 9,11,11,13,12, 4, 6, 6, 7, 7, 9, 9,11,11,12,
	12, 6, 7, 7, 9, 9,11,11,12,12,13,13, 6, 7, 7, 9,
	 9,11,11,12,12,13,13, 8, 9, 9,11,11,12,12,13,13,
	14,14, 8, 9, 9,11,11,12,12,13,13,14,14, 9,11,11,
	12,12,13,13,14,14,15,15, 9,11,11,12,12,13,13,14,
	14,15,14,11,12,12,13,13,14,14,15,15,16,16,11,12,
	12,13,13,14,14,15,15,15,15,
};

static float _vq_quantthresh__8u1__p8_0[] = {
	-49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5, 27.5,
	38.5, 49.5,
};

static long _vq_quantmap__8u1__p8_0[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__8u1__p8_0 = {
	_vq_quantthresh__8u1__p8_0,
	_vq_quantmap__8u1__p8_0,
	11,
	11
};

static static_codebook _8u1__p8_0 = {
	2, 121,
	_vq_lengthlist__8u1__p8_0,
	1, -524582912, 1618345984, 4, 0,
	_vq_quantlist__8u1__p8_0,
	NULL,
	&_vq_auxt__8u1__p8_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p8_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__8u1__p8_1[] = {
	 2, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 5, 6, 6, 7, 7,
	 7, 7, 8, 8, 8, 8, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8,
	 8, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 6, 7, 7, 7,
	 7, 8, 8, 8, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8,
	 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 9, 8, 9, 9, 7, 8, 8, 8, 8, 8, 8, 9,
	 8, 9, 9, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 8, 8,
	 8, 8, 8, 8, 8, 9, 9, 9, 9,
};

static float _vq_quantthresh__8u1__p8_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__8u1__p8_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__8u1__p8_1 = {
	_vq_quantthresh__8u1__p8_1,
	_vq_quantmap__8u1__p8_1,
	11,
	11
};

static static_codebook _8u1__p8_1 = {
	2, 121,
	_vq_lengthlist__8u1__p8_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__8u1__p8_1,
	NULL,
	&_vq_auxt__8u1__p8_1,
	NULL,
	0
};

static long _vq_quantlist__8u1__p9_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__8u1__p9_0[] = {
	 1, 4, 4,11,11,11,11,11,11,11,11,11,11,11,11, 3,
	11, 8,11,11,11,11,11,11,11,11,11,11,11,11, 3, 9,
	 9,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__8u1__p9_0[] = {
	-1657.5, -1402.5, -1147.5, -892.5, -637.5, -382.5, -127.5, 127.5,
	382.5, 637.5, 892.5, 1147.5, 1402.5, 1657.5,
};

static long _vq_quantmap__8u1__p9_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__8u1__p9_0 = {
	_vq_quantthresh__8u1__p9_0,
	_vq_quantmap__8u1__p9_0,
	15,
	15
};

static static_codebook _8u1__p9_0 = {
	2, 225,
	_vq_lengthlist__8u1__p9_0,
	1, -514071552, 1627381760, 4, 0,
	_vq_quantlist__8u1__p9_0,
	NULL,
	&_vq_auxt__8u1__p9_0,
	NULL,
	0
};

static long _vq_quantlist__8u1__p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__8u1__p9_1[] = {
	 1, 4, 4, 7, 7, 9, 9, 7, 7, 8, 8,10,10,11,11, 4,
	 7, 7, 9, 9,10,10, 8, 8,10,10,10,11,10,11, 4, 7,
	 7, 9, 9,10,10, 8, 8,10, 9,11,11,11,11, 7, 9, 9,
	12,12,11,12,10,10,11,10,12,11,11,11, 7, 9, 9,11,
	11,13,12, 9, 9,11,10,11,11,12,11, 9,10,10,12,12,
	14,14,10,10,11,12,12,11,11,11, 9,10,11,11,13,14,
	13,10,11,11,11,12,11,12,12, 7, 8, 8,10, 9,11,10,
	11,12,12,11,12,14,12,13, 7, 8, 8, 9,10,10,11,12,
	12,12,11,12,12,12,13, 9, 9, 9,11,11,13,12,12,12,
	12,11,12,12,13,12, 8,10,10,11,10,11,12,12,12,12,
	12,12,14,12,12, 9,11,11,11,12,12,12,12,13,13,12,
	12,13,13,12,10,11,11,12,11,12,12,12,11,12,13,12,
	12,12,13,11,11,12,12,12,13,12,12,11,12,13,13,12,
	12,13,12,11,12,12,13,13,12,13,12,13,13,13,13,14,
	13,
};

static float _vq_quantthresh__8u1__p9_1[] = {
	-110.5, -93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5,
	25.5, 42.5, 59.5, 76.5, 93.5, 110.5,
};

static long _vq_quantmap__8u1__p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__8u1__p9_1 = {
	_vq_quantthresh__8u1__p9_1,
	_vq_quantmap__8u1__p9_1,
	15,
	15
};

static static_codebook _8u1__p9_1 = {
	2, 225,
	_vq_lengthlist__8u1__p9_1,
	1, -522338304, 1620115456, 4, 0,
	_vq_quantlist__8u1__p9_1,
	NULL,
	&_vq_auxt__8u1__p9_1,
	NULL,
	0
};

static long _vq_quantlist__8u1__p9_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__8u1__p9_2[] = {
	 2, 5, 4, 6, 6, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 5, 6, 6, 7, 7, 8, 8, 9, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 5, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
	 9,10,10, 9, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9,10,10, 8, 8, 8, 9, 9, 9, 9,10,10,10, 9,
	10,10,10,10,10,10, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9,
	10,10,10,10,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9,10,
	10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9,10,10,10,
	10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9,
	 9,10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9,10,
	 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10, 9,
	10, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__8u1__p9_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__8u1__p9_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__8u1__p9_2 = {
	_vq_quantthresh__8u1__p9_2,
	_vq_quantmap__8u1__p9_2,
	17,
	17
};

static static_codebook _8u1__p9_2 = {
	2, 289,
	_vq_lengthlist__8u1__p9_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__8u1__p9_2,
	NULL,
	&_vq_auxt__8u1__p9_2,
	NULL,
	0
};

static long _huff_lengthlist__8u1__single[] = {
	 4, 7,13, 9,15, 9,16, 8,10,13, 7, 5, 8, 6, 9, 7,
	10, 7,10,11,11, 6, 7, 8, 8, 9, 9, 9,12,16, 8, 5,
	 8, 6, 8, 6, 9, 7,10,12,11, 7, 7, 7, 6, 7, 7, 7,
	11,15, 7, 5, 8, 6, 7, 5, 7, 6, 9,13,13, 9, 9, 8,
	 6, 6, 5, 5, 9,14, 8, 6, 8, 6, 6, 4, 5, 3, 5,13,
	 9, 9,11, 8,10, 7, 8, 4, 5,12,11,16,17,15,17,12,
	13, 8, 8,15,
};

static static_codebook _huff_book__8u1__single = {
	2, 100,
	_huff_lengthlist__8u1__single,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u0__long[] = {
	 5, 8,13,10,17,11,11,15, 7, 2, 4, 5, 8, 7, 9,16,
	13, 4, 3, 5, 6, 8,11,20,10, 4, 5, 5, 7, 6, 8,18,
	15, 7, 6, 7, 8,10,14,20,10, 6, 7, 6, 9, 7, 8,17,
	 9, 8,10, 8,10, 5, 4,11,12,17,19,14,16,10, 7,12,
};

static static_codebook _huff_book__44u0__long = {
	2, 64,
	_huff_lengthlist__44u0__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u0__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u0__p1_0[] = {
	 1, 4, 4, 5, 8, 7, 5, 7, 8, 5, 8, 8, 8,11,11, 8,
	10,10, 5, 8, 8, 8,11,10, 8,11,11, 4, 8, 8, 8,11,
	11, 8,11,11, 8,12,11,11,13,13,11,13,14, 7,11,11,
	10,13,12,11,13,14, 4, 8, 8, 8,11,11, 8,11,12, 8,
	11,11,11,13,13,10,12,13, 8,11,11,11,14,13,11,14,
	13,
};

static float _vq_quantthresh__44u0__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u0__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u0__p1_0 = {
	_vq_quantthresh__44u0__p1_0,
	_vq_quantmap__44u0__p1_0,
	3,
	3
};

static static_codebook _44u0__p1_0 = {
	4, 81,
	_vq_lengthlist__44u0__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u0__p1_0,
	NULL,
	&_vq_auxt__44u0__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u0__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u0__p2_0[] = {
	 2, 4, 4, 5, 6, 6, 5, 6, 6, 5, 7, 7, 7, 8, 8, 6,
	 8, 8, 5, 7, 7, 6, 8, 8, 7, 8, 8, 4, 7, 7, 7, 8,
	 8, 7, 8, 8, 7, 8, 8, 8, 9,10, 8,10,10, 6, 8, 8,
	 8,10, 8, 8,10,10, 5, 7, 7, 7, 8, 8, 7, 8, 8, 6,
	 8, 8, 8,10,10, 8, 8,10, 6, 8, 8, 8,10,10, 8,10,
	 9,
};

static float _vq_quantthresh__44u0__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u0__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u0__p2_0 = {
	_vq_quantthresh__44u0__p2_0,
	_vq_quantmap__44u0__p2_0,
	3,
	3
};

static static_codebook _44u0__p2_0 = {
	4, 81,
	_vq_lengthlist__44u0__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u0__p2_0,
	NULL,
	&_vq_auxt__44u0__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u0__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u0__p3_0[] = {
	 1, 5, 5, 8, 8, 5, 8, 7, 9, 9, 5, 7, 8, 9, 9, 9,
	10, 9,12,12, 9, 9,10,12,12, 6, 8, 8,11,10, 8,10,
	10,11,11, 8, 9,10,11,11,10,11,11,14,13,10,11,11,
	13,13, 5, 8, 8,10,10, 8,10,10,11,11, 8,10,10,11,
	11,10,11,11,13,13,10,11,11,13,13, 9,11,11,15,14,
	10,12,12,15,14,10,12,11,15,14,13,14,14,16,16,12,
	14,13,17,15, 9,11,11,14,15,10,11,12,14,16,10,11,
	12,14,16,12,13,14,16,16,13,13,15,15,18, 5, 8, 8,
	11,11, 8,10,10,12,12, 8,10,10,12,13,11,12,12,14,
	14,11,12,12,15,15, 8,10,10,13,13,10,12,12,13,13,
	10,12,12,14,14,12,13,13,15,15,12,13,13,16,16, 7,
	10,10,12,12,10,12,11,13,13,10,12,12,13,14,12,13,
	12,15,14,12,13,13,16,16,10,12,12,17,16,12,13,13,
	16,15,11,13,13,17,17,15,15,15,16,17,14,15,15,19,
	19,10,12,12,15,16,11,13,12,15,18,11,13,13,16,16,
	14,15,15,17,17,14,15,15,17,19, 5, 8, 8,11,11, 8,
	10,10,12,12, 8,10,10,12,12,11,12,12,16,15,11,12,
	12,14,15, 7,10,10,13,13,10,12,12,14,13,10,11,12,
	13,13,12,13,13,16,16,12,12,13,15,15, 8,10,10,13,
	13,10,12,12,14,14,10,12,12,13,13,12,13,13,16,16,
	12,13,13,15,15,10,12,12,16,15,11,13,13,17,16,11,
	12,13,16,15,13,15,15,19,17,14,15,14,17,16,10,12,
	12,16,16,11,13,13,16,17,12,13,13,15,17,14,15,15,
	17,19,14,15,15,17,17, 8,11,11,16,16,10,13,12,17,
	17,10,12,13,16,16,15,17,16,20,19,14,15,17,18,19,
	 9,12,12,16,17,11,13,14,17,18,11,13,13,19,18,16,
	17,18,19,19,15,16,16,19,19, 9,12,12,16,17,11,14,
	13,18,17,11,13,13,17,17,16,17,16,20,19,14,16,16,
	18,18,12,15,15,19,17,14,15,16, 0,20,13,15,16,20,
	17,18,16,20, 0, 0,15,16,19,20, 0,12,15,14,18,19,
	13,16,15,20,19,13,16,15,20,18,17,18,17, 0,20,16,
	17,16, 0, 0, 8,11,11,16,15,10,12,12,17,17,10,13,
	13,17,16,14,16,15,18,20,15,16,16,19,19, 9,12,12,
	16,16,11,13,13,17,16,11,13,14,17,18,15,15,16,20,
	20,16,16,17,19,19, 9,13,12,16,17,11,14,13,17,17,
	11,14,14,18,17,14,16,15,18,19,16,17,18,18,19,12,
	14,15,19,18,13,15,16,18, 0,13,14,15, 0, 0,16,16,
	17,20, 0,17,17,20,20, 0,12,15,15,19,20,13,15,15,
	 0, 0,14,16,15, 0, 0,15,18,16, 0, 0,17,18,16, 0,
	19,
};

static float _vq_quantthresh__44u0__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u0__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u0__p3_0 = {
	_vq_quantthresh__44u0__p3_0,
	_vq_quantmap__44u0__p3_0,
	5,
	5
};

static static_codebook _44u0__p3_0 = {
	4, 625,
	_vq_lengthlist__44u0__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u0__p3_0,
	NULL,
	&_vq_auxt__44u0__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u0__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u0__p4_0[] = {
	 4, 5, 5, 9, 9, 5, 6, 6, 9, 9, 5, 6, 6, 9, 9, 9,
	10, 9,12,12, 9, 9,10,12,12, 5, 7, 7,10,10, 7, 7,
	 8,10,10, 6, 7, 8,10,10,10,10,10,11,13,10, 9,10,
	12,13, 5, 7, 7,10,10, 6, 8, 7,10,10, 7, 8, 7,10,
	10, 9,10,10,12,12,10,10,10,13,11, 9,10,10,13,13,
	10,11,10,13,13,10,10,10,13,13,12,12,13,14,14,12,
	12,13,14,14, 9,10,10,13,13,10,10,10,13,13,10,10,
	10,13,13,12,13,12,15,14,12,13,12,15,15, 5, 7, 6,
	10,10, 7, 8, 8,10,10, 7, 8, 8,10,10,10,11,10,13,
	13,10,10,10,12,12, 7, 8, 8,11,10, 8, 8, 9,10,11,
	 8, 9, 9,11,11,11,10,11,11,14,11,11,11,13,13, 6,
	 8, 8,10,10, 7, 9, 8,11,10, 8, 9, 9,11,11,10,11,
	10,14,11,10,11,11,13,13,10,11,11,14,13,10,10,11,
	14,13,10,11,11,14,14,12,11,13,12,16,13,14,14,15,
	15,10,10,11,13,14,10,11,10,14,13,10,11,11,14,14,
	12,13,12,15,13,13,13,14,15,16, 5, 7, 7,10,10, 7,
	 8, 8,10,10, 7, 8, 8,10,10,10,10,10,13,13,10,10,
	11,12,13, 6, 8, 8,11,10, 8, 9, 9,11,11, 7, 8, 9,
	10,11,10,11,11,13,13,10,10,11,11,13, 6, 8, 8,10,
	11, 8, 9, 9,11,11, 8, 9, 8,12,10,10,11,11,13,13,
	10,11,10,14,11,10,10,10,14,13,10,11,11,14,13,10,
	10,11,13,13,12,14,14,16,16,12,12,13,13,15,10,11,
	11,13,14,10,11,11,14,15,10,11,10,13,13,13,14,13,
	16,16,12,13,11,15,12, 9,10,10,13,13,10,11,11,14,
	13,10,10,11,13,14,13,14,13,16,16,13,13,13,15,16,
	 9,10,10,13,13,10,10,11,13,14,10,11,11,15,13,13,
	13,14,14,18,13,13,14,16,15, 9,10,10,13,14,10,11,
	10,14,13,10,11,11,13,14,13,14,13,16,15,13,13,14,
	15,16,12,13,12,16,14,11,11,13,15,15,13,14,13,16,
	15,15,12,16,12,17,14,15,15,17,17,12,13,13,14,16,
	11,13,11,16,15,12,13,14,15,16,14,15,13, 0,14,14,
	16,16, 0, 0, 9,10,10,13,13,10,11,10,14,14,10,11,
	11,13,13,12,13,13,14,16,13,14,14,16,16, 9,10,10,
	14,14,11,11,11,14,13,10,10,11,14,14,13,13,13,16,
	16,13,13,14,14,17, 9,10,10,13,14,10,11,11,13,15,
	10,11,10,14,14,13,13,13,14,17,13,14,13,17,14,12,
	13,13,16,14,13,14,13,16,15,12,12,13,15,16,15,15,
	16,18,16,15,13,15,14, 0,12,12,13,14,16,13,13,14,
	15,16,11,12,11,16,14,15,16,16,17,17,14,15,12,17,
	12,
};

static float _vq_quantthresh__44u0__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u0__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u0__p4_0 = {
	_vq_quantthresh__44u0__p4_0,
	_vq_quantmap__44u0__p4_0,
	5,
	5
};

static static_codebook _44u0__p4_0 = {
	4, 625,
	_vq_lengthlist__44u0__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u0__p4_0,
	NULL,
	&_vq_auxt__44u0__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u0__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u0__p5_0[] = {
	 1, 4, 4, 7, 7, 7, 7, 9, 9, 4, 6, 6, 8, 8, 8, 8,
	 9, 9, 4, 6, 6, 8, 8, 8, 8, 9, 9, 7, 8, 8, 9, 9,
	 9, 9,11,10, 7, 8, 8, 9, 9, 9, 9,10,10, 7, 8, 8,
	 9, 9,10,10,11,11, 7, 8, 8, 9, 9,10,10,11,11, 9,
	 9, 9,10,10,11,11,12,12, 9, 9, 9,10,11,11,11,12,
	12,
};

static float _vq_quantthresh__44u0__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u0__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u0__p5_0 = {
	_vq_quantthresh__44u0__p5_0,
	_vq_quantmap__44u0__p5_0,
	9,
	9
};

static static_codebook _44u0__p5_0 = {
	2, 81,
	_vq_lengthlist__44u0__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u0__p5_0,
	NULL,
	&_vq_auxt__44u0__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u0__p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u0__p6_0[] = {
	 1, 4, 4, 6, 6, 8, 8,10, 9,11,10,14,13, 4, 6, 5,
	 8, 8, 9, 9,11,10,11,11,14,14, 4, 5, 6, 8, 8, 9,
	 9,10,10,11,11,14,14, 6, 8, 8, 9, 9,10,10,11,11,
	12,12,16,15, 7, 8, 8, 9, 9,10,10,11,11,12,12,15,
	15, 9,10,10,10,10,11,11,12,12,12,12,15,15, 9,10,
	 9,10,11,11,11,12,12,12,13,15,15,10,10,11,11,11,
	12,12,13,12,13,13,16,15,10,11,11,11,11,12,12,13,
	12,13,13,16,17,11,11,12,12,12,13,13,13,14,14,15,
	17,17,11,11,12,12,12,13,13,13,14,14,14,16,18,14,
	15,15,15,15,16,16,16,16,17,18, 0, 0,14,15,15,15,
	15,17,16,17,18,17,17,18, 0,
};

static float _vq_quantthresh__44u0__p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44u0__p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u0__p6_0 = {
	_vq_quantthresh__44u0__p6_0,
	_vq_quantmap__44u0__p6_0,
	13,
	13
};

static static_codebook _44u0__p6_0 = {
	2, 169,
	_vq_lengthlist__44u0__p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44u0__p6_0,
	NULL,
	&_vq_auxt__44u0__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u0__p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u0__p6_1[] = {
	 2, 4, 4, 5, 5, 4, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5,
	 6, 6, 6, 6, 5, 6, 6, 6, 6,
};

static float _vq_quantthresh__44u0__p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u0__p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u0__p6_1 = {
	_vq_quantthresh__44u0__p6_1,
	_vq_quantmap__44u0__p6_1,
	5,
	5
};

static static_codebook _44u0__p6_1 = {
	2, 25,
	_vq_lengthlist__44u0__p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u0__p6_1,
	NULL,
	&_vq_auxt__44u0__p6_1,
	NULL,
	0
};

static long _vq_quantlist__44u0__p7_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u0__p7_0[] = {
	 1, 4, 4,11,11, 9,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11, 9,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,10,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__44u0__p7_0[] = {
	-253.5, -84.5, 84.5, 253.5,
};

static long _vq_quantmap__44u0__p7_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u0__p7_0 = {
	_vq_quantthresh__44u0__p7_0,
	_vq_quantmap__44u0__p7_0,
	5,
	5
};

static static_codebook _44u0__p7_0 = {
	4, 625,
	_vq_lengthlist__44u0__p7_0,
	1, -518709248, 1626677248, 3, 0,
	_vq_quantlist__44u0__p7_0,
	NULL,
	&_vq_auxt__44u0__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u0__p7_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u0__p7_1[] = {
	 1, 4, 4, 6, 6, 6, 6, 7, 7, 8, 8, 9, 9, 5, 7, 7,
	 8, 7, 7, 7, 9, 8,10, 9,10,11, 5, 7, 7, 8, 8, 7,
	 7, 8, 9,10,10,11,11, 6, 8, 8, 9, 9, 9, 9,11,10,
	12,12,15,12, 6, 8, 8, 9, 9, 9, 9,11,11,12,11,14,
	12, 7, 8, 8,10,10,12,12,13,13,13,15,13,13, 7, 8,
	 8,10,10,11,11,13,12,14,15,15,15, 9,10,10,11,12,
	13,13,14,15,14,15,14,15, 8,10,10,12,12,14,14,15,
	14,14,15,15,14,10,12,12,14,14,15,14,15,15,15,14,
	15,15,10,12,12,13,14,15,14,15,15,14,15,15,15,12,
	15,13,15,14,15,15,15,15,15,15,15,15,13,13,15,15,
	15,15,15,15,15,15,15,15,15,
};

static float _vq_quantthresh__44u0__p7_1[] = {
	-71.5, -58.5, -45.5, -32.5, -19.5, -6.5, 6.5, 19.5,
	32.5, 45.5, 58.5, 71.5,
};

static long _vq_quantmap__44u0__p7_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u0__p7_1 = {
	_vq_quantthresh__44u0__p7_1,
	_vq_quantmap__44u0__p7_1,
	13,
	13
};

static static_codebook _44u0__p7_1 = {
	2, 169,
	_vq_lengthlist__44u0__p7_1,
	1, -523010048, 1618608128, 4, 0,
	_vq_quantlist__44u0__p7_1,
	NULL,
	&_vq_auxt__44u0__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u0__p7_2[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u0__p7_2[] = {
	 2, 5, 4, 6, 6, 7, 7, 8, 8, 8, 8, 9, 8, 5, 5, 6,
	 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 5, 6, 5, 7, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 6, 7, 7, 8, 8, 8, 8, 9, 8,
	 9, 9, 9, 9, 6, 7, 7, 8, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 7, 8,
	 8, 9, 8, 9, 8, 9, 9, 9, 9, 9, 9, 8, 9, 8, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9,10, 9,10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10, 9, 9, 9, 9, 9,
	 9, 9, 9,10, 9, 9,10,10, 9,
};

static float _vq_quantthresh__44u0__p7_2[] = {
	-5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5,
	2.5, 3.5, 4.5, 5.5,
};

static long _vq_quantmap__44u0__p7_2[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u0__p7_2 = {
	_vq_quantthresh__44u0__p7_2,
	_vq_quantmap__44u0__p7_2,
	13,
	13
};

static static_codebook _44u0__p7_2 = {
	2, 169,
	_vq_lengthlist__44u0__p7_2,
	1, -531103744, 1611661312, 4, 0,
	_vq_quantlist__44u0__p7_2,
	NULL,
	&_vq_auxt__44u0__p7_2,
	NULL,
	0
};

static long _huff_lengthlist__44u0__short[] = {
	12,13,14,13,17,12,15,17, 5, 5, 6,10,10,11,15,16,
	 4, 3, 3, 7, 5, 7,10,16, 7, 7, 7,10, 9,11,12,16,
	 6, 5, 5, 9, 5, 6,10,16, 8, 7, 7, 9, 6, 7, 9,16,
	11, 7, 3, 6, 4, 5, 8,16,12, 9, 4, 8, 5, 7, 9,16,
};

static static_codebook _huff_book__44u0__short = {
	2, 64,
	_huff_lengthlist__44u0__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u1__long[] = {
	 5, 8,13,10,17,11,11,15, 7, 2, 4, 5, 8, 7, 9,16,
	13, 4, 3, 5, 6, 8,11,20,10, 4, 5, 5, 7, 6, 8,18,
	15, 7, 6, 7, 8,10,14,20,10, 6, 7, 6, 9, 7, 8,17,
	 9, 8,10, 8,10, 5, 4,11,12,17,19,14,16,10, 7,12,
};

static static_codebook _huff_book__44u1__long = {
	2, 64,
	_huff_lengthlist__44u1__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u1__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u1__p1_0[] = {
	 1, 4, 4, 5, 8, 7, 5, 7, 8, 5, 8, 8, 8,11,11, 8,
	10,10, 5, 8, 8, 8,11,10, 8,11,11, 4, 8, 8, 8,11,
	11, 8,11,11, 8,12,11,11,13,13,11,13,14, 7,11,11,
	10,13,12,11,13,14, 4, 8, 8, 8,11,11, 8,11,12, 8,
	11,11,11,13,13,10,12,13, 8,11,11,11,14,13,11,14,
	13,
};

static float _vq_quantthresh__44u1__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u1__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u1__p1_0 = {
	_vq_quantthresh__44u1__p1_0,
	_vq_quantmap__44u1__p1_0,
	3,
	3
};

static static_codebook _44u1__p1_0 = {
	4, 81,
	_vq_lengthlist__44u1__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u1__p1_0,
	NULL,
	&_vq_auxt__44u1__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u1__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u1__p2_0[] = {
	 2, 4, 4, 5, 6, 6, 5, 6, 6, 5, 7, 7, 7, 8, 8, 6,
	 8, 8, 5, 7, 7, 6, 8, 8, 7, 8, 8, 4, 7, 7, 7, 8,
	 8, 7, 8, 8, 7, 8, 8, 8, 9,10, 8,10,10, 6, 8, 8,
	 8,10, 8, 8,10,10, 5, 7, 7, 7, 8, 8, 7, 8, 8, 6,
	 8, 8, 8,10,10, 8, 8,10, 6, 8, 8, 8,10,10, 8,10,
	 9,
};

static float _vq_quantthresh__44u1__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u1__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u1__p2_0 = {
	_vq_quantthresh__44u1__p2_0,
	_vq_quantmap__44u1__p2_0,
	3,
	3
};

static static_codebook _44u1__p2_0 = {
	4, 81,
	_vq_lengthlist__44u1__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u1__p2_0,
	NULL,
	&_vq_auxt__44u1__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u1__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u1__p3_0[] = {
	 1, 5, 5, 8, 8, 5, 8, 7, 9, 9, 5, 7, 8, 9, 9, 9,
	10, 9,12,12, 9, 9,10,12,12, 6, 8, 8,11,10, 8,10,
	10,11,11, 8, 9,10,11,11,10,11,11,14,13,10,11,11,
	13,13, 5, 8, 8,10,10, 8,10,10,11,11, 8,10,10,11,
	11,10,11,11,13,13,10,11,11,13,13, 9,11,11,15,14,
	10,12,12,15,14,10,12,11,15,14,13,14,14,16,16,12,
	14,13,17,15, 9,11,11,14,15,10,11,12,14,16,10,11,
	12,14,16,12,13,14,16,16,13,13,15,15,18, 5, 8, 8,
	11,11, 8,10,10,12,12, 8,10,10,12,13,11,12,12,14,
	14,11,12,12,15,15, 8,10,10,13,13,10,12,12,13,13,
	10,12,12,14,14,12,13,13,15,15,12,13,13,16,16, 7,
	10,10,12,12,10,12,11,13,13,10,12,12,13,14,12,13,
	12,15,14,12,13,13,16,16,10,12,12,17,16,12,13,13,
	16,15,11,13,13,17,17,15,15,15,16,17,14,15,15,19,
	19,10,12,12,15,16,11,13,12,15,18,11,13,13,16,16,
	14,15,15,17,17,14,15,15,17,19, 5, 8, 8,11,11, 8,
	10,10,12,12, 8,10,10,12,12,11,12,12,16,15,11,12,
	12,14,15, 7,10,10,13,13,10,12,12,14,13,10,11,12,
	13,13,12,13,13,16,16,12,12,13,15,15, 8,10,10,13,
	13,10,12,12,14,14,10,12,12,13,13,12,13,13,16,16,
	12,13,13,15,15,10,12,12,16,15,11,13,13,17,16,11,
	12,13,16,15,13,15,15,19,17,14,15,14,17,16,10,12,
	12,16,16,11,13,13,16,17,12,13,13,15,17,14,15,15,
	17,19,14,15,15,17,17, 8,11,11,16,16,10,13,12,17,
	17,10,12,13,16,16,15,17,16,20,19,14,15,17,18,19,
	 9,12,12,16,17,11,13,14,17,18,11,13,13,19,18,16,
	17,18,19,19,15,16,16,19,19, 9,12,12,16,17,11,14,
	13,18,17,11,13,13,17,17,16,17,16,20,19,14,16,16,
	18,18,12,15,15,19,17,14,15,16, 0,20,13,15,16,20,
	17,18,16,20, 0, 0,15,16,19,20, 0,12,15,14,18,19,
	13,16,15,20,19,13,16,15,20,18,17,18,17, 0,20,16,
	17,16, 0, 0, 8,11,11,16,15,10,12,12,17,17,10,13,
	13,17,16,14,16,15,18,20,15,16,16,19,19, 9,12,12,
	16,16,11,13,13,17,16,11,13,14,17,18,15,15,16,20,
	20,16,16,17,19,19, 9,13,12,16,17,11,14,13,17,17,
	11,14,14,18,17,14,16,15,18,19,16,17,18,18,19,12,
	14,15,19,18,13,15,16,18, 0,13,14,15, 0, 0,16,16,
	17,20, 0,17,17,20,20, 0,12,15,15,19,20,13,15,15,
	 0, 0,14,16,15, 0, 0,15,18,16, 0, 0,17,18,16, 0,
	19,
};

static float _vq_quantthresh__44u1__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u1__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u1__p3_0 = {
	_vq_quantthresh__44u1__p3_0,
	_vq_quantmap__44u1__p3_0,
	5,
	5
};

static static_codebook _44u1__p3_0 = {
	4, 625,
	_vq_lengthlist__44u1__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u1__p3_0,
	NULL,
	&_vq_auxt__44u1__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u1__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u1__p4_0[] = {
	 4, 5, 5, 9, 9, 5, 6, 6, 9, 9, 5, 6, 6, 9, 9, 9,
	10, 9,12,12, 9, 9,10,12,12, 5, 7, 7,10,10, 7, 7,
	 8,10,10, 6, 7, 8,10,10,10,10,10,11,13,10, 9,10,
	12,13, 5, 7, 7,10,10, 6, 8, 7,10,10, 7, 8, 7,10,
	10, 9,10,10,12,12,10,10,10,13,11, 9,10,10,13,13,
	10,11,10,13,13,10,10,10,13,13,12,12,13,14,14,12,
	12,13,14,14, 9,10,10,13,13,10,10,10,13,13,10,10,
	10,13,13,12,13,12,15,14,12,13,12,15,15, 5, 7, 6,
	10,10, 7, 8, 8,10,10, 7, 8, 8,10,10,10,11,10,13,
	13,10,10,10,12,12, 7, 8, 8,11,10, 8, 8, 9,10,11,
	 8, 9, 9,11,11,11,10,11,11,14,11,11,11,13,13, 6,
	 8, 8,10,10, 7, 9, 8,11,10, 8, 9, 9,11,11,10,11,
	10,14,11,10,11,11,13,13,10,11,11,14,13,10,10,11,
	14,13,10,11,11,14,14,12,11,13,12,16,13,14,14,15,
	15,10,10,11,13,14,10,11,10,14,13,10,11,11,14,14,
	12,13,12,15,13,13,13,14,15,16, 5, 7, 7,10,10, 7,
	 8, 8,10,10, 7, 8, 8,10,10,10,10,10,13,13,10,10,
	11,12,13, 6, 8, 8,11,10, 8, 9, 9,11,11, 7, 8, 9,
	10,11,10,11,11,13,13,10,10,11,11,13, 6, 8, 8,10,
	11, 8, 9, 9,11,11, 8, 9, 8,12,10,10,11,11,13,13,
	10,11,10,14,11,10,10,10,14,13,10,11,11,14,13,10,
	10,11,13,13,12,14,14,16,16,12,12,13,13,15,10,11,
	11,13,14,10,11,11,14,15,10,11,10,13,13,13,14,13,
	16,16,12,13,11,15,12, 9,10,10,13,13,10,11,11,14,
	13,10,10,11,13,14,13,14,13,16,16,13,13,13,15,16,
	 9,10,10,13,13,10,10,11,13,14,10,11,11,15,13,13,
	13,14,14,18,13,13,14,16,15, 9,10,10,13,14,10,11,
	10,14,13,10,11,11,13,14,13,14,13,16,15,13,13,14,
	15,16,12,13,12,16,14,11,11,13,15,15,13,14,13,16,
	15,15,12,16,12,17,14,15,15,17,17,12,13,13,14,16,
	11,13,11,16,15,12,13,14,15,16,14,15,13, 0,14,14,
	16,16, 0, 0, 9,10,10,13,13,10,11,10,14,14,10,11,
	11,13,13,12,13,13,14,16,13,14,14,16,16, 9,10,10,
	14,14,11,11,11,14,13,10,10,11,14,14,13,13,13,16,
	16,13,13,14,14,17, 9,10,10,13,14,10,11,11,13,15,
	10,11,10,14,14,13,13,13,14,17,13,14,13,17,14,12,
	13,13,16,14,13,14,13,16,15,12,12,13,15,16,15,15,
	16,18,16,15,13,15,14, 0,12,12,13,14,16,13,13,14,
	15,16,11,12,11,16,14,15,16,16,17,17,14,15,12,17,
	12,
};

static float _vq_quantthresh__44u1__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u1__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u1__p4_0 = {
	_vq_quantthresh__44u1__p4_0,
	_vq_quantmap__44u1__p4_0,
	5,
	5
};

static static_codebook _44u1__p4_0 = {
	4, 625,
	_vq_lengthlist__44u1__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u1__p4_0,
	NULL,
	&_vq_auxt__44u1__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u1__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u1__p5_0[] = {
	 1, 4, 4, 7, 7, 7, 7, 9, 9, 4, 6, 6, 8, 8, 8, 8,
	 9, 9, 4, 6, 6, 8, 8, 8, 8, 9, 9, 7, 8, 8, 9, 9,
	 9, 9,11,10, 7, 8, 8, 9, 9, 9, 9,10,10, 7, 8, 8,
	 9, 9,10,10,11,11, 7, 8, 8, 9, 9,10,10,11,11, 9,
	 9, 9,10,10,11,11,12,12, 9, 9, 9,10,11,11,11,12,
	12,
};

static float _vq_quantthresh__44u1__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u1__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u1__p5_0 = {
	_vq_quantthresh__44u1__p5_0,
	_vq_quantmap__44u1__p5_0,
	9,
	9
};

static static_codebook _44u1__p5_0 = {
	2, 81,
	_vq_lengthlist__44u1__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u1__p5_0,
	NULL,
	&_vq_auxt__44u1__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u1__p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u1__p6_0[] = {
	 1, 4, 4, 6, 6, 8, 8,10, 9,11,10,14,13, 4, 6, 5,
	 8, 8, 9, 9,11,10,11,11,14,14, 4, 5, 6, 8, 8, 9,
	 9,10,10,11,11,14,14, 6, 8, 8, 9, 9,10,10,11,11,
	12,12,16,15, 7, 8, 8, 9, 9,10,10,11,11,12,12,15,
	15, 9,10,10,10,10,11,11,12,12,12,12,15,15, 9,10,
	 9,10,11,11,11,12,12,12,13,15,15,10,10,11,11,11,
	12,12,13,12,13,13,16,15,10,11,11,11,11,12,12,13,
	12,13,13,16,17,11,11,12,12,12,13,13,13,14,14,15,
	17,17,11,11,12,12,12,13,13,13,14,14,14,16,18,14,
	15,15,15,15,16,16,16,16,17,18, 0, 0,14,15,15,15,
	15,17,16,17,18,17,17,18, 0,
};

static float _vq_quantthresh__44u1__p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44u1__p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u1__p6_0 = {
	_vq_quantthresh__44u1__p6_0,
	_vq_quantmap__44u1__p6_0,
	13,
	13
};

static static_codebook _44u1__p6_0 = {
	2, 169,
	_vq_lengthlist__44u1__p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44u1__p6_0,
	NULL,
	&_vq_auxt__44u1__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u1__p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u1__p6_1[] = {
	 2, 4, 4, 5, 5, 4, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5,
	 6, 6, 6, 6, 5, 6, 6, 6, 6,
};

static float _vq_quantthresh__44u1__p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u1__p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u1__p6_1 = {
	_vq_quantthresh__44u1__p6_1,
	_vq_quantmap__44u1__p6_1,
	5,
	5
};

static static_codebook _44u1__p6_1 = {
	2, 25,
	_vq_lengthlist__44u1__p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u1__p6_1,
	NULL,
	&_vq_auxt__44u1__p6_1,
	NULL,
	0
};

static long _vq_quantlist__44u1__p7_0[] = {
	3,
	2,
	4,
	1,
	5,
	0,
	6,
};

static long _vq_lengthlist__44u1__p7_0[] = {
	 1, 3, 2, 9, 9, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8,
};

static float _vq_quantthresh__44u1__p7_0[] = {
	-422.5, -253.5, -84.5, 84.5, 253.5, 422.5,
};

static long _vq_quantmap__44u1__p7_0[] = {
		5,	3,	1,	0,	2,	4,	6,
};

static encode_aux_threshmatch _vq_auxt__44u1__p7_0 = {
	_vq_quantthresh__44u1__p7_0,
	_vq_quantmap__44u1__p7_0,
	7,
	7
};

static static_codebook _44u1__p7_0 = {
	2, 49,
	_vq_lengthlist__44u1__p7_0,
	1, -518017024, 1626677248, 3, 0,
	_vq_quantlist__44u1__p7_0,
	NULL,
	&_vq_auxt__44u1__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u1__p7_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u1__p7_1[] = {
	 1, 4, 4, 6, 6, 6, 6, 7, 7, 8, 8, 9, 9, 5, 7, 7,
	 8, 7, 7, 7, 9, 8,10, 9,10,11, 5, 7, 7, 8, 8, 7,
	 7, 8, 9,10,10,11,11, 6, 8, 8, 9, 9, 9, 9,11,10,
	12,12,15,12, 6, 8, 8, 9, 9, 9, 9,11,11,12,11,14,
	12, 7, 8, 8,10,10,12,12,13,13,13,15,13,13, 7, 8,
	 8,10,10,11,11,13,12,14,15,15,15, 9,10,10,11,12,
	13,13,14,15,14,15,14,15, 8,10,10,12,12,14,14,15,
	14,14,15,15,14,10,12,12,14,14,15,14,15,15,15,14,
	15,15,10,12,12,13,14,15,14,15,15,14,15,15,15,12,
	15,13,15,14,15,15,15,15,15,15,15,15,13,13,15,15,
	15,15,15,15,15,15,15,15,15,
};

static float _vq_quantthresh__44u1__p7_1[] = {
	-71.5, -58.5, -45.5, -32.5, -19.5, -6.5, 6.5, 19.5,
	32.5, 45.5, 58.5, 71.5,
};

static long _vq_quantmap__44u1__p7_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u1__p7_1 = {
	_vq_quantthresh__44u1__p7_1,
	_vq_quantmap__44u1__p7_1,
	13,
	13
};

static static_codebook _44u1__p7_1 = {
	2, 169,
	_vq_lengthlist__44u1__p7_1,
	1, -523010048, 1618608128, 4, 0,
	_vq_quantlist__44u1__p7_1,
	NULL,
	&_vq_auxt__44u1__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u1__p7_2[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u1__p7_2[] = {
	 2, 5, 4, 6, 6, 7, 7, 8, 8, 8, 8, 9, 8, 5, 5, 6,
	 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 5, 6, 5, 7, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 6, 7, 7, 8, 8, 8, 8, 9, 8,
	 9, 9, 9, 9, 6, 7, 7, 8, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 7, 8,
	 8, 9, 8, 9, 8, 9, 9, 9, 9, 9, 9, 8, 9, 8, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9,10, 9,10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10, 9, 9, 9, 9, 9,
	 9, 9, 9,10, 9, 9,10,10, 9,
};

static float _vq_quantthresh__44u1__p7_2[] = {
	-5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5,
	2.5, 3.5, 4.5, 5.5,
};

static long _vq_quantmap__44u1__p7_2[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u1__p7_2 = {
	_vq_quantthresh__44u1__p7_2,
	_vq_quantmap__44u1__p7_2,
	13,
	13
};

static static_codebook _44u1__p7_2 = {
	2, 169,
	_vq_lengthlist__44u1__p7_2,
	1, -531103744, 1611661312, 4, 0,
	_vq_quantlist__44u1__p7_2,
	NULL,
	&_vq_auxt__44u1__p7_2,
	NULL,
	0
};

static long _huff_lengthlist__44u1__short[] = {
	12,13,14,13,17,12,15,17, 5, 5, 6,10,10,11,15,16,
	 4, 3, 3, 7, 5, 7,10,16, 7, 7, 7,10, 9,11,12,16,
	 6, 5, 5, 9, 5, 6,10,16, 8, 7, 7, 9, 6, 7, 9,16,
	11, 7, 3, 6, 4, 5, 8,16,12, 9, 4, 8, 5, 7, 9,16,
};

static static_codebook _huff_book__44u1__short = {
	2, 64,
	_huff_lengthlist__44u1__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u2__long[] = {
	 5, 9,14,12,15,13,10,13, 7, 4, 5, 6, 8, 7, 8,12,
	13, 4, 3, 5, 5, 6, 9,15,12, 6, 5, 6, 6, 6, 7,14,
	14, 7, 4, 6, 4, 6, 8,15,12, 6, 6, 5, 5, 5, 6,14,
	 9, 7, 8, 6, 7, 5, 4,10,10,13,14,14,15,10, 6, 8,
};

static static_codebook _huff_book__44u2__long = {
	2, 64,
	_huff_lengthlist__44u2__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u2__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u2__p1_0[] = {
	 1, 4, 4, 5, 8, 7, 5, 7, 8, 5, 8, 8, 8,11,11, 8,
	10,11, 5, 8, 8, 8,11,10, 8,11,11, 4, 8, 8, 8,11,
	11, 8,11,11, 8,11,11,11,13,14,11,13,13, 7,11,11,
	10,13,12,11,14,14, 4, 8, 8, 8,11,11, 8,11,11, 8,
	11,11,11,14,13,10,12,13, 8,11,11,11,13,13,11,13,
	13,
};

static float _vq_quantthresh__44u2__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u2__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u2__p1_0 = {
	_vq_quantthresh__44u2__p1_0,
	_vq_quantmap__44u2__p1_0,
	3,
	3
};

static static_codebook _44u2__p1_0 = {
	4, 81,
	_vq_lengthlist__44u2__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u2__p1_0,
	NULL,
	&_vq_auxt__44u2__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u2__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u2__p2_0[] = {
	 2, 5, 5, 5, 6, 6, 5, 6, 6, 5, 6, 6, 7, 8, 8, 6,
	 8, 8, 5, 6, 6, 6, 8, 7, 7, 8, 8, 5, 6, 6, 7, 8,
	 8, 6, 8, 8, 6, 8, 8, 8, 9,10, 8,10,10, 6, 8, 8,
	 7,10, 8, 8,10,10, 5, 6, 6, 6, 8, 8, 7, 8, 8, 6,
	 8, 8, 8,10,10, 8, 8,10, 6, 8, 8, 8,10,10, 8,10,
	 9,
};

static float _vq_quantthresh__44u2__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u2__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u2__p2_0 = {
	_vq_quantthresh__44u2__p2_0,
	_vq_quantmap__44u2__p2_0,
	3,
	3
};

static static_codebook _44u2__p2_0 = {
	4, 81,
	_vq_lengthlist__44u2__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u2__p2_0,
	NULL,
	&_vq_auxt__44u2__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u2__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u2__p3_0[] = {
	 2, 4, 4, 7, 8, 5, 7, 7, 9, 9, 5, 7, 7, 9, 9, 8,
	 9, 9,12,11, 8, 9, 9,11,12, 5, 7, 7,10,10, 7, 9,
	 9,11,11, 7, 9, 9,10,11,10,11,11,13,13, 9,10,11,
	12,13, 5, 7, 7,10,10, 7, 9, 9,11,10, 7, 9, 9,11,
	11, 9,11,10,13,13,10,11,11,13,13, 8,10,10,14,13,
	10,11,11,15,14, 9,11,11,15,14,13,14,13,16,14,12,
	13,13,15,16, 8,10,10,13,14, 9,11,11,14,15,10,11,
	11,14,15,12,13,13,15,15,12,13,14,15,16, 5, 7, 7,
	10,10, 7, 9, 9,11,11, 7, 9, 9,11,12,10,11,11,14,
	13,10,11,11,14,14, 7, 9, 9,12,12, 9,11,11,13,13,
	 9,11,11,13,13,12,13,12,14,14,11,12,13,15,15, 7,
	 9, 9,12,12, 8,11,10,13,12, 9,11,11,13,13,11,13,
	12,15,13,11,13,13,15,16, 9,12,11,15,15,11,12,12,
	16,15,11,12,13,16,16,13,14,15,16,15,13,15,15,17,
	17, 9,11,11,14,15,10,12,12,15,15,11,13,12,15,16,
	13,15,14,16,16,13,15,15,17,19, 5, 7, 7,10,10, 7,
	 9, 9,12,11, 7, 9, 9,11,11,10,11,11,14,14,10,11,
	11,13,14, 7, 9, 9,12,12, 9,11,11,13,13, 9,10,11,
	12,13,11,13,12,16,15,11,12,12,14,15, 7, 9, 9,12,
	12, 9,11,11,13,13, 9,11,11,13,12,11,13,12,15,16,
	12,13,13,15,14, 9,11,11,15,14,11,13,12,16,15,10,
	11,12,15,15,13,14,14,18,17,13,14,14,15,17,10,11,
	11,14,15,11,13,12,15,17,11,13,12,15,16,13,15,14,
	18,17,14,15,15,16,18, 7,10,10,14,14,10,12,12,15,
	15,10,12,12,15,15,14,15,15,18,17,13,15,15,16,16,
	 9,11,11,16,15,11,13,13,16,18,11,13,13,16,16,15,
	16,16, 0, 0,14,15,16,18,17, 9,11,11,15,15,10,13,
	12,17,16,11,12,13,16,17,14,15,16,19,19,14,15,15,
	 0,20,12,14,14, 0, 0,13,14,16,19,18,13,15,16,20,
	17,16,18, 0, 0, 0,15,16,17,18,19,11,14,14, 0,19,
	12,15,14,17,17,13,15,15, 0, 0,16,17,15,20,19,15,
	17,16,19, 0, 8,10,10,14,15,10,12,11,15,15,10,11,
	12,16,15,13,14,14,19,17,14,15,15, 0, 0, 9,11,11,
	16,15,11,13,13,17,16,10,12,13,16,17,14,15,15,18,
	18,14,15,16,20,19, 9,12,12, 0,15,11,13,13,16,17,
	11,13,13,19,17,14,16,16,18,17,15,16,16,17,19,11,
	14,14,18,18,13,14,15, 0, 0,12,14,15,19,18,15,16,
	19, 0,19,15,16,19,19,17,12,14,14,16,19,13,15,15,
	 0,17,13,15,14,18,18,15,16,15, 0,18,16,17,17, 0,
	 0,
};

static float _vq_quantthresh__44u2__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u2__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u2__p3_0 = {
	_vq_quantthresh__44u2__p3_0,
	_vq_quantmap__44u2__p3_0,
	5,
	5
};

static static_codebook _44u2__p3_0 = {
	4, 625,
	_vq_lengthlist__44u2__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u2__p3_0,
	NULL,
	&_vq_auxt__44u2__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u2__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u2__p4_0[] = {
	 4, 5, 5, 8, 8, 5, 7, 6, 9, 9, 5, 6, 7, 9, 9, 9,
	 9, 9,11,11, 9, 9, 9,11,11, 5, 7, 7, 9, 9, 7, 8,
	 8,10,10, 7, 7, 8,10,10,10,10,10,11,12, 9,10,10,
	11,12, 5, 7, 7, 9, 9, 6, 8, 7,10,10, 7, 8, 8,10,
	10, 9,10,10,12,11, 9,10,10,12,11, 9,10,10,12,12,
	10,10,10,13,12, 9,10,10,12,13,12,12,12,14,14,11,
	12,12,13,14, 9,10,10,12,12, 9,10,10,12,13,10,10,
	10,12,13,11,12,12,14,13,12,12,12,14,13, 5, 7, 7,
	10, 9, 7, 8, 8,10,10, 7, 8, 8,10,10,10,10,10,12,
	12,10,10,10,12,12, 7, 8, 8,11,10, 8, 8, 9,11,11,
	 8, 9, 9,11,11,10,11,11,12,13,10,11,11,13,13, 6,
	 8, 8,10,10, 7, 9, 8,11,10, 8, 9, 9,11,11,10,11,
	10,13,11,10,11,11,13,13, 9,10,10,13,13,10,11,11,
	13,13,10,11,11,14,13,12,11,13,12,15,12,13,13,15,
	15, 9,10,10,12,13,10,11,10,13,13,10,11,11,13,13,
	12,13,11,15,13,12,13,13,15,15, 5, 7, 7, 9,10, 7,
	 8, 8,10,10, 7, 8, 8,10,10,10,10,10,12,12,10,10,
	11,12,12, 6, 8, 8,10,10, 8, 9, 9,11,11, 7, 8, 9,
	10,11,10,11,11,13,13,10,10,11,11,13, 7, 8, 8,10,
	11, 8, 9, 9,11,11, 8, 9, 8,11,11,10,11,11,13,13,
	10,11,11,13,12, 9,10,10,13,12,10,11,11,14,13,10,
	10,11,13,13,12,13,13,15,15,12,11,13,12,14, 9,10,
	10,12,13,10,11,11,13,14,10,11,11,13,13,12,13,13,
	15,15,12,13,12,15,12, 8, 9, 9,12,12, 9,11,10,13,
	13, 9,10,10,13,13,12,13,13,15,15,12,12,12,14,14,
	 9,10,10,13,13,10,11,11,13,14,10,11,11,14,12,13,
	13,14,14,16,12,13,13,15,14, 9,10,10,13,13,10,11,
	10,14,13,10,11,11,13,14,12,14,13,16,14,13,13,13,
	14,15,11,13,12,15,14,11,12,13,14,15,12,13,13,16,
	15,14,12,15,12,16,14,15,15,17,16,11,12,12,14,15,
	11,13,11,15,14,12,13,13,15,16,13,15,12,17,13,14,
	15,15,16,16, 8, 9, 9,12,12, 9,10,10,13,13, 9,10,
	10,13,13,12,13,12,14,14,12,13,13,15,15, 9,10,10,
	13,13,10,11,11,14,13,10,10,11,13,14,12,13,13,15,
	14,12,12,14,14,16, 9,10,10,13,13,10,11,11,13,14,
	10,11,11,14,13,13,13,13,15,15,13,14,13,16,14,11,
	12,12,14,14,12,13,13,16,15,11,12,13,14,15,14,15,
	15,16,16,14,13,15,13,17,11,12,12,14,15,12,13,13,
	15,16,11,13,12,15,15,14,15,14,16,16,14,15,12,17,
	13,
};

static float _vq_quantthresh__44u2__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u2__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u2__p4_0 = {
	_vq_quantthresh__44u2__p4_0,
	_vq_quantmap__44u2__p4_0,
	5,
	5
};

static static_codebook _44u2__p4_0 = {
	4, 625,
	_vq_lengthlist__44u2__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u2__p4_0,
	NULL,
	&_vq_auxt__44u2__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u2__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u2__p5_0[] = {
	 1, 4, 4, 7, 7, 8, 8, 9, 9, 4, 6, 5, 8, 8, 8, 8,
	10,10, 4, 5, 6, 8, 8, 8, 8,10,10, 7, 8, 8, 9, 9,
	 9, 9,11,11, 7, 8, 8, 9, 9, 9, 9,11,11, 8, 8, 8,
	 9, 9,10,11,12,12, 8, 8, 8, 9, 9,10,10,12,12,10,
	10,10,11,11,12,12,13,13,10,10,10,11,11,12,12,13,
	13,
};

static float _vq_quantthresh__44u2__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u2__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u2__p5_0 = {
	_vq_quantthresh__44u2__p5_0,
	_vq_quantmap__44u2__p5_0,
	9,
	9
};

static static_codebook _44u2__p5_0 = {
	2, 81,
	_vq_lengthlist__44u2__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u2__p5_0,
	NULL,
	&_vq_auxt__44u2__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u2__p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u2__p6_0[] = {
	 1, 4, 4, 6, 6, 8, 8,10,10,11,11,14,13, 4, 6, 5,
	 8, 8, 9, 9,11,10,12,11,15,14, 4, 5, 6, 8, 8, 9,
	 9,11,11,11,11,14,14, 6, 8, 8,10, 9,11,11,11,11,
	12,12,15,15, 6, 8, 8, 9, 9,11,11,11,12,12,12,15,
	15, 8,10,10,11,11,11,11,12,12,13,13,15,16, 8,10,
	10,11,11,11,11,12,12,13,13,16,16,10,11,11,12,12,
	12,12,13,13,13,13,17,16,10,11,11,12,12,12,12,13,
	13,13,14,16,17,11,12,12,13,13,13,13,14,14,15,14,
	18,17,11,12,12,13,13,13,13,14,14,14,15,19,18,14,
	15,15,15,15,16,16,18,19,18,18, 0, 0,14,15,15,16,
	15,17,17,16,18,17,18, 0, 0,
};

static float _vq_quantthresh__44u2__p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44u2__p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u2__p6_0 = {
	_vq_quantthresh__44u2__p6_0,
	_vq_quantmap__44u2__p6_0,
	13,
	13
};

static static_codebook _44u2__p6_0 = {
	2, 169,
	_vq_lengthlist__44u2__p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44u2__p6_0,
	NULL,
	&_vq_auxt__44u2__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u2__p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u2__p6_1[] = {
	 2, 4, 4, 5, 5, 4, 5, 5, 6, 5, 4, 5, 5, 5, 6, 5,
	 6, 5, 6, 6, 5, 5, 6, 6, 6,
};

static float _vq_quantthresh__44u2__p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u2__p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u2__p6_1 = {
	_vq_quantthresh__44u2__p6_1,
	_vq_quantmap__44u2__p6_1,
	5,
	5
};

static static_codebook _44u2__p6_1 = {
	2, 25,
	_vq_lengthlist__44u2__p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u2__p6_1,
	NULL,
	&_vq_auxt__44u2__p6_1,
	NULL,
	0
};

static long _vq_quantlist__44u2__p7_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u2__p7_0[] = {
	 1, 3, 2,12,12,12,12,12,12, 4,12,12,12,12,12,12,
	12,12, 5,12,12,12,12,12,12,12,12,12,12,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,
};

static float _vq_quantthresh__44u2__p7_0[] = {
	-591.5, -422.5, -253.5, -84.5, 84.5, 253.5, 422.5, 591.5,
};

static long _vq_quantmap__44u2__p7_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u2__p7_0 = {
	_vq_quantthresh__44u2__p7_0,
	_vq_quantmap__44u2__p7_0,
	9,
	9
};

static static_codebook _44u2__p7_0 = {
	2, 81,
	_vq_lengthlist__44u2__p7_0,
	1, -516612096, 1626677248, 4, 0,
	_vq_quantlist__44u2__p7_0,
	NULL,
	&_vq_auxt__44u2__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u2__p7_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u2__p7_1[] = {
	 1, 4, 4, 7, 6, 7, 6, 8, 7, 9, 7, 9, 8, 4, 7, 6,
	 8, 8, 9, 8,10, 9,10,10,11,11, 4, 7, 7, 8, 8, 8,
	 8, 9,10,11,11,11,11, 6, 8, 8,10,10,10,10,11,11,
	12,12,12,12, 7, 8, 8,10,10,10,10,11,11,12,12,13,
	13, 7, 9, 9,11,10,12,12,13,13,14,13,14,14, 7, 9,
	 9,10,11,11,12,13,13,13,13,16,14, 9,10,10,12,12,
	13,13,14,14,15,16,15,16, 9,10,10,12,12,12,13,14,
	14,14,15,16,15,10,12,12,13,13,15,13,16,16,15,17,
	17,17,10,11,11,12,14,14,14,15,15,17,17,15,17,11,
	12,12,14,14,14,15,15,15,17,16,17,17,10,12,12,13,
	14,14,14,17,15,17,17,17,17,
};

static float _vq_quantthresh__44u2__p7_1[] = {
	-71.5, -58.5, -45.5, -32.5, -19.5, -6.5, 6.5, 19.5,
	32.5, 45.5, 58.5, 71.5,
};

static long _vq_quantmap__44u2__p7_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u2__p7_1 = {
	_vq_quantthresh__44u2__p7_1,
	_vq_quantmap__44u2__p7_1,
	13,
	13
};

static static_codebook _44u2__p7_1 = {
	2, 169,
	_vq_lengthlist__44u2__p7_1,
	1, -523010048, 1618608128, 4, 0,
	_vq_quantlist__44u2__p7_1,
	NULL,
	&_vq_auxt__44u2__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u2__p7_2[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u2__p7_2[] = {
	 2, 5, 5, 6, 6, 7, 7, 8, 7, 8, 8, 8, 8, 5, 6, 6,
	 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 5, 6, 6, 7, 7, 8,
	 7, 8, 8, 8, 8, 8, 8, 6, 7, 7, 7, 8, 8, 8, 8, 8,
	 9, 9, 9, 9, 6, 7, 7, 8, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 7, 8,
	 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,
};

static float _vq_quantthresh__44u2__p7_2[] = {
	-5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5,
	2.5, 3.5, 4.5, 5.5,
};

static long _vq_quantmap__44u2__p7_2[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u2__p7_2 = {
	_vq_quantthresh__44u2__p7_2,
	_vq_quantmap__44u2__p7_2,
	13,
	13
};

static static_codebook _44u2__p7_2 = {
	2, 169,
	_vq_lengthlist__44u2__p7_2,
	1, -531103744, 1611661312, 4, 0,
	_vq_quantlist__44u2__p7_2,
	NULL,
	&_vq_auxt__44u2__p7_2,
	NULL,
	0
};

static long _huff_lengthlist__44u2__short[] = {
	13,15,17,17,15,15,12,17,11, 9, 7,10,10, 9,12,17,
	10, 6, 3, 6, 5, 7,10,17,15,10, 6, 9, 8, 9,11,17,
	15, 8, 4, 7, 3, 5, 9,16,16,10, 5, 8, 4, 5, 8,16,
	13,11, 5, 8, 3, 3, 5,14,13,12, 7,10, 5, 5, 7,14,
};

static static_codebook _huff_book__44u2__short = {
	2, 64,
	_huff_lengthlist__44u2__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u3__long[] = {
	 6, 9,13,12,14,11,10,13, 8, 4, 5, 7, 8, 7, 8,12,
	11, 4, 3, 5, 5, 7, 9,14,11, 6, 5, 6, 6, 6, 7,13,
	13, 7, 5, 6, 4, 5, 7,14,11, 7, 6, 6, 5, 5, 6,13,
	 9, 7, 8, 6, 7, 5, 3, 9, 9,12,13,12,14,10, 6, 7,
};

static static_codebook _huff_book__44u3__long = {
	2, 64,
	_huff_lengthlist__44u3__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u3__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u3__p1_0[] = {
	 1, 4, 4, 5, 8, 7, 5, 7, 8, 5, 8, 8, 8,10,11, 8,
	10,11, 5, 8, 8, 8,11,10, 8,11,11, 4, 8, 8, 8,11,
	11, 8,11,11, 8,11,11,11,13,14,11,14,14, 8,11,11,
	10,14,12,11,14,14, 4, 8, 8, 8,11,11, 8,11,11, 7,
	11,11,11,14,14,10,12,14, 8,11,11,11,14,14,11,14,
	13,
};

static float _vq_quantthresh__44u3__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u3__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u3__p1_0 = {
	_vq_quantthresh__44u3__p1_0,
	_vq_quantmap__44u3__p1_0,
	3,
	3
};

static static_codebook _44u3__p1_0 = {
	4, 81,
	_vq_lengthlist__44u3__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u3__p1_0,
	NULL,
	&_vq_auxt__44u3__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u3__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u3__p2_0[] = {
	 2, 5, 4, 5, 6, 6, 5, 6, 6, 5, 6, 6, 7, 8, 8, 6,
	 8, 8, 5, 6, 6, 6, 8, 8, 7, 8, 8, 5, 7, 6, 7, 8,
	 8, 6, 8, 8, 7, 8, 8, 8, 9,10, 8,10,10, 6, 8, 8,
	 8,10, 8, 8,10,10, 5, 6, 6, 6, 8, 8, 7, 8, 8, 6,
	 8, 8, 8,10,10, 8, 8,10, 7, 8, 8, 8,10,10, 8,10,
	 9,
};

static float _vq_quantthresh__44u3__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u3__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u3__p2_0 = {
	_vq_quantthresh__44u3__p2_0,
	_vq_quantmap__44u3__p2_0,
	3,
	3
};

static static_codebook _44u3__p2_0 = {
	4, 81,
	_vq_lengthlist__44u3__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u3__p2_0,
	NULL,
	&_vq_auxt__44u3__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u3__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u3__p3_0[] = {
	 2, 4, 4, 7, 7, 5, 7, 7, 9, 9, 5, 7, 7, 9, 9, 8,
	 9, 9,12,12, 8, 9, 9,11,12, 5, 7, 7,10,10, 7, 9,
	 9,11,11, 7, 9, 9,10,11,10,11,11,13,13, 9,10,11,
	13,13, 5, 7, 7,10,10, 7, 9, 9,11,10, 7, 9, 9,11,
	11, 9,11,10,13,13,10,11,11,14,13, 8,10,10,14,13,
	10,11,11,15,14, 9,11,11,14,14,13,14,13,16,16,12,
	13,13,15,15, 8,10,10,13,14, 9,11,11,14,14,10,11,
	11,14,15,12,13,13,15,15,13,14,14,15,16, 5, 7, 7,
	10,10, 7, 9, 9,11,11, 7, 9, 9,11,12,10,11,11,14,
	14,10,11,11,14,14, 7, 9, 9,12,12, 9,11,11,13,13,
	 9,11,11,13,13,12,12,13,15,15,11,12,13,15,16, 7,
	 9, 9,11,11, 8,11,10,13,12, 9,11,11,13,13,11,13,
	12,15,13,11,13,13,15,16, 9,12,11,15,14,11,12,13,
	16,15,11,13,13,15,16,14,14,15,17,16,13,15,16, 0,
	17, 9,11,11,15,15,10,13,12,15,15,11,13,13,15,16,
	13,15,13,16,15,14,16,15, 0,19, 5, 7, 7,10,10, 7,
	 9, 9,11,11, 7, 9, 9,11,11,10,12,11,14,14,10,11,
	12,14,14, 7, 9, 9,12,12, 9,11,11,14,13, 9,10,11,
	12,13,11,13,13,16,16,11,12,13,13,16, 7, 9, 9,12,
	12, 9,11,11,13,13, 9,11,11,13,13,11,13,13,15,15,
	12,13,12,15,14, 9,11,11,15,14,11,13,12,16,16,10,
	12,12,15,15,13,15,15,17,19,13,14,15,16,17,10,12,
	12,15,15,11,13,13,16,16,11,13,13,15,16,13,15,15,
	 0, 0,14,15,15,16,16, 8,10,10,14,14,10,12,12,15,
	15,10,12,11,15,16,14,15,15,19,20,13,14,14,18,16,
	 9,11,11,15,15,11,13,13,17,16,11,13,13,16,16,15,
	17,17,20,20,14,15,16,17,20, 9,11,11,15,15,10,13,
	12,16,15,11,13,13,15,17,14,16,15,18, 0,14,16,15,
	18,20,12,14,14, 0, 0,14,14,16, 0, 0,13,16,15, 0,
	 0,17,17,18, 0, 0,16,17,19,19, 0,12,14,14,18, 0,
	12,16,14, 0,17,13,15,15,18, 0,16,18,17, 0,17,16,
	18,17, 0, 0, 7,10,10,14,14,10,12,11,15,15,10,12,
	12,16,15,13,15,15,18, 0,14,15,15,17, 0, 9,11,11,
	15,15,11,13,13,16,16,11,12,13,16,16,14,15,16,17,
	17,14,16,16,16,18, 9,11,12,16,16,11,13,13,17,17,
	11,14,13,20,17,15,16,16,19, 0,15,16,17, 0,19,11,
	13,14,17,16,14,15,15,20,18,13,14,15,17,19,16,18,
	18, 0,20,16,16,19,17, 0,12,15,14,17, 0,14,15,15,
	18,19,13,16,15,19,20,15,18,18, 0,20,17, 0,16, 0,
	 0,
};

static float _vq_quantthresh__44u3__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u3__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u3__p3_0 = {
	_vq_quantthresh__44u3__p3_0,
	_vq_quantmap__44u3__p3_0,
	5,
	5
};

static static_codebook _44u3__p3_0 = {
	4, 625,
	_vq_lengthlist__44u3__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u3__p3_0,
	NULL,
	&_vq_auxt__44u3__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u3__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u3__p4_0[] = {
	 4, 5, 5, 8, 8, 5, 7, 6, 9, 9, 5, 6, 7, 9, 9, 9,
	 9, 9,11,11, 9, 9, 9,11,11, 5, 7, 7, 9, 9, 7, 8,
	 8,10,10, 7, 7, 8,10,10, 9,10,10,11,12, 9,10,10,
	11,12, 5, 7, 7, 9, 9, 7, 8, 7,10,10, 7, 8, 8,10,
	10, 9,10, 9,12,11, 9,10,10,12,11, 9,10, 9,12,12,
	 9,10,10,13,12, 9,10,10,12,13,12,12,12,14,14,11,
	12,12,13,14, 9, 9,10,12,12, 9,10,10,12,12, 9,10,
	10,12,13,11,12,11,14,13,12,12,12,14,13, 5, 7, 7,
	 9, 9, 7, 8, 8,10,10, 7, 8, 8,10,10,10,10,10,12,
	12, 9,10,10,12,12, 7, 8, 8,11,10, 8, 8, 9,11,11,
	 8, 9, 9,11,11,11,11,11,12,13,10,11,11,13,13, 6,
	 8, 8,10,10, 7, 9, 8,11,10, 8, 9, 9,11,11,10,11,
	10,13,11,10,11,11,13,13, 9,11,10,13,12,10,11,11,
	13,13,10,11,11,13,13,12,12,13,12,15,12,13,13,15,
	15, 9,10,10,12,13,10,11,10,13,12,10,11,11,13,14,
	12,13,11,15,13,12,13,13,15,15, 5, 7, 7, 9, 9, 7,
	 8, 8,10,10, 7, 8, 8,10,10, 9,10,10,12,12,10,10,
	11,12,12, 6, 8, 8,10,10, 8, 9, 9,11,11, 7, 8, 9,
	10,11,10,11,11,13,13,10,10,11,11,13, 7, 8, 8,10,
	10, 8, 9, 9,11,11, 8, 9, 9,11,11,10,11,11,13,13,
	11,11,11,13,12, 9,10,10,13,12,10,11,11,14,13,10,
	10,11,12,13,12,13,13,15,15,12,11,13,13,14, 9,10,
	11,12,13,10,11,11,13,13,10,11,11,13,13,12,13,13,
	15,15,12,13,12,15,12, 8, 9, 9,12,12, 9,11,10,13,
	13, 9,10,10,13,13,12,13,13,15,14,12,12,12,14,13,
	 9,10,10,13,12,10,11,11,13,13,10,11,11,14,12,13,
	13,14,14,16,12,13,13,15,15, 9,10,10,13,13,10,11,
	10,14,13,10,11,11,13,14,12,14,13,15,14,13,13,13,
	15,15,11,13,12,15,14,11,12,13,14,15,12,13,13,16,
	14,14,12,15,12,16,14,15,15,17,15,11,12,12,14,14,
	11,13,11,15,14,12,13,13,15,15,13,15,12,17,13,14,
	15,15,16,16, 8, 9, 9,12,12, 9,10,10,12,13, 9,10,
	10,13,13,12,12,12,14,14,12,13,13,15,15, 9,10,10,
	13,12,10,11,11,14,13,10,10,11,13,14,12,13,13,15,
	15,12,12,13,14,16, 9,10,10,13,13,10,11,11,13,14,
	10,11,11,14,13,12,13,13,14,15,13,14,13,16,14,11,
	12,12,14,14,12,13,13,15,14,11,12,13,14,15,14,15,
	15,16,16,13,13,15,13,16,11,12,12,14,15,12,13,13,
	14,15,11,13,12,15,14,14,15,15,16,16,14,15,12,16,
	13,
};

static float _vq_quantthresh__44u3__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u3__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u3__p4_0 = {
	_vq_quantthresh__44u3__p4_0,
	_vq_quantmap__44u3__p4_0,
	5,
	5
};

static static_codebook _44u3__p4_0 = {
	4, 625,
	_vq_lengthlist__44u3__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u3__p4_0,
	NULL,
	&_vq_auxt__44u3__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u3__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u3__p5_0[] = {
	 2, 3, 3, 6, 6, 7, 7, 9, 9, 4, 5, 5, 7, 7, 8, 8,
	10,10, 4, 5, 5, 7, 7, 8, 8,10,10, 6, 7, 7, 8, 8,
	 9, 9,11,10, 6, 7, 7, 8, 8, 9, 9,10,10, 7, 8, 8,
	 9, 9,10,10,11,11, 7, 8, 8, 9, 9,10,10,11,11, 9,
	10,10,11,10,11,11,12,12, 9,10,10,10,10,11,11,12,
	12,
};

static float _vq_quantthresh__44u3__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u3__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u3__p5_0 = {
	_vq_quantthresh__44u3__p5_0,
	_vq_quantmap__44u3__p5_0,
	9,
	9
};

static static_codebook _44u3__p5_0 = {
	2, 81,
	_vq_lengthlist__44u3__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u3__p5_0,
	NULL,
	&_vq_auxt__44u3__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u3__p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u3__p6_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 9, 9,10,11,13,14, 4, 6, 5,
	 8, 8, 9, 9,10,10,11,11,14,14, 4, 6, 6, 8, 8, 9,
	 9,10,10,11,11,14,14, 6, 8, 8, 9, 9,10,10,11,11,
	12,12,15,15, 6, 8, 8, 9, 9,10,11,11,11,12,12,15,
	15, 8, 9, 9,11,10,11,11,12,12,13,13,15,16, 8, 9,
	 9,10,11,11,11,12,12,13,13,16,16,10,10,11,11,11,
	12,12,13,13,13,14,17,16, 9,10,11,12,11,12,12,13,
	13,13,13,16,18,11,12,11,12,12,13,13,13,14,15,14,
	17,17,11,11,12,12,12,13,13,13,14,14,15,18,17,14,
	15,15,15,15,16,16,17,17,19,18, 0,20,14,15,14,15,
	15,16,16,16,17,18,16,20,18,
};

static float _vq_quantthresh__44u3__p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44u3__p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u3__p6_0 = {
	_vq_quantthresh__44u3__p6_0,
	_vq_quantmap__44u3__p6_0,
	13,
	13
};

static static_codebook _44u3__p6_0 = {
	2, 169,
	_vq_lengthlist__44u3__p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44u3__p6_0,
	NULL,
	&_vq_auxt__44u3__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u3__p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u3__p6_1[] = {
	 2, 4, 4, 5, 5, 4, 5, 5, 6, 5, 4, 5, 5, 5, 6, 5,
	 6, 5, 6, 6, 5, 5, 6, 6, 6,
};

static float _vq_quantthresh__44u3__p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u3__p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u3__p6_1 = {
	_vq_quantthresh__44u3__p6_1,
	_vq_quantmap__44u3__p6_1,
	5,
	5
};

static static_codebook _44u3__p6_1 = {
	2, 25,
	_vq_lengthlist__44u3__p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u3__p6_1,
	NULL,
	&_vq_auxt__44u3__p6_1,
	NULL,
	0
};

static long _vq_quantlist__44u3__p7_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u3__p7_0[] = {
	 1, 3, 3,10,10,10,10,10,10, 4,10,10,10,10,10,10,
	10,10, 4,10,10,10,10,10,10,10,10,10,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9,
};

static float _vq_quantthresh__44u3__p7_0[] = {
	-892.5, -637.5, -382.5, -127.5, 127.5, 382.5, 637.5, 892.5,
};

static long _vq_quantmap__44u3__p7_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u3__p7_0 = {
	_vq_quantthresh__44u3__p7_0,
	_vq_quantmap__44u3__p7_0,
	9,
	9
};

static static_codebook _44u3__p7_0 = {
	2, 81,
	_vq_lengthlist__44u3__p7_0,
	1, -515907584, 1627381760, 4, 0,
	_vq_quantlist__44u3__p7_0,
	NULL,
	&_vq_auxt__44u3__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u3__p7_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44u3__p7_1[] = {
	 1, 4, 4, 6, 6, 7, 6, 8, 7, 9, 8,10, 9,11,11, 4,
	 7, 7, 8, 7, 9, 9,10,10,11,11,11,11,12,12, 4, 7,
	 7, 7, 7, 9, 9,10,10,11,11,12,12,12,11, 6, 8, 8,
	 9, 9,10,10,11,11,12,12,13,12,13,13, 6, 8, 8, 9,
	 9,10,11,11,11,12,12,13,14,13,13, 8, 9, 9,11,11,
	12,12,12,13,14,13,14,14,14,15, 8, 9, 9,11,11,11,
	12,13,14,13,14,15,17,14,15, 9,10,10,12,12,13,13,
	13,14,15,15,15,16,16,16, 9,11,11,12,12,13,13,14,
	14,14,15,16,16,16,16,10,12,12,13,13,14,14,15,15,
	15,16,17,17,17,17,10,12,11,13,13,15,14,15,14,16,
	17,16,16,16,16,11,13,12,14,14,14,14,15,16,17,16,
	17,17,17,17,11,13,12,14,14,14,15,17,16,17,17,17,
	17,17,17,12,13,13,15,16,15,16,17,17,16,16,17,17,
	17,17,12,13,13,15,15,15,16,17,17,17,16,17,16,17,
	17,
};

static float _vq_quantthresh__44u3__p7_1[] = {
	-110.5, -93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5,
	25.5, 42.5, 59.5, 76.5, 93.5, 110.5,
};

static long _vq_quantmap__44u3__p7_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44u3__p7_1 = {
	_vq_quantthresh__44u3__p7_1,
	_vq_quantmap__44u3__p7_1,
	15,
	15
};

static static_codebook _44u3__p7_1 = {
	2, 225,
	_vq_lengthlist__44u3__p7_1,
	1, -522338304, 1620115456, 4, 0,
	_vq_quantlist__44u3__p7_1,
	NULL,
	&_vq_auxt__44u3__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u3__p7_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44u3__p7_2[] = {
	 2, 5, 5, 7, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	10,10, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 8, 9, 9, 9,
	 9,10, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	10,10,10,10, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,10,
	 9,10,10,10,10, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	10,10,10,10,10,10, 7, 8, 8, 9, 8, 9, 9, 9, 9,10,
	 9,10,10,10,10,10,10, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	 9,10,10,10,10,10,10,10, 8, 9, 8, 9, 9, 9, 9,10,
	 9,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9, 9,10,
	 9,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9,10,
	 9,10,10,10,10,10,10,10,10,10,10, 9, 9, 9,10, 9,
	10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,10,11, 9,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,11, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	 9,10,10,10,10,10,10,10,10,10,10,10,11,11,11,10,
	11,
};

static float _vq_quantthresh__44u3__p7_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44u3__p7_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44u3__p7_2 = {
	_vq_quantthresh__44u3__p7_2,
	_vq_quantmap__44u3__p7_2,
	17,
	17
};

static static_codebook _44u3__p7_2 = {
	2, 289,
	_vq_lengthlist__44u3__p7_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44u3__p7_2,
	NULL,
	&_vq_auxt__44u3__p7_2,
	NULL,
	0
};

static long _huff_lengthlist__44u3__short[] = {
	14,14,14,15,13,15,12,16,10, 8, 7, 9, 9, 8,12,16,
	10, 5, 4, 6, 5, 6, 9,16,14, 8, 6, 8, 7, 8,10,16,
	14, 7, 4, 6, 3, 5, 8,16,15, 9, 5, 7, 4, 4, 7,16,
	13,10, 6, 7, 4, 3, 4,13,13,12, 7, 9, 5, 5, 6,12,
};

static static_codebook _huff_book__44u3__short = {
	2, 64,
	_huff_lengthlist__44u3__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u4__long[] = {
	 3, 8,12,12,13,12,11,13, 5, 4, 6, 7, 8, 8, 9,13,
	 9, 5, 4, 5, 5, 7, 9,13, 9, 6, 5, 6, 6, 7, 8,12,
	12, 7, 5, 6, 4, 5, 8,13,11, 7, 6, 6, 5, 5, 6,12,
	10, 8, 8, 7, 7, 5, 3, 8,10,12,13,12,12, 9, 6, 7,
};

static static_codebook _huff_book__44u4__long = {
	2, 64,
	_huff_lengthlist__44u4__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u4__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u4__p1_0[] = {
	 1, 4, 4, 5, 8, 7, 5, 7, 8, 5, 8, 8, 8,10,11, 8,
	10,11, 5, 8, 8, 8,11,10, 8,11,11, 4, 8, 8, 8,11,
	11, 8,11,11, 8,11,11,11,13,14,11,15,14, 8,11,11,
	10,13,12,11,14,14, 4, 8, 8, 8,11,11, 8,11,11, 7,
	11,11,11,15,14,10,12,14, 8,11,11,11,14,14,11,14,
	13,
};

static float _vq_quantthresh__44u4__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u4__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u4__p1_0 = {
	_vq_quantthresh__44u4__p1_0,
	_vq_quantmap__44u4__p1_0,
	3,
	3
};

static static_codebook _44u4__p1_0 = {
	4, 81,
	_vq_lengthlist__44u4__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u4__p1_0,
	NULL,
	&_vq_auxt__44u4__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u4__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u4__p2_0[] = {
	 2, 5, 5, 5, 6, 6, 5, 6, 6, 5, 6, 6, 7, 8, 8, 6,
	 8, 8, 5, 6, 6, 6, 8, 8, 7, 8, 8, 5, 7, 6, 6, 8,
	 8, 6, 8, 8, 6, 8, 8, 8, 9,10, 8,10,10, 6, 8, 8,
	 8,10, 8, 8,10,10, 5, 6, 6, 6, 8, 8, 6, 8, 8, 6,
	 8, 8, 8,10,10, 8, 8,10, 6, 8, 8, 8,10,10, 8,10,
	 9,
};

static float _vq_quantthresh__44u4__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u4__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u4__p2_0 = {
	_vq_quantthresh__44u4__p2_0,
	_vq_quantmap__44u4__p2_0,
	3,
	3
};

static static_codebook _44u4__p2_0 = {
	4, 81,
	_vq_lengthlist__44u4__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u4__p2_0,
	NULL,
	&_vq_auxt__44u4__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u4__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u4__p3_0[] = {
	 2, 4, 4, 8, 8, 5, 7, 7, 9, 9, 5, 7, 7, 9, 9, 8,
	10, 9,12,12, 8, 9,10,12,12, 5, 7, 7,10,10, 7, 9,
	 9,11,11, 7, 9, 9,11,11,10,12,11,14,14, 9,10,11,
	13,14, 5, 7, 7,10,10, 7, 9, 9,11,11, 7, 9, 9,11,
	11, 9,11,10,14,13,10,11,11,14,14, 8,10,10,14,13,
	10,12,12,15,14, 9,11,11,15,14,13,14,14,17,17,12,
	14,14,16,16, 8,10,10,14,14, 9,11,11,14,15,10,12,
	12,14,15,12,14,13,16,16,13,14,15,15,18, 4, 7, 7,
	10,10, 7, 9, 9,12,11, 7, 9, 9,11,12,10,12,11,15,
	14,10,11,12,14,15, 7, 9, 9,12,12, 9,11,12,13,13,
	 9,11,12,13,13,12,13,13,15,16,11,13,13,15,16, 7,
	 9, 9,12,12, 9,11,10,13,12, 9,11,12,13,14,11,13,
	12,16,14,12,13,13,15,16,10,12,12,16,15,11,13,13,
	17,16,11,13,13,17,16,14,15,15,17,17,14,16,16,18,
	20, 9,11,11,15,16,11,13,12,16,16,11,13,13,16,17,
	14,15,14,18,16,14,16,16,17,20, 5, 7, 7,10,10, 7,
	 9, 9,12,11, 7, 9,10,11,12,10,12,11,15,15,10,12,
	12,14,14, 7, 9, 9,12,12, 9,12,11,14,13, 9,10,11,
	12,13,12,13,14,16,16,11,12,13,14,16, 7, 9, 9,12,
	12, 9,12,11,13,13, 9,12,11,13,13,11,13,13,16,16,
	12,13,13,16,15, 9,11,11,16,14,11,13,13,16,16,11,
	12,13,16,16,14,16,16,17,17,13,14,15,16,17,10,12,
	12,15,15,11,13,13,16,17,11,13,13,16,16,14,16,15,
	19,19,14,15,15,17,18, 8,10,10,14,14,10,12,12,15,
	15,10,12,12,16,16,14,16,15,20,19,13,15,15,17,16,
	 9,12,12,16,16,11,13,13,16,18,11,14,13,16,17,16,
	17,16,20, 0,15,16,18,18,20, 9,11,11,15,15,11,14,
	12,17,16,11,13,13,17,17,15,17,15,20,20,14,16,16,
	17, 0,13,15,14,18,16,14,15,16, 0,18,14,16,16, 0,
	 0,18,16, 0, 0,20,16,18,18, 0, 0,12,14,14,17,18,
	13,15,14,20,18,14,16,15,19,19,16,20,16, 0,18,16,
	19,17,19, 0, 8,10,10,14,14,10,12,12,16,15,10,12,
	12,16,16,13,15,15,18,17,14,16,16,19, 0, 9,11,11,
	16,15,11,14,13,18,17,11,12,13,17,18,14,17,16,18,
	18,15,16,17,18,18, 9,12,12,16,16,11,13,13,16,18,
	11,14,13,17,17,15,16,16,18,20,16,17,17,20,20,12,
	14,14,18,17,14,16,16, 0,19,13,14,15,18, 0,16, 0,
	 0, 0, 0,16,16, 0,19,20,13,15,14, 0, 0,14,16,16,
	18,19,14,16,15, 0,20,16,20,18, 0,20,17,20,17, 0,
	 0,
};

static float _vq_quantthresh__44u4__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u4__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u4__p3_0 = {
	_vq_quantthresh__44u4__p3_0,
	_vq_quantmap__44u4__p3_0,
	5,
	5
};

static static_codebook _44u4__p3_0 = {
	4, 625,
	_vq_lengthlist__44u4__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u4__p3_0,
	NULL,
	&_vq_auxt__44u4__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u4__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u4__p4_0[] = {
	 4, 5, 5, 8, 8, 5, 7, 6, 9, 9, 5, 6, 7, 9, 9, 9,
	 9, 9,11,11, 8, 9, 9,11,11, 5, 7, 7, 9, 9, 7, 8,
	 8,10,10, 7, 7, 8,10,10, 9,10,10,11,12, 9,10,10,
	11,12, 5, 7, 7, 9, 9, 7, 8, 7,10,10, 7, 8, 8,10,
	10, 9,10,10,12,11, 9,10,10,12,11, 9,10, 9,12,12,
	 9,10,10,13,12, 9,10,10,12,12,12,12,12,14,14,11,
	12,12,13,14, 9, 9,10,12,12, 9,10,10,13,13, 9,10,
	10,12,13,11,12,12,14,13,11,12,12,14,14, 5, 7, 7,
	 9, 9, 7, 8, 8,10,10, 7, 8, 8,10,10,10,10,10,12,
	12, 9,10,10,12,12, 7, 8, 8,11,10, 8, 8, 9,11,11,
	 8, 9, 9,11,11,11,11,11,12,13,10,11,11,13,13, 6,
	 8, 8,10,10, 7, 9, 8,11,10, 8, 9, 9,11,11,10,11,
	10,13,11,10,11,11,13,13, 9,11,10,13,12,10,11,11,
	13,14,10,11,11,14,13,12,12,13,12,15,12,13,13,15,
	15, 9,10,10,12,13,10,11,10,13,12,10,11,11,13,14,
	12,13,11,15,13,13,13,13,15,15, 5, 7, 7, 9, 9, 7,
	 8, 8,10,10, 7, 8, 8,10,10, 9,10,10,12,12,10,10,
	11,12,13, 6, 8, 8,10,10, 8, 9, 9,11,11, 7, 8, 9,
	10,11,10,11,11,13,13,10,10,11,11,13, 7, 8, 8,10,
	11, 8, 9, 9,11,11, 8, 9, 8,11,11,10,11,11,13,13,
	11,12,11,13,12, 9,10,10,13,12,10,11,11,14,13,10,
	10,11,12,13,12,13,13,15,15,12,11,13,13,14, 9,10,
	11,12,13,10,11,11,13,14,10,11,11,13,13,12,13,13,
	15,15,12,13,12,15,12, 8, 9, 9,12,12, 9,11,10,13,
	13, 9,10,10,13,13,12,13,13,15,15,12,12,12,14,14,
	 9,10,10,13,13,10,11,11,13,14,10,11,11,14,13,13,
	13,14,14,16,13,13,13,15,15, 9,10,10,13,13,10,11,
	10,14,13,10,11,11,13,14,12,14,13,16,14,12,13,13,
	14,15,11,12,12,15,14,11,12,13,14,15,12,13,13,16,
	15,14,12,15,12,16,14,15,15,16,16,11,12,12,14,14,
	11,13,12,15,14,12,13,13,15,16,13,15,13,17,13,14,
	15,15,16,17, 8, 9, 9,12,12, 9,10,10,12,13, 9,10,
	10,13,13,12,12,12,14,14,12,13,13,15,15, 9,10,10,
	13,12,10,11,11,14,13,10,10,11,13,14,13,13,13,15,
	15,12,13,14,14,16, 9,10,10,13,13,10,11,11,13,14,
	10,11,11,14,14,13,13,13,15,15,13,14,13,16,14,11,
	12,12,15,14,12,13,13,16,15,11,12,13,14,15,14,15,
	15,17,16,13,13,15,13,16,11,12,13,14,15,13,13,13,
	15,16,11,13,12,15,14,14,15,15,16,16,14,15,12,17,
	13,
};

static float _vq_quantthresh__44u4__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u4__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u4__p4_0 = {
	_vq_quantthresh__44u4__p4_0,
	_vq_quantmap__44u4__p4_0,
	5,
	5
};

static static_codebook _44u4__p4_0 = {
	4, 625,
	_vq_lengthlist__44u4__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u4__p4_0,
	NULL,
	&_vq_auxt__44u4__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u4__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u4__p5_0[] = {
	 2, 3, 3, 6, 6, 7, 7, 9, 9, 4, 5, 5, 7, 7, 8, 8,
	10, 9, 4, 5, 5, 7, 7, 8, 8,10,10, 6, 7, 7, 8, 8,
	 9, 9,11,10, 6, 7, 7, 8, 8, 9, 9,10,11, 7, 8, 8,
	 9, 9,10,10,11,11, 7, 8, 8, 9, 9,10,10,11,11, 9,
	10,10,11,10,11,11,12,12, 9,10,10,10,11,11,11,12,
	12,
};

static float _vq_quantthresh__44u4__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u4__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u4__p5_0 = {
	_vq_quantthresh__44u4__p5_0,
	_vq_quantmap__44u4__p5_0,
	9,
	9
};

static static_codebook _44u4__p5_0 = {
	2, 81,
	_vq_lengthlist__44u4__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u4__p5_0,
	NULL,
	&_vq_auxt__44u4__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u4__p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u4__p6_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 9, 9,11,10,13,13, 4, 6, 5,
	 8, 8, 9, 9,10,10,11,11,14,14, 4, 6, 6, 8, 8, 9,
	 9,10,10,11,11,14,14, 6, 8, 8, 9, 9,10,10,11,11,
	12,12,15,15, 6, 8, 8, 9, 9,10,11,11,11,12,12,15,
	15, 8, 9, 9,11,10,11,11,12,12,13,13,16,16, 8, 9,
	 9,10,10,11,11,12,12,13,13,16,16,10,10,10,12,11,
	12,12,13,13,14,14,16,16,10,10,10,11,12,12,12,13,
	13,13,14,16,17,11,12,11,12,12,13,13,14,14,15,14,
	18,17,11,11,12,12,12,13,13,14,14,14,15,19,18,14,
	15,14,15,15,17,16,17,17,17,17,21, 0,14,15,15,16,
	16,16,16,17,17,18,17,20,21,
};

static float _vq_quantthresh__44u4__p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44u4__p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u4__p6_0 = {
	_vq_quantthresh__44u4__p6_0,
	_vq_quantmap__44u4__p6_0,
	13,
	13
};

static static_codebook _44u4__p6_0 = {
	2, 169,
	_vq_lengthlist__44u4__p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44u4__p6_0,
	NULL,
	&_vq_auxt__44u4__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u4__p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u4__p6_1[] = {
	 2, 4, 4, 5, 5, 4, 5, 5, 6, 5, 4, 5, 5, 5, 6, 5,
	 6, 5, 6, 6, 5, 5, 6, 6, 6,
};

static float _vq_quantthresh__44u4__p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u4__p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u4__p6_1 = {
	_vq_quantthresh__44u4__p6_1,
	_vq_quantmap__44u4__p6_1,
	5,
	5
};

static static_codebook _44u4__p6_1 = {
	2, 25,
	_vq_lengthlist__44u4__p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u4__p6_1,
	NULL,
	&_vq_auxt__44u4__p6_1,
	NULL,
	0
};

static long _vq_quantlist__44u4__p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u4__p7_0[] = {
	 1, 3, 3,12,12,12,12,12,12,12,12,12,12, 3,12,11,
	12,12,12,12,12,12,12,12,12,12, 4,11,10,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,
};

static float _vq_quantthresh__44u4__p7_0[] = {
	-1402.5, -1147.5, -892.5, -637.5, -382.5, -127.5, 127.5, 382.5,
	637.5, 892.5, 1147.5, 1402.5,
};

static long _vq_quantmap__44u4__p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u4__p7_0 = {
	_vq_quantthresh__44u4__p7_0,
	_vq_quantmap__44u4__p7_0,
	13,
	13
};

static static_codebook _44u4__p7_0 = {
	2, 169,
	_vq_lengthlist__44u4__p7_0,
	1, -514332672, 1627381760, 4, 0,
	_vq_quantlist__44u4__p7_0,
	NULL,
	&_vq_auxt__44u4__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u4__p7_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44u4__p7_1[] = {
	 1, 4, 4, 6, 6, 7, 7, 9, 8,10, 8,10, 9,11,11, 4,
	 7, 6, 8, 7, 9, 9,10,10,11,10,11,10,12,10, 4, 6,
	 7, 8, 8, 9, 9,10,10,11,11,11,11,12,12, 6, 8, 8,
	10, 9,11,10,12,11,12,12,12,12,13,13, 6, 8, 8,10,
	10,10,11,11,11,12,12,13,12,13,13, 8, 9, 9,11,11,
	12,11,12,12,13,13,13,13,13,13, 8, 9, 9,11,11,11,
	12,12,12,13,13,13,13,13,13, 9,10,10,12,11,13,13,
	13,13,14,13,13,14,14,14, 9,10,11,11,12,12,13,13,
	13,13,13,14,15,14,14,10,11,11,12,12,13,13,14,14,
	14,14,14,15,16,16,10,11,11,12,13,13,13,13,15,14,
	14,15,16,15,16,10,12,12,13,13,14,14,14,15,15,15,
	15,15,15,16,11,12,12,13,13,14,14,14,15,15,15,16,
	15,17,16,11,12,12,13,13,13,15,15,14,16,16,16,16,
	16,17,11,12,12,13,13,14,14,15,14,15,15,17,17,16,
	16,
};

static float _vq_quantthresh__44u4__p7_1[] = {
	-110.5, -93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5,
	25.5, 42.5, 59.5, 76.5, 93.5, 110.5,
};

static long _vq_quantmap__44u4__p7_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44u4__p7_1 = {
	_vq_quantthresh__44u4__p7_1,
	_vq_quantmap__44u4__p7_1,
	15,
	15
};

static static_codebook _44u4__p7_1 = {
	2, 225,
	_vq_lengthlist__44u4__p7_1,
	1, -522338304, 1620115456, 4, 0,
	_vq_quantlist__44u4__p7_1,
	NULL,
	&_vq_auxt__44u4__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u4__p7_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44u4__p7_2[] = {
	 2, 5, 5, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	10,10,10,10, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,10,
	 9,10, 9,10,10, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	10,10,10,10,10,10, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9,10,10,10,10,10,10, 8, 9, 8, 9, 9, 9, 9, 9, 9,
	10,10,10,10,10,10,10,10, 8, 8, 8, 9, 9, 9, 9, 9,
	10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9,10,10,
	10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9,10,
	10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9,
	10,10,10,10,10,10,10,10,10,11,10,10,10, 9, 9, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9, 9,
	 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9,
	10, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	 9,10, 9,10,10,10,10,10,10,10,10,10,10,11,10,10,
	10,
};

static float _vq_quantthresh__44u4__p7_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44u4__p7_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44u4__p7_2 = {
	_vq_quantthresh__44u4__p7_2,
	_vq_quantmap__44u4__p7_2,
	17,
	17
};

static static_codebook _44u4__p7_2 = {
	2, 289,
	_vq_lengthlist__44u4__p7_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44u4__p7_2,
	NULL,
	&_vq_auxt__44u4__p7_2,
	NULL,
	0
};

static long _huff_lengthlist__44u4__short[] = {
	14,17,15,17,16,14,13,16,10, 7, 7,10,13,10,15,16,
	 9, 4, 4, 6, 5, 7, 9,16,12, 8, 7, 8, 8, 8,11,16,
	14, 7, 4, 6, 3, 5, 8,15,13, 8, 5, 7, 4, 5, 7,16,
	12, 9, 6, 8, 3, 3, 5,16,14,13, 7,10, 5, 5, 7,15,
};

static static_codebook _huff_book__44u4__short = {
	2, 64,
	_huff_lengthlist__44u4__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u5__long[] = {
	 3, 8,13,12,14,12,16,11,13,14, 5, 4, 5, 6, 7, 8,
	10, 9,12,15,10, 5, 5, 5, 6, 8, 9, 9,13,15,10, 5,
	 5, 6, 6, 7, 8, 8,11,13,12, 7, 5, 6, 4, 6, 7, 7,
	11,14,11, 7, 7, 6, 6, 6, 7, 6,10,14,14, 9, 8, 8,
	 6, 7, 7, 7,11,16,11, 8, 8, 7, 6, 6, 7, 4, 7,12,
	10,10,12,10,10, 9,10, 5, 6, 9,10,12,15,13,14,14,
	14, 8, 7, 8,
};

static static_codebook _huff_book__44u5__long = {
	2, 100,
	_huff_lengthlist__44u5__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u5__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u5__p1_0[] = {
	 1, 4, 4, 5, 8, 7, 5, 7, 7, 5, 8, 8, 8,10,10, 7,
	 9,10, 5, 8, 8, 7,10, 9, 8,10,10, 5, 8, 8, 8,10,
	10, 8,10,10, 8,10,10,10,12,13,10,13,13, 7,10,10,
	10,13,11,10,13,13, 4, 8, 8, 8,11,10, 8,10,10, 7,
	10,10,10,13,13,10,11,13, 8,10,11,10,13,13,10,13,
	12,
};

static float _vq_quantthresh__44u5__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u5__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u5__p1_0 = {
	_vq_quantthresh__44u5__p1_0,
	_vq_quantmap__44u5__p1_0,
	3,
	3
};

static static_codebook _44u5__p1_0 = {
	4, 81,
	_vq_lengthlist__44u5__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u5__p1_0,
	NULL,
	&_vq_auxt__44u5__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u5__p2_0[] = {
	 3, 4, 4, 5, 6, 6, 5, 6, 6, 5, 6, 6, 6, 8, 8, 6,
	 7, 8, 5, 6, 6, 6, 8, 7, 6, 8, 8, 5, 6, 6, 6, 8,
	 8, 6, 8, 8, 6, 8, 8, 8, 9, 9, 8, 9, 9, 6, 8, 7,
	 7, 9, 8, 8, 9, 9, 5, 6, 6, 6, 8, 7, 6, 8, 8, 6,
	 8, 7, 8, 9, 9, 7, 8, 9, 6, 8, 8, 8, 9, 9, 8, 9,
	 9,
};

static float _vq_quantthresh__44u5__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u5__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u5__p2_0 = {
	_vq_quantthresh__44u5__p2_0,
	_vq_quantmap__44u5__p2_0,
	3,
	3
};

static static_codebook _44u5__p2_0 = {
	4, 81,
	_vq_lengthlist__44u5__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u5__p2_0,
	NULL,
	&_vq_auxt__44u5__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u5__p3_0[] = {
	 2, 4, 5, 8, 8, 5, 7, 6, 9, 9, 5, 6, 7, 9, 9, 8,
	10, 9,13,12, 8, 9,10,12,12, 5, 7, 7,10,10, 7, 9,
	 9,11,11, 6, 8, 9,11,11,10,11,11,14,14, 9,10,11,
	13,14, 5, 7, 7, 9,10, 7, 9, 8,11,11, 7, 9, 9,11,
	11, 9,11,10,14,13,10,11,11,14,14, 8,10,10,13,13,
	10,11,11,15,14, 9,11,11,14,14,13,14,14,17,16,12,
	13,13,15,16, 8,10,10,13,13, 9,11,11,14,15,10,11,
	11,14,15,12,14,13,16,16,13,15,14,15,17, 5, 7, 7,
	10,10, 7, 9, 9,11,11, 7, 9, 9,11,11,10,11,11,14,
	14,10,11,12,14,14, 7, 9, 9,12,11, 9,11,11,13,13,
	 9,11,11,13,13,12,13,13,15,16,11,12,13,15,16, 6,
	 9, 9,11,11, 8,11,10,13,12, 9,11,11,13,14,11,13,
	12,16,14,11,13,13,16,17,10,12,11,15,15,11,13,13,
	16,16,11,13,13,17,16,14,15,15,17,17,14,16,16,17,
	18, 9,11,11,14,15,10,12,12,15,15,11,13,13,16,17,
	13,15,13,17,15,14,15,16,18, 0, 5, 7, 7,10,10, 7,
	 9, 9,11,11, 7, 9, 9,11,11,10,11,11,14,14,10,11,
	12,14,15, 6, 9, 9,12,11, 9,11,11,13,13, 8,10,11,
	12,13,11,13,13,16,15,11,12,13,14,15, 7, 9, 9,11,
	12, 9,11,11,13,13, 9,11,11,13,13,11,13,13,15,16,
	11,13,13,15,14, 9,11,11,15,14,11,13,13,17,15,10,
	12,12,15,15,14,16,16,17,17,13,13,15,15,17,10,11,
	12,15,15,11,13,13,16,16,11,13,13,15,15,14,15,15,
	18,18,14,15,15,17,17, 8,10,10,13,13,10,12,11,15,
	15,10,11,12,15,15,14,15,15,18,18,13,14,14,18,18,
	 9,11,11,15,16,11,13,13,17,17,11,13,13,16,16,15,
	15,16,17, 0,14,15,17, 0, 0, 9,11,11,15,15,10,13,
	12,18,16,11,13,13,15,16,14,16,15,20,20,14,15,16,
	17, 0,13,14,14,20,16,14,15,16,19,18,14,15,15,19,
	 0,18,16, 0,20,20,16,18,18, 0, 0,12,14,14,18,18,
	13,15,14,18,16,14,15,16,18,20,16,19,16, 0,17,17,
	18,18,19, 0, 8,10,10,14,14,10,11,11,14,15,10,11,
	12,15,15,13,15,14,19,17,13,15,15,17, 0, 9,11,11,
	16,15,11,13,13,16,16,10,12,13,15,17,14,16,16,18,
	18,14,15,15,18, 0, 9,11,11,15,15,11,13,13,16,17,
	11,13,13,18,17,14,18,16,18,18,15,17,17,18, 0,12,
	14,14,18,18,14,15,15,20, 0,13,14,15,17, 0,16,18,
	17, 0, 0,16,16, 0,17,20,12,14,14,18,18,14,16,15,
	 0,18,14,16,15,18, 0,16,19,17, 0, 0,17,18,16, 0,
	 0,
};

static float _vq_quantthresh__44u5__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u5__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u5__p3_0 = {
	_vq_quantthresh__44u5__p3_0,
	_vq_quantmap__44u5__p3_0,
	5,
	5
};

static static_codebook _44u5__p3_0 = {
	4, 625,
	_vq_lengthlist__44u5__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u5__p3_0,
	NULL,
	&_vq_auxt__44u5__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u5__p4_0[] = {
	 4, 5, 5, 8, 8, 6, 7, 6, 9, 9, 6, 6, 7, 9, 9, 8,
	 9, 9,11,11, 8, 9, 9,11,11, 6, 7, 7, 9, 9, 7, 8,
	 8,10,10, 6, 7, 8, 9,10, 9,10,10,11,12, 9, 9,10,
	11,12, 6, 7, 7, 9, 9, 6, 8, 7,10, 9, 7, 8, 8,10,
	10, 9,10, 9,12,11, 9,10,10,12,11, 8, 9, 9,12,11,
	 9,10,10,12,12, 9,10,10,12,12,11,12,12,13,14,11,
	11,12,13,14, 8, 9, 9,11,12, 9,10,10,12,12, 9,10,
	10,12,12,11,12,11,14,13,11,12,12,13,13, 5, 7, 7,
	 9, 9, 7, 8, 8,10,10, 7, 8, 8,10,10, 9,10,10,12,
	12, 9,10,10,12,12, 7, 8, 8,10,10, 8, 8, 9,10,11,
	 8, 9, 9,11,11,10,10,11,11,13,10,11,11,12,13, 6,
	 7, 8,10,10, 7, 9, 8,11,10, 8, 9, 9,11,11,10,11,
	10,13,11,10,11,11,12,12, 9,10,10,12,12,10,10,11,
	12,13,10,11,11,13,13,12,11,13,12,15,12,13,13,14,
	15, 9,10,10,12,12, 9,11,10,13,12,10,11,11,13,13,
	11,13,11,14,12,12,13,13,14,15, 5, 7, 7, 9, 9, 7,
	 8, 8,10,10, 7, 8, 8,10,10, 9,10,10,12,12, 9,10,
	10,12,12, 6, 8, 7,10,10, 8, 9, 9,11,11, 7, 8, 9,
	10,11,10,11,11,12,12,10,10,11,11,13, 7, 8, 8,10,
	10, 8, 9, 9,11,11, 8, 9, 8,11,10,10,11,11,13,12,
	10,11,10,13,11, 9,10,10,12,12,10,11,11,13,12, 9,
	10,10,12,13,12,13,13,14,15,11,11,13,12,14, 9,10,
	10,12,12,10,11,11,13,13,10,11,10,13,12,12,13,13,
	14,14,12,13,11,14,12, 8, 9, 9,12,12, 9,10,10,12,
	12, 9,10,10,12,12,12,12,12,14,14,11,12,12,14,13,
	 9,10,10,12,12,10,11,11,13,13,10,11,11,13,12,12,
	12,13,14,15,12,13,13,15,14, 9,10,10,12,12,10,11,
	10,13,12,10,11,11,12,13,12,13,12,15,13,12,13,13,
	14,15,11,12,12,14,13,11,12,12,14,15,12,13,13,15,
	14,13,12,14,12,16,13,14,14,15,15,11,11,12,14,14,
	11,12,11,14,13,12,13,13,14,15,13,14,12,16,12,14,
	14,15,16,16, 8, 9, 9,11,12, 9,10,10,12,12, 9,10,
	10,12,13,11,12,12,13,13,12,12,13,14,14, 9,10,10,
	12,12,10,11,10,13,12,10,10,11,12,13,12,13,13,15,
	14,12,12,13,13,15, 9,10,10,12,13,10,11,11,12,13,
	10,11,11,13,13,12,13,13,14,15,12,13,12,15,14,11,
	12,11,14,13,12,13,13,15,14,11,11,12,13,14,14,15,
	14,16,15,13,12,14,13,16,11,12,12,13,14,12,13,13,
	14,15,11,12,11,14,14,14,14,14,15,16,13,15,12,16,
	12,
};

static float _vq_quantthresh__44u5__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u5__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u5__p4_0 = {
	_vq_quantthresh__44u5__p4_0,
	_vq_quantmap__44u5__p4_0,
	5,
	5
};

static static_codebook _44u5__p4_0 = {
	4, 625,
	_vq_lengthlist__44u5__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u5__p4_0,
	NULL,
	&_vq_auxt__44u5__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u5__p5_0[] = {
	 2, 3, 3, 6, 6, 8, 8,10,10, 4, 5, 5, 8, 7, 8, 8,
	11,10, 3, 5, 5, 7, 8, 8, 8,10,11, 6, 8, 7,10, 9,
	10,10,11,11, 6, 7, 8, 9, 9, 9,10,11,12, 8, 8, 8,
	10,10,11,11,13,12, 8, 8, 9, 9,10,11,11,12,13,10,
	11,10,12,11,13,12,14,14,10,10,11,11,12,12,13,14,
	14,
};

static float _vq_quantthresh__44u5__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u5__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u5__p5_0 = {
	_vq_quantthresh__44u5__p5_0,
	_vq_quantmap__44u5__p5_0,
	9,
	9
};

static static_codebook _44u5__p5_0 = {
	2, 81,
	_vq_lengthlist__44u5__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u5__p5_0,
	NULL,
	&_vq_auxt__44u5__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p6_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u5__p6_0[] = {
	 3, 4, 4, 5, 5, 7, 7, 9, 9, 4, 5, 4, 6, 6, 7, 7,
	 9, 9, 4, 4, 5, 6, 6, 7, 7, 9, 9, 5, 6, 6, 7, 7,
	 8, 8,10,10, 6, 6, 6, 7, 7, 8, 8,10,10, 7, 7, 7,
	 8, 8, 9, 9,11,10, 7, 7, 7, 8, 8, 9, 9,10,11, 9,
	 9, 9,10,10,11,10,11,11, 9, 9, 9,10,10,11,10,11,
	11,
};

static float _vq_quantthresh__44u5__p6_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u5__p6_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u5__p6_0 = {
	_vq_quantthresh__44u5__p6_0,
	_vq_quantmap__44u5__p6_0,
	9,
	9
};

static static_codebook _44u5__p6_0 = {
	2, 81,
	_vq_lengthlist__44u5__p6_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u5__p6_0,
	NULL,
	&_vq_auxt__44u5__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u5__p7_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 9, 9, 8,11,10, 7,
	11,10, 5, 9, 9, 7,10,10, 8,10,11, 4, 9, 9, 9,12,
	12, 9,12,12, 8,12,12,11,12,12,10,12,13, 7,12,12,
	11,12,12,10,12,13, 4, 9, 9, 9,12,12, 9,12,12, 7,
	12,11,10,13,13,11,12,12, 7,12,12,10,13,13,11,12,
	12,
};

static float _vq_quantthresh__44u5__p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44u5__p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u5__p7_0 = {
	_vq_quantthresh__44u5__p7_0,
	_vq_quantmap__44u5__p7_0,
	3,
	3
};

static static_codebook _44u5__p7_0 = {
	4, 81,
	_vq_lengthlist__44u5__p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44u5__p7_0,
	NULL,
	&_vq_auxt__44u5__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u5__p7_1[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8, 4, 5, 5, 7, 7,
	 8, 8, 9, 8, 8, 9, 4, 5, 5, 7, 7, 8, 8, 9, 9, 8,
	 9, 6, 7, 7, 8, 8, 9, 8, 9, 9, 9, 9, 6, 7, 7, 8,
	 8, 9, 9, 9, 9, 9, 9, 7, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 8, 9, 9,
	 9, 9, 9, 9,10,10,10,10, 8, 9, 9, 9, 9, 9, 9,10,
	10,10,10, 8, 9, 9, 9, 9, 9, 9,10,10,10,10, 8, 9,
	 9, 9, 9, 9, 9,10,10,10,10,
};

static float _vq_quantthresh__44u5__p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u5__p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u5__p7_1 = {
	_vq_quantthresh__44u5__p7_1,
	_vq_quantmap__44u5__p7_1,
	11,
	11
};

static static_codebook _44u5__p7_1 = {
	2, 121,
	_vq_lengthlist__44u5__p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u5__p7_1,
	NULL,
	&_vq_auxt__44u5__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u5__p8_0[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u5__p8_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 9, 9,10,10, 4, 6, 6, 7, 7,
	 9, 9,10,10,11,11, 4, 6, 6, 7, 7, 9, 9,10,10,11,
	11, 6, 8, 7, 9, 9,10,10,11,11,13,12, 6, 8, 8, 9,
	 9,10,10,11,11,12,13, 8, 9, 9,10,10,12,12,13,12,
	14,13, 8, 9, 9,10,10,12,12,13,13,14,14, 9,11,11,
	12,12,13,13,14,14,15,14, 9,11,11,12,12,13,13,14,
	14,15,14,11,12,12,13,13,14,14,15,14,15,14,11,11,
	12,13,13,14,14,14,14,15,15,
};

static float _vq_quantthresh__44u5__p8_0[] = {
	-49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5, 27.5,
	38.5, 49.5,
};

static long _vq_quantmap__44u5__p8_0[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u5__p8_0 = {
	_vq_quantthresh__44u5__p8_0,
	_vq_quantmap__44u5__p8_0,
	11,
	11
};

static static_codebook _44u5__p8_0 = {
	2, 121,
	_vq_lengthlist__44u5__p8_0,
	1, -524582912, 1618345984, 4, 0,
	_vq_quantlist__44u5__p8_0,
	NULL,
	&_vq_auxt__44u5__p8_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p8_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u5__p8_1[] = {
	 3, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 5, 6, 5, 7, 6,
	 7, 7, 8, 8, 8, 8, 5, 5, 5, 6, 6, 7, 7, 8, 8, 8,
	 8, 6, 7, 6, 7, 7, 8, 8, 8, 8, 8, 8, 6, 6, 7, 7,
	 7, 8, 8, 8, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8,
	 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44u5__p8_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u5__p8_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u5__p8_1 = {
	_vq_quantthresh__44u5__p8_1,
	_vq_quantmap__44u5__p8_1,
	11,
	11
};

static static_codebook _44u5__p8_1 = {
	2, 121,
	_vq_lengthlist__44u5__p8_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u5__p8_1,
	NULL,
	&_vq_auxt__44u5__p8_1,
	NULL,
	0
};

static long _vq_quantlist__44u5__p9_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u5__p9_0[] = {
	 1, 3, 2,12,10,13,13,13,13,13,13,13,13, 4, 9, 9,
	13,13,13,13,13,13,13,13,13,13, 5,10, 9,13,13,13,
	13,13,13,13,13,13,13,12,13,13,13,13,13,13,13,13,
	13,13,13,13,11,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
	13,13,13,13,13,13,13,13,13,13,13,13,13,12,12,12,
	12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
	12,12,12,12,12,12,12,12,12,
};

static float _vq_quantthresh__44u5__p9_0[] = {
	-1402.5, -1147.5, -892.5, -637.5, -382.5, -127.5, 127.5, 382.5,
	637.5, 892.5, 1147.5, 1402.5,
};

static long _vq_quantmap__44u5__p9_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u5__p9_0 = {
	_vq_quantthresh__44u5__p9_0,
	_vq_quantmap__44u5__p9_0,
	13,
	13
};

static static_codebook _44u5__p9_0 = {
	2, 169,
	_vq_lengthlist__44u5__p9_0,
	1, -514332672, 1627381760, 4, 0,
	_vq_quantlist__44u5__p9_0,
	NULL,
	&_vq_auxt__44u5__p9_0,
	NULL,
	0
};

static long _vq_quantlist__44u5__p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44u5__p9_1[] = {
	 1, 4, 4, 7, 7, 8, 8, 8, 7, 8, 7, 9, 8, 9, 9, 4,
	 7, 6, 9, 8,10,10, 9, 8, 9, 9, 9, 9, 9, 8, 5, 6,
	 6, 8, 9,10,10, 9, 9, 9,10,10,10,10,11, 7, 8, 8,
	10,10,11,11,10,10,11,11,11,12,11,11, 7, 8, 8,10,
	10,11,11,10,10,11,11,12,11,11,11, 8, 9, 9,11,11,
	12,12,11,11,12,11,12,12,12,12, 8, 9,10,11,11,12,
	12,11,11,12,12,12,12,12,12, 8, 9, 9,10,10,12,11,
	12,12,12,12,12,12,12,13, 8, 9, 9,11,11,11,11,12,
	12,12,12,13,12,13,13, 9,10,10,11,11,12,12,12,13,
	12,13,13,13,14,13, 9,10,10,11,11,12,12,12,13,13,
	12,13,13,14,13, 9,11,10,12,11,13,12,12,13,13,13,
	13,13,13,14, 9,10,10,12,12,12,12,12,13,13,13,13,
	13,14,14,10,11,11,12,12,12,13,13,13,14,14,13,14,
	14,14,10,11,11,12,12,12,12,13,12,13,14,13,14,14,
	14,
};

static float _vq_quantthresh__44u5__p9_1[] = {
	-110.5, -93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5,
	25.5, 42.5, 59.5, 76.5, 93.5, 110.5,
};

static long _vq_quantmap__44u5__p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44u5__p9_1 = {
	_vq_quantthresh__44u5__p9_1,
	_vq_quantmap__44u5__p9_1,
	15,
	15
};

static static_codebook _44u5__p9_1 = {
	2, 225,
	_vq_lengthlist__44u5__p9_1,
	1, -522338304, 1620115456, 4, 0,
	_vq_quantlist__44u5__p9_1,
	NULL,
	&_vq_auxt__44u5__p9_1,
	NULL,
	0
};

static long _vq_quantlist__44u5__p9_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44u5__p9_2[] = {
	 2, 5, 5, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 5, 6, 6, 7, 7, 8, 8, 9, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 5, 6, 6, 7, 7, 8, 8, 9, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 7, 7, 7, 8, 8, 9, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 7, 7, 7, 8, 8, 9, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9,
	 9,10, 9,10,10,10, 8, 8, 8, 9, 8, 9, 9, 9, 9, 9,
	 9, 9,10, 9,10, 9,10, 8, 9, 9, 9, 9, 9, 9, 9, 9,
	 9,10, 9,10,10,10,10,10, 8, 9, 9, 9, 9, 9, 9,10,
	 9,10, 9,10,10,10,10,10,10, 9, 9, 9, 9, 9,10, 9,
	10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9, 9,
	 9,10, 9,10, 9,10,10,10,10,10,10, 9, 9, 9, 9, 9,
	10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9,
	 9, 9,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9,
	 9,10,10, 9,10,10,10,10,10,10,10,10,10,10, 9, 9,
	 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10, 9,
	 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,
	 9, 9, 9,10, 9,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__44u5__p9_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44u5__p9_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44u5__p9_2 = {
	_vq_quantthresh__44u5__p9_2,
	_vq_quantmap__44u5__p9_2,
	17,
	17
};

static static_codebook _44u5__p9_2 = {
	2, 289,
	_vq_lengthlist__44u5__p9_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44u5__p9_2,
	NULL,
	&_vq_auxt__44u5__p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44u5__short[] = {
	 4,10,17,13,17,13,17,17,17,17, 3, 6, 8, 9,11, 9,
	15,12,16,17, 6, 5, 5, 7, 7, 8,10,11,17,17, 7, 8,
	 7, 9, 9,10,13,13,17,17, 8, 6, 5, 7, 4, 7, 5, 8,
	14,17, 9, 9, 8, 9, 7, 9, 8,10,16,17,12,10, 7, 8,
	 4, 7, 4, 7,16,17,12,11, 9,10, 6, 9, 5, 7,14,17,
	14,13,10,15, 4, 8, 3, 5,14,17,17,14,11,15, 6,10,
	 6, 8,15,17,
};

static static_codebook _huff_book__44u5__short = {
	2, 100,
	_huff_lengthlist__44u5__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u6__long[] = {
	 3, 9,14,13,14,13,16,12,13,14, 5, 4, 6, 6, 8, 9,
	11,10,12,15,10, 5, 5, 6, 6, 8,10,10,13,16,10, 6,
	 6, 6, 6, 8, 9, 9,12,14,13, 7, 6, 6, 4, 6, 6, 7,
	11,14,10, 7, 7, 7, 6, 6, 6, 7,10,13,15,10, 9, 8,
	 5, 6, 5, 6,10,14,10, 9, 8, 8, 6, 6, 5, 4, 6,11,
	11,11,12,11,10, 9, 9, 5, 5, 9,10,12,15,13,13,13,
	13, 8, 7, 7,
};

static static_codebook _huff_book__44u6__long = {
	2, 100,
	_huff_lengthlist__44u6__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u6__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u6__p1_0[] = {
	 1, 4, 4, 4, 8, 7, 5, 7, 7, 5, 8, 8, 8,10,10, 7,
	 9,10, 5, 8, 8, 7,10, 9, 8,10,10, 5, 8, 8, 8,10,
	10, 8,10,10, 8,10,10,10,12,13,10,13,13, 7,10,10,
	10,13,11,10,13,13, 5, 8, 8, 8,11,10, 8,10,10, 7,
	10,10,10,13,13,10,11,13, 8,10,11,10,13,13,10,13,
	12,
};

static float _vq_quantthresh__44u6__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u6__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u6__p1_0 = {
	_vq_quantthresh__44u6__p1_0,
	_vq_quantmap__44u6__p1_0,
	3,
	3
};

static static_codebook _44u6__p1_0 = {
	4, 81,
	_vq_lengthlist__44u6__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u6__p1_0,
	NULL,
	&_vq_auxt__44u6__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u6__p2_0[] = {
	 3, 4, 4, 5, 6, 6, 5, 6, 6, 5, 6, 6, 6, 8, 8, 6,
	 7, 8, 5, 6, 6, 6, 8, 7, 6, 8, 8, 5, 6, 6, 6, 8,
	 8, 6, 8, 8, 6, 8, 8, 8, 9, 9, 8, 9, 9, 6, 7, 7,
	 7, 9, 8, 8, 9, 9, 5, 6, 6, 6, 8, 7, 6, 8, 8, 6,
	 8, 8, 8, 9, 9, 7, 8, 9, 6, 8, 8, 8, 9, 9, 8, 9,
	 9,
};

static float _vq_quantthresh__44u6__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u6__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u6__p2_0 = {
	_vq_quantthresh__44u6__p2_0,
	_vq_quantmap__44u6__p2_0,
	3,
	3
};

static static_codebook _44u6__p2_0 = {
	4, 81,
	_vq_lengthlist__44u6__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u6__p2_0,
	NULL,
	&_vq_auxt__44u6__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u6__p3_0[] = {
	 2, 5, 4, 8, 8, 5, 7, 6, 9, 9, 5, 6, 7, 9, 9, 8,
	 9, 9,13,12, 8, 9,10,12,13, 5, 7, 7,10, 9, 7, 9,
	 9,11,11, 7, 8, 9,11,11,10,11,11,14,14, 9,10,11,
	13,14, 5, 7, 7, 9,10, 6, 9, 8,11,11, 7, 9, 9,11,
	11, 9,11,10,14,13,10,11,11,14,13, 8,10,10,13,13,
	10,11,11,15,15, 9,11,11,14,14,13,14,14,17,16,12,
	13,14,16,16, 8,10,10,13,14, 9,11,11,14,15,10,11,
	12,14,15,12,14,13,16,15,13,14,14,15,17, 5, 7, 7,
	10,10, 7, 9, 9,11,11, 7, 9, 9,11,11,10,12,11,14,
	14,10,11,11,14,14, 7, 9, 9,12,11, 9,11,11,13,13,
	 9,11,11,13,13,11,13,13,14,15,11,12,13,15,16, 6,
	 9, 9,11,12, 8,11,10,13,12, 9,11,11,13,14,11,13,
	12,16,14,11,13,13,15,16,10,12,11,14,15,11,13,13,
	15,17,11,13,13,17,16,15,15,16,17,16,14,15,16,18,
	 0, 9,11,11,14,15,10,12,12,16,15,11,13,13,16,16,
	13,15,14,18,15,14,16,16, 0, 0, 5, 7, 7,10,10, 7,
	 9, 9,11,11, 7, 9, 9,11,11,10,11,11,14,14,10,11,
	12,14,14, 6, 9, 9,11,11, 9,11,11,13,13, 8,10,11,
	12,13,11,13,13,16,15,11,12,13,14,16, 7, 9, 9,11,
	12, 9,11,11,13,13, 9,11,11,13,13,11,13,13,16,15,
	11,13,12,15,15, 9,11,11,15,14,11,13,13,17,16,10,
	12,13,15,16,14,16,16, 0,18,14,14,15,15,17,10,11,
	12,15,15,11,13,13,16,16,11,13,13,16,16,14,16,16,
	19,17,14,15,15,17,17, 8,10,10,14,14,10,12,11,15,
	15,10,11,12,16,15,14,15,15,18,20,13,14,16,17,18,
	 9,11,11,15,16,11,13,13,17,17,11,13,13,17,16,15,
	16,16, 0, 0,15,16,16, 0, 0, 9,11,11,15,15,10,13,
	12,17,15,11,13,13,17,16,15,17,15,20,19,15,16,16,
	19, 0,13,15,14, 0,17,14,15,16, 0,20,15,16,16, 0,
	19,17,18, 0, 0, 0,16,17,18, 0, 0,12,14,14,19,18,
	13,15,14, 0,17,14,15,16,19,19,16,18,16, 0,19,19,
	20,17,20, 0, 8,10,10,13,14,10,11,11,15,15,10,12,
	12,15,16,14,15,14,19,16,14,15,15, 0,18, 9,11,11,
	16,15,11,13,13, 0,16,11,12,13,16,17,14,16,17, 0,
	19,15,16,16,18, 0, 9,11,11,15,16,11,13,13,16,16,
	11,14,13,18,17,15,16,16,18,20,15,17,19, 0, 0,12,
	14,14,17,17,14,16,15, 0, 0,13,14,15,19, 0,16,18,
	20, 0, 0,16,16,18,18, 0,12,14,14,17,20,14,16,16,
	19, 0,14,16,14, 0,20,16,20,17, 0, 0,17, 0,15, 0,
	19,
};

static float _vq_quantthresh__44u6__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u6__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u6__p3_0 = {
	_vq_quantthresh__44u6__p3_0,
	_vq_quantmap__44u6__p3_0,
	5,
	5
};

static static_codebook _44u6__p3_0 = {
	4, 625,
	_vq_lengthlist__44u6__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u6__p3_0,
	NULL,
	&_vq_auxt__44u6__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u6__p4_0[] = {
	 4, 5, 5, 8, 8, 6, 7, 6, 9, 9, 6, 6, 7, 9, 9, 8,
	 9, 9,11,11, 8, 9, 9,11,11, 6, 7, 7, 9, 9, 7, 8,
	 8,10,10, 7, 7, 8, 9,10, 9,10,10,11,11, 9, 9,10,
	11,12, 6, 7, 7, 9, 9, 7, 8, 7,10, 9, 7, 8, 8,10,
	10, 9,10, 9,12,11, 9,10,10,12,11, 8, 9, 9,11,11,
	 9,10,10,12,12, 9,10,10,12,12,11,12,12,14,13,11,
	11,12,13,13, 8, 9, 9,11,11, 9,10,10,12,12, 9,10,
	10,12,12,11,12,11,13,12,11,12,12,13,13, 5, 7, 7,
	 9, 9, 7, 8, 7,10,10, 7, 7, 8,10,10, 9,10,10,12,
	11, 9,10,10,11,12, 7, 8, 8,10,10, 8, 8, 9,11,11,
	 8, 9, 9,11,11,10,10,11,12,13,10,10,11,12,12, 6,
	 7, 7,10,10, 7, 9, 8,11,10, 8, 8, 9,10,11,10,11,
	10,13,11,10,11,11,12,12, 9,10,10,12,12,10,10,11,
	13,13,10,11,11,12,13,12,12,12,13,14,12,12,13,14,
	14, 9,10,10,12,12, 9,10,10,13,12,10,11,11,13,13,
	11,12,11,14,12,12,13,13,14,14, 6, 7, 7, 9, 9, 7,
	 8, 7,10,10, 7, 8, 8,10,10, 9,10,10,12,11, 9,10,
	10,11,12, 6, 7, 7,10,10, 8, 9, 8,11,10, 7, 8, 9,
	10,11,10,11,11,12,12,10,10,11,11,13, 7, 8, 8,10,
	10, 8, 9, 9,11,11, 8, 9, 8,11,11,10,11,10,13,12,
	10,11,11,13,12, 9,10,10,12,12,10,11,11,13,12, 9,
	10,10,12,13,12,13,12,14,14,11,11,12,12,14, 9,10,
	10,12,12,10,11,11,13,13,10,11,10,13,12,12,12,12,
	14,14,12,13,12,14,13, 8, 9, 9,11,11, 9,10,10,12,
	12, 9,10,10,12,12,11,12,12,14,13,11,12,12,13,14,
	 9,10,10,12,12,10,11,11,13,13,10,11,11,13,13,12,
	12,13,14,15,12,12,13,14,14, 9,10,10,12,12, 9,11,
	10,13,12,10,10,11,12,13,12,13,12,14,13,12,12,13,
	14,15,11,12,12,14,13,11,12,12,14,14,12,13,13,14,
	14,13,13,14,14,16,13,14,14,15,15,11,12,11,13,13,
	11,12,11,14,13,12,12,13,14,15,12,14,12,15,12,13,
	14,15,15,16, 8, 9, 9,11,11, 9,10,10,12,12, 9,10,
	10,12,12,11,12,12,14,13,11,12,12,13,13, 9,10,10,
	12,12,10,11,10,13,12, 9,10,11,12,13,12,13,12,14,
	14,12,12,13,13,14, 9,10,10,12,12,10,11,11,13,13,
	10,11,11,13,13,12,13,12,14,14,12,13,13,14,14,11,
	11,11,13,13,12,13,12,14,14,11,11,12,13,14,14,14,
	14,16,15,12,12,14,12,15,11,12,12,13,14,12,13,13,
	14,15,11,12,12,14,14,13,14,14,16,16,13,14,13,16,
	13,
};

static float _vq_quantthresh__44u6__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u6__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u6__p4_0 = {
	_vq_quantthresh__44u6__p4_0,
	_vq_quantmap__44u6__p4_0,
	5,
	5
};

static static_codebook _44u6__p4_0 = {
	4, 625,
	_vq_lengthlist__44u6__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u6__p4_0,
	NULL,
	&_vq_auxt__44u6__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u6__p5_0[] = {
	 2, 3, 3, 6, 6, 8, 8,10,10, 4, 5, 5, 8, 7, 8, 8,
	11,11, 3, 5, 5, 7, 8, 8, 8,11,11, 6, 8, 7, 9, 9,
	10, 9,12,11, 6, 7, 8, 9, 9, 9,10,11,12, 8, 8, 8,
	10, 9,12,11,13,13, 8, 8, 9, 9,10,11,12,13,13,10,
	11,11,12,12,13,13,14,14,10,10,11,11,12,13,13,14,
	14,
};

static float _vq_quantthresh__44u6__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u6__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u6__p5_0 = {
	_vq_quantthresh__44u6__p5_0,
	_vq_quantmap__44u6__p5_0,
	9,
	9
};

static static_codebook _44u6__p5_0 = {
	2, 81,
	_vq_lengthlist__44u6__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u6__p5_0,
	NULL,
	&_vq_auxt__44u6__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p6_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u6__p6_0[] = {
	 3, 4, 4, 5, 5, 7, 7, 9, 9, 4, 5, 4, 6, 6, 7, 7,
	 9, 9, 4, 4, 5, 6, 6, 7, 8, 9, 9, 5, 6, 6, 7, 7,
	 8, 8,10,10, 5, 6, 6, 7, 7, 8, 8,10,10, 7, 8, 7,
	 8, 8,10, 9,11,11, 7, 7, 8, 8, 8, 9,10,10,11, 9,
	 9, 9,10,10,11,11,12,11, 9, 9, 9,10,10,11,11,11,
	12,
};

static float _vq_quantthresh__44u6__p6_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u6__p6_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u6__p6_0 = {
	_vq_quantthresh__44u6__p6_0,
	_vq_quantmap__44u6__p6_0,
	9,
	9
};

static static_codebook _44u6__p6_0 = {
	2, 81,
	_vq_lengthlist__44u6__p6_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u6__p6_0,
	NULL,
	&_vq_auxt__44u6__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u6__p7_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 9, 8, 7,10,10, 8,
	10,10, 5, 8, 9, 7,10,10, 7,10, 9, 4, 8, 8, 9,11,
	11, 8,11,11, 7,11,11,10,10,13,10,13,13, 7,11,11,
	10,13,12,10,13,13, 5, 9, 8, 8,11,11, 9,11,11, 7,
	11,11,10,13,13,10,12,13, 7,11,11,10,13,13, 9,13,
	10,
};

static float _vq_quantthresh__44u6__p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44u6__p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u6__p7_0 = {
	_vq_quantthresh__44u6__p7_0,
	_vq_quantmap__44u6__p7_0,
	3,
	3
};

static static_codebook _44u6__p7_0 = {
	4, 81,
	_vq_lengthlist__44u6__p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44u6__p7_0,
	NULL,
	&_vq_auxt__44u6__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u6__p7_1[] = {
	 3, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8, 4, 5, 5, 7, 6,
	 8, 8, 8, 8, 8, 8, 4, 5, 5, 6, 7, 8, 8, 8, 8, 8,
	 8, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 6, 7, 7, 7,
	 7, 8, 8, 8, 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 9, 9,
	 9, 9, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 8, 8, 8,
	 8, 8, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 8, 8,
	 8, 8, 8, 9, 9, 9, 9, 9, 9,
};

static float _vq_quantthresh__44u6__p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u6__p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u6__p7_1 = {
	_vq_quantthresh__44u6__p7_1,
	_vq_quantmap__44u6__p7_1,
	11,
	11
};

static static_codebook _44u6__p7_1 = {
	2, 121,
	_vq_lengthlist__44u6__p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u6__p7_1,
	NULL,
	&_vq_auxt__44u6__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u6__p8_0[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u6__p8_0[] = {
	 1, 4, 4, 6, 6, 8, 8, 9, 9,10,10, 4, 6, 6, 7, 7,
	 9, 9,10,10,11,11, 4, 6, 6, 7, 7, 9, 9,10,10,11,
	11, 6, 8, 8, 9, 9,10,10,11,11,12,12, 6, 8, 8, 9,
	 9,10,10,11,11,12,12, 8, 9, 9,10,10,11,11,12,12,
	13,13, 8, 9, 9,10,10,11,11,12,12,13,13,10,10,10,
	11,11,13,13,13,13,15,14, 9,10,10,12,11,12,13,13,
	13,14,15,11,12,12,13,13,13,13,15,14,15,15,11,11,
	12,13,13,14,14,14,15,15,15,
};

static float _vq_quantthresh__44u6__p8_0[] = {
	-49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5, 27.5,
	38.5, 49.5,
};

static long _vq_quantmap__44u6__p8_0[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u6__p8_0 = {
	_vq_quantthresh__44u6__p8_0,
	_vq_quantmap__44u6__p8_0,
	11,
	11
};

static static_codebook _44u6__p8_0 = {
	2, 121,
	_vq_lengthlist__44u6__p8_0,
	1, -524582912, 1618345984, 4, 0,
	_vq_quantlist__44u6__p8_0,
	NULL,
	&_vq_auxt__44u6__p8_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p8_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u6__p8_1[] = {
	 3, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 5, 6, 5, 7, 7,
	 7, 7, 8, 7, 8, 8, 5, 5, 6, 6, 7, 7, 7, 7, 7, 8,
	 8, 6, 7, 7, 7, 7, 8, 7, 8, 8, 8, 8, 6, 6, 7, 7,
	 7, 7, 8, 8, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8,
	 8, 8, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7,
	 8, 8, 8, 8, 8, 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44u6__p8_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u6__p8_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u6__p8_1 = {
	_vq_quantthresh__44u6__p8_1,
	_vq_quantmap__44u6__p8_1,
	11,
	11
};

static static_codebook _44u6__p8_1 = {
	2, 121,
	_vq_lengthlist__44u6__p8_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u6__p8_1,
	NULL,
	&_vq_auxt__44u6__p8_1,
	NULL,
	0
};

static long _vq_quantlist__44u6__p9_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44u6__p9_0[] = {
	 1, 3, 2, 9, 8,15,15,15,15,15,15,15,15,15,15, 4,
	 8, 9,13,14,14,14,14,14,14,14,14,14,14,14, 5, 8,
	 9,14,14,14,14,14,14,14,14,14,14,14,14,11,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,11,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
	14,
};

static float _vq_quantthresh__44u6__p9_0[] = {
	-1657.5, -1402.5, -1147.5, -892.5, -637.5, -382.5, -127.5, 127.5,
	382.5, 637.5, 892.5, 1147.5, 1402.5, 1657.5,
};

static long _vq_quantmap__44u6__p9_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44u6__p9_0 = {
	_vq_quantthresh__44u6__p9_0,
	_vq_quantmap__44u6__p9_0,
	15,
	15
};

static static_codebook _44u6__p9_0 = {
	2, 225,
	_vq_lengthlist__44u6__p9_0,
	1, -514071552, 1627381760, 4, 0,
	_vq_quantlist__44u6__p9_0,
	NULL,
	&_vq_auxt__44u6__p9_0,
	NULL,
	0
};

static long _vq_quantlist__44u6__p9_1[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44u6__p9_1[] = {
	 1, 4, 4, 7, 7, 8, 9, 8, 8, 9, 8, 9, 8, 9, 9, 4,
	 7, 6, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 4, 7,
	 6, 9, 9,10,10, 9, 9,10,10,10,10,11,11, 7, 9, 8,
	10,10,11,11,10,10,11,11,11,11,11,11, 7, 8, 9,10,
	10,11,11,10,10,11,11,11,11,11,12, 8,10,10,11,11,
	12,12,11,11,12,12,12,12,13,12, 8,10,10,11,11,12,
	11,11,11,11,12,12,12,12,13, 8, 9, 9,11,10,11,11,
	12,12,12,12,13,12,13,12, 8, 9, 9,11,11,11,11,12,
	12,12,12,12,13,13,13, 9,10,10,11,12,12,12,12,12,
	13,13,13,13,13,13, 9,10,10,11,11,12,12,12,12,13,
	13,13,13,14,13,10,10,10,12,11,12,12,13,13,13,13,
	13,13,13,13,10,10,11,11,11,12,12,13,13,13,13,13,
	13,13,13,10,11,11,12,12,13,12,12,13,13,13,13,13,
	13,14,10,11,11,12,12,13,12,13,13,13,14,13,13,14,
	13,
};

static float _vq_quantthresh__44u6__p9_1[] = {
	-110.5, -93.5, -76.5, -59.5, -42.5, -25.5, -8.5, 8.5,
	25.5, 42.5, 59.5, 76.5, 93.5, 110.5,
};

static long _vq_quantmap__44u6__p9_1[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44u6__p9_1 = {
	_vq_quantthresh__44u6__p9_1,
	_vq_quantmap__44u6__p9_1,
	15,
	15
};

static static_codebook _44u6__p9_1 = {
	2, 225,
	_vq_lengthlist__44u6__p9_1,
	1, -522338304, 1620115456, 4, 0,
	_vq_quantlist__44u6__p9_1,
	NULL,
	&_vq_auxt__44u6__p9_1,
	NULL,
	0
};

static long _vq_quantlist__44u6__p9_2[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44u6__p9_2[] = {
	 3, 5, 5, 7, 7, 8, 8, 8, 8, 8, 8, 9, 8, 8, 9, 9,
	 9, 5, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9,
	 9, 9, 5, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9,
	 9, 9, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10, 9, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10, 9, 9, 9,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9,10, 9, 9, 9,10, 9, 9,10, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10,10,10, 9,10, 9,10,10,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10, 9,10,10, 9, 9,
	10,
};

static float _vq_quantthresh__44u6__p9_2[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44u6__p9_2[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44u6__p9_2 = {
	_vq_quantthresh__44u6__p9_2,
	_vq_quantmap__44u6__p9_2,
	17,
	17
};

static static_codebook _44u6__p9_2 = {
	2, 289,
	_vq_lengthlist__44u6__p9_2,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44u6__p9_2,
	NULL,
	&_vq_auxt__44u6__p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44u6__short[] = {
	 4,11,16,13,17,13,17,16,17,17, 4, 7, 9, 9,13,10,
	16,12,16,17, 7, 6, 5, 7, 8, 9,12,12,16,17, 6, 9,
	 7, 9,10,10,15,15,17,17, 6, 7, 5, 7, 5, 7, 7,10,
	16,17, 7, 9, 8, 9, 8,10,11,11,15,17, 7, 7, 7, 8,
	 5, 8, 8, 9,15,17, 8, 7, 9, 9, 7, 8, 7, 2, 7,15,
	14,13,13,15, 5,10, 4, 3, 6,17,17,15,13,17, 7,11,
	 7, 6, 9,16,
};

static static_codebook _huff_book__44u6__short = {
	2, 100,
	_huff_lengthlist__44u6__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u7__long[] = {
	 3, 9,14,13,15,14,16,13,13,14, 5, 5, 7, 7, 8, 9,
	11,10,12,15,10, 6, 5, 6, 6, 9,10,10,13,16,10, 6,
	 6, 6, 6, 8, 9, 9,12,15,14, 7, 6, 6, 5, 6, 6, 8,
	12,15,10, 8, 7, 7, 6, 7, 7, 7,11,13,14,10, 9, 8,
	 5, 6, 4, 5, 9,12,10, 9, 9, 8, 6, 6, 5, 3, 6,11,
	12,11,12,12,10, 9, 8, 5, 5, 8,10,11,15,13,13,13,
	12, 8, 6, 7,
};

static static_codebook _huff_book__44u7__long = {
	2, 100,
	_huff_lengthlist__44u7__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u7__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u7__p1_0[] = {
	 1, 4, 4, 4, 7, 7, 5, 7, 7, 5, 8, 8, 8,10,10, 7,
	10,10, 5, 8, 8, 7,10,10, 8,10,10, 5, 8, 8, 8,11,
	10, 8,10,10, 8,10,10,10,12,13,10,13,13, 7,10,10,
	10,13,12,10,13,13, 5, 8, 8, 8,11,10, 8,10,11, 7,
	10,10,10,13,13,10,12,13, 8,11,11,10,13,13,10,13,
	12,
};

static float _vq_quantthresh__44u7__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u7__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u7__p1_0 = {
	_vq_quantthresh__44u7__p1_0,
	_vq_quantmap__44u7__p1_0,
	3,
	3
};

static static_codebook _44u7__p1_0 = {
	4, 81,
	_vq_lengthlist__44u7__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u7__p1_0,
	NULL,
	&_vq_auxt__44u7__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u7__p2_0[] = {
	 3, 4, 4, 5, 6, 6, 5, 6, 6, 5, 6, 6, 6, 8, 8, 6,
	 7, 8, 5, 6, 6, 6, 8, 7, 6, 8, 8, 5, 6, 6, 6, 8,
	 7, 6, 8, 8, 6, 8, 8, 8, 9, 9, 8, 9, 9, 6, 8, 7,
	 7, 9, 8, 8, 9, 9, 5, 6, 6, 6, 8, 7, 6, 8, 8, 6,
	 8, 8, 8, 9, 9, 7, 8, 9, 6, 8, 8, 8, 9, 9, 8, 9,
	 9,
};

static float _vq_quantthresh__44u7__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u7__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u7__p2_0 = {
	_vq_quantthresh__44u7__p2_0,
	_vq_quantmap__44u7__p2_0,
	3,
	3
};

static static_codebook _44u7__p2_0 = {
	4, 81,
	_vq_lengthlist__44u7__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u7__p2_0,
	NULL,
	&_vq_auxt__44u7__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u7__p3_0[] = {
	 2, 5, 4, 8, 8, 5, 7, 6, 9, 9, 5, 6, 7, 9, 9, 8,
	 9, 9,13,12, 8, 9,10,12,13, 5, 7, 7,10, 9, 7, 9,
	 9,11,11, 6, 8, 9,11,11,10,11,11,14,14, 9,10,11,
	13,14, 5, 7, 7, 9, 9, 7, 9, 8,11,11, 7, 9, 9,11,
	11, 9,11,10,14,13,10,11,11,14,14, 8,10,10,14,13,
	10,11,12,15,14, 9,11,11,15,14,13,14,14,16,16,12,
	13,14,17,16, 8,10,10,13,13, 9,11,11,14,15,10,11,
	12,14,15,12,14,13,16,16,13,14,15,15,17, 5, 7, 7,
	10,10, 7, 9, 9,11,11, 7, 9, 9,11,11,10,12,11,15,
	14,10,11,12,14,14, 7, 9, 9,12,12, 9,11,11,13,13,
	 9,11,11,13,13,11,13,13,14,17,11,13,13,15,16, 6,
	 9, 9,11,11, 8,11,10,13,12, 9,11,11,13,13,11,13,
	12,16,14,11,13,13,16,16,10,12,12,15,15,11,13,13,
	16,16,11,13,13,16,15,14,16,17,17,19,14,16,16,18,
	 0, 9,11,11,14,15,10,13,12,16,15,11,13,13,16,16,
	14,15,14, 0,16,14,16,16,18, 0, 5, 7, 7,10,10, 7,
	 9, 9,12,11, 7, 9, 9,11,12,10,11,11,15,14,10,11,
	12,14,14, 6, 9, 9,11,11, 9,11,11,13,13, 8,10,11,
	12,13,11,13,13,17,15,11,12,13,14,15, 7, 9, 9,11,
	12, 9,11,11,13,13, 9,11,11,13,13,11,13,12,16,16,
	11,13,13,15,14, 9,11,11,14,15,11,13,13,16,15,10,
	12,13,16,16,15,16,16, 0, 0,14,13,15,16,18,10,11,
	11,15,15,11,13,14,16,18,11,13,13,16,15,15,16,16,
	19, 0,14,15,15,16,16, 8,10,10,13,13,10,12,11,16,
	15,10,11,11,16,15,13,15,16,18, 0,13,14,15,17,17,
	 9,11,11,15,15,11,13,13,16,18,11,13,13,16,17,15,
	16,16, 0, 0,15,18,16, 0,17, 9,11,11,15,15,11,13,
	12,17,15,11,13,14,16,17,15,18,15, 0,17,15,16,16,
	18,19,13,15,14, 0,18,14,16,16,19,18,14,16,15,19,
	19,16,18,19, 0, 0,16,17, 0, 0, 0,12,14,14,17,17,
	13,16,14, 0,18,14,16,15,18, 0,16,18,16,19,17,18,
	19,17, 0, 0, 8,10,10,14,14, 9,12,11,15,15,10,11,
	12,15,17,13,15,15,18,16,14,16,15,18,17, 9,11,11,
	16,15,11,13,13, 0,16,11,12,13,16,15,15,16,16, 0,
	17,15,15,16,18,17, 9,12,11,15,17,11,13,13,16,16,
	11,14,13,16,16,15,15,16,18,19,16,18,16, 0, 0,12,
	14,14, 0,16,14,16,16, 0,18,13,14,15,16, 0,17,16,
	18, 0, 0,16,16,17,19, 0,13,14,14,17, 0,14,17,16,
	 0,19,14,15,15,18,19,17,16,18, 0, 0,15,19,16, 0,
	 0,
};

static float _vq_quantthresh__44u7__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u7__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u7__p3_0 = {
	_vq_quantthresh__44u7__p3_0,
	_vq_quantmap__44u7__p3_0,
	5,
	5
};

static static_codebook _44u7__p3_0 = {
	4, 625,
	_vq_lengthlist__44u7__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u7__p3_0,
	NULL,
	&_vq_auxt__44u7__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u7__p4_0[] = {
	 4, 5, 5, 8, 8, 6, 7, 6, 9, 9, 6, 6, 7, 9, 9, 8,
	 9, 9,11,11, 8, 9, 9,10,11, 6, 7, 7, 9, 9, 7, 8,
	 8,10,10, 6, 7, 8, 9,10, 9,10,10,12,12, 9, 9,10,
	11,12, 6, 7, 7, 9, 9, 6, 8, 7,10, 9, 7, 8, 8,10,
	10, 9,10, 9,12,11, 9,10,10,12,11, 8, 9, 9,11,11,
	 9,10,10,12,12, 9,10,10,12,12,11,12,12,13,14,11,
	11,12,13,13, 8, 9, 9,11,11, 9,10,10,12,11, 9,10,
	10,12,12,11,12,11,13,13,11,12,12,13,13, 6, 7, 7,
	 9, 9, 7, 8, 7,10,10, 7, 7, 8,10,10, 9,10,10,12,
	11, 9,10,10,12,12, 7, 8, 8,10,10, 8, 8, 9,11,11,
	 8, 9, 9,11,11,10,11,11,12,12,10,10,11,12,13, 6,
	 7, 7,10,10, 7, 9, 8,11,10, 8, 8, 9,10,11,10,11,
	10,13,11,10,11,11,12,12, 9,10,10,12,12,10,10,11,
	13,13,10,11,11,13,12,12,12,13,13,14,12,12,13,14,
	14, 9,10,10,12,12, 9,10,10,12,12,10,11,11,13,13,
	11,12,11,14,12,12,13,13,14,14, 6, 7, 7, 9, 9, 7,
	 8, 7,10,10, 7, 7, 8,10,10, 9,10,10,12,11, 9,10,
	10,11,12, 6, 7, 7,10,10, 8, 9, 8,11,10, 7, 8, 9,
	10,11,10,11,11,13,12,10,10,11,11,13, 7, 8, 8,10,
	10, 8, 9, 9,11,11, 8, 9, 9,11,11,10,11,10,13,12,
	10,11,11,12,12, 9,10,10,12,12,10,11,11,13,12, 9,
	10,10,12,13,12,13,12,14,14,11,11,12,12,14, 9,10,
	10,12,12,10,11,11,13,13,10,11,11,13,13,12,13,12,
	14,14,12,13,12,14,13, 8, 9, 9,11,11, 9,10,10,12,
	12, 9,10,10,12,12,11,12,12,14,13,11,12,12,13,13,
	 9,10,10,12,12,10,11,11,13,13,10,11,11,13,12,12,
	13,13,14,14,12,12,13,14,14, 9,10,10,12,12, 9,11,
	10,13,12,10,10,11,12,13,11,13,12,14,13,12,12,13,
	14,14,11,12,12,13,13,11,12,13,14,14,12,13,13,14,
	14,13,13,14,14,16,13,14,14,16,16,11,11,11,13,13,
	11,12,11,14,13,12,12,13,14,15,13,14,12,16,13,14,
	14,14,15,16, 8, 9, 9,11,11, 9,10,10,12,12, 9,10,
	10,12,12,11,12,12,14,13,11,12,12,13,14, 9,10,10,
	12,12,10,11,10,13,12, 9,10,11,12,13,12,13,12,14,
	14,12,12,13,13,14, 9,10,10,12,12,10,11,11,12,13,
	10,11,11,13,13,12,13,12,14,14,12,13,13,14,14,11,
	12,12,13,13,12,13,12,14,14,11,11,12,13,14,13,15,
	14,16,15,13,12,14,13,16,11,12,12,13,13,12,13,13,
	14,14,12,12,12,14,14,13,14,14,15,15,13,14,13,16,
	14,
};

static float _vq_quantthresh__44u7__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u7__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u7__p4_0 = {
	_vq_quantthresh__44u7__p4_0,
	_vq_quantmap__44u7__p4_0,
	5,
	5
};

static static_codebook _44u7__p4_0 = {
	4, 625,
	_vq_lengthlist__44u7__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u7__p4_0,
	NULL,
	&_vq_auxt__44u7__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u7__p5_0[] = {
	 2, 3, 3, 6, 6, 7, 8,10,10, 4, 5, 5, 8, 7, 8, 8,
	11,11, 3, 5, 5, 7, 7, 8, 9,11,11, 6, 8, 7, 9, 9,
	10,10,12,12, 6, 7, 8, 9,10,10,10,12,12, 8, 8, 8,
	10,10,12,11,13,13, 8, 8, 9,10,10,11,11,13,13,10,
	11,11,12,12,13,13,14,14,10,11,11,12,12,13,13,14,
	14,
};

static float _vq_quantthresh__44u7__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u7__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u7__p5_0 = {
	_vq_quantthresh__44u7__p5_0,
	_vq_quantmap__44u7__p5_0,
	9,
	9
};

static static_codebook _44u7__p5_0 = {
	2, 81,
	_vq_lengthlist__44u7__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u7__p5_0,
	NULL,
	&_vq_auxt__44u7__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p6_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u7__p6_0[] = {
	 3, 4, 4, 5, 5, 7, 7, 9, 9, 4, 5, 4, 6, 6, 8, 7,
	 9, 9, 4, 4, 5, 6, 6, 7, 7, 9, 9, 5, 6, 6, 7, 7,
	 8, 8,10,10, 5, 6, 6, 7, 7, 8, 8,10,10, 7, 8, 7,
	 8, 8,10, 9,11,11, 7, 7, 8, 8, 8, 9,10,11,11, 9,
	 9, 9,10,10,11,10,12,11, 9, 9, 9,10,10,11,11,11,
	12,
};

static float _vq_quantthresh__44u7__p6_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u7__p6_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u7__p6_0 = {
	_vq_quantthresh__44u7__p6_0,
	_vq_quantmap__44u7__p6_0,
	9,
	9
};

static static_codebook _44u7__p6_0 = {
	2, 81,
	_vq_lengthlist__44u7__p6_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u7__p6_0,
	NULL,
	&_vq_auxt__44u7__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p7_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u7__p7_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 9, 8, 8, 9, 9, 7,
	10,10, 5, 8, 9, 7, 9,10, 8, 9, 9, 4, 9, 9, 9,11,
	10, 8,10,10, 7,11,10,10,10,12,10,12,12, 7,10,10,
	10,12,11,10,12,12, 5, 9, 9, 8,10,10, 9,11,11, 7,
	11,10,10,12,12,10,11,12, 7,10,11,10,12,12,10,12,
	10,
};

static float _vq_quantthresh__44u7__p7_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44u7__p7_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u7__p7_0 = {
	_vq_quantthresh__44u7__p7_0,
	_vq_quantmap__44u7__p7_0,
	3,
	3
};

static static_codebook _44u7__p7_0 = {
	4, 81,
	_vq_lengthlist__44u7__p7_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44u7__p7_0,
	NULL,
	&_vq_auxt__44u7__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u7__p7_1[] = {
	 3, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8, 4, 5, 5, 6, 6,
	 8, 7, 8, 8, 8, 8, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
	 8, 6, 7, 6, 7, 7, 8, 8, 9, 9, 9, 9, 6, 6, 7, 7,
	 7, 8, 8, 9, 9, 9, 9, 7, 8, 7, 8, 8, 9, 9, 9, 9,
	 9, 9, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9, 8, 8, 8,
	 9, 9, 9, 9,10, 9, 9, 9, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9,10, 8, 8, 8, 9, 9, 9, 9,10, 9,10,10, 8, 8,
	 8, 9, 9, 9, 9, 9,10,10,10,
};

static float _vq_quantthresh__44u7__p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u7__p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u7__p7_1 = {
	_vq_quantthresh__44u7__p7_1,
	_vq_quantmap__44u7__p7_1,
	11,
	11
};

static static_codebook _44u7__p7_1 = {
	2, 121,
	_vq_lengthlist__44u7__p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u7__p7_1,
	NULL,
	&_vq_auxt__44u7__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u7__p8_0[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u7__p8_0[] = {
	 1, 4, 4, 6, 6, 8, 8,10,10,11,11, 4, 6, 6, 7, 7,
	 9, 9,11,10,12,12, 5, 6, 5, 7, 7, 9, 9,10,11,12,
	12, 6, 7, 7, 8, 8,10,10,11,11,13,13, 6, 7, 7, 8,
	 8,10,10,11,12,13,13, 8, 9, 9,10,10,11,11,12,12,
	14,14, 8, 9, 9,10,10,11,11,12,12,14,14,10,10,10,
	11,11,13,12,14,14,15,15,10,10,10,12,12,13,13,14,
	14,15,15,11,12,12,13,13,14,14,15,14,16,15,11,12,
	12,13,13,14,14,15,15,15,16,
};

static float _vq_quantthresh__44u7__p8_0[] = {
	-49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5, 27.5,
	38.5, 49.5,
};

static long _vq_quantmap__44u7__p8_0[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u7__p8_0 = {
	_vq_quantthresh__44u7__p8_0,
	_vq_quantmap__44u7__p8_0,
	11,
	11
};

static static_codebook _44u7__p8_0 = {
	2, 121,
	_vq_lengthlist__44u7__p8_0,
	1, -524582912, 1618345984, 4, 0,
	_vq_quantlist__44u7__p8_0,
	NULL,
	&_vq_auxt__44u7__p8_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p8_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u7__p8_1[] = {
	 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 5, 6, 6, 7, 7,
	 7, 7, 7, 7, 7, 7, 5, 6, 6, 6, 7, 7, 7, 7, 7, 7,
	 7, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 6, 7, 7, 7,
	 7, 7, 7, 7, 7, 8, 8, 7, 7, 7, 7, 7, 8, 7, 8, 8,
	 8, 8, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7,
	 7, 7, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 8, 8, 8,
	 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7,
	 7, 8, 8, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44u7__p8_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u7__p8_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u7__p8_1 = {
	_vq_quantthresh__44u7__p8_1,
	_vq_quantmap__44u7__p8_1,
	11,
	11
};

static static_codebook _44u7__p8_1 = {
	2, 121,
	_vq_lengthlist__44u7__p8_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u7__p8_1,
	NULL,
	&_vq_auxt__44u7__p8_1,
	NULL,
	0
};

static long _vq_quantlist__44u7__p9_0[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u7__p9_0[] = {
	 1, 3, 3,10,10,10,10,10,10,10,10, 4,10,10,10,10,
	10,10,10,10,10,10, 4,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,
};

static float _vq_quantthresh__44u7__p9_0[] = {
	-2866.5, -2229.5, -1592.5, -955.5, -318.5, 318.5, 955.5, 1592.5,
	2229.5, 2866.5,
};

static long _vq_quantmap__44u7__p9_0[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u7__p9_0 = {
	_vq_quantthresh__44u7__p9_0,
	_vq_quantmap__44u7__p9_0,
	11,
	11
};

static static_codebook _44u7__p9_0 = {
	2, 121,
	_vq_lengthlist__44u7__p9_0,
	1, -512171520, 1630791680, 4, 0,
	_vq_quantlist__44u7__p9_0,
	NULL,
	&_vq_auxt__44u7__p9_0,
	NULL,
	0
};

static long _vq_quantlist__44u7__p9_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u7__p9_1[] = {
	 1, 4, 4, 6, 5, 8, 6, 9, 8,10, 9,11,10, 4, 6, 6,
	 8, 8, 9, 9,11,10,11,11,11,11, 4, 6, 6, 8, 8,10,
	 9,11,11,11,11,11,12, 6, 8, 8,10,10,11,11,12,12,
	13,12,13,13, 6, 8, 8,10,10,11,11,12,12,12,13,14,
	13, 8,10,10,11,11,12,13,14,14,14,14,15,15, 8,10,
	10,11,12,12,13,13,14,14,14,14,15, 9,11,11,13,13,
	14,14,15,14,16,15,17,15, 9,11,11,12,13,14,14,15,
	14,15,15,15,16,10,12,12,13,14,15,15,15,15,16,17,
	16,17,10,13,12,13,14,14,16,16,16,16,15,16,17,11,
	13,13,14,15,14,17,15,16,17,17,17,17,11,13,13,14,
	15,15,15,15,17,17,16,17,16,
};

static float _vq_quantthresh__44u7__p9_1[] = {
	-269.5, -220.5, -171.5, -122.5, -73.5, -24.5, 24.5, 73.5,
	122.5, 171.5, 220.5, 269.5,
};

static long _vq_quantmap__44u7__p9_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u7__p9_1 = {
	_vq_quantthresh__44u7__p9_1,
	_vq_quantmap__44u7__p9_1,
	13,
	13
};

static static_codebook _44u7__p9_1 = {
	2, 169,
	_vq_lengthlist__44u7__p9_1,
	1, -518889472, 1622704128, 4, 0,
	_vq_quantlist__44u7__p9_1,
	NULL,
	&_vq_auxt__44u7__p9_1,
	NULL,
	0
};

static long _vq_quantlist__44u7__p9_2[] = {
	24,
	23,
	25,
	22,
	26,
	21,
	27,
	20,
	28,
	19,
	29,
	18,
	30,
	17,
	31,
	16,
	32,
	15,
	33,
	14,
	34,
	13,
	35,
	12,
	36,
	11,
	37,
	10,
	38,
	9,
	39,
	8,
	40,
	7,
	41,
	6,
	42,
	5,
	43,
	4,
	44,
	3,
	45,
	2,
	46,
	1,
	47,
	0,
	48,
};

static long _vq_lengthlist__44u7__p9_2[] = {
	 2, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6,
	 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8,
	 8,
};

static float _vq_quantthresh__44u7__p9_2[] = {
	-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5,
	-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
	8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
	16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5,
};

static long _vq_quantmap__44u7__p9_2[] = {
	   47,   45,   43,   41,   39,   37,   35,   33,
	   31,   29,   27,   25,   23,   21,   19,   17,
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,   18,   20,   22,   24,   26,   28,   30,
	   32,   34,   36,   38,   40,   42,   44,   46,
	   48,
};

static encode_aux_threshmatch _vq_auxt__44u7__p9_2 = {
	_vq_quantthresh__44u7__p9_2,
	_vq_quantmap__44u7__p9_2,
	49,
	49
};

static static_codebook _44u7__p9_2 = {
	1, 49,
	_vq_lengthlist__44u7__p9_2,
	1, -526909440, 1611661312, 6, 0,
	_vq_quantlist__44u7__p9_2,
	NULL,
	&_vq_auxt__44u7__p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44u7__short[] = {
	 5,12,17,16,16,17,17,17,17,17, 4, 7,11,11,12, 9,
	17,10,17,17, 7, 7, 8, 9, 7, 9,11,10,15,17, 7, 9,
	10,11,10,12,14,12,16,17, 7, 8, 5, 7, 4, 7, 7, 8,
	16,16, 6,10, 9,10, 7,10,11,11,16,17, 6, 8, 8, 9,
	 5, 7, 5, 8,16,17, 5, 5, 8, 7, 6, 7, 7, 6, 6,14,
	12,10,12,11, 7,11, 4, 4, 2, 7,17,15,15,15, 8,15,
	 6, 8, 5, 9,
};

static static_codebook _huff_book__44u7__short = {
	2, 100,
	_huff_lengthlist__44u7__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u8__long[] = {
	 3, 9,13,14,14,15,14,14,15,15, 5, 4, 6, 8,10,12,
	12,14,15,15, 9, 5, 4, 5, 8,10,11,13,16,16,10, 7,
	 4, 3, 5, 7, 9,11,13,13,10, 9, 7, 4, 4, 6, 8,10,
	12,14,13,11, 9, 6, 5, 5, 6, 8,12,14,13,11,10, 8,
	 7, 6, 6, 7,10,14,13,11,12,10, 8, 7, 6, 6, 9,13,
	12,11,14,12,11, 9, 8, 7, 9,11,11,12,14,13,14,11,
	10, 8, 8, 9,
};

static static_codebook _huff_book__44u8__long = {
	2, 100,
	_huff_lengthlist__44u8__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u8__short[] = {
	 6,14,18,18,17,17,17,17,17,17, 4, 7, 9, 9,10,13,
	15,17,17,17, 6, 7, 5, 6, 8,11,16,17,16,17, 5, 7,
	 5, 4, 6,10,14,17,17,17, 6, 6, 6, 5, 7,10,13,16,
	17,17, 7, 6, 7, 7, 7, 8, 7,10,15,16,12, 9, 9, 6,
	 6, 5, 3, 5,11,15,14,14,13, 5, 5, 7, 3, 4, 8,15,
	17,17,13, 7, 7,10, 6, 6,10,15,17,17,16,10,11,14,
	10,10,15,17,
};

static static_codebook _huff_book__44u8__short = {
	2, 100,
	_huff_lengthlist__44u8__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u8_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u8_p1_0[] = {
	 1, 5, 5, 5, 7, 7, 5, 7, 7, 5, 7, 7, 8, 9, 9, 7,
	 9, 9, 5, 7, 7, 7, 9, 9, 8, 9, 9, 5, 7, 7, 7, 9,
	 9, 7, 9, 9, 7, 9, 9, 9,10,11, 9,11,10, 7, 9, 9,
	 9,11,10, 9,10,11, 5, 7, 7, 7, 9, 9, 7, 9, 9, 7,
	 9, 9, 9,11,10, 9,10,10, 8, 9, 9, 9,11,11, 9,11,
	10,
};

static float _vq_quantthresh__44u8_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u8_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u8_p1_0 = {
	_vq_quantthresh__44u8_p1_0,
	_vq_quantmap__44u8_p1_0,
	3,
	3
};

static static_codebook _44u8_p1_0 = {
	4, 81,
	_vq_lengthlist__44u8_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u8_p1_0,
	NULL,
	&_vq_auxt__44u8_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u8_p2_0[] = {
	 4, 5, 5, 8, 8, 5, 7, 6, 9, 9, 5, 6, 7, 9, 9, 8,
	 9, 9,11,11, 8, 9, 9,11,11, 5, 7, 7, 9, 9, 7, 8,
	 8,10,10, 7, 8, 8,10,10, 9,10,10,12,12, 9,10,10,
	11,12, 5, 7, 7, 9, 9, 7, 8, 7,10,10, 7, 8, 8,10,
	10, 9,10, 9,12,11, 9,10,10,12,12, 8, 9, 9,12,11,
	 9,10,10,12,12, 9,10,10,12,12,11,12,12,14,14,11,
	11,12,13,14, 8, 9, 9,11,11, 9,10,10,12,12, 9,10,
	10,12,12,11,12,11,13,13,11,12,12,14,14, 5, 7, 7,
	 9, 9, 7, 8, 8,10,10, 7, 8, 8,10,10, 9,10,10,12,
	12, 9,10,10,11,12, 7, 8, 8,10,10, 8, 9, 9,11,11,
	 8, 9, 9,11,11,10,11,11,12,13,10,11,11,12,13, 6,
	 8, 8,10,10, 8, 9, 8,11,10, 8, 9, 9,11,11,10,11,
	10,13,12,10,11,11,13,13, 9,10,10,12,12,10,11,11,
	13,13,10,11,11,13,13,12,12,13,13,14,12,13,13,14,
	14, 9,10,10,12,12,10,11,10,13,12,10,11,11,13,13,
	11,13,12,14,13,12,13,13,14,14, 5, 7, 7, 9, 9, 7,
	 8, 8,10,10, 7, 8, 8,10,10, 9,10,10,12,12, 9,10,
	10,12,12, 7, 8, 8,10,10, 8, 9, 9,11,11, 8, 8, 9,
	10,11,10,11,11,13,13,10,10,11,12,13, 7, 8, 8,10,
	10, 8, 9, 9,11,11, 8, 9, 9,11,11,10,11,11,13,13,
	10,11,11,13,12, 9,10,10,12,12,10,11,11,13,13,10,
	10,11,12,13,12,13,13,14,14,12,12,13,13,14, 9,10,
	10,12,12,10,11,11,13,13,10,11,11,13,13,12,13,13,
	15,14,12,13,13,14,13, 8, 9, 9,11,11, 9,10,10,12,
	12, 9,10,10,12,12,12,12,12,14,13,11,12,12,14,14,
	 9,10,10,12,12,10,11,11,13,13,10,11,11,13,13,12,
	13,13,14,15,12,13,13,14,15, 9,10,10,12,12,10,11,
	10,13,12,10,11,11,13,13,12,13,12,15,14,12,13,13,
	14,15,11,12,12,14,14,12,13,13,14,14,12,13,13,15,
	14,14,14,14,14,16,14,14,15,16,16,11,12,12,14,14,
	11,12,12,14,14,12,13,13,14,15,13,14,13,16,14,14,
	14,14,16,16, 8, 9, 9,11,11, 9,10,10,12,12, 9,10,
	10,12,12,11,12,12,14,13,11,12,12,14,14, 9,10,10,
	12,12,10,11,11,13,13,10,10,11,12,13,12,13,13,15,
	14,12,12,13,13,14, 9,10,10,12,12,10,11,11,13,13,
	10,11,11,13,13,12,13,13,14,14,12,13,13,15,14,11,
	12,12,14,13,12,13,13,15,14,11,12,12,13,14,14,15,
	14,16,15,13,13,14,13,16,11,12,12,14,14,12,13,13,
	14,15,12,13,12,15,14,14,14,14,16,15,14,15,13,16,
	14,
};

static float _vq_quantthresh__44u8_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u8_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u8_p2_0 = {
	_vq_quantthresh__44u8_p2_0,
	_vq_quantmap__44u8_p2_0,
	5,
	5
};

static static_codebook _44u8_p2_0 = {
	4, 625,
	_vq_lengthlist__44u8_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u8_p2_0,
	NULL,
	&_vq_auxt__44u8_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u8_p3_0[] = {
	 3, 4, 4, 5, 5, 7, 7, 9, 9, 4, 5, 4, 6, 6, 7, 7,
	 9, 9, 4, 4, 5, 6, 6, 7, 7, 9, 9, 5, 6, 6, 7, 7,
	 8, 8,10,10, 6, 6, 6, 7, 7, 8, 8,10,10, 7, 7, 7,
	 8, 8, 9, 9,11,10, 7, 7, 7, 8, 8, 9, 9,10,11, 9,
	 9, 9,10,10,11,10,12,11, 9, 9, 9, 9,10,11,11,11,
	12,
};

static float _vq_quantthresh__44u8_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u8_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u8_p3_0 = {
	_vq_quantthresh__44u8_p3_0,
	_vq_quantmap__44u8_p3_0,
	9,
	9
};

static static_codebook _44u8_p3_0 = {
	2, 81,
	_vq_lengthlist__44u8_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u8_p3_0,
	NULL,
	&_vq_auxt__44u8_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p4_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44u8_p4_0[] = {
	 4, 4, 4, 6, 6, 7, 7, 8, 8, 8, 8,10,10,11,11,11,
	11, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,11,
	12,12, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,
	11,12,12, 6, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,10,
	11,11,12,12, 6, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9,10,
	10,11,11,12,12, 7, 7, 7, 8, 8, 9, 8,10, 9,10, 9,
	11,10,12,11,13,12, 7, 7, 7, 8, 8, 8, 9, 9,10, 9,
	10,10,11,11,12,12,13, 8, 8, 8, 9, 9, 9, 9,10,10,
	11,10,11,11,12,12,13,13, 8, 8, 8, 9, 9, 9,10,10,
	10,10,11,11,11,12,12,12,13, 8, 9, 9, 9, 9,10, 9,
	11,10,11,11,12,11,13,12,13,13, 8, 9, 9, 9, 9, 9,
	10,10,11,11,11,11,12,12,13,13,13,10,10,10,10,10,
	11,10,11,11,12,11,13,12,13,13,14,13,10,10,10,10,
	10,10,11,11,11,11,12,12,13,13,13,13,14,11,11,11,
	11,11,12,11,12,12,13,12,13,13,14,13,14,14,11,11,
	11,11,11,11,12,12,12,12,13,13,13,13,14,14,14,11,
	12,12,12,12,13,12,13,12,13,13,14,13,14,14,14,14,
	11,12,12,12,12,12,12,13,13,13,13,13,14,14,14,14,
	14,
};

static float _vq_quantthresh__44u8_p4_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44u8_p4_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44u8_p4_0 = {
	_vq_quantthresh__44u8_p4_0,
	_vq_quantmap__44u8_p4_0,
	17,
	17
};

static static_codebook _44u8_p4_0 = {
	2, 289,
	_vq_lengthlist__44u8_p4_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44u8_p4_0,
	NULL,
	&_vq_auxt__44u8_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p5_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u8_p5_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 8, 8, 8, 9, 9, 7,
	 9, 9, 5, 8, 8, 7, 9, 9, 8, 9, 9, 5, 8, 8, 8,10,
	10, 8,10,10, 7,10,10, 9,10,12, 9,12,11, 7,10,10,
	 9,11,10, 9,11,12, 5, 8, 8, 8,10,10, 8,10,10, 7,
	10,10, 9,11,11, 9,10,11, 7,10,10, 9,11,11,10,12,
	10,
};

static float _vq_quantthresh__44u8_p5_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44u8_p5_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u8_p5_0 = {
	_vq_quantthresh__44u8_p5_0,
	_vq_quantmap__44u8_p5_0,
	3,
	3
};

static static_codebook _44u8_p5_0 = {
	4, 81,
	_vq_lengthlist__44u8_p5_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44u8_p5_0,
	NULL,
	&_vq_auxt__44u8_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p5_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u8_p5_1[] = {
	 4, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 5, 5, 5, 6, 6,
	 7, 7, 8, 8, 8, 8, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8,
	 8, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 6, 6, 6, 7,
	 7, 7, 7, 8, 8, 8, 8, 7, 7, 7, 7, 7, 8, 8, 8, 8,
	 8, 8, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 7, 8, 7,
	 8, 8, 8, 8, 8, 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 8, 8,
	 8, 8, 8, 8, 8, 8, 8, 9, 9,
};

static float _vq_quantthresh__44u8_p5_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u8_p5_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u8_p5_1 = {
	_vq_quantthresh__44u8_p5_1,
	_vq_quantmap__44u8_p5_1,
	11,
	11
};

static static_codebook _44u8_p5_1 = {
	2, 121,
	_vq_lengthlist__44u8_p5_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u8_p5_1,
	NULL,
	&_vq_auxt__44u8_p5_1,
	NULL,
	0
};

static long _vq_quantlist__44u8_p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u8_p6_0[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 4, 6, 5,
	 7, 7, 8, 8, 8, 8, 9, 9,10,10, 4, 6, 6, 7, 7, 8,
	 8, 8, 8, 9, 9,10,10, 6, 7, 7, 7, 8, 8, 8, 8, 9,
	 9,10,10,10, 6, 7, 7, 8, 8, 8, 8, 9, 8,10, 9,11,
	10, 7, 8, 8, 8, 8, 8, 9, 9, 9,10,10,11,11, 7, 8,
	 8, 8, 8, 9, 8, 9, 9,10,10,11,11, 8, 8, 8, 9, 9,
	 9, 9, 9,10,10,10,11,11, 8, 8, 8, 9, 9, 9, 9,10,
	 9,10,10,11,11, 9, 9, 9, 9,10,10,10,10,10,10,11,
	11,12, 9, 9, 9,10, 9,10,10,10,10,11,10,12,11,10,
	10,10,10,10,11,11,11,11,11,12,12,12,10,10,10,10,
	11,11,11,11,11,12,11,12,12,
};

static float _vq_quantthresh__44u8_p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44u8_p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u8_p6_0 = {
	_vq_quantthresh__44u8_p6_0,
	_vq_quantmap__44u8_p6_0,
	13,
	13
};

static static_codebook _44u8_p6_0 = {
	2, 169,
	_vq_lengthlist__44u8_p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44u8_p6_0,
	NULL,
	&_vq_auxt__44u8_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u8_p6_1[] = {
	 3, 4, 4, 5, 5, 4, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5,
	 5, 5, 5, 5, 5, 5, 5, 5, 5,
};

static float _vq_quantthresh__44u8_p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u8_p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u8_p6_1 = {
	_vq_quantthresh__44u8_p6_1,
	_vq_quantmap__44u8_p6_1,
	5,
	5
};

static static_codebook _44u8_p6_1 = {
	2, 25,
	_vq_lengthlist__44u8_p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u8_p6_1,
	NULL,
	&_vq_auxt__44u8_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44u8_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u8_p7_0[] = {
	 1, 4, 5, 6, 6, 7, 7, 8, 8,10,10,11,11, 5, 6, 6,
	 7, 7, 8, 8, 9, 9,11,10,12,11, 5, 6, 6, 7, 7, 8,
	 8, 9, 9,10,11,11,12, 6, 7, 7, 8, 8, 9, 9,10,10,
	11,11,12,12, 6, 7, 7, 8, 8, 9, 9,10,10,11,12,13,
	12, 7, 8, 8, 9, 9,10,10,11,11,12,12,13,13, 8, 8,
	 8, 9, 9,10,10,11,11,12,12,13,13, 9, 9, 9,10,10,
	11,11,12,12,13,13,14,14, 9, 9, 9,10,10,11,11,12,
	12,13,13,14,14,10,11,11,12,11,13,12,13,13,14,14,
	15,15,10,11,11,11,12,12,13,13,14,14,14,15,15,11,
	12,12,13,13,14,13,15,14,15,15,16,15,11,11,12,13,
	13,13,14,14,14,15,15,15,16,
};

static float _vq_quantthresh__44u8_p7_0[] = {
	-60.5, -49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5,
	27.5, 38.5, 49.5, 60.5,
};

static long _vq_quantmap__44u8_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u8_p7_0 = {
	_vq_quantthresh__44u8_p7_0,
	_vq_quantmap__44u8_p7_0,
	13,
	13
};

static static_codebook _44u8_p7_0 = {
	2, 169,
	_vq_lengthlist__44u8_p7_0,
	1, -523206656, 1618345984, 4, 0,
	_vq_quantlist__44u8_p7_0,
	NULL,
	&_vq_auxt__44u8_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u8_p7_1[] = {
	 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 5, 6, 6, 7, 7,
	 7, 7, 7, 7, 7, 7, 5, 6, 6, 7, 7, 7, 7, 7, 7, 7,
	 7, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 6, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 8, 7, 7, 7, 7, 7, 7, 7, 8, 8,
	 8, 8, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 7, 7, 7,
	 8, 7, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 8, 8, 8,
	 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7,
	 7, 8, 8, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44u8_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u8_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u8_p7_1 = {
	_vq_quantthresh__44u8_p7_1,
	_vq_quantmap__44u8_p7_1,
	11,
	11
};

static static_codebook _44u8_p7_1 = {
	2, 121,
	_vq_lengthlist__44u8_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u8_p7_1,
	NULL,
	&_vq_auxt__44u8_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u8_p8_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44u8_p8_0[] = {
	 1, 4, 4, 7, 7, 8, 8, 8, 7, 9, 8,10, 9,11,10, 4,
	 6, 6, 8, 8,10, 9, 9, 9,10,10,11,10,12,10, 4, 6,
	 6, 8, 8,10,10, 9, 9,10,10,11,11,11,12, 7, 8, 8,
	10,10,11,11,11,10,12,11,12,12,13,11, 7, 8, 8,10,
	10,11,11,10,10,11,11,12,12,13,13, 8,10,10,11,11,
	12,11,12,11,13,12,13,12,14,13, 8,10, 9,11,11,12,
	12,12,12,12,12,13,13,14,13, 8, 9, 9,11,10,12,11,
	13,12,13,13,14,13,14,13, 8, 9, 9,10,11,12,12,12,
	12,13,13,14,15,14,14, 9,10,10,12,11,13,12,13,13,
	14,13,14,14,14,14, 9,10,10,12,12,12,12,13,13,14,
	14,14,15,14,14,10,11,11,13,12,13,12,14,14,14,14,
	14,14,15,15,10,11,11,12,12,13,13,14,14,14,15,15,
	14,16,15,11,12,12,13,12,14,14,14,13,15,14,15,15,
	15,17,11,12,12,13,13,14,14,14,15,15,14,15,15,14,
	17,
};

static float _vq_quantthresh__44u8_p8_0[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__44u8_p8_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44u8_p8_0 = {
	_vq_quantthresh__44u8_p8_0,
	_vq_quantmap__44u8_p8_0,
	15,
	15
};

static static_codebook _44u8_p8_0 = {
	2, 225,
	_vq_lengthlist__44u8_p8_0,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__44u8_p8_0,
	NULL,
	&_vq_auxt__44u8_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p8_1[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__44u8_p8_1[] = {
	 4, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 6, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 6, 6, 7, 7, 8,
	 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7,
	 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10, 9,10, 8, 8,
	 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,10,
	10, 9,10, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,
	10,10,10,10,10,10,10,10, 8, 9, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10,10,10, 9,10,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10, 9,10,10,10,10,10,10,
	10,10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,10,
	10,10,10,10,10,10,10, 9, 9, 9, 9, 9, 9, 9,10, 9,
	10,10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,
	10, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10, 9, 9, 9, 9, 9, 9, 9,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9,
	 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10, 9, 9, 9,10, 9,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9,10,
	 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9,
	 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10, 9, 9, 9,10, 9,10, 9,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__44u8_p8_1[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__44u8_p8_1[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__44u8_p8_1 = {
	_vq_quantthresh__44u8_p8_1,
	_vq_quantmap__44u8_p8_1,
	21,
	21
};

static static_codebook _44u8_p8_1 = {
	2, 441,
	_vq_lengthlist__44u8_p8_1,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__44u8_p8_1,
	NULL,
	&_vq_auxt__44u8_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44u8_p9_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u8_p9_0[] = {
	 1, 3, 3, 9, 9, 9, 9, 9, 9, 4, 9, 9, 9, 9, 9, 9,
	 9, 9, 5, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8,
	 8,
};

static float _vq_quantthresh__44u8_p9_0[] = {
	-3258.5, -2327.5, -1396.5, -465.5, 465.5, 1396.5, 2327.5, 3258.5,
};

static long _vq_quantmap__44u8_p9_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u8_p9_0 = {
	_vq_quantthresh__44u8_p9_0,
	_vq_quantmap__44u8_p9_0,
	9,
	9
};

static static_codebook _44u8_p9_0 = {
	2, 81,
	_vq_lengthlist__44u8_p9_0,
	1, -511895552, 1631393792, 4, 0,
	_vq_quantlist__44u8_p9_0,
	NULL,
	&_vq_auxt__44u8_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44u8_p9_1[] = {
	9,
	8,
	10,
	7,
	11,
	6,
	12,
	5,
	13,
	4,
	14,
	3,
	15,
	2,
	16,
	1,
	17,
	0,
	18,
};

static long _vq_lengthlist__44u8_p9_1[] = {
	 1, 4, 4, 7, 7, 8, 7, 8, 6, 9, 7,10, 8,11,10,11,
	11,11,11, 4, 7, 6, 9, 9,10, 9, 9, 9,10,10,11,10,
	11,10,11,11,13,11, 4, 7, 7, 9, 9, 9, 9, 9, 9,10,
	10,11,10,11,11,11,12,11,12, 7, 9, 8,11,11,11,11,
	10,10,11,11,12,12,12,12,12,12,14,13, 7, 8, 9,10,
	11,11,11,10,10,11,11,11,11,12,12,14,12,13,14, 8,
	 9, 9,11,11,11,11,11,11,12,12,14,12,15,14,14,14,
	15,14, 8, 9, 9,11,11,11,11,12,11,12,12,13,13,13,
	13,13,13,14,14, 8, 9, 9,11,10,12,11,12,12,13,13,
	13,13,15,14,14,14,16,16, 8, 9, 9,10,11,11,12,12,
	12,13,13,13,14,14,14,15,16,15,15, 9,10,10,11,12,
	12,13,13,13,14,14,16,14,14,16,16,16,16,15, 9,10,
	10,11,11,12,13,13,14,15,14,16,14,15,16,16,16,16,
	15,10,11,11,12,13,13,14,15,15,15,15,15,16,15,16,
	15,16,15,15,10,11,11,13,13,14,13,13,15,14,15,15,
	16,15,15,15,16,15,16,10,12,12,14,14,14,14,14,16,
	16,15,15,15,16,16,16,16,16,16,11,12,12,14,14,14,
	14,15,15,16,15,16,15,16,15,16,16,16,16,12,12,13,
	14,14,15,16,16,16,16,16,16,15,16,16,16,16,16,16,
	12,13,13,14,14,14,14,15,16,15,16,16,16,16,16,16,
	16,16,16,12,13,14,14,14,16,15,16,15,16,16,16,16,
	16,16,16,16,16,16,12,14,13,14,15,15,15,16,15,16,
	16,15,16,16,16,16,16,16,16,
};

static float _vq_quantthresh__44u8_p9_1[] = {
	-416.5, -367.5, -318.5, -269.5, -220.5, -171.5, -122.5, -73.5,
	-24.5, 24.5, 73.5, 122.5, 171.5, 220.5, 269.5, 318.5,
	367.5, 416.5,
};

static long _vq_quantmap__44u8_p9_1[] = {
	   17,   15,   13,   11,	9,	7,	5,	3,
		1,	0,	2,	4,	6,	8,   10,   12,
	   14,   16,   18,
};

static encode_aux_threshmatch _vq_auxt__44u8_p9_1 = {
	_vq_quantthresh__44u8_p9_1,
	_vq_quantmap__44u8_p9_1,
	19,
	19
};

static static_codebook _44u8_p9_1 = {
	2, 361,
	_vq_lengthlist__44u8_p9_1,
	1, -518287360, 1622704128, 5, 0,
	_vq_quantlist__44u8_p9_1,
	NULL,
	&_vq_auxt__44u8_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44u8_p9_2[] = {
	24,
	23,
	25,
	22,
	26,
	21,
	27,
	20,
	28,
	19,
	29,
	18,
	30,
	17,
	31,
	16,
	32,
	15,
	33,
	14,
	34,
	13,
	35,
	12,
	36,
	11,
	37,
	10,
	38,
	9,
	39,
	8,
	40,
	7,
	41,
	6,
	42,
	5,
	43,
	4,
	44,
	3,
	45,
	2,
	46,
	1,
	47,
	0,
	48,
};

static long _vq_lengthlist__44u8_p9_2[] = {
	 2, 3, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
	 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__44u8_p9_2[] = {
	-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5,
	-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
	8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
	16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5,
};

static long _vq_quantmap__44u8_p9_2[] = {
	   47,   45,   43,   41,   39,   37,   35,   33,
	   31,   29,   27,   25,   23,   21,   19,   17,
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,   18,   20,   22,   24,   26,   28,   30,
	   32,   34,   36,   38,   40,   42,   44,   46,
	   48,
};

static encode_aux_threshmatch _vq_auxt__44u8_p9_2 = {
	_vq_quantthresh__44u8_p9_2,
	_vq_quantmap__44u8_p9_2,
	49,
	49
};

static static_codebook _44u8_p9_2 = {
	1, 49,
	_vq_lengthlist__44u8_p9_2,
	1, -526909440, 1611661312, 6, 0,
	_vq_quantlist__44u8_p9_2,
	NULL,
	&_vq_auxt__44u8_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44u9__long[] = {
	 3, 9,13,13,14,15,14,14,15,15, 5, 5, 9,10,12,12,
	13,14,16,15,10, 6, 6, 6, 8,11,12,13,16,15,11, 7,
	 5, 3, 5, 8,10,12,15,15,10,10, 7, 4, 3, 5, 8,10,
	12,12,12,12, 9, 7, 5, 4, 6, 8,10,13,13,12,11, 9,
	 7, 5, 5, 6, 9,12,14,12,12,10, 8, 6, 6, 6, 7,11,
	13,12,14,13,10, 8, 7, 7, 7,10,11,11,12,13,12,11,
	10, 8, 8, 9,
};

static static_codebook _huff_book__44u9__long = {
	2, 100,
	_huff_lengthlist__44u9__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _huff_lengthlist__44u9__short[] = {
	 9,16,18,18,17,17,17,17,17,17, 5, 8,11,12,11,12,
	17,17,16,16, 6, 6, 8, 8, 9,10,14,15,16,16, 6, 7,
	 7, 4, 6, 9,13,16,16,16, 6, 6, 7, 4, 5, 8,11,15,
	17,16, 7, 6, 7, 6, 6, 8, 9,10,14,16,11, 8, 8, 7,
	 6, 6, 3, 4,10,15,14,12,12,10, 5, 6, 3, 3, 8,13,
	15,17,15,11, 6, 8, 6, 6, 9,14,17,15,15,12, 8,10,
	 9, 9,12,15,
};

static static_codebook _huff_book__44u9__short = {
	2, 100,
	_huff_lengthlist__44u9__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44u9_p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u9_p1_0[] = {
	 1, 5, 5, 5, 7, 7, 5, 7, 7, 5, 7, 7, 7, 9, 9, 7,
	 9, 9, 5, 7, 7, 7, 9, 9, 7, 9, 9, 5, 7, 7, 7, 9,
	 9, 7, 9, 9, 8, 9, 9, 9,10,11, 9,11,11, 7, 9, 9,
	 9,11,10, 9,11,11, 5, 7, 7, 7, 9, 9, 8, 9,10, 7,
	 9, 9, 9,11,11, 9,10,11, 7, 9,10, 9,11,11, 9,11,
	10,
};

static float _vq_quantthresh__44u9_p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44u9_p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u9_p1_0 = {
	_vq_quantthresh__44u9_p1_0,
	_vq_quantmap__44u9_p1_0,
	3,
	3
};

static static_codebook _44u9_p1_0 = {
	4, 81,
	_vq_lengthlist__44u9_p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44u9_p1_0,
	NULL,
	&_vq_auxt__44u9_p1_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p2_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u9_p2_0[] = {
	 3, 5, 5, 8, 8, 5, 7, 7, 9, 9, 6, 7, 7, 9, 9, 8,
	 9, 9,11,10, 8, 9, 9,11,11, 6, 7, 7, 9, 9, 7, 8,
	 8,10,10, 7, 8, 8, 9,10, 9,10,10,11,11, 9, 9,10,
	11,11, 6, 7, 7, 9, 9, 7, 8, 8,10, 9, 7, 8, 8,10,
	10, 9,10, 9,11,11, 9,10,10,11,11, 8, 9, 9,11,11,
	 9,10,10,12,11, 9,10,10,11,12,11,11,11,13,13,11,
	11,11,12,13, 8, 9, 9,11,11, 9,10,10,11,11, 9,10,
	10,12,11,11,12,11,13,12,11,11,12,13,13, 6, 7, 7,
	 9, 9, 7, 8, 8,10,10, 7, 8, 8,10,10, 9,10,10,12,
	11, 9,10,10,11,12, 7, 8, 8,10,10, 8, 9, 9,11,11,
	 8, 9, 9,10,10,10,11,11,12,12,10,10,11,12,12, 7,
	 8, 8,10,10, 8, 9, 8,10,10, 8, 9, 9,10,10,10,11,
	10,12,11,10,10,11,12,12, 9,10,10,11,12,10,11,11,
	12,12,10,11,10,12,12,12,12,12,13,13,11,12,12,13,
	13, 9,10,10,11,11, 9,10,10,12,12,10,11,11,12,13,
	11,12,11,13,12,12,12,12,13,14, 6, 7, 7, 9, 9, 7,
	 8, 8,10,10, 7, 8, 8,10,10, 9,10,10,11,11, 9,10,
	10,11,12, 7, 8, 8,10,10, 8, 9, 9,11,10, 8, 8, 9,
	10,10,10,11,10,12,12,10,10,11,11,12, 7, 8, 8,10,
	10, 8, 9, 9,10,10, 8, 9, 9,10,10,10,11,10,12,12,
	10,11,10,12,12, 9,10,10,12,11,10,11,11,12,12, 9,
	10,10,12,12,12,12,12,13,13,11,11,12,12,14, 9,10,
	10,11,12,10,11,11,12,12,10,11,11,12,12,11,12,12,
	14,14,12,12,12,13,13, 8, 9, 9,11,11, 9,10,10,12,
	11, 9,10,10,12,12,11,12,11,13,13,11,11,12,13,13,
	 9,10,10,12,12,10,11,11,12,12,10,11,11,12,12,12,
	12,12,14,14,12,12,12,13,13, 9,10,10,12,11,10,11,
	10,12,12,10,11,11,12,12,11,12,12,14,13,12,12,12,
	13,14,11,12,11,13,13,11,12,12,13,13,12,12,12,14,
	14,13,13,13,13,15,13,13,14,15,15,11,11,11,13,13,
	11,12,11,13,13,11,12,12,13,13,12,13,12,15,13,13,
	13,14,14,15, 8, 9, 9,11,11, 9,10,10,11,12, 9,10,
	10,11,12,11,12,11,13,13,11,12,12,13,13, 9,10,10,
	11,12,10,11,10,12,12,10,10,11,12,13,12,12,12,14,
	13,11,12,12,13,14, 9,10,10,12,12,10,11,11,12,12,
	10,11,11,12,12,12,12,12,14,13,12,12,12,14,13,11,
	11,11,13,13,11,12,12,14,13,11,11,12,13,13,13,13,
	13,15,14,12,12,13,13,15,11,12,12,13,13,12,12,12,
	13,14,11,12,12,13,13,13,13,14,14,15,13,13,13,14,
	14,
};

static float _vq_quantthresh__44u9_p2_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u9_p2_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u9_p2_0 = {
	_vq_quantthresh__44u9_p2_0,
	_vq_quantmap__44u9_p2_0,
	5,
	5
};

static static_codebook _44u9_p2_0 = {
	4, 625,
	_vq_lengthlist__44u9_p2_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u9_p2_0,
	NULL,
	&_vq_auxt__44u9_p2_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p3_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44u9_p3_0[] = {
	 3, 4, 4, 5, 5, 7, 7, 8, 8, 4, 5, 5, 6, 6, 7, 7,
	 9, 9, 4, 4, 5, 6, 6, 7, 7, 9, 9, 5, 6, 6, 7, 7,
	 8, 8, 9, 9, 5, 6, 6, 7, 7, 8, 8, 9, 9, 7, 7, 7,
	 8, 8, 9, 9,10,10, 7, 7, 7, 8, 8, 9, 9,10,10, 8,
	 9, 9,10, 9,10,10,11,11, 8, 9, 9, 9,10,10,10,11,
	11,
};

static float _vq_quantthresh__44u9_p3_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44u9_p3_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44u9_p3_0 = {
	_vq_quantthresh__44u9_p3_0,
	_vq_quantmap__44u9_p3_0,
	9,
	9
};

static static_codebook _44u9_p3_0 = {
	2, 81,
	_vq_lengthlist__44u9_p3_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44u9_p3_0,
	NULL,
	&_vq_auxt__44u9_p3_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p4_0[] = {
	8,
	7,
	9,
	6,
	10,
	5,
	11,
	4,
	12,
	3,
	13,
	2,
	14,
	1,
	15,
	0,
	16,
};

static long _vq_lengthlist__44u9_p4_0[] = {
	 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,11,
	11, 5, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,
	11,11, 5, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,
	10,11,11, 6, 6, 6, 7, 6, 7, 7, 8, 8, 9, 9,10,10,
	11,11,12,11, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9,10,
	10,11,11,11,12, 7, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9,
	10,10,11,11,12,12, 7, 7, 7, 7, 7, 8, 8, 9, 9, 9,
	 9,10,10,11,11,12,12, 8, 8, 8, 8, 8, 9, 8,10, 9,
	10,10,11,10,12,11,13,12, 8, 8, 8, 8, 8, 9, 9, 9,
	10,10,10,10,11,11,12,12,12, 8, 8, 8, 9, 9, 9, 9,
	10,10,11,10,12,11,12,12,13,12, 8, 8, 8, 9, 9, 9,
	 9,10,10,10,11,11,11,12,12,12,13, 9, 9, 9,10,10,
	10,10,11,10,11,11,12,11,13,12,13,13, 9, 9,10,10,
	10,10,10,10,11,11,11,11,12,12,13,13,13,10,11,10,
	11,11,11,11,12,11,12,12,13,12,13,13,14,13,10,10,
	10,11,11,11,11,11,12,12,12,12,13,13,13,13,14,11,
	11,11,12,11,12,12,12,12,13,13,13,13,14,13,14,14,
	11,11,11,11,12,12,12,12,12,12,13,13,13,13,14,14,
	14,
};

static float _vq_quantthresh__44u9_p4_0[] = {
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
};

static long _vq_quantmap__44u9_p4_0[] = {
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,
};

static encode_aux_threshmatch _vq_auxt__44u9_p4_0 = {
	_vq_quantthresh__44u9_p4_0,
	_vq_quantmap__44u9_p4_0,
	17,
	17
};

static static_codebook _44u9_p4_0 = {
	2, 289,
	_vq_lengthlist__44u9_p4_0,
	1, -529530880, 1611661312, 5, 0,
	_vq_quantlist__44u9_p4_0,
	NULL,
	&_vq_auxt__44u9_p4_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p5_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44u9_p5_0[] = {
	 1, 4, 4, 5, 7, 7, 5, 7, 7, 5, 8, 8, 8, 9, 9, 7,
	 9, 9, 5, 8, 8, 7, 9, 9, 8, 9, 9, 5, 8, 8, 8,10,
	10, 8,10,10, 7,10,10, 9,10,12, 9,11,11, 7,10,10,
	 9,11,10, 9,11,12, 5, 8, 8, 8,10,10, 8,10,10, 7,
	10,10, 9,12,11, 9,10,11, 7,10,10, 9,11,11,10,12,
	10,
};

static float _vq_quantthresh__44u9_p5_0[] = {
	-5.5, 5.5,
};

static long _vq_quantmap__44u9_p5_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44u9_p5_0 = {
	_vq_quantthresh__44u9_p5_0,
	_vq_quantmap__44u9_p5_0,
	3,
	3
};

static static_codebook _44u9_p5_0 = {
	4, 81,
	_vq_lengthlist__44u9_p5_0,
	1, -529137664, 1618345984, 2, 0,
	_vq_quantlist__44u9_p5_0,
	NULL,
	&_vq_auxt__44u9_p5_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p5_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u9_p5_1[] = {
	 5, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 5, 6, 6, 6, 6,
	 7, 7, 7, 7, 8, 7, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7,
	 7, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 6, 6, 6, 7,
	 7, 7, 7, 7, 7, 8, 8, 7, 7, 7, 7, 7, 8, 7, 8, 8,
	 8, 8, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7,
	 8, 7, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 8, 8, 8, 8,
	 8, 8, 8, 7, 8, 7, 8, 8, 8, 8, 8, 8, 8, 8, 7, 8,
	 8, 8, 8, 8, 8, 8, 8, 8, 8,
};

static float _vq_quantthresh__44u9_p5_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u9_p5_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u9_p5_1 = {
	_vq_quantthresh__44u9_p5_1,
	_vq_quantmap__44u9_p5_1,
	11,
	11
};

static static_codebook _44u9_p5_1 = {
	2, 121,
	_vq_lengthlist__44u9_p5_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u9_p5_1,
	NULL,
	&_vq_auxt__44u9_p5_1,
	NULL,
	0
};

static long _vq_quantlist__44u9_p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u9_p6_0[] = {
	 2, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 4, 6, 5,
	 7, 7, 8, 8, 8, 8, 9, 9,10,10, 4, 5, 6, 7, 7, 8,
	 8, 8, 8, 9, 9,10,10, 6, 7, 7, 8, 8, 8, 8, 9, 9,
	10,10,10,10, 6, 7, 7, 8, 8, 8, 8, 9, 9,10,10,10,
	10, 7, 8, 8, 8, 8, 9, 9, 9, 9,10,10,11,11, 7, 8,
	 8, 8, 8, 9, 9, 9, 9,10,10,11,11, 8, 8, 8, 9, 9,
	 9, 9, 9,10,10,10,11,11, 8, 8, 8, 9, 9, 9, 9,10,
	 9,10,10,11,11, 9, 9, 9,10,10,10,10,10,11,11,11,
	11,12, 9, 9, 9,10,10,10,10,10,10,11,10,12,11,10,
	10,10,10,10,11,11,11,11,11,12,12,12,10,10,10,10,
	10,11,11,11,11,12,11,12,12,
};

static float _vq_quantthresh__44u9_p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44u9_p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u9_p6_0 = {
	_vq_quantthresh__44u9_p6_0,
	_vq_quantmap__44u9_p6_0,
	13,
	13
};

static static_codebook _44u9_p6_0 = {
	2, 169,
	_vq_lengthlist__44u9_p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44u9_p6_0,
	NULL,
	&_vq_auxt__44u9_p6_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44u9_p6_1[] = {
	 4, 4, 4, 5, 5, 4, 5, 4, 5, 5, 4, 4, 5, 5, 5, 5,
	 5, 5, 5, 5, 5, 5, 5, 5, 5,
};

static float _vq_quantthresh__44u9_p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44u9_p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44u9_p6_1 = {
	_vq_quantthresh__44u9_p6_1,
	_vq_quantmap__44u9_p6_1,
	5,
	5
};

static static_codebook _44u9_p6_1 = {
	2, 25,
	_vq_lengthlist__44u9_p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44u9_p6_1,
	NULL,
	&_vq_auxt__44u9_p6_1,
	NULL,
	0
};

static long _vq_quantlist__44u9_p7_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44u9_p7_0[] = {
	 1, 4, 5, 6, 6, 7, 7, 8, 9,10,10,11,11, 5, 6, 6,
	 7, 7, 8, 8, 9, 9,10,10,11,11, 5, 6, 6, 7, 7, 8,
	 8, 9, 9,10,10,11,11, 6, 7, 7, 8, 8, 9, 9,10,10,
	11,11,12,12, 6, 7, 7, 8, 8, 9, 9,10,10,11,11,12,
	12, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13, 8, 8,
	 8, 9, 9,10,10,11,11,12,12,13,13, 9, 9, 9,10,10,
	11,11,12,12,13,13,13,13, 9, 9, 9,10,10,11,11,12,
	12,13,13,14,14,10,10,10,11,11,12,12,13,13,14,13,
	15,14,10,10,10,11,11,12,12,13,13,14,14,14,14,11,
	11,12,12,12,13,13,14,14,14,14,15,15,11,11,12,12,
	12,13,13,14,14,14,15,15,15,
};

static float _vq_quantthresh__44u9_p7_0[] = {
	-60.5, -49.5, -38.5, -27.5, -16.5, -5.5, 5.5, 16.5,
	27.5, 38.5, 49.5, 60.5,
};

static long _vq_quantmap__44u9_p7_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44u9_p7_0 = {
	_vq_quantthresh__44u9_p7_0,
	_vq_quantmap__44u9_p7_0,
	13,
	13
};

static static_codebook _44u9_p7_0 = {
	2, 169,
	_vq_lengthlist__44u9_p7_0,
	1, -523206656, 1618345984, 4, 0,
	_vq_quantlist__44u9_p7_0,
	NULL,
	&_vq_auxt__44u9_p7_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p7_1[] = {
	5,
	4,
	6,
	3,
	7,
	2,
	8,
	1,
	9,
	0,
	10,
};

static long _vq_lengthlist__44u9_p7_1[] = {
	 5, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 7, 7,
	 7, 7, 7, 7, 7, 7, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 8, 8, 7, 7, 7, 7, 7, 7, 7, 8, 7, 8, 8, 7, 7,
	 7, 7, 7, 7, 7, 8, 8, 8, 8,
};

static float _vq_quantthresh__44u9_p7_1[] = {
	-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
	3.5, 4.5,
};

static long _vq_quantmap__44u9_p7_1[] = {
		9,	7,	5,	3,	1,	0,	2,	4,
		6,	8,   10,
};

static encode_aux_threshmatch _vq_auxt__44u9_p7_1 = {
	_vq_quantthresh__44u9_p7_1,
	_vq_quantmap__44u9_p7_1,
	11,
	11
};

static static_codebook _44u9_p7_1 = {
	2, 121,
	_vq_lengthlist__44u9_p7_1,
	1, -531365888, 1611661312, 4, 0,
	_vq_quantlist__44u9_p7_1,
	NULL,
	&_vq_auxt__44u9_p7_1,
	NULL,
	0
};

static long _vq_quantlist__44u9_p8_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44u9_p8_0[] = {
	 1, 4, 4, 7, 7, 8, 8, 8, 8, 9, 9,10, 9,11,10, 4,
	 6, 6, 8, 8, 9, 9, 9, 9,10,10,11,10,12,10, 4, 6,
	 6, 8, 8, 9,10, 9, 9,10,10,11,11,12,12, 7, 8, 8,
	10,10,11,11,10,10,11,11,12,12,13,12, 7, 8, 8,10,
	10,11,11,10,10,11,11,12,12,12,13, 8,10, 9,11,11,
	12,12,11,11,12,12,13,13,14,13, 8, 9, 9,11,11,12,
	12,11,12,12,12,13,13,14,13, 8, 9, 9,10,10,12,11,
	13,12,13,13,14,13,15,14, 8, 9, 9,10,10,11,12,12,
	12,13,13,13,14,14,14, 9,10,10,12,11,13,12,13,13,
	14,13,14,14,14,15, 9,10,10,11,12,12,12,13,13,14,
	14,14,15,15,15,10,11,11,12,12,13,13,14,14,14,14,
	15,14,16,15,10,11,11,12,12,13,13,13,14,14,14,14,
	14,15,16,11,12,12,13,13,14,13,14,14,15,14,15,16,
	16,16,11,12,12,13,13,14,13,14,14,15,15,15,16,15,
	15,
};

static float _vq_quantthresh__44u9_p8_0[] = {
	-136.5, -115.5, -94.5, -73.5, -52.5, -31.5, -10.5, 10.5,
	31.5, 52.5, 73.5, 94.5, 115.5, 136.5,
};

static long _vq_quantmap__44u9_p8_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44u9_p8_0 = {
	_vq_quantthresh__44u9_p8_0,
	_vq_quantmap__44u9_p8_0,
	15,
	15
};

static static_codebook _44u9_p8_0 = {
	2, 225,
	_vq_lengthlist__44u9_p8_0,
	1, -520986624, 1620377600, 4, 0,
	_vq_quantlist__44u9_p8_0,
	NULL,
	&_vq_auxt__44u9_p8_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p8_1[] = {
	10,
	9,
	11,
	8,
	12,
	7,
	13,
	6,
	14,
	5,
	15,
	4,
	16,
	3,
	17,
	2,
	18,
	1,
	19,
	0,
	20,
};

static long _vq_lengthlist__44u9_p8_1[] = {
	 4, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 6, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 6, 6, 7, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7,
	 7, 7, 8, 8, 8, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9, 9,10, 9,10,10,10, 8, 8,
	 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9,10,10, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	10, 9,10, 9,10,10,10,10, 8, 8, 8, 9, 9, 9, 9, 9,
	 9, 9, 9, 9, 9,10,10, 9,10,10,10,10,10, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9, 9,10, 9,10,10,10,10,10,10,
	10,10, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,
	10,10,10,10,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	 9, 9,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9,
	 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,
	10, 9, 9, 9, 9, 9, 9, 9,10, 9,10,10,10,10,10,10,
	10,10,10,10,10,10, 9, 9, 9, 9, 9, 9, 9, 9,10,10,
	10,10,10,10,10,10,10,10,10,10,10, 9, 9, 9, 9, 9,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	 9, 9, 9, 9,10, 9, 9,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10, 9, 9, 9,10, 9,10, 9,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10, 9, 9, 9,10, 9,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9,
	 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10, 9, 9, 9,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,
};

static float _vq_quantthresh__44u9_p8_1[] = {
	-9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5,
	-1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5,
	6.5, 7.5, 8.5, 9.5,
};

static long _vq_quantmap__44u9_p8_1[] = {
	   19,   17,   15,   13,   11,	9,	7,	5,
		3,	1,	0,	2,	4,	6,	8,   10,
	   12,   14,   16,   18,   20,
};

static encode_aux_threshmatch _vq_auxt__44u9_p8_1 = {
	_vq_quantthresh__44u9_p8_1,
	_vq_quantmap__44u9_p8_1,
	21,
	21
};

static static_codebook _44u9_p8_1 = {
	2, 441,
	_vq_lengthlist__44u9_p8_1,
	1, -529268736, 1611661312, 5, 0,
	_vq_quantlist__44u9_p8_1,
	NULL,
	&_vq_auxt__44u9_p8_1,
	NULL,
	0
};

static long _vq_quantlist__44u9_p9_0[] = {
	7,
	6,
	8,
	5,
	9,
	4,
	10,
	3,
	11,
	2,
	12,
	1,
	13,
	0,
	14,
};

static long _vq_lengthlist__44u9_p9_0[] = {
	 1, 3, 3,11,11,11,11,11,11,11,11,11,11,11,11, 4,
	10,11,11,11,11,11,11,11,11,11,11,11,11,11, 4,10,
	10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__44u9_p9_0[] = {
	-6051.5, -5120.5, -4189.5, -3258.5, -2327.5, -1396.5, -465.5, 465.5,
	1396.5, 2327.5, 3258.5, 4189.5, 5120.5, 6051.5,
};

static long _vq_quantmap__44u9_p9_0[] = {
	   13,   11,	9,	7,	5,	3,	1,	0,
		2,	4,	6,	8,   10,   12,   14,
};

static encode_aux_threshmatch _vq_auxt__44u9_p9_0 = {
	_vq_quantthresh__44u9_p9_0,
	_vq_quantmap__44u9_p9_0,
	15,
	15
};

static static_codebook _44u9_p9_0 = {
	2, 225,
	_vq_lengthlist__44u9_p9_0,
	1, -510036736, 1631393792, 4, 0,
	_vq_quantlist__44u9_p9_0,
	NULL,
	&_vq_auxt__44u9_p9_0,
	NULL,
	0
};

static long _vq_quantlist__44u9_p9_1[] = {
	9,
	8,
	10,
	7,
	11,
	6,
	12,
	5,
	13,
	4,
	14,
	3,
	15,
	2,
	16,
	1,
	17,
	0,
	18,
};

static long _vq_lengthlist__44u9_p9_1[] = {
	 1, 4, 4, 7, 7, 8, 7, 8, 7, 9, 8,10, 9,10,10,11,
	11,12,12, 4, 7, 6, 9, 9,10, 9, 9, 8,10,10,11,10,
	12,10,13,12,13,12, 4, 6, 6, 9, 9, 9, 9, 9, 9,10,
	10,11,11,11,12,12,12,12,12, 7, 9, 8,11,10,10,10,
	11,10,11,11,12,12,13,12,13,13,13,13, 7, 8, 9,10,
	10,11,11,10,10,11,11,11,12,13,13,13,13,14,14, 8,
	 9, 9,11,11,12,11,12,12,13,12,12,13,13,14,15,14,
	14,14, 8, 9, 9,10,11,11,11,12,12,13,12,13,13,14,
	14,14,15,14,16, 8, 9, 9,11,10,12,12,12,12,15,13,
	13,13,17,14,15,15,15,14, 8, 9, 9,10,11,11,12,13,
	12,13,13,13,14,15,14,14,14,16,15, 9,11,10,12,12,
	13,13,13,13,14,14,16,15,14,14,14,15,15,17, 9,10,
	10,11,11,13,13,13,14,14,13,15,14,15,14,15,16,15,
	16,10,11,11,12,12,13,14,15,14,15,14,14,15,17,16,
	15,15,17,17,10,12,11,13,12,14,14,13,14,15,15,15,
	15,16,17,17,15,17,16,11,12,12,14,13,15,14,15,16,
	17,15,17,15,17,15,15,16,17,15,11,11,12,14,14,14,
	14,14,15,15,16,15,17,17,17,16,17,16,15,12,12,13,
	14,14,14,15,14,15,15,16,16,17,16,17,15,17,17,16,
	12,14,12,14,14,15,15,15,14,14,16,16,16,15,16,16,
	15,17,15,12,13,13,14,15,14,15,17,15,17,16,17,17,
	17,16,17,16,17,17,12,13,13,14,16,15,15,15,16,15,
	17,17,15,17,15,17,16,16,17,
};

static float _vq_quantthresh__44u9_p9_1[] = {
	-416.5, -367.5, -318.5, -269.5, -220.5, -171.5, -122.5, -73.5,
	-24.5, 24.5, 73.5, 122.5, 171.5, 220.5, 269.5, 318.5,
	367.5, 416.5,
};

static long _vq_quantmap__44u9_p9_1[] = {
	   17,   15,   13,   11,	9,	7,	5,	3,
		1,	0,	2,	4,	6,	8,   10,   12,
	   14,   16,   18,
};

static encode_aux_threshmatch _vq_auxt__44u9_p9_1 = {
	_vq_quantthresh__44u9_p9_1,
	_vq_quantmap__44u9_p9_1,
	19,
	19
};

static static_codebook _44u9_p9_1 = {
	2, 361,
	_vq_lengthlist__44u9_p9_1,
	1, -518287360, 1622704128, 5, 0,
	_vq_quantlist__44u9_p9_1,
	NULL,
	&_vq_auxt__44u9_p9_1,
	NULL,
	0
};

static long _vq_quantlist__44u9_p9_2[] = {
	24,
	23,
	25,
	22,
	26,
	21,
	27,
	20,
	28,
	19,
	29,
	18,
	30,
	17,
	31,
	16,
	32,
	15,
	33,
	14,
	34,
	13,
	35,
	12,
	36,
	11,
	37,
	10,
	38,
	9,
	39,
	8,
	40,
	7,
	41,
	6,
	42,
	5,
	43,
	4,
	44,
	3,
	45,
	2,
	46,
	1,
	47,
	0,
	48,
};

static long _vq_lengthlist__44u9_p9_2[] = {
	 2, 4, 4, 5, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6,
	 6, 6, 6, 7, 6, 7, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
	 7,
};

static float _vq_quantthresh__44u9_p9_2[] = {
	-23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5,
	-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
	-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
	0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
	8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
	16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5,
};

static long _vq_quantmap__44u9_p9_2[] = {
	   47,   45,   43,   41,   39,   37,   35,   33,
	   31,   29,   27,   25,   23,   21,   19,   17,
	   15,   13,   11,	9,	7,	5,	3,	1,
		0,	2,	4,	6,	8,   10,   12,   14,
	   16,   18,   20,   22,   24,   26,   28,   30,
	   32,   34,   36,   38,   40,   42,   44,   46,
	   48,
};

static encode_aux_threshmatch _vq_auxt__44u9_p9_2 = {
	_vq_quantthresh__44u9_p9_2,
	_vq_quantmap__44u9_p9_2,
	49,
	49
};

static static_codebook _44u9_p9_2 = {
	1, 49,
	_vq_lengthlist__44u9_p9_2,
	1, -526909440, 1611661312, 6, 0,
	_vq_quantlist__44u9_p9_2,
	NULL,
	&_vq_auxt__44u9_p9_2,
	NULL,
	0
};

static long _huff_lengthlist__44un1__long[] = {
	 5, 6,12, 9,14, 9, 9,19, 6, 1, 5, 5, 8, 7, 9,19,
	12, 4, 4, 7, 7, 9,11,18, 9, 5, 6, 6, 8, 7, 8,17,
	14, 8, 7, 8, 8,10,12,18, 9, 6, 8, 6, 8, 6, 8,18,
	 9, 8,11, 8,11, 7, 5,15,16,18,18,18,17,15,11,18,
};

static static_codebook _huff_book__44un1__long = {
	2, 64,
	_huff_lengthlist__44un1__long,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};

static long _vq_quantlist__44un1__p1_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44un1__p1_0[] = {
	 1, 4, 4, 5, 8, 7, 5, 7, 8, 5, 8, 8, 8,10,11, 8,
	10,11, 5, 8, 8, 8,11,10, 8,11,10, 4, 9, 9, 8,11,
	11, 8,11,11, 8,12,11,10,12,14,11,13,13, 7,11,11,
	10,13,11,11,13,14, 4, 8, 9, 8,11,11, 8,11,12, 7,
	11,11,11,14,13,10,11,13, 8,11,12,11,13,13,10,14,
	12,
};

static float _vq_quantthresh__44un1__p1_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44un1__p1_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44un1__p1_0 = {
	_vq_quantthresh__44un1__p1_0,
	_vq_quantmap__44un1__p1_0,
	3,
	3
};

static static_codebook _44un1__p1_0 = {
	4, 81,
	_vq_lengthlist__44un1__p1_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44un1__p1_0,
	NULL,
	&_vq_auxt__44un1__p1_0,
	NULL,
	0
};

static long _vq_quantlist__44un1__p2_0[] = {
	1,
	0,
	2,
};

static long _vq_lengthlist__44un1__p2_0[] = {
	 2, 4, 4, 5, 6, 6, 5, 6, 6, 5, 7, 7, 7, 8, 8, 6,
	 7, 9, 5, 7, 7, 6, 8, 7, 7, 9, 8, 4, 7, 7, 7, 9,
	 8, 7, 8, 8, 7, 9, 8, 8, 8,10, 9,10,10, 6, 8, 8,
	 7,10, 8, 9,10,10, 5, 7, 7, 7, 8, 8, 7, 8, 9, 6,
	 8, 8, 9,10,10, 7, 8,10, 6, 8, 9, 9,10,10, 8,10,
	 8,
};

static float _vq_quantthresh__44un1__p2_0[] = {
	-0.5, 0.5,
};

static long _vq_quantmap__44un1__p2_0[] = {
		1,	0,	2,
};

static encode_aux_threshmatch _vq_auxt__44un1__p2_0 = {
	_vq_quantthresh__44un1__p2_0,
	_vq_quantmap__44un1__p2_0,
	3,
	3
};

static static_codebook _44un1__p2_0 = {
	4, 81,
	_vq_lengthlist__44un1__p2_0,
	1, -535822336, 1611661312, 2, 0,
	_vq_quantlist__44un1__p2_0,
	NULL,
	&_vq_auxt__44un1__p2_0,
	NULL,
	0
};

static long _vq_quantlist__44un1__p3_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44un1__p3_0[] = {
	 1, 5, 5, 8, 8, 5, 8, 7, 9, 9, 5, 7, 8, 9, 9, 9,
	10, 9,12,12, 9, 9,10,11,12, 6, 8, 8,10,10, 8,10,
	10,11,11, 8, 9,10,11,11,10,11,11,13,13,10,11,11,
	12,13, 6, 8, 8,10,10, 8,10, 9,11,11, 8,10,10,11,
	11,10,11,11,13,12,10,11,11,13,12, 9,11,11,15,13,
	10,12,11,15,13,10,11,11,15,14,12,14,13,16,15,12,
	13,13,17,16, 9,11,11,13,15,10,11,12,14,15,10,11,
	12,14,15,12,13,13,15,16,12,13,13,16,16, 5, 8, 8,
	11,11, 8,10,10,12,12, 8,10,10,12,12,11,12,12,14,
	14,11,12,12,14,14, 8,11,10,13,12,10,11,12,12,13,
	10,12,12,13,13,12,12,13,13,15,11,12,13,15,14, 7,
	10,10,12,12, 9,12,11,13,12,10,12,12,13,14,12,13,
	12,15,13,11,13,12,14,15,10,12,12,16,14,11,12,12,
	16,15,11,13,12,17,16,13,13,15,15,17,13,15,15,20,
	17,10,12,12,14,16,11,12,12,15,15,11,13,13,15,18,
	13,14,13,15,15,13,15,14,16,16, 5, 8, 8,11,11, 8,
	10,10,12,12, 8,10,10,12,12,11,12,12,14,14,11,12,
	12,14,15, 7,10,10,13,12,10,12,12,14,13, 9,10,12,
	12,13,11,13,13,15,15,11,12,13,13,15, 8,10,10,12,
	13,10,12,12,13,13,10,12,11,13,13,11,13,12,15,15,
	12,13,12,15,13,10,12,12,16,14,11,12,12,16,15,10,
	12,12,16,14,14,15,14,18,16,13,13,14,15,16,10,12,
	12,14,16,11,13,13,16,16,11,13,12,14,16,13,15,15,
	18,18,13,15,13,16,14, 8,11,11,16,16,10,13,13,17,
	16,10,12,12,16,15,14,16,15,20,17,13,14,14,17,17,
	 9,12,12,16,16,11,13,14,16,17,11,13,13,16,16,15,
	15,19,18, 0,14,15,15,18,18, 9,12,12,17,16,11,13,
	12,17,16,11,12,13,15,17,15,16,15, 0,19,14,15,14,
	19,18,12,14,14, 0,16,13,14,14,19,18,13,15,16,17,
	16,15,15,17,18, 0,14,16,16,19, 0,12,14,14,16,18,
	13,15,13,17,18,13,15,14,17,18,15,18,14,18,18,16,
	17,16, 0,17, 8,11,11,15,15,10,12,12,16,16,10,13,
	13,16,16,13,15,14,17,17,14,15,17,17,18, 9,12,12,
	16,15,11,13,13,16,16,11,12,13,17,17,14,14,15,17,
	17,14,15,16, 0,18, 9,12,12,16,17,11,13,13,16,17,
	11,14,13,18,17,14,16,14,17,17,15,17,17,18,18,12,
	14,14, 0,16,13,15,15,19, 0,12,13,15, 0, 0,14,17,
	16,19, 0,16,15,18,18, 0,12,14,14,17, 0,13,14,14,
	17, 0,13,15,14, 0,18,15,16,16, 0,18,15,18,15, 0,
	17,
};

static float _vq_quantthresh__44un1__p3_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44un1__p3_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44un1__p3_0 = {
	_vq_quantthresh__44un1__p3_0,
	_vq_quantmap__44un1__p3_0,
	5,
	5
};

static static_codebook _44un1__p3_0 = {
	4, 625,
	_vq_lengthlist__44un1__p3_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44un1__p3_0,
	NULL,
	&_vq_auxt__44un1__p3_0,
	NULL,
	0
};

static long _vq_quantlist__44un1__p4_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44un1__p4_0[] = {
	 3, 5, 5, 9, 9, 5, 6, 6,10, 9, 5, 6, 6, 9,10,10,
	10,10,12,11, 9,10,10,12,12, 5, 7, 7,10,10, 7, 7,
	 8,10,11, 7, 7, 8,10,11,10,10,11,11,13,10,10,11,
	11,13, 6, 7, 7,10,10, 7, 8, 7,11,10, 7, 8, 7,10,
	10,10,11, 9,13,11,10,11,10,13,11,10,10,10,14,13,
	10,11,11,14,13,10,10,11,13,14,12,12,13,15,15,12,
	12,13,13,14,10,10,10,12,13,10,11,10,13,13,10,11,
	11,13,13,12,13,12,14,13,12,13,13,14,13, 5, 7, 7,
	10,10, 7, 8, 8,11,10, 7, 8, 8,10,10,11,11,11,13,
	13,10,11,11,12,12, 7, 8, 8,11,11, 7, 8, 9,10,12,
	 8, 9, 9,11,11,11,10,12,11,14,11,11,12,13,13, 6,
	 8, 8,10,11, 7, 9, 7,12,10, 8, 9,10,11,12,10,12,
	10,14,11,11,12,11,13,13,10,11,11,14,14,10,10,11,
	13,14,11,12,12,15,13,12,11,14,12,16,12,13,14,15,
	16,10,10,11,13,14,10,11,10,14,12,11,12,12,13,14,
	12,13,11,15,12,14,14,14,15,15, 5, 7, 7,10,10, 7,
	 8, 8,10,10, 7, 8, 8,10,11,10,11,10,12,12,10,11,
	11,12,13, 6, 8, 8,11,11, 8, 9, 9,12,11, 7, 7, 9,
	10,12,11,11,11,12,13,11,10,12,11,15, 7, 8, 8,11,
	11, 8, 9, 9,11,11, 7, 9, 8,12,10,11,12,11,13,12,
	11,12,10,15,11,10,11,10,14,12,11,12,11,14,13,10,
	10,11,13,14,13,13,13,17,15,12,11,14,12,15,10,10,
	11,13,14,11,12,12,14,14,10,11,10,14,13,13,14,13,
	16,17,12,14,11,16,12, 9,10,10,14,13,10,11,10,14,
	14,10,11,11,13,13,13,14,14,16,15,12,13,13,14,14,
	 9,11,10,14,13,10,10,12,13,14,11,12,11,14,13,13,
	14,14,14,15,13,14,14,15,15, 9,10,11,13,14,10,11,
	10,15,13,11,11,12,12,15,13,14,12,15,14,13,13,14,
	14,15,12,13,12,16,14,11,11,12,15,14,13,15,13,16,
	14,13,12,15,12,17,15,16,15,16,16,12,12,13,13,15,
	11,13,11,15,14,13,13,14,15,17,13,14,12, 0,13,14,
	15,14,15, 0, 9,10,10,13,13,10,11,11,13,13,10,11,
	11,13,13,12,13,12,14,14,13,14,14,15,17, 9,10,10,
	13,13,11,12,11,15,12,10,10,11,13,16,13,14,13,15,
	14,13,13,14,15,16,10,10,11,13,14,11,11,12,13,14,
	10,12,11,14,14,13,13,13,14,15,13,15,13,16,15,12,
	13,12,15,13,12,15,13,15,15,11,11,13,14,15,15,15,
	15,15,17,13,12,14,13,17,12,12,14,14,15,13,13,14,
	14,16,11,13,11,16,15,14,16,16,17, 0,14,13,11,16,
	12,
};

static float _vq_quantthresh__44un1__p4_0[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44un1__p4_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44un1__p4_0 = {
	_vq_quantthresh__44un1__p4_0,
	_vq_quantmap__44un1__p4_0,
	5,
	5
};

static static_codebook _44un1__p4_0 = {
	4, 625,
	_vq_lengthlist__44un1__p4_0,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44un1__p4_0,
	NULL,
	&_vq_auxt__44un1__p4_0,
	NULL,
	0
};

static long _vq_quantlist__44un1__p5_0[] = {
	4,
	3,
	5,
	2,
	6,
	1,
	7,
	0,
	8,
};

static long _vq_lengthlist__44un1__p5_0[] = {
	 1, 4, 4, 7, 7, 8, 8, 9, 9, 4, 6, 5, 8, 7, 8, 8,
	10, 9, 4, 6, 6, 8, 8, 8, 8,10,10, 7, 8, 7, 9, 9,
	 9, 9,11,10, 7, 8, 8, 9, 9, 9, 9,10,11, 8, 8, 8,
	 9, 9,10,10,11,11, 8, 8, 8, 9, 9,10,10,11,11, 9,
	10,10,11,10,11,11,12,12, 9,10,10,10,11,11,11,12,
	12,
};

static float _vq_quantthresh__44un1__p5_0[] = {
	-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5,
};

static long _vq_quantmap__44un1__p5_0[] = {
		7,	5,	3,	1,	0,	2,	4,	6,
		8,
};

static encode_aux_threshmatch _vq_auxt__44un1__p5_0 = {
	_vq_quantthresh__44un1__p5_0,
	_vq_quantmap__44un1__p5_0,
	9,
	9
};

static static_codebook _44un1__p5_0 = {
	2, 81,
	_vq_lengthlist__44un1__p5_0,
	1, -531628032, 1611661312, 4, 0,
	_vq_quantlist__44un1__p5_0,
	NULL,
	&_vq_auxt__44un1__p5_0,
	NULL,
	0
};

static long _vq_quantlist__44un1__p6_0[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44un1__p6_0[] = {
	 1, 4, 4, 6, 6, 8, 8,10,10,11,11,15,15, 4, 5, 5,
	 8, 8, 9, 9,11,11,12,12,16,16, 4, 5, 6, 8, 8, 9,
	 9,11,11,12,12,14,14, 7, 8, 8, 9, 9,10,10,11,12,
	13,13,16,17, 7, 8, 8, 9, 9,10,10,12,12,12,13,15,
	15, 9,10,10,10,10,11,11,12,12,13,13,15,16, 9, 9,
	 9,10,10,11,11,13,12,13,13,17,17,10,11,11,11,12,
	12,12,13,13,14,15, 0,18,10,11,11,12,12,12,13,14,
	13,14,14,17,16,11,12,12,13,13,14,14,14,14,15,16,
	17,16,11,12,12,13,13,14,14,14,14,15,15,17,17,14,
	15,15,16,16,16,17,17,16, 0,17, 0,18,14,15,15,16,
	16, 0,15,18,18, 0,16, 0, 0,
};

static float _vq_quantthresh__44un1__p6_0[] = {
	-27.5, -22.5, -17.5, -12.5, -7.5, -2.5, 2.5, 7.5,
	12.5, 17.5, 22.5, 27.5,
};

static long _vq_quantmap__44un1__p6_0[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44un1__p6_0 = {
	_vq_quantthresh__44un1__p6_0,
	_vq_quantmap__44un1__p6_0,
	13,
	13
};

static static_codebook _44un1__p6_0 = {
	2, 169,
	_vq_lengthlist__44un1__p6_0,
	1, -526516224, 1616117760, 4, 0,
	_vq_quantlist__44un1__p6_0,
	NULL,
	&_vq_auxt__44un1__p6_0,
	NULL,
	0
};

static long _vq_quantlist__44un1__p6_1[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44un1__p6_1[] = {
	 2, 4, 4, 5, 5, 4, 5, 5, 5, 5, 4, 5, 5, 6, 5, 5,
	 6, 5, 6, 6, 5, 6, 6, 6, 6,
};

static float _vq_quantthresh__44un1__p6_1[] = {
	-1.5, -0.5, 0.5, 1.5,
};

static long _vq_quantmap__44un1__p6_1[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44un1__p6_1 = {
	_vq_quantthresh__44un1__p6_1,
	_vq_quantmap__44un1__p6_1,
	5,
	5
};

static static_codebook _44un1__p6_1 = {
	2, 25,
	_vq_lengthlist__44un1__p6_1,
	1, -533725184, 1611661312, 3, 0,
	_vq_quantlist__44un1__p6_1,
	NULL,
	&_vq_auxt__44un1__p6_1,
	NULL,
	0
};

static long _vq_quantlist__44un1__p7_0[] = {
	2,
	1,
	3,
	0,
	4,
};

static long _vq_lengthlist__44un1__p7_0[] = {
	 1, 5, 3,11,11,11,11,11,11,11, 8,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,10,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,10,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11, 8,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,10,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11, 7,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,10,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
	10,
};

static float _vq_quantthresh__44un1__p7_0[] = {
	-253.5, -84.5, 84.5, 253.5,
};

static long _vq_quantmap__44un1__p7_0[] = {
		3,	1,	0,	2,	4,
};

static encode_aux_threshmatch _vq_auxt__44un1__p7_0 = {
	_vq_quantthresh__44un1__p7_0,
	_vq_quantmap__44un1__p7_0,
	5,
	5
};

static static_codebook _44un1__p7_0 = {
	4, 625,
	_vq_lengthlist__44un1__p7_0,
	1, -518709248, 1626677248, 3, 0,
	_vq_quantlist__44un1__p7_0,
	NULL,
	&_vq_auxt__44un1__p7_0,
	NULL,
	0
};

static long _vq_quantlist__44un1__p7_1[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44un1__p7_1[] = {
	 1, 4, 4, 6, 6, 6, 6, 9, 8, 9, 8, 8, 8, 5, 7, 7,
	 7, 7, 8, 8, 8,10, 8,10, 8, 9, 5, 7, 7, 8, 7, 7,
	 8,10,10,11,10,12,11, 7, 8, 8, 9, 9, 9,10,11,11,
	11,11,11,11, 7, 8, 8, 8, 9, 9, 9,10,10,10,11,11,
	12, 7, 8, 8, 9, 9,10,11,11,12,11,12,11,11, 7, 8,
	 8, 9, 9,10,10,11,11,11,12,12,11, 8,10,10,10,10,
	11,11,14,11,12,12,12,13, 9,10,10,10,10,12,11,14,
	11,14,11,12,13,10,11,11,11,11,13,11,14,14,13,13,
	13,14,11,11,11,12,11,12,12,12,13,14,14,13,14,12,
	11,12,12,12,12,13,13,13,14,13,14,14,11,12,12,14,
	12,13,13,12,13,13,14,14,14,
};

static float _vq_quantthresh__44un1__p7_1[] = {
	-71.5, -58.5, -45.5, -32.5, -19.5, -6.5, 6.5, 19.5,
	32.5, 45.5, 58.5, 71.5,
};

static long _vq_quantmap__44un1__p7_1[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44un1__p7_1 = {
	_vq_quantthresh__44un1__p7_1,
	_vq_quantmap__44un1__p7_1,
	13,
	13
};

static static_codebook _44un1__p7_1 = {
	2, 169,
	_vq_lengthlist__44un1__p7_1,
	1, -523010048, 1618608128, 4, 0,
	_vq_quantlist__44un1__p7_1,
	NULL,
	&_vq_auxt__44un1__p7_1,
	NULL,
	0
};

static long _vq_quantlist__44un1__p7_2[] = {
	6,
	5,
	7,
	4,
	8,
	3,
	9,
	2,
	10,
	1,
	11,
	0,
	12,
};

static long _vq_lengthlist__44un1__p7_2[] = {
	 3, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 9, 8, 4, 5, 5,
	 6, 6, 8, 8, 9, 8, 9, 9, 9, 9, 4, 5, 5, 7, 6, 8,
	 8, 8, 8, 9, 8, 9, 8, 6, 7, 7, 7, 8, 8, 8, 9, 9,
	 9, 9, 9, 9, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9, 9,
	 9, 7, 8, 8, 8, 8, 9, 8, 9, 9,10, 9, 9,10, 7, 8,
	 8, 8, 8, 9, 9, 9, 9, 9, 9,10,10, 8, 9, 9, 9, 9,
	 9, 9, 9, 9,10,10, 9,10, 8, 9, 9, 9, 9, 9, 9, 9,
	 9, 9, 9,10,10, 9, 9, 9,10, 9, 9,10, 9, 9,10,10,
	10,10, 9, 9, 9, 9, 9, 9, 9,10, 9,10,10,10,10, 9,
	 9, 9,10, 9, 9,10,10, 9,10,10,10,10, 9, 9, 9,10,
	 9, 9, 9,10,10,10,10,10,10,
};

static float _vq_quantthresh__44un1__p7_2[] = {
	-5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5,
	2.5, 3.5, 4.5, 5.5,
};

static long _vq_quantmap__44un1__p7_2[] = {
	   11,	9,	7,	5,	3,	1,	0,	2,
		4,	6,	8,   10,   12,
};

static encode_aux_threshmatch _vq_auxt__44un1__p7_2 = {
	_vq_quantthresh__44un1__p7_2,
	_vq_quantmap__44un1__p7_2,
	13,
	13
};

static static_codebook _44un1__p7_2 = {
	2, 169,
	_vq_lengthlist__44un1__p7_2,
	1, -531103744, 1611661312, 4, 0,
	_vq_quantlist__44un1__p7_2,
	NULL,
	&_vq_auxt__44un1__p7_2,
	NULL,
	0
};

static long _huff_lengthlist__44un1__short[] = {
	12,12,14,12,14,14,14,14,12, 6, 6, 8, 9, 9,11,14,
	12, 4, 2, 6, 6, 7,11,14,13, 6, 5, 7, 8, 9,11,14,
	13, 8, 5, 8, 6, 8,12,14,12, 7, 7, 8, 8, 8,10,14,
	12, 6, 3, 4, 4, 4, 7,14,11, 7, 4, 6, 6, 6, 8,14,
};

static static_codebook _huff_book__44un1__short = {
	2, 64,
	_huff_lengthlist__44un1__short,
	0, 0, 0, 0, 0,
	NULL,
	NULL,
	NULL,
	NULL,
	0
};
/*** End of inlined file: res_books_uncoupled.h ***/

static vorbis_info_residue0 _residue_44_low_un={
  0,-1, -1, 8,-1,
  {0},
  {-1},
  {  .5,  1.5,  1.5,  2.5,  2.5,  4.5, 28.5},
  {  -1,   25,   -1,   45,   -1,   -1,   -1}
};

static vorbis_info_residue0 _residue_44_mid_un={
  0,-1, -1, 10,-1,
  {0},
  {-1},
  {  .5,  1.5,  1.5,  2.5,  2.5,  4.5,  4.5, 16.5, 60.5},
  {  -1,   30,   -1,   50,   -1,   80,   -1,   -1,   -1}
};

static vorbis_info_residue0 _residue_44_hi_un={
  0,-1, -1, 10,-1,
  {0},
  {-1},
  {  .5,  1.5,  2.5,  4.5,  8.5, 16.5, 32.5, 71.5,157.5},
  {  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1}
};

static vorbis_info_mapping0 _map_nominal_u[2]={
  {1, {0,0}, {0}, {0}, 0,{0},{0}},
  {1, {0,0}, {1}, {1}, 0,{0},{0}}
};

static static_bookblock _resbook_44u_n1={
  {
	{0},
	{0,0,&_44un1__p1_0},
	{0,0,&_44un1__p2_0},
	{0,0,&_44un1__p3_0},
	{0,0,&_44un1__p4_0},
	{0,0,&_44un1__p5_0},
	{&_44un1__p6_0,&_44un1__p6_1},
	{&_44un1__p7_0,&_44un1__p7_1,&_44un1__p7_2}
   }
};
static static_bookblock _resbook_44u_0={
  {
	{0},
	{0,0,&_44u0__p1_0},
	{0,0,&_44u0__p2_0},
	{0,0,&_44u0__p3_0},
	{0,0,&_44u0__p4_0},
	{0,0,&_44u0__p5_0},
	{&_44u0__p6_0,&_44u0__p6_1},
	{&_44u0__p7_0,&_44u0__p7_1,&_44u0__p7_2}
   }
};
static static_bookblock _resbook_44u_1={
  {
	{0},
	{0,0,&_44u1__p1_0},
	{0,0,&_44u1__p2_0},
	{0,0,&_44u1__p3_0},
	{0,0,&_44u1__p4_0},
	{0,0,&_44u1__p5_0},
	{&_44u1__p6_0,&_44u1__p6_1},
	{&_44u1__p7_0,&_44u1__p7_1,&_44u1__p7_2}
   }
};
static static_bookblock _resbook_44u_2={
  {
	{0},
	{0,0,&_44u2__p1_0},
	{0,0,&_44u2__p2_0},
	{0,0,&_44u2__p3_0},
	{0,0,&_44u2__p4_0},
	{0,0,&_44u2__p5_0},
	{&_44u2__p6_0,&_44u2__p6_1},
	{&_44u2__p7_0,&_44u2__p7_1,&_44u2__p7_2}
   }
};
static static_bookblock _resbook_44u_3={
  {
	{0},
	{0,0,&_44u3__p1_0},
	{0,0,&_44u3__p2_0},
	{0,0,&_44u3__p3_0},
	{0,0,&_44u3__p4_0},
	{0,0,&_44u3__p5_0},
	{&_44u3__p6_0,&_44u3__p6_1},
	{&_44u3__p7_0,&_44u3__p7_1,&_44u3__p7_2}
   }
};
static static_bookblock _resbook_44u_4={
  {
	{0},
	{0,0,&_44u4__p1_0},
	{0,0,&_44u4__p2_0},
	{0,0,&_44u4__p3_0},
	{0,0,&_44u4__p4_0},
	{0,0,&_44u4__p5_0},
	{&_44u4__p6_0,&_44u4__p6_1},
	{&_44u4__p7_0,&_44u4__p7_1,&_44u4__p7_2}
   }
};
static static_bookblock _resbook_44u_5={
  {
	{0},
	{0,0,&_44u5__p1_0},
	{0,0,&_44u5__p2_0},
	{0,0,&_44u5__p3_0},
	{0,0,&_44u5__p4_0},
	{0,0,&_44u5__p5_0},
	{0,0,&_44u5__p6_0},
	{&_44u5__p7_0,&_44u5__p7_1},
	{&_44u5__p8_0,&_44u5__p8_1},
	{&_44u5__p9_0,&_44u5__p9_1,&_44u5__p9_2}
   }
};
static static_bookblock _resbook_44u_6={
  {
	{0},
	{0,0,&_44u6__p1_0},
	{0,0,&_44u6__p2_0},
	{0,0,&_44u6__p3_0},
	{0,0,&_44u6__p4_0},
	{0,0,&_44u6__p5_0},
	{0,0,&_44u6__p6_0},
	{&_44u6__p7_0,&_44u6__p7_1},
	{&_44u6__p8_0,&_44u6__p8_1},
	{&_44u6__p9_0,&_44u6__p9_1,&_44u6__p9_2}
   }
};
static static_bookblock _resbook_44u_7={
  {
	{0},
	{0,0,&_44u7__p1_0},
	{0,0,&_44u7__p2_0},
	{0,0,&_44u7__p3_0},
	{0,0,&_44u7__p4_0},
	{0,0,&_44u7__p5_0},
	{0,0,&_44u7__p6_0},
	{&_44u7__p7_0,&_44u7__p7_1},
	{&_44u7__p8_0,&_44u7__p8_1},
	{&_44u7__p9_0,&_44u7__p9_1,&_44u7__p9_2}
   }
};
static static_bookblock _resbook_44u_8={
  {
	{0},
	{0,0,&_44u8_p1_0},
	{0,0,&_44u8_p2_0},
	{0,0,&_44u8_p3_0},
	{0,0,&_44u8_p4_0},
	{&_44u8_p5_0,&_44u8_p5_1},
	{&_44u8_p6_0,&_44u8_p6_1},
	{&_44u8_p7_0,&_44u8_p7_1},
	{&_44u8_p8_0,&_44u8_p8_1},
	{&_44u8_p9_0,&_44u8_p9_1,&_44u8_p9_2}
   }
};
static static_bookblock _resbook_44u_9={
  {
	{0},
	{0,0,&_44u9_p1_0},
	{0,0,&_44u9_p2_0},
	{0,0,&_44u9_p3_0},
	{0,0,&_44u9_p4_0},
	{&_44u9_p5_0,&_44u9_p5_1},
	{&_44u9_p6_0,&_44u9_p6_1},
	{&_44u9_p7_0,&_44u9_p7_1},
	{&_44u9_p8_0,&_44u9_p8_1},
	{&_44u9_p9_0,&_44u9_p9_1,&_44u9_p9_2}
   }
};

static vorbis_residue_template _res_44u_n1[]={
  {1,0,  &_residue_44_low_un,
   &_huff_book__44un1__short,&_huff_book__44un1__short,
   &_resbook_44u_n1,&_resbook_44u_n1},

  {1,0,  &_residue_44_low_un,
   &_huff_book__44un1__long,&_huff_book__44un1__long,
   &_resbook_44u_n1,&_resbook_44u_n1}
};
static vorbis_residue_template _res_44u_0[]={
  {1,0,  &_residue_44_low_un,
   &_huff_book__44u0__short,&_huff_book__44u0__short,
   &_resbook_44u_0,&_resbook_44u_0},

  {1,0,  &_residue_44_low_un,
   &_huff_book__44u0__long,&_huff_book__44u0__long,
   &_resbook_44u_0,&_resbook_44u_0}
};
static vorbis_residue_template _res_44u_1[]={
  {1,0,  &_residue_44_low_un,
   &_huff_book__44u1__short,&_huff_book__44u1__short,
   &_resbook_44u_1,&_resbook_44u_1},

  {1,0,  &_residue_44_low_un,
   &_huff_book__44u1__long,&_huff_book__44u1__long,
   &_resbook_44u_1,&_resbook_44u_1}
};
static vorbis_residue_template _res_44u_2[]={
  {1,0,  &_residue_44_low_un,
   &_huff_book__44u2__short,&_huff_book__44u2__short,
   &_resbook_44u_2,&_resbook_44u_2},

  {1,0,  &_residue_44_low_un,
   &_huff_book__44u2__long,&_huff_book__44u2__long,
   &_resbook_44u_2,&_resbook_44u_2}
};
static vorbis_residue_template _res_44u_3[]={
  {1,0,  &_residue_44_low_un,
   &_huff_book__44u3__short,&_huff_book__44u3__short,
   &_resbook_44u_3,&_resbook_44u_3},

  {1,0,  &_residue_44_low_un,
   &_huff_book__44u3__long,&_huff_book__44u3__long,
   &_resbook_44u_3,&_resbook_44u_3}
};
static vorbis_residue_template _res_44u_4[]={
  {1,0,  &_residue_44_low_un,
   &_huff_book__44u4__short,&_huff_book__44u4__short,
   &_resbook_44u_4,&_resbook_44u_4},

  {1,0,  &_residue_44_low_un,
   &_huff_book__44u4__long,&_huff_book__44u4__long,
   &_resbook_44u_4,&_resbook_44u_4}
};

static vorbis_residue_template _res_44u_5[]={
  {1,0,  &_residue_44_mid_un,
   &_huff_book__44u5__short,&_huff_book__44u5__short,
   &_resbook_44u_5,&_resbook_44u_5},

  {1,0,  &_residue_44_mid_un,
   &_huff_book__44u5__long,&_huff_book__44u5__long,
   &_resbook_44u_5,&_resbook_44u_5}
};

static vorbis_residue_template _res_44u_6[]={
  {1,0,  &_residue_44_mid_un,
   &_huff_book__44u6__short,&_huff_book__44u6__short,
   &_resbook_44u_6,&_resbook_44u_6},

  {1,0,  &_residue_44_mid_un,
   &_huff_book__44u6__long,&_huff_book__44u6__long,
   &_resbook_44u_6,&_resbook_44u_6}
};

static vorbis_residue_template _res_44u_7[]={
  {1,0,  &_residue_44_mid_un,
   &_huff_book__44u7__short,&_huff_book__44u7__short,
   &_resbook_44u_7,&_resbook_44u_7},

  {1,0,  &_residue_44_mid_un,
   &_huff_book__44u7__long,&_huff_book__44u7__long,
   &_resbook_44u_7,&_resbook_44u_7}
};

static vorbis_residue_template _res_44u_8[]={
  {1,0,  &_residue_44_hi_un,
   &_huff_book__44u8__short,&_huff_book__44u8__short,
   &_resbook_44u_8,&_resbook_44u_8},

  {1,0,  &_residue_44_hi_un,
   &_huff_book__44u8__long,&_huff_book__44u8__long,
   &_resbook_44u_8,&_resbook_44u_8}
};
static vorbis_residue_template _res_44u_9[]={
  {1,0,  &_residue_44_hi_un,
   &_huff_book__44u9__short,&_huff_book__44u9__short,
   &_resbook_44u_9,&_resbook_44u_9},

  {1,0,  &_residue_44_hi_un,
   &_huff_book__44u9__long,&_huff_book__44u9__long,
   &_resbook_44u_9,&_resbook_44u_9}
};

static vorbis_mapping_template _mapres_template_44_uncoupled[]={
  { _map_nominal_u, _res_44u_n1 }, /* -1 */
  { _map_nominal_u, _res_44u_0 }, /* 0 */
  { _map_nominal_u, _res_44u_1 }, /* 1 */
  { _map_nominal_u, _res_44u_2 }, /* 2 */
  { _map_nominal_u, _res_44u_3 }, /* 3 */
  { _map_nominal_u, _res_44u_4 }, /* 4 */
  { _map_nominal_u, _res_44u_5 }, /* 5 */
  { _map_nominal_u, _res_44u_6 }, /* 6 */
  { _map_nominal_u, _res_44u_7 }, /* 7 */
  { _map_nominal_u, _res_44u_8 }, /* 8 */
  { _map_nominal_u, _res_44u_9 }, /* 9 */
};
/*** End of inlined file: residue_44u.h ***/

static double rate_mapping_44_un[12]={
  32000.,48000.,60000.,70000.,80000.,86000.,
  96000.,110000.,120000.,140000.,160000.,240001.
};

ve_setup_data_template ve_setup_44_uncoupled={
  11,
  rate_mapping_44_un,
  quality_mapping_44,
  -1,
  40000,
  50000,

  blocksize_short_44,
  blocksize_long_44,

  _psy_tone_masteratt_44,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_otherblock,
  _vp_tonemask_adj_longblock,
  _vp_tonemask_adj_otherblock,

  _psy_noiseguards_44,
  _psy_noisebias_impulse,
  _psy_noisebias_padding,
  _psy_noisebias_trans,
  _psy_noisebias_long,
  _psy_noise_suppress,

  _psy_compand_44,
  _psy_compand_short_mapping,
  _psy_compand_long_mapping,

  {_noise_start_short_44,_noise_start_long_44},
  {_noise_part_short_44,_noise_part_long_44},
  _noise_thresh_44,

  _psy_ath_floater,
  _psy_ath_abs,

  _psy_lowpass_44,

  _psy_global_44,
  _global_mapping_44,
  NULL,

  _floor_books,
  _floor,
  _floor_short_mapping_44,
  _floor_long_mapping_44,

  _mapres_template_44_uncoupled
};
/*** End of inlined file: setup_44u.h ***/


/*** Start of inlined file: setup_32.h ***/
static double rate_mapping_32[12]={
  18000.,28000.,35000.,45000.,56000.,60000.,
  75000.,90000.,100000.,115000.,150000.,190000.,
};

static double rate_mapping_32_un[12]={
  30000.,42000.,52000.,64000.,72000.,78000.,
  86000.,92000.,110000.,120000.,140000.,190000.,
};

static double _psy_lowpass_32[12]={
  12.3,13.,13.,14.,15.,99.,99.,99.,99.,99.,99.,99.
};

ve_setup_data_template ve_setup_32_stereo={
  11,
  rate_mapping_32,
  quality_mapping_44,
  2,
  26000,
  40000,

  blocksize_short_44,
  blocksize_long_44,

  _psy_tone_masteratt_44,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_otherblock,
  _vp_tonemask_adj_longblock,
  _vp_tonemask_adj_otherblock,

  _psy_noiseguards_44,
  _psy_noisebias_impulse,
  _psy_noisebias_padding,
  _psy_noisebias_trans,
  _psy_noisebias_long,
  _psy_noise_suppress,

  _psy_compand_44,
  _psy_compand_short_mapping,
  _psy_compand_long_mapping,

  {_noise_start_short_44,_noise_start_long_44},
  {_noise_part_short_44,_noise_part_long_44},
  _noise_thresh_44,

  _psy_ath_floater,
  _psy_ath_abs,

  _psy_lowpass_32,

  _psy_global_44,
  _global_mapping_44,
  _psy_stereo_modes_44,

  _floor_books,
  _floor,
  _floor_short_mapping_44,
  _floor_long_mapping_44,

  _mapres_template_44_stereo
};

ve_setup_data_template ve_setup_32_uncoupled={
  11,
  rate_mapping_32_un,
  quality_mapping_44,
  -1,
  26000,
  40000,

  blocksize_short_44,
  blocksize_long_44,

  _psy_tone_masteratt_44,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_otherblock,
  _vp_tonemask_adj_longblock,
  _vp_tonemask_adj_otherblock,

  _psy_noiseguards_44,
  _psy_noisebias_impulse,
  _psy_noisebias_padding,
  _psy_noisebias_trans,
  _psy_noisebias_long,
  _psy_noise_suppress,

  _psy_compand_44,
  _psy_compand_short_mapping,
  _psy_compand_long_mapping,

  {_noise_start_short_44,_noise_start_long_44},
  {_noise_part_short_44,_noise_part_long_44},
  _noise_thresh_44,

  _psy_ath_floater,
  _psy_ath_abs,

  _psy_lowpass_32,

  _psy_global_44,
  _global_mapping_44,
  NULL,

  _floor_books,
  _floor,
  _floor_short_mapping_44,
  _floor_long_mapping_44,

  _mapres_template_44_uncoupled
};
/*** End of inlined file: setup_32.h ***/


/*** Start of inlined file: setup_8.h ***/

/*** Start of inlined file: psych_8.h ***/
static att3 _psy_tone_masteratt_8[3]={
  {{ 32,  25,  12},  0,   0},  /* 0 */
  {{ 30,  25,  12},  0,   0},  /* 0 */
  {{ 20,   0, -14},  0,   0}, /* 0 */
};

static vp_adjblock _vp_tonemask_adj_8[3]={
  {{-15,-15,-15,-15,-10,-10, -6, 0, 0, 0, 0,10, 0, 0,99,99,99}}, /* 1 */
  {{-15,-15,-15,-15,-10,-10, -6, 0, 0, 0, 0,10, 0, 0,99,99,99}}, /* 1 */
  {{-15,-15,-15,-15,-10,-10, -6, 0, 0, 0, 0, 0, 0, 0,99,99,99}}, /* 1 */
};

static noise3 _psy_noisebias_8[3]={
  {{{-10,-10,-10,-10, -5, -5, -5,  0,  4,  8,  8,  8, 10, 10, 99, 99, 99},
	{-10,-10,-10,-10, -5, -5, -5,  0,  0,  4,  4,  4,  4,  4, 99, 99, 99},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, 99, 99, 99}}},

  {{{-10,-10,-10,-10, -5, -5, -5,  0,  4,  8,  8,  8, 10, 10, 99, 99, 99},
	{-10,-10,-10,-10,-10,-10, -5, -5, -5,  0,  0,  0,  0,  0, 99, 99, 99},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, 99, 99, 99}}},

  {{{-15,-15,-15,-15,-15,-12,-10, -8,  0,  2,  4,  4,  5,  5, 99, 99, 99},
	{-30,-30,-30,-30,-26,-22,-20,-14,-12,-12,-10,-10,-10,-10, 99, 99, 99},
	{-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26,-24, 99, 99, 99}}},
};

static adj_stereo _psy_stereo_modes_8[3]={
  {{  4,  4,  4,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3},
   {  6,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
   {  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  4,  4,  4,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3},
   {  6,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
   {  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3},
   {  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
   {  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
};

static noiseguard _psy_noiseguards_8[2]={
  {10,10,-1},
  {10,10,-1},
};

static compandblock _psy_compand_8[2]={
  {{
	 0, 1, 2, 3, 4, 5, 6,  7,	 /* 7dB */
	 8, 8, 9, 9,10,10,11, 11,	 /* 15dB */
	12,12,13,13,14,14,15, 15,	 /* 23dB */
	16,16,17,17,17,18,18, 19,	 /* 31dB */
	19,19,20,21,22,23,24, 25,	 /* 39dB */
  }},
  {{
	 0, 1, 2, 3, 4, 5, 6,  6,	 /* 7dB */
	 7, 7, 6, 6, 5, 5, 4,  4,	 /* 15dB */
	 3, 3, 3, 4, 5, 6, 7,  8,	 /* 23dB */
	 9,10,11,12,13,14,15, 16,	 /* 31dB */
	17,18,19,20,21,22,23, 24,	 /* 39dB */
  }},
};

static double _psy_lowpass_8[3]={3.,4.,4.};
static int _noise_start_8[2]={
  64,64,
};
static int _noise_part_8[2]={
  8,8,
};

static int _psy_ath_floater_8[3]={
  -100,-100,-105,
};

static int _psy_ath_abs_8[3]={
  -130,-130,-140,
};
/*** End of inlined file: psych_8.h ***/


/*** Start of inlined file: residue_8.h ***/
static static_bookblock _resbook_8s_0={
  {
	{0},{0,0,&_8c0_s_p1_0},{0,0,&_8c0_s_p2_0},{0,0,&_8c0_s_p3_0},
	{0,0,&_8c0_s_p4_0},{0,0,&_8c0_s_p5_0},{0,0,&_8c0_s_p6_0},
	{&_8c0_s_p7_0,&_8c0_s_p7_1},{&_8c0_s_p8_0,&_8c0_s_p8_1},
	{&_8c0_s_p9_0,&_8c0_s_p9_1,&_8c0_s_p9_2}
   }
};
static static_bookblock _resbook_8s_1={
  {
	{0},{0,0,&_8c1_s_p1_0},{0,0,&_8c1_s_p2_0},{0,0,&_8c1_s_p3_0},
	{0,0,&_8c1_s_p4_0},{0,0,&_8c1_s_p5_0},{0,0,&_8c1_s_p6_0},
	{&_8c1_s_p7_0,&_8c1_s_p7_1},{&_8c1_s_p8_0,&_8c1_s_p8_1},
	{&_8c1_s_p9_0,&_8c1_s_p9_1,&_8c1_s_p9_2}
   }
};

static vorbis_residue_template _res_8s_0[]={
  {2,0,  &_residue_44_mid,
   &_huff_book__8c0_s_single,&_huff_book__8c0_s_single,
   &_resbook_8s_0,&_resbook_8s_0},
};
static vorbis_residue_template _res_8s_1[]={
  {2,0,  &_residue_44_mid,
   &_huff_book__8c1_s_single,&_huff_book__8c1_s_single,
   &_resbook_8s_1,&_resbook_8s_1},
};

static vorbis_mapping_template _mapres_template_8_stereo[2]={
  { _map_nominal, _res_8s_0 }, /* 0 */
  { _map_nominal, _res_8s_1 }, /* 1 */
};

static static_bookblock _resbook_8u_0={
  {
	{0},
	{0,0,&_8u0__p1_0},
	{0,0,&_8u0__p2_0},
	{0,0,&_8u0__p3_0},
	{0,0,&_8u0__p4_0},
	{0,0,&_8u0__p5_0},
	{&_8u0__p6_0,&_8u0__p6_1},
	{&_8u0__p7_0,&_8u0__p7_1,&_8u0__p7_2}
   }
};
static static_bookblock _resbook_8u_1={
  {
	{0},
	{0,0,&_8u1__p1_0},
	{0,0,&_8u1__p2_0},
	{0,0,&_8u1__p3_0},
	{0,0,&_8u1__p4_0},
	{0,0,&_8u1__p5_0},
	{0,0,&_8u1__p6_0},
	{&_8u1__p7_0,&_8u1__p7_1},
	{&_8u1__p8_0,&_8u1__p8_1},
	{&_8u1__p9_0,&_8u1__p9_1,&_8u1__p9_2}
   }
};

static vorbis_residue_template _res_8u_0[]={
  {1,0,  &_residue_44_low_un,
   &_huff_book__8u0__single,&_huff_book__8u0__single,
   &_resbook_8u_0,&_resbook_8u_0},
};
static vorbis_residue_template _res_8u_1[]={
  {1,0,  &_residue_44_mid_un,
   &_huff_book__8u1__single,&_huff_book__8u1__single,
   &_resbook_8u_1,&_resbook_8u_1},
};

static vorbis_mapping_template _mapres_template_8_uncoupled[2]={
  { _map_nominal_u, _res_8u_0 }, /* 0 */
  { _map_nominal_u, _res_8u_1 }, /* 1 */
};
/*** End of inlined file: residue_8.h ***/

static int blocksize_8[2]={
  512,512
};

static int _floor_mapping_8[2]={
  6,6,
};

static double rate_mapping_8[3]={
  6000.,9000.,32000.,
};

static double rate_mapping_8_uncoupled[3]={
  8000.,14000.,42000.,
};

static double quality_mapping_8[3]={
  -.1,.0,1.
};

static double _psy_compand_8_mapping[3]={ 0., 1., 1.};

static double _global_mapping_8[3]={ 1., 2., 3. };

ve_setup_data_template ve_setup_8_stereo={
  2,
  rate_mapping_8,
  quality_mapping_8,
  2,
  8000,
  9000,

  blocksize_8,
  blocksize_8,

  _psy_tone_masteratt_8,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_8,
  NULL,
  _vp_tonemask_adj_8,

  _psy_noiseguards_8,
  _psy_noisebias_8,
  _psy_noisebias_8,
  NULL,
  NULL,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_8_mapping,
  NULL,

  {_noise_start_8,_noise_start_8},
  {_noise_part_8,_noise_part_8},
  _noise_thresh_5only,

  _psy_ath_floater_8,
  _psy_ath_abs_8,

  _psy_lowpass_8,

  _psy_global_44,
  _global_mapping_8,
  _psy_stereo_modes_8,

  _floor_books,
  _floor,
  _floor_mapping_8,
  NULL,

  _mapres_template_8_stereo
};

ve_setup_data_template ve_setup_8_uncoupled={
  2,
  rate_mapping_8_uncoupled,
  quality_mapping_8,
  -1,
  8000,
  9000,

  blocksize_8,
  blocksize_8,

  _psy_tone_masteratt_8,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_8,
  NULL,
  _vp_tonemask_adj_8,

  _psy_noiseguards_8,
  _psy_noisebias_8,
  _psy_noisebias_8,
  NULL,
  NULL,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_8_mapping,
  NULL,

  {_noise_start_8,_noise_start_8},
  {_noise_part_8,_noise_part_8},
  _noise_thresh_5only,

  _psy_ath_floater_8,
  _psy_ath_abs_8,

  _psy_lowpass_8,

  _psy_global_44,
  _global_mapping_8,
  _psy_stereo_modes_8,

  _floor_books,
  _floor,
  _floor_mapping_8,
  NULL,

  _mapres_template_8_uncoupled
};
/*** End of inlined file: setup_8.h ***/


/*** Start of inlined file: setup_11.h ***/

/*** Start of inlined file: psych_11.h ***/
static double _psy_lowpass_11[3]={4.5,5.5,30.,};

static att3 _psy_tone_masteratt_11[3]={
  {{ 30,  25,  12},  0,   0},  /* 0 */
  {{ 30,  25,  12},  0,   0},  /* 0 */
  {{ 20,   0, -14},  0,   0}, /* 0 */
};

static vp_adjblock _vp_tonemask_adj_11[3]={
  {{-20,-20,-20,-20,-20,-16,-10, 0, 0, 0, 0,10, 2, 0,99,99,99}}, /* 0 */
  {{-20,-20,-20,-20,-20,-16,-10, 0, 0, 0, 0, 5, 0, 0,99,99,99}}, /* 1 */
  {{-20,-20,-20,-20,-20,-16,-10, 0, 0, 0, 0, 0, 0, 0,99,99,99}}, /* 2 */
};

static noise3 _psy_noisebias_11[3]={
  {{{-10,-10,-10,-10, -5, -5, -5,  0,  4, 10, 10, 12, 12, 12, 99, 99, 99},
	{-15,-15,-15,-15,-10,-10, -5,  0,  0,  4,  4,  5,  5, 10, 99, 99, 99},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, 99, 99, 99}}},

  {{{-10,-10,-10,-10, -5, -5, -5,  0,  4, 10, 10, 12, 12, 12, 99, 99, 99},
	{-15,-15,-15,-15,-10,-10, -5, -5, -5,  0,  0,  0,  0,  0, 99, 99, 99},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, 99, 99, 99}}},

  {{{-15,-15,-15,-15,-15,-12,-10, -8,  0,  2,  4,  4,  5,  5, 99, 99, 99},
	{-30,-30,-30,-30,-26,-22,-20,-14,-12,-12,-10,-10,-10,-10, 99, 99, 99},
	{-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26,-24, 99, 99, 99}}},
};

static double _noise_thresh_11[3]={ .3,.5,.5 };
/*** End of inlined file: psych_11.h ***/

static int blocksize_11[2]={
  512,512
};

static int _floor_mapping_11[2]={
  6,6,
};

static double rate_mapping_11[3]={
  8000.,13000.,44000.,
};

static double rate_mapping_11_uncoupled[3]={
  12000.,20000.,50000.,
};

static double quality_mapping_11[3]={
  -.1,.0,1.
};

ve_setup_data_template ve_setup_11_stereo={
  2,
  rate_mapping_11,
  quality_mapping_11,
  2,
  9000,
  15000,

  blocksize_11,
  blocksize_11,

  _psy_tone_masteratt_11,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_11,
  NULL,
  _vp_tonemask_adj_11,

  _psy_noiseguards_8,
  _psy_noisebias_11,
  _psy_noisebias_11,
  NULL,
  NULL,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_8_mapping,
  NULL,

  {_noise_start_8,_noise_start_8},
  {_noise_part_8,_noise_part_8},
  _noise_thresh_11,

  _psy_ath_floater_8,
  _psy_ath_abs_8,

  _psy_lowpass_11,

  _psy_global_44,
  _global_mapping_8,
  _psy_stereo_modes_8,

  _floor_books,
  _floor,
  _floor_mapping_11,
  NULL,

  _mapres_template_8_stereo
};

ve_setup_data_template ve_setup_11_uncoupled={
  2,
  rate_mapping_11_uncoupled,
  quality_mapping_11,
  -1,
  9000,
  15000,

  blocksize_11,
  blocksize_11,

  _psy_tone_masteratt_11,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_11,
  NULL,
  _vp_tonemask_adj_11,

  _psy_noiseguards_8,
  _psy_noisebias_11,
  _psy_noisebias_11,
  NULL,
  NULL,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_8_mapping,
  NULL,

  {_noise_start_8,_noise_start_8},
  {_noise_part_8,_noise_part_8},
  _noise_thresh_11,

  _psy_ath_floater_8,
  _psy_ath_abs_8,

  _psy_lowpass_11,

  _psy_global_44,
  _global_mapping_8,
  _psy_stereo_modes_8,

  _floor_books,
  _floor,
  _floor_mapping_11,
  NULL,

  _mapres_template_8_uncoupled
};
/*** End of inlined file: setup_11.h ***/


/*** Start of inlined file: setup_16.h ***/

/*** Start of inlined file: psych_16.h ***/
static adj_stereo _psy_stereo_modes_16[4]={
  {{  4,  4,  4,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3},
   {  6,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
   {  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  4,  4},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  4,  4,  4,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3},
   {  6,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
   {  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  4,  4,  4,  4,  4},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3},
   {  5,  4,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3},
   {  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
  {{  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
   {  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8},
   { 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}},
};

static double _psy_lowpass_16[4]={6.5,8,30.,99.};

static att3 _psy_tone_masteratt_16[4]={
  {{ 30,  25,  12},  0,   0},  /* 0 */
  {{ 25,  22,  12},  0,   0},  /* 0 */
  {{ 20,  12,   0},  0,   0},  /* 0 */
  {{ 15,   0, -14},  0,   0}, /* 0 */
};

static vp_adjblock _vp_tonemask_adj_16[4]={
  {{-20,-20,-20,-20,-20,-16,-10,  0,  0, 0, 0,10, 0, 0, 0, 0, 0}}, /* 0 */
  {{-20,-20,-20,-20,-20,-16,-10,  0,  0, 0, 0,10, 0, 0, 0, 0, 0}}, /* 1 */
  {{-20,-20,-20,-20,-20,-16,-10,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 2 */
  {{-30,-30,-30,-30,-30,-26,-20,-10, -5, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 2 */
};

static noise3 _psy_noisebias_16_short[4]={
  {{{-15,-15,-15,-15,-15,-10,-10,-5,   4, 10, 10, 10, 10, 12, 12, 14, 20},
	{-15,-15,-15,-15,-15,-10,-10, -5,  0,  0,  4,  5,  5,  6,  8,  8, 15},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, -6, -6, -6}}},

  {{{-15,-15,-15,-15,-15,-10,-10,-5,   4,  6,  6,  6,  6,  8, 10, 12, 20},
	{-15,-15,-15,-15,-15,-15,-15,-10, -5, -5, -5,  4,  5,  6,  8,  8, 15},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10,-10,-10,-10,-10,-10,-10,-10,-10}}},

  {{{-15,-15,-15,-15,-15,-12,-10, -8,  0,  2,  4,  4,  5,  5,  5,  8, 12},
	{-20,-20,-20,-20,-16,-12,-20,-14,-10,-10, -8,  0,  0,  0,  0,  2,  5},
	{-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26,-24,-20,-20,-20}}},

  {{{-15,-15,-15,-15,-15,-12,-10, -8, -5, -5, -5, -5, -5,  0,  0,  0,  6},
	{-30,-30,-30,-30,-26,-22,-20,-14,-12,-12,-10,-10,-10,-10,-10,-10, -6},
	{-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26,-24,-20,-20,-20}}},
};

static noise3 _psy_noisebias_16_impulse[4]={
  {{{-15,-15,-15,-15,-15,-10,-10,-5,   4, 10, 10, 10, 10, 12, 12, 14, 20},
	{-15,-15,-15,-15,-15,-10,-10, -5,  0,  0,  4,  5,  5,  6,  8,  8, 15},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, -6, -6, -6}}},

  {{{-15,-15,-15,-15,-15,-10,-10,-5,   4,  4,  4,  4,  5,  5,  6,  8, 15},
	{-15,-15,-15,-15,-15,-15,-15,-10, -5, -5, -5,  0,  0,  0,  0,  4, 10},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10,-10,-10,-10,-10,-10,-10,-10,-10}}},

  {{{-15,-15,-15,-15,-15,-12,-10, -8,  0,  0,  0,  0,  0,  0,  0,  4, 10},
	{-20,-20,-20,-20,-16,-12,-20,-14,-10,-10,-10,-10,-10,-10,-10, -7, -5},
	{-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26,-24,-20,-20,-20}}},

  {{{-15,-15,-15,-15,-15,-12,-10, -8, -5, -5, -5, -5, -5,  0,  0,  0,  6},
	{-30,-30,-30,-30,-26,-22,-20,-18,-18,-18,-20,-20,-20,-20,-20,-20,-16},
	{-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26,-24,-20,-20,-20}}},
};

static noise3 _psy_noisebias_16[4]={
  {{{-10,-10,-10,-10, -5, -5, -5,  0,  4,  6,  8,  8, 10, 10, 10, 14, 20},
	{-10,-10,-10,-10,-10, -5, -2, -2,  0,  0,  0,  4,  5,  6,  8,  8, 15},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, -6, -6, -6}}},

  {{{-10,-10,-10,-10, -5, -5, -5,  0,  4,  6,  6,  6,  6,  8, 10, 12, 20},
	{-15,-15,-15,-15,-15,-10, -5, -5,  0,  0,  0,  4,  5,  6,  8,  8, 15},
	{-30,-30,-30,-30,-30,-24,-20,-14,-10, -6, -8, -8, -6, -6, -6, -6, -6}}},

  {{{-15,-15,-15,-15,-15,-12,-10, -8,  0,  2,  4,  4,  5,  5,  5,  8, 12},
	{-20,-20,-20,-20,-16,-12,-20,-10, -5, -5,  0,  0,  0,  0,  0,  2,  5},
	{-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26,-24,-20,-20,-20}}},

  {{{-15,-15,-15,-15,-15,-12,-10, -8, -5, -5, -5, -5, -5,  0,  0,  0,  6},
	{-30,-30,-30,-30,-26,-22,-20,-14,-12,-12,-10,-10,-10,-10,-10,-10, -6},
	{-30,-30,-30,-30,-26,-26,-26,-26,-26,-26,-26,-26,-26,-24,-20,-20,-20}}},
};

static double _noise_thresh_16[4]={ .3,.5,.5,.5 };

static int _noise_start_16[3]={ 256,256,9999 };
static int _noise_part_16[4]={ 8,8,8,8 };

static int _psy_ath_floater_16[4]={
  -100,-100,-100,-105,
};

static int _psy_ath_abs_16[4]={
  -130,-130,-130,-140,
};
/*** End of inlined file: psych_16.h ***/


/*** Start of inlined file: residue_16.h ***/
static static_bookblock _resbook_16s_0={
  {
	{0},
	{0,0,&_16c0_s_p1_0},
	{0,0,&_16c0_s_p2_0},
	{0,0,&_16c0_s_p3_0},
	{0,0,&_16c0_s_p4_0},
	{0,0,&_16c0_s_p5_0},
	{0,0,&_16c0_s_p6_0},
	{&_16c0_s_p7_0,&_16c0_s_p7_1},
	{&_16c0_s_p8_0,&_16c0_s_p8_1},
	{&_16c0_s_p9_0,&_16c0_s_p9_1,&_16c0_s_p9_2}
   }
};
static static_bookblock _resbook_16s_1={
  {
	{0},
	{0,0,&_16c1_s_p1_0},
	{0,0,&_16c1_s_p2_0},
	{0,0,&_16c1_s_p3_0},
	{0,0,&_16c1_s_p4_0},
	{0,0,&_16c1_s_p5_0},
	{0,0,&_16c1_s_p6_0},
	{&_16c1_s_p7_0,&_16c1_s_p7_1},
	{&_16c1_s_p8_0,&_16c1_s_p8_1},
	{&_16c1_s_p9_0,&_16c1_s_p9_1,&_16c1_s_p9_2}
   }
};
static static_bookblock _resbook_16s_2={
  {
	{0},
	{0,0,&_16c2_s_p1_0},
	{0,0,&_16c2_s_p2_0},
	{0,0,&_16c2_s_p3_0},
	{0,0,&_16c2_s_p4_0},
	{&_16c2_s_p5_0,&_16c2_s_p5_1},
	{&_16c2_s_p6_0,&_16c2_s_p6_1},
	{&_16c2_s_p7_0,&_16c2_s_p7_1},
	{&_16c2_s_p8_0,&_16c2_s_p8_1},
	{&_16c2_s_p9_0,&_16c2_s_p9_1,&_16c2_s_p9_2}
   }
};

static vorbis_residue_template _res_16s_0[]={
  {2,0,  &_residue_44_mid,
   &_huff_book__16c0_s_single,&_huff_book__16c0_s_single,
   &_resbook_16s_0,&_resbook_16s_0},
};
static vorbis_residue_template _res_16s_1[]={
  {2,0,  &_residue_44_mid,
   &_huff_book__16c1_s_short,&_huff_book__16c1_s_short,
   &_resbook_16s_1,&_resbook_16s_1},

  {2,0,  &_residue_44_mid,
   &_huff_book__16c1_s_long,&_huff_book__16c1_s_long,
   &_resbook_16s_1,&_resbook_16s_1}
};
static vorbis_residue_template _res_16s_2[]={
  {2,0,  &_residue_44_high,
   &_huff_book__16c2_s_short,&_huff_book__16c2_s_short,
   &_resbook_16s_2,&_resbook_16s_2},

  {2,0,  &_residue_44_high,
   &_huff_book__16c2_s_long,&_huff_book__16c2_s_long,
   &_resbook_16s_2,&_resbook_16s_2}
};

static vorbis_mapping_template _mapres_template_16_stereo[3]={
  { _map_nominal, _res_16s_0 }, /* 0 */
  { _map_nominal, _res_16s_1 }, /* 1 */
  { _map_nominal, _res_16s_2 }, /* 2 */
};

static static_bookblock _resbook_16u_0={
  {
	{0},
	{0,0,&_16u0__p1_0},
	{0,0,&_16u0__p2_0},
	{0,0,&_16u0__p3_0},
	{0,0,&_16u0__p4_0},
	{0,0,&_16u0__p5_0},
	{&_16u0__p6_0,&_16u0__p6_1},
	{&_16u0__p7_0,&_16u0__p7_1,&_16u0__p7_2}
   }
};
static static_bookblock _resbook_16u_1={
  {
	{0},
	{0,0,&_16u1__p1_0},
	{0,0,&_16u1__p2_0},
	{0,0,&_16u1__p3_0},
	{0,0,&_16u1__p4_0},
	{0,0,&_16u1__p5_0},
	{0,0,&_16u1__p6_0},
	{&_16u1__p7_0,&_16u1__p7_1},
	{&_16u1__p8_0,&_16u1__p8_1},
	{&_16u1__p9_0,&_16u1__p9_1,&_16u1__p9_2}
   }
};
static static_bookblock _resbook_16u_2={
  {
	{0},
	{0,0,&_16u2_p1_0},
	{0,0,&_16u2_p2_0},
	{0,0,&_16u2_p3_0},
	{0,0,&_16u2_p4_0},
	{&_16u2_p5_0,&_16u2_p5_1},
	{&_16u2_p6_0,&_16u2_p6_1},
	{&_16u2_p7_0,&_16u2_p7_1},
	{&_16u2_p8_0,&_16u2_p8_1},
	{&_16u2_p9_0,&_16u2_p9_1,&_16u2_p9_2}
   }
};

static vorbis_residue_template _res_16u_0[]={
  {1,0,  &_residue_44_low_un,
   &_huff_book__16u0__single,&_huff_book__16u0__single,
   &_resbook_16u_0,&_resbook_16u_0},
};
static vorbis_residue_template _res_16u_1[]={
  {1,0,  &_residue_44_mid_un,
   &_huff_book__16u1__short,&_huff_book__16u1__short,
   &_resbook_16u_1,&_resbook_16u_1},

  {1,0,  &_residue_44_mid_un,
   &_huff_book__16u1__long,&_huff_book__16u1__long,
   &_resbook_16u_1,&_resbook_16u_1}
};
static vorbis_residue_template _res_16u_2[]={
  {1,0,  &_residue_44_hi_un,
   &_huff_book__16u2__short,&_huff_book__16u2__short,
   &_resbook_16u_2,&_resbook_16u_2},

  {1,0,  &_residue_44_hi_un,
   &_huff_book__16u2__long,&_huff_book__16u2__long,
   &_resbook_16u_2,&_resbook_16u_2}
};

static vorbis_mapping_template _mapres_template_16_uncoupled[3]={
  { _map_nominal_u, _res_16u_0 }, /* 0 */
  { _map_nominal_u, _res_16u_1 }, /* 1 */
  { _map_nominal_u, _res_16u_2 }, /* 2 */
};
/*** End of inlined file: residue_16.h ***/

static int blocksize_16_short[3]={
  1024,512,512
};
static int blocksize_16_long[3]={
  1024,1024,1024
};

static int _floor_mapping_16_short[3]={
  9,3,3
};
static int _floor_mapping_16[3]={
  9,9,9
};

static double rate_mapping_16[4]={
  12000.,20000.,44000.,86000.
};

static double rate_mapping_16_uncoupled[4]={
  16000.,28000.,64000.,100000.
};

static double _global_mapping_16[4]={ 1., 2., 3., 4. };

static double quality_mapping_16[4]={ -.1,.05,.5,1. };

static double _psy_compand_16_mapping[4]={ 0., .8, 1., 1.};

ve_setup_data_template ve_setup_16_stereo={
  3,
  rate_mapping_16,
  quality_mapping_16,
  2,
  15000,
  19000,

  blocksize_16_short,
  blocksize_16_long,

  _psy_tone_masteratt_16,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_16,
  _vp_tonemask_adj_16,
  _vp_tonemask_adj_16,

  _psy_noiseguards_8,
  _psy_noisebias_16_impulse,
  _psy_noisebias_16_short,
  _psy_noisebias_16_short,
  _psy_noisebias_16,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_16_mapping,
  _psy_compand_16_mapping,

  {_noise_start_16,_noise_start_16},
  { _noise_part_16, _noise_part_16},
  _noise_thresh_16,

  _psy_ath_floater_16,
  _psy_ath_abs_16,

  _psy_lowpass_16,

  _psy_global_44,
  _global_mapping_16,
  _psy_stereo_modes_16,

  _floor_books,
  _floor,
  _floor_mapping_16_short,
  _floor_mapping_16,

  _mapres_template_16_stereo
};

ve_setup_data_template ve_setup_16_uncoupled={
  3,
  rate_mapping_16_uncoupled,
  quality_mapping_16,
  -1,
  15000,
  19000,

  blocksize_16_short,
  blocksize_16_long,

  _psy_tone_masteratt_16,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_16,
  _vp_tonemask_adj_16,
  _vp_tonemask_adj_16,

  _psy_noiseguards_8,
  _psy_noisebias_16_impulse,
  _psy_noisebias_16_short,
  _psy_noisebias_16_short,
  _psy_noisebias_16,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_16_mapping,
  _psy_compand_16_mapping,

  {_noise_start_16,_noise_start_16},
  { _noise_part_16, _noise_part_16},
  _noise_thresh_16,

  _psy_ath_floater_16,
  _psy_ath_abs_16,

  _psy_lowpass_16,

  _psy_global_44,
  _global_mapping_16,
  _psy_stereo_modes_16,

  _floor_books,
  _floor,
  _floor_mapping_16_short,
  _floor_mapping_16,

  _mapres_template_16_uncoupled
};
/*** End of inlined file: setup_16.h ***/


/*** Start of inlined file: setup_22.h ***/
static double rate_mapping_22[4]={
  15000.,20000.,44000.,86000.
};

static double rate_mapping_22_uncoupled[4]={
  16000.,28000.,50000.,90000.
};

static double _psy_lowpass_22[4]={9.5,11.,30.,99.};

ve_setup_data_template ve_setup_22_stereo={
  3,
  rate_mapping_22,
  quality_mapping_16,
  2,
  19000,
  26000,

  blocksize_16_short,
  blocksize_16_long,

  _psy_tone_masteratt_16,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_16,
  _vp_tonemask_adj_16,
  _vp_tonemask_adj_16,

  _psy_noiseguards_8,
  _psy_noisebias_16_impulse,
  _psy_noisebias_16_short,
  _psy_noisebias_16_short,
  _psy_noisebias_16,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_8_mapping,
  _psy_compand_8_mapping,

  {_noise_start_16,_noise_start_16},
  { _noise_part_16, _noise_part_16},
  _noise_thresh_16,

  _psy_ath_floater_16,
  _psy_ath_abs_16,

  _psy_lowpass_22,

  _psy_global_44,
  _global_mapping_16,
  _psy_stereo_modes_16,

  _floor_books,
  _floor,
  _floor_mapping_16_short,
  _floor_mapping_16,

  _mapres_template_16_stereo
};

ve_setup_data_template ve_setup_22_uncoupled={
  3,
  rate_mapping_22_uncoupled,
  quality_mapping_16,
  -1,
  19000,
  26000,

  blocksize_16_short,
  blocksize_16_long,

  _psy_tone_masteratt_16,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_16,
  _vp_tonemask_adj_16,
  _vp_tonemask_adj_16,

  _psy_noiseguards_8,
  _psy_noisebias_16_impulse,
  _psy_noisebias_16_short,
  _psy_noisebias_16_short,
  _psy_noisebias_16,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_8_mapping,
  _psy_compand_8_mapping,

  {_noise_start_16,_noise_start_16},
  { _noise_part_16, _noise_part_16},
  _noise_thresh_16,

  _psy_ath_floater_16,
  _psy_ath_abs_16,

  _psy_lowpass_22,

  _psy_global_44,
  _global_mapping_16,
  _psy_stereo_modes_16,

  _floor_books,
  _floor,
  _floor_mapping_16_short,
  _floor_mapping_16,

  _mapres_template_16_uncoupled
};
/*** End of inlined file: setup_22.h ***/


/*** Start of inlined file: setup_X.h ***/
static double rate_mapping_X[12]={
  -1.,-1.,-1.,-1.,-1.,-1.,
  -1.,-1.,-1.,-1.,-1.,-1.
};

ve_setup_data_template ve_setup_X_stereo={
  11,
  rate_mapping_X,
  quality_mapping_44,
  2,
  50000,
  200000,

  blocksize_short_44,
  blocksize_long_44,

  _psy_tone_masteratt_44,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_otherblock,
  _vp_tonemask_adj_longblock,
  _vp_tonemask_adj_otherblock,

  _psy_noiseguards_44,
  _psy_noisebias_impulse,
  _psy_noisebias_padding,
  _psy_noisebias_trans,
  _psy_noisebias_long,
  _psy_noise_suppress,

  _psy_compand_44,
  _psy_compand_short_mapping,
  _psy_compand_long_mapping,

  {_noise_start_short_44,_noise_start_long_44},
  {_noise_part_short_44,_noise_part_long_44},
  _noise_thresh_44,

  _psy_ath_floater,
  _psy_ath_abs,

  _psy_lowpass_44,

  _psy_global_44,
  _global_mapping_44,
  _psy_stereo_modes_44,

  _floor_books,
  _floor,
  _floor_short_mapping_44,
  _floor_long_mapping_44,

  _mapres_template_44_stereo
};

ve_setup_data_template ve_setup_X_uncoupled={
  11,
  rate_mapping_X,
  quality_mapping_44,
  -1,
  50000,
  200000,

  blocksize_short_44,
  blocksize_long_44,

  _psy_tone_masteratt_44,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_otherblock,
  _vp_tonemask_adj_longblock,
  _vp_tonemask_adj_otherblock,

  _psy_noiseguards_44,
  _psy_noisebias_impulse,
  _psy_noisebias_padding,
  _psy_noisebias_trans,
  _psy_noisebias_long,
  _psy_noise_suppress,

  _psy_compand_44,
  _psy_compand_short_mapping,
  _psy_compand_long_mapping,

  {_noise_start_short_44,_noise_start_long_44},
  {_noise_part_short_44,_noise_part_long_44},
  _noise_thresh_44,

  _psy_ath_floater,
  _psy_ath_abs,

  _psy_lowpass_44,

  _psy_global_44,
  _global_mapping_44,
  NULL,

  _floor_books,
  _floor,
  _floor_short_mapping_44,
  _floor_long_mapping_44,

  _mapres_template_44_uncoupled
};

ve_setup_data_template ve_setup_XX_stereo={
  2,
  rate_mapping_X,
  quality_mapping_8,
  2,
  0,
  8000,

  blocksize_8,
  blocksize_8,

  _psy_tone_masteratt_8,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_8,
  NULL,
  _vp_tonemask_adj_8,

  _psy_noiseguards_8,
  _psy_noisebias_8,
  _psy_noisebias_8,
  NULL,
  NULL,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_8_mapping,
  NULL,

  {_noise_start_8,_noise_start_8},
  {_noise_part_8,_noise_part_8},
  _noise_thresh_5only,

  _psy_ath_floater_8,
  _psy_ath_abs_8,

  _psy_lowpass_8,

  _psy_global_44,
  _global_mapping_8,
  _psy_stereo_modes_8,

  _floor_books,
  _floor,
  _floor_mapping_8,
  NULL,

  _mapres_template_8_stereo
};

ve_setup_data_template ve_setup_XX_uncoupled={
  2,
  rate_mapping_X,
  quality_mapping_8,
  -1,
  0,
  8000,

  blocksize_8,
  blocksize_8,

  _psy_tone_masteratt_8,
  _psy_tone_0dB,
  _psy_tone_suppress,

  _vp_tonemask_adj_8,
  NULL,
  _vp_tonemask_adj_8,

  _psy_noiseguards_8,
  _psy_noisebias_8,
  _psy_noisebias_8,
  NULL,
  NULL,
  _psy_noise_suppress,

  _psy_compand_8,
  _psy_compand_8_mapping,
  NULL,

  {_noise_start_8,_noise_start_8},
  {_noise_part_8,_noise_part_8},
  _noise_thresh_5only,

  _psy_ath_floater_8,
  _psy_ath_abs_8,

  _psy_lowpass_8,

  _psy_global_44,
  _global_mapping_8,
  _psy_stereo_modes_8,

  _floor_books,
  _floor,
  _floor_mapping_8,
  NULL,

  _mapres_template_8_uncoupled
};
/*** End of inlined file: setup_X.h ***/

static ve_setup_data_template *setup_list[]={
  &ve_setup_44_stereo,
  &ve_setup_44_uncoupled,

  &ve_setup_32_stereo,
  &ve_setup_32_uncoupled,

  &ve_setup_22_stereo,
  &ve_setup_22_uncoupled,
  &ve_setup_16_stereo,
  &ve_setup_16_uncoupled,

  &ve_setup_11_stereo,
  &ve_setup_11_uncoupled,
  &ve_setup_8_stereo,
  &ve_setup_8_uncoupled,

  &ve_setup_X_stereo,
  &ve_setup_X_uncoupled,
  &ve_setup_XX_stereo,
  &ve_setup_XX_uncoupled,
  0
};

static int vorbis_encode_toplevel_setup(vorbis_info *vi,int ch,long rate){
  if(vi && vi->codec_setup){

	vi->version=0;
	vi->channels=ch;
	vi->rate=rate;

	return(0);
  }
  return(OV_EINVAL);
}

static void vorbis_encode_floor_setup(vorbis_info *vi,double s,int block,
					 static_codebook	***books,
					 vorbis_info_floor1 *in,
					 int *x){
  int i,k,is=s;
  vorbis_info_floor1 *f=(vorbis_info_floor1*) _ogg_calloc(1,sizeof(*f));
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;

  memcpy(f,in+x[is],sizeof(*f));
  f->n=ci->blocksizes[block]>>1;

  {
	int partitions=f->partitions;
	int maxclass=-1;
	int maxbook=-1;
	for(i=0;i<partitions;i++)
	  if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
	for(i=0;i<=maxclass;i++){
	  if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
	  f->class_book[i]+=ci->books;
	  for(k=0;k<(1<<f->class_subs[i]);k++){
	if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
	if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
	  }
	}

	for(i=0;i<=maxbook;i++)
	  ci->book_param[ci->books++]=books[x[is]][i];
  }

  ci->floor_type[ci->floors]=1;
  ci->floor_param[ci->floors]=f;
  ci->floors++;

  return;
}

static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
						vorbis_info_psy_global *in,
						double *x){
  int i,is=s;
  double ds=s-is;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy_global *g=&ci->psy_g_param;

  memcpy(g,in+(int)x[is],sizeof(*g));

  ds=x[is]*(1.-ds)+x[is+1]*ds;
  is=(int)ds;
  ds-=is;
  if(ds==0 && is>0){
	is--;
	ds=1.;
  }

  for(i=0;i<4;i++){
	g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
	g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
  }
  g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
  return;
}

static void vorbis_encode_global_stereo(vorbis_info *vi,
					   highlevel_encode_setup *hi,
					   adj_stereo *p){
  float s=hi->stereo_point_setting;
  int i,is=s;
  double ds=s-is;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy_global *g=&ci->psy_g_param;

  if(p){
	memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
	memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);

	if(hi->managed){
	  for(i=0;i<PACKETBLOBS;i++){
	float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
	g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
	g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
	g->coupling_pkHz[i]=kHz;

	kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
	g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
	g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];

	  }
	}else{
	  float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
	  for(i=0;i<PACKETBLOBS;i++){
	g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
	g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
	g->coupling_pkHz[i]=kHz;
	  }

	  kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
	  for(i=0;i<PACKETBLOBS;i++){
	g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
	g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
	  }
	}
  }else{
	for(i=0;i<PACKETBLOBS;i++){
	  g->sliding_lowpass[0][i]=ci->blocksizes[0];
	  g->sliding_lowpass[1][i]=ci->blocksizes[1];
	}
  }
  return;
}

static void vorbis_encode_psyset_setup(vorbis_info *vi,double s,
					  int *nn_start,
					  int *nn_partition,
					  double *nn_thresh,
					  int block){
  codec_setup_info *ci=(codec_setup_info*) vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];
  highlevel_encode_setup *hi=&ci->hi;
  int is=s;

  if(block>=ci->psys)
	ci->psys=block+1;
  if(!p){
	p=(vorbis_info_psy*)_ogg_calloc(1,sizeof(*p));
	ci->psy_param[block]=p;
  }

  memcpy(p,&_psy_info_template,sizeof(*p));
  p->blockflag=block>>1;

  if(hi->noise_normalize_p){
	p->normal_channel_p=1;
	p->normal_point_p=1;
	p->normal_start=nn_start[is];
	p->normal_partition=nn_partition[is];
	p->normal_thresh=nn_thresh[is];
  }

  return;
}

static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
					 att3 *att,
					 int  *max,
					 vp_adjblock *in){
  int i,is=s;
  double ds=s-is;
  codec_setup_info *ci=(codec_setup_info*) vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

  p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds;
  p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds;
  p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds;
  p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds;
  p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds;

  p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds;

  for(i=0;i<P_BANDS;i++)
	p->toneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds;
  return;
}

static void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
					   compandblock *in, double *x){
  int i,is=s;
  double ds=s-is;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

  ds=x[is]*(1.-ds)+x[is+1]*ds;
  is=(int)ds;
  ds-=is;
  if(ds==0 && is>0){
	is--;
	ds=1.;
  }

  for(i=0;i<NOISE_COMPAND_LEVELS;i++)
	p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
  return;
}

static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
					int *suppress){
  int is=s;
  double ds=s-is;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

  p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;

  return;
}

static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
					 int *suppress,
					 noise3 *in,
					 noiseguard *guard,
					 double userbias){
  int i,is=s,j;
  double ds=s-is;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

  p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
  p->noisewindowlomin=guard[block].lo;
  p->noisewindowhimin=guard[block].hi;
  p->noisewindowfixed=guard[block].fixed;

  for(j=0;j<P_NOISECURVES;j++)
	for(i=0;i<P_BANDS;i++)
	  p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;

  for(j=0;j<P_NOISECURVES;j++){
	float min=p->noiseoff[j][0]+6; /* the lowest it can go */
	for(i=0;i<P_BANDS;i++){
	  p->noiseoff[j][i]+=userbias;
	  if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
	}
  }

  return;
}

static void vorbis_encode_ath_setup(vorbis_info *vi,int block){
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

  p->ath_adjatt=ci->hi.ath_floating_dB;
  p->ath_maxatt=ci->hi.ath_absolute_dB;
  return;
}

static int book_dup_or_new(codec_setup_info *ci,static_codebook *book){
  int i;
  for(i=0;i<ci->books;i++)
	if(ci->book_param[i]==book)return(i);

  return(ci->books++);
}

static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
					 int *shortb,int *longb){

  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  int is=s;

  int blockshort=shortb[is];
  int blocklong=longb[is];
  ci->blocksizes[0]=blockshort;
  ci->blocksizes[1]=blocklong;

}

static void vorbis_encode_residue_setup(vorbis_info *vi,
					   int number, int block,
					   vorbis_residue_template *res){

  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  int i,n;

  vorbis_info_residue0 *r=(vorbis_info_residue0*)(ci->residue_param[number]=
	(vorbis_info_residue0*)_ogg_malloc(sizeof(*r)));

  memcpy(r,res->res,sizeof(*r));
  if(ci->residues<=number)ci->residues=number+1;

  switch(ci->blocksizes[block]){
  case 64:case 128:case 256:
	r->grouping=16;
	break;
  default:
	r->grouping=32;
	break;
  }
  ci->residue_type[number]=res->res_type;

  n=r->end=ci->blocksizes[block]>>1;
  if(res->res_type==2)
	n=r->end*=vi->channels;

  {
	int booklist=0,k;

	if(ci->hi.managed){
	  for(i=0;i<r->partitions;i++)
	for(k=0;k<3;k++)
	  if(res->books_base_managed->books[i][k])
		r->secondstages[i]|=(1<<k);

	  r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
	  ci->book_param[r->groupbook]=res->book_aux_managed;

	  for(i=0;i<r->partitions;i++){
	for(k=0;k<3;k++){
	  if(res->books_base_managed->books[i][k]){
		int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
		r->booklist[booklist++]=bookid;
		ci->book_param[bookid]=res->books_base_managed->books[i][k];
	  }
	}
	  }

	}else{

	  for(i=0;i<r->partitions;i++)
	for(k=0;k<3;k++)
	  if(res->books_base->books[i][k])
		r->secondstages[i]|=(1<<k);

	  r->groupbook=book_dup_or_new(ci,res->book_aux);
	  ci->book_param[r->groupbook]=res->book_aux;

	  for(i=0;i<r->partitions;i++){
	for(k=0;k<3;k++){
	  if(res->books_base->books[i][k]){
		int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
		r->booklist[booklist++]=bookid;
		ci->book_param[bookid]=res->books_base->books[i][k];
	  }
	}
	  }
	}
  }

  {
	double freq=ci->hi.lowpass_kHz*1000.;
	vorbis_info_floor1 *f=(vorbis_info_floor1*)ci->floor_param[block]; /* by convention */
	double nyq=vi->rate/2.;
	long blocksize=ci->blocksizes[block]>>1;

	if(freq>nyq)freq=nyq;
	f->n=freq/nyq*blocksize;

	if(res->limit_type){
	  if(ci->hi.managed)
	freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.;
	  else
	freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.;
	  if(freq>nyq)freq=nyq;
	}

	if(ci->residue_type[block]==2)
	  r->end=(int)((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
	r->grouping;
	else
	  r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
	r->grouping;
  }
}

static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
					  vorbis_mapping_template *maps){

  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  int i,j,is=s,modes=2;
  vorbis_info_mapping0 *map=maps[is].map;
  vorbis_info_mode *mode=_mode_template;
  vorbis_residue_template *res=maps[is].res;

  if(ci->blocksizes[0]==ci->blocksizes[1])modes=1;

  for(i=0;i<modes;i++){

	ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
	ci->mode_param[i]=(vorbis_info_mode*)_ogg_calloc(1,sizeof(*mode));

	memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
	if(i>=ci->modes)ci->modes=i+1;

	ci->map_type[i]=0;
	memcpy(ci->map_param[i],map+i,sizeof(*map));
	if(i>=ci->maps)ci->maps=i+1;

	for(j=0;j<map[i].submaps;j++)
	  vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
				  ,res+map[i].residuesubmap[j]);
  }
}

static double setting_to_approx_bitrate(vorbis_info *vi){
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  highlevel_encode_setup *hi=&ci->hi;
  ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
  int is=hi->base_setting;
  double ds=hi->base_setting-is;
  int ch=vi->channels;
  double *r=setup->rate_mapping;

  if(r==NULL)
	return(-1);

  return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
}

static void get_setup_template(vorbis_info *vi,
				   long ch,long srate,
				   double req,int q_or_bitrate){
  int i=0,j;
  codec_setup_info *ci=(codec_setup_info*) vi->codec_setup;
  highlevel_encode_setup *hi=&ci->hi;
  if(q_or_bitrate)req/=ch;

  while(setup_list[i]){
	if(setup_list[i]->coupling_restriction==-1 ||
	   setup_list[i]->coupling_restriction==ch){
	  if(srate>=setup_list[i]->samplerate_min_restriction &&
	 srate<=setup_list[i]->samplerate_max_restriction){
	int mappings=setup_list[i]->mappings;
	double *map=(q_or_bitrate?
			 setup_list[i]->rate_mapping:
			 setup_list[i]->quality_mapping);

	if(req<map[0]){++i;continue;}
	if(req>map[setup_list[i]->mappings]){++i;continue;}
	for(j=0;j<mappings;j++)
	  if(req>=map[j] && req<map[j+1])break;
	hi->setup=setup_list[i];
	if(j==mappings)
	  hi->base_setting=j-.001;
	else{
	  float low=map[j];
	  float high=map[j+1];
	  float del=(req-low)/(high-low);
	  hi->base_setting=j+del;
	}

	return;
	  }
	}
	i++;
  }

  hi->setup=NULL;
}

int vorbis_encode_setup_init(vorbis_info *vi){
  int i0=0,singleblock=0;
  codec_setup_info *ci=(codec_setup_info*) vi->codec_setup;
  ve_setup_data_template *setup=NULL;
  highlevel_encode_setup *hi=&ci->hi;

  if(ci==NULL)return(OV_EINVAL);
  if(!hi->impulse_block_p)i0=1;

  if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80;
  if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200;

  if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.;
  if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.;

  setup=(ve_setup_data_template *)hi->setup;
  if(setup==NULL)return(OV_EINVAL);

  hi->set_in_stone=1;
  vorbis_encode_blocksize_setup(vi,hi->base_setting,
				setup->blocksize_short,
				setup->blocksize_long);
  if(ci->blocksizes[0]==ci->blocksizes[1])singleblock=1;

  vorbis_encode_floor_setup(vi,hi->short_setting,0,
				setup->floor_books,
				setup->floor_params,
				setup->floor_short_mapping);
  if(!singleblock)
	vorbis_encode_floor_setup(vi,hi->long_setting,1,
				  setup->floor_books,
				  setup->floor_params,
				  setup->floor_long_mapping);

  vorbis_encode_global_psych_setup(vi,hi->trigger_setting,
				   setup->global_params,
				   setup->global_mapping);
  vorbis_encode_global_stereo(vi,hi,setup->stereo_modes);

  vorbis_encode_psyset_setup(vi,hi->short_setting,
				 setup->psy_noise_normal_start[0],
				 setup->psy_noise_normal_partition[0],
				 setup->psy_noise_normal_thresh,
				 0);
  vorbis_encode_psyset_setup(vi,hi->short_setting,
				 setup->psy_noise_normal_start[0],
				 setup->psy_noise_normal_partition[0],
				 setup->psy_noise_normal_thresh,
				 1);
  if(!singleblock){
	vorbis_encode_psyset_setup(vi,hi->long_setting,
				   setup->psy_noise_normal_start[1],
				   setup->psy_noise_normal_partition[1],
					setup->psy_noise_normal_thresh,
				   2);
	vorbis_encode_psyset_setup(vi,hi->long_setting,
				   setup->psy_noise_normal_start[1],
				   setup->psy_noise_normal_partition[1],
				   setup->psy_noise_normal_thresh,
				   3);
  }

  vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0,
				   setup->psy_tone_masteratt,
				   setup->psy_tone_0dB,
				   setup->psy_tone_adj_impulse);
  vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1,
				   setup->psy_tone_masteratt,
				   setup->psy_tone_0dB,
				   setup->psy_tone_adj_other);
  if(!singleblock){
	vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2,
				 setup->psy_tone_masteratt,
				 setup->psy_tone_0dB,
				 setup->psy_tone_adj_other);
	vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3,
				 setup->psy_tone_masteratt,
				 setup->psy_tone_0dB,
				 setup->psy_tone_adj_long);
  }

  vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0,
				  setup->psy_noise_compand,
				  setup->psy_noise_compand_short_mapping);
  vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1,
				  setup->psy_noise_compand,
				  setup->psy_noise_compand_short_mapping);
  if(!singleblock){
	vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2,
				setup->psy_noise_compand,
				setup->psy_noise_compand_long_mapping);
	vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3,
				setup->psy_noise_compand,
				setup->psy_noise_compand_long_mapping);
  }

  vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0,
			   setup->psy_tone_dBsuppress);
  vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1,
			   setup->psy_tone_dBsuppress);
  if(!singleblock){
	vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2,
				 setup->psy_tone_dBsuppress);
	vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3,
				 setup->psy_tone_dBsuppress);
  }

  vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0,
				setup->psy_noise_dBsuppress,
				setup->psy_noise_bias_impulse,
				setup->psy_noiseguards,
				(i0==0?hi->impulse_noisetune:0.));
  vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1,
				setup->psy_noise_dBsuppress,
				setup->psy_noise_bias_padding,
				setup->psy_noiseguards,0.);
  if(!singleblock){
	vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2,
				  setup->psy_noise_dBsuppress,
				  setup->psy_noise_bias_trans,
				  setup->psy_noiseguards,0.);
	vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3,
				  setup->psy_noise_dBsuppress,
				  setup->psy_noise_bias_long,
				  setup->psy_noiseguards,0.);
  }

  vorbis_encode_ath_setup(vi,0);
  vorbis_encode_ath_setup(vi,1);
  if(!singleblock){
	vorbis_encode_ath_setup(vi,2);
	vorbis_encode_ath_setup(vi,3);
  }

  vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps);

  if(hi->bitrate_av>0)
	vi->bitrate_nominal=hi->bitrate_av;
  else{
	vi->bitrate_nominal=setting_to_approx_bitrate(vi);
  }

  vi->bitrate_lower=hi->bitrate_min;
  vi->bitrate_upper=hi->bitrate_max;
  if(hi->bitrate_av)
	vi->bitrate_window=(double)hi->bitrate_reservoir/hi->bitrate_av;
  else
	vi->bitrate_window=0.;

  if(hi->managed){
	ci->bi.avg_rate=hi->bitrate_av;
	ci->bi.min_rate=hi->bitrate_min;
	ci->bi.max_rate=hi->bitrate_max;

	ci->bi.reservoir_bits=hi->bitrate_reservoir;
	ci->bi.reservoir_bias=
	  hi->bitrate_reservoir_bias;

	ci->bi.slew_damp=hi->bitrate_av_damp;

  }

  return(0);

}

static int vorbis_encode_setup_setting(vorbis_info *vi,
					   long  channels,
					   long  rate){
  int ret=0,i,is;
  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  highlevel_encode_setup *hi=&ci->hi;
  ve_setup_data_template *setup=(ve_setup_data_template*) hi->setup;
  double ds;

  ret=vorbis_encode_toplevel_setup(vi,channels,rate);
  if(ret)return(ret);

  is=hi->base_setting;
  ds=hi->base_setting-is;

  hi->short_setting=hi->base_setting;
  hi->long_setting=hi->base_setting;

  hi->managed=0;

  hi->impulse_block_p=1;
  hi->noise_normalize_p=1;

  hi->stereo_point_setting=hi->base_setting;
  hi->lowpass_kHz=
	setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;

  hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
	setup->psy_ath_float[is+1]*ds;
  hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
	setup->psy_ath_abs[is+1]*ds;

  hi->amplitude_track_dBpersec=-6.;
  hi->trigger_setting=hi->base_setting;

  for(i=0;i<4;i++){
	hi->block[i].tone_mask_setting=hi->base_setting;
	hi->block[i].tone_peaklimit_setting=hi->base_setting;
	hi->block[i].noise_bias_setting=hi->base_setting;
	hi->block[i].noise_compand_setting=hi->base_setting;
  }

  return(ret);
}

int vorbis_encode_setup_vbr(vorbis_info *vi,
				long  channels,
				long  rate,
				float quality){
  codec_setup_info *ci=(codec_setup_info*) vi->codec_setup;
  highlevel_encode_setup *hi=&ci->hi;

  quality+=.0000001;
  if(quality>=1.)quality=.9999;

  get_setup_template(vi,channels,rate,quality,0);
  if(!hi->setup)return OV_EIMPL;

  return vorbis_encode_setup_setting(vi,channels,rate);
}

int vorbis_encode_init_vbr(vorbis_info *vi,
			   long channels,
			   long rate,

			   float base_quality /* 0. to 1. */
			   ){
  int ret=0;

  ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);

  if(ret){
	vorbis_info_clear(vi);
	return ret;
  }
  ret=vorbis_encode_setup_init(vi);
  if(ret)
	vorbis_info_clear(vi);
  return(ret);
}

int vorbis_encode_setup_managed(vorbis_info *vi,
				long channels,
				long rate,

				long max_bitrate,
				long nominal_bitrate,
				long min_bitrate){

  codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
  highlevel_encode_setup *hi=&ci->hi;
  double tnominal=nominal_bitrate;
  int ret=0;

  if(nominal_bitrate<=0.){
	if(max_bitrate>0.){
	  if(min_bitrate>0.)
	nominal_bitrate=(max_bitrate+min_bitrate)*.5;
	  else
	nominal_bitrate=max_bitrate*.875;
	}else{
	  if(min_bitrate>0.){
	nominal_bitrate=min_bitrate;
	  }else{
	return(OV_EINVAL);
	  }
	}
  }

  get_setup_template(vi,channels,rate,nominal_bitrate,1);
  if(!hi->setup)return OV_EIMPL;

  ret=vorbis_encode_setup_setting(vi,channels,rate);
  if(ret){
	vorbis_info_clear(vi);
	return ret;
  }

  hi->managed=1;
  hi->bitrate_min=min_bitrate;
  hi->bitrate_max=max_bitrate;
  hi->bitrate_av=tnominal;
  hi->bitrate_av_damp=1.5f; /* full range in no less than 1.5 second */
  hi->bitrate_reservoir=nominal_bitrate*2;
  hi->bitrate_reservoir_bias=.1; /* bias toward hoarding bits */

  return(ret);

}

int vorbis_encode_init(vorbis_info *vi,
			   long channels,
			   long rate,

			   long max_bitrate,
			   long nominal_bitrate,
			   long min_bitrate){

  int ret=vorbis_encode_setup_managed(vi,channels,rate,
					  max_bitrate,
					  nominal_bitrate,
					  min_bitrate);
  if(ret){
	vorbis_info_clear(vi);
	return(ret);
  }

  ret=vorbis_encode_setup_init(vi);
  if(ret)
	vorbis_info_clear(vi);
  return(ret);
}

int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
  if(vi){
	codec_setup_info *ci=(codec_setup_info*)vi->codec_setup;
	highlevel_encode_setup *hi=&ci->hi;
	int setp=(number&0xf); /* a read request has a low nibble of 0 */

	if(setp && hi->set_in_stone)return(OV_EINVAL);

	switch(number){

	case OV_ECTL_RATEMANAGE_GET:
	  {

	struct ovectl_ratemanage_arg *ai=
	  (struct ovectl_ratemanage_arg *)arg;

	ai->management_active=hi->managed;
	ai->bitrate_hard_window=ai->bitrate_av_window=
	  (double)hi->bitrate_reservoir/vi->rate;
	ai->bitrate_av_window_center=1.;
	ai->bitrate_hard_min=hi->bitrate_min;
	ai->bitrate_hard_max=hi->bitrate_max;
	ai->bitrate_av_lo=hi->bitrate_av;
	ai->bitrate_av_hi=hi->bitrate_av;

	  }
	  return(0);

	case OV_ECTL_RATEMANAGE_SET:
	  {
	struct ovectl_ratemanage_arg *ai=
	  (struct ovectl_ratemanage_arg *)arg;
	if(ai==NULL){
	  hi->managed=0;
	}else{
	  hi->managed=ai->management_active;
	  vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg);
	  vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg);
	}
	  }
	  return 0;

	case OV_ECTL_RATEMANAGE_AVG:
	  {
	struct ovectl_ratemanage_arg *ai=
	  (struct ovectl_ratemanage_arg *)arg;
	if(ai==NULL){
	  hi->bitrate_av=0;
	}else{
	  hi->bitrate_av=(ai->bitrate_av_lo+ai->bitrate_av_hi)*.5;
	}
	  }
	  return(0);
	case OV_ECTL_RATEMANAGE_HARD:
	  {
	struct ovectl_ratemanage_arg *ai=
	  (struct ovectl_ratemanage_arg *)arg;
	if(ai==NULL){
	  hi->bitrate_min=0;
	  hi->bitrate_max=0;
	}else{
	  hi->bitrate_min=ai->bitrate_hard_min;
	  hi->bitrate_max=ai->bitrate_hard_max;
	  hi->bitrate_reservoir=ai->bitrate_hard_window*
		(hi->bitrate_max+hi->bitrate_min)*.5;
	}
	if(hi->bitrate_reservoir<128.)
	  hi->bitrate_reservoir=128.;
	  }
	  return(0);

	case OV_ECTL_RATEMANAGE2_GET:
	  {
	struct ovectl_ratemanage2_arg *ai=
	  (struct ovectl_ratemanage2_arg *)arg;
	if(ai==NULL)return OV_EINVAL;

	ai->management_active=hi->managed;
	ai->bitrate_limit_min_kbps=hi->bitrate_min/1000;
	ai->bitrate_limit_max_kbps=hi->bitrate_max/1000;
	ai->bitrate_average_kbps=hi->bitrate_av/1000;
	ai->bitrate_average_damping=hi->bitrate_av_damp;
	ai->bitrate_limit_reservoir_bits=hi->bitrate_reservoir;
	ai->bitrate_limit_reservoir_bias=hi->bitrate_reservoir_bias;
	  }
	  return (0);
	case OV_ECTL_RATEMANAGE2_SET:
	  {
	struct ovectl_ratemanage2_arg *ai=
	  (struct ovectl_ratemanage2_arg *)arg;
	if(ai==NULL){
	  hi->managed=0;
	}else{
	  if(ai->bitrate_limit_min_kbps>0 &&
		 ai->bitrate_average_kbps>0 &&
		 ai->bitrate_limit_min_kbps>ai->bitrate_average_kbps)
		return OV_EINVAL;

	  if(ai->bitrate_limit_max_kbps>0 &&
		 ai->bitrate_average_kbps>0 &&
		 ai->bitrate_limit_max_kbps<ai->bitrate_average_kbps)
		return OV_EINVAL;

	  if(ai->bitrate_limit_min_kbps>0 &&
		 ai->bitrate_limit_max_kbps>0 &&
		 ai->bitrate_limit_min_kbps>ai->bitrate_limit_max_kbps)
		return OV_EINVAL;

	  if(ai->bitrate_average_damping <= 0.)
		return OV_EINVAL;

	  if(ai->bitrate_limit_reservoir_bits < 0)
		return OV_EINVAL;

	  if(ai->bitrate_limit_reservoir_bias < 0.)
		return OV_EINVAL;

	  if(ai->bitrate_limit_reservoir_bias > 1.)
		return OV_EINVAL;

	  hi->managed=ai->management_active;
	  hi->bitrate_min=ai->bitrate_limit_min_kbps * 1000;
	  hi->bitrate_max=ai->bitrate_limit_max_kbps * 1000;
	  hi->bitrate_av=ai->bitrate_average_kbps * 1000;
	  hi->bitrate_av_damp=ai->bitrate_average_damping;
	  hi->bitrate_reservoir=ai->bitrate_limit_reservoir_bits;
	  hi->bitrate_reservoir_bias=ai->bitrate_limit_reservoir_bias;
	}
	  }
	  return 0;

	case OV_ECTL_LOWPASS_GET:
	  {
	double *farg=(double *)arg;
	*farg=hi->lowpass_kHz;
	  }
	  return(0);
	case OV_ECTL_LOWPASS_SET:
	  {
	double *farg=(double *)arg;
	hi->lowpass_kHz=*farg;

	if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.;
	if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.;
	  }
	  return(0);
	case OV_ECTL_IBLOCK_GET:
	  {
	double *farg=(double *)arg;
	*farg=hi->impulse_noisetune;
	  }
	  return(0);
	case OV_ECTL_IBLOCK_SET:
	  {
	double *farg=(double *)arg;
	hi->impulse_noisetune=*farg;

	if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.;
	if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.;
	  }
	  return(0);
	}

	return(OV_EIMPL);
  }
  return(OV_EINVAL);
}

#endif
/*** End of inlined file: vorbisenc.c ***/


/*** Start of inlined file: vorbisfile.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <math.h>

#define CHUNKSIZE 8500 /* a shade over 8k; anyone using pages well
						  over 8k gets what they deserve */
static long _get_data(OggVorbis_File *vf){
  errno=0;
  if(vf->datasource){
	char *buffer=ogg_sync_buffer(&vf->oy,CHUNKSIZE);
	long bytes=(vf->callbacks.read_func)(buffer,1,CHUNKSIZE,vf->datasource);
	if(bytes>0)ogg_sync_wrote(&vf->oy,bytes);
	if(bytes==0 && errno)return(-1);
	return(bytes);
  }else
	return(0);
}

static void _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){
  if(vf->datasource){
	(vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET);
	vf->offset=offset;
	ogg_sync_reset(&vf->oy);
  }else{
	return;
  }
}

static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og,
				  ogg_int64_t boundary){
  if(boundary>0)boundary+=vf->offset;
  while(1){
	long more;

	if(boundary>0 && vf->offset>=boundary)return(OV_FALSE);
	more=ogg_sync_pageseek(&vf->oy,og);

	if(more<0){
	  vf->offset-=more;
	}else{
	  if(more==0){
	if(!boundary)return(OV_FALSE);
	{
	  long ret=_get_data(vf);
	  if(ret==0)return(OV_EOF);
	  if(ret<0)return(OV_EREAD);
	}
	  }else{
	ogg_int64_t ret=vf->offset;
	vf->offset+=more;
	return(ret);

	  }
	}
  }
}

static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_page *og){
  ogg_int64_t begin=vf->offset;
  ogg_int64_t end=begin;
  ogg_int64_t ret;
  ogg_int64_t offset=-1;

  while(offset==-1){
	begin-=CHUNKSIZE;
	if(begin<0)
	  begin=0;
	_seek_helper(vf,begin);
	while(vf->offset<end){
	  ret=_get_next_page(vf,og,end-vf->offset);
	  if(ret==OV_EREAD)return(OV_EREAD);
	  if(ret<0){
	break;
	  }else{
	offset=ret;
	  }
	}
  }

  _seek_helper(vf,offset);
  ret=_get_next_page(vf,og,CHUNKSIZE);
  if(ret<0)
	return(OV_EFAULT);

  return(offset);
}

static int _bisect_forward_serialno(OggVorbis_File *vf,
					ogg_int64_t begin,
					ogg_int64_t searched,
					ogg_int64_t end,
					long currentno,
					long m){
  ogg_int64_t endsearched=end;
  ogg_int64_t next=end;
  ogg_page og;
  ogg_int64_t ret;

  while(searched<endsearched){
	ogg_int64_t bisect;

	if(endsearched-searched<CHUNKSIZE){
	  bisect=searched;
	}else{
	  bisect=(searched+endsearched)/2;
	}

	_seek_helper(vf,bisect);
	ret=_get_next_page(vf,&og,-1);
	if(ret==OV_EREAD)return(OV_EREAD);
	if(ret<0 || ogg_page_serialno(&og)!=currentno){
	  endsearched=bisect;
	  if(ret>=0)next=ret;
	}else{
	  searched=ret+og.header_len+og.body_len;
	}
  }

  _seek_helper(vf,next);
  ret=_get_next_page(vf,&og,-1);
  if(ret==OV_EREAD)return(OV_EREAD);

  if(searched>=end || ret<0){
	vf->links=m+1;
	vf->offsets=(ogg_int64_t*)_ogg_malloc((vf->links+1)*sizeof(*vf->offsets));
	vf->serialnos=(long*)_ogg_malloc(vf->links*sizeof(*vf->serialnos));
	vf->offsets[m+1]=searched;
  }else{
	ret=_bisect_forward_serialno(vf,next,vf->offset,
				 end,ogg_page_serialno(&og),m+1);
	if(ret==OV_EREAD)return(OV_EREAD);
  }

  vf->offsets[m]=begin;
  vf->serialnos[m]=currentno;
  return(0);
}

static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc,
			  long *serialno,ogg_page *og_ptr){
  ogg_page og;
  ogg_packet op;
  int i,ret;

  if(!og_ptr){
	ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE);
	if(llret==OV_EREAD)return(OV_EREAD);
	if(llret<0)return OV_ENOTVORBIS;
	og_ptr=&og;
  }

  ogg_stream_reset_serialno(&vf->os,ogg_page_serialno(og_ptr));
  if(serialno)*serialno=vf->os.serialno;
  vf->ready_state=STREAMSET;

  vorbis_info_init(vi);
  vorbis_comment_init(vc);

  i=0;
  while(i<3){
	ogg_stream_pagein(&vf->os,og_ptr);
	while(i<3){
	  int result=ogg_stream_packetout(&vf->os,&op);
	  if(result==0)break;
	  if(result==-1){
	ret=OV_EBADHEADER;
	goto bail_header;
	  }
	  if((ret=vorbis_synthesis_headerin(vi,vc,&op))){
	goto bail_header;
	  }
	  i++;
	}
	if(i<3)
	  if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){
	ret=OV_EBADHEADER;
	goto bail_header;
	  }
  }
  return 0;

 bail_header:
  vorbis_info_clear(vi);
  vorbis_comment_clear(vc);
  vf->ready_state=OPENED;

  return ret;
}

static void _prefetch_all_headers(OggVorbis_File *vf, ogg_int64_t dataoffset){
  ogg_page og;
  int i;
  ogg_int64_t ret;

  vf->vi=(vorbis_info*) _ogg_realloc(vf->vi,vf->links*sizeof(*vf->vi));
  vf->vc=(vorbis_comment*) _ogg_realloc(vf->vc,vf->links*sizeof(*vf->vc));
  vf->dataoffsets=(ogg_int64_t*) _ogg_malloc(vf->links*sizeof(*vf->dataoffsets));
  vf->pcmlengths=(ogg_int64_t*) _ogg_malloc(vf->links*2*sizeof(*vf->pcmlengths));

  for(i=0;i<vf->links;i++){
	if(i==0){
	  vf->dataoffsets[i]=dataoffset;
	  _seek_helper(vf,dataoffset);

	}else{

	  _seek_helper(vf,vf->offsets[i]);
	  if(_fetch_headers(vf,vf->vi+i,vf->vc+i,NULL,NULL)<0){
		vf->dataoffsets[i]=-1;
	  }else{
	vf->dataoffsets[i]=vf->offset;
	  }
	}

	if(vf->dataoffsets[i]!=-1){
	  ogg_int64_t accumulated=0;
	  long	lastblock=-1;
	  int	 result;

	  ogg_stream_reset_serialno(&vf->os,vf->serialnos[i]);

	  while(1){
	ogg_packet op;

	ret=_get_next_page(vf,&og,-1);
	if(ret<0)
	  break;

	if(ogg_page_serialno(&og)!=vf->serialnos[i])
	  break;

	ogg_stream_pagein(&vf->os,&og);
	while((result=ogg_stream_packetout(&vf->os,&op))){
	  if(result>0){ /* ignore holes */
		long thisblock=vorbis_packet_blocksize(vf->vi+i,&op);
		if(lastblock!=-1)
		  accumulated+=(lastblock+thisblock)>>2;
		lastblock=thisblock;
	  }
	}

	if(ogg_page_granulepos(&og)!=-1){
	  accumulated= ogg_page_granulepos(&og)-accumulated;
	  break;
	}
	  }

	  if(accumulated<0)accumulated=0;

	  vf->pcmlengths[i*2]=accumulated;
	}

	{
	  ogg_int64_t end=vf->offsets[i+1];
	  _seek_helper(vf,end);

	  while(1){
	ret=_get_prev_page(vf,&og);
	if(ret<0){
	  vorbis_info_clear(vf->vi+i);
	  vorbis_comment_clear(vf->vc+i);
	  break;
	}
	if(ogg_page_granulepos(&og)!=-1){
	  vf->pcmlengths[i*2+1]=ogg_page_granulepos(&og)-vf->pcmlengths[i*2];
	  break;
	}
	vf->offset=ret;
	  }
	}
  }
}

static int _make_decode_ready(OggVorbis_File *vf){
  if(vf->ready_state>STREAMSET)return 0;
  if(vf->ready_state<STREAMSET)return OV_EFAULT;
  if(vf->seekable){
	if(vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link))
	  return OV_EBADLINK;
  }else{
	if(vorbis_synthesis_init(&vf->vd,vf->vi))
	  return OV_EBADLINK;
  }
  vorbis_block_init(&vf->vd,&vf->vb);
  vf->ready_state=INITSET;
  vf->bittrack=0.f;
  vf->samptrack=0.f;
  return 0;
}

static int _open_seekable2(OggVorbis_File *vf){
  long serialno=vf->current_serialno;
  ogg_int64_t dataoffset=vf->offset, end;
  ogg_page og;

  (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END);
  vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource);

  end=_get_prev_page(vf,&og);
  if(end<0)return(end);

  if(ogg_page_serialno(&og)!=serialno){

	if(_bisect_forward_serialno(vf,0,0,end+1,serialno,0)<0)return(OV_EREAD);

  }else{

	if(_bisect_forward_serialno(vf,0,end,end+1,serialno,0))return(OV_EREAD);

  }

  _prefetch_all_headers(vf,dataoffset);
  return(ov_raw_seek(vf,0));
}

static void _decode_clear(OggVorbis_File *vf){
  vorbis_dsp_clear(&vf->vd);
  vorbis_block_clear(&vf->vb);
  vf->ready_state=OPENED;
}

static int _fetch_and_process_packet(OggVorbis_File *vf,
					 ogg_packet *op_in,
					 int readp,
					 int spanp){
  ogg_page og;

  while(1){

	if(vf->ready_state==INITSET){
	  while(1) {
	  	ogg_packet op;
	  	ogg_packet *op_ptr=(op_in?op_in:&op);
	int result=ogg_stream_packetout(&vf->os,op_ptr);
	ogg_int64_t granulepos;

	op_in=NULL;
	if(result==-1)return(OV_HOLE); /* hole in the data. */
	if(result>0){
	  granulepos=op_ptr->granulepos;
	  if(!vorbis_synthesis(&vf->vb,op_ptr)){ /* lazy check for lazy
							header handling.  The
							header packets aren't
							audio, so if/when we
							submit them,
							vorbis_synthesis will
							reject them */

		{
		  int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL);
		  if(oldsamples)return(OV_EFAULT);

		  vorbis_synthesis_blockin(&vf->vd,&vf->vb);
		  vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples;
		  vf->bittrack+=op_ptr->bytes*8;
		}

		if(granulepos!=-1 && !op_ptr->e_o_s){
		  int link=(vf->seekable?vf->current_link:0);
		  int i,samples;

		  if(vf->seekable && link>0)
		granulepos-=vf->pcmlengths[link*2];
		  if(granulepos<0)granulepos=0; /* actually, this
						   shouldn't be possible
						   here unless the stream
						   is very broken */

		  samples=vorbis_synthesis_pcmout(&vf->vd,NULL);

		  granulepos-=samples;
		  for(i=0;i<link;i++)
		granulepos+=vf->pcmlengths[i*2+1];
		  vf->pcm_offset=granulepos;
		}
		return(1);
	  }
	}
	else
	  break;
	  }
	}

	if(vf->ready_state>=OPENED){
	  ogg_int64_t ret;
	  if(!readp)return(0);
	  if((ret=_get_next_page(vf,&og,-1))<0){
	return(OV_EOF); /* eof.
			   leave unitialized */
	  }

	  vf->bittrack+=og.header_len*8;

	  if(vf->ready_state==INITSET){
	if(vf->current_serialno!=ogg_page_serialno(&og)){
	  if(!spanp)
		return(OV_EOF);

	  _decode_clear(vf);

	  if(!vf->seekable){
		vorbis_info_clear(vf->vi);
		vorbis_comment_clear(vf->vc);
	  }
	}
	  }
	}

	if(vf->ready_state!=INITSET){
	  int link;

	  if(vf->ready_state<STREAMSET){
	if(vf->seekable){
	  vf->current_serialno=ogg_page_serialno(&og);

	  for(link=0;link<vf->links;link++)
		if(vf->serialnos[link]==vf->current_serialno)break;
	  if(link==vf->links)return(OV_EBADLINK); /* sign of a bogus
							 stream.  error out,
							 leave machine
							 uninitialized */

	  vf->current_link=link;

	  ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
	  vf->ready_state=STREAMSET;

	}else{

	  int ret=_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,&og);
	  if(ret)return(ret);
	  vf->current_link++;
	  link=0;
	}
	  }

	  {
	int ret=_make_decode_ready(vf);
	if(ret<0)return ret;
	  }
	}
	ogg_stream_pagein(&vf->os,&og);
  }
}

static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
  if(f==NULL)return(-1);
  return fseek(f,off,whence);
}

static int _ov_open1(void *f,OggVorbis_File *vf,char *initial,
			 long ibytes, ov_callbacks callbacks){
  int offsettest=(f?callbacks.seek_func(f,0,SEEK_CUR):-1);
  int ret;

  memset(vf,0,sizeof(*vf));
  vf->datasource=f;
  vf->callbacks = callbacks;

  ogg_sync_init(&vf->oy);

  if(initial){
	char *buffer=ogg_sync_buffer(&vf->oy,ibytes);
	memcpy(buffer,initial,ibytes);
	ogg_sync_wrote(&vf->oy,ibytes);
  }

  if(offsettest!=-1)vf->seekable=1;

  vf->links=1;
  vf->vi=(vorbis_info*) _ogg_calloc(vf->links,sizeof(*vf->vi));
  vf->vc=(vorbis_comment*) _ogg_calloc(vf->links,sizeof(*vf->vc));
  ogg_stream_init(&vf->os,-1); /* fill in the serialno later */

  if((ret=_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,NULL))<0){
	vf->datasource=NULL;
	ov_clear(vf);
  }else
	vf->ready_state=PARTOPEN;
  return(ret);
}

static int _ov_open2(OggVorbis_File *vf){
  if(vf->ready_state != PARTOPEN) return OV_EINVAL;
  vf->ready_state=OPENED;
  if(vf->seekable){
	int ret=_open_seekable2(vf);
	if(ret){
	  vf->datasource=NULL;
	  ov_clear(vf);
	}
	return(ret);
  }else
	vf->ready_state=STREAMSET;

  return 0;
}

int ov_clear(OggVorbis_File *vf){
  if(vf){
	vorbis_block_clear(&vf->vb);
	vorbis_dsp_clear(&vf->vd);
	ogg_stream_clear(&vf->os);

	if(vf->vi && vf->links){
	  int i;
	  for(i=0;i<vf->links;i++){
	vorbis_info_clear(vf->vi+i);
	vorbis_comment_clear(vf->vc+i);
	  }
	  _ogg_free(vf->vi);
	  _ogg_free(vf->vc);
	}
	if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
	if(vf->pcmlengths)_ogg_free(vf->pcmlengths);
	if(vf->serialnos)_ogg_free(vf->serialnos);
	if(vf->offsets)_ogg_free(vf->offsets);
	ogg_sync_clear(&vf->oy);
	if(vf->datasource)(vf->callbacks.close_func)(vf->datasource);
	memset(vf,0,sizeof(*vf));
  }
#ifdef DEBUG_LEAKS
  _VDBG_dump();
#endif
  return(0);
}

int ov_open_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
	ov_callbacks callbacks){
  int ret=_ov_open1(f,vf,initial,ibytes,callbacks);
  if(ret)return ret;
  return _ov_open2(vf);
}

int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
  ov_callbacks callbacks = {
	(size_t (*)(void *, size_t, size_t, void *))  fread,
	(int (*)(void *, ogg_int64_t, int))		  _fseek64_wrap,
	(int (*)(void *))				 fclose,
	(long (*)(void *))				ftell
  };

  return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks);
}

int ov_halfrate(OggVorbis_File *vf,int flag){
  int i;
  if(vf->vi==NULL)return OV_EINVAL;
  if(!vf->seekable)return OV_EINVAL;
  if(vf->ready_state>=STREAMSET)
	_decode_clear(vf); /* clear out stream state; later on libvorbis
						  will be able to swap this on the fly, but
						  for now dumping the decode machine is needed
						  to reinit the MDCT lookups.  1.1 libvorbis
						  is planned to be able to switch on the fly */

  for(i=0;i<vf->links;i++){
	if(vorbis_synthesis_halfrate(vf->vi+i,flag)){
	  ov_halfrate(vf,0);
	  return OV_EINVAL;
	}
  }
  return 0;
}

int ov_halfrate_p(OggVorbis_File *vf){
  if(vf->vi==NULL)return OV_EINVAL;
  return vorbis_synthesis_halfrate_p(vf->vi);
}

int ov_test_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
	ov_callbacks callbacks)
{
  return _ov_open1(f,vf,initial,ibytes,callbacks);
}

int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
  ov_callbacks callbacks = {
	(size_t (*)(void *, size_t, size_t, void *))  fread,
	(int (*)(void *, ogg_int64_t, int))		  _fseek64_wrap,
	(int (*)(void *))				 fclose,
	(long (*)(void *))				ftell
  };

  return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks);
}

int ov_test_open(OggVorbis_File *vf){
  if(vf->ready_state!=PARTOPEN)return(OV_EINVAL);
  return _ov_open2(vf);
}

long ov_streams(OggVorbis_File *vf){
  return vf->links;
}

long ov_seekable(OggVorbis_File *vf){
  return vf->seekable;
}

long ov_bitrate(OggVorbis_File *vf,int i){
  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(i>=vf->links)return(OV_EINVAL);
  if(!vf->seekable && i!=0)return(ov_bitrate(vf,0));
  if(i<0){
	ogg_int64_t bits=0;
	int i;
	float br;
	for(i=0;i<vf->links;i++)
	  bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8;
	br = bits/ov_time_total(vf,-1);
	return(rint(br));
  }else{
	if(vf->seekable){
	  return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i)));
	}else{
	  if(vf->vi[i].bitrate_nominal>0){
	return vf->vi[i].bitrate_nominal;
	  }else{
	if(vf->vi[i].bitrate_upper>0){
	  if(vf->vi[i].bitrate_lower>0){
		return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2;
	  }else{
		return vf->vi[i].bitrate_upper;
	  }
	}
	return(OV_FALSE);
	  }
	}
  }
}

long ov_bitrate_instant(OggVorbis_File *vf){
  int link=(vf->seekable?vf->current_link:0);
  long ret;
  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(vf->samptrack==0)return(OV_FALSE);
  ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5;
  vf->bittrack=0.f;
  vf->samptrack=0.f;
  return(ret);
}

long ov_serialnumber(OggVorbis_File *vf,int i){
  if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1));
  if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1));
  if(i<0){
	return(vf->current_serialno);
  }else{
	return(vf->serialnos[i]);
  }
}

ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){
  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  if(i<0){
	ogg_int64_t acc=0;
	int i;
	for(i=0;i<vf->links;i++)
	  acc+=ov_raw_total(vf,i);
	return(acc);
  }else{
	return(vf->offsets[i+1]-vf->offsets[i]);
  }
}

ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){
  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  if(i<0){
	ogg_int64_t acc=0;
	int i;
	for(i=0;i<vf->links;i++)
	  acc+=ov_pcm_total(vf,i);
	return(acc);
  }else{
	return(vf->pcmlengths[i*2+1]);
  }
}

double ov_time_total(OggVorbis_File *vf,int i){
  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  if(i<0){
	double acc=0;
	int i;
	for(i=0;i<vf->links;i++)
	  acc+=ov_time_total(vf,i);
	return(acc);
  }else{
	return((double)(vf->pcmlengths[i*2+1])/vf->vi[i].rate);
  }
}

int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){
  ogg_stream_state work_os;

  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(!vf->seekable)
	return(OV_ENOSEEK); /* don't dump machine if we can't seek */

  if(pos<0 || pos>vf->end)return(OV_EINVAL);

  vf->pcm_offset=-1;
  ogg_stream_reset_serialno(&vf->os,
				vf->current_serialno); /* must set serialno */
  vorbis_synthesis_restart(&vf->vd);

  _seek_helper(vf,pos);

  {
	ogg_page og;
	ogg_packet op;
	int lastblock=0;
	int accblock=0;
	int thisblock;
	int eosflag;

	ogg_stream_init(&work_os,vf->current_serialno); /* get the memory ready */
	ogg_stream_reset(&work_os); /* eliminate the spurious OV_HOLE
								   return from not necessarily
								   starting from the beginning */

	while(1){
	  if(vf->ready_state>=STREAMSET){
	int result=ogg_stream_packetout(&work_os,&op);

	if(result>0){

	  if(vf->vi[vf->current_link].codec_setup){
		thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
		if(thisblock<0){
		  ogg_stream_packetout(&vf->os,NULL);
		  thisblock=0;
		}else{

		  if(eosflag)
		  ogg_stream_packetout(&vf->os,NULL);
		  else
		if(lastblock)accblock+=(lastblock+thisblock)>>2;
		}

		if(op.granulepos!=-1){
		  int i,link=vf->current_link;
		  ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2];
		  if(granulepos<0)granulepos=0;

		  for(i=0;i<link;i++)
		granulepos+=vf->pcmlengths[i*2+1];
		  vf->pcm_offset=granulepos-accblock;
		  break;
		}
		lastblock=thisblock;
		continue;
	  }else
		ogg_stream_packetout(&vf->os,NULL);
	}
	  }

	  if(!lastblock){
	if(_get_next_page(vf,&og,-1)<0){
	  vf->pcm_offset=ov_pcm_total(vf,-1);
	  break;
	}
	  }else{
	vf->pcm_offset=-1;
	break;
	  }

	  if(vf->ready_state>=STREAMSET)
	if(vf->current_serialno!=ogg_page_serialno(&og)){
	  _decode_clear(vf); /* clear out stream state */
	  ogg_stream_clear(&work_os);
	}

	  if(vf->ready_state<STREAMSET){
	int link;

	vf->current_serialno=ogg_page_serialno(&og);
	for(link=0;link<vf->links;link++)
	  if(vf->serialnos[link]==vf->current_serialno)break;
	if(link==vf->links)goto seek_error; /* sign of a bogus stream.
						   error out, leave
						   machine uninitialized */
	vf->current_link=link;

	ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
	ogg_stream_reset_serialno(&work_os,vf->current_serialno);
	vf->ready_state=STREAMSET;

	  }

	  ogg_stream_pagein(&vf->os,&og);
	  ogg_stream_pagein(&work_os,&og);
	  eosflag=ogg_page_eos(&og);
	}
  }

  ogg_stream_clear(&work_os);
  vf->bittrack=0.f;
  vf->samptrack=0.f;
  return(0);

 seek_error:
  vf->pcm_offset=-1;
  ogg_stream_clear(&work_os);
  _decode_clear(vf);
  return OV_EBADLINK;
}

int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
  int link=-1;
  ogg_int64_t result=0;
  ogg_int64_t total=ov_pcm_total(vf,-1);

  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(!vf->seekable)return(OV_ENOSEEK);

  if(pos<0 || pos>total)return(OV_EINVAL);

  for(link=vf->links-1;link>=0;link--){
	total-=vf->pcmlengths[link*2+1];
	if(pos>=total)break;
  }

  {
	ogg_int64_t end=vf->offsets[link+1];
	ogg_int64_t begin=vf->offsets[link];
	ogg_int64_t begintime = vf->pcmlengths[link*2];
	ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime;
	ogg_int64_t target=pos-total+begintime;
	ogg_int64_t best=begin;

	ogg_page og;
	while(begin<end){
	  ogg_int64_t bisect;

	  if(end-begin<CHUNKSIZE){
	bisect=begin;
	  }else{
	bisect=begin +
	  (target-begintime)*(end-begin)/(endtime-begintime) - CHUNKSIZE;
	if(bisect<=begin)
	  bisect=begin+1;
	  }

	  _seek_helper(vf,bisect);

	  while(begin<end){
	result=_get_next_page(vf,&og,end-vf->offset);
	if(result==OV_EREAD) goto seek_error;
	if(result<0){
	  if(bisect<=begin+1)
		end=begin; /* found it */
	  else{
		if(bisect==0) goto seek_error;
		bisect-=CHUNKSIZE;
		if(bisect<=begin)bisect=begin+1;
		_seek_helper(vf,bisect);
	  }
	}else{
	  ogg_int64_t granulepos=ogg_page_granulepos(&og);
	  if(granulepos==-1)continue;
	  if(granulepos<target){
		best=result;  /* raw offset of packet with granulepos */
		begin=vf->offset; /* raw offset of next page */
		begintime=granulepos;

		if(target-begintime>44100)break;
		bisect=begin; /* *not* begin + 1 */
	  }else{
		if(bisect<=begin+1)
		  end=begin;  /* found it */
		else{
		  if(end==vf->offset){ /* we're pretty close - we'd be stuck in */
		end=result;
		bisect-=CHUNKSIZE; /* an endless loop otherwise. */
		if(bisect<=begin)bisect=begin+1;
		_seek_helper(vf,bisect);
		  }else{
		end=result;
		endtime=granulepos;
		break;
		  }
		}
	  }
	}
	  }
	}

	{
	  ogg_page og;
	  ogg_packet op;

	  _seek_helper(vf,best);
	  vf->pcm_offset=-1;

	  if(_get_next_page(vf,&og,-1)<0)return(OV_EOF); /* shouldn't happen */

	  if(link!=vf->current_link){
	_decode_clear(vf);

	vf->current_link=link;
	vf->current_serialno=ogg_page_serialno(&og);
	vf->ready_state=STREAMSET;

	  }else{
	vorbis_synthesis_restart(&vf->vd);
	  }

	  ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
	  ogg_stream_pagein(&vf->os,&og);

	  while(1){
	result=ogg_stream_packetpeek(&vf->os,&op);
	if(result==0){

	  _seek_helper(vf,best);

	  while(1){
		result=_get_prev_page(vf,&og);
		if(result<0) goto seek_error;
		if(ogg_page_granulepos(&og)>-1 ||
		   !ogg_page_continued(&og)){
		  return ov_raw_seek(vf,result);
		}
		vf->offset=result;
	  }
	}
	if(result<0){
	  result = OV_EBADPACKET;
	  goto seek_error;
	}
	if(op.granulepos!=-1){
	  vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
	  if(vf->pcm_offset<0)vf->pcm_offset=0;
	  vf->pcm_offset+=total;
	  break;
	}else
	  result=ogg_stream_packetout(&vf->os,NULL);
	  }
	}
  }

  if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){
	result=OV_EFAULT;
	goto seek_error;
  }
  vf->bittrack=0.f;
  vf->samptrack=0.f;
  return(0);

 seek_error:
  vf->pcm_offset=-1;
  _decode_clear(vf);
  return (int)result;
}

int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){
  int thisblock,lastblock=0;
  int ret=ov_pcm_seek_page(vf,pos);
  if(ret<0)return(ret);
  if((ret=_make_decode_ready(vf)))return ret;

  while(1){
	ogg_packet op;
	ogg_page og;

	int ret=ogg_stream_packetpeek(&vf->os,&op);
	if(ret>0){
	  thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
	  if(thisblock<0){
	ogg_stream_packetout(&vf->os,NULL);
	continue; /* non audio packet */
	  }
	  if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2;

	  if(vf->pcm_offset+((thisblock+
			  vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break;

	  ogg_stream_packetout(&vf->os,NULL);
	  vorbis_synthesis_trackonly(&vf->vb,&op);  /* set up a vb with
												   only tracking, no
												   pcm_decode */
	  vorbis_synthesis_blockin(&vf->vd,&vf->vb);

	  if(op.granulepos>-1){
	int i;
	vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
	if(vf->pcm_offset<0)vf->pcm_offset=0;
	for(i=0;i<vf->current_link;i++)
	  vf->pcm_offset+=vf->pcmlengths[i*2+1];
	  }

	  lastblock=thisblock;

	}else{
	  if(ret<0 && ret!=OV_HOLE)break;

	  if(_get_next_page(vf,&og,-1)<0)break;
	  if(vf->current_serialno!=ogg_page_serialno(&og))_decode_clear(vf);

	  if(vf->ready_state<STREAMSET){
	int link;

	vf->current_serialno=ogg_page_serialno(&og);
	for(link=0;link<vf->links;link++)
	  if(vf->serialnos[link]==vf->current_serialno)break;
	if(link==vf->links)return(OV_EBADLINK);
	vf->current_link=link;

	ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
	vf->ready_state=STREAMSET;
	ret=_make_decode_ready(vf);
	if(ret)return ret;
	lastblock=0;
	  }

	  ogg_stream_pagein(&vf->os,&og);
	}
  }

  vf->bittrack=0.f;
  vf->samptrack=0.f;
  while(vf->pcm_offset<pos){
	ogg_int64_t target=pos-vf->pcm_offset;
	long samples=vorbis_synthesis_pcmout(&vf->vd,NULL);

	if(samples>target)samples=target;
	vorbis_synthesis_read(&vf->vd,samples);
	vf->pcm_offset+=samples;

	if(samples<target)
	  if(_fetch_and_process_packet(vf,NULL,1,1)<=0)
	vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */
  }
  return 0;
}

int ov_time_seek(OggVorbis_File *vf,double seconds){

  int link=-1;
  ogg_int64_t pcm_total=ov_pcm_total(vf,-1);
  double time_total=ov_time_total(vf,-1);

  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(!vf->seekable)return(OV_ENOSEEK);
  if(seconds<0 || seconds>time_total)return(OV_EINVAL);

  for(link=vf->links-1;link>=0;link--){
	pcm_total-=vf->pcmlengths[link*2+1];
	time_total-=ov_time_total(vf,link);
	if(seconds>=time_total)break;
  }

  {
	ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
	return(ov_pcm_seek(vf,target));
  }
}

int ov_time_seek_page(OggVorbis_File *vf,double seconds){

  int link=-1;
  ogg_int64_t pcm_total=ov_pcm_total(vf,-1);
  double time_total=ov_time_total(vf,-1);

  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(!vf->seekable)return(OV_ENOSEEK);
  if(seconds<0 || seconds>time_total)return(OV_EINVAL);

  for(link=vf->links-1;link>=0;link--){
	pcm_total-=vf->pcmlengths[link*2+1];
	time_total-=ov_time_total(vf,link);
	if(seconds>=time_total)break;
  }

  {
	ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
	return(ov_pcm_seek_page(vf,target));
  }
}

ogg_int64_t ov_raw_tell(OggVorbis_File *vf){
  if(vf->ready_state<OPENED)return(OV_EINVAL);
  return(vf->offset);
}

ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){
  if(vf->ready_state<OPENED)return(OV_EINVAL);
  return(vf->pcm_offset);
}

double ov_time_tell(OggVorbis_File *vf){
  int link=0;
  ogg_int64_t pcm_total=0;
  double time_total=0.f;

  if(vf->ready_state<OPENED)return(OV_EINVAL);
  if(vf->seekable){
	pcm_total=ov_pcm_total(vf,-1);
	time_total=ov_time_total(vf,-1);

	for(link=vf->links-1;link>=0;link--){
	  pcm_total-=vf->pcmlengths[link*2+1];
	  time_total-=ov_time_total(vf,link);
	  if(vf->pcm_offset>=pcm_total)break;
	}
  }

  return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate);
}

vorbis_info *ov_info(OggVorbis_File *vf,int link){
  if(vf->seekable){
	if(link<0)
	  if(vf->ready_state>=STREAMSET)
	return vf->vi+vf->current_link;
	  else
	  return vf->vi;
	else
	  if(link>=vf->links)
	return NULL;
	  else
	return vf->vi+link;
  }else{
	return vf->vi;
  }
}

vorbis_comment *ov_comment(OggVorbis_File *vf,int link){
  if(vf->seekable){
	if(link<0)
	  if(vf->ready_state>=STREAMSET)
	return vf->vc+vf->current_link;
	  else
	return vf->vc;
	else
	  if(link>=vf->links)
	return NULL;
	  else
	return vf->vc+link;
  }else{
	return vf->vc;
  }
}

static int host_is_big_endian() {
  ogg_int32_t pattern = 0xfeedface; /* deadbeef */
  unsigned char *bytewise = (unsigned char *)&pattern;
  if (bytewise[0] == 0xfe) return 1;
  return 0;
}

long ov_read(OggVorbis_File *vf,char *buffer,int length,
			int bigendianp,int word,int sgned,int *bitstream){
  int i,j;
  int host_endian = host_is_big_endian();

  float **pcm;
  long samples;

  if(vf->ready_state<OPENED)return(OV_EINVAL);

  while(1){
	if(vf->ready_state==INITSET){
	  samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
	  if(samples)break;
	}

	{
	  int ret=_fetch_and_process_packet(vf,NULL,1,1);
	  if(ret==OV_EOF)
	return(0);
	  if(ret<=0)
	return(ret);
	}

  }

  if(samples>0){

	long channels=ov_info(vf,-1)->channels;
	long bytespersample=word * channels;
	vorbis_fpu_control fpu;
	(void) fpu; // (to avoid a warning about it being unused)
	if(samples>length/bytespersample)samples=length/bytespersample;

	if(samples <= 0)
	  return OV_EINVAL;

	{
	  int val;
	  if(word==1){
	int off=(sgned?0:128);
	vorbis_fpu_setround(&fpu);
	for(j=0;j<samples;j++)
	  for(i=0;i<channels;i++){
		val=vorbis_ftoi(pcm[i][j]*128.f);
		if(val>127)val=127;
		else if(val<-128)val=-128;
		*buffer++=val+off;
	  }
	vorbis_fpu_restore(fpu);
	  }else{
	int off=(sgned?0:32768);

	if(host_endian==bigendianp){
	  if(sgned){

		vorbis_fpu_setround(&fpu);
		for(i=0;i<channels;i++) { /* It's faster in this order */
		  float *src=pcm[i];
		  short *dest=((short *)buffer)+i;
		  for(j=0;j<samples;j++) {
		val=vorbis_ftoi(src[j]*32768.f);
		if(val>32767)val=32767;
		else if(val<-32768)val=-32768;
		*dest=val;
		dest+=channels;
		  }
		}
		vorbis_fpu_restore(fpu);

	  }else{

		vorbis_fpu_setround(&fpu);
		for(i=0;i<channels;i++) {
		  float *src=pcm[i];
		  short *dest=((short *)buffer)+i;
		  for(j=0;j<samples;j++) {
		val=vorbis_ftoi(src[j]*32768.f);
		if(val>32767)val=32767;
		else if(val<-32768)val=-32768;
		*dest=val+off;
		dest+=channels;
		  }
		}
		vorbis_fpu_restore(fpu);

	  }
	}else if(bigendianp){

	  vorbis_fpu_setround(&fpu);
	  for(j=0;j<samples;j++)
		for(i=0;i<channels;i++){
		  val=vorbis_ftoi(pcm[i][j]*32768.f);
		  if(val>32767)val=32767;
		  else if(val<-32768)val=-32768;
		  val+=off;
		  *buffer++=(val>>8);
		  *buffer++=(val&0xff);
		}
	  vorbis_fpu_restore(fpu);

	}else{
	  int val;
	  vorbis_fpu_setround(&fpu);
	  for(j=0;j<samples;j++)
		for(i=0;i<channels;i++){
		  val=vorbis_ftoi(pcm[i][j]*32768.f);
		  if(val>32767)val=32767;
		  else if(val<-32768)val=-32768;
		  val+=off;
		  *buffer++=(val&0xff);
		  *buffer++=(val>>8);
	  	}
	  vorbis_fpu_restore(fpu);

	}
	  }
	}

	vorbis_synthesis_read(&vf->vd,samples);
	vf->pcm_offset+=samples;
	if(bitstream)*bitstream=vf->current_link;
	return(samples*bytespersample);
  }else{
	return(samples);
  }
}

long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int length,
		   int *bitstream){

  if(vf->ready_state<OPENED)return(OV_EINVAL);

  while(1){
	if(vf->ready_state==INITSET){
	  float **pcm;
	  long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
	  if(samples){
	if(pcm_channels)*pcm_channels=pcm;
	if(samples>length)samples=length;
	vorbis_synthesis_read(&vf->vd,samples);
	vf->pcm_offset+=samples;
	if(bitstream)*bitstream=vf->current_link;
	return samples;

	  }
	}

	{
	  int ret=_fetch_and_process_packet(vf,NULL,1,1);
	  if(ret==OV_EOF)return(0);
	  if(ret<=0)return(ret);
	}

  }
}

extern float *vorbis_window(vorbis_dsp_state *v,int W);
extern void _analysis_output_always(const char *base,int i,float *v,int n,int bark,int dB,
				 ogg_int64_t off);

static void _ov_splice(float **pcm,float **lappcm,
			   int n1, int n2,
			   int ch1, int ch2,
			   float *w1, float *w2){
  int i,j;
  float *w=w1;
  int n=n1;

  if(n1>n2){
	n=n2;
	w=w2;
  }

  for(j=0;j<ch1 && j<ch2;j++){
	float *s=lappcm[j];
	float *d=pcm[j];

	for(i=0;i<n;i++){
	  float wd=w[i]*w[i];
	  float ws=1.-wd;
	  d[i]=d[i]*wd + s[i]*ws;
	}
  }
  for(;j<ch2;j++){
	float *d=pcm[j];
	for(i=0;i<n;i++){
	  float wd=w[i]*w[i];
	  d[i]=d[i]*wd;
	}
  }

}

static int _ov_initset(OggVorbis_File *vf){
  while(1){
	if(vf->ready_state==INITSET)break;
	{
	  int ret=_fetch_and_process_packet(vf,NULL,1,0);
	  if(ret<0 && ret!=OV_HOLE)return(ret);
	}
  }
  return 0;
}

static int _ov_initprime(OggVorbis_File *vf){
  vorbis_dsp_state *vd=&vf->vd;
  while(1){
	if(vf->ready_state==INITSET)
	  if(vorbis_synthesis_pcmout(vd,NULL))break;

	{
	  int ret=_fetch_and_process_packet(vf,NULL,1,0);
	  if(ret<0 && ret!=OV_HOLE)return(ret);
	}
  }
  return 0;
}

static void _ov_getlap(OggVorbis_File *vf,vorbis_info *vi,vorbis_dsp_state *vd,
			   float **lappcm,int lapsize){
  int lapcount=0,i;
  float **pcm;

  while(lapcount<lapsize){
	int samples=vorbis_synthesis_pcmout(vd,&pcm);
	if(samples){
	  if(samples>lapsize-lapcount)samples=lapsize-lapcount;
	  for(i=0;i<vi->channels;i++)
	memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples);
	  lapcount+=samples;
	  vorbis_synthesis_read(vd,samples);
	}else{
	  int ret=_fetch_and_process_packet(vf,NULL,1,0); /* do *not* span */
	  if(ret==OV_EOF)break;
	}
  }
  if(lapcount<lapsize){
	int samples=vorbis_synthesis_lapout(&vf->vd,&pcm);
	if(samples==0){
	  for(i=0;i<vi->channels;i++)
	memset(lappcm[i]+lapcount,0,sizeof(**pcm)*lapsize-lapcount);
	  lapcount=lapsize;
	}else{
	  if(samples>lapsize-lapcount)samples=lapsize-lapcount;
	  for(i=0;i<vi->channels;i++)
	memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples);
	  lapcount+=samples;
	}
  }
}

int ov_crosslap(OggVorbis_File *vf1, OggVorbis_File *vf2){
  vorbis_info *vi1,*vi2;
  float **lappcm;
  float **pcm;
  float *w1,*w2;
  int n1,n2,i,ret,hs1,hs2;

  if(vf1==vf2)return(0); /* degenerate case */
  if(vf1->ready_state<OPENED)return(OV_EINVAL);
  if(vf2->ready_state<OPENED)return(OV_EINVAL);

  ret=_ov_initset(vf1);
  if(ret)return(ret);
  ret=_ov_initprime(vf2);
  if(ret)return(ret);

  vi1=ov_info(vf1,-1);
  vi2=ov_info(vf2,-1);
  hs1=ov_halfrate_p(vf1);
  hs2=ov_halfrate_p(vf2);

  lappcm=(float**) alloca(sizeof(*lappcm)*vi1->channels);
  n1=vorbis_info_blocksize(vi1,0)>>(1+hs1);
  n2=vorbis_info_blocksize(vi2,0)>>(1+hs2);
  w1=vorbis_window(&vf1->vd,0);
  w2=vorbis_window(&vf2->vd,0);

  for(i=0;i<vi1->channels;i++)
	lappcm[i]=(float*) alloca(sizeof(**lappcm)*n1);

  _ov_getlap(vf1,vi1,&vf1->vd,lappcm,n1);

  vorbis_synthesis_lapout(&vf2->vd,&pcm);
  _analysis_output_always("pcmL",0,pcm[0],n1*2,0,0,0);
  _analysis_output_always("pcmR",0,pcm[1],n1*2,0,0,0);

  _ov_splice(pcm,lappcm,n1,n2,vi1->channels,vi2->channels,w1,w2);

  return(0);
}

static int _ov_64_seek_lap(OggVorbis_File *vf,ogg_int64_t pos,
			   int (*localseek)(OggVorbis_File *,ogg_int64_t)){
  vorbis_info *vi;
  float **lappcm;
  float **pcm;
  float *w1,*w2;
  int n1,n2,ch1,ch2,hs;
  int i,ret;

  if(vf->ready_state<OPENED)return(OV_EINVAL);
  ret=_ov_initset(vf);
  if(ret)return(ret);
  vi=ov_info(vf,-1);
  hs=ov_halfrate_p(vf);

  ch1=vi->channels;
  n1=vorbis_info_blocksize(vi,0)>>(1+hs);
  w1=vorbis_window(&vf->vd,0);  /* window arrays from libvorbis are
				   persistent; even if the decode state
				   from this link gets dumped, this
				   window array continues to exist */

  lappcm=(float**) alloca(sizeof(*lappcm)*ch1);
  for(i=0;i<ch1;i++)
	lappcm[i]=(float*) alloca(sizeof(**lappcm)*n1);
  _ov_getlap(vf,vi,&vf->vd,lappcm,n1);

  ret=localseek(vf,pos);
  if(ret)return ret;
  ret=_ov_initprime(vf);
  if(ret)return(ret);

  vi=ov_info(vf,-1);
  ch2=vi->channels;
  n2=vorbis_info_blocksize(vi,0)>>(1+hs);
  w2=vorbis_window(&vf->vd,0);

  vorbis_synthesis_lapout(&vf->vd,&pcm);

  _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2);

  return(0);
}

int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){
  return _ov_64_seek_lap(vf,pos,ov_raw_seek);
}

int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){
  return _ov_64_seek_lap(vf,pos,ov_pcm_seek);
}

int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos){
  return _ov_64_seek_lap(vf,pos,ov_pcm_seek_page);
}

static int _ov_d_seek_lap(OggVorbis_File *vf,double pos,
			   int (*localseek)(OggVorbis_File *,double)){
  vorbis_info *vi;
  float **lappcm;
  float **pcm;
  float *w1,*w2;
  int n1,n2,ch1,ch2,hs;
  int i,ret;

  if(vf->ready_state<OPENED)return(OV_EINVAL);
  ret=_ov_initset(vf);
  if(ret)return(ret);
  vi=ov_info(vf,-1);
  hs=ov_halfrate_p(vf);

  ch1=vi->channels;
  n1=vorbis_info_blocksize(vi,0)>>(1+hs);
  w1=vorbis_window(&vf->vd,0);  /* window arrays from libvorbis are
				   persistent; even if the decode state
				   from this link gets dumped, this
				   window array continues to exist */

  lappcm=(float**) alloca(sizeof(*lappcm)*ch1);
  for(i=0;i<ch1;i++)
	lappcm[i]=(float*) alloca(sizeof(**lappcm)*n1);
  _ov_getlap(vf,vi,&vf->vd,lappcm,n1);

  ret=localseek(vf,pos);
  if(ret)return ret;
  ret=_ov_initprime(vf);
  if(ret)return(ret);

  vi=ov_info(vf,-1);
  ch2=vi->channels;
  n2=vorbis_info_blocksize(vi,0)>>(1+hs);
  w2=vorbis_window(&vf->vd,0);

  vorbis_synthesis_lapout(&vf->vd,&pcm);

  _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2);

  return(0);
}

int ov_time_seek_lap(OggVorbis_File *vf,double pos){
  return _ov_d_seek_lap(vf,pos,ov_time_seek);
}

int ov_time_seek_page_lap(OggVorbis_File *vf,double pos){
  return _ov_d_seek_lap(vf,pos,ov_time_seek_page);
}

#endif
/*** End of inlined file: vorbisfile.c ***/


/*** Start of inlined file: window.c ***/

/*** Start of inlined file: juce_OggVorbisHeader.h ***/
// This file is included at the start of each Ogg-Vorbis .c file, just to do a few housekeeping
// tasks..

#if JUCE_MSVC
  #pragma warning (disable: 4267 4127 4244 4996 4100 4701 4702 4013 4133 4206 4305 4189 4706)
#endif
/*** End of inlined file: juce_OggVorbisHeader.h ***/

#if JUCE_USE_OGGVORBIS

#include <stdlib.h>
#include <math.h>

static float vwin64[32] = {
  0.0009460463F, 0.0085006468F, 0.0235352254F, 0.0458950567F,
  0.0753351908F, 0.1115073077F, 0.1539457973F, 0.2020557475F,
  0.2551056759F, 0.3122276645F, 0.3724270287F, 0.4346027792F,
  0.4975789974F, 0.5601459521F, 0.6211085051F, 0.6793382689F,
  0.7338252629F, 0.7837245849F, 0.8283939355F, 0.8674186656F,
  0.9006222429F, 0.9280614787F, 0.9500073081F, 0.9669131782F,
  0.9793740220F, 0.9880792941F, 0.9937636139F, 0.9971582668F,
  0.9989462667F, 0.9997230082F, 0.9999638688F, 0.9999995525F,
};

static float vwin128[64] = {
  0.0002365472F, 0.0021280687F, 0.0059065254F, 0.0115626550F,
  0.0190823442F, 0.0284463735F, 0.0396300935F, 0.0526030430F,
  0.0673285281F, 0.0837631763F, 0.1018564887F, 0.1215504095F,
  0.1427789367F, 0.1654677960F, 0.1895342001F, 0.2148867160F,
  0.2414252576F, 0.2690412240F, 0.2976177952F, 0.3270303960F,
  0.3571473350F, 0.3878306189F, 0.4189369387F, 0.4503188188F,
  0.4818259135F, 0.5133064334F, 0.5446086751F, 0.5755826278F,
  0.6060816248F, 0.6359640047F, 0.6650947483F, 0.6933470543F,
  0.7206038179F, 0.7467589810F, 0.7717187213F, 0.7954024542F,
  0.8177436264F, 0.8386902831F, 0.8582053981F, 0.8762669622F,
  0.8928678298F, 0.9080153310F, 0.9217306608F, 0.9340480615F,
  0.9450138200F, 0.9546851041F, 0.9631286621F, 0.9704194171F,
  0.9766389810F, 0.9818741197F, 0.9862151938F, 0.9897546035F,
  0.9925852598F, 0.9947991032F, 0.9964856900F, 0.9977308602F,
  0.9986155015F, 0.9992144193F, 0.9995953200F, 0.9998179155F,
  0.9999331503F, 0.9999825563F, 0.9999977357F, 0.9999999720F,
};

static float vwin256[128] = {
  0.0000591390F, 0.0005321979F, 0.0014780301F, 0.0028960636F,
  0.0047854363F, 0.0071449926F, 0.0099732775F, 0.0132685298F,
  0.0170286741F, 0.0212513119F, 0.0259337111F, 0.0310727950F,
  0.0366651302F, 0.0427069140F, 0.0491939614F, 0.0561216907F,
  0.0634851102F, 0.0712788035F, 0.0794969160F, 0.0881331402F,
  0.0971807028F, 0.1066323515F, 0.1164803426F, 0.1267164297F,
  0.1373318534F, 0.1483173323F, 0.1596630553F, 0.1713586755F,
  0.1833933062F, 0.1957555184F, 0.2084333404F, 0.2214142599F,
  0.2346852280F, 0.2482326664F, 0.2620424757F, 0.2761000481F,
  0.2903902813F, 0.3048975959F, 0.3196059553F, 0.3344988887F,
  0.3495595160F, 0.3647705766F, 0.3801144597F, 0.3955732382F,
  0.4111287047F, 0.4267624093F, 0.4424557009F, 0.4581897696F,
  0.4739456913F, 0.4897044744F, 0.5054471075F, 0.5211546088F,
  0.5368080763F, 0.5523887395F, 0.5678780103F, 0.5832575361F,
  0.5985092508F, 0.6136154277F, 0.6285587300F, 0.6433222619F,
  0.6578896175F, 0.6722449294F, 0.6863729144F, 0.7002589187F,
  0.7138889597F, 0.7272497662F, 0.7403288154F, 0.7531143679F,
  0.7655954985F, 0.7777621249F, 0.7896050322F, 0.8011158947F,
  0.8122872932F, 0.8231127294F, 0.8335866365F, 0.8437043850F,
  0.8534622861F, 0.8628575905F, 0.8718884835F, 0.8805540765F,
  0.8888543947F, 0.8967903616F, 0.9043637797F, 0.9115773078F,
  0.9184344360F, 0.9249394562F, 0.9310974312F, 0.9369141608F,
  0.9423961446F, 0.9475505439F, 0.9523851406F, 0.9569082947F,
  0.9611289005F, 0.9650563408F, 0.9687004405F, 0.9720714191F,
  0.9751798427F, 0.9780365753F, 0.9806527301F, 0.9830396204F,
  0.9852087111F, 0.9871715701F, 0.9889398207F, 0.9905250941F,
  0.9919389832F, 0.9931929973F, 0.9942985174F, 0.9952667537F,
  0.9961087037F, 0.9968351119F, 0.9974564312F, 0.9979827858F,
  0.9984239359F, 0.9987892441F, 0.9990876435F, 0.9993276081F,
  0.9995171241F, 0.9996636648F, 0.9997741654F, 0.9998550016F,
  0.9999119692F, 0.9999502656F, 0.9999744742F, 0.9999885497F,
  0.9999958064F, 0.9999989077F, 0.9999998584F, 0.9999999983F,
};

static float vwin512[256] = {
  0.0000147849F, 0.0001330607F, 0.0003695946F, 0.0007243509F,
  0.0011972759F, 0.0017882983F, 0.0024973285F, 0.0033242588F,
  0.0042689632F, 0.0053312973F, 0.0065110982F, 0.0078081841F,
  0.0092223540F, 0.0107533880F, 0.0124010466F, 0.0141650703F,
  0.0160451800F, 0.0180410758F, 0.0201524373F, 0.0223789233F,
  0.0247201710F, 0.0271757958F, 0.0297453914F, 0.0324285286F,
  0.0352247556F, 0.0381335972F, 0.0411545545F, 0.0442871045F,
  0.0475306997F, 0.0508847676F, 0.0543487103F, 0.0579219038F,
  0.0616036982F, 0.0653934164F, 0.0692903546F, 0.0732937809F,
  0.0774029356F, 0.0816170305F, 0.0859352485F, 0.0903567428F,
  0.0948806375F, 0.0995060259F, 0.1042319712F, 0.1090575056F,
  0.1139816300F, 0.1190033137F, 0.1241214941F, 0.1293350764F,
  0.1346429333F, 0.1400439046F, 0.1455367974F, 0.1511203852F,
  0.1567934083F, 0.1625545735F, 0.1684025537F, 0.1743359881F,
  0.1803534820F, 0.1864536069F, 0.1926349000F, 0.1988958650F,
  0.2052349715F, 0.2116506555F, 0.2181413191F, 0.2247053313F,
  0.2313410275F, 0.2380467105F, 0.2448206500F, 0.2516610835F,
  0.2585662164F, 0.2655342226F, 0.2725632448F, 0.2796513950F,
  0.2867967551F, 0.2939973773F, 0.3012512852F, 0.3085564739F,
  0.3159109111F, 0.3233125375F, 0.3307592680F, 0.3382489922F,
  0.3457795756F, 0.3533488602F, 0.3609546657F, 0.3685947904F,
  0.3762670121F, 0.3839690896F, 0.3916987634F, 0.3994537572F,
  0.4072317788F, 0.4150305215F, 0.4228476653F, 0.4306808783F,
  0.4385278181F, 0.4463861329F, 0.4542534630F, 0.4621274424F,
  0.4700057001F, 0.4778858615F, 0.4857655502F, 0.4936423891F,
  0.5015140023F, 0.5093780165F, 0.5172320626F, 0.5250737772F,
  0.5329008043F, 0.5407107971F, 0.5485014192F, 0.5562703465F,
  0.5640152688F, 0.5717338914F, 0.5794239366F, 0.5870831457F,
  0.5947092801F, 0.6023001235F, 0.6098534829F, 0.6173671907F,
  0.6248391059F, 0.6322671161F, 0.6396491384F, 0.6469831217F,
  0.6542670475F, 0.6614989319F, 0.6686768267F, 0.6757988210F,
  0.6828630426F, 0.6898676592F, 0.6968108799F, 0.7036909564F,
  0.7105061843F, 0.7172549043F, 0.7239355032F, 0.7305464154F,
  0.7370861235F, 0.7435531598F, 0.7499461068F, 0.7562635986F,
  0.7625043214F, 0.7686670148F, 0.7747504721F, 0.7807535410F,
  0.7866751247F, 0.7925141825F, 0.7982697296F, 0.8039408387F,
  0.8095266395F, 0.8150263196F, 0.8204391248F, 0.8257643590F,
  0.8310013848F, 0.8361496236F, 0.8412085555F, 0.8461777194F,
  0.8510567129F, 0.8558451924F, 0.8605428730F, 0.8651495278F,
  0.8696649882F, 0.8740891432F, 0.8784219392F, 0.8826633797F,
  0.8868135244F, 0.8908724888F, 0.8948404441F, 0.8987176157F,
  0.9025042831F, 0.9062007791F, 0.9098074886F, 0.9133248482F,
  0.9167533451F, 0.9200935163F, 0.9233459472F, 0.9265112712F,
  0.9295901680F, 0.9325833632F, 0.9354916263F, 0.9383157705F,
  0.9410566504F, 0.9437151618F, 0.9462922398F, 0.9487888576F,
  0.9512060252F, 0.9535447882F, 0.9558062262F, 0.9579914516F,
  0.9601016078F, 0.9621378683F, 0.9641014348F, 0.9659935361F,
  0.9678154261F, 0.9695683830F, 0.9712537071F, 0.9728727198F,
  0.9744267618F, 0.9759171916F, 0.9773453842F, 0.9787127293F,
  0.9800206298F, 0.9812705006F, 0.9824637665F, 0.9836018613F,
  0.9846862258F, 0.9857183066F, 0.9866995544F, 0.9876314227F,
  0.9885153662F, 0.9893528393F, 0.9901452948F, 0.9908941823F,
  0.9916009470F, 0.9922670279F, 0.9928938570F, 0.9934828574F,
  0.9940354423F, 0.9945530133F, 0.9950369595F, 0.9954886562F,
  0.9959094633F, 0.9963007242F, 0.9966637649F, 0.9969998925F,
  0.9973103939F, 0.9975965351F, 0.9978595598F, 0.9981006885F,
  0.9983211172F, 0.9985220166F, 0.9987045311F, 0.9988697776F,
  0.9990188449F, 0.9991527924F, 0.9992726499F, 0.9993794157F,
  0.9994740570F, 0.9995575079F, 0.9996306699F, 0.9996944099F,
  0.9997495605F, 0.9997969190F, 0.9998372465F, 0.9998712678F,
  0.9998996704F, 0.9999231041F, 0.9999421807F, 0.9999574732F,
  0.9999695157F, 0.9999788026F, 0.9999857885F, 0.9999908879F,
  0.9999944746F, 0.9999968817F, 0.9999984010F, 0.9999992833F,
  0.9999997377F, 0.9999999317F, 0.9999999911F, 0.9999999999F,
};

static float vwin1024[512] = {
  0.0000036962F, 0.0000332659F, 0.0000924041F, 0.0001811086F,
  0.0002993761F, 0.0004472021F, 0.0006245811F, 0.0008315063F,
  0.0010679699F, 0.0013339631F, 0.0016294757F, 0.0019544965F,
  0.0023090133F, 0.0026930125F, 0.0031064797F, 0.0035493989F,
  0.0040217533F, 0.0045235250F, 0.0050546946F, 0.0056152418F,
  0.0062051451F, 0.0068243817F, 0.0074729278F, 0.0081507582F,
  0.0088578466F, 0.0095941655F, 0.0103596863F, 0.0111543789F,
  0.0119782122F, 0.0128311538F, 0.0137131701F, 0.0146242260F,
  0.0155642855F, 0.0165333111F, 0.0175312640F, 0.0185581042F,
  0.0196137903F, 0.0206982797F, 0.0218115284F, 0.0229534910F,
  0.0241241208F, 0.0253233698F, 0.0265511886F, 0.0278075263F,
  0.0290923308F, 0.0304055484F, 0.0317471241F, 0.0331170013F,
  0.0345151222F, 0.0359414274F, 0.0373958560F, 0.0388783456F,
  0.0403888325F, 0.0419272511F, 0.0434935347F, 0.0450876148F,
  0.0467094213F, 0.0483588828F, 0.0500359261F, 0.0517404765F,
  0.0534724575F, 0.0552317913F, 0.0570183983F, 0.0588321971F,
  0.0606731048F, 0.0625410369F, 0.0644359070F, 0.0663576272F,
  0.0683061077F, 0.0702812571F, 0.0722829821F, 0.0743111878F,
  0.0763657775F, 0.0784466526F, 0.0805537129F, 0.0826868561F,
  0.0848459782F, 0.0870309736F, 0.0892417345F, 0.0914781514F,
  0.0937401128F, 0.0960275056F, 0.0983402145F, 0.1006781223F,
  0.1030411101F, 0.1054290568F, 0.1078418397F, 0.1102793336F,
  0.1127414119F, 0.1152279457F, 0.1177388042F, 0.1202738544F,
  0.1228329618F, 0.1254159892F, 0.1280227980F, 0.1306532471F,
  0.1333071937F, 0.1359844927F, 0.1386849970F, 0.1414085575F,
  0.1441550230F, 0.1469242403F, 0.1497160539F, 0.1525303063F,
  0.1553668381F, 0.1582254875F, 0.1611060909F, 0.1640084822F,
  0.1669324936F, 0.1698779549F, 0.1728446939F, 0.1758325362F,
  0.1788413055F, 0.1818708232F, 0.1849209084F, 0.1879913785F,
  0.1910820485F, 0.1941927312F, 0.1973232376F, 0.2004733764F,
  0.2036429541F, 0.2068317752F, 0.2100396421F, 0.2132663552F,
  0.2165117125F, 0.2197755102F, 0.2230575422F, 0.2263576007F,
  0.2296754753F, 0.2330109540F, 0.2363638225F, 0.2397338646F,
  0.2431208619F, 0.2465245941F, 0.2499448389F, 0.2533813719F,
  0.2568339669F, 0.2603023956F, 0.2637864277F, 0.2672858312F,
  0.2708003718F, 0.2743298135F, 0.2778739186F, 0.2814324472F,
  0.2850051576F, 0.2885918065F, 0.2921921485F, 0.2958059366F,
  0.2994329219F, 0.3030728538F, 0.3067254799F, 0.3103905462F,
  0.3140677969F, 0.3177569747F, 0.3214578205F, 0.3251700736F,
  0.3288934718F, 0.3326277513F, 0.3363726468F, 0.3401278914F,
  0.3438932168F, 0.3476683533F, 0.3514530297F, 0.3552469734F,
  0.3590499106F, 0.3628615659F, 0.3666816630F, 0.3705099239F,
  0.3743460698F, 0.3781898204F, 0.3820408945F, 0.3858990095F,
  0.3897638820F, 0.3936352274F, 0.3975127601F, 0.4013961936F,
  0.4052852405F, 0.4091796123F, 0.4130790198F, 0.4169831732F,
  0.4208917815F, 0.4248045534F, 0.4287211965F, 0.4326414181F,
  0.4365649248F, 0.4404914225F, 0.4444206167F, 0.4483522125F,
  0.4522859146F, 0.4562214270F, 0.4601584538F, 0.4640966984F,
  0.4680358644F, 0.4719756548F, 0.4759157726F, 0.4798559209F,
  0.4837958024F, 0.4877351199F, 0.4916735765F, 0.4956108751F,
  0.4995467188F, 0.5034808109F, 0.5074128550F, 0.5113425550F,
  0.5152696149F, 0.5191937395F, 0.5231146336F, 0.5270320028F,
  0.5309455530F, 0.5348549910F, 0.5387600239F, 0.5426603597F,
  0.5465557070F, 0.5504457754F, 0.5543302752F, 0.5582089175F,
  0.5620814145F, 0.5659474793F, 0.5698068262F, 0.5736591704F,
  0.5775042283F, 0.5813417176F, 0.5851713571F, 0.5889928670F,
  0.5928059689F, 0.5966103856F, 0.6004058415F, 0.6041920626F,
  0.6079687761F, 0.6117357113F, 0.6154925986F, 0.6192391705F,
  0.6229751612F, 0.6267003064F, 0.6304143441F, 0.6341170137F,
  0.6378080569F, 0.6414872173F, 0.6451542405F, 0.6488088741F,
  0.6524508681F, 0.6560799742F, 0.6596959469F, 0.6632985424F,
  0.6668875197F, 0.6704626398F, 0.6740236662F, 0.6775703649F,
  0.6811025043F, 0.6846198554F, 0.6881221916F, 0.6916092892F,
  0.6950809269F, 0.6985368861F, 0.7019769510F, 0.7054009085F,
  0.7088085484F, 0.7121996632F, 0.7155740484F, 0.7189315023F,
  0.7222718263F, 0.7255948245F, 0.7289003043F, 0.7321880760F,
  0.7354579530F, 0.7387097518F, 0.7419432921F, 0.7451583966F,
  0.7483548915F, 0.7515326059F, 0.7546913723F, 0.7578310265F,
  0.7609514077F, 0.7640523581F, 0.7671337237F, 0.7701953535F,
  0.7732371001F, 0.7762588195F, 0.7792603711F, 0.7822416178F,
  0.7852024259F, 0.7881426654F, 0.7910622097F, 0.7939609356F,
  0.7968387237F, 0.7996954579F, 0.8025310261F, 0.8053453193F,
  0.8081382324F, 0.8109096638F, 0.8136595156F, 0.8163876936F,
  0.8190941071F, 0.8217786690F, 0.8244412960F, 0.8270819086F,
  0.8297004305F, 0.8322967896F, 0.8348709171F, 0.8374227481F,
  0.8399522213F, 0.8424592789F, 0.8449438672F, 0.8474059356F,
  0.8498454378F, 0.8522623306F, 0.8546565748F, 0.8570281348F,
  0.8593769787F, 0.8617030779F, 0.8640064080F, 0.8662869477F,
  0.8685446796F, 0.8707795899F, 0.8729916682F, 0.8751809079F,
  0.8773473059F, 0.8794908626F, 0.8816115819F, 0.8837094713F,
  0.8857845418F, 0.8878368079F, 0.8898662874F, 0.8918730019F,
  0.8938569760F, 0.8958182380F, 0.8977568194F, 0.8996727552F,
  0.9015660837F, 0.9034368465F, 0.9052850885F, 0.9071108577F,
  0.9089142057F, 0.9106951869F, 0.9124538591F, 0.9141902832F,
  0.9159045233F, 0.9175966464F, 0.9192667228F, 0.9209148257F,
  0.9225410313F, 0.9241454187F, 0.9257280701F, 0.9272890704F,
  0.9288285075F, 0.9303464720F, 0.9318430576F, 0.9333183603F,
  0.9347724792F, 0.9362055158F, 0.9376175745F, 0.9390087622F,
  0.9403791881F, 0.9417289644F, 0.9430582055F, 0.9443670283F,
  0.9456555521F, 0.9469238986F, 0.9481721917F, 0.9494005577F,
  0.9506091252F, 0.9517980248F, 0.9529673894F, 0.9541173540F,
  0.9552480557F, 0.9563596334F, 0.9574522282F, 0.9585259830F,
  0.9595810428F, 0.9606175542F, 0.9616356656F, 0.9626355274F,
  0.9636172915F, 0.9645811114F, 0.9655271425F, 0.9664555414F,
  0.9673664664F, 0.9682600774F, 0.9691365355F, 0.9699960034F,
  0.9708386448F, 0.9716646250F, 0.9724741103F, 0.9732672685F,
  0.9740442683F, 0.9748052795F, 0.9755504729F, 0.9762800205F,
  0.9769940950F, 0.9776928703F, 0.9783765210F, 0.9790452223F,
  0.9796991504F, 0.9803384823F, 0.9809633954F, 0.9815740679F,
  0.9821706784F, 0.9827534063F, 0.9833224312F, 0.9838779332F,
  0.9844200928F, 0.9849490910F, 0.9854651087F, 0.9859683274F,
  0.9864589286F, 0.9869370940F, 0.9874030054F, 0.9878568447F,
  0.9882987937F, 0.9887290343F, 0.9891477481F, 0.9895551169F,
  0.9899513220F, 0.9903365446F, 0.9907109658F, 0.9910747662F,
  0.9914281260F, 0.9917712252F, 0.9921042433F, 0.9924273593F,
  0.9927407516F, 0.9930445982F, 0.9933390763F, 0.9936243626F,
  0.9939006331F, 0.9941680631F, 0.9944268269F, 0.9946770982F,
  0.9949190498F, 0.9951528537F, 0.9953786808F, 0.9955967011F,
  0.9958070836F, 0.9960099963F, 0.9962056061F, 0.9963940787F,
  0.9965755786F, 0.9967502693F, 0.9969183129F, 0.9970798704F,
  0.9972351013F, 0.9973841640F, 0.9975272151F, 0.9976644103F,
  0.9977959036F, 0.9979218476F, 0.9980423932F, 0.9981576901F,
  0.9982678862F, 0.9983731278F, 0.9984735596F, 0.9985693247F,
  0.9986605645F, 0.9987474186F, 0.9988300248F, 0.9989085193F,
  0.9989830364F, 0.9990537085F, 0.9991206662F, 0.9991840382F,
  0.9992439513F, 0.9993005303F, 0.9993538982F, 0.9994041757F,
  0.9994514817F, 0.9994959330F, 0.9995376444F, 0.9995767286F,
  0.9996132960F, 0.9996474550F, 0.9996793121F, 0.9997089710F,
  0.9997365339F, 0.9997621003F, 0.9997857677F, 0.9998076311F,
  0.9998277836F, 0.9998463156F, 0.9998633155F, 0.9998788692F,
  0.9998930603F, 0.9999059701F, 0.9999176774F, 0.9999282586F,
  0.9999377880F, 0.9999463370F, 0.9999539749F, 0.9999607685F,
  0.9999667820F, 0.9999720773F, 0.9999767136F, 0.9999807479F,
  0.9999842344F, 0.9999872249F, 0.9999897688F, 0.9999919127F,
  0.9999937009F, 0.9999951749F, 0.9999963738F, 0.9999973342F,
  0.9999980900F, 0.9999986724F, 0.9999991103F, 0.9999994297F,
  0.9999996543F, 0.9999998049F, 0.9999999000F, 0.9999999552F,
  0.9999999836F, 0.9999999957F, 0.9999999994F, 1.0000000000F,
};

static float vwin2048[1024] = {
  0.0000009241F, 0.0000083165F, 0.0000231014F, 0.0000452785F,
  0.0000748476F, 0.0001118085F, 0.0001561608F, 0.0002079041F,
  0.0002670379F, 0.0003335617F, 0.0004074748F, 0.0004887765F,
  0.0005774661F, 0.0006735427F, 0.0007770054F, 0.0008878533F,
  0.0010060853F, 0.0011317002F, 0.0012646969F, 0.0014050742F,
  0.0015528307F, 0.0017079650F, 0.0018704756F, 0.0020403610F,
  0.0022176196F, 0.0024022497F, 0.0025942495F, 0.0027936173F,
  0.0030003511F, 0.0032144490F, 0.0034359088F, 0.0036647286F,
  0.0039009061F, 0.0041444391F, 0.0043953253F, 0.0046535621F,
  0.0049191472F, 0.0051920781F, 0.0054723520F, 0.0057599664F,
  0.0060549184F, 0.0063572052F, 0.0066668239F, 0.0069837715F,
  0.0073080449F, 0.0076396410F, 0.0079785566F, 0.0083247884F,
  0.0086783330F, 0.0090391871F, 0.0094073470F, 0.0097828092F,
  0.0101655700F, 0.0105556258F, 0.0109529726F, 0.0113576065F,
  0.0117695237F, 0.0121887200F, 0.0126151913F, 0.0130489335F,
  0.0134899422F, 0.0139382130F, 0.0143937415F, 0.0148565233F,
  0.0153265536F, 0.0158038279F, 0.0162883413F, 0.0167800889F,
  0.0172790660F, 0.0177852675F, 0.0182986882F, 0.0188193231F,
  0.0193471668F, 0.0198822141F, 0.0204244594F, 0.0209738974F,
  0.0215305225F, 0.0220943289F, 0.0226653109F, 0.0232434627F,
  0.0238287784F, 0.0244212519F, 0.0250208772F, 0.0256276481F,
  0.0262415582F, 0.0268626014F, 0.0274907711F, 0.0281260608F,
  0.0287684638F, 0.0294179736F, 0.0300745833F, 0.0307382859F,
  0.0314090747F, 0.0320869424F, 0.0327718819F, 0.0334638860F,
  0.0341629474F, 0.0348690586F, 0.0355822122F, 0.0363024004F,
  0.0370296157F, 0.0377638502F, 0.0385050960F, 0.0392533451F,
  0.0400085896F, 0.0407708211F, 0.0415400315F, 0.0423162123F,
  0.0430993552F, 0.0438894515F, 0.0446864926F, 0.0454904698F,
  0.0463013742F, 0.0471191969F, 0.0479439288F, 0.0487755607F,
  0.0496140836F, 0.0504594879F, 0.0513117642F, 0.0521709031F,
  0.0530368949F, 0.0539097297F, 0.0547893979F, 0.0556758894F,
  0.0565691941F, 0.0574693019F, 0.0583762026F, 0.0592898858F,
  0.0602103410F, 0.0611375576F, 0.0620715250F, 0.0630122324F,
  0.0639596688F, 0.0649138234F, 0.0658746848F, 0.0668422421F,
  0.0678164838F, 0.0687973985F, 0.0697849746F, 0.0707792005F,
  0.0717800645F, 0.0727875547F, 0.0738016591F, 0.0748223656F,
  0.0758496620F, 0.0768835359F, 0.0779239751F, 0.0789709668F,
  0.0800244985F, 0.0810845574F, 0.0821511306F, 0.0832242052F,
  0.0843037679F, 0.0853898056F, 0.0864823050F, 0.0875812525F,
  0.0886866347F, 0.0897984378F, 0.0909166480F, 0.0920412513F,
  0.0931722338F, 0.0943095813F, 0.0954532795F, 0.0966033140F,
  0.0977596702F, 0.0989223336F, 0.1000912894F, 0.1012665227F,
  0.1024480185F, 0.1036357616F, 0.1048297369F, 0.1060299290F,
  0.1072363224F, 0.1084489014F, 0.1096676504F, 0.1108925534F,
  0.1121235946F, 0.1133607577F, 0.1146040267F, 0.1158533850F,
  0.1171088163F, 0.1183703040F, 0.1196378312F, 0.1209113812F,
  0.1221909370F, 0.1234764815F, 0.1247679974F, 0.1260654674F,
  0.1273688740F, 0.1286781995F, 0.1299934263F, 0.1313145365F,
  0.1326415121F, 0.1339743349F, 0.1353129866F, 0.1366574490F,
  0.1380077035F, 0.1393637315F, 0.1407255141F, 0.1420930325F,
  0.1434662677F, 0.1448452004F, 0.1462298115F, 0.1476200814F,
  0.1490159906F, 0.1504175195F, 0.1518246482F, 0.1532373569F,
  0.1546556253F, 0.1560794333F, 0.1575087606F, 0.1589435866F,
  0.1603838909F, 0.1618296526F, 0.1632808509F, 0.1647374648F,
  0.1661994731F, 0.1676668546F, 0.1691395880F, 0.1706176516F,
  0.1721010238F, 0.1735896829F, 0.1750836068F, 0.1765827736F,
  0.1780871610F, 0.1795967468F, 0.1811115084F, 0.1826314234F,
  0.1841564689F, 0.1856866221F, 0.1872218600F, 0.1887621595F,
  0.1903074974F, 0.1918578503F, 0.1934131947F, 0.1949735068F,
  0.1965387630F, 0.1981089393F, 0.1996840117F, 0.2012639560F,
  0.2028487479F, 0.2044383630F, 0.2060327766F, 0.2076319642F,
  0.2092359007F, 0.2108445614F, 0.2124579211F, 0.2140759545F,
  0.2156986364F, 0.2173259411F, 0.2189578432F, 0.2205943168F,
  0.2222353361F, 0.2238808751F, 0.2255309076F, 0.2271854073F,
  0.2288443480F, 0.2305077030F, 0.2321754457F, 0.2338475493F,
  0.2355239869F, 0.2372047315F, 0.2388897560F, 0.2405790329F,
  0.2422725350F, 0.2439702347F, 0.2456721043F, 0.2473781159F,
  0.2490882418F, 0.2508024539F, 0.2525207240F, 0.2542430237F,
  0.2559693248F, 0.2576995986F, 0.2594338166F, 0.2611719498F,
  0.2629139695F, 0.2646598466F, 0.2664095520F, 0.2681630564F,
  0.2699203304F, 0.2716813445F, 0.2734460691F, 0.2752144744F,
  0.2769865307F, 0.2787622079F, 0.2805414760F, 0.2823243047F,
  0.2841106637F, 0.2859005227F, 0.2876938509F, 0.2894906179F,
  0.2912907928F, 0.2930943447F, 0.2949012426F, 0.2967114554F,
  0.2985249520F, 0.3003417009F, 0.3021616708F, 0.3039848301F,
  0.3058111471F, 0.3076405901F, 0.3094731273F, 0.3113087266F,
  0.3131473560F, 0.3149889833F, 0.3168335762F, 0.3186811024F,
  0.3205315294F, 0.3223848245F, 0.3242409552F, 0.3260998886F,
  0.3279615918F, 0.3298260319F, 0.3316931758F, 0.3335629903F,
  0.3354354423F, 0.3373104982F, 0.3391881247F, 0.3410682882F,
  0.3429509551F, 0.3448360917F, 0.3467236642F, 0.3486136387F,
  0.3505059811F, 0.3524006575F, 0.3542976336F, 0.3561968753F,
  0.3580983482F, 0.3600020179F, 0.3619078499F, 0.3638158096F,
  0.3657258625F, 0.3676379737F, 0.3695521086F, 0.3714682321F,
  0.3733863094F, 0.3753063055F, 0.3772281852F, 0.3791519134F,
  0.3810774548F, 0.3830047742F, 0.3849338362F, 0.3868646053F,
  0.3887970459F, 0.3907311227F, 0.3926667998F, 0.3946040417F,
  0.3965428125F, 0.3984830765F, 0.4004247978F, 0.4023679403F,
  0.4043124683F, 0.4062583455F, 0.4082055359F, 0.4101540034F,
  0.4121037117F, 0.4140546246F, 0.4160067058F, 0.4179599190F,
  0.4199142277F, 0.4218695956F, 0.4238259861F, 0.4257833627F,
  0.4277416888F, 0.4297009279F, 0.4316610433F, 0.4336219983F,
  0.4355837562F, 0.4375462803F, 0.4395095337F, 0.4414734797F,
  0.4434380815F, 0.4454033021F, 0.4473691046F, 0.4493354521F,
  0.4513023078F, 0.4532696345F, 0.4552373954F, 0.4572055533F,
  0.4591740713F, 0.4611429123F, 0.4631120393F, 0.4650814151F,
  0.4670510028F, 0.4690207650F, 0.4709906649F, 0.4729606651F,
  0.4749307287F, 0.4769008185F, 0.4788708972F, 0.4808409279F,
  0.4828108732F, 0.4847806962F, 0.4867503597F, 0.4887198264F,
  0.4906890593F, 0.4926580213F, 0.4946266753F, 0.4965949840F,
  0.4985629105F, 0.5005304176F, 0.5024974683F, 0.5044640255F,
  0.5064300522F, 0.5083955114F, 0.5103603659F, 0.5123245790F,
  0.5142881136F, 0.5162509328F, 0.5182129997F, 0.5201742774F,
  0.5221347290F, 0.5240943178F, 0.5260530070F, 0.5280107598F,
  0.5299675395F, 0.5319233095F, 0.5338780330F, 0.5358316736F,
  0.5377841946F, 0.5397355596F, 0.5416857320F, 0.5436346755F,
  0.5455823538F, 0.5475287304F, 0.5494737691F, 0.5514174337F,
  0.5533596881F, 0.5553004962F, 0.5572398218F, 0.5591776291F,
  0.5611138821F, 0.5630485449F, 0.5649815818F, 0.5669129570F,
  0.5688426349F, 0.5707705799F, 0.5726967564F, 0.5746211290F,
  0.5765436624F, 0.5784643212F, 0.5803830702F, 0.5822998743F,
  0.5842146984F, 0.5861275076F, 0.5880382669F, 0.5899469416F,
  0.5918534968F, 0.5937578981F, 0.5956601107F, 0.5975601004F,
  0.5994578326F, 0.6013532732F, 0.6032463880F, 0.6051371429F,
  0.6070255039F, 0.6089114372F, 0.6107949090F, 0.6126758856F,
  0.6145543334F, 0.6164302191F, 0.6183035092F, 0.6201741706F,
  0.6220421700F, 0.6239074745F, 0.6257700513F, 0.6276298674F,
  0.6294868903F, 0.6313410873F, 0.6331924262F, 0.6350408745F,
  0.6368864001F, 0.6387289710F, 0.6405685552F, 0.6424051209F,
  0.6442386364F, 0.6460690702F, 0.6478963910F, 0.6497205673F,
  0.6515415682F, 0.6533593625F, 0.6551739194F, 0.6569852082F,
  0.6587931984F, 0.6605978593F, 0.6623991609F, 0.6641970728F,
  0.6659915652F, 0.6677826081F, 0.6695701718F, 0.6713542268F,
  0.6731347437F, 0.6749116932F, 0.6766850461F, 0.6784547736F,
  0.6802208469F, 0.6819832374F, 0.6837419164F, 0.6854968559F,
  0.6872480275F, 0.6889954034F, 0.6907389556F, 0.6924786566F,
  0.6942144788F, 0.6959463950F, 0.6976743780F, 0.6993984008F,
  0.7011184365F, 0.7028344587F, 0.7045464407F, 0.7062543564F,
  0.7079581796F, 0.7096578844F, 0.7113534450F, 0.7130448359F,
  0.7147320316F, 0.7164150070F, 0.7180937371F, 0.7197681970F,
  0.7214383620F, 0.7231042077F, 0.7247657098F, 0.7264228443F,
  0.7280755871F, 0.7297239147F, 0.7313678035F, 0.7330072301F,
  0.7346421715F, 0.7362726046F, 0.7378985069F, 0.7395198556F,
  0.7411366285F, 0.7427488034F, 0.7443563584F, 0.7459592717F,
  0.7475575218F, 0.7491510873F, 0.7507399471F, 0.7523240803F,
  0.7539034661F, 0.7554780839F, 0.7570479136F, 0.7586129349F,
  0.7601731279F, 0.7617284730F, 0.7632789506F, 0.7648245416F,
  0.7663652267F, 0.7679009872F, 0.7694318044F, 0.7709576599F,
  0.7724785354F, 0.7739944130F, 0.7755052749F, 0.7770111035F,
  0.7785118815F, 0.7800075916F, 0.7814982170F, 0.7829837410F,
  0.7844641472F, 0.7859394191F, 0.7874095408F, 0.7888744965F,
  0.7903342706F, 0.7917888476F, 0.7932382124F, 0.7946823501F,
  0.7961212460F, 0.7975548855F, 0.7989832544F, 0.8004063386F,
  0.8018241244F, 0.8032365981F, 0.8046437463F, 0.8060455560F,
  0.8074420141F, 0.8088331080F, 0.8102188253F, 0.8115991536F,
  0.8129740810F, 0.8143435957F, 0.8157076861F, 0.8170663409F,
  0.8184195489F, 0.8197672994F, 0.8211095817F, 0.8224463853F,
  0.8237777001F, 0.8251035161F, 0.8264238235F, 0.8277386129F,
  0.8290478750F, 0.8303516008F, 0.8316497814F, 0.8329424083F,
  0.8342294731F, 0.8355109677F, 0.8367868841F, 0.8380572148F,
  0.8393219523F, 0.8405810893F, 0.8418346190F, 0.8430825345F,
  0.8443248294F, 0.8455614974F, 0.8467925323F, 0.8480179285F,
  0.8492376802F, 0.8504517822F, 0.8516602292F, 0.8528630164F,
  0.8540601391F, 0.8552515928F, 0.8564373733F, 0.8576174766F,
  0.8587918990F, 0.8599606368F, 0.8611236868F, 0.8622810460F,
  0.8634327113F, 0.8645786802F, 0.8657189504F, 0.8668535195F,
  0.8679823857F, 0.8691055472F, 0.8702230025F, 0.8713347503F,
  0.8724407896F, 0.8735411194F, 0.8746357394F, 0.8757246489F,
  0.8768078479F, 0.8778853364F, 0.8789571146F, 0.8800231832F,
  0.8810835427F, 0.8821381942F, 0.8831871387F, 0.8842303777F,
  0.8852679127F, 0.8862997456F, 0.8873258784F, 0.8883463132F,
  0.8893610527F, 0.8903700994F, 0.8913734562F, 0.8923711263F,
  0.8933631129F, 0.8943494196F, 0.8953300500F, 0.8963050083F,
  0.8972742985F, 0.8982379249F, 0.8991958922F, 0.9001482052F,
  0.9010948688F, 0.9020358883F, 0.9029712690F, 0.9039010165F,
  0.9048251367F, 0.9057436357F, 0.9066565195F, 0.9075637946F,
  0.9084654678F, 0.9093615456F, 0.9102520353F, 0.9111369440F,
  0.9120162792F, 0.9128900484F, 0.9137582595F, 0.9146209204F,
  0.9154780394F, 0.9163296248F, 0.9171756853F, 0.9180162296F,
  0.9188512667F, 0.9196808057F, 0.9205048559F, 0.9213234270F,
  0.9221365285F, 0.9229441704F, 0.9237463629F, 0.9245431160F,
  0.9253344404F, 0.9261203465F, 0.9269008453F, 0.9276759477F,
  0.9284456648F, 0.9292100080F, 0.9299689889F, 0.9307226190F,
  0.9314709103F, 0.9322138747F, 0.9329515245F, 0.9336838721F,
  0.9344109300F, 0.9351327108F, 0.9358492275F, 0.9365604931F,
  0.9372665208F, 0.9379673239F, 0.9386629160F, 0.9393533107F,
  0.9400385220F, 0.9407185637F, 0.9413934501F, 0.9420631954F,
  0.9427278141F, 0.9433873208F, 0.9440417304F, 0.9446910576F,
  0.9453353176F, 0.9459745255F, 0.9466086968F, 0.9472378469F,
  0.9478619915F, 0.9484811463F, 0.9490953274F, 0.9497045506F,
  0.9503088323F, 0.9509081888F, 0.9515026365F, 0.9520921921F,
  0.9526768723F, 0.9532566940F, 0.9538316742F, 0.9544018300F,
  0.9549671786F, 0.9555277375F, 0.9560835241F, 0.9566345562F,
  0.9571808513F, 0.9577224275F, 0.9582593027F, 0.9587914949F,
  0.9593190225F, 0.9598419038F, 0.9603601571F, 0.9608738012F,
  0.9613828546F, 0.9618873361F, 0.9623872646F, 0.9628826591F,
  0.9633735388F, 0.9638599227F, 0.9643418303F, 0.9648192808F,
  0.9652922939F, 0.9657608890F, 0.9662250860F, 0.9666849046F,
  0.9671403646F, 0.9675914861F, 0.9680382891F, 0.9684807937F,
  0.9689190202F, 0.9693529890F, 0.9697827203F, 0.9702082347F,
  0.9706295529F, 0.9710466953F, 0.9714596828F, 0.9718685362F,
  0.9722732762F, 0.9726739240F, 0.9730705005F, 0.9734630267F,
  0.9738515239F, 0.9742360134F, 0.9746165163F, 0.9749930540F,
  0.9753656481F, 0.9757343198F, 0.9760990909F, 0.9764599829F,
  0.9768170175F, 0.9771702164F, 0.9775196013F, 0.9778651941F,
  0.9782070167F, 0.9785450909F, 0.9788794388F, 0.9792100824F,
  0.9795370437F, 0.9798603449F, 0.9801800080F, 0.9804960554F,
  0.9808085092F, 0.9811173916F, 0.9814227251F, 0.9817245318F,
  0.9820228343F, 0.9823176549F, 0.9826090160F, 0.9828969402F,
  0.9831814498F, 0.9834625674F, 0.9837403156F, 0.9840147169F,
  0.9842857939F, 0.9845535692F, 0.9848180654F, 0.9850793052F,
  0.9853373113F, 0.9855921062F, 0.9858437127F, 0.9860921535F,
  0.9863374512F, 0.9865796287F, 0.9868187085F, 0.9870547136F,
  0.9872876664F, 0.9875175899F, 0.9877445067F, 0.9879684396F,
  0.9881894112F, 0.9884074444F, 0.9886225619F, 0.9888347863F,
  0.9890441404F, 0.9892506468F, 0.9894543284F, 0.9896552077F,
  0.9898533074F, 0.9900486502F, 0.9902412587F, 0.9904311555F,
  0.9906183633F, 0.9908029045F, 0.9909848019F, 0.9911640779F,
  0.9913407550F, 0.9915148557F, 0.9916864025F, 0.9918554179F,
  0.9920219241F, 0.9921859437F, 0.9923474989F, 0.9925066120F,
  0.9926633054F, 0.9928176012F, 0.9929695218F, 0.9931190891F,
  0.9932663254F, 0.9934112527F, 0.9935538932F, 0.9936942686F,
  0.9938324012F, 0.9939683126F, 0.9941020248F, 0.9942335597F,
  0.9943629388F, 0.9944901841F, 0.9946153170F, 0.9947383593F,
  0.9948593325F, 0.9949782579F, 0.9950951572F, 0.9952100516F,
  0.9953229625F, 0.9954339111F, 0.9955429186F, 0.9956500062F,
  0.9957551948F, 0.9958585056F, 0.9959599593F, 0.9960595769F,
  0.9961573792F, 0.9962533869F, 0.9963476206F, 0.9964401009F,
  0.9965308483F, 0.9966198833F, 0.9967072261F, 0.9967928971F,
  0.9968769164F, 0.9969593041F, 0.9970400804F, 0.9971192651F,
  0.9971968781F, 0.9972729391F, 0.9973474680F, 0.9974204842F,
  0.9974920074F, 0.9975620569F, 0.9976306521F, 0.9976978122F,
  0.9977635565F, 0.9978279039F, 0.9978908736F, 0.9979524842F,
  0.9980127547F, 0.9980717037F, 0.9981293499F, 0.9981857116F,
  0.9982408073F, 0.9982946554F, 0.9983472739F, 0.9983986810F,
  0.9984488947F, 0.9984979328F, 0.9985458132F, 0.9985925534F,
  0.9986381711F, 0.9986826838F, 0.9987261086F, 0.9987684630F,
  0.9988097640F, 0.9988500286F, 0.9988892738F, 0.9989275163F,
  0.9989647727F, 0.9990010597F, 0.9990363938F, 0.9990707911F,
  0.9991042679F, 0.9991368404F, 0.9991685244F, 0.9991993358F,
  0.9992292905F, 0.9992584038F, 0.9992866914F, 0.9993141686F,
  0.9993408506F, 0.9993667526F, 0.9993918895F, 0.9994162761F,
  0.9994399273F, 0.9994628576F, 0.9994850815F, 0.9995066133F,
  0.9995274672F, 0.9995476574F, 0.9995671978F, 0.9995861021F,
  0.9996043841F, 0.9996220573F, 0.9996391352F, 0.9996556310F,
  0.9996715579F, 0.9996869288F, 0.9997017568F, 0.9997160543F,
  0.9997298342F, 0.9997431088F, 0.9997558905F, 0.9997681914F,
  0.9997800236F, 0.9997913990F, 0.9998023292F, 0.9998128261F,
  0.9998229009F, 0.9998325650F, 0.9998418296F, 0.9998507058F,
  0.9998592044F, 0.9998673362F, 0.9998751117F, 0.9998825415F,
  0.9998896358F, 0.9998964047F, 0.9999028584F, 0.9999090066F,
  0.9999148590F, 0.9999204253F, 0.9999257148F, 0.9999307368F,
  0.9999355003F, 0.9999400144F, 0.9999442878F, 0.9999483293F,
  0.9999521472F, 0.9999557499F, 0.9999591457F, 0.9999623426F,
  0.9999653483F, 0.9999681708F, 0.9999708175F, 0.9999732959F,
  0.9999756132F, 0.9999777765F, 0.9999797928F, 0.9999816688F,
  0.9999834113F, 0.9999850266F, 0.9999865211F, 0.9999879009F,
  0.9999891721F, 0.9999903405F, 0.9999914118F, 0.9999923914F,
  0.9999932849F, 0.9999940972F, 0.9999948336F, 0.9999954989F,
  0.9999960978F, 0.9999966349F, 0.9999971146F, 0.9999975411F,
  0.9999979185F, 0.9999982507F, 0.9999985414F, 0.9999987944F,
  0.9999990129F, 0.9999992003F, 0.9999993596F, 0.9999994939F,
  0.9999996059F, 0.9999996981F, 0.9999997732F, 0.9999998333F,
  0.9999998805F, 0.9999999170F, 0.9999999444F, 0.9999999643F,
  0.9999999784F, 0.9999999878F, 0.9999999937F, 0.9999999972F,
  0.9999999990F, 0.9999999997F, 1.0000000000F, 1.0000000000F,
};

static float vwin4096[2048] = {
  0.0000002310F, 0.0000020791F, 0.0000057754F, 0.0000113197F,
  0.0000187121F, 0.0000279526F, 0.0000390412F, 0.0000519777F,
  0.0000667623F, 0.0000833949F, 0.0001018753F, 0.0001222036F,
  0.0001443798F, 0.0001684037F, 0.0001942754F, 0.0002219947F,
  0.0002515616F, 0.0002829761F, 0.0003162380F, 0.0003513472F,
  0.0003883038F, 0.0004271076F, 0.0004677584F, 0.0005102563F,
  0.0005546011F, 0.0006007928F, 0.0006488311F, 0.0006987160F,
  0.0007504474F, 0.0008040251F, 0.0008594490F, 0.0009167191F,
  0.0009758351F, 0.0010367969F, 0.0010996044F, 0.0011642574F,
  0.0012307558F, 0.0012990994F, 0.0013692880F, 0.0014413216F,
  0.0015151998F, 0.0015909226F, 0.0016684898F, 0.0017479011F,
  0.0018291565F, 0.0019122556F, 0.0019971983F, 0.0020839845F,
  0.0021726138F, 0.0022630861F, 0.0023554012F, 0.0024495588F,
  0.0025455588F, 0.0026434008F, 0.0027430847F, 0.0028446103F,
  0.0029479772F, 0.0030531853F, 0.0031602342F, 0.0032691238F,
  0.0033798538F, 0.0034924239F, 0.0036068338F, 0.0037230833F,
  0.0038411721F, 0.0039610999F, 0.0040828664F, 0.0042064714F,
  0.0043319145F, 0.0044591954F, 0.0045883139F, 0.0047192696F,
  0.0048520622F, 0.0049866914F, 0.0051231569F, 0.0052614583F,
  0.0054015953F, 0.0055435676F, 0.0056873748F, 0.0058330166F,
  0.0059804926F, 0.0061298026F, 0.0062809460F, 0.0064339226F,
  0.0065887320F, 0.0067453738F, 0.0069038476F, 0.0070641531F,
  0.0072262899F, 0.0073902575F, 0.0075560556F, 0.0077236838F,
  0.0078931417F, 0.0080644288F, 0.0082375447F, 0.0084124891F,
  0.0085892615F, 0.0087678614F, 0.0089482885F, 0.0091305422F,
  0.0093146223F, 0.0095005281F, 0.0096882592F, 0.0098778153F,
  0.0100691958F, 0.0102624002F, 0.0104574281F, 0.0106542791F,
  0.0108529525F, 0.0110534480F, 0.0112557651F, 0.0114599032F,
  0.0116658618F, 0.0118736405F, 0.0120832387F, 0.0122946560F,
  0.0125078917F, 0.0127229454F, 0.0129398166F, 0.0131585046F,
  0.0133790090F, 0.0136013292F, 0.0138254647F, 0.0140514149F,
  0.0142791792F, 0.0145087572F, 0.0147401481F, 0.0149733515F,
  0.0152083667F, 0.0154451932F, 0.0156838304F, 0.0159242777F,
  0.0161665345F, 0.0164106001F, 0.0166564741F, 0.0169041557F,
  0.0171536443F, 0.0174049393F, 0.0176580401F, 0.0179129461F,
  0.0181696565F, 0.0184281708F, 0.0186884883F, 0.0189506084F,
  0.0192145303F, 0.0194802535F, 0.0197477772F, 0.0200171008F,
  0.0202882236F, 0.0205611449F, 0.0208358639F, 0.0211123801F,
  0.0213906927F, 0.0216708011F, 0.0219527043F, 0.0222364019F,
  0.0225218930F, 0.0228091769F, 0.0230982529F, 0.0233891203F,
  0.0236817782F, 0.0239762259F, 0.0242724628F, 0.0245704880F,
  0.0248703007F, 0.0251719002F, 0.0254752858F, 0.0257804565F,
  0.0260874117F, 0.0263961506F, 0.0267066722F, 0.0270189760F,
  0.0273330609F, 0.0276489263F, 0.0279665712F, 0.0282859949F,
  0.0286071966F, 0.0289301753F, 0.0292549303F, 0.0295814607F,
  0.0299097656F, 0.0302398442F, 0.0305716957F, 0.0309053191F,
  0.0312407135F, 0.0315778782F, 0.0319168122F, 0.0322575145F,
  0.0325999844F, 0.0329442209F, 0.0332902231F, 0.0336379900F,
  0.0339875208F, 0.0343388146F, 0.0346918703F, 0.0350466871F,
  0.0354032640F, 0.0357616000F, 0.0361216943F, 0.0364835458F,
  0.0368471535F, 0.0372125166F, 0.0375796339F, 0.0379485046F,
  0.0383191276F, 0.0386915020F, 0.0390656267F, 0.0394415008F,
  0.0398191231F, 0.0401984927F, 0.0405796086F, 0.0409624698F,
  0.0413470751F, 0.0417334235F, 0.0421215141F, 0.0425113457F,
  0.0429029172F, 0.0432962277F, 0.0436912760F, 0.0440880610F,
  0.0444865817F, 0.0448868370F, 0.0452888257F, 0.0456925468F,
  0.0460979992F, 0.0465051816F, 0.0469140931F, 0.0473247325F,
  0.0477370986F, 0.0481511902F, 0.0485670064F, 0.0489845458F,
  0.0494038074F, 0.0498247899F, 0.0502474922F, 0.0506719131F,
  0.0510980514F, 0.0515259060F, 0.0519554756F, 0.0523867590F,
  0.0528197550F, 0.0532544624F, 0.0536908800F, 0.0541290066F,
  0.0545688408F, 0.0550103815F, 0.0554536274F, 0.0558985772F,
  0.0563452297F, 0.0567935837F, 0.0572436377F, 0.0576953907F,
  0.0581488412F, 0.0586039880F, 0.0590608297F, 0.0595193651F,
  0.0599795929F, 0.0604415117F, 0.0609051202F, 0.0613704170F,
  0.0618374009F, 0.0623060704F, 0.0627764243F, 0.0632484611F,
  0.0637221795F, 0.0641975781F, 0.0646746555F, 0.0651534104F,
  0.0656338413F, 0.0661159469F, 0.0665997257F, 0.0670851763F,
  0.0675722973F, 0.0680610873F, 0.0685515448F, 0.0690436684F,
  0.0695374567F, 0.0700329081F, 0.0705300213F, 0.0710287947F,
  0.0715292269F, 0.0720313163F, 0.0725350616F, 0.0730404612F,
  0.0735475136F, 0.0740562172F, 0.0745665707F, 0.0750785723F,
  0.0755922207F, 0.0761075143F, 0.0766244515F, 0.0771430307F,
  0.0776632505F, 0.0781851092F, 0.0787086052F, 0.0792337371F,
  0.0797605032F, 0.0802889018F, 0.0808189315F, 0.0813505905F,
  0.0818838773F, 0.0824187903F, 0.0829553277F, 0.0834934881F,
  0.0840332697F, 0.0845746708F, 0.0851176899F, 0.0856623252F,
  0.0862085751F, 0.0867564379F, 0.0873059119F, 0.0878569954F,
  0.0884096867F, 0.0889639840F, 0.0895198858F, 0.0900773902F,
  0.0906364955F, 0.0911972000F, 0.0917595019F, 0.0923233995F,
  0.0928888909F, 0.0934559745F, 0.0940246485F, 0.0945949110F,
  0.0951667604F, 0.0957401946F, 0.0963152121F, 0.0968918109F,
  0.0974699893F, 0.0980497454F, 0.0986310773F, 0.0992139832F,
  0.0997984614F, 0.1003845098F, 0.1009721267F, 0.1015613101F,
  0.1021520582F, 0.1027443692F, 0.1033382410F, 0.1039336718F,
  0.1045306597F, 0.1051292027F, 0.1057292990F, 0.1063309466F,
  0.1069341435F, 0.1075388878F, 0.1081451776F, 0.1087530108F,
  0.1093623856F, 0.1099732998F, 0.1105857516F, 0.1111997389F,
  0.1118152597F, 0.1124323121F, 0.1130508939F, 0.1136710032F,
  0.1142926379F, 0.1149157960F, 0.1155404755F, 0.1161666742F,
  0.1167943901F, 0.1174236211F, 0.1180543652F, 0.1186866202F,
  0.1193203841F, 0.1199556548F, 0.1205924300F, 0.1212307078F,
  0.1218704860F, 0.1225117624F, 0.1231545349F, 0.1237988013F,
  0.1244445596F, 0.1250918074F, 0.1257405427F, 0.1263907632F,
  0.1270424667F, 0.1276956512F, 0.1283503142F, 0.1290064537F,
  0.1296640674F, 0.1303231530F, 0.1309837084F, 0.1316457312F,
  0.1323092193F, 0.1329741703F, 0.1336405820F, 0.1343084520F,
  0.1349777782F, 0.1356485582F, 0.1363207897F, 0.1369944704F,
  0.1376695979F, 0.1383461700F, 0.1390241842F, 0.1397036384F,
  0.1403845300F, 0.1410668567F, 0.1417506162F, 0.1424358061F,
  0.1431224240F, 0.1438104674F, 0.1444999341F, 0.1451908216F,
  0.1458831274F, 0.1465768492F, 0.1472719844F, 0.1479685308F,
  0.1486664857F, 0.1493658468F, 0.1500666115F, 0.1507687775F,
  0.1514723422F, 0.1521773031F, 0.1528836577F, 0.1535914035F,
  0.1543005380F, 0.1550110587F, 0.1557229631F, 0.1564362485F,
  0.1571509124F, 0.1578669524F, 0.1585843657F, 0.1593031499F,
  0.1600233024F, 0.1607448205F, 0.1614677017F, 0.1621919433F,
  0.1629175428F, 0.1636444975F, 0.1643728047F, 0.1651024619F,
  0.1658334665F, 0.1665658156F, 0.1672995067F, 0.1680345371F,
  0.1687709041F, 0.1695086050F, 0.1702476372F, 0.1709879978F,
  0.1717296843F, 0.1724726938F, 0.1732170237F, 0.1739626711F,
  0.1747096335F, 0.1754579079F, 0.1762074916F, 0.1769583819F,
  0.1777105760F, 0.1784640710F, 0.1792188642F, 0.1799749529F,
  0.1807323340F, 0.1814910049F, 0.1822509628F, 0.1830122046F,
  0.1837747277F, 0.1845385292F, 0.1853036062F, 0.1860699558F,
  0.1868375751F, 0.1876064613F, 0.1883766114F, 0.1891480226F,
  0.1899206919F, 0.1906946164F, 0.1914697932F, 0.1922462194F,
  0.1930238919F, 0.1938028079F, 0.1945829643F, 0.1953643583F,
  0.1961469868F, 0.1969308468F, 0.1977159353F, 0.1985022494F,
  0.1992897859F, 0.2000785420F, 0.2008685145F, 0.2016597005F,
  0.2024520968F, 0.2032457005F, 0.2040405084F, 0.2048365175F,
  0.2056337247F, 0.2064321269F, 0.2072317211F, 0.2080325041F,
  0.2088344727F, 0.2096376240F, 0.2104419547F, 0.2112474618F,
  0.2120541420F, 0.2128619923F, 0.2136710094F, 0.2144811902F,
  0.2152925315F, 0.2161050301F, 0.2169186829F, 0.2177334866F,
  0.2185494381F, 0.2193665340F, 0.2201847712F, 0.2210041465F,
  0.2218246565F, 0.2226462981F, 0.2234690680F, 0.2242929629F,
  0.2251179796F, 0.2259441147F, 0.2267713650F, 0.2275997272F,
  0.2284291979F, 0.2292597739F, 0.2300914518F, 0.2309242283F,
  0.2317581001F, 0.2325930638F, 0.2334291160F, 0.2342662534F,
  0.2351044727F, 0.2359437703F, 0.2367841431F, 0.2376255875F,
  0.2384681001F, 0.2393116776F, 0.2401563165F, 0.2410020134F,
  0.2418487649F, 0.2426965675F, 0.2435454178F, 0.2443953122F,
  0.2452462474F, 0.2460982199F, 0.2469512262F, 0.2478052628F,
  0.2486603262F, 0.2495164129F, 0.2503735194F, 0.2512316421F,
  0.2520907776F, 0.2529509222F, 0.2538120726F, 0.2546742250F,
  0.2555373760F, 0.2564015219F, 0.2572666593F, 0.2581327845F,
  0.2589998939F, 0.2598679840F, 0.2607370510F, 0.2616070916F,
  0.2624781019F, 0.2633500783F, 0.2642230173F, 0.2650969152F,
  0.2659717684F, 0.2668475731F, 0.2677243257F, 0.2686020226F,
  0.2694806601F, 0.2703602344F, 0.2712407419F, 0.2721221789F,
  0.2730045417F, 0.2738878265F, 0.2747720297F, 0.2756571474F,
  0.2765431760F, 0.2774301117F, 0.2783179508F, 0.2792066895F,
  0.2800963240F, 0.2809868505F, 0.2818782654F, 0.2827705647F,
  0.2836637447F, 0.2845578016F, 0.2854527315F, 0.2863485307F,
  0.2872451953F, 0.2881427215F, 0.2890411055F, 0.2899403433F,
  0.2908404312F, 0.2917413654F, 0.2926431418F, 0.2935457567F,
  0.2944492061F, 0.2953534863F, 0.2962585932F, 0.2971645230F,
  0.2980712717F, 0.2989788356F, 0.2998872105F, 0.3007963927F,
  0.3017063781F, 0.3026171629F, 0.3035287430F, 0.3044411145F,
  0.3053542736F, 0.3062682161F, 0.3071829381F, 0.3080984356F,
  0.3090147047F, 0.3099317413F, 0.3108495414F, 0.3117681011F,
  0.3126874163F, 0.3136074830F, 0.3145282972F, 0.3154498548F,
  0.3163721517F, 0.3172951841F, 0.3182189477F, 0.3191434385F,
  0.3200686525F, 0.3209945856F, 0.3219212336F, 0.3228485927F,
  0.3237766585F, 0.3247054271F, 0.3256348943F, 0.3265650560F,
  0.3274959081F, 0.3284274465F, 0.3293596671F, 0.3302925657F,
  0.3312261382F, 0.3321603804F, 0.3330952882F, 0.3340308574F,
  0.3349670838F, 0.3359039634F, 0.3368414919F, 0.3377796651F,
  0.3387184789F, 0.3396579290F, 0.3405980113F, 0.3415387216F,
  0.3424800556F, 0.3434220091F, 0.3443645779F, 0.3453077578F,
  0.3462515446F, 0.3471959340F, 0.3481409217F, 0.3490865036F,
  0.3500326754F, 0.3509794328F, 0.3519267715F, 0.3528746873F,
  0.3538231759F, 0.3547722330F, 0.3557218544F, 0.3566720357F,
  0.3576227727F, 0.3585740610F, 0.3595258964F, 0.3604782745F,
  0.3614311910F, 0.3623846417F, 0.3633386221F, 0.3642931280F,
  0.3652481549F, 0.3662036987F, 0.3671597548F, 0.3681163191F,
  0.3690733870F, 0.3700309544F, 0.3709890167F, 0.3719475696F,
  0.3729066089F, 0.3738661299F, 0.3748261285F, 0.3757866002F,
  0.3767475406F, 0.3777089453F, 0.3786708100F, 0.3796331302F,
  0.3805959014F, 0.3815591194F, 0.3825227796F, 0.3834868777F,
  0.3844514093F, 0.3854163698F, 0.3863817549F, 0.3873475601F,
  0.3883137810F, 0.3892804131F, 0.3902474521F, 0.3912148933F,
  0.3921827325F, 0.3931509650F, 0.3941195865F, 0.3950885925F,
  0.3960579785F, 0.3970277400F, 0.3979978725F, 0.3989683716F,
  0.3999392328F, 0.4009104516F, 0.4018820234F, 0.4028539438F,
  0.4038262084F, 0.4047988125F, 0.4057717516F, 0.4067450214F,
  0.4077186172F, 0.4086925345F, 0.4096667688F, 0.4106413155F,
  0.4116161703F, 0.4125913284F, 0.4135667854F, 0.4145425368F,
  0.4155185780F, 0.4164949044F, 0.4174715116F, 0.4184483949F,
  0.4194255498F, 0.4204029718F, 0.4213806563F, 0.4223585987F,
  0.4233367946F, 0.4243152392F, 0.4252939281F, 0.4262728566F,
  0.4272520202F, 0.4282314144F, 0.4292110345F, 0.4301908760F,
  0.4311709343F, 0.4321512047F, 0.4331316828F, 0.4341123639F,
  0.4350932435F, 0.4360743168F, 0.4370555794F, 0.4380370267F,
  0.4390186540F, 0.4400004567F, 0.4409824303F, 0.4419645701F,
  0.4429468716F, 0.4439293300F, 0.4449119409F, 0.4458946996F,
  0.4468776014F, 0.4478606418F, 0.4488438162F, 0.4498271199F,
  0.4508105483F, 0.4517940967F, 0.4527777607F, 0.4537615355F,
  0.4547454165F, 0.4557293991F, 0.4567134786F, 0.4576976505F,
  0.4586819101F, 0.4596662527F, 0.4606506738F, 0.4616351687F,
  0.4626197328F, 0.4636043614F, 0.4645890499F, 0.4655737936F,
  0.4665585880F, 0.4675434284F, 0.4685283101F, 0.4695132286F,
  0.4704981791F, 0.4714831570F, 0.4724681577F, 0.4734531766F,
  0.4744382089F, 0.4754232501F, 0.4764082956F, 0.4773933406F,
  0.4783783806F, 0.4793634108F, 0.4803484267F, 0.4813334237F,
  0.4823183969F, 0.4833033419F, 0.4842882540F, 0.4852731285F,
  0.4862579608F, 0.4872427462F, 0.4882274802F, 0.4892121580F,
  0.4901967751F, 0.4911813267F, 0.4921658083F, 0.4931502151F,
  0.4941345427F, 0.4951187863F, 0.4961029412F, 0.4970870029F,
  0.4980709667F, 0.4990548280F, 0.5000385822F, 0.5010222245F,
  0.5020057505F, 0.5029891553F, 0.5039724345F, 0.5049555834F,
  0.5059385973F, 0.5069214716F, 0.5079042018F, 0.5088867831F,
  0.5098692110F, 0.5108514808F, 0.5118335879F, 0.5128155277F,
  0.5137972956F, 0.5147788869F, 0.5157602971F, 0.5167415215F,
  0.5177225555F, 0.5187033945F, 0.5196840339F, 0.5206644692F,
  0.5216446956F, 0.5226247086F, 0.5236045035F, 0.5245840759F,
  0.5255634211F, 0.5265425344F, 0.5275214114F, 0.5285000474F,
  0.5294784378F, 0.5304565781F, 0.5314344637F, 0.5324120899F,
  0.5333894522F, 0.5343665461F, 0.5353433670F, 0.5363199102F,
  0.5372961713F, 0.5382721457F, 0.5392478287F, 0.5402232159F,
  0.5411983027F, 0.5421730845F, 0.5431475569F, 0.5441217151F,
  0.5450955548F, 0.5460690714F, 0.5470422602F, 0.5480151169F,
  0.5489876368F, 0.5499598155F, 0.5509316484F, 0.5519031310F,
  0.5528742587F, 0.5538450271F, 0.5548154317F, 0.5557854680F,
  0.5567551314F, 0.5577244174F, 0.5586933216F, 0.5596618395F,
  0.5606299665F, 0.5615976983F, 0.5625650302F, 0.5635319580F,
  0.5644984770F, 0.5654645828F, 0.5664302709F, 0.5673955370F,
  0.5683603765F, 0.5693247850F, 0.5702887580F, 0.5712522912F,
  0.5722153800F, 0.5731780200F, 0.5741402069F, 0.5751019362F,
  0.5760632034F, 0.5770240042F, 0.5779843341F, 0.5789441889F,
  0.5799035639F, 0.5808624549F, 0.5818208575F, 0.5827787673F,
  0.5837361800F, 0.5846930910F, 0.5856494961F, 0.5866053910F,
  0.5875607712F, 0.5885156324F, 0.5894699703F, 0.5904237804F,
  0.5913770586F, 0.5923298004F, 0.5932820016F, 0.5942336578F,
  0.5951847646F, 0.5961353179F, 0.5970853132F, 0.5980347464F,
  0.5989836131F, 0.5999319090F, 0.6008796298F, 0.6018267713F,
  0.6027733292F, 0.6037192993F, 0.6046646773F, 0.6056094589F,
  0.6065536400F, 0.6074972162F, 0.6084401833F, 0.6093825372F,
  0.6103242736F, 0.6112653884F, 0.6122058772F, 0.6131457359F,
  0.6140849604F, 0.6150235464F, 0.6159614897F, 0.6168987862F,
  0.6178354318F, 0.6187714223F, 0.6197067535F, 0.6206414213F,
  0.6215754215F, 0.6225087501F, 0.6234414028F, 0.6243733757F,
  0.6253046646F, 0.6262352654F, 0.6271651739F, 0.6280943862F,
  0.6290228982F, 0.6299507057F, 0.6308778048F, 0.6318041913F,
  0.6327298612F, 0.6336548105F, 0.6345790352F, 0.6355025312F,
  0.6364252945F, 0.6373473211F, 0.6382686070F, 0.6391891483F,
  0.6401089409F, 0.6410279808F, 0.6419462642F, 0.6428637869F,
  0.6437805452F, 0.6446965350F, 0.6456117524F, 0.6465261935F,
  0.6474398544F, 0.6483527311F, 0.6492648197F, 0.6501761165F,
  0.6510866174F, 0.6519963186F, 0.6529052162F, 0.6538133064F,
  0.6547205854F, 0.6556270492F, 0.6565326941F, 0.6574375162F,
  0.6583415117F, 0.6592446769F, 0.6601470079F, 0.6610485009F,
  0.6619491521F, 0.6628489578F, 0.6637479143F, 0.6646460177F,
  0.6655432643F, 0.6664396505F, 0.6673351724F, 0.6682298264F,
  0.6691236087F, 0.6700165157F, 0.6709085436F, 0.6717996889F,
  0.6726899478F, 0.6735793167F, 0.6744677918F, 0.6753553697F,
  0.6762420466F, 0.6771278190F, 0.6780126832F, 0.6788966357F,
  0.6797796728F, 0.6806617909F, 0.6815429866F, 0.6824232562F,
  0.6833025961F, 0.6841810030F, 0.6850584731F, 0.6859350031F,
  0.6868105894F, 0.6876852284F, 0.6885589168F, 0.6894316510F,
  0.6903034275F, 0.6911742430F, 0.6920440939F, 0.6929129769F,
  0.6937808884F, 0.6946478251F, 0.6955137837F, 0.6963787606F,
  0.6972427525F, 0.6981057560F, 0.6989677678F, 0.6998287845F,
  0.7006888028F, 0.7015478194F, 0.7024058309F, 0.7032628340F,
  0.7041188254F, 0.7049738019F, 0.7058277601F, 0.7066806969F,
  0.7075326089F, 0.7083834929F, 0.7092333457F, 0.7100821640F,
  0.7109299447F, 0.7117766846F, 0.7126223804F, 0.7134670291F,
  0.7143106273F, 0.7151531721F, 0.7159946602F, 0.7168350885F,
  0.7176744539F, 0.7185127534F, 0.7193499837F, 0.7201861418F,
  0.7210212247F, 0.7218552293F, 0.7226881526F, 0.7235199914F,
  0.7243507428F, 0.7251804039F, 0.7260089715F, 0.7268364426F,
  0.7276628144F, 0.7284880839F, 0.7293122481F, 0.7301353040F,
  0.7309572487F, 0.7317780794F, 0.7325977930F, 0.7334163868F,
  0.7342338579F, 0.7350502033F, 0.7358654202F, 0.7366795059F,
  0.7374924573F, 0.7383042718F, 0.7391149465F, 0.7399244787F,
  0.7407328655F, 0.7415401041F, 0.7423461920F, 0.7431511261F,
  0.7439549040F, 0.7447575227F, 0.7455589797F, 0.7463592723F,
  0.7471583976F, 0.7479563532F, 0.7487531363F, 0.7495487443F,
  0.7503431745F, 0.7511364244F, 0.7519284913F, 0.7527193726F,
  0.7535090658F, 0.7542975683F, 0.7550848776F, 0.7558709910F,
  0.7566559062F, 0.7574396205F, 0.7582221314F, 0.7590034366F,
  0.7597835334F, 0.7605624194F, 0.7613400923F, 0.7621165495F,
  0.7628917886F, 0.7636658072F, 0.7644386030F, 0.7652101735F,
  0.7659805164F, 0.7667496292F, 0.7675175098F, 0.7682841556F,
  0.7690495645F, 0.7698137341F, 0.7705766622F, 0.7713383463F,
  0.7720987844F, 0.7728579741F, 0.7736159132F, 0.7743725994F,
  0.7751280306F, 0.7758822046F, 0.7766351192F, 0.7773867722F,
  0.7781371614F, 0.7788862848F, 0.7796341401F, 0.7803807253F,
  0.7811260383F, 0.7818700769F, 0.7826128392F, 0.7833543230F,
  0.7840945263F, 0.7848334471F, 0.7855710833F, 0.7863074330F,
  0.7870424941F, 0.7877762647F, 0.7885087428F, 0.7892399264F,
  0.7899698137F, 0.7906984026F, 0.7914256914F, 0.7921516780F,
  0.7928763607F, 0.7935997375F, 0.7943218065F, 0.7950425661F,
  0.7957620142F, 0.7964801492F, 0.7971969692F, 0.7979124724F,
  0.7986266570F, 0.7993395214F, 0.8000510638F, 0.8007612823F,
  0.8014701754F, 0.8021777413F, 0.8028839784F, 0.8035888849F,
  0.8042924592F, 0.8049946997F, 0.8056956048F, 0.8063951727F,
  0.8070934020F, 0.8077902910F, 0.8084858381F, 0.8091800419F,
  0.8098729007F, 0.8105644130F, 0.8112545774F, 0.8119433922F,
  0.8126308561F, 0.8133169676F, 0.8140017251F, 0.8146851272F,
  0.8153671726F, 0.8160478598F, 0.8167271874F, 0.8174051539F,
  0.8180817582F, 0.8187569986F, 0.8194308741F, 0.8201033831F,
  0.8207745244F, 0.8214442966F, 0.8221126986F, 0.8227797290F,
  0.8234453865F, 0.8241096700F, 0.8247725781F, 0.8254341097F,
  0.8260942636F, 0.8267530385F, 0.8274104334F, 0.8280664470F,
  0.8287210782F, 0.8293743259F, 0.8300261889F, 0.8306766662F,
  0.8313257566F, 0.8319734591F, 0.8326197727F, 0.8332646963F,
  0.8339082288F, 0.8345503692F, 0.8351911167F, 0.8358304700F,
  0.8364684284F, 0.8371049907F, 0.8377401562F, 0.8383739238F,
  0.8390062927F, 0.8396372618F, 0.8402668305F, 0.8408949977F,
  0.8415217626F, 0.8421471245F, 0.8427710823F, 0.8433936354F,
  0.8440147830F, 0.8446345242F, 0.8452528582F, 0.8458697844F,
  0.8464853020F, 0.8470994102F, 0.8477121084F, 0.8483233958F,
  0.8489332718F, 0.8495417356F, 0.8501487866F, 0.8507544243F,
  0.8513586479F, 0.8519614568F, 0.8525628505F, 0.8531628283F,
  0.8537613897F, 0.8543585341F, 0.8549542611F, 0.8555485699F,
  0.8561414603F, 0.8567329315F, 0.8573229832F, 0.8579116149F,
  0.8584988262F, 0.8590846165F, 0.8596689855F, 0.8602519327F,
  0.8608334577F, 0.8614135603F, 0.8619922399F, 0.8625694962F,
  0.8631453289F, 0.8637197377F, 0.8642927222F, 0.8648642821F,
  0.8654344172F, 0.8660031272F, 0.8665704118F, 0.8671362708F,
  0.8677007039F, 0.8682637109F, 0.8688252917F, 0.8693854460F,
  0.8699441737F, 0.8705014745F, 0.8710573485F, 0.8716117953F,
  0.8721648150F, 0.8727164073F, 0.8732665723F, 0.8738153098F,
  0.8743626197F, 0.8749085021F, 0.8754529569F, 0.8759959840F,
  0.8765375835F, 0.8770777553F, 0.8776164996F, 0.8781538162F,
  0.8786897054F, 0.8792241670F, 0.8797572013F, 0.8802888082F,
  0.8808189880F, 0.8813477407F, 0.8818750664F, 0.8824009653F,
  0.8829254375F, 0.8834484833F, 0.8839701028F, 0.8844902961F,
  0.8850090636F, 0.8855264054F, 0.8860423218F, 0.8865568131F,
  0.8870698794F, 0.8875815212F, 0.8880917386F, 0.8886005319F,
  0.8891079016F, 0.8896138479F, 0.8901183712F, 0.8906214719F,
  0.8911231503F, 0.8916234067F, 0.8921222417F, 0.8926196556F,
  0.8931156489F, 0.8936102219F, 0.8941033752F, 0.8945951092F,
  0.8950854244F, 0.8955743212F, 0.8960618003F, 0.8965478621F,
  0.8970325071F, 0.8975157359F, 0.8979975490F, 0.8984779471F,
  0.8989569307F, 0.8994345004F, 0.8999106568F, 0.9003854005F,
  0.9008587323F, 0.9013306526F, 0.9018011623F, 0.9022702619F,
  0.9027379521F, 0.9032042337F, 0.9036691074F, 0.9041325739F,
  0.9045946339F, 0.9050552882F, 0.9055145376F, 0.9059723828F,
  0.9064288246F, 0.9068838638F, 0.9073375013F, 0.9077897379F,
  0.9082405743F, 0.9086900115F, 0.9091380503F, 0.9095846917F,
  0.9100299364F, 0.9104737854F, 0.9109162397F, 0.9113573001F,
  0.9117969675F, 0.9122352430F, 0.9126721275F, 0.9131076219F,
  0.9135417273F, 0.9139744447F, 0.9144057750F, 0.9148357194F,
  0.9152642787F, 0.9156914542F, 0.9161172468F, 0.9165416576F,
  0.9169646877F, 0.9173863382F, 0.9178066102F, 0.9182255048F,
  0.9186430232F, 0.9190591665F, 0.9194739359F, 0.9198873324F,
  0.9202993574F, 0.9207100120F, 0.9211192973F, 0.9215272147F,
  0.9219337653F, 0.9223389504F, 0.9227427713F, 0.9231452290F,
  0.9235463251F, 0.9239460607F, 0.9243444371F, 0.9247414557F,
  0.9251371177F, 0.9255314245F, 0.9259243774F, 0.9263159778F,
  0.9267062270F, 0.9270951264F, 0.9274826774F, 0.9278688814F,
  0.9282537398F, 0.9286372540F, 0.9290194254F, 0.9294002555F,
  0.9297797458F, 0.9301578976F, 0.9305347125F, 0.9309101919F,
  0.9312843373F, 0.9316571503F, 0.9320286323F, 0.9323987849F,
  0.9327676097F, 0.9331351080F, 0.9335012816F, 0.9338661320F,
  0.9342296607F, 0.9345918694F, 0.9349527596F, 0.9353123330F,
  0.9356705911F, 0.9360275357F, 0.9363831683F, 0.9367374905F,
  0.9370905042F, 0.9374422108F, 0.9377926122F, 0.9381417099F,
  0.9384895057F, 0.9388360014F, 0.9391811985F, 0.9395250989F,
  0.9398677043F, 0.9402090165F, 0.9405490371F, 0.9408877680F,
  0.9412252110F, 0.9415613678F, 0.9418962402F, 0.9422298301F,
  0.9425621392F, 0.9428931695F, 0.9432229226F, 0.9435514005F,
  0.9438786050F, 0.9442045381F, 0.9445292014F, 0.9448525971F,
  0.9451747268F, 0.9454955926F, 0.9458151963F, 0.9461335399F,
  0.9464506253F, 0.9467664545F, 0.9470810293F, 0.9473943517F,
  0.9477064238F, 0.9480172474F, 0.9483268246F, 0.9486351573F,
  0.9489422475F, 0.9492480973F, 0.9495527087F, 0.9498560837F,
  0.9501582243F, 0.9504591325F, 0.9507588105F, 0.9510572603F,
  0.9513544839F, 0.9516504834F, 0.9519452609F, 0.9522388186F,
  0.9525311584F, 0.9528222826F, 0.9531121932F, 0.9534008923F,
  0.9536883821F, 0.9539746647F, 0.9542597424F, 0.9545436171F,
  0.9548262912F, 0.9551077667F, 0.9553880459F, 0.9556671309F,
  0.9559450239F, 0.9562217272F, 0.9564972429F, 0.9567715733F,
  0.9570447206F, 0.9573166871F, 0.9575874749F, 0.9578570863F,
  0.9581255236F, 0.9583927890F, 0.9586588849F, 0.9589238134F,
  0.9591875769F, 0.9594501777F, 0.9597116180F, 0.9599719003F,
  0.9602310267F, 0.9604889995F, 0.9607458213F, 0.9610014942F,
  0.9612560206F, 0.9615094028F, 0.9617616433F, 0.9620127443F,
  0.9622627083F, 0.9625115376F, 0.9627592345F, 0.9630058016F,
  0.9632512411F, 0.9634955555F, 0.9637387471F, 0.9639808185F,
  0.9642217720F, 0.9644616100F, 0.9647003349F, 0.9649379493F,
  0.9651744556F, 0.9654098561F, 0.9656441534F, 0.9658773499F,
  0.9661094480F, 0.9663404504F, 0.9665703593F, 0.9667991774F,
  0.9670269071F, 0.9672535509F, 0.9674791114F, 0.9677035909F,
  0.9679269921F, 0.9681493174F, 0.9683705694F, 0.9685907506F,
  0.9688098636F, 0.9690279108F, 0.9692448948F, 0.9694608182F,
  0.9696756836F, 0.9698894934F, 0.9701022503F, 0.9703139569F,
  0.9705246156F, 0.9707342291F, 0.9709428000F, 0.9711503309F,
  0.9713568243F, 0.9715622829F, 0.9717667093F, 0.9719701060F,
  0.9721724757F, 0.9723738210F, 0.9725741446F, 0.9727734490F,
  0.9729717369F, 0.9731690109F, 0.9733652737F, 0.9735605279F,
  0.9737547762F, 0.9739480212F, 0.9741402656F, 0.9743315120F,
  0.9745217631F, 0.9747110216F, 0.9748992901F, 0.9750865714F,
  0.9752728681F, 0.9754581829F, 0.9756425184F, 0.9758258775F,
  0.9760082627F, 0.9761896768F, 0.9763701224F, 0.9765496024F,
  0.9767281193F, 0.9769056760F, 0.9770822751F, 0.9772579193F,
  0.9774326114F, 0.9776063542F, 0.9777791502F, 0.9779510023F,
  0.9781219133F, 0.9782918858F, 0.9784609226F, 0.9786290264F,
  0.9787962000F, 0.9789624461F, 0.9791277676F, 0.9792921671F,
  0.9794556474F, 0.9796182113F, 0.9797798615F, 0.9799406009F,
  0.9801004321F, 0.9802593580F, 0.9804173813F, 0.9805745049F,
  0.9807307314F, 0.9808860637F, 0.9810405046F, 0.9811940568F,
  0.9813467232F, 0.9814985065F, 0.9816494095F, 0.9817994351F,
  0.9819485860F, 0.9820968650F, 0.9822442750F, 0.9823908186F,
  0.9825364988F, 0.9826813184F, 0.9828252801F, 0.9829683868F,
  0.9831106413F, 0.9832520463F, 0.9833926048F, 0.9835323195F,
  0.9836711932F, 0.9838092288F, 0.9839464291F, 0.9840827969F,
  0.9842183351F, 0.9843530464F, 0.9844869337F, 0.9846199998F,
  0.9847522475F, 0.9848836798F, 0.9850142993F, 0.9851441090F,
  0.9852731117F, 0.9854013101F, 0.9855287073F, 0.9856553058F,
  0.9857811087F, 0.9859061188F, 0.9860303388F, 0.9861537717F,
  0.9862764202F, 0.9863982872F, 0.9865193756F, 0.9866396882F,
  0.9867592277F, 0.9868779972F, 0.9869959993F, 0.9871132370F,
  0.9872297131F, 0.9873454304F, 0.9874603918F, 0.9875746001F,
  0.9876880581F, 0.9878007688F, 0.9879127348F, 0.9880239592F,
  0.9881344447F, 0.9882441941F, 0.9883532104F, 0.9884614962F,
  0.9885690546F, 0.9886758883F, 0.9887820001F, 0.9888873930F,
  0.9889920697F, 0.9890960331F, 0.9891992859F, 0.9893018312F,
  0.9894036716F, 0.9895048100F, 0.9896052493F, 0.9897049923F,
  0.9898040418F, 0.9899024006F, 0.9900000717F, 0.9900970577F,
  0.9901933616F, 0.9902889862F, 0.9903839343F, 0.9904782087F,
  0.9905718122F, 0.9906647477F, 0.9907570180F, 0.9908486259F,
  0.9909395742F, 0.9910298658F, 0.9911195034F, 0.9912084899F,
  0.9912968281F, 0.9913845208F, 0.9914715708F, 0.9915579810F,
  0.9916437540F, 0.9917288928F, 0.9918134001F, 0.9918972788F,
  0.9919805316F, 0.9920631613F, 0.9921451707F, 0.9922265626F,
  0.9923073399F, 0.9923875052F, 0.9924670615F, 0.9925460114F,
  0.9926243577F, 0.9927021033F, 0.9927792508F, 0.9928558032F,
  0.9929317631F, 0.9930071333F, 0.9930819167F, 0.9931561158F,
  0.9932297337F, 0.9933027728F, 0.9933752362F, 0.9934471264F,
  0.9935184462F, 0.9935891985F, 0.9936593859F, 0.9937290112F,
  0.9937980771F, 0.9938665864F, 0.9939345418F, 0.9940019460F,
  0.9940688018F, 0.9941351118F, 0.9942008789F, 0.9942661057F,
  0.9943307950F, 0.9943949494F, 0.9944585717F, 0.9945216645F,
  0.9945842307F, 0.9946462728F, 0.9947077936F, 0.9947687957F,
  0.9948292820F, 0.9948892550F, 0.9949487174F, 0.9950076719F,
  0.9950661212F, 0.9951240679F, 0.9951815148F, 0.9952384645F,
  0.9952949196F, 0.9953508828F, 0.9954063568F, 0.9954613442F,
  0.9955158476F, 0.9955698697F, 0.9956234132F, 0.9956764806F,
  0.9957290746F, 0.9957811978F, 0.9958328528F, 0.9958840423F,
  0.9959347688F, 0.9959850351F, 0.9960348435F, 0.9960841969F,
  0.9961330977F, 0.9961815486F, 0.9962295521F, 0.9962771108F,
  0.9963242274F, 0.9963709043F, 0.9964171441F, 0.9964629494F,
  0.9965083228F, 0.9965532668F, 0.9965977840F, 0.9966418768F,
  0.9966855479F, 0.9967287998F, 0.9967716350F, 0.9968140559F,
  0.9968560653F, 0.9968976655F, 0.9969388591F, 0.9969796485F,
  0.9970200363F, 0.9970600250F, 0.9970996170F, 0.9971388149F,
  0.9971776211F, 0.9972160380F, 0.9972540683F, 0.9972917142F,
  0.9973289783F, 0.9973658631F, 0.9974023709F, 0.9974385042F,
  0.9974742655F, 0.9975096571F, 0.9975446816F, 0.9975793413F,
  0.9976136386F, 0.9976475759F, 0.9976811557F, 0.9977143803F,
  0.9977472521F, 0.9977797736F, 0.9978119470F, 0.9978437748F,
  0.9978752593F, 0.9979064029F, 0.9979372079F, 0.9979676768F,
  0.9979978117F, 0.9980276151F, 0.9980570893F, 0.9980862367F,
  0.9981150595F, 0.9981435600F, 0.9981717406F, 0.9981996035F,
  0.9982271511F, 0.9982543856F, 0.9982813093F, 0.9983079246F,
  0.9983342336F, 0.9983602386F, 0.9983859418F, 0.9984113456F,
  0.9984364522F, 0.9984612638F, 0.9984857825F, 0.9985100108F,
  0.9985339507F, 0.9985576044F, 0.9985809743F, 0.9986040624F,
  0.9986268710F, 0.9986494022F, 0.9986716583F, 0.9986936413F,
  0.9987153535F, 0.9987367969F, 0.9987579738F, 0.9987788864F,
  0.9987995366F, 0.9988199267F, 0.9988400587F, 0.9988599348F,
  0.9988795572F, 0.9988989278F, 0.9989180487F, 0.9989369222F,
  0.9989555501F, 0.9989739347F, 0.9989920780F, 0.9990099820F,
  0.9990276487F, 0.9990450803F, 0.9990622787F, 0.9990792460F,
  0.9990959841F, 0.9991124952F, 0.9991287812F, 0.9991448440F,
  0.9991606858F, 0.9991763084F, 0.9991917139F, 0.9992069042F,
  0.9992218813F, 0.9992366471F, 0.9992512035F, 0.9992655525F,
  0.9992796961F, 0.9992936361F, 0.9993073744F, 0.9993209131F,
  0.9993342538F, 0.9993473987F, 0.9993603494F, 0.9993731080F,
  0.9993856762F, 0.9993980559F, 0.9994102490F, 0.9994222573F,
  0.9994340827F, 0.9994457269F, 0.9994571918F, 0.9994684793F,
  0.9994795910F, 0.9994905288F, 0.9995012945F, 0.9995118898F,
  0.9995223165F, 0.9995325765F, 0.9995426713F, 0.9995526029F,
  0.9995623728F, 0.9995719829F, 0.9995814349F, 0.9995907304F,
  0.9995998712F, 0.9996088590F, 0.9996176954F, 0.9996263821F,
  0.9996349208F, 0.9996433132F, 0.9996515609F, 0.9996596656F,
  0.9996676288F, 0.9996754522F, 0.9996831375F, 0.9996906862F,
  0.9996981000F, 0.9997053804F, 0.9997125290F, 0.9997195474F,
  0.9997264371F, 0.9997331998F, 0.9997398369F, 0.9997463500F,
  0.9997527406F, 0.9997590103F, 0.9997651606F, 0.9997711930F,
  0.9997771089F, 0.9997829098F, 0.9997885973F, 0.9997941728F,
  0.9997996378F, 0.9998049936F, 0.9998102419F, 0.9998153839F,
  0.9998204211F, 0.9998253550F, 0.9998301868F, 0.9998349182F,
  0.9998395503F, 0.9998440847F, 0.9998485226F, 0.9998528654F,
  0.9998571146F, 0.9998612713F, 0.9998653370F, 0.9998693130F,
  0.9998732007F, 0.9998770012F, 0.9998807159F, 0.9998843461F,
  0.9998878931F, 0.9998913581F, 0.9998947424F, 0.9998980473F,
  0.9999012740F, 0.9999044237F, 0.9999074976F, 0.9999104971F,
  0.9999134231F, 0.9999162771F, 0.9999190601F, 0.9999217733F,
  0.9999244179F, 0.9999269950F, 0.9999295058F, 0.9999319515F,
  0.9999343332F, 0.9999366519F, 0.9999389088F, 0.9999411050F,
  0.9999432416F, 0.9999453196F, 0.9999473402F, 0.9999493044F,
  0.9999512132F, 0.9999530677F, 0.9999548690F, 0.9999566180F,
  0.9999583157F, 0.9999599633F, 0.9999615616F, 0.9999631116F,
  0.9999646144F, 0.9999660709F, 0.9999674820F, 0.9999688487F,
  0.9999701719F, 0.9999714526F, 0.9999726917F, 0.9999738900F,
  0.9999750486F, 0.9999761682F, 0.9999772497F, 0.9999782941F,
  0.9999793021F, 0.9999802747F, 0.9999812126F, 0.9999821167F,
  0.9999829878F, 0.9999838268F, 0.9999846343F, 0.9999854113F,
  0.9999861584F, 0.9999868765F, 0.9999875664F, 0.9999882287F,
  0.9999888642F, 0.9999894736F, 0.9999900577F, 0.9999906172F,
  0.9999911528F, 0.9999916651F, 0.9999921548F, 0.9999926227F,
  0.9999930693F, 0.9999934954F, 0.9999939015F, 0.9999942883F,
  0.9999946564F, 0.9999950064F, 0.9999953390F, 0.9999956547F,
  0.9999959541F, 0.9999962377F, 0.9999965062F, 0.9999967601F,
  0.9999969998F, 0.9999972260F, 0.9999974392F, 0.9999976399F,
  0.9999978285F, 0.9999980056F, 0.9999981716F, 0.9999983271F,
  0.9999984724F, 0.9999986081F, 0.9999987345F, 0.9999988521F,
  0.9999989613F, 0.9999990625F, 0.9999991562F, 0.9999992426F,
  0.9999993223F, 0.9999993954F, 0.9999994625F, 0.9999995239F,
  0.9999995798F, 0.9999996307F, 0.9999996768F, 0.9999997184F,
  0.9999997559F, 0.9999997895F, 0.9999998195F, 0.9999998462F,
  0.9999998698F, 0.9999998906F, 0.9999999088F, 0.9999999246F,
  0.9999999383F, 0.9999999500F, 0.9999999600F, 0.9999999684F,
  0.9999999754F, 0.9999999811F, 0.9999999858F, 0.9999999896F,
  0.9999999925F, 0.9999999948F, 0.9999999965F, 0.9999999978F,
  0.9999999986F, 0.9999999992F, 0.9999999996F, 0.9999999998F,
  0.9999999999F, 1.0000000000F, 1.0000000000F, 1.0000000000F,
};

static float vwin8192[4096] = {
  0.0000000578F, 0.0000005198F, 0.0000014438F, 0.0000028299F,
  0.0000046780F, 0.0000069882F, 0.0000097604F, 0.0000129945F,
  0.0000166908F, 0.0000208490F, 0.0000254692F, 0.0000305515F,
  0.0000360958F, 0.0000421021F, 0.0000485704F, 0.0000555006F,
  0.0000628929F, 0.0000707472F, 0.0000790635F, 0.0000878417F,
  0.0000970820F, 0.0001067842F, 0.0001169483F, 0.0001275744F,
  0.0001386625F, 0.0001502126F, 0.0001622245F, 0.0001746984F,
  0.0001876343F, 0.0002010320F, 0.0002148917F, 0.0002292132F,
  0.0002439967F, 0.0002592421F, 0.0002749493F, 0.0002911184F,
  0.0003077493F, 0.0003248421F, 0.0003423967F, 0.0003604132F,
  0.0003788915F, 0.0003978316F, 0.0004172335F, 0.0004370971F,
  0.0004574226F, 0.0004782098F, 0.0004994587F, 0.0005211694F,
  0.0005433418F, 0.0005659759F, 0.0005890717F, 0.0006126292F,
  0.0006366484F, 0.0006611292F, 0.0006860716F, 0.0007114757F,
  0.0007373414F, 0.0007636687F, 0.0007904576F, 0.0008177080F,
  0.0008454200F, 0.0008735935F, 0.0009022285F, 0.0009313250F,
  0.0009608830F, 0.0009909025F, 0.0010213834F, 0.0010523257F,
  0.0010837295F, 0.0011155946F, 0.0011479211F, 0.0011807090F,
  0.0012139582F, 0.0012476687F, 0.0012818405F, 0.0013164736F,
  0.0013515679F, 0.0013871235F, 0.0014231402F, 0.0014596182F,
  0.0014965573F, 0.0015339576F, 0.0015718190F, 0.0016101415F,
  0.0016489251F, 0.0016881698F, 0.0017278754F, 0.0017680421F,
  0.0018086698F, 0.0018497584F, 0.0018913080F, 0.0019333185F,
  0.0019757898F, 0.0020187221F, 0.0020621151F, 0.0021059690F,
  0.0021502837F, 0.0021950591F, 0.0022402953F, 0.0022859921F,
  0.0023321497F, 0.0023787679F, 0.0024258467F, 0.0024733861F,
  0.0025213861F, 0.0025698466F, 0.0026187676F, 0.0026681491F,
  0.0027179911F, 0.0027682935F, 0.0028190562F, 0.0028702794F,
  0.0029219628F, 0.0029741066F, 0.0030267107F, 0.0030797749F,
  0.0031332994F, 0.0031872841F, 0.0032417289F, 0.0032966338F,
  0.0033519988F, 0.0034078238F, 0.0034641089F, 0.0035208539F,
  0.0035780589F, 0.0036357237F, 0.0036938485F, 0.0037524331F,
  0.0038114775F, 0.0038709817F, 0.0039309456F, 0.0039913692F,
  0.0040522524F, 0.0041135953F, 0.0041753978F, 0.0042376599F,
  0.0043003814F, 0.0043635624F, 0.0044272029F, 0.0044913028F,
  0.0045558620F, 0.0046208806F, 0.0046863585F, 0.0047522955F,
  0.0048186919F, 0.0048855473F, 0.0049528619F, 0.0050206356F,
  0.0050888684F, 0.0051575601F, 0.0052267108F, 0.0052963204F,
  0.0053663890F, 0.0054369163F, 0.0055079025F, 0.0055793474F,
  0.0056512510F, 0.0057236133F, 0.0057964342F, 0.0058697137F,
  0.0059434517F, 0.0060176482F, 0.0060923032F, 0.0061674166F,
  0.0062429883F, 0.0063190183F, 0.0063955066F, 0.0064724532F,
  0.0065498579F, 0.0066277207F, 0.0067060416F, 0.0067848205F,
  0.0068640575F, 0.0069437523F, 0.0070239051F, 0.0071045157F,
  0.0071855840F, 0.0072671102F, 0.0073490940F, 0.0074315355F,
  0.0075144345F, 0.0075977911F, 0.0076816052F, 0.0077658768F,
  0.0078506057F, 0.0079357920F, 0.0080214355F, 0.0081075363F,
  0.0081940943F, 0.0082811094F, 0.0083685816F, 0.0084565108F,
  0.0085448970F, 0.0086337401F, 0.0087230401F, 0.0088127969F,
  0.0089030104F, 0.0089936807F, 0.0090848076F, 0.0091763911F,
  0.0092684311F, 0.0093609276F, 0.0094538805F, 0.0095472898F,
  0.0096411554F, 0.0097354772F, 0.0098302552F, 0.0099254894F,
  0.0100211796F, 0.0101173259F, 0.0102139281F, 0.0103109863F,
  0.0104085002F, 0.0105064700F, 0.0106048955F, 0.0107037766F,
  0.0108031133F, 0.0109029056F, 0.0110031534F, 0.0111038565F,
  0.0112050151F, 0.0113066289F, 0.0114086980F, 0.0115112222F,
  0.0116142015F, 0.0117176359F, 0.0118215252F, 0.0119258695F,
  0.0120306686F, 0.0121359225F, 0.0122416312F, 0.0123477944F,
  0.0124544123F, 0.0125614847F, 0.0126690116F, 0.0127769928F,
  0.0128854284F, 0.0129943182F, 0.0131036623F, 0.0132134604F,
  0.0133237126F, 0.0134344188F, 0.0135455790F, 0.0136571929F,
  0.0137692607F, 0.0138817821F, 0.0139947572F, 0.0141081859F,
  0.0142220681F, 0.0143364037F, 0.0144511927F, 0.0145664350F,
  0.0146821304F, 0.0147982791F, 0.0149148808F, 0.0150319355F,
  0.0151494431F, 0.0152674036F, 0.0153858168F, 0.0155046828F,
  0.0156240014F, 0.0157437726F, 0.0158639962F, 0.0159846723F,
  0.0161058007F, 0.0162273814F, 0.0163494142F, 0.0164718991F,
  0.0165948361F, 0.0167182250F, 0.0168420658F, 0.0169663584F,
  0.0170911027F, 0.0172162987F, 0.0173419462F, 0.0174680452F,
  0.0175945956F, 0.0177215974F, 0.0178490504F, 0.0179769545F,
  0.0181053098F, 0.0182341160F, 0.0183633732F, 0.0184930812F,
  0.0186232399F, 0.0187538494F, 0.0188849094F, 0.0190164200F,
  0.0191483809F, 0.0192807923F, 0.0194136539F, 0.0195469656F,
  0.0196807275F, 0.0198149394F, 0.0199496012F, 0.0200847128F,
  0.0202202742F, 0.0203562853F, 0.0204927460F, 0.0206296561F,
  0.0207670157F, 0.0209048245F, 0.0210430826F, 0.0211817899F,
  0.0213209462F, 0.0214605515F, 0.0216006057F, 0.0217411086F,
  0.0218820603F, 0.0220234605F, 0.0221653093F, 0.0223076066F,
  0.0224503521F, 0.0225935459F, 0.0227371879F, 0.0228812779F,
  0.0230258160F, 0.0231708018F, 0.0233162355F, 0.0234621169F,
  0.0236084459F, 0.0237552224F, 0.0239024462F, 0.0240501175F,
  0.0241982359F, 0.0243468015F, 0.0244958141F, 0.0246452736F,
  0.0247951800F, 0.0249455331F, 0.0250963329F, 0.0252475792F,
  0.0253992720F, 0.0255514111F, 0.0257039965F, 0.0258570281F,
  0.0260105057F, 0.0261644293F, 0.0263187987F, 0.0264736139F,
  0.0266288747F, 0.0267845811F, 0.0269407330F, 0.0270973302F,
  0.0272543727F, 0.0274118604F, 0.0275697930F, 0.0277281707F,
  0.0278869932F, 0.0280462604F, 0.0282059723F, 0.0283661287F,
  0.0285267295F, 0.0286877747F, 0.0288492641F, 0.0290111976F,
  0.0291735751F, 0.0293363965F, 0.0294996617F, 0.0296633706F,
  0.0298275231F, 0.0299921190F, 0.0301571583F, 0.0303226409F,
  0.0304885667F, 0.0306549354F, 0.0308217472F, 0.0309890017F,
  0.0311566989F, 0.0313248388F, 0.0314934211F, 0.0316624459F,
  0.0318319128F, 0.0320018220F, 0.0321721732F, 0.0323429663F,
  0.0325142013F, 0.0326858779F, 0.0328579962F, 0.0330305559F,
  0.0332035570F, 0.0333769994F, 0.0335508829F, 0.0337252074F,
  0.0338999728F, 0.0340751790F, 0.0342508259F, 0.0344269134F,
  0.0346034412F, 0.0347804094F, 0.0349578178F, 0.0351356663F,
  0.0353139548F, 0.0354926831F, 0.0356718511F, 0.0358514588F,
  0.0360315059F, 0.0362119924F, 0.0363929182F, 0.0365742831F,
  0.0367560870F, 0.0369383297F, 0.0371210113F, 0.0373041315F,
  0.0374876902F, 0.0376716873F, 0.0378561226F, 0.0380409961F,
  0.0382263077F, 0.0384120571F, 0.0385982443F, 0.0387848691F,
  0.0389719315F, 0.0391594313F, 0.0393473683F, 0.0395357425F,
  0.0397245537F, 0.0399138017F, 0.0401034866F, 0.0402936080F,
  0.0404841660F, 0.0406751603F, 0.0408665909F, 0.0410584576F,
  0.0412507603F, 0.0414434988F, 0.0416366731F, 0.0418302829F,
  0.0420243282F, 0.0422188088F, 0.0424137246F, 0.0426090755F,
  0.0428048613F, 0.0430010819F, 0.0431977371F, 0.0433948269F,
  0.0435923511F, 0.0437903095F, 0.0439887020F, 0.0441875285F,
  0.0443867889F, 0.0445864830F, 0.0447866106F, 0.0449871717F,
  0.0451881661F, 0.0453895936F, 0.0455914542F, 0.0457937477F,
  0.0459964738F, 0.0461996326F, 0.0464032239F, 0.0466072475F,
  0.0468117032F, 0.0470165910F, 0.0472219107F, 0.0474276622F,
  0.0476338452F, 0.0478404597F, 0.0480475056F, 0.0482549827F,
  0.0484628907F, 0.0486712297F, 0.0488799994F, 0.0490891998F,
  0.0492988306F, 0.0495088917F, 0.0497193830F, 0.0499303043F,
  0.0501416554F, 0.0503534363F, 0.0505656468F, 0.0507782867F,
  0.0509913559F, 0.0512048542F, 0.0514187815F, 0.0516331376F,
  0.0518479225F, 0.0520631358F, 0.0522787775F, 0.0524948475F,
  0.0527113455F, 0.0529282715F, 0.0531456252F, 0.0533634066F,
  0.0535816154F, 0.0538002515F, 0.0540193148F, 0.0542388051F,
  0.0544587222F, 0.0546790660F, 0.0548998364F, 0.0551210331F,
  0.0553426561F, 0.0555647051F, 0.0557871801F, 0.0560100807F,
  0.0562334070F, 0.0564571587F, 0.0566813357F, 0.0569059378F,
  0.0571309649F, 0.0573564168F, 0.0575822933F, 0.0578085942F,
  0.0580353195F, 0.0582624689F, 0.0584900423F, 0.0587180396F,
  0.0589464605F, 0.0591753049F, 0.0594045726F, 0.0596342635F,
  0.0598643774F, 0.0600949141F, 0.0603258735F, 0.0605572555F,
  0.0607890597F, 0.0610212862F, 0.0612539346F, 0.0614870049F,
  0.0617204968F, 0.0619544103F, 0.0621887451F, 0.0624235010F,
  0.0626586780F, 0.0628942758F, 0.0631302942F, 0.0633667331F,
  0.0636035923F, 0.0638408717F, 0.0640785710F, 0.0643166901F,
  0.0645552288F, 0.0647941870F, 0.0650335645F, 0.0652733610F,
  0.0655135765F, 0.0657542108F, 0.0659952636F, 0.0662367348F,
  0.0664786242F, 0.0667209316F, 0.0669636570F, 0.0672068000F,
  0.0674503605F, 0.0676943384F, 0.0679387334F, 0.0681835454F,
  0.0684287742F, 0.0686744196F, 0.0689204814F, 0.0691669595F,
  0.0694138536F, 0.0696611637F, 0.0699088894F, 0.0701570307F,
  0.0704055873F, 0.0706545590F, 0.0709039458F, 0.0711537473F,
  0.0714039634F, 0.0716545939F, 0.0719056387F, 0.0721570975F,
  0.0724089702F, 0.0726612565F, 0.0729139563F, 0.0731670694F,
  0.0734205956F, 0.0736745347F, 0.0739288866F, 0.0741836510F,
  0.0744388277F, 0.0746944166F, 0.0749504175F, 0.0752068301F,
  0.0754636543F, 0.0757208899F, 0.0759785367F, 0.0762365946F,
  0.0764950632F, 0.0767539424F, 0.0770132320F, 0.0772729319F,
  0.0775330418F, 0.0777935616F, 0.0780544909F, 0.0783158298F,
  0.0785775778F, 0.0788397349F, 0.0791023009F, 0.0793652755F,
  0.0796286585F, 0.0798924498F, 0.0801566492F, 0.0804212564F,
  0.0806862712F, 0.0809516935F, 0.0812175231F, 0.0814837597F,
  0.0817504031F, 0.0820174532F, 0.0822849097F, 0.0825527724F,
  0.0828210412F, 0.0830897158F, 0.0833587960F, 0.0836282816F,
  0.0838981724F, 0.0841684682F, 0.0844391688F, 0.0847102740F,
  0.0849817835F, 0.0852536973F, 0.0855260150F, 0.0857987364F,
  0.0860718614F, 0.0863453897F, 0.0866193211F, 0.0868936554F,
  0.0871683924F, 0.0874435319F, 0.0877190737F, 0.0879950175F,
  0.0882713632F, 0.0885481105F, 0.0888252592F, 0.0891028091F,
  0.0893807600F, 0.0896591117F, 0.0899378639F, 0.0902170165F,
  0.0904965692F, 0.0907765218F, 0.0910568740F, 0.0913376258F,
  0.0916187767F, 0.0919003268F, 0.0921822756F, 0.0924646230F,
  0.0927473687F, 0.0930305126F, 0.0933140545F, 0.0935979940F,
  0.0938823310F, 0.0941670653F, 0.0944521966F, 0.0947377247F,
  0.0950236494F, 0.0953099704F, 0.0955966876F, 0.0958838007F,
  0.0961713094F, 0.0964592136F, 0.0967475131F, 0.0970362075F,
  0.0973252967F, 0.0976147805F, 0.0979046585F, 0.0981949307F,
  0.0984855967F, 0.0987766563F, 0.0990681093F, 0.0993599555F,
  0.0996521945F, 0.0999448263F, 0.1002378506F, 0.1005312671F,
  0.1008250755F, 0.1011192757F, 0.1014138675F, 0.1017088505F,
  0.1020042246F, 0.1022999895F, 0.1025961450F, 0.1028926909F,
  0.1031896268F, 0.1034869526F, 0.1037846680F, 0.1040827729F,
  0.1043812668F, 0.1046801497F, 0.1049794213F, 0.1052790813F,
  0.1055791294F, 0.1058795656F, 0.1061803894F, 0.1064816006F,
  0.1067831991F, 0.1070851846F, 0.1073875568F, 0.1076903155F,
  0.1079934604F, 0.1082969913F, 0.1086009079F, 0.1089052101F,
  0.1092098975F, 0.1095149699F, 0.1098204270F, 0.1101262687F,
  0.1104324946F, 0.1107391045F, 0.1110460982F, 0.1113534754F,
  0.1116612359F, 0.1119693793F, 0.1122779055F, 0.1125868142F,
  0.1128961052F, 0.1132057781F, 0.1135158328F, 0.1138262690F,
  0.1141370863F, 0.1144482847F, 0.1147598638F, 0.1150718233F,
  0.1153841631F, 0.1156968828F, 0.1160099822F, 0.1163234610F,
  0.1166373190F, 0.1169515559F, 0.1172661714F, 0.1175811654F,
  0.1178965374F, 0.1182122874F, 0.1185284149F, 0.1188449198F,
  0.1191618018F, 0.1194790606F, 0.1197966960F, 0.1201147076F,
  0.1204330953F, 0.1207518587F, 0.1210709976F, 0.1213905118F,
  0.1217104009F, 0.1220306647F, 0.1223513029F, 0.1226723153F,
  0.1229937016F, 0.1233154615F, 0.1236375948F, 0.1239601011F,
  0.1242829803F, 0.1246062319F, 0.1249298559F, 0.1252538518F,
  0.1255782195F, 0.1259029586F, 0.1262280689F, 0.1265535501F,
  0.1268794019F, 0.1272056241F, 0.1275322163F, 0.1278591784F,
  0.1281865099F, 0.1285142108F, 0.1288422805F, 0.1291707190F,
  0.1294995259F, 0.1298287009F, 0.1301582437F, 0.1304881542F,
  0.1308184319F, 0.1311490766F, 0.1314800881F, 0.1318114660F,
  0.1321432100F, 0.1324753200F, 0.1328077955F, 0.1331406364F,
  0.1334738422F, 0.1338074129F, 0.1341413479F, 0.1344756472F,
  0.1348103103F, 0.1351453370F, 0.1354807270F, 0.1358164801F,
  0.1361525959F, 0.1364890741F, 0.1368259145F, 0.1371631167F,
  0.1375006805F, 0.1378386056F, 0.1381768917F, 0.1385155384F,
  0.1388545456F, 0.1391939129F, 0.1395336400F, 0.1398737266F,
  0.1402141724F, 0.1405549772F, 0.1408961406F, 0.1412376623F,
  0.1415795421F, 0.1419217797F, 0.1422643746F, 0.1426073268F,
  0.1429506358F, 0.1432943013F, 0.1436383231F, 0.1439827008F,
  0.1443274342F, 0.1446725229F, 0.1450179667F, 0.1453637652F,
  0.1457099181F, 0.1460564252F, 0.1464032861F, 0.1467505006F,
  0.1470980682F, 0.1474459888F, 0.1477942620F, 0.1481428875F,
  0.1484918651F, 0.1488411942F, 0.1491908748F, 0.1495409065F,
  0.1498912889F, 0.1502420218F, 0.1505931048F, 0.1509445376F,
  0.1512963200F, 0.1516484516F, 0.1520009321F, 0.1523537612F,
  0.1527069385F, 0.1530604638F, 0.1534143368F, 0.1537685571F,
  0.1541231244F, 0.1544780384F, 0.1548332987F, 0.1551889052F,
  0.1555448574F, 0.1559011550F, 0.1562577978F, 0.1566147853F,
  0.1569721173F, 0.1573297935F, 0.1576878135F, 0.1580461771F,
  0.1584048838F, 0.1587639334F, 0.1591233255F, 0.1594830599F,
  0.1598431361F, 0.1602035540F, 0.1605643131F, 0.1609254131F,
  0.1612868537F, 0.1616486346F, 0.1620107555F, 0.1623732160F,
  0.1627360158F, 0.1630991545F, 0.1634626319F, 0.1638264476F,
  0.1641906013F, 0.1645550926F, 0.1649199212F, 0.1652850869F,
  0.1656505892F, 0.1660164278F, 0.1663826024F, 0.1667491127F,
  0.1671159583F, 0.1674831388F, 0.1678506541F, 0.1682185036F,
  0.1685866872F, 0.1689552044F, 0.1693240549F, 0.1696932384F,
  0.1700627545F, 0.1704326029F, 0.1708027833F, 0.1711732952F,
  0.1715441385F, 0.1719153127F, 0.1722868175F, 0.1726586526F,
  0.1730308176F, 0.1734033121F, 0.1737761359F, 0.1741492886F,
  0.1745227698F, 0.1748965792F, 0.1752707164F, 0.1756451812F,
  0.1760199731F, 0.1763950918F, 0.1767705370F, 0.1771463083F,
  0.1775224054F, 0.1778988279F, 0.1782755754F, 0.1786526477F,
  0.1790300444F, 0.1794077651F, 0.1797858094F, 0.1801641771F,
  0.1805428677F, 0.1809218810F, 0.1813012165F, 0.1816808739F,
  0.1820608528F, 0.1824411530F, 0.1828217739F, 0.1832027154F,
  0.1835839770F, 0.1839655584F, 0.1843474592F, 0.1847296790F,
  0.1851122175F, 0.1854950744F, 0.1858782492F, 0.1862617417F,
  0.1866455514F, 0.1870296780F, 0.1874141211F, 0.1877988804F,
  0.1881839555F, 0.1885693461F, 0.1889550517F, 0.1893410721F,
  0.1897274068F, 0.1901140555F, 0.1905010178F, 0.1908882933F,
  0.1912758818F, 0.1916637828F, 0.1920519959F, 0.1924405208F,
  0.1928293571F, 0.1932185044F, 0.1936079625F, 0.1939977308F,
  0.1943878091F, 0.1947781969F, 0.1951688939F, 0.1955598998F,
  0.1959512141F, 0.1963428364F, 0.1967347665F, 0.1971270038F,
  0.1975195482F, 0.1979123990F, 0.1983055561F, 0.1986990190F,
  0.1990927873F, 0.1994868607F, 0.1998812388F, 0.2002759212F,
  0.2006709075F, 0.2010661974F, 0.2014617904F, 0.2018576862F,
  0.2022538844F, 0.2026503847F, 0.2030471865F, 0.2034442897F,
  0.2038416937F, 0.2042393982F, 0.2046374028F, 0.2050357071F,
  0.2054343107F, 0.2058332133F, 0.2062324145F, 0.2066319138F,
  0.2070317110F, 0.2074318055F, 0.2078321970F, 0.2082328852F,
  0.2086338696F, 0.2090351498F, 0.2094367255F, 0.2098385962F,
  0.2102407617F, 0.2106432213F, 0.2110459749F, 0.2114490220F,
  0.2118523621F, 0.2122559950F, 0.2126599202F, 0.2130641373F,
  0.2134686459F, 0.2138734456F, 0.2142785361F, 0.2146839168F,
  0.2150895875F, 0.2154955478F, 0.2159017972F, 0.2163083353F,
  0.2167151617F, 0.2171222761F, 0.2175296780F, 0.2179373670F,
  0.2183453428F, 0.2187536049F, 0.2191621529F, 0.2195709864F,
  0.2199801051F, 0.2203895085F, 0.2207991961F, 0.2212091677F,
  0.2216194228F, 0.2220299610F, 0.2224407818F, 0.2228518850F,
  0.2232632699F, 0.2236749364F, 0.2240868839F, 0.2244991121F,
  0.2249116204F, 0.2253244086F, 0.2257374763F, 0.2261508229F,
  0.2265644481F, 0.2269783514F, 0.2273925326F, 0.2278069911F,
  0.2282217265F, 0.2286367384F, 0.2290520265F, 0.2294675902F,
  0.2298834292F, 0.2302995431F, 0.2307159314F, 0.2311325937F,
  0.2315495297F, 0.2319667388F, 0.2323842207F, 0.2328019749F,
  0.2332200011F, 0.2336382988F, 0.2340568675F, 0.2344757070F,
  0.2348948166F, 0.2353141961F, 0.2357338450F, 0.2361537629F,
  0.2365739493F, 0.2369944038F, 0.2374151261F, 0.2378361156F,
  0.2382573720F, 0.2386788948F, 0.2391006836F, 0.2395227380F,
  0.2399450575F, 0.2403676417F, 0.2407904902F, 0.2412136026F,
  0.2416369783F, 0.2420606171F, 0.2424845185F, 0.2429086820F,
  0.2433331072F, 0.2437577936F, 0.2441827409F, 0.2446079486F,
  0.2450334163F, 0.2454591435F, 0.2458851298F, 0.2463113747F,
  0.2467378779F, 0.2471646389F, 0.2475916573F, 0.2480189325F,
  0.2484464643F, 0.2488742521F, 0.2493022955F, 0.2497305940F,
  0.2501591473F, 0.2505879549F, 0.2510170163F, 0.2514463311F,
  0.2518758989F, 0.2523057193F, 0.2527357916F, 0.2531661157F,
  0.2535966909F, 0.2540275169F, 0.2544585931F, 0.2548899193F,
  0.2553214948F, 0.2557533193F, 0.2561853924F, 0.2566177135F,
  0.2570502822F, 0.2574830981F, 0.2579161608F, 0.2583494697F,
  0.2587830245F, 0.2592168246F, 0.2596508697F, 0.2600851593F,
  0.2605196929F, 0.2609544701F, 0.2613894904F, 0.2618247534F,
  0.2622602586F, 0.2626960055F, 0.2631319938F, 0.2635682230F,
  0.2640046925F, 0.2644414021F, 0.2648783511F, 0.2653155391F,
  0.2657529657F, 0.2661906305F, 0.2666285329F, 0.2670666725F,
  0.2675050489F, 0.2679436616F, 0.2683825101F, 0.2688215940F,
  0.2692609127F, 0.2697004660F, 0.2701402532F, 0.2705802739F,
  0.2710205278F, 0.2714610142F, 0.2719017327F, 0.2723426830F,
  0.2727838644F, 0.2732252766F, 0.2736669191F, 0.2741087914F,
  0.2745508930F, 0.2749932235F, 0.2754357824F, 0.2758785693F,
  0.2763215837F, 0.2767648251F, 0.2772082930F, 0.2776519870F,
  0.2780959066F, 0.2785400513F, 0.2789844207F, 0.2794290143F,
  0.2798738316F, 0.2803188722F, 0.2807641355F, 0.2812096211F,
  0.2816553286F, 0.2821012574F, 0.2825474071F, 0.2829937773F,
  0.2834403673F, 0.2838871768F, 0.2843342053F, 0.2847814523F,
  0.2852289174F, 0.2856765999F, 0.2861244996F, 0.2865726159F,
  0.2870209482F, 0.2874694962F, 0.2879182594F, 0.2883672372F,
  0.2888164293F, 0.2892658350F, 0.2897154540F, 0.2901652858F,
  0.2906153298F, 0.2910655856F, 0.2915160527F, 0.2919667306F,
  0.2924176189F, 0.2928687171F, 0.2933200246F, 0.2937715409F,
  0.2942232657F, 0.2946751984F, 0.2951273386F, 0.2955796856F,
  0.2960322391F, 0.2964849986F, 0.2969379636F, 0.2973911335F,
  0.2978445080F, 0.2982980864F, 0.2987518684F, 0.2992058534F,
  0.2996600409F, 0.3001144305F, 0.3005690217F, 0.3010238139F,
  0.3014788067F, 0.3019339995F, 0.3023893920F, 0.3028449835F,
  0.3033007736F, 0.3037567618F, 0.3042129477F, 0.3046693306F,
  0.3051259102F, 0.3055826859F, 0.3060396572F, 0.3064968236F,
  0.3069541847F, 0.3074117399F, 0.3078694887F, 0.3083274307F,
  0.3087855653F, 0.3092438920F, 0.3097024104F, 0.3101611199F,
  0.3106200200F, 0.3110791103F, 0.3115383902F, 0.3119978592F,
  0.3124575169F, 0.3129173627F, 0.3133773961F, 0.3138376166F,
  0.3142980238F, 0.3147586170F, 0.3152193959F, 0.3156803598F,
  0.3161415084F, 0.3166028410F, 0.3170643573F, 0.3175260566F,
  0.3179879384F, 0.3184500023F, 0.3189122478F, 0.3193746743F,
  0.3198372814F, 0.3203000685F, 0.3207630351F, 0.3212261807F,
  0.3216895048F, 0.3221530069F, 0.3226166865F, 0.3230805430F,
  0.3235445760F, 0.3240087849F, 0.3244731693F, 0.3249377285F,
  0.3254024622F, 0.3258673698F, 0.3263324507F, 0.3267977045F,
  0.3272631306F, 0.3277287286F, 0.3281944978F, 0.3286604379F,
  0.3291265482F, 0.3295928284F, 0.3300592777F, 0.3305258958F,
  0.3309926821F, 0.3314596361F, 0.3319267573F, 0.3323940451F,
  0.3328614990F, 0.3333291186F, 0.3337969033F, 0.3342648525F,
  0.3347329658F, 0.3352012427F, 0.3356696825F, 0.3361382849F,
  0.3366070492F, 0.3370759749F, 0.3375450616F, 0.3380143087F,
  0.3384837156F, 0.3389532819F, 0.3394230071F, 0.3398928905F,
  0.3403629317F, 0.3408331302F, 0.3413034854F, 0.3417739967F,
  0.3422446638F, 0.3427154860F, 0.3431864628F, 0.3436575938F,
  0.3441288782F, 0.3446003158F, 0.3450719058F, 0.3455436478F,
  0.3460155412F, 0.3464875856F, 0.3469597804F, 0.3474321250F,
  0.3479046189F, 0.3483772617F, 0.3488500527F, 0.3493229914F,
  0.3497960774F, 0.3502693100F, 0.3507426887F, 0.3512162131F,
  0.3516898825F, 0.3521636965F, 0.3526376545F, 0.3531117559F,
  0.3535860003F, 0.3540603870F, 0.3545349157F, 0.3550095856F,
  0.3554843964F, 0.3559593474F, 0.3564344381F, 0.3569096680F,
  0.3573850366F, 0.3578605432F, 0.3583361875F, 0.3588119687F,
  0.3592878865F, 0.3597639402F, 0.3602401293F, 0.3607164533F,
  0.3611929117F, 0.3616695038F, 0.3621462292F, 0.3626230873F,
  0.3631000776F, 0.3635771995F, 0.3640544525F, 0.3645318360F,
  0.3650093496F, 0.3654869926F, 0.3659647645F, 0.3664426648F,
  0.3669206930F, 0.3673988484F, 0.3678771306F, 0.3683555390F,
  0.3688340731F, 0.3693127322F, 0.3697915160F, 0.3702704237F,
  0.3707494549F, 0.3712286091F, 0.3717078857F, 0.3721872840F,
  0.3726668037F, 0.3731464441F, 0.3736262047F, 0.3741060850F,
  0.3745860843F, 0.3750662023F, 0.3755464382F, 0.3760267915F,
  0.3765072618F, 0.3769878484F, 0.3774685509F, 0.3779493686F,
  0.3784303010F, 0.3789113475F, 0.3793925076F, 0.3798737809F,
  0.3803551666F, 0.3808366642F, 0.3813182733F, 0.3817999932F,
  0.3822818234F, 0.3827637633F, 0.3832458124F, 0.3837279702F,
  0.3842102360F, 0.3846926093F, 0.3851750897F, 0.3856576764F,
  0.3861403690F, 0.3866231670F, 0.3871060696F, 0.3875890765F,
  0.3880721870F, 0.3885554007F, 0.3890387168F, 0.3895221349F,
  0.3900056544F, 0.3904892748F, 0.3909729955F, 0.3914568160F,
  0.3919407356F, 0.3924247539F, 0.3929088702F, 0.3933930841F,
  0.3938773949F, 0.3943618021F, 0.3948463052F, 0.3953309035F,
  0.3958155966F, 0.3963003838F, 0.3967852646F, 0.3972702385F,
  0.3977553048F, 0.3982404631F, 0.3987257127F, 0.3992110531F,
  0.3996964838F, 0.4001820041F, 0.4006676136F, 0.4011533116F,
  0.4016390976F, 0.4021249710F, 0.4026109313F, 0.4030969779F,
  0.4035831102F, 0.4040693277F, 0.4045556299F, 0.4050420160F,
  0.4055284857F, 0.4060150383F, 0.4065016732F, 0.4069883899F,
  0.4074751879F, 0.4079620665F, 0.4084490252F, 0.4089360635F,
  0.4094231807F, 0.4099103763F, 0.4103976498F, 0.4108850005F,
  0.4113724280F, 0.4118599315F, 0.4123475107F, 0.4128351648F,
  0.4133228934F, 0.4138106959F, 0.4142985716F, 0.4147865201F,
  0.4152745408F, 0.4157626330F, 0.4162507963F, 0.4167390301F,
  0.4172273337F, 0.4177157067F, 0.4182041484F, 0.4186926583F,
  0.4191812359F, 0.4196698805F, 0.4201585915F, 0.4206473685F,
  0.4211362108F, 0.4216251179F, 0.4221140892F, 0.4226031241F,
  0.4230922221F, 0.4235813826F, 0.4240706050F, 0.4245598887F,
  0.4250492332F, 0.4255386379F, 0.4260281022F, 0.4265176256F,
  0.4270072075F, 0.4274968473F, 0.4279865445F, 0.4284762984F,
  0.4289661086F, 0.4294559743F, 0.4299458951F, 0.4304358704F,
  0.4309258996F, 0.4314159822F, 0.4319061175F, 0.4323963050F,
  0.4328865441F, 0.4333768342F, 0.4338671749F, 0.4343575654F,
  0.4348480052F, 0.4353384938F, 0.4358290306F, 0.4363196149F,
  0.4368102463F, 0.4373009241F, 0.4377916478F, 0.4382824168F,
  0.4387732305F, 0.4392640884F, 0.4397549899F, 0.4402459343F,
  0.4407369212F, 0.4412279499F, 0.4417190198F, 0.4422101305F,
  0.4427012813F, 0.4431924717F, 0.4436837010F, 0.4441749686F,
  0.4446662742F, 0.4451576169F, 0.4456489963F, 0.4461404118F,
  0.4466318628F, 0.4471233487F, 0.4476148690F, 0.4481064230F,
  0.4485980103F, 0.4490896302F, 0.4495812821F, 0.4500729654F,
  0.4505646797F, 0.4510564243F, 0.4515481986F, 0.4520400021F,
  0.4525318341F, 0.4530236942F, 0.4535155816F, 0.4540074959F,
  0.4544994365F, 0.4549914028F, 0.4554833941F, 0.4559754100F,
  0.4564674499F, 0.4569595131F, 0.4574515991F, 0.4579437074F,
  0.4584358372F, 0.4589279881F, 0.4594201595F, 0.4599123508F,
  0.4604045615F, 0.4608967908F, 0.4613890383F, 0.4618813034F,
  0.4623735855F, 0.4628658841F, 0.4633581984F, 0.4638505281F,
  0.4643428724F, 0.4648352308F, 0.4653276028F, 0.4658199877F,
  0.4663123849F, 0.4668047940F, 0.4672972143F, 0.4677896451F,
  0.4682820861F, 0.4687745365F, 0.4692669958F, 0.4697594634F,
  0.4702519387F, 0.4707444211F, 0.4712369102F, 0.4717294052F,
  0.4722219056F, 0.4727144109F, 0.4732069204F, 0.4736994336F,
  0.4741919498F, 0.4746844686F, 0.4751769893F, 0.4756695113F,
  0.4761620341F, 0.4766545571F, 0.4771470797F, 0.4776396013F,
  0.4781321213F, 0.4786246392F, 0.4791171544F, 0.4796096663F,
  0.4801021744F, 0.4805946779F, 0.4810871765F, 0.4815796694F,
  0.4820721561F, 0.4825646360F, 0.4830571086F, 0.4835495732F,
  0.4840420293F, 0.4845344763F, 0.4850269136F, 0.4855193407F,
  0.4860117569F, 0.4865041617F, 0.4869965545F, 0.4874889347F,
  0.4879813018F, 0.4884736551F, 0.4889659941F, 0.4894583182F,
  0.4899506268F, 0.4904429193F, 0.4909351952F, 0.4914274538F,
  0.4919196947F, 0.4924119172F, 0.4929041207F, 0.4933963046F,
  0.4938884685F, 0.4943806116F, 0.4948727335F, 0.4953648335F,
  0.4958569110F, 0.4963489656F, 0.4968409965F, 0.4973330032F,
  0.4978249852F, 0.4983169419F, 0.4988088726F, 0.4993007768F,
  0.4997926539F, 0.5002845034F, 0.5007763247F, 0.5012681171F,
  0.5017598801F, 0.5022516132F, 0.5027433157F, 0.5032349871F,
  0.5037266268F, 0.5042182341F, 0.5047098086F, 0.5052013497F,
  0.5056928567F, 0.5061843292F, 0.5066757664F, 0.5071671679F,
  0.5076585330F, 0.5081498613F, 0.5086411520F, 0.5091324047F,
  0.5096236187F, 0.5101147934F, 0.5106059284F, 0.5110970230F,
  0.5115880766F, 0.5120790887F, 0.5125700587F, 0.5130609860F,
  0.5135518700F, 0.5140427102F, 0.5145335059F, 0.5150242566F,
  0.5155149618F, 0.5160056208F, 0.5164962331F, 0.5169867980F,
  0.5174773151F, 0.5179677837F, 0.5184582033F, 0.5189485733F,
  0.5194388931F, 0.5199291621F, 0.5204193798F, 0.5209095455F,
  0.5213996588F, 0.5218897190F, 0.5223797256F, 0.5228696779F,
  0.5233595755F, 0.5238494177F, 0.5243392039F, 0.5248289337F,
  0.5253186063F, 0.5258082213F, 0.5262977781F, 0.5267872760F,
  0.5272767146F, 0.5277660932F, 0.5282554112F, 0.5287446682F,
  0.5292338635F, 0.5297229965F, 0.5302120667F, 0.5307010736F,
  0.5311900164F, 0.5316788947F, 0.5321677079F, 0.5326564554F,
  0.5331451366F, 0.5336337511F, 0.5341222981F, 0.5346107771F,
  0.5350991876F, 0.5355875290F, 0.5360758007F, 0.5365640021F,
  0.5370521327F, 0.5375401920F, 0.5380281792F, 0.5385160939F,
  0.5390039355F, 0.5394917034F, 0.5399793971F, 0.5404670159F,
  0.5409545594F, 0.5414420269F, 0.5419294179F, 0.5424167318F,
  0.5429039680F, 0.5433911261F, 0.5438782053F, 0.5443652051F,
  0.5448521250F, 0.5453389644F, 0.5458257228F, 0.5463123995F,
  0.5467989940F, 0.5472855057F, 0.5477719341F, 0.5482582786F,
  0.5487445387F, 0.5492307137F, 0.5497168031F, 0.5502028063F,
  0.5506887228F, 0.5511745520F, 0.5516602934F, 0.5521459463F,
  0.5526315103F, 0.5531169847F, 0.5536023690F, 0.5540876626F,
  0.5545728649F, 0.5550579755F, 0.5555429937F, 0.5560279189F,
  0.5565127507F, 0.5569974884F, 0.5574821315F, 0.5579666794F,
  0.5584511316F, 0.5589354875F, 0.5594197465F, 0.5599039080F,
  0.5603879716F, 0.5608719367F, 0.5613558026F, 0.5618395689F,
  0.5623232350F, 0.5628068002F, 0.5632902642F, 0.5637736262F,
  0.5642568858F, 0.5647400423F, 0.5652230953F, 0.5657060442F,
  0.5661888883F, 0.5666716272F, 0.5671542603F, 0.5676367870F,
  0.5681192069F, 0.5686015192F, 0.5690837235F, 0.5695658192F,
  0.5700478058F, 0.5705296827F, 0.5710114494F, 0.5714931052F,
  0.5719746497F, 0.5724560822F, 0.5729374023F, 0.5734186094F,
  0.5738997029F, 0.5743806823F, 0.5748615470F, 0.5753422965F,
  0.5758229301F, 0.5763034475F, 0.5767838480F, 0.5772641310F,
  0.5777442960F, 0.5782243426F, 0.5787042700F, 0.5791840778F,
  0.5796637654F, 0.5801433322F, 0.5806227778F, 0.5811021016F,
  0.5815813029F, 0.5820603814F, 0.5825393363F, 0.5830181673F,
  0.5834968737F, 0.5839754549F, 0.5844539105F, 0.5849322399F,
  0.5854104425F, 0.5858885179F, 0.5863664653F, 0.5868442844F,
  0.5873219746F, 0.5877995353F, 0.5882769660F, 0.5887542661F,
  0.5892314351F, 0.5897084724F, 0.5901853776F, 0.5906621500F,
  0.5911387892F, 0.5916152945F, 0.5920916655F, 0.5925679016F,
  0.5930440022F, 0.5935199669F, 0.5939957950F, 0.5944714861F,
  0.5949470396F, 0.5954224550F, 0.5958977317F, 0.5963728692F,
  0.5968478669F, 0.5973227244F, 0.5977974411F, 0.5982720163F,
  0.5987464497F, 0.5992207407F, 0.5996948887F, 0.6001688932F,
  0.6006427537F, 0.6011164696F, 0.6015900405F, 0.6020634657F,
  0.6025367447F, 0.6030098770F, 0.6034828621F, 0.6039556995F,
  0.6044283885F, 0.6049009288F, 0.6053733196F, 0.6058455606F,
  0.6063176512F, 0.6067895909F, 0.6072613790F, 0.6077330152F,
  0.6082044989F, 0.6086758295F, 0.6091470065F, 0.6096180294F,
  0.6100888977F, 0.6105596108F, 0.6110301682F, 0.6115005694F,
  0.6119708139F, 0.6124409011F, 0.6129108305F, 0.6133806017F,
  0.6138502139F, 0.6143196669F, 0.6147889599F, 0.6152580926F,
  0.6157270643F, 0.6161958746F, 0.6166645230F, 0.6171330088F,
  0.6176013317F, 0.6180694910F, 0.6185374863F, 0.6190053171F,
  0.6194729827F, 0.6199404828F, 0.6204078167F, 0.6208749841F,
  0.6213419842F, 0.6218088168F, 0.6222754811F, 0.6227419768F,
  0.6232083032F, 0.6236744600F, 0.6241404465F, 0.6246062622F,
  0.6250719067F, 0.6255373795F, 0.6260026799F, 0.6264678076F,
  0.6269327619F, 0.6273975425F, 0.6278621487F, 0.6283265800F,
  0.6287908361F, 0.6292549163F, 0.6297188201F, 0.6301825471F,
  0.6306460966F, 0.6311094683F, 0.6315726617F, 0.6320356761F,
  0.6324985111F, 0.6329611662F, 0.6334236410F, 0.6338859348F,
  0.6343480472F, 0.6348099777F, 0.6352717257F, 0.6357332909F,
  0.6361946726F, 0.6366558704F, 0.6371168837F, 0.6375777122F,
  0.6380383552F, 0.6384988123F, 0.6389590830F, 0.6394191668F,
  0.6398790631F, 0.6403387716F, 0.6407982916F, 0.6412576228F,
  0.6417167645F, 0.6421757163F, 0.6426344778F, 0.6430930483F,
  0.6435514275F, 0.6440096149F, 0.6444676098F, 0.6449254119F,
  0.6453830207F, 0.6458404356F, 0.6462976562F, 0.6467546820F,
  0.6472115125F, 0.6476681472F, 0.6481245856F, 0.6485808273F,
  0.6490368717F, 0.6494927183F, 0.6499483667F, 0.6504038164F,
  0.6508590670F, 0.6513141178F, 0.6517689684F, 0.6522236185F,
  0.6526780673F, 0.6531323146F, 0.6535863598F, 0.6540402024F,
  0.6544938419F, 0.6549472779F, 0.6554005099F, 0.6558535373F,
  0.6563063598F, 0.6567589769F, 0.6572113880F, 0.6576635927F,
  0.6581155906F, 0.6585673810F, 0.6590189637F, 0.6594703380F,
  0.6599215035F, 0.6603724598F, 0.6608232064F, 0.6612737427F,
  0.6617240684F, 0.6621741829F, 0.6626240859F, 0.6630737767F,
  0.6635232550F, 0.6639725202F, 0.6644215720F, 0.6648704098F,
  0.6653190332F, 0.6657674417F, 0.6662156348F, 0.6666636121F,
  0.6671113731F, 0.6675589174F, 0.6680062445F, 0.6684533538F,
  0.6689002450F, 0.6693469177F, 0.6697933712F, 0.6702396052F,
  0.6706856193F, 0.6711314129F, 0.6715769855F, 0.6720223369F,
  0.6724674664F, 0.6729123736F, 0.6733570581F, 0.6738015194F,
  0.6742457570F, 0.6746897706F, 0.6751335596F, 0.6755771236F,
  0.6760204621F, 0.6764635747F, 0.6769064609F, 0.6773491204F,
  0.6777915525F, 0.6782337570F, 0.6786757332F, 0.6791174809F,
  0.6795589995F, 0.6800002886F, 0.6804413477F, 0.6808821765F,
  0.6813227743F, 0.6817631409F, 0.6822032758F, 0.6826431785F,
  0.6830828485F, 0.6835222855F, 0.6839614890F, 0.6844004585F,
  0.6848391936F, 0.6852776939F, 0.6857159589F, 0.6861539883F,
  0.6865917815F, 0.6870293381F, 0.6874666576F, 0.6879037398F,
  0.6883405840F, 0.6887771899F, 0.6892135571F, 0.6896496850F,
  0.6900855733F, 0.6905212216F, 0.6909566294F, 0.6913917963F,
  0.6918267218F, 0.6922614055F, 0.6926958471F, 0.6931300459F,
  0.6935640018F, 0.6939977141F, 0.6944311825F, 0.6948644066F,
  0.6952973859F, 0.6957301200F, 0.6961626085F, 0.6965948510F,
  0.6970268470F, 0.6974585961F, 0.6978900980F, 0.6983213521F,
  0.6987523580F, 0.6991831154F, 0.6996136238F, 0.7000438828F,
  0.7004738921F, 0.7009036510F, 0.7013331594F, 0.7017624166F,
  0.7021914224F, 0.7026201763F, 0.7030486779F, 0.7034769268F,
  0.7039049226F, 0.7043326648F, 0.7047601531F, 0.7051873870F,
  0.7056143662F, 0.7060410902F, 0.7064675586F, 0.7068937711F,
  0.7073197271F, 0.7077454264F, 0.7081708684F, 0.7085960529F,
  0.7090209793F, 0.7094456474F, 0.7098700566F, 0.7102942066F,
  0.7107180970F, 0.7111417274F, 0.7115650974F, 0.7119882066F,
  0.7124110545F, 0.7128336409F, 0.7132559653F, 0.7136780272F,
  0.7140998264F, 0.7145213624F, 0.7149426348F, 0.7153636433F,
  0.7157843874F, 0.7162048668F, 0.7166250810F, 0.7170450296F,
  0.7174647124F, 0.7178841289F, 0.7183032786F, 0.7187221613F,
  0.7191407765F, 0.7195591239F, 0.7199772030F, 0.7203950135F,
  0.7208125550F, 0.7212298271F, 0.7216468294F, 0.7220635616F,
  0.7224800233F, 0.7228962140F, 0.7233121335F, 0.7237277813F,
  0.7241431571F, 0.7245582604F, 0.7249730910F, 0.7253876484F,
  0.7258019322F, 0.7262159422F, 0.7266296778F, 0.7270431388F,
  0.7274563247F, 0.7278692353F, 0.7282818700F, 0.7286942287F,
  0.7291063108F, 0.7295181160F, 0.7299296440F, 0.7303408944F,
  0.7307518669F, 0.7311625609F, 0.7315729763F, 0.7319831126F,
  0.7323929695F, 0.7328025466F, 0.7332118435F, 0.7336208600F,
  0.7340295955F, 0.7344380499F, 0.7348462226F, 0.7352541134F,
  0.7356617220F, 0.7360690478F, 0.7364760907F, 0.7368828502F,
  0.7372893259F, 0.7376955176F, 0.7381014249F, 0.7385070475F,
  0.7389123849F, 0.7393174368F, 0.7397222029F, 0.7401266829F,
  0.7405308763F, 0.7409347829F, 0.7413384023F, 0.7417417341F,
  0.7421447780F, 0.7425475338F, 0.7429500009F, 0.7433521791F,
  0.7437540681F, 0.7441556674F, 0.7445569769F, 0.7449579960F,
  0.7453587245F, 0.7457591621F, 0.7461593084F, 0.7465591631F,
  0.7469587259F, 0.7473579963F, 0.7477569741F, 0.7481556590F,
  0.7485540506F, 0.7489521486F, 0.7493499526F, 0.7497474623F,
  0.7501446775F, 0.7505415977F, 0.7509382227F, 0.7513345521F,
  0.7517305856F, 0.7521263229F, 0.7525217636F, 0.7529169074F,
  0.7533117541F, 0.7537063032F, 0.7541005545F, 0.7544945076F,
  0.7548881623F, 0.7552815182F, 0.7556745749F, 0.7560673323F,
  0.7564597899F, 0.7568519474F, 0.7572438046F, 0.7576353611F,
  0.7580266166F, 0.7584175708F, 0.7588082235F, 0.7591985741F,
  0.7595886226F, 0.7599783685F, 0.7603678116F, 0.7607569515F,
  0.7611457879F, 0.7615343206F, 0.7619225493F, 0.7623104735F,
  0.7626980931F, 0.7630854078F, 0.7634724171F, 0.7638591209F,
  0.7642455188F, 0.7646316106F, 0.7650173959F, 0.7654028744F,
  0.7657880459F, 0.7661729100F, 0.7665574664F, 0.7669417150F,
  0.7673256553F, 0.7677092871F, 0.7680926100F, 0.7684756239F,
  0.7688583284F, 0.7692407232F, 0.7696228080F, 0.7700045826F,
  0.7703860467F, 0.7707671999F, 0.7711480420F, 0.7715285728F,
  0.7719087918F, 0.7722886989F, 0.7726682938F, 0.7730475762F,
  0.7734265458F, 0.7738052023F, 0.7741835454F, 0.7745615750F,
  0.7749392906F, 0.7753166921F, 0.7756937791F, 0.7760705514F,
  0.7764470087F, 0.7768231508F, 0.7771989773F, 0.7775744880F,
  0.7779496827F, 0.7783245610F, 0.7786991227F, 0.7790733676F,
  0.7794472953F, 0.7798209056F, 0.7801941982F, 0.7805671729F,
  0.7809398294F, 0.7813121675F, 0.7816841869F, 0.7820558873F,
  0.7824272684F, 0.7827983301F, 0.7831690720F, 0.7835394940F,
  0.7839095957F, 0.7842793768F, 0.7846488373F, 0.7850179767F,
  0.7853867948F, 0.7857552914F, 0.7861234663F, 0.7864913191F,
  0.7868588497F, 0.7872260578F, 0.7875929431F, 0.7879595055F,
  0.7883257445F, 0.7886916601F, 0.7890572520F, 0.7894225198F,
  0.7897874635F, 0.7901520827F, 0.7905163772F, 0.7908803468F,
  0.7912439912F, 0.7916073102F, 0.7919703035F, 0.7923329710F,
  0.7926953124F, 0.7930573274F, 0.7934190158F, 0.7937803774F,
  0.7941414120F, 0.7945021193F, 0.7948624991F, 0.7952225511F,
  0.7955822752F, 0.7959416711F, 0.7963007387F, 0.7966594775F,
  0.7970178875F, 0.7973759685F, 0.7977337201F, 0.7980911422F,
  0.7984482346F, 0.7988049970F, 0.7991614292F, 0.7995175310F,
  0.7998733022F, 0.8002287426F, 0.8005838519F, 0.8009386299F,
  0.8012930765F, 0.8016471914F, 0.8020009744F, 0.8023544253F,
  0.8027075438F, 0.8030603298F, 0.8034127831F, 0.8037649035F,
  0.8041166906F, 0.8044681445F, 0.8048192647F, 0.8051700512F,
  0.8055205038F, 0.8058706222F, 0.8062204062F, 0.8065698556F,
  0.8069189702F, 0.8072677499F, 0.8076161944F, 0.8079643036F,
  0.8083120772F, 0.8086595151F, 0.8090066170F, 0.8093533827F,
  0.8096998122F, 0.8100459051F, 0.8103916613F, 0.8107370806F,
  0.8110821628F, 0.8114269077F, 0.8117713151F, 0.8121153849F,
  0.8124591169F, 0.8128025108F, 0.8131455666F, 0.8134882839F,
  0.8138306627F, 0.8141727027F, 0.8145144038F, 0.8148557658F,
  0.8151967886F, 0.8155374718F, 0.8158778154F, 0.8162178192F,
  0.8165574830F, 0.8168968067F, 0.8172357900F, 0.8175744328F,
  0.8179127349F, 0.8182506962F, 0.8185883164F, 0.8189255955F,
  0.8192625332F, 0.8195991295F, 0.8199353840F, 0.8202712967F,
  0.8206068673F, 0.8209420958F, 0.8212769820F, 0.8216115256F,
  0.8219457266F, 0.8222795848F, 0.8226131000F, 0.8229462721F,
  0.8232791009F, 0.8236115863F, 0.8239437280F, 0.8242755260F,
  0.8246069801F, 0.8249380901F, 0.8252688559F, 0.8255992774F,
  0.8259293544F, 0.8262590867F, 0.8265884741F, 0.8269175167F,
  0.8272462141F, 0.8275745663F, 0.8279025732F, 0.8282302344F,
  0.8285575501F, 0.8288845199F, 0.8292111437F, 0.8295374215F,
  0.8298633530F, 0.8301889382F, 0.8305141768F, 0.8308390688F,
  0.8311636141F, 0.8314878124F, 0.8318116637F, 0.8321351678F,
  0.8324583246F, 0.8327811340F, 0.8331035957F, 0.8334257098F,
  0.8337474761F, 0.8340688944F, 0.8343899647F, 0.8347106867F,
  0.8350310605F, 0.8353510857F, 0.8356707624F, 0.8359900904F,
  0.8363090696F, 0.8366276999F, 0.8369459811F, 0.8372639131F,
  0.8375814958F, 0.8378987292F, 0.8382156130F, 0.8385321472F,
  0.8388483316F, 0.8391641662F, 0.8394796508F, 0.8397947853F,
  0.8401095697F, 0.8404240037F, 0.8407380873F, 0.8410518204F,
  0.8413652029F, 0.8416782347F, 0.8419909156F, 0.8423032456F,
  0.8426152245F, 0.8429268523F, 0.8432381289F, 0.8435490541F,
  0.8438596279F, 0.8441698502F, 0.8444797208F, 0.8447892396F,
  0.8450984067F, 0.8454072218F, 0.8457156849F, 0.8460237959F,
  0.8463315547F, 0.8466389612F, 0.8469460154F, 0.8472527170F,
  0.8475590661F, 0.8478650625F, 0.8481707063F, 0.8484759971F,
  0.8487809351F, 0.8490855201F, 0.8493897521F, 0.8496936308F,
  0.8499971564F, 0.8503003286F, 0.8506031474F, 0.8509056128F,
  0.8512077246F, 0.8515094828F, 0.8518108872F, 0.8521119379F,
  0.8524126348F, 0.8527129777F, 0.8530129666F, 0.8533126015F,
  0.8536118822F, 0.8539108087F, 0.8542093809F, 0.8545075988F,
  0.8548054623F, 0.8551029712F, 0.8554001257F, 0.8556969255F,
  0.8559933707F, 0.8562894611F, 0.8565851968F, 0.8568805775F,
  0.8571756034F, 0.8574702743F, 0.8577645902F, 0.8580585509F,
  0.8583521566F, 0.8586454070F, 0.8589383021F, 0.8592308420F,
  0.8595230265F, 0.8598148556F, 0.8601063292F, 0.8603974473F,
  0.8606882098F, 0.8609786167F, 0.8612686680F, 0.8615583636F,
  0.8618477034F, 0.8621366874F, 0.8624253156F, 0.8627135878F,
  0.8630015042F, 0.8632890646F, 0.8635762690F, 0.8638631173F,
  0.8641496096F, 0.8644357457F, 0.8647215257F, 0.8650069495F,
  0.8652920171F, 0.8655767283F, 0.8658610833F, 0.8661450820F,
  0.8664287243F, 0.8667120102F, 0.8669949397F, 0.8672775127F,
  0.8675597293F, 0.8678415894F, 0.8681230929F, 0.8684042398F,
  0.8686850302F, 0.8689654640F, 0.8692455412F, 0.8695252617F,
  0.8698046255F, 0.8700836327F, 0.8703622831F, 0.8706405768F,
  0.8709185138F, 0.8711960940F, 0.8714733174F, 0.8717501840F,
  0.8720266939F, 0.8723028469F, 0.8725786430F, 0.8728540824F,
  0.8731291648F, 0.8734038905F, 0.8736782592F, 0.8739522711F,
  0.8742259261F, 0.8744992242F, 0.8747721653F, 0.8750447496F,
  0.8753169770F, 0.8755888475F, 0.8758603611F, 0.8761315177F,
  0.8764023175F, 0.8766727603F, 0.8769428462F, 0.8772125752F,
  0.8774819474F, 0.8777509626F, 0.8780196209F, 0.8782879224F,
  0.8785558669F, 0.8788234546F, 0.8790906854F, 0.8793575594F,
  0.8796240765F, 0.8798902368F, 0.8801560403F, 0.8804214870F,
  0.8806865768F, 0.8809513099F, 0.8812156863F, 0.8814797059F,
  0.8817433687F, 0.8820066749F, 0.8822696243F, 0.8825322171F,
  0.8827944532F, 0.8830563327F, 0.8833178556F, 0.8835790219F,
  0.8838398316F, 0.8841002848F, 0.8843603815F, 0.8846201217F,
  0.8848795054F, 0.8851385327F, 0.8853972036F, 0.8856555182F,
  0.8859134764F, 0.8861710783F, 0.8864283239F, 0.8866852133F,
  0.8869417464F, 0.8871979234F, 0.8874537443F, 0.8877092090F,
  0.8879643177F, 0.8882190704F, 0.8884734671F, 0.8887275078F,
  0.8889811927F, 0.8892345216F, 0.8894874948F, 0.8897401122F,
  0.8899923738F, 0.8902442798F, 0.8904958301F, 0.8907470248F,
  0.8909978640F, 0.8912483477F, 0.8914984759F, 0.8917482487F,
  0.8919976662F, 0.8922467284F, 0.8924954353F, 0.8927437871F,
  0.8929917837F, 0.8932394252F, 0.8934867118F, 0.8937336433F,
  0.8939802199F, 0.8942264417F, 0.8944723087F, 0.8947178210F,
  0.8949629785F, 0.8952077815F, 0.8954522299F, 0.8956963239F,
  0.8959400634F, 0.8961834486F, 0.8964264795F, 0.8966691561F,
  0.8969114786F, 0.8971534470F, 0.8973950614F, 0.8976363219F,
  0.8978772284F, 0.8981177812F, 0.8983579802F, 0.8985978256F,
  0.8988373174F, 0.8990764556F, 0.8993152405F, 0.8995536720F,
  0.8997917502F, 0.9000294751F, 0.9002668470F, 0.9005038658F,
  0.9007405317F, 0.9009768446F, 0.9012128048F, 0.9014484123F,
  0.9016836671F, 0.9019185693F, 0.9021531191F, 0.9023873165F,
  0.9026211616F, 0.9028546546F, 0.9030877954F, 0.9033205841F,
  0.9035530210F, 0.9037851059F, 0.9040168392F, 0.9042482207F,
  0.9044792507F, 0.9047099293F, 0.9049402564F, 0.9051702323F,
  0.9053998569F, 0.9056291305F, 0.9058580531F, 0.9060866248F,
  0.9063148457F, 0.9065427159F, 0.9067702355F, 0.9069974046F,
  0.9072242233F, 0.9074506917F, 0.9076768100F, 0.9079025782F,
  0.9081279964F, 0.9083530647F, 0.9085777833F, 0.9088021523F,
  0.9090261717F, 0.9092498417F, 0.9094731623F, 0.9096961338F,
  0.9099187561F, 0.9101410295F, 0.9103629540F, 0.9105845297F,
  0.9108057568F, 0.9110266354F, 0.9112471656F, 0.9114673475F,
  0.9116871812F, 0.9119066668F, 0.9121258046F, 0.9123445945F,
  0.9125630367F, 0.9127811314F, 0.9129988786F, 0.9132162785F,
  0.9134333312F, 0.9136500368F, 0.9138663954F, 0.9140824073F,
  0.9142980724F, 0.9145133910F, 0.9147283632F, 0.9149429890F,
  0.9151572687F, 0.9153712023F, 0.9155847900F, 0.9157980319F,
  0.9160109282F, 0.9162234790F, 0.9164356844F, 0.9166475445F,
  0.9168590595F, 0.9170702296F, 0.9172810548F, 0.9174915354F,
  0.9177016714F, 0.9179114629F, 0.9181209102F, 0.9183300134F,
  0.9185387726F, 0.9187471879F, 0.9189552595F, 0.9191629876F,
  0.9193703723F, 0.9195774136F, 0.9197841119F, 0.9199904672F,
  0.9201964797F, 0.9204021495F, 0.9206074767F, 0.9208124616F,
  0.9210171043F, 0.9212214049F, 0.9214253636F, 0.9216289805F,
  0.9218322558F, 0.9220351896F, 0.9222377821F, 0.9224400335F,
  0.9226419439F, 0.9228435134F, 0.9230447423F, 0.9232456307F,
  0.9234461787F, 0.9236463865F, 0.9238462543F, 0.9240457822F,
  0.9242449704F, 0.9244438190F, 0.9246423282F, 0.9248404983F,
  0.9250383293F, 0.9252358214F, 0.9254329747F, 0.9256297896F,
  0.9258262660F, 0.9260224042F, 0.9262182044F, 0.9264136667F,
  0.9266087913F, 0.9268035783F, 0.9269980280F, 0.9271921405F,
  0.9273859160F, 0.9275793546F, 0.9277724566F, 0.9279652221F,
  0.9281576513F, 0.9283497443F, 0.9285415014F, 0.9287329227F,
  0.9289240084F, 0.9291147586F, 0.9293051737F, 0.9294952536F,
  0.9296849987F, 0.9298744091F, 0.9300634850F, 0.9302522266F,
  0.9304406340F, 0.9306287074F, 0.9308164471F, 0.9310038532F,
  0.9311909259F, 0.9313776654F, 0.9315640719F, 0.9317501455F,
  0.9319358865F, 0.9321212951F, 0.9323063713F, 0.9324911155F,
  0.9326755279F, 0.9328596085F, 0.9330433577F, 0.9332267756F,
  0.9334098623F, 0.9335926182F, 0.9337750434F, 0.9339571380F,
  0.9341389023F, 0.9343203366F, 0.9345014409F, 0.9346822155F,
  0.9348626606F, 0.9350427763F, 0.9352225630F, 0.9354020207F,
  0.9355811498F, 0.9357599503F, 0.9359384226F, 0.9361165667F,
  0.9362943830F, 0.9364718716F, 0.9366490327F, 0.9368258666F,
  0.9370023733F, 0.9371785533F, 0.9373544066F, 0.9375299335F,
  0.9377051341F, 0.9378800087F, 0.9380545576F, 0.9382287809F,
  0.9384026787F, 0.9385762515F, 0.9387494993F, 0.9389224223F,
  0.9390950209F, 0.9392672951F, 0.9394392453F, 0.9396108716F,
  0.9397821743F, 0.9399531536F, 0.9401238096F, 0.9402941427F,
  0.9404641530F, 0.9406338407F, 0.9408032061F, 0.9409722495F,
  0.9411409709F, 0.9413093707F, 0.9414774491F, 0.9416452062F,
  0.9418126424F, 0.9419797579F, 0.9421465528F, 0.9423130274F,
  0.9424791819F, 0.9426450166F, 0.9428105317F, 0.9429757274F,
  0.9431406039F, 0.9433051616F, 0.9434694005F, 0.9436333209F,
  0.9437969232F, 0.9439602074F, 0.9441231739F, 0.9442858229F,
  0.9444481545F, 0.9446101691F, 0.9447718669F, 0.9449332481F,
  0.9450943129F, 0.9452550617F, 0.9454154945F, 0.9455756118F,
  0.9457354136F, 0.9458949003F, 0.9460540721F, 0.9462129292F,
  0.9463714719F, 0.9465297003F, 0.9466876149F, 0.9468452157F,
  0.9470025031F, 0.9471594772F, 0.9473161384F, 0.9474724869F,
  0.9476285229F, 0.9477842466F, 0.9479396584F, 0.9480947585F,
  0.9482495470F, 0.9484040243F, 0.9485581906F, 0.9487120462F,
  0.9488655913F, 0.9490188262F, 0.9491717511F, 0.9493243662F,
  0.9494766718F, 0.9496286683F, 0.9497803557F, 0.9499317345F,
  0.9500828047F, 0.9502335668F, 0.9503840209F, 0.9505341673F,
  0.9506840062F, 0.9508335380F, 0.9509827629F, 0.9511316810F,
  0.9512802928F, 0.9514285984F, 0.9515765982F, 0.9517242923F,
  0.9518716810F, 0.9520187646F, 0.9521655434F, 0.9523120176F,
  0.9524581875F, 0.9526040534F, 0.9527496154F, 0.9528948739F,
  0.9530398292F, 0.9531844814F, 0.9533288310F, 0.9534728780F,
  0.9536166229F, 0.9537600659F, 0.9539032071F, 0.9540460470F,
  0.9541885858F, 0.9543308237F, 0.9544727611F, 0.9546143981F,
  0.9547557351F, 0.9548967723F, 0.9550375100F, 0.9551779485F,
  0.9553180881F, 0.9554579290F, 0.9555974714F, 0.9557367158F,
  0.9558756623F, 0.9560143112F, 0.9561526628F, 0.9562907174F,
  0.9564284752F, 0.9565659366F, 0.9567031017F, 0.9568399710F,
  0.9569765446F, 0.9571128229F, 0.9572488061F, 0.9573844944F,
  0.9575198883F, 0.9576549879F, 0.9577897936F, 0.9579243056F,
  0.9580585242F, 0.9581924497F, 0.9583260824F, 0.9584594226F,
  0.9585924705F, 0.9587252264F, 0.9588576906F, 0.9589898634F,
  0.9591217452F, 0.9592533360F, 0.9593846364F, 0.9595156465F,
  0.9596463666F, 0.9597767971F, 0.9599069382F, 0.9600367901F,
  0.9601663533F, 0.9602956279F, 0.9604246143F, 0.9605533128F,
  0.9606817236F, 0.9608098471F, 0.9609376835F, 0.9610652332F,
  0.9611924963F, 0.9613194733F, 0.9614461644F, 0.9615725699F,
  0.9616986901F, 0.9618245253F, 0.9619500757F, 0.9620753418F,
  0.9622003238F, 0.9623250219F, 0.9624494365F, 0.9625735679F,
  0.9626974163F, 0.9628209821F, 0.9629442656F, 0.9630672671F,
  0.9631899868F, 0.9633124251F, 0.9634345822F, 0.9635564585F,
  0.9636780543F, 0.9637993699F, 0.9639204056F, 0.9640411616F,
  0.9641616383F, 0.9642818359F, 0.9644017549F, 0.9645213955F,
  0.9646407579F, 0.9647598426F, 0.9648786497F, 0.9649971797F,
  0.9651154328F, 0.9652334092F, 0.9653511095F, 0.9654685337F,
  0.9655856823F, 0.9657025556F, 0.9658191538F, 0.9659354773F,
  0.9660515263F, 0.9661673013F, 0.9662828024F, 0.9663980300F,
  0.9665129845F, 0.9666276660F, 0.9667420750F, 0.9668562118F,
  0.9669700766F, 0.9670836698F, 0.9671969917F, 0.9673100425F,
  0.9674228227F, 0.9675353325F, 0.9676475722F, 0.9677595422F,
  0.9678712428F, 0.9679826742F, 0.9680938368F, 0.9682047309F,
  0.9683153569F, 0.9684257150F, 0.9685358056F, 0.9686456289F,
  0.9687551853F, 0.9688644752F, 0.9689734987F, 0.9690822564F,
  0.9691907483F, 0.9692989750F, 0.9694069367F, 0.9695146337F,
  0.9696220663F, 0.9697292349F, 0.9698361398F, 0.9699427813F,
  0.9700491597F, 0.9701552754F, 0.9702611286F, 0.9703667197F,
  0.9704720490F, 0.9705771169F, 0.9706819236F, 0.9707864695F,
  0.9708907549F, 0.9709947802F, 0.9710985456F, 0.9712020514F,
  0.9713052981F, 0.9714082859F, 0.9715110151F, 0.9716134862F,
  0.9717156993F, 0.9718176549F, 0.9719193532F, 0.9720207946F,
  0.9721219794F, 0.9722229080F, 0.9723235806F, 0.9724239976F,
  0.9725241593F, 0.9726240661F, 0.9727237183F, 0.9728231161F,
  0.9729222601F, 0.9730211503F, 0.9731197873F, 0.9732181713F,
  0.9733163027F, 0.9734141817F, 0.9735118088F, 0.9736091842F,
  0.9737063083F, 0.9738031814F, 0.9738998039F, 0.9739961760F,
  0.9740922981F, 0.9741881706F, 0.9742837938F, 0.9743791680F,
  0.9744742935F, 0.9745691707F, 0.9746637999F, 0.9747581814F,
  0.9748523157F, 0.9749462029F, 0.9750398435F, 0.9751332378F,
  0.9752263861F, 0.9753192887F, 0.9754119461F, 0.9755043585F,
  0.9755965262F, 0.9756884496F, 0.9757801291F, 0.9758715650F,
  0.9759627575F, 0.9760537071F, 0.9761444141F, 0.9762348789F,
  0.9763251016F, 0.9764150828F, 0.9765048228F, 0.9765943218F,
  0.9766835802F, 0.9767725984F, 0.9768613767F, 0.9769499154F,
  0.9770382149F, 0.9771262755F, 0.9772140976F, 0.9773016815F,
  0.9773890275F, 0.9774761360F, 0.9775630073F, 0.9776496418F,
  0.9777360398F, 0.9778222016F, 0.9779081277F, 0.9779938182F,
  0.9780792736F, 0.9781644943F, 0.9782494805F, 0.9783342326F,
  0.9784187509F, 0.9785030359F, 0.9785870877F, 0.9786709069F,
  0.9787544936F, 0.9788378484F, 0.9789209714F, 0.9790038631F,
  0.9790865238F, 0.9791689538F, 0.9792511535F, 0.9793331232F,
  0.9794148633F, 0.9794963742F, 0.9795776561F, 0.9796587094F,
  0.9797395345F, 0.9798201316F, 0.9799005013F, 0.9799806437F,
  0.9800605593F, 0.9801402483F, 0.9802197112F, 0.9802989483F,
  0.9803779600F, 0.9804567465F, 0.9805353082F, 0.9806136455F,
  0.9806917587F, 0.9807696482F, 0.9808473143F, 0.9809247574F,
  0.9810019778F, 0.9810789759F, 0.9811557519F, 0.9812323064F,
  0.9813086395F, 0.9813847517F, 0.9814606433F, 0.9815363147F,
  0.9816117662F, 0.9816869981F, 0.9817620108F, 0.9818368047F,
  0.9819113801F, 0.9819857374F, 0.9820598769F, 0.9821337989F,
  0.9822075038F, 0.9822809920F, 0.9823542638F, 0.9824273195F,
  0.9825001596F, 0.9825727843F, 0.9826451940F, 0.9827173891F,
  0.9827893700F, 0.9828611368F, 0.9829326901F, 0.9830040302F,
  0.9830751574F, 0.9831460720F, 0.9832167745F, 0.9832872652F,
  0.9833575444F, 0.9834276124F, 0.9834974697F, 0.9835671166F,
  0.9836365535F, 0.9837057806F, 0.9837747983F, 0.9838436071F,
  0.9839122072F, 0.9839805990F, 0.9840487829F, 0.9841167591F,
  0.9841845282F, 0.9842520903F, 0.9843194459F, 0.9843865953F,
  0.9844535389F, 0.9845202771F, 0.9845868101F, 0.9846531383F,
  0.9847192622F, 0.9847851820F, 0.9848508980F, 0.9849164108F,
  0.9849817205F, 0.9850468276F, 0.9851117324F, 0.9851764352F,
  0.9852409365F, 0.9853052366F, 0.9853693358F, 0.9854332344F,
  0.9854969330F, 0.9855604317F, 0.9856237309F, 0.9856868310F,
  0.9857497325F, 0.9858124355F, 0.9858749404F, 0.9859372477F,
  0.9859993577F, 0.9860612707F, 0.9861229871F, 0.9861845072F,
  0.9862458315F, 0.9863069601F, 0.9863678936F, 0.9864286322F,
  0.9864891764F, 0.9865495264F, 0.9866096826F, 0.9866696454F,
  0.9867294152F, 0.9867889922F, 0.9868483769F, 0.9869075695F,
  0.9869665706F, 0.9870253803F, 0.9870839991F, 0.9871424273F,
  0.9872006653F, 0.9872587135F, 0.9873165721F, 0.9873742415F,
  0.9874317222F, 0.9874890144F, 0.9875461185F, 0.9876030348F,
  0.9876597638F, 0.9877163057F, 0.9877726610F, 0.9878288300F,
  0.9878848130F, 0.9879406104F, 0.9879962225F, 0.9880516497F,
  0.9881068924F, 0.9881619509F, 0.9882168256F, 0.9882715168F,
  0.9883260249F, 0.9883803502F, 0.9884344931F, 0.9884884539F,
  0.9885422331F, 0.9885958309F, 0.9886492477F, 0.9887024838F,
  0.9887555397F, 0.9888084157F, 0.9888611120F, 0.9889136292F,
  0.9889659675F, 0.9890181273F, 0.9890701089F, 0.9891219128F,
  0.9891735392F, 0.9892249885F, 0.9892762610F, 0.9893273572F,
  0.9893782774F, 0.9894290219F, 0.9894795911F, 0.9895299853F,
  0.9895802049F, 0.9896302502F, 0.9896801217F, 0.9897298196F,
  0.9897793443F, 0.9898286961F, 0.9898778755F, 0.9899268828F,
  0.9899757183F, 0.9900243823F, 0.9900728753F, 0.9901211976F,
  0.9901693495F, 0.9902173314F, 0.9902651436F, 0.9903127865F,
  0.9903602605F, 0.9904075659F, 0.9904547031F, 0.9905016723F,
  0.9905484740F, 0.9905951086F, 0.9906415763F, 0.9906878775F,
  0.9907340126F, 0.9907799819F, 0.9908257858F, 0.9908714247F,
  0.9909168988F, 0.9909622086F, 0.9910073543F, 0.9910523364F,
  0.9910971552F, 0.9911418110F, 0.9911863042F, 0.9912306351F,
  0.9912748042F, 0.9913188117F, 0.9913626580F, 0.9914063435F,
  0.9914498684F, 0.9914932333F, 0.9915364383F, 0.9915794839F,
  0.9916223703F, 0.9916650981F, 0.9917076674F, 0.9917500787F,
  0.9917923323F, 0.9918344286F, 0.9918763679F, 0.9919181505F,
  0.9919597769F, 0.9920012473F, 0.9920425621F, 0.9920837217F,
  0.9921247263F, 0.9921655765F, 0.9922062724F, 0.9922468145F,
  0.9922872030F, 0.9923274385F, 0.9923675211F, 0.9924074513F,
  0.9924472294F, 0.9924868557F, 0.9925263306F, 0.9925656544F,
  0.9926048275F, 0.9926438503F, 0.9926827230F, 0.9927214461F,
  0.9927600199F, 0.9927984446F, 0.9928367208F, 0.9928748486F,
  0.9929128285F, 0.9929506608F, 0.9929883459F, 0.9930258841F,
  0.9930632757F, 0.9931005211F, 0.9931376207F, 0.9931745747F,
  0.9932113836F, 0.9932480476F, 0.9932845671F, 0.9933209425F,
  0.9933571742F, 0.9933932623F, 0.9934292074F, 0.9934650097F,
  0.9935006696F, 0.9935361874F, 0.9935715635F, 0.9936067982F,
  0.9936418919F, 0.9936768448F, 0.9937116574F, 0.9937463300F,
  0.9937808629F, 0.9938152565F, 0.9938495111F, 0.9938836271F,
  0.9939176047F, 0.9939514444F, 0.9939851465F, 0.9940187112F,
  0.9940521391F, 0.9940854303F, 0.9941185853F, 0.9941516044F,
  0.9941844879F, 0.9942172361F, 0.9942498495F, 0.9942823283F,
  0.9943146729F, 0.9943468836F, 0.9943789608F, 0.9944109047F,
  0.9944427158F, 0.9944743944F, 0.9945059408F, 0.9945373553F,
  0.9945686384F, 0.9945997902F, 0.9946308112F, 0.9946617017F,
  0.9946924621F, 0.9947230926F, 0.9947535937F, 0.9947839656F,
  0.9948142086F, 0.9948443232F, 0.9948743097F, 0.9949041683F,
  0.9949338995F, 0.9949635035F, 0.9949929807F, 0.9950223315F,
  0.9950515561F, 0.9950806549F, 0.9951096282F, 0.9951384764F,
  0.9951671998F, 0.9951957987F, 0.9952242735F, 0.9952526245F,
  0.9952808520F, 0.9953089564F, 0.9953369380F, 0.9953647971F,
  0.9953925340F, 0.9954201491F, 0.9954476428F, 0.9954750153F,
  0.9955022670F, 0.9955293981F, 0.9955564092F, 0.9955833003F,
  0.9956100720F, 0.9956367245F, 0.9956632582F, 0.9956896733F,
  0.9957159703F, 0.9957421494F, 0.9957682110F, 0.9957941553F,
  0.9958199828F, 0.9958456937F, 0.9958712884F, 0.9958967672F,
  0.9959221305F, 0.9959473784F, 0.9959725115F, 0.9959975300F,
  0.9960224342F, 0.9960472244F, 0.9960719011F, 0.9960964644F,
  0.9961209148F, 0.9961452525F, 0.9961694779F, 0.9961935913F,
  0.9962175930F, 0.9962414834F, 0.9962652627F, 0.9962889313F,
  0.9963124895F, 0.9963359377F, 0.9963592761F, 0.9963825051F,
  0.9964056250F, 0.9964286361F, 0.9964515387F, 0.9964743332F,
  0.9964970198F, 0.9965195990F, 0.9965420709F, 0.9965644360F,
  0.9965866946F, 0.9966088469F, 0.9966308932F, 0.9966528340F,
  0.9966746695F, 0.9966964001F, 0.9967180260F, 0.9967395475F,
  0.9967609651F, 0.9967822789F, 0.9968034894F, 0.9968245968F,
  0.9968456014F, 0.9968665036F, 0.9968873037F, 0.9969080019F,
  0.9969285987F, 0.9969490942F, 0.9969694889F, 0.9969897830F,
  0.9970099769F, 0.9970300708F, 0.9970500651F, 0.9970699601F,
  0.9970897561F, 0.9971094533F, 0.9971290522F, 0.9971485531F,
  0.9971679561F, 0.9971872617F, 0.9972064702F, 0.9972255818F,
  0.9972445968F, 0.9972635157F, 0.9972823386F, 0.9973010659F,
  0.9973196980F, 0.9973382350F, 0.9973566773F, 0.9973750253F,
  0.9973932791F, 0.9974114392F, 0.9974295059F, 0.9974474793F,
  0.9974653599F, 0.9974831480F, 0.9975008438F, 0.9975184476F,
  0.9975359598F, 0.9975533806F, 0.9975707104F, 0.9975879495F,
  0.9976050981F, 0.9976221566F, 0.9976391252F, 0.9976560043F,
  0.9976727941F, 0.9976894950F, 0.9977061073F, 0.9977226312F,
  0.9977390671F, 0.9977554152F, 0.9977716759F, 0.9977878495F,
  0.9978039361F, 0.9978199363F, 0.9978358501F, 0.9978516780F,
  0.9978674202F, 0.9978830771F, 0.9978986488F, 0.9979141358F,
  0.9979295383F, 0.9979448566F, 0.9979600909F, 0.9979752417F,
  0.9979903091F, 0.9980052936F, 0.9980201952F, 0.9980350145F,
  0.9980497515F, 0.9980644067F, 0.9980789804F, 0.9980934727F,
  0.9981078841F, 0.9981222147F, 0.9981364649F, 0.9981506350F,
  0.9981647253F, 0.9981787360F, 0.9981926674F, 0.9982065199F,
  0.9982202936F, 0.9982339890F, 0.9982476062F, 0.9982611456F,
  0.9982746074F, 0.9982879920F, 0.9983012996F, 0.9983145304F,
  0.9983276849F, 0.9983407632F, 0.9983537657F, 0.9983666926F,
  0.9983795442F, 0.9983923208F, 0.9984050226F, 0.9984176501F,
  0.9984302033F, 0.9984426827F, 0.9984550884F, 0.9984674208F,
  0.9984796802F, 0.9984918667F, 0.9985039808F, 0.9985160227F,
  0.9985279926F, 0.9985398909F, 0.9985517177F, 0.9985634734F,
  0.9985751583F, 0.9985867727F, 0.9985983167F, 0.9986097907F,
  0.9986211949F, 0.9986325297F, 0.9986437953F, 0.9986549919F,
  0.9986661199F, 0.9986771795F, 0.9986881710F, 0.9986990946F,
  0.9987099507F, 0.9987207394F, 0.9987314611F, 0.9987421161F,
  0.9987527045F, 0.9987632267F, 0.9987736829F, 0.9987840734F,
  0.9987943985F, 0.9988046584F, 0.9988148534F, 0.9988249838F,
  0.9988350498F, 0.9988450516F, 0.9988549897F, 0.9988648641F,
  0.9988746753F, 0.9988844233F, 0.9988941086F, 0.9989037313F,
  0.9989132918F, 0.9989227902F, 0.9989322269F, 0.9989416021F,
  0.9989509160F, 0.9989601690F, 0.9989693613F, 0.9989784931F,
  0.9989875647F, 0.9989965763F, 0.9990055283F, 0.9990144208F,
  0.9990232541F, 0.9990320286F, 0.9990407443F, 0.9990494016F,
  0.9990580008F, 0.9990665421F, 0.9990750257F, 0.9990834519F,
  0.9990918209F, 0.9991001331F, 0.9991083886F, 0.9991165877F,
  0.9991247307F, 0.9991328177F, 0.9991408491F, 0.9991488251F,
  0.9991567460F, 0.9991646119F, 0.9991724232F, 0.9991801801F,
  0.9991878828F, 0.9991955316F, 0.9992031267F, 0.9992106684F,
  0.9992181569F, 0.9992255925F, 0.9992329753F, 0.9992403057F,
  0.9992475839F, 0.9992548101F, 0.9992619846F, 0.9992691076F,
  0.9992761793F, 0.9992832001F, 0.9992901701F, 0.9992970895F,
  0.9993039587F, 0.9993107777F, 0.9993175470F, 0.9993242667F,
  0.9993309371F, 0.9993375583F, 0.9993441307F, 0.9993506545F,
  0.9993571298F, 0.9993635570F, 0.9993699362F, 0.9993762678F,
  0.9993825519F, 0.9993887887F, 0.9993949785F, 0.9994011216F,
  0.9994072181F, 0.9994132683F, 0.9994192725F, 0.9994252307F,
  0.9994311434F, 0.9994370107F, 0.9994428327F, 0.9994486099F,
  0.9994543423F, 0.9994600303F, 0.9994656739F, 0.9994712736F,
  0.9994768294F, 0.9994823417F, 0.9994878105F, 0.9994932363F,
  0.9994986191F, 0.9995039592F, 0.9995092568F, 0.9995145122F,
  0.9995197256F, 0.9995248971F, 0.9995300270F, 0.9995351156F,
  0.9995401630F, 0.9995451695F, 0.9995501352F, 0.9995550604F,
  0.9995599454F, 0.9995647903F, 0.9995695953F, 0.9995743607F,
  0.9995790866F, 0.9995837734F, 0.9995884211F, 0.9995930300F,
  0.9995976004F, 0.9996021324F, 0.9996066263F, 0.9996110822F,
  0.9996155004F, 0.9996198810F, 0.9996242244F, 0.9996285306F,
  0.9996327999F, 0.9996370326F, 0.9996412287F, 0.9996453886F,
  0.9996495125F, 0.9996536004F, 0.9996576527F, 0.9996616696F,
  0.9996656512F, 0.9996695977F, 0.9996735094F, 0.9996773865F,
  0.9996812291F, 0.9996850374F, 0.9996888118F, 0.9996925523F,
  0.9996962591F, 0.9996999325F, 0.9997035727F, 0.9997071798F,
  0.9997107541F, 0.9997142957F, 0.9997178049F, 0.9997212818F,
  0.9997247266F, 0.9997281396F, 0.9997315209F, 0.9997348708F,
  0.9997381893F, 0.9997414767F, 0.9997447333F, 0.9997479591F,
  0.9997511544F, 0.9997543194F, 0.9997574542F, 0.9997605591F,
  0.9997636342F, 0.9997666797F, 0.9997696958F, 0.9997726828F,
  0.9997756407F, 0.9997785698F, 0.9997814703F, 0.9997843423F,
  0.9997871860F, 0.9997900016F, 0.9997927894F, 0.9997955494F,
  0.9997982818F, 0.9998009869F, 0.9998036648F, 0.9998063157F,
  0.9998089398F, 0.9998115373F, 0.9998141082F, 0.9998166529F,
  0.9998191715F, 0.9998216642F, 0.9998241311F, 0.9998265724F,
  0.9998289884F, 0.9998313790F, 0.9998337447F, 0.9998360854F,
  0.9998384015F, 0.9998406930F, 0.9998429602F, 0.9998452031F,
  0.9998474221F, 0.9998496171F, 0.9998517885F, 0.9998539364F,
  0.9998560610F, 0.9998581624F, 0.9998602407F, 0.9998622962F,
  0.9998643291F, 0.9998663394F, 0.9998683274F, 0.9998702932F,
  0.9998722370F, 0.9998741589F, 0.9998760591F, 0.9998779378F,
  0.9998797952F, 0.9998816313F, 0.9998834464F, 0.9998852406F,
  0.9998870141F, 0.9998887670F, 0.9998904995F, 0.9998922117F,
  0.9998939039F, 0.9998955761F, 0.9998972285F, 0.9998988613F,
  0.9999004746F, 0.9999020686F, 0.9999036434F, 0.9999051992F,
  0.9999067362F, 0.9999082544F, 0.9999097541F, 0.9999112354F,
  0.9999126984F, 0.9999141433F, 0.9999155703F, 0.9999169794F,
  0.9999183709F, 0.9999197449F, 0.9999211014F, 0.9999224408F,
  0.9999237631F, 0.9999250684F, 0.9999263570F, 0.9999276289F,
  0.9999288843F, 0.9999301233F, 0.9999313461F, 0.9999325529F,
  0.9999337437F, 0.9999349187F, 0.9999360780F, 0.9999372218F,
  0.9999383503F, 0.9999394635F, 0.9999405616F, 0.9999416447F,
  0.9999427129F, 0.9999437665F, 0.9999448055F, 0.9999458301F,
  0.9999468404F, 0.9999478365F, 0.9999488185F, 0.9999497867F,
  0.9999507411F, 0.9999516819F, 0.9999526091F, 0.9999535230F,
  0.9999544236F, 0.9999553111F, 0.9999561856F, 0.9999570472F,
  0.9999578960F, 0.9999587323F, 0.9999595560F, 0.9999603674F,
  0.9999611666F, 0.9999619536F, 0.9999627286F, 0.9999634917F,
  0.9999642431F, 0.9999649828F, 0.9999657110F, 0.9999664278F,
  0.9999671334F, 0.9999678278F, 0.9999685111F, 0.9999691835F,
  0.9999698451F, 0.9999704960F, 0.9999711364F, 0.9999717662F,
  0.9999723858F, 0.9999729950F, 0.9999735942F, 0.9999741834F,
  0.9999747626F, 0.9999753321F, 0.9999758919F, 0.9999764421F,
  0.9999769828F, 0.9999775143F, 0.9999780364F, 0.9999785495F,
  0.9999790535F, 0.9999795485F, 0.9999800348F, 0.9999805124F,
  0.9999809813F, 0.9999814417F, 0.9999818938F, 0.9999823375F,
  0.9999827731F, 0.9999832005F, 0.9999836200F, 0.9999840316F,
  0.9999844353F, 0.9999848314F, 0.9999852199F, 0.9999856008F,
  0.9999859744F, 0.9999863407F, 0.9999866997F, 0.9999870516F,
  0.9999873965F, 0.9999877345F, 0.9999880656F, 0.9999883900F,
  0.9999887078F, 0.9999890190F, 0.9999893237F, 0.9999896220F,
  0.9999899140F, 0.9999901999F, 0.9999904796F, 0.9999907533F,
  0.9999910211F, 0.9999912830F, 0.9999915391F, 0.9999917896F,
  0.9999920345F, 0.9999922738F, 0.9999925077F, 0.9999927363F,
  0.9999929596F, 0.9999931777F, 0.9999933907F, 0.9999935987F,
  0.9999938018F, 0.9999940000F, 0.9999941934F, 0.9999943820F,
  0.9999945661F, 0.9999947456F, 0.9999949206F, 0.9999950912F,
  0.9999952575F, 0.9999954195F, 0.9999955773F, 0.9999957311F,
  0.9999958807F, 0.9999960265F, 0.9999961683F, 0.9999963063F,
  0.9999964405F, 0.9999965710F, 0.9999966979F, 0.9999968213F,
  0.9999969412F, 0.9999970576F, 0.9999971707F, 0.9999972805F,
  0.9999973871F, 0.9999974905F, 0.9999975909F, 0.9999976881F,
  0.9999977824F, 0.9999978738F, 0.9999979624F, 0.9999980481F,
  0.9999981311F, 0.9999982115F, 0.9999982892F, 0.9999983644F,
  0.9999984370F, 0.9999985072F, 0.9999985750F, 0.9999986405F,
  0.9999987037F, 0.9999987647F, 0.9999988235F, 0.9999988802F,
  0.9999989348F, 0.9999989873F, 0.9999990379F, 0.9999990866F,
  0.9999991334F, 0.9999991784F, 0.9999992217F, 0.9999992632F,
  0.9999993030F, 0.9999993411F, 0.9999993777F, 0.9999994128F,
  0.9999994463F, 0.9999994784F, 0.9999995091F, 0.9999995384F,
  0.9999995663F, 0.9999995930F, 0.9999996184F, 0.9999996426F,
  0.9999996657F, 0.9999996876F, 0.9999997084F, 0.9999997282F,
  0.9999997469F, 0.9999997647F, 0.9999997815F, 0.9999997973F,
  0.9999998123F, 0.9999998265F, 0.9999998398F, 0.9999998524F,
  0.9999998642F, 0.9999998753F, 0.9999998857F, 0.9999998954F,
  0.9999999045F, 0.9999999130F, 0.9999999209F, 0.9999999282F,
  0.9999999351F, 0.9999999414F, 0.9999999472F, 0.9999999526F,
  0.9999999576F, 0.9999999622F, 0.9999999664F, 0.9999999702F,
  0.9999999737F, 0.9999999769F, 0.9999999798F, 0.9999999824F,
  0.9999999847F, 0.9999999868F, 0.9999999887F, 0.9999999904F,
  0.9999999919F, 0.9999999932F, 0.9999999943F, 0.9999999953F,
  0.9999999961F, 0.9999999969F, 0.9999999975F, 0.9999999980F,
  0.9999999985F, 0.9999999988F, 0.9999999991F, 0.9999999993F,
  0.9999999995F, 0.9999999997F, 0.9999999998F, 0.9999999999F,
  0.9999999999F, 1.0000000000F, 1.0000000000F, 1.0000000000F,
  1.0000000000F, 1.0000000000F, 1.0000000000F, 1.0000000000F,
};

static float *vwin[8] = {
  vwin64,
  vwin128,
  vwin256,
  vwin512,
  vwin1024,
  vwin2048,
  vwin4096,
  vwin8192,
};

float *_vorbis_window_get(int n){
  return vwin[n];
}

void _vorbis_apply_window(float *d,int *winno,long *blocksizes,
			  int lW,int W,int nW){
  lW=(W?lW:0);
  nW=(W?nW:0);

  {
	float *windowLW=vwin[winno[lW]];
	float *windowNW=vwin[winno[nW]];

	long n=blocksizes[W];
	long ln=blocksizes[lW];
	long rn=blocksizes[nW];

	long leftbegin=n/4-ln/4;
	long leftend=leftbegin+ln/2;

	long rightbegin=n/2+n/4-rn/4;
	long rightend=rightbegin+rn/2;

	int i,p;

	for(i=0;i<leftbegin;i++)
	  d[i]=0.f;

	for(p=0;i<leftend;i++,p++)
	  d[i]*=windowLW[p];

	for(i=rightbegin,p=rn/2-1;i<rightend;i++,p--)
	  d[i]*=windowNW[p];

	for(;i<n;i++)
	  d[i]=0.f;
  }
}

#endif
/*** End of inlined file: window.c ***/

#else
 #include <vorbis/vorbisenc.h>
 #include <vorbis/codec.h>
 #include <vorbis/vorbisfile.h>
#endif
}

#undef max
#undef min

BEGIN_JUCE_NAMESPACE

static const char* const oggFormatName = "Ogg-Vorbis file";
static const juce_wchar* const oggExtensions[] = { T(".ogg"), 0 };

class OggReader : public AudioFormatReader
{
	OggVorbisNamespace::OggVorbis_File ovFile;
	OggVorbisNamespace::ov_callbacks callbacks;
	AudioSampleBuffer reservoir;
	int reservoirStart, samplesInReservoir;

public:

	OggReader (InputStream* const inp)
		: AudioFormatReader (inp, TRANS (oggFormatName)),
		  reservoir (2, 4096),
		  reservoirStart (0),
		  samplesInReservoir (0)
	{
		using namespace OggVorbisNamespace;
		sampleRate = 0;
		usesFloatingPointData = true;

		callbacks.read_func = &oggReadCallback;
		callbacks.seek_func = &oggSeekCallback;
		callbacks.close_func = &oggCloseCallback;
		callbacks.tell_func = &oggTellCallback;

		const int err = ov_open_callbacks (input, &ovFile, 0, 0, callbacks);

		if (err == 0)
		{
			vorbis_info* info = ov_info (&ovFile, -1);
			lengthInSamples = (uint32) ov_pcm_total (&ovFile, -1);
			numChannels = info->channels;
			bitsPerSample = 16;
			sampleRate = info->rate;

			reservoir.setSize (numChannels,
							   (int) jmin (lengthInSamples, (int64) reservoir.getNumSamples()));
		}
	}

	~OggReader()
	{
		OggVorbisNamespace::ov_clear (&ovFile);
	}

	bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
					  int64 startSampleInFile, int numSamples)
	{
		while (numSamples > 0)
		{
			const int numAvailable = reservoirStart + samplesInReservoir - startSampleInFile;

			if (startSampleInFile >= reservoirStart && numAvailable > 0)
			{
				// got a few samples overlapping, so use them before seeking..

				const int numToUse = jmin (numSamples, numAvailable);

				for (int i = jmin (numDestChannels, reservoir.getNumChannels()); --i >= 0;)
					if (destSamples[i] != 0)
						memcpy (destSamples[i] + startOffsetInDestBuffer,
								reservoir.getSampleData (i, (int) (startSampleInFile - reservoirStart)),
								sizeof (float) * numToUse);

				startSampleInFile += numToUse;
				numSamples -= numToUse;
				startOffsetInDestBuffer += numToUse;

				if (numSamples == 0)
					break;
			}

			if (startSampleInFile < reservoirStart
				|| startSampleInFile + numSamples > reservoirStart + samplesInReservoir)
			{
				// buffer miss, so refill the reservoir
				int bitStream = 0;

				reservoirStart = jmax (0, (int) startSampleInFile);
				samplesInReservoir = reservoir.getNumSamples();

				if (reservoirStart != (int) OggVorbisNamespace::ov_pcm_tell (&ovFile))
					OggVorbisNamespace::ov_pcm_seek (&ovFile, reservoirStart);

				int offset = 0;
				int numToRead = samplesInReservoir;

				while (numToRead > 0)
				{
					float** dataIn = 0;

					const int samps = OggVorbisNamespace::ov_read_float (&ovFile, &dataIn, numToRead, &bitStream);
					if (samps <= 0)
						break;

					jassert (samps <= numToRead);

					for (int i = jmin ((int) numChannels, reservoir.getNumChannels()); --i >= 0;)
					{
						memcpy (reservoir.getSampleData (i, offset),
								dataIn[i],
								sizeof (float) * samps);
					}

					numToRead -= samps;
					offset += samps;
				}

				if (numToRead > 0)
					reservoir.clear (offset, numToRead);
			}
		}

		if (numSamples > 0)
		{
			for (int i = numDestChannels; --i >= 0;)
				if (destSamples[i] != 0)
					zeromem (destSamples[i] + startOffsetInDestBuffer,
							 sizeof (int) * numSamples);
		}

		return true;
	}

	static size_t oggReadCallback (void* ptr, size_t size, size_t nmemb, void* datasource)
	{
		return (size_t) (static_cast <InputStream*> (datasource)->read (ptr, (int) (size * nmemb)) / size);
	}

	static int oggSeekCallback (void* datasource, OggVorbisNamespace::ogg_int64_t offset, int whence)
	{
		InputStream* const in = static_cast <InputStream*> (datasource);

		if (whence == SEEK_CUR)
			offset += in->getPosition();
		else if (whence == SEEK_END)
			offset += in->getTotalLength();

		in->setPosition (offset);
		return 0;
	}

	static int oggCloseCallback (void*)
	{
		return 0;
	}

	static long oggTellCallback (void* datasource)
	{
		return (long) static_cast <InputStream*> (datasource)->getPosition();
	}

	juce_UseDebuggingNewOperator
};

class OggWriter  : public AudioFormatWriter
{
	OggVorbisNamespace::ogg_stream_state os;
	OggVorbisNamespace::ogg_page og;
	OggVorbisNamespace::ogg_packet op;
	OggVorbisNamespace::vorbis_info vi;
	OggVorbisNamespace::vorbis_comment vc;
	OggVorbisNamespace::vorbis_dsp_state vd;
	OggVorbisNamespace::vorbis_block vb;

public:
	bool ok;

	OggWriter (OutputStream* const out,
			   const double sampleRate,
			   const int numChannels,
			   const int bitsPerSample,
			   const int qualityIndex)
		: AudioFormatWriter (out, TRANS (oggFormatName),
							 sampleRate,
							 numChannels,
							 bitsPerSample)
	{
		using namespace OggVorbisNamespace;
		ok = false;

		vorbis_info_init (&vi);

		if (vorbis_encode_init_vbr (&vi,
									numChannels,
									(int) sampleRate,
									jlimit (0.0f, 1.0f, qualityIndex * 0.5f)) == 0)
		{
			vorbis_comment_init (&vc);

			if (JUCEApplication::getInstance() != 0)
				vorbis_comment_add_tag (&vc, "ENCODER", const_cast <char*> (JUCEApplication::getInstance()->getApplicationName().toUTF8()));

			vorbis_analysis_init (&vd, &vi);
			vorbis_block_init (&vd, &vb);

			ogg_stream_init (&os, Random::getSystemRandom().nextInt());

			ogg_packet header;
			ogg_packet header_comm;
			ogg_packet header_code;

			vorbis_analysis_headerout (&vd, &vc, &header, &header_comm, &header_code);

			ogg_stream_packetin (&os, &header);
			ogg_stream_packetin (&os, &header_comm);
			ogg_stream_packetin (&os, &header_code);

			for (;;)
			{
				if (ogg_stream_flush (&os, &og) == 0)
					break;

				output->write (og.header, og.header_len);
				output->write (og.body, og.body_len);
			}

			ok = true;
		}
	}

	~OggWriter()
	{
		using namespace OggVorbisNamespace;
		if (ok)
		{
			// write a zero-length packet to show ogg that we're finished..
			write (0, 0);

			ogg_stream_clear (&os);
			vorbis_block_clear (&vb);
			vorbis_dsp_clear (&vd);
			vorbis_comment_clear (&vc);

			vorbis_info_clear (&vi);
			output->flush();
		}
		else
		{
			vorbis_info_clear (&vi);
			output = 0; // to stop the base class deleting this, as it needs to be returned
						// to the caller of createWriter()
		}
	}

	bool write (const int** samplesToWrite, int numSamples)
	{
		using namespace OggVorbisNamespace;
		if (! ok)
			return false;

		if (numSamples > 0)
		{
			const double gain = 1.0 / 0x80000000u;
			float** const vorbisBuffer = vorbis_analysis_buffer (&vd, numSamples);

			for (int i = numChannels; --i >= 0;)
			{
				float* const dst = vorbisBuffer[i];
				const int* const src = samplesToWrite [i];

				if (src != 0 && dst != 0)
				{
					for (int j = 0; j < numSamples; ++j)
						dst[j] = (float) (src[j] * gain);
				}
			}
		}

		vorbis_analysis_wrote (&vd, numSamples);

		while (vorbis_analysis_blockout (&vd, &vb) == 1)
		{
			vorbis_analysis (&vb, 0);
			vorbis_bitrate_addblock (&vb);

			while (vorbis_bitrate_flushpacket (&vd, &op))
			{
				ogg_stream_packetin (&os, &op);

				for (;;)
				{
					if (ogg_stream_pageout (&os, &og) == 0)
						break;

					output->write (og.header, og.header_len);
					output->write (og.body, og.body_len);

					if (ogg_page_eos (&og))
						break;
				}
			}
		}

		return true;
	}

	juce_UseDebuggingNewOperator
};

OggVorbisAudioFormat::OggVorbisAudioFormat()
	: AudioFormat (TRANS (oggFormatName), (const juce_wchar**) oggExtensions)
{
}

OggVorbisAudioFormat::~OggVorbisAudioFormat()
{
}

const Array <int> OggVorbisAudioFormat::getPossibleSampleRates()
{
	const int rates[] = { 22050, 32000, 44100, 48000, 0 };
	return Array <int> (rates);
}

const Array <int> OggVorbisAudioFormat::getPossibleBitDepths()
{
	Array <int> depths;
	depths.add (32);
	return depths;
}

bool OggVorbisAudioFormat::canDoStereo()
{
	return true;
}

bool OggVorbisAudioFormat::canDoMono()
{
	return true;
}

AudioFormatReader* OggVorbisAudioFormat::createReaderFor (InputStream* in,
														  const bool deleteStreamIfOpeningFails)
{
	ScopedPointer <OggReader> r (new OggReader (in));

	if (r->sampleRate != 0)
		return r.release();

	if (! deleteStreamIfOpeningFails)
		r->input = 0;

	return 0;
}

AudioFormatWriter* OggVorbisAudioFormat::createWriterFor (OutputStream* out,
														  double sampleRate,
														  unsigned int numChannels,
														  int bitsPerSample,
														  const StringPairArray& /*metadataValues*/,
														  int qualityOptionIndex)
{
	ScopedPointer <OggWriter> w (new OggWriter (out,
												sampleRate,
												numChannels,
												bitsPerSample,
												qualityOptionIndex));

	return w->ok ? w.release() : 0;
}

bool OggVorbisAudioFormat::isCompressed()
{
	return true;
}

const StringArray OggVorbisAudioFormat::getQualityOptions()
{
	StringArray s;
	s.add ("Low Quality");
	s.add ("Medium Quality");
	s.add ("High Quality");
	return s;
}

int OggVorbisAudioFormat::estimateOggFileQuality (const File& source)
{
	FileInputStream* const in = source.createInputStream();

	if (in != 0)
	{
		ScopedPointer <AudioFormatReader> r (createReaderFor (in, true));

		if (r != 0)
		{
			const int64 numSamps = r->lengthInSamples;
			r = 0;

			const int64 fileNumSamps = source.getSize() / 4;
			const double ratio = numSamps / (double) fileNumSamps;

			if (ratio > 12.0)
				return 0;
			else if (ratio > 6.0)
				return 1;
			else
				return 2;
		}
	}

	return 1;
}

END_JUCE_NAMESPACE

#endif
/*** End of inlined file: juce_OggVorbisAudioFormat.cpp ***/

#endif

#if JUCE_BUILD_CORE && ! JUCE_ONLY_BUILD_CORE_LIBRARY // do these in the core section to help balance the sizes

/*** Start of inlined file: juce_JPEGLoader.cpp ***/
#if JUCE_MSVC
  #pragma warning (push)
#endif

namespace jpeglibNamespace
{
#if JUCE_INCLUDE_JPEGLIB_CODE
  #if JUCE_MINGW
	typedef unsigned char boolean;
  #endif
  extern "C"
  {
	#define JPEG_INTERNALS
	#undef FAR

/*** Start of inlined file: jpeglib.h ***/
#ifndef JPEGLIB_H
#define JPEGLIB_H

#ifndef JCONFIG_INCLUDED	/* in case jinclude.h already did */

/*** Start of inlined file: jconfig.h ***/
// disable all the warnings under MSVC
#ifdef _MSC_VER
#pragma warning (disable: 4996 4267 4100 4127 4702 4244)
#endif

#ifdef __BORLANDC__
#pragma warn -8057
#pragma warn -8019
#pragma warn -8004
#pragma warn -8008
#endif

#define HAVE_PROTOTYPES
#define HAVE_UNSIGNED_CHAR
#define HAVE_UNSIGNED_SHORT
#undef CHAR_IS_UNSIGNED
#define HAVE_STDDEF_H
#define HAVE_STDLIB_H
#undef NEED_BSD_STRINGS
#undef NEED_SYS_TYPES_H
#undef NEED_FAR_POINTERS	/* we presume a 32-bit flat memory model */
#undef NEED_SHORT_EXTERNAL_NAMES
#undef INCOMPLETE_TYPES_BROKEN

#ifndef __RPCNDR_H__		/* don't conflict if rpcndr.h already read */
typedef unsigned char boolean;
#endif
#define HAVE_BOOLEAN		/* prevent jmorecfg.h from redefining it */

#ifdef JPEG_INTERNALS

#undef RIGHT_SHIFT_IS_UNSIGNED

#endif /* JPEG_INTERNALS */

#ifdef JPEG_CJPEG_DJPEG

#define BMP_SUPPORTED		/* BMP image file format */
#define GIF_SUPPORTED		/* GIF image file format */
#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
#undef RLE_SUPPORTED		/* Utah RLE image file format */
#define TARGA_SUPPORTED		/* Targa image file format */

#define TWO_FILE_COMMANDLINE	/* optional */
#define USE_SETMODE		/* Microsoft has setmode() */
#undef NEED_SIGNAL_CATCHER
#undef DONT_USE_B_MODE
#undef PROGRESS_REPORT		/* optional */

#endif /* JPEG_CJPEG_DJPEG */
/*** End of inlined file: jconfig.h ***/

		/* widely used configuration options */
#endif

/*** Start of inlined file: jmorecfg.h ***/
#define BITS_IN_JSAMPLE  8	/* use 8 or 12 */

#define MAX_COMPONENTS  10	/* maximum number of image components */

#if BITS_IN_JSAMPLE == 8

#ifdef HAVE_UNSIGNED_CHAR

typedef unsigned char JSAMPLE;
#define GETJSAMPLE(value)  ((int) (value))

#else /* not HAVE_UNSIGNED_CHAR */

typedef char JSAMPLE;
#ifdef CHAR_IS_UNSIGNED
#define GETJSAMPLE(value)  ((int) (value))
#else
#define GETJSAMPLE(value)  ((int) (value) & 0xFF)
#endif /* CHAR_IS_UNSIGNED */

#endif /* HAVE_UNSIGNED_CHAR */

#define MAXJSAMPLE	255
#define CENTERJSAMPLE	128

#endif /* BITS_IN_JSAMPLE == 8 */

#if BITS_IN_JSAMPLE == 12

typedef short JSAMPLE;
#define GETJSAMPLE(value)  ((int) (value))

#define MAXJSAMPLE	4095
#define CENTERJSAMPLE	2048

#endif /* BITS_IN_JSAMPLE == 12 */

typedef short JCOEF;

#ifdef HAVE_UNSIGNED_CHAR

typedef unsigned char JOCTET;
#define GETJOCTET(value)  (value)

#else /* not HAVE_UNSIGNED_CHAR */

typedef char JOCTET;
#ifdef CHAR_IS_UNSIGNED
#define GETJOCTET(value)  (value)
#else
#define GETJOCTET(value)  ((value) & 0xFF)
#endif /* CHAR_IS_UNSIGNED */

#endif /* HAVE_UNSIGNED_CHAR */

#ifdef HAVE_UNSIGNED_CHAR
typedef unsigned char UINT8;
#else /* not HAVE_UNSIGNED_CHAR */
#ifdef CHAR_IS_UNSIGNED
typedef char UINT8;
#else /* not CHAR_IS_UNSIGNED */
typedef short UINT8;
#endif /* CHAR_IS_UNSIGNED */
#endif /* HAVE_UNSIGNED_CHAR */

#ifdef HAVE_UNSIGNED_SHORT
typedef unsigned short UINT16;
#else /* not HAVE_UNSIGNED_SHORT */
typedef unsigned int UINT16;
#endif /* HAVE_UNSIGNED_SHORT */

#ifndef XMD_H			/* X11/xmd.h correctly defines INT16 */
typedef short INT16;
#endif

#ifndef XMD_H			/* X11/xmd.h correctly defines INT32 */
typedef long INT32;
#endif

typedef unsigned int JDIMENSION;

#define JPEG_MAX_DIMENSION  65500L  /* a tad under 64K to prevent overflows */

#define METHODDEF(type)		static type
#define LOCAL(type)		static type
#define GLOBAL(type)		type
#define EXTERN(type)		extern type

#ifdef HAVE_PROTOTYPES
#define JMETHOD(type,methodname,arglist)  type (*methodname) arglist
#else
#define JMETHOD(type,methodname,arglist)  type (*methodname) ()
#endif

#ifdef NEED_FAR_POINTERS
#define FAR  far
#else
#define FAR
#endif

#ifndef HAVE_BOOLEAN
typedef int boolean;
#endif
#ifndef FALSE			/* in case these macros already exist */
#define FALSE	0		/* values of boolean */
#endif
#ifndef TRUE
#define TRUE	1
#endif

#ifdef JPEG_INTERNALS
#define JPEG_INTERNAL_OPTIONS
#endif

#ifdef JPEG_INTERNAL_OPTIONS

#define DCT_ISLOW_SUPPORTED	/* slow but accurate integer algorithm */
#define DCT_IFAST_SUPPORTED	/* faster, less accurate integer method */
#define DCT_FLOAT_SUPPORTED	/* floating-point: accurate, fast on fast HW */

#undef  C_ARITH_CODING_SUPPORTED	/* Arithmetic coding back end? */
#define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
#define C_PROGRESSIVE_SUPPORTED		/* Progressive JPEG? (Requires MULTISCAN)*/
#define ENTROPY_OPT_SUPPORTED		/* Optimization of entropy coding parms? */
#define INPUT_SMOOTHING_SUPPORTED   /* Input image smoothing option? */

#undef  D_ARITH_CODING_SUPPORTED	/* Arithmetic coding back end? */
#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
#define D_PROGRESSIVE_SUPPORTED		/* Progressive JPEG? (Requires MULTISCAN)*/
#define SAVE_MARKERS_SUPPORTED		/* jpeg_save_markers() needed? */
#define BLOCK_SMOOTHING_SUPPORTED   /* Block smoothing? (Progressive only) */
#define IDCT_SCALING_SUPPORTED		/* Output rescaling via IDCT? */
#undef  UPSAMPLE_SCALING_SUPPORTED  /* Output rescaling at upsample stage? */
#define UPSAMPLE_MERGING_SUPPORTED  /* Fast path for sloppy upsampling? */
#define QUANT_1PASS_SUPPORTED		/* 1-pass color quantization? */
#define QUANT_2PASS_SUPPORTED		/* 2-pass color quantization? */

#define RGB_RED		0	/* Offset of Red in an RGB scanline element */
#define RGB_GREEN	1	/* Offset of Green */
#define RGB_BLUE	2	/* Offset of Blue */
#define RGB_PIXELSIZE	3	/* JSAMPLEs per RGB scanline element */

#ifndef INLINE
#ifdef __GNUC__			/* for instance, GNU C knows about inline */
#define INLINE __inline__
#endif
#ifndef INLINE
#define INLINE			/* default is to define it as empty */
#endif
#endif

#ifndef MULTIPLIER
#define MULTIPLIER  int		/* type for fastest integer multiply */
#endif

#ifndef FAST_FLOAT
#ifdef HAVE_PROTOTYPES
#define FAST_FLOAT  float
#else
#define FAST_FLOAT  double
#endif
#endif

#endif /* JPEG_INTERNAL_OPTIONS */
/*** End of inlined file: jmorecfg.h ***/

		/* seldom changed options */

#define JPEG_LIB_VERSION  62	/* Version 6b */

#define DCTSIZE			8	/* The basic DCT block is 8x8 samples */
#define DCTSIZE2		64	/* DCTSIZE squared; # of elements in a block */
#define NUM_QUANT_TBLS	  4	/* Quantization tables are numbered 0..3 */
#define NUM_HUFF_TBLS	   4	/* Huffman tables are numbered 0..3 */
#define NUM_ARITH_TBLS	  16	/* Arith-coding tables are numbered 0..15 */
#define MAX_COMPS_IN_SCAN   4	/* JPEG limit on # of components in one scan */
#define MAX_SAMP_FACTOR	 4	/* JPEG limit on sampling factors */
#define C_MAX_BLOCKS_IN_MCU   10 /* compressor's limit on blocks per MCU */
#ifndef D_MAX_BLOCKS_IN_MCU
#define D_MAX_BLOCKS_IN_MCU   10 /* decompressor's limit on blocks per MCU */
#endif

typedef JSAMPLE FAR *JSAMPROW;	/* ptr to one image row of pixel samples. */
typedef JSAMPROW *JSAMPARRAY;	/* ptr to some rows (a 2-D sample array) */
typedef JSAMPARRAY *JSAMPIMAGE;	/* a 3-D sample array: top index is color */

typedef JCOEF JBLOCK[DCTSIZE2];	/* one block of coefficients */
typedef JBLOCK FAR *JBLOCKROW;	/* pointer to one row of coefficient blocks */
typedef JBLOCKROW *JBLOCKARRAY;		/* a 2-D array of coefficient blocks */
typedef JBLOCKARRAY *JBLOCKIMAGE;	/* a 3-D array of coefficient blocks */

typedef JCOEF FAR *JCOEFPTR;	/* useful in a couple of places */

typedef struct {
  UINT16 quantval[DCTSIZE2];	/* quantization step for each coefficient */
  boolean sent_table;		/* TRUE when table has been output */
} JQUANT_TBL;

typedef struct {
  UINT8 bits[17];		/* bits[k] = # of symbols with codes of */
  UINT8 huffval[256];		/* The symbols, in order of incr code length */
  boolean sent_table;		/* TRUE when table has been output */
} JHUFF_TBL;

typedef struct {
  int component_id;		/* identifier for this component (0..255) */
  int component_index;		/* its index in SOF or cinfo->comp_info[] */
  int h_samp_factor;		/* horizontal sampling factor (1..4) */
  int v_samp_factor;		/* vertical sampling factor (1..4) */
  int quant_tbl_no;		/* quantization table selector (0..3) */
  int dc_tbl_no;		/* DC entropy table selector (0..3) */
  int ac_tbl_no;		/* AC entropy table selector (0..3) */

  JDIMENSION width_in_blocks;
  JDIMENSION height_in_blocks;
  int DCT_scaled_size;
  JDIMENSION downsampled_width;	 /* actual width in samples */
  JDIMENSION downsampled_height; /* actual height in samples */
  boolean component_needed;	/* do we need the value of this component? */

  int MCU_width;		/* number of blocks per MCU, horizontally */
  int MCU_height;		/* number of blocks per MCU, vertically */
  int MCU_blocks;		/* MCU_width * MCU_height */
  int MCU_sample_width;		/* MCU width in samples, MCU_width*DCT_scaled_size */
  int last_col_width;		/* # of non-dummy blocks across in last MCU */
  int last_row_height;		/* # of non-dummy blocks down in last MCU */

  JQUANT_TBL * quant_table;

  void * dct_table;
} jpeg_component_info;

typedef struct {
  int comps_in_scan;		/* number of components encoded in this scan */
  int component_index[MAX_COMPS_IN_SCAN]; /* their SOF/comp_info[] indexes */
  int Ss, Se;			/* progressive JPEG spectral selection parms */
  int Ah, Al;			/* progressive JPEG successive approx. parms */
} jpeg_scan_info;

typedef struct jpeg_marker_struct FAR * jpeg_saved_marker_ptr;

struct jpeg_marker_struct {
  jpeg_saved_marker_ptr next;	/* next in list, or NULL */
  UINT8 marker;			/* marker code: JPEG_COM, or JPEG_APP0+n */
  unsigned int original_length;	/* # bytes of data in the file */
  unsigned int data_length;	/* # bytes of data saved at data[] */
  JOCTET FAR * data;		/* the data contained in the marker */
};

typedef enum {
	JCS_UNKNOWN,		/* error/unspecified */
	JCS_GRAYSCALE,		/* monochrome */
	JCS_RGB,		/* red/green/blue */
	JCS_YCbCr,		/* Y/Cb/Cr (also known as YUV) */
	JCS_CMYK,		/* C/M/Y/K */
	JCS_YCCK		/* Y/Cb/Cr/K */
} J_COLOR_SPACE;

typedef enum {
	JDCT_ISLOW,		/* slow but accurate integer algorithm */
	JDCT_IFAST,		/* faster, less accurate integer method */
	JDCT_FLOAT		/* floating-point: accurate, fast on fast HW */
} J_DCT_METHOD;

#ifndef JDCT_DEFAULT		/* may be overridden in jconfig.h */
#define JDCT_DEFAULT  JDCT_ISLOW
#endif
#ifndef JDCT_FASTEST		/* may be overridden in jconfig.h */
#define JDCT_FASTEST  JDCT_IFAST
#endif

typedef enum {
	JDITHER_NONE,		/* no dithering */
	JDITHER_ORDERED,	/* simple ordered dither */
	JDITHER_FS		/* Floyd-Steinberg error diffusion dither */
} J_DITHER_MODE;

#define jpeg_common_fields \
  struct jpeg_error_mgr * err;	/* Error handler module */\
  struct jpeg_memory_mgr * mem;	/* Memory manager module */\
  struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\
  void * client_data;		/* Available for use by application */\
  boolean is_decompressor;	/* So common code can tell which is which */\
  int global_state		/* For checking call sequence validity */

struct jpeg_common_struct {
  jpeg_common_fields;		/* Fields common to both master struct types */
};

typedef struct jpeg_common_struct * j_common_ptr;
typedef struct jpeg_compress_struct * j_compress_ptr;
typedef struct jpeg_decompress_struct * j_decompress_ptr;

struct jpeg_compress_struct {
  jpeg_common_fields;		/* Fields shared with jpeg_decompress_struct */

  struct jpeg_destination_mgr * dest;

  JDIMENSION image_width;	/* input image width */
  JDIMENSION image_height;	/* input image height */
  int input_components;		/* # of color components in input image */
  J_COLOR_SPACE in_color_space;	/* colorspace of input image */

  double input_gamma;		/* image gamma of input image */

  int data_precision;		/* bits of precision in image data */

  int num_components;		/* # of color components in JPEG image */
  J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */

  jpeg_component_info * comp_info;

  JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];

  JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];

  UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */

  int num_scans;		/* # of entries in scan_info array */
  const jpeg_scan_info * scan_info; /* script for multi-scan file, or NULL */

  boolean raw_data_in;		/* TRUE=caller supplies downsampled data */
  boolean arith_code;		/* TRUE=arithmetic coding, FALSE=Huffman */
  boolean optimize_coding;	/* TRUE=optimize entropy encoding parms */
  boolean CCIR601_sampling;	/* TRUE=first samples are cosited */
  int smoothing_factor;		/* 1..100, or 0 for no input smoothing */
  J_DCT_METHOD dct_method;	/* DCT algorithm selector */

  unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */
  int restart_in_rows;		/* if > 0, MCU rows per restart interval */

  boolean write_JFIF_header;	/* should a JFIF marker be written? */
  UINT8 JFIF_major_version;	/* What to write for the JFIF version number */
  UINT8 JFIF_minor_version;
  UINT8 density_unit;		/* JFIF code for pixel size units */
  UINT16 X_density;		/* Horizontal pixel density */
  UINT16 Y_density;		/* Vertical pixel density */
  boolean write_Adobe_marker;	/* should an Adobe marker be written? */

  JDIMENSION next_scanline;	/* 0 .. image_height-1  */

  boolean progressive_mode;	/* TRUE if scan script uses progressive mode */
  int max_h_samp_factor;	/* largest h_samp_factor */
  int max_v_samp_factor;	/* largest v_samp_factor */

  JDIMENSION total_iMCU_rows;	/* # of iMCU rows to be input to coef ctlr */

  int comps_in_scan;		/* # of JPEG components in this scan */
  jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];

  JDIMENSION MCUs_per_row;	/* # of MCUs across the image */
  JDIMENSION MCU_rows_in_scan;	/* # of MCU rows in the image */

  int blocks_in_MCU;		/* # of DCT blocks per MCU */
  int MCU_membership[C_MAX_BLOCKS_IN_MCU];

  int Ss, Se, Ah, Al;		/* progressive JPEG parameters for scan */

  struct jpeg_comp_master * master;
  struct jpeg_c_main_controller * main;
  struct jpeg_c_prep_controller * prep;
  struct jpeg_c_coef_controller * coef;
  struct jpeg_marker_writer * marker;
  struct jpeg_color_converter * cconvert;
  struct jpeg_downsampler * downsample;
  struct jpeg_forward_dct * fdct;
  struct jpeg_entropy_encoder * entropy;
  jpeg_scan_info * script_space; /* workspace for jpeg_simple_progression */
  int script_space_size;
};

struct jpeg_decompress_struct {
  jpeg_common_fields;		/* Fields shared with jpeg_compress_struct */

  struct jpeg_source_mgr * src;

  JDIMENSION image_width;	/* nominal image width (from SOF marker) */
  JDIMENSION image_height;	/* nominal image height */
  int num_components;		/* # of color components in JPEG image */
  J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */

  J_COLOR_SPACE out_color_space; /* colorspace for output */

  unsigned int scale_num, scale_denom; /* fraction by which to scale image */

  double output_gamma;		/* image gamma wanted in output */

  boolean buffered_image;	/* TRUE=multiple output passes */
  boolean raw_data_out;		/* TRUE=downsampled data wanted */

  J_DCT_METHOD dct_method;	/* IDCT algorithm selector */
  boolean do_fancy_upsampling;	/* TRUE=apply fancy upsampling */
  boolean do_block_smoothing;	/* TRUE=apply interblock smoothing */

  boolean quantize_colors;	/* TRUE=colormapped output wanted */
  J_DITHER_MODE dither_mode;	/* type of color dithering to use */
  boolean two_pass_quantize;	/* TRUE=use two-pass color quantization */
  int desired_number_of_colors;	/* max # colors to use in created colormap */
  boolean enable_1pass_quant;	/* enable future use of 1-pass quantizer */
  boolean enable_external_quant;/* enable future use of external colormap */
  boolean enable_2pass_quant;	/* enable future use of 2-pass quantizer */

  JDIMENSION output_width;	/* scaled image width */
  JDIMENSION output_height;	/* scaled image height */
  int out_color_components;	/* # of color components in out_color_space */
  int output_components;	/* # of color components returned */
  int rec_outbuf_height;	/* min recommended height of scanline buffer */

  int actual_number_of_colors;	/* number of entries in use */
  JSAMPARRAY colormap;		/* The color map as a 2-D pixel array */

  JDIMENSION output_scanline;	/* 0 .. output_height-1  */

  int input_scan_number;	/* Number of SOS markers seen so far */
  JDIMENSION input_iMCU_row;	/* Number of iMCU rows completed */

  int output_scan_number;	/* Nominal scan number being displayed */
  JDIMENSION output_iMCU_row;	/* Number of iMCU rows read */

  int (*coef_bits)[DCTSIZE2];	/* -1 or current Al value for each coef */

  JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];

  JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];

  int data_precision;		/* bits of precision in image data */

  jpeg_component_info * comp_info;

  boolean progressive_mode;	/* TRUE if SOFn specifies progressive mode */
  boolean arith_code;		/* TRUE=arithmetic coding, FALSE=Huffman */

  UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */

  unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */

  boolean saw_JFIF_marker;	/* TRUE iff a JFIF APP0 marker was found */
  UINT8 JFIF_major_version;	/* JFIF version number */
  UINT8 JFIF_minor_version;
  UINT8 density_unit;		/* JFIF code for pixel size units */
  UINT16 X_density;		/* Horizontal pixel density */
  UINT16 Y_density;		/* Vertical pixel density */
  boolean saw_Adobe_marker;	/* TRUE iff an Adobe APP14 marker was found */
  UINT8 Adobe_transform;	/* Color transform code from Adobe marker */

  boolean CCIR601_sampling;	/* TRUE=first samples are cosited */

  jpeg_saved_marker_ptr marker_list; /* Head of list of saved markers */

  int max_h_samp_factor;	/* largest h_samp_factor */
  int max_v_samp_factor;	/* largest v_samp_factor */

  int min_DCT_scaled_size;	/* smallest DCT_scaled_size of any component */

  JDIMENSION total_iMCU_rows;	/* # of iMCU rows in image */

  JSAMPLE * sample_range_limit; /* table for fast range-limiting */

  int comps_in_scan;		/* # of JPEG components in this scan */
  jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];

  JDIMENSION MCUs_per_row;	/* # of MCUs across the image */
  JDIMENSION MCU_rows_in_scan;	/* # of MCU rows in the image */

  int blocks_in_MCU;		/* # of DCT blocks per MCU */
  int MCU_membership[D_MAX_BLOCKS_IN_MCU];

  int Ss, Se, Ah, Al;		/* progressive JPEG parameters for scan */

  int unread_marker;

  struct jpeg_decomp_master * master;
  struct jpeg_d_main_controller * main;
  struct jpeg_d_coef_controller * coef;
  struct jpeg_d_post_controller * post;
  struct jpeg_input_controller * inputctl;
  struct jpeg_marker_reader * marker;
  struct jpeg_entropy_decoder * entropy;
  struct jpeg_inverse_dct * idct;
  struct jpeg_upsampler * upsample;
  struct jpeg_color_deconverter * cconvert;
  struct jpeg_color_quantizer * cquantize;
};

struct jpeg_error_mgr {
  JMETHOD(void, error_exit, (j_common_ptr cinfo));
  JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level));
  JMETHOD(void, output_message, (j_common_ptr cinfo));
  JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer));
#define JMSG_LENGTH_MAX  200	/* recommended size of format_message buffer */
  JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo));

  int msg_code;
#define JMSG_STR_PARM_MAX  80
  union {
	int i[8];
	char s[JMSG_STR_PARM_MAX];
  } msg_parm;

  int trace_level;		/* max msg_level that will be displayed */

  long num_warnings;		/* number of corrupt-data warnings */

  const char * const * jpeg_message_table; /* Library errors */
  int last_jpeg_message;	/* Table contains strings 0..last_jpeg_message */
  const char * const * addon_message_table; /* Non-library errors */
  int first_addon_message;	/* code for first string in addon table */
  int last_addon_message;	/* code for last string in addon table */
};

struct jpeg_progress_mgr {
  JMETHOD(void, progress_monitor, (j_common_ptr cinfo));

  long pass_counter;		/* work units completed in this pass */
  long pass_limit;		/* total number of work units in this pass */
  int completed_passes;		/* passes completed so far */
  int total_passes;		/* total number of passes expected */
};

struct jpeg_destination_mgr {
  JOCTET * next_output_byte;	/* => next byte to write in buffer */
  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */

  JMETHOD(void, init_destination, (j_compress_ptr cinfo));
  JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo));
  JMETHOD(void, term_destination, (j_compress_ptr cinfo));
};

struct jpeg_source_mgr {
  const JOCTET * next_input_byte; /* => next byte to read from buffer */
  size_t bytes_in_buffer;	/* # of bytes remaining in buffer */

  JMETHOD(void, init_source, (j_decompress_ptr cinfo));
  JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo));
  JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes));
  JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo, int desired));
  JMETHOD(void, term_source, (j_decompress_ptr cinfo));
};

#define JPOOL_PERMANENT	0	/* lasts until master record is destroyed */
#define JPOOL_IMAGE	1	/* lasts until done with image/datastream */
#define JPOOL_NUMPOOLS	2

typedef struct jvirt_sarray_control * jvirt_sarray_ptr;
typedef struct jvirt_barray_control * jvirt_barray_ptr;

struct jpeg_memory_mgr {
  JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id,
				size_t sizeofobject));
  JMETHOD(void FAR *, alloc_large, (j_common_ptr cinfo, int pool_id,
					 size_t sizeofobject));
  JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id,
					 JDIMENSION samplesperrow,
					 JDIMENSION numrows));
  JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id,
					  JDIMENSION blocksperrow,
					  JDIMENSION numrows));
  JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo,
						  int pool_id,
						  boolean pre_zero,
						  JDIMENSION samplesperrow,
						  JDIMENSION numrows,
						  JDIMENSION maxaccess));
  JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo,
						  int pool_id,
						  boolean pre_zero,
						  JDIMENSION blocksperrow,
						  JDIMENSION numrows,
						  JDIMENSION maxaccess));
  JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo));
  JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo,
					   jvirt_sarray_ptr ptr,
					   JDIMENSION start_row,
					   JDIMENSION num_rows,
					   boolean writable));
  JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo,
						jvirt_barray_ptr ptr,
						JDIMENSION start_row,
						JDIMENSION num_rows,
						boolean writable));
  JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id));
  JMETHOD(void, self_destruct, (j_common_ptr cinfo));

  long max_memory_to_use;

  long max_alloc_chunk;
};

typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));

#ifdef HAVE_PROTOTYPES
#define JPP(arglist)	arglist
#else
#define JPP(arglist)	()
#endif

#ifdef NEED_SHORT_EXTERNAL_NAMES
#define jpeg_std_error		jStdError
#define jpeg_CreateCompress	jCreaCompress
#define jpeg_CreateDecompress	jCreaDecompress
#define jpeg_destroy_compress	jDestCompress
#define jpeg_destroy_decompress	jDestDecompress
#define jpeg_stdio_dest		jStdDest
#define jpeg_stdio_src		jStdSrc
#define jpeg_set_defaults	jSetDefaults
#define jpeg_set_colorspace	jSetColorspace
#define jpeg_default_colorspace	jDefColorspace
#define jpeg_set_quality	jSetQuality
#define jpeg_set_linear_quality	jSetLQuality
#define jpeg_add_quant_table	jAddQuantTable
#define jpeg_quality_scaling	jQualityScaling
#define jpeg_simple_progression	jSimProgress
#define jpeg_suppress_tables	jSuppressTables
#define jpeg_alloc_quant_table	jAlcQTable
#define jpeg_alloc_huff_table	jAlcHTable
#define jpeg_start_compress	jStrtCompress
#define jpeg_write_scanlines	jWrtScanlines
#define jpeg_finish_compress	jFinCompress
#define jpeg_write_raw_data	jWrtRawData
#define jpeg_write_marker	jWrtMarker
#define jpeg_write_m_header	jWrtMHeader
#define jpeg_write_m_byte	jWrtMByte
#define jpeg_write_tables	jWrtTables
#define jpeg_read_header	jReadHeader
#define jpeg_start_decompress	jStrtDecompress
#define jpeg_read_scanlines	jReadScanlines
#define jpeg_finish_decompress	jFinDecompress
#define jpeg_read_raw_data	jReadRawData
#define jpeg_has_multiple_scans	jHasMultScn
#define jpeg_start_output	jStrtOutput
#define jpeg_finish_output	jFinOutput
#define jpeg_input_complete	jInComplete
#define jpeg_new_colormap	jNewCMap
#define jpeg_consume_input	jConsumeInput
#define jpeg_calc_output_dimensions	jCalcDimensions
#define jpeg_save_markers	jSaveMarkers
#define jpeg_set_marker_processor	jSetMarker
#define jpeg_read_coefficients	jReadCoefs
#define jpeg_write_coefficients	jWrtCoefs
#define jpeg_copy_critical_parameters	jCopyCrit
#define jpeg_abort_compress	jAbrtCompress
#define jpeg_abort_decompress	jAbrtDecompress
#define jpeg_abort		jAbort
#define jpeg_destroy		jDestroy
#define jpeg_resync_to_restart	jResyncRestart
#endif /* NEED_SHORT_EXTERNAL_NAMES */

EXTERN(struct jpeg_error_mgr *) jpeg_std_error
	JPP((struct jpeg_error_mgr * err));

#define jpeg_create_compress(cinfo) \
	jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
			(size_t) sizeof(struct jpeg_compress_struct))
#define jpeg_create_decompress(cinfo) \
	jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
			  (size_t) sizeof(struct jpeg_decompress_struct))
EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo,
					  int version, size_t structsize));
EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo,
					int version, size_t structsize));
EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo));
EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo));

EXTERN(void) jpeg_stdio_dest JPP((j_compress_ptr cinfo, FILE * outfile));
EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile));

EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo));
EXTERN(void) jpeg_set_colorspace JPP((j_compress_ptr cinfo,
					  J_COLOR_SPACE colorspace));
EXTERN(void) jpeg_default_colorspace JPP((j_compress_ptr cinfo));
EXTERN(void) jpeg_set_quality JPP((j_compress_ptr cinfo, int quality,
				   boolean force_baseline));
EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo,
					  int scale_factor,
					  boolean force_baseline));
EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl,
					   const unsigned int *basic_table,
					   int scale_factor,
					   boolean force_baseline));
EXTERN(int) jpeg_quality_scaling JPP((int quality));
EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo));
EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
					   boolean suppress));
EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo));
EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo));

EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo,
					  boolean write_all_tables));
EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo,
						 JSAMPARRAY scanlines,
						 JDIMENSION num_lines));
EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo));

EXTERN(JDIMENSION) jpeg_write_raw_data JPP((j_compress_ptr cinfo,
						JSAMPIMAGE data,
						JDIMENSION num_lines));

EXTERN(void) jpeg_write_marker
	JPP((j_compress_ptr cinfo, int marker,
		 const JOCTET * dataptr, unsigned int datalen));
EXTERN(void) jpeg_write_m_header
	JPP((j_compress_ptr cinfo, int marker, unsigned int datalen));
EXTERN(void) jpeg_write_m_byte
	JPP((j_compress_ptr cinfo, int val));

EXTERN(void) jpeg_write_tables JPP((j_compress_ptr cinfo));

EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo,
				  boolean require_image));
#define JPEG_SUSPENDED		0 /* Suspended due to lack of input data */
#define JPEG_HEADER_OK		1 /* Found valid image datastream */
#define JPEG_HEADER_TABLES_ONLY	2 /* Found valid table-specs-only datastream */

EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo));
EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo,
						JSAMPARRAY scanlines,
						JDIMENSION max_lines));
EXTERN(boolean) jpeg_finish_decompress JPP((j_decompress_ptr cinfo));

EXTERN(JDIMENSION) jpeg_read_raw_data JPP((j_decompress_ptr cinfo,
					   JSAMPIMAGE data,
					   JDIMENSION max_lines));

EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo));
EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo,
					   int scan_number));
EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo));
EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo));
EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo));
EXTERN(int) jpeg_consume_input JPP((j_decompress_ptr cinfo));
#define JPEG_REACHED_SOS	1 /* Reached start of new scan */
#define JPEG_REACHED_EOI	2 /* Reached end of image */
#define JPEG_ROW_COMPLETED	3 /* Completed one iMCU row */
#define JPEG_SCAN_COMPLETED	4 /* Completed last iMCU row of a scan */

EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo));

EXTERN(void) jpeg_save_markers
	JPP((j_decompress_ptr cinfo, int marker_code,
		 unsigned int length_limit));

EXTERN(void) jpeg_set_marker_processor
	JPP((j_decompress_ptr cinfo, int marker_code,
		 jpeg_marker_parser_method routine));

EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo));
EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo,
					  jvirt_barray_ptr * coef_arrays));
EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo,
						j_compress_ptr dstinfo));

EXTERN(void) jpeg_abort_compress JPP((j_compress_ptr cinfo));
EXTERN(void) jpeg_abort_decompress JPP((j_decompress_ptr cinfo));

EXTERN(void) jpeg_abort JPP((j_common_ptr cinfo));
EXTERN(void) jpeg_destroy JPP((j_common_ptr cinfo));

EXTERN(boolean) jpeg_resync_to_restart JPP((j_decompress_ptr cinfo,
						int desired));

#define JPEG_RST0	0xD0	/* RST0 marker code */
#define JPEG_EOI	0xD9	/* EOI marker code */
#define JPEG_APP0	0xE0	/* APP0 marker code */
#define JPEG_COM	0xFE	/* COM marker code */

#ifdef INCOMPLETE_TYPES_BROKEN
#ifndef JPEG_INTERNALS		/* will be defined in jpegint.h */
struct jvirt_sarray_control { long dummy; };
struct jvirt_barray_control { long dummy; };
struct jpeg_comp_master { long dummy; };
struct jpeg_c_main_controller { long dummy; };
struct jpeg_c_prep_controller { long dummy; };
struct jpeg_c_coef_controller { long dummy; };
struct jpeg_marker_writer { long dummy; };
struct jpeg_color_converter { long dummy; };
struct jpeg_downsampler { long dummy; };
struct jpeg_forward_dct { long dummy; };
struct jpeg_entropy_encoder { long dummy; };
struct jpeg_decomp_master { long dummy; };
struct jpeg_d_main_controller { long dummy; };
struct jpeg_d_coef_controller { long dummy; };
struct jpeg_d_post_controller { long dummy; };
struct jpeg_input_controller { long dummy; };
struct jpeg_marker_reader { long dummy; };
struct jpeg_entropy_decoder { long dummy; };
struct jpeg_inverse_dct { long dummy; };
struct jpeg_upsampler { long dummy; };
struct jpeg_color_deconverter { long dummy; };
struct jpeg_color_quantizer { long dummy; };
#endif /* JPEG_INTERNALS */
#endif /* INCOMPLETE_TYPES_BROKEN */

#ifdef JPEG_INTERNALS

/*** Start of inlined file: jpegint.h ***/
typedef enum {			/* Operating modes for buffer controllers */
	JBUF_PASS_THRU,		/* Plain stripwise operation */
	JBUF_SAVE_SOURCE,	/* Run source subobject only, save output */
	JBUF_CRANK_DEST,	/* Run dest subobject only, using saved data */
	JBUF_SAVE_AND_PASS	/* Run both subobjects, save output */
} J_BUF_MODE;

#define CSTATE_START	100	/* after create_compress */
#define CSTATE_SCANNING	101	/* start_compress done, write_scanlines OK */
#define CSTATE_RAW_OK	102	/* start_compress done, write_raw_data OK */
#define CSTATE_WRCOEFS	103	/* jpeg_write_coefficients done */
#define DSTATE_START	200	/* after create_decompress */
#define DSTATE_INHEADER	201	/* reading header markers, no SOS yet */
#define DSTATE_READY	202	/* found SOS, ready for start_decompress */
#define DSTATE_PRELOAD	203	/* reading multiscan file in start_decompress*/
#define DSTATE_PRESCAN	204	/* performing dummy pass for 2-pass quant */
#define DSTATE_SCANNING	205	/* start_decompress done, read_scanlines OK */
#define DSTATE_RAW_OK	206	/* start_decompress done, read_raw_data OK */
#define DSTATE_BUFIMAGE	207	/* expecting jpeg_start_output */
#define DSTATE_BUFPOST	208	/* looking for SOS/EOI in jpeg_finish_output */
#define DSTATE_RDCOEFS	209	/* reading file in jpeg_read_coefficients */
#define DSTATE_STOPPING	210	/* looking for EOI in jpeg_finish_decompress */

struct jpeg_comp_master {
  JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
  JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
  JMETHOD(void, finish_pass, (j_compress_ptr cinfo));

  boolean call_pass_startup;	/* True if pass_startup must be called */
  boolean is_last_pass;		/* True during last pass */
};

struct jpeg_c_main_controller {
  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
  JMETHOD(void, process_data, (j_compress_ptr cinfo,
				   JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
				   JDIMENSION in_rows_avail));
};

struct jpeg_c_prep_controller {
  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
  JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
				   JSAMPARRAY input_buf,
				   JDIMENSION *in_row_ctr,
				   JDIMENSION in_rows_avail,
				   JSAMPIMAGE output_buf,
				   JDIMENSION *out_row_group_ctr,
				   JDIMENSION out_row_groups_avail));
};

struct jpeg_c_coef_controller {
  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
  JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
				   JSAMPIMAGE input_buf));
};

struct jpeg_color_converter {
  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
  JMETHOD(void, color_convert, (j_compress_ptr cinfo,
				JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
				JDIMENSION output_row, int num_rows));
};

struct jpeg_downsampler {
  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
  JMETHOD(void, downsample, (j_compress_ptr cinfo,
				 JSAMPIMAGE input_buf, JDIMENSION in_row_index,
				 JSAMPIMAGE output_buf,
				 JDIMENSION out_row_group_index));

  boolean need_context_rows;	/* TRUE if need rows above & below */
};

struct jpeg_forward_dct {
  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
  JMETHOD(void, forward_DCT, (j_compress_ptr cinfo,
				  jpeg_component_info * compptr,
				  JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
				  JDIMENSION start_row, JDIMENSION start_col,
				  JDIMENSION num_blocks));
};

struct jpeg_entropy_encoder {
  JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
  JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
  JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
};

struct jpeg_marker_writer {
  JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
  JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
  JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
  JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
  JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
  JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
					  unsigned int datalen));
  JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
};

struct jpeg_decomp_master {
  JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
  JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));

  boolean is_dummy_pass;	/* True during 1st pass for 2-pass quant */
};

struct jpeg_input_controller {
  JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
  JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
  JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
  JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));

  boolean has_multiple_scans;	/* True if file has multiple scans */
  boolean eoi_reached;		/* True when EOI has been consumed */
};

struct jpeg_d_main_controller {
  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
  JMETHOD(void, process_data, (j_decompress_ptr cinfo,
				   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
				   JDIMENSION out_rows_avail));
};

struct jpeg_d_coef_controller {
  JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
  JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
  JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
  JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
				 JSAMPIMAGE output_buf));
  jvirt_barray_ptr *coef_arrays;
};

struct jpeg_d_post_controller {
  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
  JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
					JSAMPIMAGE input_buf,
					JDIMENSION *in_row_group_ctr,
					JDIMENSION in_row_groups_avail,
					JSAMPARRAY output_buf,
					JDIMENSION *out_row_ctr,
					JDIMENSION out_rows_avail));
};

struct jpeg_marker_reader {
  JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
  JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
  jpeg_marker_parser_method read_restart_marker;

  boolean saw_SOI;		/* found SOI? */
  boolean saw_SOF;		/* found SOF? */
  int next_restart_num;		/* next restart number expected (0-7) */
  unsigned int discarded_bytes;	/* # of bytes skipped looking for a marker */
};

struct jpeg_entropy_decoder {
  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
				JBLOCKROW *MCU_data));

  boolean insufficient_data;	/* set TRUE after emitting warning */
};

typedef JMETHOD(void, inverse_DCT_method_ptr,
		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
		 JCOEFPTR coef_block,
		 JSAMPARRAY output_buf, JDIMENSION output_col));

struct jpeg_inverse_dct {
  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
};

struct jpeg_upsampler {
  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  JMETHOD(void, upsample, (j_decompress_ptr cinfo,
			   JSAMPIMAGE input_buf,
			   JDIMENSION *in_row_group_ctr,
			   JDIMENSION in_row_groups_avail,
			   JSAMPARRAY output_buf,
			   JDIMENSION *out_row_ctr,
			   JDIMENSION out_rows_avail));

  boolean need_context_rows;	/* TRUE if need rows above & below */
};

struct jpeg_color_deconverter {
  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
				JSAMPIMAGE input_buf, JDIMENSION input_row,
				JSAMPARRAY output_buf, int num_rows));
};

struct jpeg_color_quantizer {
  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
  JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
				 JSAMPARRAY input_buf, JSAMPARRAY output_buf,
				 int num_rows));
  JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
  JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
};

#undef MAX
#define MAX(a,b)	((a) > (b) ? (a) : (b))
#undef MIN
#define MIN(a,b)	((a) < (b) ? (a) : (b))

#ifdef RIGHT_SHIFT_IS_UNSIGNED
#define SHIFT_TEMPS	INT32 shift_temp;
#define RIGHT_SHIFT(x,shft)  \
	((shift_temp = (x)) < 0 ? \
	 (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
	 (shift_temp >> (shft)))
#else
#define SHIFT_TEMPS
#define RIGHT_SHIFT(x,shft)	((x) >> (shft))
#endif

#ifdef NEED_SHORT_EXTERNAL_NAMES
#define jinit_compress_master	jICompress
#define jinit_c_master_control	jICMaster
#define jinit_c_main_controller	jICMainC
#define jinit_c_prep_controller	jICPrepC
#define jinit_c_coef_controller	jICCoefC
#define jinit_color_converter	jICColor
#define jinit_downsampler	jIDownsampler
#define jinit_forward_dct	jIFDCT
#define jinit_huff_encoder	jIHEncoder
#define jinit_phuff_encoder	jIPHEncoder
#define jinit_marker_writer	jIMWriter
#define jinit_master_decompress	jIDMaster
#define jinit_d_main_controller	jIDMainC
#define jinit_d_coef_controller	jIDCoefC
#define jinit_d_post_controller	jIDPostC
#define jinit_input_controller	jIInCtlr
#define jinit_marker_reader	jIMReader
#define jinit_huff_decoder	jIHDecoder
#define jinit_phuff_decoder	jIPHDecoder
#define jinit_inverse_dct	jIIDCT
#define jinit_upsampler		jIUpsampler
#define jinit_color_deconverter	jIDColor
#define jinit_1pass_quantizer	jI1Quant
#define jinit_2pass_quantizer	jI2Quant
#define jinit_merged_upsampler	jIMUpsampler
#define jinit_memory_mgr	jIMemMgr
#define jdiv_round_up		jDivRound
#define jround_up		jRound
#define jcopy_sample_rows	jCopySamples
#define jcopy_block_row		jCopyBlocks
#define jzero_far		jZeroFar
#define jpeg_zigzag_order	jZIGTable
#define jpeg_natural_order	jZAGTable
#endif /* NEED_SHORT_EXTERNAL_NAMES */

EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
					 boolean transcode_only));
EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
					  boolean need_full_buffer));
EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
					  boolean need_full_buffer));
EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
					  boolean need_full_buffer));
EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo));
EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
					  boolean need_full_buffer));
EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
					  boolean need_full_buffer));
EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
					  boolean need_full_buffer));
EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));

EXTERN(long) jdiv_round_up JPP((long a, long b));
EXTERN(long) jround_up JPP((long a, long b));
EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
					JSAMPARRAY output_array, int dest_row,
					int num_rows, JDIMENSION num_cols));
EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
				  JDIMENSION num_blocks));
EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
#if 0				/* This table is not actually needed in v6a */
extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
#endif
extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */

#ifdef INCOMPLETE_TYPES_BROKEN
#ifndef AM_MEMORY_MANAGER	/* only jmemmgr.c defines these */
struct jvirt_sarray_control { long dummy; };
struct jvirt_barray_control { long dummy; };
#endif
#endif /* INCOMPLETE_TYPES_BROKEN */
/*** End of inlined file: jpegint.h ***/

		/* fetch private declarations */

/*** Start of inlined file: jerror.h ***/
#ifndef JMESSAGE
#ifndef JERROR_H
#define JMAKE_ENUM_LIST
#else
#define JMESSAGE(code,string)
#endif /* JERROR_H */
#endif /* JMESSAGE */

#ifdef JMAKE_ENUM_LIST

typedef enum {

#define JMESSAGE(code,string)	code ,

#endif /* JMAKE_ENUM_LIST */

JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */

JMESSAGE(JERR_ARITH_NOTIMPL,
	 "Sorry, there are legal restrictions on arithmetic coding")
JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
JMESSAGE(JERR_BAD_LIB_VERSION,
	 "Wrong JPEG library version: library is %d, caller expects %d")
JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
JMESSAGE(JERR_BAD_PROGRESSION,
	 "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
JMESSAGE(JERR_BAD_PROG_SCRIPT,
	 "Invalid progressive parameters at scan script entry %d")
JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
JMESSAGE(JERR_BAD_STRUCT_SIZE,
	 "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
JMESSAGE(JERR_FILE_READ, "Input file read error")
JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
JMESSAGE(JERR_MISMATCHED_QUANT_TABLE,
	 "Cannot transcode due to multiple use of quantization table %d")
JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
JMESSAGE(JERR_QUANT_COMPONENTS,
	 "Cannot quantize more than %d color components")
JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
JMESSAGE(JERR_TFILE_WRITE,
	 "Write failed on temporary file --- out of disk space?")
JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
JMESSAGE(JMSG_VERSION, JVERSION)
JMESSAGE(JTRC_16BIT_TABLES,
	 "Caution: quantization tables are too coarse for baseline JPEG")
JMESSAGE(JTRC_ADOBE,
	 "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
JMESSAGE(JTRC_DQT, "Define Quantization Table %d  precision %d")
JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
JMESSAGE(JTRC_EOI, "End Of Image")
JMESSAGE(JTRC_HUFFBITS, "        %3d %3d %3d %3d %3d %3d %3d %3d")
JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d  %d")
JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
	 "Warning: thumbnail image size does not match data length %u")
JMESSAGE(JTRC_JFIF_EXTENSION,
	 "JFIF extension marker: type 0x%02x, length %u")
JMESSAGE(JTRC_JFIF_THUMBNAIL, "    with %d x %d thumbnail image")
JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
JMESSAGE(JTRC_QUANTVALS, "        %4u %4u %4u %4u %4u %4u %4u %4u")
JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
JMESSAGE(JTRC_RST, "RST%d")
JMESSAGE(JTRC_SMOOTH_NOTIMPL,
	 "Smoothing not supported with nonstandard sampling ratios")
JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
JMESSAGE(JTRC_SOF_COMPONENT, "    Component %d: %dhx%dv q=%d")
JMESSAGE(JTRC_SOI, "Start of Image")
JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
JMESSAGE(JTRC_SOS_COMPONENT, "    Component %d: dc=%d ac=%d")
JMESSAGE(JTRC_SOS_PARAMS, "  Ss=%d, Se=%d, Ah=%d, Al=%d")
JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
JMESSAGE(JTRC_THUMB_JPEG,
	 "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
JMESSAGE(JTRC_THUMB_PALETTE,
	 "JFIF extension marker: palette thumbnail image, length %u")
JMESSAGE(JTRC_THUMB_RGB,
	 "JFIF extension marker: RGB thumbnail image, length %u")
JMESSAGE(JTRC_UNKNOWN_IDS,
	 "Unrecognized component IDs %d %d %d, assuming YCbCr")
JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
JMESSAGE(JWRN_BOGUS_PROGRESSION,
	 "Inconsistent progression sequence for component %d coefficient %d")
JMESSAGE(JWRN_EXTRANEOUS_DATA,
	 "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
JMESSAGE(JWRN_MUST_RESYNC,
	 "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")

#ifdef JMAKE_ENUM_LIST

  JMSG_LASTMSGCODE
} J_MESSAGE_CODE;

#undef JMAKE_ENUM_LIST
#endif /* JMAKE_ENUM_LIST */

#undef JMESSAGE

#ifndef JERROR_H
#define JERROR_H

#define ERREXIT(cinfo,code)  \
  ((cinfo)->err->msg_code = (code), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT1(cinfo,code,p1)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT2(cinfo,code,p1,p2)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT3(cinfo,code,p1,p2,p3)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (cinfo)->err->msg_parm.i[2] = (p3), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT4(cinfo,code,p1,p2,p3,p4)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (cinfo)->err->msg_parm.i[2] = (p3), \
   (cinfo)->err->msg_parm.i[3] = (p4), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXITS(cinfo,code,str)  \
  ((cinfo)->err->msg_code = (code), \
   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))

#define MAKESTMT(stuff)		do { stuff } while (0)

#define WARNMS(cinfo,code)  \
  ((cinfo)->err->msg_code = (code), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
#define WARNMS1(cinfo,code,p1)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
#define WARNMS2(cinfo,code,p1,p2)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))

#define TRACEMS(cinfo,lvl,code)  \
  ((cinfo)->err->msg_code = (code), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS1(cinfo,lvl,code,p1)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS2(cinfo,lvl,code,p1,p2)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS3(cinfo,lvl,code,p1,p2,p3)  \
  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
	   (cinfo)->err->msg_code = (code); \
	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4)  \
  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
	   (cinfo)->err->msg_code = (code); \
	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5)  \
  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
	   _mp[4] = (p5); \
	   (cinfo)->err->msg_code = (code); \
	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8)  \
  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
	   _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
	   (cinfo)->err->msg_code = (code); \
	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMSS(cinfo,lvl,code,str)  \
  ((cinfo)->err->msg_code = (code), \
   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))

#endif /* JERROR_H */
/*** End of inlined file: jerror.h ***/

		/* fetch error codes too */
#endif

#endif /* JPEGLIB_H */
/*** End of inlined file: jpeglib.h ***/



/*** Start of inlined file: jcapimin.c ***/
#define JPEG_INTERNALS

/*** Start of inlined file: jinclude.h ***/
#ifndef __jinclude_h__
#define __jinclude_h__


/*** Start of inlined file: jconfig.h ***/
// disable all the warnings under MSVC
#ifdef _MSC_VER
#pragma warning (disable: 4996 4267 4100 4127 4702 4244)
#endif

#ifdef __BORLANDC__
#pragma warn -8057
#pragma warn -8019
#pragma warn -8004
#pragma warn -8008
#endif

#define HAVE_PROTOTYPES
#define HAVE_UNSIGNED_CHAR
#define HAVE_UNSIGNED_SHORT
#undef CHAR_IS_UNSIGNED
#define HAVE_STDDEF_H
#define HAVE_STDLIB_H
#undef NEED_BSD_STRINGS
#undef NEED_SYS_TYPES_H
#undef NEED_FAR_POINTERS	/* we presume a 32-bit flat memory model */
#undef NEED_SHORT_EXTERNAL_NAMES
#undef INCOMPLETE_TYPES_BROKEN

#ifndef __RPCNDR_H__		/* don't conflict if rpcndr.h already read */
typedef unsigned char boolean;
#endif
#define HAVE_BOOLEAN		/* prevent jmorecfg.h from redefining it */

#ifdef JPEG_INTERNALS

#undef RIGHT_SHIFT_IS_UNSIGNED

#endif /* JPEG_INTERNALS */

#ifdef JPEG_CJPEG_DJPEG

#define BMP_SUPPORTED		/* BMP image file format */
#define GIF_SUPPORTED		/* GIF image file format */
#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
#undef RLE_SUPPORTED		/* Utah RLE image file format */
#define TARGA_SUPPORTED		/* Targa image file format */

#define TWO_FILE_COMMANDLINE	/* optional */
#define USE_SETMODE		/* Microsoft has setmode() */
#undef NEED_SIGNAL_CATCHER
#undef DONT_USE_B_MODE
#undef PROGRESS_REPORT		/* optional */

#endif /* JPEG_CJPEG_DJPEG */
/*** End of inlined file: jconfig.h ***/

		/* auto configuration options */
#define JCONFIG_INCLUDED	/* so that jpeglib.h doesn't do it again */

#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef NEED_SYS_TYPES_H
#include <sys/types.h>
#endif

#include <stdio.h>

#ifdef NEED_BSD_STRINGS

#include <strings.h>
#define MEMZERO(target,size)	bzero((void *)(target), (size_t)(size))
#define MEMCOPY(dest,src,size)	bcopy((const void *)(src), (void *)(dest), (size_t)(size))

#else /* not BSD, assume ANSI/SysV string lib */

#include <string.h>
#define MEMZERO(target,size)	memset((void *)(target), 0, (size_t)(size))
#define MEMCOPY(dest,src,size)	memcpy((void *)(dest), (const void *)(src), (size_t)(size))

#endif

#define SIZEOF(object)	((size_t) sizeof(object))

#define JFREAD(file,buf,sizeofbuf)  \
  ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
#define JFWRITE(file,buf,sizeofbuf)  \
  ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))

typedef enum {			/* JPEG marker codes */
  M_SOF0  = 0xc0,
  M_SOF1  = 0xc1,
  M_SOF2  = 0xc2,
  M_SOF3  = 0xc3,

  M_SOF5  = 0xc5,
  M_SOF6  = 0xc6,
  M_SOF7  = 0xc7,

  M_JPG   = 0xc8,
  M_SOF9  = 0xc9,
  M_SOF10 = 0xca,
  M_SOF11 = 0xcb,

  M_SOF13 = 0xcd,
  M_SOF14 = 0xce,
  M_SOF15 = 0xcf,

  M_DHT   = 0xc4,

  M_DAC   = 0xcc,

  M_RST0  = 0xd0,
  M_RST1  = 0xd1,
  M_RST2  = 0xd2,
  M_RST3  = 0xd3,
  M_RST4  = 0xd4,
  M_RST5  = 0xd5,
  M_RST6  = 0xd6,
  M_RST7  = 0xd7,

  M_SOI   = 0xd8,
  M_EOI   = 0xd9,
  M_SOS   = 0xda,
  M_DQT   = 0xdb,
  M_DNL   = 0xdc,
  M_DRI   = 0xdd,
  M_DHP   = 0xde,
  M_EXP   = 0xdf,

  M_APP0  = 0xe0,
  M_APP1  = 0xe1,
  M_APP2  = 0xe2,
  M_APP3  = 0xe3,
  M_APP4  = 0xe4,
  M_APP5  = 0xe5,
  M_APP6  = 0xe6,
  M_APP7  = 0xe7,
  M_APP8  = 0xe8,
  M_APP9  = 0xe9,
  M_APP10 = 0xea,
  M_APP11 = 0xeb,
  M_APP12 = 0xec,
  M_APP13 = 0xed,
  M_APP14 = 0xee,
  M_APP15 = 0xef,

  M_JPG0  = 0xf0,
  M_JPG13 = 0xfd,
  M_COM   = 0xfe,

  M_TEM   = 0x01,

  M_ERROR = 0x100
} JPEG_MARKER;

#ifdef AVOID_TABLES

#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))

#else

#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))

static const int extend_test[16] =   /* entry n is 2**(n-1) */
  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
	0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };

static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
	((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
	((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
	((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };

#endif /* AVOID_TABLES */

#endif
/*** End of inlined file: jinclude.h ***/


GLOBAL(void)
jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
{
  int i;

  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
  if (version != JPEG_LIB_VERSION)
	ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  if (structsize != SIZEOF(struct jpeg_compress_struct))
	ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
		 (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);

  {
	struct jpeg_error_mgr * err = cinfo->err;
	void * client_data = cinfo->client_data; /* ignore Purify complaint here */
	MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
	cinfo->err = err;
	cinfo->client_data = client_data;
  }
  cinfo->is_decompressor = FALSE;

  jinit_memory_mgr((j_common_ptr) cinfo);

  cinfo->progress = NULL;
  cinfo->dest = NULL;

  cinfo->comp_info = NULL;

  for (i = 0; i < NUM_QUANT_TBLS; i++)
	cinfo->quant_tbl_ptrs[i] = NULL;

  for (i = 0; i < NUM_HUFF_TBLS; i++) {
	cinfo->dc_huff_tbl_ptrs[i] = NULL;
	cinfo->ac_huff_tbl_ptrs[i] = NULL;
  }

  cinfo->script_space = NULL;

  cinfo->input_gamma = 1.0;	/* in case application forgets */

  cinfo->global_state = CSTATE_START;
}

GLOBAL(void)
jpeg_destroy_compress (j_compress_ptr cinfo)
{
  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
}

GLOBAL(void)
jpeg_abort_compress (j_compress_ptr cinfo)
{
  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
}

GLOBAL(void)
jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
{
  int i;
  JQUANT_TBL * qtbl;
  JHUFF_TBL * htbl;

  for (i = 0; i < NUM_QUANT_TBLS; i++) {
	if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
	  qtbl->sent_table = suppress;
  }

  for (i = 0; i < NUM_HUFF_TBLS; i++) {
	if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
	  htbl->sent_table = suppress;
	if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
	  htbl->sent_table = suppress;
  }
}

GLOBAL(void)
jpeg_finish_compress (j_compress_ptr cinfo)
{
  JDIMENSION iMCU_row;

  if (cinfo->global_state == CSTATE_SCANNING ||
	  cinfo->global_state == CSTATE_RAW_OK) {
	if (cinfo->next_scanline < cinfo->image_height)
	  ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
	(*cinfo->master->finish_pass) (cinfo);
  } else if (cinfo->global_state != CSTATE_WRCOEFS)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  while (! cinfo->master->is_last_pass) {
	(*cinfo->master->prepare_for_pass) (cinfo);
	for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
	  if (cinfo->progress != NULL) {
	cinfo->progress->pass_counter = (long) iMCU_row;
	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
	  }
	  if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
	ERREXIT(cinfo, JERR_CANT_SUSPEND);
	}
	(*cinfo->master->finish_pass) (cinfo);
  }
  (*cinfo->marker->write_file_trailer) (cinfo);
  (*cinfo->dest->term_destination) (cinfo);
  jpeg_abort((j_common_ptr) cinfo);
}

GLOBAL(void)
jpeg_write_marker (j_compress_ptr cinfo, int marker,
		   const JOCTET *dataptr, unsigned int datalen)
{
  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));

  if (cinfo->next_scanline != 0 ||
	  (cinfo->global_state != CSTATE_SCANNING &&
	   cinfo->global_state != CSTATE_RAW_OK &&
	   cinfo->global_state != CSTATE_WRCOEFS))
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
  write_marker_byte = cinfo->marker->write_marker_byte;	/* copy for speed */
  while (datalen--) {
	(*write_marker_byte) (cinfo, *dataptr);
	dataptr++;
  }
}

GLOBAL(void)
jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
{
  if (cinfo->next_scanline != 0 ||
	  (cinfo->global_state != CSTATE_SCANNING &&
	   cinfo->global_state != CSTATE_RAW_OK &&
	   cinfo->global_state != CSTATE_WRCOEFS))
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
}

GLOBAL(void)
jpeg_write_m_byte (j_compress_ptr cinfo, int val)
{
  (*cinfo->marker->write_marker_byte) (cinfo, val);
}

GLOBAL(void)
jpeg_write_tables (j_compress_ptr cinfo)
{
  if (cinfo->global_state != CSTATE_START)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  (*cinfo->dest->init_destination) (cinfo);
  jinit_marker_writer(cinfo);
  (*cinfo->marker->write_tables_only) (cinfo);
  (*cinfo->dest->term_destination) (cinfo);
}
/*** End of inlined file: jcapimin.c ***/


/*** Start of inlined file: jcapistd.c ***/
#define JPEG_INTERNALS

GLOBAL(void)
jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
{
  if (cinfo->global_state != CSTATE_START)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  if (write_all_tables)
	jpeg_suppress_tables(cinfo, FALSE);	/* mark all tables to be written */

  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  (*cinfo->dest->init_destination) (cinfo);
  jinit_compress_master(cinfo);
  (*cinfo->master->prepare_for_pass) (cinfo);
  cinfo->next_scanline = 0;
  cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
}

GLOBAL(JDIMENSION)
jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
			  JDIMENSION num_lines)
{
  JDIMENSION row_ctr, rows_left;

  if (cinfo->global_state != CSTATE_SCANNING)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  if (cinfo->next_scanline >= cinfo->image_height)
	WARNMS(cinfo, JWRN_TOO_MUCH_DATA);

  if (cinfo->progress != NULL) {
	cinfo->progress->pass_counter = (long) cinfo->next_scanline;
	cinfo->progress->pass_limit = (long) cinfo->image_height;
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  }

  if (cinfo->master->call_pass_startup)
	(*cinfo->master->pass_startup) (cinfo);

  rows_left = cinfo->image_height - cinfo->next_scanline;
  if (num_lines > rows_left)
	num_lines = rows_left;

  row_ctr = 0;
  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  cinfo->next_scanline += row_ctr;
  return row_ctr;
}

GLOBAL(JDIMENSION)
jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
			 JDIMENSION num_lines)
{
  JDIMENSION lines_per_iMCU_row;

  if (cinfo->global_state != CSTATE_RAW_OK)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  if (cinfo->next_scanline >= cinfo->image_height) {
	WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
	return 0;
  }

  if (cinfo->progress != NULL) {
	cinfo->progress->pass_counter = (long) cinfo->next_scanline;
	cinfo->progress->pass_limit = (long) cinfo->image_height;
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  }

  if (cinfo->master->call_pass_startup)
	(*cinfo->master->pass_startup) (cinfo);

  lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  if (num_lines < lines_per_iMCU_row)
	ERREXIT(cinfo, JERR_BUFFER_SIZE);

  if (! (*cinfo->coef->compress_data) (cinfo, data)) {
	return 0;
  }

  cinfo->next_scanline += lines_per_iMCU_row;
  return lines_per_iMCU_row;
}
/*** End of inlined file: jcapistd.c ***/


/*** Start of inlined file: jccoefct.c ***/
#define JPEG_INTERNALS

#ifdef ENTROPY_OPT_SUPPORTED
#define FULL_COEF_BUFFER_SUPPORTED
#else
#ifdef C_MULTISCAN_FILES_SUPPORTED
#define FULL_COEF_BUFFER_SUPPORTED
#endif
#endif

typedef struct {
  struct jpeg_c_coef_controller pub; /* public fields */

  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
  int MCU_rows_per_iMCU_row;	/* number of such rows needed */

  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];

  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
} my_coef_controller;

typedef my_coef_controller * my_coef_ptr;

METHODDEF(boolean) compress_data
	JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
#ifdef FULL_COEF_BUFFER_SUPPORTED
METHODDEF(boolean) compress_first_pass
	JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
METHODDEF(boolean) compress_output
	JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
#endif

LOCAL(void)
start_iMCU_row (j_compress_ptr cinfo)
{
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;

  if (cinfo->comps_in_scan > 1) {
	coef->MCU_rows_per_iMCU_row = 1;
  } else {
	if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
	  coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
	else
	  coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  }

  coef->mcu_ctr = 0;
  coef->MCU_vert_offset = 0;
}

METHODDEF(void)
start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
{
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;

  coef->iMCU_row_num = 0;
  start_iMCU_row(cinfo);

  switch (pass_mode) {
  case JBUF_PASS_THRU:
	if (coef->whole_image[0] != NULL)
	  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	coef->pub.compress_data = compress_data;
	break;
#ifdef FULL_COEF_BUFFER_SUPPORTED
  case JBUF_SAVE_AND_PASS:
	if (coef->whole_image[0] == NULL)
	  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	coef->pub.compress_data = compress_first_pass;
	break;
  case JBUF_CRANK_DEST:
	if (coef->whole_image[0] == NULL)
	  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	coef->pub.compress_data = compress_output;
	break;
#endif
  default:
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	break;
  }
}

METHODDEF(boolean)
compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  int blkn, bi, ci, yindex, yoffset, blockcnt;
  JDIMENSION ypos, xpos;
  jpeg_component_info *compptr;

  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
	   yoffset++) {
	for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
	 MCU_col_num++) {
	  blkn = 0;
	  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
						: compptr->last_col_width;
	xpos = MCU_col_num * compptr->MCU_sample_width;
	ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
	  if (coef->iMCU_row_num < last_iMCU_row ||
		  yoffset+yindex < compptr->last_row_height) {
		(*cinfo->fdct->forward_DCT) (cinfo, compptr,
					 input_buf[compptr->component_index],
					 coef->MCU_buffer[blkn],
					 ypos, xpos, (JDIMENSION) blockcnt);
		if (blockcnt < compptr->MCU_width) {
		  jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
			(compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
		  for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
		coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
		  }
		}
	  } else {
		jzero_far((void FAR *) coef->MCU_buffer[blkn],
			  compptr->MCU_width * SIZEOF(JBLOCK));
		for (bi = 0; bi < compptr->MCU_width; bi++) {
		  coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
		}
	  }
	  blkn += compptr->MCU_width;
	  ypos += DCTSIZE;
	}
	  }
	  if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
	coef->MCU_vert_offset = yoffset;
	coef->mcu_ctr = MCU_col_num;
	return FALSE;
	  }
	}
	coef->mcu_ctr = 0;
  }
  coef->iMCU_row_num++;
  start_iMCU_row(cinfo);
  return TRUE;
}

#ifdef FULL_COEF_BUFFER_SUPPORTED

METHODDEF(boolean)
compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  JDIMENSION blocks_across, MCUs_across, MCUindex;
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  JCOEF lastDC;
  jpeg_component_info *compptr;
  JBLOCKARRAY buffer;
  JBLOCKROW thisblockrow, lastblockrow;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	buffer = (*cinfo->mem->access_virt_barray)
	  ((j_common_ptr) cinfo, coef->whole_image[ci],
	   coef->iMCU_row_num * compptr->v_samp_factor,
	   (JDIMENSION) compptr->v_samp_factor, TRUE);
	if (coef->iMCU_row_num < last_iMCU_row)
	  block_rows = compptr->v_samp_factor;
	else {
	  block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
	  if (block_rows == 0) block_rows = compptr->v_samp_factor;
	}
	blocks_across = compptr->width_in_blocks;
	h_samp_factor = compptr->h_samp_factor;
	ndummy = (int) (blocks_across % h_samp_factor);
	if (ndummy > 0)
	  ndummy = h_samp_factor - ndummy;
	for (block_row = 0; block_row < block_rows; block_row++) {
	  thisblockrow = buffer[block_row];
	  (*cinfo->fdct->forward_DCT) (cinfo, compptr,
				   input_buf[ci], thisblockrow,
				   (JDIMENSION) (block_row * DCTSIZE),
				   (JDIMENSION) 0, blocks_across);
	  if (ndummy > 0) {
	thisblockrow += blocks_across; /* => first dummy block */
	jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
	lastDC = thisblockrow[-1][0];
	for (bi = 0; bi < ndummy; bi++) {
	  thisblockrow[bi][0] = lastDC;
	}
	  }
	}
	if (coef->iMCU_row_num == last_iMCU_row) {
	  blocks_across += ndummy;	/* include lower right corner */
	  MCUs_across = blocks_across / h_samp_factor;
	  for (block_row = block_rows; block_row < compptr->v_samp_factor;
	   block_row++) {
	thisblockrow = buffer[block_row];
	lastblockrow = buffer[block_row-1];
	jzero_far((void FAR *) thisblockrow,
		  (size_t) (blocks_across * SIZEOF(JBLOCK)));
	for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
	  lastDC = lastblockrow[h_samp_factor-1][0];
	  for (bi = 0; bi < h_samp_factor; bi++) {
		thisblockrow[bi][0] = lastDC;
	  }
	  thisblockrow += h_samp_factor; /* advance to next MCU in row */
	  lastblockrow += h_samp_factor;
	}
	  }
	}
  }

  return compress_output(cinfo, input_buf);
}

METHODDEF(boolean)
compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  int blkn, ci, xindex, yindex, yoffset;
  JDIMENSION start_col;
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  JBLOCKROW buffer_ptr;
  jpeg_component_info *compptr;

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	buffer[ci] = (*cinfo->mem->access_virt_barray)
	  ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
	   coef->iMCU_row_num * compptr->v_samp_factor,
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
  }

  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
	   yoffset++) {
	for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
	 MCU_col_num++) {
	  blkn = 0;			/* index of current DCT block within MCU */
	  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	start_col = MCU_col_num * compptr->MCU_width;
	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
		coef->MCU_buffer[blkn++] = buffer_ptr++;
	  }
	}
	  }
	  if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
	coef->MCU_vert_offset = yoffset;
	coef->mcu_ctr = MCU_col_num;
	return FALSE;
	  }
	}
	coef->mcu_ctr = 0;
  }
  coef->iMCU_row_num++;
  start_iMCU_row(cinfo);
  return TRUE;
}

#endif /* FULL_COEF_BUFFER_SUPPORTED */

GLOBAL(void)
jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
{
  my_coef_ptr coef;

  coef = (my_coef_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_coef_controller));
  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  coef->pub.start_pass = start_pass_coef;

  if (need_full_buffer) {
#ifdef FULL_COEF_BUFFER_SUPPORTED
	int ci;
	jpeg_component_info *compptr;

	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
	  coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
				(long) compptr->h_samp_factor),
	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
				(long) compptr->v_samp_factor),
	 (JDIMENSION) compptr->v_samp_factor);
	}
#else
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
#endif
  } else {
	JBLOCKROW buffer;
	int i;

	buffer = (JBLOCKROW)
	  (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
	for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
	  coef->MCU_buffer[i] = buffer + i;
	}
	coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  }
}
/*** End of inlined file: jccoefct.c ***/


/*** Start of inlined file: jccolor.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_color_converter pub; /* public fields */

  INT32 * rgb_ycc_tab;		/* => table for RGB to YCbCr conversion */
} my_color_converter;

typedef my_color_converter * my_cconvert_ptr;

#define SCALEBITS	16	/* speediest right-shift on some machines */
#define CBCR_OFFSET	((INT32) CENTERJSAMPLE << SCALEBITS)
#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))

#define R_Y_OFF		0			/* offset to R => Y section */
#define G_Y_OFF		(1*(MAXJSAMPLE+1))	/* offset to G => Y section */
#define B_Y_OFF		(2*(MAXJSAMPLE+1))	/* etc. */
#define R_CB_OFF	(3*(MAXJSAMPLE+1))
#define G_CB_OFF	(4*(MAXJSAMPLE+1))
#define B_CB_OFF	(5*(MAXJSAMPLE+1))
#define R_CR_OFF	B_CB_OFF		/* B=>Cb, R=>Cr are the same */
#define G_CR_OFF	(6*(MAXJSAMPLE+1))
#define B_CR_OFF	(7*(MAXJSAMPLE+1))
#define TABLE_SIZE	(8*(MAXJSAMPLE+1))

METHODDEF(void)
rgb_ycc_start (j_compress_ptr cinfo)
{
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  INT32 * rgb_ycc_tab;
  INT32 i;

  cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(TABLE_SIZE * SIZEOF(INT32)));

  for (i = 0; i <= MAXJSAMPLE; i++) {
	rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
	rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
	rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i	 + ONE_HALF;
	rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
	rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
	rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i	+ CBCR_OFFSET + ONE_HALF-1;
	rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
	rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  }
}

METHODDEF(void)
rgb_ycc_convert (j_compress_ptr cinfo,
		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
		 JDIMENSION output_row, int num_rows)
{
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  register int r, g, b;
  register INT32 * ctab = cconvert->rgb_ycc_tab;
  register JSAMPROW inptr;
  register JSAMPROW outptr0, outptr1, outptr2;
  register JDIMENSION col;
  JDIMENSION num_cols = cinfo->image_width;

  while (--num_rows >= 0) {
	inptr = *input_buf++;
	outptr0 = output_buf[0][output_row];
	outptr1 = output_buf[1][output_row];
	outptr2 = output_buf[2][output_row];
	output_row++;
	for (col = 0; col < num_cols; col++) {
	  r = GETJSAMPLE(inptr[RGB_RED]);
	  g = GETJSAMPLE(inptr[RGB_GREEN]);
	  b = GETJSAMPLE(inptr[RGB_BLUE]);
	  inptr += RGB_PIXELSIZE;
	  outptr0[col] = (JSAMPLE)
		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
		 >> SCALEBITS);
	  outptr1[col] = (JSAMPLE)
		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
		 >> SCALEBITS);
	  outptr2[col] = (JSAMPLE)
		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
		 >> SCALEBITS);
	}
  }
}

METHODDEF(void)
rgb_gray_convert (j_compress_ptr cinfo,
		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
		  JDIMENSION output_row, int num_rows)
{
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  register int r, g, b;
  register INT32 * ctab = cconvert->rgb_ycc_tab;
  register JSAMPROW inptr;
  register JSAMPROW outptr;
  register JDIMENSION col;
  JDIMENSION num_cols = cinfo->image_width;

  while (--num_rows >= 0) {
	inptr = *input_buf++;
	outptr = output_buf[0][output_row];
	output_row++;
	for (col = 0; col < num_cols; col++) {
	  r = GETJSAMPLE(inptr[RGB_RED]);
	  g = GETJSAMPLE(inptr[RGB_GREEN]);
	  b = GETJSAMPLE(inptr[RGB_BLUE]);
	  inptr += RGB_PIXELSIZE;
	  outptr[col] = (JSAMPLE)
		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
		 >> SCALEBITS);
	}
  }
}

METHODDEF(void)
cmyk_ycck_convert (j_compress_ptr cinfo,
		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
		   JDIMENSION output_row, int num_rows)
{
  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  register int r, g, b;
  register INT32 * ctab = cconvert->rgb_ycc_tab;
  register JSAMPROW inptr;
  register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  register JDIMENSION col;
  JDIMENSION num_cols = cinfo->image_width;

  while (--num_rows >= 0) {
	inptr = *input_buf++;
	outptr0 = output_buf[0][output_row];
	outptr1 = output_buf[1][output_row];
	outptr2 = output_buf[2][output_row];
	outptr3 = output_buf[3][output_row];
	output_row++;
	for (col = 0; col < num_cols; col++) {
	  r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
	  g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
	  b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
	  outptr3[col] = inptr[3];	/* don't need GETJSAMPLE here */
	  inptr += 4;
	  outptr0[col] = (JSAMPLE)
		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
		 >> SCALEBITS);
	  outptr1[col] = (JSAMPLE)
		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
		 >> SCALEBITS);
	  outptr2[col] = (JSAMPLE)
		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
		 >> SCALEBITS);
	}
  }
}

METHODDEF(void)
grayscale_convert (j_compress_ptr cinfo,
		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
		   JDIMENSION output_row, int num_rows)
{
  register JSAMPROW inptr;
  register JSAMPROW outptr;
  register JDIMENSION col;
  JDIMENSION num_cols = cinfo->image_width;
  int instride = cinfo->input_components;

  while (--num_rows >= 0) {
	inptr = *input_buf++;
	outptr = output_buf[0][output_row];
	output_row++;
	for (col = 0; col < num_cols; col++) {
	  outptr[col] = inptr[0];	/* don't need GETJSAMPLE() here */
	  inptr += instride;
	}
  }
}

METHODDEF(void)
null_convert (j_compress_ptr cinfo,
		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
		  JDIMENSION output_row, int num_rows)
{
  register JSAMPROW inptr;
  register JSAMPROW outptr;
  register JDIMENSION col;
  register int ci;
  int nc = cinfo->num_components;
  JDIMENSION num_cols = cinfo->image_width;

  while (--num_rows >= 0) {
	for (ci = 0; ci < nc; ci++) {
	  inptr = *input_buf;
	  outptr = output_buf[ci][output_row];
	  for (col = 0; col < num_cols; col++) {
	outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
	inptr += nc;
	  }
	}
	input_buf++;
	output_row++;
  }
}

METHODDEF(void)
null_method (j_compress_ptr cinfo)
{
}

GLOBAL(void)
jinit_color_converter (j_compress_ptr cinfo)
{
  my_cconvert_ptr cconvert;

  cconvert = (my_cconvert_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_color_converter));
  cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  cconvert->pub.start_pass = null_method;

  switch (cinfo->in_color_space) {
  case JCS_GRAYSCALE:
	if (cinfo->input_components != 1)
	  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
	break;

  case JCS_RGB:
#if RGB_PIXELSIZE != 3
	if (cinfo->input_components != RGB_PIXELSIZE)
	  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
	break;
#endif /* else share code with YCbCr */

  case JCS_YCbCr:
	if (cinfo->input_components != 3)
	  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
	break;

  case JCS_CMYK:
  case JCS_YCCK:
	if (cinfo->input_components != 4)
	  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
	break;

  default:			/* JCS_UNKNOWN can be anything */
	if (cinfo->input_components < 1)
	  ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
	break;
  }

  switch (cinfo->jpeg_color_space) {
  case JCS_GRAYSCALE:
	if (cinfo->num_components != 1)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	if (cinfo->in_color_space == JCS_GRAYSCALE)
	  cconvert->pub.color_convert = grayscale_convert;
	else if (cinfo->in_color_space == JCS_RGB) {
	  cconvert->pub.start_pass = rgb_ycc_start;
	  cconvert->pub.color_convert = rgb_gray_convert;
	} else if (cinfo->in_color_space == JCS_YCbCr)
	  cconvert->pub.color_convert = grayscale_convert;
	else
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;

  case JCS_RGB:
	if (cinfo->num_components != 3)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
	  cconvert->pub.color_convert = null_convert;
	else
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;

  case JCS_YCbCr:
	if (cinfo->num_components != 3)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	if (cinfo->in_color_space == JCS_RGB) {
	  cconvert->pub.start_pass = rgb_ycc_start;
	  cconvert->pub.color_convert = rgb_ycc_convert;
	} else if (cinfo->in_color_space == JCS_YCbCr)
	  cconvert->pub.color_convert = null_convert;
	else
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;

  case JCS_CMYK:
	if (cinfo->num_components != 4)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	if (cinfo->in_color_space == JCS_CMYK)
	  cconvert->pub.color_convert = null_convert;
	else
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;

  case JCS_YCCK:
	if (cinfo->num_components != 4)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	if (cinfo->in_color_space == JCS_CMYK) {
	  cconvert->pub.start_pass = rgb_ycc_start;
	  cconvert->pub.color_convert = cmyk_ycck_convert;
	} else if (cinfo->in_color_space == JCS_YCCK)
	  cconvert->pub.color_convert = null_convert;
	else
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;

  default:			/* allow null conversion of JCS_UNKNOWN */
	if (cinfo->jpeg_color_space != cinfo->in_color_space ||
	cinfo->num_components != cinfo->input_components)
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	cconvert->pub.color_convert = null_convert;
	break;
  }
}
/*** End of inlined file: jccolor.c ***/

	#undef FIX

/*** Start of inlined file: jcdctmgr.c ***/
#define JPEG_INTERNALS


/*** Start of inlined file: jdct.h ***/
#ifndef __jdct_h__
#define __jdct_h__

#if BITS_IN_JSAMPLE == 8
typedef int DCTELEM;		/* 16 or 32 bits is fine */
#else
typedef INT32 DCTELEM;		/* must have 32 bits */
#endif

typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data));
typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data));

typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
#if BITS_IN_JSAMPLE == 8
typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
#define IFAST_SCALE_BITS  2	/* fractional bits in scale factors */
#else
typedef INT32 IFAST_MULT_TYPE;	/* need 32 bits for scaled quantizers */
#define IFAST_SCALE_BITS  13	/* fractional bits in scale factors */
#endif
typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */

#define IDCT_range_limit(cinfo)  ((cinfo)->sample_range_limit + CENTERJSAMPLE)

#define RANGE_MASK  (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */

#ifdef NEED_SHORT_EXTERNAL_NAMES
#define jpeg_fdct_islow		jFDislow
#define jpeg_fdct_ifast		jFDifast
#define jpeg_fdct_float		jFDfloat
#define jpeg_idct_islow		jRDislow
#define jpeg_idct_ifast		jRDifast
#define jpeg_idct_float		jRDfloat
#define jpeg_idct_4x4		jRD4x4
#define jpeg_idct_2x2		jRD2x2
#define jpeg_idct_1x1		jRD1x1
#endif /* NEED_SHORT_EXTERNAL_NAMES */

EXTERN(void) jpeg_fdct_islow JPP((DCTELEM * data));
EXTERN(void) jpeg_fdct_ifast JPP((DCTELEM * data));
EXTERN(void) jpeg_fdct_float JPP((FAST_FLOAT * data));

EXTERN(void) jpeg_idct_islow
	JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
EXTERN(void) jpeg_idct_ifast
	JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
EXTERN(void) jpeg_idct_float
	JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
EXTERN(void) jpeg_idct_4x4
	JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
EXTERN(void) jpeg_idct_2x2
	JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
EXTERN(void) jpeg_idct_1x1
	JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));

#define ONE	((INT32) 1)
#define CONST_SCALE (ONE << CONST_BITS)

#define FIX(x)	((INT32) ((x) * CONST_SCALE + 0.5))

#define DESCALE(x,n)  RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)

#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
#define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT16) (const)))
#endif
#ifdef SHORTxLCONST_32		/* known to work with Microsoft C 6.0 */
#define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT32) (const)))
#endif

#ifndef MULTIPLY16C16		/* default definition */
#define MULTIPLY16C16(var,const)  ((var) * (const))
#endif

#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
#define MULTIPLY16V16(var1,var2)  (((INT16) (var1)) * ((INT16) (var2)))
#endif

#ifndef MULTIPLY16V16		/* default definition */
#define MULTIPLY16V16(var1,var2)  ((var1) * (var2))
#endif

#endif
/*** End of inlined file: jdct.h ***/

		/* Private declarations for DCT subsystem */

typedef struct {
  struct jpeg_forward_dct pub;	/* public fields */

  forward_DCT_method_ptr do_dct;

  DCTELEM * divisors[NUM_QUANT_TBLS];

#ifdef DCT_FLOAT_SUPPORTED
  float_DCT_method_ptr do_float_dct;
  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
#endif
} my_fdct_controller;

typedef my_fdct_controller * my_fdct_ptr;

METHODDEF(void)
start_pass_fdctmgr (j_compress_ptr cinfo)
{
  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  int ci, qtblno, i;
  jpeg_component_info *compptr;
  JQUANT_TBL * qtbl;
  DCTELEM * dtbl;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	qtblno = compptr->quant_tbl_no;
	if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
	cinfo->quant_tbl_ptrs[qtblno] == NULL)
	  ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
	qtbl = cinfo->quant_tbl_ptrs[qtblno];
	switch (cinfo->dct_method) {
#ifdef DCT_ISLOW_SUPPORTED
	case JDCT_ISLOW:
	  if (fdct->divisors[qtblno] == NULL) {
	fdct->divisors[qtblno] = (DCTELEM *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					  DCTSIZE2 * SIZEOF(DCTELEM));
	  }
	  dtbl = fdct->divisors[qtblno];
	  for (i = 0; i < DCTSIZE2; i++) {
	dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
	  }
	  break;
#endif
#ifdef DCT_IFAST_SUPPORTED
	case JDCT_IFAST:
	  {
#define CONST_BITS 14
	static const INT16 aanscales[DCTSIZE2] = {
	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
	};
	SHIFT_TEMPS

	if (fdct->divisors[qtblno] == NULL) {
	  fdct->divisors[qtblno] = (DCTELEM *)
		(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					DCTSIZE2 * SIZEOF(DCTELEM));
	}
	dtbl = fdct->divisors[qtblno];
	for (i = 0; i < DCTSIZE2; i++) {
	  dtbl[i] = (DCTELEM)
		DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
				  (INT32) aanscales[i]),
			CONST_BITS-3);
	}
	  }
	  break;
#endif
#ifdef DCT_FLOAT_SUPPORTED
	case JDCT_FLOAT:
	  {
	FAST_FLOAT * fdtbl;
	int row, col;
	static const double aanscalefactor[DCTSIZE] = {
	  1.0, 1.387039845, 1.306562965, 1.175875602,
	  1.0, 0.785694958, 0.541196100, 0.275899379
	};

	if (fdct->float_divisors[qtblno] == NULL) {
	  fdct->float_divisors[qtblno] = (FAST_FLOAT *)
		(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					DCTSIZE2 * SIZEOF(FAST_FLOAT));
	}
	fdtbl = fdct->float_divisors[qtblno];
	i = 0;
	for (row = 0; row < DCTSIZE; row++) {
	  for (col = 0; col < DCTSIZE; col++) {
		fdtbl[i] = (FAST_FLOAT)
		  (1.0 / (((double) qtbl->quantval[i] *
			   aanscalefactor[row] * aanscalefactor[col] * 8.0)));
		i++;
	  }
	}
	  }
	  break;
#endif
	default:
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
	  break;
	}
  }
}

METHODDEF(void)
forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
		 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
		 JDIMENSION start_row, JDIMENSION start_col,
		 JDIMENSION num_blocks)
{
  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  forward_DCT_method_ptr do_dct = fdct->do_dct;
  DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
  DCTELEM workspace[DCTSIZE2];	/* work area for FDCT subroutine */
  JDIMENSION bi;

  sample_data += start_row;	/* fold in the vertical offset once */

  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
	{ register DCTELEM *workspaceptr;
	  register JSAMPROW elemptr;
	  register int elemr;

	  workspaceptr = workspace;
	  for (elemr = 0; elemr < DCTSIZE; elemr++) {
	elemptr = sample_data[elemr] + start_col;
#if DCTSIZE == 8		/* unroll the inner loop */
	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
#else
	{ register int elemc;
	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
		*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
	  }
	}
#endif
	  }
	}

	(*do_dct) (workspace);

	{ register DCTELEM temp, qval;
	  register int i;
	  register JCOEFPTR output_ptr = coef_blocks[bi];

	  for (i = 0; i < DCTSIZE2; i++) {
	qval = divisors[i];
	temp = workspace[i];
#ifdef FAST_DIVIDE
#define DIVIDE_BY(a,b)	a /= b
#else
#define DIVIDE_BY(a,b)	if (a >= b) a /= b; else a = 0
#endif
	if (temp < 0) {
	  temp = -temp;
	  temp += qval>>1;	/* for rounding */
	  DIVIDE_BY(temp, qval);
	  temp = -temp;
	} else {
	  temp += qval>>1;	/* for rounding */
	  DIVIDE_BY(temp, qval);
	}
	output_ptr[i] = (JCOEF) temp;
	  }
	}
  }
}

#ifdef DCT_FLOAT_SUPPORTED

METHODDEF(void)
forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
		   JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
		   JDIMENSION start_row, JDIMENSION start_col,
		   JDIMENSION num_blocks)
{
  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  float_DCT_method_ptr do_dct = fdct->do_float_dct;
  FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  JDIMENSION bi;

  sample_data += start_row;	/* fold in the vertical offset once */

  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
	{ register FAST_FLOAT *workspaceptr;
	  register JSAMPROW elemptr;
	  register int elemr;

	  workspaceptr = workspace;
	  for (elemr = 0; elemr < DCTSIZE; elemr++) {
	elemptr = sample_data[elemr] + start_col;
#if DCTSIZE == 8		/* unroll the inner loop */
	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
#else
	{ register int elemc;
	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
		*workspaceptr++ = (FAST_FLOAT)
		  (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
	  }
	}
#endif
	  }
	}

	(*do_dct) (workspace);

	{ register FAST_FLOAT temp;
	  register int i;
	  register JCOEFPTR output_ptr = coef_blocks[bi];

	  for (i = 0; i < DCTSIZE2; i++) {
	temp = workspace[i] * divisors[i];
	output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
	  }
	}
  }
}

#endif /* DCT_FLOAT_SUPPORTED */

GLOBAL(void)
jinit_forward_dct (j_compress_ptr cinfo)
{
  my_fdct_ptr fdct;
  int i;

  fdct = (my_fdct_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_fdct_controller));
  cinfo->fdct = (struct jpeg_forward_dct *) fdct;
  fdct->pub.start_pass = start_pass_fdctmgr;

  switch (cinfo->dct_method) {
#ifdef DCT_ISLOW_SUPPORTED
  case JDCT_ISLOW:
	fdct->pub.forward_DCT = forward_DCT;
	fdct->do_dct = jpeg_fdct_islow;
	break;
#endif
#ifdef DCT_IFAST_SUPPORTED
  case JDCT_IFAST:
	fdct->pub.forward_DCT = forward_DCT;
	fdct->do_dct = jpeg_fdct_ifast;
	break;
#endif
#ifdef DCT_FLOAT_SUPPORTED
  case JDCT_FLOAT:
	fdct->pub.forward_DCT = forward_DCT_float;
	fdct->do_float_dct = jpeg_fdct_float;
	break;
#endif
  default:
	ERREXIT(cinfo, JERR_NOT_COMPILED);
	break;
  }

  for (i = 0; i < NUM_QUANT_TBLS; i++) {
	fdct->divisors[i] = NULL;
#ifdef DCT_FLOAT_SUPPORTED
	fdct->float_divisors[i] = NULL;
#endif
  }
}
/*** End of inlined file: jcdctmgr.c ***/


	#undef CONST_BITS

/*** Start of inlined file: jchuff.c ***/
#define JPEG_INTERNALS


/*** Start of inlined file: jchuff.h ***/
#ifndef _jchuff_h_
#define _jchuff_h_

#if BITS_IN_JSAMPLE == 8
#define MAX_COEF_BITS 10
#else
#define MAX_COEF_BITS 14
#endif

typedef struct {
  unsigned int ehufco[256];	/* code for each symbol */
  char ehufsi[256];		/* length of code for each symbol */
} c_derived_tbl;

#ifdef NEED_SHORT_EXTERNAL_NAMES
#define jpeg_make_c_derived_tbl	jMkCDerived
#define jpeg_gen_optimal_table	jGenOptTbl
#endif /* NEED_SHORT_EXTERNAL_NAMES */

EXTERN(void) jpeg_make_c_derived_tbl
	JPP((j_compress_ptr cinfo, boolean isDC, int tblno,
		 c_derived_tbl ** pdtbl));

EXTERN(void) jpeg_gen_optimal_table
	JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[]));

#endif
/*** End of inlined file: jchuff.h ***/

		/* Declarations shared with jcphuff.c */

typedef struct {
  INT32 put_buffer;		/* current bit-accumulation buffer */
  int put_bits;			/* # of bits now in it */
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
} savable_state;

#ifndef NO_STRUCT_ASSIGN
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
#else
#if MAX_COMPS_IN_SCAN == 4
#define ASSIGN_STATE(dest,src)  \
	((dest).put_buffer = (src).put_buffer, \
	 (dest).put_bits = (src).put_bits, \
	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
	 (dest).last_dc_val[3] = (src).last_dc_val[3])
#endif
#endif

typedef struct {
  struct jpeg_entropy_encoder pub; /* public fields */

  savable_state saved;		/* Bit buffer & DC state at start of MCU */

  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
  int next_restart_num;		/* next restart number to write (0-7) */

  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];

#ifdef ENTROPY_OPT_SUPPORTED	/* Statistics tables for optimization */
  long * dc_count_ptrs[NUM_HUFF_TBLS];
  long * ac_count_ptrs[NUM_HUFF_TBLS];
#endif
} huff_entropy_encoder;

typedef huff_entropy_encoder * huff_entropy_ptr;

typedef struct {
  JOCTET * next_output_byte;	/* => next byte to write in buffer */
  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
  savable_state cur;		/* Current bit buffer & DC state */
  j_compress_ptr cinfo;		/* dump_buffer needs access to this */
} working_state;

METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
					JBLOCKROW *MCU_data));
METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
#ifdef ENTROPY_OPT_SUPPORTED
METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
					  JBLOCKROW *MCU_data));
METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
#endif

METHODDEF(void)
start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  int ci, dctbl, actbl;
  jpeg_component_info * compptr;

  if (gather_statistics) {
#ifdef ENTROPY_OPT_SUPPORTED
	entropy->pub.encode_mcu = encode_mcu_gather;
	entropy->pub.finish_pass = finish_pass_gather;
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
  } else {
	entropy->pub.encode_mcu = encode_mcu_huff;
	entropy->pub.finish_pass = finish_pass_huff;
  }

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	dctbl = compptr->dc_tbl_no;
	actbl = compptr->ac_tbl_no;
	if (gather_statistics) {
#ifdef ENTROPY_OPT_SUPPORTED
	  if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
	  if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
	  if (entropy->dc_count_ptrs[dctbl] == NULL)
	entropy->dc_count_ptrs[dctbl] = (long *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					  257 * SIZEOF(long));
	  MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
	  if (entropy->ac_count_ptrs[actbl] == NULL)
	entropy->ac_count_ptrs[actbl] = (long *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					  257 * SIZEOF(long));
	  MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
#endif
	} else {
	  jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
				  & entropy->dc_derived_tbls[dctbl]);
	  jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
				  & entropy->ac_derived_tbls[actbl]);
	}
	entropy->saved.last_dc_val[ci] = 0;
  }

  entropy->saved.put_buffer = 0;
  entropy->saved.put_bits = 0;

  entropy->restarts_to_go = cinfo->restart_interval;
  entropy->next_restart_num = 0;
}

GLOBAL(void)
jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
			 c_derived_tbl ** pdtbl)
{
  JHUFF_TBL *htbl;
  c_derived_tbl *dtbl;
  int p, i, l, lastp, si, maxsymbol;
  char huffsize[257];
  unsigned int huffcode[257];
  unsigned int code;

  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  htbl =
	isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  if (htbl == NULL)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);

  if (*pdtbl == NULL)
	*pdtbl = (c_derived_tbl *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  SIZEOF(c_derived_tbl));
  dtbl = *pdtbl;

  p = 0;
  for (l = 1; l <= 16; l++) {
	i = (int) htbl->bits[l];
	if (i < 0 || p + i > 256)	/* protect against table overrun */
	  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
	while (i--)
	  huffsize[p++] = (char) l;
  }
  huffsize[p] = 0;
  lastp = p;

  code = 0;
  si = huffsize[0];
  p = 0;
  while (huffsize[p]) {
	while (((int) huffsize[p]) == si) {
	  huffcode[p++] = code;
	  code++;
	}
	if (((INT32) code) >= (((INT32) 1) << si))
	  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
	code <<= 1;
	si++;
  }

  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));

  maxsymbol = isDC ? 15 : 255;

  for (p = 0; p < lastp; p++) {
	i = htbl->huffval[p];
	if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
	  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
	dtbl->ehufco[i] = huffcode[p];
	dtbl->ehufsi[i] = huffsize[p];
  }
}

#define emit_byte(state,val,action)  \
	{ *(state)->next_output_byte++ = (JOCTET) (val);  \
	  if (--(state)->free_in_buffer == 0)  \
		if (! dump_buffer(state))  \
		  { action; } }

LOCAL(boolean)
dump_buffer (working_state * state)
{
  struct jpeg_destination_mgr * dest = state->cinfo->dest;

  if (! (*dest->empty_output_buffer) (state->cinfo))
	return FALSE;
  state->next_output_byte = dest->next_output_byte;
  state->free_in_buffer = dest->free_in_buffer;
  return TRUE;
}

INLINE
LOCAL(boolean)
emit_bits (working_state * state, unsigned int code, int size)
{
  register INT32 put_buffer = (INT32) code;
  register int put_bits = state->cur.put_bits;

  if (size == 0)
	ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);

  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */

  put_bits += size;		/* new number of bits in buffer */

  put_buffer <<= 24 - put_bits; /* align incoming bits */

  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */

  while (put_bits >= 8) {
	int c = (int) ((put_buffer >> 16) & 0xFF);

	emit_byte(state, c, return FALSE);
	if (c == 0xFF) {		/* need to stuff a zero byte? */
	  emit_byte(state, 0, return FALSE);
	}
	put_buffer <<= 8;
	put_bits -= 8;
  }

  state->cur.put_buffer = put_buffer; /* update state variables */
  state->cur.put_bits = put_bits;

  return TRUE;
}

LOCAL(boolean)
flush_bits (working_state * state)
{
  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
	return FALSE;
  state->cur.put_buffer = 0;	/* and reset bit-buffer to empty */
  state->cur.put_bits = 0;
  return TRUE;
}

LOCAL(boolean)
encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
		  c_derived_tbl *dctbl, c_derived_tbl *actbl)
{
  register int temp, temp2;
  register int nbits;
  register int k, r, i;

  temp = temp2 = block[0] - last_dc_val;

  if (temp < 0) {
	temp = -temp;		/* temp is abs value of input */
	temp2--;
  }

  nbits = 0;
  while (temp) {
	nbits++;
	temp >>= 1;
  }
  if (nbits > MAX_COEF_BITS+1)
	ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);

  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
	return FALSE;

  if (nbits)			/* emit_bits rejects calls with size 0 */
	if (! emit_bits(state, (unsigned int) temp2, nbits))
	  return FALSE;

  r = 0;			/* r = run length of zeros */

  for (k = 1; k < DCTSIZE2; k++) {
	if ((temp = block[jpeg_natural_order[k]]) == 0) {
	  r++;
	} else {
	  while (r > 15) {
	if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
	  return FALSE;
	r -= 16;
	  }

	  temp2 = temp;
	  if (temp < 0) {
	temp = -temp;		/* temp is abs value of input */
	temp2--;
	  }

	  nbits = 1;		/* there must be at least one 1 bit */
	  while ((temp >>= 1))
	nbits++;
	  if (nbits > MAX_COEF_BITS)
	ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);

	  i = (r << 4) + nbits;
	  if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
	return FALSE;

	  if (! emit_bits(state, (unsigned int) temp2, nbits))
	return FALSE;

	  r = 0;
	}
  }

  if (r > 0)
	if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
	  return FALSE;

  return TRUE;
}

LOCAL(boolean)
emit_restart (working_state * state, int restart_num)
{
  int ci;

  if (! flush_bits(state))
	return FALSE;

  emit_byte(state, 0xFF, return FALSE);
  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);

  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
	state->cur.last_dc_val[ci] = 0;

  return TRUE;
}

METHODDEF(boolean)
encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  working_state state;
  int blkn, ci;
  jpeg_component_info * compptr;

  state.next_output_byte = cinfo->dest->next_output_byte;
  state.free_in_buffer = cinfo->dest->free_in_buffer;
  ASSIGN_STATE(state.cur, entropy->saved);
  state.cinfo = cinfo;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0)
	  if (! emit_restart(&state, entropy->next_restart_num))
	return FALSE;
  }

  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
	ci = cinfo->MCU_membership[blkn];
	compptr = cinfo->cur_comp_info[ci];
	if (! encode_one_block(&state,
			   MCU_data[blkn][0], state.cur.last_dc_val[ci],
			   entropy->dc_derived_tbls[compptr->dc_tbl_no],
			   entropy->ac_derived_tbls[compptr->ac_tbl_no]))
	  return FALSE;
	state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  }

  cinfo->dest->next_output_byte = state.next_output_byte;
  cinfo->dest->free_in_buffer = state.free_in_buffer;
  ASSIGN_STATE(entropy->saved, state.cur);

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0) {
	  entropy->restarts_to_go = cinfo->restart_interval;
	  entropy->next_restart_num++;
	  entropy->next_restart_num &= 7;
	}
	entropy->restarts_to_go--;
  }

  return TRUE;
}

METHODDEF(void)
finish_pass_huff (j_compress_ptr cinfo)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  working_state state;

  state.next_output_byte = cinfo->dest->next_output_byte;
  state.free_in_buffer = cinfo->dest->free_in_buffer;
  ASSIGN_STATE(state.cur, entropy->saved);
  state.cinfo = cinfo;

  if (! flush_bits(&state))
	ERREXIT(cinfo, JERR_CANT_SUSPEND);

  cinfo->dest->next_output_byte = state.next_output_byte;
  cinfo->dest->free_in_buffer = state.free_in_buffer;
  ASSIGN_STATE(entropy->saved, state.cur);
}

#ifdef ENTROPY_OPT_SUPPORTED

LOCAL(void)
htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
		 long dc_counts[], long ac_counts[])
{
  register int temp;
  register int nbits;
  register int k, r;

  temp = block[0] - last_dc_val;
  if (temp < 0)
	temp = -temp;

  nbits = 0;
  while (temp) {
	nbits++;
	temp >>= 1;
  }
  if (nbits > MAX_COEF_BITS+1)
	ERREXIT(cinfo, JERR_BAD_DCT_COEF);

  dc_counts[nbits]++;

  r = 0;			/* r = run length of zeros */

  for (k = 1; k < DCTSIZE2; k++) {
	if ((temp = block[jpeg_natural_order[k]]) == 0) {
	  r++;
	} else {
	  while (r > 15) {
	ac_counts[0xF0]++;
	r -= 16;
	  }

	  if (temp < 0)
	temp = -temp;

	  nbits = 1;		/* there must be at least one 1 bit */
	  while ((temp >>= 1))
	nbits++;
	  if (nbits > MAX_COEF_BITS)
	ERREXIT(cinfo, JERR_BAD_DCT_COEF);

	  ac_counts[(r << 4) + nbits]++;

	  r = 0;
	}
  }

  if (r > 0)
	ac_counts[0]++;
}

METHODDEF(boolean)
encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  int blkn, ci;
  jpeg_component_info * compptr;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0) {
	  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
	entropy->saved.last_dc_val[ci] = 0;
	  entropy->restarts_to_go = cinfo->restart_interval;
	}
	entropy->restarts_to_go--;
  }

  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
	ci = cinfo->MCU_membership[blkn];
	compptr = cinfo->cur_comp_info[ci];
	htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
			entropy->dc_count_ptrs[compptr->dc_tbl_no],
			entropy->ac_count_ptrs[compptr->ac_tbl_no]);
	entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
  }

  return TRUE;
}

GLOBAL(void)
jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
{
#define MAX_CLEN 32		/* assumed maximum initial code length */
  UINT8 bits[MAX_CLEN+1];	/* bits[k] = # of symbols with code length k */
  int codesize[257];		/* codesize[k] = code length of symbol k */
  int others[257];		/* next symbol in current branch of tree */
  int c1, c2;
  int p, i, j;
  long v;

  MEMZERO(bits, SIZEOF(bits));
  MEMZERO(codesize, SIZEOF(codesize));
  for (i = 0; i < 257; i++)
	others[i] = -1;		/* init links to empty */

  freq[256] = 1;		/* make sure 256 has a nonzero count */

  for (;;) {
	c1 = -1;
	v = 1000000000L;
	for (i = 0; i <= 256; i++) {
	  if (freq[i] && freq[i] <= v) {
	v = freq[i];
	c1 = i;
	  }
	}

	c2 = -1;
	v = 1000000000L;
	for (i = 0; i <= 256; i++) {
	  if (freq[i] && freq[i] <= v && i != c1) {
	v = freq[i];
	c2 = i;
	  }
	}

	if (c2 < 0)
	  break;

	freq[c1] += freq[c2];
	freq[c2] = 0;

	codesize[c1]++;
	while (others[c1] >= 0) {
	  c1 = others[c1];
	  codesize[c1]++;
	}

	others[c1] = c2;		/* chain c2 onto c1's tree branch */

	codesize[c2]++;
	while (others[c2] >= 0) {
	  c2 = others[c2];
	  codesize[c2]++;
	}
  }

  for (i = 0; i <= 256; i++) {
	if (codesize[i]) {
	  if (codesize[i] > MAX_CLEN)
	ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);

	  bits[codesize[i]]++;
	}
  }

  for (i = MAX_CLEN; i > 16; i--) {
	while (bits[i] > 0) {
	  j = i - 2;		/* find length of new prefix to be used */
	  while (bits[j] == 0)
	j--;

	  bits[i] -= 2;		/* remove two symbols */
	  bits[i-1]++;		/* one goes in this length */
	  bits[j+1] += 2;		/* two new symbols in this length */
	  bits[j]--;		/* symbol of this length is now a prefix */
	}
  }

  while (bits[i] == 0)		/* find largest codelength still in use */
	i--;
  bits[i]--;

  MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));

  p = 0;
  for (i = 1; i <= MAX_CLEN; i++) {
	for (j = 0; j <= 255; j++) {
	  if (codesize[j] == i) {
	htbl->huffval[p] = (UINT8) j;
	p++;
	  }
	}
  }

  htbl->sent_table = FALSE;
}

METHODDEF(void)
finish_pass_gather (j_compress_ptr cinfo)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  int ci, dctbl, actbl;
  jpeg_component_info * compptr;
  JHUFF_TBL **htblptr;
  boolean did_dc[NUM_HUFF_TBLS];
  boolean did_ac[NUM_HUFF_TBLS];

  MEMZERO(did_dc, SIZEOF(did_dc));
  MEMZERO(did_ac, SIZEOF(did_ac));

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	dctbl = compptr->dc_tbl_no;
	actbl = compptr->ac_tbl_no;
	if (! did_dc[dctbl]) {
	  htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
	  if (*htblptr == NULL)
	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
	  jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
	  did_dc[dctbl] = TRUE;
	}
	if (! did_ac[actbl]) {
	  htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
	  if (*htblptr == NULL)
	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
	  jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
	  did_ac[actbl] = TRUE;
	}
  }
}

#endif /* ENTROPY_OPT_SUPPORTED */

GLOBAL(void)
jinit_huff_encoder (j_compress_ptr cinfo)
{
  huff_entropy_ptr entropy;
  int i;

  entropy = (huff_entropy_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(huff_entropy_encoder));
  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  entropy->pub.start_pass = start_pass_huff;

  for (i = 0; i < NUM_HUFF_TBLS; i++) {
	entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
#ifdef ENTROPY_OPT_SUPPORTED
	entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
#endif
  }
}
/*** End of inlined file: jchuff.c ***/


	#undef emit_byte

/*** Start of inlined file: jcinit.c ***/
#define JPEG_INTERNALS

GLOBAL(void)
jinit_compress_master (j_compress_ptr cinfo)
{
  jinit_c_master_control(cinfo, FALSE /* full compression */);

  if (! cinfo->raw_data_in) {
	jinit_color_converter(cinfo);
	jinit_downsampler(cinfo);
	jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
  }
  jinit_forward_dct(cinfo);
  if (cinfo->arith_code) {
	ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  } else {
	if (cinfo->progressive_mode) {
#ifdef C_PROGRESSIVE_SUPPORTED
	  jinit_phuff_encoder(cinfo);
#else
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
	} else
	  jinit_huff_encoder(cinfo);
  }

  jinit_c_coef_controller(cinfo,
		(boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
  jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);

  jinit_marker_writer(cinfo);

  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);

  (*cinfo->marker->write_file_header) (cinfo);
}
/*** End of inlined file: jcinit.c ***/



/*** Start of inlined file: jcmainct.c ***/
#define JPEG_INTERNALS

#undef FULL_MAIN_BUFFER_SUPPORTED

typedef struct {
  struct jpeg_c_main_controller pub; /* public fields */

  JDIMENSION cur_iMCU_row;	/* number of current iMCU row */
  JDIMENSION rowgroup_ctr;	/* counts row groups received in iMCU row */
  boolean suspended;		/* remember if we suspended output */
  J_BUF_MODE pass_mode;		/* current operating mode */

  JSAMPARRAY buffer[MAX_COMPONENTS];

#ifdef FULL_MAIN_BUFFER_SUPPORTED
  jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
#endif
} my_main_controller;

typedef my_main_controller * my_main_ptr;

METHODDEF(void) process_data_simple_main
	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
		 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
#ifdef FULL_MAIN_BUFFER_SUPPORTED
METHODDEF(void) process_data_buffer_main
	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
		 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
#endif

METHODDEF(void)
start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
{
  my_main_ptr main_ = (my_main_ptr) cinfo->main;

  if (cinfo->raw_data_in)
	return;

  main_->cur_iMCU_row = 0;	/* initialize counters */
  main_->rowgroup_ctr = 0;
  main_->suspended = FALSE;
  main_->pass_mode = pass_mode;	/* save mode for use by process_data */

  switch (pass_mode) {
  case JBUF_PASS_THRU:
#ifdef FULL_MAIN_BUFFER_SUPPORTED
	if (main_->whole_image[0] != NULL)
	  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
#endif
	main_->pub.process_data = process_data_simple_main;
	break;
#ifdef FULL_MAIN_BUFFER_SUPPORTED
  case JBUF_SAVE_SOURCE:
  case JBUF_CRANK_DEST:
  case JBUF_SAVE_AND_PASS:
	if (main_->whole_image[0] == NULL)
	  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	main_->pub.process_data = process_data_buffer_main;
	break;
#endif
  default:
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	break;
  }
}

METHODDEF(void)
process_data_simple_main (j_compress_ptr cinfo,
			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
			  JDIMENSION in_rows_avail)
{
  my_main_ptr main_ = (my_main_ptr) cinfo->main;

  while (main_->cur_iMCU_row < cinfo->total_iMCU_rows) {
	if (main_->rowgroup_ctr < DCTSIZE)
	  (*cinfo->prep->pre_process_data) (cinfo,
					input_buf, in_row_ctr, in_rows_avail,
					main_->buffer, &main_->rowgroup_ctr,
					(JDIMENSION) DCTSIZE);

	if (main_->rowgroup_ctr != DCTSIZE)
	  return;

	if (! (*cinfo->coef->compress_data) (cinfo, main_->buffer)) {
	  if (! main_->suspended) {
	(*in_row_ctr)--;
	main_->suspended = TRUE;
	  }
	  return;
	}
	if (main_->suspended) {
	  (*in_row_ctr)++;
	  main_->suspended = FALSE;
	}
	main_->rowgroup_ctr = 0;
	main_->cur_iMCU_row++;
  }
}

#ifdef FULL_MAIN_BUFFER_SUPPORTED

METHODDEF(void)
process_data_buffer_main (j_compress_ptr cinfo,
			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
			  JDIMENSION in_rows_avail)
{
  my_main_ptr main = (my_main_ptr) cinfo->main;
  int ci;
  jpeg_component_info *compptr;
  boolean writing = (main->pass_mode != JBUF_CRANK_DEST);

  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
	if (main->rowgroup_ctr == 0) {
	  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
	  ((j_common_ptr) cinfo, main->whole_image[ci],
	   main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
	   (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
	  }
	  if (! writing) {
	*in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
	main->rowgroup_ctr = DCTSIZE;
	  }
	}

	if (writing) {
	  (*cinfo->prep->pre_process_data) (cinfo,
					input_buf, in_row_ctr, in_rows_avail,
					main->buffer, &main->rowgroup_ctr,
					(JDIMENSION) DCTSIZE);
	  if (main->rowgroup_ctr < DCTSIZE)
	return;
	}

	if (main->pass_mode != JBUF_SAVE_SOURCE) {
	  if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
	if (! main->suspended) {
	  (*in_row_ctr)--;
	  main->suspended = TRUE;
	}
	return;
	  }
	  if (main->suspended) {
	(*in_row_ctr)++;
	main->suspended = FALSE;
	  }
	}

	main->rowgroup_ctr = 0;
	main->cur_iMCU_row++;
  }
}

#endif /* FULL_MAIN_BUFFER_SUPPORTED */

GLOBAL(void)
jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
{
  my_main_ptr main_;
  int ci;
  jpeg_component_info *compptr;

  main_ = (my_main_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_main_controller));
  cinfo->main = (struct jpeg_c_main_controller *) main_;
  main_->pub.start_pass = start_pass_main;

  if (cinfo->raw_data_in)
	return;

  if (need_full_buffer) {
#ifdef FULL_MAIN_BUFFER_SUPPORTED
	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
	  main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
	 compptr->width_in_blocks * DCTSIZE,
	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
				(long) compptr->v_samp_factor) * DCTSIZE,
	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
	}
#else
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
#endif
  } else {
#ifdef FULL_MAIN_BUFFER_SUPPORTED
	main_->whole_image[0] = NULL; /* flag for no virtual arrays */
#endif
	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
	  main_->buffer[ci] = (*cinfo->mem->alloc_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE,
	 compptr->width_in_blocks * DCTSIZE,
	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
	}
  }
}
/*** End of inlined file: jcmainct.c ***/


/*** Start of inlined file: jcmarker.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_marker_writer pub; /* public fields */

  unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
} my_marker_writer;

typedef my_marker_writer * my_marker_ptr;

LOCAL(void)
emit_byte (j_compress_ptr cinfo, int val)
{
  struct jpeg_destination_mgr * dest = cinfo->dest;

  *(dest->next_output_byte)++ = (JOCTET) val;
  if (--dest->free_in_buffer == 0) {
	if (! (*dest->empty_output_buffer) (cinfo))
	  ERREXIT(cinfo, JERR_CANT_SUSPEND);
  }
}

LOCAL(void)
emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
{
  emit_byte(cinfo, 0xFF);
  emit_byte(cinfo, (int) mark);
}

LOCAL(void)
emit_2bytes (j_compress_ptr cinfo, int value)
{
  emit_byte(cinfo, (value >> 8) & 0xFF);
  emit_byte(cinfo, value & 0xFF);
}

LOCAL(int)
emit_dqt (j_compress_ptr cinfo, int index)
{
  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
  int prec;
  int i;

  if (qtbl == NULL)
	ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);

  prec = 0;
  for (i = 0; i < DCTSIZE2; i++) {
	if (qtbl->quantval[i] > 255)
	  prec = 1;
  }

  if (! qtbl->sent_table) {
	emit_marker(cinfo, M_DQT);

	emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);

	emit_byte(cinfo, index + (prec<<4));

	for (i = 0; i < DCTSIZE2; i++) {
	  unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
	  if (prec)
	emit_byte(cinfo, (int) (qval >> 8));
	  emit_byte(cinfo, (int) (qval & 0xFF));
	}

	qtbl->sent_table = TRUE;
  }

  return prec;
}

LOCAL(void)
emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
{
  JHUFF_TBL * htbl;
  int length, i;

  if (is_ac) {
	htbl = cinfo->ac_huff_tbl_ptrs[index];
	index += 0x10;		/* output index has AC bit set */
  } else {
	htbl = cinfo->dc_huff_tbl_ptrs[index];
  }

  if (htbl == NULL)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);

  if (! htbl->sent_table) {
	emit_marker(cinfo, M_DHT);

	length = 0;
	for (i = 1; i <= 16; i++)
	  length += htbl->bits[i];

	emit_2bytes(cinfo, length + 2 + 1 + 16);
	emit_byte(cinfo, index);

	for (i = 1; i <= 16; i++)
	  emit_byte(cinfo, htbl->bits[i]);

	for (i = 0; i < length; i++)
	  emit_byte(cinfo, htbl->huffval[i]);

	htbl->sent_table = TRUE;
  }
}

LOCAL(void)
emit_dac (j_compress_ptr cinfo)
{
#ifdef C_ARITH_CODING_SUPPORTED
  char dc_in_use[NUM_ARITH_TBLS];
  char ac_in_use[NUM_ARITH_TBLS];
  int length, i;
  jpeg_component_info *compptr;

  for (i = 0; i < NUM_ARITH_TBLS; i++)
	dc_in_use[i] = ac_in_use[i] = 0;

  for (i = 0; i < cinfo->comps_in_scan; i++) {
	compptr = cinfo->cur_comp_info[i];
	dc_in_use[compptr->dc_tbl_no] = 1;
	ac_in_use[compptr->ac_tbl_no] = 1;
  }

  length = 0;
  for (i = 0; i < NUM_ARITH_TBLS; i++)
	length += dc_in_use[i] + ac_in_use[i];

  emit_marker(cinfo, M_DAC);

  emit_2bytes(cinfo, length*2 + 2);

  for (i = 0; i < NUM_ARITH_TBLS; i++) {
	if (dc_in_use[i]) {
	  emit_byte(cinfo, i);
	  emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
	}
	if (ac_in_use[i]) {
	  emit_byte(cinfo, i + 0x10);
	  emit_byte(cinfo, cinfo->arith_ac_K[i]);
	}
  }
#endif /* C_ARITH_CODING_SUPPORTED */
}

LOCAL(void)
emit_dri (j_compress_ptr cinfo)
{
  emit_marker(cinfo, M_DRI);

  emit_2bytes(cinfo, 4);	/* fixed length */

  emit_2bytes(cinfo, (int) cinfo->restart_interval);
}

LOCAL(void)
emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
{
  int ci;
  jpeg_component_info *compptr;

  emit_marker(cinfo, code);

  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */

  if ((long) cinfo->image_height > 65535L ||
	  (long) cinfo->image_width > 65535L)
	ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);

  emit_byte(cinfo, cinfo->data_precision);
  emit_2bytes(cinfo, (int) cinfo->image_height);
  emit_2bytes(cinfo, (int) cinfo->image_width);

  emit_byte(cinfo, cinfo->num_components);

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	emit_byte(cinfo, compptr->component_id);
	emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
	emit_byte(cinfo, compptr->quant_tbl_no);
  }
}

LOCAL(void)
emit_sos (j_compress_ptr cinfo)
{
  int i, td, ta;
  jpeg_component_info *compptr;

  emit_marker(cinfo, M_SOS);

  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */

  emit_byte(cinfo, cinfo->comps_in_scan);

  for (i = 0; i < cinfo->comps_in_scan; i++) {
	compptr = cinfo->cur_comp_info[i];
	emit_byte(cinfo, compptr->component_id);
	td = compptr->dc_tbl_no;
	ta = compptr->ac_tbl_no;
	if (cinfo->progressive_mode) {
	  if (cinfo->Ss == 0) {
	ta = 0;			/* DC scan */
	if (cinfo->Ah != 0 && !cinfo->arith_code)
	  td = 0;		/* no DC table either */
	  } else {
	td = 0;			/* AC scan */
	  }
	}
	emit_byte(cinfo, (td << 4) + ta);
  }

  emit_byte(cinfo, cinfo->Ss);
  emit_byte(cinfo, cinfo->Se);
  emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
}

LOCAL(void)
emit_jfif_app0 (j_compress_ptr cinfo)
{

  emit_marker(cinfo, M_APP0);

  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */

  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
  emit_byte(cinfo, 0x46);
  emit_byte(cinfo, 0x49);
  emit_byte(cinfo, 0x46);
  emit_byte(cinfo, 0);
  emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
  emit_byte(cinfo, cinfo->JFIF_minor_version);
  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
  emit_2bytes(cinfo, (int) cinfo->X_density);
  emit_2bytes(cinfo, (int) cinfo->Y_density);
  emit_byte(cinfo, 0);		/* No thumbnail image */
  emit_byte(cinfo, 0);
}

LOCAL(void)
emit_adobe_app14 (j_compress_ptr cinfo)
{

  emit_marker(cinfo, M_APP14);

  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */

  emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
  emit_byte(cinfo, 0x64);
  emit_byte(cinfo, 0x6F);
  emit_byte(cinfo, 0x62);
  emit_byte(cinfo, 0x65);
  emit_2bytes(cinfo, 100);	/* Version */
  emit_2bytes(cinfo, 0);	/* Flags0 */
  emit_2bytes(cinfo, 0);	/* Flags1 */
  switch (cinfo->jpeg_color_space) {
  case JCS_YCbCr:
	emit_byte(cinfo, 1);	/* Color transform = 1 */
	break;
  case JCS_YCCK:
	emit_byte(cinfo, 2);	/* Color transform = 2 */
	break;
  default:
	emit_byte(cinfo, 0);	/* Color transform = 0 */
	break;
  }
}

METHODDEF(void)
write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
{
  if (datalen > (unsigned int) 65533)		/* safety check */
	ERREXIT(cinfo, JERR_BAD_LENGTH);

  emit_marker(cinfo, (JPEG_MARKER) marker);

  emit_2bytes(cinfo, (int) (datalen + 2));	/* total length */
}

METHODDEF(void)
write_marker_byte (j_compress_ptr cinfo, int val)
{
  emit_byte(cinfo, val);
}

METHODDEF(void)
write_file_header (j_compress_ptr cinfo)
{
  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;

  emit_marker(cinfo, M_SOI);	/* first the SOI */

  marker->last_restart_interval = 0;

  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
	emit_jfif_app0(cinfo);
  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
	emit_adobe_app14(cinfo);
}

METHODDEF(void)
write_frame_header (j_compress_ptr cinfo)
{
  int ci, prec;
  boolean is_baseline;
  jpeg_component_info *compptr;

  prec = 0;
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	prec += emit_dqt(cinfo, compptr->quant_tbl_no);
  }

  if (cinfo->arith_code || cinfo->progressive_mode ||
	  cinfo->data_precision != 8) {
	is_baseline = FALSE;
  } else {
	is_baseline = TRUE;
	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
	  if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
	is_baseline = FALSE;
	}
	if (prec && is_baseline) {
	  is_baseline = FALSE;
	  TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
	}
  }

  if (cinfo->arith_code) {
	emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
  } else {
	if (cinfo->progressive_mode)
	  emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
	else if (is_baseline)
	  emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
	else
	  emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
  }
}

METHODDEF(void)
write_scan_header (j_compress_ptr cinfo)
{
  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  int i;
  jpeg_component_info *compptr;

  if (cinfo->arith_code) {
	emit_dac(cinfo);
  } else {
	for (i = 0; i < cinfo->comps_in_scan; i++) {
	  compptr = cinfo->cur_comp_info[i];
	  if (cinfo->progressive_mode) {
	if (cinfo->Ss == 0) {
	  if (cinfo->Ah == 0)	/* DC needs no table for refinement scan */
		emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
	} else {
	  emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
	}
	  } else {
	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
	  }
	}
  }

  if (cinfo->restart_interval != marker->last_restart_interval) {
	emit_dri(cinfo);
	marker->last_restart_interval = cinfo->restart_interval;
  }

  emit_sos(cinfo);
}

METHODDEF(void)
write_file_trailer (j_compress_ptr cinfo)
{
  emit_marker(cinfo, M_EOI);
}

METHODDEF(void)
write_tables_only (j_compress_ptr cinfo)
{
  int i;

  emit_marker(cinfo, M_SOI);

  for (i = 0; i < NUM_QUANT_TBLS; i++) {
	if (cinfo->quant_tbl_ptrs[i] != NULL)
	  (void) emit_dqt(cinfo, i);
  }

  if (! cinfo->arith_code) {
	for (i = 0; i < NUM_HUFF_TBLS; i++) {
	  if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
	emit_dht(cinfo, i, FALSE);
	  if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
	emit_dht(cinfo, i, TRUE);
	}
  }

  emit_marker(cinfo, M_EOI);
}

GLOBAL(void)
jinit_marker_writer (j_compress_ptr cinfo)
{
  my_marker_ptr marker;

  marker = (my_marker_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_marker_writer));
  cinfo->marker = (struct jpeg_marker_writer *) marker;
  marker->pub.write_file_header = write_file_header;
  marker->pub.write_frame_header = write_frame_header;
  marker->pub.write_scan_header = write_scan_header;
  marker->pub.write_file_trailer = write_file_trailer;
  marker->pub.write_tables_only = write_tables_only;
  marker->pub.write_marker_header = write_marker_header;
  marker->pub.write_marker_byte = write_marker_byte;
  marker->last_restart_interval = 0;
}
/*** End of inlined file: jcmarker.c ***/


/*** Start of inlined file: jcmaster.c ***/
#define JPEG_INTERNALS

typedef enum {
	main_pass,		/* input data, also do first output step */
	huff_opt_pass,		/* Huffman code optimization pass */
	output_pass		/* data output pass */
} c_pass_type;

typedef struct {
  struct jpeg_comp_master pub;	/* public fields */

  c_pass_type pass_type;	/* the type of the current pass */

  int pass_number;		/* # of passes completed */
  int total_passes;		/* total # of passes needed */

  int scan_number;		/* current index in scan_info[] */
} my_comp_master;

typedef my_comp_master * my_master_ptr;

LOCAL(void)
initial_setup (j_compress_ptr cinfo)
{
  int ci;
  jpeg_component_info *compptr;
  long samplesperrow;
  JDIMENSION jd_samplesperrow;

  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
	  || cinfo->num_components <= 0 || cinfo->input_components <= 0)
	ERREXIT(cinfo, JERR_EMPTY_IMAGE);

  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
	  (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
	ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);

  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  jd_samplesperrow = (JDIMENSION) samplesperrow;
  if ((long) jd_samplesperrow != samplesperrow)
	ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);

  if (cinfo->data_precision != BITS_IN_JSAMPLE)
	ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);

  if (cinfo->num_components > MAX_COMPONENTS)
	ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
		 MAX_COMPONENTS);

  cinfo->max_h_samp_factor = 1;
  cinfo->max_v_samp_factor = 1;
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
	  ERREXIT(cinfo, JERR_BAD_SAMPLING);
	cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
				   compptr->h_samp_factor);
	cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
				   compptr->v_samp_factor);
  }

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	compptr->component_index = ci;
	compptr->DCT_scaled_size = DCTSIZE;
	compptr->width_in_blocks = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
			(long) (cinfo->max_h_samp_factor * DCTSIZE));
	compptr->height_in_blocks = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
			(long) (cinfo->max_v_samp_factor * DCTSIZE));
	compptr->downsampled_width = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
			(long) cinfo->max_h_samp_factor);
	compptr->downsampled_height = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
			(long) cinfo->max_v_samp_factor);
	compptr->component_needed = TRUE;
  }

  cinfo->total_iMCU_rows = (JDIMENSION)
	jdiv_round_up((long) cinfo->image_height,
		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
}

#ifdef C_MULTISCAN_FILES_SUPPORTED

LOCAL(void)
validate_script (j_compress_ptr cinfo)
{
  const jpeg_scan_info * scanptr;
  int scanno, ncomps, ci, coefi, thisi;
  int Ss, Se, Ah, Al;
  boolean component_sent[MAX_COMPONENTS];
#ifdef C_PROGRESSIVE_SUPPORTED
  int * last_bitpos_ptr;
  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
#endif

  if (cinfo->num_scans <= 0)
	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);

  scanptr = cinfo->scan_info;
  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
#ifdef C_PROGRESSIVE_SUPPORTED
	cinfo->progressive_mode = TRUE;
	last_bitpos_ptr = & last_bitpos[0][0];
	for (ci = 0; ci < cinfo->num_components; ci++)
	  for (coefi = 0; coefi < DCTSIZE2; coefi++)
	*last_bitpos_ptr++ = -1;
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
  } else {
	cinfo->progressive_mode = FALSE;
	for (ci = 0; ci < cinfo->num_components; ci++)
	  component_sent[ci] = FALSE;
  }

  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
	ncomps = scanptr->comps_in_scan;
	if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
	  ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
	for (ci = 0; ci < ncomps; ci++) {
	  thisi = scanptr->component_index[ci];
	  if (thisi < 0 || thisi >= cinfo->num_components)
	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
	  if (ci > 0 && thisi <= scanptr->component_index[ci-1])
	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
	}
	Ss = scanptr->Ss;
	Se = scanptr->Se;
	Ah = scanptr->Ah;
	Al = scanptr->Al;
	if (cinfo->progressive_mode) {
#ifdef C_PROGRESSIVE_SUPPORTED
#if BITS_IN_JSAMPLE == 8
#define MAX_AH_AL 10
#else
#define MAX_AH_AL 13
#endif
	  if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
	  Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
	  if (Ss == 0) {
	if (Se != 0)		/* DC and AC together not OK */
	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
	  } else {
	if (ncomps != 1)	/* AC scans must be for only one component */
	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
	  }
	  for (ci = 0; ci < ncomps; ci++) {
	last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
	if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
	for (coefi = Ss; coefi <= Se; coefi++) {
	  if (last_bitpos_ptr[coefi] < 0) {
		if (Ah != 0)
		  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
	  } else {
		if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
		  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
	  }
	  last_bitpos_ptr[coefi] = Al;
	}
	  }
#endif
	} else {
	  if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
	  for (ci = 0; ci < ncomps; ci++) {
	thisi = scanptr->component_index[ci];
	if (component_sent[thisi])
	  ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
	component_sent[thisi] = TRUE;
	  }
	}
  }

  if (cinfo->progressive_mode) {
#ifdef C_PROGRESSIVE_SUPPORTED
	for (ci = 0; ci < cinfo->num_components; ci++) {
	  if (last_bitpos[ci][0] < 0)
	ERREXIT(cinfo, JERR_MISSING_DATA);
	}
#endif
  } else {
	for (ci = 0; ci < cinfo->num_components; ci++) {
	  if (! component_sent[ci])
	ERREXIT(cinfo, JERR_MISSING_DATA);
	}
  }
}

#endif /* C_MULTISCAN_FILES_SUPPORTED */

LOCAL(void)
select_scan_parameters (j_compress_ptr cinfo)
{
  int ci;

#ifdef C_MULTISCAN_FILES_SUPPORTED
  if (cinfo->scan_info != NULL) {
	my_master_ptr master = (my_master_ptr) cinfo->master;
	const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;

	cinfo->comps_in_scan = scanptr->comps_in_scan;
	for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
	  cinfo->cur_comp_info[ci] =
	&cinfo->comp_info[scanptr->component_index[ci]];
	}
	cinfo->Ss = scanptr->Ss;
	cinfo->Se = scanptr->Se;
	cinfo->Ah = scanptr->Ah;
	cinfo->Al = scanptr->Al;
  }
  else
#endif
  {
	if (cinfo->num_components > MAX_COMPS_IN_SCAN)
	  ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
		   MAX_COMPS_IN_SCAN);
	cinfo->comps_in_scan = cinfo->num_components;
	for (ci = 0; ci < cinfo->num_components; ci++) {
	  cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
	}
	cinfo->Ss = 0;
	cinfo->Se = DCTSIZE2-1;
	cinfo->Ah = 0;
	cinfo->Al = 0;
  }
}

LOCAL(void)
per_scan_setup (j_compress_ptr cinfo)
{
  int ci, mcublks, tmp;
  jpeg_component_info *compptr;

  if (cinfo->comps_in_scan == 1) {

	compptr = cinfo->cur_comp_info[0];

	cinfo->MCUs_per_row = compptr->width_in_blocks;
	cinfo->MCU_rows_in_scan = compptr->height_in_blocks;

	compptr->MCU_width = 1;
	compptr->MCU_height = 1;
	compptr->MCU_blocks = 1;
	compptr->MCU_sample_width = DCTSIZE;
	compptr->last_col_width = 1;
	tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
	if (tmp == 0) tmp = compptr->v_samp_factor;
	compptr->last_row_height = tmp;

	cinfo->blocks_in_MCU = 1;
	cinfo->MCU_membership[0] = 0;

  } else {

	if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
	  ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
		   MAX_COMPS_IN_SCAN);

	cinfo->MCUs_per_row = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width,
			(long) (cinfo->max_h_samp_factor*DCTSIZE));
	cinfo->MCU_rows_in_scan = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height,
			(long) (cinfo->max_v_samp_factor*DCTSIZE));

	cinfo->blocks_in_MCU = 0;

	for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	  compptr = cinfo->cur_comp_info[ci];
	  compptr->MCU_width = compptr->h_samp_factor;
	  compptr->MCU_height = compptr->v_samp_factor;
	  compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
	  compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
	  tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
	  if (tmp == 0) tmp = compptr->MCU_width;
	  compptr->last_col_width = tmp;
	  tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
	  if (tmp == 0) tmp = compptr->MCU_height;
	  compptr->last_row_height = tmp;
	  mcublks = compptr->MCU_blocks;
	  if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
	  while (mcublks-- > 0) {
	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
	  }
	}

  }

  if (cinfo->restart_in_rows > 0) {
	long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
	cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  }
}

METHODDEF(void)
prepare_for_pass (j_compress_ptr cinfo)
{
  my_master_ptr master = (my_master_ptr) cinfo->master;

  switch (master->pass_type) {
  case main_pass:
	select_scan_parameters(cinfo);
	per_scan_setup(cinfo);
	if (! cinfo->raw_data_in) {
	  (*cinfo->cconvert->start_pass) (cinfo);
	  (*cinfo->downsample->start_pass) (cinfo);
	  (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
	}
	(*cinfo->fdct->start_pass) (cinfo);
	(*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
	(*cinfo->coef->start_pass) (cinfo,
				(master->total_passes > 1 ?
				 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
	(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
	if (cinfo->optimize_coding) {
	  master->pub.call_pass_startup = FALSE;
	} else {
	  master->pub.call_pass_startup = TRUE;
	}
	break;
#ifdef ENTROPY_OPT_SUPPORTED
  case huff_opt_pass:
	select_scan_parameters(cinfo);
	per_scan_setup(cinfo);
	if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
	  (*cinfo->entropy->start_pass) (cinfo, TRUE);
	  (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
	  master->pub.call_pass_startup = FALSE;
	  break;
	}
	master->pass_type = output_pass;
	master->pass_number++;
#endif
  case output_pass:
	if (! cinfo->optimize_coding) {
	  select_scan_parameters(cinfo);
	  per_scan_setup(cinfo);
	}
	(*cinfo->entropy->start_pass) (cinfo, FALSE);
	(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
	if (master->scan_number == 0)
	  (*cinfo->marker->write_frame_header) (cinfo);
	(*cinfo->marker->write_scan_header) (cinfo);
	master->pub.call_pass_startup = FALSE;
	break;
  default:
	ERREXIT(cinfo, JERR_NOT_COMPILED);
  }

  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);

  if (cinfo->progress != NULL) {
	cinfo->progress->completed_passes = master->pass_number;
	cinfo->progress->total_passes = master->total_passes;
  }
}

METHODDEF(void)
pass_startup (j_compress_ptr cinfo)
{
  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */

  (*cinfo->marker->write_frame_header) (cinfo);
  (*cinfo->marker->write_scan_header) (cinfo);
}

METHODDEF(void)
finish_pass_master (j_compress_ptr cinfo)
{
  my_master_ptr master = (my_master_ptr) cinfo->master;

  (*cinfo->entropy->finish_pass) (cinfo);

  switch (master->pass_type) {
  case main_pass:
	master->pass_type = output_pass;
	if (! cinfo->optimize_coding)
	  master->scan_number++;
	break;
  case huff_opt_pass:
	master->pass_type = output_pass;
	break;
  case output_pass:
	if (cinfo->optimize_coding)
	  master->pass_type = huff_opt_pass;
	master->scan_number++;
	break;
  }

  master->pass_number++;
}

GLOBAL(void)
jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
{
  my_master_ptr master;

  master = (my_master_ptr)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  SIZEOF(my_comp_master));
  cinfo->master = (struct jpeg_comp_master *) master;
  master->pub.prepare_for_pass = prepare_for_pass;
  master->pub.pass_startup = pass_startup;
  master->pub.finish_pass = finish_pass_master;
  master->pub.is_last_pass = FALSE;

  initial_setup(cinfo);

  if (cinfo->scan_info != NULL) {
#ifdef C_MULTISCAN_FILES_SUPPORTED
	validate_script(cinfo);
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
  } else {
	cinfo->progressive_mode = FALSE;
	cinfo->num_scans = 1;
  }

  if (cinfo->progressive_mode)	/*  TEMPORARY HACK ??? */
	cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */

  if (transcode_only) {
	if (cinfo->optimize_coding)
	  master->pass_type = huff_opt_pass;
	else
	  master->pass_type = output_pass;
  } else {
	master->pass_type = main_pass;
  }
  master->scan_number = 0;
  master->pass_number = 0;
  if (cinfo->optimize_coding)
	master->total_passes = cinfo->num_scans * 2;
  else
	master->total_passes = cinfo->num_scans;
}
/*** End of inlined file: jcmaster.c ***/


/*** Start of inlined file: jcomapi.c ***/
#define JPEG_INTERNALS

GLOBAL(void)
jpeg_abort (j_common_ptr cinfo)
{
  int pool;

  if (cinfo->mem == NULL)
	return;

  for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
	(*cinfo->mem->free_pool) (cinfo, pool);
  }

  if (cinfo->is_decompressor) {
	cinfo->global_state = DSTATE_START;
	((j_decompress_ptr) cinfo)->marker_list = NULL;
  } else {
	cinfo->global_state = CSTATE_START;
  }
}

GLOBAL(void)
jpeg_destroy (j_common_ptr cinfo)
{
  if (cinfo->mem != NULL)
	(*cinfo->mem->self_destruct) (cinfo);
  cinfo->mem = NULL;		/* be safe if jpeg_destroy is called twice */
  cinfo->global_state = 0;	/* mark it destroyed */
}

GLOBAL(JQUANT_TBL *)
jpeg_alloc_quant_table (j_common_ptr cinfo)
{
  JQUANT_TBL *tbl;

  tbl = (JQUANT_TBL *)
	(*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
  return tbl;
}

GLOBAL(JHUFF_TBL *)
jpeg_alloc_huff_table (j_common_ptr cinfo)
{
  JHUFF_TBL *tbl;

  tbl = (JHUFF_TBL *)
	(*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
  return tbl;
}
/*** End of inlined file: jcomapi.c ***/


/*** Start of inlined file: jcparam.c ***/
#define JPEG_INTERNALS

GLOBAL(void)
jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
			  const unsigned int *basic_table,
			  int scale_factor, boolean force_baseline)
{
  JQUANT_TBL ** qtblptr;
  int i;
  long temp;

  if (cinfo->global_state != CSTATE_START)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
	ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);

  qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];

  if (*qtblptr == NULL)
	*qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);

  for (i = 0; i < DCTSIZE2; i++) {
	temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
	if (temp <= 0L) temp = 1L;
	if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
	if (force_baseline && temp > 255L)
	  temp = 255L;		/* limit to baseline range if requested */
	(*qtblptr)->quantval[i] = (UINT16) temp;
  }

  (*qtblptr)->sent_table = FALSE;
}

GLOBAL(void)
jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
			 boolean force_baseline)
{
  static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
	16,  11,  10,  16,  24,  40,  51,  61,
	12,  12,  14,  19,  26,  58,  60,  55,
	14,  13,  16,  24,  40,  57,  69,  56,
	14,  17,  22,  29,  51,  87,  80,  62,
	18,  22,  37,  56,  68, 109, 103,  77,
	24,  35,  55,  64,  81, 104, 113,  92,
	49,  64,  78,  87, 103, 121, 120, 101,
	72,  92,  95,  98, 112, 100, 103,  99
  };
  static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
	17,  18,  24,  47,  99,  99,  99,  99,
	18,  21,  26,  66,  99,  99,  99,  99,
	24,  26,  56,  99,  99,  99,  99,  99,
	47,  66,  99,  99,  99,  99,  99,  99,
	99,  99,  99,  99,  99,  99,  99,  99,
	99,  99,  99,  99,  99,  99,  99,  99,
	99,  99,  99,  99,  99,  99,  99,  99,
	99,  99,  99,  99,  99,  99,  99,  99
  };

  jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
			   scale_factor, force_baseline);
  jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
			   scale_factor, force_baseline);
}

GLOBAL(int)
jpeg_quality_scaling (int quality)
{
  if (quality <= 0) quality = 1;
  if (quality > 100) quality = 100;

  if (quality < 50)
	quality = 5000 / quality;
  else
	quality = 200 - quality*2;

  return quality;
}

GLOBAL(void)
jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
{
  quality = jpeg_quality_scaling(quality);

  jpeg_set_linear_quality(cinfo, quality, force_baseline);
}

LOCAL(void)
add_huff_table (j_compress_ptr cinfo,
		JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
{
  int nsymbols, len;

  if (*htblptr == NULL)
	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);

  MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));

  nsymbols = 0;
  for (len = 1; len <= 16; len++)
	nsymbols += bits[len];
  if (nsymbols < 1 || nsymbols > 256)
	ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);

  MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));

  (*htblptr)->sent_table = FALSE;
}

LOCAL(void)
std_huff_tables (j_compress_ptr cinfo)
{
  static const UINT8 bits_dc_luminance[17] =
	{ /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  static const UINT8 val_dc_luminance[] =
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

  static const UINT8 bits_dc_chrominance[17] =
	{ /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  static const UINT8 val_dc_chrominance[] =
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

  static const UINT8 bits_ac_luminance[17] =
	{ /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  static const UINT8 val_ac_luminance[] =
	{ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
	  0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
	  0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
	  0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
	  0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
	  0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
	  0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
	  0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
	  0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
	  0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
	  0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
	  0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
	  0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
	  0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
	  0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
	  0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
	  0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
	  0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
	  0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
	  0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
	  0xf9, 0xfa };

  static const UINT8 bits_ac_chrominance[17] =
	{ /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  static const UINT8 val_ac_chrominance[] =
	{ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
	  0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
	  0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
	  0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
	  0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
	  0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
	  0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
	  0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
	  0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
	  0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
	  0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
	  0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
	  0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
	  0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
	  0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
	  0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
	  0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
	  0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
	  0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
	  0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
	  0xf9, 0xfa };

  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
		 bits_dc_luminance, val_dc_luminance);
  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
		 bits_ac_luminance, val_ac_luminance);
  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
		 bits_dc_chrominance, val_dc_chrominance);
  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
		 bits_ac_chrominance, val_ac_chrominance);
}

GLOBAL(void)
jpeg_set_defaults (j_compress_ptr cinfo)
{
  int i;

  if (cinfo->global_state != CSTATE_START)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  if (cinfo->comp_info == NULL)
	cinfo->comp_info = (jpeg_component_info *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));

  cinfo->data_precision = BITS_IN_JSAMPLE;
  jpeg_set_quality(cinfo, 75, TRUE);
  std_huff_tables(cinfo);

  for (i = 0; i < NUM_ARITH_TBLS; i++) {
	cinfo->arith_dc_L[i] = 0;
	cinfo->arith_dc_U[i] = 1;
	cinfo->arith_ac_K[i] = 5;
  }

  cinfo->scan_info = NULL;
  cinfo->num_scans = 0;

  cinfo->raw_data_in = FALSE;

  cinfo->arith_code = FALSE;

  cinfo->optimize_coding = FALSE;
  if (cinfo->data_precision > 8)
	cinfo->optimize_coding = TRUE;

  cinfo->CCIR601_sampling = FALSE;

  cinfo->smoothing_factor = 0;

  cinfo->dct_method = JDCT_DEFAULT;

  cinfo->restart_interval = 0;
  cinfo->restart_in_rows = 0;

  cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
  cinfo->JFIF_minor_version = 1;
  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
  cinfo->Y_density = 1;

  jpeg_default_colorspace(cinfo);
}

GLOBAL(void)
jpeg_default_colorspace (j_compress_ptr cinfo)
{
  switch (cinfo->in_color_space) {
  case JCS_GRAYSCALE:
	jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
	break;
  case JCS_RGB:
	jpeg_set_colorspace(cinfo, JCS_YCbCr);
	break;
  case JCS_YCbCr:
	jpeg_set_colorspace(cinfo, JCS_YCbCr);
	break;
  case JCS_CMYK:
	jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
	break;
  case JCS_YCCK:
	jpeg_set_colorspace(cinfo, JCS_YCCK);
	break;
  case JCS_UNKNOWN:
	jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
	break;
  default:
	ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  }
}

GLOBAL(void)
jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
{
  jpeg_component_info * compptr;
  int ci;

#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
  (compptr = &cinfo->comp_info[index], \
   compptr->component_id = (id), \
   compptr->h_samp_factor = (hsamp), \
   compptr->v_samp_factor = (vsamp), \
   compptr->quant_tbl_no = (quant), \
   compptr->dc_tbl_no = (dctbl), \
   compptr->ac_tbl_no = (actbl) )

  if (cinfo->global_state != CSTATE_START)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  cinfo->jpeg_color_space = colorspace;

  cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
  cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */

  switch (colorspace) {
  case JCS_GRAYSCALE:
	cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
	cinfo->num_components = 1;
	SET_COMP(0, 1, 1,1, 0, 0,0);
	break;
  case JCS_RGB:
	cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
	cinfo->num_components = 3;
	SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
	SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
	SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
	break;
  case JCS_YCbCr:
	cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
	cinfo->num_components = 3;
	SET_COMP(0, 1, 2,2, 0, 0,0);
	SET_COMP(1, 2, 1,1, 1, 1,1);
	SET_COMP(2, 3, 1,1, 1, 1,1);
	break;
  case JCS_CMYK:
	cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
	cinfo->num_components = 4;
	SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
	SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
	SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
	SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
	break;
  case JCS_YCCK:
	cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
	cinfo->num_components = 4;
	SET_COMP(0, 1, 2,2, 0, 0,0);
	SET_COMP(1, 2, 1,1, 1, 1,1);
	SET_COMP(2, 3, 1,1, 1, 1,1);
	SET_COMP(3, 4, 2,2, 0, 0,0);
	break;
  case JCS_UNKNOWN:
	cinfo->num_components = cinfo->input_components;
	if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
	  ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
		   MAX_COMPONENTS);
	for (ci = 0; ci < cinfo->num_components; ci++) {
	  SET_COMP(ci, ci, 1,1, 0, 0,0);
	}
	break;
  default:
	ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  }
}

#ifdef C_PROGRESSIVE_SUPPORTED

LOCAL(jpeg_scan_info *)
fill_a_scan (jpeg_scan_info * scanptr, int ci,
		 int Ss, int Se, int Ah, int Al)
{
  scanptr->comps_in_scan = 1;
  scanptr->component_index[0] = ci;
  scanptr->Ss = Ss;
  scanptr->Se = Se;
  scanptr->Ah = Ah;
  scanptr->Al = Al;
  scanptr++;
  return scanptr;
}

LOCAL(jpeg_scan_info *)
fill_scans (jpeg_scan_info * scanptr, int ncomps,
		int Ss, int Se, int Ah, int Al)
{
  int ci;

  for (ci = 0; ci < ncomps; ci++) {
	scanptr->comps_in_scan = 1;
	scanptr->component_index[0] = ci;
	scanptr->Ss = Ss;
	scanptr->Se = Se;
	scanptr->Ah = Ah;
	scanptr->Al = Al;
	scanptr++;
  }
  return scanptr;
}

LOCAL(jpeg_scan_info *)
fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
{
  int ci;

  if (ncomps <= MAX_COMPS_IN_SCAN) {
	scanptr->comps_in_scan = ncomps;
	for (ci = 0; ci < ncomps; ci++)
	  scanptr->component_index[ci] = ci;
	scanptr->Ss = scanptr->Se = 0;
	scanptr->Ah = Ah;
	scanptr->Al = Al;
	scanptr++;
  } else {
	scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
  }
  return scanptr;
}

GLOBAL(void)
jpeg_simple_progression (j_compress_ptr cinfo)
{
  int ncomps = cinfo->num_components;
  int nscans;
  jpeg_scan_info * scanptr;

  if (cinfo->global_state != CSTATE_START)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
	nscans = 10;
  } else {
	if (ncomps > MAX_COMPS_IN_SCAN)
	  nscans = 6 * ncomps;	/* 2 DC + 4 AC scans per component */
	else
	  nscans = 2 + 4 * ncomps;	/* 2 DC scans; 4 AC scans per component */
  }

  if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
	cinfo->script_space_size = MAX(nscans, 10);
	cinfo->script_space = (jpeg_scan_info *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
			cinfo->script_space_size * SIZEOF(jpeg_scan_info));
  }
  scanptr = cinfo->script_space;
  cinfo->scan_info = scanptr;
  cinfo->num_scans = nscans;

  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
	scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
	scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
	scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
	scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
	scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
	scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
	scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
	scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
	scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
	scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
  } else {
	scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
	scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
	scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
	scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
	scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
	scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
  }
}

#endif /* C_PROGRESSIVE_SUPPORTED */
/*** End of inlined file: jcparam.c ***/


/*** Start of inlined file: jcphuff.c ***/
#define JPEG_INTERNALS

#ifdef C_PROGRESSIVE_SUPPORTED

typedef struct {
  struct jpeg_entropy_encoder pub; /* public fields */

  boolean gather_statistics;

  JOCTET * next_output_byte;	/* => next byte to write in buffer */
  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
  INT32 put_buffer;		/* current bit-accumulation buffer */
  int put_bits;			/* # of bits now in it */
  j_compress_ptr cinfo;		/* link to cinfo (needed for dump_buffer) */

  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */

  int ac_tbl_no;		/* the table number of the single component */
  unsigned int EOBRUN;		/* run length of EOBs */
  unsigned int BE;		/* # of buffered correction bits before MCU */
  char * bit_buffer;		/* buffer for correction bits (1 per char) */

  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
  int next_restart_num;		/* next restart number to write (0-7) */

  c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];

  long * count_ptrs[NUM_HUFF_TBLS];
} phuff_entropy_encoder;

typedef phuff_entropy_encoder * phuff_entropy_ptr;

#define MAX_CORR_BITS  1000	/* Max # of correction bits I can buffer */

#ifdef RIGHT_SHIFT_IS_UNSIGNED
#define ISHIFT_TEMPS	int ishift_temp;
#define IRIGHT_SHIFT(x,shft)  \
	((ishift_temp = (x)) < 0 ? \
	 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
	 (ishift_temp >> (shft)))
#else
#define ISHIFT_TEMPS
#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
#endif

METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
						JBLOCKROW *MCU_data));
METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
						JBLOCKROW *MCU_data));
METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
						 JBLOCKROW *MCU_data));
METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
						 JBLOCKROW *MCU_data));
METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));

METHODDEF(void)
start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  boolean is_DC_band;
  int ci, tbl;
  jpeg_component_info * compptr;

  entropy->cinfo = cinfo;
  entropy->gather_statistics = gather_statistics;

  is_DC_band = (cinfo->Ss == 0);

  if (cinfo->Ah == 0) {
	if (is_DC_band)
	  entropy->pub.encode_mcu = encode_mcu_DC_first;
	else
	  entropy->pub.encode_mcu = encode_mcu_AC_first;
  } else {
	if (is_DC_band)
	  entropy->pub.encode_mcu = encode_mcu_DC_refine;
	else {
	  entropy->pub.encode_mcu = encode_mcu_AC_refine;
	  if (entropy->bit_buffer == NULL)
	entropy->bit_buffer = (char *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					  MAX_CORR_BITS * SIZEOF(char));
	}
  }
  if (gather_statistics)
	entropy->pub.finish_pass = finish_pass_gather_phuff;
  else
	entropy->pub.finish_pass = finish_pass_phuff;

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	entropy->last_dc_val[ci] = 0;
	if (is_DC_band) {
	  if (cinfo->Ah != 0)	/* DC refinement needs no table */
	continue;
	  tbl = compptr->dc_tbl_no;
	} else {
	  entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
	}
	if (gather_statistics) {
	  if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
		ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
	  if (entropy->count_ptrs[tbl] == NULL)
	entropy->count_ptrs[tbl] = (long *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					  257 * SIZEOF(long));
	  MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
	} else {
	  jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
				  & entropy->derived_tbls[tbl]);
	}
  }

  entropy->EOBRUN = 0;
  entropy->BE = 0;

  entropy->put_buffer = 0;
  entropy->put_bits = 0;

  entropy->restarts_to_go = cinfo->restart_interval;
  entropy->next_restart_num = 0;
}

#define emit_byte(entropy,val)  \
	{ *(entropy)->next_output_byte++ = (JOCTET) (val);  \
	  if (--(entropy)->free_in_buffer == 0)  \
		dump_buffer_p(entropy); }

LOCAL(void)
dump_buffer_p (phuff_entropy_ptr entropy)
{
  struct jpeg_destination_mgr * dest = entropy->cinfo->dest;

  if (! (*dest->empty_output_buffer) (entropy->cinfo))
	ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
  entropy->next_output_byte = dest->next_output_byte;
  entropy->free_in_buffer = dest->free_in_buffer;
}

INLINE
LOCAL(void)
emit_bits_p (phuff_entropy_ptr entropy, unsigned int code, int size)
{
  register INT32 put_buffer = (INT32) code;
  register int put_bits = entropy->put_bits;

  if (size == 0)
	ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);

  if (entropy->gather_statistics)
	return;			/* do nothing if we're only getting stats */

  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */

  put_bits += size;		/* new number of bits in buffer */

  put_buffer <<= 24 - put_bits; /* align incoming bits */

  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */

  while (put_bits >= 8) {
	int c = (int) ((put_buffer >> 16) & 0xFF);

	emit_byte(entropy, c);
	if (c == 0xFF) {		/* need to stuff a zero byte? */
	  emit_byte(entropy, 0);
	}
	put_buffer <<= 8;
	put_bits -= 8;
  }

  entropy->put_buffer = put_buffer; /* update variables */
  entropy->put_bits = put_bits;
}

LOCAL(void)
flush_bits_p (phuff_entropy_ptr entropy)
{
  emit_bits_p(entropy, 0x7F, 7); /* fill any partial byte with ones */
  entropy->put_buffer = 0;	 /* and reset bit-buffer to empty */
  entropy->put_bits = 0;
}

INLINE
LOCAL(void)
emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
{
  if (entropy->gather_statistics)
	entropy->count_ptrs[tbl_no][symbol]++;
  else {
	c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
	emit_bits_p(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
  }
}

LOCAL(void)
emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
			unsigned int nbits)
{
  if (entropy->gather_statistics)
	return;			/* no real work */

  while (nbits > 0) {
	emit_bits_p(entropy, (unsigned int) (*bufstart), 1);
	bufstart++;
	nbits--;
  }
}

LOCAL(void)
emit_eobrun (phuff_entropy_ptr entropy)
{
  register int temp, nbits;

  if (entropy->EOBRUN > 0) {	/* if there is any pending EOBRUN */
	temp = entropy->EOBRUN;
	nbits = 0;
	while ((temp >>= 1))
	  nbits++;
	if (nbits > 14)
	  ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);

	emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
	if (nbits)
	  emit_bits_p(entropy, entropy->EOBRUN, nbits);

	entropy->EOBRUN = 0;

	emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
	entropy->BE = 0;
  }
}

LOCAL(void)
emit_restart_p (phuff_entropy_ptr entropy, int restart_num)
{
  int ci;

  emit_eobrun(entropy);

  if (! entropy->gather_statistics) {
	flush_bits_p(entropy);
	emit_byte(entropy, 0xFF);
	emit_byte(entropy, JPEG_RST0 + restart_num);
  }

  if (entropy->cinfo->Ss == 0) {
	for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
	  entropy->last_dc_val[ci] = 0;
  } else {
	entropy->EOBRUN = 0;
	entropy->BE = 0;
  }
}

METHODDEF(boolean)
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  register int temp, temp2;
  register int nbits;
  int blkn, ci;
  int Al = cinfo->Al;
  JBLOCKROW block;
  jpeg_component_info * compptr;
  ISHIFT_TEMPS

  entropy->next_output_byte = cinfo->dest->next_output_byte;
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;

  if (cinfo->restart_interval)
	if (entropy->restarts_to_go == 0)
	  emit_restart_p(entropy, entropy->next_restart_num);

  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
	block = MCU_data[blkn];
	ci = cinfo->MCU_membership[blkn];
	compptr = cinfo->cur_comp_info[ci];

	temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);

	temp = temp2 - entropy->last_dc_val[ci];
	entropy->last_dc_val[ci] = temp2;

	temp2 = temp;
	if (temp < 0) {
	  temp = -temp;		/* temp is abs value of input */
	  temp2--;
	}

	nbits = 0;
	while (temp) {
	  nbits++;
	  temp >>= 1;
	}
	if (nbits > MAX_COEF_BITS+1)
	  ERREXIT(cinfo, JERR_BAD_DCT_COEF);

	emit_symbol(entropy, compptr->dc_tbl_no, nbits);

	if (nbits)			/* emit_bits rejects calls with size 0 */
	  emit_bits_p(entropy, (unsigned int) temp2, nbits);
  }

  cinfo->dest->next_output_byte = entropy->next_output_byte;
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0) {
	  entropy->restarts_to_go = cinfo->restart_interval;
	  entropy->next_restart_num++;
	  entropy->next_restart_num &= 7;
	}
	entropy->restarts_to_go--;
  }

  return TRUE;
}

METHODDEF(boolean)
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  register int temp, temp2;
  register int nbits;
  register int r, k;
  int Se = cinfo->Se;
  int Al = cinfo->Al;
  JBLOCKROW block;

  entropy->next_output_byte = cinfo->dest->next_output_byte;
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;

  if (cinfo->restart_interval)
	if (entropy->restarts_to_go == 0)
	  emit_restart_p(entropy, entropy->next_restart_num);

  block = MCU_data[0];

  r = 0;			/* r = run length of zeros */

  for (k = cinfo->Ss; k <= Se; k++) {
	if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
	  r++;
	  continue;
	}
	if (temp < 0) {
	  temp = -temp;		/* temp is abs value of input */
	  temp >>= Al;		/* apply the point transform */
	  temp2 = ~temp;
	} else {
	  temp >>= Al;		/* apply the point transform */
	  temp2 = temp;
	}
	if (temp == 0) {
	  r++;
	  continue;
	}

	if (entropy->EOBRUN > 0)
	  emit_eobrun(entropy);
	while (r > 15) {
	  emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
	  r -= 16;
	}

	nbits = 1;			/* there must be at least one 1 bit */
	while ((temp >>= 1))
	  nbits++;
	if (nbits > MAX_COEF_BITS)
	  ERREXIT(cinfo, JERR_BAD_DCT_COEF);

	emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);

	emit_bits_p(entropy, (unsigned int) temp2, nbits);

	r = 0;			/* reset zero run length */
  }

  if (r > 0) {			/* If there are trailing zeroes, */
	entropy->EOBRUN++;		/* count an EOB */
	if (entropy->EOBRUN == 0x7FFF)
	  emit_eobrun(entropy);	/* force it out to avoid overflow */
  }

  cinfo->dest->next_output_byte = entropy->next_output_byte;
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0) {
	  entropy->restarts_to_go = cinfo->restart_interval;
	  entropy->next_restart_num++;
	  entropy->next_restart_num &= 7;
	}
	entropy->restarts_to_go--;
  }

  return TRUE;
}

METHODDEF(boolean)
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  register int temp;
  int blkn;
  int Al = cinfo->Al;
  JBLOCKROW block;

  entropy->next_output_byte = cinfo->dest->next_output_byte;
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;

  if (cinfo->restart_interval)
	if (entropy->restarts_to_go == 0)
	  emit_restart_p(entropy, entropy->next_restart_num);

  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
	block = MCU_data[blkn];

	temp = (*block)[0];
	emit_bits_p(entropy, (unsigned int) (temp >> Al), 1);
  }

  cinfo->dest->next_output_byte = entropy->next_output_byte;
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0) {
	  entropy->restarts_to_go = cinfo->restart_interval;
	  entropy->next_restart_num++;
	  entropy->next_restart_num &= 7;
	}
	entropy->restarts_to_go--;
  }

  return TRUE;
}

METHODDEF(boolean)
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  register int temp;
  register int r, k;
  int EOB;
  char *BR_buffer;
  unsigned int BR;
  int Se = cinfo->Se;
  int Al = cinfo->Al;
  JBLOCKROW block;
  int absvalues[DCTSIZE2];

  entropy->next_output_byte = cinfo->dest->next_output_byte;
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;

  if (cinfo->restart_interval)
	if (entropy->restarts_to_go == 0)
	  emit_restart_p(entropy, entropy->next_restart_num);

  block = MCU_data[0];

  EOB = 0;
  for (k = cinfo->Ss; k <= Se; k++) {
	temp = (*block)[jpeg_natural_order[k]];
	if (temp < 0)
	  temp = -temp;		/* temp is abs value of input */
	temp >>= Al;		/* apply the point transform */
	absvalues[k] = temp;	/* save abs value for main pass */
	if (temp == 1)
	  EOB = k;			/* EOB = index of last newly-nonzero coef */
  }

  r = 0;			/* r = run length of zeros */
  BR = 0;			/* BR = count of buffered bits added now */
  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */

  for (k = cinfo->Ss; k <= Se; k++) {
	if ((temp = absvalues[k]) == 0) {
	  r++;
	  continue;
	}

	while (r > 15 && k <= EOB) {
	  emit_eobrun(entropy);
	  emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
	  r -= 16;
	  emit_buffered_bits(entropy, BR_buffer, BR);
	  BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
	  BR = 0;
	}

	if (temp > 1) {
	  BR_buffer[BR++] = (char) (temp & 1);
	  continue;
	}

	emit_eobrun(entropy);

	emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);

	temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
	emit_bits_p(entropy, (unsigned int) temp, 1);

	emit_buffered_bits(entropy, BR_buffer, BR);
	BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
	BR = 0;
	r = 0;			/* reset zero run length */
  }

  if (r > 0 || BR > 0) {	/* If there are trailing zeroes, */
	entropy->EOBRUN++;		/* count an EOB */
	entropy->BE += BR;		/* concat my correction bits to older ones */
	if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
	  emit_eobrun(entropy);
  }

  cinfo->dest->next_output_byte = entropy->next_output_byte;
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0) {
	  entropy->restarts_to_go = cinfo->restart_interval;
	  entropy->next_restart_num++;
	  entropy->next_restart_num &= 7;
	}
	entropy->restarts_to_go--;
  }

  return TRUE;
}

METHODDEF(void)
finish_pass_phuff (j_compress_ptr cinfo)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;

  entropy->next_output_byte = cinfo->dest->next_output_byte;
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;

  emit_eobrun(entropy);
  flush_bits_p(entropy);

  cinfo->dest->next_output_byte = entropy->next_output_byte;
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
}

METHODDEF(void)
finish_pass_gather_phuff (j_compress_ptr cinfo)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  boolean is_DC_band;
  int ci, tbl;
  jpeg_component_info * compptr;
  JHUFF_TBL **htblptr;
  boolean did[NUM_HUFF_TBLS];

  emit_eobrun(entropy);

  is_DC_band = (cinfo->Ss == 0);

  MEMZERO(did, SIZEOF(did));

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	if (is_DC_band) {
	  if (cinfo->Ah != 0)	/* DC refinement needs no table */
	continue;
	  tbl = compptr->dc_tbl_no;
	} else {
	  tbl = compptr->ac_tbl_no;
	}
	if (! did[tbl]) {
	  if (is_DC_band)
		htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
	  else
		htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
	  if (*htblptr == NULL)
		*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
	  jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
	  did[tbl] = TRUE;
	}
  }
}

GLOBAL(void)
jinit_phuff_encoder (j_compress_ptr cinfo)
{
  phuff_entropy_ptr entropy;
  int i;

  entropy = (phuff_entropy_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(phuff_entropy_encoder));
  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  entropy->pub.start_pass = start_pass_phuff;

  for (i = 0; i < NUM_HUFF_TBLS; i++) {
	entropy->derived_tbls[i] = NULL;
	entropy->count_ptrs[i] = NULL;
  }
  entropy->bit_buffer = NULL;	/* needed only in AC refinement scan */
}

#endif /* C_PROGRESSIVE_SUPPORTED */
/*** End of inlined file: jcphuff.c ***/


/*** Start of inlined file: jcprepct.c ***/
#define JPEG_INTERNALS

#ifdef INPUT_SMOOTHING_SUPPORTED
#define CONTEXT_ROWS_SUPPORTED
#endif

typedef struct {
  struct jpeg_c_prep_controller pub; /* public fields */

  JSAMPARRAY color_buf[MAX_COMPONENTS];

  JDIMENSION rows_to_go;	/* counts rows remaining in source image */
  int next_buf_row;		/* index of next row to store in color_buf */

#ifdef CONTEXT_ROWS_SUPPORTED	/* only needed for context case */
  int this_row_group;		/* starting row index of group to process */
  int next_buf_stop;		/* downsample when we reach this index */
#endif
} my_prep_controller;

typedef my_prep_controller * my_prep_ptr;

METHODDEF(void)
start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
{
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;

  if (pass_mode != JBUF_PASS_THRU)
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);

  prep->rows_to_go = cinfo->image_height;
  prep->next_buf_row = 0;
#ifdef CONTEXT_ROWS_SUPPORTED
  prep->this_row_group = 0;
  prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
#endif
}

LOCAL(void)
expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
			int input_rows, int output_rows)
{
  register int row;

  for (row = input_rows; row < output_rows; row++) {
	jcopy_sample_rows(image_data, input_rows-1, image_data, row,
			  1, num_cols);
  }
}

METHODDEF(void)
pre_process_data (j_compress_ptr cinfo,
		  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
		  JDIMENSION in_rows_avail,
		  JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
		  JDIMENSION out_row_groups_avail)
{
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  int numrows, ci;
  JDIMENSION inrows;
  jpeg_component_info * compptr;

  while (*in_row_ctr < in_rows_avail &&
	 *out_row_group_ctr < out_row_groups_avail) {
	inrows = in_rows_avail - *in_row_ctr;
	numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
	numrows = (int) MIN((JDIMENSION) numrows, inrows);
	(*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
					   prep->color_buf,
					   (JDIMENSION) prep->next_buf_row,
					   numrows);
	*in_row_ctr += numrows;
	prep->next_buf_row += numrows;
	prep->rows_to_go -= numrows;
	if (prep->rows_to_go == 0 &&
	prep->next_buf_row < cinfo->max_v_samp_factor) {
	  for (ci = 0; ci < cinfo->num_components; ci++) {
	expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
			   prep->next_buf_row, cinfo->max_v_samp_factor);
	  }
	  prep->next_buf_row = cinfo->max_v_samp_factor;
	}
	if (prep->next_buf_row == cinfo->max_v_samp_factor) {
	  (*cinfo->downsample->downsample) (cinfo,
					prep->color_buf, (JDIMENSION) 0,
					output_buf, *out_row_group_ctr);
	  prep->next_buf_row = 0;
	  (*out_row_group_ctr)++;
	}
	if (prep->rows_to_go == 0 &&
	*out_row_group_ctr < out_row_groups_avail) {
	  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	expand_bottom_edge(output_buf[ci],
			   compptr->width_in_blocks * DCTSIZE,
			   (int) (*out_row_group_ctr * compptr->v_samp_factor),
			   (int) (out_row_groups_avail * compptr->v_samp_factor));
	  }
	  *out_row_group_ctr = out_row_groups_avail;
	  break;			/* can exit outer loop without test */
	}
  }
}

#ifdef CONTEXT_ROWS_SUPPORTED

METHODDEF(void)
pre_process_context (j_compress_ptr cinfo,
			 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
			 JDIMENSION in_rows_avail,
			 JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
			 JDIMENSION out_row_groups_avail)
{
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  int numrows, ci;
  int buf_height = cinfo->max_v_samp_factor * 3;
  JDIMENSION inrows;

  while (*out_row_group_ctr < out_row_groups_avail) {
	if (*in_row_ctr < in_rows_avail) {
	  inrows = in_rows_avail - *in_row_ctr;
	  numrows = prep->next_buf_stop - prep->next_buf_row;
	  numrows = (int) MIN((JDIMENSION) numrows, inrows);
	  (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
					 prep->color_buf,
					 (JDIMENSION) prep->next_buf_row,
					 numrows);
	  if (prep->rows_to_go == cinfo->image_height) {
	for (ci = 0; ci < cinfo->num_components; ci++) {
	  int row;
	  for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
		jcopy_sample_rows(prep->color_buf[ci], 0,
				  prep->color_buf[ci], -row,
				  1, cinfo->image_width);
	  }
	}
	  }
	  *in_row_ctr += numrows;
	  prep->next_buf_row += numrows;
	  prep->rows_to_go -= numrows;
	} else {
	  if (prep->rows_to_go != 0)
	break;
	  if (prep->next_buf_row < prep->next_buf_stop) {
	for (ci = 0; ci < cinfo->num_components; ci++) {
	  expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
				 prep->next_buf_row, prep->next_buf_stop);
	}
	prep->next_buf_row = prep->next_buf_stop;
	  }
	}
	if (prep->next_buf_row == prep->next_buf_stop) {
	  (*cinfo->downsample->downsample) (cinfo,
					prep->color_buf,
					(JDIMENSION) prep->this_row_group,
					output_buf, *out_row_group_ctr);
	  (*out_row_group_ctr)++;
	  prep->this_row_group += cinfo->max_v_samp_factor;
	  if (prep->this_row_group >= buf_height)
	prep->this_row_group = 0;
	  if (prep->next_buf_row >= buf_height)
	prep->next_buf_row = 0;
	  prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
	}
  }
}

LOCAL(void)
create_context_buffer (j_compress_ptr cinfo)
{
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  int rgroup_height = cinfo->max_v_samp_factor;
  int ci, i;
  jpeg_component_info * compptr;
  JSAMPARRAY true_buffer, fake_buffer;

  fake_buffer = (JSAMPARRAY)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(cinfo->num_components * 5 * rgroup_height) *
				SIZEOF(JSAMPROW));

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	true_buffer = (*cinfo->mem->alloc_sarray)
	  ((j_common_ptr) cinfo, JPOOL_IMAGE,
	   (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
			  cinfo->max_h_samp_factor) / compptr->h_samp_factor),
	   (JDIMENSION) (3 * rgroup_height));
	MEMCOPY(fake_buffer + rgroup_height, true_buffer,
		3 * rgroup_height * SIZEOF(JSAMPROW));
	for (i = 0; i < rgroup_height; i++) {
	  fake_buffer[i] = true_buffer[2 * rgroup_height + i];
	  fake_buffer[4 * rgroup_height + i] = true_buffer[i];
	}
	prep->color_buf[ci] = fake_buffer + rgroup_height;
	fake_buffer += 5 * rgroup_height; /* point to space for next component */
  }
}

#endif /* CONTEXT_ROWS_SUPPORTED */

GLOBAL(void)
jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
{
  my_prep_ptr prep;
  int ci;
  jpeg_component_info * compptr;

  if (need_full_buffer)		/* safety check */
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);

  prep = (my_prep_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_prep_controller));
  cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  prep->pub.start_pass = start_pass_prep;

  if (cinfo->downsample->need_context_rows) {
#ifdef CONTEXT_ROWS_SUPPORTED
	prep->pub.pre_process_data = pre_process_context;
	create_context_buffer(cinfo);
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
  } else {
	prep->pub.pre_process_data = pre_process_data;
	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
	  prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE,
	 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
			cinfo->max_h_samp_factor) / compptr->h_samp_factor),
	 (JDIMENSION) cinfo->max_v_samp_factor);
	}
  }
}
/*** End of inlined file: jcprepct.c ***/


/*** Start of inlined file: jcsample.c ***/
#define JPEG_INTERNALS

typedef JMETHOD(void, downsample1_ptr,
		(j_compress_ptr cinfo, jpeg_component_info * compptr,
		 JSAMPARRAY input_data, JSAMPARRAY output_data));

typedef struct {
  struct jpeg_downsampler pub;	/* public fields */

  downsample1_ptr methods[MAX_COMPONENTS];
} my_downsampler;

typedef my_downsampler * my_downsample_ptr;

METHODDEF(void)
start_pass_downsample (j_compress_ptr cinfo)
{
}

LOCAL(void)
expand_right_edge (JSAMPARRAY image_data, int num_rows,
		   JDIMENSION input_cols, JDIMENSION output_cols)
{
  register JSAMPROW ptr;
  register JSAMPLE pixval;
  register int count;
  int row;
  int numcols = (int) (output_cols - input_cols);

  if (numcols > 0) {
	for (row = 0; row < num_rows; row++) {
	  ptr = image_data[row] + input_cols;
	  pixval = ptr[-1];		/* don't need GETJSAMPLE() here */
	  for (count = numcols; count > 0; count--)
	*ptr++ = pixval;
	}
  }
}

METHODDEF(void)
sep_downsample (j_compress_ptr cinfo,
		JSAMPIMAGE input_buf, JDIMENSION in_row_index,
		JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
{
  my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
  int ci;
  jpeg_component_info * compptr;
  JSAMPARRAY in_ptr, out_ptr;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	in_ptr = input_buf[ci] + in_row_index;
	out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
	(*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
  }
}

METHODDEF(void)
int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
		JSAMPARRAY input_data, JSAMPARRAY output_data)
{
  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
  JDIMENSION outcol, outcol_h;	/* outcol_h == outcol*h_expand */
  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  JSAMPROW inptr, outptr;
  INT32 outvalue;

  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  numpix = h_expand * v_expand;
  numpix2 = numpix/2;

  expand_right_edge(input_data, cinfo->max_v_samp_factor,
			cinfo->image_width, output_cols * h_expand);

  inrow = 0;
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
	outptr = output_data[outrow];
	for (outcol = 0, outcol_h = 0; outcol < output_cols;
	 outcol++, outcol_h += h_expand) {
	  outvalue = 0;
	  for (v = 0; v < v_expand; v++) {
	inptr = input_data[inrow+v] + outcol_h;
	for (h = 0; h < h_expand; h++) {
	  outvalue += (INT32) GETJSAMPLE(*inptr++);
	}
	  }
	  *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
	}
	inrow += v_expand;
  }
}

METHODDEF(void)
fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
			 JSAMPARRAY input_data, JSAMPARRAY output_data)
{
  jcopy_sample_rows(input_data, 0, output_data, 0,
			cinfo->max_v_samp_factor, cinfo->image_width);
  expand_right_edge(output_data, cinfo->max_v_samp_factor,
			cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
}

METHODDEF(void)
h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
		 JSAMPARRAY input_data, JSAMPARRAY output_data)
{
  int outrow;
  JDIMENSION outcol;
  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  register JSAMPROW inptr, outptr;
  register int bias;

  expand_right_edge(input_data, cinfo->max_v_samp_factor,
			cinfo->image_width, output_cols * 2);

  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
	outptr = output_data[outrow];
	inptr = input_data[outrow];
	bias = 0;			/* bias = 0,1,0,1,... for successive samples */
	for (outcol = 0; outcol < output_cols; outcol++) {
	  *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
				  + bias) >> 1);
	  bias ^= 1;		/* 0=>1, 1=>0 */
	  inptr += 2;
	}
  }
}

METHODDEF(void)
h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
		 JSAMPARRAY input_data, JSAMPARRAY output_data)
{
  int inrow, outrow;
  JDIMENSION outcol;
  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  register JSAMPROW inptr0, inptr1, outptr;
  register int bias;

  expand_right_edge(input_data, cinfo->max_v_samp_factor,
			cinfo->image_width, output_cols * 2);

  inrow = 0;
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
	outptr = output_data[outrow];
	inptr0 = input_data[inrow];
	inptr1 = input_data[inrow+1];
	bias = 1;			/* bias = 1,2,1,2,... for successive samples */
	for (outcol = 0; outcol < output_cols; outcol++) {
	  *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
				  GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
				  + bias) >> 2);
	  bias ^= 3;		/* 1=>2, 2=>1 */
	  inptr0 += 2; inptr1 += 2;
	}
	inrow += 2;
  }
}

#ifdef INPUT_SMOOTHING_SUPPORTED

METHODDEF(void)
h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
			JSAMPARRAY input_data, JSAMPARRAY output_data)
{
  int inrow, outrow;
  JDIMENSION colctr;
  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
  INT32 membersum, neighsum, memberscale, neighscale;

  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
			cinfo->image_width, output_cols * 2);

  memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
  neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */

  inrow = 0;
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
	outptr = output_data[outrow];
	inptr0 = input_data[inrow];
	inptr1 = input_data[inrow+1];
	above_ptr = input_data[inrow-1];
	below_ptr = input_data[inrow+2];

	membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
	neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
		   GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
		   GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
		   GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
	neighsum += neighsum;
	neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
		GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
	membersum = membersum * memberscale + neighsum * neighscale;
	*outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
	inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;

	for (colctr = output_cols - 2; colctr > 0; colctr--) {
	  membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
		  GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
	  neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
		 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
		 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
		 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
	  neighsum += neighsum;
	  neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
		  GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
	  membersum = membersum * memberscale + neighsum * neighscale;
	  *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
	  inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
	}

	membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
	neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
		   GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
		   GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
		   GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
	neighsum += neighsum;
	neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
		GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
	membersum = membersum * memberscale + neighsum * neighscale;
	*outptr = (JSAMPLE) ((membersum + 32768) >> 16);

	inrow += 2;
  }
}

METHODDEF(void)
fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
				JSAMPARRAY input_data, JSAMPARRAY output_data)
{
  int outrow;
  JDIMENSION colctr;
  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  register JSAMPROW inptr, above_ptr, below_ptr, outptr;
  INT32 membersum, neighsum, memberscale, neighscale;
  int colsum, lastcolsum, nextcolsum;

  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
			cinfo->image_width, output_cols);

  memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
  neighscale = cinfo->smoothing_factor * 64; /* scaled SF */

  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
	outptr = output_data[outrow];
	inptr = input_data[outrow];
	above_ptr = input_data[outrow-1];
	below_ptr = input_data[outrow+1];

	colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
		 GETJSAMPLE(*inptr);
	membersum = GETJSAMPLE(*inptr++);
	nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
		 GETJSAMPLE(*inptr);
	neighsum = colsum + (colsum - membersum) + nextcolsum;
	membersum = membersum * memberscale + neighsum * neighscale;
	*outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
	lastcolsum = colsum; colsum = nextcolsum;

	for (colctr = output_cols - 2; colctr > 0; colctr--) {
	  membersum = GETJSAMPLE(*inptr++);
	  above_ptr++; below_ptr++;
	  nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
		   GETJSAMPLE(*inptr);
	  neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
	  membersum = membersum * memberscale + neighsum * neighscale;
	  *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
	  lastcolsum = colsum; colsum = nextcolsum;
	}

	membersum = GETJSAMPLE(*inptr);
	neighsum = lastcolsum + (colsum - membersum) + colsum;
	membersum = membersum * memberscale + neighsum * neighscale;
	*outptr = (JSAMPLE) ((membersum + 32768) >> 16);

  }
}

#endif /* INPUT_SMOOTHING_SUPPORTED */

GLOBAL(void)
jinit_downsampler (j_compress_ptr cinfo)
{
  my_downsample_ptr downsample;
  int ci;
  jpeg_component_info * compptr;
  boolean smoothok = TRUE;

  downsample = (my_downsample_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_downsampler));
  cinfo->downsample = (struct jpeg_downsampler *) downsample;
  downsample->pub.start_pass = start_pass_downsample;
  downsample->pub.downsample = sep_downsample;
  downsample->pub.need_context_rows = FALSE;

  if (cinfo->CCIR601_sampling)
	ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
	compptr->v_samp_factor == cinfo->max_v_samp_factor) {
#ifdef INPUT_SMOOTHING_SUPPORTED
	  if (cinfo->smoothing_factor) {
	downsample->methods[ci] = fullsize_smooth_downsample;
	downsample->pub.need_context_rows = TRUE;
	  } else
#endif
	downsample->methods[ci] = fullsize_downsample;
	} else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
		   compptr->v_samp_factor == cinfo->max_v_samp_factor) {
	  smoothok = FALSE;
	  downsample->methods[ci] = h2v1_downsample;
	} else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
		   compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
#ifdef INPUT_SMOOTHING_SUPPORTED
	  if (cinfo->smoothing_factor) {
	downsample->methods[ci] = h2v2_smooth_downsample;
	downsample->pub.need_context_rows = TRUE;
	  } else
#endif
	downsample->methods[ci] = h2v2_downsample;
	} else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
		   (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
	  smoothok = FALSE;
	  downsample->methods[ci] = int_downsample;
	} else
	  ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  }

#ifdef INPUT_SMOOTHING_SUPPORTED
  if (cinfo->smoothing_factor && !smoothok)
	TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
#endif
}
/*** End of inlined file: jcsample.c ***/


/*** Start of inlined file: jctrans.c ***/
#define JPEG_INTERNALS

LOCAL(void) transencode_master_selection
	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
LOCAL(void) transencode_coef_controller
	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));

GLOBAL(void)
jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
{
  if (cinfo->global_state != CSTATE_START)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  jpeg_suppress_tables(cinfo, FALSE);
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  (*cinfo->dest->init_destination) (cinfo);
  transencode_master_selection(cinfo, coef_arrays);
  cinfo->next_scanline = 0;	/* so jpeg_write_marker works */
  cinfo->global_state = CSTATE_WRCOEFS;
}

GLOBAL(void)
jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
				   j_compress_ptr dstinfo)
{
  JQUANT_TBL ** qtblptr;
  jpeg_component_info *incomp, *outcomp;
  JQUANT_TBL *c_quant, *slot_quant;
  int tblno, ci, coefi;

  if (dstinfo->global_state != CSTATE_START)
	ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
  dstinfo->image_width = srcinfo->image_width;
  dstinfo->image_height = srcinfo->image_height;
  dstinfo->input_components = srcinfo->num_components;
  dstinfo->in_color_space = srcinfo->jpeg_color_space;
  jpeg_set_defaults(dstinfo);
  jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
  dstinfo->data_precision = srcinfo->data_precision;
  dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
	if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
	  qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
	  if (*qtblptr == NULL)
	*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
	  MEMCOPY((*qtblptr)->quantval,
		  srcinfo->quant_tbl_ptrs[tblno]->quantval,
		  SIZEOF((*qtblptr)->quantval));
	  (*qtblptr)->sent_table = FALSE;
	}
  }
  dstinfo->num_components = srcinfo->num_components;
  if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
	ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
		 MAX_COMPONENTS);
  for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
	   ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
	outcomp->component_id = incomp->component_id;
	outcomp->h_samp_factor = incomp->h_samp_factor;
	outcomp->v_samp_factor = incomp->v_samp_factor;
	outcomp->quant_tbl_no = incomp->quant_tbl_no;
	tblno = outcomp->quant_tbl_no;
	if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
	srcinfo->quant_tbl_ptrs[tblno] == NULL)
	  ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
	slot_quant = srcinfo->quant_tbl_ptrs[tblno];
	c_quant = incomp->quant_table;
	if (c_quant != NULL) {
	  for (coefi = 0; coefi < DCTSIZE2; coefi++) {
	if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
	  ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
	  }
	}
  }
  if (srcinfo->saw_JFIF_marker) {
	if (srcinfo->JFIF_major_version == 1) {
	  dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
	  dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
	}
	dstinfo->density_unit = srcinfo->density_unit;
	dstinfo->X_density = srcinfo->X_density;
	dstinfo->Y_density = srcinfo->Y_density;
  }
}

LOCAL(void)
transencode_master_selection (j_compress_ptr cinfo,
				  jvirt_barray_ptr * coef_arrays)
{
  cinfo->input_components = 1;
  jinit_c_master_control(cinfo, TRUE /* transcode only */);

  if (cinfo->arith_code) {
	ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  } else {
	if (cinfo->progressive_mode) {
#ifdef C_PROGRESSIVE_SUPPORTED
	  jinit_phuff_encoder(cinfo);
#else
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
	} else
	  jinit_huff_encoder(cinfo);
  }

  transencode_coef_controller(cinfo, coef_arrays);

  jinit_marker_writer(cinfo);

  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);

  (*cinfo->marker->write_file_header) (cinfo);
}

typedef struct {
  struct jpeg_c_coef_controller pub; /* public fields */

  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
  int MCU_rows_per_iMCU_row;	/* number of such rows needed */

  jvirt_barray_ptr * whole_image;

  JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
} my_coef_controller2;

typedef my_coef_controller2 * my_coef_ptr2;

LOCAL(void)
start_iMCU_row2 (j_compress_ptr cinfo)
{
  my_coef_ptr2 coef = (my_coef_ptr2) cinfo->coef;

  if (cinfo->comps_in_scan > 1) {
	coef->MCU_rows_per_iMCU_row = 1;
  } else {
	if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
	  coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
	else
	  coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  }

  coef->mcu_ctr = 0;
  coef->MCU_vert_offset = 0;
}

METHODDEF(void)
start_pass_coef2 (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
{
  my_coef_ptr2 coef = (my_coef_ptr2) cinfo->coef;

  if (pass_mode != JBUF_CRANK_DEST)
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);

  coef->iMCU_row_num = 0;
  start_iMCU_row2(cinfo);
}

METHODDEF(boolean)
compress_output2 (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
  my_coef_ptr2 coef = (my_coef_ptr2) cinfo->coef;
  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  int blkn, ci, xindex, yindex, yoffset, blockcnt;
  JDIMENSION start_col;
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  JBLOCKROW buffer_ptr;
  jpeg_component_info *compptr;

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	buffer[ci] = (*cinfo->mem->access_virt_barray)
	  ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
	   coef->iMCU_row_num * compptr->v_samp_factor,
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
  }

  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
	   yoffset++) {
	for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
	 MCU_col_num++) {
	  blkn = 0;			/* index of current DCT block within MCU */
	  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	start_col = MCU_col_num * compptr->MCU_width;
	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
						: compptr->last_col_width;
	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
	  if (coef->iMCU_row_num < last_iMCU_row ||
		  yindex+yoffset < compptr->last_row_height) {
		buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
		for (xindex = 0; xindex < blockcnt; xindex++)
		  MCU_buffer[blkn++] = buffer_ptr++;
	  } else {
		xindex = 0;
	  }
	  for (; xindex < compptr->MCU_width; xindex++) {
		MCU_buffer[blkn] = coef->dummy_buffer[blkn];
		MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
		blkn++;
	  }
	}
	  }
	  if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
	coef->MCU_vert_offset = yoffset;
	coef->mcu_ctr = MCU_col_num;
	return FALSE;
	  }
	}
	coef->mcu_ctr = 0;
  }
  coef->iMCU_row_num++;
  start_iMCU_row2(cinfo);
  return TRUE;
}

LOCAL(void)
transencode_coef_controller (j_compress_ptr cinfo,
				 jvirt_barray_ptr * coef_arrays)
{
  my_coef_ptr2 coef;
  JBLOCKROW buffer;
  int i;

  coef = (my_coef_ptr2)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_coef_controller2));
  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  coef->pub.start_pass = start_pass_coef2;
  coef->pub.compress_data = compress_output2;

  coef->whole_image = coef_arrays;

  buffer = (JBLOCKROW)
	(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
	coef->dummy_buffer[i] = buffer + i;
  }
}
/*** End of inlined file: jctrans.c ***/


/*** Start of inlined file: jdapistd.c ***/
#define JPEG_INTERNALS

LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));

GLOBAL(boolean)
jpeg_start_decompress (j_decompress_ptr cinfo)
{
  if (cinfo->global_state == DSTATE_READY) {
	jinit_master_decompress(cinfo);
	if (cinfo->buffered_image) {
	  cinfo->global_state = DSTATE_BUFIMAGE;
	  return TRUE;
	}
	cinfo->global_state = DSTATE_PRELOAD;
  }
  if (cinfo->global_state == DSTATE_PRELOAD) {
	if (cinfo->inputctl->has_multiple_scans) {
#ifdef D_MULTISCAN_FILES_SUPPORTED
	  for (;;) {
	int retcode;
	if (cinfo->progress != NULL)
	  (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
	retcode = (*cinfo->inputctl->consume_input) (cinfo);
	if (retcode == JPEG_SUSPENDED)
	  return FALSE;
	if (retcode == JPEG_REACHED_EOI)
	  break;
	if (cinfo->progress != NULL &&
		(retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
	  if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
		cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
	  }
	}
	  }
#else
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif /* D_MULTISCAN_FILES_SUPPORTED */
	}
	cinfo->output_scan_number = cinfo->input_scan_number;
  } else if (cinfo->global_state != DSTATE_PRESCAN)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  return output_pass_setup(cinfo);
}

LOCAL(boolean)
output_pass_setup (j_decompress_ptr cinfo)
{
  if (cinfo->global_state != DSTATE_PRESCAN) {
	(*cinfo->master->prepare_for_output_pass) (cinfo);
	cinfo->output_scanline = 0;
	cinfo->global_state = DSTATE_PRESCAN;
  }
  while (cinfo->master->is_dummy_pass) {
#ifdef QUANT_2PASS_SUPPORTED
	while (cinfo->output_scanline < cinfo->output_height) {
	  JDIMENSION last_scanline;
	  if (cinfo->progress != NULL) {
	cinfo->progress->pass_counter = (long) cinfo->output_scanline;
	cinfo->progress->pass_limit = (long) cinfo->output_height;
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
	  }
	  last_scanline = cinfo->output_scanline;
	  (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
					&cinfo->output_scanline, (JDIMENSION) 0);
	  if (cinfo->output_scanline == last_scanline)
	return FALSE;		/* No progress made, must suspend */
	}
	(*cinfo->master->finish_output_pass) (cinfo);
	(*cinfo->master->prepare_for_output_pass) (cinfo);
	cinfo->output_scanline = 0;
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif /* QUANT_2PASS_SUPPORTED */
  }
  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
  return TRUE;
}

GLOBAL(JDIMENSION)
jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
			 JDIMENSION max_lines)
{
  JDIMENSION row_ctr;

  if (cinfo->global_state != DSTATE_SCANNING)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  if (cinfo->output_scanline >= cinfo->output_height) {
	WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
	return 0;
  }

  if (cinfo->progress != NULL) {
	cinfo->progress->pass_counter = (long) cinfo->output_scanline;
	cinfo->progress->pass_limit = (long) cinfo->output_height;
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  }

  row_ctr = 0;
  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
  cinfo->output_scanline += row_ctr;
  return row_ctr;
}

GLOBAL(JDIMENSION)
jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
			JDIMENSION max_lines)
{
  JDIMENSION lines_per_iMCU_row;

  if (cinfo->global_state != DSTATE_RAW_OK)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  if (cinfo->output_scanline >= cinfo->output_height) {
	WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
	return 0;
  }

  if (cinfo->progress != NULL) {
	cinfo->progress->pass_counter = (long) cinfo->output_scanline;
	cinfo->progress->pass_limit = (long) cinfo->output_height;
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  }

  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
  if (max_lines < lines_per_iMCU_row)
	ERREXIT(cinfo, JERR_BUFFER_SIZE);

  if (! (*cinfo->coef->decompress_data) (cinfo, data))
	return 0;			/* suspension forced, can do nothing more */

  cinfo->output_scanline += lines_per_iMCU_row;
  return lines_per_iMCU_row;
}

#ifdef D_MULTISCAN_FILES_SUPPORTED

GLOBAL(boolean)
jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
{
  if (cinfo->global_state != DSTATE_BUFIMAGE &&
	  cinfo->global_state != DSTATE_PRESCAN)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  if (scan_number <= 0)
	scan_number = 1;
  if (cinfo->inputctl->eoi_reached &&
	  scan_number > cinfo->input_scan_number)
	scan_number = cinfo->input_scan_number;
  cinfo->output_scan_number = scan_number;
  return output_pass_setup(cinfo);
}

GLOBAL(boolean)
jpeg_finish_output (j_decompress_ptr cinfo)
{
  if ((cinfo->global_state == DSTATE_SCANNING ||
	   cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
	(*cinfo->master->finish_output_pass) (cinfo);
	cinfo->global_state = DSTATE_BUFPOST;
  } else if (cinfo->global_state != DSTATE_BUFPOST) {
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  }
  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
	 ! cinfo->inputctl->eoi_reached) {
	if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
	  return FALSE;		/* Suspend, come back later */
  }
  cinfo->global_state = DSTATE_BUFIMAGE;
  return TRUE;
}

#endif /* D_MULTISCAN_FILES_SUPPORTED */
/*** End of inlined file: jdapistd.c ***/


/*** Start of inlined file: jdapimin.c ***/
#define JPEG_INTERNALS

GLOBAL(void)
jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
{
  int i;

  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
  if (version != JPEG_LIB_VERSION)
	ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  if (structsize != SIZEOF(struct jpeg_decompress_struct))
	ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
		 (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);

  {
	struct jpeg_error_mgr * err = cinfo->err;
	void * client_data = cinfo->client_data; /* ignore Purify complaint here */
	MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
	cinfo->err = err;
	cinfo->client_data = client_data;
  }
  cinfo->is_decompressor = TRUE;

  jinit_memory_mgr((j_common_ptr) cinfo);

  cinfo->progress = NULL;
  cinfo->src = NULL;

  for (i = 0; i < NUM_QUANT_TBLS; i++)
	cinfo->quant_tbl_ptrs[i] = NULL;

  for (i = 0; i < NUM_HUFF_TBLS; i++) {
	cinfo->dc_huff_tbl_ptrs[i] = NULL;
	cinfo->ac_huff_tbl_ptrs[i] = NULL;
  }

  cinfo->marker_list = NULL;
  jinit_marker_reader(cinfo);

  jinit_input_controller(cinfo);

  cinfo->global_state = DSTATE_START;
}

GLOBAL(void)
jpeg_destroy_decompress (j_decompress_ptr cinfo)
{
  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
}

GLOBAL(void)
jpeg_abort_decompress (j_decompress_ptr cinfo)
{
  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
}

LOCAL(void)
default_decompress_parms (j_decompress_ptr cinfo)
{
  switch (cinfo->num_components) {
  case 1:
	cinfo->jpeg_color_space = JCS_GRAYSCALE;
	cinfo->out_color_space = JCS_GRAYSCALE;
	break;

  case 3:
	if (cinfo->saw_JFIF_marker) {
	  cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
	} else if (cinfo->saw_Adobe_marker) {
	  switch (cinfo->Adobe_transform) {
	  case 0:
	cinfo->jpeg_color_space = JCS_RGB;
	break;
	  case 1:
	cinfo->jpeg_color_space = JCS_YCbCr;
	break;
	  default:
	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
	break;
	  }
	} else {
	  int cid0 = cinfo->comp_info[0].component_id;
	  int cid1 = cinfo->comp_info[1].component_id;
	  int cid2 = cinfo->comp_info[2].component_id;

	  if (cid0 == 1 && cid1 == 2 && cid2 == 3)
	cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
	  else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
	cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
	  else {
	TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
	  }
	}
	cinfo->out_color_space = JCS_RGB;
	break;

  case 4:
	if (cinfo->saw_Adobe_marker) {
	  switch (cinfo->Adobe_transform) {
	  case 0:
	cinfo->jpeg_color_space = JCS_CMYK;
	break;
	  case 2:
	cinfo->jpeg_color_space = JCS_YCCK;
	break;
	  default:
	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
	cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
	break;
	  }
	} else {
	  cinfo->jpeg_color_space = JCS_CMYK;
	}
	cinfo->out_color_space = JCS_CMYK;
	break;

  default:
	cinfo->jpeg_color_space = JCS_UNKNOWN;
	cinfo->out_color_space = JCS_UNKNOWN;
	break;
  }

  cinfo->scale_num = 1;		/* 1:1 scaling */
  cinfo->scale_denom = 1;
  cinfo->output_gamma = 1.0;
  cinfo->buffered_image = FALSE;
  cinfo->raw_data_out = FALSE;
  cinfo->dct_method = JDCT_DEFAULT;
  cinfo->do_fancy_upsampling = TRUE;
  cinfo->do_block_smoothing = TRUE;
  cinfo->quantize_colors = FALSE;
  cinfo->dither_mode = JDITHER_FS;
#ifdef QUANT_2PASS_SUPPORTED
  cinfo->two_pass_quantize = TRUE;
#else
  cinfo->two_pass_quantize = FALSE;
#endif
  cinfo->desired_number_of_colors = 256;
  cinfo->colormap = NULL;
  cinfo->enable_1pass_quant = FALSE;
  cinfo->enable_external_quant = FALSE;
  cinfo->enable_2pass_quant = FALSE;
}

GLOBAL(int)
jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
{
  int retcode;

  if (cinfo->global_state != DSTATE_START &&
	  cinfo->global_state != DSTATE_INHEADER)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  retcode = jpeg_consume_input(cinfo);

  switch (retcode) {
  case JPEG_REACHED_SOS:
	retcode = JPEG_HEADER_OK;
	break;
  case JPEG_REACHED_EOI:
	if (require_image)		/* Complain if application wanted an image */
	  ERREXIT(cinfo, JERR_NO_IMAGE);
	jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
	retcode = JPEG_HEADER_TABLES_ONLY;
	break;
  case JPEG_SUSPENDED:
	break;
  }

  return retcode;
}

GLOBAL(int)
jpeg_consume_input (j_decompress_ptr cinfo)
{
  int retcode = JPEG_SUSPENDED;

  switch (cinfo->global_state) {
  case DSTATE_START:
	(*cinfo->inputctl->reset_input_controller) (cinfo);
	(*cinfo->src->init_source) (cinfo);
	cinfo->global_state = DSTATE_INHEADER;
  case DSTATE_INHEADER:
	retcode = (*cinfo->inputctl->consume_input) (cinfo);
	if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
	  default_decompress_parms(cinfo);
	  cinfo->global_state = DSTATE_READY;
	}
	break;
  case DSTATE_READY:
	retcode = JPEG_REACHED_SOS;
	break;
  case DSTATE_PRELOAD:
  case DSTATE_PRESCAN:
  case DSTATE_SCANNING:
  case DSTATE_RAW_OK:
  case DSTATE_BUFIMAGE:
  case DSTATE_BUFPOST:
  case DSTATE_STOPPING:
	retcode = (*cinfo->inputctl->consume_input) (cinfo);
	break;
  default:
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  }
  return retcode;
}

GLOBAL(boolean)
jpeg_input_complete (j_decompress_ptr cinfo)
{
  if (cinfo->global_state < DSTATE_START ||
	  cinfo->global_state > DSTATE_STOPPING)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  return cinfo->inputctl->eoi_reached;
}

GLOBAL(boolean)
jpeg_has_multiple_scans (j_decompress_ptr cinfo)
{
  if (cinfo->global_state < DSTATE_READY ||
	  cinfo->global_state > DSTATE_STOPPING)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  return cinfo->inputctl->has_multiple_scans;
}

GLOBAL(boolean)
jpeg_finish_decompress (j_decompress_ptr cinfo)
{
  if ((cinfo->global_state == DSTATE_SCANNING ||
	   cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
	if (cinfo->output_scanline < cinfo->output_height)
	  ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
	(*cinfo->master->finish_output_pass) (cinfo);
	cinfo->global_state = DSTATE_STOPPING;
  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
	cinfo->global_state = DSTATE_STOPPING;
  } else if (cinfo->global_state != DSTATE_STOPPING) {
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  }
  while (! cinfo->inputctl->eoi_reached) {
	if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
	  return FALSE;		/* Suspend, come back later */
  }
  (*cinfo->src->term_source) (cinfo);
  jpeg_abort((j_common_ptr) cinfo);
  return TRUE;
}
/*** End of inlined file: jdapimin.c ***/


/*** Start of inlined file: jdatasrc.c ***/

/*** Start of inlined file: jerror.h ***/
#ifndef JMESSAGE
#ifndef JERROR_H
#define JMAKE_ENUM_LIST
#else
#define JMESSAGE(code,string)
#endif /* JERROR_H */
#endif /* JMESSAGE */

#ifdef JMAKE_ENUM_LIST

typedef enum {

#define JMESSAGE(code,string)	code ,

#endif /* JMAKE_ENUM_LIST */

JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */

JMESSAGE(JERR_ARITH_NOTIMPL,
	 "Sorry, there are legal restrictions on arithmetic coding")
JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
JMESSAGE(JERR_BAD_LIB_VERSION,
	 "Wrong JPEG library version: library is %d, caller expects %d")
JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
JMESSAGE(JERR_BAD_PROGRESSION,
	 "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
JMESSAGE(JERR_BAD_PROG_SCRIPT,
	 "Invalid progressive parameters at scan script entry %d")
JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
JMESSAGE(JERR_BAD_STRUCT_SIZE,
	 "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
JMESSAGE(JERR_FILE_READ, "Input file read error")
JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
JMESSAGE(JERR_MISMATCHED_QUANT_TABLE,
	 "Cannot transcode due to multiple use of quantization table %d")
JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
JMESSAGE(JERR_QUANT_COMPONENTS,
	 "Cannot quantize more than %d color components")
JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
JMESSAGE(JERR_TFILE_WRITE,
	 "Write failed on temporary file --- out of disk space?")
JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
JMESSAGE(JMSG_VERSION, JVERSION)
JMESSAGE(JTRC_16BIT_TABLES,
	 "Caution: quantization tables are too coarse for baseline JPEG")
JMESSAGE(JTRC_ADOBE,
	 "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
JMESSAGE(JTRC_DQT, "Define Quantization Table %d  precision %d")
JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
JMESSAGE(JTRC_EOI, "End Of Image")
JMESSAGE(JTRC_HUFFBITS, "        %3d %3d %3d %3d %3d %3d %3d %3d")
JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d  %d")
JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
	 "Warning: thumbnail image size does not match data length %u")
JMESSAGE(JTRC_JFIF_EXTENSION,
	 "JFIF extension marker: type 0x%02x, length %u")
JMESSAGE(JTRC_JFIF_THUMBNAIL, "    with %d x %d thumbnail image")
JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
JMESSAGE(JTRC_QUANTVALS, "        %4u %4u %4u %4u %4u %4u %4u %4u")
JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
JMESSAGE(JTRC_RST, "RST%d")
JMESSAGE(JTRC_SMOOTH_NOTIMPL,
	 "Smoothing not supported with nonstandard sampling ratios")
JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
JMESSAGE(JTRC_SOF_COMPONENT, "    Component %d: %dhx%dv q=%d")
JMESSAGE(JTRC_SOI, "Start of Image")
JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
JMESSAGE(JTRC_SOS_COMPONENT, "    Component %d: dc=%d ac=%d")
JMESSAGE(JTRC_SOS_PARAMS, "  Ss=%d, Se=%d, Ah=%d, Al=%d")
JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
JMESSAGE(JTRC_THUMB_JPEG,
	 "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
JMESSAGE(JTRC_THUMB_PALETTE,
	 "JFIF extension marker: palette thumbnail image, length %u")
JMESSAGE(JTRC_THUMB_RGB,
	 "JFIF extension marker: RGB thumbnail image, length %u")
JMESSAGE(JTRC_UNKNOWN_IDS,
	 "Unrecognized component IDs %d %d %d, assuming YCbCr")
JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
JMESSAGE(JWRN_BOGUS_PROGRESSION,
	 "Inconsistent progression sequence for component %d coefficient %d")
JMESSAGE(JWRN_EXTRANEOUS_DATA,
	 "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
JMESSAGE(JWRN_MUST_RESYNC,
	 "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")

#ifdef JMAKE_ENUM_LIST

  JMSG_LASTMSGCODE
} J_MESSAGE_CODE;

#undef JMAKE_ENUM_LIST
#endif /* JMAKE_ENUM_LIST */

#undef JMESSAGE

#ifndef JERROR_H
#define JERROR_H

#define ERREXIT(cinfo,code)  \
  ((cinfo)->err->msg_code = (code), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT1(cinfo,code,p1)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT2(cinfo,code,p1,p2)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT3(cinfo,code,p1,p2,p3)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (cinfo)->err->msg_parm.i[2] = (p3), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT4(cinfo,code,p1,p2,p3,p4)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (cinfo)->err->msg_parm.i[2] = (p3), \
   (cinfo)->err->msg_parm.i[3] = (p4), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXITS(cinfo,code,str)  \
  ((cinfo)->err->msg_code = (code), \
   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))

#define MAKESTMT(stuff)		do { stuff } while (0)

#define WARNMS(cinfo,code)  \
  ((cinfo)->err->msg_code = (code), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
#define WARNMS1(cinfo,code,p1)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
#define WARNMS2(cinfo,code,p1,p2)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))

#define TRACEMS(cinfo,lvl,code)  \
  ((cinfo)->err->msg_code = (code), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS1(cinfo,lvl,code,p1)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS2(cinfo,lvl,code,p1,p2)  \
  ((cinfo)->err->msg_code = (code), \
   (cinfo)->err->msg_parm.i[0] = (p1), \
   (cinfo)->err->msg_parm.i[1] = (p2), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS3(cinfo,lvl,code,p1,p2,p3)  \
  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
	   (cinfo)->err->msg_code = (code); \
	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4)  \
  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
	   (cinfo)->err->msg_code = (code); \
	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5)  \
  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
	   _mp[4] = (p5); \
	   (cinfo)->err->msg_code = (code); \
	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8)  \
  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
	   _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
	   (cinfo)->err->msg_code = (code); \
	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMSS(cinfo,lvl,code,str)  \
  ((cinfo)->err->msg_code = (code), \
   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))

#endif /* JERROR_H */
/*** End of inlined file: jerror.h ***/

typedef struct {
  struct jpeg_source_mgr pub;	/* public fields */

  FILE * infile;		/* source stream */
  JOCTET * buffer;		/* start of buffer */
  boolean start_of_file;	/* have we gotten any data yet? */
} my_source_mgr;

typedef my_source_mgr * my_src_ptr;

#define INPUT_BUF_SIZE  4096	/* choose an efficiently fread'able size */

METHODDEF(void)
init_source (j_decompress_ptr cinfo)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;

  src->start_of_file = TRUE;
}

METHODDEF(boolean)
fill_input_buffer (j_decompress_ptr cinfo)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  size_t nbytes;

  nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);

  if (nbytes <= 0) {
	if (src->start_of_file)	/* Treat empty input file as fatal error */
	  ERREXIT(cinfo, JERR_INPUT_EMPTY);
	WARNMS(cinfo, JWRN_JPEG_EOF);
	src->buffer[0] = (JOCTET) 0xFF;
	src->buffer[1] = (JOCTET) JPEG_EOI;
	nbytes = 2;
  }

  src->pub.next_input_byte = src->buffer;
  src->pub.bytes_in_buffer = nbytes;
  src->start_of_file = FALSE;

  return TRUE;
}

METHODDEF(void)
skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;

  if (num_bytes > 0) {
	while (num_bytes > (long) src->pub.bytes_in_buffer) {
	  num_bytes -= (long) src->pub.bytes_in_buffer;
	  (void) fill_input_buffer(cinfo);
	}
	src->pub.next_input_byte += (size_t) num_bytes;
	src->pub.bytes_in_buffer -= (size_t) num_bytes;
  }
}

METHODDEF(void)
term_source (j_decompress_ptr cinfo)
{
}

GLOBAL(void)
jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
{
  my_src_ptr src;

  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
	cinfo->src = (struct jpeg_source_mgr *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				  SIZEOF(my_source_mgr));
	src = (my_src_ptr) cinfo->src;
	src->buffer = (JOCTET *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				  INPUT_BUF_SIZE * SIZEOF(JOCTET));
  }

  src = (my_src_ptr) cinfo->src;
  src->pub.init_source = init_source;
  src->pub.fill_input_buffer = fill_input_buffer;
  src->pub.skip_input_data = skip_input_data;
  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  src->pub.term_source = term_source;
  src->infile = infile;
  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  src->pub.next_input_byte = NULL; /* until buffer loaded */
}
/*** End of inlined file: jdatasrc.c ***/


/*** Start of inlined file: jdcoefct.c ***/
#define JPEG_INTERNALS

#ifndef D_PROGRESSIVE_SUPPORTED
#undef BLOCK_SMOOTHING_SUPPORTED
#endif

typedef struct {
  struct jpeg_d_coef_controller pub; /* public fields */

  JDIMENSION MCU_ctr;		/* counts MCUs processed in current row */
  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
  int MCU_rows_per_iMCU_row;	/* number of such rows needed */

  JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];

#ifdef D_MULTISCAN_FILES_SUPPORTED
  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
#endif

#ifdef BLOCK_SMOOTHING_SUPPORTED
  int * coef_bits_latch;
#define SAVED_COEFS  6		/* we save coef_bits[0..5] */
#endif
} my_coef_controller3;

typedef my_coef_controller3 * my_coef_ptr3;

METHODDEF(int) decompress_onepass
	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
#ifdef D_MULTISCAN_FILES_SUPPORTED
METHODDEF(int) decompress_data
	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
#endif
#ifdef BLOCK_SMOOTHING_SUPPORTED
LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
METHODDEF(int) decompress_smooth_data
	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
#endif

LOCAL(void)
start_iMCU_row3 (j_decompress_ptr cinfo)
{
  my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;

  if (cinfo->comps_in_scan > 1) {
	coef->MCU_rows_per_iMCU_row = 1;
  } else {
	if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
	  coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
	else
	  coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  }

  coef->MCU_ctr = 0;
  coef->MCU_vert_offset = 0;
}

METHODDEF(void)
start_input_pass (j_decompress_ptr cinfo)
{
  cinfo->input_iMCU_row = 0;
  start_iMCU_row3(cinfo);
}

METHODDEF(void)
start_output_pass (j_decompress_ptr cinfo)
{
#ifdef BLOCK_SMOOTHING_SUPPORTED
  my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;

  if (coef->pub.coef_arrays != NULL) {
	if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
	  coef->pub.decompress_data = decompress_smooth_data;
	else
	  coef->pub.decompress_data = decompress_data;
  }
#endif
  cinfo->output_iMCU_row = 0;
}

METHODDEF(int)
decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
{
  my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  int blkn, ci, xindex, yindex, yoffset, useful_width;
  JSAMPARRAY output_ptr;
  JDIMENSION start_col, output_col;
  jpeg_component_info *compptr;
  inverse_DCT_method_ptr inverse_DCT;

  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
	   yoffset++) {
	for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
	 MCU_col_num++) {
	  jzero_far((void FAR *) coef->MCU_buffer[0],
		(size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
	  if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
	coef->MCU_vert_offset = yoffset;
	coef->MCU_ctr = MCU_col_num;
	return JPEG_SUSPENDED;
	  }
	  blkn = 0;			/* index of current DCT block within MCU */
	  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	if (! compptr->component_needed) {
	  blkn += compptr->MCU_blocks;
	  continue;
	}
	inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
	useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
							: compptr->last_col_width;
	output_ptr = output_buf[compptr->component_index] +
	  yoffset * compptr->DCT_scaled_size;
	start_col = MCU_col_num * compptr->MCU_sample_width;
	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
	  if (cinfo->input_iMCU_row < last_iMCU_row ||
		  yoffset+yindex < compptr->last_row_height) {
		output_col = start_col;
		for (xindex = 0; xindex < useful_width; xindex++) {
		  (*inverse_DCT) (cinfo, compptr,
				  (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
				  output_ptr, output_col);
		  output_col += compptr->DCT_scaled_size;
		}
	  }
	  blkn += compptr->MCU_width;
	  output_ptr += compptr->DCT_scaled_size;
	}
	  }
	}
	coef->MCU_ctr = 0;
  }
  cinfo->output_iMCU_row++;
  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
	start_iMCU_row3(cinfo);
	return JPEG_ROW_COMPLETED;
  }
  (*cinfo->inputctl->finish_input_pass) (cinfo);
  return JPEG_SCAN_COMPLETED;
}

METHODDEF(int)
dummy_consume_data (j_decompress_ptr cinfo)
{
  return JPEG_SUSPENDED;	/* Always indicate nothing was done */
}

#ifdef D_MULTISCAN_FILES_SUPPORTED

METHODDEF(int)
consume_data (j_decompress_ptr cinfo)
{
  my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  int blkn, ci, xindex, yindex, yoffset;
  JDIMENSION start_col;
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  JBLOCKROW buffer_ptr;
  jpeg_component_info *compptr;

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	buffer[ci] = (*cinfo->mem->access_virt_barray)
	  ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
	   cinfo->input_iMCU_row * compptr->v_samp_factor,
	   (JDIMENSION) compptr->v_samp_factor, TRUE);
  }

  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
	   yoffset++) {
	for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
	 MCU_col_num++) {
	  blkn = 0;			/* index of current DCT block within MCU */
	  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	start_col = MCU_col_num * compptr->MCU_width;
	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
		coef->MCU_buffer[blkn++] = buffer_ptr++;
	  }
	}
	  }
	  if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
	coef->MCU_vert_offset = yoffset;
	coef->MCU_ctr = MCU_col_num;
	return JPEG_SUSPENDED;
	  }
	}
	coef->MCU_ctr = 0;
  }
  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
	start_iMCU_row3(cinfo);
	return JPEG_ROW_COMPLETED;
  }
  (*cinfo->inputctl->finish_input_pass) (cinfo);
  return JPEG_SCAN_COMPLETED;
}

METHODDEF(int)
decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
{
  my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  JDIMENSION block_num;
  int ci, block_row, block_rows;
  JBLOCKARRAY buffer;
  JBLOCKROW buffer_ptr;
  JSAMPARRAY output_ptr;
  JDIMENSION output_col;
  jpeg_component_info *compptr;
  inverse_DCT_method_ptr inverse_DCT;

  while (cinfo->input_scan_number < cinfo->output_scan_number ||
	 (cinfo->input_scan_number == cinfo->output_scan_number &&
	  cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
	if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
	  return JPEG_SUSPENDED;
  }

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	if (! compptr->component_needed)
	  continue;
	buffer = (*cinfo->mem->access_virt_barray)
	  ((j_common_ptr) cinfo, coef->whole_image[ci],
	   cinfo->output_iMCU_row * compptr->v_samp_factor,
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
	if (cinfo->output_iMCU_row < last_iMCU_row)
	  block_rows = compptr->v_samp_factor;
	else {
	  block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
	  if (block_rows == 0) block_rows = compptr->v_samp_factor;
	}
	inverse_DCT = cinfo->idct->inverse_DCT[ci];
	output_ptr = output_buf[ci];
	for (block_row = 0; block_row < block_rows; block_row++) {
	  buffer_ptr = buffer[block_row];
	  output_col = 0;
	  for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
			output_ptr, output_col);
	buffer_ptr++;
	output_col += compptr->DCT_scaled_size;
	  }
	  output_ptr += compptr->DCT_scaled_size;
	}
  }

  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
	return JPEG_ROW_COMPLETED;
  return JPEG_SCAN_COMPLETED;
}

#endif /* D_MULTISCAN_FILES_SUPPORTED */

#ifdef BLOCK_SMOOTHING_SUPPORTED

#define Q01_POS  1
#define Q10_POS  8
#define Q20_POS  16
#define Q11_POS  9
#define Q02_POS  2

LOCAL(boolean)
smoothing_ok (j_decompress_ptr cinfo)
{
  my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  boolean smoothing_useful = FALSE;
  int ci, coefi;
  jpeg_component_info *compptr;
  JQUANT_TBL * qtable;
  int * coef_bits;
  int * coef_bits_latch;

  if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
	return FALSE;

  if (coef->coef_bits_latch == NULL)
	coef->coef_bits_latch = (int *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  cinfo->num_components *
				  (SAVED_COEFS * SIZEOF(int)));
  coef_bits_latch = coef->coef_bits_latch;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	if ((qtable = compptr->quant_table) == NULL)
	  return FALSE;
	if (qtable->quantval[0] == 0 ||
	qtable->quantval[Q01_POS] == 0 ||
	qtable->quantval[Q10_POS] == 0 ||
	qtable->quantval[Q20_POS] == 0 ||
	qtable->quantval[Q11_POS] == 0 ||
	qtable->quantval[Q02_POS] == 0)
	  return FALSE;
	coef_bits = cinfo->coef_bits[ci];
	if (coef_bits[0] < 0)
	  return FALSE;
	for (coefi = 1; coefi <= 5; coefi++) {
	  coef_bits_latch[coefi] = coef_bits[coefi];
	  if (coef_bits[coefi] != 0)
	smoothing_useful = TRUE;
	}
	coef_bits_latch += SAVED_COEFS;
  }

  return smoothing_useful;
}

METHODDEF(int)
decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
{
  my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  JDIMENSION block_num, last_block_column;
  int ci, block_row, block_rows, access_rows;
  JBLOCKARRAY buffer;
  JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
  JSAMPARRAY output_ptr;
  JDIMENSION output_col;
  jpeg_component_info *compptr;
  inverse_DCT_method_ptr inverse_DCT;
  boolean first_row, last_row;
  JBLOCK workspace;
  int *coef_bits;
  JQUANT_TBL *quanttbl;
  INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
  int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
  int Al, pred;

  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
	 ! cinfo->inputctl->eoi_reached) {
	if (cinfo->input_scan_number == cinfo->output_scan_number) {
	  JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
	  if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
	break;
	}
	if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
	  return JPEG_SUSPENDED;
  }

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	if (! compptr->component_needed)
	  continue;
	if (cinfo->output_iMCU_row < last_iMCU_row) {
	  block_rows = compptr->v_samp_factor;
	  access_rows = block_rows * 2; /* this and next iMCU row */
	  last_row = FALSE;
	} else {
	  block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
	  if (block_rows == 0) block_rows = compptr->v_samp_factor;
	  access_rows = block_rows; /* this iMCU row only */
	  last_row = TRUE;
	}
	if (cinfo->output_iMCU_row > 0) {
	  access_rows += compptr->v_samp_factor; /* prior iMCU row too */
	  buffer = (*cinfo->mem->access_virt_barray)
	((j_common_ptr) cinfo, coef->whole_image[ci],
	 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
	 (JDIMENSION) access_rows, FALSE);
	  buffer += compptr->v_samp_factor;	/* point to current iMCU row */
	  first_row = FALSE;
	} else {
	  buffer = (*cinfo->mem->access_virt_barray)
	((j_common_ptr) cinfo, coef->whole_image[ci],
	 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
	  first_row = TRUE;
	}
	coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
	quanttbl = compptr->quant_table;
	Q00 = quanttbl->quantval[0];
	Q01 = quanttbl->quantval[Q01_POS];
	Q10 = quanttbl->quantval[Q10_POS];
	Q20 = quanttbl->quantval[Q20_POS];
	Q11 = quanttbl->quantval[Q11_POS];
	Q02 = quanttbl->quantval[Q02_POS];
	inverse_DCT = cinfo->idct->inverse_DCT[ci];
	output_ptr = output_buf[ci];
	for (block_row = 0; block_row < block_rows; block_row++) {
	  buffer_ptr = buffer[block_row];
	  if (first_row && block_row == 0)
	prev_block_row = buffer_ptr;
	  else
	prev_block_row = buffer[block_row-1];
	  if (last_row && block_row == block_rows-1)
	next_block_row = buffer_ptr;
	  else
	next_block_row = buffer[block_row+1];
	  DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
	  DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
	  DC7 = DC8 = DC9 = (int) next_block_row[0][0];
	  output_col = 0;
	  last_block_column = compptr->width_in_blocks - 1;
	  for (block_num = 0; block_num <= last_block_column; block_num++) {
	jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
	if (block_num < last_block_column) {
	  DC3 = (int) prev_block_row[1][0];
	  DC6 = (int) buffer_ptr[1][0];
	  DC9 = (int) next_block_row[1][0];
	}
	if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
	  num = 36 * Q00 * (DC4 - DC6);
	  if (num >= 0) {
		pred = (int) (((Q01<<7) + num) / (Q01<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
	  } else {
		pred = (int) (((Q01<<7) - num) / (Q01<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
		pred = -pred;
	  }
	  workspace[1] = (JCOEF) pred;
	}
	if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
	  num = 36 * Q00 * (DC2 - DC8);
	  if (num >= 0) {
		pred = (int) (((Q10<<7) + num) / (Q10<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
	  } else {
		pred = (int) (((Q10<<7) - num) / (Q10<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
		pred = -pred;
	  }
	  workspace[8] = (JCOEF) pred;
	}
	if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
	  num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
	  if (num >= 0) {
		pred = (int) (((Q20<<7) + num) / (Q20<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
	  } else {
		pred = (int) (((Q20<<7) - num) / (Q20<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
		pred = -pred;
	  }
	  workspace[16] = (JCOEF) pred;
	}
	if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
	  num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
	  if (num >= 0) {
		pred = (int) (((Q11<<7) + num) / (Q11<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
	  } else {
		pred = (int) (((Q11<<7) - num) / (Q11<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
		pred = -pred;
	  }
	  workspace[9] = (JCOEF) pred;
	}
	if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
	  num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
	  if (num >= 0) {
		pred = (int) (((Q02<<7) + num) / (Q02<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
	  } else {
		pred = (int) (((Q02<<7) - num) / (Q02<<8));
		if (Al > 0 && pred >= (1<<Al))
		  pred = (1<<Al)-1;
		pred = -pred;
	  }
	  workspace[2] = (JCOEF) pred;
	}
	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
			output_ptr, output_col);
	DC1 = DC2; DC2 = DC3;
	DC4 = DC5; DC5 = DC6;
	DC7 = DC8; DC8 = DC9;
	buffer_ptr++, prev_block_row++, next_block_row++;
	output_col += compptr->DCT_scaled_size;
	  }
	  output_ptr += compptr->DCT_scaled_size;
	}
  }

  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
	return JPEG_ROW_COMPLETED;
  return JPEG_SCAN_COMPLETED;
}

#endif /* BLOCK_SMOOTHING_SUPPORTED */

GLOBAL(void)
jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
{
  my_coef_ptr3 coef;

  coef = (my_coef_ptr3)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_coef_controller3));
  cinfo->coef = (struct jpeg_d_coef_controller *) coef;
  coef->pub.start_input_pass = start_input_pass;
  coef->pub.start_output_pass = start_output_pass;
#ifdef BLOCK_SMOOTHING_SUPPORTED
  coef->coef_bits_latch = NULL;
#endif

  if (need_full_buffer) {
#ifdef D_MULTISCAN_FILES_SUPPORTED
	int ci, access_rows;
	jpeg_component_info *compptr;

	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
	  access_rows = compptr->v_samp_factor;
#ifdef BLOCK_SMOOTHING_SUPPORTED
	  if (cinfo->progressive_mode)
	access_rows *= 3;
#endif
	  coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
	((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
				(long) compptr->h_samp_factor),
	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
				(long) compptr->v_samp_factor),
	 (JDIMENSION) access_rows);
	}
	coef->pub.consume_data = consume_data;
	coef->pub.decompress_data = decompress_data;
	coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
  } else {
	JBLOCKROW buffer;
	int i;

	buffer = (JBLOCKROW)
	  (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
	for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
	  coef->MCU_buffer[i] = buffer + i;
	}
	coef->pub.consume_data = dummy_consume_data;
	coef->pub.decompress_data = decompress_onepass;
	coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
  }
}
/*** End of inlined file: jdcoefct.c ***/

	#undef FIX

/*** Start of inlined file: jdcolor.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_color_deconverter pub; /* public fields */

  int * Cr_r_tab;		/* => table for Cr to R conversion */
  int * Cb_b_tab;		/* => table for Cb to B conversion */
  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
} my_color_deconverter2;

typedef my_color_deconverter2 * my_cconvert_ptr2;

#define SCALEBITS	16	/* speediest right-shift on some machines */
#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))

LOCAL(void)
build_ycc_rgb_table (j_decompress_ptr cinfo)
{
  my_cconvert_ptr2 cconvert = (my_cconvert_ptr2) cinfo->cconvert;
  int i;
  INT32 x;
  SHIFT_TEMPS

  cconvert->Cr_r_tab = (int *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(MAXJSAMPLE+1) * SIZEOF(int));
  cconvert->Cb_b_tab = (int *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(MAXJSAMPLE+1) * SIZEOF(int));
  cconvert->Cr_g_tab = (INT32 *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(MAXJSAMPLE+1) * SIZEOF(INT32));
  cconvert->Cb_g_tab = (INT32 *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(MAXJSAMPLE+1) * SIZEOF(INT32));

  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
	cconvert->Cr_r_tab[i] = (int)
			RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
	cconvert->Cb_b_tab[i] = (int)
			RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
	cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
	cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
  }
}

METHODDEF(void)
ycc_rgb_convert (j_decompress_ptr cinfo,
		 JSAMPIMAGE input_buf, JDIMENSION input_row,
		 JSAMPARRAY output_buf, int num_rows)
{
  my_cconvert_ptr2 cconvert = (my_cconvert_ptr2) cinfo->cconvert;
  register int y, cb, cr;
  register JSAMPROW outptr;
  register JSAMPROW inptr0, inptr1, inptr2;
  register JDIMENSION col;
  JDIMENSION num_cols = cinfo->output_width;
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  register int * Crrtab = cconvert->Cr_r_tab;
  register int * Cbbtab = cconvert->Cb_b_tab;
  register INT32 * Crgtab = cconvert->Cr_g_tab;
  register INT32 * Cbgtab = cconvert->Cb_g_tab;
  SHIFT_TEMPS

  while (--num_rows >= 0) {
	inptr0 = input_buf[0][input_row];
	inptr1 = input_buf[1][input_row];
	inptr2 = input_buf[2][input_row];
	input_row++;
	outptr = *output_buf++;
	for (col = 0; col < num_cols; col++) {
	  y  = GETJSAMPLE(inptr0[col]);
	  cb = GETJSAMPLE(inptr1[col]);
	  cr = GETJSAMPLE(inptr2[col]);
	  outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
	  outptr[RGB_GREEN] = range_limit[y +
				  ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
						 SCALEBITS))];
	  outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
	  outptr += RGB_PIXELSIZE;
	}
  }
}

METHODDEF(void)
null_convert2 (j_decompress_ptr cinfo,
		  JSAMPIMAGE input_buf, JDIMENSION input_row,
		  JSAMPARRAY output_buf, int num_rows)
{
  register JSAMPROW inptr, outptr;
  register JDIMENSION count;
  register int num_components = cinfo->num_components;
  JDIMENSION num_cols = cinfo->output_width;
  int ci;

  while (--num_rows >= 0) {
	for (ci = 0; ci < num_components; ci++) {
	  inptr = input_buf[ci][input_row];
	  outptr = output_buf[0] + ci;
	  for (count = num_cols; count > 0; count--) {
	*outptr = *inptr++;	/* needn't bother with GETJSAMPLE() here */
	outptr += num_components;
	  }
	}
	input_row++;
	output_buf++;
  }
}

METHODDEF(void)
grayscale_convert2 (j_decompress_ptr cinfo,
		   JSAMPIMAGE input_buf, JDIMENSION input_row,
		   JSAMPARRAY output_buf, int num_rows)
{
  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
			num_rows, cinfo->output_width);
}

METHODDEF(void)
gray_rgb_convert (j_decompress_ptr cinfo,
		  JSAMPIMAGE input_buf, JDIMENSION input_row,
		  JSAMPARRAY output_buf, int num_rows)
{
  register JSAMPROW inptr, outptr;
  register JDIMENSION col;
  JDIMENSION num_cols = cinfo->output_width;

  while (--num_rows >= 0) {
	inptr = input_buf[0][input_row++];
	outptr = *output_buf++;
	for (col = 0; col < num_cols; col++) {
	  outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
	  outptr += RGB_PIXELSIZE;
	}
  }
}

METHODDEF(void)
ycck_cmyk_convert (j_decompress_ptr cinfo,
		   JSAMPIMAGE input_buf, JDIMENSION input_row,
		   JSAMPARRAY output_buf, int num_rows)
{
  my_cconvert_ptr2 cconvert = (my_cconvert_ptr2) cinfo->cconvert;
  register int y, cb, cr;
  register JSAMPROW outptr;
  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  register JDIMENSION col;
  JDIMENSION num_cols = cinfo->output_width;
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  register int * Crrtab = cconvert->Cr_r_tab;
  register int * Cbbtab = cconvert->Cb_b_tab;
  register INT32 * Crgtab = cconvert->Cr_g_tab;
  register INT32 * Cbgtab = cconvert->Cb_g_tab;
  SHIFT_TEMPS

  while (--num_rows >= 0) {
	inptr0 = input_buf[0][input_row];
	inptr1 = input_buf[1][input_row];
	inptr2 = input_buf[2][input_row];
	inptr3 = input_buf[3][input_row];
	input_row++;
	outptr = *output_buf++;
	for (col = 0; col < num_cols; col++) {
	  y  = GETJSAMPLE(inptr0[col]);
	  cb = GETJSAMPLE(inptr1[col]);
	  cr = GETJSAMPLE(inptr2[col]);
	  outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];	/* red */
	  outptr[1] = range_limit[MAXJSAMPLE - (y +			/* green */
				  ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
						 SCALEBITS)))];
	  outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];	/* blue */
	  outptr[3] = inptr3[col];	/* don't need GETJSAMPLE here */
	  outptr += 4;
	}
  }
}

METHODDEF(void)
start_pass_dcolor (j_decompress_ptr cinfo)
{
}

GLOBAL(void)
jinit_color_deconverter (j_decompress_ptr cinfo)
{
  my_cconvert_ptr2 cconvert;
  int ci;

  cconvert = (my_cconvert_ptr2)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_color_deconverter2));
  cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
  cconvert->pub.start_pass = start_pass_dcolor;

  switch (cinfo->jpeg_color_space) {
  case JCS_GRAYSCALE:
	if (cinfo->num_components != 1)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	break;

  case JCS_RGB:
  case JCS_YCbCr:
	if (cinfo->num_components != 3)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	break;

  case JCS_CMYK:
  case JCS_YCCK:
	if (cinfo->num_components != 4)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	break;

  default:			/* JCS_UNKNOWN can be anything */
	if (cinfo->num_components < 1)
	  ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
	break;
  }

  switch (cinfo->out_color_space) {
  case JCS_GRAYSCALE:
	cinfo->out_color_components = 1;
	if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
	cinfo->jpeg_color_space == JCS_YCbCr) {
	  cconvert->pub.color_convert = grayscale_convert2;
	  for (ci = 1; ci < cinfo->num_components; ci++)
	cinfo->comp_info[ci].component_needed = FALSE;
	} else
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;

  case JCS_RGB:
	cinfo->out_color_components = RGB_PIXELSIZE;
	if (cinfo->jpeg_color_space == JCS_YCbCr) {
	  cconvert->pub.color_convert = ycc_rgb_convert;
	  build_ycc_rgb_table(cinfo);
	} else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
	  cconvert->pub.color_convert = gray_rgb_convert;
	} else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
	  cconvert->pub.color_convert = null_convert2;
	} else
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;

  case JCS_CMYK:
	cinfo->out_color_components = 4;
	if (cinfo->jpeg_color_space == JCS_YCCK) {
	  cconvert->pub.color_convert = ycck_cmyk_convert;
	  build_ycc_rgb_table(cinfo);
	} else if (cinfo->jpeg_color_space == JCS_CMYK) {
	  cconvert->pub.color_convert = null_convert2;
	} else
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;

  default:
	if (cinfo->out_color_space == cinfo->jpeg_color_space) {
	  cinfo->out_color_components = cinfo->num_components;
	  cconvert->pub.color_convert = null_convert2;
	} else			/* unsupported non-null conversion */
	  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
	break;
  }

  if (cinfo->quantize_colors)
	cinfo->output_components = 1; /* single colormapped output component */
  else
	cinfo->output_components = cinfo->out_color_components;
}
/*** End of inlined file: jdcolor.c ***/


	#undef FIX

/*** Start of inlined file: jddctmgr.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_inverse_dct pub;	/* public fields */

  int cur_method[MAX_COMPONENTS];
} my_idct_controller;

typedef my_idct_controller * my_idct_ptr;

typedef union {
  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
#ifdef DCT_IFAST_SUPPORTED
  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
#endif
#ifdef DCT_FLOAT_SUPPORTED
  FLOAT_MULT_TYPE float_array[DCTSIZE2];
#endif
} multiplier_table;

#ifdef DCT_ISLOW_SUPPORTED
#define PROVIDE_ISLOW_TABLES
#else
#ifdef IDCT_SCALING_SUPPORTED
#define PROVIDE_ISLOW_TABLES
#endif
#endif

METHODDEF(void)
start_pass (j_decompress_ptr cinfo)
{
  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
  int ci, i;
  jpeg_component_info *compptr;
  int method = 0;
  inverse_DCT_method_ptr method_ptr = NULL;
  JQUANT_TBL * qtbl;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	switch (compptr->DCT_scaled_size) {
#ifdef IDCT_SCALING_SUPPORTED
	case 1:
	  method_ptr = jpeg_idct_1x1;
	  method = JDCT_ISLOW;	/* jidctred uses islow-style table */
	  break;
	case 2:
	  method_ptr = jpeg_idct_2x2;
	  method = JDCT_ISLOW;	/* jidctred uses islow-style table */
	  break;
	case 4:
	  method_ptr = jpeg_idct_4x4;
	  method = JDCT_ISLOW;	/* jidctred uses islow-style table */
	  break;
#endif
	case DCTSIZE:
	  switch (cinfo->dct_method) {
#ifdef DCT_ISLOW_SUPPORTED
	  case JDCT_ISLOW:
	method_ptr = jpeg_idct_islow;
	method = JDCT_ISLOW;
	break;
#endif
#ifdef DCT_IFAST_SUPPORTED
	  case JDCT_IFAST:
	method_ptr = jpeg_idct_ifast;
	method = JDCT_IFAST;
	break;
#endif
#ifdef DCT_FLOAT_SUPPORTED
	  case JDCT_FLOAT:
	method_ptr = jpeg_idct_float;
	method = JDCT_FLOAT;
	break;
#endif
	  default:
	ERREXIT(cinfo, JERR_NOT_COMPILED);
	break;
	  }
	  break;
	default:
	  ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
	  break;
	}
	idct->pub.inverse_DCT[ci] = method_ptr;
	if (! compptr->component_needed || idct->cur_method[ci] == method)
	  continue;
	qtbl = compptr->quant_table;
	if (qtbl == NULL)		/* happens if no data yet for component */
	  continue;
	idct->cur_method[ci] = method;
	switch (method) {
#ifdef PROVIDE_ISLOW_TABLES
	case JDCT_ISLOW:
	  {
	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
	for (i = 0; i < DCTSIZE2; i++) {
	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
	}
	  }
	  break;
#endif
#ifdef DCT_IFAST_SUPPORTED
	case JDCT_IFAST:
	  {
	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
#define CONST_BITS 14
	static const INT16 aanscales[DCTSIZE2] = {
	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
	};
	SHIFT_TEMPS

	for (i = 0; i < DCTSIZE2; i++) {
	  ifmtbl[i] = (IFAST_MULT_TYPE)
		DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
				  (INT32) aanscales[i]),
			CONST_BITS-IFAST_SCALE_BITS);
	}
	  }
	  break;
#endif
#ifdef DCT_FLOAT_SUPPORTED
	case JDCT_FLOAT:
	  {
	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
	int row, col;
	static const double aanscalefactor[DCTSIZE] = {
	  1.0, 1.387039845, 1.306562965, 1.175875602,
	  1.0, 0.785694958, 0.541196100, 0.275899379
	};

	i = 0;
	for (row = 0; row < DCTSIZE; row++) {
	  for (col = 0; col < DCTSIZE; col++) {
		fmtbl[i] = (FLOAT_MULT_TYPE)
		  ((double) qtbl->quantval[i] *
		   aanscalefactor[row] * aanscalefactor[col]);
		i++;
	  }
	}
	  }
	  break;
#endif
	default:
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
	  break;
	}
  }
}

GLOBAL(void)
jinit_inverse_dct (j_decompress_ptr cinfo)
{
  my_idct_ptr idct;
  int ci;
  jpeg_component_info *compptr;

  idct = (my_idct_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_idct_controller));
  cinfo->idct = (struct jpeg_inverse_dct *) idct;
  idct->pub.start_pass = start_pass;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	compptr->dct_table =
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  SIZEOF(multiplier_table));
	MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
	idct->cur_method[ci] = -1;
  }
}
/*** End of inlined file: jddctmgr.c ***/


	#undef CONST_BITS
	#undef ASSIGN_STATE

/*** Start of inlined file: jdhuff.c ***/
#define JPEG_INTERNALS


/*** Start of inlined file: jdhuff.h ***/
#ifndef __jdhuff_h__
#define __jdhuff_h__

#ifdef NEED_SHORT_EXTERNAL_NAMES
#define jpeg_make_d_derived_tbl	jMkDDerived
#define jpeg_fill_bit_buffer	jFilBitBuf
#define jpeg_huff_decode	jHufDecode
#endif /* NEED_SHORT_EXTERNAL_NAMES */

#define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */

typedef struct {
  INT32 maxcode[18];		/* largest code of length k (-1 if none) */
  INT32 valoffset[17];		/* huffval[] offset for codes of length k */

  JHUFF_TBL *pub;

  int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
  UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
} d_derived_tbl;

EXTERN(void) jpeg_make_d_derived_tbl
	JPP((j_decompress_ptr cinfo, boolean isDC, int tblno,
		 d_derived_tbl ** pdtbl));

typedef INT32 bit_buf_type;	/* type of bit-extraction buffer */
#define BIT_BUF_SIZE  32	/* size of buffer in bits */

typedef struct {		/* Bitreading state saved across MCUs */
  bit_buf_type get_buffer;	/* current bit-extraction buffer */
  int bits_left;		/* # of unused bits in it */
} bitread_perm_state;

typedef struct {		/* Bitreading working state within an MCU */
  const JOCTET * next_input_byte; /* => next byte to read from source */
  size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */
  bit_buf_type get_buffer;	/* current bit-extraction buffer */
  int bits_left;		/* # of unused bits in it */
  j_decompress_ptr cinfo;	/* back link to decompress master record */
} bitread_working_state;

#define BITREAD_STATE_VARS  \
	register bit_buf_type get_buffer;  \
	register int bits_left;  \
	bitread_working_state br_state

#define BITREAD_LOAD_STATE(cinfop,permstate)  \
	br_state.cinfo = cinfop; \
	br_state.next_input_byte = cinfop->src->next_input_byte; \
	br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
	get_buffer = permstate.get_buffer; \
	bits_left = permstate.bits_left;

#define BITREAD_SAVE_STATE(cinfop,permstate)  \
	cinfop->src->next_input_byte = br_state.next_input_byte; \
	cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
	permstate.get_buffer = get_buffer; \
	permstate.bits_left = bits_left

#define CHECK_BIT_BUFFER(state,nbits,action) \
	{ if (bits_left < (nbits)) {  \
		if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
		  { action; }  \
		get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }

#define GET_BITS(nbits) \
	(((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))

#define PEEK_BITS(nbits) \
	(((int) (get_buffer >> (bits_left -  (nbits)))) & ((1<<(nbits))-1))

#define DROP_BITS(nbits) \
	(bits_left -= (nbits))

EXTERN(boolean) jpeg_fill_bit_buffer
	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
		 register int bits_left, int nbits));

#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
{ register int nb, look; \
  if (bits_left < HUFF_LOOKAHEAD) { \
	if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
	get_buffer = state.get_buffer; bits_left = state.bits_left; \
	if (bits_left < HUFF_LOOKAHEAD) { \
	  nb = 1; goto slowlabel; \
	} \
  } \
  look = PEEK_BITS(HUFF_LOOKAHEAD); \
  if ((nb = htbl->look_nbits[look]) != 0) { \
	DROP_BITS(nb); \
	result = htbl->look_sym[look]; \
  } else { \
	nb = HUFF_LOOKAHEAD+1; \
slowlabel: \
	if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
	{ failaction; } \
	get_buffer = state.get_buffer; bits_left = state.bits_left; \
  } \
}

EXTERN(int) jpeg_huff_decode
	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
		 register int bits_left, d_derived_tbl * htbl, int min_bits));

#endif
/*** End of inlined file: jdhuff.h ***/

		/* Declarations shared with jdphuff.c */

typedef struct {
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
} savable_state2;

#ifndef NO_STRUCT_ASSIGN
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
#else
#if MAX_COMPS_IN_SCAN == 4
#define ASSIGN_STATE(dest,src)  \
	((dest).last_dc_val[0] = (src).last_dc_val[0], \
	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
	 (dest).last_dc_val[3] = (src).last_dc_val[3])
#endif
#endif

typedef struct {
  struct jpeg_entropy_decoder pub; /* public fields */

  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
  savable_state2 saved;		/* Other state at start of MCU */

  unsigned int restarts_to_go;	/* MCUs left in this restart interval */

  d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
  d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];

  d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
  d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
  boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
  boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
} huff_entropy_decoder2;

typedef huff_entropy_decoder2 * huff_entropy_ptr2;

METHODDEF(void)
start_pass_huff_decoder (j_decompress_ptr cinfo)
{
  huff_entropy_ptr2 entropy = (huff_entropy_ptr2) cinfo->entropy;
  int ci, blkn, dctbl, actbl;
  jpeg_component_info * compptr;

  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
	  cinfo->Ah != 0 || cinfo->Al != 0)
	WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	dctbl = compptr->dc_tbl_no;
	actbl = compptr->ac_tbl_no;
	jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
				& entropy->dc_derived_tbls[dctbl]);
	jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
				& entropy->ac_derived_tbls[actbl]);
	entropy->saved.last_dc_val[ci] = 0;
  }

  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
	ci = cinfo->MCU_membership[blkn];
	compptr = cinfo->cur_comp_info[ci];
	entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
	entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
	if (compptr->component_needed) {
	  entropy->dc_needed[blkn] = TRUE;
	  entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
	} else {
	  entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
	}
  }

  entropy->bitstate.bits_left = 0;
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  entropy->pub.insufficient_data = FALSE;

  entropy->restarts_to_go = cinfo->restart_interval;
}

GLOBAL(void)
jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
			 d_derived_tbl ** pdtbl)
{
  JHUFF_TBL *htbl;
  d_derived_tbl *dtbl;
  int p, i, l, si, numsymbols;
  int lookbits, ctr;
  char huffsize[257];
  unsigned int huffcode[257];
  unsigned int code;

  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  htbl =
	isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  if (htbl == NULL)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);

  if (*pdtbl == NULL)
	*pdtbl = (d_derived_tbl *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  SIZEOF(d_derived_tbl));
  dtbl = *pdtbl;
  dtbl->pub = htbl;		/* fill in back link */

  p = 0;
  for (l = 1; l <= 16; l++) {
	i = (int) htbl->bits[l];
	if (i < 0 || p + i > 256)	/* protect against table overrun */
	  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
	while (i--)
	  huffsize[p++] = (char) l;
  }
  huffsize[p] = 0;
  numsymbols = p;

  code = 0;
  si = huffsize[0];
  p = 0;
  while (huffsize[p]) {
	while (((int) huffsize[p]) == si) {
	  huffcode[p++] = code;
	  code++;
	}
	if (((INT32) code) >= (((INT32) 1) << si))
	  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
	code <<= 1;
	si++;
  }

  p = 0;
  for (l = 1; l <= 16; l++) {
	if (htbl->bits[l]) {
	  dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
	  p += htbl->bits[l];
	  dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
	} else {
	  dtbl->maxcode[l] = -1;	/* -1 if no codes of this length */
	}
  }
  dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */

  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));

  p = 0;
  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
	for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
	  lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
	  for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
	dtbl->look_nbits[lookbits] = l;
	dtbl->look_sym[lookbits] = htbl->huffval[p];
	lookbits++;
	  }
	}
  }

  if (isDC) {
	for (i = 0; i < numsymbols; i++) {
	  int sym = htbl->huffval[i];
	  if (sym < 0 || sym > 15)
	ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
	}
  }
}

#ifdef SLOW_SHIFT_32
#define MIN_GET_BITS  15	/* minimum allowable value */
#else
#define MIN_GET_BITS  (BIT_BUF_SIZE-7)
#endif

GLOBAL(boolean)
jpeg_fill_bit_buffer (bitread_working_state * state,
			  register bit_buf_type get_buffer, register int bits_left,
			  int nbits)
{
  register const JOCTET * next_input_byte = state->next_input_byte;
  register size_t bytes_in_buffer = state->bytes_in_buffer;
  j_decompress_ptr cinfo = state->cinfo;

  if (cinfo->unread_marker == 0) {	/* cannot advance past a marker */
	while (bits_left < MIN_GET_BITS) {
	  register int c;

	  if (bytes_in_buffer == 0) {
	if (! (*cinfo->src->fill_input_buffer) (cinfo))
	  return FALSE;
	next_input_byte = cinfo->src->next_input_byte;
	bytes_in_buffer = cinfo->src->bytes_in_buffer;
	  }
	  bytes_in_buffer--;
	  c = GETJOCTET(*next_input_byte++);

	  if (c == 0xFF) {
	do {
	  if (bytes_in_buffer == 0) {
		if (! (*cinfo->src->fill_input_buffer) (cinfo))
		  return FALSE;
		next_input_byte = cinfo->src->next_input_byte;
		bytes_in_buffer = cinfo->src->bytes_in_buffer;
	  }
	  bytes_in_buffer--;
	  c = GETJOCTET(*next_input_byte++);
	} while (c == 0xFF);

	if (c == 0) {
	  c = 0xFF;
	} else {
	  cinfo->unread_marker = c;
	  goto no_more_bytes;
	}
	  }

	  get_buffer = (get_buffer << 8) | c;
	  bits_left += 8;
	} /* end while */
  } else {
  no_more_bytes:
	if (nbits > bits_left) {
	  if (! cinfo->entropy->insufficient_data) {
	WARNMS(cinfo, JWRN_HIT_MARKER);
	cinfo->entropy->insufficient_data = TRUE;
	  }
	  get_buffer <<= MIN_GET_BITS - bits_left;
	  bits_left = MIN_GET_BITS;
	}
  }

  state->next_input_byte = next_input_byte;
  state->bytes_in_buffer = bytes_in_buffer;
  state->get_buffer = get_buffer;
  state->bits_left = bits_left;

  return TRUE;
}

GLOBAL(int)
jpeg_huff_decode (bitread_working_state * state,
		  register bit_buf_type get_buffer, register int bits_left,
		  d_derived_tbl * htbl, int min_bits)
{
  register int l = min_bits;
  register INT32 code;

  CHECK_BIT_BUFFER(*state, l, return -1);
  code = GET_BITS(l);

  while (code > htbl->maxcode[l]) {
	code <<= 1;
	CHECK_BIT_BUFFER(*state, 1, return -1);
	code |= GET_BITS(1);
	l++;
  }

  state->get_buffer = get_buffer;
  state->bits_left = bits_left;

  if (l > 16) {
	WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
	return 0;			/* fake a zero as the safest result */
  }

  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
}

LOCAL(boolean)
process_restart (j_decompress_ptr cinfo)
{
  huff_entropy_ptr2 entropy = (huff_entropy_ptr2) cinfo->entropy;
  int ci;

  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  entropy->bitstate.bits_left = 0;

  if (! (*cinfo->marker->read_restart_marker) (cinfo))
	return FALSE;

  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
	entropy->saved.last_dc_val[ci] = 0;

  entropy->restarts_to_go = cinfo->restart_interval;

  if (cinfo->unread_marker == 0)
	entropy->pub.insufficient_data = FALSE;

  return TRUE;
}

METHODDEF(boolean)
decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
  huff_entropy_ptr2 entropy = (huff_entropy_ptr2) cinfo->entropy;
  int blkn;
  BITREAD_STATE_VARS;
  savable_state2 state;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0)
	  if (! process_restart(cinfo))
	return FALSE;
  }

  if (! entropy->pub.insufficient_data) {

	BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
	ASSIGN_STATE(state, entropy->saved);

	for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
	  JBLOCKROW block = MCU_data[blkn];
	  d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
	  d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
	  register int s, k, r;

	  HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
	  if (s) {
	CHECK_BIT_BUFFER(br_state, s, return FALSE);
	r = GET_BITS(s);
	s = HUFF_EXTEND(r, s);
	  }

	  if (entropy->dc_needed[blkn]) {
	int ci = cinfo->MCU_membership[blkn];
	s += state.last_dc_val[ci];
	state.last_dc_val[ci] = s;
	(*block)[0] = (JCOEF) s;
	  }

	  if (entropy->ac_needed[blkn]) {

	for (k = 1; k < DCTSIZE2; k++) {
	  HUFF_DECODE(s, br_state, actbl, return FALSE, label2);

	  r = s >> 4;
	  s &= 15;

	  if (s) {
		k += r;
		CHECK_BIT_BUFFER(br_state, s, return FALSE);
		r = GET_BITS(s);
		s = HUFF_EXTEND(r, s);
		(*block)[jpeg_natural_order[k]] = (JCOEF) s;
	  } else {
		if (r != 15)
		  break;
		k += 15;
	  }
	}

	  } else {

	for (k = 1; k < DCTSIZE2; k++) {
	  HUFF_DECODE(s, br_state, actbl, return FALSE, label3);

	  r = s >> 4;
	  s &= 15;

	  if (s) {
		k += r;
		CHECK_BIT_BUFFER(br_state, s, return FALSE);
		DROP_BITS(s);
	  } else {
		if (r != 15)
		  break;
		k += 15;
	  }
	}

	  }
	}

	BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
	ASSIGN_STATE(entropy->saved, state);
  }

  entropy->restarts_to_go--;

  return TRUE;
}

GLOBAL(void)
jinit_huff_decoder (j_decompress_ptr cinfo)
{
  huff_entropy_ptr2 entropy;
  int i;

  entropy = (huff_entropy_ptr2)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(huff_entropy_decoder2));
  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  entropy->pub.start_pass = start_pass_huff_decoder;
  entropy->pub.decode_mcu = decode_mcu;

  for (i = 0; i < NUM_HUFF_TBLS; i++) {
	entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  }
}
/*** End of inlined file: jdhuff.c ***/



/*** Start of inlined file: jdinput.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_input_controller pub; /* public fields */

  boolean inheaders;		/* TRUE until first SOS is reached */
} my_input_controller;

typedef my_input_controller * my_inputctl_ptr;

METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));

LOCAL(void)
initial_setup2 (j_decompress_ptr cinfo)
{
  int ci;
  jpeg_component_info *compptr;

  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
	  (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
	ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);

  if (cinfo->data_precision != BITS_IN_JSAMPLE)
	ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);

  if (cinfo->num_components > MAX_COMPONENTS)
	ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
		 MAX_COMPONENTS);

  cinfo->max_h_samp_factor = 1;
  cinfo->max_v_samp_factor = 1;
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
	  ERREXIT(cinfo, JERR_BAD_SAMPLING);
	cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
				   compptr->h_samp_factor);
	cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
				   compptr->v_samp_factor);
  }

  cinfo->min_DCT_scaled_size = DCTSIZE;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	compptr->DCT_scaled_size = DCTSIZE;
	compptr->width_in_blocks = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
			(long) (cinfo->max_h_samp_factor * DCTSIZE));
	compptr->height_in_blocks = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
			(long) (cinfo->max_v_samp_factor * DCTSIZE));
	compptr->downsampled_width = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
			(long) cinfo->max_h_samp_factor);
	compptr->downsampled_height = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
			(long) cinfo->max_v_samp_factor);
	compptr->component_needed = TRUE;
	compptr->quant_table = NULL;
  }

  cinfo->total_iMCU_rows = (JDIMENSION)
	jdiv_round_up((long) cinfo->image_height,
		  (long) (cinfo->max_v_samp_factor*DCTSIZE));

  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
	cinfo->inputctl->has_multiple_scans = TRUE;
  else
	cinfo->inputctl->has_multiple_scans = FALSE;
}

LOCAL(void)
per_scan_setup2 (j_decompress_ptr cinfo)
{
  int ci, mcublks, tmp;
  jpeg_component_info *compptr;

  if (cinfo->comps_in_scan == 1) {

	compptr = cinfo->cur_comp_info[0];

	cinfo->MCUs_per_row = compptr->width_in_blocks;
	cinfo->MCU_rows_in_scan = compptr->height_in_blocks;

	compptr->MCU_width = 1;
	compptr->MCU_height = 1;
	compptr->MCU_blocks = 1;
	compptr->MCU_sample_width = compptr->DCT_scaled_size;
	compptr->last_col_width = 1;
	tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
	if (tmp == 0) tmp = compptr->v_samp_factor;
	compptr->last_row_height = tmp;

	cinfo->blocks_in_MCU = 1;
	cinfo->MCU_membership[0] = 0;

  } else {

	if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
	  ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
		   MAX_COMPS_IN_SCAN);

	cinfo->MCUs_per_row = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width,
			(long) (cinfo->max_h_samp_factor*DCTSIZE));
	cinfo->MCU_rows_in_scan = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height,
			(long) (cinfo->max_v_samp_factor*DCTSIZE));

	cinfo->blocks_in_MCU = 0;

	for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	  compptr = cinfo->cur_comp_info[ci];
	  compptr->MCU_width = compptr->h_samp_factor;
	  compptr->MCU_height = compptr->v_samp_factor;
	  compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
	  compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
	  tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
	  if (tmp == 0) tmp = compptr->MCU_width;
	  compptr->last_col_width = tmp;
	  tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
	  if (tmp == 0) tmp = compptr->MCU_height;
	  compptr->last_row_height = tmp;
	  mcublks = compptr->MCU_blocks;
	  if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
	  while (mcublks-- > 0) {
	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
	  }
	}

  }
}

LOCAL(void)
latch_quant_tables (j_decompress_ptr cinfo)
{
  int ci, qtblno;
  jpeg_component_info *compptr;
  JQUANT_TBL * qtbl;

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	if (compptr->quant_table != NULL)
	  continue;
	qtblno = compptr->quant_tbl_no;
	if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
	cinfo->quant_tbl_ptrs[qtblno] == NULL)
	  ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
	qtbl = (JQUANT_TBL *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  SIZEOF(JQUANT_TBL));
	MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
	compptr->quant_table = qtbl;
  }
}

METHODDEF(void)
start_input_pass2 (j_decompress_ptr cinfo)
{
  per_scan_setup2(cinfo);
  latch_quant_tables(cinfo);
  (*cinfo->entropy->start_pass) (cinfo);
  (*cinfo->coef->start_input_pass) (cinfo);
  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
}

METHODDEF(void)
finish_input_pass (j_decompress_ptr cinfo)
{
  cinfo->inputctl->consume_input = consume_markers;
}

METHODDEF(int)
consume_markers (j_decompress_ptr cinfo)
{
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  int val;

  if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
	return JPEG_REACHED_EOI;

  val = (*cinfo->marker->read_markers) (cinfo);

  switch (val) {
  case JPEG_REACHED_SOS:	/* Found SOS */
	if (inputctl->inheaders) {	/* 1st SOS */
	  initial_setup2(cinfo);
	  inputctl->inheaders = FALSE;
	} else {			/* 2nd or later SOS marker */
	  if (! inputctl->pub.has_multiple_scans)
	ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
	  start_input_pass2(cinfo);
	}
	break;
  case JPEG_REACHED_EOI:	/* Found EOI */
	inputctl->pub.eoi_reached = TRUE;
	if (inputctl->inheaders) {	/* Tables-only datastream, apparently */
	  if (cinfo->marker->saw_SOF)
	ERREXIT(cinfo, JERR_SOF_NO_SOS);
	} else {
	  if (cinfo->output_scan_number > cinfo->input_scan_number)
	cinfo->output_scan_number = cinfo->input_scan_number;
	}
	break;
  case JPEG_SUSPENDED:
	break;
  }

  return val;
}

METHODDEF(void)
reset_input_controller (j_decompress_ptr cinfo)
{
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;

  inputctl->pub.consume_input = consume_markers;
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  inputctl->pub.eoi_reached = FALSE;
  inputctl->inheaders = TRUE;
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  (*cinfo->marker->reset_marker_reader) (cinfo);
  cinfo->coef_bits = NULL;
}

GLOBAL(void)
jinit_input_controller (j_decompress_ptr cinfo)
{
  my_inputctl_ptr inputctl;

  inputctl = (my_inputctl_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				SIZEOF(my_input_controller));
  cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
  inputctl->pub.consume_input = consume_markers;
  inputctl->pub.reset_input_controller = reset_input_controller;
  inputctl->pub.start_input_pass = start_input_pass2;
  inputctl->pub.finish_input_pass = finish_input_pass;
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  inputctl->pub.eoi_reached = FALSE;
  inputctl->inheaders = TRUE;
}
/*** End of inlined file: jdinput.c ***/


/*** Start of inlined file: jdmainct.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_d_main_controller pub; /* public fields */

  JSAMPARRAY buffer[MAX_COMPONENTS];

  boolean buffer_full;		/* Have we gotten an iMCU row from decoder? */
  JDIMENSION rowgroup_ctr;	/* counts row groups output to postprocessor */

  JSAMPIMAGE xbuffer[2];	/* pointers to weird pointer lists */

  int whichptr;			/* indicates which pointer set is now in use */
  int context_state;		/* process_data state machine status */
  JDIMENSION rowgroups_avail;	/* row groups available to postprocessor */
  JDIMENSION iMCU_row_ctr;	/* counts iMCU rows to detect image top/bot */
} my_main_controller4;

typedef my_main_controller4 * my_main_ptr4;

#define CTX_PREPARE_FOR_IMCU	0	/* need to prepare for MCU row */
#define CTX_PROCESS_IMCU	1	/* feeding iMCU to postprocessor */
#define CTX_POSTPONED_ROW	2	/* feeding postponed row group */

METHODDEF(void) process_data_simple_main2
	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
		 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
METHODDEF(void) process_data_context_main
	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
		 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
#ifdef QUANT_2PASS_SUPPORTED
METHODDEF(void) process_data_crank_post
	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
		 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
#endif

LOCAL(void)
alloc_funny_pointers (j_decompress_ptr cinfo)
{
  my_main_ptr4 main_ = (my_main_ptr4) cinfo->main;
  int ci, rgroup;
  int M = cinfo->min_DCT_scaled_size;
  jpeg_component_info *compptr;
  JSAMPARRAY xbuf;

  main_->xbuffer[0] = (JSAMPIMAGE)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
  main_->xbuffer[1] = main_->xbuffer[0] + cinfo->num_components;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
	  cinfo->min_DCT_scaled_size; /* height of a row group of component */
	xbuf = (JSAMPARRAY)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
	xbuf += rgroup;		/* want one row group at negative offsets */
	main_->xbuffer[0][ci] = xbuf;
	xbuf += rgroup * (M + 4);
	main_->xbuffer[1][ci] = xbuf;
  }
}

LOCAL(void)
make_funny_pointers (j_decompress_ptr cinfo)
{
  my_main_ptr4 main_ = (my_main_ptr4) cinfo->main;
  int ci, i, rgroup;
  int M = cinfo->min_DCT_scaled_size;
  jpeg_component_info *compptr;
  JSAMPARRAY buf, xbuf0, xbuf1;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
	  cinfo->min_DCT_scaled_size; /* height of a row group of component */
	xbuf0 = main_->xbuffer[0][ci];
	xbuf1 = main_->xbuffer[1][ci];
	buf = main_->buffer[ci];
	for (i = 0; i < rgroup * (M + 2); i++) {
	  xbuf0[i] = xbuf1[i] = buf[i];
	}
	for (i = 0; i < rgroup * 2; i++) {
	  xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
	  xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
	}
	for (i = 0; i < rgroup; i++) {
	  xbuf0[i - rgroup] = xbuf0[0];
	}
  }
}

LOCAL(void)
set_wraparound_pointers (j_decompress_ptr cinfo)
{
  my_main_ptr4 main_ = (my_main_ptr4) cinfo->main;
  int ci, i, rgroup;
  int M = cinfo->min_DCT_scaled_size;
  jpeg_component_info *compptr;
  JSAMPARRAY xbuf0, xbuf1;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
	  cinfo->min_DCT_scaled_size; /* height of a row group of component */
	xbuf0 = main_->xbuffer[0][ci];
	xbuf1 = main_->xbuffer[1][ci];
	for (i = 0; i < rgroup; i++) {
	  xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
	  xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
	  xbuf0[rgroup*(M+2) + i] = xbuf0[i];
	  xbuf1[rgroup*(M+2) + i] = xbuf1[i];
	}
  }
}

LOCAL(void)
set_bottom_pointers (j_decompress_ptr cinfo)
{
  my_main_ptr4 main_ = (my_main_ptr4) cinfo->main;
  int ci, i, rgroup, iMCUheight, rows_left;
  jpeg_component_info *compptr;
  JSAMPARRAY xbuf;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
	rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
	rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
	if (rows_left == 0) rows_left = iMCUheight;
	if (ci == 0) {
	  main_->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
	}
	xbuf = main_->xbuffer[main_->whichptr][ci];
	for (i = 0; i < rgroup * 2; i++) {
	  xbuf[rows_left + i] = xbuf[rows_left-1];
	}
  }
}

METHODDEF(void)
start_pass_main2 (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
{
  my_main_ptr4 main_ = (my_main_ptr4) cinfo->main;

  switch (pass_mode) {
  case JBUF_PASS_THRU:
	if (cinfo->upsample->need_context_rows) {
	  main_->pub.process_data = process_data_context_main;
	  make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
	  main_->whichptr = 0;	/* Read first iMCU row into xbuffer[0] */
	  main_->context_state = CTX_PREPARE_FOR_IMCU;
	  main_->iMCU_row_ctr = 0;
	} else {
	  main_->pub.process_data = process_data_simple_main2;
	}
	main_->buffer_full = FALSE;	/* Mark buffer empty */
	main_->rowgroup_ctr = 0;
	break;
#ifdef QUANT_2PASS_SUPPORTED
  case JBUF_CRANK_DEST:
	main_->pub.process_data = process_data_crank_post;
	break;
#endif
  default:
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	break;
  }
}

METHODDEF(void)
process_data_simple_main2 (j_decompress_ptr cinfo,
			  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
			  JDIMENSION out_rows_avail)
{
  my_main_ptr4 main_ = (my_main_ptr4) cinfo->main;
  JDIMENSION rowgroups_avail;

  if (! main_->buffer_full) {
	if (! (*cinfo->coef->decompress_data) (cinfo, main_->buffer))
	  return;			/* suspension forced, can do nothing more */
	main_->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
  }

  rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;

  (*cinfo->post->post_process_data) (cinfo, main_->buffer,
					 &main_->rowgroup_ctr, rowgroups_avail,
					 output_buf, out_row_ctr, out_rows_avail);

  if (main_->rowgroup_ctr >= rowgroups_avail) {
	main_->buffer_full = FALSE;
	main_->rowgroup_ctr = 0;
  }
}

METHODDEF(void)
process_data_context_main (j_decompress_ptr cinfo,
			   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
			   JDIMENSION out_rows_avail)
{
  my_main_ptr4 main_ = (my_main_ptr4) cinfo->main;

  if (! main_->buffer_full) {
	if (! (*cinfo->coef->decompress_data) (cinfo,
					   main_->xbuffer[main_->whichptr]))
	  return;			/* suspension forced, can do nothing more */
	main_->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
	main_->iMCU_row_ctr++;	/* count rows received */
  }

  switch (main_->context_state) {
  case CTX_POSTPONED_ROW:
	(*cinfo->post->post_process_data) (cinfo, main_->xbuffer[main_->whichptr],
			&main_->rowgroup_ctr, main_->rowgroups_avail,
			output_buf, out_row_ctr, out_rows_avail);
	if (main_->rowgroup_ctr < main_->rowgroups_avail)
	  return;			/* Need to suspend */
	main_->context_state = CTX_PREPARE_FOR_IMCU;
	if (*out_row_ctr >= out_rows_avail)
	  return;			/* Postprocessor exactly filled output buf */
  case CTX_PREPARE_FOR_IMCU:
	main_->rowgroup_ctr = 0;
	main_->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
	if (main_->iMCU_row_ctr == cinfo->total_iMCU_rows)
	  set_bottom_pointers(cinfo);
	main_->context_state = CTX_PROCESS_IMCU;
  case CTX_PROCESS_IMCU:
	(*cinfo->post->post_process_data) (cinfo, main_->xbuffer[main_->whichptr],
			&main_->rowgroup_ctr, main_->rowgroups_avail,
			output_buf, out_row_ctr, out_rows_avail);
	if (main_->rowgroup_ctr < main_->rowgroups_avail)
	  return;			/* Need to suspend */
	if (main_->iMCU_row_ctr == 1)
	  set_wraparound_pointers(cinfo);
	main_->whichptr ^= 1;	/* 0=>1 or 1=>0 */
	main_->buffer_full = FALSE;
	main_->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
	main_->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
	main_->context_state = CTX_POSTPONED_ROW;
  }
}

#ifdef QUANT_2PASS_SUPPORTED

METHODDEF(void)
process_data_crank_post (j_decompress_ptr cinfo,
			 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
			 JDIMENSION out_rows_avail)
{
  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
					 (JDIMENSION *) NULL, (JDIMENSION) 0,
					 output_buf, out_row_ctr, out_rows_avail);
}

#endif /* QUANT_2PASS_SUPPORTED */

GLOBAL(void)
jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
{
  my_main_ptr4 main_;
  int ci, rgroup, ngroups;
  jpeg_component_info *compptr;

  main_ = (my_main_ptr4)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_main_controller4));
  cinfo->main = (struct jpeg_d_main_controller *) main_;
  main_->pub.start_pass = start_pass_main2;

  if (need_full_buffer)		/* shouldn't happen */
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);

  if (cinfo->upsample->need_context_rows) {
	if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
	  ERREXIT(cinfo, JERR_NOTIMPL);
	alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
	ngroups = cinfo->min_DCT_scaled_size + 2;
  } else {
	ngroups = cinfo->min_DCT_scaled_size;
  }

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
	  cinfo->min_DCT_scaled_size; /* height of a row group of component */
	main_->buffer[ci] = (*cinfo->mem->alloc_sarray)
			((j_common_ptr) cinfo, JPOOL_IMAGE,
			 compptr->width_in_blocks * compptr->DCT_scaled_size,
			 (JDIMENSION) (rgroup * ngroups));
  }
}
/*** End of inlined file: jdmainct.c ***/


/*** Start of inlined file: jdmarker.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_marker_reader pub; /* public fields */

  jpeg_marker_parser_method process_COM;
  jpeg_marker_parser_method process_APPn[16];

  unsigned int length_limit_COM;
  unsigned int length_limit_APPn[16];

  jpeg_saved_marker_ptr cur_marker;	/* NULL if not processing a marker */
  unsigned int bytes_read;		/* data bytes read so far in marker */
} my_marker_reader;

typedef my_marker_reader * my_marker_ptr2;

#define INPUT_VARS(cinfo)  \
	struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
	const JOCTET * next_input_byte = datasrc->next_input_byte;  \
	size_t bytes_in_buffer = datasrc->bytes_in_buffer

#define INPUT_SYNC(cinfo)  \
	( datasrc->next_input_byte = next_input_byte,  \
	  datasrc->bytes_in_buffer = bytes_in_buffer )

#define INPUT_RELOAD(cinfo)  \
	( next_input_byte = datasrc->next_input_byte,  \
	  bytes_in_buffer = datasrc->bytes_in_buffer )

#define MAKE_BYTE_AVAIL(cinfo,action)  \
	if (bytes_in_buffer == 0) {  \
	  if (! (*datasrc->fill_input_buffer) (cinfo))  \
		{ action; }  \
	  INPUT_RELOAD(cinfo);  \
	}

#define INPUT_BYTE(cinfo,V,action)  \
	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
		  bytes_in_buffer--; \
		  V = GETJOCTET(*next_input_byte++); )

#define INPUT_2BYTES(cinfo,V,action)  \
	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
		  bytes_in_buffer--; \
		  V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
		  MAKE_BYTE_AVAIL(cinfo,action); \
		  bytes_in_buffer--; \
		  V += GETJOCTET(*next_input_byte++); )

LOCAL(boolean)
get_soi (j_decompress_ptr cinfo)
{
  int i;

  TRACEMS(cinfo, 1, JTRC_SOI);

  if (cinfo->marker->saw_SOI)
	ERREXIT(cinfo, JERR_SOI_DUPLICATE);

  for (i = 0; i < NUM_ARITH_TBLS; i++) {
	cinfo->arith_dc_L[i] = 0;
	cinfo->arith_dc_U[i] = 1;
	cinfo->arith_ac_K[i] = 5;
  }
  cinfo->restart_interval = 0;

  cinfo->jpeg_color_space = JCS_UNKNOWN;
  cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */

  cinfo->saw_JFIF_marker = FALSE;
  cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
  cinfo->JFIF_minor_version = 1;
  cinfo->density_unit = 0;
  cinfo->X_density = 1;
  cinfo->Y_density = 1;
  cinfo->saw_Adobe_marker = FALSE;
  cinfo->Adobe_transform = 0;

  cinfo->marker->saw_SOI = TRUE;

  return TRUE;
}

LOCAL(boolean)
get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
{
  INT32 length;
  int c, ci;
  jpeg_component_info * compptr;
  INPUT_VARS(cinfo);

  cinfo->progressive_mode = is_prog;
  cinfo->arith_code = is_arith;

  INPUT_2BYTES(cinfo, length, return FALSE);

  INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
  INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
  INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
  INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);

  length -= 8;

  TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
	   (int) cinfo->image_width, (int) cinfo->image_height,
	   cinfo->num_components);

  if (cinfo->marker->saw_SOF)
	ERREXIT(cinfo, JERR_SOF_DUPLICATE);

  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
	  || cinfo->num_components <= 0)
	ERREXIT(cinfo, JERR_EMPTY_IMAGE);

  if (length != (cinfo->num_components * 3))
	ERREXIT(cinfo, JERR_BAD_LENGTH);

  if (cinfo->comp_info == NULL)	/* do only once, even if suspend */
	cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
			((j_common_ptr) cinfo, JPOOL_IMAGE,
			 cinfo->num_components * SIZEOF(jpeg_component_info));

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	compptr->component_index = ci;
	INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
	INPUT_BYTE(cinfo, c, return FALSE);
	compptr->h_samp_factor = (c >> 4) & 15;
	compptr->v_samp_factor = (c	 ) & 15;
	INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);

	TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
		 compptr->component_id, compptr->h_samp_factor,
		 compptr->v_samp_factor, compptr->quant_tbl_no);
  }

  cinfo->marker->saw_SOF = TRUE;

  INPUT_SYNC(cinfo);
  return TRUE;
}

LOCAL(boolean)
get_sos (j_decompress_ptr cinfo)
{
  INT32 length;
  int i, ci, n, c, cc;
  jpeg_component_info * compptr;
  INPUT_VARS(cinfo);

  if (! cinfo->marker->saw_SOF)
	ERREXIT(cinfo, JERR_SOS_NO_SOF);

  INPUT_2BYTES(cinfo, length, return FALSE);

  INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */

  TRACEMS1(cinfo, 1, JTRC_SOS, n);

  if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
	ERREXIT(cinfo, JERR_BAD_LENGTH);

  cinfo->comps_in_scan = n;

  for (i = 0; i < n; i++) {
	INPUT_BYTE(cinfo, cc, return FALSE);
	INPUT_BYTE(cinfo, c, return FALSE);

	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
	  if (cc == compptr->component_id)
	goto id_found;
	}

	ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);

  id_found:

	cinfo->cur_comp_info[i] = compptr;
	compptr->dc_tbl_no = (c >> 4) & 15;
	compptr->ac_tbl_no = (c	 ) & 15;

	TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
		 compptr->dc_tbl_no, compptr->ac_tbl_no);
  }

  INPUT_BYTE(cinfo, c, return FALSE);
  cinfo->Ss = c;
  INPUT_BYTE(cinfo, c, return FALSE);
  cinfo->Se = c;
  INPUT_BYTE(cinfo, c, return FALSE);
  cinfo->Ah = (c >> 4) & 15;
  cinfo->Al = (c	 ) & 15;

  TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
	   cinfo->Ah, cinfo->Al);

  cinfo->marker->next_restart_num = 0;

  cinfo->input_scan_number++;

  INPUT_SYNC(cinfo);
  return TRUE;
}

#ifdef D_ARITH_CODING_SUPPORTED

LOCAL(boolean)
get_dac (j_decompress_ptr cinfo)
{
  INT32 length;
  int index, val;
  INPUT_VARS(cinfo);

  INPUT_2BYTES(cinfo, length, return FALSE);
  length -= 2;

  while (length > 0) {
	INPUT_BYTE(cinfo, index, return FALSE);
	INPUT_BYTE(cinfo, val, return FALSE);

	length -= 2;

	TRACEMS2(cinfo, 1, JTRC_DAC, index, val);

	if (index < 0 || index >= (2*NUM_ARITH_TBLS))
	  ERREXIT1(cinfo, JERR_DAC_INDEX, index);

	if (index >= NUM_ARITH_TBLS) { /* define AC table */
	  cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
	} else {			/* define DC table */
	  cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
	  cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
	  if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
	ERREXIT1(cinfo, JERR_DAC_VALUE, val);
	}
  }

  if (length != 0)
	ERREXIT(cinfo, JERR_BAD_LENGTH);

  INPUT_SYNC(cinfo);
  return TRUE;
}

#else /* ! D_ARITH_CODING_SUPPORTED */

#define get_dac(cinfo)  skip_variable(cinfo)

#endif /* D_ARITH_CODING_SUPPORTED */

LOCAL(boolean)
get_dht (j_decompress_ptr cinfo)
{
  INT32 length;
  UINT8 bits[17];
  UINT8 huffval[256];
  int i, index, count;
  JHUFF_TBL **htblptr;
  INPUT_VARS(cinfo);

  INPUT_2BYTES(cinfo, length, return FALSE);
  length -= 2;

  while (length > 16) {
	INPUT_BYTE(cinfo, index, return FALSE);

	TRACEMS1(cinfo, 1, JTRC_DHT, index);

	bits[0] = 0;
	count = 0;
	for (i = 1; i <= 16; i++) {
	  INPUT_BYTE(cinfo, bits[i], return FALSE);
	  count += bits[i];
	}

	length -= 1 + 16;

	TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
		 bits[1], bits[2], bits[3], bits[4],
		 bits[5], bits[6], bits[7], bits[8]);
	TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
		 bits[9], bits[10], bits[11], bits[12],
		 bits[13], bits[14], bits[15], bits[16]);

	if (count > 256 || ((INT32) count) > length)
	  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);

	for (i = 0; i < count; i++)
	  INPUT_BYTE(cinfo, huffval[i], return FALSE);

	length -= count;

	if (index & 0x10) {		/* AC table definition */
	  index -= 0x10;
	  htblptr = &cinfo->ac_huff_tbl_ptrs[index];
	} else {			/* DC table definition */
	  htblptr = &cinfo->dc_huff_tbl_ptrs[index];
	}

	if (index < 0 || index >= NUM_HUFF_TBLS)
	  ERREXIT1(cinfo, JERR_DHT_INDEX, index);

	if (*htblptr == NULL)
	  *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);

	MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
	MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  }

  if (length != 0)
	ERREXIT(cinfo, JERR_BAD_LENGTH);

  INPUT_SYNC(cinfo);
  return TRUE;
}

LOCAL(boolean)
get_dqt (j_decompress_ptr cinfo)
{
  INT32 length;
  int n, i, prec;
  unsigned int tmp;
  JQUANT_TBL *quant_ptr;
  INPUT_VARS(cinfo);

  INPUT_2BYTES(cinfo, length, return FALSE);
  length -= 2;

  while (length > 0) {
	INPUT_BYTE(cinfo, n, return FALSE);
	prec = n >> 4;
	n &= 0x0F;

	TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);

	if (n >= NUM_QUANT_TBLS)
	  ERREXIT1(cinfo, JERR_DQT_INDEX, n);

	if (cinfo->quant_tbl_ptrs[n] == NULL)
	  cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
	quant_ptr = cinfo->quant_tbl_ptrs[n];

	for (i = 0; i < DCTSIZE2; i++) {
	  if (prec)
	INPUT_2BYTES(cinfo, tmp, return FALSE);
	  else
	INPUT_BYTE(cinfo, tmp, return FALSE);
	  quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
	}

	if (cinfo->err->trace_level >= 2) {
	  for (i = 0; i < DCTSIZE2; i += 8) {
	TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
		 quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
		 quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
		 quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
		 quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
	  }
	}

	length -= DCTSIZE2+1;
	if (prec) length -= DCTSIZE2;
  }

  if (length != 0)
	ERREXIT(cinfo, JERR_BAD_LENGTH);

  INPUT_SYNC(cinfo);
  return TRUE;
}

LOCAL(boolean)
get_dri (j_decompress_ptr cinfo)
{
  INT32 length;
  unsigned int tmp;
  INPUT_VARS(cinfo);

  INPUT_2BYTES(cinfo, length, return FALSE);

  if (length != 4)
	ERREXIT(cinfo, JERR_BAD_LENGTH);

  INPUT_2BYTES(cinfo, tmp, return FALSE);

  TRACEMS1(cinfo, 1, JTRC_DRI, tmp);

  cinfo->restart_interval = tmp;

  INPUT_SYNC(cinfo);
  return TRUE;
}

#define APP0_DATA_LEN	14	/* Length of interesting data in APP0 */
#define APP14_DATA_LEN	12	/* Length of interesting data in APP14 */
#define APPN_DATA_LEN	14	/* Must be the largest of the above!! */

LOCAL(void)
examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
		  unsigned int datalen, INT32 remaining)
{
  INT32 totallen = (INT32) datalen + remaining;

  if (datalen >= APP0_DATA_LEN &&
	  GETJOCTET(data[0]) == 0x4A &&
	  GETJOCTET(data[1]) == 0x46 &&
	  GETJOCTET(data[2]) == 0x49 &&
	  GETJOCTET(data[3]) == 0x46 &&
	  GETJOCTET(data[4]) == 0) {
	cinfo->saw_JFIF_marker = TRUE;
	cinfo->JFIF_major_version = GETJOCTET(data[5]);
	cinfo->JFIF_minor_version = GETJOCTET(data[6]);
	cinfo->density_unit = GETJOCTET(data[7]);
	cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
	cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
	if (cinfo->JFIF_major_version != 1)
	  WARNMS2(cinfo, JWRN_JFIF_MAJOR,
		  cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
	TRACEMS5(cinfo, 1, JTRC_JFIF,
		 cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
		 cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
	if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
	  TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
		   GETJOCTET(data[12]), GETJOCTET(data[13]));
	totallen -= APP0_DATA_LEN;
	if (totallen !=
	((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
	  TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
  } else if (datalen >= 6 &&
	  GETJOCTET(data[0]) == 0x4A &&
	  GETJOCTET(data[1]) == 0x46 &&
	  GETJOCTET(data[2]) == 0x58 &&
	  GETJOCTET(data[3]) == 0x58 &&
	  GETJOCTET(data[4]) == 0) {
	switch (GETJOCTET(data[5])) {
	case 0x10:
	  TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
	  break;
	case 0x11:
	  TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
	  break;
	case 0x13:
	  TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
	  break;
	default:
	  TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
		   GETJOCTET(data[5]), (int) totallen);
	  break;
	}
  } else {
	TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
  }
}

LOCAL(void)
examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
		   unsigned int datalen, INT32 remaining)
{
  unsigned int version, flags0, flags1, transform;

  if (datalen >= APP14_DATA_LEN &&
	  GETJOCTET(data[0]) == 0x41 &&
	  GETJOCTET(data[1]) == 0x64 &&
	  GETJOCTET(data[2]) == 0x6F &&
	  GETJOCTET(data[3]) == 0x62 &&
	  GETJOCTET(data[4]) == 0x65) {
	version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
	flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
	flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
	transform = GETJOCTET(data[11]);
	TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
	cinfo->saw_Adobe_marker = TRUE;
	cinfo->Adobe_transform = (UINT8) transform;
  } else {
	TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
  }
}

METHODDEF(boolean)
get_interesting_appn (j_decompress_ptr cinfo)
{
  INT32 length;
  JOCTET b[APPN_DATA_LEN];
  unsigned int i, numtoread;
  INPUT_VARS(cinfo);

  INPUT_2BYTES(cinfo, length, return FALSE);
  length -= 2;

  if (length >= APPN_DATA_LEN)
	numtoread = APPN_DATA_LEN;
  else if (length > 0)
	numtoread = (unsigned int) length;
  else
	numtoread = 0;
  for (i = 0; i < numtoread; i++)
	INPUT_BYTE(cinfo, b[i], return FALSE);
  length -= numtoread;

  switch (cinfo->unread_marker) {
  case M_APP0:
	examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
	break;
  case M_APP14:
	examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
	break;
  default:
	ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
	break;
  }

  INPUT_SYNC(cinfo);
  if (length > 0)
	(*cinfo->src->skip_input_data) (cinfo, (long) length);

  return TRUE;
}

#ifdef SAVE_MARKERS_SUPPORTED

METHODDEF(boolean)
save_marker (j_decompress_ptr cinfo)
{
  my_marker_ptr2 marker = (my_marker_ptr2) cinfo->marker;
  jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
  unsigned int bytes_read, data_length;
  JOCTET FAR * data;
  INT32 length = 0;
  INPUT_VARS(cinfo);

  if (cur_marker == NULL) {
	INPUT_2BYTES(cinfo, length, return FALSE);
	length -= 2;
	if (length >= 0) {		/* watch out for bogus length word */
	  unsigned int limit;
	  if (cinfo->unread_marker == (int) M_COM)
	limit = marker->length_limit_COM;
	  else
	limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
	  if ((unsigned int) length < limit)
	limit = (unsigned int) length;
	  cur_marker = (jpeg_saved_marker_ptr)
	(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					SIZEOF(struct jpeg_marker_struct) + limit);
	  cur_marker->next = NULL;
	  cur_marker->marker = (UINT8) cinfo->unread_marker;
	  cur_marker->original_length = (unsigned int) length;
	  cur_marker->data_length = limit;
	  data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
	  marker->cur_marker = cur_marker;
	  marker->bytes_read = 0;
	  bytes_read = 0;
	  data_length = limit;
	} else {
	  bytes_read = data_length = 0;
	  data = NULL;
	}
  } else {
	bytes_read = marker->bytes_read;
	data_length = cur_marker->data_length;
	data = cur_marker->data + bytes_read;
  }

  while (bytes_read < data_length) {
	INPUT_SYNC(cinfo);		/* move the restart point to here */
	marker->bytes_read = bytes_read;
	MAKE_BYTE_AVAIL(cinfo, return FALSE);
	while (bytes_read < data_length && bytes_in_buffer > 0) {
	  *data++ = *next_input_byte++;
	  bytes_in_buffer--;
	  bytes_read++;
	}
  }

  if (cur_marker != NULL) {	/* will be NULL if bogus length word */
	if (cinfo->marker_list == NULL) {
	  cinfo->marker_list = cur_marker;
	} else {
	  jpeg_saved_marker_ptr prev = cinfo->marker_list;
	  while (prev->next != NULL)
	prev = prev->next;
	  prev->next = cur_marker;
	}
	data = cur_marker->data;
	length = cur_marker->original_length - data_length;
  }
  marker->cur_marker = NULL;

  switch (cinfo->unread_marker) {
  case M_APP0:
	examine_app0(cinfo, data, data_length, length);
	break;
  case M_APP14:
	examine_app14(cinfo, data, data_length, length);
	break;
  default:
	TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
		 (int) (data_length + length));
	break;
  }

  INPUT_SYNC(cinfo);		/* do before skip_input_data */
  if (length > 0)
	(*cinfo->src->skip_input_data) (cinfo, (long) length);

  return TRUE;
}

#endif /* SAVE_MARKERS_SUPPORTED */

METHODDEF(boolean)
skip_variable (j_decompress_ptr cinfo)
{
  INT32 length;
  INPUT_VARS(cinfo);

  INPUT_2BYTES(cinfo, length, return FALSE);
  length -= 2;

  TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);

  INPUT_SYNC(cinfo);		/* do before skip_input_data */
  if (length > 0)
	(*cinfo->src->skip_input_data) (cinfo, (long) length);

  return TRUE;
}

LOCAL(boolean)
next_marker (j_decompress_ptr cinfo)
{
  int c;
  INPUT_VARS(cinfo);

  for (;;) {
	INPUT_BYTE(cinfo, c, return FALSE);
	while (c != 0xFF) {
	  cinfo->marker->discarded_bytes++;
	  INPUT_SYNC(cinfo);
	  INPUT_BYTE(cinfo, c, return FALSE);
	}
	do {
	  INPUT_BYTE(cinfo, c, return FALSE);
	} while (c == 0xFF);
	if (c != 0)
	  break;			/* found a valid marker, exit loop */
	cinfo->marker->discarded_bytes += 2;
	INPUT_SYNC(cinfo);
  }

  if (cinfo->marker->discarded_bytes != 0) {
	WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
	cinfo->marker->discarded_bytes = 0;
  }

  cinfo->unread_marker = c;

  INPUT_SYNC(cinfo);
  return TRUE;
}

LOCAL(boolean)
first_marker (j_decompress_ptr cinfo)
{
  int c, c2;
  INPUT_VARS(cinfo);

  INPUT_BYTE(cinfo, c, return FALSE);
  INPUT_BYTE(cinfo, c2, return FALSE);
  if (c != 0xFF || c2 != (int) M_SOI)
	ERREXIT2(cinfo, JERR_NO_SOI, c, c2);

  cinfo->unread_marker = c2;

  INPUT_SYNC(cinfo);
  return TRUE;
}

METHODDEF(int)
read_markers (j_decompress_ptr cinfo)
{
  for (;;) {
	if (cinfo->unread_marker == 0) {
	  if (! cinfo->marker->saw_SOI) {
	if (! first_marker(cinfo))
	  return JPEG_SUSPENDED;
	  } else {
	if (! next_marker(cinfo))
	  return JPEG_SUSPENDED;
	  }
	}
	switch (cinfo->unread_marker) {
	case M_SOI:
	  if (! get_soi(cinfo))
	return JPEG_SUSPENDED;
	  break;

	case M_SOF0:		/* Baseline */
	case M_SOF1:		/* Extended sequential, Huffman */
	  if (! get_sof(cinfo, FALSE, FALSE))
	return JPEG_SUSPENDED;
	  break;

	case M_SOF2:		/* Progressive, Huffman */
	  if (! get_sof(cinfo, TRUE, FALSE))
	return JPEG_SUSPENDED;
	  break;

	case M_SOF9:		/* Extended sequential, arithmetic */
	  if (! get_sof(cinfo, FALSE, TRUE))
	return JPEG_SUSPENDED;
	  break;

	case M_SOF10:		/* Progressive, arithmetic */
	  if (! get_sof(cinfo, TRUE, TRUE))
	return JPEG_SUSPENDED;
	  break;

	case M_SOF3:		/* Lossless, Huffman */
	case M_SOF5:		/* Differential sequential, Huffman */
	case M_SOF6:		/* Differential progressive, Huffman */
	case M_SOF7:		/* Differential lossless, Huffman */
	case M_JPG:			/* Reserved for JPEG extensions */
	case M_SOF11:		/* Lossless, arithmetic */
	case M_SOF13:		/* Differential sequential, arithmetic */
	case M_SOF14:		/* Differential progressive, arithmetic */
	case M_SOF15:		/* Differential lossless, arithmetic */
	  ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
	  break;

	case M_SOS:
	  if (! get_sos(cinfo))
	return JPEG_SUSPENDED;
	  cinfo->unread_marker = 0;	/* processed the marker */
	  return JPEG_REACHED_SOS;

	case M_EOI:
	  TRACEMS(cinfo, 1, JTRC_EOI);
	  cinfo->unread_marker = 0;	/* processed the marker */
	  return JPEG_REACHED_EOI;

	case M_DAC:
	  if (! get_dac(cinfo))
	return JPEG_SUSPENDED;
	  break;

	case M_DHT:
	  if (! get_dht(cinfo))
	return JPEG_SUSPENDED;
	  break;

	case M_DQT:
	  if (! get_dqt(cinfo))
	return JPEG_SUSPENDED;
	  break;

	case M_DRI:
	  if (! get_dri(cinfo))
	return JPEG_SUSPENDED;
	  break;

	case M_APP0:
	case M_APP1:
	case M_APP2:
	case M_APP3:
	case M_APP4:
	case M_APP5:
	case M_APP6:
	case M_APP7:
	case M_APP8:
	case M_APP9:
	case M_APP10:
	case M_APP11:
	case M_APP12:
	case M_APP13:
	case M_APP14:
	case M_APP15:
	  if (! (*((my_marker_ptr2) cinfo->marker)->process_APPn[
		cinfo->unread_marker - (int) M_APP0]) (cinfo))
	return JPEG_SUSPENDED;
	  break;

	case M_COM:
	  if (! (*((my_marker_ptr2) cinfo->marker)->process_COM) (cinfo))
	return JPEG_SUSPENDED;
	  break;

	case M_RST0:		/* these are all parameterless */
	case M_RST1:
	case M_RST2:
	case M_RST3:
	case M_RST4:
	case M_RST5:
	case M_RST6:
	case M_RST7:
	case M_TEM:
	  TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
	  break;

	case M_DNL:			/* Ignore DNL ... perhaps the wrong thing */
	  if (! skip_variable(cinfo))
	return JPEG_SUSPENDED;
	  break;

	default:			/* must be DHP, EXP, JPGn, or RESn */
	  ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
	  break;
	}
	cinfo->unread_marker = 0;
  } /* end loop */
}

METHODDEF(boolean)
read_restart_marker (j_decompress_ptr cinfo)
{
  if (cinfo->unread_marker == 0) {
	if (! next_marker(cinfo))
	  return FALSE;
  }

  if (cinfo->unread_marker ==
	  ((int) M_RST0 + cinfo->marker->next_restart_num)) {
	TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
	cinfo->unread_marker = 0;
  } else {
	if (! (*cinfo->src->resync_to_restart) (cinfo,
						cinfo->marker->next_restart_num))
	  return FALSE;
  }

  cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;

  return TRUE;
}

GLOBAL(boolean)
jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
{
  int marker = cinfo->unread_marker;
  int action = 1;

  WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);

  for (;;) {
	if (marker < (int) M_SOF0)
	  action = 2;		/* invalid marker */
	else if (marker < (int) M_RST0 || marker > (int) M_RST7)
	  action = 3;		/* valid non-restart marker */
	else {
	  if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
	  marker == ((int) M_RST0 + ((desired+2) & 7)))
	action = 3;		/* one of the next two expected restarts */
	  else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
		   marker == ((int) M_RST0 + ((desired-2) & 7)))
	action = 2;		/* a prior restart, so advance */
	  else
	action = 1;		/* desired restart or too far away */
	}
	TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
	switch (action) {
	case 1:
	  cinfo->unread_marker = 0;
	  return TRUE;
	case 2:
	  if (! next_marker(cinfo))
	return FALSE;
	  marker = cinfo->unread_marker;
	  break;
	case 3:
	  return TRUE;
	}
  } /* end loop */
}

METHODDEF(void)
reset_marker_reader (j_decompress_ptr cinfo)
{
  my_marker_ptr2 marker = (my_marker_ptr2) cinfo->marker;

  cinfo->comp_info = NULL;		/* until allocated by get_sof */
  cinfo->input_scan_number = 0;		/* no SOS seen yet */
  cinfo->unread_marker = 0;		/* no pending marker */
  marker->pub.saw_SOI = FALSE;		/* set internal state too */
  marker->pub.saw_SOF = FALSE;
  marker->pub.discarded_bytes = 0;
  marker->cur_marker = NULL;
}

GLOBAL(void)
jinit_marker_reader (j_decompress_ptr cinfo)
{
  my_marker_ptr2 marker;
  int i;

  marker = (my_marker_ptr2)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				SIZEOF(my_marker_reader));
  cinfo->marker = (struct jpeg_marker_reader *) marker;
  marker->pub.reset_marker_reader = reset_marker_reader;
  marker->pub.read_markers = read_markers;
  marker->pub.read_restart_marker = read_restart_marker;
  marker->process_COM = skip_variable;
  marker->length_limit_COM = 0;
  for (i = 0; i < 16; i++) {
	marker->process_APPn[i] = skip_variable;
	marker->length_limit_APPn[i] = 0;
  }
  marker->process_APPn[0] = get_interesting_appn;
  marker->process_APPn[14] = get_interesting_appn;
  reset_marker_reader(cinfo);
}

#ifdef SAVE_MARKERS_SUPPORTED

GLOBAL(void)
jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
		   unsigned int length_limit)
{
  my_marker_ptr2 marker = (my_marker_ptr2) cinfo->marker;
  long maxlength;
  jpeg_marker_parser_method processor;

  maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
  if (((long) length_limit) > maxlength)
	length_limit = (unsigned int) maxlength;

  if (length_limit) {
	processor = save_marker;
	if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
	  length_limit = APP0_DATA_LEN;
	else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
	  length_limit = APP14_DATA_LEN;
  } else {
	processor = skip_variable;
	if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
	  processor = get_interesting_appn;
  }

  if (marker_code == (int) M_COM) {
	marker->process_COM = processor;
	marker->length_limit_COM = length_limit;
  } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
	marker->process_APPn[marker_code - (int) M_APP0] = processor;
	marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
  } else
	ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
}

#endif /* SAVE_MARKERS_SUPPORTED */

GLOBAL(void)
jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
			   jpeg_marker_parser_method routine)
{
  my_marker_ptr2 marker = (my_marker_ptr2) cinfo->marker;

  if (marker_code == (int) M_COM)
	marker->process_COM = routine;
  else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
	marker->process_APPn[marker_code - (int) M_APP0] = routine;
  else
	ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
}
/*** End of inlined file: jdmarker.c ***/


/*** Start of inlined file: jdmaster.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_decomp_master pub; /* public fields */

  int pass_number;		/* # of passes completed */

  boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */

  struct jpeg_color_quantizer * quantizer_1pass;
  struct jpeg_color_quantizer * quantizer_2pass;
} my_decomp_master;

typedef my_decomp_master * my_master_ptr6;

LOCAL(boolean)
use_merged_upsample (j_decompress_ptr cinfo)
{
#ifdef UPSAMPLE_MERGING_SUPPORTED
  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
	return FALSE;
  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
	  cinfo->out_color_space != JCS_RGB ||
	  cinfo->out_color_components != RGB_PIXELSIZE)
	return FALSE;
  if (cinfo->comp_info[0].h_samp_factor != 2 ||
	  cinfo->comp_info[1].h_samp_factor != 1 ||
	  cinfo->comp_info[2].h_samp_factor != 1 ||
	  cinfo->comp_info[0].v_samp_factor >  2 ||
	  cinfo->comp_info[1].v_samp_factor != 1 ||
	  cinfo->comp_info[2].v_samp_factor != 1)
	return FALSE;
  if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
	  cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
	  cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
	return FALSE;
  return TRUE;			/* by golly, it'll work... */
#else
  return FALSE;
#endif
}

GLOBAL(void)
jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
{
#ifdef IDCT_SCALING_SUPPORTED
  int ci;
  jpeg_component_info *compptr;
#endif

  if (cinfo->global_state != DSTATE_READY)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

#ifdef IDCT_SCALING_SUPPORTED

  if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
	cinfo->output_width = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width, 8L);
	cinfo->output_height = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height, 8L);
	cinfo->min_DCT_scaled_size = 1;
  } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
	cinfo->output_width = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width, 4L);
	cinfo->output_height = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height, 4L);
	cinfo->min_DCT_scaled_size = 2;
  } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
	cinfo->output_width = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width, 2L);
	cinfo->output_height = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height, 2L);
	cinfo->min_DCT_scaled_size = 4;
  } else {
	cinfo->output_width = cinfo->image_width;
	cinfo->output_height = cinfo->image_height;
	cinfo->min_DCT_scaled_size = DCTSIZE;
  }
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	int ssize = cinfo->min_DCT_scaled_size;
	while (ssize < DCTSIZE &&
	   (compptr->h_samp_factor * ssize * 2 <=
		cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
	   (compptr->v_samp_factor * ssize * 2 <=
		cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
	  ssize = ssize * 2;
	}
	compptr->DCT_scaled_size = ssize;
  }

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	compptr->downsampled_width = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_width *
			(long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
			(long) (cinfo->max_h_samp_factor * DCTSIZE));
	compptr->downsampled_height = (JDIMENSION)
	  jdiv_round_up((long) cinfo->image_height *
			(long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
			(long) (cinfo->max_v_samp_factor * DCTSIZE));
  }

#else /* !IDCT_SCALING_SUPPORTED */

  cinfo->output_width = cinfo->image_width;
  cinfo->output_height = cinfo->image_height;

#endif /* IDCT_SCALING_SUPPORTED */

  switch (cinfo->out_color_space) {
  case JCS_GRAYSCALE:
	cinfo->out_color_components = 1;
	break;
  case JCS_RGB:
#if RGB_PIXELSIZE != 3
	cinfo->out_color_components = RGB_PIXELSIZE;
	break;
#endif /* else share code with YCbCr */
  case JCS_YCbCr:
	cinfo->out_color_components = 3;
	break;
  case JCS_CMYK:
  case JCS_YCCK:
	cinfo->out_color_components = 4;
	break;
  default:			/* else must be same colorspace as in file */
	cinfo->out_color_components = cinfo->num_components;
	break;
  }
  cinfo->output_components = (cinfo->quantize_colors ? 1 :
				  cinfo->out_color_components);

  if (use_merged_upsample(cinfo))
	cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  else
	cinfo->rec_outbuf_height = 1;
}

LOCAL(void)
prepare_range_limit_table (j_decompress_ptr cinfo)
{
  JSAMPLE * table;
  int i;

  table = (JSAMPLE *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  table += (MAXJSAMPLE+1);	/* allow negative subscripts of simple table */
  cinfo->sample_range_limit = table;
  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  for (i = 0; i <= MAXJSAMPLE; i++)
	table[i] = (JSAMPLE) i;
  table += CENTERJSAMPLE;	/* Point to where post-IDCT table starts */
  for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
	table[i] = MAXJSAMPLE;
  MEMZERO(table + (2 * (MAXJSAMPLE+1)),
	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
	  cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
}

LOCAL(void)
master_selection (j_decompress_ptr cinfo)
{
  my_master_ptr6 master = (my_master_ptr6) cinfo->master;
  boolean use_c_buffer;
  long samplesperrow;
  JDIMENSION jd_samplesperrow;

  jpeg_calc_output_dimensions(cinfo);
  prepare_range_limit_table(cinfo);

  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  jd_samplesperrow = (JDIMENSION) samplesperrow;
  if ((long) jd_samplesperrow != samplesperrow)
	ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);

  master->pass_number = 0;
  master->using_merged_upsample = use_merged_upsample(cinfo);

  master->quantizer_1pass = NULL;
  master->quantizer_2pass = NULL;
  if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
	cinfo->enable_1pass_quant = FALSE;
	cinfo->enable_external_quant = FALSE;
	cinfo->enable_2pass_quant = FALSE;
  }
  if (cinfo->quantize_colors) {
	if (cinfo->raw_data_out)
	  ERREXIT(cinfo, JERR_NOTIMPL);
	if (cinfo->out_color_components != 3) {
	  cinfo->enable_1pass_quant = TRUE;
	  cinfo->enable_external_quant = FALSE;
	  cinfo->enable_2pass_quant = FALSE;
	  cinfo->colormap = NULL;
	} else if (cinfo->colormap != NULL) {
	  cinfo->enable_external_quant = TRUE;
	} else if (cinfo->two_pass_quantize) {
	  cinfo->enable_2pass_quant = TRUE;
	} else {
	  cinfo->enable_1pass_quant = TRUE;
	}

	if (cinfo->enable_1pass_quant) {
#ifdef QUANT_1PASS_SUPPORTED
	  jinit_1pass_quantizer(cinfo);
	  master->quantizer_1pass = cinfo->cquantize;
#else
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
	}

	if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
#ifdef QUANT_2PASS_SUPPORTED
	  jinit_2pass_quantizer(cinfo);
	  master->quantizer_2pass = cinfo->cquantize;
#else
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
	}
  }

  if (! cinfo->raw_data_out) {
	if (master->using_merged_upsample) {
#ifdef UPSAMPLE_MERGING_SUPPORTED
	  jinit_merged_upsampler(cinfo); /* does color conversion too */
#else
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
	} else {
	  jinit_color_deconverter(cinfo);
	  jinit_upsampler(cinfo);
	}
	jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  }
  jinit_inverse_dct(cinfo);
  if (cinfo->arith_code) {
	ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  } else {
	if (cinfo->progressive_mode) {
#ifdef D_PROGRESSIVE_SUPPORTED
	  jinit_phuff_decoder(cinfo);
#else
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
	} else
	  jinit_huff_decoder(cinfo);
  }

  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  jinit_d_coef_controller(cinfo, use_c_buffer);

  if (! cinfo->raw_data_out)
	jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);

  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);

  (*cinfo->inputctl->start_input_pass) (cinfo);

#ifdef D_MULTISCAN_FILES_SUPPORTED
  if (cinfo->progress != NULL && ! cinfo->buffered_image &&
	  cinfo->inputctl->has_multiple_scans) {
	int nscans;
	if (cinfo->progressive_mode) {
	  nscans = 2 + 3 * cinfo->num_components;
	} else {
	  nscans = cinfo->num_components;
	}
	cinfo->progress->pass_counter = 0L;
	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
	cinfo->progress->completed_passes = 0;
	cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
	master->pass_number++;
  }
#endif /* D_MULTISCAN_FILES_SUPPORTED */
}

METHODDEF(void)
prepare_for_output_pass (j_decompress_ptr cinfo)
{
  my_master_ptr6 master = (my_master_ptr6) cinfo->master;

  if (master->pub.is_dummy_pass) {
#ifdef QUANT_2PASS_SUPPORTED
	master->pub.is_dummy_pass = FALSE;
	(*cinfo->cquantize->start_pass) (cinfo, FALSE);
	(*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
	(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif /* QUANT_2PASS_SUPPORTED */
  } else {
	if (cinfo->quantize_colors && cinfo->colormap == NULL) {
	  if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
	cinfo->cquantize = master->quantizer_2pass;
	master->pub.is_dummy_pass = TRUE;
	  } else if (cinfo->enable_1pass_quant) {
	cinfo->cquantize = master->quantizer_1pass;
	  } else {
	ERREXIT(cinfo, JERR_MODE_CHANGE);
	  }
	}
	(*cinfo->idct->start_pass) (cinfo);
	(*cinfo->coef->start_output_pass) (cinfo);
	if (! cinfo->raw_data_out) {
	  if (! master->using_merged_upsample)
	(*cinfo->cconvert->start_pass) (cinfo);
	  (*cinfo->upsample->start_pass) (cinfo);
	  if (cinfo->quantize_colors)
	(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
	  (*cinfo->post->start_pass) (cinfo,
		(master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
	  (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
	}
  }

  if (cinfo->progress != NULL) {
	cinfo->progress->completed_passes = master->pass_number;
	cinfo->progress->total_passes = master->pass_number +
					(master->pub.is_dummy_pass ? 2 : 1);
	if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
	  cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
	}
  }
}

METHODDEF(void)
finish_output_pass (j_decompress_ptr cinfo)
{
  my_master_ptr6 master = (my_master_ptr6) cinfo->master;

  if (cinfo->quantize_colors)
	(*cinfo->cquantize->finish_pass) (cinfo);
  master->pass_number++;
}

#ifdef D_MULTISCAN_FILES_SUPPORTED

GLOBAL(void)
jpeg_new_colormap (j_decompress_ptr cinfo)
{
  my_master_ptr6 master = (my_master_ptr6) cinfo->master;

  if (cinfo->global_state != DSTATE_BUFIMAGE)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
	  cinfo->colormap != NULL) {
	cinfo->cquantize = master->quantizer_2pass;
	(*cinfo->cquantize->new_color_map) (cinfo);
	master->pub.is_dummy_pass = FALSE; /* just in case */
  } else
	ERREXIT(cinfo, JERR_MODE_CHANGE);
}

#endif /* D_MULTISCAN_FILES_SUPPORTED */

GLOBAL(void)
jinit_master_decompress (j_decompress_ptr cinfo)
{
  my_master_ptr6 master;

  master = (my_master_ptr6)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  SIZEOF(my_decomp_master));
  cinfo->master = (struct jpeg_decomp_master *) master;
  master->pub.prepare_for_output_pass = prepare_for_output_pass;
  master->pub.finish_output_pass = finish_output_pass;

  master->pub.is_dummy_pass = FALSE;

  master_selection(cinfo);
}
/*** End of inlined file: jdmaster.c ***/

	#undef FIX

/*** Start of inlined file: jdmerge.c ***/
#define JPEG_INTERNALS

#ifdef UPSAMPLE_MERGING_SUPPORTED

typedef struct {
  struct jpeg_upsampler pub;	/* public fields */

  JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
			   JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
			   JSAMPARRAY output_buf));

  int * Cr_r_tab;		/* => table for Cr to R conversion */
  int * Cb_b_tab;		/* => table for Cb to B conversion */
  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */

  JSAMPROW spare_row;
  boolean spare_full;		/* T if spare buffer is occupied */

  JDIMENSION out_row_width;	/* samples per output row */
  JDIMENSION rows_to_go;	/* counts rows remaining in image */
} my_upsampler;

typedef my_upsampler * my_upsample_ptr;

#define SCALEBITS	16	/* speediest right-shift on some machines */
#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))

LOCAL(void)
build_ycc_rgb_table2 (j_decompress_ptr cinfo)
{
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  int i;
  INT32 x;
  SHIFT_TEMPS

  upsample->Cr_r_tab = (int *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(MAXJSAMPLE+1) * SIZEOF(int));
  upsample->Cb_b_tab = (int *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(MAXJSAMPLE+1) * SIZEOF(int));
  upsample->Cr_g_tab = (INT32 *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(MAXJSAMPLE+1) * SIZEOF(INT32));
  upsample->Cb_g_tab = (INT32 *)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(MAXJSAMPLE+1) * SIZEOF(INT32));

  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
	upsample->Cr_r_tab[i] = (int)
			RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
	upsample->Cb_b_tab[i] = (int)
			RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
	upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
	upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
  }
}

METHODDEF(void)
start_pass_merged_upsample (j_decompress_ptr cinfo)
{
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;

  upsample->spare_full = FALSE;
  upsample->rows_to_go = cinfo->output_height;
}

METHODDEF(void)
merged_2v_upsample (j_decompress_ptr cinfo,
			JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
			JDIMENSION in_row_groups_avail,
			JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
			JDIMENSION out_rows_avail)
{
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  JSAMPROW work_ptrs[2];
  JDIMENSION num_rows;		/* number of rows returned to caller */

  if (upsample->spare_full) {
	jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
			  1, upsample->out_row_width);
	num_rows = 1;
	upsample->spare_full = FALSE;
  } else {
	num_rows = 2;
	if (num_rows > upsample->rows_to_go)
	  num_rows = upsample->rows_to_go;
	out_rows_avail -= *out_row_ctr;
	if (num_rows > out_rows_avail)
	  num_rows = out_rows_avail;
	work_ptrs[0] = output_buf[*out_row_ctr];
	if (num_rows > 1) {
	  work_ptrs[1] = output_buf[*out_row_ctr + 1];
	} else {
	  work_ptrs[1] = upsample->spare_row;
	  upsample->spare_full = TRUE;
	}
	(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  }

  *out_row_ctr += num_rows;
  upsample->rows_to_go -= num_rows;
  if (! upsample->spare_full)
	(*in_row_group_ctr)++;
}

METHODDEF(void)
merged_1v_upsample (j_decompress_ptr cinfo,
			JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
			JDIMENSION in_row_groups_avail,
			JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
			JDIMENSION out_rows_avail)
{
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;

  (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
			 output_buf + *out_row_ctr);
  (*out_row_ctr)++;
  (*in_row_group_ctr)++;
}

METHODDEF(void)
h2v1_merged_upsample (j_decompress_ptr cinfo,
			  JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
			  JSAMPARRAY output_buf)
{
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  register int y, cred, cgreen, cblue;
  int cb, cr;
  register JSAMPROW outptr;
  JSAMPROW inptr0, inptr1, inptr2;
  JDIMENSION col;
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  int * Crrtab = upsample->Cr_r_tab;
  int * Cbbtab = upsample->Cb_b_tab;
  INT32 * Crgtab = upsample->Cr_g_tab;
  INT32 * Cbgtab = upsample->Cb_g_tab;
  SHIFT_TEMPS

  inptr0 = input_buf[0][in_row_group_ctr];
  inptr1 = input_buf[1][in_row_group_ctr];
  inptr2 = input_buf[2][in_row_group_ctr];
  outptr = output_buf[0];
  for (col = cinfo->output_width >> 1; col > 0; col--) {
	cb = GETJSAMPLE(*inptr1++);
	cr = GETJSAMPLE(*inptr2++);
	cred = Crrtab[cr];
	cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
	cblue = Cbbtab[cb];
	y  = GETJSAMPLE(*inptr0++);
	outptr[RGB_RED] =   range_limit[y + cred];
	outptr[RGB_GREEN] = range_limit[y + cgreen];
	outptr[RGB_BLUE] =  range_limit[y + cblue];
	outptr += RGB_PIXELSIZE;
	y  = GETJSAMPLE(*inptr0++);
	outptr[RGB_RED] =   range_limit[y + cred];
	outptr[RGB_GREEN] = range_limit[y + cgreen];
	outptr[RGB_BLUE] =  range_limit[y + cblue];
	outptr += RGB_PIXELSIZE;
  }
  if (cinfo->output_width & 1) {
	cb = GETJSAMPLE(*inptr1);
	cr = GETJSAMPLE(*inptr2);
	cred = Crrtab[cr];
	cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
	cblue = Cbbtab[cb];
	y  = GETJSAMPLE(*inptr0);
	outptr[RGB_RED] =   range_limit[y + cred];
	outptr[RGB_GREEN] = range_limit[y + cgreen];
	outptr[RGB_BLUE] =  range_limit[y + cblue];
  }
}

METHODDEF(void)
h2v2_merged_upsample (j_decompress_ptr cinfo,
			  JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
			  JSAMPARRAY output_buf)
{
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  register int y, cred, cgreen, cblue;
  int cb, cr;
  register JSAMPROW outptr0, outptr1;
  JSAMPROW inptr00, inptr01, inptr1, inptr2;
  JDIMENSION col;
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  int * Crrtab = upsample->Cr_r_tab;
  int * Cbbtab = upsample->Cb_b_tab;
  INT32 * Crgtab = upsample->Cr_g_tab;
  INT32 * Cbgtab = upsample->Cb_g_tab;
  SHIFT_TEMPS

  inptr00 = input_buf[0][in_row_group_ctr*2];
  inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
  inptr1 = input_buf[1][in_row_group_ctr];
  inptr2 = input_buf[2][in_row_group_ctr];
  outptr0 = output_buf[0];
  outptr1 = output_buf[1];
  for (col = cinfo->output_width >> 1; col > 0; col--) {
	cb = GETJSAMPLE(*inptr1++);
	cr = GETJSAMPLE(*inptr2++);
	cred = Crrtab[cr];
	cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
	cblue = Cbbtab[cb];
	y  = GETJSAMPLE(*inptr00++);
	outptr0[RGB_RED] =   range_limit[y + cred];
	outptr0[RGB_GREEN] = range_limit[y + cgreen];
	outptr0[RGB_BLUE] =  range_limit[y + cblue];
	outptr0 += RGB_PIXELSIZE;
	y  = GETJSAMPLE(*inptr00++);
	outptr0[RGB_RED] =   range_limit[y + cred];
	outptr0[RGB_GREEN] = range_limit[y + cgreen];
	outptr0[RGB_BLUE] =  range_limit[y + cblue];
	outptr0 += RGB_PIXELSIZE;
	y  = GETJSAMPLE(*inptr01++);
	outptr1[RGB_RED] =   range_limit[y + cred];
	outptr1[RGB_GREEN] = range_limit[y + cgreen];
	outptr1[RGB_BLUE] =  range_limit[y + cblue];
	outptr1 += RGB_PIXELSIZE;
	y  = GETJSAMPLE(*inptr01++);
	outptr1[RGB_RED] =   range_limit[y + cred];
	outptr1[RGB_GREEN] = range_limit[y + cgreen];
	outptr1[RGB_BLUE] =  range_limit[y + cblue];
	outptr1 += RGB_PIXELSIZE;
  }
  if (cinfo->output_width & 1) {
	cb = GETJSAMPLE(*inptr1);
	cr = GETJSAMPLE(*inptr2);
	cred = Crrtab[cr];
	cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
	cblue = Cbbtab[cb];
	y  = GETJSAMPLE(*inptr00);
	outptr0[RGB_RED] =   range_limit[y + cred];
	outptr0[RGB_GREEN] = range_limit[y + cgreen];
	outptr0[RGB_BLUE] =  range_limit[y + cblue];
	y  = GETJSAMPLE(*inptr01);
	outptr1[RGB_RED] =   range_limit[y + cred];
	outptr1[RGB_GREEN] = range_limit[y + cgreen];
	outptr1[RGB_BLUE] =  range_limit[y + cblue];
  }
}

GLOBAL(void)
jinit_merged_upsampler (j_decompress_ptr cinfo)
{
  my_upsample_ptr upsample;

  upsample = (my_upsample_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_upsampler));
  cinfo->upsample = (struct jpeg_upsampler *) upsample;
  upsample->pub.start_pass = start_pass_merged_upsample;
  upsample->pub.need_context_rows = FALSE;

  upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;

  if (cinfo->max_v_samp_factor == 2) {
	upsample->pub.upsample = merged_2v_upsample;
	upsample->upmethod = h2v2_merged_upsample;
	upsample->spare_row = (JSAMPROW)
	  (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
		(size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
  } else {
	upsample->pub.upsample = merged_1v_upsample;
	upsample->upmethod = h2v1_merged_upsample;
	upsample->spare_row = NULL;
  }

  build_ycc_rgb_table2(cinfo);
}

#endif /* UPSAMPLE_MERGING_SUPPORTED */
/*** End of inlined file: jdmerge.c ***/


	#undef ASSIGN_STATE

/*** Start of inlined file: jdphuff.c ***/
#define JPEG_INTERNALS

#ifdef D_PROGRESSIVE_SUPPORTED

typedef struct {
  unsigned int EOBRUN;			/* remaining EOBs in EOBRUN */
  int last_dc_val[MAX_COMPS_IN_SCAN];	/* last DC coef for each component */
} savable_state3;

#ifndef NO_STRUCT_ASSIGN
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
#else
#if MAX_COMPS_IN_SCAN == 4
#define ASSIGN_STATE(dest,src)  \
	((dest).EOBRUN = (src).EOBRUN, \
	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
	 (dest).last_dc_val[3] = (src).last_dc_val[3])
#endif
#endif

typedef struct {
  struct jpeg_entropy_decoder pub; /* public fields */

  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
  savable_state3 saved;		/* Other state at start of MCU */

  unsigned int restarts_to_go;	/* MCUs left in this restart interval */

  d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];

  d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
} phuff_entropy_decoder;

typedef phuff_entropy_decoder * phuff_entropy_ptr2;

METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
						JBLOCKROW *MCU_data));
METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
						JBLOCKROW *MCU_data));
METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
						 JBLOCKROW *MCU_data));
METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
						 JBLOCKROW *MCU_data));

METHODDEF(void)
start_pass_phuff_decoder (j_decompress_ptr cinfo)
{
  phuff_entropy_ptr2 entropy = (phuff_entropy_ptr2) cinfo->entropy;
  boolean is_DC_band, bad;
  int ci, coefi, tbl;
  int *coef_bit_ptr;
  jpeg_component_info * compptr;

  is_DC_band = (cinfo->Ss == 0);

  bad = FALSE;
  if (is_DC_band) {
	if (cinfo->Se != 0)
	  bad = TRUE;
  } else {
	if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
	  bad = TRUE;
	if (cinfo->comps_in_scan != 1)
	  bad = TRUE;
  }
  if (cinfo->Ah != 0) {
	if (cinfo->Al != cinfo->Ah-1)
	  bad = TRUE;
  }
  if (cinfo->Al > 13)		/* need not check for < 0 */
	bad = TRUE;
  if (bad)
	ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
		 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	int cindex = cinfo->cur_comp_info[ci]->component_index;
	coef_bit_ptr = & cinfo->coef_bits[cindex][0];
	if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
	  WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
	for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
	  int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
	  if (cinfo->Ah != expected)
	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
	  coef_bit_ptr[coefi] = cinfo->Al;
	}
  }

  if (cinfo->Ah == 0) {
	if (is_DC_band)
	  entropy->pub.decode_mcu = decode_mcu_DC_first;
	else
	  entropy->pub.decode_mcu = decode_mcu_AC_first;
  } else {
	if (is_DC_band)
	  entropy->pub.decode_mcu = decode_mcu_DC_refine;
	else
	  entropy->pub.decode_mcu = decode_mcu_AC_refine;
  }

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	if (is_DC_band) {
	  if (cinfo->Ah == 0) {	/* DC refinement needs no table */
	tbl = compptr->dc_tbl_no;
	jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
				& entropy->derived_tbls[tbl]);
	  }
	} else {
	  tbl = compptr->ac_tbl_no;
	  jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
				  & entropy->derived_tbls[tbl]);
	  entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
	}
	entropy->saved.last_dc_val[ci] = 0;
  }

  entropy->bitstate.bits_left = 0;
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  entropy->pub.insufficient_data = FALSE;

  entropy->saved.EOBRUN = 0;

  entropy->restarts_to_go = cinfo->restart_interval;
}

LOCAL(boolean)
process_restartp (j_decompress_ptr cinfo)
{
  phuff_entropy_ptr2 entropy = (phuff_entropy_ptr2) cinfo->entropy;
  int ci;

  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  entropy->bitstate.bits_left = 0;

  if (! (*cinfo->marker->read_restart_marker) (cinfo))
	return FALSE;

  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
	entropy->saved.last_dc_val[ci] = 0;
  entropy->saved.EOBRUN = 0;

  entropy->restarts_to_go = cinfo->restart_interval;

  if (cinfo->unread_marker == 0)
	entropy->pub.insufficient_data = FALSE;

  return TRUE;
}

METHODDEF(boolean)
decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr2 entropy = (phuff_entropy_ptr2) cinfo->entropy;
  int Al = cinfo->Al;
  register int s, r;
  int blkn, ci;
  JBLOCKROW block;
  BITREAD_STATE_VARS;
  savable_state3 state;
  d_derived_tbl * tbl;
  jpeg_component_info * compptr;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0)
	  if (! process_restartp(cinfo))
	return FALSE;
  }

  if (! entropy->pub.insufficient_data) {

	BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
	ASSIGN_STATE(state, entropy->saved);

	for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
	  block = MCU_data[blkn];
	  ci = cinfo->MCU_membership[blkn];
	  compptr = cinfo->cur_comp_info[ci];
	  tbl = entropy->derived_tbls[compptr->dc_tbl_no];

	  HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
	  if (s) {
	CHECK_BIT_BUFFER(br_state, s, return FALSE);
	r = GET_BITS(s);
	s = HUFF_EXTEND(r, s);
	  }

	  s += state.last_dc_val[ci];
	  state.last_dc_val[ci] = s;
	  (*block)[0] = (JCOEF) (s << Al);
	}

	BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
	ASSIGN_STATE(entropy->saved, state);
  }

  entropy->restarts_to_go--;

  return TRUE;
}

METHODDEF(boolean)
decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr2 entropy = (phuff_entropy_ptr2) cinfo->entropy;
  int Se = cinfo->Se;
  int Al = cinfo->Al;
  register int s, k, r;
  unsigned int EOBRUN;
  JBLOCKROW block;
  BITREAD_STATE_VARS;
  d_derived_tbl * tbl;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0)
	  if (! process_restartp(cinfo))
	return FALSE;
  }

  if (! entropy->pub.insufficient_data) {

	EOBRUN = entropy->saved.EOBRUN;	/* only part of saved state we need */

	if (EOBRUN > 0)		/* if it's a band of zeroes... */
	  EOBRUN--;			/* ...process it now (we do nothing) */
	else {
	  BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
	  block = MCU_data[0];
	  tbl = entropy->ac_derived_tbl;

	  for (k = cinfo->Ss; k <= Se; k++) {
	HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
	r = s >> 4;
	s &= 15;
	if (s) {
	  k += r;
	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
	  r = GET_BITS(s);
	  s = HUFF_EXTEND(r, s);
	  (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
	} else {
	  if (r == 15) {	/* ZRL */
		k += 15;		/* skip 15 zeroes in band */
	  } else {		/* EOBr, run length is 2^r + appended bits */
		EOBRUN = 1 << r;
		if (r) {		/* EOBr, r > 0 */
		  CHECK_BIT_BUFFER(br_state, r, return FALSE);
		  r = GET_BITS(r);
		  EOBRUN += r;
		}
		EOBRUN--;		/* this band is processed at this moment */
		break;		/* force end-of-band */
	  }
	}
	  }

	  BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
	}

	entropy->saved.EOBRUN = EOBRUN;	/* only part of saved state we need */
  }

  entropy->restarts_to_go--;

  return TRUE;
}

METHODDEF(boolean)
decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr2 entropy = (phuff_entropy_ptr2) cinfo->entropy;
  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
  int blkn;
  JBLOCKROW block;
  BITREAD_STATE_VARS;

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0)
	  if (! process_restartp(cinfo))
	return FALSE;
  }

  BITREAD_LOAD_STATE(cinfo,entropy->bitstate);

  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
	block = MCU_data[blkn];

	CHECK_BIT_BUFFER(br_state, 1, return FALSE);
	if (GET_BITS(1))
	  (*block)[0] |= p1;
  }

  BITREAD_SAVE_STATE(cinfo,entropy->bitstate);

  entropy->restarts_to_go--;

  return TRUE;
}

METHODDEF(boolean)
decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr2 entropy = (phuff_entropy_ptr2) cinfo->entropy;
  int Se = cinfo->Se;
  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
  int m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
  register int s, k, r;
  unsigned int EOBRUN;
  JBLOCKROW block;
  JCOEFPTR thiscoef;
  BITREAD_STATE_VARS;
  d_derived_tbl * tbl;
  int num_newnz;
  int newnz_pos[DCTSIZE2];

  if (cinfo->restart_interval) {
	if (entropy->restarts_to_go == 0)
	  if (! process_restartp(cinfo))
	return FALSE;
  }

  if (! entropy->pub.insufficient_data) {

	BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
	EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */

	block = MCU_data[0];
	tbl = entropy->ac_derived_tbl;

	num_newnz = 0;

	k = cinfo->Ss;

	if (EOBRUN == 0) {
	  for (; k <= Se; k++) {
	HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
	r = s >> 4;
	s &= 15;
	if (s) {
	  if (s != 1)		/* size of new coef should always be 1 */
		WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
	  if (GET_BITS(1))
		s = p1;		/* newly nonzero coef is positive */
	  else
		s = m1;		/* newly nonzero coef is negative */
	} else {
	  if (r != 15) {
		EOBRUN = 1 << r;	/* EOBr, run length is 2^r + appended bits */
		if (r) {
		  CHECK_BIT_BUFFER(br_state, r, goto undoit);
		  r = GET_BITS(r);
		  EOBRUN += r;
		}
		break;		/* rest of block is handled by EOB logic */
	  }
	}
	do {
	  thiscoef = *block + jpeg_natural_order[k];
	  if (*thiscoef != 0) {
		CHECK_BIT_BUFFER(br_state, 1, goto undoit);
		if (GET_BITS(1)) {
		  if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
		if (*thiscoef >= 0)
		  *thiscoef += p1;
		else
		  *thiscoef += m1;
		  }
		}
	  } else {
		if (--r < 0)
		  break;		/* reached target zero coefficient */
	  }
	  k++;
	} while (k <= Se);
	if (s) {
	  int pos = jpeg_natural_order[k];
	  (*block)[pos] = (JCOEF) s;
	  newnz_pos[num_newnz++] = pos;
	}
	  }
	}

	if (EOBRUN > 0) {
	  for (; k <= Se; k++) {
	thiscoef = *block + jpeg_natural_order[k];
	if (*thiscoef != 0) {
	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
	  if (GET_BITS(1)) {
		if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
		  if (*thiscoef >= 0)
		*thiscoef += p1;
		  else
		*thiscoef += m1;
		}
	  }
	}
	  }
	  EOBRUN--;
	}

	BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
	entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
  }

  entropy->restarts_to_go--;

  return TRUE;

undoit:
  while (num_newnz > 0)
	(*block)[newnz_pos[--num_newnz]] = 0;

  return FALSE;
}

GLOBAL(void)
jinit_phuff_decoder (j_decompress_ptr cinfo)
{
  phuff_entropy_ptr2 entropy;
  int *coef_bit_ptr;
  int ci, i;

  entropy = (phuff_entropy_ptr2)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(phuff_entropy_decoder));
  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  entropy->pub.start_pass = start_pass_phuff_decoder;

  for (i = 0; i < NUM_HUFF_TBLS; i++) {
	entropy->derived_tbls[i] = NULL;
  }

  cinfo->coef_bits = (int (*)[DCTSIZE2])
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				cinfo->num_components*DCTSIZE2*SIZEOF(int));
  coef_bit_ptr = & cinfo->coef_bits[0][0];
  for (ci = 0; ci < cinfo->num_components; ci++)
	for (i = 0; i < DCTSIZE2; i++)
	  *coef_bit_ptr++ = -1;
}

#endif /* D_PROGRESSIVE_SUPPORTED */
/*** End of inlined file: jdphuff.c ***/



/*** Start of inlined file: jdpostct.c ***/
#define JPEG_INTERNALS

typedef struct {
  struct jpeg_d_post_controller pub; /* public fields */

  jvirt_sarray_ptr whole_image;	/* virtual array, or NULL if one-pass */
  JSAMPARRAY buffer;		/* strip buffer, or current strip of virtual */
  JDIMENSION strip_height;	/* buffer size in rows */
  JDIMENSION starting_row;	/* row # of first row in current strip */
  JDIMENSION next_row;		/* index of next row to fill/empty in strip */
} my_post_controller;

typedef my_post_controller * my_post_ptr;

METHODDEF(void) post_process_1pass
	JPP((j_decompress_ptr cinfo,
		 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
		 JDIMENSION in_row_groups_avail,
		 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
		 JDIMENSION out_rows_avail));
#ifdef QUANT_2PASS_SUPPORTED
METHODDEF(void) post_process_prepass
	JPP((j_decompress_ptr cinfo,
		 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
		 JDIMENSION in_row_groups_avail,
		 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
		 JDIMENSION out_rows_avail));
METHODDEF(void) post_process_2pass
	JPP((j_decompress_ptr cinfo,
		 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
		 JDIMENSION in_row_groups_avail,
		 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
		 JDIMENSION out_rows_avail));
#endif

METHODDEF(void)
start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
{
  my_post_ptr post = (my_post_ptr) cinfo->post;

  switch (pass_mode) {
  case JBUF_PASS_THRU:
	if (cinfo->quantize_colors) {
	  post->pub.post_process_data = post_process_1pass;
	  if (post->buffer == NULL) {
	post->buffer = (*cinfo->mem->access_virt_sarray)
	  ((j_common_ptr) cinfo, post->whole_image,
	   (JDIMENSION) 0, post->strip_height, TRUE);
	  }
	} else {
	  post->pub.post_process_data = cinfo->upsample->upsample;
	}
	break;
#ifdef QUANT_2PASS_SUPPORTED
  case JBUF_SAVE_AND_PASS:
	if (post->whole_image == NULL)
	  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	post->pub.post_process_data = post_process_prepass;
	break;
  case JBUF_CRANK_DEST:
	if (post->whole_image == NULL)
	  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	post->pub.post_process_data = post_process_2pass;
	break;
#endif /* QUANT_2PASS_SUPPORTED */
  default:
	ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
	break;
  }
  post->starting_row = post->next_row = 0;
}

METHODDEF(void)
post_process_1pass (j_decompress_ptr cinfo,
			JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
			JDIMENSION in_row_groups_avail,
			JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
			JDIMENSION out_rows_avail)
{
  my_post_ptr post = (my_post_ptr) cinfo->post;
  JDIMENSION num_rows, max_rows;

  max_rows = out_rows_avail - *out_row_ctr;
  if (max_rows > post->strip_height)
	max_rows = post->strip_height;
  num_rows = 0;
  (*cinfo->upsample->upsample) (cinfo,
		input_buf, in_row_group_ctr, in_row_groups_avail,
		post->buffer, &num_rows, max_rows);
  (*cinfo->cquantize->color_quantize) (cinfo,
		post->buffer, output_buf + *out_row_ctr, (int) num_rows);
  *out_row_ctr += num_rows;
}

#ifdef QUANT_2PASS_SUPPORTED

METHODDEF(void)
post_process_prepass (j_decompress_ptr cinfo,
			  JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
			  JDIMENSION in_row_groups_avail,
			  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
			  JDIMENSION out_rows_avail)
{
  my_post_ptr post = (my_post_ptr) cinfo->post;
  JDIMENSION old_next_row, num_rows;

  if (post->next_row == 0) {
	post->buffer = (*cinfo->mem->access_virt_sarray)
	((j_common_ptr) cinfo, post->whole_image,
	 post->starting_row, post->strip_height, TRUE);
  }

  old_next_row = post->next_row;
  (*cinfo->upsample->upsample) (cinfo,
		input_buf, in_row_group_ctr, in_row_groups_avail,
		post->buffer, &post->next_row, post->strip_height);

  if (post->next_row > old_next_row) {
	num_rows = post->next_row - old_next_row;
	(*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
					 (JSAMPARRAY) NULL, (int) num_rows);
	*out_row_ctr += num_rows;
  }

  if (post->next_row >= post->strip_height) {
	post->starting_row += post->strip_height;
	post->next_row = 0;
  }
}

METHODDEF(void)
post_process_2pass (j_decompress_ptr cinfo,
			JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
			JDIMENSION in_row_groups_avail,
			JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
			JDIMENSION out_rows_avail)
{
  my_post_ptr post = (my_post_ptr) cinfo->post;
  JDIMENSION num_rows, max_rows;

  if (post->next_row == 0) {
	post->buffer = (*cinfo->mem->access_virt_sarray)
	((j_common_ptr) cinfo, post->whole_image,
	 post->starting_row, post->strip_height, FALSE);
  }

  num_rows = post->strip_height - post->next_row; /* available in strip */
  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
  if (num_rows > max_rows)
	num_rows = max_rows;
  max_rows = cinfo->output_height - post->starting_row;
  if (num_rows > max_rows)
	num_rows = max_rows;

  (*cinfo->cquantize->color_quantize) (cinfo,
		post->buffer + post->next_row, output_buf + *out_row_ctr,
		(int) num_rows);
  *out_row_ctr += num_rows;

  post->next_row += num_rows;
  if (post->next_row >= post->strip_height) {
	post->starting_row += post->strip_height;
	post->next_row = 0;
  }
}

#endif /* QUANT_2PASS_SUPPORTED */

GLOBAL(void)
jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
{
  my_post_ptr post;

  post = (my_post_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_post_controller));
  cinfo->post = (struct jpeg_d_post_controller *) post;
  post->pub.start_pass = start_pass_dpost;
  post->whole_image = NULL;	/* flag for no virtual arrays */
  post->buffer = NULL;		/* flag for no strip buffer */

  if (cinfo->quantize_colors) {
	post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
	if (need_full_buffer) {
#ifdef QUANT_2PASS_SUPPORTED
	  post->whole_image = (*cinfo->mem->request_virt_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
	 cinfo->output_width * cinfo->out_color_components,
	 (JDIMENSION) jround_up((long) cinfo->output_height,
				(long) post->strip_height),
	 post->strip_height);
#else
	  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
#endif /* QUANT_2PASS_SUPPORTED */
	} else {
	  post->buffer = (*cinfo->mem->alloc_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE,
	 cinfo->output_width * cinfo->out_color_components,
	 post->strip_height);
	}
  }
}
/*** End of inlined file: jdpostct.c ***/

	#undef FIX

/*** Start of inlined file: jdsample.c ***/
#define JPEG_INTERNALS

typedef JMETHOD(void, upsample1_ptr,
		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
		 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));

typedef struct {
  struct jpeg_upsampler pub;	/* public fields */

  JSAMPARRAY color_buf[MAX_COMPONENTS];

  upsample1_ptr methods[MAX_COMPONENTS];

  int next_row_out;		/* counts rows emitted from color_buf */
  JDIMENSION rows_to_go;	/* counts rows remaining in image */

  int rowgroup_height[MAX_COMPONENTS];

  UINT8 h_expand[MAX_COMPONENTS];
  UINT8 v_expand[MAX_COMPONENTS];
} my_upsampler2;

typedef my_upsampler2 * my_upsample_ptr2;

METHODDEF(void)
start_pass_upsample (j_decompress_ptr cinfo)
{
  my_upsample_ptr2 upsample = (my_upsample_ptr2) cinfo->upsample;

  upsample->next_row_out = cinfo->max_v_samp_factor;
  upsample->rows_to_go = cinfo->output_height;
}

METHODDEF(void)
sep_upsample (j_decompress_ptr cinfo,
		  JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
		  JDIMENSION in_row_groups_avail,
		  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
		  JDIMENSION out_rows_avail)
{
  my_upsample_ptr2 upsample = (my_upsample_ptr2) cinfo->upsample;
  int ci;
  jpeg_component_info * compptr;
  JDIMENSION num_rows;

  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	 ci++, compptr++) {
	  (*upsample->methods[ci]) (cinfo, compptr,
	input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
	upsample->color_buf + ci);
	}
	upsample->next_row_out = 0;
  }

  num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
  if (num_rows > upsample->rows_to_go)
	num_rows = upsample->rows_to_go;
  out_rows_avail -= *out_row_ctr;
  if (num_rows > out_rows_avail)
	num_rows = out_rows_avail;

  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
					 (JDIMENSION) upsample->next_row_out,
					 output_buf + *out_row_ctr,
					 (int) num_rows);

  *out_row_ctr += num_rows;
  upsample->rows_to_go -= num_rows;
  upsample->next_row_out += num_rows;
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
	(*in_row_group_ctr)++;
}

METHODDEF(void)
fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
  *output_data_ptr = input_data;
}

METHODDEF(void)
noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
  *output_data_ptr = NULL;	/* safety check */
}

METHODDEF(void)
int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		  JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
  my_upsample_ptr2 upsample = (my_upsample_ptr2) cinfo->upsample;
  JSAMPARRAY output_data = *output_data_ptr;
  register JSAMPROW inptr, outptr;
  register JSAMPLE invalue;
  register int h;
  JSAMPROW outend;
  int h_expand, v_expand;
  int inrow, outrow;

  h_expand = upsample->h_expand[compptr->component_index];
  v_expand = upsample->v_expand[compptr->component_index];

  inrow = outrow = 0;
  while (outrow < cinfo->max_v_samp_factor) {
	inptr = input_data[inrow];
	outptr = output_data[outrow];
	outend = outptr + cinfo->output_width;
	while (outptr < outend) {
	  invalue = *inptr++;	/* don't need GETJSAMPLE() here */
	  for (h = h_expand; h > 0; h--) {
	*outptr++ = invalue;
	  }
	}
	if (v_expand > 1) {
	  jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
			v_expand-1, cinfo->output_width);
	}
	inrow++;
	outrow += v_expand;
  }
}

METHODDEF(void)
h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
  JSAMPARRAY output_data = *output_data_ptr;
  register JSAMPROW inptr, outptr;
  register JSAMPLE invalue;
  JSAMPROW outend;
  int inrow;

  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
	inptr = input_data[inrow];
	outptr = output_data[inrow];
	outend = outptr + cinfo->output_width;
	while (outptr < outend) {
	  invalue = *inptr++;	/* don't need GETJSAMPLE() here */
	  *outptr++ = invalue;
	  *outptr++ = invalue;
	}
  }
}

METHODDEF(void)
h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
  JSAMPARRAY output_data = *output_data_ptr;
  register JSAMPROW inptr, outptr;
  register JSAMPLE invalue;
  JSAMPROW outend;
  int inrow, outrow;

  inrow = outrow = 0;
  while (outrow < cinfo->max_v_samp_factor) {
	inptr = input_data[inrow];
	outptr = output_data[outrow];
	outend = outptr + cinfo->output_width;
	while (outptr < outend) {
	  invalue = *inptr++;	/* don't need GETJSAMPLE() here */
	  *outptr++ = invalue;
	  *outptr++ = invalue;
	}
	jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
			  1, cinfo->output_width);
	inrow++;
	outrow += 2;
  }
}

METHODDEF(void)
h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
			 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
  JSAMPARRAY output_data = *output_data_ptr;
  register JSAMPROW inptr, outptr;
  register int invalue;
  register JDIMENSION colctr;
  int inrow;

  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
	inptr = input_data[inrow];
	outptr = output_data[inrow];
	invalue = GETJSAMPLE(*inptr++);
	*outptr++ = (JSAMPLE) invalue;
	*outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);

	for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
	  invalue = GETJSAMPLE(*inptr++) * 3;
	  *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
	  *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
	}

	invalue = GETJSAMPLE(*inptr);
	*outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
	*outptr++ = (JSAMPLE) invalue;
  }
}

METHODDEF(void)
h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
			 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
  JSAMPARRAY output_data = *output_data_ptr;
  register JSAMPROW inptr0, inptr1, outptr;
#if BITS_IN_JSAMPLE == 8
  register int thiscolsum, lastcolsum, nextcolsum;
#else
  register INT32 thiscolsum, lastcolsum, nextcolsum;
#endif
  register JDIMENSION colctr;
  int inrow, outrow, v;

  inrow = outrow = 0;
  while (outrow < cinfo->max_v_samp_factor) {
	for (v = 0; v < 2; v++) {
	  inptr0 = input_data[inrow];
	  if (v == 0)		/* next nearest is row above */
	inptr1 = input_data[inrow-1];
	  else			/* next nearest is row below */
	inptr1 = input_data[inrow+1];
	  outptr = output_data[outrow++];

	  thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
	  nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
	  *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
	  *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
	  lastcolsum = thiscolsum; thiscolsum = nextcolsum;

	  for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
	nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
	lastcolsum = thiscolsum; thiscolsum = nextcolsum;
	  }

	  *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
	  *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
	}
	inrow++;
  }
}

GLOBAL(void)
jinit_upsampler (j_decompress_ptr cinfo)
{
  my_upsample_ptr2 upsample;
  int ci;
  jpeg_component_info * compptr;
  boolean need_buffer, do_fancy;
  int h_in_group, v_in_group, h_out_group, v_out_group;

  upsample = (my_upsample_ptr2)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_upsampler2));
  cinfo->upsample = (struct jpeg_upsampler *) upsample;
  upsample->pub.start_pass = start_pass_upsample;
  upsample->pub.upsample = sep_upsample;
  upsample->pub.need_context_rows = FALSE; /* until we find out differently */

  if (cinfo->CCIR601_sampling)	/* this isn't supported */
	ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);

  do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
	   ci++, compptr++) {
	h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
		 cinfo->min_DCT_scaled_size;
	v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
		 cinfo->min_DCT_scaled_size;
	h_out_group = cinfo->max_h_samp_factor;
	v_out_group = cinfo->max_v_samp_factor;
	upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
	need_buffer = TRUE;
	if (! compptr->component_needed) {
	  upsample->methods[ci] = noop_upsample;
	  need_buffer = FALSE;
	} else if (h_in_group == h_out_group && v_in_group == v_out_group) {
	  upsample->methods[ci] = fullsize_upsample;
	  need_buffer = FALSE;
	} else if (h_in_group * 2 == h_out_group &&
		   v_in_group == v_out_group) {
	  if (do_fancy && compptr->downsampled_width > 2)
	upsample->methods[ci] = h2v1_fancy_upsample;
	  else
	upsample->methods[ci] = h2v1_upsample;
	} else if (h_in_group * 2 == h_out_group &&
		   v_in_group * 2 == v_out_group) {
	  if (do_fancy && compptr->downsampled_width > 2) {
	upsample->methods[ci] = h2v2_fancy_upsample;
	upsample->pub.need_context_rows = TRUE;
	  } else
	upsample->methods[ci] = h2v2_upsample;
	} else if ((h_out_group % h_in_group) == 0 &&
		   (v_out_group % v_in_group) == 0) {
	  upsample->methods[ci] = int_upsample;
	  upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
	  upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
	} else
	  ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
	if (need_buffer) {
	  upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE,
	 (JDIMENSION) jround_up((long) cinfo->output_width,
				(long) cinfo->max_h_samp_factor),
	 (JDIMENSION) cinfo->max_v_samp_factor);
	}
  }
}
/*** End of inlined file: jdsample.c ***/



/*** Start of inlined file: jdtrans.c ***/
#define JPEG_INTERNALS

LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));

GLOBAL(jvirt_barray_ptr *)
jpeg_read_coefficients (j_decompress_ptr cinfo)
{
  if (cinfo->global_state == DSTATE_READY) {
	transdecode_master_selection(cinfo);
	cinfo->global_state = DSTATE_RDCOEFS;
  }
  if (cinfo->global_state == DSTATE_RDCOEFS) {
	for (;;) {
	  int retcode;
	  if (cinfo->progress != NULL)
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
	  retcode = (*cinfo->inputctl->consume_input) (cinfo);
	  if (retcode == JPEG_SUSPENDED)
	return NULL;
	  if (retcode == JPEG_REACHED_EOI)
	break;
	  if (cinfo->progress != NULL &&
	  (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
	if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
	  cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
	}
	  }
	}
	cinfo->global_state = DSTATE_STOPPING;
  }
  if ((cinfo->global_state == DSTATE_STOPPING ||
	   cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
	return cinfo->coef->coef_arrays;
  }
  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  return NULL;			/* keep compiler happy */
}

LOCAL(void)
transdecode_master_selection (j_decompress_ptr cinfo)
{
  cinfo->buffered_image = TRUE;

  if (cinfo->arith_code) {
	ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  } else {
	if (cinfo->progressive_mode) {
#ifdef D_PROGRESSIVE_SUPPORTED
	  jinit_phuff_decoder(cinfo);
#else
	  ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
	} else
	  jinit_huff_decoder(cinfo);
  }

  jinit_d_coef_controller(cinfo, TRUE);

  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);

  (*cinfo->inputctl->start_input_pass) (cinfo);

  if (cinfo->progress != NULL) {
	int nscans;
	if (cinfo->progressive_mode) {
	  nscans = 2 + 3 * cinfo->num_components;
	} else if (cinfo->inputctl->has_multiple_scans) {
	  nscans = cinfo->num_components;
	} else {
	  nscans = 1;
	}
	cinfo->progress->pass_counter = 0L;
	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
	cinfo->progress->completed_passes = 0;
	cinfo->progress->total_passes = 1;
  }
}
/*** End of inlined file: jdtrans.c ***/


/*** Start of inlined file: jfdctflt.c ***/
#define JPEG_INTERNALS

#ifdef DCT_FLOAT_SUPPORTED

#if DCTSIZE != 8
  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
#endif

GLOBAL(void)
jpeg_fdct_float (FAST_FLOAT * data)
{
  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
  FAST_FLOAT z1, z2, z3, z4, z5, z11, z13;
  FAST_FLOAT *dataptr;
  int ctr;

  dataptr = data;
  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
	tmp0 = dataptr[0] + dataptr[7];
	tmp7 = dataptr[0] - dataptr[7];
	tmp1 = dataptr[1] + dataptr[6];
	tmp6 = dataptr[1] - dataptr[6];
	tmp2 = dataptr[2] + dataptr[5];
	tmp5 = dataptr[2] - dataptr[5];
	tmp3 = dataptr[3] + dataptr[4];
	tmp4 = dataptr[3] - dataptr[4];

	tmp10 = tmp0 + tmp3;	/* phase 2 */
	tmp13 = tmp0 - tmp3;
	tmp11 = tmp1 + tmp2;
	tmp12 = tmp1 - tmp2;

	dataptr[0] = tmp10 + tmp11; /* phase 3 */
	dataptr[4] = tmp10 - tmp11;

	z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
	dataptr[2] = tmp13 + z1;	/* phase 5 */
	dataptr[6] = tmp13 - z1;

	tmp10 = tmp4 + tmp5;	/* phase 2 */
	tmp11 = tmp5 + tmp6;
	tmp12 = tmp6 + tmp7;

	z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
	z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
	z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
	z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */

	z11 = tmp7 + z3;		/* phase 5 */
	z13 = tmp7 - z3;

	dataptr[5] = z13 + z2;	/* phase 6 */
	dataptr[3] = z13 - z2;
	dataptr[1] = z11 + z4;
	dataptr[7] = z11 - z4;

	dataptr += DCTSIZE;		/* advance pointer to next row */
  }

  dataptr = data;
  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
	tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
	tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
	tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
	tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
	tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
	tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
	tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
	tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];

	tmp10 = tmp0 + tmp3;	/* phase 2 */
	tmp13 = tmp0 - tmp3;
	tmp11 = tmp1 + tmp2;
	tmp12 = tmp1 - tmp2;

	dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
	dataptr[DCTSIZE*4] = tmp10 - tmp11;

	z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
	dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
	dataptr[DCTSIZE*6] = tmp13 - z1;

	tmp10 = tmp4 + tmp5;	/* phase 2 */
	tmp11 = tmp5 + tmp6;
	tmp12 = tmp6 + tmp7;

	z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
	z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
	z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
	z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */

	z11 = tmp7 + z3;		/* phase 5 */
	z13 = tmp7 - z3;

	dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
	dataptr[DCTSIZE*3] = z13 - z2;
	dataptr[DCTSIZE*1] = z11 + z4;
	dataptr[DCTSIZE*7] = z11 - z4;

	dataptr++;			/* advance pointer to next column */
  }
}

#endif /* DCT_FLOAT_SUPPORTED */
/*** End of inlined file: jfdctflt.c ***/


/*** Start of inlined file: jfdctint.c ***/
#define JPEG_INTERNALS

#ifdef DCT_ISLOW_SUPPORTED

#if DCTSIZE != 8
  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
#endif

#if BITS_IN_JSAMPLE == 8
#define CONST_BITS  13
#define PASS1_BITS  2
#else
#define CONST_BITS  13
#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
#endif

#if CONST_BITS == 13
#define FIX_0_298631336  ((INT32)  2446)	/* FIX(0.298631336) */
#define FIX_0_390180644  ((INT32)  3196)	/* FIX(0.390180644) */
#define FIX_0_541196100  ((INT32)  4433)	/* FIX(0.541196100) */
#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
#define FIX_1_175875602  ((INT32)  9633)	/* FIX(1.175875602) */
#define FIX_1_501321110  ((INT32)  12299)	/* FIX(1.501321110) */
#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
#define FIX_1_961570560  ((INT32)  16069)	/* FIX(1.961570560) */
#define FIX_2_053119869  ((INT32)  16819)	/* FIX(2.053119869) */
#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
#define FIX_3_072711026  ((INT32)  25172)	/* FIX(3.072711026) */
#else
#define FIX_0_298631336  FIX(0.298631336)
#define FIX_0_390180644  FIX(0.390180644)
#define FIX_0_541196100  FIX(0.541196100)
#define FIX_0_765366865  FIX(0.765366865)
#define FIX_0_899976223  FIX(0.899976223)
#define FIX_1_175875602  FIX(1.175875602)
#define FIX_1_501321110  FIX(1.501321110)
#define FIX_1_847759065  FIX(1.847759065)
#define FIX_1_961570560  FIX(1.961570560)
#define FIX_2_053119869  FIX(2.053119869)
#define FIX_2_562915447  FIX(2.562915447)
#define FIX_3_072711026  FIX(3.072711026)
#endif

#if BITS_IN_JSAMPLE == 8
#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
#else
#define MULTIPLY(var,const)  ((var) * (const))
#endif

GLOBAL(void)
jpeg_fdct_islow (DCTELEM * data)
{
  INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  INT32 tmp10, tmp11, tmp12, tmp13;
  INT32 z1, z2, z3, z4, z5;
  DCTELEM *dataptr;
  int ctr;
  SHIFT_TEMPS

  dataptr = data;
  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
	tmp0 = dataptr[0] + dataptr[7];
	tmp7 = dataptr[0] - dataptr[7];
	tmp1 = dataptr[1] + dataptr[6];
	tmp6 = dataptr[1] - dataptr[6];
	tmp2 = dataptr[2] + dataptr[5];
	tmp5 = dataptr[2] - dataptr[5];
	tmp3 = dataptr[3] + dataptr[4];
	tmp4 = dataptr[3] - dataptr[4];

	tmp10 = tmp0 + tmp3;
	tmp13 = tmp0 - tmp3;
	tmp11 = tmp1 + tmp2;
	tmp12 = tmp1 - tmp2;

	dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS);
	dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);

	z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
	dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
				   CONST_BITS-PASS1_BITS);
	dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
				   CONST_BITS-PASS1_BITS);

	z1 = tmp4 + tmp7;
	z2 = tmp5 + tmp6;
	z3 = tmp4 + tmp6;
	z4 = tmp5 + tmp7;
	z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */

	tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
	tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
	tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
	tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
	z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
	z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
	z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
	z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */

	z3 += z5;
	z4 += z5;

	dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS);
	dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS);
	dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS);
	dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS);

	dataptr += DCTSIZE;		/* advance pointer to next row */
  }

  dataptr = data;
  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
	tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
	tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
	tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
	tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
	tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
	tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
	tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
	tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];

	tmp10 = tmp0 + tmp3;
	tmp13 = tmp0 - tmp3;
	tmp11 = tmp1 + tmp2;
	tmp12 = tmp1 - tmp2;

	dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS);
	dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS);

	z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
	dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
					   CONST_BITS+PASS1_BITS);
	dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
					   CONST_BITS+PASS1_BITS);

	z1 = tmp4 + tmp7;
	z2 = tmp5 + tmp6;
	z3 = tmp4 + tmp6;
	z4 = tmp5 + tmp7;
	z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */

	tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
	tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
	tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
	tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
	z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
	z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
	z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
	z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */

	z3 += z5;
	z4 += z5;

	dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3,
					   CONST_BITS+PASS1_BITS);
	dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4,
					   CONST_BITS+PASS1_BITS);
	dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3,
					   CONST_BITS+PASS1_BITS);
	dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4,
					   CONST_BITS+PASS1_BITS);

	dataptr++;			/* advance pointer to next column */
  }
}

#endif /* DCT_ISLOW_SUPPORTED */
/*** End of inlined file: jfdctint.c ***/

	#undef CONST_BITS
	#undef MULTIPLY
	#undef FIX_0_541196100

/*** Start of inlined file: jfdctfst.c ***/
#define JPEG_INTERNALS

#ifdef DCT_IFAST_SUPPORTED

#if DCTSIZE != 8
  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
#endif

#define CONST_BITS  8

#if CONST_BITS == 8
#define FIX_0_382683433  ((INT32)   98)		/* FIX(0.382683433) */
#define FIX_0_541196100  ((INT32)  139)		/* FIX(0.541196100) */
#define FIX_0_707106781  ((INT32)  181)		/* FIX(0.707106781) */
#define FIX_1_306562965  ((INT32)  334)		/* FIX(1.306562965) */
#else
#define FIX_0_382683433  FIX(0.382683433)
#define FIX_0_541196100  FIX(0.541196100)
#define FIX_0_707106781  FIX(0.707106781)
#define FIX_1_306562965  FIX(1.306562965)
#endif

#ifndef USE_ACCURATE_ROUNDING
#undef DESCALE
#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
#endif

#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))

GLOBAL(void)
jpeg_fdct_ifast (DCTELEM * data)
{
  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  DCTELEM tmp10, tmp11, tmp12, tmp13;
  DCTELEM z1, z2, z3, z4, z5, z11, z13;
  DCTELEM *dataptr;
  int ctr;
  SHIFT_TEMPS

  dataptr = data;
  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
	tmp0 = dataptr[0] + dataptr[7];
	tmp7 = dataptr[0] - dataptr[7];
	tmp1 = dataptr[1] + dataptr[6];
	tmp6 = dataptr[1] - dataptr[6];
	tmp2 = dataptr[2] + dataptr[5];
	tmp5 = dataptr[2] - dataptr[5];
	tmp3 = dataptr[3] + dataptr[4];
	tmp4 = dataptr[3] - dataptr[4];

	tmp10 = tmp0 + tmp3;	/* phase 2 */
	tmp13 = tmp0 - tmp3;
	tmp11 = tmp1 + tmp2;
	tmp12 = tmp1 - tmp2;

	dataptr[0] = tmp10 + tmp11; /* phase 3 */
	dataptr[4] = tmp10 - tmp11;

	z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
	dataptr[2] = tmp13 + z1;	/* phase 5 */
	dataptr[6] = tmp13 - z1;

	tmp10 = tmp4 + tmp5;	/* phase 2 */
	tmp11 = tmp5 + tmp6;
	tmp12 = tmp6 + tmp7;

	z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
	z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
	z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
	z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */

	z11 = tmp7 + z3;		/* phase 5 */
	z13 = tmp7 - z3;

	dataptr[5] = z13 + z2;	/* phase 6 */
	dataptr[3] = z13 - z2;
	dataptr[1] = z11 + z4;
	dataptr[7] = z11 - z4;

	dataptr += DCTSIZE;		/* advance pointer to next row */
  }

  dataptr = data;
  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
	tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
	tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
	tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
	tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
	tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
	tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
	tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
	tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];

	tmp10 = tmp0 + tmp3;	/* phase 2 */
	tmp13 = tmp0 - tmp3;
	tmp11 = tmp1 + tmp2;
	tmp12 = tmp1 - tmp2;

	dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
	dataptr[DCTSIZE*4] = tmp10 - tmp11;

	z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
	dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
	dataptr[DCTSIZE*6] = tmp13 - z1;

	tmp10 = tmp4 + tmp5;	/* phase 2 */
	tmp11 = tmp5 + tmp6;
	tmp12 = tmp6 + tmp7;

	z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
	z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
	z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
	z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */

	z11 = tmp7 + z3;		/* phase 5 */
	z13 = tmp7 - z3;

	dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
	dataptr[DCTSIZE*3] = z13 - z2;
	dataptr[DCTSIZE*1] = z11 + z4;
	dataptr[DCTSIZE*7] = z11 - z4;

	dataptr++;			/* advance pointer to next column */
  }
}

#endif /* DCT_IFAST_SUPPORTED */
/*** End of inlined file: jfdctfst.c ***/


	#undef FIX_0_541196100

/*** Start of inlined file: jidctflt.c ***/
#define JPEG_INTERNALS

#ifdef DCT_FLOAT_SUPPORTED

#if DCTSIZE != 8
  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
#endif

#define DEQUANTIZE(coef,quantval)  (((FAST_FLOAT) (coef)) * (quantval))

GLOBAL(void)
jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		 JCOEFPTR coef_block,
		 JSAMPARRAY output_buf, JDIMENSION output_col)
{
  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
  FAST_FLOAT z5, z10, z11, z12, z13;
  JCOEFPTR inptr;
  FLOAT_MULT_TYPE * quantptr;
  FAST_FLOAT * wsptr;
  JSAMPROW outptr;
  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  int ctr;
  FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
  SHIFT_TEMPS

  inptr = coef_block;
  quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
  wsptr = workspace;
  for (ctr = DCTSIZE; ctr > 0; ctr--) {

	if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
	inptr[DCTSIZE*7] == 0) {
	  FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);

	  wsptr[DCTSIZE*0] = dcval;
	  wsptr[DCTSIZE*1] = dcval;
	  wsptr[DCTSIZE*2] = dcval;
	  wsptr[DCTSIZE*3] = dcval;
	  wsptr[DCTSIZE*4] = dcval;
	  wsptr[DCTSIZE*5] = dcval;
	  wsptr[DCTSIZE*6] = dcval;
	  wsptr[DCTSIZE*7] = dcval;

	  inptr++;			/* advance pointers to next column */
	  quantptr++;
	  wsptr++;
	  continue;
	}

	tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
	tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
	tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
	tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);

	tmp10 = tmp0 + tmp2;	/* phase 3 */
	tmp11 = tmp0 - tmp2;

	tmp13 = tmp1 + tmp3;	/* phases 5-3 */
	tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */

	tmp0 = tmp10 + tmp13;	/* phase 2 */
	tmp3 = tmp10 - tmp13;
	tmp1 = tmp11 + tmp12;
	tmp2 = tmp11 - tmp12;

	tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
	tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
	tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
	tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);

	z13 = tmp6 + tmp5;		/* phase 6 */
	z10 = tmp6 - tmp5;
	z11 = tmp4 + tmp7;
	z12 = tmp4 - tmp7;

	tmp7 = z11 + z13;		/* phase 5 */
	tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */

	z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
	tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
	tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */

	tmp6 = tmp12 - tmp7;	/* phase 2 */
	tmp5 = tmp11 - tmp6;
	tmp4 = tmp10 + tmp5;

	wsptr[DCTSIZE*0] = tmp0 + tmp7;
	wsptr[DCTSIZE*7] = tmp0 - tmp7;
	wsptr[DCTSIZE*1] = tmp1 + tmp6;
	wsptr[DCTSIZE*6] = tmp1 - tmp6;
	wsptr[DCTSIZE*2] = tmp2 + tmp5;
	wsptr[DCTSIZE*5] = tmp2 - tmp5;
	wsptr[DCTSIZE*4] = tmp3 + tmp4;
	wsptr[DCTSIZE*3] = tmp3 - tmp4;

	inptr++;			/* advance pointers to next column */
	quantptr++;
	wsptr++;
  }

  wsptr = workspace;
  for (ctr = 0; ctr < DCTSIZE; ctr++) {
	outptr = output_buf[ctr] + output_col;

	tmp10 = wsptr[0] + wsptr[4];
	tmp11 = wsptr[0] - wsptr[4];

	tmp13 = wsptr[2] + wsptr[6];
	tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;

	tmp0 = tmp10 + tmp13;
	tmp3 = tmp10 - tmp13;
	tmp1 = tmp11 + tmp12;
	tmp2 = tmp11 - tmp12;

	z13 = wsptr[5] + wsptr[3];
	z10 = wsptr[5] - wsptr[3];
	z11 = wsptr[1] + wsptr[7];
	z12 = wsptr[1] - wsptr[7];

	tmp7 = z11 + z13;
	tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);

	z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
	tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
	tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */

	tmp6 = tmp12 - tmp7;
	tmp5 = tmp11 - tmp6;
	tmp4 = tmp10 + tmp5;

	outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3)
				& RANGE_MASK];
	outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3)
				& RANGE_MASK];
	outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3)
				& RANGE_MASK];
	outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3)
				& RANGE_MASK];
	outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3)
				& RANGE_MASK];
	outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3)
				& RANGE_MASK];
	outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3)
				& RANGE_MASK];
	outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3)
				& RANGE_MASK];

	wsptr += DCTSIZE;		/* advance pointer to next row */
  }
}

#endif /* DCT_FLOAT_SUPPORTED */
/*** End of inlined file: jidctflt.c ***/


	#undef CONST_BITS
	#undef FIX_1_847759065
	#undef MULTIPLY
	#undef DEQUANTIZE
	#undef DESCALE

/*** Start of inlined file: jidctfst.c ***/
#define JPEG_INTERNALS

#ifdef DCT_IFAST_SUPPORTED

#if DCTSIZE != 8
  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
#endif

#if BITS_IN_JSAMPLE == 8
#define CONST_BITS  8
#define PASS1_BITS  2
#else
#define CONST_BITS  8
#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
#endif

#if CONST_BITS == 8
#define FIX_1_082392200  ((INT32)  277)		/* FIX(1.082392200) */
#define FIX_1_414213562  ((INT32)  362)		/* FIX(1.414213562) */
#define FIX_1_847759065  ((INT32)  473)		/* FIX(1.847759065) */
#define FIX_2_613125930  ((INT32)  669)		/* FIX(2.613125930) */
#else
#define FIX_1_082392200  FIX(1.082392200)
#define FIX_1_414213562  FIX(1.414213562)
#define FIX_1_847759065  FIX(1.847759065)
#define FIX_2_613125930  FIX(2.613125930)
#endif

#ifndef USE_ACCURATE_ROUNDING
#undef DESCALE
#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
#endif

#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))

#if BITS_IN_JSAMPLE == 8
#define DEQUANTIZE(coef,quantval)  (((IFAST_MULT_TYPE) (coef)) * (quantval))
#else
#define DEQUANTIZE(coef,quantval)  \
	DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
#endif

#ifdef RIGHT_SHIFT_IS_UNSIGNED
#define ISHIFT_TEMPS	DCTELEM ishift_temp;
#if BITS_IN_JSAMPLE == 8
#define DCTELEMBITS  16		/* DCTELEM may be 16 or 32 bits */
#else
#define DCTELEMBITS  32		/* DCTELEM must be 32 bits */
#endif
#define IRIGHT_SHIFT(x,shft)  \
	((ishift_temp = (x)) < 0 ? \
	 (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
	 (ishift_temp >> (shft)))
#else
#define ISHIFT_TEMPS
#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
#endif

#ifdef USE_ACCURATE_ROUNDING
#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
#else
#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT(x, n))
#endif

GLOBAL(void)
jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		 JCOEFPTR coef_block,
		 JSAMPARRAY output_buf, JDIMENSION output_col)
{
  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  DCTELEM tmp10, tmp11, tmp12, tmp13;
  DCTELEM z5, z10, z11, z12, z13;
  JCOEFPTR inptr;
  IFAST_MULT_TYPE * quantptr;
  int * wsptr;
  JSAMPROW outptr;
  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  int ctr;
  int workspace[DCTSIZE2];	/* buffers data between passes */
  SHIFT_TEMPS			/* for DESCALE */
  ISHIFT_TEMPS			/* for IDESCALE */

  inptr = coef_block;
  quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
  wsptr = workspace;
  for (ctr = DCTSIZE; ctr > 0; ctr--) {

	if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
	inptr[DCTSIZE*7] == 0) {
	  int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);

	  wsptr[DCTSIZE*0] = dcval;
	  wsptr[DCTSIZE*1] = dcval;
	  wsptr[DCTSIZE*2] = dcval;
	  wsptr[DCTSIZE*3] = dcval;
	  wsptr[DCTSIZE*4] = dcval;
	  wsptr[DCTSIZE*5] = dcval;
	  wsptr[DCTSIZE*6] = dcval;
	  wsptr[DCTSIZE*7] = dcval;

	  inptr++;			/* advance pointers to next column */
	  quantptr++;
	  wsptr++;
	  continue;
	}

	tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
	tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
	tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
	tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);

	tmp10 = tmp0 + tmp2;	/* phase 3 */
	tmp11 = tmp0 - tmp2;

	tmp13 = tmp1 + tmp3;	/* phases 5-3 */
	tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */

	tmp0 = tmp10 + tmp13;	/* phase 2 */
	tmp3 = tmp10 - tmp13;
	tmp1 = tmp11 + tmp12;
	tmp2 = tmp11 - tmp12;

	tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
	tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
	tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
	tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);

	z13 = tmp6 + tmp5;		/* phase 6 */
	z10 = tmp6 - tmp5;
	z11 = tmp4 + tmp7;
	z12 = tmp4 - tmp7;

	tmp7 = z11 + z13;		/* phase 5 */
	tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */

	z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
	tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
	tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */

	tmp6 = tmp12 - tmp7;	/* phase 2 */
	tmp5 = tmp11 - tmp6;
	tmp4 = tmp10 + tmp5;

	wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
	wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
	wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
	wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
	wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
	wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
	wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
	wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);

	inptr++;			/* advance pointers to next column */
	quantptr++;
	wsptr++;
  }

  wsptr = workspace;
  for (ctr = 0; ctr < DCTSIZE; ctr++) {
	outptr = output_buf[ctr] + output_col;

#ifndef NO_ZERO_ROW_TEST
	if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
	  JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
				  & RANGE_MASK];

	  outptr[0] = dcval;
	  outptr[1] = dcval;
	  outptr[2] = dcval;
	  outptr[3] = dcval;
	  outptr[4] = dcval;
	  outptr[5] = dcval;
	  outptr[6] = dcval;
	  outptr[7] = dcval;

	  wsptr += DCTSIZE;		/* advance pointer to next row */
	  continue;
	}
#endif

	tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
	tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);

	tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
	tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
		- tmp13;

	tmp0 = tmp10 + tmp13;
	tmp3 = tmp10 - tmp13;
	tmp1 = tmp11 + tmp12;
	tmp2 = tmp11 - tmp12;

	z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
	z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
	z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
	z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];

	tmp7 = z11 + z13;		/* phase 5 */
	tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */

	z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
	tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
	tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */

	tmp6 = tmp12 - tmp7;	/* phase 2 */
	tmp5 = tmp11 - tmp6;
	tmp4 = tmp10 + tmp5;

	outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
				& RANGE_MASK];
	outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
				& RANGE_MASK];
	outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
				& RANGE_MASK];
	outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
				& RANGE_MASK];
	outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
				& RANGE_MASK];
	outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
				& RANGE_MASK];
	outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
				& RANGE_MASK];
	outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
				& RANGE_MASK];

	wsptr += DCTSIZE;		/* advance pointer to next row */
  }
}

#endif /* DCT_IFAST_SUPPORTED */
/*** End of inlined file: jidctfst.c ***/


	#undef CONST_BITS
	#undef FIX_1_847759065
	#undef MULTIPLY
	#undef DEQUANTIZE

/*** Start of inlined file: jidctint.c ***/
#define JPEG_INTERNALS

#ifdef DCT_ISLOW_SUPPORTED

#if DCTSIZE != 8
  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
#endif

#if BITS_IN_JSAMPLE == 8
#define CONST_BITS  13
#define PASS1_BITS  2
#else
#define CONST_BITS  13
#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
#endif

#if CONST_BITS == 13
#define FIX_0_298631336  ((INT32)  2446)	/* FIX(0.298631336) */
#define FIX_0_390180644  ((INT32)  3196)	/* FIX(0.390180644) */
#define FIX_0_541196100  ((INT32)  4433)	/* FIX(0.541196100) */
#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
#define FIX_1_175875602  ((INT32)  9633)	/* FIX(1.175875602) */
#define FIX_1_501321110  ((INT32)  12299)	/* FIX(1.501321110) */
#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
#define FIX_1_961570560  ((INT32)  16069)	/* FIX(1.961570560) */
#define FIX_2_053119869  ((INT32)  16819)	/* FIX(2.053119869) */
#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
#define FIX_3_072711026  ((INT32)  25172)	/* FIX(3.072711026) */
#else
#define FIX_0_298631336  FIX(0.298631336)
#define FIX_0_390180644  FIX(0.390180644)
#define FIX_0_541196100  FIX(0.541196100)
#define FIX_0_765366865  FIX(0.765366865)
#define FIX_0_899976223  FIX(0.899976223)
#define FIX_1_175875602  FIX(1.175875602)
#define FIX_1_501321110  FIX(1.501321110)
#define FIX_1_847759065  FIX(1.847759065)
#define FIX_1_961570560  FIX(1.961570560)
#define FIX_2_053119869  FIX(2.053119869)
#define FIX_2_562915447  FIX(2.562915447)
#define FIX_3_072711026  FIX(3.072711026)
#endif

#if BITS_IN_JSAMPLE == 8
#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
#else
#define MULTIPLY(var,const)  ((var) * (const))
#endif

#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))

GLOBAL(void)
jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		 JCOEFPTR coef_block,
		 JSAMPARRAY output_buf, JDIMENSION output_col)
{
  INT32 tmp0, tmp1, tmp2, tmp3;
  INT32 tmp10, tmp11, tmp12, tmp13;
  INT32 z1, z2, z3, z4, z5;
  JCOEFPTR inptr;
  ISLOW_MULT_TYPE * quantptr;
  int * wsptr;
  JSAMPROW outptr;
  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  int ctr;
  int workspace[DCTSIZE2];	/* buffers data between passes */
  SHIFT_TEMPS

  inptr = coef_block;
  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  wsptr = workspace;
  for (ctr = DCTSIZE; ctr > 0; ctr--) {

	if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
	inptr[DCTSIZE*7] == 0) {
	  int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;

	  wsptr[DCTSIZE*0] = dcval;
	  wsptr[DCTSIZE*1] = dcval;
	  wsptr[DCTSIZE*2] = dcval;
	  wsptr[DCTSIZE*3] = dcval;
	  wsptr[DCTSIZE*4] = dcval;
	  wsptr[DCTSIZE*5] = dcval;
	  wsptr[DCTSIZE*6] = dcval;
	  wsptr[DCTSIZE*7] = dcval;

	  inptr++;			/* advance pointers to next column */
	  quantptr++;
	  wsptr++;
	  continue;
	}

	z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
	z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);

	z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
	tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
	tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);

	z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
	z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);

	tmp0 = (z2 + z3) << CONST_BITS;
	tmp1 = (z2 - z3) << CONST_BITS;

	tmp10 = tmp0 + tmp3;
	tmp13 = tmp0 - tmp3;
	tmp11 = tmp1 + tmp2;
	tmp12 = tmp1 - tmp2;

	tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
	tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
	tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
	tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);

	z1 = tmp0 + tmp3;
	z2 = tmp1 + tmp2;
	z3 = tmp0 + tmp2;
	z4 = tmp1 + tmp3;
	z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */

	tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
	tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
	tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
	tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
	z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
	z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
	z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
	z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */

	z3 += z5;
	z4 += z5;

	tmp0 += z1 + z3;
	tmp1 += z2 + z4;
	tmp2 += z2 + z3;
	tmp3 += z1 + z4;

	wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
	wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
	wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
	wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
	wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
	wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
	wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
	wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);

	inptr++;			/* advance pointers to next column */
	quantptr++;
	wsptr++;
  }

  wsptr = workspace;
  for (ctr = 0; ctr < DCTSIZE; ctr++) {
	outptr = output_buf[ctr] + output_col;

#ifndef NO_ZERO_ROW_TEST
	if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
	  JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
				  & RANGE_MASK];

	  outptr[0] = dcval;
	  outptr[1] = dcval;
	  outptr[2] = dcval;
	  outptr[3] = dcval;
	  outptr[4] = dcval;
	  outptr[5] = dcval;
	  outptr[6] = dcval;
	  outptr[7] = dcval;

	  wsptr += DCTSIZE;		/* advance pointer to next row */
	  continue;
	}
#endif

	z2 = (INT32) wsptr[2];
	z3 = (INT32) wsptr[6];

	z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
	tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
	tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);

	tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
	tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;

	tmp10 = tmp0 + tmp3;
	tmp13 = tmp0 - tmp3;
	tmp11 = tmp1 + tmp2;
	tmp12 = tmp1 - tmp2;

	tmp0 = (INT32) wsptr[7];
	tmp1 = (INT32) wsptr[5];
	tmp2 = (INT32) wsptr[3];
	tmp3 = (INT32) wsptr[1];

	z1 = tmp0 + tmp3;
	z2 = tmp1 + tmp2;
	z3 = tmp0 + tmp2;
	z4 = tmp1 + tmp3;
	z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */

	tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
	tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
	tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
	tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
	z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
	z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
	z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
	z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */

	z3 += z5;
	z4 += z5;

	tmp0 += z1 + z3;
	tmp1 += z2 + z4;
	tmp2 += z2 + z3;
	tmp3 += z1 + z4;

	outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
					  CONST_BITS+PASS1_BITS+3)
				& RANGE_MASK];
	outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
					  CONST_BITS+PASS1_BITS+3)
				& RANGE_MASK];
	outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
					  CONST_BITS+PASS1_BITS+3)
				& RANGE_MASK];
	outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
					  CONST_BITS+PASS1_BITS+3)
				& RANGE_MASK];
	outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
					  CONST_BITS+PASS1_BITS+3)
				& RANGE_MASK];
	outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
					  CONST_BITS+PASS1_BITS+3)
				& RANGE_MASK];
	outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
					  CONST_BITS+PASS1_BITS+3)
				& RANGE_MASK];
	outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
					  CONST_BITS+PASS1_BITS+3)
				& RANGE_MASK];

	wsptr += DCTSIZE;		/* advance pointer to next row */
  }
}

#endif /* DCT_ISLOW_SUPPORTED */
/*** End of inlined file: jidctint.c ***/



/*** Start of inlined file: jidctred.c ***/
#define JPEG_INTERNALS

#ifdef IDCT_SCALING_SUPPORTED

#if DCTSIZE != 8
  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
#endif

#if BITS_IN_JSAMPLE == 8
#define CONST_BITS  13
#define PASS1_BITS  2
#else
#define CONST_BITS  13
#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
#endif

#if CONST_BITS == 13
#define FIX_0_211164243  ((INT32)  1730)	/* FIX(0.211164243) */
#define FIX_0_509795579  ((INT32)  4176)	/* FIX(0.509795579) */
#define FIX_0_601344887  ((INT32)  4926)	/* FIX(0.601344887) */
#define FIX_0_720959822  ((INT32)  5906)	/* FIX(0.720959822) */
#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
#define FIX_0_850430095  ((INT32)  6967)	/* FIX(0.850430095) */
#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
#define FIX_1_061594337  ((INT32)  8697)	/* FIX(1.061594337) */
#define FIX_1_272758580  ((INT32)  10426)	/* FIX(1.272758580) */
#define FIX_1_451774981  ((INT32)  11893)	/* FIX(1.451774981) */
#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
#define FIX_2_172734803  ((INT32)  17799)	/* FIX(2.172734803) */
#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
#define FIX_3_624509785  ((INT32)  29692)	/* FIX(3.624509785) */
#else
#define FIX_0_211164243  FIX(0.211164243)
#define FIX_0_509795579  FIX(0.509795579)
#define FIX_0_601344887  FIX(0.601344887)
#define FIX_0_720959822  FIX(0.720959822)
#define FIX_0_765366865  FIX(0.765366865)
#define FIX_0_850430095  FIX(0.850430095)
#define FIX_0_899976223  FIX(0.899976223)
#define FIX_1_061594337  FIX(1.061594337)
#define FIX_1_272758580  FIX(1.272758580)
#define FIX_1_451774981  FIX(1.451774981)
#define FIX_1_847759065  FIX(1.847759065)
#define FIX_2_172734803  FIX(2.172734803)
#define FIX_2_562915447  FIX(2.562915447)
#define FIX_3_624509785  FIX(3.624509785)
#endif

#if BITS_IN_JSAMPLE == 8
#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
#else
#define MULTIPLY(var,const)  ((var) * (const))
#endif

#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))

GLOBAL(void)
jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		   JCOEFPTR coef_block,
		   JSAMPARRAY output_buf, JDIMENSION output_col)
{
  INT32 tmp0, tmp2, tmp10, tmp12;
  INT32 z1, z2, z3, z4;
  JCOEFPTR inptr;
  ISLOW_MULT_TYPE * quantptr;
  int * wsptr;
  JSAMPROW outptr;
  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  int ctr;
  int workspace[DCTSIZE*4];	/* buffers data between passes */
  SHIFT_TEMPS

  inptr = coef_block;
  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  wsptr = workspace;
  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
	if (ctr == DCTSIZE-4)
	  continue;
	if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
	inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
	  int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;

	  wsptr[DCTSIZE*0] = dcval;
	  wsptr[DCTSIZE*1] = dcval;
	  wsptr[DCTSIZE*2] = dcval;
	  wsptr[DCTSIZE*3] = dcval;

	  continue;
	}

	tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
	tmp0 <<= (CONST_BITS+1);

	z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
	z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);

	tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);

	tmp10 = tmp0 + tmp2;
	tmp12 = tmp0 - tmp2;

	z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
	z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
	z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
	z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);

	tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */

	tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */

	wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
	wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
	wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
	wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
  }

  wsptr = workspace;
  for (ctr = 0; ctr < 4; ctr++) {
	outptr = output_buf[ctr] + output_col;

#ifndef NO_ZERO_ROW_TEST
	if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
	  JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
				  & RANGE_MASK];

	  outptr[0] = dcval;
	  outptr[1] = dcval;
	  outptr[2] = dcval;
	  outptr[3] = dcval;

	  wsptr += DCTSIZE;		/* advance pointer to next row */
	  continue;
	}
#endif

	tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);

	tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
	 + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);

	tmp10 = tmp0 + tmp2;
	tmp12 = tmp0 - tmp2;

	z1 = (INT32) wsptr[7];
	z2 = (INT32) wsptr[5];
	z3 = (INT32) wsptr[3];
	z4 = (INT32) wsptr[1];

	tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */

	tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */

	outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
					  CONST_BITS+PASS1_BITS+3+1)
				& RANGE_MASK];
	outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
					  CONST_BITS+PASS1_BITS+3+1)
				& RANGE_MASK];
	outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
					  CONST_BITS+PASS1_BITS+3+1)
				& RANGE_MASK];
	outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
					  CONST_BITS+PASS1_BITS+3+1)
				& RANGE_MASK];

	wsptr += DCTSIZE;		/* advance pointer to next row */
  }
}

GLOBAL(void)
jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		   JCOEFPTR coef_block,
		   JSAMPARRAY output_buf, JDIMENSION output_col)
{
  INT32 tmp0, tmp10, z1;
  JCOEFPTR inptr;
  ISLOW_MULT_TYPE * quantptr;
  int * wsptr;
  JSAMPROW outptr;
  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  int ctr;
  int workspace[DCTSIZE*2];	/* buffers data between passes */
  SHIFT_TEMPS

  inptr = coef_block;
  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  wsptr = workspace;
  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
	if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
	  continue;
	if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
	  int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;

	  wsptr[DCTSIZE*0] = dcval;
	  wsptr[DCTSIZE*1] = dcval;

	  continue;
	}

	z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
	tmp10 = z1 << (CONST_BITS+2);

	z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
	tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
	z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
	tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
	z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
	tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
	z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
	tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */

	wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
	wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
  }

  wsptr = workspace;
  for (ctr = 0; ctr < 2; ctr++) {
	outptr = output_buf[ctr] + output_col;

#ifndef NO_ZERO_ROW_TEST
	if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
	  JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
				  & RANGE_MASK];

	  outptr[0] = dcval;
	  outptr[1] = dcval;

	  wsptr += DCTSIZE;		/* advance pointer to next row */
	  continue;
	}
#endif

	tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);

	tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
	 + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
	 + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
	 + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */

	outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
					  CONST_BITS+PASS1_BITS+3+2)
				& RANGE_MASK];
	outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
					  CONST_BITS+PASS1_BITS+3+2)
				& RANGE_MASK];

	wsptr += DCTSIZE;		/* advance pointer to next row */
  }
}

GLOBAL(void)
jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
		   JCOEFPTR coef_block,
		   JSAMPARRAY output_buf, JDIMENSION output_col)
{
  int dcval;
  ISLOW_MULT_TYPE * quantptr;
  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  SHIFT_TEMPS

  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
  dcval = (int) DESCALE((INT32) dcval, 3);

  output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
}

#endif /* IDCT_SCALING_SUPPORTED */
/*** End of inlined file: jidctred.c ***/


/*** Start of inlined file: jmemmgr.c ***/
#define JPEG_INTERNALS
#define AM_MEMORY_MANAGER	/* we define jvirt_Xarray_control structs */


/*** Start of inlined file: jmemsys.h ***/
#ifndef __jmemsys_h__
#define __jmemsys_h__

#ifdef NEED_SHORT_EXTERNAL_NAMES
#define jpeg_get_small		jGetSmall
#define jpeg_free_small		jFreeSmall
#define jpeg_get_large		jGetLarge
#define jpeg_free_large		jFreeLarge
#define jpeg_mem_available	jMemAvail
#define jpeg_open_backing_store	jOpenBackStore
#define jpeg_mem_init		jMemInit
#define jpeg_mem_term		jMemTerm
#endif /* NEED_SHORT_EXTERNAL_NAMES */

EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
				  size_t sizeofobject));

EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
					   size_t sizeofobject));
EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
				  size_t sizeofobject));

#ifndef MAX_ALLOC_CHUNK		/* may be overridden in jconfig.h */
#define MAX_ALLOC_CHUNK  1000000000L
#endif

EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,
					 long min_bytes_needed,
					 long max_bytes_needed,
					 long already_allocated));

#define TEMP_NAME_LENGTH   64	/* max length of a temporary file's name */

#ifdef USE_MSDOS_MEMMGR		/* DOS-specific junk */

typedef unsigned short XMSH;	/* type of extended-memory handles */
typedef unsigned short EMSH;	/* type of expanded-memory handles */

typedef union {
  short file_handle;		/* DOS file handle if it's a temp file */
  XMSH xms_handle;		/* handle if it's a chunk of XMS */
  EMSH ems_handle;		/* handle if it's a chunk of EMS */
} handle_union;

#endif /* USE_MSDOS_MEMMGR */

#ifdef USE_MAC_MEMMGR		/* Mac-specific junk */
#include <Files.h>
#endif /* USE_MAC_MEMMGR */

//typedef struct backing_store_struct * backing_store_ptr;

typedef struct backing_store_struct {
  JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
					 struct backing_store_struct *info,
					 void FAR * buffer_address,
					 long file_offset, long byte_count));
  JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
					  struct backing_store_struct *info,
					  void FAR * buffer_address,
					  long file_offset, long byte_count));
  JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
					  struct backing_store_struct *info));

#ifdef USE_MSDOS_MEMMGR
  handle_union handle;		/* reference to backing-store storage object */
  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
#else
#ifdef USE_MAC_MEMMGR
  short temp_file;		/* file reference number to temp file */
  FSSpec tempSpec;		/* the FSSpec for the temp file */
  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
#else
  FILE * temp_file;		/* stdio reference to temp file */
  char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
#endif
#endif
} backing_store_info;

EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
					  struct backing_store_struct *info,
					  long total_bytes_needed));

EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo));
EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));

#endif
/*** End of inlined file: jmemsys.h ***/

		/* import the system-dependent declarations */

#ifndef NO_GETENV
#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare getenv() */
extern char * getenv JPP((const char * name));
#endif
#endif

#ifndef ALIGN_TYPE		/* so can override from jconfig.h */
#define ALIGN_TYPE  double
#endif

typedef union small_pool_struct * small_pool_ptr;

typedef union small_pool_struct {
  struct {
	small_pool_ptr next;	/* next in list of pools */
	size_t bytes_used;		/* how many bytes already used within pool */
	size_t bytes_left;		/* bytes still available in this pool */
  } hdr;
  ALIGN_TYPE dummy;		/* included in union to ensure alignment */
} small_pool_hdr;

typedef union large_pool_struct FAR * large_pool_ptr;

typedef union large_pool_struct {
  struct {
	large_pool_ptr next;	/* next in list of pools */
	size_t bytes_used;		/* how many bytes already used within pool */
	size_t bytes_left;		/* bytes still available in this pool */
  } hdr;
  ALIGN_TYPE dummy;		/* included in union to ensure alignment */
} large_pool_hdr;

typedef struct {
  struct jpeg_memory_mgr pub;	/* public fields */

  small_pool_ptr small_list[JPOOL_NUMPOOLS];
  large_pool_ptr large_list[JPOOL_NUMPOOLS];

  jvirt_sarray_ptr virt_sarray_list;
  jvirt_barray_ptr virt_barray_list;

  long total_space_allocated;

  JDIMENSION last_rowsperchunk;	/* from most recent alloc_sarray/barray */
} my_memory_mgr;

typedef my_memory_mgr * my_mem_ptr;

struct jvirt_sarray_control {
  JSAMPARRAY mem_buffer;	/* => the in-memory buffer */
  JDIMENSION rows_in_array;	/* total virtual array height */
  JDIMENSION samplesperrow;	/* width of array (and of memory buffer) */
  JDIMENSION maxaccess;		/* max rows accessed by access_virt_sarray */
  JDIMENSION rows_in_mem;	/* height of memory buffer */
  JDIMENSION rowsperchunk;	/* allocation chunk size in mem_buffer */
  JDIMENSION cur_start_row;	/* first logical row # in the buffer */
  JDIMENSION first_undef_row;	/* row # of first uninitialized row */
  boolean pre_zero;		/* pre-zero mode requested? */
  boolean dirty;		/* do current buffer contents need written? */
  boolean b_s_open;		/* is backing-store data valid? */
  jvirt_sarray_ptr next;	/* link to next virtual sarray control block */
  backing_store_info b_s_info;	/* System-dependent control info */
};

struct jvirt_barray_control {
  JBLOCKARRAY mem_buffer;	/* => the in-memory buffer */
  JDIMENSION rows_in_array;	/* total virtual array height */
  JDIMENSION blocksperrow;	/* width of array (and of memory buffer) */
  JDIMENSION maxaccess;		/* max rows accessed by access_virt_barray */
  JDIMENSION rows_in_mem;	/* height of memory buffer */
  JDIMENSION rowsperchunk;	/* allocation chunk size in mem_buffer */
  JDIMENSION cur_start_row;	/* first logical row # in the buffer */
  JDIMENSION first_undef_row;	/* row # of first uninitialized row */
  boolean pre_zero;		/* pre-zero mode requested? */
  boolean dirty;		/* do current buffer contents need written? */
  boolean b_s_open;		/* is backing-store data valid? */
  jvirt_barray_ptr next;	/* link to next virtual barray control block */
  backing_store_info b_s_info;	/* System-dependent control info */
};

#ifdef MEM_STATS		/* optional extra stuff for statistics */

LOCAL(void)
print_mem_stats (j_common_ptr cinfo, int pool_id)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  small_pool_ptr shdr_ptr;
  large_pool_ptr lhdr_ptr;

  fprintf(stderr, "Freeing pool %d, total space = %ld\n",
	  pool_id, mem->total_space_allocated);

  for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
	   lhdr_ptr = lhdr_ptr->hdr.next) {
	fprintf(stderr, "  Large chunk used %ld\n",
		(long) lhdr_ptr->hdr.bytes_used);
  }

  for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
	   shdr_ptr = shdr_ptr->hdr.next) {
	fprintf(stderr, "  Small chunk used %ld free %ld\n",
		(long) shdr_ptr->hdr.bytes_used,
		(long) shdr_ptr->hdr.bytes_left);
  }
}

#endif /* MEM_STATS */

LOCAL(void)
out_of_memory (j_common_ptr cinfo, int which)
{
#ifdef MEM_STATS
  cinfo->err->trace_level = 2;	/* force self_destruct to report stats */
#endif
  ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
}

static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
{
	1600,			/* first PERMANENT pool */
	16000			/* first IMAGE pool */
};

static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
{
	0,			/* additional PERMANENT pools */
	5000			/* additional IMAGE pools */
};

#define MIN_SLOP  50		/* greater than 0 to avoid futile looping */

METHODDEF(void *)
alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  small_pool_ptr hdr_ptr, prev_hdr_ptr;
  char * data_ptr;
  size_t odd_bytes, min_request, slop;

  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
	out_of_memory(cinfo, 1);	/* request exceeds malloc's ability */

  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  if (odd_bytes > 0)
	sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;

  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
	ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  prev_hdr_ptr = NULL;
  hdr_ptr = mem->small_list[pool_id];
  while (hdr_ptr != NULL) {
	if (hdr_ptr->hdr.bytes_left >= sizeofobject)
	  break;			/* found pool with enough space */
	prev_hdr_ptr = hdr_ptr;
	hdr_ptr = hdr_ptr->hdr.next;
  }

  if (hdr_ptr == NULL) {
	min_request = sizeofobject + SIZEOF(small_pool_hdr);
	if (prev_hdr_ptr == NULL)	/* first pool in class? */
	  slop = first_pool_slop[pool_id];
	else
	  slop = extra_pool_slop[pool_id];
	if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
	  slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
	for (;;) {
	  hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
	  if (hdr_ptr != NULL)
	break;
	  slop /= 2;
	  if (slop < MIN_SLOP)	/* give up when it gets real small */
	out_of_memory(cinfo, 2); /* jpeg_get_small failed */
	}
	mem->total_space_allocated += min_request + slop;
	hdr_ptr->hdr.next = NULL;
	hdr_ptr->hdr.bytes_used = 0;
	hdr_ptr->hdr.bytes_left = sizeofobject + slop;
	if (prev_hdr_ptr == NULL)	/* first pool in class? */
	  mem->small_list[pool_id] = hdr_ptr;
	else
	  prev_hdr_ptr->hdr.next = hdr_ptr;
  }

  data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
  data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
  hdr_ptr->hdr.bytes_used += sizeofobject;
  hdr_ptr->hdr.bytes_left -= sizeofobject;

  return (void *) data_ptr;
}

METHODDEF(void FAR *)
alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  large_pool_ptr hdr_ptr;
  size_t odd_bytes;

  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
	out_of_memory(cinfo, 3);	/* request exceeds malloc's ability */

  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  if (odd_bytes > 0)
	sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;

  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
	ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */

  hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
						SIZEOF(large_pool_hdr));
  if (hdr_ptr == NULL)
	out_of_memory(cinfo, 4);	/* jpeg_get_large failed */
  mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);

  hdr_ptr->hdr.next = mem->large_list[pool_id];
  hdr_ptr->hdr.bytes_used = sizeofobject;
  hdr_ptr->hdr.bytes_left = 0;
  mem->large_list[pool_id] = hdr_ptr;

  return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
}

METHODDEF(JSAMPARRAY)
alloc_sarray (j_common_ptr cinfo, int pool_id,
		  JDIMENSION samplesperrow, JDIMENSION numrows)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  JSAMPARRAY result;
  JSAMPROW workspace;
  JDIMENSION rowsperchunk, currow, i;
  long ltemp;

  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
	  ((long) samplesperrow * SIZEOF(JSAMPLE));
  if (ltemp <= 0)
	ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  if (ltemp < (long) numrows)
	rowsperchunk = (JDIMENSION) ltemp;
  else
	rowsperchunk = numrows;
  mem->last_rowsperchunk = rowsperchunk;

  result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
					(size_t) (numrows * SIZEOF(JSAMPROW)));

  currow = 0;
  while (currow < numrows) {
	rowsperchunk = MIN(rowsperchunk, numrows - currow);
	workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
	(size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
		  * SIZEOF(JSAMPLE)));
	for (i = rowsperchunk; i > 0; i--) {
	  result[currow++] = workspace;
	  workspace += samplesperrow;
	}
  }

  return result;
}

METHODDEF(JBLOCKARRAY)
alloc_barray (j_common_ptr cinfo, int pool_id,
		  JDIMENSION blocksperrow, JDIMENSION numrows)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  JBLOCKARRAY result;
  JBLOCKROW workspace;
  JDIMENSION rowsperchunk, currow, i;
  long ltemp;

  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
	  ((long) blocksperrow * SIZEOF(JBLOCK));
  if (ltemp <= 0)
	ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  if (ltemp < (long) numrows)
	rowsperchunk = (JDIMENSION) ltemp;
  else
	rowsperchunk = numrows;
  mem->last_rowsperchunk = rowsperchunk;

  result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
					 (size_t) (numrows * SIZEOF(JBLOCKROW)));

  currow = 0;
  while (currow < numrows) {
	rowsperchunk = MIN(rowsperchunk, numrows - currow);
	workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
	(size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
		  * SIZEOF(JBLOCK)));
	for (i = rowsperchunk; i > 0; i--) {
	  result[currow++] = workspace;
	  workspace += blocksperrow;
	}
  }

  return result;
}

METHODDEF(jvirt_sarray_ptr)
request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
			 JDIMENSION samplesperrow, JDIMENSION numrows,
			 JDIMENSION maxaccess)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  jvirt_sarray_ptr result;

  if (pool_id != JPOOL_IMAGE)
	ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */

  result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
					  SIZEOF(struct jvirt_sarray_control));

  result->mem_buffer = NULL;	/* marks array not yet realized */
  result->rows_in_array = numrows;
  result->samplesperrow = samplesperrow;
  result->maxaccess = maxaccess;
  result->pre_zero = pre_zero;
  result->b_s_open = FALSE;	/* no associated backing-store object */
  result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
  mem->virt_sarray_list = result;

  return result;
}

METHODDEF(jvirt_barray_ptr)
request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
			 JDIMENSION blocksperrow, JDIMENSION numrows,
			 JDIMENSION maxaccess)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  jvirt_barray_ptr result;

  if (pool_id != JPOOL_IMAGE)
	ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */

  result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
					  SIZEOF(struct jvirt_barray_control));

  result->mem_buffer = NULL;	/* marks array not yet realized */
  result->rows_in_array = numrows;
  result->blocksperrow = blocksperrow;
  result->maxaccess = maxaccess;
  result->pre_zero = pre_zero;
  result->b_s_open = FALSE;	/* no associated backing-store object */
  result->next = mem->virt_barray_list; /* add to list of virtual arrays */
  mem->virt_barray_list = result;

  return result;
}

METHODDEF(void)
realize_virt_arrays (j_common_ptr cinfo)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  long space_per_minheight, maximum_space, avail_mem;
  long minheights, max_minheights;
  jvirt_sarray_ptr sptr;
  jvirt_barray_ptr bptr;

  space_per_minheight = 0;
  maximum_space = 0;
  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
	if (sptr->mem_buffer == NULL) { /* if not realized yet */
	  space_per_minheight += (long) sptr->maxaccess *
				 (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
	  maximum_space += (long) sptr->rows_in_array *
			   (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
	}
  }
  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
	if (bptr->mem_buffer == NULL) { /* if not realized yet */
	  space_per_minheight += (long) bptr->maxaccess *
				 (long) bptr->blocksperrow * SIZEOF(JBLOCK);
	  maximum_space += (long) bptr->rows_in_array *
			   (long) bptr->blocksperrow * SIZEOF(JBLOCK);
	}
  }

  if (space_per_minheight <= 0)
	return;			/* no unrealized arrays, no work */

  avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
				 mem->total_space_allocated);

  if (avail_mem >= maximum_space)
	max_minheights = 1000000000L;
  else {
	max_minheights = avail_mem / space_per_minheight;
	if (max_minheights <= 0)
	  max_minheights = 1;
  }

  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
	if (sptr->mem_buffer == NULL) { /* if not realized yet */
	  minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
	  if (minheights <= max_minheights) {
	sptr->rows_in_mem = sptr->rows_in_array;
	  } else {
	sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
	jpeg_open_backing_store(cinfo, & sptr->b_s_info,
				(long) sptr->rows_in_array *
				(long) sptr->samplesperrow *
				(long) SIZEOF(JSAMPLE));
	sptr->b_s_open = TRUE;
	  }
	  sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
					  sptr->samplesperrow, sptr->rows_in_mem);
	  sptr->rowsperchunk = mem->last_rowsperchunk;
	  sptr->cur_start_row = 0;
	  sptr->first_undef_row = 0;
	  sptr->dirty = FALSE;
	}
  }

  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
	if (bptr->mem_buffer == NULL) { /* if not realized yet */
	  minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
	  if (minheights <= max_minheights) {
	bptr->rows_in_mem = bptr->rows_in_array;
	  } else {
	bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
	jpeg_open_backing_store(cinfo, & bptr->b_s_info,
				(long) bptr->rows_in_array *
				(long) bptr->blocksperrow *
				(long) SIZEOF(JBLOCK));
	bptr->b_s_open = TRUE;
	  }
	  bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
					  bptr->blocksperrow, bptr->rows_in_mem);
	  bptr->rowsperchunk = mem->last_rowsperchunk;
	  bptr->cur_start_row = 0;
	  bptr->first_undef_row = 0;
	  bptr->dirty = FALSE;
	}
  }
}

LOCAL(void)
do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
{
  long bytesperrow, file_offset, byte_count, rows, thisrow, i;

  bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
  file_offset = ptr->cur_start_row * bytesperrow;
  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
	rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
	thisrow = (long) ptr->cur_start_row + i;
	rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
	rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
	if (rows <= 0)		/* this chunk might be past end of file! */
	  break;
	byte_count = rows * bytesperrow;
	if (writing)
	  (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
						(void FAR *) ptr->mem_buffer[i],
						file_offset, byte_count);
	else
	  (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
					   (void FAR *) ptr->mem_buffer[i],
					   file_offset, byte_count);
	file_offset += byte_count;
  }
}

LOCAL(void)
do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
{
  long bytesperrow, file_offset, byte_count, rows, thisrow, i;

  bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
  file_offset = ptr->cur_start_row * bytesperrow;
  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
	rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
	thisrow = (long) ptr->cur_start_row + i;
	rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
	rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
	if (rows <= 0)		/* this chunk might be past end of file! */
	  break;
	byte_count = rows * bytesperrow;
	if (writing)
	  (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
						(void FAR *) ptr->mem_buffer[i],
						file_offset, byte_count);
	else
	  (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
					   (void FAR *) ptr->mem_buffer[i],
					   file_offset, byte_count);
	file_offset += byte_count;
  }
}

METHODDEF(JSAMPARRAY)
access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
			JDIMENSION start_row, JDIMENSION num_rows,
			boolean writable)
{
  JDIMENSION end_row = start_row + num_rows;
  JDIMENSION undef_row;

  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
	  ptr->mem_buffer == NULL)
	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);

  if (start_row < ptr->cur_start_row ||
	  end_row > ptr->cur_start_row+ptr->rows_in_mem) {
	if (! ptr->b_s_open)
	  ERREXIT(cinfo, JERR_VIRTUAL_BUG);
	if (ptr->dirty) {
	  do_sarray_io(cinfo, ptr, TRUE);
	  ptr->dirty = FALSE;
	}
	if (start_row > ptr->cur_start_row) {
	  ptr->cur_start_row = start_row;
	} else {
	  long ltemp;

	  ltemp = (long) end_row - (long) ptr->rows_in_mem;
	  if (ltemp < 0)
	ltemp = 0;		/* don't fall off front end of file */
	  ptr->cur_start_row = (JDIMENSION) ltemp;
	}
	do_sarray_io(cinfo, ptr, FALSE);
  }
  if (ptr->first_undef_row < end_row) {
	if (ptr->first_undef_row < start_row) {
	  if (writable)		/* writer skipped over a section of array */
	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
	  undef_row = start_row;	/* but reader is allowed to read ahead */
	} else {
	  undef_row = ptr->first_undef_row;
	}
	if (writable)
	  ptr->first_undef_row = end_row;
	if (ptr->pre_zero) {
	  size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
	  undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
	  end_row -= ptr->cur_start_row;
	  while (undef_row < end_row) {
	jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
	undef_row++;
	  }
	} else {
	  if (! writable)		/* reader looking at undefined data */
	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
	}
  }
  if (writable)
	ptr->dirty = TRUE;
  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
}

METHODDEF(JBLOCKARRAY)
access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
			JDIMENSION start_row, JDIMENSION num_rows,
			boolean writable)
{
  JDIMENSION end_row = start_row + num_rows;
  JDIMENSION undef_row;

  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
	  ptr->mem_buffer == NULL)
	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);

  if (start_row < ptr->cur_start_row ||
	  end_row > ptr->cur_start_row+ptr->rows_in_mem) {
	if (! ptr->b_s_open)
	  ERREXIT(cinfo, JERR_VIRTUAL_BUG);
	if (ptr->dirty) {
	  do_barray_io(cinfo, ptr, TRUE);
	  ptr->dirty = FALSE;
	}
	if (start_row > ptr->cur_start_row) {
	  ptr->cur_start_row = start_row;
	} else {
	  long ltemp;

	  ltemp = (long) end_row - (long) ptr->rows_in_mem;
	  if (ltemp < 0)
	ltemp = 0;		/* don't fall off front end of file */
	  ptr->cur_start_row = (JDIMENSION) ltemp;
	}
	do_barray_io(cinfo, ptr, FALSE);
  }
  if (ptr->first_undef_row < end_row) {
	if (ptr->first_undef_row < start_row) {
	  if (writable)		/* writer skipped over a section of array */
	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
	  undef_row = start_row;	/* but reader is allowed to read ahead */
	} else {
	  undef_row = ptr->first_undef_row;
	}
	if (writable)
	  ptr->first_undef_row = end_row;
	if (ptr->pre_zero) {
	  size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
	  undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
	  end_row -= ptr->cur_start_row;
	  while (undef_row < end_row) {
	jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
	undef_row++;
	  }
	} else {
	  if (! writable)		/* reader looking at undefined data */
	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
	}
  }
  if (writable)
	ptr->dirty = TRUE;
  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
}

METHODDEF(void)
free_pool (j_common_ptr cinfo, int pool_id)
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  small_pool_ptr shdr_ptr;
  large_pool_ptr lhdr_ptr;
  size_t space_freed;

  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
	ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */

#ifdef MEM_STATS
  if (cinfo->err->trace_level > 1)
	print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
#endif

  if (pool_id == JPOOL_IMAGE) {
	jvirt_sarray_ptr sptr;
	jvirt_barray_ptr bptr;

	for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
	  if (sptr->b_s_open) {	/* there may be no backing store */
	sptr->b_s_open = FALSE;	/* prevent recursive close if error */
	(*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
	  }
	}
	mem->virt_sarray_list = NULL;
	for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
	  if (bptr->b_s_open) {	/* there may be no backing store */
	bptr->b_s_open = FALSE;	/* prevent recursive close if error */
	(*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
	  }
	}
	mem->virt_barray_list = NULL;
  }

  lhdr_ptr = mem->large_list[pool_id];
  mem->large_list[pool_id] = NULL;

  while (lhdr_ptr != NULL) {
	large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
	space_freed = lhdr_ptr->hdr.bytes_used +
		  lhdr_ptr->hdr.bytes_left +
		  SIZEOF(large_pool_hdr);
	jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
	mem->total_space_allocated -= space_freed;
	lhdr_ptr = next_lhdr_ptr;
  }

  shdr_ptr = mem->small_list[pool_id];
  mem->small_list[pool_id] = NULL;

  while (shdr_ptr != NULL) {
	small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
	space_freed = shdr_ptr->hdr.bytes_used +
		  shdr_ptr->hdr.bytes_left +
		  SIZEOF(small_pool_hdr);
	jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
	mem->total_space_allocated -= space_freed;
	shdr_ptr = next_shdr_ptr;
  }
}

METHODDEF(void)
self_destruct (j_common_ptr cinfo)
{
  int pool;

  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
	free_pool(cinfo, pool);
  }

  jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
  cinfo->mem = NULL;		/* ensures I will be called only once */

  jpeg_mem_term(cinfo);		/* system-dependent cleanup */
}

GLOBAL(void)
jinit_memory_mgr (j_common_ptr cinfo)
{
  my_mem_ptr mem;
  long max_to_use;
  int pool;
  size_t test_mac;

  cinfo->mem = NULL;		/* for safety if init fails */

  if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
	ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
  test_mac = (size_t) MAX_ALLOC_CHUNK;
  if ((long) test_mac != MAX_ALLOC_CHUNK ||
	  (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
	ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);

  max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */

  mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));

  if (mem == NULL) {
	jpeg_mem_term(cinfo);	/* system-dependent cleanup */
	ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
  }

  mem->pub.alloc_small = alloc_small;
  mem->pub.alloc_large = alloc_large;
  mem->pub.alloc_sarray = alloc_sarray;
  mem->pub.alloc_barray = alloc_barray;
  mem->pub.request_virt_sarray = request_virt_sarray;
  mem->pub.request_virt_barray = request_virt_barray;
  mem->pub.realize_virt_arrays = realize_virt_arrays;
  mem->pub.access_virt_sarray = access_virt_sarray;
  mem->pub.access_virt_barray = access_virt_barray;
  mem->pub.free_pool = free_pool;
  mem->pub.self_destruct = self_destruct;

  mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;

  mem->pub.max_memory_to_use = max_to_use;

  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
	mem->small_list[pool] = NULL;
	mem->large_list[pool] = NULL;
  }
  mem->virt_sarray_list = NULL;
  mem->virt_barray_list = NULL;

  mem->total_space_allocated = SIZEOF(my_memory_mgr);

  cinfo->mem = & mem->pub;

#ifndef NO_GETENV
  { char * memenv;

	if ((memenv = getenv("JPEGMEM")) != NULL) {
	  char ch = 'x';

	  if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
	if (ch == 'm' || ch == 'M')
	  max_to_use *= 1000L;
	mem->pub.max_memory_to_use = max_to_use * 1000L;
	  }
	}
  }
#endif

}
/*** End of inlined file: jmemmgr.c ***/


/*** Start of inlined file: jmemnobs.c ***/
#define JPEG_INTERNALS

#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
extern void * malloc JPP((size_t size));
extern void free JPP((void *ptr));
#endif

GLOBAL(void *)
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
{
  return (void *) malloc(sizeofobject);
}

GLOBAL(void)
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
{
  free(object);
}

GLOBAL(void FAR *)
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
{
  return (void FAR *) malloc(sizeofobject);
}

GLOBAL(void)
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
{
  free(object);
}

GLOBAL(long)
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
			long max_bytes_needed, long already_allocated)
{
  return max_bytes_needed;
}

GLOBAL(void)
jpeg_open_backing_store (j_common_ptr cinfo, struct backing_store_struct *info,
			 long total_bytes_needed)
{
  ERREXIT(cinfo, JERR_NO_BACKING_STORE);
}

GLOBAL(long)
jpeg_mem_init (j_common_ptr cinfo)
{
  return 0;			/* just set max_memory_to_use to 0 */
}

GLOBAL(void)
jpeg_mem_term (j_common_ptr cinfo)
{
}
/*** End of inlined file: jmemnobs.c ***/


/*** Start of inlined file: jquant1.c ***/
#define JPEG_INTERNALS

#ifdef QUANT_1PASS_SUPPORTED

#define ODITHER_SIZE  16	/* dimension of dither matrix */
#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE)	/* # cells in matrix */
#define ODITHER_MASK  (ODITHER_SIZE-1) /* mask for wrapping around counters */

typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE];

static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
  {   0,192, 48,240, 12,204, 60,252,  3,195, 51,243, 15,207, 63,255 },
  { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
  {  32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
  { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
  {   8,200, 56,248,  4,196, 52,244, 11,203, 59,251,  7,199, 55,247 },
  { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
  {  40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
  { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
  {   2,194, 50,242, 14,206, 62,254,  1,193, 49,241, 13,205, 61,253 },
  { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
  {  34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
  { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
  {  10,202, 58,250,  6,198, 54,246,  9,201, 57,249,  5,197, 53,245 },
  { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
  {  42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
  { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
};

#if BITS_IN_JSAMPLE == 8
typedef INT16 FSERROR;		/* 16 bits should be enough */
typedef int LOCFSERROR;		/* use 'int' for calculation temps */
#else
typedef INT32 FSERROR;		/* may need more than 16 bits */
typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
#endif

typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */

#define MAX_Q_COMPS 4		/* max components I can handle */

typedef struct {
  struct jpeg_color_quantizer pub; /* public fields */

  JSAMPARRAY sv_colormap;	/* The color map as a 2-D pixel array */
  int sv_actual;		/* number of entries in use */

  JSAMPARRAY colorindex;	/* Precomputed mapping for speed */
  boolean is_padded;		/* is the colorindex padded for odither? */

  int Ncolors[MAX_Q_COMPS];	/* # of values alloced to each component */

  int row_index;		/* cur row's vertical index in dither matrix */
  ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */

  FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
  boolean on_odd_row;		/* flag to remember which row we are on */
} my_cquantizer;

typedef my_cquantizer * my_cquantize_ptr;

LOCAL(int)
select_ncolors (j_decompress_ptr cinfo, int Ncolors[])
{
  int nc = cinfo->out_color_components; /* number of color components */
  int max_colors = cinfo->desired_number_of_colors;
  int total_colors, iroot, i, j;
  boolean changed;
  long temp;
  static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };

  iroot = 1;
  do {
	iroot++;
	temp = iroot;		/* set temp = iroot ** nc */
	for (i = 1; i < nc; i++)
	  temp *= iroot;
  } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
  iroot--;			/* now iroot = floor(root) */

  if (iroot < 2)
	ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp);

  total_colors = 1;
  for (i = 0; i < nc; i++) {
	Ncolors[i] = iroot;
	total_colors *= iroot;
  }
  do {
	changed = FALSE;
	for (i = 0; i < nc; i++) {
	  j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i);
	  temp = total_colors / Ncolors[j];
	  temp *= Ncolors[j]+1;	/* done in long arith to avoid oflo */
	  if (temp > (long) max_colors)
	break;			/* won't fit, done with this pass */
	  Ncolors[j]++;		/* OK, apply the increment */
	  total_colors = (int) temp;
	  changed = TRUE;
	}
  } while (changed);

  return total_colors;
}

LOCAL(int)
output_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
{
  return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj);
}

LOCAL(int)
largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
{
  return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj));
}

LOCAL(void)
create_colormap (j_decompress_ptr cinfo)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  JSAMPARRAY colormap;		/* Created colormap */
  int total_colors;		/* Number of distinct output colors */
  int i,j,k, nci, blksize, blkdist, ptr, val;

  total_colors = select_ncolors(cinfo, cquantize->Ncolors);

  if (cinfo->out_color_components == 3)
	TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
		 total_colors, cquantize->Ncolors[0],
		 cquantize->Ncolors[1], cquantize->Ncolors[2]);
  else
	TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);

  colormap = (*cinfo->mem->alloc_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE,
	 (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components);

  blkdist = total_colors;

  for (i = 0; i < cinfo->out_color_components; i++) {
	nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
	blksize = blkdist / nci;
	for (j = 0; j < nci; j++) {
	  val = output_value(cinfo, i, j, nci-1);
	  for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
	for (k = 0; k < blksize; k++)
	  colormap[i][ptr+k] = (JSAMPLE) val;
	  }
	}
	blkdist = blksize;		/* blksize of this color is blkdist of next */
  }

  cquantize->sv_colormap = colormap;
  cquantize->sv_actual = total_colors;
}

LOCAL(void)
create_colorindex (j_decompress_ptr cinfo)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  JSAMPROW indexptr;
  int i,j,k, nci, blksize, val, pad;

  if (cinfo->dither_mode == JDITHER_ORDERED) {
	pad = MAXJSAMPLE*2;
	cquantize->is_padded = TRUE;
  } else {
	pad = 0;
	cquantize->is_padded = FALSE;
  }

  cquantize->colorindex = (*cinfo->mem->alloc_sarray)
	((j_common_ptr) cinfo, JPOOL_IMAGE,
	 (JDIMENSION) (MAXJSAMPLE+1 + pad),
	 (JDIMENSION) cinfo->out_color_components);

  blksize = cquantize->sv_actual;

  for (i = 0; i < cinfo->out_color_components; i++) {
	nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
	blksize = blksize / nci;

	if (pad)
	  cquantize->colorindex[i] += MAXJSAMPLE;

	indexptr = cquantize->colorindex[i];
	val = 0;
	k = largest_input_value(cinfo, i, 0, nci-1);
	for (j = 0; j <= MAXJSAMPLE; j++) {
	  while (j > k)		/* advance val if past boundary */
	k = largest_input_value(cinfo, i, ++val, nci-1);
	  indexptr[j] = (JSAMPLE) (val * blksize);
	}
	if (pad)
	  for (j = 1; j <= MAXJSAMPLE; j++) {
	indexptr[-j] = indexptr[0];
	indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
	  }
  }
}

LOCAL(ODITHER_MATRIX_PTR)
make_odither_array (j_decompress_ptr cinfo, int ncolors)
{
  ODITHER_MATRIX_PTR odither;
  int j,k;
  INT32 num,den;

  odither = (ODITHER_MATRIX_PTR)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(ODITHER_MATRIX));
  den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1));
  for (j = 0; j < ODITHER_SIZE; j++) {
	for (k = 0; k < ODITHER_SIZE; k++) {
	  num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k])))
		* MAXJSAMPLE;
	  odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den);
	}
  }
  return odither;
}

LOCAL(void)
create_odither_tables (j_decompress_ptr cinfo)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  ODITHER_MATRIX_PTR odither;
  int i, j, nci;

  for (i = 0; i < cinfo->out_color_components; i++) {
	nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
	odither = NULL;		/* search for matching prior component */
	for (j = 0; j < i; j++) {
	  if (nci == cquantize->Ncolors[j]) {
	odither = cquantize->odither[j];
	break;
	  }
	}
	if (odither == NULL)	/* need a new table? */
	  odither = make_odither_array(cinfo, nci);
	cquantize->odither[i] = odither;
  }
}

METHODDEF(void)
color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
		JSAMPARRAY output_buf, int num_rows)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  JSAMPARRAY colorindex = cquantize->colorindex;
  register int pixcode, ci;
  register JSAMPROW ptrin, ptrout;
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;
  register int nc = cinfo->out_color_components;

  for (row = 0; row < num_rows; row++) {
	ptrin = input_buf[row];
	ptrout = output_buf[row];
	for (col = width; col > 0; col--) {
	  pixcode = 0;
	  for (ci = 0; ci < nc; ci++) {
	pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
	  }
	  *ptrout++ = (JSAMPLE) pixcode;
	}
  }
}

METHODDEF(void)
color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
		 JSAMPARRAY output_buf, int num_rows)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  register int pixcode;
  register JSAMPROW ptrin, ptrout;
  JSAMPROW colorindex0 = cquantize->colorindex[0];
  JSAMPROW colorindex1 = cquantize->colorindex[1];
  JSAMPROW colorindex2 = cquantize->colorindex[2];
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;

  for (row = 0; row < num_rows; row++) {
	ptrin = input_buf[row];
	ptrout = output_buf[row];
	for (col = width; col > 0; col--) {
	  pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]);
	  pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]);
	  pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]);
	  *ptrout++ = (JSAMPLE) pixcode;
	}
  }
}

METHODDEF(void)
quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
			 JSAMPARRAY output_buf, int num_rows)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  register JSAMPROW input_ptr;
  register JSAMPROW output_ptr;
  JSAMPROW colorindex_ci;
  int * dither;			/* points to active row of dither matrix */
  int row_index, col_index;	/* current indexes into dither matrix */
  int nc = cinfo->out_color_components;
  int ci;
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;

  for (row = 0; row < num_rows; row++) {
	jzero_far((void FAR *) output_buf[row],
		  (size_t) (width * SIZEOF(JSAMPLE)));
	row_index = cquantize->row_index;
	for (ci = 0; ci < nc; ci++) {
	  input_ptr = input_buf[row] + ci;
	  output_ptr = output_buf[row];
	  colorindex_ci = cquantize->colorindex[ci];
	  dither = cquantize->odither[ci][row_index];
	  col_index = 0;

	  for (col = width; col > 0; col--) {
	*output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
	input_ptr += nc;
	output_ptr++;
	col_index = (col_index + 1) & ODITHER_MASK;
	  }
	}
	row_index = (row_index + 1) & ODITHER_MASK;
	cquantize->row_index = row_index;
  }
}

METHODDEF(void)
quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
			  JSAMPARRAY output_buf, int num_rows)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  register int pixcode;
  register JSAMPROW input_ptr;
  register JSAMPROW output_ptr;
  JSAMPROW colorindex0 = cquantize->colorindex[0];
  JSAMPROW colorindex1 = cquantize->colorindex[1];
  JSAMPROW colorindex2 = cquantize->colorindex[2];
  int * dither0;		/* points to active row of dither matrix */
  int * dither1;
  int * dither2;
  int row_index, col_index;	/* current indexes into dither matrix */
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;

  for (row = 0; row < num_rows; row++) {
	row_index = cquantize->row_index;
	input_ptr = input_buf[row];
	output_ptr = output_buf[row];
	dither0 = cquantize->odither[0][row_index];
	dither1 = cquantize->odither[1][row_index];
	dither2 = cquantize->odither[2][row_index];
	col_index = 0;

	for (col = width; col > 0; col--) {
	  pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) +
					dither0[col_index]]);
	  pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) +
					dither1[col_index]]);
	  pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) +
					dither2[col_index]]);
	  *output_ptr++ = (JSAMPLE) pixcode;
	  col_index = (col_index + 1) & ODITHER_MASK;
	}
	row_index = (row_index + 1) & ODITHER_MASK;
	cquantize->row_index = row_index;
  }
}

METHODDEF(void)
quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
			JSAMPARRAY output_buf, int num_rows)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  register LOCFSERROR cur;	/* current error or pixel value */
  LOCFSERROR belowerr;		/* error for pixel below cur */
  LOCFSERROR bpreverr;		/* error for below/prev col */
  LOCFSERROR bnexterr;		/* error for below/next col */
  LOCFSERROR delta;
  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
  register JSAMPROW input_ptr;
  register JSAMPROW output_ptr;
  JSAMPROW colorindex_ci;
  JSAMPROW colormap_ci;
  int pixcode;
  int nc = cinfo->out_color_components;
  int dir;			/* 1 for left-to-right, -1 for right-to-left */
  int dirnc;			/* dir * nc */
  int ci;
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;
  JSAMPLE *range_limit = cinfo->sample_range_limit;
  SHIFT_TEMPS

  for (row = 0; row < num_rows; row++) {
	jzero_far((void FAR *) output_buf[row],
		  (size_t) (width * SIZEOF(JSAMPLE)));
	for (ci = 0; ci < nc; ci++) {
	  input_ptr = input_buf[row] + ci;
	  output_ptr = output_buf[row];
	  if (cquantize->on_odd_row) {
	input_ptr += (width-1) * nc; /* so point to rightmost pixel */
	output_ptr += width-1;
	dir = -1;
	dirnc = -nc;
	errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
	  } else {
	dir = 1;
	dirnc = nc;
	errorptr = cquantize->fserrors[ci]; /* => entry before first column */
	  }
	  colorindex_ci = cquantize->colorindex[ci];
	  colormap_ci = cquantize->sv_colormap[ci];
	  cur = 0;
	  belowerr = bpreverr = 0;

	  for (col = width; col > 0; col--) {
	cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
	cur += GETJSAMPLE(*input_ptr);
	cur = GETJSAMPLE(range_limit[cur]);
	pixcode = GETJSAMPLE(colorindex_ci[cur]);
	*output_ptr += (JSAMPLE) pixcode;
	cur -= GETJSAMPLE(colormap_ci[pixcode]);
	bnexterr = cur;
	delta = cur * 2;
	cur += delta;		/* form error * 3 */
	errorptr[0] = (FSERROR) (bpreverr + cur);
	cur += delta;		/* form error * 5 */
	bpreverr = belowerr + cur;
	belowerr = bnexterr;
	cur += delta;		/* form error * 7 */
	input_ptr += dirnc;	/* advance input ptr to next column */
	output_ptr += dir;	/* advance output ptr to next column */
	errorptr += dir;	/* advance errorptr to current column */
	  }
	  errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
	}
	cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
  }
}

LOCAL(void)
alloc_fs_workspace (j_decompress_ptr cinfo)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  size_t arraysize;
  int i;

  arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
  for (i = 0; i < cinfo->out_color_components; i++) {
	cquantize->fserrors[i] = (FSERRPTR)
	  (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
  }
}

METHODDEF(void)
start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  size_t arraysize;
  int i;

  cinfo->colormap = cquantize->sv_colormap;
  cinfo->actual_number_of_colors = cquantize->sv_actual;

  switch (cinfo->dither_mode) {
  case JDITHER_NONE:
	if (cinfo->out_color_components == 3)
	  cquantize->pub.color_quantize = color_quantize3;
	else
	  cquantize->pub.color_quantize = color_quantize;
	break;
  case JDITHER_ORDERED:
	if (cinfo->out_color_components == 3)
	  cquantize->pub.color_quantize = quantize3_ord_dither;
	else
	  cquantize->pub.color_quantize = quantize_ord_dither;
	cquantize->row_index = 0;	/* initialize state for ordered dither */
	if (! cquantize->is_padded)
	  create_colorindex(cinfo);
	if (cquantize->odither[0] == NULL)
	  create_odither_tables(cinfo);
	break;
  case JDITHER_FS:
	cquantize->pub.color_quantize = quantize_fs_dither;
	cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
	if (cquantize->fserrors[0] == NULL)
	  alloc_fs_workspace(cinfo);
	arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
	for (i = 0; i < cinfo->out_color_components; i++)
	  jzero_far((void FAR *) cquantize->fserrors[i], arraysize);
	break;
  default:
	ERREXIT(cinfo, JERR_NOT_COMPILED);
	break;
  }
}

METHODDEF(void)
finish_pass_1_quant (j_decompress_ptr cinfo)
{
}

METHODDEF(void)
new_color_map_1_quant (j_decompress_ptr cinfo)
{
  ERREXIT(cinfo, JERR_MODE_CHANGE);
}

GLOBAL(void)
jinit_1pass_quantizer (j_decompress_ptr cinfo)
{
  my_cquantize_ptr cquantize;

  cquantize = (my_cquantize_ptr)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_cquantizer));
  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
  cquantize->pub.start_pass = start_pass_1_quant;
  cquantize->pub.finish_pass = finish_pass_1_quant;
  cquantize->pub.new_color_map = new_color_map_1_quant;
  cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
  cquantize->odither[0] = NULL;	/* Also flag odither arrays not allocated */

  if (cinfo->out_color_components > MAX_Q_COMPS)
	ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
  if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
	ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1);

  create_colormap(cinfo);
  create_colorindex(cinfo);

  if (cinfo->dither_mode == JDITHER_FS)
	alloc_fs_workspace(cinfo);
}

#endif /* QUANT_1PASS_SUPPORTED */
/*** End of inlined file: jquant1.c ***/


/*** Start of inlined file: jquant2.c ***/
#define JPEG_INTERNALS

#ifdef QUANT_2PASS_SUPPORTED

#define R_SCALE 2		/* scale R distances by this much */
#define G_SCALE 3		/* scale G distances by this much */
#define B_SCALE 1		/* and B by this much */

#if RGB_RED == 0
#define C0_SCALE R_SCALE
#endif
#if RGB_BLUE == 0
#define C0_SCALE B_SCALE
#endif
#if RGB_GREEN == 1
#define C1_SCALE G_SCALE
#endif
#if RGB_RED == 2
#define C2_SCALE R_SCALE
#endif
#if RGB_BLUE == 2
#define C2_SCALE B_SCALE
#endif

#define MAXNUMCOLORS  (MAXJSAMPLE+1) /* maximum size of colormap */

#define HIST_C0_BITS  5		/* bits of precision in R/B histogram */
#define HIST_C1_BITS  6		/* bits of precision in G histogram */
#define HIST_C2_BITS  5		/* bits of precision in B/R histogram */

#define HIST_C0_ELEMS  (1<<HIST_C0_BITS)
#define HIST_C1_ELEMS  (1<<HIST_C1_BITS)
#define HIST_C2_ELEMS  (1<<HIST_C2_BITS)

#define C0_SHIFT  (BITS_IN_JSAMPLE-HIST_C0_BITS)
#define C1_SHIFT  (BITS_IN_JSAMPLE-HIST_C1_BITS)
#define C2_SHIFT  (BITS_IN_JSAMPLE-HIST_C2_BITS)

typedef UINT16 histcell;	/* histogram cell; prefer an unsigned type */

typedef histcell FAR * histptr;	/* for pointers to histogram cells */

typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */
typedef hist1d FAR * hist2d;	/* type for the 2nd-level pointers */
typedef hist2d * hist3d;	/* type for top-level pointer */

#if BITS_IN_JSAMPLE == 8
typedef INT16 FSERROR;		/* 16 bits should be enough */
typedef int LOCFSERROR;		/* use 'int' for calculation temps */
#else
typedef INT32 FSERROR;		/* may need more than 16 bits */
typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
#endif

typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */

typedef struct {
  struct jpeg_color_quantizer pub; /* public fields */

  JSAMPARRAY sv_colormap;	/* colormap allocated at init time */
  int desired;			/* desired # of colors = size of colormap */

  hist3d histogram;		/* pointer to the histogram */

  boolean needs_zeroed;		/* TRUE if next pass must zero histogram */

  FSERRPTR fserrors;		/* accumulated errors */
  boolean on_odd_row;		/* flag to remember which row we are on */
  int * error_limiter;		/* table for clamping the applied error */
} my_cquantizer2;

typedef my_cquantizer2 * my_cquantize_ptr2;

METHODDEF(void)
prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
		  JSAMPARRAY output_buf, int num_rows)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  register JSAMPROW ptr;
  register histptr histp;
  register hist3d histogram = cquantize->histogram;
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;

  for (row = 0; row < num_rows; row++) {
	ptr = input_buf[row];
	for (col = width; col > 0; col--) {
	  histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
			 [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
			 [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
	  if (++(*histp) <= 0)
	(*histp)--;
	  ptr += 3;
	}
  }
}

typedef struct {
  int c0min, c0max;
  int c1min, c1max;
  int c2min, c2max;
  INT32 volume;
  long colorcount;
} box;

typedef box * boxptr;

LOCAL(boxptr)
find_biggest_color_pop (boxptr boxlist, int numboxes)
{
  register boxptr boxp;
  register int i;
  register long maxc = 0;
  boxptr which = NULL;

  for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
	if (boxp->colorcount > maxc && boxp->volume > 0) {
	  which = boxp;
	  maxc = boxp->colorcount;
	}
  }
  return which;
}

LOCAL(boxptr)
find_biggest_volume (boxptr boxlist, int numboxes)
{
  register boxptr boxp;
  register int i;
  register INT32 maxv = 0;
  boxptr which = NULL;

  for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
	if (boxp->volume > maxv) {
	  which = boxp;
	  maxv = boxp->volume;
	}
  }
  return which;
}

LOCAL(void)
update_box (j_decompress_ptr cinfo, boxptr boxp)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  histptr histp;
  int c0,c1,c2;
  int c0min,c0max,c1min,c1max,c2min,c2max;
  INT32 dist0,dist1,dist2;
  long ccount;

  c0min = boxp->c0min;  c0max = boxp->c0max;
  c1min = boxp->c1min;  c1max = boxp->c1max;
  c2min = boxp->c2min;  c2max = boxp->c2max;

  if (c0max > c0min)
	for (c0 = c0min; c0 <= c0max; c0++)
	  for (c1 = c1min; c1 <= c1max; c1++) {
	histp = & histogram[c0][c1][c2min];
	for (c2 = c2min; c2 <= c2max; c2++)
	  if (*histp++ != 0) {
		boxp->c0min = c0min = c0;
		goto have_c0min;
	  }
	  }
 have_c0min:
  if (c0max > c0min)
	for (c0 = c0max; c0 >= c0min; c0--)
	  for (c1 = c1min; c1 <= c1max; c1++) {
	histp = & histogram[c0][c1][c2min];
	for (c2 = c2min; c2 <= c2max; c2++)
	  if (*histp++ != 0) {
		boxp->c0max = c0max = c0;
		goto have_c0max;
	  }
	  }
 have_c0max:
  if (c1max > c1min)
	for (c1 = c1min; c1 <= c1max; c1++)
	  for (c0 = c0min; c0 <= c0max; c0++) {
	histp = & histogram[c0][c1][c2min];
	for (c2 = c2min; c2 <= c2max; c2++)
	  if (*histp++ != 0) {
		boxp->c1min = c1min = c1;
		goto have_c1min;
	  }
	  }
 have_c1min:
  if (c1max > c1min)
	for (c1 = c1max; c1 >= c1min; c1--)
	  for (c0 = c0min; c0 <= c0max; c0++) {
	histp = & histogram[c0][c1][c2min];
	for (c2 = c2min; c2 <= c2max; c2++)
	  if (*histp++ != 0) {
		boxp->c1max = c1max = c1;
		goto have_c1max;
	  }
	  }
 have_c1max:
  if (c2max > c2min)
	for (c2 = c2min; c2 <= c2max; c2++)
	  for (c0 = c0min; c0 <= c0max; c0++) {
	histp = & histogram[c0][c1min][c2];
	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
	  if (*histp != 0) {
		boxp->c2min = c2min = c2;
		goto have_c2min;
	  }
	  }
 have_c2min:
  if (c2max > c2min)
	for (c2 = c2max; c2 >= c2min; c2--)
	  for (c0 = c0min; c0 <= c0max; c0++) {
	histp = & histogram[c0][c1min][c2];
	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
	  if (*histp != 0) {
		boxp->c2max = c2max = c2;
		goto have_c2max;
	  }
	  }
 have_c2max:

  dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
  dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
  dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
  boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;

  ccount = 0;
  for (c0 = c0min; c0 <= c0max; c0++)
	for (c1 = c1min; c1 <= c1max; c1++) {
	  histp = & histogram[c0][c1][c2min];
	  for (c2 = c2min; c2 <= c2max; c2++, histp++)
	if (*histp != 0) {
	  ccount++;
	}
	}
  boxp->colorcount = ccount;
}

LOCAL(int)
median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes,
		int desired_colors)
{
  int n,lb;
  int c0,c1,c2,cmax;
  register boxptr b1,b2;

  while (numboxes < desired_colors) {
	if (numboxes*2 <= desired_colors) {
	  b1 = find_biggest_color_pop(boxlist, numboxes);
	} else {
	  b1 = find_biggest_volume(boxlist, numboxes);
	}
	if (b1 == NULL)		/* no splittable boxes left! */
	  break;
	b2 = &boxlist[numboxes];	/* where new box will go */
	b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
	b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
	c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
	c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
	c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
#if RGB_RED == 0
	cmax = c1; n = 1;
	if (c0 > cmax) { cmax = c0; n = 0; }
	if (c2 > cmax) { n = 2; }
#else
	cmax = c1; n = 1;
	if (c2 > cmax) { cmax = c2; n = 2; }
	if (c0 > cmax) { n = 0; }
#endif
	switch (n) {
	case 0:
	  lb = (b1->c0max + b1->c0min) / 2;
	  b1->c0max = lb;
	  b2->c0min = lb+1;
	  break;
	case 1:
	  lb = (b1->c1max + b1->c1min) / 2;
	  b1->c1max = lb;
	  b2->c1min = lb+1;
	  break;
	case 2:
	  lb = (b1->c2max + b1->c2min) / 2;
	  b1->c2max = lb;
	  b2->c2min = lb+1;
	  break;
	}
	update_box(cinfo, b1);
	update_box(cinfo, b2);
	numboxes++;
  }
  return numboxes;
}

LOCAL(void)
compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  histptr histp;
  int c0,c1,c2;
  int c0min,c0max,c1min,c1max,c2min,c2max;
  long count;
  long total = 0;
  long c0total = 0;
  long c1total = 0;
  long c2total = 0;

  c0min = boxp->c0min;  c0max = boxp->c0max;
  c1min = boxp->c1min;  c1max = boxp->c1max;
  c2min = boxp->c2min;  c2max = boxp->c2max;

  for (c0 = c0min; c0 <= c0max; c0++)
	for (c1 = c1min; c1 <= c1max; c1++) {
	  histp = & histogram[c0][c1][c2min];
	  for (c2 = c2min; c2 <= c2max; c2++) {
	if ((count = *histp++) != 0) {
	  total += count;
	  c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
	  c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
	  c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
	}
	  }
	}

  cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
  cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
  cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
}

LOCAL(void)
select_colors (j_decompress_ptr cinfo, int desired_colors)
{
  boxptr boxlist;
  int numboxes;
  int i;

  boxlist = (boxptr) (*cinfo->mem->alloc_small)
	((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box));
  numboxes = 1;
  boxlist[0].c0min = 0;
  boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
  boxlist[0].c1min = 0;
  boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
  boxlist[0].c2min = 0;
  boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
  update_box(cinfo, & boxlist[0]);
  numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors);
  for (i = 0; i < numboxes; i++)
	compute_color(cinfo, & boxlist[i], i);
  cinfo->actual_number_of_colors = numboxes;
  TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
}

#define BOX_C0_LOG  (HIST_C0_BITS-3)
#define BOX_C1_LOG  (HIST_C1_BITS-3)
#define BOX_C2_LOG  (HIST_C2_BITS-3)

#define BOX_C0_ELEMS  (1<<BOX_C0_LOG) /* # of hist cells in update box */
#define BOX_C1_ELEMS  (1<<BOX_C1_LOG)
#define BOX_C2_ELEMS  (1<<BOX_C2_LOG)

#define BOX_C0_SHIFT  (C0_SHIFT + BOX_C0_LOG)
#define BOX_C1_SHIFT  (C1_SHIFT + BOX_C1_LOG)
#define BOX_C2_SHIFT  (C2_SHIFT + BOX_C2_LOG)

LOCAL(int)
find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
			JSAMPLE colorlist[])
{
  int numcolors = cinfo->actual_number_of_colors;
  int maxc0, maxc1, maxc2;
  int centerc0, centerc1, centerc2;
  int i, x, ncolors;
  INT32 minmaxdist, min_dist, max_dist, tdist;
  INT32 mindist[MAXNUMCOLORS];	/* min distance to colormap entry i */

  maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
  centerc0 = (minc0 + maxc0) >> 1;
  maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
  centerc1 = (minc1 + maxc1) >> 1;
  maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
  centerc2 = (minc2 + maxc2) >> 1;

  minmaxdist = 0x7FFFFFFFL;

  for (i = 0; i < numcolors; i++) {
	x = GETJSAMPLE(cinfo->colormap[0][i]);
	if (x < minc0) {
	  tdist = (x - minc0) * C0_SCALE;
	  min_dist = tdist*tdist;
	  tdist = (x - maxc0) * C0_SCALE;
	  max_dist = tdist*tdist;
	} else if (x > maxc0) {
	  tdist = (x - maxc0) * C0_SCALE;
	  min_dist = tdist*tdist;
	  tdist = (x - minc0) * C0_SCALE;
	  max_dist = tdist*tdist;
	} else {
	  min_dist = 0;
	  if (x <= centerc0) {
	tdist = (x - maxc0) * C0_SCALE;
	max_dist = tdist*tdist;
	  } else {
	tdist = (x - minc0) * C0_SCALE;
	max_dist = tdist*tdist;
	  }
	}

	x = GETJSAMPLE(cinfo->colormap[1][i]);
	if (x < minc1) {
	  tdist = (x - minc1) * C1_SCALE;
	  min_dist += tdist*tdist;
	  tdist = (x - maxc1) * C1_SCALE;
	  max_dist += tdist*tdist;
	} else if (x > maxc1) {
	  tdist = (x - maxc1) * C1_SCALE;
	  min_dist += tdist*tdist;
	  tdist = (x - minc1) * C1_SCALE;
	  max_dist += tdist*tdist;
	} else {
	  if (x <= centerc1) {
	tdist = (x - maxc1) * C1_SCALE;
	max_dist += tdist*tdist;
	  } else {
	tdist = (x - minc1) * C1_SCALE;
	max_dist += tdist*tdist;
	  }
	}

	x = GETJSAMPLE(cinfo->colormap[2][i]);
	if (x < minc2) {
	  tdist = (x - minc2) * C2_SCALE;
	  min_dist += tdist*tdist;
	  tdist = (x - maxc2) * C2_SCALE;
	  max_dist += tdist*tdist;
	} else if (x > maxc2) {
	  tdist = (x - maxc2) * C2_SCALE;
	  min_dist += tdist*tdist;
	  tdist = (x - minc2) * C2_SCALE;
	  max_dist += tdist*tdist;
	} else {
	  if (x <= centerc2) {
	tdist = (x - maxc2) * C2_SCALE;
	max_dist += tdist*tdist;
	  } else {
	tdist = (x - minc2) * C2_SCALE;
	max_dist += tdist*tdist;
	  }
	}

	mindist[i] = min_dist;	/* save away the results */
	if (max_dist < minmaxdist)
	  minmaxdist = max_dist;
  }

  ncolors = 0;
  for (i = 0; i < numcolors; i++) {
	if (mindist[i] <= minmaxdist)
	  colorlist[ncolors++] = (JSAMPLE) i;
  }
  return ncolors;
}

LOCAL(void)
find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
		  int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
{
  int ic0, ic1, ic2;
  int i, icolor;
  register INT32 * bptr;	/* pointer into bestdist[] array */
  JSAMPLE * cptr;		/* pointer into bestcolor[] array */
  INT32 dist0, dist1;		/* initial distance values */
  register INT32 dist2;		/* current distance in inner loop */
  INT32 xx0, xx1;		/* distance increments */
  register INT32 xx2;
  INT32 inc0, inc1, inc2;	/* initial values for increments */
  INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];

  bptr = bestdist;
  for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
	*bptr++ = 0x7FFFFFFFL;

#define STEP_C0  ((1 << C0_SHIFT) * C0_SCALE)
#define STEP_C1  ((1 << C1_SHIFT) * C1_SCALE)
#define STEP_C2  ((1 << C2_SHIFT) * C2_SCALE)

  for (i = 0; i < numcolors; i++) {
	icolor = GETJSAMPLE(colorlist[i]);
	inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
	dist0 = inc0*inc0;
	inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
	dist0 += inc1*inc1;
	inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
	dist0 += inc2*inc2;
	inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
	inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
	inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
	bptr = bestdist;
	cptr = bestcolor;
	xx0 = inc0;
	for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
	  dist1 = dist0;
	  xx1 = inc1;
	  for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
	dist2 = dist1;
	xx2 = inc2;
	for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
	  if (dist2 < *bptr) {
		*bptr = dist2;
		*cptr = (JSAMPLE) icolor;
	  }
	  dist2 += xx2;
	  xx2 += 2 * STEP_C2 * STEP_C2;
	  bptr++;
	  cptr++;
	}
	dist1 += xx1;
	xx1 += 2 * STEP_C1 * STEP_C1;
	  }
	  dist0 += xx0;
	  xx0 += 2 * STEP_C0 * STEP_C0;
	}
  }
}

LOCAL(void)
fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  int minc0, minc1, minc2;	/* lower left corner of update box */
  int ic0, ic1, ic2;
  register JSAMPLE * cptr;	/* pointer into bestcolor[] array */
  register histptr cachep;	/* pointer into main cache array */
  JSAMPLE colorlist[MAXNUMCOLORS];
  int numcolors;		/* number of candidate colors */
  JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];

  c0 >>= BOX_C0_LOG;
  c1 >>= BOX_C1_LOG;
  c2 >>= BOX_C2_LOG;

  minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
  minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
  minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);

  numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);

  find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
		   bestcolor);

  c0 <<= BOX_C0_LOG;		/* convert ID back to base cell indexes */
  c1 <<= BOX_C1_LOG;
  c2 <<= BOX_C2_LOG;
  cptr = bestcolor;
  for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
	for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
	  cachep = & histogram[c0+ic0][c1+ic1][c2];
	  for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
	*cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
	  }
	}
  }
}

METHODDEF(void)
pass2_no_dither (j_decompress_ptr cinfo,
		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  register JSAMPROW inptr, outptr;
  register histptr cachep;
  register int c0, c1, c2;
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;

  for (row = 0; row < num_rows; row++) {
	inptr = input_buf[row];
	outptr = output_buf[row];
	for (col = width; col > 0; col--) {
	  c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
	  c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
	  c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
	  cachep = & histogram[c0][c1][c2];
	  if (*cachep == 0)
	fill_inverse_cmap(cinfo, c0,c1,c2);
	  *outptr++ = (JSAMPLE) (*cachep - 1);
	}
  }
}

METHODDEF(void)
pass2_fs_dither (j_decompress_ptr cinfo,
		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  register LOCFSERROR cur0, cur1, cur2;	/* current error or pixel value */
  LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
  LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
  JSAMPROW inptr;		/* => current input pixel */
  JSAMPROW outptr;		/* => current output pixel */
  histptr cachep;
  int dir;			/* +1 or -1 depending on direction */
  int dir3;			/* 3*dir, for advancing inptr & errorptr */
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;
  JSAMPLE *range_limit = cinfo->sample_range_limit;
  int *error_limit = cquantize->error_limiter;
  JSAMPROW colormap0 = cinfo->colormap[0];
  JSAMPROW colormap1 = cinfo->colormap[1];
  JSAMPROW colormap2 = cinfo->colormap[2];
  SHIFT_TEMPS

  for (row = 0; row < num_rows; row++) {
	inptr = input_buf[row];
	outptr = output_buf[row];
	if (cquantize->on_odd_row) {
	  inptr += (width-1) * 3;	/* so point to rightmost pixel */
	  outptr += width-1;
	  dir = -1;
	  dir3 = -3;
	  errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
	  cquantize->on_odd_row = FALSE; /* flip for next time */
	} else {
	  dir = 1;
	  dir3 = 3;
	  errorptr = cquantize->fserrors; /* => entry before first real column */
	  cquantize->on_odd_row = TRUE; /* flip for next time */
	}
	cur0 = cur1 = cur2 = 0;
	belowerr0 = belowerr1 = belowerr2 = 0;
	bpreverr0 = bpreverr1 = bpreverr2 = 0;

	for (col = width; col > 0; col--) {
	  cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
	  cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
	  cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
	  cur0 = error_limit[cur0];
	  cur1 = error_limit[cur1];
	  cur2 = error_limit[cur2];
	  cur0 += GETJSAMPLE(inptr[0]);
	  cur1 += GETJSAMPLE(inptr[1]);
	  cur2 += GETJSAMPLE(inptr[2]);
	  cur0 = GETJSAMPLE(range_limit[cur0]);
	  cur1 = GETJSAMPLE(range_limit[cur1]);
	  cur2 = GETJSAMPLE(range_limit[cur2]);
	  cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
	  if (*cachep == 0)
	fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
	  { register int pixcode = *cachep - 1;
	*outptr = (JSAMPLE) pixcode;
	cur0 -= GETJSAMPLE(colormap0[pixcode]);
	cur1 -= GETJSAMPLE(colormap1[pixcode]);
	cur2 -= GETJSAMPLE(colormap2[pixcode]);
	  }
	  { register LOCFSERROR bnexterr, delta;

	bnexterr = cur0;	/* Process component 0 */
	delta = cur0 * 2;
	cur0 += delta;		/* form error * 3 */
	errorptr[0] = (FSERROR) (bpreverr0 + cur0);
	cur0 += delta;		/* form error * 5 */
	bpreverr0 = belowerr0 + cur0;
	belowerr0 = bnexterr;
	cur0 += delta;		/* form error * 7 */
	bnexterr = cur1;	/* Process component 1 */
	delta = cur1 * 2;
	cur1 += delta;		/* form error * 3 */
	errorptr[1] = (FSERROR) (bpreverr1 + cur1);
	cur1 += delta;		/* form error * 5 */
	bpreverr1 = belowerr1 + cur1;
	belowerr1 = bnexterr;
	cur1 += delta;		/* form error * 7 */
	bnexterr = cur2;	/* Process component 2 */
	delta = cur2 * 2;
	cur2 += delta;		/* form error * 3 */
	errorptr[2] = (FSERROR) (bpreverr2 + cur2);
	cur2 += delta;		/* form error * 5 */
	bpreverr2 = belowerr2 + cur2;
	belowerr2 = bnexterr;
	cur2 += delta;		/* form error * 7 */
	  }
	  inptr += dir3;		/* Advance pixel pointers to next column */
	  outptr += dir;
	  errorptr += dir3;		/* advance errorptr to current column */
	}
	errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
	errorptr[1] = (FSERROR) bpreverr1;
	errorptr[2] = (FSERROR) bpreverr2;
  }
}

LOCAL(void)
init_error_limit (j_decompress_ptr cinfo)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  int * table;
  int in, out;

  table = (int *) (*cinfo->mem->alloc_small)
	((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
  table += MAXJSAMPLE;		/* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
  cquantize->error_limiter = table;

#define STEPSIZE ((MAXJSAMPLE+1)/16)
  out = 0;
  for (in = 0; in < STEPSIZE; in++, out++) {
	table[in] = out; table[-in] = -out;
  }
  for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
	table[in] = out; table[-in] = -out;
  }
  for (; in <= MAXJSAMPLE; in++) {
	table[in] = out; table[-in] = -out;
  }
#undef STEPSIZE
}

METHODDEF(void)
finish_pass1 (j_decompress_ptr cinfo)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;

  cinfo->colormap = cquantize->sv_colormap;
  select_colors(cinfo, cquantize->desired);
  cquantize->needs_zeroed = TRUE;
}

METHODDEF(void)
finish_pass2 (j_decompress_ptr cinfo)
{
}

METHODDEF(void)
start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  int i;

  if (cinfo->dither_mode != JDITHER_NONE)
	cinfo->dither_mode = JDITHER_FS;

  if (is_pre_scan) {
	cquantize->pub.color_quantize = prescan_quantize;
	cquantize->pub.finish_pass = finish_pass1;
	cquantize->needs_zeroed = TRUE; /* Always zero histogram */
  } else {
	if (cinfo->dither_mode == JDITHER_FS)
	  cquantize->pub.color_quantize = pass2_fs_dither;
	else
	  cquantize->pub.color_quantize = pass2_no_dither;
	cquantize->pub.finish_pass = finish_pass2;

	i = cinfo->actual_number_of_colors;
	if (i < 1)
	  ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
	if (i > MAXNUMCOLORS)
	  ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);

	if (cinfo->dither_mode == JDITHER_FS) {
	  size_t arraysize = (size_t) ((cinfo->output_width + 2) *
				   (3 * SIZEOF(FSERROR)));
	  if (cquantize->fserrors == NULL)
	cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
	  ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
	  jzero_far((void FAR *) cquantize->fserrors, arraysize);
	  if (cquantize->error_limiter == NULL)
	init_error_limit(cinfo);
	  cquantize->on_odd_row = FALSE;
	}

  }
  if (cquantize->needs_zeroed) {
	for (i = 0; i < HIST_C0_ELEMS; i++) {
	  jzero_far((void FAR *) histogram[i],
		HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
	}
	cquantize->needs_zeroed = FALSE;
  }
}

METHODDEF(void)
new_color_map_2_quant (j_decompress_ptr cinfo)
{
  my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;

  cquantize->needs_zeroed = TRUE;
}

GLOBAL(void)
jinit_2pass_quantizer (j_decompress_ptr cinfo)
{
  my_cquantize_ptr2 cquantize;
  int i;

  cquantize = (my_cquantize_ptr2)
	(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				SIZEOF(my_cquantizer2));
  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
  cquantize->pub.start_pass = start_pass_2_quant;
  cquantize->pub.new_color_map = new_color_map_2_quant;
  cquantize->fserrors = NULL;	/* flag optional arrays not allocated */
  cquantize->error_limiter = NULL;

  if (cinfo->out_color_components != 3)
	ERREXIT(cinfo, JERR_NOTIMPL);

  cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
	((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
  for (i = 0; i < HIST_C0_ELEMS; i++) {
	cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
	  ((j_common_ptr) cinfo, JPOOL_IMAGE,
	   HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
  }
  cquantize->needs_zeroed = TRUE; /* histogram is garbage now */

  if (cinfo->enable_2pass_quant) {
	int desired = cinfo->desired_number_of_colors;
	if (desired < 8)
	  ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
	if (desired > MAXNUMCOLORS)
	  ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
	cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
	  ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3);
	cquantize->desired = desired;
  } else
	cquantize->sv_colormap = NULL;

  if (cinfo->dither_mode != JDITHER_NONE)
	cinfo->dither_mode = JDITHER_FS;

  if (cinfo->dither_mode == JDITHER_FS) {
	cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
	  ((j_common_ptr) cinfo, JPOOL_IMAGE,
	   (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))));
	init_error_limit(cinfo);
  }
}

#endif /* QUANT_2PASS_SUPPORTED */
/*** End of inlined file: jquant2.c ***/


/*** Start of inlined file: jutils.c ***/
#define JPEG_INTERNALS

#if 0				/* This table is not actually needed in v6a */

const int jpeg_zigzag_order[DCTSIZE2] = {
   0,  1,  5,  6, 14, 15, 27, 28,
   2,  4,  7, 13, 16, 26, 29, 42,
   3,  8, 12, 17, 25, 30, 41, 43,
   9, 11, 18, 24, 31, 40, 44, 53,
  10, 19, 23, 32, 39, 45, 52, 54,
  20, 22, 33, 38, 46, 51, 55, 60,
  21, 34, 37, 47, 50, 56, 59, 61,
  35, 36, 48, 49, 57, 58, 62, 63
};

#endif

const int jpeg_natural_order[DCTSIZE2+16] = {
  0,  1,  8, 16,  9,  2,  3, 10,
 17, 24, 32, 25, 18, 11,  4,  5,
 12, 19, 26, 33, 40, 48, 41, 34,
 27, 20, 13,  6,  7, 14, 21, 28,
 35, 42, 49, 56, 57, 50, 43, 36,
 29, 22, 15, 23, 30, 37, 44, 51,
 58, 59, 52, 45, 38, 31, 39, 46,
 53, 60, 61, 54, 47, 55, 62, 63,
 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
 63, 63, 63, 63, 63, 63, 63, 63
};

GLOBAL(long)
jdiv_round_up (long a, long b)
{
  return (a + b - 1L) / b;
}

GLOBAL(long)
jround_up (long a, long b)
{
  a += b - 1L;
  return a - (a % b);
}

#ifndef NEED_FAR_POINTERS	/* normal case, same as regular macros */
#define FMEMCOPY(dest,src,size)	MEMCOPY(dest,src,size)
#define FMEMZERO(target,size)	MEMZERO(target,size)
#else				/* 80x86 case, define if we can */
#ifdef USE_FMEM
#define FMEMCOPY(dest,src,size)	_fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
#define FMEMZERO(target,size)	_fmemset((void FAR *)(target), 0, (size_t)(size))
#endif
#endif

GLOBAL(void)
jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
		   JSAMPARRAY output_array, int dest_row,
		   int num_rows, JDIMENSION num_cols)
{
  register JSAMPROW inptr, outptr;
#ifdef FMEMCOPY
  register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
#else
  register JDIMENSION count;
#endif
  register int row;

  input_array += source_row;
  output_array += dest_row;

  for (row = num_rows; row > 0; row--) {
	inptr = *input_array++;
	outptr = *output_array++;
#ifdef FMEMCOPY
	FMEMCOPY(outptr, inptr, count);
#else
	for (count = num_cols; count > 0; count--)
	  *outptr++ = *inptr++;	/* needn't bother with GETJSAMPLE() here */
#endif
  }
}

GLOBAL(void)
jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
		 JDIMENSION num_blocks)
{
#ifdef FMEMCOPY
  FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
#else
  register JCOEFPTR inptr, outptr;
  register long count;

  inptr = (JCOEFPTR) input_row;
  outptr = (JCOEFPTR) output_row;
  for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
	*outptr++ = *inptr++;
  }
#endif
}

GLOBAL(void)
jzero_far (void FAR * target, size_t bytestozero)
{
#ifdef FMEMZERO
  FMEMZERO(target, bytestozero);
#else
  register char FAR * ptr = (char FAR *) target;
  register size_t count;

  for (count = bytestozero; count > 0; count--) {
	*ptr++ = 0;
  }
#endif
}
/*** End of inlined file: jutils.c ***/


/*** Start of inlined file: transupp.c ***/
#define JPEG_INTERNALS


/*** Start of inlined file: transupp.h ***/
#ifndef TRANSFORMS_SUPPORTED
#define TRANSFORMS_SUPPORTED 1		/* 0 disables transform code */
#endif

#ifdef NEED_SHORT_EXTERNAL_NAMES
#define jtransform_request_workspace		jTrRequest
#define jtransform_adjust_parameters		jTrAdjust
#define jtransform_execute_transformation	jTrExec
#define jcopy_markers_setup			jCMrkSetup
#define jcopy_markers_execute			jCMrkExec
#endif /* NEED_SHORT_EXTERNAL_NAMES */

typedef enum {
	JXFORM_NONE,		/* no transformation */
	JXFORM_FLIP_H,		/* horizontal flip */
	JXFORM_FLIP_V,		/* vertical flip */
	JXFORM_TRANSPOSE,	/* transpose across UL-to-LR axis */
	JXFORM_TRANSVERSE,	/* transpose across UR-to-LL axis */
	JXFORM_ROT_90,		/* 90-degree clockwise rotation */
	JXFORM_ROT_180,		/* 180-degree rotation */
	JXFORM_ROT_270		/* 270-degree clockwise (or 90 ccw) */
} JXFORM_CODE;

typedef struct {
  JXFORM_CODE transform;	/* image transform operator */
  boolean trim;			/* if TRUE, trim partial MCUs as needed */
  boolean force_grayscale;	/* if TRUE, convert color image to grayscale */

  int num_components;		/* # of components in workspace */
  jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
} jpeg_transform_info;

#if TRANSFORMS_SUPPORTED

EXTERN(void) jtransform_request_workspace
	JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info));
EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
		 jvirt_barray_ptr *src_coef_arrays,
		 jpeg_transform_info *info));
EXTERN(void) jtransform_execute_transformation
	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
		 jvirt_barray_ptr *src_coef_arrays,
		 jpeg_transform_info *info));

#endif /* TRANSFORMS_SUPPORTED */

typedef enum {
	JCOPYOPT_NONE,		/* copy no optional markers */
	JCOPYOPT_COMMENTS,	/* copy only comment (COM) markers */
	JCOPYOPT_ALL		/* copy all optional markers */
} JCOPY_OPTION;

#define JCOPYOPT_DEFAULT  JCOPYOPT_COMMENTS	/* recommended default */

EXTERN(void) jcopy_markers_setup
	JPP((j_decompress_ptr srcinfo, JCOPY_OPTION option));
EXTERN(void) jcopy_markers_execute
	JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
		 JCOPY_OPTION option));
/*** End of inlined file: transupp.h ***/

		/* My own external interface */

#if TRANSFORMS_SUPPORTED

LOCAL(void)
do_flip_h (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
	   jvirt_barray_ptr *src_coef_arrays)
{
  JDIMENSION MCU_cols, comp_width, blk_x, blk_y;
  int ci, k, offset_y;
  JBLOCKARRAY buffer;
  JCOEFPTR ptr1, ptr2;
  JCOEF temp1, temp2;
  jpeg_component_info *compptr;

  MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);

  for (ci = 0; ci < dstinfo->num_components; ci++) {
	compptr = dstinfo->comp_info + ci;
	comp_width = MCU_cols * compptr->h_samp_factor;
	for (blk_y = 0; blk_y < compptr->height_in_blocks;
	 blk_y += compptr->v_samp_factor) {
	  buffer = (*srcinfo->mem->access_virt_barray)
	((j_common_ptr) srcinfo, src_coef_arrays[ci], blk_y,
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
	  for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
	for (blk_x = 0; blk_x * 2 < comp_width; blk_x++) {
	  ptr1 = buffer[offset_y][blk_x];
	  ptr2 = buffer[offset_y][comp_width - blk_x - 1];
	  for (k = 0; k < DCTSIZE2; k += 2) {
		temp1 = *ptr1;	/* swap even column */
		temp2 = *ptr2;
		*ptr1++ = temp2;
		*ptr2++ = temp1;
		temp1 = *ptr1;	/* swap odd column with sign change */
		temp2 = *ptr2;
		*ptr1++ = -temp2;
		*ptr2++ = -temp1;
	  }
	}
	  }
	}
  }
}

LOCAL(void)
do_flip_v (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
	   jvirt_barray_ptr *src_coef_arrays,
	   jvirt_barray_ptr *dst_coef_arrays)
{
  JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
  int ci, i, j, offset_y;
  JBLOCKARRAY src_buffer, dst_buffer;
  JBLOCKROW src_row_ptr, dst_row_ptr;
  JCOEFPTR src_ptr, dst_ptr;
  jpeg_component_info *compptr;

  MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);

  for (ci = 0; ci < dstinfo->num_components; ci++) {
	compptr = dstinfo->comp_info + ci;
	comp_height = MCU_rows * compptr->v_samp_factor;
	for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
	 dst_blk_y += compptr->v_samp_factor) {
	  dst_buffer = (*srcinfo->mem->access_virt_barray)
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
	  if (dst_blk_y < comp_height) {
	src_buffer = (*srcinfo->mem->access_virt_barray)
	  ((j_common_ptr) srcinfo, src_coef_arrays[ci],
	   comp_height - dst_blk_y - (JDIMENSION) compptr->v_samp_factor,
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
	  } else {
	src_buffer = (*srcinfo->mem->access_virt_barray)
	  ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_y,
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
	  }
	  for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
	if (dst_blk_y < comp_height) {
	  dst_row_ptr = dst_buffer[offset_y];
	  src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
	  for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
		   dst_blk_x++) {
		dst_ptr = dst_row_ptr[dst_blk_x];
		src_ptr = src_row_ptr[dst_blk_x];
		for (i = 0; i < DCTSIZE; i += 2) {
		  for (j = 0; j < DCTSIZE; j++)
		*dst_ptr++ = *src_ptr++;
		  for (j = 0; j < DCTSIZE; j++)
		*dst_ptr++ = - *src_ptr++;
		}
	  }
	} else {
	  jcopy_block_row(src_buffer[offset_y], dst_buffer[offset_y],
			  compptr->width_in_blocks);
	}
	  }
	}
  }
}

LOCAL(void)
do_transpose (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
		  jvirt_barray_ptr *src_coef_arrays,
		  jvirt_barray_ptr *dst_coef_arrays)
{
  JDIMENSION dst_blk_x, dst_blk_y;
  int ci, i, j, offset_x, offset_y;
  JBLOCKARRAY src_buffer, dst_buffer;
  JCOEFPTR src_ptr, dst_ptr;
  jpeg_component_info *compptr;

  for (ci = 0; ci < dstinfo->num_components; ci++) {
	compptr = dstinfo->comp_info + ci;
	for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
	 dst_blk_y += compptr->v_samp_factor) {
	  dst_buffer = (*srcinfo->mem->access_virt_barray)
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
	  for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
		 dst_blk_x += compptr->h_samp_factor) {
	  src_buffer = (*srcinfo->mem->access_virt_barray)
		((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
		 (JDIMENSION) compptr->h_samp_factor, FALSE);
	  for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
		src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
		dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
		for (i = 0; i < DCTSIZE; i++)
		  for (j = 0; j < DCTSIZE; j++)
		dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
	  }
	}
	  }
	}
  }
}

LOCAL(void)
do_rot_90 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
	   jvirt_barray_ptr *src_coef_arrays,
	   jvirt_barray_ptr *dst_coef_arrays)
{
  JDIMENSION MCU_cols, comp_width, dst_blk_x, dst_blk_y;
  int ci, i, j, offset_x, offset_y;
  JBLOCKARRAY src_buffer, dst_buffer;
  JCOEFPTR src_ptr, dst_ptr;
  jpeg_component_info *compptr;

  MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);

  for (ci = 0; ci < dstinfo->num_components; ci++) {
	compptr = dstinfo->comp_info + ci;
	comp_width = MCU_cols * compptr->h_samp_factor;
	for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
	 dst_blk_y += compptr->v_samp_factor) {
	  dst_buffer = (*srcinfo->mem->access_virt_barray)
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
	  for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
		 dst_blk_x += compptr->h_samp_factor) {
	  src_buffer = (*srcinfo->mem->access_virt_barray)
		((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
		 (JDIMENSION) compptr->h_samp_factor, FALSE);
	  for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
		src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
		if (dst_blk_x < comp_width) {
		  dst_ptr = dst_buffer[offset_y]
		[comp_width - dst_blk_x - offset_x - 1];
		  for (i = 0; i < DCTSIZE; i++) {
		for (j = 0; j < DCTSIZE; j++)
		  dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
		i++;
		for (j = 0; j < DCTSIZE; j++)
		  dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
		  }
		} else {
		  dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
		  for (i = 0; i < DCTSIZE; i++)
		for (j = 0; j < DCTSIZE; j++)
		  dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
		}
	  }
	}
	  }
	}
  }
}

LOCAL(void)
do_rot_270 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
		jvirt_barray_ptr *src_coef_arrays,
		jvirt_barray_ptr *dst_coef_arrays)
{
  JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
  int ci, i, j, offset_x, offset_y;
  JBLOCKARRAY src_buffer, dst_buffer;
  JCOEFPTR src_ptr, dst_ptr;
  jpeg_component_info *compptr;

  MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);

  for (ci = 0; ci < dstinfo->num_components; ci++) {
	compptr = dstinfo->comp_info + ci;
	comp_height = MCU_rows * compptr->v_samp_factor;
	for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
	 dst_blk_y += compptr->v_samp_factor) {
	  dst_buffer = (*srcinfo->mem->access_virt_barray)
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
	  for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
		 dst_blk_x += compptr->h_samp_factor) {
	  src_buffer = (*srcinfo->mem->access_virt_barray)
		((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
		 (JDIMENSION) compptr->h_samp_factor, FALSE);
	  for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
		dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
		if (dst_blk_y < comp_height) {
		  src_ptr = src_buffer[offset_x]
		[comp_height - dst_blk_y - offset_y - 1];
		  for (i = 0; i < DCTSIZE; i++) {
		for (j = 0; j < DCTSIZE; j++) {
		  dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
		  j++;
		  dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
		}
		  }
		} else {
		  src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
		  for (i = 0; i < DCTSIZE; i++)
		for (j = 0; j < DCTSIZE; j++)
		  dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
		}
	  }
	}
	  }
	}
  }
}

LOCAL(void)
do_rot_180 (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
		jvirt_barray_ptr *src_coef_arrays,
		jvirt_barray_ptr *dst_coef_arrays)
{
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
  int ci, i, j, offset_y;
  JBLOCKARRAY src_buffer, dst_buffer;
  JBLOCKROW src_row_ptr, dst_row_ptr;
  JCOEFPTR src_ptr, dst_ptr;
  jpeg_component_info *compptr;

  MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
  MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);

  for (ci = 0; ci < dstinfo->num_components; ci++) {
	compptr = dstinfo->comp_info + ci;
	comp_width = MCU_cols * compptr->h_samp_factor;
	comp_height = MCU_rows * compptr->v_samp_factor;
	for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
	 dst_blk_y += compptr->v_samp_factor) {
	  dst_buffer = (*srcinfo->mem->access_virt_barray)
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
	  if (dst_blk_y < comp_height) {
	src_buffer = (*srcinfo->mem->access_virt_barray)
	  ((j_common_ptr) srcinfo, src_coef_arrays[ci],
	   comp_height - dst_blk_y - (JDIMENSION) compptr->v_samp_factor,
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
	  } else {
	src_buffer = (*srcinfo->mem->access_virt_barray)
	  ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_y,
	   (JDIMENSION) compptr->v_samp_factor, FALSE);
	  }
	  for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
	if (dst_blk_y < comp_height) {
	  dst_row_ptr = dst_buffer[offset_y];
	  src_row_ptr = src_buffer[compptr->v_samp_factor - offset_y - 1];
	  for (dst_blk_x = 0; dst_blk_x < comp_width; dst_blk_x++) {
		dst_ptr = dst_row_ptr[dst_blk_x];
		src_ptr = src_row_ptr[comp_width - dst_blk_x - 1];
		for (i = 0; i < DCTSIZE; i += 2) {
		  for (j = 0; j < DCTSIZE; j += 2) {
		*dst_ptr++ = *src_ptr++;
		*dst_ptr++ = - *src_ptr++;
		  }
		  for (j = 0; j < DCTSIZE; j += 2) {
		*dst_ptr++ = - *src_ptr++;
		*dst_ptr++ = *src_ptr++;
		  }
		}
	  }
	  for (; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
		dst_ptr = dst_row_ptr[dst_blk_x];
		src_ptr = src_row_ptr[dst_blk_x];
		for (i = 0; i < DCTSIZE; i += 2) {
		  for (j = 0; j < DCTSIZE; j++)
		*dst_ptr++ = *src_ptr++;
		  for (j = 0; j < DCTSIZE; j++)
		*dst_ptr++ = - *src_ptr++;
		}
	  }
	} else {
	  dst_row_ptr = dst_buffer[offset_y];
	  src_row_ptr = src_buffer[offset_y];
	  for (dst_blk_x = 0; dst_blk_x < comp_width; dst_blk_x++) {
		dst_ptr = dst_row_ptr[dst_blk_x];
		src_ptr = src_row_ptr[comp_width - dst_blk_x - 1];
		for (i = 0; i < DCTSIZE2; i += 2) {
		  *dst_ptr++ = *src_ptr++;
		  *dst_ptr++ = - *src_ptr++;
		}
	  }
	  for (; dst_blk_x < compptr->width_in_blocks; dst_blk_x++) {
		dst_ptr = dst_row_ptr[dst_blk_x];
		src_ptr = src_row_ptr[dst_blk_x];
		for (i = 0; i < DCTSIZE2; i++)
		  *dst_ptr++ = *src_ptr++;
	  }
	}
	  }
	}
  }
}

LOCAL(void)
do_transverse (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
		   jvirt_barray_ptr *src_coef_arrays,
		   jvirt_barray_ptr *dst_coef_arrays)
{
  JDIMENSION MCU_cols, MCU_rows, comp_width, comp_height, dst_blk_x, dst_blk_y;
  int ci, i, j, offset_x, offset_y;
  JBLOCKARRAY src_buffer, dst_buffer;
  JCOEFPTR src_ptr, dst_ptr;
  jpeg_component_info *compptr;

  MCU_cols = dstinfo->image_width / (dstinfo->max_h_samp_factor * DCTSIZE);
  MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);

  for (ci = 0; ci < dstinfo->num_components; ci++) {
	compptr = dstinfo->comp_info + ci;
	comp_width = MCU_cols * compptr->h_samp_factor;
	comp_height = MCU_rows * compptr->v_samp_factor;
	for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
	 dst_blk_y += compptr->v_samp_factor) {
	  dst_buffer = (*srcinfo->mem->access_virt_barray)
	((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
	 (JDIMENSION) compptr->v_samp_factor, TRUE);
	  for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
	for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
		 dst_blk_x += compptr->h_samp_factor) {
	  src_buffer = (*srcinfo->mem->access_virt_barray)
		((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_x,
		 (JDIMENSION) compptr->h_samp_factor, FALSE);
	  for (offset_x = 0; offset_x < compptr->h_samp_factor; offset_x++) {
		if (dst_blk_y < comp_height) {
		  src_ptr = src_buffer[offset_x]
		[comp_height - dst_blk_y - offset_y - 1];
		  if (dst_blk_x < comp_width) {
		dst_ptr = dst_buffer[offset_y]
		  [comp_width - dst_blk_x - offset_x - 1];
		for (i = 0; i < DCTSIZE; i++) {
		  for (j = 0; j < DCTSIZE; j++) {
			dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
			j++;
			dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
		  }
		  i++;
		  for (j = 0; j < DCTSIZE; j++) {
			dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
			j++;
			dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
		  }
		}
		  } else {
		dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
		for (i = 0; i < DCTSIZE; i++) {
		  for (j = 0; j < DCTSIZE; j++) {
			dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
			j++;
			dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
		  }
		}
		  }
		} else {
		  src_ptr = src_buffer[offset_x][dst_blk_y + offset_y];
		  if (dst_blk_x < comp_width) {
		dst_ptr = dst_buffer[offset_y]
		  [comp_width - dst_blk_x - offset_x - 1];
		for (i = 0; i < DCTSIZE; i++) {
		  for (j = 0; j < DCTSIZE; j++)
			dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
		  i++;
		  for (j = 0; j < DCTSIZE; j++)
			dst_ptr[j*DCTSIZE+i] = -src_ptr[i*DCTSIZE+j];
		}
		  } else {
		dst_ptr = dst_buffer[offset_y][dst_blk_x + offset_x];
		for (i = 0; i < DCTSIZE; i++)
		  for (j = 0; j < DCTSIZE; j++)
			dst_ptr[j*DCTSIZE+i] = src_ptr[i*DCTSIZE+j];
		  }
		}
	  }
	}
	  }
	}
  }
}

GLOBAL(void)
jtransform_request_workspace (j_decompress_ptr srcinfo,
				  jpeg_transform_info *info)
{
  jvirt_barray_ptr *coef_arrays = NULL;
  jpeg_component_info *compptr;
  int ci;

  if (info->force_grayscale &&
	  srcinfo->jpeg_color_space == JCS_YCbCr &&
	  srcinfo->num_components == 3) {
	info->num_components = 1;
  } else {
	info->num_components = srcinfo->num_components;
  }

  switch (info->transform) {
  case JXFORM_NONE:
  case JXFORM_FLIP_H:
	break;
  case JXFORM_FLIP_V:
  case JXFORM_ROT_180:
	coef_arrays = (jvirt_barray_ptr *)
	  (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
	SIZEOF(jvirt_barray_ptr) * info->num_components);
	for (ci = 0; ci < info->num_components; ci++) {
	  compptr = srcinfo->comp_info + ci;
	  coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
	((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
				(long) compptr->h_samp_factor),
	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
				(long) compptr->v_samp_factor),
	 (JDIMENSION) compptr->v_samp_factor);
	}
	break;
  case JXFORM_TRANSPOSE:
  case JXFORM_TRANSVERSE:
  case JXFORM_ROT_90:
  case JXFORM_ROT_270:
	coef_arrays = (jvirt_barray_ptr *)
	  (*srcinfo->mem->alloc_small) ((j_common_ptr) srcinfo, JPOOL_IMAGE,
	SIZEOF(jvirt_barray_ptr) * info->num_components);
	for (ci = 0; ci < info->num_components; ci++) {
	  compptr = srcinfo->comp_info + ci;
	  coef_arrays[ci] = (*srcinfo->mem->request_virt_barray)
	((j_common_ptr) srcinfo, JPOOL_IMAGE, FALSE,
	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
				(long) compptr->v_samp_factor),
	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
				(long) compptr->h_samp_factor),
	 (JDIMENSION) compptr->h_samp_factor);
	}
	break;
  }
  info->workspace_coef_arrays = coef_arrays;
}

LOCAL(void)
transpose_critical_parameters (j_compress_ptr dstinfo)
{
  int tblno, i, j, ci, itemp;
  jpeg_component_info *compptr;
  JQUANT_TBL *qtblptr;
  JDIMENSION dtemp;
  UINT16 qtemp;

  dtemp = dstinfo->image_width;
  dstinfo->image_width = dstinfo->image_height;
  dstinfo->image_height = dtemp;

  for (ci = 0; ci < dstinfo->num_components; ci++) {
	compptr = dstinfo->comp_info + ci;
	itemp = compptr->h_samp_factor;
	compptr->h_samp_factor = compptr->v_samp_factor;
	compptr->v_samp_factor = itemp;
  }

  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
	qtblptr = dstinfo->quant_tbl_ptrs[tblno];
	if (qtblptr != NULL) {
	  for (i = 0; i < DCTSIZE; i++) {
	for (j = 0; j < i; j++) {
	  qtemp = qtblptr->quantval[i*DCTSIZE+j];
	  qtblptr->quantval[i*DCTSIZE+j] = qtblptr->quantval[j*DCTSIZE+i];
	  qtblptr->quantval[j*DCTSIZE+i] = qtemp;
	}
	  }
	}
  }
}

LOCAL(void)
trim_right_edge (j_compress_ptr dstinfo)
{
  int ci, max_h_samp_factor;
  JDIMENSION MCU_cols;

  max_h_samp_factor = 1;
  for (ci = 0; ci < dstinfo->num_components; ci++) {
	int h_samp_factor = dstinfo->comp_info[ci].h_samp_factor;
	max_h_samp_factor = MAX(max_h_samp_factor, h_samp_factor);
  }
  MCU_cols = dstinfo->image_width / (max_h_samp_factor * DCTSIZE);
  if (MCU_cols > 0)		/* can't trim to 0 pixels */
	dstinfo->image_width = MCU_cols * (max_h_samp_factor * DCTSIZE);
}

LOCAL(void)
trim_bottom_edge (j_compress_ptr dstinfo)
{
  int ci, max_v_samp_factor;
  JDIMENSION MCU_rows;

  max_v_samp_factor = 1;
  for (ci = 0; ci < dstinfo->num_components; ci++) {
	int v_samp_factor = dstinfo->comp_info[ci].v_samp_factor;
	max_v_samp_factor = MAX(max_v_samp_factor, v_samp_factor);
  }
  MCU_rows = dstinfo->image_height / (max_v_samp_factor * DCTSIZE);
  if (MCU_rows > 0)		/* can't trim to 0 pixels */
	dstinfo->image_height = MCU_rows * (max_v_samp_factor * DCTSIZE);
}

GLOBAL(jvirt_barray_ptr *)
jtransform_adjust_parameters (j_decompress_ptr srcinfo,
				  j_compress_ptr dstinfo,
				  jvirt_barray_ptr *src_coef_arrays,
				  jpeg_transform_info *info)
{
  if (info->force_grayscale) {
	if ((dstinfo->jpeg_color_space == JCS_YCbCr &&
	 dstinfo->num_components == 3) ||
	(dstinfo->jpeg_color_space == JCS_GRAYSCALE &&
	 dstinfo->num_components == 1)) {
	  int sv_quant_tbl_no = dstinfo->comp_info[0].quant_tbl_no;
	  jpeg_set_colorspace(dstinfo, JCS_GRAYSCALE);
	  dstinfo->comp_info[0].quant_tbl_no = sv_quant_tbl_no;
	} else {
	  ERREXIT(dstinfo, JERR_CONVERSION_NOTIMPL);
	}
  }

  switch (info->transform) {
  case JXFORM_NONE:
	break;
  case JXFORM_FLIP_H:
	if (info->trim)
	  trim_right_edge(dstinfo);
	break;
  case JXFORM_FLIP_V:
	if (info->trim)
	  trim_bottom_edge(dstinfo);
	break;
  case JXFORM_TRANSPOSE:
	transpose_critical_parameters(dstinfo);
	break;
  case JXFORM_TRANSVERSE:
	transpose_critical_parameters(dstinfo);
	if (info->trim) {
	  trim_right_edge(dstinfo);
	  trim_bottom_edge(dstinfo);
	}
	break;
  case JXFORM_ROT_90:
	transpose_critical_parameters(dstinfo);
	if (info->trim)
	  trim_right_edge(dstinfo);
	break;
  case JXFORM_ROT_180:
	if (info->trim) {
	  trim_right_edge(dstinfo);
	  trim_bottom_edge(dstinfo);
	}
	break;
  case JXFORM_ROT_270:
	transpose_critical_parameters(dstinfo);
	if (info->trim)
	  trim_bottom_edge(dstinfo);
	break;
  }

  if (info->workspace_coef_arrays != NULL)
	return info->workspace_coef_arrays;
  return src_coef_arrays;
}

GLOBAL(void)
jtransform_execute_transformation (j_decompress_ptr srcinfo,
				   j_compress_ptr dstinfo,
				   jvirt_barray_ptr *src_coef_arrays,
				   jpeg_transform_info *info)
{
  jvirt_barray_ptr *dst_coef_arrays = info->workspace_coef_arrays;

  switch (info->transform) {
  case JXFORM_NONE:
	break;
  case JXFORM_FLIP_H:
	do_flip_h(srcinfo, dstinfo, src_coef_arrays);
	break;
  case JXFORM_FLIP_V:
	do_flip_v(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
	break;
  case JXFORM_TRANSPOSE:
	do_transpose(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
	break;
  case JXFORM_TRANSVERSE:
	do_transverse(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
	break;
  case JXFORM_ROT_90:
	do_rot_90(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
	break;
  case JXFORM_ROT_180:
	do_rot_180(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
	break;
  case JXFORM_ROT_270:
	do_rot_270(srcinfo, dstinfo, src_coef_arrays, dst_coef_arrays);
	break;
  }
}

#endif /* TRANSFORMS_SUPPORTED */

GLOBAL(void)
jcopy_markers_setup (j_decompress_ptr srcinfo, JCOPY_OPTION option)
{
#ifdef SAVE_MARKERS_SUPPORTED
  int m;

  if (option != JCOPYOPT_NONE) {
	jpeg_save_markers(srcinfo, JPEG_COM, 0xFFFF);
  }
  if (option == JCOPYOPT_ALL) {
	for (m = 0; m < 16; m++)
	  jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF);
  }
#endif /* SAVE_MARKERS_SUPPORTED */
}

GLOBAL(void)
jcopy_markers_execute (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
			   JCOPY_OPTION option)
{
  jpeg_saved_marker_ptr marker;

  for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) {
	if (dstinfo->write_JFIF_header &&
	marker->marker == JPEG_APP0 &&
	marker->data_length >= 5 &&
	GETJOCTET(marker->data[0]) == 0x4A &&
	GETJOCTET(marker->data[1]) == 0x46 &&
	GETJOCTET(marker->data[2]) == 0x49 &&
	GETJOCTET(marker->data[3]) == 0x46 &&
	GETJOCTET(marker->data[4]) == 0)
	  continue;			/* reject duplicate JFIF */
	if (dstinfo->write_Adobe_marker &&
	marker->marker == JPEG_APP0+14 &&
	marker->data_length >= 5 &&
	GETJOCTET(marker->data[0]) == 0x41 &&
	GETJOCTET(marker->data[1]) == 0x64 &&
	GETJOCTET(marker->data[2]) == 0x6F &&
	GETJOCTET(marker->data[3]) == 0x62 &&
	GETJOCTET(marker->data[4]) == 0x65)
	  continue;			/* reject duplicate Adobe */
#ifdef NEED_FAR_POINTERS
	{
	  unsigned int i;
	  jpeg_write_m_header(dstinfo, marker->marker, marker->data_length);
	  for (i = 0; i < marker->data_length; i++)
	jpeg_write_m_byte(dstinfo, marker->data[i]);
	}
#else
	jpeg_write_marker(dstinfo, marker->marker,
			  marker->data, marker->data_length);
#endif
  }
}
/*** End of inlined file: transupp.c ***/

  }
#else
  #define JPEG_INTERNALS
  #undef FAR
  #include <jpeglib.h>
#endif
}

#undef max
#undef min

#if JUCE_MSVC
  #pragma warning (pop)
#endif

BEGIN_JUCE_NAMESPACE

namespace JPEGHelpers
{
	using namespace jpeglibNamespace;

	#if ! JUCE_MSVC
	 using jpeglibNamespace::boolean;
	#endif

	struct JPEGDecodingFailure {};

	static void fatalErrorHandler (j_common_ptr)
	{
		throw JPEGDecodingFailure();
	}

	static void silentErrorCallback1 (j_common_ptr)	 {}
	static void silentErrorCallback2 (j_common_ptr, int)	{}
	static void silentErrorCallback3 (j_common_ptr, char*)  {}

	static void setupSilentErrorHandler (struct jpeg_error_mgr& err)
	{
		zerostruct (err);

		err.error_exit = fatalErrorHandler;
		err.emit_message = silentErrorCallback2;
		err.output_message = silentErrorCallback1;
		err.format_message = silentErrorCallback3;
		err.reset_error_mgr = silentErrorCallback1;
	}

	static void dummyCallback1 (j_decompress_ptr)
	{
	}

	static void jpegSkip (j_decompress_ptr decompStruct, long num)
	{
		decompStruct->src->next_input_byte += num;

		num = jmin (num, (long) decompStruct->src->bytes_in_buffer);
		decompStruct->src->bytes_in_buffer -= num;
	}

	static boolean jpegFill (j_decompress_ptr)
	{
		return 0;
	}

	static const int jpegBufferSize = 512;

	struct JuceJpegDest  : public jpeg_destination_mgr
	{
		OutputStream* output;
		char* buffer;
	};

	static void jpegWriteInit (j_compress_ptr)
	{
	}

	static void jpegWriteTerminate (j_compress_ptr cinfo)
	{
		JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);

		const size_t numToWrite = jpegBufferSize - dest->free_in_buffer;
		dest->output->write (dest->buffer, (int) numToWrite);
	}

	static boolean jpegWriteFlush (j_compress_ptr cinfo)
	{
		JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);

		const int numToWrite = jpegBufferSize;

		dest->next_output_byte = reinterpret_cast <JOCTET*> (dest->buffer);
		dest->free_in_buffer = jpegBufferSize;

		return dest->output->write (dest->buffer, numToWrite);
	}
}

Image* juce_loadJPEGImageFromStream (InputStream& in)
{
	using namespace jpeglibNamespace;
	using namespace JPEGHelpers;

	MemoryBlock mb;
	in.readIntoMemoryBlock (mb);

	Image* image = 0;

	if (mb.getSize() > 16)
	{
		struct jpeg_decompress_struct jpegDecompStruct;

		struct jpeg_error_mgr jerr;
		setupSilentErrorHandler (jerr);
		jpegDecompStruct.err = &jerr;

		jpeg_create_decompress (&jpegDecompStruct);

		jpegDecompStruct.src = (jpeg_source_mgr*)(jpegDecompStruct.mem->alloc_small)
			((j_common_ptr)(&jpegDecompStruct), JPOOL_PERMANENT, sizeof (jpeg_source_mgr));

		jpegDecompStruct.src->init_source	   = dummyCallback1;
		jpegDecompStruct.src->fill_input_buffer = jpegFill;
		jpegDecompStruct.src->skip_input_data   = jpegSkip;
		jpegDecompStruct.src->resync_to_restart = jpeg_resync_to_restart;
		jpegDecompStruct.src->term_source	   = dummyCallback1;

		jpegDecompStruct.src->next_input_byte   = static_cast <const unsigned char*> (mb.getData());
		jpegDecompStruct.src->bytes_in_buffer   = mb.getSize();

		try
		{
			jpeg_read_header (&jpegDecompStruct, TRUE);

			jpeg_calc_output_dimensions (&jpegDecompStruct);

			const int width = jpegDecompStruct.output_width;
			const int height = jpegDecompStruct.output_height;

			jpegDecompStruct.out_color_space = JCS_RGB;

			JSAMPARRAY buffer
				= (*jpegDecompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegDecompStruct,
														 JPOOL_IMAGE,
														 width * 3, 1);

			if (jpeg_start_decompress (&jpegDecompStruct))
			{
				image = Image::createNativeImage (Image::RGB, width, height, false);
				const bool hasAlphaChan = image->hasAlphaChannel();

				const Image::BitmapData destData (*image, 0, 0, width, height, true);

				for (int y = 0; y < height; ++y)
				{
					jpeg_read_scanlines (&jpegDecompStruct, buffer, 1);

					const uint8* src = *buffer;
					uint8* dest = destData.getLinePointer (y);

					if (hasAlphaChan)
					{
						for (int i = width; --i >= 0;)
						{
							((PixelARGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
							((PixelARGB*) dest)->premultiply();
							dest += destData.pixelStride;
							src += 3;
						}
					}
					else
					{
						for (int i = width; --i >= 0;)
						{
							((PixelRGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
							dest += destData.pixelStride;
							src += 3;
						}
					}
				}

				jpeg_finish_decompress (&jpegDecompStruct);

				in.setPosition (((char*) jpegDecompStruct.src->next_input_byte) - (char*) mb.getData());
			}

			jpeg_destroy_decompress (&jpegDecompStruct);
		}
		catch (...)
		{}
	}

	return image;
}

bool juce_writeJPEGImageToStream (const Image& image,
								  OutputStream& out,
								  float quality)
{
	using namespace jpeglibNamespace;
	using namespace JPEGHelpers;

	if (image.hasAlphaChannel())
	{
		// this method could fill the background in white and still save the image..
		jassertfalse
		return true;
	}

	struct jpeg_compress_struct jpegCompStruct;

	struct jpeg_error_mgr jerr;
	setupSilentErrorHandler (jerr);
	jpegCompStruct.err = &jerr;

	jpeg_create_compress (&jpegCompStruct);

	JuceJpegDest dest;
	jpegCompStruct.dest = &dest;

	dest.output = &out;
	HeapBlock <char> tempBuffer (jpegBufferSize);
	dest.buffer = tempBuffer;
	dest.next_output_byte = (JOCTET*) dest.buffer;
	dest.free_in_buffer = jpegBufferSize;
	dest.init_destination = jpegWriteInit;
	dest.empty_output_buffer = jpegWriteFlush;
	dest.term_destination = jpegWriteTerminate;

	jpegCompStruct.image_width = image.getWidth();
	jpegCompStruct.image_height = image.getHeight();
	jpegCompStruct.input_components = 3;
	jpegCompStruct.in_color_space = JCS_RGB;
	jpegCompStruct.write_JFIF_header = 1;

	jpegCompStruct.X_density = 72;
	jpegCompStruct.Y_density = 72;

	jpeg_set_defaults (&jpegCompStruct);

	jpegCompStruct.dct_method = JDCT_FLOAT;
	jpegCompStruct.optimize_coding = 1;
	//jpegCompStruct.smoothing_factor = 10;

	if (quality < 0.0f)
		quality = 0.85f;

	jpeg_set_quality (&jpegCompStruct, jlimit (0, 100, roundToInt (quality * 100.0f)), TRUE);

	jpeg_start_compress (&jpegCompStruct, TRUE);

	const int strideBytes = jpegCompStruct.image_width * jpegCompStruct.input_components;

	JSAMPARRAY buffer = (*jpegCompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegCompStruct,
															 JPOOL_IMAGE, strideBytes, 1);

	const Image::BitmapData srcData (image, 0, 0, jpegCompStruct.image_width, jpegCompStruct.image_height);

	while (jpegCompStruct.next_scanline < jpegCompStruct.image_height)
	{
		const uint8* src = srcData.getLinePointer (jpegCompStruct.next_scanline);
		uint8* dst = *buffer;

		for (int i = jpegCompStruct.image_width; --i >= 0;)
		{
			*dst++ = ((const PixelRGB*) src)->getRed();
			*dst++ = ((const PixelRGB*) src)->getGreen();
			*dst++ = ((const PixelRGB*) src)->getBlue();
			src += srcData.pixelStride;
		}

		jpeg_write_scanlines (&jpegCompStruct, buffer, 1);
	}

	jpeg_finish_compress (&jpegCompStruct);
	jpeg_destroy_compress (&jpegCompStruct);

	out.flush();

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_JPEGLoader.cpp ***/



/*** Start of inlined file: juce_PNGLoader.cpp ***/
#ifdef _MSC_VER
  #pragma warning (push)
  #pragma warning (disable: 4390 4611)
#endif

namespace zlibNamespace
{
#if JUCE_INCLUDE_ZLIB_CODE
  #undef OS_CODE
  #undef fdopen

  #undef OS_CODE
#else
  #include <zlib.h>
#endif
}

namespace pnglibNamespace
{
  using namespace zlibNamespace;

#if JUCE_INCLUDE_PNGLIB_CODE

 #if _MSC_VER != 1310
  using ::calloc; // (causes conflict in VS.NET 2003)
  using ::malloc;
  using ::free;
 #endif

  extern "C"
  {
	using ::abs;
	#define PNG_INTERNAL
	#define NO_DUMMY_DECL
	#define PNG_SETJMP_NOT_SUPPORTED


/*** Start of inlined file: png.h ***/
#ifndef PNG_H
#define PNG_H

#define PNG_LIBPNG_VER_STRING "1.2.21"
#define PNG_HEADER_VERSION_STRING \
   " libpng version 1.2.21 - October 4, 2007\n"

#define PNG_LIBPNG_VER_SONUM   0
#define PNG_LIBPNG_VER_DLLNUM  13

#define PNG_LIBPNG_VER_MAJOR   1
#define PNG_LIBPNG_VER_MINOR   2
#define PNG_LIBPNG_VER_RELEASE 21

#define PNG_LIBPNG_VER_BUILD  0

#define PNG_LIBPNG_BUILD_ALPHA	1
#define PNG_LIBPNG_BUILD_BETA	 2
#define PNG_LIBPNG_BUILD_RC	   3
#define PNG_LIBPNG_BUILD_STABLE   4
#define PNG_LIBPNG_BUILD_RELEASE_STATUS_MASK 7

#define PNG_LIBPNG_BUILD_PATCH	8 /* Can be OR'ed with
									   PNG_LIBPNG_BUILD_STABLE only */
#define PNG_LIBPNG_BUILD_PRIVATE 16 /* Cannot be OR'ed with
									   PNG_LIBPNG_BUILD_SPECIAL */
#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with
									   PNG_LIBPNG_BUILD_PRIVATE */

#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_STABLE

#define PNG_LIBPNG_VER 10221 /* 1.2.21 */

#ifndef PNG_VERSION_INFO_ONLY

#endif


/*** Start of inlined file: pngconf.h ***/
#ifndef PNGCONF_H
#define PNGCONF_H

#define PNG_1_2_X

// These are some Juce config settings that should remove any unnecessary code bloat..

#define PNG_NO_STDIO 1
#define PNG_DEBUG 0
#define PNG_NO_WARNINGS 1
#define PNG_NO_ERROR_TEXT 1
#define PNG_NO_ERROR_NUMBERS 1
#define PNG_NO_USER_MEM 1
#define PNG_NO_READ_iCCP 1
#define PNG_NO_READ_UNKNOWN_CHUNKS 1
#define PNG_NO_READ_USER_CHUNKS 1
#define PNG_NO_READ_iTXt 1
#define PNG_NO_READ_sCAL 1
#define PNG_NO_READ_sPLT 1

#define png_error(a, b) png_err(a)
#define png_warning(a, b)
#define png_chunk_error(a, b) png_err(a)
#define png_chunk_warning(a, b)

#ifdef PNG_USER_CONFIG
#  ifndef PNG_USER_PRIVATEBUILD
#	define PNG_USER_PRIVATEBUILD
#  endif
#include "pngusr.h"
#endif

#ifdef PNG_CONFIGURE_LIBPNG
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#endif

#ifdef __STDC__
#ifdef SPECIALBUILD
#  pragma message("PNG_LIBPNG_SPECIALBUILD (and deprecated SPECIALBUILD)\
 are now LIBPNG reserved macros. Use PNG_USER_PRIVATEBUILD instead.")
#endif

#ifdef PRIVATEBUILD
# pragma message("PRIVATEBUILD is deprecated.\
 Use PNG_USER_PRIVATEBUILD instead.")
# define PNG_USER_PRIVATEBUILD PRIVATEBUILD
#endif
#endif /* __STDC__ */

#ifndef PNG_VERSION_INFO_ONLY

#	define PNG_WARN_UNINITIALIZED_ROW 1

#ifndef PNG_ZBUF_SIZE
#  define PNG_ZBUF_SIZE 8192
#endif

#ifndef PNG_NO_READ_SUPPORTED
#  define PNG_READ_SUPPORTED
#endif

#ifndef PNG_NO_WRITE_SUPPORTED
#  define PNG_WRITE_SUPPORTED
#endif

#if !defined(PNG_1_0_X) && !defined(PNG_NO_MNG_FEATURES)
#  ifndef PNG_MNG_FEATURES_SUPPORTED
#	define PNG_MNG_FEATURES_SUPPORTED
#  endif
#endif

#ifndef PNG_NO_FLOATING_POINT_SUPPORTED
#  ifndef PNG_FLOATING_POINT_SUPPORTED
#	define PNG_FLOATING_POINT_SUPPORTED
#  endif
#endif

#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
#  define PNG_MAX_MALLOC_64K
#endif

#if defined(__CYGWIN__)
#  if defined(ALL_STATIC)
#	if defined(PNG_BUILD_DLL)
#	  undef PNG_BUILD_DLL
#	endif
#	if defined(PNG_USE_DLL)
#	  undef PNG_USE_DLL
#	endif
#	if defined(PNG_DLL)
#	  undef PNG_DLL
#	endif
#	if !defined(PNG_STATIC)
#	  define PNG_STATIC
#	endif
#  else
#	if defined (PNG_BUILD_DLL)
#	  if defined(PNG_STATIC)
#	undef PNG_STATIC
#	  endif
#	  if defined(PNG_USE_DLL)
#	undef PNG_USE_DLL
#	  endif
#	  if !defined(PNG_DLL)
#	define PNG_DLL
#	  endif
#	else
#	  if defined(PNG_STATIC)
#	if defined(PNG_USE_DLL)
#	  undef PNG_USE_DLL
#	endif
#	if defined(PNG_DLL)
#	  undef PNG_DLL
#	endif
#	  else
#	if !defined(PNG_USE_DLL)
#	  define PNG_USE_DLL
#	endif
#	if !defined(PNG_DLL)
#	  define PNG_DLL
#	endif
#	  endif
#	endif
#  endif
#endif

#if defined(_WIN32_WCE)
#  include <windows.h>
#  define PNG_NO_CONSOLE_IO
#  ifdef PNG_DEBUG
#	undef PNG_DEBUG
#  endif
#endif

#ifdef PNG_BUILD_DLL
#  ifndef PNG_CONSOLE_IO_SUPPORTED
#	ifndef PNG_NO_CONSOLE_IO
#	  define PNG_NO_CONSOLE_IO
#	endif
#  endif
#endif

#  ifdef PNG_NO_STDIO
#	ifndef PNG_NO_CONSOLE_IO
#	  define PNG_NO_CONSOLE_IO
#	endif
#	ifdef PNG_DEBUG
#	  if (PNG_DEBUG > 0)
#	include <stdio.h>
#	  endif
#	endif
#  else
#	if !defined(_WIN32_WCE)
#	  include <stdio.h>
#	endif
#  endif

#ifndef PNGARG

#ifdef OF /* zlib prototype munger */
#  define PNGARG(arglist) OF(arglist)
#else

#ifdef _NO_PROTO
#  define PNGARG(arglist) ()
#  ifndef PNG_TYPECAST_NULL
#	 define PNG_TYPECAST_NULL
#  endif
#else
#  define PNGARG(arglist) arglist
#endif /* _NO_PROTO */

#endif /* OF */

#endif /* PNGARG */

#ifndef MACOS
#  if (defined(__MWERKS__) && defined(macintosh)) || defined(applec) || \
	  defined(THINK_C) || defined(__SC__) || defined(TARGET_OS_MAC)
#	define MACOS
#  endif
#endif

#if !defined(MACOS) && !defined(RISCOS) && !defined(_WIN32_WCE)
#  include <sys/types.h>
#endif

#if !defined(PNG_SETJMP_NOT_SUPPORTED) && !defined(PNG_NO_SETJMP_SUPPORTED)
#  define PNG_SETJMP_SUPPORTED
#endif

#ifdef PNG_SETJMP_SUPPORTED

#  ifdef __linux__
#	ifdef _BSD_SOURCE
#	  define PNG_SAVE_BSD_SOURCE
#	  undef _BSD_SOURCE
#	endif
#	ifdef _SETJMP_H
		 __png.h__ already includes setjmp.h;
		 __dont__ include it again.;
#	endif
#  endif /* __linux__ */

#  include <setjmp.h>

#  ifdef __linux__
#	ifdef PNG_SAVE_BSD_SOURCE
#	  define _BSD_SOURCE
#	  undef PNG_SAVE_BSD_SOURCE
#	endif
#  endif /* __linux__ */
#endif /* PNG_SETJMP_SUPPORTED */

#ifdef BSD
#if ! JUCE_MAC
#  include <strings.h>
#endif
#else
#  include <string.h>
#endif

#ifdef PNG_INTERNAL

#include <stdlib.h>

#define PNG_EXTERN

#if defined(PNG_FLOATING_POINT_SUPPORTED)
#  if defined(MACOS)
#	if !defined(__MATH_H__) && !defined(__MATH_H) && !defined(__cmath__)
#	  include <fp.h>
#	endif
#  else
#	include <math.h>
#  endif
#  if defined(_AMIGA) && defined(__SASC) && defined(_M68881)
#	include <m68881.h>
#  endif
#endif

#if (defined(__MWERKS__) && defined(WIN32)) || defined(__STDC__)
#  define PNG_ALWAYS_EXTERN
#endif

#if defined(__TURBOC__) && defined(__MSDOS__)
#  include <mem.h>
#  include <alloc.h>
#endif

#if defined(_MSC_VER) && (defined(WIN32) || defined(_Windows) || \
	defined(_WINDOWS) || defined(_WIN32) || defined(__WIN32__))
#  include <malloc.h>
#endif

#ifndef PNG_DITHER_RED_BITS
#  define PNG_DITHER_RED_BITS 5
#endif
#ifndef PNG_DITHER_GREEN_BITS
#  define PNG_DITHER_GREEN_BITS 5
#endif
#ifndef PNG_DITHER_BLUE_BITS
#  define PNG_DITHER_BLUE_BITS 5
#endif

#ifndef PNG_MAX_GAMMA_8
#  define PNG_MAX_GAMMA_8 11
#endif

#ifndef PNG_GAMMA_THRESHOLD
#  define PNG_GAMMA_THRESHOLD 0.05
#endif

#endif /* PNG_INTERNAL */

#ifndef PNG_NO_CONST
#  define PNG_CONST const
#else
#  define PNG_CONST
#endif

#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
#  ifndef PNG_NO_iTXt_SUPPORTED
#	define PNG_NO_iTXt_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_iTXt
#	define PNG_NO_READ_iTXt
#  endif
#  ifndef PNG_NO_WRITE_iTXt
#	define PNG_NO_WRITE_iTXt
#  endif
#endif

#if !defined(PNG_NO_iTXt_SUPPORTED)
#  if !defined(PNG_READ_iTXt_SUPPORTED) && !defined(PNG_NO_READ_iTXt)
#	define PNG_READ_iTXt
#  endif
#  if !defined(PNG_WRITE_iTXt_SUPPORTED) && !defined(PNG_NO_WRITE_iTXt)
#	define PNG_WRITE_iTXt
#  endif
#endif

#ifdef PNG_LEGACY_SUPPORTED
#  define PNG_NO_FREE_ME
#  define PNG_NO_READ_UNKNOWN_CHUNKS
#  define PNG_NO_WRITE_UNKNOWN_CHUNKS
#  define PNG_NO_READ_USER_CHUNKS
#  define PNG_NO_READ_iCCP
#  define PNG_NO_WRITE_iCCP
#  define PNG_NO_READ_iTXt
#  define PNG_NO_WRITE_iTXt
#  define PNG_NO_READ_sCAL
#  define PNG_NO_WRITE_sCAL
#  define PNG_NO_READ_sPLT
#  define PNG_NO_WRITE_sPLT
#  define PNG_NO_INFO_IMAGE
#  define PNG_NO_READ_RGB_TO_GRAY
#  define PNG_NO_READ_USER_TRANSFORM
#  define PNG_NO_WRITE_USER_TRANSFORM
#  define PNG_NO_USER_MEM
#  define PNG_NO_READ_EMPTY_PLTE
#  define PNG_NO_MNG_FEATURES
#  define PNG_NO_FIXED_POINT_SUPPORTED
#endif

#if !defined(PNG_FLOATING_POINT_SUPPORTED) || \
	!defined(PNG_NO_FIXED_POINT_SUPPORTED)
#  define PNG_FIXED_POINT_SUPPORTED
#endif

#ifndef PNG_NO_FREE_ME
#  define PNG_FREE_ME_SUPPORTED
#endif

#if defined(PNG_READ_SUPPORTED)

#if !defined(PNG_READ_TRANSFORMS_NOT_SUPPORTED) && \
	  !defined(PNG_NO_READ_TRANSFORMS)
#  define PNG_READ_TRANSFORMS_SUPPORTED
#endif

#ifdef PNG_READ_TRANSFORMS_SUPPORTED
#  ifndef PNG_NO_READ_EXPAND
#	define PNG_READ_EXPAND_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_SHIFT
#	define PNG_READ_SHIFT_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_PACK
#	define PNG_READ_PACK_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_BGR
#	define PNG_READ_BGR_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_SWAP
#	define PNG_READ_SWAP_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_PACKSWAP
#	define PNG_READ_PACKSWAP_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_INVERT
#	define PNG_READ_INVERT_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_DITHER
#	define PNG_READ_DITHER_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_BACKGROUND
#	define PNG_READ_BACKGROUND_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_16_TO_8
#	define PNG_READ_16_TO_8_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_FILLER
#	define PNG_READ_FILLER_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_GAMMA
#	define PNG_READ_GAMMA_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_GRAY_TO_RGB
#	define PNG_READ_GRAY_TO_RGB_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_SWAP_ALPHA
#	define PNG_READ_SWAP_ALPHA_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_INVERT_ALPHA
#	define PNG_READ_INVERT_ALPHA_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_STRIP_ALPHA
#	define PNG_READ_STRIP_ALPHA_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_USER_TRANSFORM
#	define PNG_READ_USER_TRANSFORM_SUPPORTED
#  endif
#  ifndef PNG_NO_READ_RGB_TO_GRAY
#	define PNG_READ_RGB_TO_GRAY_SUPPORTED
#  endif
#endif /* PNG_READ_TRANSFORMS_SUPPORTED */

#if !defined(PNG_NO_PROGRESSIVE_READ) && \
 !defined(PNG_PROGRESSIVE_READ_SUPPORTED) /* if you don't do progressive   */
#  define PNG_PROGRESSIVE_READ_SUPPORTED  /* reading.  This is not talking */
#endif				/* about interlacing capability!  You'll */

#define PNG_READ_INTERLACING_SUPPORTED /* required in PNG-compliant decoders */

#ifndef PNG_NO_READ_COMPOSITE_NODIV
#  ifndef PNG_NO_READ_COMPOSITED_NODIV  /* libpng-1.0.x misspelling */
#	define PNG_READ_COMPOSITE_NODIV_SUPPORTED  /* well tested on Intel, SGI */
#  endif
#endif

#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
#ifndef PNG_NO_READ_EMPTY_PLTE
#  define PNG_READ_EMPTY_PLTE_SUPPORTED
#endif
#endif

#endif /* PNG_READ_SUPPORTED */

#if defined(PNG_WRITE_SUPPORTED)

# if !defined(PNG_WRITE_TRANSFORMS_NOT_SUPPORTED) && \
	!defined(PNG_NO_WRITE_TRANSFORMS)
#  define PNG_WRITE_TRANSFORMS_SUPPORTED
#endif

#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
#  ifndef PNG_NO_WRITE_SHIFT
#	define PNG_WRITE_SHIFT_SUPPORTED
#  endif
#  ifndef PNG_NO_WRITE_PACK
#	define PNG_WRITE_PACK_SUPPORTED
#  endif
#  ifndef PNG_NO_WRITE_BGR
#	define PNG_WRITE_BGR_SUPPORTED
#  endif
#  ifndef PNG_NO_WRITE_SWAP
#	define PNG_WRITE_SWAP_SUPPORTED
#  endif
#  ifndef PNG_NO_WRITE_PACKSWAP
#	define PNG_WRITE_PACKSWAP_SUPPORTED
#  endif
#  ifndef PNG_NO_WRITE_INVERT
#	define PNG_WRITE_INVERT_SUPPORTED
#  endif
#  ifndef PNG_NO_WRITE_FILLER
#	define PNG_WRITE_FILLER_SUPPORTED   /* same as WRITE_STRIP_ALPHA */
#  endif
#  ifndef PNG_NO_WRITE_SWAP_ALPHA
#	define PNG_WRITE_SWAP_ALPHA_SUPPORTED
#  endif
#  ifndef PNG_NO_WRITE_INVERT_ALPHA
#	define PNG_WRITE_INVERT_ALPHA_SUPPORTED
#  endif
#  ifndef PNG_NO_WRITE_USER_TRANSFORM
#	define PNG_WRITE_USER_TRANSFORM_SUPPORTED
#  endif
#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */

#if !defined(PNG_NO_WRITE_INTERLACING_SUPPORTED) && \
	!defined(PNG_WRITE_INTERLACING_SUPPORTED)
#define PNG_WRITE_INTERLACING_SUPPORTED  /* not required for PNG-compliant
											encoders, but can cause trouble
											if left undefined */
#endif

#if !defined(PNG_NO_WRITE_WEIGHTED_FILTER) && \
	!defined(PNG_WRITE_WEIGHTED_FILTER) && \
	 defined(PNG_FLOATING_POINT_SUPPORTED)
#  define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
#endif

#ifndef PNG_NO_WRITE_FLUSH
#  define PNG_WRITE_FLUSH_SUPPORTED
#endif

#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
#ifndef PNG_NO_WRITE_EMPTY_PLTE
#  define PNG_WRITE_EMPTY_PLTE_SUPPORTED
#endif
#endif

#endif /* PNG_WRITE_SUPPORTED */

#ifndef PNG_1_0_X
#  ifndef PNG_NO_ERROR_NUMBERS
#	define PNG_ERROR_NUMBERS_SUPPORTED
#  endif
#endif /* PNG_1_0_X */

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
#  ifndef PNG_NO_USER_TRANSFORM_PTR
#	define PNG_USER_TRANSFORM_PTR_SUPPORTED
#  endif
#endif

#ifndef PNG_NO_STDIO
#  define PNG_TIME_RFC1123_SUPPORTED
#endif

#if !defined(PNG_NO_EASY_ACCESS) && !defined(PNG_EASY_ACCESS_SUPPORTED)
#  define PNG_EASY_ACCESS_SUPPORTED
#endif

#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_OPTIMIZED_CODE)
#  ifndef PNG_OPTIMIZED_CODE_SUPPORTED
#	define PNG_OPTIMIZED_CODE_SUPPORTED
#  endif
#endif

#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_ASSEMBLER_CODE)
#  ifndef PNG_ASSEMBLER_CODE_SUPPORTED
#	define PNG_ASSEMBLER_CODE_SUPPORTED
#  endif

#  if defined(__GNUC__) && defined(__x86_64__) && (__GNUC__ < 4)
#	if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
#	  define PNG_NO_MMX_CODE
#	endif
#  endif

#  if defined(__APPLE__)
#	if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
#	  define PNG_NO_MMX_CODE
#	endif
#  endif

#  if (defined(__MWERKS__) && ((__MWERKS__ < 0x0900) || macintosh))
#	if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
#	  define PNG_NO_MMX_CODE
#	endif
#  endif

#  if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
#	define PNG_MMX_CODE_SUPPORTED
#  endif

#endif

#if !defined(PNG_1_0_X)
#if !defined(PNG_NO_USER_MEM) && !defined(PNG_USER_MEM_SUPPORTED)
#  define PNG_USER_MEM_SUPPORTED
#endif
#endif /* PNG_1_0_X */

#if !defined(PNG_1_0_X)
#ifndef PNG_SET_USER_LIMITS_SUPPORTED
#if !defined(PNG_NO_SET_USER_LIMITS) && !defined(PNG_SET_USER_LIMITS_SUPPORTED)
#  define PNG_SET_USER_LIMITS_SUPPORTED
#endif
#endif
#endif /* PNG_1_0_X */

#ifndef PNG_USER_WIDTH_MAX
#  define PNG_USER_WIDTH_MAX 1000000L
#endif
#ifndef PNG_USER_HEIGHT_MAX
#  define PNG_USER_HEIGHT_MAX 1000000L
#endif

#if defined(PNG_READ_SUPPORTED) && \
	!defined(PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \
	!defined(PNG_NO_READ_ANCILLARY_CHUNKS)
#  define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
#endif

#if defined(PNG_WRITE_SUPPORTED) && \
	!defined(PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \
	!defined(PNG_NO_WRITE_ANCILLARY_CHUNKS)
#  define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
#endif

#ifdef PNG_READ_ANCILLARY_CHUNKS_SUPPORTED

#ifdef PNG_NO_READ_TEXT
#  define PNG_NO_READ_iTXt
#  define PNG_NO_READ_tEXt
#  define PNG_NO_READ_zTXt
#endif
#ifndef PNG_NO_READ_bKGD
#  define PNG_READ_bKGD_SUPPORTED
#  define PNG_bKGD_SUPPORTED
#endif
#ifndef PNG_NO_READ_cHRM
#  define PNG_READ_cHRM_SUPPORTED
#  define PNG_cHRM_SUPPORTED
#endif
#ifndef PNG_NO_READ_gAMA
#  define PNG_READ_gAMA_SUPPORTED
#  define PNG_gAMA_SUPPORTED
#endif
#ifndef PNG_NO_READ_hIST
#  define PNG_READ_hIST_SUPPORTED
#  define PNG_hIST_SUPPORTED
#endif
#ifndef PNG_NO_READ_iCCP
#  define PNG_READ_iCCP_SUPPORTED
#  define PNG_iCCP_SUPPORTED
#endif
#ifndef PNG_NO_READ_iTXt
#  ifndef PNG_READ_iTXt_SUPPORTED
#	define PNG_READ_iTXt_SUPPORTED
#  endif
#  ifndef PNG_iTXt_SUPPORTED
#	define PNG_iTXt_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_READ_oFFs
#  define PNG_READ_oFFs_SUPPORTED
#  define PNG_oFFs_SUPPORTED
#endif
#ifndef PNG_NO_READ_pCAL
#  define PNG_READ_pCAL_SUPPORTED
#  define PNG_pCAL_SUPPORTED
#endif
#ifndef PNG_NO_READ_sCAL
#  define PNG_READ_sCAL_SUPPORTED
#  define PNG_sCAL_SUPPORTED
#endif
#ifndef PNG_NO_READ_pHYs
#  define PNG_READ_pHYs_SUPPORTED
#  define PNG_pHYs_SUPPORTED
#endif
#ifndef PNG_NO_READ_sBIT
#  define PNG_READ_sBIT_SUPPORTED
#  define PNG_sBIT_SUPPORTED
#endif
#ifndef PNG_NO_READ_sPLT
#  define PNG_READ_sPLT_SUPPORTED
#  define PNG_sPLT_SUPPORTED
#endif
#ifndef PNG_NO_READ_sRGB
#  define PNG_READ_sRGB_SUPPORTED
#  define PNG_sRGB_SUPPORTED
#endif
#ifndef PNG_NO_READ_tEXt
#  define PNG_READ_tEXt_SUPPORTED
#  define PNG_tEXt_SUPPORTED
#endif
#ifndef PNG_NO_READ_tIME
#  define PNG_READ_tIME_SUPPORTED
#  define PNG_tIME_SUPPORTED
#endif
#ifndef PNG_NO_READ_tRNS
#  define PNG_READ_tRNS_SUPPORTED
#  define PNG_tRNS_SUPPORTED
#endif
#ifndef PNG_NO_READ_zTXt
#  define PNG_READ_zTXt_SUPPORTED
#  define PNG_zTXt_SUPPORTED
#endif
#ifndef PNG_NO_READ_UNKNOWN_CHUNKS
#  define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED
#	define PNG_UNKNOWN_CHUNKS_SUPPORTED
#  endif
#  ifndef PNG_NO_HANDLE_AS_UNKNOWN
#	define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
#  endif
#endif
#if !defined(PNG_NO_READ_USER_CHUNKS) && \
	 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
#  define PNG_READ_USER_CHUNKS_SUPPORTED
#  define PNG_USER_CHUNKS_SUPPORTED
#  ifdef PNG_NO_READ_UNKNOWN_CHUNKS
#	undef PNG_NO_READ_UNKNOWN_CHUNKS
#  endif
#  ifdef PNG_NO_HANDLE_AS_UNKNOWN
#	undef PNG_NO_HANDLE_AS_UNKNOWN
#  endif
#endif
#ifndef PNG_NO_READ_OPT_PLTE
#  define PNG_READ_OPT_PLTE_SUPPORTED /* only affects support of the */
#endif			  /* optional PLTE chunk in RGB and RGBA images */
#if defined(PNG_READ_iTXt_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) || \
	defined(PNG_READ_zTXt_SUPPORTED)
#  define PNG_READ_TEXT_SUPPORTED
#  define PNG_TEXT_SUPPORTED
#endif

#endif /* PNG_READ_ANCILLARY_CHUNKS_SUPPORTED */

#ifdef PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED

#ifdef PNG_NO_WRITE_TEXT
#  define PNG_NO_WRITE_iTXt
#  define PNG_NO_WRITE_tEXt
#  define PNG_NO_WRITE_zTXt
#endif
#ifndef PNG_NO_WRITE_bKGD
#  define PNG_WRITE_bKGD_SUPPORTED
#  ifndef PNG_bKGD_SUPPORTED
#	define PNG_bKGD_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_cHRM
#  define PNG_WRITE_cHRM_SUPPORTED
#  ifndef PNG_cHRM_SUPPORTED
#	define PNG_cHRM_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_gAMA
#  define PNG_WRITE_gAMA_SUPPORTED
#  ifndef PNG_gAMA_SUPPORTED
#	define PNG_gAMA_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_hIST
#  define PNG_WRITE_hIST_SUPPORTED
#  ifndef PNG_hIST_SUPPORTED
#	define PNG_hIST_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_iCCP
#  define PNG_WRITE_iCCP_SUPPORTED
#  ifndef PNG_iCCP_SUPPORTED
#	define PNG_iCCP_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_iTXt
#  ifndef PNG_WRITE_iTXt_SUPPORTED
#	define PNG_WRITE_iTXt_SUPPORTED
#  endif
#  ifndef PNG_iTXt_SUPPORTED
#	define PNG_iTXt_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_oFFs
#  define PNG_WRITE_oFFs_SUPPORTED
#  ifndef PNG_oFFs_SUPPORTED
#	define PNG_oFFs_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_pCAL
#  define PNG_WRITE_pCAL_SUPPORTED
#  ifndef PNG_pCAL_SUPPORTED
#	define PNG_pCAL_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_sCAL
#  define PNG_WRITE_sCAL_SUPPORTED
#  ifndef PNG_sCAL_SUPPORTED
#	define PNG_sCAL_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_pHYs
#  define PNG_WRITE_pHYs_SUPPORTED
#  ifndef PNG_pHYs_SUPPORTED
#	define PNG_pHYs_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_sBIT
#  define PNG_WRITE_sBIT_SUPPORTED
#  ifndef PNG_sBIT_SUPPORTED
#	define PNG_sBIT_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_sPLT
#  define PNG_WRITE_sPLT_SUPPORTED
#  ifndef PNG_sPLT_SUPPORTED
#	define PNG_sPLT_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_sRGB
#  define PNG_WRITE_sRGB_SUPPORTED
#  ifndef PNG_sRGB_SUPPORTED
#	define PNG_sRGB_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_tEXt
#  define PNG_WRITE_tEXt_SUPPORTED
#  ifndef PNG_tEXt_SUPPORTED
#	define PNG_tEXt_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_tIME
#  define PNG_WRITE_tIME_SUPPORTED
#  ifndef PNG_tIME_SUPPORTED
#	define PNG_tIME_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_tRNS
#  define PNG_WRITE_tRNS_SUPPORTED
#  ifndef PNG_tRNS_SUPPORTED
#	define PNG_tRNS_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_zTXt
#  define PNG_WRITE_zTXt_SUPPORTED
#  ifndef PNG_zTXt_SUPPORTED
#	define PNG_zTXt_SUPPORTED
#  endif
#endif
#ifndef PNG_NO_WRITE_UNKNOWN_CHUNKS
#  define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED
#	define PNG_UNKNOWN_CHUNKS_SUPPORTED
#  endif
#  ifndef PNG_NO_HANDLE_AS_UNKNOWN
#	 ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
#	   define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
#	 endif
#  endif
#endif
#if defined(PNG_WRITE_iTXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \
	defined(PNG_WRITE_zTXt_SUPPORTED)
#  define PNG_WRITE_TEXT_SUPPORTED
#  ifndef PNG_TEXT_SUPPORTED
#	define PNG_TEXT_SUPPORTED
#  endif
#endif

#endif /* PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED */

#ifndef PNG_NO_INFO_IMAGE
#  define PNG_INFO_IMAGE_SUPPORTED
#endif

#if defined(PNG_tIME_SUPPORTED)
#  if !defined(_WIN32_WCE)
#	include <time.h>
#  endif
#endif

typedef unsigned long png_uint_32;
typedef long png_int_32;
typedef unsigned short png_uint_16;
typedef short png_int_16;
typedef unsigned char png_byte;

#ifdef PNG_SIZE_T
   typedef PNG_SIZE_T png_size_t;
#  define png_sizeof(x) png_convert_size(sizeof (x))
#else
   typedef size_t png_size_t;
#  define png_sizeof(x) sizeof (x)
#endif

#ifdef __BORLANDC__
#  if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__)
#	define LDATA 1
#  else
#	define LDATA 0
#  endif
#  if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__)
#	define PNG_MAX_MALLOC_64K
#	if (LDATA != 1)
#	  ifndef FAR
#	define FAR __far
#	  endif
#	  define USE_FAR_KEYWORD
#	endif   /* LDATA != 1 */
#  endif  /* __WIN32__, __FLAT__, __CYGWIN__ */
#endif   /* __BORLANDC__ */

#if defined(FAR)
#  if defined(M_I86MM)
#	define USE_FAR_KEYWORD
#	define FARDATA FAR
#	include <dos.h>
#  endif
#endif

#ifndef FAR
#  define FAR
#endif

#ifndef FARDATA
#  define FARDATA
#endif

typedef png_int_32 png_fixed_point;

typedef void		FAR * png_voidp;
typedef png_byte	FAR * png_bytep;
typedef png_uint_32	 FAR * png_uint_32p;
typedef png_int_32	  FAR * png_int_32p;
typedef png_uint_16	 FAR * png_uint_16p;
typedef png_int_16	  FAR * png_int_16p;
typedef PNG_CONST char  FAR * png_const_charp;
typedef char		FAR * png_charp;
typedef png_fixed_point FAR * png_fixed_point_p;

#ifndef PNG_NO_STDIO
#if defined(_WIN32_WCE)
typedef HANDLE		png_FILE_p;
#else
typedef FILE		* png_FILE_p;
#endif
#endif

#ifdef PNG_FLOATING_POINT_SUPPORTED
typedef double	  FAR * png_doublep;
#endif

typedef png_byte	FAR * FAR * png_bytepp;
typedef png_uint_32	 FAR * FAR * png_uint_32pp;
typedef png_int_32	  FAR * FAR * png_int_32pp;
typedef png_uint_16	 FAR * FAR * png_uint_16pp;
typedef png_int_16	  FAR * FAR * png_int_16pp;
typedef PNG_CONST char  FAR * FAR * png_const_charpp;
typedef char		FAR * FAR * png_charpp;
typedef png_fixed_point FAR * FAR * png_fixed_point_pp;
#ifdef PNG_FLOATING_POINT_SUPPORTED
typedef double	  FAR * FAR * png_doublepp;
#endif

typedef char		FAR * FAR * FAR * png_charppp;

#if 0
typedef charf *	 png_zcharp;
typedef charf * FAR *   png_zcharpp;
typedef z_stream FAR *  png_zstreamp;
#endif /* (PNG_1_0_X) || defined(PNG_1_2_X) */

#if !defined(PNG_DLL) && (defined(PNG_BUILD_DLL) || defined(PNG_USE_DLL))
#  define PNG_DLL
#endif
#if defined(__CYGWIN__)
#  if !defined(PNG_STATIC)
#	if defined(PNG_USE_GLOBAL_ARRAYS)
#	  undef PNG_USE_GLOBAL_ARRAYS
#	endif
#	if !defined(PNG_USE_LOCAL_ARRAYS)
#	  define PNG_USE_LOCAL_ARRAYS
#	endif
#  else
#	if defined(PNG_USE_LOCAL_ARRAYS) || defined(PNG_NO_GLOBAL_ARRAYS)
#	  if defined(PNG_USE_GLOBAL_ARRAYS)
#	undef PNG_USE_GLOBAL_ARRAYS
#	  endif
#	endif
#  endif
#  if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS)
#	define PNG_USE_LOCAL_ARRAYS
#  endif
#endif

#if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS)
#  if defined(PNG_NO_GLOBAL_ARRAYS) || \
	  (defined(__GNUC__) && defined(PNG_DLL)) || defined(_MSC_VER)
#	define PNG_USE_LOCAL_ARRAYS
#  else
#	define PNG_USE_GLOBAL_ARRAYS
#  endif
#endif

#if defined(__CYGWIN__)
#  undef PNGAPI
#  define PNGAPI __cdecl
#  undef PNG_IMPEXP
#  define PNG_IMPEXP
#endif

#if defined(__MINGW32__) && !defined(PNG_MODULEDEF)
#  ifndef PNG_NO_MODULEDEF
#	define PNG_NO_MODULEDEF
#  endif
#endif

#if !defined(PNG_IMPEXP) && defined(PNG_BUILD_DLL) && !defined(PNG_NO_MODULEDEF)
#  define PNG_IMPEXP
#endif

#if defined(PNG_DLL) || defined(_DLL) || defined(__DLL__ ) || \
	(( defined(_Windows) || defined(_WINDOWS) || \
	   defined(WIN32) || defined(_WIN32) || defined(__WIN32__) ))

#  ifndef PNGAPI
#	 if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800))
#	define PNGAPI __cdecl
#	 else
#	define PNGAPI _cdecl
#	 endif
#  endif

#  if !defined(PNG_IMPEXP) && (!defined(PNG_DLL) || \
	   0 /* WINCOMPILER_WITH_NO_SUPPORT_FOR_DECLIMPEXP */)
#	 define PNG_IMPEXP
#  endif

#  if !defined(PNG_IMPEXP)

#	 define PNG_EXPORT_TYPE1(type,symbol)  PNG_IMPEXP type PNGAPI symbol
#	 define PNG_EXPORT_TYPE2(type,symbol)  type PNG_IMPEXP PNGAPI symbol

#	 if defined(_MSC_VER) || defined(__BORLANDC__)
#	if (_MSC_VER >= 800) || (__BORLANDC__ >= 0x500)
#	   define PNG_EXPORT PNG_EXPORT_TYPE1
#	else
#	   define PNG_EXPORT PNG_EXPORT_TYPE2
#	   if defined(PNG_BUILD_DLL)
#		  define PNG_IMPEXP __export
#	   else
#		  define PNG_IMPEXP /*__import */ /* doesn't exist AFAIK in
												 VC++ */
#	   endif				 /* Exists in Borland C++ for
												 C++ classes (== huge) */
#	endif
#	 endif

#	 if !defined(PNG_IMPEXP)
#	if defined(PNG_BUILD_DLL)
#	   define PNG_IMPEXP __declspec(dllexport)
#	else
#	   define PNG_IMPEXP __declspec(dllimport)
#	endif
#	 endif
#  endif  /* PNG_IMPEXP */
#else /* !(DLL || non-cygwin WINDOWS) */
#   if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__)
#	  ifndef PNGAPI
#	 define PNGAPI _System
#	  endif
#   else
#	  if 0 /* ... other platforms, with other meanings */
#	  endif
#   endif
#endif

#ifndef PNGAPI
#  define PNGAPI
#endif
#ifndef PNG_IMPEXP
#  define PNG_IMPEXP
#endif

#ifdef PNG_BUILDSYMS
#  ifndef PNG_EXPORT
#	define PNG_EXPORT(type,symbol) PNG_FUNCTION_EXPORT symbol END
#  endif
#  ifdef PNG_USE_GLOBAL_ARRAYS
#	ifndef PNG_EXPORT_VAR
#	  define PNG_EXPORT_VAR(type) PNG_DATA_EXPORT
#	endif
#  endif
#endif

#ifndef PNG_EXPORT
#  define PNG_EXPORT(type,symbol) PNG_IMPEXP type PNGAPI symbol
#endif

#ifdef PNG_USE_GLOBAL_ARRAYS
#  ifndef PNG_EXPORT_VAR
#	define PNG_EXPORT_VAR(type) extern PNG_IMPEXP type
#  endif
#endif

#ifndef PNG_ABORT
#  define PNG_ABORT() abort()
#endif

#ifdef PNG_SETJMP_SUPPORTED
#  define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
#else
#  define png_jmpbuf(png_ptr) \
   (LIBPNG_WAS_COMPILED_WITH__PNG_SETJMP_NOT_SUPPORTED)
#endif

#if defined(USE_FAR_KEYWORD)  /* memory model independent fns */
#  define CHECK   1
#  define NOCHECK 0
#  define CVT_PTR(ptr) (png_far_to_near(png_ptr,ptr,CHECK))
#  define CVT_PTR_NOCHECK(ptr) (png_far_to_near(png_ptr,ptr,NOCHECK))
#  define png_snprintf _fsnprintf   /* Added to v 1.2.19 */
#  define png_strcpy  _fstrcpy
#  define png_strncpy _fstrncpy   /* Added to v 1.2.6 */
#  define png_strlen  _fstrlen
#  define png_memcmp  _fmemcmp	/* SJT: added */
#  define png_memcpy  _fmemcpy
#  define png_memset  _fmemset
#else /* use the usual functions */
#  define CVT_PTR(ptr)	 (ptr)
#  define CVT_PTR_NOCHECK(ptr) (ptr)
#  ifndef PNG_NO_SNPRINTF
#	ifdef _MSC_VER
#	  define png_snprintf _snprintf   /* Added to v 1.2.19 */
#	  define png_snprintf2 _snprintf
#	  define png_snprintf6 _snprintf
#	else
#	  define png_snprintf snprintf   /* Added to v 1.2.19 */
#	  define png_snprintf2 snprintf
#	  define png_snprintf6 snprintf
#	endif
#  else
#	define png_snprintf(s1,n,fmt,x1) sprintf(s1,fmt,x1)
#	define png_snprintf2(s1,n,fmt,x1,x2) sprintf(s1,fmt,x1,x2)
#	define png_snprintf6(s1,n,fmt,x1,x2,x3,x4,x5,x6) \
		sprintf(s1,fmt,x1,x2,x3,x4,x5,x6)
#  endif
#  define png_strcpy  strcpy
#  define png_strncpy strncpy	 /* Added to v 1.2.6 */
#  define png_strlen  strlen
#  define png_memcmp  memcmp	  /* SJT: added */
#  define png_memcpy  memcpy
#  define png_memset  memset
#endif

#if (PNG_ZBUF_SIZE > 65536L) && defined(PNG_MAX_MALLOC_64K)
#  undef PNG_ZBUF_SIZE
#  define PNG_ZBUF_SIZE 65536L
#endif

#endif /* PNG_VERSION_INFO_ONLY */

#endif /* PNGCONF_H */
/*** End of inlined file: pngconf.h ***/

#ifdef _MSC_VER
 #pragma warning (disable: 4996 4100)
#endif

#if defined(PNG_USER_PRIVATEBUILD)
#  define PNG_LIBPNG_BUILD_TYPE \
		  (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_PRIVATE)
#else
#  if defined(PNG_LIBPNG_SPECIALBUILD)
#	define PNG_LIBPNG_BUILD_TYPE \
			(PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_SPECIAL)
#  else
#	define PNG_LIBPNG_BUILD_TYPE (PNG_LIBPNG_BUILD_BASE_TYPE)
#  endif
#endif

#ifndef PNG_VERSION_INFO_ONLY

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#ifndef PNG_NO_TYPECAST_NULL
#define int_p_NULL		(int *)NULL
#define png_bytep_NULL		(png_bytep)NULL
#define png_bytepp_NULL	   (png_bytepp)NULL
#define png_doublep_NULL	  (png_doublep)NULL
#define png_error_ptr_NULL	(png_error_ptr)NULL
#define png_flush_ptr_NULL	(png_flush_ptr)NULL
#define png_free_ptr_NULL	 (png_free_ptr)NULL
#define png_infopp_NULL	   (png_infopp)NULL
#define png_malloc_ptr_NULL	   (png_malloc_ptr)NULL
#define png_read_status_ptr_NULL  (png_read_status_ptr)NULL
#define png_rw_ptr_NULL	   (png_rw_ptr)NULL
#define png_structp_NULL	  (png_structp)NULL
#define png_uint_16p_NULL	 (png_uint_16p)NULL
#define png_voidp_NULL		(png_voidp)NULL
#define png_write_status_ptr_NULL (png_write_status_ptr)NULL
#else
#define int_p_NULL		NULL
#define png_bytep_NULL		NULL
#define png_bytepp_NULL	   NULL
#define png_doublep_NULL	  NULL
#define png_error_ptr_NULL	NULL
#define png_flush_ptr_NULL	NULL
#define png_free_ptr_NULL	 NULL
#define png_infopp_NULL	   NULL
#define png_malloc_ptr_NULL	   NULL
#define png_read_status_ptr_NULL  NULL
#define png_rw_ptr_NULL	   NULL
#define png_structp_NULL	  NULL
#define png_uint_16p_NULL	 NULL
#define png_voidp_NULL		NULL
#define png_write_status_ptr_NULL NULL
#endif

#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
#ifdef PNG_USE_GLOBAL_ARRAYS
PNG_EXPORT_VAR (PNG_CONST char) png_libpng_ver[18];
#else
#define png_libpng_ver png_get_header_ver(NULL)
#endif

#ifdef PNG_USE_GLOBAL_ARRAYS
PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_start[7];
PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_inc[7];
PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_ystart[7];
PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_yinc[7];
PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_mask[7];
PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_dsp_mask[7];
#endif

#endif /* PNG_NO_EXTERN */

typedef struct png_color_struct
{
   png_byte red;
   png_byte green;
   png_byte blue;
} png_color;
typedef png_color FAR * png_colorp;
typedef png_color FAR * FAR * png_colorpp;

typedef struct png_color_16_struct
{
   png_byte index;	/* used for palette files */
   png_uint_16 red;   /* for use in red green blue files */
   png_uint_16 green;
   png_uint_16 blue;
   png_uint_16 gray;  /* for use in grayscale files */
} png_color_16;
typedef png_color_16 FAR * png_color_16p;
typedef png_color_16 FAR * FAR * png_color_16pp;

typedef struct png_color_8_struct
{
   png_byte red;   /* for use in red green blue files */
   png_byte green;
   png_byte blue;
   png_byte gray;  /* for use in grayscale files */
   png_byte alpha; /* for alpha channel files */
} png_color_8;
typedef png_color_8 FAR * png_color_8p;
typedef png_color_8 FAR * FAR * png_color_8pp;

typedef struct png_sPLT_entry_struct
{
   png_uint_16 red;
   png_uint_16 green;
   png_uint_16 blue;
   png_uint_16 alpha;
   png_uint_16 frequency;
} png_sPLT_entry;
typedef png_sPLT_entry FAR * png_sPLT_entryp;
typedef png_sPLT_entry FAR * FAR * png_sPLT_entrypp;

typedef struct png_sPLT_struct
{
   png_charp name;	   /* palette name */
   png_byte depth;	   /* depth of palette samples */
   png_sPLT_entryp entries;  /* palette entries */
   png_int_32 nentries;	  /* number of palette entries */
} png_sPLT_t;
typedef png_sPLT_t FAR * png_sPLT_tp;
typedef png_sPLT_t FAR * FAR * png_sPLT_tpp;

#ifdef PNG_TEXT_SUPPORTED
typedef struct png_text_struct
{
   int  compression;	   /* compression value:
							 -1: tEXt, none
							  0: zTXt, deflate
							  1: iTXt, none
							  2: iTXt, deflate  */
   png_charp key;          /* keyword, 1-79 character description of "text" */
   png_charp text;         /* comment, may be an empty string (ie "")
							  or a NULL pointer */
   png_size_t text_length; /* length of the text string */
#ifdef PNG_iTXt_SUPPORTED
   png_size_t itxt_length; /* length of the itxt string */
   png_charp lang;	 /* language code, 0-79 characters
							  or a NULL pointer */
   png_charp lang_key;	 /* keyword translated UTF-8 string, 0 or more
							  chars or a NULL pointer */
#endif
} png_text;
typedef png_text FAR * png_textp;
typedef png_text FAR * FAR * png_textpp;
#endif

#define PNG_TEXT_COMPRESSION_NONE_WR -3
#define PNG_TEXT_COMPRESSION_zTXt_WR -2
#define PNG_TEXT_COMPRESSION_NONE	-1
#define PNG_TEXT_COMPRESSION_zTXt	 0
#define PNG_ITXT_COMPRESSION_NONE	 1
#define PNG_ITXT_COMPRESSION_zTXt	 2
#define PNG_TEXT_COMPRESSION_LAST	 3  /* Not a valid value */

typedef struct png_time_struct
{
   png_uint_16 year; /* full year, as in, 1995 */
   png_byte month;   /* month of year, 1 - 12 */
   png_byte day;	 /* day of month, 1 - 31 */
   png_byte hour;	/* hour of day, 0 - 23 */
   png_byte minute;  /* minute of hour, 0 - 59 */
   png_byte second;  /* second of minute, 0 - 60 (for leap seconds) */
} png_time;
typedef png_time FAR * png_timep;
typedef png_time FAR * FAR * png_timepp;

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
typedef struct png_unknown_chunk_t
{
	png_byte name[5];
	png_byte *data;
	png_size_t size;

	png_byte location; /* mode of operation at read time */
}
png_unknown_chunk;
typedef png_unknown_chunk FAR * png_unknown_chunkp;
typedef png_unknown_chunk FAR * FAR * png_unknown_chunkpp;
#endif

typedef struct png_info_struct
{
   png_uint_32 width;	   /* width of image in pixels (from IHDR) */
   png_uint_32 height;	  /* height of image in pixels (from IHDR) */
   png_uint_32 valid;	   /* valid chunk data (see PNG_INFO_ below) */
   png_uint_32 rowbytes;	/* bytes needed to hold an untransformed row */
   png_colorp palette;	  /* array of color values (valid & PNG_INFO_PLTE) */
   png_uint_16 num_palette; /* number of color entries in "palette" (PLTE) */
   png_uint_16 num_trans;   /* number of transparent palette color (tRNS) */
   png_byte bit_depth;	  /* 1, 2, 4, 8, or 16 bits/channel (from IHDR) */
   png_byte color_type;	 /* see PNG_COLOR_TYPE_ below (from IHDR) */
   png_byte compression_type; /* must be PNG_COMPRESSION_TYPE_BASE (IHDR) */
   png_byte filter_type;	/* must be PNG_FILTER_TYPE_BASE (from IHDR) */
   png_byte interlace_type; /* One of PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */

   png_byte channels;	   /* number of data channels per pixel (1, 2, 3, 4) */
   png_byte pixel_depth;	/* number of bits per pixel */
   png_byte spare_byte;	 /* to align the data, and for future use */
   png_byte signature[8];   /* magic bytes read by libpng from start of file */

#if defined(PNG_gAMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
   float gamma; /* gamma value of image, if (valid & PNG_INFO_gAMA) */
#endif

#if defined(PNG_sRGB_SUPPORTED)
   png_byte srgb_intent; /* sRGB rendering intent [0, 1, 2, or 3] */
#endif

#if defined(PNG_TEXT_SUPPORTED)
   int num_text; /* number of comments read/to write */
   int max_text; /* current size of text array */
   png_textp text; /* array of comments read/to write */
#endif /* PNG_TEXT_SUPPORTED */

#if defined(PNG_tIME_SUPPORTED)
   png_time mod_time;
#endif

#if defined(PNG_sBIT_SUPPORTED)
   png_color_8 sig_bit; /* significant bits in color channels */
#endif

#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_EXPAND_SUPPORTED) || \
defined(PNG_READ_BACKGROUND_SUPPORTED)
   png_bytep trans; /* transparent values for paletted image */
   png_color_16 trans_values; /* transparent color for non-palette image */
#endif

#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
   png_color_16 background;
#endif

#if defined(PNG_oFFs_SUPPORTED)
   png_int_32 x_offset; /* x offset on page */
   png_int_32 y_offset; /* y offset on page */
   png_byte offset_unit_type; /* offset units type */
#endif

#if defined(PNG_pHYs_SUPPORTED)
   png_uint_32 x_pixels_per_unit; /* horizontal pixel density */
   png_uint_32 y_pixels_per_unit; /* vertical pixel density */
   png_byte phys_unit_type; /* resolution type (see PNG_RESOLUTION_ below) */
#endif

#if defined(PNG_hIST_SUPPORTED)
   png_uint_16p hist;
#endif

#ifdef PNG_cHRM_SUPPORTED
#ifdef PNG_FLOATING_POINT_SUPPORTED
   float x_white;
   float y_white;
   float x_red;
   float y_red;
   float x_green;
   float y_green;
   float x_blue;
   float y_blue;
#endif
#endif

#if defined(PNG_pCAL_SUPPORTED)
   png_charp pcal_purpose;  /* pCAL chunk description string */
   png_int_32 pcal_X0;	  /* minimum value */
   png_int_32 pcal_X1;	  /* maximum value */
   png_charp pcal_units;	/* Latin-1 string giving physical units */
   png_charpp pcal_params;  /* ASCII strings containing parameter values */
   png_byte pcal_type;	  /* equation type (see PNG_EQUATION_ below) */
   png_byte pcal_nparams;   /* number of parameters given in pcal_params */
#endif

#ifdef PNG_FREE_ME_SUPPORTED
   png_uint_32 free_me;	 /* flags items libpng is responsible for freeing */
#endif

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
   png_unknown_chunkp unknown_chunks;
   png_size_t unknown_chunks_num;
#endif

#if defined(PNG_iCCP_SUPPORTED)
   png_charp iccp_name;	 /* profile name */
   png_charp iccp_profile;  /* International Color Consortium profile data */
   png_uint_32 iccp_proflen;  /* ICC profile data length */
   png_byte iccp_compression; /* Always zero */
#endif

#if defined(PNG_sPLT_SUPPORTED)
   png_sPLT_tp splt_palettes;
   png_uint_32 splt_palettes_num;
#endif

#if defined(PNG_sCAL_SUPPORTED)
   png_byte scal_unit;	 /* unit of physical scale */
#ifdef PNG_FLOATING_POINT_SUPPORTED
   double scal_pixel_width;	/* width of one pixel */
   double scal_pixel_height;   /* height of one pixel */
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_charp scal_s_width;	 /* string containing height */
   png_charp scal_s_height;	/* string containing width */
#endif
#endif

#if defined(PNG_INFO_IMAGE_SUPPORTED)
   png_bytepp row_pointers;	/* the image bits */
#endif

#if defined(PNG_FIXED_POINT_SUPPORTED) && defined(PNG_gAMA_SUPPORTED)
   png_fixed_point int_gamma; /* gamma of image, if (valid & PNG_INFO_gAMA) */
#endif

#if defined(PNG_cHRM_SUPPORTED) && defined(PNG_FIXED_POINT_SUPPORTED)
   png_fixed_point int_x_white;
   png_fixed_point int_y_white;
   png_fixed_point int_x_red;
   png_fixed_point int_y_red;
   png_fixed_point int_x_green;
   png_fixed_point int_y_green;
   png_fixed_point int_x_blue;
   png_fixed_point int_y_blue;
#endif

} png_info;

typedef png_info FAR * png_infop;
typedef png_info FAR * FAR * png_infopp;

#define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL)
#define PNG_UINT_32_MAX ((png_uint_32)(-1))
#define PNG_SIZE_MAX ((png_size_t)(-1))
#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
#define PNG_MAX_UINT PNG_UINT_31_MAX
#endif

#define PNG_COLOR_MASK_PALETTE	1
#define PNG_COLOR_MASK_COLOR	  2
#define PNG_COLOR_MASK_ALPHA	  4

#define PNG_COLOR_TYPE_GRAY 0
#define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
#define PNG_COLOR_TYPE_RGB	(PNG_COLOR_MASK_COLOR)
#define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
#define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA
#define PNG_COLOR_TYPE_GA  PNG_COLOR_TYPE_GRAY_ALPHA

#define PNG_COMPRESSION_TYPE_BASE 0 /* Deflate method 8, 32K window */
#define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE

#define PNG_FILTER_TYPE_BASE	  0 /* Single row per-byte filtering */
#define PNG_INTRAPIXEL_DIFFERENCING 64 /* Used only in MNG datastreams */
#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE

#define PNG_INTERLACE_NONE	0 /* Non-interlaced image */
#define PNG_INTERLACE_ADAM7	   1 /* Adam7 interlacing */
#define PNG_INTERLACE_LAST	2 /* Not a valid value */

#define PNG_OFFSET_PIXEL	  0 /* Offset in pixels */
#define PNG_OFFSET_MICROMETER	 1 /* Offset in micrometers (1/10^6 meter) */
#define PNG_OFFSET_LAST	   2 /* Not a valid value */

#define PNG_EQUATION_LINEAR	   0 /* Linear transformation */
#define PNG_EQUATION_BASE_E	   1 /* Exponential base e transform */
#define PNG_EQUATION_ARBITRARY	2 /* Arbitrary base exponential transform */
#define PNG_EQUATION_HYPERBOLIC   3 /* Hyperbolic sine transformation */
#define PNG_EQUATION_LAST	 4 /* Not a valid value */

#define PNG_SCALE_UNKNOWN	 0 /* unknown unit (image scale) */
#define PNG_SCALE_METER	   1 /* meters per pixel */
#define PNG_SCALE_RADIAN	  2 /* radians per pixel */
#define PNG_SCALE_LAST		3 /* Not a valid value */

#define PNG_RESOLUTION_UNKNOWN	0 /* pixels/unknown unit (aspect ratio) */
#define PNG_RESOLUTION_METER	  1 /* pixels/meter */
#define PNG_RESOLUTION_LAST	   2 /* Not a valid value */

#define PNG_sRGB_INTENT_PERCEPTUAL 0
#define PNG_sRGB_INTENT_RELATIVE   1
#define PNG_sRGB_INTENT_SATURATION 2
#define PNG_sRGB_INTENT_ABSOLUTE   3
#define PNG_sRGB_INTENT_LAST	   4 /* Not a valid value */

#define PNG_KEYWORD_MAX_LENGTH	 79

#define PNG_MAX_PALETTE_LENGTH	256

#define PNG_INFO_gAMA 0x0001
#define PNG_INFO_sBIT 0x0002
#define PNG_INFO_cHRM 0x0004
#define PNG_INFO_PLTE 0x0008
#define PNG_INFO_tRNS 0x0010
#define PNG_INFO_bKGD 0x0020
#define PNG_INFO_hIST 0x0040
#define PNG_INFO_pHYs 0x0080
#define PNG_INFO_oFFs 0x0100
#define PNG_INFO_tIME 0x0200
#define PNG_INFO_pCAL 0x0400
#define PNG_INFO_sRGB 0x0800   /* GR-P, 0.96a */
#define PNG_INFO_iCCP 0x1000   /* ESR, 1.0.6 */
#define PNG_INFO_sPLT 0x2000   /* ESR, 1.0.6 */
#define PNG_INFO_sCAL 0x4000   /* ESR, 1.0.6 */
#define PNG_INFO_IDAT 0x8000L  /* ESR, 1.0.6 */

typedef struct png_row_info_struct
{
   png_uint_32 width; /* width of row */
   png_uint_32 rowbytes; /* number of bytes in row */
   png_byte color_type; /* color type of row */
   png_byte bit_depth; /* bit depth of row */
   png_byte channels; /* number of channels (1, 2, 3, or 4) */
   png_byte pixel_depth; /* bits per pixel (depth * channels) */
} png_row_info;

typedef png_row_info FAR * png_row_infop;
typedef png_row_info FAR * FAR * png_row_infopp;

typedef struct png_struct_def png_struct;
typedef png_struct FAR * png_structp;

typedef void (PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp));
typedef void (PNGAPI *png_rw_ptr) PNGARG((png_structp, png_bytep, png_size_t));
typedef void (PNGAPI *png_flush_ptr) PNGARG((png_structp));
typedef void (PNGAPI *png_read_status_ptr) PNGARG((png_structp, png_uint_32,
   int));
typedef void (PNGAPI *png_write_status_ptr) PNGARG((png_structp, png_uint_32,
   int));

#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
typedef void (PNGAPI *png_progressive_info_ptr) PNGARG((png_structp, png_infop));
typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop));
typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep,
   png_uint_32, int));
#endif

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_LEGACY_SUPPORTED)
typedef void (PNGAPI *png_user_transform_ptr) PNGARG((png_structp,
	png_row_infop, png_bytep));
#endif

#if defined(PNG_USER_CHUNKS_SUPPORTED)
typedef int (PNGAPI *png_user_chunk_ptr) PNGARG((png_structp, png_unknown_chunkp));
#endif
#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
typedef void (PNGAPI *png_unknown_chunk_ptr) PNGARG((png_structp));
#endif

#define PNG_TRANSFORM_IDENTITY	   0x0000	/* read and write */
#define PNG_TRANSFORM_STRIP_16	   0x0001	/* read only */
#define PNG_TRANSFORM_STRIP_ALPHA	0x0002	/* read only */
#define PNG_TRANSFORM_PACKING	0x0004	/* read and write */
#define PNG_TRANSFORM_PACKSWAP	   0x0008	/* read and write */
#define PNG_TRANSFORM_EXPAND	 0x0010	/* read only */
#define PNG_TRANSFORM_INVERT_MONO	0x0020	/* read and write */
#define PNG_TRANSFORM_SHIFT	  0x0040	/* read and write */
#define PNG_TRANSFORM_BGR		0x0080	/* read and write */
#define PNG_TRANSFORM_SWAP_ALPHA	 0x0100	/* read and write */
#define PNG_TRANSFORM_SWAP_ENDIAN	0x0200	/* read and write */
#define PNG_TRANSFORM_INVERT_ALPHA   0x0400	/* read and write */
#define PNG_TRANSFORM_STRIP_FILLER   0x0800	/* WRITE only */

#define PNG_FLAG_MNG_EMPTY_PLTE	 0x01
#define PNG_FLAG_MNG_FILTER_64	  0x04
#define PNG_ALL_MNG_FEATURES	0x05

typedef png_voidp (*png_malloc_ptr) PNGARG((png_structp, png_size_t));
typedef void (*png_free_ptr) PNGARG((png_structp, png_voidp));

struct png_struct_def
{
#ifdef PNG_SETJMP_SUPPORTED
   jmp_buf jmpbuf;		/* used in png_error */
#endif
   png_error_ptr error_fn;	/* function for printing errors and aborting */
   png_error_ptr warning_fn;  /* function for printing warnings */
   png_voidp error_ptr;	   /* user supplied struct for error functions */
   png_rw_ptr write_data_fn;  /* function for writing output data */
   png_rw_ptr read_data_fn;   /* function for reading input data */
   png_voidp io_ptr;	  /* ptr to application struct for I/O functions */

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
   png_user_transform_ptr read_user_transform_fn; /* user read transform */
#endif

#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   png_user_transform_ptr write_user_transform_fn; /* user write transform */
#endif

#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   png_voidp user_transform_ptr; /* user supplied struct for user transform */
   png_byte user_transform_depth;	/* bit depth of user transformed pixels */
   png_byte user_transform_channels; /* channels in user transformed pixels */
#endif
#endif

   png_uint_32 mode;	  /* tells us where we are in the PNG file */
   png_uint_32 flags;	 /* flags indicating various things to libpng */
   png_uint_32 transformations; /* which transformations to perform */

   z_stream zstream;	  /* pointer to decompression structure (below) */
   png_bytep zbuf;		/* buffer for zlib */
   png_size_t zbuf_size;	  /* size of zbuf */
   int zlib_level;		/* holds zlib compression level */
   int zlib_method;	   /* holds zlib compression method */
   int zlib_window_bits;	  /* holds zlib compression window bits */
   int zlib_mem_level;	/* holds zlib compression memory level */
   int zlib_strategy;	 /* holds zlib compression strategy */

   png_uint_32 width;	 /* width of image in pixels */
   png_uint_32 height;	/* height of image in pixels */
   png_uint_32 num_rows;	  /* number of rows in current pass */
   png_uint_32 usr_width;	 /* width of row at start of write */
   png_uint_32 rowbytes;	  /* size of row in bytes */
   png_uint_32 irowbytes;	 /* size of current interlaced row in bytes */
   png_uint_32 iwidth;	/* width of current interlaced row in pixels */
   png_uint_32 row_number;	/* current row in interlace pass */
   png_bytep prev_row;	/* buffer to save previous (unfiltered) row */
   png_bytep row_buf;	 /* buffer to save current (unfiltered) row */
   png_bytep sub_row;         /* buffer to save "sub" row when filtering */
   png_bytep up_row;          /* buffer to save "up" row when filtering */
   png_bytep avg_row;         /* buffer to save "avg" row when filtering */
   png_bytep paeth_row;       /* buffer to save "Paeth" row when filtering */
   png_row_info row_info;	 /* used for transformation routines */

   png_uint_32 idat_size;	 /* current IDAT size for read */
   png_uint_32 crc;	   /* current chunk CRC value */
   png_colorp palette;	/* palette from the input file */
   png_uint_16 num_palette;   /* number of color entries in palette */
   png_uint_16 num_trans;	 /* number of transparency values */
   png_byte chunk_name[5];	/* null-terminated name of current chunk */
   png_byte compression;	  /* file compression type (always 0) */
   png_byte filter;	   /* file filter type (always 0) */
   png_byte interlaced;	   /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
   png_byte pass;		 /* current interlace pass (0 - 6) */
   png_byte do_filter;	/* row filter flags (see PNG_FILTER_ below ) */
   png_byte color_type;	   /* color type of file */
   png_byte bit_depth;	/* bit depth of file */
   png_byte usr_bit_depth;	/* bit depth of users row */
   png_byte pixel_depth;	  /* number of bits per pixel */
   png_byte channels;	 /* number of channels in file */
   png_byte usr_channels;	 /* channels at start of write */
   png_byte sig_bytes;	/* magic bytes read/written from start of file */

#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
#ifdef PNG_LEGACY_SUPPORTED
   png_byte filler;	   /* filler byte for pixel expansion */
#else
   png_uint_16 filler;	   /* filler bytes for pixel expansion */
#endif
#endif

#if defined(PNG_bKGD_SUPPORTED)
   png_byte background_gamma_type;
#  ifdef PNG_FLOATING_POINT_SUPPORTED
   float background_gamma;
#  endif
   png_color_16 background;   /* background color in screen gamma space */
#if defined(PNG_READ_GAMMA_SUPPORTED)
   png_color_16 background_1; /* background normalized to gamma 1.0 */
#endif
#endif /* PNG_bKGD_SUPPORTED */

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
   png_flush_ptr output_flush_fn;/* Function for flushing output */
   png_uint_32 flush_dist;	/* how many rows apart to flush, 0 - no flush */
   png_uint_32 flush_rows;	/* number of rows written since last flush */
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
   int gamma_shift;      /* number of "insignificant" bits 16-bit gamma */
#ifdef PNG_FLOATING_POINT_SUPPORTED
   float gamma;	  /* file gamma value */
   float screen_gamma;   /* screen gamma value (display_exponent) */
#endif
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
   png_bytep gamma_table;	 /* gamma table for 8-bit depth files */
   png_bytep gamma_from_1;	/* converts from 1.0 to screen */
   png_bytep gamma_to_1;	  /* converts from file to 1.0 */
   png_uint_16pp gamma_16_table; /* gamma table for 16-bit depth files */
   png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */
   png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED)
   png_color_8 sig_bit;	   /* significant bits in each available channel */
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
   png_color_8 shift;	 /* shift for significant bit tranformation */
#endif

#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \
 || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
   png_bytep trans;	   /* transparency values for paletted files */
   png_color_16 trans_values; /* transparency values for non-paletted files */
#endif

   png_read_status_ptr read_row_fn;   /* called after each row is decoded */
   png_write_status_ptr write_row_fn; /* called after each row is encoded */
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
   png_progressive_info_ptr info_fn; /* called after header data fully read */
   png_progressive_row_ptr row_fn;   /* called after each prog. row is decoded */
   png_progressive_end_ptr end_fn;   /* called after image is complete */
   png_bytep save_buffer_ptr;	/* current location in save_buffer */
   png_bytep save_buffer;		/* buffer for previously read data */
   png_bytep current_buffer_ptr;	 /* current location in current_buffer */
   png_bytep current_buffer;	 /* buffer for recently used data */
   png_uint_32 push_length;	  /* size of current input chunk */
   png_uint_32 skip_length;	  /* bytes to skip in input data */
   png_size_t save_buffer_size;	  /* amount of data now in save_buffer */
   png_size_t save_buffer_max;	   /* total size of save_buffer */
   png_size_t buffer_size;	   /* total amount of available input data */
   png_size_t current_buffer_size;   /* amount of data now in current_buffer */
   int process_mode;		 /* what push library is currently doing */
   int cur_palette;		  /* current push library palette index */

#  if defined(PNG_TEXT_SUPPORTED)
	 png_size_t current_text_size;   /* current size of text input data */
	 png_size_t current_text_left;   /* how much text left to read in input */
	 png_charp current_text;	 /* current text chunk buffer */
	 png_charp current_text_ptr;	 /* current location in current_text */
#  endif /* PNG_TEXT_SUPPORTED */
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */

#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
   png_bytepp offset_table_ptr;
   png_bytep offset_table;
   png_uint_16 offset_table_number;
   png_uint_16 offset_table_count;
   png_uint_16 offset_table_count_free;
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
   png_bytep palette_lookup;	 /* lookup table for dithering */
   png_bytep dither_index;	   /* index translation for palette files */
#endif

#if defined(PNG_READ_DITHER_SUPPORTED) || defined(PNG_hIST_SUPPORTED)
   png_uint_16p hist;		/* histogram */
#endif

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
   png_byte heuristic_method;	/* heuristic for row filter selection */
   png_byte num_prev_filters;	/* number of weights for previous rows */
   png_bytep prev_filters;	   /* filter type(s) of previous row(s) */
   png_uint_16p filter_weights;	  /* weight(s) for previous line(s) */
   png_uint_16p inv_filter_weights;  /* 1/weight(s) for previous line(s) */
   png_uint_16p filter_costs;	/* relative filter calculation cost */
   png_uint_16p inv_filter_costs;	/* 1/relative filter calculation cost */
#endif

#if defined(PNG_TIME_RFC1123_SUPPORTED)
   png_charp time_buffer;		/* String to hold RFC 1123 time text */
#endif

#ifdef PNG_FREE_ME_SUPPORTED
   png_uint_32 free_me;	   /* flags items libpng is responsible for freeing */
#endif

#if defined(PNG_USER_CHUNKS_SUPPORTED)
   png_voidp user_chunk_ptr;
   png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */
#endif

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
   int num_chunk_list;
   png_bytep chunk_list;
#endif

#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
   png_byte rgb_to_gray_status;
   png_uint_16 rgb_to_gray_red_coeff;
   png_uint_16 rgb_to_gray_green_coeff;
   png_uint_16 rgb_to_gray_blue_coeff;
#endif

#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
	defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
	defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
#ifdef PNG_1_0_X
   png_byte mng_features_permitted;
#else
   png_uint_32 mng_features_permitted;
#endif /* PNG_1_0_X */
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
   png_fixed_point int_gamma;
#endif

#if defined(PNG_MNG_FEATURES_SUPPORTED)
   png_byte filter_type;
#endif

#if defined(PNG_1_0_X)
   png_uint_32 row_buf_size;
#endif

#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
#  if !defined(PNG_1_0_X)
#	if defined(PNG_MMX_CODE_SUPPORTED)
   png_byte	 mmx_bitdepth_threshold;
   png_uint_32  mmx_rowbytes_threshold;
#	endif
   png_uint_32  asm_flags;
#  endif
#endif

#ifdef PNG_USER_MEM_SUPPORTED
   png_voidp mem_ptr;		/* user supplied struct for mem functions */
   png_malloc_ptr malloc_fn;	 /* function for allocating memory */
   png_free_ptr free_fn;		 /* function for freeing memory */
#endif

   png_bytep big_row_buf;	 /* buffer to save current (unfiltered) row */

#if defined(PNG_READ_DITHER_SUPPORTED)
   png_bytep dither_sort;		/* working sort array */
   png_bytep index_to_palette;	   /* where the original index currently is */
   png_bytep palette_to_index;	   /* which original index points to this */
#endif

   png_byte compression_type;

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   png_uint_32 user_width_max;
   png_uint_32 user_height_max;
#endif

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
   png_unknown_chunk unknown_chunk;
#endif
};

typedef png_structp version_1_2_21;

typedef png_struct FAR * FAR * png_structpp;

extern PNG_EXPORT(png_uint_32,png_access_version_number) PNGARG((void));

extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
   int num_bytes));

extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig, png_size_t start,
   png_size_t num_to_check));

extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));

extern PNG_EXPORT(png_structp,png_create_read_struct)
   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn));

extern PNG_EXPORT(png_structp,png_create_write_struct)
   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn));

#ifdef PNG_WRITE_SUPPORTED
extern PNG_EXPORT(png_uint_32,png_get_compression_buffer_size)
   PNGARG((png_structp png_ptr));
#endif

#ifdef PNG_WRITE_SUPPORTED
extern PNG_EXPORT(void,png_set_compression_buffer_size)
   PNGARG((png_structp png_ptr, png_uint_32 size));
#endif

extern PNG_EXPORT(int,png_reset_zstream) PNGARG((png_structp png_ptr));

#ifdef PNG_USER_MEM_SUPPORTED
extern PNG_EXPORT(png_structp,png_create_read_struct_2)
   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
   png_malloc_ptr malloc_fn, png_free_ptr free_fn));
extern PNG_EXPORT(png_structp,png_create_write_struct_2)
   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
   png_malloc_ptr malloc_fn, png_free_ptr free_fn));
#endif

extern PNG_EXPORT(void,png_write_chunk) PNGARG((png_structp png_ptr,
   png_bytep chunk_name, png_bytep data, png_size_t length));

extern PNG_EXPORT(void,png_write_chunk_start) PNGARG((png_structp png_ptr,
   png_bytep chunk_name, png_uint_32 length));

extern PNG_EXPORT(void,png_write_chunk_data) PNGARG((png_structp png_ptr,
   png_bytep data, png_size_t length));

extern PNG_EXPORT(void,png_write_chunk_end) PNGARG((png_structp png_ptr));

extern PNG_EXPORT(png_infop,png_create_info_struct)
   PNGARG((png_structp png_ptr));

#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
extern PNG_EXPORT(void,png_info_init) PNGARG((png_infop info_ptr));
#undef png_info_init
#define png_info_init(info_ptr) png_info_init_3(&info_ptr,\
	png_sizeof(png_info));
#endif

extern PNG_EXPORT(void,png_info_init_3) PNGARG((png_infopp info_ptr,
	png_size_t png_info_struct_size));

extern PNG_EXPORT(void,png_write_info_before_PLTE) PNGARG((png_structp png_ptr,
   png_infop info_ptr));
extern PNG_EXPORT(void,png_write_info) PNGARG((png_structp png_ptr,
   png_infop info_ptr));

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
extern PNG_EXPORT(void,png_read_info) PNGARG((png_structp png_ptr,
   png_infop info_ptr));
#endif

#if defined(PNG_TIME_RFC1123_SUPPORTED)
extern PNG_EXPORT(png_charp,png_convert_to_rfc1123)
   PNGARG((png_structp png_ptr, png_timep ptime));
#endif

#if !defined(_WIN32_WCE)
#if defined(PNG_WRITE_tIME_SUPPORTED)
extern PNG_EXPORT(void,png_convert_from_struct_tm) PNGARG((png_timep ptime,
   struct tm FAR * ttime));

extern PNG_EXPORT(void,png_convert_from_time_t) PNGARG((png_timep ptime,
   time_t ttime));
#endif /* PNG_WRITE_tIME_SUPPORTED */
#endif /* _WIN32_WCE */

#if defined(PNG_READ_EXPAND_SUPPORTED)
extern PNG_EXPORT(void,png_set_expand) PNGARG((png_structp png_ptr));
#if !defined(PNG_1_0_X)
extern PNG_EXPORT(void,png_set_expand_gray_1_2_4_to_8) PNGARG((png_structp
  png_ptr));
#endif
extern PNG_EXPORT(void,png_set_palette_to_rgb) PNGARG((png_structp png_ptr));
extern PNG_EXPORT(void,png_set_tRNS_to_alpha) PNGARG((png_structp png_ptr));
#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
extern PNG_EXPORT(void,png_set_gray_1_2_4_to_8) PNGARG((png_structp png_ptr));
#endif
#endif

#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
extern PNG_EXPORT(void,png_set_bgr) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
extern PNG_EXPORT(void,png_set_gray_to_rgb) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr,
   int error_action, double red, double green ));
#endif
extern PNG_EXPORT(void,png_set_rgb_to_gray_fixed) PNGARG((png_structp png_ptr,
   int error_action, png_fixed_point red, png_fixed_point green ));
extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp
   png_ptr));
#endif

extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth,
   png_colorp palette));

#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
extern PNG_EXPORT(void,png_set_strip_alpha) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
	defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
extern PNG_EXPORT(void,png_set_swap_alpha) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
	defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
extern PNG_EXPORT(void,png_set_invert_alpha) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
extern PNG_EXPORT(void,png_set_filler) PNGARG((png_structp png_ptr,
   png_uint_32 filler, int flags));
#define PNG_FILLER_BEFORE 0
#define PNG_FILLER_AFTER 1
#if !defined(PNG_1_0_X)
extern PNG_EXPORT(void,png_set_add_alpha) PNGARG((png_structp png_ptr,
   png_uint_32 filler, int flags));
#endif
#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */

#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
extern PNG_EXPORT(void,png_set_swap) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
extern PNG_EXPORT(void,png_set_packing) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED)
extern PNG_EXPORT(void,png_set_packswap) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
extern PNG_EXPORT(void,png_set_shift) PNGARG((png_structp png_ptr,
   png_color_8p true_bits));
#endif

#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
	defined(PNG_WRITE_INTERLACING_SUPPORTED)
extern PNG_EXPORT(int,png_set_interlace_handling) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
extern PNG_EXPORT(void,png_set_invert_mono) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_BACKGROUND_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_background) PNGARG((png_structp png_ptr,
   png_color_16p background_color, int background_gamma_code,
   int need_expand, double background_gamma));
#endif
#define PNG_BACKGROUND_GAMMA_UNKNOWN 0
#define PNG_BACKGROUND_GAMMA_SCREEN  1
#define PNG_BACKGROUND_GAMMA_FILE	2
#define PNG_BACKGROUND_GAMMA_UNIQUE  3
#endif

#if defined(PNG_READ_16_TO_8_SUPPORTED)
extern PNG_EXPORT(void,png_set_strip_16) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
extern PNG_EXPORT(void,png_set_dither) PNGARG((png_structp png_ptr,
   png_colorp palette, int num_palette, int maximum_colors,
   png_uint_16p histogram, int full_dither));
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_gamma) PNGARG((png_structp png_ptr,
   double screen_gamma, double default_file_gamma));
#endif
#endif

#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
	defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
extern PNG_EXPORT(void,png_permit_empty_plte) PNGARG((png_structp png_ptr,
   int empty_plte_permitted));
#endif
#endif

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
extern PNG_EXPORT(void,png_set_flush) PNGARG((png_structp png_ptr, int nrows));
extern PNG_EXPORT(void,png_write_flush) PNGARG((png_structp png_ptr));
#endif

extern PNG_EXPORT(void,png_start_read_image) PNGARG((png_structp png_ptr));

extern PNG_EXPORT(void,png_read_update_info) PNGARG((png_structp png_ptr,
   png_infop info_ptr));

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
extern PNG_EXPORT(void,png_read_rows) PNGARG((png_structp png_ptr,
   png_bytepp row, png_bytepp display_row, png_uint_32 num_rows));
#endif

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
extern PNG_EXPORT(void,png_read_row) PNGARG((png_structp png_ptr,
   png_bytep row,
   png_bytep display_row));
#endif

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
extern PNG_EXPORT(void,png_read_image) PNGARG((png_structp png_ptr,
   png_bytepp image));
#endif

extern PNG_EXPORT(void,png_write_row) PNGARG((png_structp png_ptr,
   png_bytep row));

extern PNG_EXPORT(void,png_write_rows) PNGARG((png_structp png_ptr,
   png_bytepp row, png_uint_32 num_rows));

extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr,
   png_bytepp image));

extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr,
   png_infop info_ptr));

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
extern PNG_EXPORT(void,png_read_end) PNGARG((png_structp png_ptr,
   png_infop info_ptr));
#endif

extern PNG_EXPORT(void,png_destroy_info_struct) PNGARG((png_structp png_ptr,
   png_infopp info_ptr_ptr));

extern PNG_EXPORT(void,png_destroy_read_struct) PNGARG((png_structpp
   png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr));

extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_infop end_info_ptr));

extern PNG_EXPORT(void,png_destroy_write_struct)
   PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr));

extern void png_write_destroy PNGARG((png_structp png_ptr));

extern PNG_EXPORT(void,png_set_crc_action) PNGARG((png_structp png_ptr,
   int crit_action, int ancil_action));

#define PNG_CRC_DEFAULT	   0  /* error/quit	  warn/discard data */
#define PNG_CRC_ERROR_QUIT	1  /* error/quit	  error/quit	*/
#define PNG_CRC_WARN_DISCARD  2  /* (INVALID)	   warn/discard data */
#define PNG_CRC_WARN_USE	  3  /* warn/use data	   warn/use data	 */
#define PNG_CRC_QUIET_USE	 4  /* quiet/use data	  quiet/use data	*/
#define PNG_CRC_NO_CHANGE	 5  /* use current value   use current value */

extern PNG_EXPORT(void,png_set_filter) PNGARG((png_structp png_ptr, int method,
   int filters));

#define PNG_NO_FILTERS	 0x00
#define PNG_FILTER_NONE	0x08
#define PNG_FILTER_SUB	 0x10
#define PNG_FILTER_UP	  0x20
#define PNG_FILTER_AVG	 0x40
#define PNG_FILTER_PAETH   0x80
#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \
						 PNG_FILTER_AVG | PNG_FILTER_PAETH)

#define PNG_FILTER_VALUE_NONE  0
#define PNG_FILTER_VALUE_SUB   1
#define PNG_FILTER_VALUE_UP	2
#define PNG_FILTER_VALUE_AVG   3
#define PNG_FILTER_VALUE_PAETH 4
#define PNG_FILTER_VALUE_LAST  5

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* EXPERIMENTAL */
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_filter_heuristics) PNGARG((png_structp png_ptr,
   int heuristic_method, int num_weights, png_doublep filter_weights,
   png_doublep filter_costs));
#endif
#endif /*  PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */

#define PNG_FILTER_HEURISTIC_DEFAULT    0  /* Currently "UNWEIGHTED" */
#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1  /* Used by libpng < 0.95 */
#define PNG_FILTER_HEURISTIC_WEIGHTED   2  /* Experimental feature */
#define PNG_FILTER_HEURISTIC_LAST	   3  /* Not a valid value */

extern PNG_EXPORT(void,png_set_compression_level) PNGARG((png_structp png_ptr,
   int level));

extern PNG_EXPORT(void,png_set_compression_mem_level)
   PNGARG((png_structp png_ptr, int mem_level));

extern PNG_EXPORT(void,png_set_compression_strategy)
   PNGARG((png_structp png_ptr, int strategy));

extern PNG_EXPORT(void,png_set_compression_window_bits)
   PNGARG((png_structp png_ptr, int window_bits));

extern PNG_EXPORT(void,png_set_compression_method) PNGARG((png_structp png_ptr,
   int method));

#if !defined(PNG_NO_STDIO)
extern PNG_EXPORT(void,png_init_io) PNGARG((png_structp png_ptr, png_FILE_p fp));
#endif

extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr,
   png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn));

extern PNG_EXPORT(png_voidp,png_get_error_ptr) PNGARG((png_structp png_ptr));

extern PNG_EXPORT(void,png_set_write_fn) PNGARG((png_structp png_ptr,
   png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));

extern PNG_EXPORT(void,png_set_read_fn) PNGARG((png_structp png_ptr,
   png_voidp io_ptr, png_rw_ptr read_data_fn));

extern PNG_EXPORT(png_voidp,png_get_io_ptr) PNGARG((png_structp png_ptr));

extern PNG_EXPORT(void,png_set_read_status_fn) PNGARG((png_structp png_ptr,
   png_read_status_ptr read_row_fn));

extern PNG_EXPORT(void,png_set_write_status_fn) PNGARG((png_structp png_ptr,
   png_write_status_ptr write_row_fn));

#ifdef PNG_USER_MEM_SUPPORTED
extern PNG_EXPORT(void,png_set_mem_fn) PNGARG((png_structp png_ptr,
   png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn));
extern PNG_EXPORT(png_voidp,png_get_mem_ptr) PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_LEGACY_SUPPORTED)
extern PNG_EXPORT(void,png_set_read_user_transform_fn) PNGARG((png_structp
   png_ptr, png_user_transform_ptr read_user_transform_fn));
#endif

#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_LEGACY_SUPPORTED)
extern PNG_EXPORT(void,png_set_write_user_transform_fn) PNGARG((png_structp
   png_ptr, png_user_transform_ptr write_user_transform_fn));
#endif

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_LEGACY_SUPPORTED)
extern PNG_EXPORT(void,png_set_user_transform_info) PNGARG((png_structp
   png_ptr, png_voidp user_transform_ptr, int user_transform_depth,
   int user_transform_channels));
extern PNG_EXPORT(png_voidp,png_get_user_transform_ptr)
   PNGARG((png_structp png_ptr));
#endif

#ifdef PNG_USER_CHUNKS_SUPPORTED
extern PNG_EXPORT(void,png_set_read_user_chunk_fn) PNGARG((png_structp png_ptr,
   png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn));
extern PNG_EXPORT(png_voidp,png_get_user_chunk_ptr) PNGARG((png_structp
   png_ptr));
#endif

#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
extern PNG_EXPORT(void,png_set_progressive_read_fn) PNGARG((png_structp png_ptr,
   png_voidp progressive_ptr,
   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
   png_progressive_end_ptr end_fn));

extern PNG_EXPORT(png_voidp,png_get_progressive_ptr)
   PNGARG((png_structp png_ptr));

extern PNG_EXPORT(void,png_process_data) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_bytep buffer, png_size_t buffer_size));

extern PNG_EXPORT(void,png_progressive_combine_row) PNGARG((png_structp png_ptr,
   png_bytep old_row, png_bytep new_row));
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */

extern PNG_EXPORT(png_voidp,png_malloc) PNGARG((png_structp png_ptr,
   png_uint_32 size));

#if defined(PNG_1_0_X)
#  define png_malloc_warn png_malloc
#else
extern PNG_EXPORT(png_voidp,png_malloc_warn) PNGARG((png_structp png_ptr,
   png_uint_32 size));
#endif

extern PNG_EXPORT(void,png_free) PNGARG((png_structp png_ptr, png_voidp ptr));

#if defined(PNG_1_0_X)
extern PNG_EXPORT(voidpf,png_zalloc) PNGARG((voidpf png_ptr, uInt items,
   uInt size));

extern PNG_EXPORT(void,png_zfree) PNGARG((voidpf png_ptr, voidpf ptr));
#endif

extern PNG_EXPORT(void,png_free_data) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 free_me, int num));
#ifdef PNG_FREE_ME_SUPPORTED
extern PNG_EXPORT(void,png_data_freer) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int freer, png_uint_32 mask));
#endif
#define PNG_DESTROY_WILL_FREE_DATA 1
#define PNG_SET_WILL_FREE_DATA 1
#define PNG_USER_WILL_FREE_DATA 2
#define PNG_FREE_HIST 0x0008
#define PNG_FREE_ICCP 0x0010
#define PNG_FREE_SPLT 0x0020
#define PNG_FREE_ROWS 0x0040
#define PNG_FREE_PCAL 0x0080
#define PNG_FREE_SCAL 0x0100
#define PNG_FREE_UNKN 0x0200
#define PNG_FREE_LIST 0x0400
#define PNG_FREE_PLTE 0x1000
#define PNG_FREE_TRNS 0x2000
#define PNG_FREE_TEXT 0x4000
#define PNG_FREE_ALL  0x7fff
#define PNG_FREE_MUL  0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */

#ifdef PNG_USER_MEM_SUPPORTED
extern PNG_EXPORT(png_voidp,png_malloc_default) PNGARG((png_structp png_ptr,
   png_uint_32 size));
extern PNG_EXPORT(void,png_free_default) PNGARG((png_structp png_ptr,
   png_voidp ptr));
#endif

extern PNG_EXPORT(png_voidp,png_memcpy_check) PNGARG((png_structp png_ptr,
   png_voidp s1, png_voidp s2, png_uint_32 size));

extern PNG_EXPORT(png_voidp,png_memset_check) PNGARG((png_structp png_ptr,
   png_voidp s1, int value, png_uint_32 size));

#if defined(USE_FAR_KEYWORD)  /* memory model conversion function */
extern void *png_far_to_near PNGARG((png_structp png_ptr,png_voidp ptr,
   int check));
#endif /* USE_FAR_KEYWORD */

#ifndef PNG_NO_ERROR_TEXT
extern PNG_EXPORT(void,png_error) PNGARG((png_structp png_ptr,
   png_const_charp error_message));

extern PNG_EXPORT(void,png_chunk_error) PNGARG((png_structp png_ptr,
   png_const_charp error_message));
#else
extern PNG_EXPORT(void,png_err) PNGARG((png_structp png_ptr));
#endif

#ifndef PNG_NO_WARNINGS
extern PNG_EXPORT(void,png_warning) PNGARG((png_structp png_ptr,
   png_const_charp warning_message));

#ifdef PNG_READ_SUPPORTED
extern PNG_EXPORT(void,png_chunk_warning) PNGARG((png_structp png_ptr,
   png_const_charp warning_message));
#endif /* PNG_READ_SUPPORTED */
#endif /* PNG_NO_WARNINGS */

extern PNG_EXPORT(png_uint_32,png_get_valid) PNGARG((png_structp png_ptr,
png_infop info_ptr, png_uint_32 flag));

extern PNG_EXPORT(png_uint_32,png_get_rowbytes) PNGARG((png_structp png_ptr,
png_infop info_ptr));

#if defined(PNG_INFO_IMAGE_SUPPORTED)
extern PNG_EXPORT(png_bytepp,png_get_rows) PNGARG((png_structp png_ptr,
png_infop info_ptr));
extern PNG_EXPORT(void,png_set_rows) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_bytepp row_pointers));
#endif

extern PNG_EXPORT(png_byte,png_get_channels) PNGARG((png_structp png_ptr,
png_infop info_ptr));

#ifdef PNG_EASY_ACCESS_SUPPORTED
extern PNG_EXPORT(png_uint_32, png_get_image_width) PNGARG((png_structp
png_ptr, png_infop info_ptr));

extern PNG_EXPORT(png_uint_32, png_get_image_height) PNGARG((png_structp
png_ptr, png_infop info_ptr));

extern PNG_EXPORT(png_byte, png_get_bit_depth) PNGARG((png_structp
png_ptr, png_infop info_ptr));

extern PNG_EXPORT(png_byte, png_get_color_type) PNGARG((png_structp
png_ptr, png_infop info_ptr));

extern PNG_EXPORT(png_byte, png_get_filter_type) PNGARG((png_structp
png_ptr, png_infop info_ptr));

extern PNG_EXPORT(png_byte, png_get_interlace_type) PNGARG((png_structp
png_ptr, png_infop info_ptr));

extern PNG_EXPORT(png_byte, png_get_compression_type) PNGARG((png_structp
png_ptr, png_infop info_ptr));

extern PNG_EXPORT(png_uint_32, png_get_pixels_per_meter) PNGARG((png_structp
png_ptr, png_infop info_ptr));
extern PNG_EXPORT(png_uint_32, png_get_x_pixels_per_meter) PNGARG((png_structp
png_ptr, png_infop info_ptr));
extern PNG_EXPORT(png_uint_32, png_get_y_pixels_per_meter) PNGARG((png_structp
png_ptr, png_infop info_ptr));

#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(float, png_get_pixel_aspect_ratio) PNGARG((png_structp
png_ptr, png_infop info_ptr));
#endif

extern PNG_EXPORT(png_int_32, png_get_x_offset_pixels) PNGARG((png_structp
png_ptr, png_infop info_ptr));
extern PNG_EXPORT(png_int_32, png_get_y_offset_pixels) PNGARG((png_structp
png_ptr, png_infop info_ptr));
extern PNG_EXPORT(png_int_32, png_get_x_offset_microns) PNGARG((png_structp
png_ptr, png_infop info_ptr));
extern PNG_EXPORT(png_int_32, png_get_y_offset_microns) PNGARG((png_structp
png_ptr, png_infop info_ptr));

#endif /* PNG_EASY_ACCESS_SUPPORTED */

extern PNG_EXPORT(png_bytep,png_get_signature) PNGARG((png_structp png_ptr,
png_infop info_ptr));

#if defined(PNG_bKGD_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_bKGD) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_color_16p *background));
#endif

#if defined(PNG_bKGD_SUPPORTED)
extern PNG_EXPORT(void,png_set_bKGD) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_color_16p background));
#endif

#if defined(PNG_cHRM_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(png_uint_32,png_get_cHRM) PNGARG((png_structp png_ptr,
   png_infop info_ptr, double *white_x, double *white_y, double *red_x,
   double *red_y, double *green_x, double *green_y, double *blue_x,
   double *blue_y));
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
extern PNG_EXPORT(png_uint_32,png_get_cHRM_fixed) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_fixed_point *int_white_x, png_fixed_point
   *int_white_y, png_fixed_point *int_red_x, png_fixed_point *int_red_y,
   png_fixed_point *int_green_x, png_fixed_point *int_green_y, png_fixed_point
   *int_blue_x, png_fixed_point *int_blue_y));
#endif
#endif

#if defined(PNG_cHRM_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_cHRM) PNGARG((png_structp png_ptr,
   png_infop info_ptr, double white_x, double white_y, double red_x,
   double red_y, double green_x, double green_y, double blue_x, double blue_y));
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_cHRM_fixed) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y,
   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
   png_fixed_point int_blue_y));
#endif
#endif

#if defined(PNG_gAMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(png_uint_32,png_get_gAMA) PNGARG((png_structp png_ptr,
   png_infop info_ptr, double *file_gamma));
#endif
extern PNG_EXPORT(png_uint_32,png_get_gAMA_fixed) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_fixed_point *int_file_gamma));
#endif

#if defined(PNG_gAMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_gAMA) PNGARG((png_structp png_ptr,
   png_infop info_ptr, double file_gamma));
#endif
extern PNG_EXPORT(void,png_set_gAMA_fixed) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_fixed_point int_file_gamma));
#endif

#if defined(PNG_hIST_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_hIST) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_16p *hist));
#endif

#if defined(PNG_hIST_SUPPORTED)
extern PNG_EXPORT(void,png_set_hIST) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_16p hist));
#endif

extern PNG_EXPORT(png_uint_32,png_get_IHDR) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 *width, png_uint_32 *height,
   int *bit_depth, int *color_type, int *interlace_method,
   int *compression_method, int *filter_method));

extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,
   int color_type, int interlace_method, int compression_method,
   int filter_method));

#if defined(PNG_oFFs_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_oFFs) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y,
   int *unit_type));
#endif

#if defined(PNG_oFFs_SUPPORTED)
extern PNG_EXPORT(void,png_set_oFFs) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y,
   int unit_type));
#endif

#if defined(PNG_pCAL_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_pCAL) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1,
   int *type, int *nparams, png_charp *units, png_charpp *params));
#endif

#if defined(PNG_pCAL_SUPPORTED)
extern PNG_EXPORT(void,png_set_pCAL) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1,
   int type, int nparams, png_charp units, png_charpp params));
#endif

#if defined(PNG_pHYs_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_pHYs) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
#endif

#if defined(PNG_pHYs_SUPPORTED)
extern PNG_EXPORT(void,png_set_pHYs) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type));
#endif

extern PNG_EXPORT(png_uint_32,png_get_PLTE) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_colorp *palette, int *num_palette));

extern PNG_EXPORT(void,png_set_PLTE) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_colorp palette, int num_palette));

#if defined(PNG_sBIT_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_sBIT) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_color_8p *sig_bit));
#endif

#if defined(PNG_sBIT_SUPPORTED)
extern PNG_EXPORT(void,png_set_sBIT) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_color_8p sig_bit));
#endif

#if defined(PNG_sRGB_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_sRGB) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int *intent));
#endif

#if defined(PNG_sRGB_SUPPORTED)
extern PNG_EXPORT(void,png_set_sRGB) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int intent));
extern PNG_EXPORT(void,png_set_sRGB_gAMA_and_cHRM) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int intent));
#endif

#if defined(PNG_iCCP_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_iCCP) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_charpp name, int *compression_type,
   png_charpp profile, png_uint_32 *proflen));
#endif

#if defined(PNG_iCCP_SUPPORTED)
extern PNG_EXPORT(void,png_set_iCCP) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_charp name, int compression_type,
   png_charp profile, png_uint_32 proflen));
#endif

#if defined(PNG_sPLT_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_sPLT) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_sPLT_tpp entries));
#endif

#if defined(PNG_sPLT_SUPPORTED)
extern PNG_EXPORT(void,png_set_sPLT) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_sPLT_tp entries, int nentries));
#endif

#if defined(PNG_TEXT_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_text) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_textp *text_ptr, int *num_text));
#endif

#if defined(PNG_TEXT_SUPPORTED)
extern PNG_EXPORT(void,png_set_text) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_textp text_ptr, int num_text));
#endif

#if defined(PNG_tIME_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_tIME) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_timep *mod_time));
#endif

#if defined(PNG_tIME_SUPPORTED)
extern PNG_EXPORT(void,png_set_tIME) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_timep mod_time));
#endif

#if defined(PNG_tRNS_SUPPORTED)
extern PNG_EXPORT(png_uint_32,png_get_tRNS) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_bytep *trans, int *num_trans,
   png_color_16p *trans_values));
#endif

#if defined(PNG_tRNS_SUPPORTED)
extern PNG_EXPORT(void,png_set_tRNS) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_bytep trans, int num_trans,
   png_color_16p trans_values));
#endif

#if defined(PNG_tRNS_SUPPORTED)
#endif

#if defined(PNG_sCAL_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(png_uint_32,png_get_sCAL) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int *unit, double *width, double *height));
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
extern PNG_EXPORT(png_uint_32,png_get_sCAL_s) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int *unit, png_charpp swidth, png_charpp sheight));
#endif
#endif
#endif /* PNG_sCAL_SUPPORTED */

#if defined(PNG_sCAL_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_sCAL) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int unit, double width, double height));
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
extern PNG_EXPORT(void,png_set_sCAL_s) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int unit, png_charp swidth, png_charp sheight));
#endif
#endif
#endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
extern PNG_EXPORT(void, png_set_keep_unknown_chunks) PNGARG((png_structp
   png_ptr, int keep, png_bytep chunk_list, int num_chunks));
extern PNG_EXPORT(void, png_set_unknown_chunks) PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns));
extern PNG_EXPORT(void, png_set_unknown_chunk_location)
   PNGARG((png_structp png_ptr, png_infop info_ptr, int chunk, int location));
extern PNG_EXPORT(png_uint_32,png_get_unknown_chunks) PNGARG((png_structp
   png_ptr, png_infop info_ptr, png_unknown_chunkpp entries));
#endif
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
PNG_EXPORT(int,png_handle_as_unknown) PNGARG((png_structp png_ptr, png_bytep
   chunk_name));
#endif

extern PNG_EXPORT(void, png_set_invalid) PNGARG((png_structp png_ptr,
   png_infop info_ptr, int mask));

#if defined(PNG_INFO_IMAGE_SUPPORTED)
extern PNG_EXPORT(void, png_read_png) PNGARG((png_structp png_ptr,
						png_infop info_ptr,
						int transforms,
						png_voidp params));
extern PNG_EXPORT(void, png_write_png) PNGARG((png_structp png_ptr,
						png_infop info_ptr,
						int transforms,
						png_voidp params));
#endif

#ifdef PNG_DEBUG
#if (PNG_DEBUG > 0)
#if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER)
#include <crtdbg.h>
#if (PNG_DEBUG > 1)
#define png_debug(l,m)  _RPT0(_CRT_WARN,m)
#define png_debug1(l,m,p1)  _RPT1(_CRT_WARN,m,p1)
#define png_debug2(l,m,p1,p2) _RPT2(_CRT_WARN,m,p1,p2)
#endif
#else /* PNG_DEBUG_FILE || !_MSC_VER */
#ifndef PNG_DEBUG_FILE
#define PNG_DEBUG_FILE stderr
#endif /* PNG_DEBUG_FILE */
#if (PNG_DEBUG > 1)
#define png_debug(l,m) \
{ \
	 int num_tabs=l; \
	 fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
	   (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":"")))); \
}
#define png_debug1(l,m,p1) \
{ \
	 int num_tabs=l; \
	 fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
	   (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1); \
}
#define png_debug2(l,m,p1,p2) \
{ \
	 int num_tabs=l; \
	 fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
	   (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1,p2); \
}
#endif /* (PNG_DEBUG > 1) */
#endif /* _MSC_VER */
#endif /* (PNG_DEBUG > 0) */
#endif /* PNG_DEBUG */
#ifndef png_debug
#define png_debug(l, m)
#endif
#ifndef png_debug1
#define png_debug1(l, m, p1)
#endif
#ifndef png_debug2
#define png_debug2(l, m, p1, p2)
#endif

extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr));
extern PNG_EXPORT(png_charp,png_get_header_ver) PNGARG((png_structp png_ptr));
extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp png_ptr));
extern PNG_EXPORT(png_charp,png_get_libpng_ver) PNGARG((png_structp png_ptr));

#ifdef PNG_MNG_FEATURES_SUPPORTED
extern PNG_EXPORT(png_uint_32,png_permit_mng_features) PNGARG((png_structp
   png_ptr, png_uint_32 mng_features_permitted));
#endif

#define PNG_HANDLE_CHUNK_AS_DEFAULT   0
#define PNG_HANDLE_CHUNK_NEVER	1
#define PNG_HANDLE_CHUNK_IF_SAFE	  2
#define PNG_HANDLE_CHUNK_ALWAYS	   3

#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
#if defined(PNG_MMX_CODE_SUPPORTED)
#define PNG_ASM_FLAG_MMX_SUPPORT_COMPILED  0x01  /* not user-settable */
#define PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU	0x02  /* not user-settable */
#define PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  0x04
#define PNG_ASM_FLAG_MMX_READ_INTERLACE	0x08
#define PNG_ASM_FLAG_MMX_READ_FILTER_SUB   0x10
#define PNG_ASM_FLAG_MMX_READ_FILTER_UP	0x20
#define PNG_ASM_FLAG_MMX_READ_FILTER_AVG   0x40
#define PNG_ASM_FLAG_MMX_READ_FILTER_PAETH 0x80
#define PNG_ASM_FLAGS_INITIALIZED	  0x80000000  /* not user-settable */

#define PNG_MMX_READ_FLAGS ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  \
						   | PNG_ASM_FLAG_MMX_READ_INTERLACE	\
						   | PNG_ASM_FLAG_MMX_READ_FILTER_SUB   \
						   | PNG_ASM_FLAG_MMX_READ_FILTER_UP	\
						   | PNG_ASM_FLAG_MMX_READ_FILTER_AVG   \
						   | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH )
#define PNG_MMX_WRITE_FLAGS ( 0 )

#define PNG_MMX_FLAGS ( PNG_ASM_FLAG_MMX_SUPPORT_COMPILED \
					  | PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU   \
					  | PNG_MMX_READ_FLAGS		\
					  | PNG_MMX_WRITE_FLAGS )

#define PNG_SELECT_READ   1
#define PNG_SELECT_WRITE  2
#endif /* PNG_MMX_CODE_SUPPORTED */

#if !defined(PNG_1_0_X)
extern PNG_EXPORT(png_uint_32,png_get_mmx_flagmask)
   PNGARG((int flag_select, int *compilerID));

extern PNG_EXPORT(png_uint_32,png_get_asm_flagmask)
   PNGARG((int flag_select));

extern PNG_EXPORT(png_uint_32,png_get_asm_flags)
   PNGARG((png_structp png_ptr));

extern PNG_EXPORT(png_byte,png_get_mmx_bitdepth_threshold)
   PNGARG((png_structp png_ptr));

extern PNG_EXPORT(png_uint_32,png_get_mmx_rowbytes_threshold)
   PNGARG((png_structp png_ptr));

extern PNG_EXPORT(void,png_set_asm_flags)
   PNGARG((png_structp png_ptr, png_uint_32 asm_flags));

extern PNG_EXPORT(void,png_set_mmx_thresholds)
   PNGARG((png_structp png_ptr, png_byte mmx_bitdepth_threshold,
   png_uint_32 mmx_rowbytes_threshold));

#endif /* PNG_1_0_X */

#if !defined(PNG_1_0_X)
extern PNG_EXPORT(int,png_mmx_support) PNGARG((void));
#endif /* PNG_ASSEMBLER_CODE_SUPPORTED */

#ifdef PNG_ERROR_NUMBERS_SUPPORTED
extern PNG_EXPORT(void,png_set_strip_error_numbers) PNGARG((png_structp
   png_ptr, png_uint_32 strip_mode));
#endif

#endif /* PNG_1_0_X */

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
extern PNG_EXPORT(void,png_set_user_limits) PNGARG((png_structp
   png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max));
extern PNG_EXPORT(png_uint_32,png_get_user_width_max) PNGARG((png_structp
   png_ptr));
extern PNG_EXPORT(png_uint_32,png_get_user_height_max) PNGARG((png_structp
   png_ptr));
#endif

#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED

#  define png_composite(composite, fg, alpha, bg)				\
	 { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) * (png_uint_16)(alpha) \
						+	(png_uint_16)(bg)*(png_uint_16)(255 -	   \
						(png_uint_16)(alpha)) + (png_uint_16)128);	   \
	   (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }

#  define png_composite_16(composite, fg, alpha, bg)			 \
	 { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg) * (png_uint_32)(alpha) \
						+ (png_uint_32)(bg)*(png_uint_32)(65535L -	   \
						(png_uint_32)(alpha)) + (png_uint_32)32768L);	\
	   (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }

#else  /* standard method using integer division */

#  define png_composite(composite, fg, alpha, bg)				\
	 (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) +	\
	   (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) +	   \
	   (png_uint_16)127) / 255)

#  define png_composite_16(composite, fg, alpha, bg)			 \
	 (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \
	   (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) +	  \
	   (png_uint_32)32767) / (png_uint_32)65535L)

#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */

#if defined(PNG_READ_BIG_ENDIAN_SUPPORTED)
#  define png_get_uint_32(buf) ( *((png_uint_32p) (buf)))
#  define png_get_uint_16(buf) ( *((png_uint_16p) (buf)))
#  define png_get_int_32(buf)  ( *((png_int_32p)  (buf)))
#else
extern PNG_EXPORT(png_uint_32,png_get_uint_32) PNGARG((png_bytep buf));
extern PNG_EXPORT(png_uint_16,png_get_uint_16) PNGARG((png_bytep buf));
extern PNG_EXPORT(png_int_32,png_get_int_32) PNGARG((png_bytep buf));
#endif /* !PNG_READ_BIG_ENDIAN_SUPPORTED */
extern PNG_EXPORT(png_uint_32,png_get_uint_31)
  PNGARG((png_structp png_ptr, png_bytep buf));

extern PNG_EXPORT(void,png_save_uint_32)
   PNGARG((png_bytep buf, png_uint_32 i));
extern PNG_EXPORT(void,png_save_int_32)
   PNGARG((png_bytep buf, png_int_32 i));

extern PNG_EXPORT(void,png_save_uint_16)
   PNGARG((png_bytep buf, unsigned int i));

#define PNG_HAVE_IHDR		   0x01
#define PNG_HAVE_PLTE		   0x02
#define PNG_HAVE_IDAT		   0x04
#define PNG_AFTER_IDAT		  0x08 /* Have complete zlib datastream */
#define PNG_HAVE_IEND		   0x10

#if defined(PNG_INTERNAL)

#define PNG_HAVE_gAMA		   0x20
#define PNG_HAVE_cHRM		   0x40
#define PNG_HAVE_sRGB		   0x80
#define PNG_HAVE_CHUNK_HEADER	  0x100
#define PNG_WROTE_tIME		 0x200
#define PNG_WROTE_INFO_BEFORE_PLTE 0x400
#define PNG_BACKGROUND_IS_GRAY	 0x800
#define PNG_HAVE_PNG_SIGNATURE	0x1000
#define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */

#define PNG_BGR		0x0001
#define PNG_INTERLACE	  0x0002
#define PNG_PACK		   0x0004
#define PNG_SHIFT		  0x0008
#define PNG_SWAP_BYTES	 0x0010
#define PNG_INVERT_MONO	0x0020
#define PNG_DITHER		 0x0040
#define PNG_BACKGROUND	 0x0080
#define PNG_BACKGROUND_EXPAND  0x0100
#define PNG_16_TO_8		0x0400
#define PNG_RGBA		   0x0800
#define PNG_EXPAND		 0x1000
#define PNG_GAMMA		  0x2000
#define PNG_GRAY_TO_RGB	0x4000
#define PNG_FILLER		 0x8000L
#define PNG_PACKSWAP	  0x10000L
#define PNG_SWAP_ALPHA	0x20000L
#define PNG_STRIP_ALPHA	   0x40000L
#define PNG_INVERT_ALPHA	  0x80000L
#define PNG_USER_TRANSFORM   0x100000L
#define PNG_RGB_TO_GRAY_ERR  0x200000L
#define PNG_RGB_TO_GRAY_WARN 0x400000L
#define PNG_RGB_TO_GRAY	  0x600000L  /* two bits, RGB_TO_GRAY_ERR|WARN */
#define PNG_ADD_ALPHA	   0x1000000L  /* Added to libpng-1.2.7 */
#define PNG_EXPAND_tRNS	 0x2000000L  /* Added to libpng-1.2.9 */

#define PNG_STRUCT_PNG   0x0001
#define PNG_STRUCT_INFO  0x0002

#define PNG_WEIGHT_SHIFT 8
#define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT))
#define PNG_COST_SHIFT 3
#define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT))

#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY	 0x0001
#define PNG_FLAG_ZLIB_CUSTOM_LEVEL	0x0002
#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL	0x0004
#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS  0x0008
#define PNG_FLAG_ZLIB_CUSTOM_METHOD	   0x0010
#define PNG_FLAG_ZLIB_FINISHED		0x0020
#define PNG_FLAG_ROW_INIT		 0x0040
#define PNG_FLAG_FILLER_AFTER		 0x0080
#define PNG_FLAG_CRC_ANCILLARY_USE	0x0100
#define PNG_FLAG_CRC_ANCILLARY_NOWARN	 0x0200
#define PNG_FLAG_CRC_CRITICAL_USE	 0x0400
#define PNG_FLAG_CRC_CRITICAL_IGNORE	  0x0800
#define PNG_FLAG_FREE_PLTE		0x1000
#define PNG_FLAG_FREE_TRNS		0x2000
#define PNG_FLAG_FREE_HIST		0x4000
#define PNG_FLAG_KEEP_UNKNOWN_CHUNKS	  0x8000L
#define PNG_FLAG_KEEP_UNSAFE_CHUNKS	   0x10000L
#define PNG_FLAG_LIBRARY_MISMATCH	 0x20000L
#define PNG_FLAG_STRIP_ERROR_NUMBERS	  0x40000L
#define PNG_FLAG_STRIP_ERROR_TEXT	 0x80000L
#define PNG_FLAG_MALLOC_NULL_MEM_OK	   0x100000L
#define PNG_FLAG_ADD_ALPHA		0x200000L  /* Added to libpng-1.2.8 */
#define PNG_FLAG_STRIP_ALPHA		  0x400000L  /* Added to libpng-1.2.8 */

#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \
									 PNG_FLAG_CRC_ANCILLARY_NOWARN)

#define PNG_FLAG_CRC_CRITICAL_MASK  (PNG_FLAG_CRC_CRITICAL_USE | \
									 PNG_FLAG_CRC_CRITICAL_IGNORE)

#define PNG_FLAG_CRC_MASK	   (PNG_FLAG_CRC_ANCILLARY_MASK | \
									 PNG_FLAG_CRC_CRITICAL_MASK)

#define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \
   abs((int)((c1).green) - (int)((c2).green)) + \
   abs((int)((c1).blue) - (int)((c2).blue)))

#define PNG_ROWBYTES(pixel_bits, width) \
	((pixel_bits) >= 8 ? \
	((width) * (((png_uint_32)(pixel_bits)) >> 3)) : \
	(( ((width) * ((png_uint_32)(pixel_bits))) + 7) >> 3) )

#define PNG_OUT_OF_RANGE(value, ideal, delta) \
		( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) )

#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
#ifdef PNG_USE_GLOBAL_ARRAYS
   PNG_EXPORT_VAR (PNG_CONST png_byte FARDATA) png_sig[8];
#else
#endif
#endif /* PNG_NO_EXTERN */

#define PNG_IHDR png_byte png_IHDR[5] = { 73,  72,  68,  82, '\0'}
#define PNG_IDAT png_byte png_IDAT[5] = { 73,  68,  65,  84, '\0'}
#define PNG_IEND png_byte png_IEND[5] = { 73,  69,  78,  68, '\0'}
#define PNG_PLTE png_byte png_PLTE[5] = { 80,  76,  84,  69, '\0'}
#define PNG_bKGD png_byte png_bKGD[5] = { 98,  75,  71,  68, '\0'}
#define PNG_cHRM png_byte png_cHRM[5] = { 99,  72,  82,  77, '\0'}
#define PNG_gAMA png_byte png_gAMA[5] = {103,  65,  77,  65, '\0'}
#define PNG_hIST png_byte png_hIST[5] = {104,  73,  83,  84, '\0'}
#define PNG_iCCP png_byte png_iCCP[5] = {105,  67,  67,  80, '\0'}
#define PNG_iTXt png_byte png_iTXt[5] = {105,  84,  88, 116, '\0'}
#define PNG_oFFs png_byte png_oFFs[5] = {111,  70,  70, 115, '\0'}
#define PNG_pCAL png_byte png_pCAL[5] = {112,  67,  65,  76, '\0'}
#define PNG_sCAL png_byte png_sCAL[5] = {115,  67,  65,  76, '\0'}
#define PNG_pHYs png_byte png_pHYs[5] = {112,  72,  89, 115, '\0'}
#define PNG_sBIT png_byte png_sBIT[5] = {115,  66,  73,  84, '\0'}
#define PNG_sPLT png_byte png_sPLT[5] = {115,  80,  76,  84, '\0'}
#define PNG_sRGB png_byte png_sRGB[5] = {115,  82,  71,  66, '\0'}
#define PNG_tEXt png_byte png_tEXt[5] = {116,  69,  88, 116, '\0'}
#define PNG_tIME png_byte png_tIME[5] = {116,  73,  77,  69, '\0'}
#define PNG_tRNS png_byte png_tRNS[5] = {116,  82,  78,  83, '\0'}
#define PNG_zTXt png_byte png_zTXt[5] = {122,  84,  88, 116, '\0'}

#ifdef PNG_USE_GLOBAL_ARRAYS
PNG_EXPORT_VAR (png_byte FARDATA) png_IHDR[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_IDAT[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_IEND[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_PLTE[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_bKGD[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_cHRM[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_gAMA[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_hIST[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_iCCP[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_iTXt[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_oFFs[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_pCAL[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_sCAL[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_pHYs[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_sBIT[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_sPLT[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_sRGB[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_tEXt[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_tIME[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_tRNS[5];
PNG_EXPORT_VAR (png_byte FARDATA) png_zTXt[5];
#endif /* PNG_USE_GLOBAL_ARRAYS */

#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
extern PNG_EXPORT(void,png_read_init) PNGARG((png_structp png_ptr));
#undef png_read_init
#define png_read_init(png_ptr) png_read_init_3(&png_ptr, \
	PNG_LIBPNG_VER_STRING,  png_sizeof(png_struct));
#endif

extern PNG_EXPORT(void,png_read_init_3) PNGARG((png_structpp ptr_ptr,
	png_const_charp user_png_ver, png_size_t png_struct_size));
#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
extern PNG_EXPORT(void,png_read_init_2) PNGARG((png_structp png_ptr,
	png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t
	png_info_size));
#endif

#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
extern PNG_EXPORT(void,png_write_init) PNGARG((png_structp png_ptr));
#undef png_write_init
#define png_write_init(png_ptr) png_write_init_3(&png_ptr, \
	PNG_LIBPNG_VER_STRING, png_sizeof(png_struct));
#endif

extern PNG_EXPORT(void,png_write_init_3) PNGARG((png_structpp ptr_ptr,
	png_const_charp user_png_ver, png_size_t png_struct_size));
extern PNG_EXPORT(void,png_write_init_2) PNGARG((png_structp png_ptr,
	png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t
	png_info_size));

PNG_EXTERN png_voidp png_create_struct PNGARG((int type));

PNG_EXTERN void png_destroy_struct PNGARG((png_voidp struct_ptr));

PNG_EXTERN png_voidp png_create_struct_2 PNGARG((int type, png_malloc_ptr
  malloc_fn, png_voidp mem_ptr));
PNG_EXTERN void png_destroy_struct_2 PNGARG((png_voidp struct_ptr,
   png_free_ptr free_fn, png_voidp mem_ptr));

PNG_EXTERN void png_info_destroy PNGARG((png_structp png_ptr,
   png_infop info_ptr));

#ifndef PNG_1_0_X
PNG_EXTERN voidpf png_zalloc PNGARG((voidpf png_ptr, uInt items, uInt size));

PNG_EXTERN void png_zfree PNGARG((voidpf png_ptr, voidpf ptr));

#ifdef PNG_SIZE_T
   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
#endif

PNG_EXTERN void PNGAPI png_default_read_data PNGARG((png_structp png_ptr,
   png_bytep data, png_size_t length));

#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
PNG_EXTERN void PNGAPI png_push_fill_buffer PNGARG((png_structp png_ptr,
   png_bytep buffer, png_size_t length));
#endif

PNG_EXTERN void PNGAPI png_default_write_data PNGARG((png_structp png_ptr,
   png_bytep data, png_size_t length));

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
#if !defined(PNG_NO_STDIO)
PNG_EXTERN void PNGAPI png_default_flush PNGARG((png_structp png_ptr));
#endif
#endif
#else /* PNG_1_0_X */
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
PNG_EXTERN void png_push_fill_buffer PNGARG((png_structp png_ptr,
   png_bytep buffer, png_size_t length));
#endif
#endif /* PNG_1_0_X */

PNG_EXTERN void png_reset_crc PNGARG((png_structp png_ptr));

PNG_EXTERN void png_write_data PNGARG((png_structp png_ptr, png_bytep data,
   png_size_t length));

PNG_EXTERN void png_read_data PNGARG((png_structp png_ptr, png_bytep data,
   png_size_t length));

PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf,
   png_size_t length));

#if defined(PNG_zTXt_SUPPORTED) || defined(PNG_iTXt_SUPPORTED) || \
	defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
PNG_EXTERN png_charp png_decompress_chunk PNGARG((png_structp png_ptr,
   int comp_type, png_charp chunkdata, png_size_t chunklength,
   png_size_t prefix_length, png_size_t *data_length));
#endif

PNG_EXTERN int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip));

PNG_EXTERN int png_crc_error PNGARG((png_structp png_ptr));

PNG_EXTERN void png_calculate_crc PNGARG((png_structp png_ptr, png_bytep ptr,
   png_size_t length));

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
PNG_EXTERN void png_flush PNGARG((png_structp png_ptr));
#endif

PNG_EXTERN void png_write_sig PNGARG((png_structp png_ptr));

PNG_EXTERN void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width,
   png_uint_32 height,
   int bit_depth, int color_type, int compression_method, int filter_method,
   int interlace_method));

PNG_EXTERN void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette,
   png_uint_32 num_pal));

PNG_EXTERN void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data,
   png_size_t length));

PNG_EXTERN void png_write_IEND PNGARG((png_structp png_ptr));

#if defined(PNG_WRITE_gAMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
PNG_EXTERN void png_write_gAMA PNGARG((png_structp png_ptr, double file_gamma));
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
PNG_EXTERN void png_write_gAMA_fixed PNGARG((png_structp png_ptr, png_fixed_point
	file_gamma));
#endif
#endif

#if defined(PNG_WRITE_sBIT_SUPPORTED)
PNG_EXTERN void png_write_sBIT PNGARG((png_structp png_ptr, png_color_8p sbit,
   int color_type));
#endif

#if defined(PNG_WRITE_cHRM_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
PNG_EXTERN void png_write_cHRM PNGARG((png_structp png_ptr,
   double white_x, double white_y,
   double red_x, double red_y, double green_x, double green_y,
   double blue_x, double blue_y));
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
PNG_EXTERN void png_write_cHRM_fixed PNGARG((png_structp png_ptr,
   png_fixed_point int_white_x, png_fixed_point int_white_y,
   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
   png_fixed_point int_blue_y));
#endif
#endif

#if defined(PNG_WRITE_sRGB_SUPPORTED)
PNG_EXTERN void png_write_sRGB PNGARG((png_structp png_ptr,
   int intent));
#endif

#if defined(PNG_WRITE_iCCP_SUPPORTED)
PNG_EXTERN void png_write_iCCP PNGARG((png_structp png_ptr,
   png_charp name, int compression_type,
   png_charp profile, int proflen));
#endif

#if defined(PNG_WRITE_sPLT_SUPPORTED)
PNG_EXTERN void png_write_sPLT PNGARG((png_structp png_ptr,
   png_sPLT_tp palette));
#endif

#if defined(PNG_WRITE_tRNS_SUPPORTED)
PNG_EXTERN void png_write_tRNS PNGARG((png_structp png_ptr, png_bytep trans,
   png_color_16p values, int number, int color_type));
#endif

#if defined(PNG_WRITE_bKGD_SUPPORTED)
PNG_EXTERN void png_write_bKGD PNGARG((png_structp png_ptr,
   png_color_16p values, int color_type));
#endif

#if defined(PNG_WRITE_hIST_SUPPORTED)
PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist,
   int num_hist));
#endif

#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
	defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr,
   png_charp key, png_charpp new_key));
#endif

#if defined(PNG_WRITE_tEXt_SUPPORTED)
PNG_EXTERN void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key,
   png_charp text, png_size_t text_len));
#endif

#if defined(PNG_WRITE_zTXt_SUPPORTED)
PNG_EXTERN void png_write_zTXt PNGARG((png_structp png_ptr, png_charp key,
   png_charp text, png_size_t text_len, int compression));
#endif

#if defined(PNG_WRITE_iTXt_SUPPORTED)
PNG_EXTERN void png_write_iTXt PNGARG((png_structp png_ptr,
   int compression, png_charp key, png_charp lang, png_charp lang_key,
   png_charp text));
#endif

#if defined(PNG_TEXT_SUPPORTED)  /* Added at version 1.0.14 and 1.2.4 */
PNG_EXTERN int png_set_text_2 PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_textp text_ptr, int num_text));
#endif

#if defined(PNG_WRITE_oFFs_SUPPORTED)
PNG_EXTERN void png_write_oFFs PNGARG((png_structp png_ptr,
   png_int_32 x_offset, png_int_32 y_offset, int unit_type));
#endif

#if defined(PNG_WRITE_pCAL_SUPPORTED)
PNG_EXTERN void png_write_pCAL PNGARG((png_structp png_ptr, png_charp purpose,
   png_int_32 X0, png_int_32 X1, int type, int nparams,
   png_charp units, png_charpp params));
#endif

#if defined(PNG_WRITE_pHYs_SUPPORTED)
PNG_EXTERN void png_write_pHYs PNGARG((png_structp png_ptr,
   png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit,
   int unit_type));
#endif

#if defined(PNG_WRITE_tIME_SUPPORTED)
PNG_EXTERN void png_write_tIME PNGARG((png_structp png_ptr,
   png_timep mod_time));
#endif

#if defined(PNG_WRITE_sCAL_SUPPORTED)
#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
PNG_EXTERN void png_write_sCAL PNGARG((png_structp png_ptr,
   int unit, double width, double height));
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
PNG_EXTERN void png_write_sCAL_s PNGARG((png_structp png_ptr,
   int unit, png_charp width, png_charp height));
#endif
#endif
#endif

PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr));

PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr));

#if defined(PNG_READ_GAMMA_SUPPORTED)
PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr));
#endif

PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row,
   int mask));

#if defined(PNG_READ_INTERLACING_SUPPORTED)
PNG_EXTERN void png_do_read_interlace PNGARG((png_structp png_ptr));
#endif

#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
PNG_EXTERN void png_do_write_interlace PNGARG((png_row_infop row_info,
   png_bytep row, int pass));
#endif

PNG_EXTERN void png_read_filter_row PNGARG((png_structp png_ptr,
   png_row_infop row_info, png_bytep row, png_bytep prev_row, int filter));

PNG_EXTERN void png_write_find_filter PNGARG((png_structp png_ptr,
   png_row_infop row_info));

PNG_EXTERN void png_write_filtered_row PNGARG((png_structp png_ptr,
   png_bytep filtered_row));
PNG_EXTERN void png_read_finish_row PNGARG((png_structp png_ptr));

PNG_EXTERN void png_read_start_row PNGARG((png_structp png_ptr));
PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr,
   png_infop info_ptr));

#if defined(PNG_READ_FILLER_SUPPORTED)
PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info,
   png_bytep row, png_uint_32 filler, png_uint_32 flags));
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
PNG_EXTERN void png_do_read_swap_alpha PNGARG((png_row_infop row_info,
   png_bytep row));
#endif

#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
PNG_EXTERN void png_do_write_swap_alpha PNGARG((png_row_infop row_info,
   png_bytep row));
#endif

#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
PNG_EXTERN void png_do_read_invert_alpha PNGARG((png_row_infop row_info,
   png_bytep row));
#endif

#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
PNG_EXTERN void png_do_write_invert_alpha PNGARG((png_row_infop row_info,
   png_bytep row));
#endif

#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
	defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
PNG_EXTERN void png_do_strip_filler PNGARG((png_row_infop row_info,
   png_bytep row, png_uint_32 flags));
#endif

#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
PNG_EXTERN void png_do_swap PNGARG((png_row_infop row_info, png_bytep row));
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED)
PNG_EXTERN void png_do_packswap PNGARG((png_row_infop row_info, png_bytep row));
#endif

#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, png_row_infop
   row_info, png_bytep row));
#endif

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
PNG_EXTERN void png_do_gray_to_rgb PNGARG((png_row_infop row_info,
   png_bytep row));
#endif

#if defined(PNG_READ_PACK_SUPPORTED)
PNG_EXTERN void png_do_unpack PNGARG((png_row_infop row_info, png_bytep row));
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED)
PNG_EXTERN void png_do_unshift PNGARG((png_row_infop row_info, png_bytep row,
   png_color_8p sig_bits));
#endif

#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
PNG_EXTERN void png_do_invert PNGARG((png_row_infop row_info, png_bytep row));
#endif

#if defined(PNG_READ_16_TO_8_SUPPORTED)
PNG_EXTERN void png_do_chop PNGARG((png_row_infop row_info, png_bytep row));
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
PNG_EXTERN void png_do_dither PNGARG((png_row_infop row_info,
   png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup));

#  if defined(PNG_CORRECT_PALETTE_SUPPORTED)
PNG_EXTERN void png_correct_palette PNGARG((png_structp png_ptr,
   png_colorp palette, int num_palette));
#  endif
#endif

#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
PNG_EXTERN void png_do_bgr PNGARG((png_row_infop row_info, png_bytep row));
#endif

#if defined(PNG_WRITE_PACK_SUPPORTED)
PNG_EXTERN void png_do_pack PNGARG((png_row_infop row_info,
   png_bytep row, png_uint_32 bit_depth));
#endif

#if defined(PNG_WRITE_SHIFT_SUPPORTED)
PNG_EXTERN void png_do_shift PNGARG((png_row_infop row_info, png_bytep row,
   png_color_8p bit_depth));
#endif

#if defined(PNG_READ_BACKGROUND_SUPPORTED)
#if defined(PNG_READ_GAMMA_SUPPORTED)
PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
   png_color_16p trans_values, png_color_16p background,
   png_color_16p background_1,
   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
   png_uint_16pp gamma_16_to_1, int gamma_shift));
#else
PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
   png_color_16p trans_values, png_color_16p background));
#endif
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED)
PNG_EXTERN void png_do_gamma PNGARG((png_row_infop row_info, png_bytep row,
   png_bytep gamma_table, png_uint_16pp gamma_16_table,
   int gamma_shift));
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
PNG_EXTERN void png_do_expand_palette PNGARG((png_row_infop row_info,
   png_bytep row, png_colorp palette, png_bytep trans, int num_trans));
PNG_EXTERN void png_do_expand PNGARG((png_row_infop row_info,
   png_bytep row, png_color_16p trans_value));
#endif

PNG_EXTERN void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
PNG_EXTERN void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
PNG_EXTERN void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));

#if defined(PNG_READ_bKGD_SUPPORTED)
PNG_EXTERN void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_cHRM_SUPPORTED)
PNG_EXTERN void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_gAMA_SUPPORTED)
PNG_EXTERN void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_hIST_SUPPORTED)
PNG_EXTERN void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_iCCP_SUPPORTED)
extern void png_handle_iCCP PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif /* PNG_READ_iCCP_SUPPORTED */

#if defined(PNG_READ_iTXt_SUPPORTED)
PNG_EXTERN void png_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_oFFs_SUPPORTED)
PNG_EXTERN void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_pCAL_SUPPORTED)
PNG_EXTERN void png_handle_pCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_pHYs_SUPPORTED)
PNG_EXTERN void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_sBIT_SUPPORTED)
PNG_EXTERN void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_sCAL_SUPPORTED)
PNG_EXTERN void png_handle_sCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_sPLT_SUPPORTED)
extern void png_handle_sPLT PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif /* PNG_READ_sPLT_SUPPORTED */

#if defined(PNG_READ_sRGB_SUPPORTED)
PNG_EXTERN void png_handle_sRGB PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_tEXt_SUPPORTED)
PNG_EXTERN void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_tIME_SUPPORTED)
PNG_EXTERN void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_tRNS_SUPPORTED)
PNG_EXTERN void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

#if defined(PNG_READ_zTXt_SUPPORTED)
PNG_EXTERN void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
   png_uint_32 length));
#endif

PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 length));

PNG_EXTERN void png_check_chunk_name PNGARG((png_structp png_ptr,
   png_bytep chunk_name));

PNG_EXTERN void png_do_read_transformations PNGARG((png_structp png_ptr));
PNG_EXTERN void png_do_write_transformations PNGARG((png_structp png_ptr));

PNG_EXTERN void png_init_read_transformations PNGARG((png_structp png_ptr));

#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
PNG_EXTERN void png_push_read_chunk PNGARG((png_structp png_ptr,
   png_infop info_ptr));
PNG_EXTERN void png_push_read_sig PNGARG((png_structp png_ptr,
   png_infop info_ptr));
PNG_EXTERN void png_push_check_crc PNGARG((png_structp png_ptr));
PNG_EXTERN void png_push_crc_skip PNGARG((png_structp png_ptr,
   png_uint_32 length));
PNG_EXTERN void png_push_crc_finish PNGARG((png_structp png_ptr));
PNG_EXTERN void png_push_save_buffer PNGARG((png_structp png_ptr));
PNG_EXTERN void png_push_restore_buffer PNGARG((png_structp png_ptr,
   png_bytep buffer, png_size_t buffer_length));
PNG_EXTERN void png_push_read_IDAT PNGARG((png_structp png_ptr));
PNG_EXTERN void png_process_IDAT_data PNGARG((png_structp png_ptr,
   png_bytep buffer, png_size_t buffer_length));
PNG_EXTERN void png_push_process_row PNGARG((png_structp png_ptr));
PNG_EXTERN void png_push_handle_unknown PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 length));
PNG_EXTERN void png_push_have_info PNGARG((png_structp png_ptr,
   png_infop info_ptr));
PNG_EXTERN void png_push_have_end PNGARG((png_structp png_ptr,
   png_infop info_ptr));
PNG_EXTERN void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row));
PNG_EXTERN void png_push_read_end PNGARG((png_structp png_ptr,
   png_infop info_ptr));
PNG_EXTERN void png_process_some_data PNGARG((png_structp png_ptr,
   png_infop info_ptr));
PNG_EXTERN void png_read_push_finish_row PNGARG((png_structp png_ptr));
#if defined(PNG_READ_tEXt_SUPPORTED)
PNG_EXTERN void png_push_handle_tEXt PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 length));
PNG_EXTERN void png_push_read_tEXt PNGARG((png_structp png_ptr,
   png_infop info_ptr));
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
PNG_EXTERN void png_push_handle_zTXt PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 length));
PNG_EXTERN void png_push_read_zTXt PNGARG((png_structp png_ptr,
   png_infop info_ptr));
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
PNG_EXTERN void png_push_handle_iTXt PNGARG((png_structp png_ptr,
   png_infop info_ptr, png_uint_32 length));
PNG_EXTERN void png_push_read_iTXt PNGARG((png_structp png_ptr,
   png_infop info_ptr));
#endif

#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */

#ifdef PNG_MNG_FEATURES_SUPPORTED
PNG_EXTERN void png_do_read_intrapixel PNGARG((png_row_infop row_info,
   png_bytep row));
PNG_EXTERN void png_do_write_intrapixel PNGARG((png_row_infop row_info,
   png_bytep row));
#endif

#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
#if defined(PNG_MMX_CODE_SUPPORTED)
 /* PRIVATE */
PNG_EXTERN void png_init_mmx_flags PNGARG((png_structp png_ptr));
#endif
#endif

#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
PNG_EXTERN png_uint_32 png_get_pixels_per_inch PNGARG((png_structp png_ptr,
png_infop info_ptr));

PNG_EXTERN png_uint_32 png_get_x_pixels_per_inch PNGARG((png_structp png_ptr,
png_infop info_ptr));

PNG_EXTERN png_uint_32 png_get_y_pixels_per_inch PNGARG((png_structp png_ptr,
png_infop info_ptr));

PNG_EXTERN float png_get_x_offset_inches PNGARG((png_structp png_ptr,
png_infop info_ptr));

PNG_EXTERN float png_get_y_offset_inches PNGARG((png_structp png_ptr,
png_infop info_ptr));

#if defined(PNG_pHYs_SUPPORTED)
PNG_EXTERN png_uint_32 png_get_pHYs_dpi PNGARG((png_structp png_ptr,
png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
#endif /* PNG_pHYs_SUPPORTED */
#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */

#endif /* PNG_INTERNAL */

#ifdef __cplusplus
}
#endif

#endif /* PNG_VERSION_INFO_ONLY */
#endif /* PNG_H */
/*** End of inlined file: png.h ***/

	#define PNG_NO_EXTERN

/*** Start of inlined file: png.c ***/
#define PNG_INTERNAL
#define PNG_NO_EXTERN

typedef version_1_2_21 Your_png_h_is_not_version_1_2_21;

#ifdef PNG_USE_GLOBAL_ARRAYS
PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;

#ifdef PNG_READ_SUPPORTED

PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
#endif /* PNG_READ_SUPPORTED */

PNG_IHDR;
PNG_IDAT;
PNG_IEND;
PNG_PLTE;
PNG_bKGD;
PNG_cHRM;
PNG_gAMA;
PNG_hIST;
PNG_iCCP;
PNG_iTXt;
PNG_oFFs;
PNG_pCAL;
PNG_sCAL;
PNG_pHYs;
PNG_sBIT;
PNG_sPLT;
PNG_sRGB;
PNG_tEXt;
PNG_tIME;
PNG_tRNS;
PNG_zTXt;

#ifdef PNG_READ_SUPPORTED

PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};

PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};

PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};

PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};

PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};

PNG_CONST int FARDATA png_pass_dsp_mask[]
   = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};

#endif /* PNG_READ_SUPPORTED */
#endif /* PNG_USE_GLOBAL_ARRAYS */

#ifdef PNG_READ_SUPPORTED
void PNGAPI
png_set_sig_bytes(png_structp png_ptr, int num_bytes)
{
   if(png_ptr == NULL) return;
   png_debug(1, "in png_set_sig_bytes\n");
   if (num_bytes > 8)
	  png_error(png_ptr, "Too many bytes for PNG signature.");

   png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
}

int PNGAPI
png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
{
   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
   if (num_to_check > 8)
	  num_to_check = 8;
   else if (num_to_check < 1)
	  return (-1);

   if (start > 7)
	  return (-1);

   if (start + num_to_check > 8)
	  num_to_check = 8 - start;

   return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
}

#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
int PNGAPI
png_check_sig(png_bytep sig, int num)
{
  return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
}
#endif
#endif /* PNG_READ_SUPPORTED */

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
#ifdef PNG_1_0_X
voidpf PNGAPI
#else
voidpf /* private */
#endif
png_zalloc(voidpf png_ptr, uInt items, uInt size)
{
   png_voidp ptr;
   png_structp p=(png_structp)png_ptr;
   png_uint_32 save_flags=p->flags;
   png_uint_32 num_bytes;

   if(png_ptr == NULL) return (NULL);
   if (items > PNG_UINT_32_MAX/size)
   {
	 png_warning (p, "Potential overflow in png_zalloc()");
	 return (NULL);
   }
   num_bytes = (png_uint_32)items * size;

   p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
   ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
   p->flags=save_flags;

#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
   if (ptr == NULL)
	   return ((voidpf)ptr);

   if (num_bytes > (png_uint_32)0x8000L)
   {
	  png_memset(ptr, 0, (png_size_t)0x8000L);
	  png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
		 (png_size_t)(num_bytes - (png_uint_32)0x8000L));
   }
   else
   {
	  png_memset(ptr, 0, (png_size_t)num_bytes);
   }
#endif
   return ((voidpf)ptr);
}

#ifdef PNG_1_0_X
void PNGAPI
#else
void /* private */
#endif
png_zfree(voidpf png_ptr, voidpf ptr)
{
   png_free((png_structp)png_ptr, (png_voidp)ptr);
}

void /* PRIVATE */
png_reset_crc(png_structp png_ptr)
{
   png_ptr->crc = crc32(0, Z_NULL, 0);
}

void /* PRIVATE */
png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
{
   int need_crc = 1;

   if (png_ptr->chunk_name[0] & 0x20)			 /* ancillary */
   {
	  if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
		  (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
		 need_crc = 0;
   }
   else							/* critical */
   {
	  if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
		 need_crc = 0;
   }

   if (need_crc)
	  png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
}

png_infop PNGAPI
png_create_info_struct(png_structp png_ptr)
{
   png_infop info_ptr;

   png_debug(1, "in png_create_info_struct\n");
   if(png_ptr == NULL) return (NULL);
#ifdef PNG_USER_MEM_SUPPORTED
   info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
	  png_ptr->malloc_fn, png_ptr->mem_ptr);
#else
   info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
#endif
   if (info_ptr != NULL)
	  png_info_init_3(&info_ptr, png_sizeof(png_info));

   return (info_ptr);
}

void PNGAPI
png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
{
   png_infop info_ptr = NULL;
   if(png_ptr == NULL) return;

   png_debug(1, "in png_destroy_info_struct\n");
   if (info_ptr_ptr != NULL)
	  info_ptr = *info_ptr_ptr;

   if (info_ptr != NULL)
   {
	  png_info_destroy(png_ptr, info_ptr);

#ifdef PNG_USER_MEM_SUPPORTED
	  png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
		  png_ptr->mem_ptr);
#else
	  png_destroy_struct((png_voidp)info_ptr);
#endif
	  *info_ptr_ptr = NULL;
   }
}

#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
#undef png_info_init
void PNGAPI
png_info_init(png_infop info_ptr)
{
   png_info_init_3(&info_ptr, 0);
}
#endif

void PNGAPI
png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
{
   png_infop info_ptr = *ptr_ptr;

   if(info_ptr == NULL) return;

   png_debug(1, "in png_info_init_3\n");

   if(png_sizeof(png_info) > png_info_struct_size)
	 {
	   png_destroy_struct(info_ptr);
	   info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
	   *ptr_ptr = info_ptr;
	 }

   png_memset(info_ptr, 0, png_sizeof (png_info));
}

#ifdef PNG_FREE_ME_SUPPORTED
void PNGAPI
png_data_freer(png_structp png_ptr, png_infop info_ptr,
   int freer, png_uint_32 mask)
{
   png_debug(1, "in png_data_freer\n");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;
   if(freer == PNG_DESTROY_WILL_FREE_DATA)
	  info_ptr->free_me |= mask;
   else if(freer == PNG_USER_WILL_FREE_DATA)
	  info_ptr->free_me &= ~mask;
   else
	  png_warning(png_ptr,
		 "Unknown freer parameter in png_data_freer.");
}
#endif

void PNGAPI
png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
   int num)
{
   png_debug(1, "in png_free_data\n");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

#if defined(PNG_TEXT_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
#else
if (mask & PNG_FREE_TEXT)
#endif
{
   if (num != -1)
   {
	 if (info_ptr->text && info_ptr->text[num].key)
	 {
		 png_free(png_ptr, info_ptr->text[num].key);
		 info_ptr->text[num].key = NULL;
	 }
   }
   else
   {
	   int i;
	   for (i = 0; i < info_ptr->num_text; i++)
		   png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
	   png_free(png_ptr, info_ptr->text);
	   info_ptr->text = NULL;
	   info_ptr->num_text=0;
   }
}
#endif

#if defined(PNG_tRNS_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
#else
if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
#endif
{
	png_free(png_ptr, info_ptr->trans);
	info_ptr->valid &= ~PNG_INFO_tRNS;
#ifndef PNG_FREE_ME_SUPPORTED
	png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
#endif
	info_ptr->trans = NULL;
}
#endif

#if defined(PNG_sCAL_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
#else
if (mask & PNG_FREE_SCAL)
#endif
{
#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
	png_free(png_ptr, info_ptr->scal_s_width);
	png_free(png_ptr, info_ptr->scal_s_height);
	info_ptr->scal_s_width = NULL;
	info_ptr->scal_s_height = NULL;
#endif
	info_ptr->valid &= ~PNG_INFO_sCAL;
}
#endif

#if defined(PNG_pCAL_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
#else
if (mask & PNG_FREE_PCAL)
#endif
{
	png_free(png_ptr, info_ptr->pcal_purpose);
	png_free(png_ptr, info_ptr->pcal_units);
	info_ptr->pcal_purpose = NULL;
	info_ptr->pcal_units = NULL;
	if (info_ptr->pcal_params != NULL)
	{
		int i;
		for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
		{
		  png_free(png_ptr, info_ptr->pcal_params[i]);
		  info_ptr->pcal_params[i]=NULL;
		}
		png_free(png_ptr, info_ptr->pcal_params);
		info_ptr->pcal_params = NULL;
	}
	info_ptr->valid &= ~PNG_INFO_pCAL;
}
#endif

#if defined(PNG_iCCP_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
#else
if (mask & PNG_FREE_ICCP)
#endif
{
	png_free(png_ptr, info_ptr->iccp_name);
	png_free(png_ptr, info_ptr->iccp_profile);
	info_ptr->iccp_name = NULL;
	info_ptr->iccp_profile = NULL;
	info_ptr->valid &= ~PNG_INFO_iCCP;
}
#endif

#if defined(PNG_sPLT_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
#else
if (mask & PNG_FREE_SPLT)
#endif
{
   if (num != -1)
   {
	  if(info_ptr->splt_palettes)
	  {
		  png_free(png_ptr, info_ptr->splt_palettes[num].name);
		  png_free(png_ptr, info_ptr->splt_palettes[num].entries);
		  info_ptr->splt_palettes[num].name = NULL;
		  info_ptr->splt_palettes[num].entries = NULL;
	  }
   }
   else
   {
	   if(info_ptr->splt_palettes_num)
	   {
		 int i;
		 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
			png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);

		 png_free(png_ptr, info_ptr->splt_palettes);
		 info_ptr->splt_palettes = NULL;
		 info_ptr->splt_palettes_num = 0;
	   }
	   info_ptr->valid &= ~PNG_INFO_sPLT;
   }
}
#endif

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  if(png_ptr->unknown_chunk.data)
  {
	png_free(png_ptr, png_ptr->unknown_chunk.data);
	png_ptr->unknown_chunk.data = NULL;
  }
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
#else
if (mask & PNG_FREE_UNKN)
#endif
{
   if (num != -1)
   {
	   if(info_ptr->unknown_chunks)
	   {
		  png_free(png_ptr, info_ptr->unknown_chunks[num].data);
		  info_ptr->unknown_chunks[num].data = NULL;
	   }
   }
   else
   {
	   int i;

	   if(info_ptr->unknown_chunks_num)
	   {
		 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
			png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);

		 png_free(png_ptr, info_ptr->unknown_chunks);
		 info_ptr->unknown_chunks = NULL;
		 info_ptr->unknown_chunks_num = 0;
	   }
   }
}
#endif

#if defined(PNG_hIST_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)
#else
if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
#endif
{
	png_free(png_ptr, info_ptr->hist);
	info_ptr->hist = NULL;
	info_ptr->valid &= ~PNG_INFO_hIST;
#ifndef PNG_FREE_ME_SUPPORTED
	png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
#endif
}
#endif

#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
#else
if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
#endif
{
	png_zfree(png_ptr, info_ptr->palette);
	info_ptr->palette = NULL;
	info_ptr->valid &= ~PNG_INFO_PLTE;
#ifndef PNG_FREE_ME_SUPPORTED
	png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
#endif
	info_ptr->num_palette = 0;
}

#if defined(PNG_INFO_IMAGE_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
#else
if (mask & PNG_FREE_ROWS)
#endif
{
	if(info_ptr->row_pointers)
	{
	   int row;
	   for (row = 0; row < (int)info_ptr->height; row++)
	   {
		  png_free(png_ptr, info_ptr->row_pointers[row]);
		  info_ptr->row_pointers[row]=NULL;
	   }
	   png_free(png_ptr, info_ptr->row_pointers);
	   info_ptr->row_pointers=NULL;
	}
	info_ptr->valid &= ~PNG_INFO_IDAT;
}
#endif

#ifdef PNG_FREE_ME_SUPPORTED
   if(num == -1)
	 info_ptr->free_me &= ~mask;
   else
	 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
#endif
}

void /* PRIVATE */
png_info_destroy(png_structp png_ptr, png_infop info_ptr)
{
   png_debug(1, "in png_info_destroy\n");

   png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
   if (png_ptr->num_chunk_list)
   {
	   png_free(png_ptr, png_ptr->chunk_list);
	   png_ptr->chunk_list=NULL;
	   png_ptr->num_chunk_list=0;
   }
#endif

   png_info_init_3(&info_ptr, png_sizeof(png_info));
}
#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */

png_voidp PNGAPI
png_get_io_ptr(png_structp png_ptr)
{
   if(png_ptr == NULL) return (NULL);
   return (png_ptr->io_ptr);
}

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
#if !defined(PNG_NO_STDIO)
void PNGAPI
png_init_io(png_structp png_ptr, png_FILE_p fp)
{
   png_debug(1, "in png_init_io\n");
   if(png_ptr == NULL) return;
   png_ptr->io_ptr = (png_voidp)fp;
}
#endif

#if defined(PNG_TIME_RFC1123_SUPPORTED)
png_charp PNGAPI
png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
{
   static PNG_CONST char short_months[12][4] =
		{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

   if(png_ptr == NULL) return (NULL);
   if (png_ptr->time_buffer == NULL)
   {
	  png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
		 png_sizeof(char)));
   }

#if defined(_WIN32_WCE)
   {
	  wchar_t time_buf[29];
	  wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
		  ptime->day % 32, short_months[(ptime->month - 1) % 12],
		ptime->year, ptime->hour % 24, ptime->minute % 60,
		  ptime->second % 61);
	  WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
		  NULL, NULL);
   }
#else
#ifdef USE_FAR_KEYWORD
   {
	  char near_time_buf[29];
	  png_snprintf6(near_time_buf,29,"%d %s %d %02d:%02d:%02d +0000",
		  ptime->day % 32, short_months[(ptime->month - 1) % 12],
		  ptime->year, ptime->hour % 24, ptime->minute % 60,
		  ptime->second % 61);
	  png_memcpy(png_ptr->time_buffer, near_time_buf,
		  29*png_sizeof(char));
   }
#else
   png_snprintf6(png_ptr->time_buffer,29,"%d %s %d %02d:%02d:%02d +0000",
	   ptime->day % 32, short_months[(ptime->month - 1) % 12],
	   ptime->year, ptime->hour % 24, ptime->minute % 60,
	   ptime->second % 61);
#endif
#endif /* _WIN32_WCE */
   return ((png_charp)png_ptr->time_buffer);
}
#endif /* PNG_TIME_RFC1123_SUPPORTED */

#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */

png_charp PNGAPI
png_get_copyright(png_structp png_ptr)
{
   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
   return ((png_charp) "\n libpng version 1.2.21 - October 4, 2007\n\
   Copyright (c) 1998-2007 Glenn Randers-Pehrson\n\
   Copyright (c) 1996-1997 Andreas Dilger\n\
   Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
}

png_charp PNGAPI
png_get_libpng_ver(png_structp png_ptr)
{
   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
   return ((png_charp) PNG_LIBPNG_VER_STRING);
}

png_charp PNGAPI
png_get_header_ver(png_structp png_ptr)
{
   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
   return ((png_charp) PNG_LIBPNG_VER_STRING);
}

png_charp PNGAPI
png_get_header_version(png_structp png_ptr)
{
   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
   return ((png_charp) PNG_HEADER_VERSION_STRING
#ifndef PNG_READ_SUPPORTED
   "     (NO READ SUPPORT)"
#endif
   "\n");
}

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
int PNGAPI
png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
{
   int i;
   png_bytep p;
   if(png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
	  return 0;
   p=png_ptr->chunk_list+png_ptr->num_chunk_list*5-5;
   for (i = png_ptr->num_chunk_list; i; i--, p-=5)
	  if (!png_memcmp(chunk_name, p, 4))
		return ((int)*(p+4));
   return 0;
}
#endif

int PNGAPI
png_reset_zstream(png_structp png_ptr)
{
   if (png_ptr == NULL) return Z_STREAM_ERROR;
   return (inflateReset(&png_ptr->zstream));
}
#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */

png_uint_32 PNGAPI
png_access_version_number(void)
{
   return((png_uint_32) PNG_LIBPNG_VER);
}

#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
#if !defined(PNG_1_0_X)
int PNGAPI
png_mmx_support(void)
{
	return -1;
}
#endif /* PNG_1_0_X */
#endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
#ifdef PNG_SIZE_T
   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
png_size_t PNGAPI
png_convert_size(size_t size)
{
  if (size > (png_size_t)-1)
	 PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */
  return ((png_size_t)size);
}
#endif /* PNG_SIZE_T */
#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
/*** End of inlined file: png.c ***/



/*** Start of inlined file: pngerror.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
static void /* PRIVATE */
png_default_error PNGARG((png_structp png_ptr,
  png_const_charp error_message));
#ifndef PNG_NO_WARNINGS
static void /* PRIVATE */
png_default_warning PNGARG((png_structp png_ptr,
  png_const_charp warning_message));
#endif /* PNG_NO_WARNINGS */

#ifndef PNG_NO_ERROR_TEXT
void PNGAPI
png_error(png_structp png_ptr, png_const_charp error_message)
{
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   char msg[16];
   if (png_ptr != NULL)
   {
	 if (png_ptr->flags&
	   (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
	 {
	   if (*error_message == '#')
	   {
		   int offset;
		   for (offset=1; offset<15; offset++)
			  if (*(error_message+offset) == ' ')
				  break;
		   if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
		   {
			  int i;
			  for (i=0; i<offset-1; i++)
				 msg[i]=error_message[i+1];
			  msg[i]='\0';
			  error_message=msg;
		   }
		   else
			  error_message+=offset;
	   }
	   else
	   {
		   if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
		   {
			  msg[0]='0';
			  msg[1]='\0';
			  error_message=msg;
		   }
	   }
	 }
   }
#endif
   if (png_ptr != NULL && png_ptr->error_fn != NULL)
	  (*(png_ptr->error_fn))(png_ptr, error_message);

   png_default_error(png_ptr, error_message);
}
#else
void PNGAPI
png_err(png_structp png_ptr)
{
   if (png_ptr != NULL && png_ptr->error_fn != NULL)
	  (*(png_ptr->error_fn))(png_ptr, '\0');

   png_default_error(png_ptr, '\0');
}
#endif /* PNG_NO_ERROR_TEXT */

#ifndef PNG_NO_WARNINGS
void PNGAPI
png_warning(png_structp png_ptr, png_const_charp warning_message)
{
   int offset = 0;
   if (png_ptr != NULL)
   {
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   if (png_ptr->flags&
	 (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
#endif
	 {
	   if (*warning_message == '#')
	   {
		   for (offset=1; offset<15; offset++)
			  if (*(warning_message+offset) == ' ')
				  break;
	   }
	 }
	 if (png_ptr != NULL && png_ptr->warning_fn != NULL)
		(*(png_ptr->warning_fn))(png_ptr, warning_message+offset);
   }
   else
	  png_default_warning(png_ptr, warning_message+offset);
}
#endif /* PNG_NO_WARNINGS */

#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))

#if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
static void /* PRIVATE */
png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
   error_message)
{
   int iout = 0, iin = 0;

   while (iin < 4)
   {
	  int c = png_ptr->chunk_name[iin++];
	  if (isnonalpha(c))
	  {
		 buffer[iout++] = '[';
		 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
		 buffer[iout++] = png_digit[c & 0x0f];
		 buffer[iout++] = ']';
	  }
	  else
	  {
		 buffer[iout++] = (png_byte)c;
	  }
   }

   if (error_message == NULL)
	  buffer[iout] = 0;
   else
   {
	  buffer[iout++] = ':';
	  buffer[iout++] = ' ';
	  png_strncpy(buffer+iout, error_message, 63);
	  buffer[iout+63] = 0;
   }
}

#ifdef PNG_READ_SUPPORTED
void PNGAPI
png_chunk_error(png_structp png_ptr, png_const_charp error_message)
{
   char msg[18+64];
   if (png_ptr == NULL)
	 png_error(png_ptr, error_message);
   else
   {
	 png_format_buffer(png_ptr, msg, error_message);
	 png_error(png_ptr, msg);
   }
}
#endif /* PNG_READ_SUPPORTED */
#endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */

#ifndef PNG_NO_WARNINGS
void PNGAPI
png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
{
   char msg[18+64];
   if (png_ptr == NULL)
	 png_warning(png_ptr, warning_message);
   else
   {
	 png_format_buffer(png_ptr, msg, warning_message);
	 png_warning(png_ptr, msg);
   }
}
#endif /* PNG_NO_WARNINGS */

static void /* PRIVATE */
png_default_error(png_structp png_ptr, png_const_charp error_message)
{
#ifndef PNG_NO_CONSOLE_IO
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   if (*error_message == '#')
   {
	 int offset;
	 char error_number[16];
	 for (offset=0; offset<15; offset++)
	 {
		 error_number[offset] = *(error_message+offset+1);
		 if (*(error_message+offset) == ' ')
			 break;
	 }
	 if((offset > 1) && (offset < 15))
	 {
	   error_number[offset-1]='\0';
	   fprintf(stderr, "libpng error no. %s: %s\n", error_number,
		  error_message+offset);
	 }
	 else
	   fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
   }
   else
#endif
   fprintf(stderr, "libpng error: %s\n", error_message);
#endif

#ifdef PNG_SETJMP_SUPPORTED
   if (png_ptr)
   {
#  ifdef USE_FAR_KEYWORD
   {
	  jmp_buf jmpbuf;
	  png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
	  longjmp(jmpbuf, 1);
   }
#  else
   longjmp(png_ptr->jmpbuf, 1);
#  endif
   }
#else
   PNG_ABORT();
#endif
#ifdef PNG_NO_CONSOLE_IO
   error_message = error_message; /* make compiler happy */
#endif
}

#ifndef PNG_NO_WARNINGS
static void /* PRIVATE */
png_default_warning(png_structp png_ptr, png_const_charp warning_message)
{
#ifndef PNG_NO_CONSOLE_IO
#  ifdef PNG_ERROR_NUMBERS_SUPPORTED
   if (*warning_message == '#')
   {
	 int offset;
	 char warning_number[16];
	 for (offset=0; offset<15; offset++)
	 {
		warning_number[offset]=*(warning_message+offset+1);
		if (*(warning_message+offset) == ' ')
			break;
	 }
	 if((offset > 1) && (offset < 15))
	 {
	   warning_number[offset-1]='\0';
	   fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
		  warning_message+offset);
	 }
	 else
	   fprintf(stderr, "libpng warning: %s\n", warning_message);
   }
   else
#  endif
	 fprintf(stderr, "libpng warning: %s\n", warning_message);
#else
   warning_message = warning_message; /* make compiler happy */
#endif
   png_ptr = png_ptr; /* make compiler happy */
}
#endif /* PNG_NO_WARNINGS */

void PNGAPI
png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warning_fn)
{
   if (png_ptr == NULL)
	  return;
   png_ptr->error_ptr = error_ptr;
   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
}

png_voidp PNGAPI
png_get_error_ptr(png_structp png_ptr)
{
   if (png_ptr == NULL)
	  return NULL;
   return ((png_voidp)png_ptr->error_ptr);
}

#ifdef PNG_ERROR_NUMBERS_SUPPORTED
void PNGAPI
png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
{
   if(png_ptr != NULL)
   {
	 png_ptr->flags &=
	   ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
   }
}
#endif
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngerror.c ***/


/*** Start of inlined file: pngget.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)

png_uint_32 PNGAPI
png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
{
   if (png_ptr != NULL && info_ptr != NULL)
	  return(info_ptr->valid & flag);
   else
	  return(0);
}

png_uint_32 PNGAPI
png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
	  return(info_ptr->rowbytes);
   else
	  return(0);
}

#if defined(PNG_INFO_IMAGE_SUPPORTED)
png_bytepp PNGAPI
png_get_rows(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
	  return(info_ptr->row_pointers);
   else
	  return(0);
}
#endif

#ifdef PNG_EASY_ACCESS_SUPPORTED
png_uint_32 PNGAPI
png_get_image_width(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
   {
	  return info_ptr->width;
   }
   return (0);
}

png_uint_32 PNGAPI
png_get_image_height(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
   {
	  return info_ptr->height;
   }
   return (0);
}

png_byte PNGAPI
png_get_bit_depth(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
   {
	  return info_ptr->bit_depth;
   }
   return (0);
}

png_byte PNGAPI
png_get_color_type(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
   {
	  return info_ptr->color_type;
   }
   return (0);
}

png_byte PNGAPI
png_get_filter_type(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
   {
	  return info_ptr->filter_type;
   }
   return (0);
}

png_byte PNGAPI
png_get_interlace_type(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
   {
	  return info_ptr->interlace_type;
   }
   return (0);
}

png_byte PNGAPI
png_get_compression_type(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
   {
	  return info_ptr->compression_type;
   }
   return (0);
}

png_uint_32 PNGAPI
png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
#if defined(PNG_pHYs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_pHYs)
   {
	  png_debug1(1, "in %s retrieval function\n", "png_get_x_pixels_per_meter");
	  if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
		  return (0);
	  else return (info_ptr->x_pixels_per_unit);
   }
#else
   return (0);
#endif
   return (0);
}

png_uint_32 PNGAPI
png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
#if defined(PNG_pHYs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_pHYs)
   {
	  png_debug1(1, "in %s retrieval function\n", "png_get_y_pixels_per_meter");
	  if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
		  return (0);
	  else return (info_ptr->y_pixels_per_unit);
   }
#else
   return (0);
#endif
   return (0);
}

png_uint_32 PNGAPI
png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
#if defined(PNG_pHYs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_pHYs)
   {
	  png_debug1(1, "in %s retrieval function\n", "png_get_pixels_per_meter");
	  if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER ||
		 info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit)
		  return (0);
	  else return (info_ptr->x_pixels_per_unit);
   }
#else
   return (0);
#endif
   return (0);
}

#ifdef PNG_FLOATING_POINT_SUPPORTED
float PNGAPI
png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr)
   {
   if (png_ptr != NULL && info_ptr != NULL)
#if defined(PNG_pHYs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_pHYs)
   {
	  png_debug1(1, "in %s retrieval function\n", "png_get_aspect_ratio");
	  if (info_ptr->x_pixels_per_unit == 0)
		 return ((float)0.0);
	  else
		 return ((float)((float)info_ptr->y_pixels_per_unit
			/(float)info_ptr->x_pixels_per_unit));
   }
#else
   return (0.0);
#endif
   return ((float)0.0);
}
#endif

png_int_32 PNGAPI
png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
#if defined(PNG_oFFs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_oFFs)
   {
	  png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
	  if(info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
		  return (0);
	  else return (info_ptr->x_offset);
   }
#else
   return (0);
#endif
   return (0);
}

png_int_32 PNGAPI
png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
#if defined(PNG_oFFs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_oFFs)
   {
	  png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
	  if(info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
		  return (0);
	  else return (info_ptr->y_offset);
   }
#else
   return (0);
#endif
   return (0);
}

png_int_32 PNGAPI
png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
#if defined(PNG_oFFs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_oFFs)
   {
	  png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
	  if(info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
		  return (0);
	  else return (info_ptr->x_offset);
   }
#else
   return (0);
#endif
   return (0);
}

png_int_32 PNGAPI
png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
#if defined(PNG_oFFs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_oFFs)
   {
	  png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
	  if(info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
		  return (0);
	  else return (info_ptr->y_offset);
   }
#else
   return (0);
#endif
   return (0);
}

#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
png_uint_32 PNGAPI
png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
{
   return ((png_uint_32)((float)png_get_pixels_per_meter(png_ptr, info_ptr)
	 *.0254 +.5));
}

png_uint_32 PNGAPI
png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
{
   return ((png_uint_32)((float)png_get_x_pixels_per_meter(png_ptr, info_ptr)
	 *.0254 +.5));
}

png_uint_32 PNGAPI
png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
{
   return ((png_uint_32)((float)png_get_y_pixels_per_meter(png_ptr, info_ptr)
	 *.0254 +.5));
}

float PNGAPI
png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr)
{
   return ((float)png_get_x_offset_microns(png_ptr, info_ptr)
	 *.00003937);
}

float PNGAPI
png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr)
{
   return ((float)png_get_y_offset_microns(png_ptr, info_ptr)
	 *.00003937);
}

#if defined(PNG_pHYs_SUPPORTED)
png_uint_32 PNGAPI
png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr,
   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
{
   png_uint_32 retval = 0;

   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
   {
	  png_debug1(1, "in %s retrieval function\n", "pHYs");
	  if (res_x != NULL)
	  {
		 *res_x = info_ptr->x_pixels_per_unit;
		 retval |= PNG_INFO_pHYs;
	  }
	  if (res_y != NULL)
	  {
		 *res_y = info_ptr->y_pixels_per_unit;
		 retval |= PNG_INFO_pHYs;
	  }
	  if (unit_type != NULL)
	  {
		 *unit_type = (int)info_ptr->phys_unit_type;
		 retval |= PNG_INFO_pHYs;
		 if(*unit_type == 1)
		 {
			if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50);
			if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50);
		 }
	  }
   }
   return (retval);
}
#endif /* PNG_pHYs_SUPPORTED */
#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */

#endif  /* PNG_EASY_ACCESS_SUPPORTED */

png_byte PNGAPI
png_get_channels(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
	  return(info_ptr->channels);
   else
	  return (0);
}

png_bytep PNGAPI
png_get_signature(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr != NULL && info_ptr != NULL)
	  return(info_ptr->signature);
   else
	  return (NULL);
}

#if defined(PNG_bKGD_SUPPORTED)
png_uint_32 PNGAPI
png_get_bKGD(png_structp png_ptr, png_infop info_ptr,
   png_color_16p *background)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)
	  && background != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "bKGD");
	  *background = &(info_ptr->background);
	  return (PNG_INFO_bKGD);
   }
   return (0);
}
#endif

#if defined(PNG_cHRM_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
png_uint_32 PNGAPI
png_get_cHRM(png_structp png_ptr, png_infop info_ptr,
   double *white_x, double *white_y, double *red_x, double *red_y,
   double *green_x, double *green_y, double *blue_x, double *blue_y)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
   {
	  png_debug1(1, "in %s retrieval function\n", "cHRM");
	  if (white_x != NULL)
		 *white_x = (double)info_ptr->x_white;
	  if (white_y != NULL)
		 *white_y = (double)info_ptr->y_white;
	  if (red_x != NULL)
		 *red_x = (double)info_ptr->x_red;
	  if (red_y != NULL)
		 *red_y = (double)info_ptr->y_red;
	  if (green_x != NULL)
		 *green_x = (double)info_ptr->x_green;
	  if (green_y != NULL)
		 *green_y = (double)info_ptr->y_green;
	  if (blue_x != NULL)
		 *blue_x = (double)info_ptr->x_blue;
	  if (blue_y != NULL)
		 *blue_y = (double)info_ptr->y_blue;
	  return (PNG_INFO_cHRM);
   }
   return (0);
}
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
png_uint_32 PNGAPI
png_get_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
   png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x,
   png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y,
   png_fixed_point *blue_x, png_fixed_point *blue_y)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
   {
	  png_debug1(1, "in %s retrieval function\n", "cHRM");
	  if (white_x != NULL)
		 *white_x = info_ptr->int_x_white;
	  if (white_y != NULL)
		 *white_y = info_ptr->int_y_white;
	  if (red_x != NULL)
		 *red_x = info_ptr->int_x_red;
	  if (red_y != NULL)
		 *red_y = info_ptr->int_y_red;
	  if (green_x != NULL)
		 *green_x = info_ptr->int_x_green;
	  if (green_y != NULL)
		 *green_y = info_ptr->int_y_green;
	  if (blue_x != NULL)
		 *blue_x = info_ptr->int_x_blue;
	  if (blue_y != NULL)
		 *blue_y = info_ptr->int_y_blue;
	  return (PNG_INFO_cHRM);
   }
   return (0);
}
#endif
#endif

#if defined(PNG_gAMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
png_uint_32 PNGAPI
png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
	  && file_gamma != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "gAMA");
	  *file_gamma = (double)info_ptr->gamma;
	  return (PNG_INFO_gAMA);
   }
   return (0);
}
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
png_uint_32 PNGAPI
png_get_gAMA_fixed(png_structp png_ptr, png_infop info_ptr,
	png_fixed_point *int_file_gamma)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
	  && int_file_gamma != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "gAMA");
	  *int_file_gamma = info_ptr->int_gamma;
	  return (PNG_INFO_gAMA);
   }
   return (0);
}
#endif
#endif

#if defined(PNG_sRGB_SUPPORTED)
png_uint_32 PNGAPI
png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)
	  && file_srgb_intent != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "sRGB");
	  *file_srgb_intent = (int)info_ptr->srgb_intent;
	  return (PNG_INFO_sRGB);
   }
   return (0);
}
#endif

#if defined(PNG_iCCP_SUPPORTED)
png_uint_32 PNGAPI
png_get_iCCP(png_structp png_ptr, png_infop info_ptr,
			 png_charpp name, int *compression_type,
			 png_charpp profile, png_uint_32 *proflen)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)
	  && name != NULL && profile != NULL && proflen != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "iCCP");
	  *name = info_ptr->iccp_name;
	  *profile = info_ptr->iccp_profile;
	  *proflen = (int)info_ptr->iccp_proflen;
	  *compression_type = (int)info_ptr->iccp_compression;
	  return (PNG_INFO_iCCP);
   }
   return (0);
}
#endif

#if defined(PNG_sPLT_SUPPORTED)
png_uint_32 PNGAPI
png_get_sPLT(png_structp png_ptr, png_infop info_ptr,
			 png_sPLT_tpp spalettes)
{
   if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)
   {
	 *spalettes = info_ptr->splt_palettes;
	 return ((png_uint_32)info_ptr->splt_palettes_num);
   }
   return (0);
}
#endif

#if defined(PNG_hIST_SUPPORTED)
png_uint_32 PNGAPI
png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)
	  && hist != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "hIST");
	  *hist = info_ptr->hist;
	  return (PNG_INFO_hIST);
   }
   return (0);
}
#endif

png_uint_32 PNGAPI
png_get_IHDR(png_structp png_ptr, png_infop info_ptr,
   png_uint_32 *width, png_uint_32 *height, int *bit_depth,
   int *color_type, int *interlace_type, int *compression_type,
   int *filter_type)

{
   if (png_ptr != NULL && info_ptr != NULL && width != NULL && height != NULL &&
	  bit_depth != NULL && color_type != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "IHDR");
	  *width = info_ptr->width;
	  *height = info_ptr->height;
	  *bit_depth = info_ptr->bit_depth;
	  if (info_ptr->bit_depth < 1 || info_ptr->bit_depth > 16)
		png_error(png_ptr, "Invalid bit depth");
	  *color_type = info_ptr->color_type;
	  if (info_ptr->color_type > 6)
		png_error(png_ptr, "Invalid color type");
	  if (compression_type != NULL)
		 *compression_type = info_ptr->compression_type;
	  if (filter_type != NULL)
		 *filter_type = info_ptr->filter_type;
	  if (interlace_type != NULL)
		 *interlace_type = info_ptr->interlace_type;

	  if (*width == 0 || *width > PNG_UINT_31_MAX)
		png_error(png_ptr, "Invalid image width");
	  if (*height == 0 || *height > PNG_UINT_31_MAX)
		png_error(png_ptr, "Invalid image height");
	  if (info_ptr->width > (PNG_UINT_32_MAX
				 >> 3)	  /* 8-byte RGBA pixels */
				 - 64	   /* bigrowbuf hack */
				 - 1	/* filter byte */
				 - 7*8	  /* rounding of width to multiple of 8 pixels */
				 - 8)	   /* extra max_pixel_depth pad */
	  {
		 png_warning(png_ptr,
			"Width too large for libpng to process image data.");
	  }
	  return (1);
   }
   return (0);
}

#if defined(PNG_oFFs_SUPPORTED)
png_uint_32 PNGAPI
png_get_oFFs(png_structp png_ptr, png_infop info_ptr,
   png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)
	  && offset_x != NULL && offset_y != NULL && unit_type != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "oFFs");
	  *offset_x = info_ptr->x_offset;
	  *offset_y = info_ptr->y_offset;
	  *unit_type = (int)info_ptr->offset_unit_type;
	  return (PNG_INFO_oFFs);
   }
   return (0);
}
#endif

#if defined(PNG_pCAL_SUPPORTED)
png_uint_32 PNGAPI
png_get_pCAL(png_structp png_ptr, png_infop info_ptr,
   png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,
   png_charp *units, png_charpp *params)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)
	  && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&
	  nparams != NULL && units != NULL && params != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "pCAL");
	  *purpose = info_ptr->pcal_purpose;
	  *X0 = info_ptr->pcal_X0;
	  *X1 = info_ptr->pcal_X1;
	  *type = (int)info_ptr->pcal_type;
	  *nparams = (int)info_ptr->pcal_nparams;
	  *units = info_ptr->pcal_units;
	  *params = info_ptr->pcal_params;
	  return (PNG_INFO_pCAL);
   }
   return (0);
}
#endif

#if defined(PNG_sCAL_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
png_uint_32 PNGAPI
png_get_sCAL(png_structp png_ptr, png_infop info_ptr,
			 int *unit, double *width, double *height)
{
	if (png_ptr != NULL && info_ptr != NULL &&
	   (info_ptr->valid & PNG_INFO_sCAL))
	{
		*unit = info_ptr->scal_unit;
		*width = info_ptr->scal_pixel_width;
		*height = info_ptr->scal_pixel_height;
		return (PNG_INFO_sCAL);
	}
	return(0);
}
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
png_uint_32 PNGAPI
png_get_sCAL_s(png_structp png_ptr, png_infop info_ptr,
			 int *unit, png_charpp width, png_charpp height)
{
	if (png_ptr != NULL && info_ptr != NULL &&
	   (info_ptr->valid & PNG_INFO_sCAL))
	{
		*unit = info_ptr->scal_unit;
		*width = info_ptr->scal_s_width;
		*height = info_ptr->scal_s_height;
		return (PNG_INFO_sCAL);
	}
	return(0);
}
#endif
#endif
#endif

#if defined(PNG_pHYs_SUPPORTED)
png_uint_32 PNGAPI
png_get_pHYs(png_structp png_ptr, png_infop info_ptr,
   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
{
   png_uint_32 retval = 0;

   if (png_ptr != NULL && info_ptr != NULL &&
	  (info_ptr->valid & PNG_INFO_pHYs))
   {
	  png_debug1(1, "in %s retrieval function\n", "pHYs");
	  if (res_x != NULL)
	  {
		 *res_x = info_ptr->x_pixels_per_unit;
		 retval |= PNG_INFO_pHYs;
	  }
	  if (res_y != NULL)
	  {
		 *res_y = info_ptr->y_pixels_per_unit;
		 retval |= PNG_INFO_pHYs;
	  }
	  if (unit_type != NULL)
	  {
		 *unit_type = (int)info_ptr->phys_unit_type;
		 retval |= PNG_INFO_pHYs;
	  }
   }
   return (retval);
}
#endif

png_uint_32 PNGAPI
png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette,
   int *num_palette)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE)
	   && palette != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "PLTE");
	  *palette = info_ptr->palette;
	  *num_palette = info_ptr->num_palette;
	  png_debug1(3, "num_palette = %d\n", *num_palette);
	  return (PNG_INFO_PLTE);
   }
   return (0);
}

#if defined(PNG_sBIT_SUPPORTED)
png_uint_32 PNGAPI
png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)
	  && sig_bit != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "sBIT");
	  *sig_bit = &(info_ptr->sig_bit);
	  return (PNG_INFO_sBIT);
   }
   return (0);
}
#endif

#if defined(PNG_TEXT_SUPPORTED)
png_uint_32 PNGAPI
png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr,
   int *num_text)
{
   if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)
   {
	  png_debug1(1, "in %s retrieval function\n",
		 (png_ptr->chunk_name[0] == '\0' ? "text"
			 : (png_const_charp)png_ptr->chunk_name));
	  if (text_ptr != NULL)
		 *text_ptr = info_ptr->text;
	  if (num_text != NULL)
		 *num_text = info_ptr->num_text;
	  return ((png_uint_32)info_ptr->num_text);
   }
   if (num_text != NULL)
	 *num_text = 0;
   return(0);
}
#endif

#if defined(PNG_tIME_SUPPORTED)
png_uint_32 PNGAPI
png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time)
{
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)
	   && mod_time != NULL)
   {
	  png_debug1(1, "in %s retrieval function\n", "tIME");
	  *mod_time = &(info_ptr->mod_time);
	  return (PNG_INFO_tIME);
   }
   return (0);
}
#endif

#if defined(PNG_tRNS_SUPPORTED)
png_uint_32 PNGAPI
png_get_tRNS(png_structp png_ptr, png_infop info_ptr,
   png_bytep *trans, int *num_trans, png_color_16p *trans_values)
{
   png_uint_32 retval = 0;
   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
   {
	  png_debug1(1, "in %s retrieval function\n", "tRNS");
	  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  {
		  if (trans != NULL)
		  {
			 *trans = info_ptr->trans;
			 retval |= PNG_INFO_tRNS;
		  }
		  if (trans_values != NULL)
			 *trans_values = &(info_ptr->trans_values);
	  }
	  else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
	  {
		  if (trans_values != NULL)
		  {
			 *trans_values = &(info_ptr->trans_values);
			 retval |= PNG_INFO_tRNS;
		  }
		  if(trans != NULL)
			 *trans = NULL;
	  }
	  if(num_trans != NULL)
	  {
		 *num_trans = info_ptr->num_trans;
		 retval |= PNG_INFO_tRNS;
	  }
   }
   return (retval);
}
#endif

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
png_uint_32 PNGAPI
png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr,
			 png_unknown_chunkpp unknowns)
{
   if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)
   {
	 *unknowns = info_ptr->unknown_chunks;
	 return ((png_uint_32)info_ptr->unknown_chunks_num);
   }
   return (0);
}
#endif

#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
png_byte PNGAPI
png_get_rgb_to_gray_status (png_structp png_ptr)
{
   return (png_byte)(png_ptr? png_ptr->rgb_to_gray_status : 0);
}
#endif

#if defined(PNG_USER_CHUNKS_SUPPORTED)
png_voidp PNGAPI
png_get_user_chunk_ptr(png_structp png_ptr)
{
   return (png_ptr? png_ptr->user_chunk_ptr : NULL);
}
#endif

#ifdef PNG_WRITE_SUPPORTED
png_uint_32 PNGAPI
png_get_compression_buffer_size(png_structp png_ptr)
{
   return (png_uint_32)(png_ptr? png_ptr->zbuf_size : 0L);
}
#endif

#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
#ifndef PNG_1_0_X
png_uint_32 PNGAPI
png_get_asm_flags (png_structp png_ptr)
{
	return (png_ptr? 0L: 0L);
}

png_uint_32 PNGAPI
png_get_asm_flagmask (int flag_select)
{
	flag_select=flag_select;
	return 0L;
}

png_uint_32 PNGAPI
png_get_mmx_flagmask (int flag_select, int *compilerID)
{
	flag_select=flag_select;
	*compilerID = -1;   /* unknown (i.e., no asm/MMX code compiled) */
	return 0L;
}

png_byte PNGAPI
png_get_mmx_bitdepth_threshold (png_structp png_ptr)
{
	return (png_ptr? 0: 0);
}

png_uint_32 PNGAPI
png_get_mmx_rowbytes_threshold (png_structp png_ptr)
{
	return (png_ptr? 0L: 0L);
}
#endif /* ?PNG_1_0_X */
#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
png_uint_32 PNGAPI
png_get_user_width_max (png_structp png_ptr)
{
	return (png_ptr? png_ptr->user_width_max : 0);
}
png_uint_32 PNGAPI
png_get_user_height_max (png_structp png_ptr)
{
	return (png_ptr? png_ptr->user_height_max : 0);
}
#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */

#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngget.c ***/


/*** Start of inlined file: pngmem.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)

#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)

png_voidp /* PRIVATE */
png_create_struct(int type)
{
#ifdef PNG_USER_MEM_SUPPORTED
   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
}

png_voidp /* PRIVATE */
png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
{
#endif /* PNG_USER_MEM_SUPPORTED */
   png_size_t size;
   png_voidp struct_ptr;

   if (type == PNG_STRUCT_INFO)
	 size = png_sizeof(png_info);
   else if (type == PNG_STRUCT_PNG)
	 size = png_sizeof(png_struct);
   else
	 return (png_get_copyright(NULL));

#ifdef PNG_USER_MEM_SUPPORTED
   if(malloc_fn != NULL)
   {
	  png_struct dummy_struct;
	  png_structp png_ptr = &dummy_struct;
	  png_ptr->mem_ptr=mem_ptr;
	  struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
   }
   else
#endif /* PNG_USER_MEM_SUPPORTED */
	  struct_ptr = (png_voidp)farmalloc(size);
   if (struct_ptr != NULL)
	  png_memset(struct_ptr, 0, size);
   return (struct_ptr);
}

void /* PRIVATE */
png_destroy_struct(png_voidp struct_ptr)
{
#ifdef PNG_USER_MEM_SUPPORTED
   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
}

void /* PRIVATE */
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
	png_voidp mem_ptr)
{
#endif
   if (struct_ptr != NULL)
   {
#ifdef PNG_USER_MEM_SUPPORTED
	  if(free_fn != NULL)
	  {
		 png_struct dummy_struct;
		 png_structp png_ptr = &dummy_struct;
		 png_ptr->mem_ptr=mem_ptr;
		 (*(free_fn))(png_ptr, struct_ptr);
		 return;
	  }
#endif /* PNG_USER_MEM_SUPPORTED */
	  farfree (struct_ptr);
   }
}

png_voidp PNGAPI
png_malloc(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ret;

   if (png_ptr == NULL || size == 0)
	  return (NULL);

#ifdef PNG_USER_MEM_SUPPORTED
   if(png_ptr->malloc_fn != NULL)
	   ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
   else
	   ret = (png_malloc_default(png_ptr, size));
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
	   png_error(png_ptr, "Out of memory!");
   return (ret);
}

png_voidp PNGAPI
png_malloc_default(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ret;
#endif /* PNG_USER_MEM_SUPPORTED */

   if (png_ptr == NULL || size == 0)
	  return (NULL);

#ifdef PNG_MAX_MALLOC_64K
   if (size > (png_uint_32)65536L)
   {
	  png_warning(png_ptr, "Cannot Allocate > 64K");
	  ret = NULL;
   }
   else
#endif

   if (size != (size_t)size)
	 ret = NULL;
   else if (size == (png_uint_32)65536L)
   {
	  if (png_ptr->offset_table == NULL)
	  {
		 ret = farmalloc(size);
		 if (ret == NULL || ((png_size_t)ret & 0xffff))
		 {
			int num_blocks;
			png_uint_32 total_size;
			png_bytep table;
			int i;
			png_byte huge * hptr;

			if (ret != NULL)
			{
			   farfree(ret);
			   ret = NULL;
			}

			if(png_ptr->zlib_window_bits > 14)
			   num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
			else
			   num_blocks = 1;
			if (png_ptr->zlib_mem_level >= 7)
			   num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
			else
			   num_blocks++;

			total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;

			table = farmalloc(total_size);

			if (table == NULL)
			{
#ifndef PNG_USER_MEM_SUPPORTED
			   if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
				  png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
			   else
				  png_warning(png_ptr, "Out Of Memory.");
#endif
			   return (NULL);
			}

			if ((png_size_t)table & 0xfff0)
			{
#ifndef PNG_USER_MEM_SUPPORTED
			   if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
				  png_error(png_ptr,
					"Farmalloc didn't return normalized pointer");
			   else
				  png_warning(png_ptr,
					"Farmalloc didn't return normalized pointer");
#endif
			   return (NULL);
			}

			png_ptr->offset_table = table;
			png_ptr->offset_table_ptr = farmalloc(num_blocks *
			   png_sizeof (png_bytep));

			if (png_ptr->offset_table_ptr == NULL)
			{
#ifndef PNG_USER_MEM_SUPPORTED
			   if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
				  png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
			   else
				  png_warning(png_ptr, "Out Of memory.");
#endif
			   return (NULL);
			}

			hptr = (png_byte huge *)table;
			if ((png_size_t)hptr & 0xf)
			{
			   hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
			   hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
			}
			for (i = 0; i < num_blocks; i++)
			{
			   png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
			   hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
			}

			png_ptr->offset_table_number = num_blocks;
			png_ptr->offset_table_count = 0;
			png_ptr->offset_table_count_free = 0;
		 }
	  }

	  if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
	  {
#ifndef PNG_USER_MEM_SUPPORTED
		 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
			png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
		 else
			png_warning(png_ptr, "Out of Memory.");
#endif
		 return (NULL);
	  }

	  ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
   }
   else
	  ret = farmalloc(size);

#ifndef PNG_USER_MEM_SUPPORTED
   if (ret == NULL)
   {
	  if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
		 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
	  else
		 png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
   }
#endif

   return (ret);
}

void PNGAPI
png_free(png_structp png_ptr, png_voidp ptr)
{
   if (png_ptr == NULL || ptr == NULL)
	  return;

#ifdef PNG_USER_MEM_SUPPORTED
   if (png_ptr->free_fn != NULL)
   {
	  (*(png_ptr->free_fn))(png_ptr, ptr);
	  return;
   }
   else png_free_default(png_ptr, ptr);
}

void PNGAPI
png_free_default(png_structp png_ptr, png_voidp ptr)
{
#endif /* PNG_USER_MEM_SUPPORTED */

   if(png_ptr == NULL) return;

   if (png_ptr->offset_table != NULL)
   {
	  int i;

	  for (i = 0; i < png_ptr->offset_table_count; i++)
	  {
		 if (ptr == png_ptr->offset_table_ptr[i])
		 {
			ptr = NULL;
			png_ptr->offset_table_count_free++;
			break;
		 }
	  }
	  if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
	  {
		 farfree(png_ptr->offset_table);
		 farfree(png_ptr->offset_table_ptr);
		 png_ptr->offset_table = NULL;
		 png_ptr->offset_table_ptr = NULL;
	  }
   }

   if (ptr != NULL)
   {
	  farfree(ptr);
   }
}

#else /* Not the Borland DOS special memory handler */

png_voidp /* PRIVATE */
png_create_struct(int type)
{
#ifdef PNG_USER_MEM_SUPPORTED
   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
}

png_voidp /* PRIVATE */
png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
{
#endif /* PNG_USER_MEM_SUPPORTED */
   png_size_t size;
   png_voidp struct_ptr;

   if (type == PNG_STRUCT_INFO)
	  size = png_sizeof(png_info);
   else if (type == PNG_STRUCT_PNG)
	  size = png_sizeof(png_struct);
   else
	  return (NULL);

#ifdef PNG_USER_MEM_SUPPORTED
   if(malloc_fn != NULL)
   {
	  png_struct dummy_struct;
	  png_structp png_ptr = &dummy_struct;
	  png_ptr->mem_ptr=mem_ptr;
	  struct_ptr = (*(malloc_fn))(png_ptr, size);
	  if (struct_ptr != NULL)
		 png_memset(struct_ptr, 0, size);
	  return (struct_ptr);
   }
#endif /* PNG_USER_MEM_SUPPORTED */

#if defined(__TURBOC__) && !defined(__FLAT__)
   struct_ptr = (png_voidp)farmalloc(size);
#else
# if defined(_MSC_VER) && defined(MAXSEG_64K)
   struct_ptr = (png_voidp)halloc(size,1);
# else
   struct_ptr = (png_voidp)malloc(size);
# endif
#endif
   if (struct_ptr != NULL)
	  png_memset(struct_ptr, 0, size);

   return (struct_ptr);
}

void /* PRIVATE */
png_destroy_struct(png_voidp struct_ptr)
{
#ifdef PNG_USER_MEM_SUPPORTED
   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
}

void /* PRIVATE */
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
	png_voidp mem_ptr)
{
#endif /* PNG_USER_MEM_SUPPORTED */
   if (struct_ptr != NULL)
   {
#ifdef PNG_USER_MEM_SUPPORTED
	  if(free_fn != NULL)
	  {
		 png_struct dummy_struct;
		 png_structp png_ptr = &dummy_struct;
		 png_ptr->mem_ptr=mem_ptr;
		 (*(free_fn))(png_ptr, struct_ptr);
		 return;
	  }
#endif /* PNG_USER_MEM_SUPPORTED */
#if defined(__TURBOC__) && !defined(__FLAT__)
	  farfree(struct_ptr);
#else
# if defined(_MSC_VER) && defined(MAXSEG_64K)
	  hfree(struct_ptr);
# else
	  free(struct_ptr);
# endif
#endif
   }
}

png_voidp PNGAPI
png_malloc(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ret;

#ifdef PNG_USER_MEM_SUPPORTED
   if (png_ptr == NULL || size == 0)
	  return (NULL);

   if(png_ptr->malloc_fn != NULL)
	   ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
   else
	   ret = (png_malloc_default(png_ptr, size));
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
	   png_error(png_ptr, "Out of Memory!");
   return (ret);
}

png_voidp PNGAPI
png_malloc_default(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ret;
#endif /* PNG_USER_MEM_SUPPORTED */

   if (png_ptr == NULL || size == 0)
	  return (NULL);

#ifdef PNG_MAX_MALLOC_64K
   if (size > (png_uint_32)65536L)
   {
#ifndef PNG_USER_MEM_SUPPORTED
	  if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
		 png_error(png_ptr, "Cannot Allocate > 64K");
	  else
#endif
		 return NULL;
   }
#endif

#if defined(__TURBOC__) && !defined(__FLAT__)
 if (size != (unsigned long)size)
   ret = NULL;
 else
   ret = farmalloc(size);
#else
# if defined(_MSC_VER) && defined(MAXSEG_64K)
 if (size != (unsigned long)size)
   ret = NULL;
 else
   ret = halloc(size, 1);
# else
 if (size != (size_t)size)
   ret = NULL;
 else
   ret = malloc((size_t)size);
# endif
#endif

#ifndef PNG_USER_MEM_SUPPORTED
   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
	  png_error(png_ptr, "Out of Memory");
#endif

   return (ret);
}

void PNGAPI
png_free(png_structp png_ptr, png_voidp ptr)
{
   if (png_ptr == NULL || ptr == NULL)
	  return;

#ifdef PNG_USER_MEM_SUPPORTED
   if (png_ptr->free_fn != NULL)
   {
	  (*(png_ptr->free_fn))(png_ptr, ptr);
	  return;
   }
   else png_free_default(png_ptr, ptr);
}
void PNGAPI
png_free_default(png_structp png_ptr, png_voidp ptr)
{
   if (png_ptr == NULL || ptr == NULL)
	  return;

#endif /* PNG_USER_MEM_SUPPORTED */

#if defined(__TURBOC__) && !defined(__FLAT__)
   farfree(ptr);
#else
# if defined(_MSC_VER) && defined(MAXSEG_64K)
   hfree(ptr);
# else
   free(ptr);
# endif
#endif
}

#endif /* Not Borland DOS special memory handler */

#if defined(PNG_1_0_X)
#  define png_malloc_warn png_malloc
#else
png_voidp PNGAPI
png_malloc_warn(png_structp png_ptr, png_uint_32 size)
{
   png_voidp ptr;
   png_uint_32 save_flags;
   if(png_ptr == NULL) return (NULL);

   save_flags=png_ptr->flags;
   png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
   ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
   png_ptr->flags=save_flags;
   return(ptr);
}
#endif

png_voidp PNGAPI
png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
   png_uint_32 length)
{
   png_size_t size;

   size = (png_size_t)length;
   if ((png_uint_32)size != length)
	  png_error(png_ptr,"Overflow in png_memcpy_check.");

   return(png_memcpy (s1, s2, size));
}

png_voidp PNGAPI
png_memset_check (png_structp png_ptr, png_voidp s1, int value,
   png_uint_32 length)
{
   png_size_t size;

   size = (png_size_t)length;
   if ((png_uint_32)size != length)
	  png_error(png_ptr,"Overflow in png_memset_check.");

   return (png_memset (s1, value, size));

}

#ifdef PNG_USER_MEM_SUPPORTED
void PNGAPI
png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  malloc_fn, png_free_ptr free_fn)
{
   if(png_ptr != NULL) {
   png_ptr->mem_ptr = mem_ptr;
   png_ptr->malloc_fn = malloc_fn;
   png_ptr->free_fn = free_fn;
   }
}

png_voidp PNGAPI
png_get_mem_ptr(png_structp png_ptr)
{
   if(png_ptr == NULL) return (NULL);
   return ((png_voidp)png_ptr->mem_ptr);
}
#endif /* PNG_USER_MEM_SUPPORTED */
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngmem.c ***/


/*** Start of inlined file: pngread.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED)

png_structp PNGAPI
png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn)
{

#ifdef PNG_USER_MEM_SUPPORTED
   return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
	  warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
}

png_structp PNGAPI
png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
{
#endif /* PNG_USER_MEM_SUPPORTED */

   png_structp png_ptr;

#ifdef PNG_SETJMP_SUPPORTED
#ifdef USE_FAR_KEYWORD
   jmp_buf jmpbuf;
#endif
#endif

   int i;

   png_debug(1, "in png_create_read_struct\n");
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
	  (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
#else
   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
#endif
   if (png_ptr == NULL)
	  return (NULL);

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
#endif

#ifdef PNG_SETJMP_SUPPORTED
#ifdef USE_FAR_KEYWORD
   if (setjmp(jmpbuf))
#else
   if (setjmp(png_ptr->jmpbuf))
#endif
   {
	  png_free(png_ptr, png_ptr->zbuf);
	  png_ptr->zbuf=NULL;
#ifdef PNG_USER_MEM_SUPPORTED
	  png_destroy_struct_2((png_voidp)png_ptr,
		 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
#else
	  png_destroy_struct((png_voidp)png_ptr);
#endif
	  return (NULL);
   }
#ifdef USE_FAR_KEYWORD
   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
#endif
#endif

#ifdef PNG_USER_MEM_SUPPORTED
   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
#endif

   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);

   i=0;
   do
   {
	 if(user_png_ver[i] != png_libpng_ver[i])
		png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
   } while (png_libpng_ver[i++]);

   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
   {
	 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
		 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
		 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
	 {
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
		char msg[80];
		if (user_png_ver)
		{
		  png_snprintf(msg, 80,
			 "Application was compiled with png.h from libpng-%.20s",
			 user_png_ver);
		  png_warning(png_ptr, msg);
		}
		png_snprintf(msg, 80,
			 "Application  is  running with png.c from libpng-%.20s",
		   png_libpng_ver);
		png_warning(png_ptr, msg);
#endif
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
		png_ptr->flags=0;
#endif
		png_error(png_ptr,
		   "Incompatible libpng version in application and library");
	 }
   }

   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
	 (png_uint_32)png_ptr->zbuf_size);
   png_ptr->zstream.zalloc = png_zalloc;
   png_ptr->zstream.zfree = png_zfree;
   png_ptr->zstream.opaque = (voidpf)png_ptr;

   switch (inflateInit(&png_ptr->zstream))
   {
	 case Z_OK: /* Do nothing */ break;
	 case Z_MEM_ERROR:
	 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
	 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
	 default: png_error(png_ptr, "Unknown zlib error");
   }

   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;

   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);

#ifdef PNG_SETJMP_SUPPORTED
#ifdef USE_FAR_KEYWORD
   if (setjmp(jmpbuf))
	  PNG_ABORT();
   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
#else
   if (setjmp(png_ptr->jmpbuf))
	  PNG_ABORT();
#endif
#endif
   return (png_ptr);
}

#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
#undef png_read_init
void PNGAPI
png_read_init(png_structp png_ptr)
{
   png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
}

void PNGAPI
png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
   png_size_t png_struct_size, png_size_t png_info_size)
{
   if(png_ptr == NULL) return;
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
   if(png_sizeof(png_struct) > png_struct_size ||
	  png_sizeof(png_info) > png_info_size)
   {
	  char msg[80];
	  png_ptr->warning_fn=NULL;
	  if (user_png_ver)
	  {
		png_snprintf(msg, 80,
		   "Application was compiled with png.h from libpng-%.20s",
		   user_png_ver);
		png_warning(png_ptr, msg);
	  }
	  png_snprintf(msg, 80,
		 "Application  is  running with png.c from libpng-%.20s",
		 png_libpng_ver);
	  png_warning(png_ptr, msg);
   }
#endif
   if(png_sizeof(png_struct) > png_struct_size)
	 {
	   png_ptr->error_fn=NULL;
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
	   png_ptr->flags=0;
#endif
	   png_error(png_ptr,
	   "The png struct allocated by the application for reading is too small.");
	 }
   if(png_sizeof(png_info) > png_info_size)
	 {
	   png_ptr->error_fn=NULL;
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
	   png_ptr->flags=0;
#endif
	   png_error(png_ptr,
		 "The info struct allocated by application for reading is too small.");
	 }
   png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
}
#endif /* PNG_1_0_X || PNG_1_2_X */

void PNGAPI
png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
   png_size_t png_struct_size)
{
#ifdef PNG_SETJMP_SUPPORTED
   jmp_buf tmp_jmp;  /* to save current jump buffer */
#endif

   int i=0;

   png_structp png_ptr=*ptr_ptr;

   if(png_ptr == NULL) return;

   do
   {
	 if(user_png_ver[i] != png_libpng_ver[i])
	 {
#ifdef PNG_LEGACY_SUPPORTED
	   png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
#else
	   png_ptr->warning_fn=NULL;
	   png_warning(png_ptr,
		"Application uses deprecated png_read_init() and should be recompiled.");
	   break;
#endif
	 }
   } while (png_libpng_ver[i++]);

   png_debug(1, "in png_read_init_3\n");

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
#endif

   if(png_sizeof(png_struct) > png_struct_size)
	 {
	   png_destroy_struct(png_ptr);
	   *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
	   png_ptr = *ptr_ptr;
	 }

   png_memset(png_ptr, 0, png_sizeof (png_struct));

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
#endif

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
#endif

   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
	 (png_uint_32)png_ptr->zbuf_size);
   png_ptr->zstream.zalloc = png_zalloc;
   png_ptr->zstream.zfree = png_zfree;
   png_ptr->zstream.opaque = (voidpf)png_ptr;

   switch (inflateInit(&png_ptr->zstream))
   {
	 case Z_OK: /* Do nothing */ break;
	 case Z_MEM_ERROR:
	 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
	 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
	 default: png_error(png_ptr, "Unknown zlib error");
   }

   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;

   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
}

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
void PNGAPI
png_read_info(png_structp png_ptr, png_infop info_ptr)
{
   if(png_ptr == NULL) return;
   png_debug(1, "in png_read_info\n");
   if (png_ptr->sig_bytes < 8)
   {
	  png_size_t num_checked = png_ptr->sig_bytes,
				 num_to_check = 8 - num_checked;

	  png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
	  png_ptr->sig_bytes = 8;

	  if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
	  {
		 if (num_checked < 4 &&
			 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
			png_error(png_ptr, "Not a PNG file");
		 else
			png_error(png_ptr, "PNG file corrupted by ASCII conversion");
	  }
	  if (num_checked < 3)
		 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
   }

   for(;;)
   {
#ifdef PNG_USE_LOCAL_ARRAYS
	  PNG_CONST PNG_IHDR;
	  PNG_CONST PNG_IDAT;
	  PNG_CONST PNG_IEND;
	  PNG_CONST PNG_PLTE;
#if defined(PNG_READ_bKGD_SUPPORTED)
	  PNG_CONST PNG_bKGD;
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
	  PNG_CONST PNG_cHRM;
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
	  PNG_CONST PNG_gAMA;
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
	  PNG_CONST PNG_hIST;
#endif
#if defined(PNG_READ_iCCP_SUPPORTED)
	  PNG_CONST PNG_iCCP;
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
	  PNG_CONST PNG_iTXt;
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
	  PNG_CONST PNG_oFFs;
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
	  PNG_CONST PNG_pCAL;
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
	  PNG_CONST PNG_pHYs;
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
	  PNG_CONST PNG_sBIT;
#endif
#if defined(PNG_READ_sCAL_SUPPORTED)
	  PNG_CONST PNG_sCAL;
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
	  PNG_CONST PNG_sPLT;
#endif
#if defined(PNG_READ_sRGB_SUPPORTED)
	  PNG_CONST PNG_sRGB;
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
	  PNG_CONST PNG_tEXt;
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
	  PNG_CONST PNG_tIME;
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
	  PNG_CONST PNG_tRNS;
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
	  PNG_CONST PNG_zTXt;
#endif
#endif /* PNG_USE_LOCAL_ARRAYS */
	  png_byte chunk_length[4];
	  png_uint_32 length;

	  png_read_data(png_ptr, chunk_length, 4);
	  length = png_get_uint_31(png_ptr,chunk_length);

	  png_reset_crc(png_ptr);
	  png_crc_read(png_ptr, png_ptr->chunk_name, 4);

	  png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name,
		 length);

	  if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
		if(png_ptr->mode & PNG_AFTER_IDAT)
		  png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;

	  if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
		 png_handle_IHDR(png_ptr, info_ptr, length);
	  else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
		 png_handle_IEND(png_ptr, info_ptr, length);
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
	  else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
	  {
		 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
			png_ptr->mode |= PNG_HAVE_IDAT;
		 png_handle_unknown(png_ptr, info_ptr, length);
		 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
			png_ptr->mode |= PNG_HAVE_PLTE;
		 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
		 {
			if (!(png_ptr->mode & PNG_HAVE_IHDR))
			   png_error(png_ptr, "Missing IHDR before IDAT");
			else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
					 !(png_ptr->mode & PNG_HAVE_PLTE))
			   png_error(png_ptr, "Missing PLTE before IDAT");
			break;
		 }
	  }
#endif
	  else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
		 png_handle_PLTE(png_ptr, info_ptr, length);
	  else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
	  {
		 if (!(png_ptr->mode & PNG_HAVE_IHDR))
			png_error(png_ptr, "Missing IHDR before IDAT");
		 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
				  !(png_ptr->mode & PNG_HAVE_PLTE))
			png_error(png_ptr, "Missing PLTE before IDAT");

		 png_ptr->idat_size = length;
		 png_ptr->mode |= PNG_HAVE_IDAT;
		 break;
	  }
#if defined(PNG_READ_bKGD_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
		 png_handle_bKGD(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
		 png_handle_cHRM(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
		 png_handle_gAMA(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
		 png_handle_hIST(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
		 png_handle_oFFs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
		 png_handle_pCAL(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sCAL_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
		 png_handle_sCAL(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
		 png_handle_pHYs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
		 png_handle_sBIT(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sRGB_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
		 png_handle_sRGB(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_iCCP_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
		 png_handle_iCCP(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
		 png_handle_sPLT(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
		 png_handle_tEXt(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
		 png_handle_tIME(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
		 png_handle_tRNS(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
		 png_handle_zTXt(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
		 png_handle_iTXt(png_ptr, info_ptr, length);
#endif
	  else
		 png_handle_unknown(png_ptr, info_ptr, length);
   }
}
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */

void PNGAPI
png_read_update_info(png_structp png_ptr, png_infop info_ptr)
{
   png_debug(1, "in png_read_update_info\n");
   if(png_ptr == NULL) return;
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
	  png_read_start_row(png_ptr);
   else
	  png_warning(png_ptr,
	  "Ignoring extra png_read_update_info() call; row buffer not reallocated");
   png_read_transform_info(png_ptr, info_ptr);
}

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
void PNGAPI
png_start_read_image(png_structp png_ptr)
{
   png_debug(1, "in png_start_read_image\n");
   if(png_ptr == NULL) return;
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
	  png_read_start_row(png_ptr);
}
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
void PNGAPI
png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_CONST PNG_IDAT;
   PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
	 0xff};
   PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
#endif
   int ret;
   if(png_ptr == NULL) return;
   png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
	  png_ptr->row_number, png_ptr->pass);
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
	  png_read_start_row(png_ptr);
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
   {
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_MONO)
	  png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
#endif
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
	  png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
#endif
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_PACKSWAP)
	  png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
#endif
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
   if (png_ptr->transformations & PNG_PACK)
	  png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
#endif
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
   if (png_ptr->transformations & PNG_SHIFT)
	  png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
#endif
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
   if (png_ptr->transformations & PNG_BGR)
	  png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
#endif
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
	  png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
#endif
   }

#if defined(PNG_READ_INTERLACING_SUPPORTED)
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
   {
	  switch (png_ptr->pass)
	  {
		 case 0:
			if (png_ptr->row_number & 0x07)
			{
			   if (dsp_row != NULL)
				  png_combine_row(png_ptr, dsp_row,
					 png_pass_dsp_mask[png_ptr->pass]);
			   png_read_finish_row(png_ptr);
			   return;
			}
			break;
		 case 1:
			if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
			{
			   if (dsp_row != NULL)
				  png_combine_row(png_ptr, dsp_row,
					 png_pass_dsp_mask[png_ptr->pass]);
			   png_read_finish_row(png_ptr);
			   return;
			}
			break;
		 case 2:
			if ((png_ptr->row_number & 0x07) != 4)
			{
			   if (dsp_row != NULL && (png_ptr->row_number & 4))
				  png_combine_row(png_ptr, dsp_row,
					 png_pass_dsp_mask[png_ptr->pass]);
			   png_read_finish_row(png_ptr);
			   return;
			}
			break;
		 case 3:
			if ((png_ptr->row_number & 3) || png_ptr->width < 3)
			{
			   if (dsp_row != NULL)
				  png_combine_row(png_ptr, dsp_row,
					 png_pass_dsp_mask[png_ptr->pass]);
			   png_read_finish_row(png_ptr);
			   return;
			}
			break;
		 case 4:
			if ((png_ptr->row_number & 3) != 2)
			{
			   if (dsp_row != NULL && (png_ptr->row_number & 2))
				  png_combine_row(png_ptr, dsp_row,
					 png_pass_dsp_mask[png_ptr->pass]);
			   png_read_finish_row(png_ptr);
			   return;
			}
			break;
		 case 5:
			if ((png_ptr->row_number & 1) || png_ptr->width < 2)
			{
			   if (dsp_row != NULL)
				  png_combine_row(png_ptr, dsp_row,
					 png_pass_dsp_mask[png_ptr->pass]);
			   png_read_finish_row(png_ptr);
			   return;
			}
			break;
		 case 6:
			if (!(png_ptr->row_number & 1))
			{
			   png_read_finish_row(png_ptr);
			   return;
			}
			break;
	  }
   }
#endif

   if (!(png_ptr->mode & PNG_HAVE_IDAT))
	  png_error(png_ptr, "Invalid attempt to read row data");

   png_ptr->zstream.next_out = png_ptr->row_buf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
   do
   {
	  if (!(png_ptr->zstream.avail_in))
	  {
		 while (!png_ptr->idat_size)
		 {
			png_byte chunk_length[4];

			png_crc_finish(png_ptr, 0);

			png_read_data(png_ptr, chunk_length, 4);
			png_ptr->idat_size = png_get_uint_31(png_ptr,chunk_length);

			png_reset_crc(png_ptr);
			png_crc_read(png_ptr, png_ptr->chunk_name, 4);
			if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
			   png_error(png_ptr, "Not enough image data");
		 }
		 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
		 png_ptr->zstream.next_in = png_ptr->zbuf;
		 if (png_ptr->zbuf_size > png_ptr->idat_size)
			png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
		 png_crc_read(png_ptr, png_ptr->zbuf,
			(png_size_t)png_ptr->zstream.avail_in);
		 png_ptr->idat_size -= png_ptr->zstream.avail_in;
	  }
	  ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
	  if (ret == Z_STREAM_END)
	  {
		 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
			png_ptr->idat_size)
			png_error(png_ptr, "Extra compressed data");
		 png_ptr->mode |= PNG_AFTER_IDAT;
		 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
		 break;
	  }
	  if (ret != Z_OK)
		 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
				   "Decompression error");

   } while (png_ptr->zstream.avail_out);

   png_ptr->row_info.color_type = png_ptr->color_type;
   png_ptr->row_info.width = png_ptr->iwidth;
   png_ptr->row_info.channels = png_ptr->channels;
   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
	   png_ptr->row_info.width);

   if(png_ptr->row_buf[0])
   png_read_filter_row(png_ptr, &(png_ptr->row_info),
	  png_ptr->row_buf + 1, png_ptr->prev_row + 1,
	  (int)(png_ptr->row_buf[0]));

   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
	  png_ptr->rowbytes + 1);

#if defined(PNG_MNG_FEATURES_SUPPORTED)
   if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
	  (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
   {
	  png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
   }
#endif

   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
	  png_do_read_transformations(png_ptr);

#if defined(PNG_READ_INTERLACING_SUPPORTED)
   if (png_ptr->interlaced &&
	  (png_ptr->transformations & PNG_INTERLACE))
   {
	  if (png_ptr->pass < 6)
		 png_do_read_interlace(png_ptr);

	  if (dsp_row != NULL)
		 png_combine_row(png_ptr, dsp_row,
			png_pass_dsp_mask[png_ptr->pass]);
	  if (row != NULL)
		 png_combine_row(png_ptr, row,
			png_pass_mask[png_ptr->pass]);
   }
   else
#endif
   {
	  if (row != NULL)
		 png_combine_row(png_ptr, row, 0xff);
	  if (dsp_row != NULL)
		 png_combine_row(png_ptr, dsp_row, 0xff);
   }
   png_read_finish_row(png_ptr);

   if (png_ptr->read_row_fn != NULL)
	  (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
}
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED

void PNGAPI
png_read_rows(png_structp png_ptr, png_bytepp row,
   png_bytepp display_row, png_uint_32 num_rows)
{
   png_uint_32 i;
   png_bytepp rp;
   png_bytepp dp;

   png_debug(1, "in png_read_rows\n");
   if(png_ptr == NULL) return;
   rp = row;
   dp = display_row;
   if (rp != NULL && dp != NULL)
	  for (i = 0; i < num_rows; i++)
	  {
		 png_bytep rptr = *rp++;
		 png_bytep dptr = *dp++;

		 png_read_row(png_ptr, rptr, dptr);
	  }
   else if(rp != NULL)
	  for (i = 0; i < num_rows; i++)
	  {
		 png_bytep rptr = *rp;
		 png_read_row(png_ptr, rptr, png_bytep_NULL);
		 rp++;
	  }
   else if(dp != NULL)
	  for (i = 0; i < num_rows; i++)
	  {
		 png_bytep dptr = *dp;
		 png_read_row(png_ptr, png_bytep_NULL, dptr);
		 dp++;
	  }
}
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
void PNGAPI
png_read_image(png_structp png_ptr, png_bytepp image)
{
   png_uint_32 i,image_height;
   int pass, j;
   png_bytepp rp;

   png_debug(1, "in png_read_image\n");
   if(png_ptr == NULL) return;

#ifdef PNG_READ_INTERLACING_SUPPORTED
   pass = png_set_interlace_handling(png_ptr);
#else
   if (png_ptr->interlaced)
	  png_error(png_ptr,
		"Cannot read interlaced image -- interlace handler disabled.");
   pass = 1;
#endif

   image_height=png_ptr->height;
   png_ptr->num_rows = image_height; /* Make sure this is set correctly */

   for (j = 0; j < pass; j++)
   {
	  rp = image;
	  for (i = 0; i < image_height; i++)
	  {
		 png_read_row(png_ptr, *rp, png_bytep_NULL);
		 rp++;
	  }
   }
}
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
void PNGAPI
png_read_end(png_structp png_ptr, png_infop info_ptr)
{
   png_byte chunk_length[4];
   png_uint_32 length;

   png_debug(1, "in png_read_end\n");
   if(png_ptr == NULL) return;
   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */

   do
   {
#ifdef PNG_USE_LOCAL_ARRAYS
	  PNG_CONST PNG_IHDR;
	  PNG_CONST PNG_IDAT;
	  PNG_CONST PNG_IEND;
	  PNG_CONST PNG_PLTE;
#if defined(PNG_READ_bKGD_SUPPORTED)
	  PNG_CONST PNG_bKGD;
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
	  PNG_CONST PNG_cHRM;
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
	  PNG_CONST PNG_gAMA;
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
	  PNG_CONST PNG_hIST;
#endif
#if defined(PNG_READ_iCCP_SUPPORTED)
	  PNG_CONST PNG_iCCP;
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
	  PNG_CONST PNG_iTXt;
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
	  PNG_CONST PNG_oFFs;
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
	  PNG_CONST PNG_pCAL;
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
	  PNG_CONST PNG_pHYs;
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
	  PNG_CONST PNG_sBIT;
#endif
#if defined(PNG_READ_sCAL_SUPPORTED)
	  PNG_CONST PNG_sCAL;
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
	  PNG_CONST PNG_sPLT;
#endif
#if defined(PNG_READ_sRGB_SUPPORTED)
	  PNG_CONST PNG_sRGB;
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
	  PNG_CONST PNG_tEXt;
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
	  PNG_CONST PNG_tIME;
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
	  PNG_CONST PNG_tRNS;
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
	  PNG_CONST PNG_zTXt;
#endif
#endif /* PNG_USE_LOCAL_ARRAYS */

	  png_read_data(png_ptr, chunk_length, 4);
	  length = png_get_uint_31(png_ptr,chunk_length);

	  png_reset_crc(png_ptr);
	  png_crc_read(png_ptr, png_ptr->chunk_name, 4);

	  png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);

	  if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
		 png_handle_IHDR(png_ptr, info_ptr, length);
	  else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
		 png_handle_IEND(png_ptr, info_ptr, length);
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
	  else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
	  {
		 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
		 {
			if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
			   png_error(png_ptr, "Too many IDAT's found");
		 }
		 png_handle_unknown(png_ptr, info_ptr, length);
		 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
			png_ptr->mode |= PNG_HAVE_PLTE;
	  }
#endif
	  else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
	  {
		 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
			png_error(png_ptr, "Too many IDAT's found");
		 png_crc_finish(png_ptr, length);
	  }
	  else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
		 png_handle_PLTE(png_ptr, info_ptr, length);
#if defined(PNG_READ_bKGD_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
		 png_handle_bKGD(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
		 png_handle_cHRM(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
		 png_handle_gAMA(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
		 png_handle_hIST(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
		 png_handle_oFFs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
		 png_handle_pCAL(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sCAL_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
		 png_handle_sCAL(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
		 png_handle_pHYs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
		 png_handle_sBIT(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sRGB_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
		 png_handle_sRGB(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_iCCP_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
		 png_handle_iCCP(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
		 png_handle_sPLT(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
		 png_handle_tEXt(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
		 png_handle_tIME(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
		 png_handle_tRNS(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
		 png_handle_zTXt(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
	  else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
		 png_handle_iTXt(png_ptr, info_ptr, length);
#endif
	  else
		 png_handle_unknown(png_ptr, info_ptr, length);
   } while (!(png_ptr->mode & PNG_HAVE_IEND));
}
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */

void PNGAPI
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
   png_infopp end_info_ptr_ptr)
{
   png_structp png_ptr = NULL;
   png_infop info_ptr = NULL, end_info_ptr = NULL;
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn;
   png_voidp mem_ptr;
#endif

   png_debug(1, "in png_destroy_read_struct\n");
   if (png_ptr_ptr != NULL)
	  png_ptr = *png_ptr_ptr;

   if (info_ptr_ptr != NULL)
	  info_ptr = *info_ptr_ptr;

   if (end_info_ptr_ptr != NULL)
	  end_info_ptr = *end_info_ptr_ptr;

#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
   mem_ptr = png_ptr->mem_ptr;
#endif

   png_read_destroy(png_ptr, info_ptr, end_info_ptr);

   if (info_ptr != NULL)
   {
#if defined(PNG_TEXT_SUPPORTED)
	  png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
#endif

#ifdef PNG_USER_MEM_SUPPORTED
	  png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
		  (png_voidp)mem_ptr);
#else
	  png_destroy_struct((png_voidp)info_ptr);
#endif
	  *info_ptr_ptr = NULL;
   }

   if (end_info_ptr != NULL)
   {
#if defined(PNG_READ_TEXT_SUPPORTED)
	  png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
#endif
#ifdef PNG_USER_MEM_SUPPORTED
	  png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
		 (png_voidp)mem_ptr);
#else
	  png_destroy_struct((png_voidp)end_info_ptr);
#endif
	  *end_info_ptr_ptr = NULL;
   }

   if (png_ptr != NULL)
   {
#ifdef PNG_USER_MEM_SUPPORTED
	  png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
		  (png_voidp)mem_ptr);
#else
	  png_destroy_struct((png_voidp)png_ptr);
#endif
	  *png_ptr_ptr = NULL;
   }
}

void /* PRIVATE */
png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
{
#ifdef PNG_SETJMP_SUPPORTED
   jmp_buf tmp_jmp;
#endif
   png_error_ptr error_fn;
   png_error_ptr warning_fn;
   png_voidp error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn;
#endif

   png_debug(1, "in png_read_destroy\n");
   if (info_ptr != NULL)
	  png_info_destroy(png_ptr, info_ptr);

   if (end_info_ptr != NULL)
	  png_info_destroy(png_ptr, end_info_ptr);

   png_free(png_ptr, png_ptr->zbuf);
   png_free(png_ptr, png_ptr->big_row_buf);
   png_free(png_ptr, png_ptr->prev_row);
#if defined(PNG_READ_DITHER_SUPPORTED)
   png_free(png_ptr, png_ptr->palette_lookup);
   png_free(png_ptr, png_ptr->dither_index);
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
   png_free(png_ptr, png_ptr->gamma_table);
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   png_free(png_ptr, png_ptr->gamma_from_1);
   png_free(png_ptr, png_ptr->gamma_to_1);
#endif
#ifdef PNG_FREE_ME_SUPPORTED
   if (png_ptr->free_me & PNG_FREE_PLTE)
	  png_zfree(png_ptr, png_ptr->palette);
   png_ptr->free_me &= ~PNG_FREE_PLTE;
#else
   if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
	  png_zfree(png_ptr, png_ptr->palette);
   png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
#endif
#if defined(PNG_tRNS_SUPPORTED) || \
	defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
   if (png_ptr->free_me & PNG_FREE_TRNS)
	  png_free(png_ptr, png_ptr->trans);
   png_ptr->free_me &= ~PNG_FREE_TRNS;
#else
   if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
	  png_free(png_ptr, png_ptr->trans);
   png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
#endif
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
   if (png_ptr->free_me & PNG_FREE_HIST)
	  png_free(png_ptr, png_ptr->hist);
   png_ptr->free_me &= ~PNG_FREE_HIST;
#else
   if (png_ptr->flags & PNG_FLAG_FREE_HIST)
	  png_free(png_ptr, png_ptr->hist);
   png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
#endif
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
   if (png_ptr->gamma_16_table != NULL)
   {
	  int i;
	  int istop = (1 << (8 - png_ptr->gamma_shift));
	  for (i = 0; i < istop; i++)
	  {
		 png_free(png_ptr, png_ptr->gamma_16_table[i]);
	  }
   png_free(png_ptr, png_ptr->gamma_16_table);
   }
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   if (png_ptr->gamma_16_from_1 != NULL)
   {
	  int i;
	  int istop = (1 << (8 - png_ptr->gamma_shift));
	  for (i = 0; i < istop; i++)
	  {
		 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
	  }
   png_free(png_ptr, png_ptr->gamma_16_from_1);
   }
   if (png_ptr->gamma_16_to_1 != NULL)
   {
	  int i;
	  int istop = (1 << (8 - png_ptr->gamma_shift));
	  for (i = 0; i < istop; i++)
	  {
		 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
	  }
   png_free(png_ptr, png_ptr->gamma_16_to_1);
   }
#endif
#endif
#if defined(PNG_TIME_RFC1123_SUPPORTED)
   png_free(png_ptr, png_ptr->time_buffer);
#endif

   inflateEnd(&png_ptr->zstream);
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
   png_free(png_ptr, png_ptr->save_buffer);
#endif

#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
#ifdef PNG_TEXT_SUPPORTED
   png_free(png_ptr, png_ptr->current_text);
#endif /* PNG_TEXT_SUPPORTED */
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
#endif

   error_fn = png_ptr->error_fn;
   warning_fn = png_ptr->warning_fn;
   error_ptr = png_ptr->error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif

   png_memset(png_ptr, 0, png_sizeof (png_struct));

   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
   png_ptr->error_ptr = error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
#endif

}

void PNGAPI
png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
{
   if(png_ptr == NULL) return;
   png_ptr->read_row_fn = read_row_fn;
}

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
#if defined(PNG_INFO_IMAGE_SUPPORTED)
void PNGAPI
png_read_png(png_structp png_ptr, png_infop info_ptr,
						   int transforms,
						   voidp params)
{
   int row;

   if(png_ptr == NULL) return;
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
	   png_set_invert_alpha(png_ptr);
#endif

   png_read_info(png_ptr, info_ptr);
   if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
	  png_error(png_ptr,"Image is too high to process with png_read_png()");

#if defined(PNG_READ_16_TO_8_SUPPORTED)
   if (transforms & PNG_TRANSFORM_STRIP_16)
	   png_set_strip_16(png_ptr);
#endif

#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
	   png_set_strip_alpha(png_ptr);
#endif

#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
   if (transforms & PNG_TRANSFORM_PACKING)
	   png_set_packing(png_ptr);
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
   if (transforms & PNG_TRANSFORM_PACKSWAP)
	   png_set_packswap(png_ptr);
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
   if (transforms & PNG_TRANSFORM_EXPAND)
	   if ((png_ptr->bit_depth < 8) ||
		   (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
		   (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
		 png_set_expand(png_ptr);
#endif

#if defined(PNG_READ_INVERT_SUPPORTED)
   if (transforms & PNG_TRANSFORM_INVERT_MONO)
	   png_set_invert_mono(png_ptr);
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED)
   if ((transforms & PNG_TRANSFORM_SHIFT)
	   && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
   {
	  png_color_8p sig_bit;

	  png_get_sBIT(png_ptr, info_ptr, &sig_bit);
	  png_set_shift(png_ptr, sig_bit);
   }
#endif

#if defined(PNG_READ_BGR_SUPPORTED)
   if (transforms & PNG_TRANSFORM_BGR)
	   png_set_bgr(png_ptr);
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
	   png_set_swap_alpha(png_ptr);
#endif

#if defined(PNG_READ_SWAP_SUPPORTED)
   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
	   png_set_swap(png_ptr);
#endif

   png_read_update_info(png_ptr, info_ptr);

#ifdef PNG_FREE_ME_SUPPORTED
   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
#endif
   if(info_ptr->row_pointers == NULL)
   {
	  info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
		 info_ptr->height * png_sizeof(png_bytep));
#ifdef PNG_FREE_ME_SUPPORTED
	  info_ptr->free_me |= PNG_FREE_ROWS;
#endif
	  for (row = 0; row < (int)info_ptr->height; row++)
	  {
		 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
			png_get_rowbytes(png_ptr, info_ptr));
	  }
   }

   png_read_image(png_ptr, info_ptr->row_pointers);
   info_ptr->valid |= PNG_INFO_IDAT;

   png_read_end(png_ptr, info_ptr);

   transforms = transforms; /* quiet compiler warnings */
   params = params;

}
#endif /* PNG_INFO_IMAGE_SUPPORTED */
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
#endif /* PNG_READ_SUPPORTED */
/*** End of inlined file: pngread.c ***/


/*** Start of inlined file: pngpread.c ***/
#define PNG_INTERNAL

#ifdef PNG_PROGRESSIVE_READ_SUPPORTED

#define PNG_READ_SIG_MODE   0
#define PNG_READ_CHUNK_MODE 1
#define PNG_READ_IDAT_MODE  2
#define PNG_SKIP_MODE	   3
#define PNG_READ_tEXt_MODE  4
#define PNG_READ_zTXt_MODE  5
#define PNG_READ_DONE_MODE  6
#define PNG_READ_iTXt_MODE  7
#define PNG_ERROR_MODE	  8

void PNGAPI
png_process_data(png_structp png_ptr, png_infop info_ptr,
   png_bytep buffer, png_size_t buffer_size)
{
   if(png_ptr == NULL) return;
   png_push_restore_buffer(png_ptr, buffer, buffer_size);

   while (png_ptr->buffer_size)
   {
	  png_process_some_data(png_ptr, info_ptr);
   }
}

void /* PRIVATE */
png_process_some_data(png_structp png_ptr, png_infop info_ptr)
{
   if(png_ptr == NULL) return;
   switch (png_ptr->process_mode)
   {
	  case PNG_READ_SIG_MODE:
	  {
		 png_push_read_sig(png_ptr, info_ptr);
		 break;
	  }
	  case PNG_READ_CHUNK_MODE:
	  {
		 png_push_read_chunk(png_ptr, info_ptr);
		 break;
	  }
	  case PNG_READ_IDAT_MODE:
	  {
		 png_push_read_IDAT(png_ptr);
		 break;
	  }
#if defined(PNG_READ_tEXt_SUPPORTED)
	  case PNG_READ_tEXt_MODE:
	  {
		 png_push_read_tEXt(png_ptr, info_ptr);
		 break;
	  }
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
	  case PNG_READ_zTXt_MODE:
	  {
		 png_push_read_zTXt(png_ptr, info_ptr);
		 break;
	  }
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
	  case PNG_READ_iTXt_MODE:
	  {
		 png_push_read_iTXt(png_ptr, info_ptr);
		 break;
	  }
#endif
	  case PNG_SKIP_MODE:
	  {
		 png_push_crc_finish(png_ptr);
		 break;
	  }
	  default:
	  {
		 png_ptr->buffer_size = 0;
		 break;
	  }
   }
}

void /* PRIVATE */
png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
{
   png_size_t num_checked = png_ptr->sig_bytes,
			 num_to_check = 8 - num_checked;

   if (png_ptr->buffer_size < num_to_check)
   {
	  num_to_check = png_ptr->buffer_size;
   }

   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
	  num_to_check);
   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes+num_to_check);

   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
   {
	  if (num_checked < 4 &&
		  png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
		 png_error(png_ptr, "Not a PNG file");
	  else
		 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
   }
   else
   {
	  if (png_ptr->sig_bytes >= 8)
	  {
		 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
	  }
   }
}

void /* PRIVATE */
png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
{
#ifdef PNG_USE_LOCAL_ARRAYS
	  PNG_CONST PNG_IHDR;
	  PNG_CONST PNG_IDAT;
	  PNG_CONST PNG_IEND;
	  PNG_CONST PNG_PLTE;
#if defined(PNG_READ_bKGD_SUPPORTED)
	  PNG_CONST PNG_bKGD;
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
	  PNG_CONST PNG_cHRM;
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
	  PNG_CONST PNG_gAMA;
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
	  PNG_CONST PNG_hIST;
#endif
#if defined(PNG_READ_iCCP_SUPPORTED)
	  PNG_CONST PNG_iCCP;
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
	  PNG_CONST PNG_iTXt;
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
	  PNG_CONST PNG_oFFs;
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
	  PNG_CONST PNG_pCAL;
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
	  PNG_CONST PNG_pHYs;
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
	  PNG_CONST PNG_sBIT;
#endif
#if defined(PNG_READ_sCAL_SUPPORTED)
	  PNG_CONST PNG_sCAL;
#endif
#if defined(PNG_READ_sRGB_SUPPORTED)
	  PNG_CONST PNG_sRGB;
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
	  PNG_CONST PNG_sPLT;
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
	  PNG_CONST PNG_tEXt;
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
	  PNG_CONST PNG_tIME;
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
	  PNG_CONST PNG_tRNS;
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
	  PNG_CONST PNG_zTXt;
#endif
#endif /* PNG_USE_LOCAL_ARRAYS */
   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
   {
	  png_byte chunk_length[4];

	  if (png_ptr->buffer_size < 8)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }

	  png_push_fill_buffer(png_ptr, chunk_length, 4);
	  png_ptr->push_length = png_get_uint_31(png_ptr,chunk_length);
	  png_reset_crc(png_ptr);
	  png_crc_read(png_ptr, png_ptr->chunk_name, 4);
	  png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
   }

   if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
	 if(png_ptr->mode & PNG_AFTER_IDAT)
		png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;

   if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
   }
   else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);

	  png_ptr->process_mode = PNG_READ_DONE_MODE;
	  png_push_have_end(png_ptr, info_ptr);
   }
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
   else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
		 png_ptr->mode |= PNG_HAVE_IDAT;
	  png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
	  if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
		 png_ptr->mode |= PNG_HAVE_PLTE;
	  else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
	  {
		 if (!(png_ptr->mode & PNG_HAVE_IHDR))
			png_error(png_ptr, "Missing IHDR before IDAT");
		 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
				  !(png_ptr->mode & PNG_HAVE_PLTE))
			png_error(png_ptr, "Missing PLTE before IDAT");
	  }
   }
#endif
   else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
   }
   else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
   {
	 if (!(png_ptr->mode & PNG_HAVE_IHDR))
	   png_error(png_ptr, "Missing IHDR before IDAT");
	 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
		 !(png_ptr->mode & PNG_HAVE_PLTE))
	   png_error(png_ptr, "Missing PLTE before IDAT");

	  if (png_ptr->mode & PNG_HAVE_IDAT)
	  {
		 if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
		   if (png_ptr->push_length == 0)
			  return;

		 if (png_ptr->mode & PNG_AFTER_IDAT)
			png_error(png_ptr, "Too many IDAT's found");
	  }

	  png_ptr->idat_size = png_ptr->push_length;
	  png_ptr->mode |= PNG_HAVE_IDAT;
	  png_ptr->process_mode = PNG_READ_IDAT_MODE;
	  png_push_have_info(png_ptr, info_ptr);
	  png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
	  png_ptr->zstream.next_out = png_ptr->row_buf;
	  return;
   }
#if defined(PNG_READ_gAMA_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_sRGB_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_iCCP_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_bKGD_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_sCAL_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
   else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
   }
#endif
   else
   {
	  if (png_ptr->push_length + 4 > png_ptr->buffer_size)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }
	  png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
   }

   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
}

void /* PRIVATE */
png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
{
   png_ptr->process_mode = PNG_SKIP_MODE;
   png_ptr->skip_length = skip;
}

void /* PRIVATE */
png_push_crc_finish(png_structp png_ptr)
{
   if (png_ptr->skip_length && png_ptr->save_buffer_size)
   {
	  png_size_t save_size;

	  if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
		 save_size = (png_size_t)png_ptr->skip_length;
	  else
		 save_size = png_ptr->save_buffer_size;

	  png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);

	  png_ptr->skip_length -= save_size;
	  png_ptr->buffer_size -= save_size;
	  png_ptr->save_buffer_size -= save_size;
	  png_ptr->save_buffer_ptr += save_size;
   }
   if (png_ptr->skip_length && png_ptr->current_buffer_size)
   {
	  png_size_t save_size;

	  if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
		 save_size = (png_size_t)png_ptr->skip_length;
	  else
		 save_size = png_ptr->current_buffer_size;

	  png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);

	  png_ptr->skip_length -= save_size;
	  png_ptr->buffer_size -= save_size;
	  png_ptr->current_buffer_size -= save_size;
	  png_ptr->current_buffer_ptr += save_size;
   }
   if (!png_ptr->skip_length)
   {
	  if (png_ptr->buffer_size < 4)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }

	  png_crc_finish(png_ptr, 0);
	  png_ptr->process_mode = PNG_READ_CHUNK_MODE;
   }
}

void PNGAPI
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
{
   png_bytep ptr;

   if(png_ptr == NULL) return;
   ptr = buffer;
   if (png_ptr->save_buffer_size)
   {
	  png_size_t save_size;

	  if (length < png_ptr->save_buffer_size)
		 save_size = length;
	  else
		 save_size = png_ptr->save_buffer_size;

	  png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
	  length -= save_size;
	  ptr += save_size;
	  png_ptr->buffer_size -= save_size;
	  png_ptr->save_buffer_size -= save_size;
	  png_ptr->save_buffer_ptr += save_size;
   }
   if (length && png_ptr->current_buffer_size)
   {
	  png_size_t save_size;

	  if (length < png_ptr->current_buffer_size)
		 save_size = length;
	  else
		 save_size = png_ptr->current_buffer_size;

	  png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
	  png_ptr->buffer_size -= save_size;
	  png_ptr->current_buffer_size -= save_size;
	  png_ptr->current_buffer_ptr += save_size;
   }
}

void /* PRIVATE */
png_push_save_buffer(png_structp png_ptr)
{
   if (png_ptr->save_buffer_size)
   {
	  if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
	  {
		 png_size_t i,istop;
		 png_bytep sp;
		 png_bytep dp;

		 istop = png_ptr->save_buffer_size;
		 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
			i < istop; i++, sp++, dp++)
		 {
			*dp = *sp;
		 }
	  }
   }
   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
	  png_ptr->save_buffer_max)
   {
	  png_size_t new_max;
	  png_bytep old_buffer;

	  if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
		 (png_ptr->current_buffer_size + 256))
	  {
		png_error(png_ptr, "Potential overflow of save_buffer");
	  }
	  new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
	  old_buffer = png_ptr->save_buffer;
	  png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
		 (png_uint_32)new_max);
	  png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
	  png_free(png_ptr, old_buffer);
	  png_ptr->save_buffer_max = new_max;
   }
   if (png_ptr->current_buffer_size)
   {
	  png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
		 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
	  png_ptr->save_buffer_size += png_ptr->current_buffer_size;
	  png_ptr->current_buffer_size = 0;
   }
   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
   png_ptr->buffer_size = 0;
}

void /* PRIVATE */
png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
   png_size_t buffer_length)
{
   png_ptr->current_buffer = buffer;
   png_ptr->current_buffer_size = buffer_length;
   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
}

void /* PRIVATE */
png_push_read_IDAT(png_structp png_ptr)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_CONST PNG_IDAT;
#endif
   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
   {
	  png_byte chunk_length[4];

	  if (png_ptr->buffer_size < 8)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }

	  png_push_fill_buffer(png_ptr, chunk_length, 4);
	  png_ptr->push_length = png_get_uint_31(png_ptr,chunk_length);
	  png_reset_crc(png_ptr);
	  png_crc_read(png_ptr, png_ptr->chunk_name, 4);
	  png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;

	  if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
	  {
		 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
		 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
			png_error(png_ptr, "Not enough compressed data");
		 return;
	  }

	  png_ptr->idat_size = png_ptr->push_length;
   }
   if (png_ptr->idat_size && png_ptr->save_buffer_size)
   {
	  png_size_t save_size;

	  if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
	  {
		 save_size = (png_size_t)png_ptr->idat_size;
		 if((png_uint_32)save_size != png_ptr->idat_size)
			png_error(png_ptr, "save_size overflowed in pngpread");
	  }
	  else
		 save_size = png_ptr->save_buffer_size;

	  png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
	  if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
		 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
	  png_ptr->idat_size -= save_size;
	  png_ptr->buffer_size -= save_size;
	  png_ptr->save_buffer_size -= save_size;
	  png_ptr->save_buffer_ptr += save_size;
   }
   if (png_ptr->idat_size && png_ptr->current_buffer_size)
   {
	  png_size_t save_size;

	  if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
	  {
		 save_size = (png_size_t)png_ptr->idat_size;
		 if((png_uint_32)save_size != png_ptr->idat_size)
			png_error(png_ptr, "save_size overflowed in pngpread");
	  }
	  else
		 save_size = png_ptr->current_buffer_size;

	  png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
	  if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
		png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);

	  png_ptr->idat_size -= save_size;
	  png_ptr->buffer_size -= save_size;
	  png_ptr->current_buffer_size -= save_size;
	  png_ptr->current_buffer_ptr += save_size;
   }
   if (!png_ptr->idat_size)
   {
	  if (png_ptr->buffer_size < 4)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }

	  png_crc_finish(png_ptr, 0);
	  png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
	  png_ptr->mode |= PNG_AFTER_IDAT;
   }
}

void /* PRIVATE */
png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
   png_size_t buffer_length)
{
   int ret;

   if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
	  png_error(png_ptr, "Extra compression data");

   png_ptr->zstream.next_in = buffer;
   png_ptr->zstream.avail_in = (uInt)buffer_length;
   for(;;)
   {
	  ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
	  if (ret != Z_OK)
	  {
		 if (ret == Z_STREAM_END)
		 {
			if (png_ptr->zstream.avail_in)
			   png_error(png_ptr, "Extra compressed data");
			if (!(png_ptr->zstream.avail_out))
			{
			   png_push_process_row(png_ptr);
			}

			png_ptr->mode |= PNG_AFTER_IDAT;
			png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
			break;
		 }
		 else if (ret == Z_BUF_ERROR)
			break;
		 else
			png_error(png_ptr, "Decompression Error");
	  }
	  if (!(png_ptr->zstream.avail_out))
	  {
		 if ((
#if defined(PNG_READ_INTERLACING_SUPPORTED)
			 png_ptr->interlaced && png_ptr->pass > 6) ||
			 (!png_ptr->interlaced &&
#endif
			 png_ptr->row_number == png_ptr->num_rows))
		 {
		   if (png_ptr->zstream.avail_in)
		   {
			 png_warning(png_ptr, "Too much data in IDAT chunks");
		   }

		   png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
		   break;
		 }
		 png_push_process_row(png_ptr);
		 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
		 png_ptr->zstream.next_out = png_ptr->row_buf;
	  }
	  else
		 break;
   }
}

void /* PRIVATE */
png_push_process_row(png_structp png_ptr)
{
   png_ptr->row_info.color_type = png_ptr->color_type;
   png_ptr->row_info.width = png_ptr->iwidth;
   png_ptr->row_info.channels = png_ptr->channels;
   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;

   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
	   png_ptr->row_info.width);

   png_read_filter_row(png_ptr, &(png_ptr->row_info),
	  png_ptr->row_buf + 1, png_ptr->prev_row + 1,
	  (int)(png_ptr->row_buf[0]));

   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
	  png_ptr->rowbytes + 1);

   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
	  png_do_read_transformations(png_ptr);

#if defined(PNG_READ_INTERLACING_SUPPORTED)
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
   {
	  if (png_ptr->pass < 6)
		 png_do_read_interlace(png_ptr);

	switch (png_ptr->pass)
	{
		 case 0:
		 {
			int i;
			for (i = 0; i < 8 && png_ptr->pass == 0; i++)
			{
			   png_push_have_row(png_ptr, png_ptr->row_buf + 1);
			   png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
			}
			if (png_ptr->pass == 2) /* pass 1 might be empty */
			{
			   for (i = 0; i < 4 && png_ptr->pass == 2; i++)
			   {
				  png_push_have_row(png_ptr, png_bytep_NULL);
				  png_read_push_finish_row(png_ptr);
			   }
			}
			if (png_ptr->pass == 4 && png_ptr->height <= 4)
			{
			   for (i = 0; i < 2 && png_ptr->pass == 4; i++)
			   {
				  png_push_have_row(png_ptr, png_bytep_NULL);
				  png_read_push_finish_row(png_ptr);
			   }
			}
			if (png_ptr->pass == 6 && png_ptr->height <= 4)
			{
				png_push_have_row(png_ptr, png_bytep_NULL);
				png_read_push_finish_row(png_ptr);
			}
			break;
		 }
		 case 1:
		 {
			int i;
			for (i = 0; i < 8 && png_ptr->pass == 1; i++)
			{
			   png_push_have_row(png_ptr, png_ptr->row_buf + 1);
			   png_read_push_finish_row(png_ptr);
			}
			if (png_ptr->pass == 2) /* skip top 4 generated rows */
			{
			   for (i = 0; i < 4 && png_ptr->pass == 2; i++)
			   {
				  png_push_have_row(png_ptr, png_bytep_NULL);
				  png_read_push_finish_row(png_ptr);
			   }
			}
			break;
		 }
		 case 2:
		 {
			int i;
			for (i = 0; i < 4 && png_ptr->pass == 2; i++)
			{
			   png_push_have_row(png_ptr, png_ptr->row_buf + 1);
			   png_read_push_finish_row(png_ptr);
			}
			for (i = 0; i < 4 && png_ptr->pass == 2; i++)
			{
			   png_push_have_row(png_ptr, png_bytep_NULL);
			   png_read_push_finish_row(png_ptr);
			}
			if (png_ptr->pass == 4) /* pass 3 might be empty */
			{
			   for (i = 0; i < 2 && png_ptr->pass == 4; i++)
			   {
				  png_push_have_row(png_ptr, png_bytep_NULL);
				  png_read_push_finish_row(png_ptr);
			   }
			}
			break;
		 }
		 case 3:
		 {
			int i;
			for (i = 0; i < 4 && png_ptr->pass == 3; i++)
			{
			   png_push_have_row(png_ptr, png_ptr->row_buf + 1);
			   png_read_push_finish_row(png_ptr);
			}
			if (png_ptr->pass == 4) /* skip top two generated rows */
			{
			   for (i = 0; i < 2 && png_ptr->pass == 4; i++)
			   {
				  png_push_have_row(png_ptr, png_bytep_NULL);
				  png_read_push_finish_row(png_ptr);
			   }
			}
			break;
		 }
		 case 4:
		 {
			int i;
			for (i = 0; i < 2 && png_ptr->pass == 4; i++)
			{
			   png_push_have_row(png_ptr, png_ptr->row_buf + 1);
			   png_read_push_finish_row(png_ptr);
			}
			for (i = 0; i < 2 && png_ptr->pass == 4; i++)
			{
			   png_push_have_row(png_ptr, png_bytep_NULL);
			   png_read_push_finish_row(png_ptr);
			}
			if (png_ptr->pass == 6) /* pass 5 might be empty */
			{
			   png_push_have_row(png_ptr, png_bytep_NULL);
			   png_read_push_finish_row(png_ptr);
			}
			break;
		 }
		 case 5:
		 {
			int i;
			for (i = 0; i < 2 && png_ptr->pass == 5; i++)
			{
			   png_push_have_row(png_ptr, png_ptr->row_buf + 1);
			   png_read_push_finish_row(png_ptr);
			}
			if (png_ptr->pass == 6) /* skip top generated row */
			{
			   png_push_have_row(png_ptr, png_bytep_NULL);
			   png_read_push_finish_row(png_ptr);
			}
			break;
		 }
		 case 6:
		 {
			png_push_have_row(png_ptr, png_ptr->row_buf + 1);
			png_read_push_finish_row(png_ptr);
			if (png_ptr->pass != 6)
			   break;
			png_push_have_row(png_ptr, png_bytep_NULL);
			png_read_push_finish_row(png_ptr);
		 }
	  }
   }
   else
#endif
   {
	  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
	  png_read_push_finish_row(png_ptr);
   }
}

void /* PRIVATE */
png_read_push_finish_row(png_structp png_ptr)
{
#ifdef PNG_USE_LOCAL_ARRAYS

   PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};

   PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};

   PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};

   PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};

#endif

   png_ptr->row_number++;
   if (png_ptr->row_number < png_ptr->num_rows)
	  return;

   if (png_ptr->interlaced)
   {
	  png_ptr->row_number = 0;
	  png_memset_check(png_ptr, png_ptr->prev_row, 0,
		 png_ptr->rowbytes + 1);
	  do
	  {
		 png_ptr->pass++;
		 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
			 (png_ptr->pass == 3 && png_ptr->width < 3) ||
			 (png_ptr->pass == 5 && png_ptr->width < 2))
		   png_ptr->pass++;

		 if (png_ptr->pass > 7)
			png_ptr->pass--;
		 if (png_ptr->pass >= 7)
			break;

		 png_ptr->iwidth = (png_ptr->width +
			png_pass_inc[png_ptr->pass] - 1 -
			png_pass_start[png_ptr->pass]) /
			png_pass_inc[png_ptr->pass];

		 png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
			png_ptr->iwidth) + 1;

		 if (png_ptr->transformations & PNG_INTERLACE)
			break;

		 png_ptr->num_rows = (png_ptr->height +
			png_pass_yinc[png_ptr->pass] - 1 -
			png_pass_ystart[png_ptr->pass]) /
			png_pass_yinc[png_ptr->pass];

	  } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
   }
}

#if defined(PNG_READ_tEXt_SUPPORTED)
void /* PRIVATE */
png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
   length)
{
   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
	  {
		 png_error(png_ptr, "Out of place tEXt");
		 info_ptr = info_ptr; /* to quiet some compiler warnings */
	  }

#ifdef PNG_MAX_MALLOC_64K
   png_ptr->skip_length = 0;  /* This may not be necessary */

   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
   {
	  png_warning(png_ptr, "tEXt chunk too large to fit in memory");
	  png_ptr->skip_length = length - (png_uint_32)65535L;
	  length = (png_uint_32)65535L;
   }
#endif

   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
		 (png_uint_32)(length+1));
   png_ptr->current_text[length] = '\0';
   png_ptr->current_text_ptr = png_ptr->current_text;
   png_ptr->current_text_size = (png_size_t)length;
   png_ptr->current_text_left = (png_size_t)length;
   png_ptr->process_mode = PNG_READ_tEXt_MODE;
}

void /* PRIVATE */
png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr->buffer_size && png_ptr->current_text_left)
   {
	  png_size_t text_size;

	  if (png_ptr->buffer_size < png_ptr->current_text_left)
		 text_size = png_ptr->buffer_size;
	  else
		 text_size = png_ptr->current_text_left;
	  png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
	  png_ptr->current_text_left -= text_size;
	  png_ptr->current_text_ptr += text_size;
   }
   if (!(png_ptr->current_text_left))
   {
	  png_textp text_ptr;
	  png_charp text;
	  png_charp key;
	  int ret;

	  if (png_ptr->buffer_size < 4)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }

	  png_push_crc_finish(png_ptr);

#if defined(PNG_MAX_MALLOC_64K)
	  if (png_ptr->skip_length)
		 return;
#endif

	  key = png_ptr->current_text;

	  for (text = key; *text; text++)
 ;

	  if (text < key + png_ptr->current_text_size)
		 text++;

	  text_ptr = (png_textp)png_malloc(png_ptr,
		 (png_uint_32)png_sizeof(png_text));
	  text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
	  text_ptr->key = key;
#ifdef PNG_iTXt_SUPPORTED
	  text_ptr->lang = NULL;
	  text_ptr->lang_key = NULL;
#endif
	  text_ptr->text = text;

	  ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);

	  png_free(png_ptr, key);
	  png_free(png_ptr, text_ptr);
	  png_ptr->current_text = NULL;

	  if (ret)
		png_warning(png_ptr, "Insufficient memory to store text chunk.");
   }
}
#endif

#if defined(PNG_READ_zTXt_SUPPORTED)
void /* PRIVATE */
png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
   length)
{
   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
	  {
		 png_error(png_ptr, "Out of place zTXt");
		 info_ptr = info_ptr; /* to quiet some compiler warnings */
	  }

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
	  png_warning(png_ptr, "zTXt chunk too large to fit in memory");
	  png_push_crc_skip(png_ptr, length);
	  return;
   }
#endif

   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
	   (png_uint_32)(length+1));
   png_ptr->current_text[length] = '\0';
   png_ptr->current_text_ptr = png_ptr->current_text;
   png_ptr->current_text_size = (png_size_t)length;
   png_ptr->current_text_left = (png_size_t)length;
   png_ptr->process_mode = PNG_READ_zTXt_MODE;
}

void /* PRIVATE */
png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr->buffer_size && png_ptr->current_text_left)
   {
	  png_size_t text_size;

	  if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
		 text_size = png_ptr->buffer_size;
	  else
		 text_size = png_ptr->current_text_left;
	  png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
	  png_ptr->current_text_left -= text_size;
	  png_ptr->current_text_ptr += text_size;
   }
   if (!(png_ptr->current_text_left))
   {
	  png_textp text_ptr;
	  png_charp text;
	  png_charp key;
	  int ret;
	  png_size_t text_size, key_size;

	  if (png_ptr->buffer_size < 4)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }

	  png_push_crc_finish(png_ptr);

	  key = png_ptr->current_text;

	  for (text = key; *text; text++)
 ;

	  if (text >= key + png_ptr->current_text_size)
	  {
		 png_ptr->current_text = NULL;
		 png_free(png_ptr, key);
		 return;
	  }

	  text++;

	  if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
	  {
		 png_ptr->current_text = NULL;
		 png_free(png_ptr, key);
		 return;
	  }

	  text++;

	  png_ptr->zstream.next_in = (png_bytep )text;
	  png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
		 (text - key));
	  png_ptr->zstream.next_out = png_ptr->zbuf;
	  png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;

	  key_size = text - key;
	  text_size = 0;
	  text = NULL;
	  ret = Z_STREAM_END;

	  while (png_ptr->zstream.avail_in)
	  {
		 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
		 if (ret != Z_OK && ret != Z_STREAM_END)
		 {
			inflateReset(&png_ptr->zstream);
			png_ptr->zstream.avail_in = 0;
			png_ptr->current_text = NULL;
			png_free(png_ptr, key);
			png_free(png_ptr, text);
			return;
		 }
		 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
		 {
			if (text == NULL)
			{
			   text = (png_charp)png_malloc(png_ptr,
				  (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
					 + key_size + 1));
			   png_memcpy(text + key_size, png_ptr->zbuf,
				  png_ptr->zbuf_size - png_ptr->zstream.avail_out);
			   png_memcpy(text, key, key_size);
			   text_size = key_size + png_ptr->zbuf_size -
				  png_ptr->zstream.avail_out;
			   *(text + text_size) = '\0';
			}
			else
			{
			   png_charp tmp;

			   tmp = text;
			   text = (png_charp)png_malloc(png_ptr, text_size +
				  (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
				   + 1));
			   png_memcpy(text, tmp, text_size);
			   png_free(png_ptr, tmp);
			   png_memcpy(text + text_size, png_ptr->zbuf,
				  png_ptr->zbuf_size - png_ptr->zstream.avail_out);
			   text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
			   *(text + text_size) = '\0';
			}
			if (ret != Z_STREAM_END)
			{
			   png_ptr->zstream.next_out = png_ptr->zbuf;
			   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
			}
		 }
		 else
		 {
			break;
		 }

		 if (ret == Z_STREAM_END)
			break;
	  }

	  inflateReset(&png_ptr->zstream);
	  png_ptr->zstream.avail_in = 0;

	  if (ret != Z_STREAM_END)
	  {
		 png_ptr->current_text = NULL;
		 png_free(png_ptr, key);
		 png_free(png_ptr, text);
		 return;
	  }

	  png_ptr->current_text = NULL;
	  png_free(png_ptr, key);
	  key = text;
	  text += key_size;

	  text_ptr = (png_textp)png_malloc(png_ptr,
		  (png_uint_32)png_sizeof(png_text));
	  text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
	  text_ptr->key = key;
#ifdef PNG_iTXt_SUPPORTED
	  text_ptr->lang = NULL;
	  text_ptr->lang_key = NULL;
#endif
	  text_ptr->text = text;

	  ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);

	  png_free(png_ptr, key);
	  png_free(png_ptr, text_ptr);

	  if (ret)
		png_warning(png_ptr, "Insufficient memory to store text chunk.");
   }
}
#endif

#if defined(PNG_READ_iTXt_SUPPORTED)
void /* PRIVATE */
png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
   length)
{
   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
	  {
		 png_error(png_ptr, "Out of place iTXt");
		 info_ptr = info_ptr; /* to quiet some compiler warnings */
	  }

#ifdef PNG_MAX_MALLOC_64K
   png_ptr->skip_length = 0;  /* This may not be necessary */

   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
   {
	  png_warning(png_ptr, "iTXt chunk too large to fit in memory");
	  png_ptr->skip_length = length - (png_uint_32)65535L;
	  length = (png_uint_32)65535L;
   }
#endif

   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
		 (png_uint_32)(length+1));
   png_ptr->current_text[length] = '\0';
   png_ptr->current_text_ptr = png_ptr->current_text;
   png_ptr->current_text_size = (png_size_t)length;
   png_ptr->current_text_left = (png_size_t)length;
   png_ptr->process_mode = PNG_READ_iTXt_MODE;
}

void /* PRIVATE */
png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
{

   if (png_ptr->buffer_size && png_ptr->current_text_left)
   {
	  png_size_t text_size;

	  if (png_ptr->buffer_size < png_ptr->current_text_left)
		 text_size = png_ptr->buffer_size;
	  else
		 text_size = png_ptr->current_text_left;
	  png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
	  png_ptr->current_text_left -= text_size;
	  png_ptr->current_text_ptr += text_size;
   }
   if (!(png_ptr->current_text_left))
   {
	  png_textp text_ptr;
	  png_charp key;
	  int comp_flag;
	  png_charp lang;
	  png_charp lang_key;
	  png_charp text;
	  int ret;

	  if (png_ptr->buffer_size < 4)
	  {
		 png_push_save_buffer(png_ptr);
		 return;
	  }

	  png_push_crc_finish(png_ptr);

#if defined(PNG_MAX_MALLOC_64K)
	  if (png_ptr->skip_length)
		 return;
#endif

	  key = png_ptr->current_text;

	  for (lang = key; *lang; lang++)
 ;

	  if (lang < key + png_ptr->current_text_size - 3)
		 lang++;

	  comp_flag = *lang++;
	  lang++;	 /* skip comp_type, always zero */

	  for (lang_key = lang; *lang_key; lang_key++)
 ;
	  lang_key++;	/* skip NUL separator */

	  text=lang_key;
	  if (lang_key < key + png_ptr->current_text_size - 1)
	  {
		for (; *text; text++)
 ;
	  }

	  if (text < key + png_ptr->current_text_size)
		 text++;

	  text_ptr = (png_textp)png_malloc(png_ptr,
		 (png_uint_32)png_sizeof(png_text));
	  text_ptr->compression = comp_flag + 2;
	  text_ptr->key = key;
	  text_ptr->lang = lang;
	  text_ptr->lang_key = lang_key;
	  text_ptr->text = text;
	  text_ptr->text_length = 0;
	  text_ptr->itxt_length = png_strlen(text);

	  ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);

	  png_ptr->current_text = NULL;

	  png_free(png_ptr, text_ptr);
	  if (ret)
		png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
   }
}
#endif

void /* PRIVATE */
png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
   length)
{
   png_uint_32 skip=0;
   png_check_chunk_name(png_ptr, png_ptr->chunk_name);

   if (!(png_ptr->chunk_name[0] & 0x20))
   {
#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
	 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
		  PNG_HANDLE_CHUNK_ALWAYS
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
		  && png_ptr->read_user_chunk_fn == NULL
#endif
		)
#endif
		png_chunk_error(png_ptr, "unknown critical chunk");

	 info_ptr = info_ptr; /* to quiet some compiler warnings */
   }

#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
   if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
   {
#ifdef PNG_MAX_MALLOC_64K
	  if (length > (png_uint_32)65535L)
	  {
		  png_warning(png_ptr, "unknown chunk too large to fit in memory");
		  skip = length - (png_uint_32)65535L;
		  length = (png_uint_32)65535L;
	  }
#endif
	  png_strncpy((png_charp)png_ptr->unknown_chunk.name,
	 (png_charp)png_ptr->chunk_name, 5);
	  png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
	  png_ptr->unknown_chunk.size = (png_size_t)length;
	  png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
	  if(png_ptr->read_user_chunk_fn != NULL)
	  {
		 int ret;
		 ret = (*(png_ptr->read_user_chunk_fn))
		   (png_ptr, &png_ptr->unknown_chunk);
		 if (ret < 0)
			png_chunk_error(png_ptr, "error in user chunk");
		 if (ret == 0)
		 {
			if (!(png_ptr->chunk_name[0] & 0x20))
			   if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
					PNG_HANDLE_CHUNK_ALWAYS)
				  png_chunk_error(png_ptr, "unknown critical chunk");
			png_set_unknown_chunks(png_ptr, info_ptr,
			   &png_ptr->unknown_chunk, 1);
		 }
	  }
#else
	  png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
#endif
	  png_free(png_ptr, png_ptr->unknown_chunk.data);
	  png_ptr->unknown_chunk.data = NULL;
   }
   else
#endif
	  skip=length;
   png_push_crc_skip(png_ptr, skip);
}

void /* PRIVATE */
png_push_have_info(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr->info_fn != NULL)
	  (*(png_ptr->info_fn))(png_ptr, info_ptr);
}

void /* PRIVATE */
png_push_have_end(png_structp png_ptr, png_infop info_ptr)
{
   if (png_ptr->end_fn != NULL)
	  (*(png_ptr->end_fn))(png_ptr, info_ptr);
}

void /* PRIVATE */
png_push_have_row(png_structp png_ptr, png_bytep row)
{
   if (png_ptr->row_fn != NULL)
	  (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
		 (int)png_ptr->pass);
}

void PNGAPI
png_progressive_combine_row (png_structp png_ptr,
   png_bytep old_row, png_bytep new_row)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_CONST int FARDATA png_pass_dsp_mask[7] =
	  {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
#endif
   if(png_ptr == NULL) return;
   if (new_row != NULL)	/* new_row must == png_ptr->row_buf here. */
	  png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
}

void PNGAPI
png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
   png_progressive_end_ptr end_fn)
{
   if(png_ptr == NULL) return;
   png_ptr->info_fn = info_fn;
   png_ptr->row_fn = row_fn;
   png_ptr->end_fn = end_fn;

   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
}

png_voidp PNGAPI
png_get_progressive_ptr(png_structp png_ptr)
{
   if(png_ptr == NULL) return (NULL);
   return png_ptr->io_ptr;
}
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
/*** End of inlined file: pngpread.c ***/


/*** Start of inlined file: pngrio.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED)

void /* PRIVATE */
png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   png_debug1(4,"reading %d bytes\n", (int)length);
   if (png_ptr->read_data_fn != NULL)
	  (*(png_ptr->read_data_fn))(png_ptr, data, length);
   else
	  png_error(png_ptr, "Call to NULL read function");
}

#if !defined(PNG_NO_STDIO)
#ifndef USE_FAR_KEYWORD
void PNGAPI
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   png_size_t check;

   if(png_ptr == NULL) return;
#if defined(_WIN32_WCE)
   if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
	  check = 0;
#else
   check = (png_size_t)fread(data, (png_size_t)1, length,
	  (png_FILE_p)png_ptr->io_ptr);
#endif

   if (check != length)
	  png_error(png_ptr, "Read Error");
}
#else

#define NEAR_BUF_SIZE 1024
#define MIN(a,b) (a <= b ? a : b)

static void PNGAPI
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   int check;
   png_byte *n_data;
   png_FILE_p io_ptr;

   if(png_ptr == NULL) return;
   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   if ((png_bytep)n_data == data)
   {
#if defined(_WIN32_WCE)
	  if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
		 check = 0;
#else
	  check = fread(n_data, 1, length, io_ptr);
#endif
   }
   else
   {
	  png_byte buf[NEAR_BUF_SIZE];
	  png_size_t read, remaining, err;
	  check = 0;
	  remaining = length;
	  do
	  {
		 read = MIN(NEAR_BUF_SIZE, remaining);
#if defined(_WIN32_WCE)
		 if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
			err = 0;
#else
		 err = fread(buf, (png_size_t)1, read, io_ptr);
#endif
		 png_memcpy(data, buf, read); /* copy far buffer to near buffer */
		 if(err != read)
			break;
		 else
			check += err;
		 data += read;
		 remaining -= read;
	  }
	  while (remaining != 0);
   }
   if ((png_uint_32)check != (png_uint_32)length)
	  png_error(png_ptr, "read Error");
}
#endif
#endif

void PNGAPI
png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
   png_rw_ptr read_data_fn)
{
   if(png_ptr == NULL) return;
   png_ptr->io_ptr = io_ptr;

#if !defined(PNG_NO_STDIO)
   if (read_data_fn != NULL)
	  png_ptr->read_data_fn = read_data_fn;
   else
	  png_ptr->read_data_fn = png_default_read_data;
#else
   png_ptr->read_data_fn = read_data_fn;
#endif

   if (png_ptr->write_data_fn != NULL)
   {
	  png_ptr->write_data_fn = NULL;
	  png_warning(png_ptr,
		 "It's an error to set both read_data_fn and write_data_fn in the ");
	  png_warning(png_ptr,
		 "same structure.  Resetting write_data_fn to NULL.");
   }

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
   png_ptr->output_flush_fn = NULL;
#endif
}
#endif /* PNG_READ_SUPPORTED */
/*** End of inlined file: pngrio.c ***/


/*** Start of inlined file: pngrtran.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED)

void PNGAPI
png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
{
   png_debug(1, "in png_set_crc_action\n");
   if(png_ptr == NULL) return;
   switch (crit_action)
   {
	  case PNG_CRC_NO_CHANGE:			/* leave setting as is */
		 break;
	  case PNG_CRC_WARN_USE:				   /* warn/use data */
		 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
		 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
		 break;
	  case PNG_CRC_QUIET_USE:				 /* quiet/use data */
		 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
		 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
						   PNG_FLAG_CRC_CRITICAL_IGNORE;
		 break;
	  case PNG_CRC_WARN_DISCARD:	/* not a valid action for critical data */
		 png_warning(png_ptr, "Can't discard critical data on CRC error.");
	  case PNG_CRC_ERROR_QUIT:				/* error/quit */
	  case PNG_CRC_DEFAULT:
	  default:
		 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
		 break;
   }

   switch (ancil_action)
   {
	  case PNG_CRC_NO_CHANGE:			   /* leave setting as is */
		 break;
	  case PNG_CRC_WARN_USE:				  /* warn/use data */
		 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
		 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
		 break;
	  case PNG_CRC_QUIET_USE:				/* quiet/use data */
		 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
		 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
						   PNG_FLAG_CRC_ANCILLARY_NOWARN;
		 break;
	  case PNG_CRC_ERROR_QUIT:				   /* error/quit */
		 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
		 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
		 break;
	  case PNG_CRC_WARN_DISCARD:			  /* warn/discard data */
	  case PNG_CRC_DEFAULT:
	  default:
		 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
		 break;
   }
}

#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
	defined(PNG_FLOATING_POINT_SUPPORTED)
void PNGAPI
png_set_background(png_structp png_ptr,
   png_color_16p background_color, int background_gamma_code,
   int need_expand, double background_gamma)
{
   png_debug(1, "in png_set_background\n");
   if(png_ptr == NULL) return;
   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
   {
	  png_warning(png_ptr, "Application must supply a known background gamma");
	  return;
   }

   png_ptr->transformations |= PNG_BACKGROUND;
   png_memcpy(&(png_ptr->background), background_color,
	  png_sizeof(png_color_16));
   png_ptr->background_gamma = (float)background_gamma;
   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
}
#endif

#if defined(PNG_READ_16_TO_8_SUPPORTED)
void PNGAPI
png_set_strip_16(png_structp png_ptr)
{
   png_debug(1, "in png_set_strip_16\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_16_TO_8;
}
#endif

#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
void PNGAPI
png_set_strip_alpha(png_structp png_ptr)
{
   png_debug(1, "in png_set_strip_alpha\n");
   if(png_ptr == NULL) return;
   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
}
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)

typedef struct png_dsort_struct
{
   struct png_dsort_struct FAR * next;
   png_byte left;
   png_byte right;
} png_dsort;
typedef png_dsort FAR *	   png_dsortp;
typedef png_dsort FAR * FAR * png_dsortpp;

void PNGAPI
png_set_dither(png_structp png_ptr, png_colorp palette,
   int num_palette, int maximum_colors, png_uint_16p histogram,
   int full_dither)
{
   png_debug(1, "in png_set_dither\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_DITHER;

   if (!full_dither)
   {
	  int i;

	  png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
		 (png_uint_32)(num_palette * png_sizeof (png_byte)));
	  for (i = 0; i < num_palette; i++)
		 png_ptr->dither_index[i] = (png_byte)i;
   }

   if (num_palette > maximum_colors)
   {
	  if (histogram != NULL)
	  {

		 int i;

		 png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
			(png_uint_32)(num_palette * png_sizeof (png_byte)));

		 for (i = 0; i < num_palette; i++)
			png_ptr->dither_sort[i] = (png_byte)i;

		 for (i = num_palette - 1; i >= maximum_colors; i--)
		 {
			int done; /* to stop early if the list is pre-sorted */
			int j;

			done = 1;
			for (j = 0; j < i; j++)
			{
			   if (histogram[png_ptr->dither_sort[j]]
				   < histogram[png_ptr->dither_sort[j + 1]])
			   {
				  png_byte t;

				  t = png_ptr->dither_sort[j];
				  png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
				  png_ptr->dither_sort[j + 1] = t;
				  done = 0;
			   }
			}
			if (done)
			   break;
		 }

		 if (full_dither)
		 {
			int j = num_palette;

			for (i = 0; i < maximum_colors; i++)
			{
			   if ((int)png_ptr->dither_sort[i] >= maximum_colors)
			   {
				  do
					 j--;
				  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
				  palette[i] = palette[j];
			   }
			}
		 }
		 else
		 {
			int j = num_palette;

			for (i = 0; i < maximum_colors; i++)
			{
			   if ((int)png_ptr->dither_sort[i] >= maximum_colors)
			   {
				  png_color tmp_color;

				  do
					 j--;
				  while ((int)png_ptr->dither_sort[j] >= maximum_colors);

				  tmp_color = palette[j];
				  palette[j] = palette[i];
				  palette[i] = tmp_color;
				  png_ptr->dither_index[j] = (png_byte)i;
				  png_ptr->dither_index[i] = (png_byte)j;
			   }
			}

			for (i = 0; i < num_palette; i++)
			{
			   if ((int)png_ptr->dither_index[i] >= maximum_colors)
			   {
				  int min_d, k, min_k, d_index;

				  d_index = png_ptr->dither_index[i];
				  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
				  for (k = 1, min_k = 0; k < maximum_colors; k++)
				  {
					 int d;

					 d = PNG_COLOR_DIST(palette[d_index], palette[k]);

					 if (d < min_d)
					 {
						min_d = d;
						min_k = k;
					 }
				  }
				  png_ptr->dither_index[i] = (png_byte)min_k;
			   }
			}
		 }
		 png_free(png_ptr, png_ptr->dither_sort);
		 png_ptr->dither_sort=NULL;
	  }
	  else
	  {
		 int i;
		 int max_d;
		 int num_new_palette;
		 png_dsortp t;
		 png_dsortpp hash;

		 t=NULL;

		 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
			(png_uint_32)(num_palette * png_sizeof (png_byte)));
		 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
			(png_uint_32)(num_palette * png_sizeof (png_byte)));

		 for (i = 0; i < num_palette; i++)
		 {
			png_ptr->index_to_palette[i] = (png_byte)i;
			png_ptr->palette_to_index[i] = (png_byte)i;
		 }

		 hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
			png_sizeof (png_dsortp)));
		 for (i = 0; i < 769; i++)
			hash[i] = NULL;

		 num_new_palette = num_palette;

		 max_d = 96;

		 while (num_new_palette > maximum_colors)
		 {
			for (i = 0; i < num_new_palette - 1; i++)
			{
			   int j;

			   for (j = i + 1; j < num_new_palette; j++)
			   {
				  int d;

				  d = PNG_COLOR_DIST(palette[i], palette[j]);

				  if (d <= max_d)
				  {

					 t = (png_dsortp)png_malloc_warn(png_ptr,
						 (png_uint_32)(png_sizeof(png_dsort)));
					 if (t == NULL)
						 break;
					 t->next = hash[d];
					 t->left = (png_byte)i;
					 t->right = (png_byte)j;
					 hash[d] = t;
				  }
			   }
			   if (t == NULL)
				  break;
			}

			if (t != NULL)
			for (i = 0; i <= max_d; i++)
			{
			   if (hash[i] != NULL)
			   {
				  png_dsortp p;

				  for (p = hash[i]; p; p = p->next)
				  {
					 if ((int)png_ptr->index_to_palette[p->left]
						< num_new_palette &&
						(int)png_ptr->index_to_palette[p->right]
						< num_new_palette)
					 {
						int j, next_j;

						if (num_new_palette & 0x01)
						{
						   j = p->left;
						   next_j = p->right;
						}
						else
						{
						   j = p->right;
						   next_j = p->left;
						}

						num_new_palette--;
						palette[png_ptr->index_to_palette[j]]
						  = palette[num_new_palette];
						if (!full_dither)
						{
						   int k;

						   for (k = 0; k < num_palette; k++)
						   {
							  if (png_ptr->dither_index[k] ==
								 png_ptr->index_to_palette[j])
								 png_ptr->dither_index[k] =
									png_ptr->index_to_palette[next_j];
							  if ((int)png_ptr->dither_index[k] ==
								 num_new_palette)
								 png_ptr->dither_index[k] =
									png_ptr->index_to_palette[j];
						   }
						}

						png_ptr->index_to_palette[png_ptr->palette_to_index
						   [num_new_palette]] = png_ptr->index_to_palette[j];
						png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
						   = png_ptr->palette_to_index[num_new_palette];

						png_ptr->index_to_palette[j] = (png_byte)num_new_palette;
						png_ptr->palette_to_index[num_new_palette] = (png_byte)j;
					 }
					 if (num_new_palette <= maximum_colors)
						break;
				  }
				  if (num_new_palette <= maximum_colors)
					 break;
			   }
			}

			for (i = 0; i < 769; i++)
			{
			   if (hash[i] != NULL)
			   {
				  png_dsortp p = hash[i];
				  while (p)
				  {
					 t = p->next;
					 png_free(png_ptr, p);
					 p = t;
				  }
			   }
			   hash[i] = 0;
			}
			max_d += 96;
		 }
		 png_free(png_ptr, hash);
		 png_free(png_ptr, png_ptr->palette_to_index);
		 png_free(png_ptr, png_ptr->index_to_palette);
		 png_ptr->palette_to_index=NULL;
		 png_ptr->index_to_palette=NULL;
	  }
	  num_palette = maximum_colors;
   }
   if (png_ptr->palette == NULL)
   {
	  png_ptr->palette = palette;
   }
   png_ptr->num_palette = (png_uint_16)num_palette;

   if (full_dither)
   {
	  int i;
	  png_bytep distance;
	  int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
		 PNG_DITHER_BLUE_BITS;
	  int num_red = (1 << PNG_DITHER_RED_BITS);
	  int num_green = (1 << PNG_DITHER_GREEN_BITS);
	  int num_blue = (1 << PNG_DITHER_BLUE_BITS);
	  png_size_t num_entries = ((png_size_t)1 << total_bits);

	  png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr,
		 (png_uint_32)(num_entries * png_sizeof (png_byte)));

	  png_memset(png_ptr->palette_lookup, 0, num_entries *
		 png_sizeof (png_byte));

	  distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
		 png_sizeof(png_byte)));

	  png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));

	  for (i = 0; i < num_palette; i++)
	  {
		 int ir, ig, ib;
		 int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
		 int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
		 int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));

		 for (ir = 0; ir < num_red; ir++)
		 {
			int dr = ((ir > r) ? ir - r : r - ir);
			int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));

			for (ig = 0; ig < num_green; ig++)
			{
			   int dg = ((ig > g) ? ig - g : g - ig);
			   int dt = dr + dg;
			   int dm = ((dr > dg) ? dr : dg);
			   int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);

			   for (ib = 0; ib < num_blue; ib++)
			   {
				  int d_index = index_g | ib;
				  int db = ((ib > b) ? ib - b : b - ib);
				  int dmax = ((dm > db) ? dm : db);
				  int d = dmax + dt + db;

				  if (d < (int)distance[d_index])
				  {
					 distance[d_index] = (png_byte)d;
					 png_ptr->palette_lookup[d_index] = (png_byte)i;
				  }
			   }
			}
		 }
	  }

	  png_free(png_ptr, distance);
   }
}
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
void PNGAPI
png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
{
   png_debug(1, "in png_set_gamma\n");
   if(png_ptr == NULL) return;
   if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
	   (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
	   (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
	 png_ptr->transformations |= PNG_GAMMA;
   png_ptr->gamma = (float)file_gamma;
   png_ptr->screen_gamma = (float)scrn_gamma;
}
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
void PNGAPI
png_set_expand(png_structp png_ptr)
{
   png_debug(1, "in png_set_expand\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
#ifdef PNG_WARN_UNINITIALIZED_ROW
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
#endif
}

void PNGAPI
png_set_palette_to_rgb(png_structp png_ptr)
{
   png_debug(1, "in png_set_palette_to_rgb\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
#ifdef PNG_WARN_UNINITIALIZED_ROW
   png_ptr->flags &= !(PNG_FLAG_ROW_INIT);
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
#endif
}

#if !defined(PNG_1_0_X)
void PNGAPI
png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
{
   png_debug(1, "in png_set_expand_gray_1_2_4_to_8\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_EXPAND;
#ifdef PNG_WARN_UNINITIALIZED_ROW
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
#endif
}
#endif

#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
void PNGAPI
png_set_gray_1_2_4_to_8(png_structp png_ptr)
{
   png_debug(1, "in png_set_gray_1_2_4_to_8\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
}
#endif

void PNGAPI
png_set_tRNS_to_alpha(png_structp png_ptr)
{
   png_debug(1, "in png_set_tRNS_to_alpha\n");
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
#ifdef PNG_WARN_UNINITIALIZED_ROW
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
#endif
}
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
void PNGAPI
png_set_gray_to_rgb(png_structp png_ptr)
{
   png_debug(1, "in png_set_gray_to_rgb\n");
   png_ptr->transformations |= PNG_GRAY_TO_RGB;
#ifdef PNG_WARN_UNINITIALIZED_ROW
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
#endif
}
#endif

#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
#if defined(PNG_FLOATING_POINT_SUPPORTED)

void PNGAPI
png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
   double green)
{
	  int red_fixed = (int)((float)red*100000.0 + 0.5);
	  int green_fixed = (int)((float)green*100000.0 + 0.5);
	  if(png_ptr == NULL) return;
	  png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
}
#endif

void PNGAPI
png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
   png_fixed_point red, png_fixed_point green)
{
   png_debug(1, "in png_set_rgb_to_gray\n");
   if(png_ptr == NULL) return;
   switch(error_action)
   {
	  case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
			  break;
	  case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
			  break;
	  case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
   }
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
#if defined(PNG_READ_EXPAND_SUPPORTED)
	  png_ptr->transformations |= PNG_EXPAND;
#else
   {
	  png_warning(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
	  png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
   }
#endif
   {
	  png_uint_16 red_int, green_int;
	  if(red < 0 || green < 0)
	  {
		 red_int   =  6968; /* .212671 * 32768 + .5 */
		 green_int = 23434; /* .715160 * 32768 + .5 */
	  }
	  else if(red + green < 100000L)
	  {
		red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
		green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
	  }
	  else
	  {
		 png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
		 red_int   =  6968;
		 green_int = 23434;
	  }
	  png_ptr->rgb_to_gray_red_coeff   = red_int;
	  png_ptr->rgb_to_gray_green_coeff = green_int;
	  png_ptr->rgb_to_gray_blue_coeff  = (png_uint_16)(32768-red_int-green_int);
   }
}
#endif

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_LEGACY_SUPPORTED)
void PNGAPI
png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
   read_user_transform_fn)
{
   png_debug(1, "in png_set_read_user_transform_fn\n");
   if(png_ptr == NULL) return;
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
   png_ptr->transformations |= PNG_USER_TRANSFORM;
   png_ptr->read_user_transform_fn = read_user_transform_fn;
#endif
#ifdef PNG_LEGACY_SUPPORTED
   if(read_user_transform_fn)
	  png_warning(png_ptr,
		"This version of libpng does not support user transforms");
#endif
}
#endif

void /* PRIVATE */
png_init_read_transformations(png_structp png_ptr)
{
   png_debug(1, "in png_init_read_transformations\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if(png_ptr != NULL)
#endif
  {
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
 || defined(PNG_READ_GAMMA_SUPPORTED)
   int color_type = png_ptr->color_type;
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
	   !(color_type & PNG_COLOR_MASK_COLOR))
   {
		  png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
   } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
			  !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
			  (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
			  png_ptr->background.red == png_ptr->background.green &&
			  png_ptr->background.red == png_ptr->background.blue)
   {
		  png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
		  png_ptr->background.gray = png_ptr->background.red;
   }
#endif

   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
	   (png_ptr->transformations & PNG_EXPAND))
   {
	  if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
	  {
		 switch (png_ptr->bit_depth)
		 {
			case 1:
			   png_ptr->background.gray *= (png_uint_16)0xff;
			   png_ptr->background.red = png_ptr->background.green
				 =  png_ptr->background.blue = png_ptr->background.gray;
			   if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
			   {
				 png_ptr->trans_values.gray *= (png_uint_16)0xff;
				 png_ptr->trans_values.red = png_ptr->trans_values.green
				   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
			   }
			   break;
			case 2:
			   png_ptr->background.gray *= (png_uint_16)0x55;
			   png_ptr->background.red = png_ptr->background.green
				 = png_ptr->background.blue = png_ptr->background.gray;
			   if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
			   {
				 png_ptr->trans_values.gray *= (png_uint_16)0x55;
				 png_ptr->trans_values.red = png_ptr->trans_values.green
				   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
			   }
			   break;
			case 4:
			   png_ptr->background.gray *= (png_uint_16)0x11;
			   png_ptr->background.red = png_ptr->background.green
				 = png_ptr->background.blue = png_ptr->background.gray;
			   if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
			   {
				 png_ptr->trans_values.gray *= (png_uint_16)0x11;
				 png_ptr->trans_values.red = png_ptr->trans_values.green
				   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
			   }
			   break;
			case 8:
			case 16:
			   png_ptr->background.red = png_ptr->background.green
				 = png_ptr->background.blue = png_ptr->background.gray;
			   break;
		 }
	  }
	  else if (color_type == PNG_COLOR_TYPE_PALETTE)
	  {
		 png_ptr->background.red   =
			png_ptr->palette[png_ptr->background.index].red;
		 png_ptr->background.green =
			png_ptr->palette[png_ptr->background.index].green;
		 png_ptr->background.blue  =
			png_ptr->palette[png_ptr->background.index].blue;

#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
		if (png_ptr->transformations & PNG_INVERT_ALPHA)
		{
#if defined(PNG_READ_EXPAND_SUPPORTED)
		   if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
#endif
		   {
			  int i,istop;
			  istop=(int)png_ptr->num_trans;
			  for (i=0; i<istop; i++)
				 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
		   }
		}
#endif

	  }
   }
#endif

#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
   png_ptr->background_1 = png_ptr->background;
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)

   if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
	   && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
		 < PNG_GAMMA_THRESHOLD))
   {
	int i,k;
	k=0;
	for (i=0; i<png_ptr->num_trans; i++)
	{
	  if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
		k=1; /* partial transparency is present */
	}
	if (k == 0)
	  png_ptr->transformations &= (~PNG_GAMMA);
   }

   if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
		png_ptr->gamma != 0.0)
   {
	  png_build_gamma_table(png_ptr);
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
	  if (png_ptr->transformations & PNG_BACKGROUND)
	  {
		 if (color_type == PNG_COLOR_TYPE_PALETTE)
		 {
			png_color back, back_1;
			png_colorp palette = png_ptr->palette;
			int num_palette = png_ptr->num_palette;
			int i;
			if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
			{
			   back.red = png_ptr->gamma_table[png_ptr->background.red];
			   back.green = png_ptr->gamma_table[png_ptr->background.green];
			   back.blue = png_ptr->gamma_table[png_ptr->background.blue];

			   back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
			   back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
			   back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
			}
			else
			{
			   double g, gs;

			   switch (png_ptr->background_gamma_type)
			   {
				  case PNG_BACKGROUND_GAMMA_SCREEN:
					 g = (png_ptr->screen_gamma);
					 gs = 1.0;
					 break;
				  case PNG_BACKGROUND_GAMMA_FILE:
					 g = 1.0 / (png_ptr->gamma);
					 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
					 break;
				  case PNG_BACKGROUND_GAMMA_UNIQUE:
					 g = 1.0 / (png_ptr->background_gamma);
					 gs = 1.0 / (png_ptr->background_gamma *
								 png_ptr->screen_gamma);
					 break;
				  default:
					 g = 1.0;	/* back_1 */
					 gs = 1.0;   /* back */
			   }

			   if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
			   {
				  back.red   = (png_byte)png_ptr->background.red;
				  back.green = (png_byte)png_ptr->background.green;
				  back.blue  = (png_byte)png_ptr->background.blue;
			   }
			   else
			   {
				  back.red = (png_byte)(pow(
					 (double)png_ptr->background.red/255, gs) * 255.0 + .5);
				  back.green = (png_byte)(pow(
					 (double)png_ptr->background.green/255, gs) * 255.0 + .5);
				  back.blue = (png_byte)(pow(
					 (double)png_ptr->background.blue/255, gs) * 255.0 + .5);
			   }

			   back_1.red = (png_byte)(pow(
				  (double)png_ptr->background.red/255, g) * 255.0 + .5);
			   back_1.green = (png_byte)(pow(
				  (double)png_ptr->background.green/255, g) * 255.0 + .5);
			   back_1.blue = (png_byte)(pow(
				  (double)png_ptr->background.blue/255, g) * 255.0 + .5);
			}
			for (i = 0; i < num_palette; i++)
			{
			   if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
			   {
				  if (png_ptr->trans[i] == 0)
				  {
					 palette[i] = back;
				  }
				  else /* if (png_ptr->trans[i] != 0xff) */
				  {
					 png_byte v, w;

					 v = png_ptr->gamma_to_1[palette[i].red];
					 png_composite(w, v, png_ptr->trans[i], back_1.red);
					 palette[i].red = png_ptr->gamma_from_1[w];

					 v = png_ptr->gamma_to_1[palette[i].green];
					 png_composite(w, v, png_ptr->trans[i], back_1.green);
					 palette[i].green = png_ptr->gamma_from_1[w];

					 v = png_ptr->gamma_to_1[palette[i].blue];
					 png_composite(w, v, png_ptr->trans[i], back_1.blue);
					 palette[i].blue = png_ptr->gamma_from_1[w];
				  }
			   }
			   else
			   {
				  palette[i].red = png_ptr->gamma_table[palette[i].red];
				  palette[i].green = png_ptr->gamma_table[palette[i].green];
				  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
			   }
			}
		 }
		 else
		 {
			double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
			double g = 1.0;
			double gs = 1.0;

			switch (png_ptr->background_gamma_type)
			{
			   case PNG_BACKGROUND_GAMMA_SCREEN:
				  g = (png_ptr->screen_gamma);
				  gs = 1.0;
				  break;
			   case PNG_BACKGROUND_GAMMA_FILE:
				  g = 1.0 / (png_ptr->gamma);
				  gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
				  break;
			   case PNG_BACKGROUND_GAMMA_UNIQUE:
				  g = 1.0 / (png_ptr->background_gamma);
				  gs = 1.0 / (png_ptr->background_gamma *
					 png_ptr->screen_gamma);
				  break;
			}

			png_ptr->background_1.gray = (png_uint_16)(pow(
			   (double)png_ptr->background.gray / m, g) * m + .5);
			png_ptr->background.gray = (png_uint_16)(pow(
			   (double)png_ptr->background.gray / m, gs) * m + .5);

			if ((png_ptr->background.red != png_ptr->background.green) ||
				(png_ptr->background.red != png_ptr->background.blue) ||
				(png_ptr->background.red != png_ptr->background.gray))
			{
			   png_ptr->background_1.red = (png_uint_16)(pow(
				  (double)png_ptr->background.red / m, g) * m + .5);
			   png_ptr->background_1.green = (png_uint_16)(pow(
				  (double)png_ptr->background.green / m, g) * m + .5);
			   png_ptr->background_1.blue = (png_uint_16)(pow(
				  (double)png_ptr->background.blue / m, g) * m + .5);
			   png_ptr->background.red = (png_uint_16)(pow(
				  (double)png_ptr->background.red / m, gs) * m + .5);
			   png_ptr->background.green = (png_uint_16)(pow(
				  (double)png_ptr->background.green / m, gs) * m + .5);
			   png_ptr->background.blue = (png_uint_16)(pow(
				  (double)png_ptr->background.blue / m, gs) * m + .5);
			}
			else
			{
			   png_ptr->background_1.red = png_ptr->background_1.green
				 = png_ptr->background_1.blue = png_ptr->background_1.gray;
			   png_ptr->background.red = png_ptr->background.green
				 = png_ptr->background.blue = png_ptr->background.gray;
			}
		 }
	  }
	  else
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
	  if (color_type == PNG_COLOR_TYPE_PALETTE)
	  {
		 png_colorp palette = png_ptr->palette;
		 int num_palette = png_ptr->num_palette;
		 int i;

		 for (i = 0; i < num_palette; i++)
		 {
			palette[i].red = png_ptr->gamma_table[palette[i].red];
			palette[i].green = png_ptr->gamma_table[palette[i].green];
			palette[i].blue = png_ptr->gamma_table[palette[i].blue];
		 }
	  }
   }
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   else
#endif
#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
	   (color_type == PNG_COLOR_TYPE_PALETTE))
   {
	  int i;
	  int istop = (int)png_ptr->num_trans;
	  png_color back;
	  png_colorp palette = png_ptr->palette;

	  back.red   = (png_byte)png_ptr->background.red;
	  back.green = (png_byte)png_ptr->background.green;
	  back.blue  = (png_byte)png_ptr->background.blue;

	  for (i = 0; i < istop; i++)
	  {
		 if (png_ptr->trans[i] == 0)
		 {
			palette[i] = back;
		 }
		 else if (png_ptr->trans[i] != 0xff)
		 {
			png_composite(palette[i].red, palette[i].red,
			   png_ptr->trans[i], back.red);
			png_composite(palette[i].green, palette[i].green,
			   png_ptr->trans[i], back.green);
			png_composite(palette[i].blue, palette[i].blue,
			   png_ptr->trans[i], back.blue);
		 }
	  }
   }
#endif /* PNG_READ_BACKGROUND_SUPPORTED */

#if defined(PNG_READ_SHIFT_SUPPORTED)
   if ((png_ptr->transformations & PNG_SHIFT) &&
	  (color_type == PNG_COLOR_TYPE_PALETTE))
   {
	  png_uint_16 i;
	  png_uint_16 istop = png_ptr->num_palette;
	  int sr = 8 - png_ptr->sig_bit.red;
	  int sg = 8 - png_ptr->sig_bit.green;
	  int sb = 8 - png_ptr->sig_bit.blue;

	  if (sr < 0 || sr > 8)
		 sr = 0;
	  if (sg < 0 || sg > 8)
		 sg = 0;
	  if (sb < 0 || sb > 8)
		 sb = 0;
	  for (i = 0; i < istop; i++)
	  {
		 png_ptr->palette[i].red >>= sr;
		 png_ptr->palette[i].green >>= sg;
		 png_ptr->palette[i].blue >>= sb;
	  }
   }
#endif  /* PNG_READ_SHIFT_SUPPORTED */
 }
#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
   if(png_ptr)
	  return;
#endif
}

void /* PRIVATE */
png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
{
   png_debug(1, "in png_read_transform_info\n");
#if defined(PNG_READ_EXPAND_SUPPORTED)
   if (png_ptr->transformations & PNG_EXPAND)
   {
	  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  {
		 if (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND_tRNS))
			info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
		 else
			info_ptr->color_type = PNG_COLOR_TYPE_RGB;
		 info_ptr->bit_depth = 8;
		 info_ptr->num_trans = 0;
	  }
	  else
	  {
		 if (png_ptr->num_trans)
		 {
			if (png_ptr->transformations & PNG_EXPAND_tRNS)
			  info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
			else
			  info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
		 }
		 if (info_ptr->bit_depth < 8)
			info_ptr->bit_depth = 8;
		 info_ptr->num_trans = 0;
	  }
   }
#endif

#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   if (png_ptr->transformations & PNG_BACKGROUND)
   {
	  info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
	  info_ptr->num_trans = 0;
	  info_ptr->background = png_ptr->background;
   }
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED)
   if (png_ptr->transformations & PNG_GAMMA)
   {
#ifdef PNG_FLOATING_POINT_SUPPORTED
	  info_ptr->gamma = png_ptr->gamma;
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
	  info_ptr->int_gamma = png_ptr->int_gamma;
#endif
   }
#endif

#if defined(PNG_READ_16_TO_8_SUPPORTED)
   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
	  info_ptr->bit_depth = 8;
#endif

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
	  info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
#endif

#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
	  info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
   if (png_ptr->transformations & PNG_DITHER)
   {
	  if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
		 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
		 png_ptr->palette_lookup && info_ptr->bit_depth == 8)
	  {
		 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
	  }
   }
#endif

#if defined(PNG_READ_PACK_SUPPORTED)
   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
	  info_ptr->bit_depth = 8;
#endif

   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  info_ptr->channels = 1;
   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
	  info_ptr->channels = 3;
   else
	  info_ptr->channels = 1;

#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
	  info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
#endif

   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
	  info_ptr->channels++;

#if defined(PNG_READ_FILLER_SUPPORTED)
   if ((png_ptr->transformations & PNG_FILLER) &&
	   ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
	   (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
   {
	  info_ptr->channels++;
#if !defined(PNG_1_0_X)
	  if (png_ptr->transformations & PNG_ADD_ALPHA)
		info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
#endif
   }
#endif

#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
   if(png_ptr->transformations & PNG_USER_TRANSFORM)
	 {
	   if(info_ptr->bit_depth < png_ptr->user_transform_depth)
		 info_ptr->bit_depth = png_ptr->user_transform_depth;
	   if(info_ptr->channels < png_ptr->user_transform_channels)
		 info_ptr->channels = png_ptr->user_transform_channels;
	 }
#endif

   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
	  info_ptr->bit_depth);

   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,info_ptr->width);

#if !defined(PNG_READ_EXPAND_SUPPORTED)
   if(png_ptr)
	  return;
#endif
}

void /* PRIVATE */
png_do_read_transformations(png_structp png_ptr)
{
   png_debug(1, "in png_do_read_transformations\n");
   if (png_ptr->row_buf == NULL)
   {
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
	  char msg[50];

	  png_snprintf2(msg, 50,
		 "NULL row buffer for row %ld, pass %d", png_ptr->row_number,
		 png_ptr->pass);
	  png_error(png_ptr, msg);
#else
	  png_error(png_ptr, "NULL row buffer");
#endif
   }
#ifdef PNG_WARN_UNINITIALIZED_ROW
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
#if (PNG_WARN_UNINITIALIZED_ROW==1)
	  png_error(png_ptr, "Uninitialized row");
#else
	  png_warning(png_ptr, "Uninitialized row");
#endif
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
   if (png_ptr->transformations & PNG_EXPAND)
   {
	  if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
	  {
		 png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
			png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
	  }
	  else
	  {
		 if (png_ptr->num_trans &&
			 (png_ptr->transformations & PNG_EXPAND_tRNS))
			png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
			   &(png_ptr->trans_values));
		 else
			png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
			   NULL);
	  }
   }
#endif

#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
	  png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
		 PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
#endif

#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
   {
	  int rgb_error =
		 png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1);
	  if(rgb_error)
	  {
		 png_ptr->rgb_to_gray_status=1;
		 if((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
			 PNG_RGB_TO_GRAY_WARN)
			png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
		 if((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
			 PNG_RGB_TO_GRAY_ERR)
			png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
	  }
   }
#endif

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
	   !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
	  png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
	  ((png_ptr->num_trans != 0 ) ||
	  (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
	  png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
		 &(png_ptr->trans_values), &(png_ptr->background)
#if defined(PNG_READ_GAMMA_SUPPORTED)
		 , &(png_ptr->background_1),
		 png_ptr->gamma_table, png_ptr->gamma_from_1,
		 png_ptr->gamma_to_1, png_ptr->gamma_16_table,
		 png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
		 png_ptr->gamma_shift
#endif
);
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED)
   if ((png_ptr->transformations & PNG_GAMMA) &&
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
	  !((png_ptr->transformations & PNG_BACKGROUND) &&
	  ((png_ptr->num_trans != 0) ||
	  (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
#endif
	  (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
	  png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
		 png_ptr->gamma_table, png_ptr->gamma_16_table,
		 png_ptr->gamma_shift);
#endif

#if defined(PNG_READ_16_TO_8_SUPPORTED)
   if (png_ptr->transformations & PNG_16_TO_8)
	  png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
   if (png_ptr->transformations & PNG_DITHER)
   {
	  png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
		 png_ptr->palette_lookup, png_ptr->dither_index);
	  if(png_ptr->row_info.rowbytes == (png_uint_32)0)
		 png_error(png_ptr, "png_do_dither returned rowbytes=0");
   }
#endif

#if defined(PNG_READ_INVERT_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_MONO)
	  png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED)
   if (png_ptr->transformations & PNG_SHIFT)
	  png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
		 &(png_ptr->shift));
#endif

#if defined(PNG_READ_PACK_SUPPORTED)
   if (png_ptr->transformations & PNG_PACK)
	  png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_BGR_SUPPORTED)
   if (png_ptr->transformations & PNG_BGR)
	  png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_PACKSWAP)
	  png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
	   (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
	  png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
	  png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
		 (png_uint_32)png_ptr->filler, png_ptr->flags);
#endif

#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_ALPHA)
	  png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_ALPHA)
	  png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
	  png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
	{
	  if(png_ptr->read_user_transform_fn != NULL)
		(*(png_ptr->read_user_transform_fn)) /* user read transform function */
		  (png_ptr,			/* png_ptr */
		   &(png_ptr->row_info),	   /* row_info:	 */
		   png_ptr->row_buf + 1);	  /* start of pixel data for row */
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
	  if(png_ptr->user_transform_depth)
		 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
	  if(png_ptr->user_transform_channels)
		 png_ptr->row_info.channels = png_ptr->user_transform_channels;
#endif
	  png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
		 png_ptr->row_info.channels);
	  png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
		 png_ptr->row_info.width);
   }
#endif

}

#if defined(PNG_READ_PACK_SUPPORTED)
void /* PRIVATE */
png_do_unpack(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_unpack\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
#else
   if (row_info->bit_depth < 8)
#endif
   {
	  png_uint_32 i;
	  png_uint_32 row_width=row_info->width;

	  switch (row_info->bit_depth)
	  {
		 case 1:
		 {
			png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
			png_bytep dp = row + (png_size_t)row_width - 1;
			png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
			for (i = 0; i < row_width; i++)
			{
			   *dp = (png_byte)((*sp >> shift) & 0x01);
			   if (shift == 7)
			   {
				  shift = 0;
				  sp--;
			   }
			   else
				  shift++;

			   dp--;
			}
			break;
		 }
		 case 2:
		 {

			png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
			png_bytep dp = row + (png_size_t)row_width - 1;
			png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
			for (i = 0; i < row_width; i++)
			{
			   *dp = (png_byte)((*sp >> shift) & 0x03);
			   if (shift == 6)
			   {
				  shift = 0;
				  sp--;
			   }
			   else
				  shift += 2;

			   dp--;
			}
			break;
		 }
		 case 4:
		 {
			png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
			png_bytep dp = row + (png_size_t)row_width - 1;
			png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
			for (i = 0; i < row_width; i++)
			{
			   *dp = (png_byte)((*sp >> shift) & 0x0f);
			   if (shift == 4)
			   {
				  shift = 0;
				  sp--;
			   }
			   else
				  shift = 4;

			   dp--;
			}
			break;
		 }
	  }
	  row_info->bit_depth = 8;
	  row_info->pixel_depth = (png_byte)(8 * row_info->channels);
	  row_info->rowbytes = row_width * row_info->channels;
   }
}
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED)
void /* PRIVATE */
png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
{
   png_debug(1, "in png_do_unshift\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL && sig_bits != NULL &&
#endif
	   row_info->color_type != PNG_COLOR_TYPE_PALETTE)
   {
	  int shift[4];
	  int channels = 0;
	  int c;
	  png_uint_16 value = 0;
	  png_uint_32 row_width = row_info->width;

	  if (row_info->color_type & PNG_COLOR_MASK_COLOR)
	  {
		 shift[channels++] = row_info->bit_depth - sig_bits->red;
		 shift[channels++] = row_info->bit_depth - sig_bits->green;
		 shift[channels++] = row_info->bit_depth - sig_bits->blue;
	  }
	  else
	  {
		 shift[channels++] = row_info->bit_depth - sig_bits->gray;
	  }
	  if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
	  {
		 shift[channels++] = row_info->bit_depth - sig_bits->alpha;
	  }

	  for (c = 0; c < channels; c++)
	  {
		 if (shift[c] <= 0)
			shift[c] = 0;
		 else
			value = 1;
	  }

	  if (!value)
		 return;

	  switch (row_info->bit_depth)
	  {
		 case 2:
		 {
			png_bytep bp;
			png_uint_32 i;
			png_uint_32 istop = row_info->rowbytes;

			for (bp = row, i = 0; i < istop; i++)
			{
			   *bp >>= 1;
			   *bp++ &= 0x55;
			}
			break;
		 }
		 case 4:
		 {
			png_bytep bp = row;
			png_uint_32 i;
			png_uint_32 istop = row_info->rowbytes;
			png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
			   (png_byte)((int)0xf >> shift[0]));

			for (i = 0; i < istop; i++)
			{
			   *bp >>= shift[0];
			   *bp++ &= mask;
			}
			break;
		 }
		 case 8:
		 {
			png_bytep bp = row;
			png_uint_32 i;
			png_uint_32 istop = row_width * channels;

			for (i = 0; i < istop; i++)
			{
			   *bp++ >>= shift[i%channels];
			}
			break;
		 }
		 case 16:
		 {
			png_bytep bp = row;
			png_uint_32 i;
			png_uint_32 istop = channels * row_width;

			for (i = 0; i < istop; i++)
			{
			   value = (png_uint_16)((*bp << 8) + *(bp + 1));
			   value >>= shift[i%channels];
			   *bp++ = (png_byte)(value >> 8);
			   *bp++ = (png_byte)(value & 0xff);
			}
			break;
		 }
	  }
   }
}
#endif

#if defined(PNG_READ_16_TO_8_SUPPORTED)
void /* PRIVATE */
png_do_chop(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_chop\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
#else
   if (row_info->bit_depth == 16)
#endif
   {
	  png_bytep sp = row;
	  png_bytep dp = row;
	  png_uint_32 i;
	  png_uint_32 istop = row_info->width * row_info->channels;

	  for (i = 0; i<istop; i++, sp += 2, dp++)
	  {
#if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)

		 *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
#else
		 *dp = *sp;
#endif
	  }
	  row_info->bit_depth = 8;
	  row_info->pixel_depth = (png_byte)(8 * row_info->channels);
	  row_info->rowbytes = row_info->width * row_info->channels;
   }
}
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
void /* PRIVATE */
png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_read_swap_alpha\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
	  png_uint_32 row_width = row_info->width;
	  if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp = row + row_info->rowbytes;
			png_bytep dp = sp;
			png_byte save;
			png_uint_32 i;

			for (i = 0; i < row_width; i++)
			{
			   save = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = save;
			}
		 }
		 else
		 {
			png_bytep sp = row + row_info->rowbytes;
			png_bytep dp = sp;
			png_byte save[2];
			png_uint_32 i;

			for (i = 0; i < row_width; i++)
			{
			   save[0] = *(--sp);
			   save[1] = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = save[0];
			   *(--dp) = save[1];
			}
		 }
	  }
	  else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp = row + row_info->rowbytes;
			png_bytep dp = sp;
			png_byte save;
			png_uint_32 i;

			for (i = 0; i < row_width; i++)
			{
			   save = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = save;
			}
		 }
		 else
		 {
			png_bytep sp = row + row_info->rowbytes;
			png_bytep dp = sp;
			png_byte save[2];
			png_uint_32 i;

			for (i = 0; i < row_width; i++)
			{
			   save[0] = *(--sp);
			   save[1] = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = save[0];
			   *(--dp) = save[1];
			}
		 }
	  }
   }
}
#endif

#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
void /* PRIVATE */
png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_read_invert_alpha\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
	  png_uint_32 row_width = row_info->width;
	  if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp = row + row_info->rowbytes;
			png_bytep dp = sp;
			png_uint_32 i;

			for (i = 0; i < row_width; i++)
			{
			   *(--dp) = (png_byte)(255 - *(--sp));

			   sp-=3;
			   dp=sp;
			}
		 }
		 else
		 {
			png_bytep sp = row + row_info->rowbytes;
			png_bytep dp = sp;
			png_uint_32 i;

			for (i = 0; i < row_width; i++)
			{
			   *(--dp) = (png_byte)(255 - *(--sp));
			   *(--dp) = (png_byte)(255 - *(--sp));

			   sp-=6;
			   dp=sp;
			}
		 }
	  }
	  else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp = row + row_info->rowbytes;
			png_bytep dp = sp;
			png_uint_32 i;

			for (i = 0; i < row_width; i++)
			{
			   *(--dp) = (png_byte)(255 - *(--sp));
			   *(--dp) = *(--sp);
			}
		 }
		 else
		 {
			png_bytep sp  = row + row_info->rowbytes;
			png_bytep dp = sp;
			png_uint_32 i;

			for (i = 0; i < row_width; i++)
			{
			   *(--dp) = (png_byte)(255 - *(--sp));
			   *(--dp) = (png_byte)(255 - *(--sp));
			   sp-=2;
			   dp=sp;
			}
		 }
	  }
   }
}
#endif

#if defined(PNG_READ_FILLER_SUPPORTED)
void /* PRIVATE */
png_do_read_filler(png_row_infop row_info, png_bytep row,
   png_uint_32 filler, png_uint_32 flags)
{
   png_uint_32 i;
   png_uint_32 row_width = row_info->width;

   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
   png_byte lo_filler = (png_byte)(filler & 0xff);

   png_debug(1, "in png_do_read_filler\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL  && row_info != NULL &&
#endif
	   row_info->color_type == PNG_COLOR_TYPE_GRAY)
   {
	  if(row_info->bit_depth == 8)
	  {
		 if (flags & PNG_FLAG_FILLER_AFTER)
		 {
			png_bytep sp = row + (png_size_t)row_width;
			png_bytep dp =  sp + (png_size_t)row_width;
			for (i = 1; i < row_width; i++)
			{
			   *(--dp) = lo_filler;
			   *(--dp) = *(--sp);
			}
			*(--dp) = lo_filler;
			row_info->channels = 2;
			row_info->pixel_depth = 16;
			row_info->rowbytes = row_width * 2;
		 }
		 else
		 {
			png_bytep sp = row + (png_size_t)row_width;
			png_bytep dp = sp  + (png_size_t)row_width;
			for (i = 0; i < row_width; i++)
			{
			   *(--dp) = *(--sp);
			   *(--dp) = lo_filler;
			}
			row_info->channels = 2;
			row_info->pixel_depth = 16;
			row_info->rowbytes = row_width * 2;
		 }
	  }
	  else if(row_info->bit_depth == 16)
	  {
		 if (flags & PNG_FLAG_FILLER_AFTER)
		 {
			png_bytep sp = row + (png_size_t)row_width * 2;
			png_bytep dp = sp  + (png_size_t)row_width * 2;
			for (i = 1; i < row_width; i++)
			{
			   *(--dp) = hi_filler;
			   *(--dp) = lo_filler;
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			}
			*(--dp) = hi_filler;
			*(--dp) = lo_filler;
			row_info->channels = 2;
			row_info->pixel_depth = 32;
			row_info->rowbytes = row_width * 4;
		 }
		 else
		 {
			png_bytep sp = row + (png_size_t)row_width * 2;
			png_bytep dp = sp  + (png_size_t)row_width * 2;
			for (i = 0; i < row_width; i++)
			{
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = hi_filler;
			   *(--dp) = lo_filler;
			}
			row_info->channels = 2;
			row_info->pixel_depth = 32;
			row_info->rowbytes = row_width * 4;
		 }
	  }
   } /* COLOR_TYPE == GRAY */
   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
   {
	  if(row_info->bit_depth == 8)
	  {
		 if (flags & PNG_FLAG_FILLER_AFTER)
		 {
			png_bytep sp = row + (png_size_t)row_width * 3;
			png_bytep dp = sp  + (png_size_t)row_width;
			for (i = 1; i < row_width; i++)
			{
			   *(--dp) = lo_filler;
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			}
			*(--dp) = lo_filler;
			row_info->channels = 4;
			row_info->pixel_depth = 32;
			row_info->rowbytes = row_width * 4;
		 }
		 else
		 {
			png_bytep sp = row + (png_size_t)row_width * 3;
			png_bytep dp = sp + (png_size_t)row_width;
			for (i = 0; i < row_width; i++)
			{
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = lo_filler;
			}
			row_info->channels = 4;
			row_info->pixel_depth = 32;
			row_info->rowbytes = row_width * 4;
		 }
	  }
	  else if(row_info->bit_depth == 16)
	  {
		 if (flags & PNG_FLAG_FILLER_AFTER)
		 {
			png_bytep sp = row + (png_size_t)row_width * 6;
			png_bytep dp = sp  + (png_size_t)row_width * 2;
			for (i = 1; i < row_width; i++)
			{
			   *(--dp) = hi_filler;
			   *(--dp) = lo_filler;
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			}
			*(--dp) = hi_filler;
			*(--dp) = lo_filler;
			row_info->channels = 4;
			row_info->pixel_depth = 64;
			row_info->rowbytes = row_width * 8;
		 }
		 else
		 {
			png_bytep sp = row + (png_size_t)row_width * 6;
			png_bytep dp = sp  + (png_size_t)row_width * 2;
			for (i = 0; i < row_width; i++)
			{
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = *(--sp);
			   *(--dp) = hi_filler;
			   *(--dp) = lo_filler;
			}
			row_info->channels = 4;
			row_info->pixel_depth = 64;
			row_info->rowbytes = row_width * 8;
		 }
	  }
   } /* COLOR_TYPE == RGB */
}
#endif

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
void /* PRIVATE */
png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
{
   png_uint_32 i;
   png_uint_32 row_width = row_info->width;

   png_debug(1, "in png_do_gray_to_rgb\n");
   if (row_info->bit_depth >= 8 &&
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	  !(row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
	  if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp = row + (png_size_t)row_width - 1;
			png_bytep dp = sp  + (png_size_t)row_width * 2;
			for (i = 0; i < row_width; i++)
			{
			   *(dp--) = *sp;
			   *(dp--) = *sp;
			   *(dp--) = *(sp--);
			}
		 }
		 else
		 {
			png_bytep sp = row + (png_size_t)row_width * 2 - 1;
			png_bytep dp = sp  + (png_size_t)row_width * 4;
			for (i = 0; i < row_width; i++)
			{
			   *(dp--) = *sp;
			   *(dp--) = *(sp - 1);
			   *(dp--) = *sp;
			   *(dp--) = *(sp - 1);
			   *(dp--) = *(sp--);
			   *(dp--) = *(sp--);
			}
		 }
	  }
	  else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp = row + (png_size_t)row_width * 2 - 1;
			png_bytep dp = sp  + (png_size_t)row_width * 2;
			for (i = 0; i < row_width; i++)
			{
			   *(dp--) = *(sp--);
			   *(dp--) = *sp;
			   *(dp--) = *sp;
			   *(dp--) = *(sp--);
			}
		 }
		 else
		 {
			png_bytep sp = row + (png_size_t)row_width * 4 - 1;
			png_bytep dp = sp  + (png_size_t)row_width * 4;
			for (i = 0; i < row_width; i++)
			{
			   *(dp--) = *(sp--);
			   *(dp--) = *(sp--);
			   *(dp--) = *sp;
			   *(dp--) = *(sp - 1);
			   *(dp--) = *sp;
			   *(dp--) = *(sp - 1);
			   *(dp--) = *(sp--);
			   *(dp--) = *(sp--);
			}
		 }
	  }
	  row_info->channels += (png_byte)2;
	  row_info->color_type |= PNG_COLOR_MASK_COLOR;
	  row_info->pixel_depth = (png_byte)(row_info->channels *
		 row_info->bit_depth);
	  row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
   }
}
#endif

#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
int /* PRIVATE */
png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)

{
   png_uint_32 i;

   png_uint_32 row_width = row_info->width;
   int rgb_error = 0;

   png_debug(1, "in png_do_rgb_to_gray\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	  (row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
	  png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
	  png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
	  png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;

	  if (row_info->color_type == PNG_COLOR_TYPE_RGB)
	  {
		 if (row_info->bit_depth == 8)
		 {
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
			if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
			{
			   png_bytep sp = row;
			   png_bytep dp = row;

			   for (i = 0; i < row_width; i++)
			   {
				  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
				  png_byte green = png_ptr->gamma_to_1[*(sp++)];
				  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
				  if(red != green || red != blue)
				  {
					 rgb_error |= 1;
					 *(dp++) = png_ptr->gamma_from_1[
					   (rc*red+gc*green+bc*blue)>>15];
				  }
				  else
					 *(dp++) = *(sp-1);
			   }
			}
			else
#endif
			{
			   png_bytep sp = row;
			   png_bytep dp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_byte red   = *(sp++);
				  png_byte green = *(sp++);
				  png_byte blue  = *(sp++);
				  if(red != green || red != blue)
				  {
					 rgb_error |= 1;
					 *(dp++) = (png_byte)((rc*red+gc*green+bc*blue)>>15);
				  }
				  else
					 *(dp++) = *(sp-1);
			   }
			}
		 }

		 else /* RGB bit_depth == 16 */
		 {
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
			if (png_ptr->gamma_16_to_1 != NULL &&
				png_ptr->gamma_16_from_1 != NULL)
			{
			   png_bytep sp = row;
			   png_bytep dp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_uint_16 red, green, blue, w;

				  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
				  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
				  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;

				  if(red == green && red == blue)
					 w = red;
				  else
				  {
					 png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
								  png_ptr->gamma_shift][red>>8];
					 png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
								  png_ptr->gamma_shift][green>>8];
					 png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
								  png_ptr->gamma_shift][blue>>8];
					 png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
								  + bc*blue_1)>>15);
					 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
						 png_ptr->gamma_shift][gray16 >> 8];
					 rgb_error |= 1;
				  }

				  *(dp++) = (png_byte)((w>>8) & 0xff);
				  *(dp++) = (png_byte)(w & 0xff);
			   }
			}
			else
#endif
			{
			   png_bytep sp = row;
			   png_bytep dp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_uint_16 red, green, blue, gray16;

				  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
				  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
				  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;

				  if(red != green || red != blue)
					 rgb_error |= 1;
				  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
				  *(dp++) = (png_byte)((gray16>>8) & 0xff);
				  *(dp++) = (png_byte)(gray16 & 0xff);
			   }
			}
		 }
	  }
	  if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
			if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
			{
			   png_bytep sp = row;
			   png_bytep dp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
				  png_byte green = png_ptr->gamma_to_1[*(sp++)];
				  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
				  if(red != green || red != blue)
					 rgb_error |= 1;
				  *(dp++) =  png_ptr->gamma_from_1
							 [(rc*red + gc*green + bc*blue)>>15];
				  *(dp++) = *(sp++);  /* alpha */
			   }
			}
			else
#endif
			{
			   png_bytep sp = row;
			   png_bytep dp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_byte red   = *(sp++);
				  png_byte green = *(sp++);
				  png_byte blue  = *(sp++);
				  if(red != green || red != blue)
					 rgb_error |= 1;
				  *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
				  *(dp++) = *(sp++);  /* alpha */
			   }
			}
		 }
		 else /* RGBA bit_depth == 16 */
		 {
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
			if (png_ptr->gamma_16_to_1 != NULL &&
				png_ptr->gamma_16_from_1 != NULL)
			{
			   png_bytep sp = row;
			   png_bytep dp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_uint_16 red, green, blue, w;

				  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
				  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
				  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;

				  if(red == green && red == blue)
					 w = red;
				  else
				  {
					 png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
								  png_ptr->gamma_shift][red>>8];
					 png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
								  png_ptr->gamma_shift][green>>8];
					 png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
								  png_ptr->gamma_shift][blue>>8];
					 png_uint_16 gray16  = (png_uint_16)((rc * red_1
								  + gc * green_1 + bc * blue_1)>>15);
					 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
						 png_ptr->gamma_shift][gray16 >> 8];
					 rgb_error |= 1;
				  }

				  *(dp++) = (png_byte)((w>>8) & 0xff);
				  *(dp++) = (png_byte)(w & 0xff);
				  *(dp++) = *(sp++);  /* alpha */
				  *(dp++) = *(sp++);
			   }
			}
			else
#endif
			{
			   png_bytep sp = row;
			   png_bytep dp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_uint_16 red, green, blue, gray16;
				  red   = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
				  green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
				  blue  = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
				  if(red != green || red != blue)
					 rgb_error |= 1;
				  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
				  *(dp++) = (png_byte)((gray16>>8) & 0xff);
				  *(dp++) = (png_byte)(gray16 & 0xff);
				  *(dp++) = *(sp++);  /* alpha */
				  *(dp++) = *(sp++);
			   }
			}
		 }
	  }
   row_info->channels -= (png_byte)2;
	  row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
	  row_info->pixel_depth = (png_byte)(row_info->channels *
		 row_info->bit_depth);
	  row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
   }
   return rgb_error;
}
#endif

void PNGAPI
png_build_grayscale_palette(int bit_depth, png_colorp palette)
{
   int num_palette;
   int color_inc;
   int i;
   int v;

   png_debug(1, "in png_do_build_grayscale_palette\n");
   if (palette == NULL)
	  return;

   switch (bit_depth)
   {
	  case 1:
		 num_palette = 2;
		 color_inc = 0xff;
		 break;
	  case 2:
		 num_palette = 4;
		 color_inc = 0x55;
		 break;
	  case 4:
		 num_palette = 16;
		 color_inc = 0x11;
		 break;
	  case 8:
		 num_palette = 256;
		 color_inc = 1;
		 break;
	  default:
		 num_palette = 0;
		 color_inc = 0;
		 break;
   }

   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
   {
	  palette[i].red = (png_byte)v;
	  palette[i].green = (png_byte)v;
	  palette[i].blue = (png_byte)v;
   }
}

#if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED)
void /* PRIVATE */
png_correct_palette(png_structp png_ptr, png_colorp palette,
   int num_palette)
{
   png_debug(1, "in png_correct_palette\n");
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
	defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
   if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
   {
	  png_color back, back_1;

	  if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
	  {
		 back.red = png_ptr->gamma_table[png_ptr->background.red];
		 back.green = png_ptr->gamma_table[png_ptr->background.green];
		 back.blue = png_ptr->gamma_table[png_ptr->background.blue];

		 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
		 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
		 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
	  }
	  else
	  {
		 double g;

		 g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);

		 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN ||
			 fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
		 {
			back.red = png_ptr->background.red;
			back.green = png_ptr->background.green;
			back.blue = png_ptr->background.blue;
		 }
		 else
		 {
			back.red =
			   (png_byte)(pow((double)png_ptr->background.red/255, g) *
				255.0 + 0.5);
			back.green =
			   (png_byte)(pow((double)png_ptr->background.green/255, g) *
				255.0 + 0.5);
			back.blue =
			   (png_byte)(pow((double)png_ptr->background.blue/255, g) *
				255.0 + 0.5);
		 }

		 g = 1.0 / png_ptr->background_gamma;

		 back_1.red =
			(png_byte)(pow((double)png_ptr->background.red/255, g) *
			 255.0 + 0.5);
		 back_1.green =
			(png_byte)(pow((double)png_ptr->background.green/255, g) *
			 255.0 + 0.5);
		 back_1.blue =
			(png_byte)(pow((double)png_ptr->background.blue/255, g) *
			 255.0 + 0.5);
	  }

	  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  {
		 png_uint_32 i;

		 for (i = 0; i < (png_uint_32)num_palette; i++)
		 {
			if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
			{
			   palette[i] = back;
			}
			else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
			{
			   png_byte v, w;

			   v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
			   png_composite(w, v, png_ptr->trans[i], back_1.red);
			   palette[i].red = png_ptr->gamma_from_1[w];

			   v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
			   png_composite(w, v, png_ptr->trans[i], back_1.green);
			   palette[i].green = png_ptr->gamma_from_1[w];

			   v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
			   png_composite(w, v, png_ptr->trans[i], back_1.blue);
			   palette[i].blue = png_ptr->gamma_from_1[w];
			}
			else
			{
			   palette[i].red = png_ptr->gamma_table[palette[i].red];
			   palette[i].green = png_ptr->gamma_table[palette[i].green];
			   palette[i].blue = png_ptr->gamma_table[palette[i].blue];
			}
		 }
	  }
	  else
	  {
		 int i;

		 for (i = 0; i < num_palette; i++)
		 {
			if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
			{
			   palette[i] = back;
			}
			else
			{
			   palette[i].red = png_ptr->gamma_table[palette[i].red];
			   palette[i].green = png_ptr->gamma_table[palette[i].green];
			   palette[i].blue = png_ptr->gamma_table[palette[i].blue];
			}
		 }
	  }
   }
   else
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
   if (png_ptr->transformations & PNG_GAMMA)
   {
	  int i;

	  for (i = 0; i < num_palette; i++)
	  {
		 palette[i].red = png_ptr->gamma_table[palette[i].red];
		 palette[i].green = png_ptr->gamma_table[palette[i].green];
		 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
	  }
   }
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   else
#endif
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   if (png_ptr->transformations & PNG_BACKGROUND)
   {
	  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  {
		 png_color back;

		 back.red   = (png_byte)png_ptr->background.red;
		 back.green = (png_byte)png_ptr->background.green;
		 back.blue  = (png_byte)png_ptr->background.blue;

		 for (i = 0; i < (int)png_ptr->num_trans; i++)
		 {
			if (png_ptr->trans[i] == 0)
			{
			   palette[i].red = back.red;
			   palette[i].green = back.green;
			   palette[i].blue = back.blue;
			}
			else if (png_ptr->trans[i] != 0xff)
			{
			   png_composite(palette[i].red, png_ptr->palette[i].red,
				  png_ptr->trans[i], back.red);
			   png_composite(palette[i].green, png_ptr->palette[i].green,
				  png_ptr->trans[i], back.green);
			   png_composite(palette[i].blue, png_ptr->palette[i].blue,
				  png_ptr->trans[i], back.blue);
			}
		 }
	  }
	  else /* assume grayscale palette (what else could it be?) */
	  {
		 int i;

		 for (i = 0; i < num_palette; i++)
		 {
			if (i == (png_byte)png_ptr->trans_values.gray)
			{
			   palette[i].red = (png_byte)png_ptr->background.red;
			   palette[i].green = (png_byte)png_ptr->background.green;
			   palette[i].blue = (png_byte)png_ptr->background.blue;
			}
		 }
	  }
   }
#endif
}
#endif

#if defined(PNG_READ_BACKGROUND_SUPPORTED)
void /* PRIVATE */
png_do_background(png_row_infop row_info, png_bytep row,
   png_color_16p trans_values, png_color_16p background
#if defined(PNG_READ_GAMMA_SUPPORTED)
   , png_color_16p background_1,
   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
   png_uint_16pp gamma_16_to_1, int gamma_shift
#endif
   )
{
   png_bytep sp, dp;
   png_uint_32 i;
   png_uint_32 row_width=row_info->width;
   int shift;

   png_debug(1, "in png_do_background\n");
   if (background != NULL &&
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	  (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
	  (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
   {
	  switch (row_info->color_type)
	  {
		 case PNG_COLOR_TYPE_GRAY:
		 {
			switch (row_info->bit_depth)
			{
			   case 1:
			   {
				  sp = row;
				  shift = 7;
				  for (i = 0; i < row_width; i++)
				  {
					 if ((png_uint_16)((*sp >> shift) & 0x01)
						== trans_values->gray)
					 {
						*sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
						*sp |= (png_byte)(background->gray << shift);
					 }
					 if (!shift)
					 {
						shift = 7;
						sp++;
					 }
					 else
						shift--;
				  }
				  break;
			   }
			   case 2:
			   {
#if defined(PNG_READ_GAMMA_SUPPORTED)
				  if (gamma_table != NULL)
				  {
					 sp = row;
					 shift = 6;
					 for (i = 0; i < row_width; i++)
					 {
						if ((png_uint_16)((*sp >> shift) & 0x03)
							== trans_values->gray)
						{
						   *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
						   *sp |= (png_byte)(background->gray << shift);
						}
						else
						{
						   png_byte p = (png_byte)((*sp >> shift) & 0x03);
						   png_byte g = (png_byte)((gamma_table [p | (p << 2) |
							   (p << 4) | (p << 6)] >> 6) & 0x03);
						   *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
						   *sp |= (png_byte)(g << shift);
						}
						if (!shift)
						{
						   shift = 6;
						   sp++;
						}
						else
						   shift -= 2;
					 }
				  }
				  else
#endif
				  {
					 sp = row;
					 shift = 6;
					 for (i = 0; i < row_width; i++)
					 {
						if ((png_uint_16)((*sp >> shift) & 0x03)
							== trans_values->gray)
						{
						   *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
						   *sp |= (png_byte)(background->gray << shift);
						}
						if (!shift)
						{
						   shift = 6;
						   sp++;
						}
						else
						   shift -= 2;
					 }
				  }
				  break;
			   }
			   case 4:
			   {
#if defined(PNG_READ_GAMMA_SUPPORTED)
				  if (gamma_table != NULL)
				  {
					 sp = row;
					 shift = 4;
					 for (i = 0; i < row_width; i++)
					 {
						if ((png_uint_16)((*sp >> shift) & 0x0f)
							== trans_values->gray)
						{
						   *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
						   *sp |= (png_byte)(background->gray << shift);
						}
						else
						{
						   png_byte p = (png_byte)((*sp >> shift) & 0x0f);
						   png_byte g = (png_byte)((gamma_table[p |
							 (p << 4)] >> 4) & 0x0f);
						   *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
						   *sp |= (png_byte)(g << shift);
						}
						if (!shift)
						{
						   shift = 4;
						   sp++;
						}
						else
						   shift -= 4;
					 }
				  }
				  else
#endif
				  {
					 sp = row;
					 shift = 4;
					 for (i = 0; i < row_width; i++)
					 {
						if ((png_uint_16)((*sp >> shift) & 0x0f)
							== trans_values->gray)
						{
						   *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
						   *sp |= (png_byte)(background->gray << shift);
						}
						if (!shift)
						{
						   shift = 4;
						   sp++;
						}
						else
						   shift -= 4;
					 }
				  }
				  break;
			   }
			   case 8:
			   {
#if defined(PNG_READ_GAMMA_SUPPORTED)
				  if (gamma_table != NULL)
				  {
					 sp = row;
					 for (i = 0; i < row_width; i++, sp++)
					 {
						if (*sp == trans_values->gray)
						{
						   *sp = (png_byte)background->gray;
						}
						else
						{
						   *sp = gamma_table[*sp];
						}
					 }
				  }
				  else
#endif
				  {
					 sp = row;
					 for (i = 0; i < row_width; i++, sp++)
					 {
						if (*sp == trans_values->gray)
						{
						   *sp = (png_byte)background->gray;
						}
					 }
				  }
				  break;
			   }
			   case 16:
			   {
#if defined(PNG_READ_GAMMA_SUPPORTED)
				  if (gamma_16 != NULL)
				  {
					 sp = row;
					 for (i = 0; i < row_width; i++, sp += 2)
					 {
						png_uint_16 v;

						v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
						if (v == trans_values->gray)
						{
						   *sp = (png_byte)((background->gray >> 8) & 0xff);
						   *(sp + 1) = (png_byte)(background->gray & 0xff);
						}
						else
						{
						   v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
						   *sp = (png_byte)((v >> 8) & 0xff);
						   *(sp + 1) = (png_byte)(v & 0xff);
						}
					 }
				  }
				  else
#endif
				  {
					 sp = row;
					 for (i = 0; i < row_width; i++, sp += 2)
					 {
						png_uint_16 v;

						v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
						if (v == trans_values->gray)
						{
						   *sp = (png_byte)((background->gray >> 8) & 0xff);
						   *(sp + 1) = (png_byte)(background->gray & 0xff);
						}
					 }
				  }
				  break;
			   }
			}
			break;
		 }
		 case PNG_COLOR_TYPE_RGB:
		 {
			if (row_info->bit_depth == 8)
			{
#if defined(PNG_READ_GAMMA_SUPPORTED)
			   if (gamma_table != NULL)
			   {
				  sp = row;
				  for (i = 0; i < row_width; i++, sp += 3)
				  {
					 if (*sp == trans_values->red &&
						*(sp + 1) == trans_values->green &&
						*(sp + 2) == trans_values->blue)
					 {
						*sp = (png_byte)background->red;
						*(sp + 1) = (png_byte)background->green;
						*(sp + 2) = (png_byte)background->blue;
					 }
					 else
					 {
						*sp = gamma_table[*sp];
						*(sp + 1) = gamma_table[*(sp + 1)];
						*(sp + 2) = gamma_table[*(sp + 2)];
					 }
				  }
			   }
			   else
#endif
			   {
				  sp = row;
				  for (i = 0; i < row_width; i++, sp += 3)
				  {
					 if (*sp == trans_values->red &&
						*(sp + 1) == trans_values->green &&
						*(sp + 2) == trans_values->blue)
					 {
						*sp = (png_byte)background->red;
						*(sp + 1) = (png_byte)background->green;
						*(sp + 2) = (png_byte)background->blue;
					 }
				  }
			   }
			}
			else /* if (row_info->bit_depth == 16) */
			{
#if defined(PNG_READ_GAMMA_SUPPORTED)
			   if (gamma_16 != NULL)
			   {
				  sp = row;
				  for (i = 0; i < row_width; i++, sp += 6)
				  {
					 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
					 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
					 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
					 if (r == trans_values->red && g == trans_values->green &&
						b == trans_values->blue)
					 {
						*sp = (png_byte)((background->red >> 8) & 0xff);
						*(sp + 1) = (png_byte)(background->red & 0xff);
						*(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
						*(sp + 3) = (png_byte)(background->green & 0xff);
						*(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
						*(sp + 5) = (png_byte)(background->blue & 0xff);
					 }
					 else
					 {
						png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
						*sp = (png_byte)((v >> 8) & 0xff);
						*(sp + 1) = (png_byte)(v & 0xff);
						v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
						*(sp + 2) = (png_byte)((v >> 8) & 0xff);
						*(sp + 3) = (png_byte)(v & 0xff);
						v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
						*(sp + 4) = (png_byte)((v >> 8) & 0xff);
						*(sp + 5) = (png_byte)(v & 0xff);
					 }
				  }
			   }
			   else
#endif
			   {
				  sp = row;
				  for (i = 0; i < row_width; i++, sp += 6)
				  {
					 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
					 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
					 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));

					 if (r == trans_values->red && g == trans_values->green &&
						b == trans_values->blue)
					 {
						*sp = (png_byte)((background->red >> 8) & 0xff);
						*(sp + 1) = (png_byte)(background->red & 0xff);
						*(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
						*(sp + 3) = (png_byte)(background->green & 0xff);
						*(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
						*(sp + 5) = (png_byte)(background->blue & 0xff);
					 }
				  }
			   }
			}
			break;
		 }
		 case PNG_COLOR_TYPE_GRAY_ALPHA:
		 {
			if (row_info->bit_depth == 8)
			{
#if defined(PNG_READ_GAMMA_SUPPORTED)
			   if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
				   gamma_table != NULL)
			   {
				  sp = row;
				  dp = row;
				  for (i = 0; i < row_width; i++, sp += 2, dp++)
				  {
					 png_uint_16 a = *(sp + 1);

					 if (a == 0xff)
					 {
						*dp = gamma_table[*sp];
					 }
					 else if (a == 0)
					 {
						*dp = (png_byte)background->gray;
					 }
					 else
					 {
						png_byte v, w;

						v = gamma_to_1[*sp];
						png_composite(w, v, a, background_1->gray);
						*dp = gamma_from_1[w];
					 }
				  }
			   }
			   else
#endif
			   {
				  sp = row;
				  dp = row;
				  for (i = 0; i < row_width; i++, sp += 2, dp++)
				  {
					 png_byte a = *(sp + 1);

					 if (a == 0xff)
					 {
						*dp = *sp;
					 }
#if defined(PNG_READ_GAMMA_SUPPORTED)
					 else if (a == 0)
					 {
						*dp = (png_byte)background->gray;
					 }
					 else
					 {
						png_composite(*dp, *sp, a, background_1->gray);
					 }
#else
					 *dp = (png_byte)background->gray;
#endif
				  }
			   }
			}
			else /* if (png_ptr->bit_depth == 16) */
			{
#if defined(PNG_READ_GAMMA_SUPPORTED)
			   if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
				   gamma_16_to_1 != NULL)
			   {
				  sp = row;
				  dp = row;
				  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
				  {
					 png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));

					 if (a == (png_uint_16)0xffff)
					 {
						png_uint_16 v;

						v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
						*dp = (png_byte)((v >> 8) & 0xff);
						*(dp + 1) = (png_byte)(v & 0xff);
					 }
#if defined(PNG_READ_GAMMA_SUPPORTED)
					 else if (a == 0)
#else
					 else
#endif
					 {
						*dp = (png_byte)((background->gray >> 8) & 0xff);
						*(dp + 1) = (png_byte)(background->gray & 0xff);
					 }
#if defined(PNG_READ_GAMMA_SUPPORTED)
					 else
					 {
						png_uint_16 g, v, w;

						g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
						png_composite_16(v, g, a, background_1->gray);
						w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
						*dp = (png_byte)((w >> 8) & 0xff);
						*(dp + 1) = (png_byte)(w & 0xff);
					 }
#endif
				  }
			   }
			   else
#endif
			   {
				  sp = row;
				  dp = row;
				  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
				  {
					 png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
					 if (a == (png_uint_16)0xffff)
					 {
						png_memcpy(dp, sp, 2);
					 }
#if defined(PNG_READ_GAMMA_SUPPORTED)
					 else if (a == 0)
#else
					 else
#endif
					 {
						*dp = (png_byte)((background->gray >> 8) & 0xff);
						*(dp + 1) = (png_byte)(background->gray & 0xff);
					 }
#if defined(PNG_READ_GAMMA_SUPPORTED)
					 else
					 {
						png_uint_16 g, v;

						g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
						png_composite_16(v, g, a, background_1->gray);
						*dp = (png_byte)((v >> 8) & 0xff);
						*(dp + 1) = (png_byte)(v & 0xff);
					 }
#endif
				  }
			   }
			}
			break;
		 }
		 case PNG_COLOR_TYPE_RGB_ALPHA:
		 {
			if (row_info->bit_depth == 8)
			{
#if defined(PNG_READ_GAMMA_SUPPORTED)
			   if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
				   gamma_table != NULL)
			   {
				  sp = row;
				  dp = row;
				  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
				  {
					 png_byte a = *(sp + 3);

					 if (a == 0xff)
					 {
						*dp = gamma_table[*sp];
						*(dp + 1) = gamma_table[*(sp + 1)];
						*(dp + 2) = gamma_table[*(sp + 2)];
					 }
					 else if (a == 0)
					 {
						*dp = (png_byte)background->red;
						*(dp + 1) = (png_byte)background->green;
						*(dp + 2) = (png_byte)background->blue;
					 }
					 else
					 {
						png_byte v, w;

						v = gamma_to_1[*sp];
						png_composite(w, v, a, background_1->red);
						*dp = gamma_from_1[w];
						v = gamma_to_1[*(sp + 1)];
						png_composite(w, v, a, background_1->green);
						*(dp + 1) = gamma_from_1[w];
						v = gamma_to_1[*(sp + 2)];
						png_composite(w, v, a, background_1->blue);
						*(dp + 2) = gamma_from_1[w];
					 }
				  }
			   }
			   else
#endif
			   {
				  sp = row;
				  dp = row;
				  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
				  {
					 png_byte a = *(sp + 3);

					 if (a == 0xff)
					 {
						*dp = *sp;
						*(dp + 1) = *(sp + 1);
						*(dp + 2) = *(sp + 2);
					 }
					 else if (a == 0)
					 {
						*dp = (png_byte)background->red;
						*(dp + 1) = (png_byte)background->green;
						*(dp + 2) = (png_byte)background->blue;
					 }
					 else
					 {
						png_composite(*dp, *sp, a, background->red);
						png_composite(*(dp + 1), *(sp + 1), a,
						   background->green);
						png_composite(*(dp + 2), *(sp + 2), a,
						   background->blue);
					 }
				  }
			   }
			}
			else /* if (row_info->bit_depth == 16) */
			{
#if defined(PNG_READ_GAMMA_SUPPORTED)
			   if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
				   gamma_16_to_1 != NULL)
			   {
				  sp = row;
				  dp = row;
				  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
				  {
					 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
						 << 8) + (png_uint_16)(*(sp + 7)));
					 if (a == (png_uint_16)0xffff)
					 {
						png_uint_16 v;

						v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
						*dp = (png_byte)((v >> 8) & 0xff);
						*(dp + 1) = (png_byte)(v & 0xff);
						v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
						*(dp + 2) = (png_byte)((v >> 8) & 0xff);
						*(dp + 3) = (png_byte)(v & 0xff);
						v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
						*(dp + 4) = (png_byte)((v >> 8) & 0xff);
						*(dp + 5) = (png_byte)(v & 0xff);
					 }
					 else if (a == 0)
					 {
						*dp = (png_byte)((background->red >> 8) & 0xff);
						*(dp + 1) = (png_byte)(background->red & 0xff);
						*(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
						*(dp + 3) = (png_byte)(background->green & 0xff);
						*(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
						*(dp + 5) = (png_byte)(background->blue & 0xff);
					 }
					 else
					 {
						png_uint_16 v, w, x;

						v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
						png_composite_16(w, v, a, background_1->red);
						x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
						*dp = (png_byte)((x >> 8) & 0xff);
						*(dp + 1) = (png_byte)(x & 0xff);
						v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
						png_composite_16(w, v, a, background_1->green);
						x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
						*(dp + 2) = (png_byte)((x >> 8) & 0xff);
						*(dp + 3) = (png_byte)(x & 0xff);
						v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
						png_composite_16(w, v, a, background_1->blue);
						x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
						*(dp + 4) = (png_byte)((x >> 8) & 0xff);
						*(dp + 5) = (png_byte)(x & 0xff);
					 }
				  }
			   }
			   else
#endif
			   {
				  sp = row;
				  dp = row;
				  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
				  {
					 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
						<< 8) + (png_uint_16)(*(sp + 7)));
					 if (a == (png_uint_16)0xffff)
					 {
						png_memcpy(dp, sp, 6);
					 }
					 else if (a == 0)
					 {
						*dp = (png_byte)((background->red >> 8) & 0xff);
						*(dp + 1) = (png_byte)(background->red & 0xff);
						*(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
						*(dp + 3) = (png_byte)(background->green & 0xff);
						*(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
						*(dp + 5) = (png_byte)(background->blue & 0xff);
					 }
					 else
					 {
						png_uint_16 v;

						png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
						png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
							+ *(sp + 3));
						png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
							+ *(sp + 5));

						png_composite_16(v, r, a, background->red);
						*dp = (png_byte)((v >> 8) & 0xff);
						*(dp + 1) = (png_byte)(v & 0xff);
						png_composite_16(v, g, a, background->green);
						*(dp + 2) = (png_byte)((v >> 8) & 0xff);
						*(dp + 3) = (png_byte)(v & 0xff);
						png_composite_16(v, b, a, background->blue);
						*(dp + 4) = (png_byte)((v >> 8) & 0xff);
						*(dp + 5) = (png_byte)(v & 0xff);
					 }
				  }
			   }
			}
			break;
		 }
	  }

	  if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
	  {
		 row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
		 row_info->channels--;
		 row_info->pixel_depth = (png_byte)(row_info->channels *
			row_info->bit_depth);
		 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
	  }
   }
}
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED)
void /* PRIVATE */
png_do_gamma(png_row_infop row_info, png_bytep row,
   png_bytep gamma_table, png_uint_16pp gamma_16_table,
   int gamma_shift)
{
   png_bytep sp;
   png_uint_32 i;
   png_uint_32 row_width=row_info->width;

   png_debug(1, "in png_do_gamma\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	   ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
		(row_info->bit_depth == 16 && gamma_16_table != NULL)))
   {
	  switch (row_info->color_type)
	  {
		 case PNG_COLOR_TYPE_RGB:
		 {
			if (row_info->bit_depth == 8)
			{
			   sp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  *sp = gamma_table[*sp];
				  sp++;
				  *sp = gamma_table[*sp];
				  sp++;
				  *sp = gamma_table[*sp];
				  sp++;
			   }
			}
			else /* if (row_info->bit_depth == 16) */
			{
			   sp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_uint_16 v;

				  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
				  *sp = (png_byte)((v >> 8) & 0xff);
				  *(sp + 1) = (png_byte)(v & 0xff);
				  sp += 2;
				  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
				  *sp = (png_byte)((v >> 8) & 0xff);
				  *(sp + 1) = (png_byte)(v & 0xff);
				  sp += 2;
				  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
				  *sp = (png_byte)((v >> 8) & 0xff);
				  *(sp + 1) = (png_byte)(v & 0xff);
				  sp += 2;
			   }
			}
			break;
		 }
		 case PNG_COLOR_TYPE_RGB_ALPHA:
		 {
			if (row_info->bit_depth == 8)
			{
			   sp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  *sp = gamma_table[*sp];
				  sp++;
				  *sp = gamma_table[*sp];
				  sp++;
				  *sp = gamma_table[*sp];
				  sp++;
				  sp++;
			   }
			}
			else /* if (row_info->bit_depth == 16) */
			{
			   sp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
				  *sp = (png_byte)((v >> 8) & 0xff);
				  *(sp + 1) = (png_byte)(v & 0xff);
				  sp += 2;
				  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
				  *sp = (png_byte)((v >> 8) & 0xff);
				  *(sp + 1) = (png_byte)(v & 0xff);
				  sp += 2;
				  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
				  *sp = (png_byte)((v >> 8) & 0xff);
				  *(sp + 1) = (png_byte)(v & 0xff);
				  sp += 4;
			   }
			}
			break;
		 }
		 case PNG_COLOR_TYPE_GRAY_ALPHA:
		 {
			if (row_info->bit_depth == 8)
			{
			   sp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  *sp = gamma_table[*sp];
				  sp += 2;
			   }
			}
			else /* if (row_info->bit_depth == 16) */
			{
			   sp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
				  *sp = (png_byte)((v >> 8) & 0xff);
				  *(sp + 1) = (png_byte)(v & 0xff);
				  sp += 4;
			   }
			}
			break;
		 }
		 case PNG_COLOR_TYPE_GRAY:
		 {
			if (row_info->bit_depth == 2)
			{
			   sp = row;
			   for (i = 0; i < row_width; i += 4)
			   {
				  int a = *sp & 0xc0;
				  int b = *sp & 0x30;
				  int c = *sp & 0x0c;
				  int d = *sp & 0x03;

				  *sp = (png_byte)(
						((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
						((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
						((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
						((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
				  sp++;
			   }
			}
			if (row_info->bit_depth == 4)
			{
			   sp = row;
			   for (i = 0; i < row_width; i += 2)
			   {
				  int msb = *sp & 0xf0;
				  int lsb = *sp & 0x0f;

				  *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
						  | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
				  sp++;
			   }
			}
			else if (row_info->bit_depth == 8)
			{
			   sp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  *sp = gamma_table[*sp];
				  sp++;
			   }
			}
			else if (row_info->bit_depth == 16)
			{
			   sp = row;
			   for (i = 0; i < row_width; i++)
			   {
				  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
				  *sp = (png_byte)((v >> 8) & 0xff);
				  *(sp + 1) = (png_byte)(v & 0xff);
				  sp += 2;
			   }
			}
			break;
		 }
	  }
   }
}
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
void /* PRIVATE */
png_do_expand_palette(png_row_infop row_info, png_bytep row,
   png_colorp palette, png_bytep trans, int num_trans)
{
   int shift, value;
   png_bytep sp, dp;
   png_uint_32 i;
   png_uint_32 row_width=row_info->width;

   png_debug(1, "in png_do_expand_palette\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	   row_info->color_type == PNG_COLOR_TYPE_PALETTE)
   {
	  if (row_info->bit_depth < 8)
	  {
		 switch (row_info->bit_depth)
		 {
			case 1:
			{
			   sp = row + (png_size_t)((row_width - 1) >> 3);
			   dp = row + (png_size_t)row_width - 1;
			   shift = 7 - (int)((row_width + 7) & 0x07);
			   for (i = 0; i < row_width; i++)
			   {
				  if ((*sp >> shift) & 0x01)
					 *dp = 1;
				  else
					 *dp = 0;
				  if (shift == 7)
				  {
					 shift = 0;
					 sp--;
				  }
				  else
					 shift++;

				  dp--;
			   }
			   break;
			}
			case 2:
			{
			   sp = row + (png_size_t)((row_width - 1) >> 2);
			   dp = row + (png_size_t)row_width - 1;
			   shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
			   for (i = 0; i < row_width; i++)
			   {
				  value = (*sp >> shift) & 0x03;
				  *dp = (png_byte)value;
				  if (shift == 6)
				  {
					 shift = 0;
					 sp--;
				  }
				  else
					 shift += 2;

				  dp--;
			   }
			   break;
			}
			case 4:
			{
			   sp = row + (png_size_t)((row_width - 1) >> 1);
			   dp = row + (png_size_t)row_width - 1;
			   shift = (int)((row_width & 0x01) << 2);
			   for (i = 0; i < row_width; i++)
			   {
				  value = (*sp >> shift) & 0x0f;
				  *dp = (png_byte)value;
				  if (shift == 4)
				  {
					 shift = 0;
					 sp--;
				  }
				  else
					 shift += 4;

				  dp--;
			   }
			   break;
			}
		 }
		 row_info->bit_depth = 8;
		 row_info->pixel_depth = 8;
		 row_info->rowbytes = row_width;
	  }
	  switch (row_info->bit_depth)
	  {
		 case 8:
		 {
			if (trans != NULL)
			{
			   sp = row + (png_size_t)row_width - 1;
			   dp = row + (png_size_t)(row_width << 2) - 1;

			   for (i = 0; i < row_width; i++)
			   {
				  if ((int)(*sp) >= num_trans)
					 *dp-- = 0xff;
				  else
					 *dp-- = trans[*sp];
				  *dp-- = palette[*sp].blue;
				  *dp-- = palette[*sp].green;
				  *dp-- = palette[*sp].red;
				  sp--;
			   }
			   row_info->bit_depth = 8;
			   row_info->pixel_depth = 32;
			   row_info->rowbytes = row_width * 4;
			   row_info->color_type = 6;
			   row_info->channels = 4;
			}
			else
			{
			   sp = row + (png_size_t)row_width - 1;
			   dp = row + (png_size_t)(row_width * 3) - 1;

			   for (i = 0; i < row_width; i++)
			   {
				  *dp-- = palette[*sp].blue;
				  *dp-- = palette[*sp].green;
				  *dp-- = palette[*sp].red;
				  sp--;
			   }
			   row_info->bit_depth = 8;
			   row_info->pixel_depth = 24;
			   row_info->rowbytes = row_width * 3;
			   row_info->color_type = 2;
			   row_info->channels = 3;
			}
			break;
		 }
	  }
   }
}

void /* PRIVATE */
png_do_expand(png_row_infop row_info, png_bytep row,
   png_color_16p trans_value)
{
   int shift, value;
   png_bytep sp, dp;
   png_uint_32 i;
   png_uint_32 row_width=row_info->width;

   png_debug(1, "in png_do_expand\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
	  if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
	  {
		 png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);

		 if (row_info->bit_depth < 8)
		 {
			switch (row_info->bit_depth)
			{
			   case 1:
			   {
				  gray = (png_uint_16)((gray&0x01)*0xff);
				  sp = row + (png_size_t)((row_width - 1) >> 3);
				  dp = row + (png_size_t)row_width - 1;
				  shift = 7 - (int)((row_width + 7) & 0x07);
				  for (i = 0; i < row_width; i++)
				  {
					 if ((*sp >> shift) & 0x01)
						*dp = 0xff;
					 else
						*dp = 0;
					 if (shift == 7)
					 {
						shift = 0;
						sp--;
					 }
					 else
						shift++;

					 dp--;
				  }
				  break;
			   }
			   case 2:
			   {
				  gray = (png_uint_16)((gray&0x03)*0x55);
				  sp = row + (png_size_t)((row_width - 1) >> 2);
				  dp = row + (png_size_t)row_width - 1;
				  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
				  for (i = 0; i < row_width; i++)
				  {
					 value = (*sp >> shift) & 0x03;
					 *dp = (png_byte)(value | (value << 2) | (value << 4) |
						(value << 6));
					 if (shift == 6)
					 {
						shift = 0;
						sp--;
					 }
					 else
						shift += 2;

					 dp--;
				  }
				  break;
			   }
			   case 4:
			   {
				  gray = (png_uint_16)((gray&0x0f)*0x11);
				  sp = row + (png_size_t)((row_width - 1) >> 1);
				  dp = row + (png_size_t)row_width - 1;
				  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
				  for (i = 0; i < row_width; i++)
				  {
					 value = (*sp >> shift) & 0x0f;
					 *dp = (png_byte)(value | (value << 4));
					 if (shift == 4)
					 {
						shift = 0;
						sp--;
					 }
					 else
						shift = 4;

					 dp--;
				  }
				  break;
			   }
			}
			row_info->bit_depth = 8;
			row_info->pixel_depth = 8;
			row_info->rowbytes = row_width;
		 }

		 if (trans_value != NULL)
		 {
			if (row_info->bit_depth == 8)
			{
			   gray = gray & 0xff;
			   sp = row + (png_size_t)row_width - 1;
			   dp = row + (png_size_t)(row_width << 1) - 1;
			   for (i = 0; i < row_width; i++)
			   {
				  if (*sp == gray)
					 *dp-- = 0;
				  else
					 *dp-- = 0xff;
				  *dp-- = *sp--;
			   }
			}
			else if (row_info->bit_depth == 16)
			{
			   png_byte gray_high = (gray >> 8) & 0xff;
			   png_byte gray_low = gray & 0xff;
			   sp = row + row_info->rowbytes - 1;
			   dp = row + (row_info->rowbytes << 1) - 1;
			   for (i = 0; i < row_width; i++)
			   {
				  if (*(sp-1) == gray_high && *(sp) == gray_low)
				  {
					 *dp-- = 0;
					 *dp-- = 0;
				  }
				  else
				  {
					 *dp-- = 0xff;
					 *dp-- = 0xff;
				  }
				  *dp-- = *sp--;
				  *dp-- = *sp--;
			   }
			}
			row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
			row_info->channels = 2;
			row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
			row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
			   row_width);
		 }
	  }
	  else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_byte red = trans_value->red & 0xff;
			png_byte green = trans_value->green & 0xff;
			png_byte blue = trans_value->blue & 0xff;
			sp = row + (png_size_t)row_info->rowbytes - 1;
			dp = row + (png_size_t)(row_width << 2) - 1;
			for (i = 0; i < row_width; i++)
			{
			   if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
				  *dp-- = 0;
			   else
				  *dp-- = 0xff;
			   *dp-- = *sp--;
			   *dp-- = *sp--;
			   *dp-- = *sp--;
			}
		 }
		 else if (row_info->bit_depth == 16)
		 {
			png_byte red_high = (trans_value->red >> 8) & 0xff;
			png_byte green_high = (trans_value->green >> 8) & 0xff;
			png_byte blue_high = (trans_value->blue >> 8) & 0xff;
			png_byte red_low = trans_value->red & 0xff;
			png_byte green_low = trans_value->green & 0xff;
			png_byte blue_low = trans_value->blue & 0xff;
			sp = row + row_info->rowbytes - 1;
			dp = row + (png_size_t)(row_width << 3) - 1;
			for (i = 0; i < row_width; i++)
			{
			   if (*(sp - 5) == red_high &&
				  *(sp - 4) == red_low &&
				  *(sp - 3) == green_high &&
				  *(sp - 2) == green_low &&
				  *(sp - 1) == blue_high &&
				  *(sp	) == blue_low)
			   {
				  *dp-- = 0;
				  *dp-- = 0;
			   }
			   else
			   {
				  *dp-- = 0xff;
				  *dp-- = 0xff;
			   }
			   *dp-- = *sp--;
			   *dp-- = *sp--;
			   *dp-- = *sp--;
			   *dp-- = *sp--;
			   *dp-- = *sp--;
			   *dp-- = *sp--;
			}
		 }
		 row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
		 row_info->channels = 4;
		 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
		 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
	  }
   }
}
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
void /* PRIVATE */
png_do_dither(png_row_infop row_info, png_bytep row,
	png_bytep palette_lookup, png_bytep dither_lookup)
{
   png_bytep sp, dp;
   png_uint_32 i;
   png_uint_32 row_width=row_info->width;

   png_debug(1, "in png_do_dither\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
	  if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
		 palette_lookup && row_info->bit_depth == 8)
	  {
		 int r, g, b, p;
		 sp = row;
		 dp = row;
		 for (i = 0; i < row_width; i++)
		 {
			r = *sp++;
			g = *sp++;
			b = *sp++;

			p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
			   ((1 << PNG_DITHER_RED_BITS) - 1)) <<
			   (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
			   (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
			   ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
			   (PNG_DITHER_BLUE_BITS)) |
			   ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
			   ((1 << PNG_DITHER_BLUE_BITS) - 1));

			*dp++ = palette_lookup[p];
		 }
		 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
		 row_info->channels = 1;
		 row_info->pixel_depth = row_info->bit_depth;
		 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
	  }
	  else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
		 palette_lookup != NULL && row_info->bit_depth == 8)
	  {
		 int r, g, b, p;
		 sp = row;
		 dp = row;
		 for (i = 0; i < row_width; i++)
		 {
			r = *sp++;
			g = *sp++;
			b = *sp++;
			sp++;

			p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
			   ((1 << PNG_DITHER_RED_BITS) - 1)) <<
			   (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
			   (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
			   ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
			   (PNG_DITHER_BLUE_BITS)) |
			   ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
			   ((1 << PNG_DITHER_BLUE_BITS) - 1));

			*dp++ = palette_lookup[p];
		 }
		 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
		 row_info->channels = 1;
		 row_info->pixel_depth = row_info->bit_depth;
		 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
	  }
	  else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
		 dither_lookup && row_info->bit_depth == 8)
	  {
		 sp = row;
		 for (i = 0; i < row_width; i++, sp++)
		 {
			*sp = dither_lookup[*sp];
		 }
	  }
   }
}
#endif

#ifdef PNG_FLOATING_POINT_SUPPORTED
#if defined(PNG_READ_GAMMA_SUPPORTED)
static PNG_CONST int png_gamma_shift[] =
   {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};

void /* PRIVATE */
png_build_gamma_table(png_structp png_ptr)
{
  png_debug(1, "in png_build_gamma_table\n");

  if (png_ptr->bit_depth <= 8)
  {
	 int i;
	 double g;

	 if (png_ptr->screen_gamma > .000001)
		g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
	 else
		g = 1.0;

	 png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
		(png_uint_32)256);

	 for (i = 0; i < 256; i++)
	 {
		png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
		   g) * 255.0 + .5);
	 }

#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
	 if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
	 {

		g = 1.0 / (png_ptr->gamma);

		png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
		   (png_uint_32)256);

		for (i = 0; i < 256; i++)
		{
		   png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
			  g) * 255.0 + .5);
		}

		png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
		   (png_uint_32)256);

		if(png_ptr->screen_gamma > 0.000001)
		   g = 1.0 / png_ptr->screen_gamma;
		else
		   g = png_ptr->gamma;   /* probably doing rgb_to_gray */

		for (i = 0; i < 256; i++)
		{
		   png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
			  g) * 255.0 + .5);

		}
	 }
#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
  }
  else
  {
	 double g;
	 int i, j, shift, num;
	 int sig_bit;
	 png_uint_32 ig;

	 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
	 {
		sig_bit = (int)png_ptr->sig_bit.red;
		if ((int)png_ptr->sig_bit.green > sig_bit)
		   sig_bit = png_ptr->sig_bit.green;
		if ((int)png_ptr->sig_bit.blue > sig_bit)
		   sig_bit = png_ptr->sig_bit.blue;
	 }
	 else
	 {
		sig_bit = (int)png_ptr->sig_bit.gray;
	 }

	 if (sig_bit > 0)
		shift = 16 - sig_bit;
	 else
		shift = 0;

	 if (png_ptr->transformations & PNG_16_TO_8)
	 {
		if (shift < (16 - PNG_MAX_GAMMA_8))
		   shift = (16 - PNG_MAX_GAMMA_8);
	 }

	 if (shift > 8)
		shift = 8;
	 if (shift < 0)
		shift = 0;

	 png_ptr->gamma_shift = (png_byte)shift;

	 num = (1 << (8 - shift));

	 if (png_ptr->screen_gamma > .000001)
		g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
	 else
		g = 1.0;

	 png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
		(png_uint_32)(num * png_sizeof (png_uint_16p)));

	 if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
	 {
		double fin, fout;
		png_uint_32 last, max;

		for (i = 0; i < num; i++)
		{
		   png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
			  (png_uint_32)(256 * png_sizeof (png_uint_16)));
		}

		g = 1.0 / g;
		last = 0;
		for (i = 0; i < 256; i++)
		{
		   fout = ((double)i + 0.5) / 256.0;
		   fin = pow(fout, g);
		   max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
		   while (last <= max)
		   {
			  png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
				 [(int)(last >> (8 - shift))] = (png_uint_16)(
				 (png_uint_16)i | ((png_uint_16)i << 8));
			  last++;
		   }
		}
		while (last < ((png_uint_32)num << 8))
		{
		   png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
			  [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
		   last++;
		}
	 }
	 else
	 {
		for (i = 0; i < num; i++)
		{
		   png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
			  (png_uint_32)(256 * png_sizeof (png_uint_16)));

		   ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
		   for (j = 0; j < 256; j++)
		   {
			  png_ptr->gamma_16_table[i][j] =
				 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
					65535.0, g) * 65535.0 + .5);
		   }
		}
	 }

#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
	 if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
	 {

		g = 1.0 / (png_ptr->gamma);

		png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
		   (png_uint_32)(num * png_sizeof (png_uint_16p )));

		for (i = 0; i < num; i++)
		{
		   png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
			  (png_uint_32)(256 * png_sizeof (png_uint_16)));

		   ig = (((png_uint_32)i *
			  (png_uint_32)png_gamma_shift[shift]) >> 4);
		   for (j = 0; j < 256; j++)
		   {
			  png_ptr->gamma_16_to_1[i][j] =
				 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
					65535.0, g) * 65535.0 + .5);
		   }
		}

		if(png_ptr->screen_gamma > 0.000001)
		   g = 1.0 / png_ptr->screen_gamma;
		else
		   g = png_ptr->gamma;   /* probably doing rgb_to_gray */

		png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
		   (png_uint_32)(num * png_sizeof (png_uint_16p)));

		for (i = 0; i < num; i++)
		{
		   png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
			  (png_uint_32)(256 * png_sizeof (png_uint_16)));

		   ig = (((png_uint_32)i *
			  (png_uint_32)png_gamma_shift[shift]) >> 4);
		   for (j = 0; j < 256; j++)
		   {
			  png_ptr->gamma_16_from_1[i][j] =
				 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
					65535.0, g) * 65535.0 + .5);
		   }
		}
	 }
#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
  }
}
#endif
#endif

#if defined(PNG_MNG_FEATURES_SUPPORTED)
void /* PRIVATE */
png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_read_intrapixel\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	   (row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
	  int bytes_per_pixel;
	  png_uint_32 row_width = row_info->width;
	  if (row_info->bit_depth == 8)
	  {
		 png_bytep rp;
		 png_uint_32 i;

		 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
			bytes_per_pixel = 3;
		 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
			bytes_per_pixel = 4;
		 else
			return;

		 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
		 {
			*(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
			*(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
		 }
	  }
	  else if (row_info->bit_depth == 16)
	  {
		 png_bytep rp;
		 png_uint_32 i;

		 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
			bytes_per_pixel = 6;
		 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
			bytes_per_pixel = 8;
		 else
			return;

		 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
		 {
			png_uint_32 s0   = (*(rp  ) << 8) | *(rp+1);
			png_uint_32 s1   = (*(rp+2) << 8) | *(rp+3);
			png_uint_32 s2   = (*(rp+4) << 8) | *(rp+5);
			png_uint_32 red  = (png_uint_32)((s0+s1+65536L) & 0xffffL);
			png_uint_32 blue = (png_uint_32)((s2+s1+65536L) & 0xffffL);
			*(rp  ) = (png_byte)((red >> 8) & 0xff);
			*(rp+1) = (png_byte)(red & 0xff);
			*(rp+4) = (png_byte)((blue >> 8) & 0xff);
			*(rp+5) = (png_byte)(blue & 0xff);
		 }
	  }
   }
}
#endif /* PNG_MNG_FEATURES_SUPPORTED */
#endif /* PNG_READ_SUPPORTED */
/*** End of inlined file: pngrtran.c ***/


/*** Start of inlined file: pngrutil.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED)

#if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
#  define WIN32_WCE_OLD
#endif

#ifdef PNG_FLOATING_POINT_SUPPORTED
#  if defined(WIN32_WCE_OLD)
__inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
{
   double result = 0;
   int len;
   wchar_t *str, *end;

   len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
   str = (wchar_t *)png_malloc(png_ptr, len * sizeof(wchar_t));
   if ( NULL != str )
   {
	  MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
	  result = wcstod(str, &end);
	  len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
	  *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
	  png_free(png_ptr, str);
   }
   return result;
}
#  else
#	define png_strtod(p,a,b) strtod(a,b)
#  endif
#endif

png_uint_32 PNGAPI
png_get_uint_31(png_structp png_ptr, png_bytep buf)
{
   png_uint_32 i = png_get_uint_32(buf);
   if (i > PNG_UINT_31_MAX)
	 png_error(png_ptr, "PNG unsigned integer out of range.");
   return (i);
}
#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
png_uint_32 PNGAPI
png_get_uint_32(png_bytep buf)
{
   png_uint_32 i = ((png_uint_32)(*buf) << 24) +
	  ((png_uint_32)(*(buf + 1)) << 16) +
	  ((png_uint_32)(*(buf + 2)) << 8) +
	  (png_uint_32)(*(buf + 3));

   return (i);
}

png_int_32 PNGAPI
png_get_int_32(png_bytep buf)
{
   png_int_32 i = ((png_int_32)(*buf) << 24) +
	  ((png_int_32)(*(buf + 1)) << 16) +
	  ((png_int_32)(*(buf + 2)) << 8) +
	  (png_int_32)(*(buf + 3));

   return (i);
}

png_uint_16 PNGAPI
png_get_uint_16(png_bytep buf)
{
   png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
	  (png_uint_16)(*(buf + 1)));

   return (i);
}
#endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */

void /* PRIVATE */
png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
{
   if(png_ptr == NULL) return;
   png_read_data(png_ptr, buf, length);
   png_calculate_crc(png_ptr, buf, length);
}

int /* PRIVATE */
png_crc_finish(png_structp png_ptr, png_uint_32 skip)
{
   png_size_t i;
   png_size_t istop = png_ptr->zbuf_size;

   for (i = (png_size_t)skip; i > istop; i -= istop)
   {
	  png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
   }
   if (i)
   {
	  png_crc_read(png_ptr, png_ptr->zbuf, i);
   }

   if (png_crc_error(png_ptr))
   {
	  if (((png_ptr->chunk_name[0] & 0x20) &&		/* Ancillary */
		   !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
		  (!(png_ptr->chunk_name[0] & 0x20) &&		 /* Critical  */
		  (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
	  {
		 png_chunk_warning(png_ptr, "CRC error");
	  }
	  else
	  {
		 png_chunk_error(png_ptr, "CRC error");
	  }
	  return (1);
   }

   return (0);
}

int /* PRIVATE */
png_crc_error(png_structp png_ptr)
{
   png_byte crc_bytes[4];
   png_uint_32 crc;
   int need_crc = 1;

   if (png_ptr->chunk_name[0] & 0x20)			 /* ancillary */
   {
	  if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
		  (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
		 need_crc = 0;
   }
   else							/* critical */
   {
	  if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
		 need_crc = 0;
   }

   png_read_data(png_ptr, crc_bytes, 4);

   if (need_crc)
   {
	  crc = png_get_uint_32(crc_bytes);
	  return ((int)(crc != png_ptr->crc));
   }
   else
	  return (0);
}

#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
	defined(PNG_READ_iCCP_SUPPORTED)
png_charp /* PRIVATE */
png_decompress_chunk(png_structp png_ptr, int comp_type,
							  png_charp chunkdata, png_size_t chunklength,
							  png_size_t prefix_size, png_size_t *newlength)
{
   static PNG_CONST char msg[] = "Error decoding compressed text";
   png_charp text;
   png_size_t text_size;

   if (comp_type == PNG_COMPRESSION_TYPE_BASE)
   {
	  int ret = Z_OK;
	  png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size);
	  png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
	  png_ptr->zstream.next_out = png_ptr->zbuf;
	  png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;

	  text_size = 0;
	  text = NULL;

	  while (png_ptr->zstream.avail_in)
	  {
		 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
		 if (ret != Z_OK && ret != Z_STREAM_END)
		 {
			if (png_ptr->zstream.msg != NULL)
			   png_warning(png_ptr, png_ptr->zstream.msg);
			else
			   png_warning(png_ptr, msg);
			inflateReset(&png_ptr->zstream);
			png_ptr->zstream.avail_in = 0;

			if (text ==  NULL)
			{
			   text_size = prefix_size + png_sizeof(msg) + 1;
			   text = (png_charp)png_malloc_warn(png_ptr, text_size);
			   if (text ==  NULL)
				 {
					png_free(png_ptr,chunkdata);
					png_error(png_ptr,"Not enough memory to decompress chunk");
				 }
			   png_memcpy(text, chunkdata, prefix_size);
			}

			text[text_size - 1] = 0x00;

			text_size = (png_size_t)(chunklength - (text - chunkdata) - 1);
			text_size = png_sizeof(msg) > text_size ? text_size :
			   png_sizeof(msg);
			png_memcpy(text + prefix_size, msg, text_size + 1);
			break;
		 }
		 if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
		 {
			if (text == NULL)
			{
			   text_size = prefix_size +
				   png_ptr->zbuf_size - png_ptr->zstream.avail_out;
			   text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
			   if (text ==  NULL)
				 {
					png_free(png_ptr,chunkdata);
					png_error(png_ptr,"Not enough memory to decompress chunk.");
				 }
			   png_memcpy(text + prefix_size, png_ptr->zbuf,
					text_size - prefix_size);
			   png_memcpy(text, chunkdata, prefix_size);
			   *(text + text_size) = 0x00;
			}
			else
			{
			   png_charp tmp;

			   tmp = text;
			   text = (png_charp)png_malloc_warn(png_ptr,
				  (png_uint_32)(text_size +
				  png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
			   if (text == NULL)
			   {
				  png_free(png_ptr, tmp);
				  png_free(png_ptr, chunkdata);
				  png_error(png_ptr,"Not enough memory to decompress chunk..");
			   }
			   png_memcpy(text, tmp, text_size);
			   png_free(png_ptr, tmp);
			   png_memcpy(text + text_size, png_ptr->zbuf,
				  (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
			   text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
			   *(text + text_size) = 0x00;
			}
			if (ret == Z_STREAM_END)
			   break;
			else
			{
			   png_ptr->zstream.next_out = png_ptr->zbuf;
			   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
			}
		 }
	  }
	  if (ret != Z_STREAM_END)
	  {
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
		 char umsg[52];

		 if (ret == Z_BUF_ERROR)
			png_snprintf(umsg, 52,
				"Buffer error in compressed datastream in %s chunk",
				png_ptr->chunk_name);
		 else if (ret == Z_DATA_ERROR)
			png_snprintf(umsg, 52,
				"Data error in compressed datastream in %s chunk",
				png_ptr->chunk_name);
		 else
			png_snprintf(umsg, 52,
				"Incomplete compressed datastream in %s chunk",
				png_ptr->chunk_name);
		 png_warning(png_ptr, umsg);
#else
		 png_warning(png_ptr,
			"Incomplete compressed datastream in chunk other than IDAT");
#endif
		 text_size=prefix_size;
		 if (text ==  NULL)
		 {
			text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
			if (text == NULL)
			  {
				png_free(png_ptr, chunkdata);
				png_error(png_ptr,"Not enough memory for text.");
			  }
			png_memcpy(text, chunkdata, prefix_size);
		 }
		 *(text + text_size) = 0x00;
	  }

	  inflateReset(&png_ptr->zstream);
	  png_ptr->zstream.avail_in = 0;

	  png_free(png_ptr, chunkdata);
	  chunkdata = text;
	  *newlength=text_size;
   }
   else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
   {
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
	  char umsg[50];

	  png_snprintf(umsg, 50,
		 "Unknown zTXt compression type %d", comp_type);
	  png_warning(png_ptr, umsg);
#else
	  png_warning(png_ptr, "Unknown zTXt compression type");
#endif

	  *(chunkdata + prefix_size) = 0x00;
	  *newlength=prefix_size;
   }

   return chunkdata;
}
#endif

void /* PRIVATE */
png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_byte buf[13];
   png_uint_32 width, height;
   int bit_depth, color_type, compression_type, filter_type;
   int interlace_type;

   png_debug(1, "in png_handle_IHDR\n");

   if (png_ptr->mode & PNG_HAVE_IHDR)
	  png_error(png_ptr, "Out of place IHDR");

   if (length != 13)
	  png_error(png_ptr, "Invalid IHDR chunk");

   png_ptr->mode |= PNG_HAVE_IHDR;

   png_crc_read(png_ptr, buf, 13);
   png_crc_finish(png_ptr, 0);

   width = png_get_uint_31(png_ptr, buf);
   height = png_get_uint_31(png_ptr, buf + 4);
   bit_depth = buf[8];
   color_type = buf[9];
   compression_type = buf[10];
   filter_type = buf[11];
   interlace_type = buf[12];

   png_ptr->width = width;
   png_ptr->height = height;
   png_ptr->bit_depth = (png_byte)bit_depth;
   png_ptr->interlaced = (png_byte)interlace_type;
   png_ptr->color_type = (png_byte)color_type;
#if defined(PNG_MNG_FEATURES_SUPPORTED)
   png_ptr->filter_type = (png_byte)filter_type;
#endif
   png_ptr->compression_type = (png_byte)compression_type;

   switch (png_ptr->color_type)
   {
	  case PNG_COLOR_TYPE_GRAY:
	  case PNG_COLOR_TYPE_PALETTE:
		 png_ptr->channels = 1;
		 break;
	  case PNG_COLOR_TYPE_RGB:
		 png_ptr->channels = 3;
		 break;
	  case PNG_COLOR_TYPE_GRAY_ALPHA:
		 png_ptr->channels = 2;
		 break;
	  case PNG_COLOR_TYPE_RGB_ALPHA:
		 png_ptr->channels = 4;
		 break;
   }

   png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
   png_ptr->channels);
   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
   png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth);
   png_debug1(3,"channels = %d\n", png_ptr->channels);
   png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes);
   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
	  color_type, interlace_type, compression_type, filter_type);
}

void /* PRIVATE */
png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_color palette[PNG_MAX_PALETTE_LENGTH];
   int num, i;
#ifndef PNG_NO_POINTER_INDEXING
   png_colorp pal_ptr;
#endif

   png_debug(1, "in png_handle_PLTE\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before PLTE");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid PLTE after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (png_ptr->mode & PNG_HAVE_PLTE)
	  png_error(png_ptr, "Duplicate PLTE chunk");

   png_ptr->mode |= PNG_HAVE_PLTE;

   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
   {
	  png_warning(png_ptr,
		"Ignoring PLTE chunk in grayscale PNG");
	  png_crc_finish(png_ptr, length);
	  return;
   }
#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
   if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
   {
	  png_crc_finish(png_ptr, length);
	  return;
   }
#endif

   if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
   {
	  if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
	  {
		 png_warning(png_ptr, "Invalid palette chunk");
		 png_crc_finish(png_ptr, length);
		 return;
	  }
	  else
	  {
		 png_error(png_ptr, "Invalid palette chunk");
	  }
   }

   num = (int)length / 3;

#ifndef PNG_NO_POINTER_INDEXING
   for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
   {
	  png_byte buf[3];

	  png_crc_read(png_ptr, buf, 3);
	  pal_ptr->red = buf[0];
	  pal_ptr->green = buf[1];
	  pal_ptr->blue = buf[2];
   }
#else
   for (i = 0; i < num; i++)
   {
	  png_byte buf[3];

	  png_crc_read(png_ptr, buf, 3);
	  palette[i].red = buf[0];
	  palette[i].green = buf[1];
	  palette[i].blue = buf[2];
   }
#endif

#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
#endif
   {
	  png_crc_finish(png_ptr, 0);
   }
#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
   else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
   {
	  if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
	  {
		 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
		 {
			png_chunk_error(png_ptr, "CRC error");
		 }
		 else
		 {
			png_chunk_warning(png_ptr, "CRC error");
			return;
		 }
	  }
	  else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
	  {
		 png_chunk_warning(png_ptr, "CRC error");
	  }
   }
#endif

   png_set_PLTE(png_ptr, info_ptr, palette, num);

#if defined(PNG_READ_tRNS_SUPPORTED)
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   {
	  if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
	  {
		 if (png_ptr->num_trans > (png_uint_16)num)
		 {
			png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
			png_ptr->num_trans = (png_uint_16)num;
		 }
		 if (info_ptr->num_trans > (png_uint_16)num)
		 {
			png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
			info_ptr->num_trans = (png_uint_16)num;
		 }
	  }
   }
#endif

}

void /* PRIVATE */
png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_debug(1, "in png_handle_IEND\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
   {
	  png_error(png_ptr, "No image in file");
   }

   png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);

   if (length != 0)
   {
	  png_warning(png_ptr, "Incorrect IEND chunk length");
   }
   png_crc_finish(png_ptr, length);

   info_ptr =info_ptr; /* quiet compiler warnings about unused info_ptr */
}

#if defined(PNG_READ_gAMA_SUPPORTED)
void /* PRIVATE */
png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_fixed_point igamma;
#ifdef PNG_FLOATING_POINT_SUPPORTED
   float file_gamma;
#endif
   png_byte buf[4];

   png_debug(1, "in png_handle_gAMA\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before gAMA");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid gAMA after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (png_ptr->mode & PNG_HAVE_PLTE)
	  png_warning(png_ptr, "Out of place gAMA chunk");

   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
#if defined(PNG_READ_sRGB_SUPPORTED)
	  && !(info_ptr->valid & PNG_INFO_sRGB)
#endif
	  )
   {
	  png_warning(png_ptr, "Duplicate gAMA chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (length != 4)
   {
	  png_warning(png_ptr, "Incorrect gAMA chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_crc_read(png_ptr, buf, 4);
   if (png_crc_finish(png_ptr, 0))
	  return;

   igamma = (png_fixed_point)png_get_uint_32(buf);
   if (igamma == 0)
	  {
		 png_warning(png_ptr,
		   "Ignoring gAMA chunk with gamma=0");
		 return;
	  }

#if defined(PNG_READ_sRGB_SUPPORTED)
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
	  if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
	  {
		 png_warning(png_ptr,
		   "Ignoring incorrect gAMA value when sRGB is also present");
#ifndef PNG_NO_CONSOLE_IO
		 fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
#endif
		 return;
	  }
#endif /* PNG_READ_sRGB_SUPPORTED */

#ifdef PNG_FLOATING_POINT_SUPPORTED
   file_gamma = (float)igamma / (float)100000.0;
#  ifdef PNG_READ_GAMMA_SUPPORTED
	 png_ptr->gamma = file_gamma;
#  endif
	 png_set_gAMA(png_ptr, info_ptr, file_gamma);
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
#endif
}
#endif

#if defined(PNG_READ_sBIT_SUPPORTED)
void /* PRIVATE */
png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_size_t truelen;
   png_byte buf[4];

   png_debug(1, "in png_handle_sBIT\n");

   buf[0] = buf[1] = buf[2] = buf[3] = 0;

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before sBIT");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid sBIT after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (png_ptr->mode & PNG_HAVE_PLTE)
   {
	  png_warning(png_ptr, "Out of place sBIT chunk");
   }
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
   {
	  png_warning(png_ptr, "Duplicate sBIT chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  truelen = 3;
   else
	  truelen = (png_size_t)png_ptr->channels;

   if (length != truelen || length > 4)
   {
	  png_warning(png_ptr, "Incorrect sBIT chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_crc_read(png_ptr, buf, truelen);
   if (png_crc_finish(png_ptr, 0))
	  return;

   if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
   {
	  png_ptr->sig_bit.red = buf[0];
	  png_ptr->sig_bit.green = buf[1];
	  png_ptr->sig_bit.blue = buf[2];
	  png_ptr->sig_bit.alpha = buf[3];
   }
   else
   {
	  png_ptr->sig_bit.gray = buf[0];
	  png_ptr->sig_bit.red = buf[0];
	  png_ptr->sig_bit.green = buf[0];
	  png_ptr->sig_bit.blue = buf[0];
	  png_ptr->sig_bit.alpha = buf[1];
   }
   png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
}
#endif

#if defined(PNG_READ_cHRM_SUPPORTED)
void /* PRIVATE */
png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_byte buf[4];
#ifdef PNG_FLOATING_POINT_SUPPORTED
   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
#endif
   png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
	  int_y_green, int_x_blue, int_y_blue;

   png_uint_32 uint_x, uint_y;

   png_debug(1, "in png_handle_cHRM\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before cHRM");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid cHRM after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (png_ptr->mode & PNG_HAVE_PLTE)
	  png_warning(png_ptr, "Missing PLTE before cHRM");

   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
#if defined(PNG_READ_sRGB_SUPPORTED)
	  && !(info_ptr->valid & PNG_INFO_sRGB)
#endif
	  )
   {
	  png_warning(png_ptr, "Duplicate cHRM chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (length != 32)
   {
	  png_warning(png_ptr, "Incorrect cHRM chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_crc_read(png_ptr, buf, 4);
   uint_x = png_get_uint_32(buf);

   png_crc_read(png_ptr, buf, 4);
   uint_y = png_get_uint_32(buf);

   if (uint_x > 80000L || uint_y > 80000L ||
	  uint_x + uint_y > 100000L)
   {
	  png_warning(png_ptr, "Invalid cHRM white point");
	  png_crc_finish(png_ptr, 24);
	  return;
   }
   int_x_white = (png_fixed_point)uint_x;
   int_y_white = (png_fixed_point)uint_y;

   png_crc_read(png_ptr, buf, 4);
   uint_x = png_get_uint_32(buf);

   png_crc_read(png_ptr, buf, 4);
   uint_y = png_get_uint_32(buf);

   if (uint_x + uint_y > 100000L)
   {
	  png_warning(png_ptr, "Invalid cHRM red point");
	  png_crc_finish(png_ptr, 16);
	  return;
   }
   int_x_red = (png_fixed_point)uint_x;
   int_y_red = (png_fixed_point)uint_y;

   png_crc_read(png_ptr, buf, 4);
   uint_x = png_get_uint_32(buf);

   png_crc_read(png_ptr, buf, 4);
   uint_y = png_get_uint_32(buf);

   if (uint_x + uint_y > 100000L)
   {
	  png_warning(png_ptr, "Invalid cHRM green point");
	  png_crc_finish(png_ptr, 8);
	  return;
   }
   int_x_green = (png_fixed_point)uint_x;
   int_y_green = (png_fixed_point)uint_y;

   png_crc_read(png_ptr, buf, 4);
   uint_x = png_get_uint_32(buf);

   png_crc_read(png_ptr, buf, 4);
   uint_y = png_get_uint_32(buf);

   if (uint_x + uint_y > 100000L)
   {
	  png_warning(png_ptr, "Invalid cHRM blue point");
	  png_crc_finish(png_ptr, 0);
	  return;
   }
   int_x_blue = (png_fixed_point)uint_x;
   int_y_blue = (png_fixed_point)uint_y;

#ifdef PNG_FLOATING_POINT_SUPPORTED
   white_x = (float)int_x_white / (float)100000.0;
   white_y = (float)int_y_white / (float)100000.0;
   red_x   = (float)int_x_red   / (float)100000.0;
   red_y   = (float)int_y_red   / (float)100000.0;
   green_x = (float)int_x_green / (float)100000.0;
   green_y = (float)int_y_green / (float)100000.0;
   blue_x  = (float)int_x_blue  / (float)100000.0;
   blue_y  = (float)int_y_blue  / (float)100000.0;
#endif

#if defined(PNG_READ_sRGB_SUPPORTED)
   if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
	  {
	  if (PNG_OUT_OF_RANGE(int_x_white, 31270,  1000) ||
		  PNG_OUT_OF_RANGE(int_y_white, 32900,  1000) ||
		  PNG_OUT_OF_RANGE(int_x_red,   64000L, 1000) ||
		  PNG_OUT_OF_RANGE(int_y_red,   33000,  1000) ||
		  PNG_OUT_OF_RANGE(int_x_green, 30000,  1000) ||
		  PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
		  PNG_OUT_OF_RANGE(int_x_blue,  15000,  1000) ||
		  PNG_OUT_OF_RANGE(int_y_blue,   6000,  1000))
		 {
			png_warning(png_ptr,
			  "Ignoring incorrect cHRM value when sRGB is also present");
#ifndef PNG_NO_CONSOLE_IO
#ifdef PNG_FLOATING_POINT_SUPPORTED
			fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n",
			   white_x, white_y, red_x, red_y);
			fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n",
			   green_x, green_y, blue_x, blue_y);
#else
			fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
			   int_x_white, int_y_white, int_x_red, int_y_red);
			fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
			   int_x_green, int_y_green, int_x_blue, int_y_blue);
#endif
#endif /* PNG_NO_CONSOLE_IO */
		 }
		 png_crc_finish(png_ptr, 0);
		 return;
	  }
#endif /* PNG_READ_sRGB_SUPPORTED */

#ifdef PNG_FLOATING_POINT_SUPPORTED
   png_set_cHRM(png_ptr, info_ptr,
	  white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_set_cHRM_fixed(png_ptr, info_ptr,
	  int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
	  int_y_green, int_x_blue, int_y_blue);
#endif
   if (png_crc_finish(png_ptr, 0))
	  return;
}
#endif

#if defined(PNG_READ_sRGB_SUPPORTED)
void /* PRIVATE */
png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   int intent;
   png_byte buf[1];

   png_debug(1, "in png_handle_sRGB\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before sRGB");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid sRGB after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (png_ptr->mode & PNG_HAVE_PLTE)
	  png_warning(png_ptr, "Out of place sRGB chunk");

   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
   {
	  png_warning(png_ptr, "Duplicate sRGB chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (length != 1)
   {
	  png_warning(png_ptr, "Incorrect sRGB chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_crc_read(png_ptr, buf, 1);
   if (png_crc_finish(png_ptr, 0))
	  return;

   intent = buf[0];
   if (intent >= PNG_sRGB_INTENT_LAST)
   {
	  png_warning(png_ptr, "Unknown sRGB intent");
	  return;
   }

#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
   {
   png_fixed_point igamma;
#ifdef PNG_FIXED_POINT_SUPPORTED
	  igamma=info_ptr->int_gamma;
#else
#  ifdef PNG_FLOATING_POINT_SUPPORTED
	  igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
#  endif
#endif
	  if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
	  {
		 png_warning(png_ptr,
		   "Ignoring incorrect gAMA value when sRGB is also present");
#ifndef PNG_NO_CONSOLE_IO
#  ifdef PNG_FIXED_POINT_SUPPORTED
		 fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma);
#  else
#	ifdef PNG_FLOATING_POINT_SUPPORTED
		 fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma);
#	endif
#  endif
#endif
	  }
   }
#endif /* PNG_READ_gAMA_SUPPORTED */

#ifdef PNG_READ_cHRM_SUPPORTED
#ifdef PNG_FIXED_POINT_SUPPORTED
   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
	  if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270,  1000) ||
		  PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900,  1000) ||
		  PNG_OUT_OF_RANGE(info_ptr->int_x_red,   64000L, 1000) ||
		  PNG_OUT_OF_RANGE(info_ptr->int_y_red,   33000,  1000) ||
		  PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000,  1000) ||
		  PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
		  PNG_OUT_OF_RANGE(info_ptr->int_x_blue,  15000,  1000) ||
		  PNG_OUT_OF_RANGE(info_ptr->int_y_blue,   6000,  1000))
		 {
			png_warning(png_ptr,
			  "Ignoring incorrect cHRM value when sRGB is also present");
		 }
#endif /* PNG_FIXED_POINT_SUPPORTED */
#endif /* PNG_READ_cHRM_SUPPORTED */

   png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
}
#endif /* PNG_READ_sRGB_SUPPORTED */

#if defined(PNG_READ_iCCP_SUPPORTED)
void /* PRIVATE */
png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_charp chunkdata;
   png_byte compression_type;
   png_bytep pC;
   png_charp profile;
   png_uint_32 skip = 0;
   png_uint_32 profile_size, profile_length;
   png_size_t slength, prefix_length, data_length;

   png_debug(1, "in png_handle_iCCP\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before iCCP");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid iCCP after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (png_ptr->mode & PNG_HAVE_PLTE)
	  png_warning(png_ptr, "Out of place iCCP chunk");

   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
   {
	  png_warning(png_ptr, "Duplicate iCCP chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
	  png_warning(png_ptr, "iCCP chunk too large to fit in memory");
	  skip = length - (png_uint_32)65535L;
	  length = (png_uint_32)65535L;
   }
#endif

   chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
   slength = (png_size_t)length;
   png_crc_read(png_ptr, (png_bytep)chunkdata, slength);

   if (png_crc_finish(png_ptr, skip))
   {
	  png_free(png_ptr, chunkdata);
	  return;
   }

   chunkdata[slength] = 0x00;

   for (profile = chunkdata; *profile; profile++)
 ;

   ++profile;

   if ( profile >= chunkdata + slength - 1)
   {
	  png_free(png_ptr, chunkdata);
	  png_warning(png_ptr, "Malformed iCCP chunk");
	  return;
   }

   compression_type = *profile++;
   if (compression_type)
   {
	  png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
	  compression_type=0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8
								 wrote nonzero) */
   }

   prefix_length = profile - chunkdata;
   chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata,
									slength, prefix_length, &data_length);

   profile_length = data_length - prefix_length;

   if ( prefix_length > data_length || profile_length < 4)
   {
	  png_free(png_ptr, chunkdata);
	  png_warning(png_ptr, "Profile size field missing from iCCP chunk");
	  return;
   }

   pC = (png_bytep)(chunkdata+prefix_length);
   profile_size = ((*(pC  ))<<24) |
				  ((*(pC+1))<<16) |
				  ((*(pC+2))<< 8) |
				  ((*(pC+3))	);

   if(profile_size < profile_length)
	  profile_length = profile_size;

   if(profile_size > profile_length)
   {
	  png_free(png_ptr, chunkdata);
	  png_warning(png_ptr, "Ignoring truncated iCCP profile.");
	  return;
   }

   png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type,
				chunkdata + prefix_length, profile_length);
   png_free(png_ptr, chunkdata);
}
#endif /* PNG_READ_iCCP_SUPPORTED */

#if defined(PNG_READ_sPLT_SUPPORTED)
void /* PRIVATE */
png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_bytep chunkdata;
   png_bytep entry_start;
   png_sPLT_t new_palette;
#ifdef PNG_NO_POINTER_INDEXING
   png_sPLT_entryp pp;
#endif
   int data_length, entry_size, i;
   png_uint_32 skip = 0;
   png_size_t slength;

   png_debug(1, "in png_handle_sPLT\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before sPLT");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid sPLT after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
	  png_warning(png_ptr, "sPLT chunk too large to fit in memory");
	  skip = length - (png_uint_32)65535L;
	  length = (png_uint_32)65535L;
   }
#endif

   chunkdata = (png_bytep)png_malloc(png_ptr, length + 1);
   slength = (png_size_t)length;
   png_crc_read(png_ptr, (png_bytep)chunkdata, slength);

   if (png_crc_finish(png_ptr, skip))
   {
	  png_free(png_ptr, chunkdata);
	  return;
   }

   chunkdata[slength] = 0x00;

   for (entry_start = chunkdata; *entry_start; entry_start++)
 ;
   ++entry_start;

   if (entry_start > chunkdata + slength - 2)
   {
	  png_free(png_ptr, chunkdata);
	  png_warning(png_ptr, "malformed sPLT chunk");
	  return;
   }

   new_palette.depth = *entry_start++;
   entry_size = (new_palette.depth == 8 ? 6 : 10);
   data_length = (slength - (entry_start - chunkdata));

   if (data_length % entry_size)
   {
	  png_free(png_ptr, chunkdata);
	  png_warning(png_ptr, "sPLT chunk has bad length");
	  return;
   }

   new_palette.nentries = (png_int_32) ( data_length / entry_size);
   if ((png_uint_32) new_palette.nentries > (png_uint_32) (PNG_SIZE_MAX /
	   png_sizeof(png_sPLT_entry)))
   {
	   png_warning(png_ptr, "sPLT chunk too long");
	   return;
   }
   new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
	   png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
   if (new_palette.entries == NULL)
   {
	   png_warning(png_ptr, "sPLT chunk requires too much memory");
	   return;
   }

#ifndef PNG_NO_POINTER_INDEXING
   for (i = 0; i < new_palette.nentries; i++)
   {
	  png_sPLT_entryp pp = new_palette.entries + i;

	  if (new_palette.depth == 8)
	  {
		  pp->red = *entry_start++;
		  pp->green = *entry_start++;
		  pp->blue = *entry_start++;
		  pp->alpha = *entry_start++;
	  }
	  else
	  {
		  pp->red   = png_get_uint_16(entry_start); entry_start += 2;
		  pp->green = png_get_uint_16(entry_start); entry_start += 2;
		  pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
		  pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
	  }
	  pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
   }
#else
   pp = new_palette.entries;
   for (i = 0; i < new_palette.nentries; i++)
   {

	  if (new_palette.depth == 8)
	  {
		  pp[i].red   = *entry_start++;
		  pp[i].green = *entry_start++;
		  pp[i].blue  = *entry_start++;
		  pp[i].alpha = *entry_start++;
	  }
	  else
	  {
		  pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
		  pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
		  pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
		  pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
	  }
	  pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
   }
#endif

   new_palette.name = (png_charp)chunkdata;

   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);

   png_free(png_ptr, chunkdata);
   png_free(png_ptr, new_palette.entries);
}
#endif /* PNG_READ_sPLT_SUPPORTED */

#if defined(PNG_READ_tRNS_SUPPORTED)
void /* PRIVATE */
png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
   int bit_mask;

   png_debug(1, "in png_handle_tRNS\n");

   bit_mask = (1 << png_ptr->bit_depth) - 1;

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before tRNS");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid tRNS after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
   {
	  png_warning(png_ptr, "Duplicate tRNS chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
   {
	  png_byte buf[2];

	  if (length != 2)
	  {
		 png_warning(png_ptr, "Incorrect tRNS chunk length");
		 png_crc_finish(png_ptr, length);
		 return;
	  }

	  png_crc_read(png_ptr, buf, 2);
	  png_ptr->num_trans = 1;
	  png_ptr->trans_values.gray = png_get_uint_16(buf) & bit_mask;
   }
   else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
   {
	  png_byte buf[6];

	  if (length != 6)
	  {
		 png_warning(png_ptr, "Incorrect tRNS chunk length");
		 png_crc_finish(png_ptr, length);
		 return;
	  }
	  png_crc_read(png_ptr, buf, (png_size_t)length);
	  png_ptr->num_trans = 1;
	  png_ptr->trans_values.red = png_get_uint_16(buf) & bit_mask;
	  png_ptr->trans_values.green = png_get_uint_16(buf + 2) & bit_mask;
	  png_ptr->trans_values.blue = png_get_uint_16(buf + 4) & bit_mask;
   }
   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   {
	  if (!(png_ptr->mode & PNG_HAVE_PLTE))
	  {
		 png_warning(png_ptr, "Missing PLTE before tRNS");
	  }
	  if (length > (png_uint_32)png_ptr->num_palette ||
		  length > PNG_MAX_PALETTE_LENGTH)
	  {
		 png_warning(png_ptr, "Incorrect tRNS chunk length");
		 png_crc_finish(png_ptr, length);
		 return;
	  }
	  if (length == 0)
	  {
		 png_warning(png_ptr, "Zero length tRNS chunk");
		 png_crc_finish(png_ptr, length);
		 return;
	  }
	  png_crc_read(png_ptr, readbuf, (png_size_t)length);
	  png_ptr->num_trans = (png_uint_16)length;
   }
   else
   {
	  png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (png_crc_finish(png_ptr, 0))
   {
	  png_ptr->num_trans = 0;
	  return;
   }

   png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
	  &(png_ptr->trans_values));
}
#endif

#if defined(PNG_READ_bKGD_SUPPORTED)
void /* PRIVATE */
png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_size_t truelen;
   png_byte buf[6];

   png_debug(1, "in png_handle_bKGD\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before bKGD");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid bKGD after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
			!(png_ptr->mode & PNG_HAVE_PLTE))
   {
	  png_warning(png_ptr, "Missing PLTE before bKGD");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
   {
	  png_warning(png_ptr, "Duplicate bKGD chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  truelen = 1;
   else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
	  truelen = 6;
   else
	  truelen = 2;

   if (length != truelen)
   {
	  png_warning(png_ptr, "Incorrect bKGD chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_crc_read(png_ptr, buf, truelen);
   if (png_crc_finish(png_ptr, 0))
	  return;

   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   {
	  png_ptr->background.index = buf[0];
	  if(info_ptr->num_palette)
	  {
		  if(buf[0] > info_ptr->num_palette)
		  {
			 png_warning(png_ptr, "Incorrect bKGD chunk index value");
			 return;
		  }
		  png_ptr->background.red =
			 (png_uint_16)png_ptr->palette[buf[0]].red;
		  png_ptr->background.green =
			 (png_uint_16)png_ptr->palette[buf[0]].green;
		  png_ptr->background.blue =
			 (png_uint_16)png_ptr->palette[buf[0]].blue;
	  }
   }
   else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
   {
	  png_ptr->background.red =
	  png_ptr->background.green =
	  png_ptr->background.blue =
	  png_ptr->background.gray = png_get_uint_16(buf);
   }
   else
   {
	  png_ptr->background.red = png_get_uint_16(buf);
	  png_ptr->background.green = png_get_uint_16(buf + 2);
	  png_ptr->background.blue = png_get_uint_16(buf + 4);
   }

   png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
}
#endif

#if defined(PNG_READ_hIST_SUPPORTED)
void /* PRIVATE */
png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   unsigned int num, i;
   png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];

   png_debug(1, "in png_handle_hIST\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before hIST");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid hIST after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (!(png_ptr->mode & PNG_HAVE_PLTE))
   {
	  png_warning(png_ptr, "Missing PLTE before hIST");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
   {
	  png_warning(png_ptr, "Duplicate hIST chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   num = length / 2 ;
   if (num != (unsigned int) png_ptr->num_palette || num >
	  (unsigned int) PNG_MAX_PALETTE_LENGTH)
   {
	  png_warning(png_ptr, "Incorrect hIST chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   for (i = 0; i < num; i++)
   {
	  png_byte buf[2];

	  png_crc_read(png_ptr, buf, 2);
	  readbuf[i] = png_get_uint_16(buf);
   }

   if (png_crc_finish(png_ptr, 0))
	  return;

   png_set_hIST(png_ptr, info_ptr, readbuf);
}
#endif

#if defined(PNG_READ_pHYs_SUPPORTED)
void /* PRIVATE */
png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_byte buf[9];
   png_uint_32 res_x, res_y;
   int unit_type;

   png_debug(1, "in png_handle_pHYs\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before pHYs");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid pHYs after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
   {
	  png_warning(png_ptr, "Duplicate pHYs chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (length != 9)
   {
	  png_warning(png_ptr, "Incorrect pHYs chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_crc_read(png_ptr, buf, 9);
   if (png_crc_finish(png_ptr, 0))
	  return;

   res_x = png_get_uint_32(buf);
   res_y = png_get_uint_32(buf + 4);
   unit_type = buf[8];
   png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
}
#endif

#if defined(PNG_READ_oFFs_SUPPORTED)
void /* PRIVATE */
png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_byte buf[9];
   png_int_32 offset_x, offset_y;
   int unit_type;

   png_debug(1, "in png_handle_oFFs\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before oFFs");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid oFFs after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
   {
	  png_warning(png_ptr, "Duplicate oFFs chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (length != 9)
   {
	  png_warning(png_ptr, "Incorrect oFFs chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_crc_read(png_ptr, buf, 9);
   if (png_crc_finish(png_ptr, 0))
	  return;

   offset_x = png_get_int_32(buf);
   offset_y = png_get_int_32(buf + 4);
   unit_type = buf[8];
   png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
}
#endif

#if defined(PNG_READ_pCAL_SUPPORTED)
void /* PRIVATE */
png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_charp purpose;
   png_int_32 X0, X1;
   png_byte type, nparams;
   png_charp buf, units, endptr;
   png_charpp params;
   png_size_t slength;
   int i;

   png_debug(1, "in png_handle_pCAL\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before pCAL");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid pCAL after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
   {
	  png_warning(png_ptr, "Duplicate pCAL chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
	  length + 1);
   purpose = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (purpose == NULL)
	 {
	   png_warning(png_ptr, "No memory for pCAL purpose.");
	   return;
	 }
   slength = (png_size_t)length;
   png_crc_read(png_ptr, (png_bytep)purpose, slength);

   if (png_crc_finish(png_ptr, 0))
   {
	  png_free(png_ptr, purpose);
	  return;
   }

   purpose[slength] = 0x00; /* null terminate the last string */

   png_debug(3, "Finding end of pCAL purpose string\n");
   for (buf = purpose; *buf; buf++)
 ;

   endptr = purpose + slength;

   if (endptr <= buf + 12)
   {
	  png_warning(png_ptr, "Invalid pCAL data");
	  png_free(png_ptr, purpose);
	  return;
   }

   png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
   X0 = png_get_int_32((png_bytep)buf+1);
   X1 = png_get_int_32((png_bytep)buf+5);
   type = buf[9];
   nparams = buf[10];
   units = buf + 11;

   png_debug(3, "Checking pCAL equation type and number of parameters\n");
   if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
	   (type == PNG_EQUATION_BASE_E && nparams != 3) ||
	   (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
	   (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
   {
	  png_warning(png_ptr, "Invalid pCAL parameters for equation type");
	  png_free(png_ptr, purpose);
	  return;
   }
   else if (type >= PNG_EQUATION_LAST)
   {
	  png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
   }

   for (buf = units; *buf; buf++)
 ;

   png_debug(3, "Allocating pCAL parameters array\n");
   params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)(nparams
	  *png_sizeof(png_charp))) ;
   if (params == NULL)
	 {
	   png_free(png_ptr, purpose);
	   png_warning(png_ptr, "No memory for pCAL params.");
	   return;
	 }

   for (i = 0; i < (int)nparams; i++)
   {
	  buf++; /* Skip the null string terminator from previous parameter. */

	  png_debug1(3, "Reading pCAL parameter %d\n", i);
	  for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
 ;

	  if (buf > endptr)
	  {
		 png_warning(png_ptr, "Invalid pCAL data");
		 png_free(png_ptr, purpose);
		 png_free(png_ptr, params);
		 return;
	  }
   }

   png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams,
	  units, params);

   png_free(png_ptr, purpose);
   png_free(png_ptr, params);
}
#endif

#if defined(PNG_READ_sCAL_SUPPORTED)
void /* PRIVATE */
png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_charp buffer, ep;
#ifdef PNG_FLOATING_POINT_SUPPORTED
   double width, height;
   png_charp vp;
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_charp swidth, sheight;
#endif
#endif
   png_size_t slength;

   png_debug(1, "in png_handle_sCAL\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before sCAL");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
	  png_warning(png_ptr, "Invalid sCAL after IDAT");
	  png_crc_finish(png_ptr, length);
	  return;
   }
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
   {
	  png_warning(png_ptr, "Duplicate sCAL chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
	  length + 1);
   buffer = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (buffer == NULL)
	 {
	   png_warning(png_ptr, "Out of memory while processing sCAL chunk");
	   return;
	 }
   slength = (png_size_t)length;
   png_crc_read(png_ptr, (png_bytep)buffer, slength);

   if (png_crc_finish(png_ptr, 0))
   {
	  png_free(png_ptr, buffer);
	  return;
   }

   buffer[slength] = 0x00; /* null terminate the last string */

   ep = buffer + 1;	/* skip unit byte */

#ifdef PNG_FLOATING_POINT_SUPPORTED
   width = png_strtod(png_ptr, ep, &vp);
   if (*vp)
   {
	   png_warning(png_ptr, "malformed width string in sCAL chunk");
	   return;
   }
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
   swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
   if (swidth == NULL)
	 {
	   png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
	   return;
	 }
   png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
#endif
#endif

   for (ep = buffer; *ep; ep++)
 ;
   ep++;

   if (buffer + slength < ep)
   {
	   png_warning(png_ptr, "Truncated sCAL chunk");
#if defined(PNG_FIXED_POINT_SUPPORTED) && \
	!defined(PNG_FLOATING_POINT_SUPPORTED)
	   png_free(png_ptr, swidth);
#endif
	  png_free(png_ptr, buffer);
	   return;
   }

#ifdef PNG_FLOATING_POINT_SUPPORTED
   height = png_strtod(png_ptr, ep, &vp);
   if (*vp)
   {
	   png_warning(png_ptr, "malformed height string in sCAL chunk");
	   return;
   }
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
   sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
   if (swidth == NULL)
	 {
	   png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
	   return;
	 }
   png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
#endif
#endif

   if (buffer + slength < ep
#ifdef PNG_FLOATING_POINT_SUPPORTED
	  || width <= 0. || height <= 0.
#endif
	  )
   {
	  png_warning(png_ptr, "Invalid sCAL data");
	  png_free(png_ptr, buffer);
#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
	  png_free(png_ptr, swidth);
	  png_free(png_ptr, sheight);
#endif
	  return;
   }

#ifdef PNG_FLOATING_POINT_SUPPORTED
   png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height);
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight);
#endif
#endif

   png_free(png_ptr, buffer);
#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
   png_free(png_ptr, swidth);
   png_free(png_ptr, sheight);
#endif
}
#endif

#if defined(PNG_READ_tIME_SUPPORTED)
void /* PRIVATE */
png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_byte buf[7];
   png_time mod_time;

   png_debug(1, "in png_handle_tIME\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Out of place tIME chunk");
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
   {
	  png_warning(png_ptr, "Duplicate tIME chunk");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   if (png_ptr->mode & PNG_HAVE_IDAT)
	  png_ptr->mode |= PNG_AFTER_IDAT;

   if (length != 7)
   {
	  png_warning(png_ptr, "Incorrect tIME chunk length");
	  png_crc_finish(png_ptr, length);
	  return;
   }

   png_crc_read(png_ptr, buf, 7);
   if (png_crc_finish(png_ptr, 0))
	  return;

   mod_time.second = buf[6];
   mod_time.minute = buf[5];
   mod_time.hour = buf[4];
   mod_time.day = buf[3];
   mod_time.month = buf[2];
   mod_time.year = png_get_uint_16(buf);

   png_set_tIME(png_ptr, info_ptr, &mod_time);
}
#endif

#if defined(PNG_READ_tEXt_SUPPORTED)
void /* PRIVATE */
png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_textp text_ptr;
   png_charp key;
   png_charp text;
   png_uint_32 skip = 0;
   png_size_t slength;
   int ret;

   png_debug(1, "in png_handle_tEXt\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before tEXt");

   if (png_ptr->mode & PNG_HAVE_IDAT)
	  png_ptr->mode |= PNG_AFTER_IDAT;

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
	  png_warning(png_ptr, "tEXt chunk too large to fit in memory");
	  skip = length - (png_uint_32)65535L;
	  length = (png_uint_32)65535L;
   }
#endif

   key = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (key == NULL)
   {
	 png_warning(png_ptr, "No memory to process text chunk.");
	 return;
   }
   slength = (png_size_t)length;
   png_crc_read(png_ptr, (png_bytep)key, slength);

   if (png_crc_finish(png_ptr, skip))
   {
	  png_free(png_ptr, key);
	  return;
   }

   key[slength] = 0x00;

   for (text = key; *text; text++)
 ;

   if (text != key + slength)
	  text++;

   text_ptr = (png_textp)png_malloc_warn(png_ptr,
	  (png_uint_32)png_sizeof(png_text));
   if (text_ptr == NULL)
   {
	 png_warning(png_ptr, "Not enough memory to process text chunk.");
	 png_free(png_ptr, key);
	 return;
   }
   text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
   text_ptr->key = key;
#ifdef PNG_iTXt_SUPPORTED
   text_ptr->lang = NULL;
   text_ptr->lang_key = NULL;
   text_ptr->itxt_length = 0;
#endif
   text_ptr->text = text;
   text_ptr->text_length = png_strlen(text);

   ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);

   png_free(png_ptr, key);
   png_free(png_ptr, text_ptr);
   if (ret)
	 png_warning(png_ptr, "Insufficient memory to process text chunk.");
}
#endif

#if defined(PNG_READ_zTXt_SUPPORTED)
void /* PRIVATE */
png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_textp text_ptr;
   png_charp chunkdata;
   png_charp text;
   int comp_type;
   int ret;
   png_size_t slength, prefix_len, data_len;

   png_debug(1, "in png_handle_zTXt\n");
   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before zTXt");

   if (png_ptr->mode & PNG_HAVE_IDAT)
	  png_ptr->mode |= PNG_AFTER_IDAT;

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
	 png_warning(png_ptr,"zTXt chunk too large to fit in memory");
	 png_crc_finish(png_ptr, length);
	 return;
   }
#endif

   chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (chunkdata == NULL)
   {
	 png_warning(png_ptr,"Out of memory processing zTXt chunk.");
	 return;
   }
   slength = (png_size_t)length;
   png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
   if (png_crc_finish(png_ptr, 0))
   {
	  png_free(png_ptr, chunkdata);
	  return;
   }

   chunkdata[slength] = 0x00;

   for (text = chunkdata; *text; text++)
 ;

   if (text >= chunkdata + slength - 2)
   {
	  png_warning(png_ptr, "Truncated zTXt chunk");
	  png_free(png_ptr, chunkdata);
	  return;
   }
   else
   {
	   comp_type = *(++text);
	   if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
	   {
		  png_warning(png_ptr, "Unknown compression type in zTXt chunk");
		  comp_type = PNG_TEXT_COMPRESSION_zTXt;
	   }
	   text++;	/* skip the compression_method byte */
   }
   prefix_len = text - chunkdata;

   chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata,
									(png_size_t)length, prefix_len, &data_len);

   text_ptr = (png_textp)png_malloc_warn(png_ptr,
	 (png_uint_32)png_sizeof(png_text));
   if (text_ptr == NULL)
   {
	 png_warning(png_ptr,"Not enough memory to process zTXt chunk.");
	 png_free(png_ptr, chunkdata);
	 return;
   }
   text_ptr->compression = comp_type;
   text_ptr->key = chunkdata;
#ifdef PNG_iTXt_SUPPORTED
   text_ptr->lang = NULL;
   text_ptr->lang_key = NULL;
   text_ptr->itxt_length = 0;
#endif
   text_ptr->text = chunkdata + prefix_len;
   text_ptr->text_length = data_len;

   ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);

   png_free(png_ptr, text_ptr);
   png_free(png_ptr, chunkdata);
   if (ret)
	 png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
}
#endif

#if defined(PNG_READ_iTXt_SUPPORTED)
void /* PRIVATE */
png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_textp text_ptr;
   png_charp chunkdata;
   png_charp key, lang, text, lang_key;
   int comp_flag;
   int comp_type = 0;
   int ret;
   png_size_t slength, prefix_len, data_len;

   png_debug(1, "in png_handle_iTXt\n");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
	  png_error(png_ptr, "Missing IHDR before iTXt");

   if (png_ptr->mode & PNG_HAVE_IDAT)
	  png_ptr->mode |= PNG_AFTER_IDAT;

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
	 png_warning(png_ptr,"iTXt chunk too large to fit in memory");
	 png_crc_finish(png_ptr, length);
	 return;
   }
#endif

   chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
   if (chunkdata == NULL)
   {
	 png_warning(png_ptr, "No memory to process iTXt chunk.");
	 return;
   }
   slength = (png_size_t)length;
   png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
   if (png_crc_finish(png_ptr, 0))
   {
	  png_free(png_ptr, chunkdata);
	  return;
   }

   chunkdata[slength] = 0x00;

   for (lang = chunkdata; *lang; lang++)
 ;
   lang++;	/* skip NUL separator */

   if (lang >= chunkdata + slength - 3)
   {
	  png_warning(png_ptr, "Truncated iTXt chunk");
	  png_free(png_ptr, chunkdata);
	  return;
   }
   else
   {
	   comp_flag = *lang++;
	   comp_type = *lang++;
   }

   for (lang_key = lang; *lang_key; lang_key++)
 ;
   lang_key++;	/* skip NUL separator */

   if (lang_key >= chunkdata + slength)
   {
	  png_warning(png_ptr, "Truncated iTXt chunk");
	  png_free(png_ptr, chunkdata);
	  return;
   }

   for (text = lang_key; *text; text++)
 ;
   text++;	/* skip NUL separator */
   if (text >= chunkdata + slength)
   {
	  png_warning(png_ptr, "Malformed iTXt chunk");
	  png_free(png_ptr, chunkdata);
	  return;
   }

   prefix_len = text - chunkdata;

   key=chunkdata;
   if (comp_flag)
	   chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata,
		  (size_t)length, prefix_len, &data_len);
   else
	   data_len=png_strlen(chunkdata + prefix_len);
   text_ptr = (png_textp)png_malloc_warn(png_ptr,
	  (png_uint_32)png_sizeof(png_text));
   if (text_ptr == NULL)
   {
	 png_warning(png_ptr,"Not enough memory to process iTXt chunk.");
	 png_free(png_ptr, chunkdata);
	 return;
   }
   text_ptr->compression = (int)comp_flag + 1;
   text_ptr->lang_key = chunkdata+(lang_key-key);
   text_ptr->lang = chunkdata+(lang-key);
   text_ptr->itxt_length = data_len;
   text_ptr->text_length = 0;
   text_ptr->key = chunkdata;
   text_ptr->text = chunkdata + prefix_len;

   ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);

   png_free(png_ptr, text_ptr);
   png_free(png_ptr, chunkdata);
   if (ret)
	 png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
}
#endif

void /* PRIVATE */
png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_uint_32 skip = 0;

   png_debug(1, "in png_handle_unknown\n");

   if (png_ptr->mode & PNG_HAVE_IDAT)
   {
#ifdef PNG_USE_LOCAL_ARRAYS
	  PNG_CONST PNG_IDAT;
#endif
	  if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* not an IDAT */
		 png_ptr->mode |= PNG_AFTER_IDAT;
   }

   png_check_chunk_name(png_ptr, png_ptr->chunk_name);

   if (!(png_ptr->chunk_name[0] & 0x20))
   {
#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
	  if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
		   PNG_HANDLE_CHUNK_ALWAYS
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
		   && png_ptr->read_user_chunk_fn == NULL
#endif
		)
#endif
		  png_chunk_error(png_ptr, "unknown critical chunk");
   }

#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
   if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
	   (png_ptr->read_user_chunk_fn != NULL))
   {
#ifdef PNG_MAX_MALLOC_64K
	   if (length > (png_uint_32)65535L)
	   {
		   png_warning(png_ptr, "unknown chunk too large to fit in memory");
		   skip = length - (png_uint_32)65535L;
		   length = (png_uint_32)65535L;
	   }
#endif
	   png_strncpy((png_charp)png_ptr->unknown_chunk.name,
	 (png_charp)png_ptr->chunk_name, 5);
	   png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
	   png_ptr->unknown_chunk.size = (png_size_t)length;
	   png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
	   if(png_ptr->read_user_chunk_fn != NULL)
	   {
		  int ret;
		  ret = (*(png_ptr->read_user_chunk_fn))
			(png_ptr, &png_ptr->unknown_chunk);
		  if (ret < 0)
			 png_chunk_error(png_ptr, "error in user chunk");
		  if (ret == 0)
		  {
			 if (!(png_ptr->chunk_name[0] & 0x20))
				if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
					 PNG_HANDLE_CHUNK_ALWAYS)
				   png_chunk_error(png_ptr, "unknown critical chunk");
			 png_set_unknown_chunks(png_ptr, info_ptr,
			   &png_ptr->unknown_chunk, 1);
		  }
	   }
#else
	   png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
#endif
	   png_free(png_ptr, png_ptr->unknown_chunk.data);
	   png_ptr->unknown_chunk.data = NULL;
   }
   else
#endif
	  skip = length;

   png_crc_finish(png_ptr, skip);

#if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
   info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
#endif
}

#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))

void /* PRIVATE */
png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
{
   png_debug(1, "in png_check_chunk_name\n");
   if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
	   isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
   {
	  png_chunk_error(png_ptr, "invalid chunk type");
   }
}

void /* PRIVATE */
png_combine_row(png_structp png_ptr, png_bytep row, int mask)
{
   png_debug(1,"in png_combine_row\n");
   if (mask == 0xff)
   {
	  png_memcpy(row, png_ptr->row_buf + 1,
		 PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
   }
   else
   {
	  switch (png_ptr->row_info.pixel_depth)
	  {
		 case 1:
		 {
			png_bytep sp = png_ptr->row_buf + 1;
			png_bytep dp = row;
			int s_inc, s_start, s_end;
			int m = 0x80;
			int shift;
			png_uint_32 i;
			png_uint_32 row_width = png_ptr->width;

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
			if (png_ptr->transformations & PNG_PACKSWAP)
			{
				s_start = 0;
				s_end = 7;
				s_inc = 1;
			}
			else
#endif
			{
				s_start = 7;
				s_end = 0;
				s_inc = -1;
			}

			shift = s_start;

			for (i = 0; i < row_width; i++)
			{
			   if (m & mask)
			   {
				  int value;

				  value = (*sp >> shift) & 0x01;
				  *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
				  *dp |= (png_byte)(value << shift);
			   }

			   if (shift == s_end)
			   {
				  shift = s_start;
				  sp++;
				  dp++;
			   }
			   else
				  shift += s_inc;

			   if (m == 1)
				  m = 0x80;
			   else
				  m >>= 1;
			}
			break;
		 }
		 case 2:
		 {
			png_bytep sp = png_ptr->row_buf + 1;
			png_bytep dp = row;
			int s_start, s_end, s_inc;
			int m = 0x80;
			int shift;
			png_uint_32 i;
			png_uint_32 row_width = png_ptr->width;
			int value;

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
			if (png_ptr->transformations & PNG_PACKSWAP)
			{
			   s_start = 0;
			   s_end = 6;
			   s_inc = 2;
			}
			else
#endif
			{
			   s_start = 6;
			   s_end = 0;
			   s_inc = -2;
			}

			shift = s_start;

			for (i = 0; i < row_width; i++)
			{
			   if (m & mask)
			   {
				  value = (*sp >> shift) & 0x03;
				  *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
				  *dp |= (png_byte)(value << shift);
			   }

			   if (shift == s_end)
			   {
				  shift = s_start;
				  sp++;
				  dp++;
			   }
			   else
				  shift += s_inc;
			   if (m == 1)
				  m = 0x80;
			   else
				  m >>= 1;
			}
			break;
		 }
		 case 4:
		 {
			png_bytep sp = png_ptr->row_buf + 1;
			png_bytep dp = row;
			int s_start, s_end, s_inc;
			int m = 0x80;
			int shift;
			png_uint_32 i;
			png_uint_32 row_width = png_ptr->width;
			int value;

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
			if (png_ptr->transformations & PNG_PACKSWAP)
			{
			   s_start = 0;
			   s_end = 4;
			   s_inc = 4;
			}
			else
#endif
			{
			   s_start = 4;
			   s_end = 0;
			   s_inc = -4;
			}
			shift = s_start;

			for (i = 0; i < row_width; i++)
			{
			   if (m & mask)
			   {
				  value = (*sp >> shift) & 0xf;
				  *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
				  *dp |= (png_byte)(value << shift);
			   }

			   if (shift == s_end)
			   {
				  shift = s_start;
				  sp++;
				  dp++;
			   }
			   else
				  shift += s_inc;
			   if (m == 1)
				  m = 0x80;
			   else
				  m >>= 1;
			}
			break;
		 }
		 default:
		 {
			png_bytep sp = png_ptr->row_buf + 1;
			png_bytep dp = row;
			png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
			png_uint_32 i;
			png_uint_32 row_width = png_ptr->width;
			png_byte m = 0x80;

			for (i = 0; i < row_width; i++)
			{
			   if (m & mask)
			   {
				  png_memcpy(dp, sp, pixel_bytes);
			   }

			   sp += pixel_bytes;
			   dp += pixel_bytes;

			   if (m == 1)
				  m = 0x80;
			   else
				  m >>= 1;
			}
			break;
		 }
	  }
   }
}

#ifdef PNG_READ_INTERLACING_SUPPORTED
void /* PRIVATE */
png_do_read_interlace(png_structp png_ptr)
{
   png_row_infop row_info = &(png_ptr->row_info);
   png_bytep row = png_ptr->row_buf + 1;
   int pass = png_ptr->pass;
   png_uint_32 transformations = png_ptr->transformations;
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
#endif

   png_debug(1,"in png_do_read_interlace\n");
   if (row != NULL && row_info != NULL)
   {
	  png_uint_32 final_width;

	  final_width = row_info->width * png_pass_inc[pass];

	  switch (row_info->pixel_depth)
	  {
		 case 1:
		 {
			png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
			png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
			int sshift, dshift;
			int s_start, s_end, s_inc;
			int jstop = png_pass_inc[pass];
			png_byte v;
			png_uint_32 i;
			int j;

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
			if (transformations & PNG_PACKSWAP)
			{
				sshift = (int)((row_info->width + 7) & 0x07);
				dshift = (int)((final_width + 7) & 0x07);
				s_start = 7;
				s_end = 0;
				s_inc = -1;
			}
			else
#endif
			{
				sshift = 7 - (int)((row_info->width + 7) & 0x07);
				dshift = 7 - (int)((final_width + 7) & 0x07);
				s_start = 0;
				s_end = 7;
				s_inc = 1;
			}

			for (i = 0; i < row_info->width; i++)
			{
			   v = (png_byte)((*sp >> sshift) & 0x01);
			   for (j = 0; j < jstop; j++)
			   {
				  *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
				  *dp |= (png_byte)(v << dshift);
				  if (dshift == s_end)
				  {
					 dshift = s_start;
					 dp--;
				  }
				  else
					 dshift += s_inc;
			   }
			   if (sshift == s_end)
			   {
				  sshift = s_start;
				  sp--;
			   }
			   else
				  sshift += s_inc;
			}
			break;
		 }
		 case 2:
		 {
			png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
			png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
			int sshift, dshift;
			int s_start, s_end, s_inc;
			int jstop = png_pass_inc[pass];
			png_uint_32 i;

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
			if (transformations & PNG_PACKSWAP)
			{
			   sshift = (int)(((row_info->width + 3) & 0x03) << 1);
			   dshift = (int)(((final_width + 3) & 0x03) << 1);
			   s_start = 6;
			   s_end = 0;
			   s_inc = -2;
			}
			else
#endif
			{
			   sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
			   dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
			   s_start = 0;
			   s_end = 6;
			   s_inc = 2;
			}

			for (i = 0; i < row_info->width; i++)
			{
			   png_byte v;
			   int j;

			   v = (png_byte)((*sp >> sshift) & 0x03);
			   for (j = 0; j < jstop; j++)
			   {
				  *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
				  *dp |= (png_byte)(v << dshift);
				  if (dshift == s_end)
				  {
					 dshift = s_start;
					 dp--;
				  }
				  else
					 dshift += s_inc;
			   }
			   if (sshift == s_end)
			   {
				  sshift = s_start;
				  sp--;
			   }
			   else
				  sshift += s_inc;
			}
			break;
		 }
		 case 4:
		 {
			png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
			png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
			int sshift, dshift;
			int s_start, s_end, s_inc;
			png_uint_32 i;
			int jstop = png_pass_inc[pass];

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
			if (transformations & PNG_PACKSWAP)
			{
			   sshift = (int)(((row_info->width + 1) & 0x01) << 2);
			   dshift = (int)(((final_width + 1) & 0x01) << 2);
			   s_start = 4;
			   s_end = 0;
			   s_inc = -4;
			}
			else
#endif
			{
			   sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
			   dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
			   s_start = 0;
			   s_end = 4;
			   s_inc = 4;
			}

			for (i = 0; i < row_info->width; i++)
			{
			   png_byte v = (png_byte)((*sp >> sshift) & 0xf);
			   int j;

			   for (j = 0; j < jstop; j++)
			   {
				  *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
				  *dp |= (png_byte)(v << dshift);
				  if (dshift == s_end)
				  {
					 dshift = s_start;
					 dp--;
				  }
				  else
					 dshift += s_inc;
			   }
			   if (sshift == s_end)
			   {
				  sshift = s_start;
				  sp--;
			   }
			   else
				  sshift += s_inc;
			}
			break;
		 }
		 default:
		 {
			png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
			png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
			png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;

			int jstop = png_pass_inc[pass];
			png_uint_32 i;

			for (i = 0; i < row_info->width; i++)
			{
			   png_byte v[8];
			   int j;

			   png_memcpy(v, sp, pixel_bytes);
			   for (j = 0; j < jstop; j++)
			   {
				  png_memcpy(dp, v, pixel_bytes);
				  dp -= pixel_bytes;
			   }
			   sp -= pixel_bytes;
			}
			break;
		 }
	  }
	  row_info->width = final_width;
	  row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,final_width);
   }
#if !defined(PNG_READ_PACKSWAP_SUPPORTED)
   transformations = transformations; /* silence compiler warning */
#endif
}
#endif /* PNG_READ_INTERLACING_SUPPORTED */

void /* PRIVATE */
png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
   png_bytep prev_row, int filter)
{
   png_debug(1, "in png_read_filter_row\n");
   png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter);
   switch (filter)
   {
	  case PNG_FILTER_VALUE_NONE:
		 break;
	  case PNG_FILTER_VALUE_SUB:
	  {
		 png_uint_32 i;
		 png_uint_32 istop = row_info->rowbytes;
		 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
		 png_bytep rp = row + bpp;
		 png_bytep lp = row;

		 for (i = bpp; i < istop; i++)
		 {
			*rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
			rp++;
		 }
		 break;
	  }
	  case PNG_FILTER_VALUE_UP:
	  {
		 png_uint_32 i;
		 png_uint_32 istop = row_info->rowbytes;
		 png_bytep rp = row;
		 png_bytep pp = prev_row;

		 for (i = 0; i < istop; i++)
		 {
			*rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
			rp++;
		 }
		 break;
	  }
	  case PNG_FILTER_VALUE_AVG:
	  {
		 png_uint_32 i;
		 png_bytep rp = row;
		 png_bytep pp = prev_row;
		 png_bytep lp = row;
		 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
		 png_uint_32 istop = row_info->rowbytes - bpp;

		 for (i = 0; i < bpp; i++)
		 {
			*rp = (png_byte)(((int)(*rp) +
			   ((int)(*pp++) / 2 )) & 0xff);
			rp++;
		 }

		 for (i = 0; i < istop; i++)
		 {
			*rp = (png_byte)(((int)(*rp) +
			   (int)(*pp++ + *lp++) / 2 ) & 0xff);
			rp++;
		 }
		 break;
	  }
	  case PNG_FILTER_VALUE_PAETH:
	  {
		 png_uint_32 i;
		 png_bytep rp = row;
		 png_bytep pp = prev_row;
		 png_bytep lp = row;
		 png_bytep cp = prev_row;
		 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
		 png_uint_32 istop=row_info->rowbytes - bpp;

		 for (i = 0; i < bpp; i++)
		 {
			*rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
			rp++;
		 }

		 for (i = 0; i < istop; i++)   /* use leftover rp,pp */
		 {
			int a, b, c, pa, pb, pc, p;

			a = *lp++;
			b = *pp++;
			c = *cp++;

			p = b - c;
			pc = a - c;

#ifdef PNG_USE_ABS
			pa = abs(p);
			pb = abs(pc);
			pc = abs(p + pc);
#else
			pa = p < 0 ? -p : p;
			pb = pc < 0 ? -pc : pc;
			pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif

			p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;

			*rp = (png_byte)(((int)(*rp) + p) & 0xff);
			rp++;
		 }
		 break;
	  }
	  default:
		 png_warning(png_ptr, "Ignoring bad adaptive filter type");
		 *row=0;
		 break;
   }
}

void /* PRIVATE */
png_read_finish_row(png_structp png_ptr)
{
#ifdef PNG_USE_LOCAL_ARRAYS

   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};

   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
#endif

   png_debug(1, "in png_read_finish_row\n");
   png_ptr->row_number++;
   if (png_ptr->row_number < png_ptr->num_rows)
	  return;

   if (png_ptr->interlaced)
   {
	  png_ptr->row_number = 0;
	  png_memset_check(png_ptr, png_ptr->prev_row, 0,
		 png_ptr->rowbytes + 1);
	  do
	  {
		 png_ptr->pass++;
		 if (png_ptr->pass >= 7)
			break;
		 png_ptr->iwidth = (png_ptr->width +
			png_pass_inc[png_ptr->pass] - 1 -
			png_pass_start[png_ptr->pass]) /
			png_pass_inc[png_ptr->pass];

		 png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
			png_ptr->iwidth) + 1;

		 if (!(png_ptr->transformations & PNG_INTERLACE))
		 {
			png_ptr->num_rows = (png_ptr->height +
			   png_pass_yinc[png_ptr->pass] - 1 -
			   png_pass_ystart[png_ptr->pass]) /
			   png_pass_yinc[png_ptr->pass];
			if (!(png_ptr->num_rows))
			   continue;
		 }
		 else  /* if (png_ptr->transformations & PNG_INTERLACE) */
			break;
	  } while (png_ptr->iwidth == 0);

	  if (png_ptr->pass < 7)
		 return;
   }

   if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
   {
#ifdef PNG_USE_LOCAL_ARRAYS
	  PNG_CONST PNG_IDAT;
#endif
	  char extra;
	  int ret;

	  png_ptr->zstream.next_out = (Bytef *)&extra;
	  png_ptr->zstream.avail_out = (uInt)1;
	  for(;;)
	  {
		 if (!(png_ptr->zstream.avail_in))
		 {
			while (!png_ptr->idat_size)
			{
			   png_byte chunk_length[4];

			   png_crc_finish(png_ptr, 0);

			   png_read_data(png_ptr, chunk_length, 4);
			   png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
			   png_reset_crc(png_ptr);
			   png_crc_read(png_ptr, png_ptr->chunk_name, 4);
			   if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
				  png_error(png_ptr, "Not enough image data");

			}
			png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
			png_ptr->zstream.next_in = png_ptr->zbuf;
			if (png_ptr->zbuf_size > png_ptr->idat_size)
			   png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
			png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
			png_ptr->idat_size -= png_ptr->zstream.avail_in;
		 }
		 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
		 if (ret == Z_STREAM_END)
		 {
			if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
			   png_ptr->idat_size)
			   png_warning(png_ptr, "Extra compressed data");
			png_ptr->mode |= PNG_AFTER_IDAT;
			png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
			break;
		 }
		 if (ret != Z_OK)
			png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
					  "Decompression Error");

		 if (!(png_ptr->zstream.avail_out))
		 {
			png_warning(png_ptr, "Extra compressed data.");
			png_ptr->mode |= PNG_AFTER_IDAT;
			png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
			break;
		 }

	  }
	  png_ptr->zstream.avail_out = 0;
   }

   if (png_ptr->idat_size || png_ptr->zstream.avail_in)
	  png_warning(png_ptr, "Extra compression data");

   inflateReset(&png_ptr->zstream);

   png_ptr->mode |= PNG_AFTER_IDAT;
}

void /* PRIVATE */
png_read_start_row(png_structp png_ptr)
{
#ifdef PNG_USE_LOCAL_ARRAYS

   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};

   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
#endif

   int max_pixel_depth;
   png_uint_32 row_bytes;

   png_debug(1, "in png_read_start_row\n");
   png_ptr->zstream.avail_in = 0;
   png_init_read_transformations(png_ptr);
   if (png_ptr->interlaced)
   {
	  if (!(png_ptr->transformations & PNG_INTERLACE))
		 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
			png_pass_ystart[0]) / png_pass_yinc[0];
	  else
		 png_ptr->num_rows = png_ptr->height;

	  png_ptr->iwidth = (png_ptr->width +
		 png_pass_inc[png_ptr->pass] - 1 -
		 png_pass_start[png_ptr->pass]) /
		 png_pass_inc[png_ptr->pass];

		 row_bytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->iwidth) + 1;

		 png_ptr->irowbytes = (png_size_t)row_bytes;
		 if((png_uint_32)png_ptr->irowbytes != row_bytes)
			png_error(png_ptr, "Rowbytes overflow in png_read_start_row");
   }
   else
   {
	  png_ptr->num_rows = png_ptr->height;
	  png_ptr->iwidth = png_ptr->width;
	  png_ptr->irowbytes = png_ptr->rowbytes + 1;
   }
   max_pixel_depth = png_ptr->pixel_depth;

#if defined(PNG_READ_PACK_SUPPORTED)
   if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
	  max_pixel_depth = 8;
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
   if (png_ptr->transformations & PNG_EXPAND)
   {
	  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  {
		 if (png_ptr->num_trans)
			max_pixel_depth = 32;
		 else
			max_pixel_depth = 24;
	  }
	  else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
	  {
		 if (max_pixel_depth < 8)
			max_pixel_depth = 8;
		 if (png_ptr->num_trans)
			max_pixel_depth *= 2;
	  }
	  else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
	  {
		 if (png_ptr->num_trans)
		 {
			max_pixel_depth *= 4;
			max_pixel_depth /= 3;
		 }
	  }
   }
#endif

#if defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & (PNG_FILLER))
   {
	  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
		 max_pixel_depth = 32;
	  else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
	  {
		 if (max_pixel_depth <= 8)
			max_pixel_depth = 16;
		 else
			max_pixel_depth = 32;
	  }
	  else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
	  {
		 if (max_pixel_depth <= 32)
			max_pixel_depth = 32;
		 else
			max_pixel_depth = 64;
	  }
   }
#endif

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
   {
	  if (
#if defined(PNG_READ_EXPAND_SUPPORTED)
		(png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
#endif
#if defined(PNG_READ_FILLER_SUPPORTED)
		(png_ptr->transformations & (PNG_FILLER)) ||
#endif
		png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
	  {
		 if (max_pixel_depth <= 16)
			max_pixel_depth = 32;
		 else
			max_pixel_depth = 64;
	  }
	  else
	  {
		 if (max_pixel_depth <= 8)
		   {
			 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
			   max_pixel_depth = 32;
			 else
			   max_pixel_depth = 24;
		   }
		 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
			max_pixel_depth = 64;
		 else
			max_pixel_depth = 48;
	  }
   }
#endif

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
   if(png_ptr->transformations & PNG_USER_TRANSFORM)
	 {
	   int user_pixel_depth=png_ptr->user_transform_depth*
		 png_ptr->user_transform_channels;
	   if(user_pixel_depth > max_pixel_depth)
		 max_pixel_depth=user_pixel_depth;
	 }
#endif

   row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
   row_bytes = PNG_ROWBYTES(max_pixel_depth,row_bytes) +
	  1 + ((max_pixel_depth + 7) >> 3);
#ifdef PNG_MAX_MALLOC_64K
   if (row_bytes > (png_uint_32)65536L)
	  png_error(png_ptr, "This image requires a row greater than 64KB");
#endif
   png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
   png_ptr->row_buf = png_ptr->big_row_buf+32;

#ifdef PNG_MAX_MALLOC_64K
   if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
	  png_error(png_ptr, "This image requires a row greater than 64KB");
#endif
   if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
	  png_error(png_ptr, "Row has too many bytes to allocate in memory.");
   png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
	  png_ptr->rowbytes + 1));

   png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);

   png_debug1(3, "width = %lu,\n", png_ptr->width);
   png_debug1(3, "height = %lu,\n", png_ptr->height);
   png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
   png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
   png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
   png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);

   png_ptr->flags |= PNG_FLAG_ROW_INIT;
}
#endif /* PNG_READ_SUPPORTED */
/*** End of inlined file: pngrutil.c ***/


/*** Start of inlined file: pngset.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)

#if defined(PNG_bKGD_SUPPORTED)
void PNGAPI
png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
{
   png_debug1(1, "in %s storage function\n", "bKGD");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
   info_ptr->valid |= PNG_INFO_bKGD;
}
#endif

#if defined(PNG_cHRM_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
void PNGAPI
png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
   double white_x, double white_y, double red_x, double red_y,
   double green_x, double green_y, double blue_x, double blue_y)
{
   png_debug1(1, "in %s storage function\n", "cHRM");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   if (white_x < 0.0 || white_y < 0.0 ||
		 red_x < 0.0 ||   red_y < 0.0 ||
	   green_x < 0.0 || green_y < 0.0 ||
		blue_x < 0.0 ||  blue_y < 0.0)
   {
	  png_warning(png_ptr,
		"Ignoring attempt to set negative chromaticity value");
	  return;
   }
   if (white_x > 21474.83 || white_y > 21474.83 ||
		 red_x > 21474.83 ||   red_y > 21474.83 ||
	   green_x > 21474.83 || green_y > 21474.83 ||
		blue_x > 21474.83 ||  blue_y > 21474.83)
   {
	  png_warning(png_ptr,
		"Ignoring attempt to set chromaticity value exceeding 21474.83");
	  return;
   }

   info_ptr->x_white = (float)white_x;
   info_ptr->y_white = (float)white_y;
   info_ptr->x_red   = (float)red_x;
   info_ptr->y_red   = (float)red_y;
   info_ptr->x_green = (float)green_x;
   info_ptr->y_green = (float)green_y;
   info_ptr->x_blue  = (float)blue_x;
   info_ptr->y_blue  = (float)blue_y;
#ifdef PNG_FIXED_POINT_SUPPORTED
   info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
   info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
   info_ptr->int_x_red   = (png_fixed_point)(  red_x*100000.+0.5);
   info_ptr->int_y_red   = (png_fixed_point)(  red_y*100000.+0.5);
   info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
   info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
   info_ptr->int_x_blue  = (png_fixed_point)( blue_x*100000.+0.5);
   info_ptr->int_y_blue  = (png_fixed_point)( blue_y*100000.+0.5);
#endif
   info_ptr->valid |= PNG_INFO_cHRM;
}
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
void PNGAPI
png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
   png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
   png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
   png_fixed_point blue_x, png_fixed_point blue_y)
{
   png_debug1(1, "in %s storage function\n", "cHRM");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   if (white_x < 0 || white_y < 0 ||
		 red_x < 0 ||   red_y < 0 ||
	   green_x < 0 || green_y < 0 ||
		blue_x < 0 ||  blue_y < 0)
   {
	  png_warning(png_ptr,
		"Ignoring attempt to set negative chromaticity value");
	  return;
   }
#ifdef PNG_FLOATING_POINT_SUPPORTED
   if (white_x > (double) PNG_UINT_31_MAX ||
	   white_y > (double) PNG_UINT_31_MAX ||
		 red_x > (double) PNG_UINT_31_MAX ||
		 red_y > (double) PNG_UINT_31_MAX ||
	   green_x > (double) PNG_UINT_31_MAX ||
	   green_y > (double) PNG_UINT_31_MAX ||
		blue_x > (double) PNG_UINT_31_MAX ||
		blue_y > (double) PNG_UINT_31_MAX)
#else
   if (white_x > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
	   white_y > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
		 red_x > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
		 red_y > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
	   green_x > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
	   green_y > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
		blue_x > (png_fixed_point) PNG_UINT_31_MAX/100000L ||
		blue_y > (png_fixed_point) PNG_UINT_31_MAX/100000L)
#endif
   {
	  png_warning(png_ptr,
		"Ignoring attempt to set chromaticity value exceeding 21474.83");
	  return;
   }
   info_ptr->int_x_white = white_x;
   info_ptr->int_y_white = white_y;
   info_ptr->int_x_red   = red_x;
   info_ptr->int_y_red   = red_y;
   info_ptr->int_x_green = green_x;
   info_ptr->int_y_green = green_y;
   info_ptr->int_x_blue  = blue_x;
   info_ptr->int_y_blue  = blue_y;
#ifdef PNG_FLOATING_POINT_SUPPORTED
   info_ptr->x_white = (float)(white_x/100000.);
   info_ptr->y_white = (float)(white_y/100000.);
   info_ptr->x_red   = (float)(  red_x/100000.);
   info_ptr->y_red   = (float)(  red_y/100000.);
   info_ptr->x_green = (float)(green_x/100000.);
   info_ptr->y_green = (float)(green_y/100000.);
   info_ptr->x_blue  = (float)( blue_x/100000.);
   info_ptr->y_blue  = (float)( blue_y/100000.);
#endif
   info_ptr->valid |= PNG_INFO_cHRM;
}
#endif
#endif

#if defined(PNG_gAMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
void PNGAPI
png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
{
   double gamma;
   png_debug1(1, "in %s storage function\n", "gAMA");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   if (file_gamma > 21474.83)
   {
	  png_warning(png_ptr, "Limiting gamma to 21474.83");
	  gamma=21474.83;
   }
   else
	  gamma=file_gamma;
   info_ptr->gamma = (float)gamma;
#ifdef PNG_FIXED_POINT_SUPPORTED
   info_ptr->int_gamma = (int)(gamma*100000.+.5);
#endif
   info_ptr->valid |= PNG_INFO_gAMA;
   if(gamma == 0.0)
	  png_warning(png_ptr, "Setting gamma=0");
}
#endif
void PNGAPI
png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
   int_gamma)
{
   png_fixed_point gamma;

   png_debug1(1, "in %s storage function\n", "gAMA");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   if (int_gamma > (png_fixed_point) PNG_UINT_31_MAX)
   {
	 png_warning(png_ptr, "Limiting gamma to 21474.83");
	 gamma=PNG_UINT_31_MAX;
   }
   else
   {
	 if (int_gamma < 0)
	 {
	   png_warning(png_ptr, "Setting negative gamma to zero");
	   gamma=0;
	 }
	 else
	   gamma=int_gamma;
   }
#ifdef PNG_FLOATING_POINT_SUPPORTED
   info_ptr->gamma = (float)(gamma/100000.);
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   info_ptr->int_gamma = gamma;
#endif
   info_ptr->valid |= PNG_INFO_gAMA;
   if(gamma == 0)
	  png_warning(png_ptr, "Setting gamma=0");
}
#endif

#if defined(PNG_hIST_SUPPORTED)
void PNGAPI
png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
{
   int i;

   png_debug1(1, "in %s storage function\n", "hIST");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;
   if (info_ptr->num_palette == 0 || info_ptr->num_palette
	   > PNG_MAX_PALETTE_LENGTH)
   {
	   png_warning(png_ptr,
		  "Invalid palette size, hIST allocation skipped.");
	   return;
   }

#ifdef PNG_FREE_ME_SUPPORTED
   png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
#endif
   png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
	  (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof (png_uint_16)));
   if (png_ptr->hist == NULL)
	 {
	   png_warning(png_ptr, "Insufficient memory for hIST chunk data.");
	   return;
	 }

   for (i = 0; i < info_ptr->num_palette; i++)
	   png_ptr->hist[i] = hist[i];
   info_ptr->hist = png_ptr->hist;
   info_ptr->valid |= PNG_INFO_hIST;

#ifdef PNG_FREE_ME_SUPPORTED
   info_ptr->free_me |= PNG_FREE_HIST;
#else
   png_ptr->flags |= PNG_FLAG_FREE_HIST;
#endif
}
#endif

void PNGAPI
png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
   png_uint_32 width, png_uint_32 height, int bit_depth,
   int color_type, int interlace_type, int compression_type,
   int filter_type)
{
   png_debug1(1, "in %s storage function\n", "IHDR");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   if (width == 0 || height == 0)
	  png_error(png_ptr, "Image width or height is zero in IHDR");
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
	  png_error(png_ptr, "image size exceeds user limits in IHDR");
#else
   if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
	  png_error(png_ptr, "image size exceeds user limits in IHDR");
#endif
   if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
	  png_error(png_ptr, "Invalid image size in IHDR");
   if ( width > (PNG_UINT_32_MAX
				 >> 3)	  /* 8-byte RGBA pixels */
				 - 64	   /* bigrowbuf hack */
				 - 1	/* filter byte */
				 - 7*8	  /* rounding of width to multiple of 8 pixels */
				 - 8)	   /* extra max_pixel_depth pad */
	  png_warning(png_ptr, "Width is too large for libpng to process pixels");

   if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
	  bit_depth != 8 && bit_depth != 16)
	  png_error(png_ptr, "Invalid bit depth in IHDR");

   if (color_type < 0 || color_type == 1 ||
	  color_type == 5 || color_type > 6)
	  png_error(png_ptr, "Invalid color type in IHDR");

   if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
	   ((color_type == PNG_COLOR_TYPE_RGB ||
		 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
		 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
	  png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");

   if (interlace_type >= PNG_INTERLACE_LAST)
	  png_error(png_ptr, "Unknown interlace method in IHDR");

   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
	  png_error(png_ptr, "Unknown compression method in IHDR");

#if defined(PNG_MNG_FEATURES_SUPPORTED)
   if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
	  png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
   if(filter_type != PNG_FILTER_TYPE_BASE)
   {
	 if(!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
		(filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
		((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
		(color_type == PNG_COLOR_TYPE_RGB ||
		 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
		png_error(png_ptr, "Unknown filter method in IHDR");
	 if(png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
		png_warning(png_ptr, "Invalid filter method in IHDR");
   }
#else
   if(filter_type != PNG_FILTER_TYPE_BASE)
	  png_error(png_ptr, "Unknown filter method in IHDR");
#endif

   info_ptr->width = width;
   info_ptr->height = height;
   info_ptr->bit_depth = (png_byte)bit_depth;
   info_ptr->color_type =(png_byte) color_type;
   info_ptr->compression_type = (png_byte)compression_type;
   info_ptr->filter_type = (png_byte)filter_type;
   info_ptr->interlace_type = (png_byte)interlace_type;
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  info_ptr->channels = 1;
   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
	  info_ptr->channels = 3;
   else
	  info_ptr->channels = 1;
   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
	  info_ptr->channels++;
   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);

   if (width > (PNG_UINT_32_MAX
				 >> 3)	  /* 8-byte RGBA pixels */
				 - 64	   /* bigrowbuf hack */
				 - 1	/* filter byte */
				 - 7*8	  /* rounding of width to multiple of 8 pixels */
				 - 8)	   /* extra max_pixel_depth pad */
	  info_ptr->rowbytes = (png_size_t)0;
   else
	  info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,width);
}

#if defined(PNG_oFFs_SUPPORTED)
void PNGAPI
png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
   png_int_32 offset_x, png_int_32 offset_y, int unit_type)
{
   png_debug1(1, "in %s storage function\n", "oFFs");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   info_ptr->x_offset = offset_x;
   info_ptr->y_offset = offset_y;
   info_ptr->offset_unit_type = (png_byte)unit_type;
   info_ptr->valid |= PNG_INFO_oFFs;
}
#endif

#if defined(PNG_pCAL_SUPPORTED)
void PNGAPI
png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
   png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
   png_charp units, png_charpp params)
{
   png_uint_32 length;
   int i;

   png_debug1(1, "in %s storage function\n", "pCAL");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   length = png_strlen(purpose) + 1;
   png_debug1(3, "allocating purpose for info (%lu bytes)\n", length);
   info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
   if (info_ptr->pcal_purpose == NULL)
	 {
	   png_warning(png_ptr, "Insufficient memory for pCAL purpose.");
	   return;
	 }
   png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);

   png_debug(3, "storing X0, X1, type, and nparams in info\n");
   info_ptr->pcal_X0 = X0;
   info_ptr->pcal_X1 = X1;
   info_ptr->pcal_type = (png_byte)type;
   info_ptr->pcal_nparams = (png_byte)nparams;

   length = png_strlen(units) + 1;
   png_debug1(3, "allocating units for info (%lu bytes)\n", length);
   info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
   if (info_ptr->pcal_units == NULL)
	 {
	   png_warning(png_ptr, "Insufficient memory for pCAL units.");
	   return;
	 }
   png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);

   info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
	  (png_uint_32)((nparams + 1) * png_sizeof(png_charp)));
   if (info_ptr->pcal_params == NULL)
	 {
	   png_warning(png_ptr, "Insufficient memory for pCAL params.");
	   return;
	 }

   info_ptr->pcal_params[nparams] = NULL;

   for (i = 0; i < nparams; i++)
   {
	  length = png_strlen(params[i]) + 1;
	  png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i, length);
	  info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
	  if (info_ptr->pcal_params[i] == NULL)
		{
		  png_warning(png_ptr, "Insufficient memory for pCAL parameter.");
		  return;
		}
	  png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
   }

   info_ptr->valid |= PNG_INFO_pCAL;
#ifdef PNG_FREE_ME_SUPPORTED
   info_ptr->free_me |= PNG_FREE_PCAL;
#endif
}
#endif

#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
void PNGAPI
png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
			 int unit, double width, double height)
{
   png_debug1(1, "in %s storage function\n", "sCAL");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   info_ptr->scal_unit = (png_byte)unit;
   info_ptr->scal_pixel_width = width;
   info_ptr->scal_pixel_height = height;

   info_ptr->valid |= PNG_INFO_sCAL;
}
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
void PNGAPI
png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
			 int unit, png_charp swidth, png_charp sheight)
{
   png_uint_32 length;

   png_debug1(1, "in %s storage function\n", "sCAL");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   info_ptr->scal_unit = (png_byte)unit;

   length = png_strlen(swidth) + 1;
   png_debug1(3, "allocating unit for info (%d bytes)\n", length);
   info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
   if (info_ptr->scal_s_width == NULL)
   {
	  png_warning(png_ptr,
	   "Memory allocation failed while processing sCAL.");
   }
   png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);

   length = png_strlen(sheight) + 1;
   png_debug1(3, "allocating unit for info (%d bytes)\n", length);
   info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
   if (info_ptr->scal_s_height == NULL)
   {
	  png_free (png_ptr, info_ptr->scal_s_width);
	  png_warning(png_ptr,
	   "Memory allocation failed while processing sCAL.");
   }
   png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);

   info_ptr->valid |= PNG_INFO_sCAL;
#ifdef PNG_FREE_ME_SUPPORTED
   info_ptr->free_me |= PNG_FREE_SCAL;
#endif
}
#endif
#endif
#endif

#if defined(PNG_pHYs_SUPPORTED)
void PNGAPI
png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
   png_uint_32 res_x, png_uint_32 res_y, int unit_type)
{
   png_debug1(1, "in %s storage function\n", "pHYs");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   info_ptr->x_pixels_per_unit = res_x;
   info_ptr->y_pixels_per_unit = res_y;
   info_ptr->phys_unit_type = (png_byte)unit_type;
   info_ptr->valid |= PNG_INFO_pHYs;
}
#endif

void PNGAPI
png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
   png_colorp palette, int num_palette)
{

   png_debug1(1, "in %s storage function\n", "PLTE");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
	 {
	   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
		 png_error(png_ptr, "Invalid palette length");
	   else
	   {
		 png_warning(png_ptr, "Invalid palette length");
		 return;
	   }
	 }

#ifdef PNG_FREE_ME_SUPPORTED
   png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
#endif

   png_ptr->palette = (png_colorp)png_malloc(png_ptr,
	  PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
   png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
	  png_sizeof(png_color));
   png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof (png_color));
   info_ptr->palette = png_ptr->palette;
   info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;

#ifdef PNG_FREE_ME_SUPPORTED
   info_ptr->free_me |= PNG_FREE_PLTE;
#else
   png_ptr->flags |= PNG_FLAG_FREE_PLTE;
#endif

   info_ptr->valid |= PNG_INFO_PLTE;
}

#if defined(PNG_sBIT_SUPPORTED)
void PNGAPI
png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
   png_color_8p sig_bit)
{
   png_debug1(1, "in %s storage function\n", "sBIT");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof (png_color_8));
   info_ptr->valid |= PNG_INFO_sBIT;
}
#endif

#if defined(PNG_sRGB_SUPPORTED)
void PNGAPI
png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
{
   png_debug1(1, "in %s storage function\n", "sRGB");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   info_ptr->srgb_intent = (png_byte)intent;
   info_ptr->valid |= PNG_INFO_sRGB;
}

void PNGAPI
png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
   int intent)
{
#if defined(PNG_gAMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
   float file_gamma;
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_fixed_point int_file_gamma;
#endif
#endif
#if defined(PNG_cHRM_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
	  int_green_y, int_blue_x, int_blue_y;
#endif
#endif
   png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   png_set_sRGB(png_ptr, info_ptr, intent);

#if defined(PNG_gAMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
   file_gamma = (float).45455;
   png_set_gAMA(png_ptr, info_ptr, file_gamma);
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
   int_file_gamma = 45455L;
   png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
#endif
#endif

#if defined(PNG_cHRM_SUPPORTED)
#ifdef PNG_FIXED_POINT_SUPPORTED
   int_white_x = 31270L;
   int_white_y = 32900L;
   int_red_x   = 64000L;
   int_red_y   = 33000L;
   int_green_x = 30000L;
   int_green_y = 60000L;
   int_blue_x  = 15000L;
   int_blue_y  =  6000L;

   png_set_cHRM_fixed(png_ptr, info_ptr,
	  int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
	  int_blue_x, int_blue_y);
#endif
#ifdef PNG_FLOATING_POINT_SUPPORTED
   white_x = (float).3127;
   white_y = (float).3290;
   red_x   = (float).64;
   red_y   = (float).33;
   green_x = (float).30;
   green_y = (float).60;
   blue_x  = (float).15;
   blue_y  = (float).06;

   png_set_cHRM(png_ptr, info_ptr,
	  white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
#endif
#endif
}
#endif

#if defined(PNG_iCCP_SUPPORTED)
void PNGAPI
png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
			 png_charp name, int compression_type,
			 png_charp profile, png_uint_32 proflen)
{
   png_charp new_iccp_name;
   png_charp new_iccp_profile;

   png_debug1(1, "in %s storage function\n", "iCCP");
   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
	  return;

   new_iccp_name = (png_charp)png_malloc_warn(png_ptr, png_strlen(name)+1);
   if (new_iccp_name == NULL)
   {
	  png_warning(png_ptr, "Insufficient memory to process iCCP chunk.");
	  return;
   }
   png_strncpy(new_iccp_name, name, png_strlen(name)+1);
   new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
   if (new_iccp_profile == NULL)
   {
	  png_free (png_ptr, new_iccp_name);
	  png_warning(png_ptr, "Insufficient memory to process iCCP profile.");
	  return;
   }
   png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);

   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);

   info_ptr->iccp_proflen = proflen;
   info_ptr->iccp_name = new_iccp_name;
   info_ptr->iccp_profile = new_iccp_profile;
   info_ptr->iccp_compression = (png_byte)compression_type;
#ifdef PNG_FREE_ME_SUPPORTED
   info_ptr->free_me |= PNG_FREE_ICCP;
#endif
   info_ptr->valid |= PNG_INFO_iCCP;
}
#endif

#if defined(PNG_TEXT_SUPPORTED)
void PNGAPI
png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
   int num_text)
{
   int ret;
   ret=png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
   if (ret)
	 png_error(png_ptr, "Insufficient memory to store text");
}

int /* PRIVATE */
png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
   int num_text)
{
   int i;

   png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
	  "text" : (png_const_charp)png_ptr->chunk_name));

   if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
	  return(0);

   if (info_ptr->num_text + num_text > info_ptr->max_text)
   {
	  if (info_ptr->text != NULL)
	  {
		 png_textp old_text;
		 int old_max;

		 old_max = info_ptr->max_text;
		 info_ptr->max_text = info_ptr->num_text + num_text + 8;
		 old_text = info_ptr->text;
		 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
			(png_uint_32)(info_ptr->max_text * png_sizeof (png_text)));
		 if (info_ptr->text == NULL)
		   {
			 png_free(png_ptr, old_text);
			 return(1);
		   }
		 png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
			png_sizeof(png_text)));
		 png_free(png_ptr, old_text);
	  }
	  else
	  {
		 info_ptr->max_text = num_text + 8;
		 info_ptr->num_text = 0;
		 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
			(png_uint_32)(info_ptr->max_text * png_sizeof (png_text)));
		 if (info_ptr->text == NULL)
		   return(1);
#ifdef PNG_FREE_ME_SUPPORTED
		 info_ptr->free_me |= PNG_FREE_TEXT;
#endif
	  }
	  png_debug1(3, "allocated %d entries for info_ptr->text\n",
		 info_ptr->max_text);
   }
   for (i = 0; i < num_text; i++)
   {
	  png_size_t text_length,key_len;
	  png_size_t lang_len,lang_key_len;
	  png_textp textp = &(info_ptr->text[info_ptr->num_text]);

	  if (text_ptr[i].key == NULL)
		  continue;

	  key_len = png_strlen(text_ptr[i].key);

	  if(text_ptr[i].compression <= 0)
	  {
		lang_len = 0;
		lang_key_len = 0;
	  }
	  else
#ifdef PNG_iTXt_SUPPORTED
	  {
		if (text_ptr[i].lang != NULL)
		  lang_len = png_strlen(text_ptr[i].lang);
		else
		  lang_len = 0;
		if (text_ptr[i].lang_key != NULL)
		  lang_key_len = png_strlen(text_ptr[i].lang_key);
		else
		  lang_key_len = 0;
	  }
#else
	  {
		png_warning(png_ptr, "iTXt chunk not supported.");
		continue;
	  }
#endif

	  if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
	  {
		 text_length = 0;
#ifdef PNG_iTXt_SUPPORTED
		 if(text_ptr[i].compression > 0)
			textp->compression = PNG_ITXT_COMPRESSION_NONE;
		 else
#endif
			textp->compression = PNG_TEXT_COMPRESSION_NONE;
	  }
	  else
	  {
		 text_length = png_strlen(text_ptr[i].text);
		 textp->compression = text_ptr[i].compression;
	  }

	  textp->key = (png_charp)png_malloc_warn(png_ptr,
		 (png_uint_32)(key_len + text_length + lang_len + lang_key_len + 4));
	  if (textp->key == NULL)
		return(1);
	  png_debug2(2, "Allocated %lu bytes at %x in png_set_text\n",
		 (png_uint_32)(key_len + lang_len + lang_key_len + text_length + 4),
		 (int)textp->key);

	  png_memcpy(textp->key, text_ptr[i].key,
		 (png_size_t)(key_len));
	  *(textp->key+key_len) = '\0';
#ifdef PNG_iTXt_SUPPORTED
	  if (text_ptr[i].compression > 0)
	  {
		 textp->lang=textp->key + key_len + 1;
		 png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
		 *(textp->lang+lang_len) = '\0';
		 textp->lang_key=textp->lang + lang_len + 1;
		 png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
		 *(textp->lang_key+lang_key_len) = '\0';
		 textp->text=textp->lang_key + lang_key_len + 1;
	  }
	  else
#endif
	  {
#ifdef PNG_iTXt_SUPPORTED
		 textp->lang=NULL;
		 textp->lang_key=NULL;
#endif
		 textp->text=textp->key + key_len + 1;
	  }
	  if(text_length)
		 png_memcpy(textp->text, text_ptr[i].text,
			(png_size_t)(text_length));
	  *(textp->text+text_length) = '\0';

#ifdef PNG_iTXt_SUPPORTED
	  if(textp->compression > 0)
	  {
		 textp->text_length = 0;
		 textp->itxt_length = text_length;
	  }
	  else
#endif
	  {
		 textp->text_length = text_length;
#ifdef PNG_iTXt_SUPPORTED
		 textp->itxt_length = 0;
#endif
	  }
	  info_ptr->num_text++;
	  png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
   }
   return(0);
}
#endif

#if defined(PNG_tIME_SUPPORTED)
void PNGAPI
png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
{
   png_debug1(1, "in %s storage function\n", "tIME");
   if (png_ptr == NULL || info_ptr == NULL ||
	   (png_ptr->mode & PNG_WROTE_tIME))
	  return;

   png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof (png_time));
   info_ptr->valid |= PNG_INFO_tIME;
}
#endif

#if defined(PNG_tRNS_SUPPORTED)
void PNGAPI
png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
   png_bytep trans, int num_trans, png_color_16p trans_values)
{
   png_debug1(1, "in %s storage function\n", "tRNS");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   if (trans != NULL)
   {
#ifdef PNG_FREE_ME_SUPPORTED
	   png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
#endif
	   png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
		   (png_uint_32)PNG_MAX_PALETTE_LENGTH);
	   if (num_trans <= PNG_MAX_PALETTE_LENGTH)
		 png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
#ifdef PNG_FREE_ME_SUPPORTED
	   info_ptr->free_me |= PNG_FREE_TRNS;
#else
	   png_ptr->flags |= PNG_FLAG_FREE_TRNS;
#endif
   }

   if (trans_values != NULL)
   {
	  png_memcpy(&(info_ptr->trans_values), trans_values,
		 png_sizeof(png_color_16));
	  if (num_trans == 0)
		num_trans = 1;
   }
   info_ptr->num_trans = (png_uint_16)num_trans;
   info_ptr->valid |= PNG_INFO_tRNS;
}
#endif

#if defined(PNG_sPLT_SUPPORTED)
void PNGAPI
png_set_sPLT(png_structp png_ptr,
			 png_infop info_ptr, png_sPLT_tp entries, int nentries)
{
	png_sPLT_tp np;
	int i;

	if (png_ptr == NULL || info_ptr == NULL)
	   return;

	np = (png_sPLT_tp)png_malloc_warn(png_ptr,
		(info_ptr->splt_palettes_num + nentries) * png_sizeof(png_sPLT_t));
	if (np == NULL)
	{
	  png_warning(png_ptr, "No memory for sPLT palettes.");
	  return;
	}

	png_memcpy(np, info_ptr->splt_palettes,
		   info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
	png_free(png_ptr, info_ptr->splt_palettes);
	info_ptr->splt_palettes=NULL;

	for (i = 0; i < nentries; i++)
	{
		png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
		png_sPLT_tp from = entries + i;

		to->name = (png_charp)png_malloc_warn(png_ptr,
		  png_strlen(from->name) + 1);
		if (to->name == NULL)
		{
		   png_warning(png_ptr,
			 "Out of memory while processing sPLT chunk");
		}
		png_strncpy(to->name, from->name, png_strlen(from->name)+1);
		to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
			from->nentries * png_sizeof(png_sPLT_entry));
		png_memcpy(to->entries, from->entries,
			from->nentries * png_sizeof(png_sPLT_entry));
		if (to->entries == NULL)
		{
		   png_warning(png_ptr,
			 "Out of memory while processing sPLT chunk");
		   png_free(png_ptr,to->name);
		   to->name = NULL;
		}
		to->nentries = from->nentries;
		to->depth = from->depth;
	}

	info_ptr->splt_palettes = np;
	info_ptr->splt_palettes_num += nentries;
	info_ptr->valid |= PNG_INFO_sPLT;
#ifdef PNG_FREE_ME_SUPPORTED
	info_ptr->free_me |= PNG_FREE_SPLT;
#endif
}
#endif /* PNG_sPLT_SUPPORTED */

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
void PNGAPI
png_set_unknown_chunks(png_structp png_ptr,
   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
{
	png_unknown_chunkp np;
	int i;

	if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
		return;

	np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
		(info_ptr->unknown_chunks_num + num_unknowns) *
		png_sizeof(png_unknown_chunk));
	if (np == NULL)
	{
	   png_warning(png_ptr,
		  "Out of memory while processing unknown chunk.");
	   return;
	}

	png_memcpy(np, info_ptr->unknown_chunks,
		   info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
	png_free(png_ptr, info_ptr->unknown_chunks);
	info_ptr->unknown_chunks=NULL;

	for (i = 0; i < num_unknowns; i++)
	{
		png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
		png_unknown_chunkp from = unknowns + i;

		png_strncpy((png_charp)to->name, (png_charp)from->name, 5);
		to->data = (png_bytep)png_malloc_warn(png_ptr, from->size);
		if (to->data == NULL)
		{
		   png_warning(png_ptr,
			  "Out of memory while processing unknown chunk.");
		}
		else
		{
		   png_memcpy(to->data, from->data, from->size);
		   to->size = from->size;

		   to->location = (png_byte)(png_ptr->mode & 0xff);
		}
	}

	info_ptr->unknown_chunks = np;
	info_ptr->unknown_chunks_num += num_unknowns;
#ifdef PNG_FREE_ME_SUPPORTED
	info_ptr->free_me |= PNG_FREE_UNKN;
#endif
}
void PNGAPI
png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
   int chunk, int location)
{
   if(png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
		 (int)info_ptr->unknown_chunks_num)
	  info_ptr->unknown_chunks[chunk].location = (png_byte)location;
}
#endif

#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
	defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
void PNGAPI
png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
{
   png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n");
   if (png_ptr == NULL)
	  return;
   png_ptr->mng_features_permitted = (png_byte)
	 ((png_ptr->mng_features_permitted & (~(PNG_FLAG_MNG_EMPTY_PLTE))) |
	 ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
}
#endif
#endif

#if defined(PNG_MNG_FEATURES_SUPPORTED)
png_uint_32 PNGAPI
png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
{
   png_debug(1, "in png_permit_mng_features\n");
   if (png_ptr == NULL)
	  return (png_uint_32)0;
   png_ptr->mng_features_permitted =
	 (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
   return (png_uint_32)png_ptr->mng_features_permitted;
}
#endif

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
void PNGAPI
png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
   chunk_list, int num_chunks)
{
	png_bytep new_list, p;
	int i, old_num_chunks;
	if (png_ptr == NULL)
	   return;
	if (num_chunks == 0)
	{
	  if(keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
		png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
	  else
		png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;

	  if(keep == PNG_HANDLE_CHUNK_ALWAYS)
		png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
	  else
		png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
	  return;
	}
	if (chunk_list == NULL)
	  return;
	old_num_chunks=png_ptr->num_chunk_list;
	new_list=(png_bytep)png_malloc(png_ptr,
	   (png_uint_32)(5*(num_chunks+old_num_chunks)));
	if(png_ptr->chunk_list != NULL)
	{
	   png_memcpy(new_list, png_ptr->chunk_list,
		  (png_size_t)(5*old_num_chunks));
	   png_free(png_ptr, png_ptr->chunk_list);
	   png_ptr->chunk_list=NULL;
	}
	png_memcpy(new_list+5*old_num_chunks, chunk_list,
	   (png_size_t)(5*num_chunks));
	for (p=new_list+5*old_num_chunks+4, i=0; i<num_chunks; i++, p+=5)
	   *p=(png_byte)keep;
	png_ptr->num_chunk_list=old_num_chunks+num_chunks;
	png_ptr->chunk_list=new_list;
#ifdef PNG_FREE_ME_SUPPORTED
	png_ptr->free_me |= PNG_FREE_LIST;
#endif
}
#endif

#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
void PNGAPI
png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
   png_user_chunk_ptr read_user_chunk_fn)
{
   png_debug(1, "in png_set_read_user_chunk_fn\n");
   if (png_ptr == NULL)
	  return;
   png_ptr->read_user_chunk_fn = read_user_chunk_fn;
   png_ptr->user_chunk_ptr = user_chunk_ptr;
}
#endif

#if defined(PNG_INFO_IMAGE_SUPPORTED)
void PNGAPI
png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
{
   png_debug1(1, "in %s storage function\n", "rows");

   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   if(info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
	  png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
   info_ptr->row_pointers = row_pointers;
   if(row_pointers)
	  info_ptr->valid |= PNG_INFO_IDAT;
}
#endif

#ifdef PNG_WRITE_SUPPORTED
void PNGAPI
png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size)
{
	if (png_ptr == NULL)
	   return;
	if(png_ptr->zbuf)
	   png_free(png_ptr, png_ptr->zbuf);
	png_ptr->zbuf_size = (png_size_t)size;
	png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
	png_ptr->zstream.next_out = png_ptr->zbuf;
	png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
}
#endif

void PNGAPI
png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
{
   if (png_ptr && info_ptr)
	  info_ptr->valid &= ~(mask);
}

#ifndef PNG_1_0_X
#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
void PNGAPI
png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
{
	if (png_ptr != NULL)
	png_ptr->asm_flags = 0;
}

void PNGAPI
png_set_mmx_thresholds (png_structp png_ptr,
						png_byte mmx_bitdepth_threshold,
						png_uint_32 mmx_rowbytes_threshold)
{
	if (png_ptr == NULL)
	   return;
}
#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
void PNGAPI
png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
	png_uint_32 user_height_max)
{
	if(png_ptr == NULL) return;
	png_ptr->user_width_max = user_width_max;
	png_ptr->user_height_max = user_height_max;
}
#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */

#endif /* ?PNG_1_0_X */
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngset.c ***/


/*** Start of inlined file: pngtrans.c ***/
#define PNG_INTERNAL

#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
void PNGAPI
png_set_bgr(png_structp png_ptr)
{
   png_debug(1, "in png_set_bgr\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_BGR;
}
#endif

#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
void PNGAPI
png_set_swap(png_structp png_ptr)
{
   png_debug(1, "in png_set_swap\n");
   if(png_ptr == NULL) return;
   if (png_ptr->bit_depth == 16)
	  png_ptr->transformations |= PNG_SWAP_BYTES;
}
#endif

#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
void PNGAPI
png_set_packing(png_structp png_ptr)
{
   png_debug(1, "in png_set_packing\n");
   if(png_ptr == NULL) return;
   if (png_ptr->bit_depth < 8)
   {
	  png_ptr->transformations |= PNG_PACK;
	  png_ptr->usr_bit_depth = 8;
   }
}
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
void PNGAPI
png_set_packswap(png_structp png_ptr)
{
   png_debug(1, "in png_set_packswap\n");
   if(png_ptr == NULL) return;
   if (png_ptr->bit_depth < 8)
	  png_ptr->transformations |= PNG_PACKSWAP;
}
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
void PNGAPI
png_set_shift(png_structp png_ptr, png_color_8p true_bits)
{
   png_debug(1, "in png_set_shift\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_SHIFT;
   png_ptr->shift = *true_bits;
}
#endif

#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
	defined(PNG_WRITE_INTERLACING_SUPPORTED)
int PNGAPI
png_set_interlace_handling(png_structp png_ptr)
{
   png_debug(1, "in png_set_interlace handling\n");
   if (png_ptr && png_ptr->interlaced)
   {
	  png_ptr->transformations |= PNG_INTERLACE;
	  return (7);
   }

   return (1);
}
#endif

#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
void PNGAPI
png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc)
{
   png_debug(1, "in png_set_filler\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_FILLER;
   png_ptr->filler = (png_byte)filler;
   if (filler_loc == PNG_FILLER_AFTER)
	  png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
   else
	  png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;

   if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
   {
	  png_ptr->usr_channels = 4;
   }

   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8)
   {
	  png_ptr->usr_channels = 2;
   }
}

#if !defined(PNG_1_0_X)
void PNGAPI
png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc)
{
   png_debug(1, "in png_set_add_alpha\n");
   if(png_ptr == NULL) return;
   png_set_filler(png_ptr, filler, filler_loc);
   png_ptr->transformations |= PNG_ADD_ALPHA;
}
#endif

#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
	defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
void PNGAPI
png_set_swap_alpha(png_structp png_ptr)
{
   png_debug(1, "in png_set_swap_alpha\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_SWAP_ALPHA;
}
#endif

#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
	defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
void PNGAPI
png_set_invert_alpha(png_structp png_ptr)
{
   png_debug(1, "in png_set_invert_alpha\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_INVERT_ALPHA;
}
#endif

#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
void PNGAPI
png_set_invert_mono(png_structp png_ptr)
{
   png_debug(1, "in png_set_invert_mono\n");
   if(png_ptr == NULL) return;
   png_ptr->transformations |= PNG_INVERT_MONO;
}

void /* PRIVATE */
png_do_invert(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_invert\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row == NULL || row_info == NULL)
	 return;
#endif
   if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
   {
	  png_bytep rp = row;
	  png_uint_32 i;
	  png_uint_32 istop = row_info->rowbytes;

	  for (i = 0; i < istop; i++)
	  {
		 *rp = (png_byte)(~(*rp));
		 rp++;
	  }
   }
   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
	  row_info->bit_depth == 8)
   {
	  png_bytep rp = row;
	  png_uint_32 i;
	  png_uint_32 istop = row_info->rowbytes;

	  for (i = 0; i < istop; i+=2)
	  {
		 *rp = (png_byte)(~(*rp));
		 rp+=2;
	  }
   }
   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
	  row_info->bit_depth == 16)
   {
	  png_bytep rp = row;
	  png_uint_32 i;
	  png_uint_32 istop = row_info->rowbytes;

	  for (i = 0; i < istop; i+=4)
	  {
		 *rp = (png_byte)(~(*rp));
		 *(rp+1) = (png_byte)(~(*(rp+1)));
		 rp+=4;
	  }
   }
}
#endif

#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
void /* PRIVATE */
png_do_swap(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_swap\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	   row_info->bit_depth == 16)
   {
	  png_bytep rp = row;
	  png_uint_32 i;
	  png_uint_32 istop= row_info->width * row_info->channels;

	  for (i = 0; i < istop; i++, rp += 2)
	  {
		 png_byte t = *rp;
		 *rp = *(rp + 1);
		 *(rp + 1) = t;
	  }
   }
}
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
static PNG_CONST png_byte onebppswaptable[256] = {
   0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
   0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
   0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
   0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
   0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
   0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
   0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
   0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
   0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
   0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
   0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
   0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
   0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
   0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
   0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
   0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
   0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
   0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
   0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
   0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
   0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
   0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
   0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
   0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
   0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
   0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
   0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
   0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
   0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
   0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
   0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
   0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
};

static PNG_CONST png_byte twobppswaptable[256] = {
   0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,
   0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,
   0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,
   0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,
   0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,
   0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,
   0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,
   0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,
   0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,
   0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,
   0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,
   0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,
   0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,
   0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,
   0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,
   0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,
   0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,
   0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,
   0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,
   0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,
   0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,
   0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,
   0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,
   0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,
   0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,
   0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,
   0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,
   0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,
   0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,
   0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,
   0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,
   0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF
};

static PNG_CONST png_byte fourbppswaptable[256] = {
   0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
   0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
   0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,
   0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,
   0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,
   0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,
   0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,
   0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,
   0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,
   0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,
   0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,
   0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,
   0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,
   0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,
   0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,
   0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,
   0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,
   0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,
   0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,
   0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,
   0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,
   0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,
   0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,
   0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,
   0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,
   0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,
   0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,
   0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,
   0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,
   0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,
   0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,
   0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF
};

void /* PRIVATE */
png_do_packswap(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_packswap\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	   row_info->bit_depth < 8)
   {
	  png_bytep rp, end, table;

	  end = row + row_info->rowbytes;

	  if (row_info->bit_depth == 1)
		 table = (png_bytep)onebppswaptable;
	  else if (row_info->bit_depth == 2)
		 table = (png_bytep)twobppswaptable;
	  else if (row_info->bit_depth == 4)
		 table = (png_bytep)fourbppswaptable;
	  else
		 return;

	  for (rp = row; rp < end; rp++)
		 *rp = table[*rp];
   }
}
#endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */

#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
	defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
void /* PRIVATE */
png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags)
{
   png_debug(1, "in png_do_strip_filler\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
	  png_bytep sp=row;
	  png_bytep dp=row;
	  png_uint_32 row_width=row_info->width;
	  png_uint_32 i;

	  if ((row_info->color_type == PNG_COLOR_TYPE_RGB ||
		 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
		 (flags & PNG_FLAG_STRIP_ALPHA))) &&
		 row_info->channels == 4)
	  {
		 if (row_info->bit_depth == 8)
		 {
			if (flags & PNG_FLAG_FILLER_AFTER)
			{
			   dp+=3; sp+=4;
			   for (i = 1; i < row_width; i++)
			   {
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  sp++;
			   }
			}
			else
			{
			   for (i = 0; i < row_width; i++)
			   {
				  sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
			   }
			}
			row_info->pixel_depth = 24;
			row_info->rowbytes = row_width * 3;
		 }
		 else /* if (row_info->bit_depth == 16) */
		 {
			if (flags & PNG_FLAG_FILLER_AFTER)
			{
			   sp += 8; dp += 6;
			   for (i = 1; i < row_width; i++)
			   {

				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  sp += 2;
			   }
			}
			else
			{
			   for (i = 0; i < row_width; i++)
			   {

				  sp+=2;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
			   }
			}
			row_info->pixel_depth = 48;
			row_info->rowbytes = row_width * 6;
		 }
		 row_info->channels = 3;
	  }
	  else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY ||
		 (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
		 (flags & PNG_FLAG_STRIP_ALPHA))) &&
		  row_info->channels == 2)
	  {
		 if (row_info->bit_depth == 8)
		 {
			if (flags & PNG_FLAG_FILLER_AFTER)
			{
			   for (i = 0; i < row_width; i++)
			   {
				  *dp++ = *sp++;
				  sp++;
			   }
			}
			else
			{
			   for (i = 0; i < row_width; i++)
			   {
				  sp++;
				  *dp++ = *sp++;
			   }
			}
			row_info->pixel_depth = 8;
			row_info->rowbytes = row_width;
		 }
		 else /* if (row_info->bit_depth == 16) */
		 {
			if (flags & PNG_FLAG_FILLER_AFTER)
			{
			   sp += 4; dp += 2;
			   for (i = 1; i < row_width; i++)
			   {
				  *dp++ = *sp++;
				  *dp++ = *sp++;
				  sp += 2;
			   }
			}
			else
			{
			   for (i = 0; i < row_width; i++)
			   {
				  sp += 2;
				  *dp++ = *sp++;
				  *dp++ = *sp++;
			   }
			}
			row_info->pixel_depth = 16;
			row_info->rowbytes = row_width * 2;
		 }
		 row_info->channels = 1;
	  }
	  if (flags & PNG_FLAG_STRIP_ALPHA)
		row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
   }
}
#endif

#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
void /* PRIVATE */
png_do_bgr(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_bgr\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	   (row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
	  png_uint_32 row_width = row_info->width;
	  if (row_info->bit_depth == 8)
	  {
		 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
		 {
			png_bytep rp;
			png_uint_32 i;

			for (i = 0, rp = row; i < row_width; i++, rp += 3)
			{
			   png_byte save = *rp;
			   *rp = *(rp + 2);
			   *(rp + 2) = save;
			}
		 }
		 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
		 {
			png_bytep rp;
			png_uint_32 i;

			for (i = 0, rp = row; i < row_width; i++, rp += 4)
			{
			   png_byte save = *rp;
			   *rp = *(rp + 2);
			   *(rp + 2) = save;
			}
		 }
	  }
	  else if (row_info->bit_depth == 16)
	  {
		 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
		 {
			png_bytep rp;
			png_uint_32 i;

			for (i = 0, rp = row; i < row_width; i++, rp += 6)
			{
			   png_byte save = *rp;
			   *rp = *(rp + 4);
			   *(rp + 4) = save;
			   save = *(rp + 1);
			   *(rp + 1) = *(rp + 5);
			   *(rp + 5) = save;
			}
		 }
		 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
		 {
			png_bytep rp;
			png_uint_32 i;

			for (i = 0, rp = row; i < row_width; i++, rp += 8)
			{
			   png_byte save = *rp;
			   *rp = *(rp + 4);
			   *(rp + 4) = save;
			   save = *(rp + 1);
			   *(rp + 1) = *(rp + 5);
			   *(rp + 5) = save;
			}
		 }
	  }
   }
}
#endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
	defined(PNG_LEGACY_SUPPORTED)
void PNGAPI
png_set_user_transform_info(png_structp png_ptr, png_voidp
   user_transform_ptr, int user_transform_depth, int user_transform_channels)
{
   png_debug(1, "in png_set_user_transform_info\n");
   if(png_ptr == NULL) return;
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
   png_ptr->user_transform_ptr = user_transform_ptr;
   png_ptr->user_transform_depth = (png_byte)user_transform_depth;
   png_ptr->user_transform_channels = (png_byte)user_transform_channels;
#else
   if(user_transform_ptr || user_transform_depth || user_transform_channels)
	  png_warning(png_ptr,
		"This version of libpng does not support user transform info");
#endif
}
#endif

png_voidp PNGAPI
png_get_user_transform_ptr(png_structp png_ptr)
{
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
   if (png_ptr == NULL) return (NULL);
   return ((png_voidp)png_ptr->user_transform_ptr);
#else
   return (NULL);
#endif
}
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngtrans.c ***/


/*** Start of inlined file: pngwio.c ***/
#define PNG_INTERNAL

#ifdef PNG_WRITE_SUPPORTED

void /* PRIVATE */
png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   if (png_ptr->write_data_fn != NULL )
	  (*(png_ptr->write_data_fn))(png_ptr, data, length);
   else
	  png_error(png_ptr, "Call to NULL write function");
}

#if !defined(PNG_NO_STDIO)
#ifndef USE_FAR_KEYWORD
void PNGAPI
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   png_uint_32 check;

   if(png_ptr == NULL) return;
#if defined(_WIN32_WCE)
   if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
	  check = 0;
#else
   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
#endif
   if (check != length)
	  png_error(png_ptr, "Write Error");
}
#else

#define NEAR_BUF_SIZE 1024
#define MIN(a,b) (a <= b ? a : b)

void PNGAPI
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   png_uint_32 check;
   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
   png_FILE_p io_ptr;

   if(png_ptr == NULL) return;
   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   if ((png_bytep)near_data == data)
   {
#if defined(_WIN32_WCE)
	  if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
		 check = 0;
#else
	  check = fwrite(near_data, 1, length, io_ptr);
#endif
   }
   else
   {
	  png_byte buf[NEAR_BUF_SIZE];
	  png_size_t written, remaining, err;
	  check = 0;
	  remaining = length;
	  do
	  {
		 written = MIN(NEAR_BUF_SIZE, remaining);
		 png_memcpy(buf, data, written); /* copy far buffer to near buffer */
#if defined(_WIN32_WCE)
		 if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
			err = 0;
#else
		 err = fwrite(buf, 1, written, io_ptr);
#endif
		 if (err != written)
			break;
		 else
			check += err;
		 data += written;
		 remaining -= written;
	  }
	  while (remaining != 0);
   }
   if (check != length)
	  png_error(png_ptr, "Write Error");
}

#endif
#endif

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
void /* PRIVATE */
png_flush(png_structp png_ptr)
{
   if (png_ptr->output_flush_fn != NULL)
	  (*(png_ptr->output_flush_fn))(png_ptr);
}

#if !defined(PNG_NO_STDIO)
void PNGAPI
png_default_flush(png_structp png_ptr)
{
#if !defined(_WIN32_WCE)
   png_FILE_p io_ptr;
#endif
   if(png_ptr == NULL) return;
#if !defined(_WIN32_WCE)
   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
   if (io_ptr != NULL)
	  fflush(io_ptr);
#endif
}
#endif
#endif

void PNGAPI
png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
{
   if(png_ptr == NULL) return;
   png_ptr->io_ptr = io_ptr;

#if !defined(PNG_NO_STDIO)
   if (write_data_fn != NULL)
	  png_ptr->write_data_fn = write_data_fn;
   else
	  png_ptr->write_data_fn = png_default_write_data;
#else
   png_ptr->write_data_fn = write_data_fn;
#endif

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
#if !defined(PNG_NO_STDIO)
   if (output_flush_fn != NULL)
	  png_ptr->output_flush_fn = output_flush_fn;
   else
	  png_ptr->output_flush_fn = png_default_flush;
#else
   png_ptr->output_flush_fn = output_flush_fn;
#endif
#endif /* PNG_WRITE_FLUSH_SUPPORTED */

   if (png_ptr->read_data_fn != NULL)
   {
	  png_ptr->read_data_fn = NULL;
	  png_warning(png_ptr,
		 "Attempted to set both read_data_fn and write_data_fn in");
	  png_warning(png_ptr,
		 "the same structure.  Resetting read_data_fn to NULL.");
   }
}

#if defined(USE_FAR_KEYWORD)
#if defined(_MSC_VER)
void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check)
{
   void *near_ptr;
   void FAR *far_ptr;
   FP_OFF(near_ptr) = FP_OFF(ptr);
   far_ptr = (void FAR *)near_ptr;
   if(check != 0)
	  if(FP_SEG(ptr) != FP_SEG(far_ptr))
		 png_error(png_ptr,"segment lost in conversion");
   return(near_ptr);
}
#  else
void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check)
{
   void *near_ptr;
   void FAR *far_ptr;
   near_ptr = (void FAR *)ptr;
   far_ptr = (void FAR *)near_ptr;
   if(check != 0)
	  if(far_ptr != ptr)
		 png_error(png_ptr,"segment lost in conversion");
   return(near_ptr);
}
#   endif
#   endif
#endif /* PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngwio.c ***/


/*** Start of inlined file: pngwrite.c ***/
#define PNG_INTERNAL

#ifdef PNG_WRITE_SUPPORTED

void PNGAPI
png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
{
   png_debug(1, "in png_write_info_before_PLTE\n");
   if (png_ptr == NULL || info_ptr == NULL)
	  return;
   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
   {
   png_write_sig(png_ptr); /* write PNG signature */
#if defined(PNG_MNG_FEATURES_SUPPORTED)
   if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
   {
	  png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
	  png_ptr->mng_features_permitted=0;
   }
#endif
   png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
	  info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
	  info_ptr->filter_type,
#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
	  info_ptr->interlace_type);
#else
	  0);
#endif
#if defined(PNG_WRITE_gAMA_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_gAMA)
   {
#  ifdef PNG_FLOATING_POINT_SUPPORTED
	  png_write_gAMA(png_ptr, info_ptr->gamma);
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
	  png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
#  endif
#endif
   }
#endif
#if defined(PNG_WRITE_sRGB_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_sRGB)
	  png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
#endif
#if defined(PNG_WRITE_iCCP_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_iCCP)
	  png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
					 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
#endif
#if defined(PNG_WRITE_sBIT_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_sBIT)
	  png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
#endif
#if defined(PNG_WRITE_cHRM_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_cHRM)
   {
#ifdef PNG_FLOATING_POINT_SUPPORTED
	  png_write_cHRM(png_ptr,
		 info_ptr->x_white, info_ptr->y_white,
		 info_ptr->x_red, info_ptr->y_red,
		 info_ptr->x_green, info_ptr->y_green,
		 info_ptr->x_blue, info_ptr->y_blue);
#else
#  ifdef PNG_FIXED_POINT_SUPPORTED
	  png_write_cHRM_fixed(png_ptr,
		 info_ptr->int_x_white, info_ptr->int_y_white,
		 info_ptr->int_x_red, info_ptr->int_y_red,
		 info_ptr->int_x_green, info_ptr->int_y_green,
		 info_ptr->int_x_blue, info_ptr->int_y_blue);
#  endif
#endif
   }
#endif
#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
   if (info_ptr->unknown_chunks_num)
   {
	   png_unknown_chunk *up;

	   png_debug(5, "writing extra chunks\n");

	   for (up = info_ptr->unknown_chunks;
			up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
			up++)
	   {
		 int keep=png_handle_as_unknown(png_ptr, up->name);
		 if (keep != PNG_HANDLE_CHUNK_NEVER &&
			up->location && !(up->location & PNG_HAVE_PLTE) &&
			!(up->location & PNG_HAVE_IDAT) &&
			((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
			(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
		 {
			png_write_chunk(png_ptr, up->name, up->data, up->size);
		 }
	   }
   }
#endif
	  png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
   }
}

void PNGAPI
png_write_info(png_structp png_ptr, png_infop info_ptr)
{
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
   int i;
#endif

   png_debug(1, "in png_write_info\n");

   if (png_ptr == NULL || info_ptr == NULL)
	  return;

   png_write_info_before_PLTE(png_ptr, info_ptr);

   if (info_ptr->valid & PNG_INFO_PLTE)
	  png_write_PLTE(png_ptr, info_ptr->palette,
		 (png_uint_32)info_ptr->num_palette);
   else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	  png_error(png_ptr, "Valid palette required for paletted images");

#if defined(PNG_WRITE_tRNS_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_tRNS)
	  {
#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
		 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
			info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
		 {
			int j;
			for (j=0; j<(int)info_ptr->num_trans; j++)
			   info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
		 }
#endif
	  png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
		 info_ptr->num_trans, info_ptr->color_type);
	  }
#endif
#if defined(PNG_WRITE_bKGD_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_bKGD)
	  png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
#endif
#if defined(PNG_WRITE_hIST_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_hIST)
	  png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
#endif
#if defined(PNG_WRITE_oFFs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_oFFs)
	  png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
		 info_ptr->offset_unit_type);
#endif
#if defined(PNG_WRITE_pCAL_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_pCAL)
	  png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
		 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
		 info_ptr->pcal_units, info_ptr->pcal_params);
#endif
#if defined(PNG_WRITE_sCAL_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_sCAL)
#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
	  png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
		  info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
	  png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
		  info_ptr->scal_s_width, info_ptr->scal_s_height);
#else
	  png_warning(png_ptr,
		  "png_write_sCAL not supported; sCAL chunk not written.");
#endif
#endif
#endif
#if defined(PNG_WRITE_pHYs_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_pHYs)
	  png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
		 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
#endif
#if defined(PNG_WRITE_tIME_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_tIME)
   {
	  png_write_tIME(png_ptr, &(info_ptr->mod_time));
	  png_ptr->mode |= PNG_WROTE_tIME;
   }
#endif
#if defined(PNG_WRITE_sPLT_SUPPORTED)
   if (info_ptr->valid & PNG_INFO_sPLT)
	 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
	   png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
#endif
#if defined(PNG_WRITE_TEXT_SUPPORTED)
   for (i = 0; i < info_ptr->num_text; i++)
   {
	  png_debug2(2, "Writing header text chunk %d, type %d\n", i,
		 info_ptr->text[i].compression);
	  if (info_ptr->text[i].compression > 0)
	  {
#if defined(PNG_WRITE_iTXt_SUPPORTED)
		  png_write_iTXt(png_ptr,
						 info_ptr->text[i].compression,
						 info_ptr->text[i].key,
						 info_ptr->text[i].lang,
						 info_ptr->text[i].lang_key,
						 info_ptr->text[i].text);
#else
		  png_warning(png_ptr, "Unable to write international text");
#endif
		  info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
	  }
	  else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
	  {
#if defined(PNG_WRITE_zTXt_SUPPORTED)
		 png_write_zTXt(png_ptr, info_ptr->text[i].key,
			info_ptr->text[i].text, 0,
			info_ptr->text[i].compression);
#else
		 png_warning(png_ptr, "Unable to write compressed text");
#endif
		 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
	  }
	  else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
	  {
#if defined(PNG_WRITE_tEXt_SUPPORTED)
		 png_write_tEXt(png_ptr, info_ptr->text[i].key,
						 info_ptr->text[i].text,
						 0);
#else
		 png_warning(png_ptr, "Unable to write uncompressed text");
#endif
		 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
	  }
   }
#endif
#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
   if (info_ptr->unknown_chunks_num)
   {
	   png_unknown_chunk *up;

	   png_debug(5, "writing extra chunks\n");

	   for (up = info_ptr->unknown_chunks;
			up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
			up++)
	   {
		 int keep=png_handle_as_unknown(png_ptr, up->name);
		 if (keep != PNG_HANDLE_CHUNK_NEVER &&
			up->location && (up->location & PNG_HAVE_PLTE) &&
			!(up->location & PNG_HAVE_IDAT) &&
			((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
			(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
		 {
			png_write_chunk(png_ptr, up->name, up->data, up->size);
		 }
	   }
   }
#endif
}

void PNGAPI
png_write_end(png_structp png_ptr, png_infop info_ptr)
{
   png_debug(1, "in png_write_end\n");
   if (png_ptr == NULL)
	  return;
   if (!(png_ptr->mode & PNG_HAVE_IDAT))
	  png_error(png_ptr, "No IDATs written into file");

   if (info_ptr != NULL)
   {
#if defined(PNG_WRITE_TEXT_SUPPORTED)
	  int i; /* local index variable */
#endif
#if defined(PNG_WRITE_tIME_SUPPORTED)
	  if ((info_ptr->valid & PNG_INFO_tIME) &&
		 !(png_ptr->mode & PNG_WROTE_tIME))
		 png_write_tIME(png_ptr, &(info_ptr->mod_time));
#endif
#if defined(PNG_WRITE_TEXT_SUPPORTED)
	  for (i = 0; i < info_ptr->num_text; i++)
	  {
		 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
			info_ptr->text[i].compression);
		 if (info_ptr->text[i].compression > 0)
		 {
#if defined(PNG_WRITE_iTXt_SUPPORTED)
			 png_write_iTXt(png_ptr,
						 info_ptr->text[i].compression,
						 info_ptr->text[i].key,
						 info_ptr->text[i].lang,
						 info_ptr->text[i].lang_key,
						 info_ptr->text[i].text);
#else
			 png_warning(png_ptr, "Unable to write international text");
#endif
			 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
		 }
		 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
		 {
#if defined(PNG_WRITE_zTXt_SUPPORTED)
			png_write_zTXt(png_ptr, info_ptr->text[i].key,
			   info_ptr->text[i].text, 0,
			   info_ptr->text[i].compression);
#else
			png_warning(png_ptr, "Unable to write compressed text");
#endif
			info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
		 }
		 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
		 {
#if defined(PNG_WRITE_tEXt_SUPPORTED)
			png_write_tEXt(png_ptr, info_ptr->text[i].key,
			   info_ptr->text[i].text, 0);
#else
			png_warning(png_ptr, "Unable to write uncompressed text");
#endif

			info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
		 }
	  }
#endif
#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
   if (info_ptr->unknown_chunks_num)
   {
	   png_unknown_chunk *up;

	   png_debug(5, "writing extra chunks\n");

	   for (up = info_ptr->unknown_chunks;
			up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
			up++)
	   {
		 int keep=png_handle_as_unknown(png_ptr, up->name);
		 if (keep != PNG_HANDLE_CHUNK_NEVER &&
			up->location && (up->location & PNG_AFTER_IDAT) &&
			((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
			(png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
		 {
			png_write_chunk(png_ptr, up->name, up->data, up->size);
		 }
	   }
   }
#endif
   }

   png_ptr->mode |= PNG_AFTER_IDAT;

   png_write_IEND(png_ptr);
}

#if defined(PNG_WRITE_tIME_SUPPORTED)
#if !defined(_WIN32_WCE)
void PNGAPI
png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
{
   png_debug(1, "in png_convert_from_struct_tm\n");
   ptime->year = (png_uint_16)(1900 + ttime->tm_year);
   ptime->month = (png_byte)(ttime->tm_mon + 1);
   ptime->day = (png_byte)ttime->tm_mday;
   ptime->hour = (png_byte)ttime->tm_hour;
   ptime->minute = (png_byte)ttime->tm_min;
   ptime->second = (png_byte)ttime->tm_sec;
}

void PNGAPI
png_convert_from_time_t(png_timep ptime, time_t ttime)
{
   struct tm *tbuf;

   png_debug(1, "in png_convert_from_time_t\n");
   tbuf = gmtime(&ttime);
   png_convert_from_struct_tm(ptime, tbuf);
}
#endif
#endif

png_structp PNGAPI
png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn)
{
#ifdef PNG_USER_MEM_SUPPORTED
   return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
	  warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
}

png_structp PNGAPI
png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
{
#endif /* PNG_USER_MEM_SUPPORTED */
   png_structp png_ptr;
#ifdef PNG_SETJMP_SUPPORTED
#ifdef USE_FAR_KEYWORD
   jmp_buf jmpbuf;
#endif
#endif
   int i;
   png_debug(1, "in png_create_write_struct\n");
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
	  (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
#else
   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
#endif /* PNG_USER_MEM_SUPPORTED */
   if (png_ptr == NULL)
	  return (NULL);

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
#endif

#ifdef PNG_SETJMP_SUPPORTED
#ifdef USE_FAR_KEYWORD
   if (setjmp(jmpbuf))
#else
   if (setjmp(png_ptr->jmpbuf))
#endif
   {
	  png_free(png_ptr, png_ptr->zbuf);
	  png_ptr->zbuf=NULL;
	  png_destroy_struct(png_ptr);
	  return (NULL);
   }
#ifdef USE_FAR_KEYWORD
   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
#endif
#endif

#ifdef PNG_USER_MEM_SUPPORTED
   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
#endif /* PNG_USER_MEM_SUPPORTED */
   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);

   i=0;
   do
   {
	 if(user_png_ver[i] != png_libpng_ver[i])
		png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
   } while (png_libpng_ver[i++]);

   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
   {
	 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
		 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
		 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
	 {
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
		char msg[80];
		if (user_png_ver)
		{
		  png_snprintf(msg, 80,
			 "Application was compiled with png.h from libpng-%.20s",
			 user_png_ver);
		  png_warning(png_ptr, msg);
		}
		png_snprintf(msg, 80,
		   "Application  is  running with png.c from libpng-%.20s",
		   png_libpng_ver);
		png_warning(png_ptr, msg);
#endif
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
		png_ptr->flags=0;
#endif
		png_error(png_ptr,
		   "Incompatible libpng version in application and library");
	 }
   }

   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
	  (png_uint_32)png_ptr->zbuf_size);

   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
	  png_flush_ptr_NULL);

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
	  1, png_doublep_NULL, png_doublep_NULL);
#endif

#ifdef PNG_SETJMP_SUPPORTED
#ifdef USE_FAR_KEYWORD
   if (setjmp(jmpbuf))
	  PNG_ABORT();
   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
#else
   if (setjmp(png_ptr->jmpbuf))
	  PNG_ABORT();
#endif
#endif
   return (png_ptr);
}

#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
#undef png_write_init
void PNGAPI
png_write_init(png_structp png_ptr)
{
   png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
}

void PNGAPI
png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
   png_size_t png_struct_size, png_size_t png_info_size)
{
   if(png_ptr == NULL) return;
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
   if(png_sizeof(png_struct) > png_struct_size ||
	  png_sizeof(png_info) > png_info_size)
   {
	  char msg[80];
	  png_ptr->warning_fn=NULL;
	  if (user_png_ver)
	  {
		png_snprintf(msg, 80,
		   "Application was compiled with png.h from libpng-%.20s",
		   user_png_ver);
		png_warning(png_ptr, msg);
	  }
	  png_snprintf(msg, 80,
		 "Application  is  running with png.c from libpng-%.20s",
		 png_libpng_ver);
	  png_warning(png_ptr, msg);
   }
#endif
   if(png_sizeof(png_struct) > png_struct_size)
	 {
	   png_ptr->error_fn=NULL;
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
	   png_ptr->flags=0;
#endif
	   png_error(png_ptr,
	   "The png struct allocated by the application for writing is too small.");
	 }
   if(png_sizeof(png_info) > png_info_size)
	 {
	   png_ptr->error_fn=NULL;
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
	   png_ptr->flags=0;
#endif
	   png_error(png_ptr,
	   "The info struct allocated by the application for writing is too small.");
	 }
   png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
}
#endif /* PNG_1_0_X || PNG_1_2_X */

void PNGAPI
png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
   png_size_t png_struct_size)
{
   png_structp png_ptr=*ptr_ptr;
#ifdef PNG_SETJMP_SUPPORTED
   jmp_buf tmp_jmp; /* to save current jump buffer */
#endif

   int i = 0;

   if (png_ptr == NULL)
	  return;

   do
   {
	 if (user_png_ver[i] != png_libpng_ver[i])
	 {
#ifdef PNG_LEGACY_SUPPORTED
	   png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
#else
	   png_ptr->warning_fn=NULL;
	   png_warning(png_ptr,
	 "Application uses deprecated png_write_init() and should be recompiled.");
	   break;
#endif
	 }
   } while (png_libpng_ver[i++]);

   png_debug(1, "in png_write_init_3\n");

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
#endif

   if (png_sizeof(png_struct) > png_struct_size)
	 {
	   png_destroy_struct(png_ptr);
	   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
	   *ptr_ptr = png_ptr;
	 }

   png_memset(png_ptr, 0, png_sizeof (png_struct));

#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
#endif

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
#endif

   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
	  png_flush_ptr_NULL);

   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
	  (png_uint_32)png_ptr->zbuf_size);

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
	  1, png_doublep_NULL, png_doublep_NULL);
#endif
}

void PNGAPI
png_write_rows(png_structp png_ptr, png_bytepp row,
   png_uint_32 num_rows)
{
   png_uint_32 i; /* row counter */
   png_bytepp rp; /* row pointer */

   png_debug(1, "in png_write_rows\n");

   if (png_ptr == NULL)
	  return;

   for (i = 0, rp = row; i < num_rows; i++, rp++)
   {
	  png_write_row(png_ptr, *rp);
   }
}

void PNGAPI
png_write_image(png_structp png_ptr, png_bytepp image)
{
   png_uint_32 i; /* row index */
   int pass, num_pass; /* pass variables */
   png_bytepp rp; /* points to current row */

   if (png_ptr == NULL)
	  return;

   png_debug(1, "in png_write_image\n");
#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
   num_pass = png_set_interlace_handling(png_ptr);
#else
   num_pass = 1;
#endif
   for (pass = 0; pass < num_pass; pass++)
   {
	  for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
	  {
		 png_write_row(png_ptr, *rp);
	  }
   }
}

void PNGAPI
png_write_row(png_structp png_ptr, png_bytep row)
{
   if (png_ptr == NULL)
	  return;
   png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
	  png_ptr->row_number, png_ptr->pass);

   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
   {
   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
	  png_error(png_ptr,
		 "png_write_info was never called before png_write_row.");

#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_MONO)
	  png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
#endif
#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
	  png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
#endif
#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_PACKSWAP)
	  png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
#endif
#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
   if (png_ptr->transformations & PNG_PACK)
	  png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
#endif
#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
   if (png_ptr->transformations & PNG_SHIFT)
	  png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
#endif
#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
   if (png_ptr->transformations & PNG_BGR)
	  png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
#endif
#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
	  png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
#endif

	  png_write_start_row(png_ptr);
   }

#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
   {
	  switch (png_ptr->pass)
	  {
		 case 0:
			if (png_ptr->row_number & 0x07)
			{
			   png_write_finish_row(png_ptr);
			   return;
			}
			break;
		 case 1:
			if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
			{
			   png_write_finish_row(png_ptr);
			   return;
			}
			break;
		 case 2:
			if ((png_ptr->row_number & 0x07) != 4)
			{
			   png_write_finish_row(png_ptr);
			   return;
			}
			break;
		 case 3:
			if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
			{
			   png_write_finish_row(png_ptr);
			   return;
			}
			break;
		 case 4:
			if ((png_ptr->row_number & 0x03) != 2)
			{
			   png_write_finish_row(png_ptr);
			   return;
			}
			break;
		 case 5:
			if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
			{
			   png_write_finish_row(png_ptr);
			   return;
			}
			break;
		 case 6:
			if (!(png_ptr->row_number & 0x01))
			{
			   png_write_finish_row(png_ptr);
			   return;
			}
			break;
	  }
   }
#endif

   png_ptr->row_info.color_type = png_ptr->color_type;
   png_ptr->row_info.width = png_ptr->usr_width;
   png_ptr->row_info.channels = png_ptr->usr_channels;
   png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
   png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
	  png_ptr->row_info.channels);

   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
	  png_ptr->row_info.width);

   png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
   png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
   png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
   png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
   png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
   png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);

   png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
	  png_ptr->row_info.rowbytes);

#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
   if (png_ptr->interlaced && png_ptr->pass < 6 &&
	  (png_ptr->transformations & PNG_INTERLACE))
   {
	  png_do_write_interlace(&(png_ptr->row_info),
		 png_ptr->row_buf + 1, png_ptr->pass);
	  if (!(png_ptr->row_info.width))
	  {
		 png_write_finish_row(png_ptr);
		 return;
	  }
   }
#endif

   if (png_ptr->transformations)
	  png_do_write_transformations(png_ptr);

#if defined(PNG_MNG_FEATURES_SUPPORTED)
   if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
	  (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
   {
	  png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
   }
#endif

   png_write_find_filter(png_ptr, &(png_ptr->row_info));

   if (png_ptr->write_row_fn != NULL)
	  (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
}

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
void PNGAPI
png_set_flush(png_structp png_ptr, int nrows)
{
   png_debug(1, "in png_set_flush\n");
   if (png_ptr == NULL)
	  return;
   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
}

void PNGAPI
png_write_flush(png_structp png_ptr)
{
   int wrote_IDAT;

   png_debug(1, "in png_write_flush\n");
   if (png_ptr == NULL)
	  return;
   if (png_ptr->row_number >= png_ptr->num_rows)
	 return;

   do
   {
	  int ret;

	  ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
	  wrote_IDAT = 0;

	  if (ret != Z_OK)
	  {
		 if (png_ptr->zstream.msg != NULL)
			png_error(png_ptr, png_ptr->zstream.msg);
		 else
			png_error(png_ptr, "zlib error");
	  }

	  if (!(png_ptr->zstream.avail_out))
	  {
		 png_write_IDAT(png_ptr, png_ptr->zbuf,
						png_ptr->zbuf_size);
		 png_ptr->zstream.next_out = png_ptr->zbuf;
		 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
		 wrote_IDAT = 1;
	  }
   } while(wrote_IDAT == 1);

   if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
   {
	  png_write_IDAT(png_ptr, png_ptr->zbuf,
					 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
	  png_ptr->zstream.next_out = png_ptr->zbuf;
	  png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
   }
   png_ptr->flush_rows = 0;
   png_flush(png_ptr);
}
#endif /* PNG_WRITE_FLUSH_SUPPORTED */

void PNGAPI
png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
{
   png_structp png_ptr = NULL;
   png_infop info_ptr = NULL;
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn = NULL;
   png_voidp mem_ptr = NULL;
#endif

   png_debug(1, "in png_destroy_write_struct\n");
   if (png_ptr_ptr != NULL)
   {
	  png_ptr = *png_ptr_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
	  free_fn = png_ptr->free_fn;
	  mem_ptr = png_ptr->mem_ptr;
#endif
   }

   if (info_ptr_ptr != NULL)
	  info_ptr = *info_ptr_ptr;

   if (info_ptr != NULL)
   {
	  png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);

#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
	  if (png_ptr->num_chunk_list)
	  {
		 png_free(png_ptr, png_ptr->chunk_list);
		 png_ptr->chunk_list=NULL;
		 png_ptr->num_chunk_list=0;
	  }
#endif

#ifdef PNG_USER_MEM_SUPPORTED
	  png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
		 (png_voidp)mem_ptr);
#else
	  png_destroy_struct((png_voidp)info_ptr);
#endif
	  *info_ptr_ptr = NULL;
   }

   if (png_ptr != NULL)
   {
	  png_write_destroy(png_ptr);
#ifdef PNG_USER_MEM_SUPPORTED
	  png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
		 (png_voidp)mem_ptr);
#else
	  png_destroy_struct((png_voidp)png_ptr);
#endif
	  *png_ptr_ptr = NULL;
   }
}

void /* PRIVATE */
png_write_destroy(png_structp png_ptr)
{
#ifdef PNG_SETJMP_SUPPORTED
   jmp_buf tmp_jmp; /* save jump buffer */
#endif
   png_error_ptr error_fn;
   png_error_ptr warning_fn;
   png_voidp error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn;
#endif

   png_debug(1, "in png_write_destroy\n");
   deflateEnd(&png_ptr->zstream);

   png_free(png_ptr, png_ptr->zbuf);
   png_free(png_ptr, png_ptr->row_buf);
   png_free(png_ptr, png_ptr->prev_row);
   png_free(png_ptr, png_ptr->sub_row);
   png_free(png_ptr, png_ptr->up_row);
   png_free(png_ptr, png_ptr->avg_row);
   png_free(png_ptr, png_ptr->paeth_row);

#if defined(PNG_TIME_RFC1123_SUPPORTED)
   png_free(png_ptr, png_ptr->time_buffer);
#endif

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
   png_free(png_ptr, png_ptr->prev_filters);
   png_free(png_ptr, png_ptr->filter_weights);
   png_free(png_ptr, png_ptr->inv_filter_weights);
   png_free(png_ptr, png_ptr->filter_costs);
   png_free(png_ptr, png_ptr->inv_filter_costs);
#endif

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
#endif

   error_fn = png_ptr->error_fn;
   warning_fn = png_ptr->warning_fn;
   error_ptr = png_ptr->error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif

   png_memset(png_ptr, 0, png_sizeof (png_struct));

   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
   png_ptr->error_ptr = error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
#endif
}

void PNGAPI
png_set_filter(png_structp png_ptr, int method, int filters)
{
   png_debug(1, "in png_set_filter\n");
   if (png_ptr == NULL)
	  return;
#if defined(PNG_MNG_FEATURES_SUPPORTED)
   if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
	  (method == PNG_INTRAPIXEL_DIFFERENCING))
		 method = PNG_FILTER_TYPE_BASE;
#endif
   if (method == PNG_FILTER_TYPE_BASE)
   {
	  switch (filters & (PNG_ALL_FILTERS | 0x07))
	  {
#ifndef PNG_NO_WRITE_FILTER
		 case 5:
		 case 6:
		 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
#endif /* PNG_NO_WRITE_FILTER */
		 case PNG_FILTER_VALUE_NONE:
			  png_ptr->do_filter=PNG_FILTER_NONE; break;
#ifndef PNG_NO_WRITE_FILTER
		 case PNG_FILTER_VALUE_SUB:
			  png_ptr->do_filter=PNG_FILTER_SUB; break;
		 case PNG_FILTER_VALUE_UP:
			  png_ptr->do_filter=PNG_FILTER_UP; break;
		 case PNG_FILTER_VALUE_AVG:
			  png_ptr->do_filter=PNG_FILTER_AVG; break;
		 case PNG_FILTER_VALUE_PAETH:
			  png_ptr->do_filter=PNG_FILTER_PAETH; break;
		 default: png_ptr->do_filter = (png_byte)filters; break;
#else
		 default: png_warning(png_ptr, "Unknown row filter for method 0");
#endif /* PNG_NO_WRITE_FILTER */
	  }

	  if (png_ptr->row_buf != NULL)
	  {
#ifndef PNG_NO_WRITE_FILTER
		 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
		 {
			png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
			  (png_ptr->rowbytes + 1));
			png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
		 }

		 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
		 {
			if (png_ptr->prev_row == NULL)
			{
			   png_warning(png_ptr, "Can't add Up filter after starting");
			   png_ptr->do_filter &= ~PNG_FILTER_UP;
			}
			else
			{
			   png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
				  (png_ptr->rowbytes + 1));
			   png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
			}
		 }

		 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
		 {
			if (png_ptr->prev_row == NULL)
			{
			   png_warning(png_ptr, "Can't add Average filter after starting");
			   png_ptr->do_filter &= ~PNG_FILTER_AVG;
			}
			else
			{
			   png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
				  (png_ptr->rowbytes + 1));
			   png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
			}
		 }

		 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
			 png_ptr->paeth_row == NULL)
		 {
			if (png_ptr->prev_row == NULL)
			{
			   png_warning(png_ptr, "Can't add Paeth filter after starting");
			   png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
			}
			else
			{
			   png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
				  (png_ptr->rowbytes + 1));
			   png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
			}
		 }

		 if (png_ptr->do_filter == PNG_NO_FILTERS)
#endif /* PNG_NO_WRITE_FILTER */
			png_ptr->do_filter = PNG_FILTER_NONE;
	  }
   }
   else
	  png_error(png_ptr, "Unknown custom filter method");
}

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)	  /* GRR 970116 */
void PNGAPI
png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
   int num_weights, png_doublep filter_weights,
   png_doublep filter_costs)
{
   int i;

   png_debug(1, "in png_set_filter_heuristics\n");
   if (png_ptr == NULL)
	  return;
   if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
   {
	  png_warning(png_ptr, "Unknown filter heuristic method");
	  return;
   }

   if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
   {
	  heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
   }

   if (num_weights < 0 || filter_weights == NULL ||
	  heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
   {
	  num_weights = 0;
   }

   png_ptr->num_prev_filters = (png_byte)num_weights;
   png_ptr->heuristic_method = (png_byte)heuristic_method;

   if (num_weights > 0)
   {
	  if (png_ptr->prev_filters == NULL)
	  {
		 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
			(png_uint_32)(png_sizeof(png_byte) * num_weights));

		 for (i = 0; i < num_weights; i++)
		 {
			png_ptr->prev_filters[i] = 255;
		 }
	  }

	  if (png_ptr->filter_weights == NULL)
	  {
		 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
			(png_uint_32)(png_sizeof(png_uint_16) * num_weights));

		 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
			(png_uint_32)(png_sizeof(png_uint_16) * num_weights));
		 for (i = 0; i < num_weights; i++)
		 {
			png_ptr->inv_filter_weights[i] =
			png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
		 }
	  }

	  for (i = 0; i < num_weights; i++)
	  {
		 if (filter_weights[i] < 0.0)
		 {
			png_ptr->inv_filter_weights[i] =
			png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
		 }
		 else
		 {
			png_ptr->inv_filter_weights[i] =
			   (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
			png_ptr->filter_weights[i] =
			   (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
		 }
	  }
   }

   if (png_ptr->filter_costs == NULL)
   {
	  png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
		 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));

	  png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
		 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));

	  for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
	  {
		 png_ptr->inv_filter_costs[i] =
		 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
	  }
   }

   for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
   {
	  if (filter_costs == NULL || filter_costs[i] < 0.0)
	  {
		 png_ptr->inv_filter_costs[i] =
		 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
	  }
	  else if (filter_costs[i] >= 1.0)
	  {
		 png_ptr->inv_filter_costs[i] =
			(png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
		 png_ptr->filter_costs[i] =
			(png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
	  }
   }
}
#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */

void PNGAPI
png_set_compression_level(png_structp png_ptr, int level)
{
   png_debug(1, "in png_set_compression_level\n");
   if (png_ptr == NULL)
	  return;
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
   png_ptr->zlib_level = level;
}

void PNGAPI
png_set_compression_mem_level(png_structp png_ptr, int mem_level)
{
   png_debug(1, "in png_set_compression_mem_level\n");
   if (png_ptr == NULL)
	  return;
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
   png_ptr->zlib_mem_level = mem_level;
}

void PNGAPI
png_set_compression_strategy(png_structp png_ptr, int strategy)
{
   png_debug(1, "in png_set_compression_strategy\n");
   if (png_ptr == NULL)
	  return;
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
   png_ptr->zlib_strategy = strategy;
}

void PNGAPI
png_set_compression_window_bits(png_structp png_ptr, int window_bits)
{
   if (png_ptr == NULL)
	  return;
   if (window_bits > 15)
	  png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
   else if (window_bits < 8)
	  png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
#ifndef WBITS_8_OK
   if (window_bits == 8)
	 {
	   png_warning(png_ptr, "Compression window is being reset to 512");
	   window_bits=9;
	 }
#endif
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
   png_ptr->zlib_window_bits = window_bits;
}

void PNGAPI
png_set_compression_method(png_structp png_ptr, int method)
{
   png_debug(1, "in png_set_compression_method\n");
   if (png_ptr == NULL)
	  return;
   if (method != 8)
	  png_warning(png_ptr, "Only compression method 8 is supported by PNG");
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
   png_ptr->zlib_method = method;
}

void PNGAPI
png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
{
   if (png_ptr == NULL)
	  return;
   png_ptr->write_row_fn = write_row_fn;
}

#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
void PNGAPI
png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
   write_user_transform_fn)
{
   png_debug(1, "in png_set_write_user_transform_fn\n");
   if (png_ptr == NULL)
	  return;
   png_ptr->transformations |= PNG_USER_TRANSFORM;
   png_ptr->write_user_transform_fn = write_user_transform_fn;
}
#endif

#if defined(PNG_INFO_IMAGE_SUPPORTED)
void PNGAPI
png_write_png(png_structp png_ptr, png_infop info_ptr,
			  int transforms, voidp params)
{
   if (png_ptr == NULL || info_ptr == NULL)
	  return;
#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
	   png_set_invert_alpha(png_ptr);
#endif

   png_write_info(png_ptr, info_ptr);

#if defined(PNG_WRITE_INVERT_SUPPORTED)
   if (transforms & PNG_TRANSFORM_INVERT_MONO)
	   png_set_invert_mono(png_ptr);
#endif

#if defined(PNG_WRITE_SHIFT_SUPPORTED)
   if ((transforms & PNG_TRANSFORM_SHIFT)
			   && (info_ptr->valid & PNG_INFO_sBIT))
	   png_set_shift(png_ptr, &info_ptr->sig_bit);
#endif

#if defined(PNG_WRITE_PACK_SUPPORTED)
   if (transforms & PNG_TRANSFORM_PACKING)
	   png_set_packing(png_ptr);
#endif

#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
	   png_set_swap_alpha(png_ptr);
#endif

#if defined(PNG_WRITE_FILLER_SUPPORTED)
   if (transforms & PNG_TRANSFORM_STRIP_FILLER)
	   png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
#endif

#if defined(PNG_WRITE_BGR_SUPPORTED)
   if (transforms & PNG_TRANSFORM_BGR)
	   png_set_bgr(png_ptr);
#endif

#if defined(PNG_WRITE_SWAP_SUPPORTED)
   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
	   png_set_swap(png_ptr);
#endif

#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
   if (transforms & PNG_TRANSFORM_PACKSWAP)
	   png_set_packswap(png_ptr);
#endif

   if (info_ptr->valid & PNG_INFO_IDAT)
	   png_write_image(png_ptr, info_ptr->row_pointers);

   png_write_end(png_ptr, info_ptr);

   transforms = transforms; /* quiet compiler warnings */
   params = params;
}
#endif
#endif /* PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngwrite.c ***/


/*** Start of inlined file: pngwtran.c ***/
#define PNG_INTERNAL

#ifdef PNG_WRITE_SUPPORTED

void /* PRIVATE */
png_do_write_transformations(png_structp png_ptr)
{
   png_debug(1, "in png_do_write_transformations\n");

   if (png_ptr == NULL)
	  return;

#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
	  if(png_ptr->write_user_transform_fn != NULL)
		(*(png_ptr->write_user_transform_fn)) /* user write transform function */
		  (png_ptr,			/* png_ptr */
		   &(png_ptr->row_info),	   /* row_info:	 */
		   png_ptr->row_buf + 1);	  /* start of pixel data for row */
#endif
#if defined(PNG_WRITE_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
	  png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
		 png_ptr->flags);
#endif
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_PACKSWAP)
	  png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
#if defined(PNG_WRITE_PACK_SUPPORTED)
   if (png_ptr->transformations & PNG_PACK)
	  png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
		 (png_uint_32)png_ptr->bit_depth);
#endif
#if defined(PNG_WRITE_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
	  png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
#if defined(PNG_WRITE_SHIFT_SUPPORTED)
   if (png_ptr->transformations & PNG_SHIFT)
	  png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
		 &(png_ptr->shift));
#endif
#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_ALPHA)
	  png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_ALPHA)
	  png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
#if defined(PNG_WRITE_BGR_SUPPORTED)
   if (png_ptr->transformations & PNG_BGR)
	  png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
#if defined(PNG_WRITE_INVERT_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_MONO)
	  png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
}

#if defined(PNG_WRITE_PACK_SUPPORTED)
void /* PRIVATE */
png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
{
   png_debug(1, "in png_do_pack\n");
   if (row_info->bit_depth == 8 &&
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	  row_info->channels == 1)
   {
	  switch ((int)bit_depth)
	  {
		 case 1:
		 {
			png_bytep sp, dp;
			int mask, v;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			sp = row;
			dp = row;
			mask = 0x80;
			v = 0;

			for (i = 0; i < row_width; i++)
			{
			   if (*sp != 0)
				  v |= mask;
			   sp++;
			   if (mask > 1)
				  mask >>= 1;
			   else
			   {
				  mask = 0x80;
				  *dp = (png_byte)v;
				  dp++;
				  v = 0;
			   }
			}
			if (mask != 0x80)
			   *dp = (png_byte)v;
			break;
		 }
		 case 2:
		 {
			png_bytep sp, dp;
			int shift, v;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			sp = row;
			dp = row;
			shift = 6;
			v = 0;
			for (i = 0; i < row_width; i++)
			{
			   png_byte value;

			   value = (png_byte)(*sp & 0x03);
			   v |= (value << shift);
			   if (shift == 0)
			   {
				  shift = 6;
				  *dp = (png_byte)v;
				  dp++;
				  v = 0;
			   }
			   else
				  shift -= 2;
			   sp++;
			}
			if (shift != 6)
			   *dp = (png_byte)v;
			break;
		 }
		 case 4:
		 {
			png_bytep sp, dp;
			int shift, v;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			sp = row;
			dp = row;
			shift = 4;
			v = 0;
			for (i = 0; i < row_width; i++)
			{
			   png_byte value;

			   value = (png_byte)(*sp & 0x0f);
			   v |= (value << shift);

			   if (shift == 0)
			   {
				  shift = 4;
				  *dp = (png_byte)v;
				  dp++;
				  v = 0;
			   }
			   else
				  shift -= 4;

			   sp++;
			}
			if (shift != 4)
			   *dp = (png_byte)v;
			break;
		 }
	  }
	  row_info->bit_depth = (png_byte)bit_depth;
	  row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
	  row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
		 row_info->width);
   }
}
#endif

#if defined(PNG_WRITE_SHIFT_SUPPORTED)
void /* PRIVATE */
png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
{
   png_debug(1, "in png_do_shift\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL &&
#else
   if (
#endif
	  row_info->color_type != PNG_COLOR_TYPE_PALETTE)
   {
	  int shift_start[4], shift_dec[4];
	  int channels = 0;

	  if (row_info->color_type & PNG_COLOR_MASK_COLOR)
	  {
		 shift_start[channels] = row_info->bit_depth - bit_depth->red;
		 shift_dec[channels] = bit_depth->red;
		 channels++;
		 shift_start[channels] = row_info->bit_depth - bit_depth->green;
		 shift_dec[channels] = bit_depth->green;
		 channels++;
		 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
		 shift_dec[channels] = bit_depth->blue;
		 channels++;
	  }
	  else
	  {
		 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
		 shift_dec[channels] = bit_depth->gray;
		 channels++;
	  }
	  if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
	  {
		 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
		 shift_dec[channels] = bit_depth->alpha;
		 channels++;
	  }

	  if (row_info->bit_depth < 8)
	  {
		 png_bytep bp = row;
		 png_uint_32 i;
		 png_byte mask;
		 png_uint_32 row_bytes = row_info->rowbytes;

		 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
			mask = 0x55;
		 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
			mask = 0x11;
		 else
			mask = 0xff;

		 for (i = 0; i < row_bytes; i++, bp++)
		 {
			png_uint_16 v;
			int j;

			v = *bp;
			*bp = 0;
			for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
			{
			   if (j > 0)
				  *bp |= (png_byte)((v << j) & 0xff);
			   else
				  *bp |= (png_byte)((v >> (-j)) & mask);
			}
		 }
	  }
	  else if (row_info->bit_depth == 8)
	  {
		 png_bytep bp = row;
		 png_uint_32 i;
		 png_uint_32 istop = channels * row_info->width;

		 for (i = 0; i < istop; i++, bp++)
		 {

			png_uint_16 v;
			int j;
			int c = (int)(i%channels);

			v = *bp;
			*bp = 0;
			for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
			{
			   if (j > 0)
				  *bp |= (png_byte)((v << j) & 0xff);
			   else
				  *bp |= (png_byte)((v >> (-j)) & 0xff);
			}
		 }
	  }
	  else
	  {
		 png_bytep bp;
		 png_uint_32 i;
		 png_uint_32 istop = channels * row_info->width;

		 for (bp = row, i = 0; i < istop; i++)
		 {
			int c = (int)(i%channels);
			png_uint_16 value, v;
			int j;

			v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
			value = 0;
			for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
			{
			   if (j > 0)
				  value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
			   else
				  value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
			}
			*bp++ = (png_byte)(value >> 8);
			*bp++ = (png_byte)(value & 0xff);
		 }
	  }
   }
}
#endif

#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
void /* PRIVATE */
png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_write_swap_alpha\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
	  if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp, dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;
			for (i = 0, sp = dp = row; i < row_width; i++)
			{
			   png_byte save = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = save;
			}
		 }
		 else
		 {
			png_bytep sp, dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			for (i = 0, sp = dp = row; i < row_width; i++)
			{
			   png_byte save[2];
			   save[0] = *(sp++);
			   save[1] = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = save[0];
			   *(dp++) = save[1];
			}
		 }
	  }
	  else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp, dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			for (i = 0, sp = dp = row; i < row_width; i++)
			{
			   png_byte save = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = save;
			}
		 }
		 else
		 {
			png_bytep sp, dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			for (i = 0, sp = dp = row; i < row_width; i++)
			{
			   png_byte save[2];
			   save[0] = *(sp++);
			   save[1] = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = *(sp++);
			   *(dp++) = save[0];
			   *(dp++) = save[1];
			}
		 }
	  }
   }
}
#endif

#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
void /* PRIVATE */
png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_write_invert_alpha\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
	  if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp, dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;
			for (i = 0, sp = dp = row; i < row_width; i++)
			{
			   sp+=3; dp = sp;
			   *(dp++) = (png_byte)(255 - *(sp++));
			}
		 }
		 else
		 {
			png_bytep sp, dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			for (i = 0, sp = dp = row; i < row_width; i++)
			{
			   sp+=6; dp = sp;
			   *(dp++) = (png_byte)(255 - *(sp++));
			   *(dp++) = (png_byte)(255 - *(sp++));
			}
		 }
	  }
	  else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
	  {
		 if (row_info->bit_depth == 8)
		 {
			png_bytep sp, dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			for (i = 0, sp = dp = row; i < row_width; i++)
			{
			   *(dp++) = *(sp++);
			   *(dp++) = (png_byte)(255 - *(sp++));
			}
		 }
		 else
		 {
			png_bytep sp, dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			for (i = 0, sp = dp = row; i < row_width; i++)
			{
			   sp+=2; dp = sp;
			   *(dp++) = (png_byte)(255 - *(sp++));
			   *(dp++) = (png_byte)(255 - *(sp++));
			}
		 }
	  }
   }
}
#endif

#if defined(PNG_MNG_FEATURES_SUPPORTED)
void /* PRIVATE */
png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_write_intrapixel\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
	   row != NULL && row_info != NULL &&
#endif
	   (row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
	  int bytes_per_pixel;
	  png_uint_32 row_width = row_info->width;
	  if (row_info->bit_depth == 8)
	  {
		 png_bytep rp;
		 png_uint_32 i;

		 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
			bytes_per_pixel = 3;
		 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
			bytes_per_pixel = 4;
		 else
			return;

		 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
		 {
			*(rp)   = (png_byte)((*rp	 - *(rp+1))&0xff);
			*(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
		 }
	  }
	  else if (row_info->bit_depth == 16)
	  {
		 png_bytep rp;
		 png_uint_32 i;

		 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
			bytes_per_pixel = 6;
		 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
			bytes_per_pixel = 8;
		 else
			return;

		 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
		 {
			png_uint_32 s0   = (*(rp  ) << 8) | *(rp+1);
			png_uint_32 s1   = (*(rp+2) << 8) | *(rp+3);
			png_uint_32 s2   = (*(rp+4) << 8) | *(rp+5);
			png_uint_32 red  = (png_uint_32)((s0-s1) & 0xffffL);
			png_uint_32 blue = (png_uint_32)((s2-s1) & 0xffffL);
			*(rp  ) = (png_byte)((red >> 8) & 0xff);
			*(rp+1) = (png_byte)(red & 0xff);
			*(rp+4) = (png_byte)((blue >> 8) & 0xff);
			*(rp+5) = (png_byte)(blue & 0xff);
		 }
	  }
   }
}
#endif /* PNG_MNG_FEATURES_SUPPORTED */
#endif /* PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngwtran.c ***/


/*** Start of inlined file: pngwutil.c ***/
#define PNG_INTERNAL

#ifdef PNG_WRITE_SUPPORTED

void PNGAPI
png_save_uint_32(png_bytep buf, png_uint_32 i)
{
   buf[0] = (png_byte)((i >> 24) & 0xff);
   buf[1] = (png_byte)((i >> 16) & 0xff);
   buf[2] = (png_byte)((i >> 8) & 0xff);
   buf[3] = (png_byte)(i & 0xff);
}

void PNGAPI
png_save_int_32(png_bytep buf, png_int_32 i)
{
   buf[0] = (png_byte)((i >> 24) & 0xff);
   buf[1] = (png_byte)((i >> 16) & 0xff);
   buf[2] = (png_byte)((i >> 8) & 0xff);
   buf[3] = (png_byte)(i & 0xff);
}

void PNGAPI
png_save_uint_16(png_bytep buf, unsigned int i)
{
   buf[0] = (png_byte)((i >> 8) & 0xff);
   buf[1] = (png_byte)(i & 0xff);
}

void PNGAPI
png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
   png_bytep data, png_size_t length)
{
   if(png_ptr == NULL) return;
   png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
   png_write_chunk_data(png_ptr, data, length);
   png_write_chunk_end(png_ptr);
}

void PNGAPI
png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
   png_uint_32 length)
{
   png_byte buf[4];
   png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
   if(png_ptr == NULL) return;

   png_save_uint_32(buf, length);
   png_write_data(png_ptr, buf, (png_size_t)4);

   png_write_data(png_ptr, chunk_name, (png_size_t)4);
   png_reset_crc(png_ptr);
   png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
}

void PNGAPI
png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   if(png_ptr == NULL) return;
   if (data != NULL && length > 0)
   {
	  png_calculate_crc(png_ptr, data, length);
	  png_write_data(png_ptr, data, length);
   }
}

void PNGAPI
png_write_chunk_end(png_structp png_ptr)
{
   png_byte buf[4];

   if(png_ptr == NULL) return;

   png_save_uint_32(buf, png_ptr->crc);

   png_write_data(png_ptr, buf, (png_size_t)4);
}

void /* PRIVATE */
png_write_sig(png_structp png_ptr)
{
   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
   png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
	  (png_size_t)8 - png_ptr->sig_bytes);
   if(png_ptr->sig_bytes < 3)
	  png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
}

#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)

typedef struct
{
	char *input;   /* the uncompressed input data */
	int input_len;   /* its length */
	int num_output_ptr; /* number of output pointers used */
	int max_output_ptr; /* size of output_ptr */
	png_charpp output_ptr; /* array of pointers to output */
} compression_state;

static int /* PRIVATE */
png_text_compress(png_structp png_ptr,
		png_charp text, png_size_t text_len, int compression,
		compression_state *comp)
{
   int ret;

   comp->num_output_ptr = 0;
   comp->max_output_ptr = 0;
   comp->output_ptr = NULL;
   comp->input = NULL;
   comp->input_len = 0;

   if (compression == PNG_TEXT_COMPRESSION_NONE)
   {
	   comp->input = text;
	   comp->input_len = text_len;
	   return((int)text_len);
   }

   if (compression >= PNG_TEXT_COMPRESSION_LAST)
   {
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
	  char msg[50];
	  png_snprintf(msg, 50, "Unknown compression type %d", compression);
	  png_warning(png_ptr, msg);
#else
	  png_warning(png_ptr, "Unknown compression type");
#endif
   }

   png_ptr->zstream.avail_in = (uInt)text_len;
   png_ptr->zstream.next_in = (Bytef *)text;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
   png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;

   do
   {
	  ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
	  if (ret != Z_OK)
	  {
		 if (png_ptr->zstream.msg != NULL)
			png_error(png_ptr, png_ptr->zstream.msg);
		 else
			png_error(png_ptr, "zlib error");
	  }
	  if (!(png_ptr->zstream.avail_out))
	  {
		 if (comp->num_output_ptr >= comp->max_output_ptr)
		 {
			int old_max;

			old_max = comp->max_output_ptr;
			comp->max_output_ptr = comp->num_output_ptr + 4;
			if (comp->output_ptr != NULL)
			{
			   png_charpp old_ptr;

			   old_ptr = comp->output_ptr;
			   comp->output_ptr = (png_charpp)png_malloc(png_ptr,
				  (png_uint_32)(comp->max_output_ptr *
				  png_sizeof (png_charpp)));
			   png_memcpy(comp->output_ptr, old_ptr, old_max
				  * png_sizeof (png_charp));
			   png_free(png_ptr, old_ptr);
			}
			else
			   comp->output_ptr = (png_charpp)png_malloc(png_ptr,
				  (png_uint_32)(comp->max_output_ptr *
				  png_sizeof (png_charp)));
		 }

		 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
			(png_uint_32)png_ptr->zbuf_size);
		 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
			png_ptr->zbuf_size);
		 comp->num_output_ptr++;

		 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
		 png_ptr->zstream.next_out = png_ptr->zbuf;
	  }
   } while (png_ptr->zstream.avail_in);

   do
   {
	  ret = deflate(&png_ptr->zstream, Z_FINISH);

	  if (ret == Z_OK)
	  {
		 if (!(png_ptr->zstream.avail_out))
		 {
			if (comp->num_output_ptr >= comp->max_output_ptr)
			{
			   int old_max;

			   old_max = comp->max_output_ptr;
			   comp->max_output_ptr = comp->num_output_ptr + 4;
			   if (comp->output_ptr != NULL)
			   {
				  png_charpp old_ptr;

				  old_ptr = comp->output_ptr;
				  comp->output_ptr = (png_charpp)png_malloc(png_ptr,
					 (png_uint_32)(comp->max_output_ptr *
					 png_sizeof (png_charpp)));
				  png_memcpy(comp->output_ptr, old_ptr,
					 old_max * png_sizeof (png_charp));
				  png_free(png_ptr, old_ptr);
			   }
			   else
				  comp->output_ptr = (png_charpp)png_malloc(png_ptr,
					 (png_uint_32)(comp->max_output_ptr *
					 png_sizeof (png_charp)));
			}

			comp->output_ptr[comp->num_output_ptr] =
			   (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
			png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
			   png_ptr->zbuf_size);
			comp->num_output_ptr++;

			png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
			png_ptr->zstream.next_out = png_ptr->zbuf;
		 }
	  }
	  else if (ret != Z_STREAM_END)
	  {
		 if (png_ptr->zstream.msg != NULL)
			png_error(png_ptr, png_ptr->zstream.msg);
		 else
			png_error(png_ptr, "zlib error");
	  }
   } while (ret != Z_STREAM_END);

   text_len = png_ptr->zbuf_size * comp->num_output_ptr;
   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
	  text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;

   return((int)text_len);
}

static void /* PRIVATE */
png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
{
   int i;

   if (comp->input)
   {
	   png_write_chunk_data(png_ptr, (png_bytep)comp->input,
							(png_size_t)comp->input_len);
	   return;
   }

   for (i = 0; i < comp->num_output_ptr; i++)
   {
	  png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
		 png_ptr->zbuf_size);
	  png_free(png_ptr, comp->output_ptr[i]);
	  comp->output_ptr[i]=NULL;
   }
   if (comp->max_output_ptr != 0)
	  png_free(png_ptr, comp->output_ptr);
	  comp->output_ptr=NULL;
   if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
	  png_write_chunk_data(png_ptr, png_ptr->zbuf,
		 png_ptr->zbuf_size - png_ptr->zstream.avail_out);

   deflateReset(&png_ptr->zstream);
   png_ptr->zstream.data_type = Z_BINARY;
}
#endif

void /* PRIVATE */
png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
   int bit_depth, int color_type, int compression_type, int filter_type,
   int interlace_type)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_IHDR;
#endif
   png_byte buf[13]; /* buffer to store the IHDR info */

   png_debug(1, "in png_write_IHDR\n");
   switch (color_type)
   {
	  case PNG_COLOR_TYPE_GRAY:
		 switch (bit_depth)
		 {
			case 1:
			case 2:
			case 4:
			case 8:
			case 16: png_ptr->channels = 1; break;
			default: png_error(png_ptr,"Invalid bit depth for grayscale image");
		 }
		 break;
	  case PNG_COLOR_TYPE_RGB:
		 if (bit_depth != 8 && bit_depth != 16)
			png_error(png_ptr, "Invalid bit depth for RGB image");
		 png_ptr->channels = 3;
		 break;
	  case PNG_COLOR_TYPE_PALETTE:
		 switch (bit_depth)
		 {
			case 1:
			case 2:
			case 4:
			case 8: png_ptr->channels = 1; break;
			default: png_error(png_ptr, "Invalid bit depth for paletted image");
		 }
		 break;
	  case PNG_COLOR_TYPE_GRAY_ALPHA:
		 if (bit_depth != 8 && bit_depth != 16)
			png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
		 png_ptr->channels = 2;
		 break;
	  case PNG_COLOR_TYPE_RGB_ALPHA:
		 if (bit_depth != 8 && bit_depth != 16)
			png_error(png_ptr, "Invalid bit depth for RGBA image");
		 png_ptr->channels = 4;
		 break;
	  default:
		 png_error(png_ptr, "Invalid image color type specified");
   }

   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
   {
	  png_warning(png_ptr, "Invalid compression type specified");
	  compression_type = PNG_COMPRESSION_TYPE_BASE;
   }

   if (
#if defined(PNG_MNG_FEATURES_SUPPORTED)
	  !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
	  ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
	  (color_type == PNG_COLOR_TYPE_RGB ||
	   color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
	  (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
#endif
	  filter_type != PNG_FILTER_TYPE_BASE)
   {
	  png_warning(png_ptr, "Invalid filter type specified");
	  filter_type = PNG_FILTER_TYPE_BASE;
   }

#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   if (interlace_type != PNG_INTERLACE_NONE &&
	  interlace_type != PNG_INTERLACE_ADAM7)
   {
	  png_warning(png_ptr, "Invalid interlace type specified");
	  interlace_type = PNG_INTERLACE_ADAM7;
   }
#else
   interlace_type=PNG_INTERLACE_NONE;
#endif

   png_ptr->bit_depth = (png_byte)bit_depth;
   png_ptr->color_type = (png_byte)color_type;
   png_ptr->interlaced = (png_byte)interlace_type;
#if defined(PNG_MNG_FEATURES_SUPPORTED)
   png_ptr->filter_type = (png_byte)filter_type;
#endif
   png_ptr->compression_type = (png_byte)compression_type;
   png_ptr->width = width;
   png_ptr->height = height;

   png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
   png_ptr->usr_width = png_ptr->width;
   png_ptr->usr_bit_depth = png_ptr->bit_depth;
   png_ptr->usr_channels = png_ptr->channels;

   png_save_uint_32(buf, width);
   png_save_uint_32(buf + 4, height);
   buf[8] = (png_byte)bit_depth;
   buf[9] = (png_byte)color_type;
   buf[10] = (png_byte)compression_type;
   buf[11] = (png_byte)filter_type;
   buf[12] = (png_byte)interlace_type;

   png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);

   png_ptr->zstream.zalloc = png_zalloc;
   png_ptr->zstream.zfree = png_zfree;
   png_ptr->zstream.opaque = (voidpf)png_ptr;
   if (!(png_ptr->do_filter))
   {
	  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
		 png_ptr->bit_depth < 8)
		 png_ptr->do_filter = PNG_FILTER_NONE;
	  else
		 png_ptr->do_filter = PNG_ALL_FILTERS;
   }
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
   {
	  if (png_ptr->do_filter != PNG_FILTER_NONE)
		 png_ptr->zlib_strategy = Z_FILTERED;
	  else
		 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
   }
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
	  png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
	  png_ptr->zlib_mem_level = 8;
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
	  png_ptr->zlib_window_bits = 15;
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
	  png_ptr->zlib_method = 8;
   if (deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
	  png_ptr->zlib_method, png_ptr->zlib_window_bits,
	  png_ptr->zlib_mem_level, png_ptr->zlib_strategy) != Z_OK)
	   png_error(png_ptr, "zlib failed to initialize compressor");
   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
   png_ptr->zstream.data_type = Z_BINARY;

   png_ptr->mode = PNG_HAVE_IHDR;
}

void /* PRIVATE */
png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_PLTE;
#endif
   png_uint_32 i;
   png_colorp pal_ptr;
   png_byte buf[3];

   png_debug(1, "in png_write_PLTE\n");
   if ((
#if defined(PNG_MNG_FEATURES_SUPPORTED)
		!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
#endif
		num_pal == 0) || num_pal > 256)
   {
	 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
	 {
		png_error(png_ptr, "Invalid number of colors in palette");
	 }
	 else
	 {
		png_warning(png_ptr, "Invalid number of colors in palette");
		return;
	 }
   }

   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
   {
	  png_warning(png_ptr,
		"Ignoring request to write a PLTE chunk in grayscale PNG");
	  return;
   }

   png_ptr->num_palette = (png_uint_16)num_pal;
   png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);

   png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
#ifndef PNG_NO_POINTER_INDEXING
   for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
   {
	  buf[0] = pal_ptr->red;
	  buf[1] = pal_ptr->green;
	  buf[2] = pal_ptr->blue;
	  png_write_chunk_data(png_ptr, buf, (png_size_t)3);
   }
#else
   pal_ptr=palette;
   for (i = 0; i < num_pal; i++)
   {
	  buf[0] = pal_ptr[i].red;
	  buf[1] = pal_ptr[i].green;
	  buf[2] = pal_ptr[i].blue;
	  png_write_chunk_data(png_ptr, buf, (png_size_t)3);
   }
#endif
   png_write_chunk_end(png_ptr);
   png_ptr->mode |= PNG_HAVE_PLTE;
}

void /* PRIVATE */
png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_IDAT;
#endif
   png_debug(1, "in png_write_IDAT\n");

   if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
	   png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
   {
	  unsigned int z_cmf = data[0];  /* zlib compression method and flags */
	  if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
	  {
		 if (length >= 2 &&
			 png_ptr->height < 16384 && png_ptr->width < 16384)
		 {
			png_uint_32 uncompressed_idat_size = png_ptr->height *
			   ((png_ptr->width *
			   png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
			unsigned int z_cinfo = z_cmf >> 4;
			unsigned int half_z_window_size = 1 << (z_cinfo + 7);
			while (uncompressed_idat_size <= half_z_window_size &&
				   half_z_window_size >= 256)
			{
			   z_cinfo--;
			   half_z_window_size >>= 1;
			}
			z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
			if (data[0] != (png_byte)z_cmf)
			{
			   data[0] = (png_byte)z_cmf;
			   data[1] &= 0xe0;
			   data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
			}
		 }
	  }
	  else
		 png_error(png_ptr,
			"Invalid zlib compression method or flags in IDAT");
   }

   png_write_chunk(png_ptr, png_IDAT, data, length);
   png_ptr->mode |= PNG_HAVE_IDAT;
}

void /* PRIVATE */
png_write_IEND(png_structp png_ptr)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_IEND;
#endif
   png_debug(1, "in png_write_IEND\n");
   png_write_chunk(png_ptr, png_IEND, png_bytep_NULL,
	 (png_size_t)0);
   png_ptr->mode |= PNG_HAVE_IEND;
}

#if defined(PNG_WRITE_gAMA_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
void /* PRIVATE */
png_write_gAMA(png_structp png_ptr, double file_gamma)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_gAMA;
#endif
   png_uint_32 igamma;
   png_byte buf[4];

   png_debug(1, "in png_write_gAMA\n");
   igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
   png_save_uint_32(buf, igamma);
   png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
}
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
void /* PRIVATE */
png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_gAMA;
#endif
   png_byte buf[4];

   png_debug(1, "in png_write_gAMA\n");
   png_save_uint_32(buf, (png_uint_32)file_gamma);
   png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
}
#endif
#endif

#if defined(PNG_WRITE_sRGB_SUPPORTED)
void /* PRIVATE */
png_write_sRGB(png_structp png_ptr, int srgb_intent)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_sRGB;
#endif
   png_byte buf[1];

   png_debug(1, "in png_write_sRGB\n");
   if(srgb_intent >= PNG_sRGB_INTENT_LAST)
		 png_warning(png_ptr,
			"Invalid sRGB rendering intent specified");
   buf[0]=(png_byte)srgb_intent;
   png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
}
#endif

#if defined(PNG_WRITE_iCCP_SUPPORTED)
void /* PRIVATE */
png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
   png_charp profile, int profile_len)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_iCCP;
#endif
   png_size_t name_len;
   png_charp new_name;
   compression_state comp;
   int embedded_profile_len = 0;

   png_debug(1, "in png_write_iCCP\n");

   comp.num_output_ptr = 0;
   comp.max_output_ptr = 0;
   comp.output_ptr = NULL;
   comp.input = NULL;
   comp.input_len = 0;

   if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
	  &new_name)) == 0)
   {
	  png_warning(png_ptr, "Empty keyword in iCCP chunk");
	  return;
   }

   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
	  png_warning(png_ptr, "Unknown compression type in iCCP chunk");

   if (profile == NULL)
	  profile_len = 0;

   if (profile_len > 3)
	  embedded_profile_len =
		  ((*( (png_bytep)profile  ))<<24) |
		  ((*( (png_bytep)profile+1))<<16) |
		  ((*( (png_bytep)profile+2))<< 8) |
		  ((*( (png_bytep)profile+3))	);

   if (profile_len < embedded_profile_len)
	 {
		png_warning(png_ptr,
		  "Embedded profile length too large in iCCP chunk");
		return;
	 }

   if (profile_len > embedded_profile_len)
	 {
		png_warning(png_ptr,
		  "Truncating profile to actual length in iCCP chunk");
		profile_len = embedded_profile_len;
	 }

   if (profile_len)
	   profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
		  PNG_COMPRESSION_TYPE_BASE, &comp);

   png_write_chunk_start(png_ptr, png_iCCP,
		  (png_uint_32)name_len+profile_len+2);
   new_name[name_len+1]=0x00;
   png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);

   if (profile_len)
	  png_write_compressed_data_out(png_ptr, &comp);

   png_write_chunk_end(png_ptr);
   png_free(png_ptr, new_name);
}
#endif

#if defined(PNG_WRITE_sPLT_SUPPORTED)
void /* PRIVATE */
png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_sPLT;
#endif
   png_size_t name_len;
   png_charp new_name;
   png_byte entrybuf[10];
   int entry_size = (spalette->depth == 8 ? 6 : 10);
   int palette_size = entry_size * spalette->nentries;
   png_sPLT_entryp ep;
#ifdef PNG_NO_POINTER_INDEXING
   int i;
#endif

   png_debug(1, "in png_write_sPLT\n");
   if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
	  spalette->name, &new_name))==0)
   {
	  png_warning(png_ptr, "Empty keyword in sPLT chunk");
	  return;
   }

   png_write_chunk_start(png_ptr, png_sPLT,
		  (png_uint_32)(name_len + 2 + palette_size));
   png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
   png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);

#ifndef PNG_NO_POINTER_INDEXING
   for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
   {
	   if (spalette->depth == 8)
	   {
		   entrybuf[0] = (png_byte)ep->red;
		   entrybuf[1] = (png_byte)ep->green;
		   entrybuf[2] = (png_byte)ep->blue;
		   entrybuf[3] = (png_byte)ep->alpha;
		   png_save_uint_16(entrybuf + 4, ep->frequency);
	   }
	   else
	   {
		   png_save_uint_16(entrybuf + 0, ep->red);
		   png_save_uint_16(entrybuf + 2, ep->green);
		   png_save_uint_16(entrybuf + 4, ep->blue);
		   png_save_uint_16(entrybuf + 6, ep->alpha);
		   png_save_uint_16(entrybuf + 8, ep->frequency);
	   }
	   png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
   }
#else
   ep=spalette->entries;
   for (i=0; i>spalette->nentries; i++)
   {
	   if (spalette->depth == 8)
	   {
		   entrybuf[0] = (png_byte)ep[i].red;
		   entrybuf[1] = (png_byte)ep[i].green;
		   entrybuf[2] = (png_byte)ep[i].blue;
		   entrybuf[3] = (png_byte)ep[i].alpha;
		   png_save_uint_16(entrybuf + 4, ep[i].frequency);
	   }
	   else
	   {
		   png_save_uint_16(entrybuf + 0, ep[i].red);
		   png_save_uint_16(entrybuf + 2, ep[i].green);
		   png_save_uint_16(entrybuf + 4, ep[i].blue);
		   png_save_uint_16(entrybuf + 6, ep[i].alpha);
		   png_save_uint_16(entrybuf + 8, ep[i].frequency);
	   }
	   png_write_chunk_data(png_ptr, entrybuf, entry_size);
   }
#endif

   png_write_chunk_end(png_ptr);
   png_free(png_ptr, new_name);
}
#endif

#if defined(PNG_WRITE_sBIT_SUPPORTED)
void /* PRIVATE */
png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_sBIT;
#endif
   png_byte buf[4];
   png_size_t size;

   png_debug(1, "in png_write_sBIT\n");
   if (color_type & PNG_COLOR_MASK_COLOR)
   {
	  png_byte maxbits;

	  maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
				png_ptr->usr_bit_depth);
	  if (sbit->red == 0 || sbit->red > maxbits ||
		  sbit->green == 0 || sbit->green > maxbits ||
		  sbit->blue == 0 || sbit->blue > maxbits)
	  {
		 png_warning(png_ptr, "Invalid sBIT depth specified");
		 return;
	  }
	  buf[0] = sbit->red;
	  buf[1] = sbit->green;
	  buf[2] = sbit->blue;
	  size = 3;
   }
   else
   {
	  if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
	  {
		 png_warning(png_ptr, "Invalid sBIT depth specified");
		 return;
	  }
	  buf[0] = sbit->gray;
	  size = 1;
   }

   if (color_type & PNG_COLOR_MASK_ALPHA)
   {
	  if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
	  {
		 png_warning(png_ptr, "Invalid sBIT depth specified");
		 return;
	  }
	  buf[size++] = sbit->alpha;
   }

   png_write_chunk(png_ptr, png_sBIT, buf, size);
}
#endif

#if defined(PNG_WRITE_cHRM_SUPPORTED)
#ifdef PNG_FLOATING_POINT_SUPPORTED
void /* PRIVATE */
png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
   double red_x, double red_y, double green_x, double green_y,
   double blue_x, double blue_y)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_cHRM;
#endif
   png_byte buf[32];
   png_uint_32 itemp;

   png_debug(1, "in png_write_cHRM\n");
   if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
	   white_x + white_y > 1.0)
   {
	  png_warning(png_ptr, "Invalid cHRM white point specified");
#if !defined(PNG_NO_CONSOLE_IO)
	  fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
#endif
	  return;
   }
   itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
   png_save_uint_32(buf, itemp);
   itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
   png_save_uint_32(buf + 4, itemp);

   if (red_x < 0 ||  red_y < 0 || red_x + red_y > 1.0)
   {
	  png_warning(png_ptr, "Invalid cHRM red point specified");
	  return;
   }
   itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
   png_save_uint_32(buf + 8, itemp);
   itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
   png_save_uint_32(buf + 12, itemp);

   if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
   {
	  png_warning(png_ptr, "Invalid cHRM green point specified");
	  return;
   }
   itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
   png_save_uint_32(buf + 16, itemp);
   itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
   png_save_uint_32(buf + 20, itemp);

   if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
   {
	  png_warning(png_ptr, "Invalid cHRM blue point specified");
	  return;
   }
   itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
   png_save_uint_32(buf + 24, itemp);
   itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
   png_save_uint_32(buf + 28, itemp);

   png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
}
#endif
#ifdef PNG_FIXED_POINT_SUPPORTED
void /* PRIVATE */
png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
   png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
   png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
   png_fixed_point blue_y)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_cHRM;
#endif
   png_byte buf[32];

   png_debug(1, "in png_write_cHRM\n");
   if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
   {
	  png_warning(png_ptr, "Invalid fixed cHRM white point specified");
#if !defined(PNG_NO_CONSOLE_IO)
	  fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
#endif
	  return;
   }
   png_save_uint_32(buf, (png_uint_32)white_x);
   png_save_uint_32(buf + 4, (png_uint_32)white_y);

   if (red_x + red_y > 100000L)
   {
	  png_warning(png_ptr, "Invalid cHRM fixed red point specified");
	  return;
   }
   png_save_uint_32(buf + 8, (png_uint_32)red_x);
   png_save_uint_32(buf + 12, (png_uint_32)red_y);

   if (green_x + green_y > 100000L)
   {
	  png_warning(png_ptr, "Invalid fixed cHRM green point specified");
	  return;
   }
   png_save_uint_32(buf + 16, (png_uint_32)green_x);
   png_save_uint_32(buf + 20, (png_uint_32)green_y);

   if (blue_x + blue_y > 100000L)
   {
	  png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
	  return;
   }
   png_save_uint_32(buf + 24, (png_uint_32)blue_x);
   png_save_uint_32(buf + 28, (png_uint_32)blue_y);

   png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
}
#endif
#endif

#if defined(PNG_WRITE_tRNS_SUPPORTED)
void /* PRIVATE */
png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
   int num_trans, int color_type)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_tRNS;
#endif
   png_byte buf[6];

   png_debug(1, "in png_write_tRNS\n");
   if (color_type == PNG_COLOR_TYPE_PALETTE)
   {
	  if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
	  {
		 png_warning(png_ptr,"Invalid number of transparent colors specified");
		 return;
	  }
	  png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
   }
   else if (color_type == PNG_COLOR_TYPE_GRAY)
   {
	  if(tran->gray >= (1 << png_ptr->bit_depth))
	  {
		 png_warning(png_ptr,
		   "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
		 return;
	  }
	  png_save_uint_16(buf, tran->gray);
	  png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
   }
   else if (color_type == PNG_COLOR_TYPE_RGB)
   {
	  png_save_uint_16(buf, tran->red);
	  png_save_uint_16(buf + 2, tran->green);
	  png_save_uint_16(buf + 4, tran->blue);
	  if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
		 {
			png_warning(png_ptr,
			  "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
			return;
		 }
	  png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
   }
   else
   {
	  png_warning(png_ptr, "Can't write tRNS with an alpha channel");
   }
}
#endif

#if defined(PNG_WRITE_bKGD_SUPPORTED)
void /* PRIVATE */
png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_bKGD;
#endif
   png_byte buf[6];

   png_debug(1, "in png_write_bKGD\n");
   if (color_type == PNG_COLOR_TYPE_PALETTE)
   {
	  if (
#if defined(PNG_MNG_FEATURES_SUPPORTED)
		  (png_ptr->num_palette ||
		  (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
#endif
		 back->index > png_ptr->num_palette)
	  {
		 png_warning(png_ptr, "Invalid background palette index");
		 return;
	  }
	  buf[0] = back->index;
	  png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
   }
   else if (color_type & PNG_COLOR_MASK_COLOR)
   {
	  png_save_uint_16(buf, back->red);
	  png_save_uint_16(buf + 2, back->green);
	  png_save_uint_16(buf + 4, back->blue);
	  if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
		 {
			png_warning(png_ptr,
			  "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
			return;
		 }
	  png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
   }
   else
   {
	  if(back->gray >= (1 << png_ptr->bit_depth))
	  {
		 png_warning(png_ptr,
		   "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
		 return;
	  }
	  png_save_uint_16(buf, back->gray);
	  png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
   }
}
#endif

#if defined(PNG_WRITE_hIST_SUPPORTED)
void /* PRIVATE */
png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_hIST;
#endif
   int i;
   png_byte buf[3];

   png_debug(1, "in png_write_hIST\n");
   if (num_hist > (int)png_ptr->num_palette)
   {
	  png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
		 png_ptr->num_palette);
	  png_warning(png_ptr, "Invalid number of histogram entries specified");
	  return;
   }

   png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
   for (i = 0; i < num_hist; i++)
   {
	  png_save_uint_16(buf, hist[i]);
	  png_write_chunk_data(png_ptr, buf, (png_size_t)2);
   }
   png_write_chunk_end(png_ptr);
}
#endif

#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
	defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
png_size_t /* PRIVATE */
png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
{
   png_size_t key_len;
   png_charp kp, dp;
   int kflag;
   int kwarn=0;

   png_debug(1, "in png_check_keyword\n");
   *new_key = NULL;

   if (key == NULL || (key_len = png_strlen(key)) == 0)
   {
	  png_warning(png_ptr, "zero length keyword");
	  return ((png_size_t)0);
   }

   png_debug1(2, "Keyword to be checked is '%s'\n", key);

   *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
   if (*new_key == NULL)
   {
	  png_warning(png_ptr, "Out of memory while procesing keyword");
	  return ((png_size_t)0);
   }

   for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
   {
	  if ((png_byte)*kp < 0x20 ||
		 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
	  {
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
		 char msg[40];

		 png_snprintf(msg, 40,
		   "invalid keyword character 0x%02X", (png_byte)*kp);
		 png_warning(png_ptr, msg);
#else
		 png_warning(png_ptr, "invalid character in keyword");
#endif
		 *dp = ' ';
	  }
	  else
	  {
		 *dp = *kp;
	  }
   }
   *dp = '\0';

   kp = *new_key + key_len - 1;
   if (*kp == ' ')
   {
	  png_warning(png_ptr, "trailing spaces removed from keyword");

	  while (*kp == ' ')
	  {
		*(kp--) = '\0';
		key_len--;
	  }
   }

   kp = *new_key;
   if (*kp == ' ')
   {
	  png_warning(png_ptr, "leading spaces removed from keyword");

	  while (*kp == ' ')
	  {
		kp++;
		key_len--;
	  }
   }

   png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);

   for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
   {
	  if (*kp == ' ' && kflag == 0)
	  {
		 *(dp++) = *kp;
		 kflag = 1;
	  }
	  else if (*kp == ' ')
	  {
		 key_len--;
		 kwarn=1;
	  }
	  else
	  {
		 *(dp++) = *kp;
		 kflag = 0;
	  }
   }
   *dp = '\0';
   if(kwarn)
	  png_warning(png_ptr, "extra interior spaces removed from keyword");

   if (key_len == 0)
   {
	  png_free(png_ptr, *new_key);
	  *new_key=NULL;
	  png_warning(png_ptr, "Zero length keyword");
   }

   if (key_len > 79)
   {
	  png_warning(png_ptr, "keyword length must be 1 - 79 characters");
	  new_key[79] = '\0';
	  key_len = 79;
   }

   return (key_len);
}
#endif

#if defined(PNG_WRITE_tEXt_SUPPORTED)
void /* PRIVATE */
png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
   png_size_t text_len)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_tEXt;
#endif
   png_size_t key_len;
   png_charp new_key;

   png_debug(1, "in png_write_tEXt\n");
   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
   {
	  png_warning(png_ptr, "Empty keyword in tEXt chunk");
	  return;
   }

   if (text == NULL || *text == '\0')
	  text_len = 0;
   else
	  text_len = png_strlen(text);

   png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
   png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
   if (text_len)
	  png_write_chunk_data(png_ptr, (png_bytep)text, text_len);

   png_write_chunk_end(png_ptr);
   png_free(png_ptr, new_key);
}
#endif

#if defined(PNG_WRITE_zTXt_SUPPORTED)
void /* PRIVATE */
png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
   png_size_t text_len, int compression)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_zTXt;
#endif
   png_size_t key_len;
   char buf[1];
   png_charp new_key;
   compression_state comp;

   png_debug(1, "in png_write_zTXt\n");

   comp.num_output_ptr = 0;
   comp.max_output_ptr = 0;
   comp.output_ptr = NULL;
   comp.input = NULL;
   comp.input_len = 0;

   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
   {
	  png_warning(png_ptr, "Empty keyword in zTXt chunk");
	  return;
   }

   if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
   {
	  png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
	  png_free(png_ptr, new_key);
	  return;
   }

   text_len = png_strlen(text);

   text_len = png_text_compress(png_ptr, text, text_len, compression,
	   &comp);

   png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)
	  (key_len+text_len+2));
   png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
   png_free(png_ptr, new_key);

   buf[0] = (png_byte)compression;
   png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
   png_write_compressed_data_out(png_ptr, &comp);

   png_write_chunk_end(png_ptr);
}
#endif

#if defined(PNG_WRITE_iTXt_SUPPORTED)
void /* PRIVATE */
png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
	png_charp lang, png_charp lang_key, png_charp text)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_iTXt;
#endif
   png_size_t lang_len, key_len, lang_key_len, text_len;
   png_charp new_lang, new_key;
   png_byte cbuf[2];
   compression_state comp;

   png_debug(1, "in png_write_iTXt\n");

   comp.num_output_ptr = 0;
   comp.max_output_ptr = 0;
   comp.output_ptr = NULL;
   comp.input = NULL;

   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
   {
	  png_warning(png_ptr, "Empty keyword in iTXt chunk");
	  return;
   }
   if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
   {
	  png_warning(png_ptr, "Empty language field in iTXt chunk");
	  new_lang = NULL;
	  lang_len = 0;
   }

   if (lang_key == NULL)
	 lang_key_len = 0;
   else
	 lang_key_len = png_strlen(lang_key);

   if (text == NULL)
	  text_len = 0;
   else
	 text_len = png_strlen(text);

   text_len = png_text_compress(png_ptr, text, text_len, compression-2,
	  &comp);

   png_write_chunk_start(png_ptr, png_iTXt,
		  (png_uint_32)(
		5 /* comp byte, comp flag, terminators for key, lang and lang_key */
		+ key_len
		+ lang_len
		+ lang_key_len
		+ text_len));

   png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);

   if (compression == PNG_ITXT_COMPRESSION_NONE || \
	   compression == PNG_TEXT_COMPRESSION_NONE)
	   cbuf[0] = 0;
   else /* compression == PNG_ITXT_COMPRESSION_zTXt */
	   cbuf[0] = 1;
   cbuf[1] = 0;
   png_write_chunk_data(png_ptr, cbuf, 2);

   cbuf[0] = 0;
   png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
   png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
   png_write_compressed_data_out(png_ptr, &comp);

   png_write_chunk_end(png_ptr);
   png_free(png_ptr, new_key);
   if (new_lang)
	 png_free(png_ptr, new_lang);
}
#endif

#if defined(PNG_WRITE_oFFs_SUPPORTED)
void /* PRIVATE */
png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
   int unit_type)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_oFFs;
#endif
   png_byte buf[9];

   png_debug(1, "in png_write_oFFs\n");
   if (unit_type >= PNG_OFFSET_LAST)
	  png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");

   png_save_int_32(buf, x_offset);
   png_save_int_32(buf + 4, y_offset);
   buf[8] = (png_byte)unit_type;

   png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
}
#endif
#if defined(PNG_WRITE_pCAL_SUPPORTED)
void /* PRIVATE */
png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
   png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_pCAL;
#endif
   png_size_t purpose_len, units_len, total_len;
   png_uint_32p params_len;
   png_byte buf[10];
   png_charp new_purpose;
   int i;

   png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
   if (type >= PNG_EQUATION_LAST)
	  png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");

   purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
   png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
   units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
   png_debug1(3, "pCAL units length = %d\n", (int)units_len);
   total_len = purpose_len + units_len + 10;

   params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
	  *png_sizeof(png_uint_32)));

   for (i = 0; i < nparams; i++)
   {
	  params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
	  png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
	  total_len += (png_size_t)params_len[i];
   }

   png_debug1(3, "pCAL total length = %d\n", (int)total_len);
   png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
   png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
   png_save_int_32(buf, X0);
   png_save_int_32(buf + 4, X1);
   buf[8] = (png_byte)type;
   buf[9] = (png_byte)nparams;
   png_write_chunk_data(png_ptr, buf, (png_size_t)10);
   png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);

   png_free(png_ptr, new_purpose);

   for (i = 0; i < nparams; i++)
   {
	  png_write_chunk_data(png_ptr, (png_bytep)params[i],
		 (png_size_t)params_len[i]);
   }

   png_free(png_ptr, params_len);
   png_write_chunk_end(png_ptr);
}
#endif

#if defined(PNG_WRITE_sCAL_SUPPORTED)
#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
void /* PRIVATE */
png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_sCAL;
#endif
   char buf[64];
   png_size_t total_len;

   png_debug(1, "in png_write_sCAL\n");

   buf[0] = (char)unit;
#if defined(_WIN32_WCE)
   {
	  wchar_t wc_buf[32];
	  size_t wc_len;
	  swprintf(wc_buf, TEXT("%12.12e"), width);
	  wc_len = wcslen(wc_buf);
	  WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
	  total_len = wc_len + 2;
	  swprintf(wc_buf, TEXT("%12.12e"), height);
	  wc_len = wcslen(wc_buf);
	  WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
		 NULL, NULL);
	  total_len += wc_len;
   }
#else
   png_snprintf(buf + 1, 63, "%12.12e", width);
   total_len = 1 + png_strlen(buf + 1) + 1;
   png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
   total_len += png_strlen(buf + total_len);
#endif

   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
   png_write_chunk(png_ptr, png_sCAL, (png_bytep)buf, total_len);
}
#else
#ifdef PNG_FIXED_POINT_SUPPORTED
void /* PRIVATE */
png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
   png_charp height)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_sCAL;
#endif
   png_byte buf[64];
   png_size_t wlen, hlen, total_len;

   png_debug(1, "in png_write_sCAL_s\n");

   wlen = png_strlen(width);
   hlen = png_strlen(height);
   total_len = wlen + hlen + 2;
   if (total_len > 64)
   {
	  png_warning(png_ptr, "Can't write sCAL (buffer too small)");
	  return;
   }

   buf[0] = (png_byte)unit;
   png_memcpy(buf + 1, width, wlen + 1);	  /* append the '\0' here */
   png_memcpy(buf + wlen + 2, height, hlen);  /* do NOT append the '\0' here */

   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
   png_write_chunk(png_ptr, png_sCAL, buf, total_len);
}
#endif
#endif
#endif

#if defined(PNG_WRITE_pHYs_SUPPORTED)
void /* PRIVATE */
png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
   png_uint_32 y_pixels_per_unit,
   int unit_type)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_pHYs;
#endif
   png_byte buf[9];

   png_debug(1, "in png_write_pHYs\n");
   if (unit_type >= PNG_RESOLUTION_LAST)
	  png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");

   png_save_uint_32(buf, x_pixels_per_unit);
   png_save_uint_32(buf + 4, y_pixels_per_unit);
   buf[8] = (png_byte)unit_type;

   png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
}
#endif

#if defined(PNG_WRITE_tIME_SUPPORTED)
void /* PRIVATE */
png_write_tIME(png_structp png_ptr, png_timep mod_time)
{
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_tIME;
#endif
   png_byte buf[7];

   png_debug(1, "in png_write_tIME\n");
   if (mod_time->month  > 12 || mod_time->month  < 1 ||
	   mod_time->day	> 31 || mod_time->day	< 1 ||
	   mod_time->hour   > 23 || mod_time->second > 60)
   {
	  png_warning(png_ptr, "Invalid time specified for tIME chunk");
	  return;
   }

   png_save_uint_16(buf, mod_time->year);
   buf[2] = mod_time->month;
   buf[3] = mod_time->day;
   buf[4] = mod_time->hour;
   buf[5] = mod_time->minute;
   buf[6] = mod_time->second;

   png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
}
#endif

void /* PRIVATE */
png_write_start_row(png_structp png_ptr)
{
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
#ifdef PNG_USE_LOCAL_ARRAYS

   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};

   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
#endif
#endif

   png_size_t buf_size;

   png_debug(1, "in png_write_start_row\n");
   buf_size = (png_size_t)(PNG_ROWBYTES(
	  png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);

   png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
   png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;

#ifndef PNG_NO_WRITE_FILTERING
   if (png_ptr->do_filter & PNG_FILTER_SUB)
   {
	  png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
		 (png_ptr->rowbytes + 1));
	  png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
   }

   if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
   {
	  png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
	  png_memset(png_ptr->prev_row, 0, buf_size);

	  if (png_ptr->do_filter & PNG_FILTER_UP)
	  {
		 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
			(png_ptr->rowbytes + 1));
		 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
	  }

	  if (png_ptr->do_filter & PNG_FILTER_AVG)
	  {
		 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
			(png_ptr->rowbytes + 1));
		 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
	  }

	  if (png_ptr->do_filter & PNG_FILTER_PAETH)
	  {
		 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
			(png_ptr->rowbytes + 1));
		 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
	  }
#endif /* PNG_NO_WRITE_FILTERING */
   }

#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   if (png_ptr->interlaced)
   {
	  if (!(png_ptr->transformations & PNG_INTERLACE))
	  {
		 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
			png_pass_ystart[0]) / png_pass_yinc[0];
		 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
			png_pass_start[0]) / png_pass_inc[0];
	  }
	  else
	  {
		 png_ptr->num_rows = png_ptr->height;
		 png_ptr->usr_width = png_ptr->width;
	  }
   }
   else
#endif
   {
	  png_ptr->num_rows = png_ptr->height;
	  png_ptr->usr_width = png_ptr->width;
   }
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
   png_ptr->zstream.next_out = png_ptr->zbuf;
}

void /* PRIVATE */
png_write_finish_row(png_structp png_ptr)
{
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
#ifdef PNG_USE_LOCAL_ARRAYS

   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};

   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};

   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
#endif
#endif

   int ret;

   png_debug(1, "in png_write_finish_row\n");
   png_ptr->row_number++;

   if (png_ptr->row_number < png_ptr->num_rows)
	  return;

#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   if (png_ptr->interlaced)
   {
	  png_ptr->row_number = 0;
	  if (png_ptr->transformations & PNG_INTERLACE)
	  {
		 png_ptr->pass++;
	  }
	  else
	  {
		 do
		 {
			png_ptr->pass++;
			if (png_ptr->pass >= 7)
			   break;
			png_ptr->usr_width = (png_ptr->width +
			   png_pass_inc[png_ptr->pass] - 1 -
			   png_pass_start[png_ptr->pass]) /
			   png_pass_inc[png_ptr->pass];
			png_ptr->num_rows = (png_ptr->height +
			   png_pass_yinc[png_ptr->pass] - 1 -
			   png_pass_ystart[png_ptr->pass]) /
			   png_pass_yinc[png_ptr->pass];
			if (png_ptr->transformations & PNG_INTERLACE)
			   break;
		 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);

	  }

	  if (png_ptr->pass < 7)
	  {
		 if (png_ptr->prev_row != NULL)
			png_memset(png_ptr->prev_row, 0,
			   (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
			   png_ptr->usr_bit_depth,png_ptr->width))+1);
		 return;
	  }
   }
#endif

   do
   {
	  ret = deflate(&png_ptr->zstream, Z_FINISH);
	  if (ret == Z_OK)
	  {
		 if (!(png_ptr->zstream.avail_out))
		 {
			png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
			png_ptr->zstream.next_out = png_ptr->zbuf;
			png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
		 }
	  }
	  else if (ret != Z_STREAM_END)
	  {
		 if (png_ptr->zstream.msg != NULL)
			png_error(png_ptr, png_ptr->zstream.msg);
		 else
			png_error(png_ptr, "zlib error");
	  }
   } while (ret != Z_STREAM_END);

   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
   {
	  png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
		 png_ptr->zstream.avail_out);
   }

   deflateReset(&png_ptr->zstream);
   png_ptr->zstream.data_type = Z_BINARY;
}

#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
void /* PRIVATE */
png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
{
#ifdef PNG_USE_LOCAL_ARRAYS

   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};

   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
#endif

   png_debug(1, "in png_do_write_interlace\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL && pass < 6)
#else
   if (pass < 6)
#endif
   {
	  switch (row_info->pixel_depth)
	  {
		 case 1:
		 {
			png_bytep sp;
			png_bytep dp;
			int shift;
			int d;
			int value;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			dp = row;
			d = 0;
			shift = 7;
			for (i = png_pass_start[pass]; i < row_width;
			   i += png_pass_inc[pass])
			{
			   sp = row + (png_size_t)(i >> 3);
			   value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
			   d |= (value << shift);

			   if (shift == 0)
			   {
				  shift = 7;
				  *dp++ = (png_byte)d;
				  d = 0;
			   }
			   else
				  shift--;

			}
			if (shift != 7)
			   *dp = (png_byte)d;
			break;
		 }
		 case 2:
		 {
			png_bytep sp;
			png_bytep dp;
			int shift;
			int d;
			int value;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			dp = row;
			shift = 6;
			d = 0;
			for (i = png_pass_start[pass]; i < row_width;
			   i += png_pass_inc[pass])
			{
			   sp = row + (png_size_t)(i >> 2);
			   value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
			   d |= (value << shift);

			   if (shift == 0)
			   {
				  shift = 6;
				  *dp++ = (png_byte)d;
				  d = 0;
			   }
			   else
				  shift -= 2;
			}
			if (shift != 6)
				   *dp = (png_byte)d;
			break;
		 }
		 case 4:
		 {
			png_bytep sp;
			png_bytep dp;
			int shift;
			int d;
			int value;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;

			dp = row;
			shift = 4;
			d = 0;
			for (i = png_pass_start[pass]; i < row_width;
			   i += png_pass_inc[pass])
			{
			   sp = row + (png_size_t)(i >> 1);
			   value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
			   d |= (value << shift);

			   if (shift == 0)
			   {
				  shift = 4;
				  *dp++ = (png_byte)d;
				  d = 0;
			   }
			   else
				  shift -= 4;
			}
			if (shift != 4)
			   *dp = (png_byte)d;
			break;
		 }
		 default:
		 {
			png_bytep sp;
			png_bytep dp;
			png_uint_32 i;
			png_uint_32 row_width = row_info->width;
			png_size_t pixel_bytes;

			dp = row;
			pixel_bytes = (row_info->pixel_depth >> 3);
			for (i = png_pass_start[pass]; i < row_width;
			   i += png_pass_inc[pass])
			{
			   sp = row + (png_size_t)i * pixel_bytes;
			   if (dp != sp)
				  png_memcpy(dp, sp, pixel_bytes);
			   dp += pixel_bytes;
			}
			break;
		 }
	  }
	  row_info->width = (row_info->width +
		 png_pass_inc[pass] - 1 -
		 png_pass_start[pass]) /
		 png_pass_inc[pass];
		 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
			row_info->width);
   }
}
#endif

#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
#define PNG_HISHIFT 10
#define PNG_LOMASK ((png_uint_32)0xffffL)
#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
void /* PRIVATE */
png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
{
   png_bytep best_row;
#ifndef PNG_NO_WRITE_FILTER
   png_bytep prev_row, row_buf;
   png_uint_32 mins, bpp;
   png_byte filter_to_do = png_ptr->do_filter;
   png_uint_32 row_bytes = row_info->rowbytes;
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
   int num_p_filters = (int)png_ptr->num_prev_filters;
#endif

   png_debug(1, "in png_write_find_filter\n");
   bpp = (row_info->pixel_depth + 7) >> 3;

   prev_row = png_ptr->prev_row;
#endif
   best_row = png_ptr->row_buf;
#ifndef PNG_NO_WRITE_FILTER
   row_buf = best_row;
   mins = PNG_MAXSUM;

   if ((filter_to_do & PNG_FILTER_NONE) &&
	   filter_to_do != PNG_FILTER_NONE)
   {
	  png_bytep rp;
	  png_uint_32 sum = 0;
	  png_uint_32 i;
	  int v;

	  for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
	  {
		 v = *rp;
		 sum += (v < 128) ? v : 256 - v;
	  }

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 png_uint_32 sumhi, sumlo;
		 int j;
		 sumlo = sum & PNG_LOMASK;
		 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
			{
			   sumlo = (sumlo * png_ptr->filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   sumhi = (sumhi * png_ptr->filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
			PNG_COST_SHIFT;
		 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
			PNG_COST_SHIFT;

		 if (sumhi > PNG_HIMASK)
			sum = PNG_MAXSUM;
		 else
			sum = (sumhi << PNG_HISHIFT) + sumlo;
	  }
#endif
	  mins = sum;
   }

   if (filter_to_do == PNG_FILTER_SUB)
   {
	  png_bytep rp, lp, dp;
	  png_uint_32 i;
	  for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
		   i++, rp++, dp++)
	  {
		 *dp = *rp;
	  }
	  for (lp = row_buf + 1; i < row_bytes;
		 i++, rp++, lp++, dp++)
	  {
		 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
	  }
	  best_row = png_ptr->sub_row;
   }

   else if (filter_to_do & PNG_FILTER_SUB)
   {
	  png_bytep rp, dp, lp;
	  png_uint_32 sum = 0, lmins = mins;
	  png_uint_32 i;
	  int v;

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 int j;
		 png_uint_32 lmhi, lmlo;
		 lmlo = lmins & PNG_LOMASK;
		 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
			{
			   lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
			PNG_COST_SHIFT;
		 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
			PNG_COST_SHIFT;

		 if (lmhi > PNG_HIMASK)
			lmins = PNG_MAXSUM;
		 else
			lmins = (lmhi << PNG_HISHIFT) + lmlo;
	  }
#endif

	  for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
		   i++, rp++, dp++)
	  {
		 v = *dp = *rp;

		 sum += (v < 128) ? v : 256 - v;
	  }
	  for (lp = row_buf + 1; i < row_bytes;
		 i++, rp++, lp++, dp++)
	  {
		 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);

		 sum += (v < 128) ? v : 256 - v;

		 if (sum > lmins)  /* We are already worse, don't continue. */
			break;
	  }

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 int j;
		 png_uint_32 sumhi, sumlo;
		 sumlo = sum & PNG_LOMASK;
		 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
			{
			   sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
			PNG_COST_SHIFT;
		 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
			PNG_COST_SHIFT;

		 if (sumhi > PNG_HIMASK)
			sum = PNG_MAXSUM;
		 else
			sum = (sumhi << PNG_HISHIFT) + sumlo;
	  }
#endif

	  if (sum < mins)
	  {
		 mins = sum;
		 best_row = png_ptr->sub_row;
	  }
   }

   if (filter_to_do == PNG_FILTER_UP)
   {
	  png_bytep rp, dp, pp;
	  png_uint_32 i;

	  for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
		   pp = prev_row + 1; i < row_bytes;
		   i++, rp++, pp++, dp++)
	  {
		 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
	  }
	  best_row = png_ptr->up_row;
   }

   else if (filter_to_do & PNG_FILTER_UP)
   {
	  png_bytep rp, dp, pp;
	  png_uint_32 sum = 0, lmins = mins;
	  png_uint_32 i;
	  int v;

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 int j;
		 png_uint_32 lmhi, lmlo;
		 lmlo = lmins & PNG_LOMASK;
		 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
			{
			   lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
			PNG_COST_SHIFT;
		 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
			PNG_COST_SHIFT;

		 if (lmhi > PNG_HIMASK)
			lmins = PNG_MAXSUM;
		 else
			lmins = (lmhi << PNG_HISHIFT) + lmlo;
	  }
#endif

	  for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
		   pp = prev_row + 1; i < row_bytes; i++)
	  {
		 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);

		 sum += (v < 128) ? v : 256 - v;

		 if (sum > lmins)  /* We are already worse, don't continue. */
			break;
	  }

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 int j;
		 png_uint_32 sumhi, sumlo;
		 sumlo = sum & PNG_LOMASK;
		 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
			{
			   sumlo = (sumlo * png_ptr->filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   sumhi = (sumhi * png_ptr->filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
			PNG_COST_SHIFT;
		 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
			PNG_COST_SHIFT;

		 if (sumhi > PNG_HIMASK)
			sum = PNG_MAXSUM;
		 else
			sum = (sumhi << PNG_HISHIFT) + sumlo;
	  }
#endif

	  if (sum < mins)
	  {
		 mins = sum;
		 best_row = png_ptr->up_row;
	  }
   }

   if (filter_to_do == PNG_FILTER_AVG)
   {
	  png_bytep rp, dp, pp, lp;
	  png_uint_32 i;
	  for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
		   pp = prev_row + 1; i < bpp; i++)
	  {
		 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
	  }
	  for (lp = row_buf + 1; i < row_bytes; i++)
	  {
		 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
				 & 0xff);
	  }
	  best_row = png_ptr->avg_row;
   }

   else if (filter_to_do & PNG_FILTER_AVG)
   {
	  png_bytep rp, dp, pp, lp;
	  png_uint_32 sum = 0, lmins = mins;
	  png_uint_32 i;
	  int v;

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 int j;
		 png_uint_32 lmhi, lmlo;
		 lmlo = lmins & PNG_LOMASK;
		 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
			{
			   lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
			PNG_COST_SHIFT;
		 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
			PNG_COST_SHIFT;

		 if (lmhi > PNG_HIMASK)
			lmins = PNG_MAXSUM;
		 else
			lmins = (lmhi << PNG_HISHIFT) + lmlo;
	  }
#endif

	  for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
		   pp = prev_row + 1; i < bpp; i++)
	  {
		 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);

		 sum += (v < 128) ? v : 256 - v;
	  }
	  for (lp = row_buf + 1; i < row_bytes; i++)
	  {
		 v = *dp++ =
		  (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);

		 sum += (v < 128) ? v : 256 - v;

		 if (sum > lmins)  /* We are already worse, don't continue. */
			break;
	  }

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 int j;
		 png_uint_32 sumhi, sumlo;
		 sumlo = sum & PNG_LOMASK;
		 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
			{
			   sumlo = (sumlo * png_ptr->filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   sumhi = (sumhi * png_ptr->filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
			PNG_COST_SHIFT;
		 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
			PNG_COST_SHIFT;

		 if (sumhi > PNG_HIMASK)
			sum = PNG_MAXSUM;
		 else
			sum = (sumhi << PNG_HISHIFT) + sumlo;
	  }
#endif

	  if (sum < mins)
	  {
		 mins = sum;
		 best_row = png_ptr->avg_row;
	  }
   }

   if (filter_to_do == PNG_FILTER_PAETH)
   {
	  png_bytep rp, dp, pp, cp, lp;
	  png_uint_32 i;
	  for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
		   pp = prev_row + 1; i < bpp; i++)
	  {
		 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
	  }

	  for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
	  {
		 int a, b, c, pa, pb, pc, p;

		 b = *pp++;
		 c = *cp++;
		 a = *lp++;

		 p = b - c;
		 pc = a - c;

#ifdef PNG_USE_ABS
		 pa = abs(p);
		 pb = abs(pc);
		 pc = abs(p + pc);
#else
		 pa = p < 0 ? -p : p;
		 pb = pc < 0 ? -pc : pc;
		 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif

		 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;

		 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
	  }
	  best_row = png_ptr->paeth_row;
   }

   else if (filter_to_do & PNG_FILTER_PAETH)
   {
	  png_bytep rp, dp, pp, cp, lp;
	  png_uint_32 sum = 0, lmins = mins;
	  png_uint_32 i;
	  int v;

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 int j;
		 png_uint_32 lmhi, lmlo;
		 lmlo = lmins & PNG_LOMASK;
		 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
			{
			   lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
			PNG_COST_SHIFT;
		 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
			PNG_COST_SHIFT;

		 if (lmhi > PNG_HIMASK)
			lmins = PNG_MAXSUM;
		 else
			lmins = (lmhi << PNG_HISHIFT) + lmlo;
	  }
#endif

	  for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
		   pp = prev_row + 1; i < bpp; i++)
	  {
		 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);

		 sum += (v < 128) ? v : 256 - v;
	  }

	  for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
	  {
		 int a, b, c, pa, pb, pc, p;

		 b = *pp++;
		 c = *cp++;
		 a = *lp++;

#ifndef PNG_SLOW_PAETH
		 p = b - c;
		 pc = a - c;
#ifdef PNG_USE_ABS
		 pa = abs(p);
		 pb = abs(pc);
		 pc = abs(p + pc);
#else
		 pa = p < 0 ? -p : p;
		 pb = pc < 0 ? -pc : pc;
		 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
#endif
		 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
#else /* PNG_SLOW_PAETH */
		 p = a + b - c;
		 pa = abs(p - a);
		 pb = abs(p - b);
		 pc = abs(p - c);
		 if (pa <= pb && pa <= pc)
			p = a;
		 else if (pb <= pc)
			p = b;
		 else
			p = c;
#endif /* PNG_SLOW_PAETH */

		 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);

		 sum += (v < 128) ? v : 256 - v;

		 if (sum > lmins)  /* We are already worse, don't continue. */
			break;
	  }

#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
	  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
	  {
		 int j;
		 png_uint_32 sumhi, sumlo;
		 sumlo = sum & PNG_LOMASK;
		 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;

		 for (j = 0; j < num_p_filters; j++)
		 {
			if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
			{
			   sumlo = (sumlo * png_ptr->filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			   sumhi = (sumhi * png_ptr->filter_weights[j]) >>
				  PNG_WEIGHT_SHIFT;
			}
		 }

		 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
			PNG_COST_SHIFT;
		 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
			PNG_COST_SHIFT;

		 if (sumhi > PNG_HIMASK)
			sum = PNG_MAXSUM;
		 else
			sum = (sumhi << PNG_HISHIFT) + sumlo;
	  }
#endif

	  if (sum < mins)
	  {
		 best_row = png_ptr->paeth_row;
	  }
   }
#endif /* PNG_NO_WRITE_FILTER */

   png_write_filtered_row(png_ptr, best_row);

#ifndef PNG_NO_WRITE_FILTER
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
   if (png_ptr->num_prev_filters > 0)
   {
	  int j;
	  for (j = 1; j < num_p_filters; j++)
	  {
		 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
	  }
	  png_ptr->prev_filters[j] = best_row[0];
   }
#endif
#endif /* PNG_NO_WRITE_FILTER */
}

void /* PRIVATE */
png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
{
   png_debug(1, "in png_write_filtered_row\n");
   png_debug1(2, "filter = %d\n", filtered_row[0]);

   png_ptr->zstream.next_in = filtered_row;
   png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
   do
   {
	  int ret; /* return of zlib */

	  ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
	  if (ret != Z_OK)
	  {
		 if (png_ptr->zstream.msg != NULL)
			png_error(png_ptr, png_ptr->zstream.msg);
		 else
			png_error(png_ptr, "zlib error");
	  }

	  if (!(png_ptr->zstream.avail_out))
	  {
		 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
		 png_ptr->zstream.next_out = png_ptr->zbuf;
		 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
	  }
   } while (png_ptr->zstream.avail_in);

   if (png_ptr->prev_row != NULL)
   {
	  png_bytep tptr;

	  tptr = png_ptr->prev_row;
	  png_ptr->prev_row = png_ptr->row_buf;
	  png_ptr->row_buf = tptr;
   }

   png_write_finish_row(png_ptr);

#if defined(PNG_WRITE_FLUSH_SUPPORTED)
   png_ptr->flush_rows++;

   if (png_ptr->flush_dist > 0 &&
	   png_ptr->flush_rows >= png_ptr->flush_dist)
   {
	  png_write_flush(png_ptr);
   }
#endif
}
#endif /* PNG_WRITE_SUPPORTED */
/*** End of inlined file: pngwutil.c ***/

  }
#else
  #define PNG_INTERNAL
  #define PNG_SETJMP_NOT_SUPPORTED

  #include <png.h>
  #include <pngconf.h>
#endif
}

#undef max
#undef min

#ifdef _MSC_VER
  #pragma warning (pop)
#endif

BEGIN_JUCE_NAMESPACE

using ::calloc;
using ::malloc;
using ::free;

namespace PNGHelpers
{
	using namespace pnglibNamespace;

	static void readCallback (png_structp png, png_bytep data, png_size_t length)
	{
		static_cast<InputStream*> (png_get_io_ptr (png))->read (data, (int) length);
	}

	static void writeDataCallback (png_structp png, png_bytep data, png_size_t length)
	{
		static_cast<OutputStream*> (png_get_io_ptr (png))->write (data, (int) length);
	}

	struct PNGErrorStruct {};

	static void errorCallback (png_structp, png_const_charp)
	{
		throw PNGErrorStruct();
	}
}

Image* juce_loadPNGImageFromStream (InputStream& in)
{
	using namespace pnglibNamespace;
	Image* image = 0;

	png_structp pngReadStruct;
	png_infop pngInfoStruct;

	pngReadStruct = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);

	if (pngReadStruct != 0)
	{
		pngInfoStruct = png_create_info_struct (pngReadStruct);

		if (pngInfoStruct == 0)
		{
			png_destroy_read_struct (&pngReadStruct, 0, 0);
			return 0;
		}

		png_set_error_fn (pngReadStruct, 0, PNGHelpers::errorCallback, PNGHelpers::errorCallback );

		// read the header..
		png_set_read_fn (pngReadStruct, &in, PNGHelpers::readCallback);

		png_uint_32 width, height;
		int bitDepth, colorType, interlaceType;

		png_read_info (pngReadStruct, pngInfoStruct);

		png_get_IHDR (pngReadStruct, pngInfoStruct,
					  &width, &height,
					  &bitDepth, &colorType,
					  &interlaceType, 0, 0);

		if (bitDepth == 16)
			png_set_strip_16 (pngReadStruct);

		if (colorType == PNG_COLOR_TYPE_PALETTE)
			png_set_expand (pngReadStruct);

		if (bitDepth < 8)
			png_set_expand (pngReadStruct);

		if (png_get_valid (pngReadStruct, pngInfoStruct, PNG_INFO_tRNS))
			png_set_expand (pngReadStruct);

		if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
			png_set_gray_to_rgb (pngReadStruct);

		png_set_add_alpha (pngReadStruct, 0xff, PNG_FILLER_AFTER);

		bool hasAlphaChan = (colorType & PNG_COLOR_MASK_ALPHA) != 0
							  || pngInfoStruct->num_trans > 0;

		// Load the image into a temp buffer in the pnglib format..
		HeapBlock <uint8> tempBuffer (height * (width << 2));

		{
			HeapBlock <png_bytep> rows (height);
			for (int y = (int) height; --y >= 0;)
				rows[y] = (png_bytep) (tempBuffer + (width << 2) * y);

			png_read_image (pngReadStruct, rows);
			png_read_end (pngReadStruct, pngInfoStruct);
		}

		png_destroy_read_struct (&pngReadStruct, &pngInfoStruct, 0);

		// now convert the data to a juce image format..
		image = Image::createNativeImage (hasAlphaChan ? Image::ARGB : Image::RGB,
										  (int) width, (int) height, hasAlphaChan);

		hasAlphaChan = image->hasAlphaChannel(); // (the native image creator may not give back what we expect)

		const Image::BitmapData destData (*image, 0, 0, (int) width, (int) height, true);
		uint8* srcRow = tempBuffer;
		uint8* destRow = destData.data;

		for (int y = 0; y < (int) height; ++y)
		{
			const uint8* src = srcRow;
			srcRow += (width << 2);
			uint8* dest = destRow;
			destRow += destData.lineStride;

			if (hasAlphaChan)
			{
				for (int i = (int) width; --i >= 0;)
				{
					((PixelARGB*) dest)->setARGB (src[3], src[0], src[1], src[2]);
					((PixelARGB*) dest)->premultiply();
					dest += destData.pixelStride;
					src += 4;
				}
			}
			else
			{
				for (int i = (int) width; --i >= 0;)
				{
					((PixelRGB*) dest)->setARGB (0, src[0], src[1], src[2]);
					dest += destData.pixelStride;
					src += 4;
				}
			}
		}
	}

	return image;
}

bool juce_writePNGImageToStream (const Image& image, OutputStream& out)
{
	using namespace pnglibNamespace;
	const int width = image.getWidth();
	const int height = image.getHeight();

	png_structp pngWriteStruct = png_create_write_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);

	if (pngWriteStruct == 0)
		return false;

	png_infop pngInfoStruct = png_create_info_struct (pngWriteStruct);

	if (pngInfoStruct == 0)
	{
		png_destroy_write_struct (&pngWriteStruct, (png_infopp) 0);
		return false;
	}

	png_set_write_fn (pngWriteStruct, &out, PNGHelpers::writeDataCallback, 0);

	png_set_IHDR (pngWriteStruct, pngInfoStruct, width, height, 8,
				  image.hasAlphaChannel() ? PNG_COLOR_TYPE_RGB_ALPHA
										  : PNG_COLOR_TYPE_RGB,
				  PNG_INTERLACE_NONE,
				  PNG_COMPRESSION_TYPE_BASE,
				  PNG_FILTER_TYPE_BASE);

	HeapBlock <uint8> rowData (width * 4);

	png_color_8 sig_bit;
	sig_bit.red = 8;
	sig_bit.green = 8;
	sig_bit.blue = 8;
	sig_bit.alpha = 8;
	png_set_sBIT (pngWriteStruct, pngInfoStruct, &sig_bit);

	png_write_info (pngWriteStruct, pngInfoStruct);

	png_set_shift (pngWriteStruct, &sig_bit);
	png_set_packing (pngWriteStruct);

	const Image::BitmapData srcData (image, 0, 0, width, height);

	for (int y = 0; y < height; ++y)
	{
		uint8* dst = rowData;
		const uint8* src = srcData.getLinePointer (y);

		if (image.hasAlphaChannel())
		{
			for (int i = width; --i >= 0;)
			{
				PixelARGB p (*(const PixelARGB*) src);
				p.unpremultiply();

				*dst++ = p.getRed();
				*dst++ = p.getGreen();
				*dst++ = p.getBlue();
				*dst++ = p.getAlpha();
				src += srcData.pixelStride;
			}
		}
		else
		{
			for (int i = width; --i >= 0;)
			{
				*dst++ = ((const PixelRGB*) src)->getRed();
				*dst++ = ((const PixelRGB*) src)->getGreen();
				*dst++ = ((const PixelRGB*) src)->getBlue();
				src += srcData.pixelStride;
			}
		}

		png_write_rows (pngWriteStruct, &rowData, 1);
	}

	png_write_end (pngWriteStruct, pngInfoStruct);
	png_destroy_write_struct (&pngWriteStruct, &pngInfoStruct);

	out.flush();

	return true;
}

END_JUCE_NAMESPACE
/*** End of inlined file: juce_PNGLoader.cpp ***/

#endif

//==============================================================================
#if JUCE_BUILD_NATIVE
 #if JUCE_WINDOWS

/*** Start of inlined file: juce_win32_NativeCode.cpp ***/
#if JUCE_WINDOWS

BEGIN_JUCE_NAMESPACE

#define JUCE_INCLUDED_FILE 1

// Now include the actual code files..

/*** Start of inlined file: juce_win32_DynamicLibraryLoader.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE


/*** Start of inlined file: juce_win32_DynamicLibraryLoader.h ***/
#ifndef __JUCE_WIN32_DYNAMICLIBRARYLOADER_JUCEHEADER__
#define __JUCE_WIN32_DYNAMICLIBRARYLOADER_JUCEHEADER__

#ifndef DOXYGEN

// use with DynamicLibraryLoader to simplify importing functions
//
// functionName: function to import
// localFunctionName: name you want to use to actually call it (must be different)
// returnType: the return type
// object: the DynamicLibraryLoader to use
// params: list of params (bracketed)
//
#define DynamicLibraryImport(functionName, localFunctionName, returnType, object, params) \
	typedef returnType (WINAPI *type##localFunctionName) params; \
	type##localFunctionName localFunctionName  \
		= (type##localFunctionName)object.findProcAddress (#functionName);

// loads and unloads a DLL automatically
class JUCE_API  DynamicLibraryLoader
{
public:
	DynamicLibraryLoader (const String& name);
	~DynamicLibraryLoader();

	void* findProcAddress (const String& functionName);

private:
	void* libHandle;
};

#endif
#endif   // __JUCE_WIN32_DYNAMICLIBRARYLOADER_JUCEHEADER__
/*** End of inlined file: juce_win32_DynamicLibraryLoader.h ***/

DynamicLibraryLoader::DynamicLibraryLoader (const String& name)
{
	libHandle = LoadLibrary (name);
}

DynamicLibraryLoader::~DynamicLibraryLoader()
{
	FreeLibrary ((HMODULE) libHandle);
}

void* DynamicLibraryLoader::findProcAddress (const String& functionName)
{
	return GetProcAddress ((HMODULE) libHandle, functionName.toCString());
}

#endif
/*** End of inlined file: juce_win32_DynamicLibraryLoader.cpp ***/



/*** Start of inlined file: juce_win32_SystemStats.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

extern void juce_initialiseThreadEvents();

void Logger::outputDebugString (const String& text) throw()
{
	OutputDebugString (text + "\n");
}

static int64 hiResTicksPerSecond;
static double hiResTicksScaleFactor;

#if JUCE_USE_INTRINSICS

// CPU info functions using intrinsics...

#pragma intrinsic (__cpuid)
#pragma intrinsic (__rdtsc)

const String SystemStats::getCpuVendor() throw()
{
	int info [4];
	__cpuid (info, 0);

	char v [12];
	memcpy (v, info + 1, 4);
	memcpy (v + 4, info + 3, 4);
	memcpy (v + 8, info + 2, 4);

	return String (v, 12);
}

#else

// CPU info functions using old fashioned inline asm...

static void juce_getCpuVendor (char* const v)
{
	int vendor[4];
	zeromem (vendor, 16);

#ifdef JUCE_64BIT
#else
  #ifndef __MINGW32__
	__try
  #endif
	{
  #if JUCE_GCC
		unsigned int dummy = 0;
		__asm__ ("cpuid" : "=a" (dummy), "=b" (vendor[0]), "=c" (vendor[2]),"=d" (vendor[1]) : "a" (0));
  #else
		__asm
		{
			mov eax, 0
			cpuid
			mov [vendor], ebx
			mov [vendor + 4], edx
			mov [vendor + 8], ecx
		}
  #endif
	}
  #ifndef __MINGW32__
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		*v = 0;
	}
  #endif
#endif

	memcpy (v, vendor, 16);
}

const String SystemStats::getCpuVendor() throw()
{
	char v [16];
	juce_getCpuVendor (v);
	return String (v, 16);
}
#endif

struct CPUFlags
{
	bool hasMMX : 1;
	bool hasSSE : 1;
	bool hasSSE2 : 1;
	bool has3DNow : 1;
};

static CPUFlags cpuFlags;

bool SystemStats::hasMMX() throw()
{
	return cpuFlags.hasMMX;
}

bool SystemStats::hasSSE() throw()
{
	return cpuFlags.hasSSE;
}

bool SystemStats::hasSSE2() throw()
{
	return cpuFlags.hasSSE2;
}

bool SystemStats::has3DNow() throw()
{
	return cpuFlags.has3DNow;
}

void SystemStats::initialiseStats() throw()
{
	juce_initialiseThreadEvents();

	cpuFlags.hasMMX   = IsProcessorFeaturePresent (PF_MMX_INSTRUCTIONS_AVAILABLE) != 0;
	cpuFlags.hasSSE   = IsProcessorFeaturePresent (PF_XMMI_INSTRUCTIONS_AVAILABLE) != 0;
	cpuFlags.hasSSE2  = IsProcessorFeaturePresent (PF_XMMI64_INSTRUCTIONS_AVAILABLE) != 0;
#ifdef PF_AMD3D_INSTRUCTIONS_AVAILABLE
	cpuFlags.has3DNow = IsProcessorFeaturePresent (PF_AMD3D_INSTRUCTIONS_AVAILABLE) != 0;
#else
	cpuFlags.has3DNow = IsProcessorFeaturePresent (PF_3DNOW_INSTRUCTIONS_AVAILABLE) != 0;
#endif

	LARGE_INTEGER f;
	QueryPerformanceFrequency (&f);
	hiResTicksPerSecond = f.QuadPart;
	hiResTicksScaleFactor = 1000.0 / hiResTicksPerSecond;

	String s (SystemStats::getJUCEVersion());

#ifdef JUCE_DEBUG
	const MMRESULT res = timeBeginPeriod (1);
	jassert (res == TIMERR_NOERROR);
#else
	timeBeginPeriod (1);
#endif

#if defined (JUCE_DEBUG) && JUCE_MSVC && JUCE_CHECK_MEMORY_LEAKS
	_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
}

SystemStats::OperatingSystemType SystemStats::getOperatingSystemType() throw()
{
	OSVERSIONINFO info;
	info.dwOSVersionInfoSize = sizeof (info);
	GetVersionEx (&info);

	if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
	{
		switch (info.dwMajorVersion)
		{
			case 5:	 return (info.dwMinorVersion == 0) ? Win2000 : WinXP;
			case 6:	 return (info.dwMinorVersion == 0) ? WinVista : Windows7;
			default:	jassertfalse; break; // !! not a supported OS!
		}
	}
	else if (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
	{
		jassert (info.dwMinorVersion != 0); // !! still running on Windows 95??
		return Win98;
	}

	return UnknownOS;
}

const String SystemStats::getOperatingSystemName() throw()
{
	const char* name = "Unknown OS";

	switch (getOperatingSystemType())
	{
		case Windows7:          name = "Windows 7";         break;
		case WinVista:          name = "Windows Vista";     break;
		case WinXP:             name = "Windows XP";        break;
		case Win2000:           name = "Windows 2000";      break;
		case Win98:             name = "Windows 98";        break;
		default:		jassertfalse; break; // !! new type of OS?
	}

	return name;
}

bool SystemStats::isOperatingSystem64Bit() throw()
{
#ifdef _WIN64
	return true;
#else
	typedef BOOL (WINAPI* LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

	LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress (GetModuleHandle (L"kernel32"), "IsWow64Process");

	BOOL isWow64 = FALSE;

	return (fnIsWow64Process != 0)
			&& fnIsWow64Process (GetCurrentProcess(), &isWow64)
			&& (isWow64 != FALSE);
#endif
}

int SystemStats::getMemorySizeInMegabytes() throw()
{
	MEMORYSTATUSEX mem;
	mem.dwLength = sizeof (mem);
	GlobalMemoryStatusEx (&mem);
	return (int) (mem.ullTotalPhys / (1024 * 1024)) + 1;
}

int SystemStats::getNumCpus() throw()
{
	SYSTEM_INFO systemInfo;
	GetSystemInfo (&systemInfo);

	return systemInfo.dwNumberOfProcessors;
}

uint32 juce_millisecondsSinceStartup() throw()
{
	return (uint32) GetTickCount();
}

int64 Time::getHighResolutionTicks() throw()
{
	LARGE_INTEGER ticks;
	QueryPerformanceCounter (&ticks);

	const int64 mainCounterAsHiResTicks = (GetTickCount() * hiResTicksPerSecond) / 1000;
	const int64 newOffset = mainCounterAsHiResTicks - ticks.QuadPart;

	// fix for a very obscure PCI hardware bug that can make the counter
	// sometimes jump forwards by a few seconds..
	static int64 hiResTicksOffset = 0;
	const int64 offsetDrift = abs64 (newOffset - hiResTicksOffset);

	if (offsetDrift > (hiResTicksPerSecond >> 1))
		hiResTicksOffset = newOffset;

	return ticks.QuadPart + hiResTicksOffset;
}

double Time::getMillisecondCounterHiRes() throw()
{
	return getHighResolutionTicks() * hiResTicksScaleFactor;
}

int64 Time::getHighResolutionTicksPerSecond() throw()
{
	return hiResTicksPerSecond;
}

int64 SystemStats::getClockCycleCounter() throw()
{
#if JUCE_USE_INTRINSICS
	// MS intrinsics version...
	return __rdtsc();

#elif JUCE_GCC
	// GNU inline asm version...
	unsigned int hi = 0, lo = 0;

	__asm__ __volatile__ (
		"xor %%eax, %%eax               \n\
		 xor %%edx, %%edx		   \n\
		 rdtsc			  \n\
		 movl %%eax, %[lo]		  \n\
		 movl %%edx, %[hi]"
		 :
		 : [hi] "m" (hi),
		   [lo] "m" (lo)
		 : "cc", "eax", "ebx", "ecx", "edx", "memory");

	return (int64) ((((uint64) hi) << 32) | lo);
#else
	// MSVC inline asm version...
	unsigned int hi = 0, lo = 0;

	__asm
	{
		xor eax, eax
		xor edx, edx
		rdtsc
		mov lo, eax
		mov hi, edx
	}

	return (int64) ((((uint64) hi) << 32) | lo);
#endif
}

int SystemStats::getCpuSpeedInMegaherz() throw()
{
	const int64 cycles = SystemStats::getClockCycleCounter();
	const uint32 millis = Time::getMillisecondCounter();
	int lastResult = 0;

	for (;;)
	{
		int n = 1000000;
		while (--n > 0) {}

		const uint32 millisElapsed = Time::getMillisecondCounter() - millis;
		const int64 cyclesNow = SystemStats::getClockCycleCounter();

		if (millisElapsed > 80)
		{
			const int newResult = (int) (((cyclesNow - cycles) / millisElapsed) / 1000);

			if (millisElapsed > 500 || (lastResult == newResult && newResult > 100))
				return newResult;

			lastResult = newResult;
		}
	}
}

bool Time::setSystemTimeToThisTime() const throw()
{
	SYSTEMTIME st;

	st.wDayOfWeek = 0;
	st.wYear	   = (WORD) getYear();
	st.wMonth	  = (WORD) (getMonth() + 1);
	st.wDay		= (WORD) getDayOfMonth();
	st.wHour	   = (WORD) getHours();
	st.wMinute	 = (WORD) getMinutes();
	st.wSecond	 = (WORD) getSeconds();
	st.wMilliseconds   = (WORD) (millisSinceEpoch % 1000);

	// do this twice because of daylight saving conversion problems - the
	// first one sets it up, the second one kicks it in.
	return SetLocalTime (&st) != 0
			&& SetLocalTime (&st) != 0;
}

int SystemStats::getPageSize() throw()
{
	SYSTEM_INFO systemInfo;
	GetSystemInfo (&systemInfo);

	return systemInfo.dwPageSize;
}

const String SystemStats::getLogonName()
{
	TCHAR text [256];
	DWORD len = numElementsInArray (text) - 2;
	zerostruct (text);
	GetUserName (text, &len);
	return String (text, len);
}

const String SystemStats::getFullUserName()
{
	return getLogonName();
}

#endif
/*** End of inlined file: juce_win32_SystemStats.cpp ***/


/*** Start of inlined file: juce_win32_Threads.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if ! JUCE_ONLY_BUILD_CORE_LIBRARY
 extern HWND juce_messageWindowHandle;
#endif

#if ! JUCE_USE_INTRINSICS
// In newer compilers, the inline versions of these are used (in juce_Atomic.h), but in
// older ones we have to actually call the ops as win32 functions..
void  Atomic::increment (int32& variable)		{ InterlockedIncrement (reinterpret_cast <volatile long*> (&variable)); }
int32 Atomic::incrementAndReturn (int32& variable)	   { return InterlockedIncrement (reinterpret_cast <volatile long*> (&variable)); }
void  Atomic::decrement (int32& variable)		{ InterlockedDecrement (reinterpret_cast <volatile long*> (&variable)); }
int32 Atomic::decrementAndReturn (int32& variable)	   { return InterlockedDecrement (reinterpret_cast <volatile long*> (&variable)); }
int32 Atomic::compareAndExchange (int32& destination, int32 newValue, int32 oldValue)
														 { return InterlockedCompareExchange (reinterpret_cast <volatile long*> (&destination), newValue, oldValue); }
#endif

void* Atomic::swapPointers (void* volatile* value1, void* volatile value2)   { return InterlockedExchangePointer (value1, value2); }

CriticalSection::CriticalSection() throw()
{
	// (just to check the MS haven't changed this structure and broken things...)
#if _MSC_VER >= 1400
	static_jassert (sizeof (CRITICAL_SECTION) <= sizeof (internal));
#else
	static_jassert (sizeof (CRITICAL_SECTION) <= 24);
#endif

	InitializeCriticalSection ((CRITICAL_SECTION*) internal);
}

CriticalSection::~CriticalSection() throw()
{
	DeleteCriticalSection ((CRITICAL_SECTION*) internal);
}

void CriticalSection::enter() const throw()
{
	EnterCriticalSection ((CRITICAL_SECTION*) internal);
}

bool CriticalSection::tryEnter() const throw()
{
	return TryEnterCriticalSection ((CRITICAL_SECTION*) internal) != FALSE;
}

void CriticalSection::exit() const throw()
{
	LeaveCriticalSection ((CRITICAL_SECTION*) internal);
}

WaitableEvent::WaitableEvent (const bool manualReset) throw()
	: internal (CreateEvent (0, manualReset ? TRUE : FALSE, FALSE, 0))
{
}

WaitableEvent::~WaitableEvent() throw()
{
	CloseHandle (internal);
}

bool WaitableEvent::wait (const int timeOutMillisecs) const throw()
{
	return WaitForSingleObject (internal, timeOutMillisecs) == WAIT_OBJECT_0;
}

void WaitableEvent::signal() const throw()
{
	SetEvent (internal);
}

void WaitableEvent::reset() const throw()
{
	ResetEvent (internal);
}

void JUCE_API juce_threadEntryPoint (void*);

static unsigned int __stdcall threadEntryProc (void* userData)
{
#if ! JUCE_ONLY_BUILD_CORE_LIBRARY
	AttachThreadInput (GetWindowThreadProcessId (juce_messageWindowHandle, 0),
					   GetCurrentThreadId(), TRUE);
#endif

	juce_threadEntryPoint (userData);

	_endthreadex (0);
	return 0;
}

void juce_CloseThreadHandle (void* handle)
{
	CloseHandle ((HANDLE) handle);
}

void* juce_createThread (void* userData)
{
	unsigned int threadId;

	return (void*) _beginthreadex (0, 0,
								   &threadEntryProc,
								   userData,
								   0, &threadId);
}

void juce_killThread (void* handle)
{
	if (handle != 0)
	{
#ifdef JUCE_DEBUG
		OutputDebugString (_T("** Warning - Forced thread termination **\n"));
#endif
		TerminateThread (handle, 0);
	}
}

void juce_setCurrentThreadName (const String& name)
{
#if defined (JUCE_DEBUG) && JUCE_MSVC
	struct
	{
		DWORD dwType;
		LPCSTR szName;
		DWORD dwThreadID;
		DWORD dwFlags;
	} info;

	info.dwType = 0x1000;
	info.szName = name.toCString();
	info.dwThreadID = GetCurrentThreadId();
	info.dwFlags = 0;

	__try
	{
		RaiseException (0x406d1388 /*MS_VC_EXCEPTION*/, 0, sizeof (info) / sizeof (ULONG_PTR), (ULONG_PTR*) &info);
	}
	__except (EXCEPTION_CONTINUE_EXECUTION)
	{}
#else
	(void) name;
#endif
}

Thread::ThreadID Thread::getCurrentThreadId()
{
	return (ThreadID) (pointer_sized_int) GetCurrentThreadId();
}

// priority 1 to 10 where 5=normal, 1=low
bool juce_setThreadPriority (void* threadHandle, int priority)
{
	int pri = THREAD_PRIORITY_TIME_CRITICAL;

	if (priority < 1)
		pri = THREAD_PRIORITY_IDLE;
	else if (priority < 2)
		pri = THREAD_PRIORITY_LOWEST;
	else if (priority < 5)
		pri = THREAD_PRIORITY_BELOW_NORMAL;
	else if (priority < 7)
		pri = THREAD_PRIORITY_NORMAL;
	else if (priority < 9)
		pri = THREAD_PRIORITY_ABOVE_NORMAL;
	else if (priority < 10)
		pri = THREAD_PRIORITY_HIGHEST;

	if (threadHandle == 0)
		threadHandle = GetCurrentThread();

	return SetThreadPriority (threadHandle, pri) != FALSE;
}

void Thread::setCurrentThreadAffinityMask (const uint32 affinityMask)
{
	SetThreadAffinityMask (GetCurrentThread(), affinityMask);
}

static HANDLE sleepEvent = 0;

void juce_initialiseThreadEvents()
{
	if (sleepEvent == 0)
#ifdef JUCE_DEBUG
		sleepEvent = CreateEvent (0, 0, 0, _T("Juce Sleep Event"));
#else
		sleepEvent = CreateEvent (0, 0, 0, 0);
#endif
}

void Thread::yield()
{
	Sleep (0);
}

void JUCE_CALLTYPE Thread::sleep (const int millisecs)
{
	if (millisecs >= 10)
	{
		Sleep (millisecs);
	}
	else
	{
		jassert (sleepEvent != 0);

		// unlike Sleep() this is guaranteed to return to the current thread after
		// the time expires, so we'll use this for short waits, which are more likely
		// to need to be accurate
		WaitForSingleObject (sleepEvent, millisecs);
	}
}

static int lastProcessPriority = -1;

// called by WindowDriver because Windows does wierd things to process priority
// when you swap apps, and this forces an update when the app is brought to the front.
void juce_repeatLastProcessPriority()
{
	if (lastProcessPriority >= 0) // (avoid changing this if it's not been explicitly set by the app..)
	{
		DWORD p;

		switch (lastProcessPriority)
		{
			case Process::LowPriority:	  p = IDLE_PRIORITY_CLASS; break;
			case Process::NormalPriority:	   p = NORMAL_PRIORITY_CLASS; break;
			case Process::HighPriority:	 p = HIGH_PRIORITY_CLASS; break;
			case Process::RealtimePriority:	 p = REALTIME_PRIORITY_CLASS; break;
			default:				jassertfalse; return; // bad priority value
		}

		SetPriorityClass (GetCurrentProcess(), p);
	}
}

void Process::setPriority (ProcessPriority prior)
{
	if (lastProcessPriority != (int) prior)
	{
		lastProcessPriority = (int) prior;
		juce_repeatLastProcessPriority();
	}
}

bool JUCE_PUBLIC_FUNCTION juce_isRunningUnderDebugger()
{
	return IsDebuggerPresent() != FALSE;
}

bool JUCE_CALLTYPE Process::isRunningUnderDebugger()
{
	return juce_isRunningUnderDebugger();
}

void Process::raisePrivilege()
{
	jassertfalse // xxx not implemented
}

void Process::lowerPrivilege()
{
	jassertfalse // xxx not implemented
}

void Process::terminate()
{
#if defined (JUCE_DEBUG) && JUCE_MSVC && JUCE_CHECK_MEMORY_LEAKS
	_CrtDumpMemoryLeaks();
#endif

	// bullet in the head in case there's a problem shutting down..
	ExitProcess (0);
}

void* PlatformUtilities::loadDynamicLibrary (const String& name)
{
	void* result = 0;

	JUCE_TRY
	{
		result = LoadLibrary (name);
	}
	JUCE_CATCH_ALL

	return result;
}

void PlatformUtilities::freeDynamicLibrary (void* h)
{
	JUCE_TRY
	{
		if (h != 0)
			FreeLibrary ((HMODULE) h);
	}
	JUCE_CATCH_ALL
}

void* PlatformUtilities::getProcedureEntryPoint (void* h, const String& name)
{
	return (h != 0) ? GetProcAddress ((HMODULE) h, name.toCString()) : 0;
}

class InterProcessLock::Pimpl
{
public:
	Pimpl (const String& name, const int timeOutMillisecs)
		: handle (0), refCount (1)
	{
		handle = CreateMutex (0, TRUE, "Global\\" + name.replaceCharacter ('\\','/'));

		if (handle != 0 && GetLastError() == ERROR_ALREADY_EXISTS)
		{
			if (timeOutMillisecs == 0)
			{
				close();
				return;
			}

			switch (WaitForSingleObject (handle, timeOutMillisecs < 0 ? INFINITE : timeOutMillisecs))
			{
			case WAIT_OBJECT_0:
			case WAIT_ABANDONED:
				break;

			case WAIT_TIMEOUT:
			default:
				close();
				break;
			}
		}
	}

	~Pimpl()
	{
		close();
	}

	void close()
	{
		if (handle != 0)
		{
			ReleaseMutex (handle);
			CloseHandle (handle);
			handle = 0;
		}
	}

	HANDLE handle;
	int refCount;
};

InterProcessLock::InterProcessLock (const String& name_)
	: name (name_)
{
}

InterProcessLock::~InterProcessLock()
{
}

bool InterProcessLock::enter (const int timeOutMillisecs)
{
	const ScopedLock sl (lock);

	if (pimpl == 0)
	{
		pimpl = new Pimpl (name, timeOutMillisecs);

		if (pimpl->handle == 0)
			pimpl = 0;
	}
	else
	{
		pimpl->refCount++;
	}

	return pimpl != 0;
}

void InterProcessLock::exit()
{
	const ScopedLock sl (lock);

	// Trying to release the lock too many times!
	jassert (pimpl != 0);

	if (pimpl != 0 && --(pimpl->refCount) == 0)
		pimpl = 0;
}

#endif
/*** End of inlined file: juce_win32_Threads.cpp ***/


/*** Start of inlined file: juce_win32_Files.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#ifndef CSIDL_MYMUSIC
 #define CSIDL_MYMUSIC 0x000d
#endif

#ifndef CSIDL_MYVIDEO
 #define CSIDL_MYVIDEO 0x000e
#endif

#ifndef INVALID_FILE_ATTRIBUTES
 #define INVALID_FILE_ATTRIBUTES ((DWORD) -1)
#endif

const juce_wchar  File::separator	= '\\';
const juce_wchar* File::separatorString  = T("\\");

bool juce_fileExists (const String& fileName, const bool dontCountDirectories)
{
	if (fileName.isEmpty())
		return false;

	const DWORD attr = GetFileAttributes (fileName);
	return dontCountDirectories ? ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
								: (attr != INVALID_FILE_ATTRIBUTES);
}

bool juce_isDirectory (const String& fileName)
{
	const DWORD attr = GetFileAttributes (fileName);
	return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) && (attr != INVALID_FILE_ATTRIBUTES);
}

bool juce_canWriteToFile (const String& fileName)
{
	const DWORD attr = GetFileAttributes (fileName);
	return (attr & FILE_ATTRIBUTE_READONLY) == 0;
}

bool juce_setFileReadOnly (const String& fileName, bool isReadOnly)
{
	DWORD attr = GetFileAttributes (fileName);

	if (attr == INVALID_FILE_ATTRIBUTES)
		return false;

	if (isReadOnly != juce_canWriteToFile (fileName))
		return true;

	if (isReadOnly)
		attr |= FILE_ATTRIBUTE_READONLY;
	else
		attr &= ~FILE_ATTRIBUTE_READONLY;

	return SetFileAttributes (fileName, attr) != FALSE;
}

bool File::isHidden() const
{
	return (GetFileAttributes (getFullPathName()) & FILE_ATTRIBUTE_HIDDEN) != 0;
}

bool juce_deleteFile (const String& fileName)
{
	if (juce_isDirectory (fileName))
		return RemoveDirectory (fileName) != 0;

	return DeleteFile (fileName) != 0;
}

bool File::moveToTrash() const
{
	if (! exists())
		return true;

	SHFILEOPSTRUCT fos;
	zerostruct (fos);

	// The string we pass in must be double null terminated..
	String doubleNullTermPath (getFullPathName() + " ");
	TCHAR* const p = const_cast <TCHAR*> (static_cast <const TCHAR*> (doubleNullTermPath));
	p [getFullPathName().length()] = 0;

	fos.wFunc = FO_DELETE;
	fos.pFrom = p;
	fos.fFlags = FOF_ALLOWUNDO | FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION
				   | FOF_NOCONFIRMMKDIR | FOF_RENAMEONCOLLISION;

	return SHFileOperation (&fos) == 0;
}

bool juce_moveFile (const String& source, const String& dest)
{
	return MoveFile (source, dest) != 0;
}

bool juce_copyFile (const String& source, const String& dest)
{
	return CopyFile (source, dest, false) != 0;
}

void juce_createDirectory (const String& fileName)
{
	if (! juce_fileExists (fileName, true))
		CreateDirectory (fileName, 0);
}

// return 0 if not possible
void* juce_fileOpen (const String& fileName, bool forWriting)
{
	HANDLE h;

	if (forWriting)
	{
		h = CreateFile (fileName, GENERIC_WRITE, FILE_SHARE_READ, 0,
						OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

		if (h != INVALID_HANDLE_VALUE)
			SetFilePointer (h, 0, 0, FILE_END);
		else
			h = 0;
	}
	else
	{
		h = CreateFile (fileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0);

		if (h == INVALID_HANDLE_VALUE)
			h = 0;
	}

	return h;
}

void juce_fileClose (void* handle)
{
	CloseHandle (handle);
}

int juce_fileRead (void* handle, void* buffer, int size)
{
	DWORD num = 0;
	ReadFile ((HANDLE) handle, buffer, size, &num, 0);
	return (int) num;
}

int juce_fileWrite (void* handle, const void* buffer, int size)
{
	DWORD num;
	WriteFile ((HANDLE) handle, buffer, size, &num, 0);
	return (int) num;
}

int64 juce_fileSetPosition (void* handle, int64 pos)
{
	LARGE_INTEGER li;
	li.QuadPart = pos;
	li.LowPart = SetFilePointer ((HANDLE) handle, li.LowPart, &li.HighPart, FILE_BEGIN);  // (returns -1 if it fails)
	return li.QuadPart;
}

int64 juce_fileGetPosition (void* handle)
{
	LARGE_INTEGER li;
	li.QuadPart = 0;
	li.LowPart = SetFilePointer ((HANDLE) handle, 0, &li.HighPart, FILE_CURRENT);  // (returns -1 if it fails)
	return jmax ((int64) 0, li.QuadPart);
}

void juce_fileFlush (void* handle)
{
	FlushFileBuffers ((HANDLE) handle);
}

int64 juce_getFileSize (const String& fileName)
{
	WIN32_FILE_ATTRIBUTE_DATA attributes;

	if (GetFileAttributesEx (fileName, GetFileExInfoStandard, &attributes))
		return (((int64) attributes.nFileSizeHigh) << 32) | attributes.nFileSizeLow;

	return 0;
}

static int64 fileTimeToTime (const FILETIME* const ft)
{
	static_jassert (sizeof (ULARGE_INTEGER) == sizeof (FILETIME)); // tell me if this fails!

	return (reinterpret_cast<const ULARGE_INTEGER*> (ft)->QuadPart - literal64bit (116444736000000000)) / 10000;
}

static void timeToFileTime (const int64 time, FILETIME* const ft)
{
	reinterpret_cast<ULARGE_INTEGER*> (ft)->QuadPart = time * 10000 + literal64bit (116444736000000000);
}

void juce_getFileTimes (const String& fileName,
						int64& modificationTime,
						int64& accessTime,
						int64& creationTime)
{
	WIN32_FILE_ATTRIBUTE_DATA attributes;

	if (GetFileAttributesEx (fileName, GetFileExInfoStandard, &attributes))
	{
		modificationTime = fileTimeToTime (&attributes.ftLastWriteTime);
		creationTime = fileTimeToTime (&attributes.ftCreationTime);
		accessTime = fileTimeToTime (&attributes.ftLastAccessTime);
	}
	else
	{
		creationTime = accessTime = modificationTime = 0;
	}
}

bool juce_setFileTimes (const String& fileName,
						int64 modificationTime,
						int64 accessTime,
						int64 creationTime)
{
	void* const h = juce_fileOpen (fileName, true);
	bool ok = false;

	if (h != 0)
	{
		FILETIME m, a, c;
		timeToFileTime (modificationTime, &m);
		timeToFileTime (accessTime, &a);
		timeToFileTime (creationTime, &c);

		ok = SetFileTime ((HANDLE) h,
						  creationTime > 0	 ? &c : 0,
						  accessTime > 0	   ? &a : 0,
						  modificationTime > 0 ? &m : 0) != 0;

		juce_fileClose (h);
	}

	return ok;
}

// return '\0' separated list of strings
const StringArray juce_getFileSystemRoots()
{
	TCHAR buffer [2048];
	buffer[0] = 0;
	buffer[1] = 0;
	GetLogicalDriveStrings (2048, buffer);

	const TCHAR* n = buffer;
	StringArray roots;

	while (*n != 0)
	{
		roots.add (String (n));

		while (*n++ != 0)
		{}
	}

	roots.sort (true);
	return roots;
}

static const String getDriveFromPath (const String& path)
{
	if (path.isNotEmpty() && path[1] == ':')
		return path.substring (0, 2) + '\\';

	return path;
}

const String juce_getVolumeLabel (const String& filenameOnVolume,
								  int& volumeSerialNumber)
{
	TCHAR dest[64];
	DWORD serialNum;

	if (! GetVolumeInformation (getDriveFromPath (filenameOnVolume), dest,
								numElementsInArray (dest), &serialNum, 0, 0, 0, 0))
	{
		dest[0] = 0;
		serialNum = 0;
	}

	volumeSerialNumber = serialNum;
	return dest;
}

static int64 getDiskSpaceInfo (const String& path, const bool total)
{
	ULARGE_INTEGER spc, tot, totFree;

	if (GetDiskFreeSpaceEx (getDriveFromPath (path), &spc, &tot, &totFree))
		return total ? (int64) tot.QuadPart
					 : (int64) spc.QuadPart;

	return 0;
}

int64 File::getBytesFreeOnVolume() const
{
	return getDiskSpaceInfo (getFullPathName(), false);
}

int64 File::getVolumeTotalSize() const
{
	return getDiskSpaceInfo (getFullPathName(), true);
}

static unsigned int getWindowsDriveType (const String& path)
{
	return GetDriveType (getDriveFromPath (path));
}

bool File::isOnCDRomDrive() const
{
	return getWindowsDriveType (getFullPathName()) == DRIVE_CDROM;
}

bool File::isOnHardDisk() const
{
	if (fullPath.isEmpty())
		return false;

	const unsigned int n = getWindowsDriveType (getFullPathName());

	if (fullPath.toLowerCase()[0] <= 'b' && fullPath[1] == ':')
		return n != DRIVE_REMOVABLE;
	else
		return n != DRIVE_CDROM && n != DRIVE_REMOTE;
}

bool File::isOnRemovableDrive() const
{
	if (fullPath.isEmpty())
		return false;

	const unsigned int n = getWindowsDriveType (getFullPathName());

	return n == DRIVE_CDROM
		|| n == DRIVE_REMOTE
		|| n == DRIVE_REMOVABLE
		|| n == DRIVE_RAMDISK;
}

static const File juce_getSpecialFolderPath (int type)
{
	WCHAR path [MAX_PATH + 256];

	if (SHGetSpecialFolderPath (0, path, type, FALSE))
		return File (String (path));

	return File::nonexistent;
}

const File JUCE_CALLTYPE File::getSpecialLocation (const SpecialLocationType type)
{
	int csidlType = 0;

	switch (type)
	{
		case userHomeDirectory:		 csidlType = CSIDL_PROFILE; break;
		case userDocumentsDirectory:		csidlType = CSIDL_PERSONAL; break;
		case userDesktopDirectory:		  csidlType = CSIDL_DESKTOP; break;
		case userApplicationDataDirectory:	  csidlType = CSIDL_APPDATA; break;
		case commonApplicationDataDirectory:	csidlType = CSIDL_COMMON_APPDATA; break;
		case globalApplicationsDirectory:	   csidlType = CSIDL_PROGRAM_FILES; break;
		case userMusicDirectory:		csidlType = CSIDL_MYMUSIC; break;
		case userMoviesDirectory:		   csidlType = CSIDL_MYVIDEO; break;

		case tempDirectory:
		{
			WCHAR dest [2048];
			dest[0] = 0;
			GetTempPath (numElementsInArray (dest), dest);
			return File (String (dest));
		}

		case invokedExecutableFile:
		case currentExecutableFile:
		case currentApplicationFile:
		{
			HINSTANCE moduleHandle = (HINSTANCE) PlatformUtilities::getCurrentModuleInstanceHandle();

			WCHAR dest [MAX_PATH + 256];
			dest[0] = 0;
			GetModuleFileName (moduleHandle, dest, numElementsInArray (dest));
			return File (String (dest));
		}
		break;

		default:
			jassertfalse // unknown type?
			return File::nonexistent;
	}

	return juce_getSpecialFolderPath (csidlType);
}

const File File::getCurrentWorkingDirectory()
{
	WCHAR dest [MAX_PATH + 256];
	dest[0] = 0;
	GetCurrentDirectory (numElementsInArray (dest), dest);
	return File (String (dest));
}

bool File::setAsCurrentWorkingDirectory() const
{
	return SetCurrentDirectory (getFullPathName()) != FALSE;
}

const String File::getVersion() const
{
	String result;

	DWORD handle = 0;
	DWORD bufferSize = GetFileVersionInfoSize (getFullPathName(), &handle);
	HeapBlock<char> buffer;
	buffer.calloc (bufferSize);

	if (GetFileVersionInfo (getFullPathName(), 0, bufferSize, buffer))
	{
		VS_FIXEDFILEINFO* vffi;
		UINT len = 0;

		if (VerQueryValue (buffer, (LPTSTR) _T("\\"), (LPVOID*) &vffi, &len))
		{
			result << (int) HIWORD (vffi->dwFileVersionMS) << '.'
				   << (int) LOWORD (vffi->dwFileVersionMS) << '.'
				   << (int) HIWORD (vffi->dwFileVersionLS) << '.'
				   << (int) LOWORD (vffi->dwFileVersionLS);
		}
	}

	return result;
}

const File File::getLinkedTarget() const
{
	File result (*this);
	String p (getFullPathName());

	if (! exists())
		p += ".lnk";
	else if (getFileExtension() != ".lnk")
		return result;

	ComSmartPtr <IShellLink> shellLink;
	if (SUCCEEDED (shellLink.CoCreateInstance (CLSID_ShellLink, CLSCTX_INPROC_SERVER)))
	{
		ComSmartPtr <IPersistFile> persistFile;
		if (SUCCEEDED (shellLink->QueryInterface (IID_IPersistFile, (LPVOID*) &persistFile)))
		{
			if (SUCCEEDED (persistFile->Load ((const WCHAR*) p, STGM_READ))
				 && SUCCEEDED (shellLink->Resolve (0, SLR_ANY_MATCH | SLR_NO_UI)))
			{
				WIN32_FIND_DATA winFindData;
				WCHAR resolvedPath [MAX_PATH];

				if (SUCCEEDED (shellLink->GetPath (resolvedPath, MAX_PATH, &winFindData, SLGP_UNCPRIORITY)))
					result = File (resolvedPath);
			}
		}
	}

	return result;
}

template <class FindDataType>
static void getFindFileInfo (FindDataType& findData,
							 String& filename, bool* const isDir, bool* const isHidden,
							 int64* const fileSize, Time* const modTime, Time* const creationTime,
							 bool* const isReadOnly)
{
	filename = findData.cFileName;

	if (isDir != 0)
		*isDir = ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);

	if (isHidden != 0)
		*isHidden = ((findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0);

	if (fileSize != 0)
		*fileSize = findData.nFileSizeLow + (((int64) findData.nFileSizeHigh) << 32);

	if (modTime != 0)
		*modTime = fileTimeToTime (&findData.ftLastWriteTime);

	if (creationTime != 0)
		*creationTime = fileTimeToTime (&findData.ftCreationTime);

	if (isReadOnly != 0)
		*isReadOnly = ((findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
}

void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResult,
						  bool* isDir, bool* isHidden, int64* fileSize,
						  Time* modTime, Time* creationTime, bool* isReadOnly)
{
	String wc (directory);

	if (! wc.endsWithChar (File::separator))
		wc += File::separator;

	wc += wildCard;

	WIN32_FIND_DATA findData;
	HANDLE h = FindFirstFile (wc, &findData);

	if (h != INVALID_HANDLE_VALUE)
	{
		getFindFileInfo (findData, firstResult, isDir, isHidden, fileSize,
						 modTime, creationTime, isReadOnly);
		return h;
	}

	firstResult = String::empty;
	return 0;
}

bool juce_findFileNext (void* handle, String& resultFile,
						bool* isDir, bool* isHidden, int64* fileSize,
						Time* modTime, Time* creationTime, bool* isReadOnly)
{
	WIN32_FIND_DATA findData;

	if (handle != 0 && FindNextFile ((HANDLE) handle, &findData) != 0)
	{
		getFindFileInfo (findData, resultFile, isDir, isHidden, fileSize,
						 modTime, creationTime, isReadOnly);
		return true;
	}

	resultFile = String::empty;
	return false;
}

void juce_findFileClose (void* handle)
{
	FindClose (handle);
}

bool juce_launchFile (const String& fileName, const String& parameters)
{
	HINSTANCE hInstance = 0;

	JUCE_TRY
	{
		hInstance = ShellExecute (0, 0, fileName, parameters, 0, SW_SHOWDEFAULT);
	}
	JUCE_CATCH_ALL

	return hInstance > (HINSTANCE) 32;
}

void File::revealToUser() const
{
	if (isDirectory())
		startAsProcess();
	else if (getParentDirectory().exists())
		getParentDirectory().startAsProcess();
}

class NamedPipeInternal
{
public:
	NamedPipeInternal (const String& file, const bool isPipe_)
		: pipeH (0),
		  cancelEvent (0),
		  connected (false),
		  isPipe (isPipe_)
	{
		cancelEvent = CreateEvent (0, FALSE, FALSE, 0);

		pipeH = isPipe ? CreateNamedPipe (file, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, 0,
										  PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, 0)
					   : CreateFile (file, GENERIC_READ | GENERIC_WRITE, 0, 0,
									 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
	}

	~NamedPipeInternal()
	{
		disconnectPipe();

		if (pipeH != 0)
			CloseHandle (pipeH);

		CloseHandle (cancelEvent);
	}

	bool connect (const int timeOutMs)
	{
		if (! isPipe)
			return true;

		if (! connected)
		{
			OVERLAPPED over;
			zerostruct (over);

			over.hEvent = CreateEvent (0, TRUE, FALSE, 0);

			if (ConnectNamedPipe (pipeH, &over))
			{
				connected = false;  // yes, you read that right. In overlapped mode it should always return 0.
			}
			else
			{
				const int err = GetLastError();

				if (err == ERROR_IO_PENDING || err == ERROR_PIPE_LISTENING)
				{
					HANDLE handles[] = { over.hEvent, cancelEvent };

					if (WaitForMultipleObjects (2, handles, FALSE,
												timeOutMs >= 0 ? timeOutMs : INFINITE) == WAIT_OBJECT_0)
						connected = true;
				}
				else if (err == ERROR_PIPE_CONNECTED)
				{
					connected = true;
				}
			}

			CloseHandle (over.hEvent);
		}

		return connected;
	}

	void disconnectPipe()
	{
		if (connected)
		{
			DisconnectNamedPipe (pipeH);
			connected = false;
		}
	}

	HANDLE pipeH;
	HANDLE cancelEvent;
	bool connected, isPipe;
};

void NamedPipe::close()
{
	cancelPendingReads();

	const ScopedLock sl (lock);
	delete static_cast<NamedPipeInternal*> (internal);
	internal = 0;
}

bool NamedPipe::openInternal (const String& pipeName, const bool createPipe)
{
	close();

	ScopedPointer<NamedPipeInternal> intern (new NamedPipeInternal ("\\\\.\\pipe\\" + pipeName, createPipe));

	if (intern->pipeH != INVALID_HANDLE_VALUE)
	{
		internal = intern.release();
		return true;
	}

	return false;
}

int NamedPipe::read (void* destBuffer, int maxBytesToRead, int timeOutMilliseconds)
{
	const ScopedLock sl (lock);
	int bytesRead = -1;
	bool waitAgain = true;

	while (waitAgain && internal != 0)
	{
		NamedPipeInternal* const intern = static_cast<NamedPipeInternal*> (internal);
		waitAgain = false;

		if (! intern->connect (timeOutMilliseconds))
			break;

		if (maxBytesToRead <= 0)
			return 0;

		OVERLAPPED over;
		zerostruct (over);
		over.hEvent = CreateEvent (0, TRUE, FALSE, 0);

		unsigned long numRead;

		if (ReadFile (intern->pipeH, destBuffer, maxBytesToRead, &numRead, &over))
		{
			bytesRead = (int) numRead;
		}
		else if (GetLastError() == ERROR_IO_PENDING)
		{
			HANDLE handles[] = { over.hEvent, intern->cancelEvent };
			DWORD waitResult = WaitForMultipleObjects (2, handles, FALSE,
													   timeOutMilliseconds >= 0 ? timeOutMilliseconds
																				: INFINITE);
			if (waitResult != WAIT_OBJECT_0)
			{
				// if the operation timed out, let's cancel it...
				CancelIo (intern->pipeH);
				WaitForSingleObject (over.hEvent, INFINITE);  // makes sure cancel is complete
			}

			if (GetOverlappedResult (intern->pipeH, &over, &numRead, FALSE))
			{
				bytesRead = (int) numRead;
			}
			else if (GetLastError() == ERROR_BROKEN_PIPE && intern->isPipe)
			{
				intern->disconnectPipe();
				waitAgain = true;
			}
		}
		else
		{
			waitAgain = internal != 0;
			Sleep (5);
		}

		CloseHandle (over.hEvent);
	}

	return bytesRead;
}

int NamedPipe::write (const void* sourceBuffer, int numBytesToWrite, int timeOutMilliseconds)
{
	int bytesWritten = -1;
	NamedPipeInternal* const intern = static_cast<NamedPipeInternal*> (internal);

	if (intern != 0 && intern->connect (timeOutMilliseconds))
	{
		if (numBytesToWrite <= 0)
			return 0;

		OVERLAPPED over;
		zerostruct (over);

		over.hEvent = CreateEvent (0, TRUE, FALSE, 0);

		unsigned long numWritten;

		if (WriteFile (intern->pipeH, sourceBuffer, numBytesToWrite, &numWritten, &over))
		{
			bytesWritten = (int) numWritten;
		}
		else if (GetLastError() == ERROR_IO_PENDING)
		{
			HANDLE handles[] = { over.hEvent, intern->cancelEvent };
			DWORD waitResult;

			waitResult = WaitForMultipleObjects (2, handles, FALSE,
												 timeOutMilliseconds >= 0 ? timeOutMilliseconds
																		  : INFINITE);

			if (waitResult != WAIT_OBJECT_0)
			{
				CancelIo (intern->pipeH);
				WaitForSingleObject (over.hEvent, INFINITE);
			}

			if (GetOverlappedResult (intern->pipeH, &over, &numWritten, FALSE))
			{
				bytesWritten = (int) numWritten;
			}
			else if (GetLastError() == ERROR_BROKEN_PIPE && intern->isPipe)
			{
				intern->disconnectPipe();
			}
		}

		CloseHandle (over.hEvent);
	}

	return bytesWritten;
}

void NamedPipe::cancelPendingReads()
{
	if (internal != 0)
		SetEvent (static_cast<NamedPipeInternal*> (internal)->cancelEvent);
}

#endif
/*** End of inlined file: juce_win32_Files.cpp ***/


/*** Start of inlined file: juce_win32_Network.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#ifndef INTERNET_FLAG_NEED_FILE
  #define INTERNET_FLAG_NEED_FILE 0x00000010
#endif

#ifndef INTERNET_OPTION_DISABLE_AUTODIAL
  #define INTERNET_OPTION_DISABLE_AUTODIAL 70
#endif

bool juce_isOnLine()
{
	DWORD connectionType;

	return InternetGetConnectedState (&connectionType, 0) != 0
			|| (connectionType & (INTERNET_CONNECTION_LAN | INTERNET_CONNECTION_PROXY)) != 0;
}

struct ConnectionAndRequestStruct
{
	HINTERNET connection, request;
};

static HINTERNET sessionHandle = 0;

#ifndef WORKAROUND_TIMEOUT_BUG
 //#define WORKAROUND_TIMEOUT_BUG 1
#endif

#if WORKAROUND_TIMEOUT_BUG
// Required because of a Microsoft bug in setting a timeout
class InternetConnectThread  : public Thread
{
public:
	InternetConnectThread (URL_COMPONENTS& uc_, HINTERNET& connection_, const bool isFtp_)
		: Thread ("Internet"), uc (uc_), connection (connection_), isFtp (isFtp_)
	{
		startThread();
	}

	~InternetConnectThread()
	{
		stopThread (60000);
	}

	void run()
	{
		connection = InternetConnect (sessionHandle, uc.lpszHostName,
									  uc.nPort, _T(""), _T(""),
									  isFtp ? INTERNET_SERVICE_FTP
											: INTERNET_SERVICE_HTTP,
									  0, 0);
		notify();
	}

	juce_UseDebuggingNewOperator

private:
	URL_COMPONENTS& uc;
	HINTERNET& connection;
	const bool isFtp;

	InternetConnectThread (const InternetConnectThread&);
	InternetConnectThread& operator= (const InternetConnectThread&);
};
#endif

void* juce_openInternetFile (const String& url,
							 const String& headers,
							 const MemoryBlock& postData,
							 const bool isPost,
							 URL::OpenStreamProgressCallback* callback,
							 void* callbackContext,
							 int timeOutMs)
{
	if (sessionHandle == 0)
		sessionHandle = InternetOpen (_T("juce"),
									  INTERNET_OPEN_TYPE_PRECONFIG,
									  0, 0, 0);

	if (sessionHandle != 0)
	{
		// break up the url..
		TCHAR file[1024], server[1024];

		URL_COMPONENTS uc;
		zerostruct (uc);

		uc.dwStructSize = sizeof (uc);
		uc.dwUrlPathLength = sizeof (file);
		uc.dwHostNameLength = sizeof (server);
		uc.lpszUrlPath = file;
		uc.lpszHostName = server;

		if (InternetCrackUrl (url, 0, 0, &uc))
		{
			int disable = 1;
			InternetSetOption (sessionHandle, INTERNET_OPTION_DISABLE_AUTODIAL, &disable, sizeof (disable));

			if (timeOutMs == 0)
				timeOutMs = 30000;
			else if (timeOutMs < 0)
				timeOutMs = -1;

			InternetSetOption (sessionHandle, INTERNET_OPTION_CONNECT_TIMEOUT, &timeOutMs, sizeof (timeOutMs));

			const bool isFtp = url.startsWithIgnoreCase ("ftp:");

#if WORKAROUND_TIMEOUT_BUG
			HINTERNET connection = 0;

			{
				InternetConnectThread connectThread (uc, connection, isFtp);
				connectThread.wait (timeOutMs);

				if (connection == 0)
				{
					InternetCloseHandle (sessionHandle);
					sessionHandle = 0;
				}
			}
#else
			HINTERNET connection = InternetConnect (sessionHandle,
													uc.lpszHostName,
													uc.nPort,
													_T(""), _T(""),
													isFtp ? INTERNET_SERVICE_FTP
														  : INTERNET_SERVICE_HTTP,
													0, 0);
#endif

			if (connection != 0)
			{
				if (isFtp)
				{
					HINTERNET request = FtpOpenFile (connection,
													 uc.lpszUrlPath,
													 GENERIC_READ,
													 FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_NEED_FILE,
													 0);

					ConnectionAndRequestStruct* const result = new ConnectionAndRequestStruct();
					result->connection = connection;
					result->request = request;
					return result;
				}
				else
				{
					const TCHAR* mimeTypes[] = { _T("*/*"), 0 };

					DWORD flags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;

					if (url.startsWithIgnoreCase ("https:"))
						flags |= INTERNET_FLAG_SECURE;  // (this flag only seems necessary if the OS is running IE6 -
														//  IE7 seems to automatically work out when it's https)

					HINTERNET request = HttpOpenRequest (connection,
														 isPost ? _T("POST")
																: _T("GET"),
														 uc.lpszUrlPath,
														 0, 0, mimeTypes, flags, 0);

					if (request != 0)
					{
						INTERNET_BUFFERS buffers;
						zerostruct (buffers);
						buffers.dwStructSize = sizeof (INTERNET_BUFFERS);
						buffers.lpcszHeader = (LPCTSTR) headers;
						buffers.dwHeadersLength = headers.length();
						buffers.dwBufferTotal = (DWORD) postData.getSize();
						ConnectionAndRequestStruct* result = 0;

						if (HttpSendRequestEx (request, &buffers, 0, HSR_INITIATE, 0))
						{
							int bytesSent = 0;

							for (;;)
							{
								const int bytesToDo = jmin (1024, (int) postData.getSize() - bytesSent);
								DWORD bytesDone = 0;

								if (bytesToDo > 0
									 && ! InternetWriteFile (request,
															 static_cast <const char*> (postData.getData()) + bytesSent,
															 bytesToDo, &bytesDone))
								{
									break;
								}

								if (bytesToDo == 0 || (int) bytesDone < bytesToDo)
								{
									result = new ConnectionAndRequestStruct();
									result->connection = connection;
									result->request = request;

									HttpEndRequest (request, 0, 0, 0);
									return result;
								}

								bytesSent += bytesDone;

								if (callback != 0 && ! callback (callbackContext, bytesSent, postData.getSize()))
									break;
							}
						}

						InternetCloseHandle (request);
					}

					InternetCloseHandle (connection);
				}
			}
		}
	}

	return 0;
}

int juce_readFromInternetFile (void* handle, void* buffer, int bytesToRead)
{
	DWORD bytesRead = 0;
	const ConnectionAndRequestStruct* const crs = static_cast <ConnectionAndRequestStruct*> (handle);

	if (crs != 0)
		InternetReadFile (crs->request,
						  buffer, bytesToRead,
						  &bytesRead);

	return bytesRead;
}

int juce_seekInInternetFile (void* handle, int newPosition)
{
	if (handle != 0)
	{
		const ConnectionAndRequestStruct* const crs = static_cast <ConnectionAndRequestStruct*> (handle);
		return InternetSetFilePointer (crs->request, newPosition, 0, FILE_BEGIN, 0);
	}
	else
	{
		return -1;
	}
}

int64 juce_getInternetFileContentLength (void* handle)
{
	const ConnectionAndRequestStruct* const crs = static_cast <ConnectionAndRequestStruct*> (handle);

	if (crs != 0)
	{
		DWORD index = 0, result = 0, size = sizeof (result);

		if (HttpQueryInfo (crs->request, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
						   &result, &size, &index))
		{
			return (int64) result;
		}
	}

	return -1;
}

void juce_closeInternetFile (void* handle)
{
	if (handle != 0)
	{
		ConnectionAndRequestStruct* const crs = static_cast <ConnectionAndRequestStruct*> (handle);
		InternetCloseHandle (crs->request);
		InternetCloseHandle (crs->connection);
		delete crs;
	}
}

static int getMACAddressViaGetAdaptersInfo (int64* addresses, int maxNum, const bool littleEndian) throw()
{
	int numFound = 0;

	DynamicLibraryLoader dll ("iphlpapi.dll");
	DynamicLibraryImport (GetAdaptersInfo, getAdaptersInfo, DWORD, dll, (PIP_ADAPTER_INFO, PULONG))

	if (getAdaptersInfo != 0)
	{
		ULONG len = sizeof (IP_ADAPTER_INFO);
		MemoryBlock mb;
		PIP_ADAPTER_INFO adapterInfo = (PIP_ADAPTER_INFO) mb.getData();

		if (getAdaptersInfo (adapterInfo, &len) == ERROR_BUFFER_OVERFLOW)
		{
			mb.setSize (len);
			adapterInfo = (PIP_ADAPTER_INFO) mb.getData();
		}

		if (getAdaptersInfo (adapterInfo, &len) == NO_ERROR)
		{
			PIP_ADAPTER_INFO adapter = adapterInfo;

			while (adapter != 0)
			{
				int64 mac = 0;
				for (unsigned int i = 0; i < adapter->AddressLength; ++i)
					mac = (mac << 8) | adapter->Address[i];

				if (littleEndian)
					mac = (int64) ByteOrder::swap ((uint64) mac);

				if (numFound < maxNum && mac != 0)
					addresses [numFound++] = mac;

				adapter = adapter->Next;
			}
		}
	}

	return numFound;
}

struct ASTAT
{
	ADAPTER_STATUS adapt;
	NAME_BUFFER	NameBuff [30];
};

static int getMACAddressesViaNetBios (int64* addresses, int maxNum, const bool littleEndian) throw()
{
	int numFound = 0;

	DynamicLibraryLoader dll ("netapi32.dll");
	DynamicLibraryImport (Netbios, NetbiosCall, UCHAR, dll, (PNCB))

	if (NetbiosCall != 0)
	{
		NCB ncb;
		zerostruct (ncb);

		ASTAT astat;
		zerostruct (astat);

		LANA_ENUM enums;
		zerostruct (enums);

		ncb.ncb_command = NCBENUM;
		ncb.ncb_buffer = (unsigned char*) &enums;
		ncb.ncb_length = sizeof (LANA_ENUM);
		NetbiosCall (&ncb);

		for (int i = 0; i < enums.length; ++i)
		{
			zerostruct (ncb);
			ncb.ncb_command = NCBRESET;
			ncb.ncb_lana_num = enums.lana[i];

			if (NetbiosCall (&ncb) == 0)
			{
				zerostruct (ncb);
				memcpy (ncb.ncb_callname, "*                   ", NCBNAMSZ);
				ncb.ncb_command = NCBASTAT;
				ncb.ncb_lana_num = enums.lana[i];

				ncb.ncb_buffer = (unsigned char*) &astat;
				ncb.ncb_length = sizeof (ASTAT);

				if (NetbiosCall (&ncb) == 0)
				{
					if (astat.adapt.adapter_type == 0xfe)
					{
						uint64 mac = 0;
						for (int i = 6; --i >= 0;)
							mac = (mac << 8) | astat.adapt.adapter_address [littleEndian ? i : (5 - i)];

						if (numFound < maxNum && mac != 0)
							addresses [numFound++] = mac;
					}
				}
			}
		}
	}

	return numFound;
}

int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian)
{
	int numFound = getMACAddressViaGetAdaptersInfo (addresses, maxNum, littleEndian);

	if (numFound == 0)
		numFound = getMACAddressesViaNetBios (addresses, maxNum, littleEndian);

	return numFound;
}

typedef ULONG (WINAPI *MAPISendMailType) (LHANDLE, ULONG, lpMapiMessage, ::FLAGS, ULONG);

bool PlatformUtilities::launchEmailWithAttachments (const String& targetEmailAddress,
													const String& emailSubject,
													const String& bodyText,
													const StringArray& filesToAttach)
{
	HMODULE h = LoadLibraryA ("MAPI32.dll");

	MAPISendMailType mapiSendMail = (MAPISendMailType) GetProcAddress (h, "MAPISendMail");
	bool ok = false;

	if (mapiSendMail != 0)
	{
		MapiMessage message;
		zerostruct (message);
		message.lpszSubject = (LPSTR) emailSubject.toCString();
		message.lpszNoteText = (LPSTR) bodyText.toCString();

		MapiRecipDesc recip;
		zerostruct (recip);
		recip.ulRecipClass = MAPI_TO;
		String targetEmailAddress_ (targetEmailAddress);
		if (targetEmailAddress_.isEmpty())
			targetEmailAddress_ = " "; // (Windows Mail can't deal with a blank address)
		recip.lpszName = (LPSTR) targetEmailAddress_.toCString();
		message.nRecipCount = 1;
		message.lpRecips = &recip;

		HeapBlock <MapiFileDesc> files;
		files.calloc (filesToAttach.size());

		message.nFileCount = filesToAttach.size();
		message.lpFiles = files;

		for (int i = 0; i < filesToAttach.size(); ++i)
		{
			files[i].nPosition = (ULONG) -1;
			files[i].lpszPathName = (LPSTR) filesToAttach[i].toCString();
		}

		ok = (mapiSendMail (0, 0, &message, MAPI_DIALOG | MAPI_LOGON_UI, 0) == SUCCESS_SUCCESS);
	}

	FreeLibrary (h);
	return ok;
}

#endif
/*** End of inlined file: juce_win32_Network.cpp ***/


/*** Start of inlined file: juce_win32_PlatformUtils.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

static HKEY findKeyForPath (String name,
							const bool createForWriting,
							String& valueName)
{
	HKEY rootKey = 0;

	if (name.startsWithIgnoreCase ("HKEY_CURRENT_USER\\"))
		rootKey = HKEY_CURRENT_USER;
	else if (name.startsWithIgnoreCase ("HKEY_LOCAL_MACHINE\\"))
		rootKey = HKEY_LOCAL_MACHINE;
	else if (name.startsWithIgnoreCase ("HKEY_CLASSES_ROOT\\"))
		rootKey = HKEY_CLASSES_ROOT;

	if (rootKey != 0)
	{
		name = name.substring (name.indexOfChar ('\\') + 1);

		const int lastSlash = name.lastIndexOfChar ('\\');
		valueName = name.substring (lastSlash + 1);
		name = name.substring (0, lastSlash);

		HKEY key;
		DWORD result;

		if (createForWriting)
		{
			if (RegCreateKeyEx (rootKey, name, 0, 0, REG_OPTION_NON_VOLATILE,
								(KEY_WRITE | KEY_QUERY_VALUE), 0, &key, &result) == ERROR_SUCCESS)
				return key;
		}
		else
		{
			if (RegOpenKeyEx (rootKey, name, 0, KEY_READ, &key) == ERROR_SUCCESS)
				return key;
		}
	}

	return 0;
}

const String PlatformUtilities::getRegistryValue (const String& regValuePath,
												  const String& defaultValue)
{
	String valueName, result (defaultValue);
	HKEY k = findKeyForPath (regValuePath, false, valueName);

	if (k != 0)
	{
		WCHAR buffer [2048];
		unsigned long bufferSize = sizeof (buffer);
		DWORD type = REG_SZ;

		if (RegQueryValueEx (k, valueName, 0, &type, (LPBYTE) buffer, &bufferSize) == ERROR_SUCCESS)
		{
			if (type == REG_SZ)
				result = buffer;
			else if (type == REG_DWORD)
				result = String ((int) *(DWORD*) buffer);
		}

		RegCloseKey (k);
	}

	return result;
}

void PlatformUtilities::setRegistryValue (const String& regValuePath,
										  const String& value)
{
	String valueName;
	HKEY k = findKeyForPath (regValuePath, true, valueName);

	if (k != 0)
	{
		RegSetValueEx (k, valueName, 0, REG_SZ,
					   (const BYTE*) (const WCHAR*) value,
					   sizeof (WCHAR) * (value.length() + 1));

		RegCloseKey (k);
	}
}

bool PlatformUtilities::registryValueExists (const String& regValuePath)
{
	bool exists = false;
	String valueName;
	HKEY k = findKeyForPath (regValuePath, false, valueName);

	if (k != 0)
	{
		unsigned char buffer [2048];
		unsigned long bufferSize = sizeof (buffer);
		DWORD type = 0;

		if (RegQueryValueEx (k, valueName, 0, &type, buffer, &bufferSize) == ERROR_SUCCESS)
			exists = true;

		RegCloseKey (k);
	}

	return exists;
}

void PlatformUtilities::deleteRegistryValue (const String& regValuePath)
{
	String valueName;
	HKEY k = findKeyForPath (regValuePath, true, valueName);

	if (k != 0)
	{
		RegDeleteValue (k, valueName);
		RegCloseKey (k);
	}
}

void PlatformUtilities::deleteRegistryKey (const String& regKeyPath)
{
	String valueName;
	HKEY k = findKeyForPath (regKeyPath, true, valueName);

	if (k != 0)
	{
		RegDeleteKey (k, valueName);
		RegCloseKey (k);
	}
}

void PlatformUtilities::registerFileAssociation (const String& fileExtension,
												 const String& symbolicDescription,
												 const String& fullDescription,
												 const File& targetExecutable,
												 int iconResourceNumber)
{
	setRegistryValue ("HKEY_CLASSES_ROOT\\" + fileExtension + "\\", symbolicDescription);

	const String key ("HKEY_CLASSES_ROOT\\" + symbolicDescription);

	if (iconResourceNumber != 0)
		setRegistryValue (key + "\\DefaultIcon\\",
						  targetExecutable.getFullPathName() + "," + String (-iconResourceNumber));

	setRegistryValue (key + "\\", fullDescription);

	setRegistryValue (key + "\\shell\\open\\command\\",
					  targetExecutable.getFullPathName() + " %1");
}

bool juce_IsRunningInWine()
{
	HKEY key;
	if (RegOpenKeyEx (HKEY_CURRENT_USER, _T("Software\\Wine"), 0, KEY_READ, &key) == ERROR_SUCCESS)
	{
		RegCloseKey (key);
		return true;
	}

	return false;
}

const String JUCE_CALLTYPE PlatformUtilities::getCurrentCommandLineParams() throw()
{
	String s (::GetCommandLineW());

	StringArray tokens;
	tokens.addTokens (s, true); // tokenise so that we can remove the initial filename argument

	return tokens.joinIntoString (" ", 1);
}

static void* currentModuleHandle = 0;

void* PlatformUtilities::getCurrentModuleInstanceHandle() throw()
{
	if (currentModuleHandle == 0)
		currentModuleHandle = GetModuleHandle (0);

	return currentModuleHandle;
}

void PlatformUtilities::setCurrentModuleInstanceHandle (void* const newHandle) throw()
{
	currentModuleHandle = newHandle;
}

void PlatformUtilities::fpuReset()
{
#if JUCE_MSVC
	_clearfp();
#endif
}

void PlatformUtilities::beep()
{
	MessageBeep (MB_OK);
}

#endif
/*** End of inlined file: juce_win32_PlatformUtils.cpp ***/

#if ! JUCE_ONLY_BUILD_CORE_LIBRARY

/*** Start of inlined file: juce_win32_Messaging.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

static const unsigned int specialId		 = WM_APP + 0x4400;
static const unsigned int broadcastId	   = WM_APP + 0x4403;
static const unsigned int specialCallbackId	 = WM_APP + 0x4402;

static const TCHAR* const messageWindowName     = _T("JUCEWindow");

HWND juce_messageWindowHandle = 0;

extern long improbableWindowNumber; // defined in windowing.cpp

#ifndef WM_APPCOMMAND
 #define WM_APPCOMMAND 0x0319
#endif

static LRESULT CALLBACK juce_MessageWndProc (HWND h,
											 const UINT message,
											 const WPARAM wParam,
											 const LPARAM lParam) throw()
{
	JUCE_TRY
	{
		if (h == juce_messageWindowHandle)
		{
			if (message == specialCallbackId)
			{
				MessageCallbackFunction* const func = (MessageCallbackFunction*) wParam;
				return (LRESULT) (*func) ((void*) lParam);
			}
			else if (message == specialId)
			{
				// these are trapped early in the dispatch call, but must also be checked
				// here in case there are windows modal dialog boxes doing their own
				// dispatch loop and not calling our version

				MessageManager::getInstance()->deliverMessage ((void*) lParam);
				return 0;
			}
			else if (message == broadcastId)
			{
				const ScopedPointer <String> messageString ((String*) lParam);
				MessageManager::getInstance()->deliverBroadcastMessage (*messageString);
				return 0;
			}
			else if (message == WM_COPYDATA && ((const COPYDATASTRUCT*) lParam)->dwData == broadcastId)
			{
				const String messageString ((const juce_wchar*) ((const COPYDATASTRUCT*) lParam)->lpData,
											((const COPYDATASTRUCT*) lParam)->cbData / sizeof (juce_wchar));

				PostMessage (juce_messageWindowHandle, broadcastId, 0, (LPARAM) new String (messageString));
				return 0;
			}
		}
	}
	JUCE_CATCH_EXCEPTION

	return DefWindowProc (h, message, wParam, lParam);
}

static bool isEventBlockedByModalComps (MSG& m)
{
	if (Component::getNumCurrentlyModalComponents() == 0
		 || GetWindowLong (m.hwnd, GWLP_USERDATA) == improbableWindowNumber)
		return false;

	switch (m.message)
	{
		case WM_MOUSEMOVE:
		case WM_NCMOUSEMOVE:
		case 0x020A: /* WM_MOUSEWHEEL */
		case 0x020E: /* WM_MOUSEHWHEEL */
		case WM_KEYUP:
		case WM_SYSKEYUP:
		case WM_CHAR:
		case WM_APPCOMMAND:
		case WM_LBUTTONUP:
		case WM_MBUTTONUP:
		case WM_RBUTTONUP:
		case WM_MOUSEACTIVATE:
		case WM_NCMOUSEHOVER:
		case WM_MOUSEHOVER:
			return true;

		case WM_NCLBUTTONDOWN:
		case WM_NCLBUTTONDBLCLK:
		case WM_NCRBUTTONDOWN:
		case WM_NCRBUTTONDBLCLK:
		case WM_NCMBUTTONDOWN:
		case WM_NCMBUTTONDBLCLK:
		case WM_LBUTTONDOWN:
		case WM_LBUTTONDBLCLK:
		case WM_MBUTTONDOWN:
		case WM_MBUTTONDBLCLK:
		case WM_RBUTTONDOWN:
		case WM_RBUTTONDBLCLK:
		case WM_KEYDOWN:
		case WM_SYSKEYDOWN:
		{
			Component* const modal = Component::getCurrentlyModalComponent (0);
			if (modal != 0)
				modal->inputAttemptWhenModal();

			return true;
		}

		default:
			break;
	}

	return false;
}

bool juce_dispatchNextMessageOnSystemQueue (const bool returnIfNoPendingMessages)
{
	MSG m;

	if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, 0))
		return false;

	if (GetMessage (&m, (HWND) 0, 0, 0) >= 0)
	{
		if (m.message == specialId && m.hwnd == juce_messageWindowHandle)
		{
			MessageManager::getInstance()->deliverMessage ((void*) m.lParam);
		}
		else if (m.message == WM_QUIT)
		{
			if (JUCEApplication::getInstance())
				JUCEApplication::getInstance()->systemRequestedQuit();
		}
		else if (! isEventBlockedByModalComps (m))
		{
			if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
				 && GetWindowLong (m.hwnd, GWLP_USERDATA) != improbableWindowNumber)
			{
				// if it's someone else's window being clicked on, and the focus is
				// currently on a juce window, pass the kb focus over..
				HWND currentFocus = GetFocus();

				if (currentFocus == 0 || GetWindowLong (currentFocus, GWLP_USERDATA) == improbableWindowNumber)
					SetFocus (m.hwnd);
			}

			TranslateMessage (&m);
			DispatchMessage (&m);
		}
	}

	return true;
}

bool juce_postMessageToSystemQueue (void* message)
{
	return PostMessage (juce_messageWindowHandle, specialId, 0, (LPARAM) message) != 0;
}

void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
												   void* userData)
{
	if (MessageManager::getInstance()->isThisTheMessageThread())
	{
		return (*callback) (userData);
	}
	else
	{
		// If a thread has a MessageManagerLock and then tries to call this method, it'll
		// deadlock because the message manager is blocked from running, and can't
		// call your function..
		jassert (! MessageManager::getInstance()->currentThreadHasLockedMessageManager());

		return (void*) SendMessage (juce_messageWindowHandle,
									specialCallbackId,
									(WPARAM) callback,
									(LPARAM) userData);
	}
}

static BOOL CALLBACK BroadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
{
	if (hwnd != juce_messageWindowHandle)
		reinterpret_cast <VoidArray*> (lParam)->add ((void*) hwnd);

	return TRUE;
}

void MessageManager::broadcastMessage (const String& value) throw()
{
	VoidArray windows;
	EnumWindows (&BroadcastEnumWindowProc, (LPARAM) &windows);

	const String localCopy (value);

	COPYDATASTRUCT data;
	data.dwData = broadcastId;
	data.cbData = (localCopy.length() + 1) * sizeof (juce_wchar);
	data.lpData = (void*) static_cast <const juce_wchar*> (localCopy);

	for (int i = windows.size(); --i >= 0;)
	{
		HWND hwnd = (HWND) windows.getUnchecked(i);

		TCHAR windowName [64]; // no need to read longer strings than this
		GetWindowText (hwnd, windowName, 64);
		windowName [63] = 0;

		if (String (windowName) == String (messageWindowName))
		{
			DWORD_PTR result;
			SendMessageTimeout (hwnd, WM_COPYDATA,
								(WPARAM) juce_messageWindowHandle,
								(LPARAM) &data,
								SMTO_BLOCK | SMTO_ABORTIFHUNG,
								8000,
								&result);
		}
	}
}

static const String getMessageWindowClassName()
{
	// this name has to be different for each app/dll instance because otherwise
	// poor old Win32 can get a bit confused (even despite it not being a process-global
	// window class).

	static int number = 0;
	if (number == 0)
		number = 0x7fffffff & (int) Time::getHighResolutionTicks();

	return "JUCEcs_" + String (number);
}

void MessageManager::doPlatformSpecificInitialisation()
{
	OleInitialize (0);

	const String className (getMessageWindowClassName());

	HMODULE hmod = (HMODULE) PlatformUtilities::getCurrentModuleInstanceHandle();

	WNDCLASSEX wc;
	zerostruct (wc);

	wc.cbSize	 = sizeof (wc);
	wc.lpfnWndProc	= (WNDPROC) juce_MessageWndProc;
	wc.cbWndExtra	 = 4;
	wc.hInstance	  = hmod;
	wc.lpszClassName  = className;

	RegisterClassEx (&wc);

	juce_messageWindowHandle = CreateWindow (wc.lpszClassName,
											 messageWindowName,
											 0, 0, 0, 0, 0, 0, 0,
											 hmod, 0);
}

void MessageManager::doPlatformSpecificShutdown()
{
	DestroyWindow (juce_messageWindowHandle);
	UnregisterClass (getMessageWindowClassName(), 0);
	OleUninitialize();
}

#endif
/*** End of inlined file: juce_win32_Messaging.cpp ***/



/*** Start of inlined file: juce_win32_Windowing.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#undef GetSystemMetrics // multimon overrides this for some reason and causes a mess..

// these are in the windows SDK, but need to be repeated here for GCC..
#ifndef GET_APPCOMMAND_LPARAM
  #define FAPPCOMMAND_MASK		  0xF000
  #define GET_APPCOMMAND_LPARAM(lParam)	 ((short) (HIWORD (lParam) & ~FAPPCOMMAND_MASK))
  #define APPCOMMAND_MEDIA_NEXTTRACK	11
  #define APPCOMMAND_MEDIA_PREVIOUSTRACK	12
  #define APPCOMMAND_MEDIA_STOP		 13
  #define APPCOMMAND_MEDIA_PLAY_PAUSE	   14
  #define WM_APPCOMMAND			 0x0319
#endif

extern void juce_repeatLastProcessPriority(); // in juce_win32_Threads.cpp
extern void juce_CheckCurrentlyFocusedTopLevelWindow();  // in juce_TopLevelWindow.cpp
extern bool juce_IsRunningInWine();

#ifndef ULW_ALPHA
  #define ULW_ALPHA	 0x00000002
#endif

#ifndef AC_SRC_ALPHA
  #define AC_SRC_ALPHA  0x01
#endif

static HPALETTE palette = 0;
static bool createPaletteIfNeeded = true;
static bool shouldDeactivateTitleBar = true;

static HICON createHICONFromImage (const Image& image, const BOOL isIcon, int hotspotX, int hotspotY) throw();
#define WM_TRAYNOTIFY WM_USER + 100

using ::abs;

typedef BOOL (WINAPI* UpdateLayeredWinFunc) (HWND, HDC, POINT*, SIZE*, HDC, POINT*, COLORREF, BLENDFUNCTION*, DWORD);
static UpdateLayeredWinFunc updateLayeredWindow = 0;

bool Desktop::canUseSemiTransparentWindows() throw()
{
	if (updateLayeredWindow == 0)
	{
		if (! juce_IsRunningInWine())
		{
			HMODULE user32Mod = GetModuleHandle (_T("user32.dll"));
			updateLayeredWindow = (UpdateLayeredWinFunc) GetProcAddress (user32Mod, "UpdateLayeredWindow");
		}
	}

	return updateLayeredWindow != 0;
}

const int extendedKeyModifier		   = 0x10000;

const int KeyPress::spaceKey		= VK_SPACE;
const int KeyPress::returnKey		   = VK_RETURN;
const int KeyPress::escapeKey		   = VK_ESCAPE;
const int KeyPress::backspaceKey		= VK_BACK;
const int KeyPress::deleteKey		   = VK_DELETE	 | extendedKeyModifier;
const int KeyPress::insertKey		   = VK_INSERT	 | extendedKeyModifier;
const int KeyPress::tabKey		  = VK_TAB;
const int KeyPress::leftKey		 = VK_LEFT	   | extendedKeyModifier;
const int KeyPress::rightKey		= VK_RIGHT	  | extendedKeyModifier;
const int KeyPress::upKey		   = VK_UP		 | extendedKeyModifier;
const int KeyPress::downKey		 = VK_DOWN	   | extendedKeyModifier;
const int KeyPress::homeKey		 = VK_HOME	   | extendedKeyModifier;
const int KeyPress::endKey		  = VK_END		| extendedKeyModifier;
const int KeyPress::pageUpKey		   = VK_PRIOR	  | extendedKeyModifier;
const int KeyPress::pageDownKey		 = VK_NEXT	   | extendedKeyModifier;
const int KeyPress::F1Key		   = VK_F1		 | extendedKeyModifier;
const int KeyPress::F2Key		   = VK_F2		 | extendedKeyModifier;
const int KeyPress::F3Key		   = VK_F3		 | extendedKeyModifier;
const int KeyPress::F4Key		   = VK_F4		 | extendedKeyModifier;
const int KeyPress::F5Key		   = VK_F5		 | extendedKeyModifier;
const int KeyPress::F6Key		   = VK_F6		 | extendedKeyModifier;
const int KeyPress::F7Key		   = VK_F7		 | extendedKeyModifier;
const int KeyPress::F8Key		   = VK_F8		 | extendedKeyModifier;
const int KeyPress::F9Key		   = VK_F9		 | extendedKeyModifier;
const int KeyPress::F10Key		  = VK_F10		| extendedKeyModifier;
const int KeyPress::F11Key		  = VK_F11		| extendedKeyModifier;
const int KeyPress::F12Key		  = VK_F12		| extendedKeyModifier;
const int KeyPress::F13Key		  = VK_F13		| extendedKeyModifier;
const int KeyPress::F14Key		  = VK_F14		| extendedKeyModifier;
const int KeyPress::F15Key		  = VK_F15		| extendedKeyModifier;
const int KeyPress::F16Key		  = VK_F16		| extendedKeyModifier;
const int KeyPress::numberPad0		  = VK_NUMPAD0	| extendedKeyModifier;
const int KeyPress::numberPad1		  = VK_NUMPAD1	| extendedKeyModifier;
const int KeyPress::numberPad2		  = VK_NUMPAD2	| extendedKeyModifier;
const int KeyPress::numberPad3		  = VK_NUMPAD3	| extendedKeyModifier;
const int KeyPress::numberPad4		  = VK_NUMPAD4	| extendedKeyModifier;
const int KeyPress::numberPad5		  = VK_NUMPAD5	| extendedKeyModifier;
const int KeyPress::numberPad6		  = VK_NUMPAD6	| extendedKeyModifier;
const int KeyPress::numberPad7		  = VK_NUMPAD7	| extendedKeyModifier;
const int KeyPress::numberPad8		  = VK_NUMPAD8	| extendedKeyModifier;
const int KeyPress::numberPad9		  = VK_NUMPAD9	| extendedKeyModifier;
const int KeyPress::numberPadAdd		= VK_ADD		| extendedKeyModifier;
const int KeyPress::numberPadSubtract	   = VK_SUBTRACT	   | extendedKeyModifier;
const int KeyPress::numberPadMultiply	   = VK_MULTIPLY	   | extendedKeyModifier;
const int KeyPress::numberPadDivide	 = VK_DIVIDE	 | extendedKeyModifier;
const int KeyPress::numberPadSeparator	  = VK_SEPARATOR	  | extendedKeyModifier;
const int KeyPress::numberPadDecimalPoint   = VK_DECIMAL	| extendedKeyModifier;
const int KeyPress::numberPadEquals	 = 0x92 /*VK_OEM_NEC_EQUAL*/  | extendedKeyModifier;
const int KeyPress::numberPadDelete	 = VK_DELETE	 | extendedKeyModifier;
const int KeyPress::playKey		 = 0x30000;
const int KeyPress::stopKey		 = 0x30001;
const int KeyPress::fastForwardKey	  = 0x30002;
const int KeyPress::rewindKey		   = 0x30003;

class WindowsBitmapImage  : public Image
{
public:

	HBITMAP hBitmap;
	BITMAPV4HEADER bitmapInfo;
	HDC hdc;
	unsigned char* bitmapData;

	WindowsBitmapImage (const PixelFormat format_,
						const int w, const int h, const bool clearImage)
		: Image (format_, w, h)
	{
		jassert (format_ == RGB || format_ == ARGB);

		pixelStride = (format_ == RGB) ? 3 : 4;

		zerostruct (bitmapInfo);
		bitmapInfo.bV4Size = sizeof (BITMAPV4HEADER);
		bitmapInfo.bV4Width = w;
		bitmapInfo.bV4Height = h;
		bitmapInfo.bV4Planes = 1;
		bitmapInfo.bV4CSType = 1;
		bitmapInfo.bV4BitCount = (unsigned short) (pixelStride * 8);

		if (format_ == ARGB)
		{
			bitmapInfo.bV4AlphaMask	= 0xff000000;
			bitmapInfo.bV4RedMask	  = 0xff0000;
			bitmapInfo.bV4GreenMask	= 0xff00;
			bitmapInfo.bV4BlueMask	 = 0xff;
			bitmapInfo.bV4V4Compression	= BI_BITFIELDS;
		}
		else
		{
			bitmapInfo.bV4V4Compression	= BI_RGB;
		}

		lineStride = -((w * pixelStride + 3) & ~3);

		HDC dc = GetDC (0);
		hdc = CreateCompatibleDC (dc);
		ReleaseDC (0, dc);

		SetMapMode (hdc, MM_TEXT);

		hBitmap = CreateDIBSection (hdc,
									(BITMAPINFO*) &(bitmapInfo),
									DIB_RGB_COLORS,
									(void**) &bitmapData,
									0, 0);

		SelectObject (hdc, hBitmap);

		if (format_ == ARGB && clearImage)
			zeromem (bitmapData, abs (h * lineStride));

		imageData = bitmapData - (lineStride * (h - 1));
	}

	~WindowsBitmapImage()
	{
		DeleteDC (hdc);
		DeleteObject (hBitmap);
	}

	void blitToWindow (HWND hwnd, HDC dc, const bool transparent,
					   const int x, const int y,
					   const RectangleList& maskedRegion) throw()
	{
		static HDRAWDIB hdd = 0;
		static bool needToCreateDrawDib = true;

		if (needToCreateDrawDib)
		{
			needToCreateDrawDib = false;

			HDC dc = GetDC (0);
			const int n = GetDeviceCaps (dc, BITSPIXEL);
			ReleaseDC (0, dc);

			// only open if we're not palettised
			if (n > 8)
				hdd = DrawDibOpen();
		}

		if (createPaletteIfNeeded)
		{
			HDC dc = GetDC (0);
			const int n = GetDeviceCaps (dc, BITSPIXEL);
			ReleaseDC (0, dc);

			if (n <= 8)
				palette = CreateHalftonePalette (dc);

			createPaletteIfNeeded = false;
		}

		if (palette != 0)
		{
			SelectPalette (dc, palette, FALSE);
			RealizePalette (dc);
			SetStretchBltMode (dc, HALFTONE);
		}

		SetMapMode (dc, MM_TEXT);

		if (transparent)
		{
			POINT p, pos;
			SIZE size;

			RECT windowBounds;
			GetWindowRect (hwnd, &windowBounds);

			p.x = -x;
			p.y = -y;
			pos.x = windowBounds.left;
			pos.y = windowBounds.top;
			size.cx = windowBounds.right - windowBounds.left;
			size.cy = windowBounds.bottom - windowBounds.top;

			BLENDFUNCTION bf;
			bf.AlphaFormat = AC_SRC_ALPHA;
			bf.BlendFlags = 0;
			bf.BlendOp = AC_SRC_OVER;
			bf.SourceConstantAlpha = 0xff;

			if (! maskedRegion.isEmpty())
			{
				for (RectangleList::Iterator i (maskedRegion); i.next();)
				{
					const Rectangle<int>& r = *i.getRectangle();
					ExcludeClipRect (hdc, r.getX(), r.getY(), r.getRight(), r.getBottom());
				}
			}

			updateLayeredWindow (hwnd, 0, &pos, &size, hdc, &p, 0, &bf, ULW_ALPHA);
		}
		else
		{
			int savedDC = 0;

			if (! maskedRegion.isEmpty())
			{
				savedDC = SaveDC (dc);

				for (RectangleList::Iterator i (maskedRegion); i.next();)
				{
					const Rectangle<int>& r = *i.getRectangle();
					ExcludeClipRect (dc, r.getX(), r.getY(), r.getRight(), r.getBottom());
				}
			}

			const int w = getWidth();
			const int h = getHeight();

			if (hdd == 0)
			{
				StretchDIBits (dc,
							   x, y, w, h,
							   0, 0, w, h,
							   bitmapData, (const BITMAPINFO*) &bitmapInfo,
							   DIB_RGB_COLORS, SRCCOPY);
			}
			else
			{
				DrawDibDraw (hdd, dc, x, y, -1, -1,
							 (BITMAPINFOHEADER*) &bitmapInfo, bitmapData,
							 0, 0, w, h, 0);
			}

			if (! maskedRegion.isEmpty())
				RestoreDC (dc, savedDC);
		}
	}

	juce_UseDebuggingNewOperator

private:
	WindowsBitmapImage (const WindowsBitmapImage&);
	WindowsBitmapImage& operator= (const WindowsBitmapImage&);
};

long improbableWindowNumber = 0xf965aa01; // also referenced by messaging.cpp

bool KeyPress::isKeyCurrentlyDown (const int keyCode)
{
	SHORT k = (SHORT) keyCode;

	if ((keyCode & extendedKeyModifier) == 0
		 && (k >= (SHORT) 'a' && k <= (SHORT) 'z'))
		k += (SHORT) 'A' - (SHORT) 'a';

	const SHORT translatedValues[] = { (SHORT) ',', VK_OEM_COMMA,
									   (SHORT) '+', VK_OEM_PLUS,
									   (SHORT) '-', VK_OEM_MINUS,
									   (SHORT) '.', VK_OEM_PERIOD,
									   (SHORT) ';', VK_OEM_1,
									   (SHORT) ':', VK_OEM_1,
									   (SHORT) '/', VK_OEM_2,
									   (SHORT) '?', VK_OEM_2,
									   (SHORT) '[', VK_OEM_4,
									   (SHORT) ']', VK_OEM_6 };

	for (int i = 0; i < numElementsInArray (translatedValues); i += 2)
		if (k == translatedValues [i])
			k = translatedValues [i + 1];

	return (GetKeyState (k) & 0x8000) != 0;
}

static void* callFunctionIfNotLocked (MessageCallbackFunction* callback, void* userData)
{
	if (MessageManager::getInstance()->currentThreadHasLockedMessageManager())
		return callback (userData);
	else
		return MessageManager::getInstance()->callFunctionOnMessageThread (callback, userData);
}

class Win32ComponentPeer  : public ComponentPeer
{
public:

	Win32ComponentPeer (Component* const component,
						const int windowStyleFlags)
		: ComponentPeer (component, windowStyleFlags),
		  dontRepaint (false),
		  fullScreen (false),
		  isDragging (false),
		  isMouseOver (false),
		  hasCreatedCaret (false),
		  currentWindowIcon (0),
		  taskBarIcon (0),
		  dropTarget (0)
	{
		callFunctionIfNotLocked (&createWindowCallback, this);

		setTitle (component->getName());

		if ((windowStyleFlags & windowHasDropShadow) != 0
			 && Desktop::canUseSemiTransparentWindows())
		{
			shadower = component->getLookAndFeel().createDropShadowerForComponent (component);

			if (shadower != 0)
				shadower->setOwner (component);
		}
		else
		{
			shadower = 0;
		}
	}

	~Win32ComponentPeer()
	{
		setTaskBarIcon (0);
		deleteAndZero (shadower);

		// do this before the next bit to avoid messages arriving for this window
		// before it's destroyed
		SetWindowLongPtr (hwnd, GWLP_USERDATA, 0);

		callFunctionIfNotLocked (&destroyWindowCallback, (void*) hwnd);

		if (currentWindowIcon != 0)
			DestroyIcon (currentWindowIcon);

		if (dropTarget != 0)
		{
			dropTarget->Release();
			dropTarget = 0;
		}
	}

	void* getNativeHandle() const
	{
		return hwnd;
	}

	void setVisible (bool shouldBeVisible)
	{
		ShowWindow (hwnd, shouldBeVisible ? SW_SHOWNA : SW_HIDE);

		if (shouldBeVisible)
			InvalidateRect (hwnd, 0, 0);
		else
			lastPaintTime = 0;
	}

	void setTitle (const String& title)
	{
		SetWindowText (hwnd, title);
	}

	void setPosition (int x, int y)
	{
		offsetWithinParent (x, y);
		SetWindowPos (hwnd, 0,
					  x - windowBorder.getLeft(),
					  y - windowBorder.getTop(),
					  0, 0,
					  SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
	}

	void repaintNowIfTransparent()
	{
		if (isTransparent() && lastPaintTime > 0 && Time::getMillisecondCounter() > lastPaintTime + 30)
			handlePaintMessage();
	}

	void updateBorderSize()
	{
		WINDOWINFO info;
		info.cbSize = sizeof (info);

		if (GetWindowInfo (hwnd, &info))
		{
			windowBorder = BorderSize (info.rcClient.top - info.rcWindow.top,
									   info.rcClient.left - info.rcWindow.left,
									   info.rcWindow.bottom - info.rcClient.bottom,
									   info.rcWindow.right - info.rcClient.right);
		}
	}

	void setSize (int w, int h)
	{
		SetWindowPos (hwnd, 0, 0, 0,
					  w + windowBorder.getLeftAndRight(),
					  h + windowBorder.getTopAndBottom(),
					  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER);

		updateBorderSize();

		repaintNowIfTransparent();
	}

	void setBounds (int x, int y, int w, int h, bool isNowFullScreen)
	{
		fullScreen = isNowFullScreen;
		offsetWithinParent (x, y);

		SetWindowPos (hwnd, 0,
					  x - windowBorder.getLeft(),
					  y - windowBorder.getTop(),
					  w + windowBorder.getLeftAndRight(),
					  h + windowBorder.getTopAndBottom(),
					  SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);

		updateBorderSize();

		repaintNowIfTransparent();
	}

	const Rectangle<int> getBounds() const
	{
		RECT r;
		GetWindowRect (hwnd, &r);

		Rectangle<int> bounds (r.left, r.top, r.right - r.left, r.bottom - r.top);

		HWND parentH = GetParent (hwnd);
		if (parentH != 0)
		{
			GetWindowRect (parentH, &r);
			bounds.translate (-r.left, -r.top);
		}

		return windowBorder.subtractedFrom (bounds);
	}

	const Point<int> getScreenPosition() const
	{
		RECT r;
		GetWindowRect (hwnd, &r);
		return Point<int> (r.left + windowBorder.getLeft(),
						   r.top + windowBorder.getTop());
	}

	const Point<int> relativePositionToGlobal (const Point<int>& relativePosition)
	{
		return relativePosition + getScreenPosition();
	}

	const Point<int> globalPositionToRelative (const Point<int>& screenPosition)
	{
		return screenPosition - getScreenPosition();
	}

	void setMinimised (bool shouldBeMinimised)
	{
		if (shouldBeMinimised != isMinimised())
			ShowWindow (hwnd, shouldBeMinimised ? SW_MINIMIZE : SW_SHOWNORMAL);
	}

	bool isMinimised() const
	{
		WINDOWPLACEMENT wp;
		wp.length = sizeof (WINDOWPLACEMENT);
		GetWindowPlacement (hwnd, &wp);

		return wp.showCmd == SW_SHOWMINIMIZED;
	}

	void setFullScreen (bool shouldBeFullScreen)
	{
		setMinimised (false);

		if (fullScreen != shouldBeFullScreen)
		{
			fullScreen = shouldBeFullScreen;
			const Component::SafePointer<Component> deletionChecker (component);

			if (! fullScreen)
			{
				const Rectangle<int> boundsCopy (lastNonFullscreenBounds);

				if (hasTitleBar())
					ShowWindow (hwnd, SW_SHOWNORMAL);

				if (! boundsCopy.isEmpty())
				{
					setBounds (boundsCopy.getX(),
							   boundsCopy.getY(),
							   boundsCopy.getWidth(),
							   boundsCopy.getHeight(),
							   false);
				}
			}
			else
			{
				if (hasTitleBar())
					ShowWindow (hwnd, SW_SHOWMAXIMIZED);
				else
					SendMessageW (hwnd, WM_SETTINGCHANGE, 0, 0);
			}

			if (deletionChecker != 0)
				handleMovedOrResized();
		}
	}

	bool isFullScreen() const
	{
		if (! hasTitleBar())
			return fullScreen;

		WINDOWPLACEMENT wp;
		wp.length = sizeof (wp);
		GetWindowPlacement (hwnd, &wp);

		return wp.showCmd == SW_SHOWMAXIMIZED;
	}

	bool contains (const Point<int>& position, bool trueIfInAChildWindow) const
	{
		if (((unsigned int) position.getX()) >= (unsigned int) component->getWidth()
			 || ((unsigned int) position.getY()) >= (unsigned int) component->getHeight())
			return false;

		RECT r;
		GetWindowRect (hwnd, &r);

		POINT p;
		p.x = position.getX() + r.left + windowBorder.getLeft();
		p.y = position.getY() + r.top + windowBorder.getTop();

		HWND w = WindowFromPoint (p);
		return w == hwnd || (trueIfInAChildWindow && (IsChild (hwnd, w) != 0));
	}

	const BorderSize getFrameSize() const
	{
		return windowBorder;
	}

	bool setAlwaysOnTop (bool alwaysOnTop)
	{
		const bool oldDeactivate = shouldDeactivateTitleBar;
		shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);

		SetWindowPos (hwnd, alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
					  0, 0, 0, 0,
					  SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);

		shouldDeactivateTitleBar = oldDeactivate;

		if (shadower != 0)
			shadower->componentBroughtToFront (*component);

		return true;
	}

	void toFront (bool makeActive)
	{
		setMinimised (false);

		const bool oldDeactivate = shouldDeactivateTitleBar;
		shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);

		callFunctionIfNotLocked (makeActive ? &toFrontCallback1 : &toFrontCallback2, hwnd);

		shouldDeactivateTitleBar = oldDeactivate;

		if (! makeActive)
		{
			// in this case a broughttofront call won't have occured, so do it now..
			handleBroughtToFront();
		}
	}

	void toBehind (ComponentPeer* other)
	{
		Win32ComponentPeer* const otherPeer = dynamic_cast <Win32ComponentPeer*> (other);

		jassert (otherPeer != 0); // wrong type of window?

		if (otherPeer != 0)
		{
			setMinimised (false);

			// must be careful not to try to put a topmost window behind a normal one, or win32
			// promotes the normal one to be topmost!
			if (getComponent()->isAlwaysOnTop() == otherPeer->getComponent()->isAlwaysOnTop())
				SetWindowPos (hwnd, otherPeer->hwnd, 0, 0, 0, 0,
							  SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
			else if (otherPeer->getComponent()->isAlwaysOnTop())
				SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
							  SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
		}
	}

	bool isFocused() const
	{
		return callFunctionIfNotLocked (&getFocusCallback, 0) == (void*) hwnd;
	}

	void grabFocus()
	{
		const bool oldDeactivate = shouldDeactivateTitleBar;
		shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);

		callFunctionIfNotLocked (&setFocusCallback, hwnd);

		shouldDeactivateTitleBar = oldDeactivate;
	}

	void textInputRequired (const Point<int>&)
	{
		if (! hasCreatedCaret)
		{
			hasCreatedCaret = true;
			CreateCaret (hwnd, (HBITMAP) 1, 0, 0);
		}

		ShowCaret (hwnd);
		SetCaretPos (0, 0);
	}

	void repaint (int x, int y, int w, int h)
	{
		const RECT r = { x, y, x + w, y + h };
		InvalidateRect (hwnd, &r, FALSE);
	}

	void performAnyPendingRepaintsNow()
	{
		MSG m;
		if (component->isVisible() && PeekMessage (&m, hwnd, WM_PAINT, WM_PAINT, PM_REMOVE))
			DispatchMessage (&m);
	}

	static Win32ComponentPeer* getOwnerOfWindow (HWND h) throw()
	{
		if (h != 0 && GetWindowLongPtr (h, GWLP_USERDATA) == improbableWindowNumber)
			return (Win32ComponentPeer*) (pointer_sized_int) GetWindowLongPtr (h, 8);

		return 0;
	}

	void setTaskBarIcon (const Image* const image)
	{
		if (image != 0)
		{
			HICON hicon = createHICONFromImage (*image, TRUE, 0, 0);

			if (taskBarIcon == 0)
			{
				taskBarIcon = new NOTIFYICONDATA();
				taskBarIcon->cbSize = sizeof (NOTIFYICONDATA);
				taskBarIcon->hWnd = (HWND) hwnd;
				taskBarIcon->uID = (int) (pointer_sized_int) hwnd;
				taskBarIcon->uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
				taskBarIcon->uCallbackMessage = WM_TRAYNOTIFY;
				taskBarIcon->hIcon = hicon;
				taskBarIcon->szTip[0] = 0;

				Shell_NotifyIcon (NIM_ADD, taskBarIcon);
			}
			else
			{
				HICON oldIcon = taskBarIcon->hIcon;

				taskBarIcon->hIcon = hicon;
				taskBarIcon->uFlags = NIF_ICON;
				Shell_NotifyIcon (NIM_MODIFY, taskBarIcon);

				DestroyIcon (oldIcon);
			}

			DestroyIcon (hicon);
		}
		else if (taskBarIcon != 0)
		{
			taskBarIcon->uFlags = 0;
			Shell_NotifyIcon (NIM_DELETE, taskBarIcon);
			DestroyIcon (taskBarIcon->hIcon);
			deleteAndZero (taskBarIcon);
		}
	}

	void setTaskBarIconToolTip (const String& toolTip) const
	{
		if (taskBarIcon != 0)
		{
			taskBarIcon->uFlags = NIF_TIP;
			toolTip.copyToUnicode (taskBarIcon->szTip, sizeof (taskBarIcon->szTip) - 1);
			Shell_NotifyIcon (NIM_MODIFY, taskBarIcon);
		}
	}

	bool isInside (HWND h) const
	{
		return GetAncestor (hwnd, GA_ROOT) == h;
	}

	static void updateKeyModifiers() throw()
	{
		int keyMods = 0;
		if (GetKeyState (VK_SHIFT) & 0x8000)	keyMods |= ModifierKeys::shiftModifier;
		if (GetKeyState (VK_CONTROL) & 0x8000)  keyMods |= ModifierKeys::ctrlModifier;
		if (GetKeyState (VK_MENU) & 0x8000)	 keyMods |= ModifierKeys::altModifier;
		if (GetKeyState (VK_RMENU) & 0x8000)	keyMods &= ~(ModifierKeys::ctrlModifier | ModifierKeys::altModifier);

		currentModifiers = currentModifiers.withOnlyMouseButtons().withFlags (keyMods);
	}

	static void updateModifiersFromWParam (const WPARAM wParam)
	{
		int mouseMods = 0;
		if (wParam & MK_LBUTTON)   mouseMods |= ModifierKeys::leftButtonModifier;
		if (wParam & MK_RBUTTON)   mouseMods |= ModifierKeys::rightButtonModifier;
		if (wParam & MK_MBUTTON)   mouseMods |= ModifierKeys::middleButtonModifier;

		currentModifiers = currentModifiers.withoutMouseButtons().withFlags (mouseMods);
		updateKeyModifiers();
	}

	static int64 getMouseEventTime()
	{
		static int64 eventTimeOffset = 0;
		static DWORD lastMessageTime = 0;
		const DWORD thisMessageTime = GetMessageTime();

		if (thisMessageTime < lastMessageTime || lastMessageTime == 0)
		{
			lastMessageTime = thisMessageTime;
			eventTimeOffset = Time::currentTimeMillis() - thisMessageTime;
		}

		return eventTimeOffset + thisMessageTime;
	}

	juce_UseDebuggingNewOperator

	bool dontRepaint;

	static ModifierKeys currentModifiers;
	static ModifierKeys modifiersAtLastCallback;

private:
	HWND hwnd;
	DropShadower* shadower;
	bool fullScreen, isDragging, isMouseOver, hasCreatedCaret;
	BorderSize windowBorder;
	HICON currentWindowIcon;
	NOTIFYICONDATA* taskBarIcon;
	IDropTarget* dropTarget;

	class TemporaryImage	: public Timer
	{
	public:

		TemporaryImage()
			: image (0)
		{
		}

		~TemporaryImage()
		{
			delete image;
		}

		WindowsBitmapImage* getImage (const bool transparent, const int w, const int h) throw()
		{
			const Image::PixelFormat format = transparent ? Image::ARGB : Image::RGB;

			if (image == 0 || image->getWidth() < w || image->getHeight() < h || image->getFormat() != format)
			{
				delete image;
				image = new WindowsBitmapImage (format, (w + 31) & ~31, (h + 31) & ~31, false);
			}

			startTimer (3000);
			return image;
		}

		void timerCallback()
		{
			stopTimer();
			deleteAndZero (image);
		}

	private:
		WindowsBitmapImage* image;

		TemporaryImage (const TemporaryImage&);
		TemporaryImage& operator= (const TemporaryImage&);
	};

	TemporaryImage offscreenImageGenerator;

	class WindowClassHolder	: public DeletedAtShutdown
	{
	public:
		WindowClassHolder()
			: windowClassName ("JUCE_")
		{
			// this name has to be different for each app/dll instance because otherwise
			// poor old Win32 can get a bit confused (even despite it not being a process-global
			// window class).
			windowClassName << (int) (Time::currentTimeMillis() & 0x7fffffff);

			HINSTANCE moduleHandle = (HINSTANCE) PlatformUtilities::getCurrentModuleInstanceHandle();

			TCHAR moduleFile [1024];
			moduleFile[0] = 0;
			GetModuleFileName (moduleHandle, moduleFile, 1024);
			WORD iconNum = 0;

			WNDCLASSEX wcex;
			wcex.cbSize	 = sizeof (wcex);
			wcex.style	  = CS_OWNDC;
			wcex.lpfnWndProc	= (WNDPROC) windowProc;
			wcex.lpszClassName  = windowClassName;
			wcex.cbClsExtra	 = 0;
			wcex.cbWndExtra	 = 32;
			wcex.hInstance	  = moduleHandle;
			wcex.hIcon	  = ExtractAssociatedIcon (moduleHandle, moduleFile, &iconNum);
			iconNum = 1;
			wcex.hIconSm	= ExtractAssociatedIcon (moduleHandle, moduleFile, &iconNum);
			wcex.hCursor	= 0;
			wcex.hbrBackground  = 0;
			wcex.lpszMenuName   = 0;

			RegisterClassEx (&wcex);
		}

		~WindowClassHolder()
		{
			if (ComponentPeer::getNumPeers() == 0)
				UnregisterClass (windowClassName, (HINSTANCE) PlatformUtilities::getCurrentModuleInstanceHandle());

			clearSingletonInstance();
		}

		String windowClassName;

		juce_DeclareSingleton_SingleThreaded_Minimal (WindowClassHolder);
	};

	static void* createWindowCallback (void* userData)
	{
		static_cast <Win32ComponentPeer*> (userData)->createWindow();
		return 0;
	}

	void createWindow()
	{
		DWORD exstyle = WS_EX_ACCEPTFILES;
		DWORD type = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

		if (hasTitleBar())
		{
			type |= WS_OVERLAPPED;
			exstyle |= WS_EX_APPWINDOW;

			if ((styleFlags & windowHasCloseButton) != 0)
			{
				type |= WS_SYSMENU;
			}
			else
			{
				// annoyingly, windows won't let you have a min/max button without a close button
				jassert ((styleFlags & (windowHasMinimiseButton | windowHasMaximiseButton)) == 0);
			}

			if ((styleFlags & windowIsResizable) != 0)
				type |= WS_THICKFRAME;
		}
		else
		{
			type |= WS_POPUP | WS_SYSMENU;

			if ((styleFlags & windowAppearsOnTaskbar) == 0)
				exstyle |= WS_EX_TOOLWINDOW;
			else
				exstyle |= WS_EX_APPWINDOW;
		}

		if ((styleFlags & windowHasMinimiseButton) != 0)
			type |= WS_MINIMIZEBOX;

		if ((styleFlags & windowHasMaximiseButton) != 0)
			type |= WS_MAXIMIZEBOX;

		if ((styleFlags & windowIgnoresMouseClicks) != 0)
			exstyle |= WS_EX_TRANSPARENT;

		if ((styleFlags & windowIsSemiTransparent) != 0
			  && Desktop::canUseSemiTransparentWindows())
			exstyle |= WS_EX_LAYERED;

		hwnd = CreateWindowEx (exstyle, WindowClassHolder::getInstance()->windowClassName, L"", type, 0, 0, 0, 0, 0, 0, 0, 0);

		if (hwnd != 0)
		{
			SetWindowLongPtr (hwnd, 0, 0);
			SetWindowLongPtr (hwnd, 8, (LONG_PTR) this);
			SetWindowLongPtr (hwnd, GWLP_USERDATA, improbableWindowNumber);

			if (dropTarget == 0)
				dropTarget = new JuceDropTarget (this);

			RegisterDragDrop (hwnd, dropTarget);

			updateBorderSize();

			// Calling this function here is (for some reason) necessary to make Windows
			// correctly enable the menu items that we specify in the wm_initmenu message.
			GetSystemMenu (hwnd, false);
		}
		else
		{
			jassertfalse
		}
	}

	static void* destroyWindowCallback (void* handle)
	{
		RevokeDragDrop ((HWND) handle);
		DestroyWindow ((HWND) handle);
		return 0;
	}

	static void* toFrontCallback1 (void* h)
	{
		SetForegroundWindow ((HWND) h);
		return 0;
	}

	static void* toFrontCallback2 (void* h)
	{
		SetWindowPos ((HWND) h, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
		return 0;
	}

	static void* setFocusCallback (void* h)
	{
		SetFocus ((HWND) h);
		return 0;
	}

	static void* getFocusCallback (void*)
	{
		return GetFocus();
	}

	void offsetWithinParent (int& x, int& y) const
	{
		if (isTransparent())
		{
			HWND parentHwnd = GetParent (hwnd);

			if (parentHwnd != 0)
			{
				RECT parentRect;
				GetWindowRect (parentHwnd, &parentRect);
				x += parentRect.left;
				y += parentRect.top;
			}
		}
	}

	bool isTransparent() const
	{
		return (GetWindowLong (hwnd, GWL_EXSTYLE) & WS_EX_LAYERED) != 0;
	}

	inline bool hasTitleBar() const throw()	 { return (styleFlags & windowHasTitleBar) != 0; }

	void setIcon (const Image& newIcon)
	{
		HICON hicon = createHICONFromImage (newIcon, TRUE, 0, 0);

		if (hicon != 0)
		{
			SendMessage (hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
			SendMessage (hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);

			if (currentWindowIcon != 0)
				DestroyIcon (currentWindowIcon);

			currentWindowIcon = hicon;
		}
	}

	void handlePaintMessage()
	{
		HRGN rgn = CreateRectRgn (0, 0, 0, 0);
		const int regionType = GetUpdateRgn (hwnd, rgn, false);

		PAINTSTRUCT paintStruct;
		HDC dc = BeginPaint (hwnd, &paintStruct); // Note this can immediately generate a WM_NCPAINT
												  // message and become re-entrant, but that's OK

		// if something in a paint handler calls, e.g. a message box, this can become reentrant and
		// corrupt the image it's using to paint into, so do a check here.
		static bool reentrant = false;
		if (reentrant)
		{
			DeleteObject (rgn);
			EndPaint (hwnd, &paintStruct);
			return;
		}

		reentrant = true;

		// this is the rectangle to update..
		int x = paintStruct.rcPaint.left;
		int y = paintStruct.rcPaint.top;
		int w = paintStruct.rcPaint.right - x;
		int h = paintStruct.rcPaint.bottom - y;

		const bool transparent = isTransparent();

		if (transparent)
		{
			// it's not possible to have a transparent window with a title bar at the moment!
			jassert (! hasTitleBar());

			RECT r;
			GetWindowRect (hwnd, &r);
			x = y = 0;
			w = r.right - r.left;
			h = r.bottom - r.top;
		}

		if (w > 0 && h > 0)
		{
			clearMaskedRegion();

			WindowsBitmapImage* const offscreenImage = offscreenImageGenerator.getImage (transparent, w, h);

			RectangleList contextClip;

			bool needToPaintAll = true;

			if (regionType == COMPLEXREGION && ! transparent)
			{
				HRGN clipRgn = CreateRectRgnIndirect (&paintStruct.rcPaint);
				CombineRgn (rgn, rgn, clipRgn, RGN_AND);
				DeleteObject (clipRgn);

				char rgnData [8192];
				const DWORD res = GetRegionData (rgn, sizeof (rgnData), (RGNDATA*) rgnData);

				if (res > 0 && res <= sizeof (rgnData))
				{
					const RGNDATAHEADER* const hdr = &(((const RGNDATA*) rgnData)->rdh);

					if (hdr->iType == RDH_RECTANGLES
						 && hdr->rcBound.right - hdr->rcBound.left >= w
						 && hdr->rcBound.bottom - hdr->rcBound.top >= h)
					{
						needToPaintAll = false;

						const RECT* rects = (const RECT*) (rgnData + sizeof (RGNDATAHEADER));
						int num = ((RGNDATA*) rgnData)->rdh.nCount;

						while (--num >= 0)
						{
							// (need to move this one pixel to the left because of a win32 bug)
							const int cx = jmax (x, (int) rects->left - 1);
							const int cy = rects->top;
							const int cw = rects->right - cx;
							const int ch = rects->bottom - rects->top;

							if (cx + cw - x <= w && cy + ch - y <= h)
							{
								contextClip.addWithoutMerging (Rectangle<int> (cx - x, cy - y, cw, ch));
							}
							else
							{
								needToPaintAll = true;
								break;
							}

							++rects;
						}
					}
				}
			}

			if (needToPaintAll)
			{
				contextClip.clear();
				contextClip.addWithoutMerging (Rectangle<int> (0, 0, w, h));
			}

			if (transparent)
			{
				RectangleList::Iterator i (contextClip);

				while (i.next())
				{
					const Rectangle<int>& r = *i.getRectangle();
					offscreenImage->clear (r.getX(), r.getY(), r.getWidth(), r.getHeight());
				}
			}

			// if the component's not opaque, this won't draw properly unless the platform can support this
			jassert (Desktop::canUseSemiTransparentWindows() || component->isOpaque());

			updateCurrentModifiers();

			LowLevelGraphicsSoftwareRenderer context (*offscreenImage);
			context.clipToRectangleList (contextClip);
			context.setOrigin (-x, -y);

			handlePaint (context);

			if (! dontRepaint)
				offscreenImage->blitToWindow (hwnd, dc, transparent, x, y, maskedRegion);
		}

		DeleteObject (rgn);
		EndPaint (hwnd, &paintStruct);
		reentrant = false;

#ifndef JUCE_GCC  //xxx should add this fn for gcc..
		_fpreset(); // because some graphics cards can unmask FP exceptions
#endif

		lastPaintTime = Time::getMillisecondCounter();
	}

	void doMouseEvent (const Point<int>& position)
	{
		handleMouseEvent (0, position, currentModifiers, getMouseEventTime());
	}

	void doMouseMove (const Point<int>& position)
	{
		if (! isMouseOver)
		{
			isMouseOver = true;
			updateKeyModifiers();

			TRACKMOUSEEVENT tme;
			tme.cbSize = sizeof (tme);
			tme.dwFlags = TME_LEAVE;
			tme.hwndTrack = hwnd;
			tme.dwHoverTime = 0;

			if (! TrackMouseEvent (&tme))
				jassertfalse;

			Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
		}
		else if (! isDragging)
		{
			if (! contains (position, false))
				return;
		}

		// (Throttling the incoming queue of mouse-events seems to still be required in XP..)
		static uint32 lastMouseTime = 0;
		const uint32 now = Time::getMillisecondCounter();
		const int maxMouseMovesPerSecond = 60;

		if (now > lastMouseTime + 1000 / maxMouseMovesPerSecond)
		{
			lastMouseTime = now;
			doMouseEvent (position);
		}
	}

	void doMouseDown (const Point<int>& position, const WPARAM wParam)
	{
		if (GetCapture() != hwnd)
			SetCapture (hwnd);

		doMouseMove (position);

		updateModifiersFromWParam (wParam);
		isDragging = true;

		doMouseEvent (position);
	}

	void doMouseUp (const Point<int>& position, const WPARAM wParam)
	{
		updateModifiersFromWParam (wParam);
		isDragging = false;

		// release the mouse capture if the user has released all buttons
		if ((wParam & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON)) == 0 && hwnd == GetCapture())
			ReleaseCapture();

		doMouseEvent (position);
	}

	void doCaptureChanged()
	{
		if (isDragging)
			doMouseUp (getCurrentMousePos(), (WPARAM) 0);
	}

	void doMouseExit()
	{
		isMouseOver = false;
		doMouseEvent (getCurrentMousePos());
	}

	void doMouseWheel (const Point<int>& position, const WPARAM wParam, const bool isVertical)
	{
		updateKeyModifiers();

		const float amount = jlimit (-1000.0f, 1000.0f, 0.75f * (short) HIWORD (wParam));

		handleMouseWheel (0, position, getMouseEventTime(),
						  isVertical ? 0.0f : amount,
						  isVertical ? amount : 0.0f);
	}

	void sendModifierKeyChangeIfNeeded()
	{
		if (modifiersAtLastCallback != currentModifiers)
		{
			modifiersAtLastCallback = currentModifiers;
			handleModifierKeysChange();
		}
	}

	bool doKeyUp (const WPARAM key)
	{
		updateKeyModifiers();

		switch (key)
		{
			case VK_SHIFT:
			case VK_CONTROL:
			case VK_MENU:
			case VK_CAPITAL:
			case VK_LWIN:
			case VK_RWIN:
			case VK_APPS:
			case VK_NUMLOCK:
			case VK_SCROLL:
			case VK_LSHIFT:
			case VK_RSHIFT:
			case VK_LCONTROL:
			case VK_LMENU:
			case VK_RCONTROL:
			case VK_RMENU:
				sendModifierKeyChangeIfNeeded();
		}

		return handleKeyUpOrDown (false)
				|| Component::getCurrentlyModalComponent() != 0;
	}

	bool doKeyDown (const WPARAM key)
	{
		updateKeyModifiers();
		bool used = false;

		switch (key)
		{
			case VK_SHIFT:
			case VK_LSHIFT:
			case VK_RSHIFT:
			case VK_CONTROL:
			case VK_LCONTROL:
			case VK_RCONTROL:
			case VK_MENU:
			case VK_LMENU:
			case VK_RMENU:
			case VK_LWIN:
			case VK_RWIN:
			case VK_CAPITAL:
			case VK_NUMLOCK:
			case VK_SCROLL:
			case VK_APPS:
				sendModifierKeyChangeIfNeeded();
				break;

			case VK_LEFT:
			case VK_RIGHT:
			case VK_UP:
			case VK_DOWN:
			case VK_PRIOR:
			case VK_NEXT:
			case VK_HOME:
			case VK_END:
			case VK_DELETE:
			case VK_INSERT:
			case VK_F1:
			case VK_F2:
			case VK_F3:
			case VK_F4:
			case VK_F5:
			case VK_F6:
			case VK_F7:
			case VK_F8:
			case VK_F9:
			case VK_F10:
			case VK_F11:
			case VK_F12:
			case VK_F13:
			case VK_F14:
			case VK_F15:
			case VK_F16:
				used = handleKeyUpOrDown (true);
				used = handleKeyPress (extendedKeyModifier | (int) key, 0) || used;
				break;

			case VK_ADD:
			case VK_SUBTRACT:
			case VK_MULTIPLY:
			case VK_DIVIDE:
			case VK_SEPARATOR:
			case VK_DECIMAL:
				used = handleKeyUpOrDown (true);
				break;

			default:
				used = handleKeyUpOrDown (true);

				{
					MSG msg;

					if (! PeekMessage (&msg, hwnd, WM_CHAR, WM_DEADCHAR, PM_NOREMOVE))
					{
						// if there isn't a WM_CHAR or WM_DEADCHAR message pending, we need to
						// manually generate the key-press event that matches this key-down.

						const UINT keyChar = MapVirtualKey (key, 2);
						used = handleKeyPress ((int) LOWORD (keyChar), 0) || used;
					}
				}

				break;
		}

		if (Component::getCurrentlyModalComponent() != 0)
			used = true;

		return used;
	}

	bool doKeyChar (int key, const LPARAM flags)
	{
		updateKeyModifiers();

		juce_wchar textChar = (juce_wchar) key;

		const int virtualScanCode = (flags >> 16) & 0xff;

		if (key >= '0' && key <= '9')
		{
			switch (virtualScanCode)  // check for a numeric keypad scan-code
			{
			case 0x52:
			case 0x4f:
			case 0x50:
			case 0x51:
			case 0x4b:
			case 0x4c:
			case 0x4d:
			case 0x47:
			case 0x48:
			case 0x49:
				key = (key - '0') + KeyPress::numberPad0;
				break;
			default:
				break;
			}
		}
		else
		{
			// convert the scan code to an unmodified character code..
			const UINT virtualKey = MapVirtualKey (virtualScanCode, 1);
			UINT keyChar = MapVirtualKey (virtualKey, 2);

			keyChar = LOWORD (keyChar);

			if (keyChar != 0)
				key = (int) keyChar;

			// avoid sending junk text characters for some control-key combinations
			if (textChar < ' ' && currentModifiers.testFlags (ModifierKeys::ctrlModifier | ModifierKeys::altModifier))
				textChar = 0;
		}

		return handleKeyPress (key, textChar);
	}

	bool doAppCommand (const LPARAM lParam)
	{
		int key = 0;

		switch (GET_APPCOMMAND_LPARAM (lParam))
		{
		case APPCOMMAND_MEDIA_PLAY_PAUSE:
			key = KeyPress::playKey;
			break;

		case APPCOMMAND_MEDIA_STOP:
			key = KeyPress::stopKey;
			break;

		case APPCOMMAND_MEDIA_NEXTTRACK:
			key = KeyPress::fastForwardKey;
			break;

		case APPCOMMAND_MEDIA_PREVIOUSTRACK:
			key = KeyPress::rewindKey;
			break;
		}

		if (key != 0)
		{
			updateKeyModifiers();

			if (hwnd == GetActiveWindow())
			{
				handleKeyPress (key, 0);
				return true;
			}
		}

		return false;
	}

	class JuceDropTarget	: public IDropTarget
	{
	public:
		JuceDropTarget (Win32ComponentPeer* const owner_)
			: owner (owner_),
			  refCount (1)
		{
		}

		virtual ~JuceDropTarget()
		{
			jassert (refCount == 0);
		}

		HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
		{
			if (id == IID_IUnknown || id == IID_IDropTarget)
			{
				AddRef();
				*result = this;
				return S_OK;
			}

			*result = 0;
			return E_NOINTERFACE;
		}

		ULONG __stdcall AddRef()	{ return ++refCount; }
		ULONG __stdcall Release()   { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }

		HRESULT __stdcall DragEnter (IDataObject* pDataObject, DWORD /*grfKeyState*/, POINTL mousePos, DWORD* pdwEffect)
		{
			updateFileList (pDataObject);
			owner->handleFileDragMove (files, owner->globalPositionToRelative (Point<int> (mousePos.x, mousePos.y)));
			*pdwEffect = DROPEFFECT_COPY;
			return S_OK;
		}

		HRESULT __stdcall DragLeave()
		{
			owner->handleFileDragExit (files);
			return S_OK;
		}

		HRESULT __stdcall DragOver (DWORD /*grfKeyState*/, POINTL mousePos, DWORD* pdwEffect)
		{
			owner->handleFileDragMove (files, owner->globalPositionToRelative (Point<int> (mousePos.x, mousePos.y)));
			*pdwEffect = DROPEFFECT_COPY;
			return S_OK;
		}

		HRESULT __stdcall Drop (IDataObject* pDataObject, DWORD /*grfKeyState*/, POINTL mousePos, DWORD* pdwEffect)
		{
			updateFileList (pDataObject);
			owner->handleFileDragDrop (files, owner->globalPositionToRelative (Point<int> (mousePos.x, mousePos.y)));
			*pdwEffect = DROPEFFECT_COPY;
			return S_OK;
		}

	private:
		Win32ComponentPeer* const owner;
		int refCount;
		StringArray files;

		void updateFileList (IDataObject* const pDataObject)
		{
			files.clear();

			FORMATETC format = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
			STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };

			if (pDataObject->GetData (&format, &medium) == S_OK)
			{
				const SIZE_T totalLen = GlobalSize (medium.hGlobal);
				const LPDROPFILES pDropFiles = (const LPDROPFILES) GlobalLock (medium.hGlobal);
				unsigned int i = 0;

				if (pDropFiles->fWide)
				{
					const WCHAR* const fname = (WCHAR*) (((const char*) pDropFiles) + sizeof (DROPFILES));

					for (;;)
					{
						unsigned int len = 0;
						while (i + len < totalLen && fname [i + len] != 0)
							++len;

						if (len == 0)
							break;

						files.add (String (fname + i, len));
						i += len + 1;
					}
				}
				else
				{
					const char* const fname = ((const char*) pDropFiles) + sizeof (DROPFILES);

					for (;;)
					{
						unsigned int len = 0;
						while (i + len < totalLen && fname [i + len] != 0)
							++len;

						if (len == 0)
							break;

						files.add (String (fname + i, len));
						i += len + 1;
					}
				}

				GlobalUnlock (medium.hGlobal);
			}
		}

		JuceDropTarget (const JuceDropTarget&);
		JuceDropTarget& operator= (const JuceDropTarget&);
	};

	void doSettingChange()
	{
		Desktop::getInstance().refreshMonitorSizes();

		if (fullScreen && ! isMinimised())
		{
			const Rectangle<int> r (component->getParentMonitorArea());

			SetWindowPos (hwnd, 0,
						  r.getX(), r.getY(), r.getWidth(), r.getHeight(),
						  SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOSENDCHANGING);
		}
	}

public:
	static LRESULT CALLBACK windowProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
	{
		Win32ComponentPeer* const peer = getOwnerOfWindow (h);

		if (peer != 0)
			return peer->peerWindowProc (h, message, wParam, lParam);

		return DefWindowProcW (h, message, wParam, lParam);
	}

private:
	static const Point<int> getPointFromLParam (LPARAM lParam) throw()
	{
		return Point<int> (GET_X_LPARAM (lParam), GET_Y_LPARAM (lParam));
	}

	const Point<int> getCurrentMousePos() throw()
	{
		RECT wr;
		GetWindowRect (hwnd, &wr);
		const DWORD mp = GetMessagePos();

		return Point<int> (GET_X_LPARAM (mp) - wr.left - windowBorder.getLeft(),
						   GET_Y_LPARAM (mp) - wr.top - windowBorder.getTop());
	}

	LRESULT peerWindowProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
	{
		if (isValidPeer (this))
		{
			switch (message)
			{

				case WM_NCHITTEST:
					if ((styleFlags & windowIgnoresMouseClicks) != 0)
						return HTTRANSPARENT;

					if (hasTitleBar())
						break;

					return HTCLIENT;

				case WM_PAINT:
					handlePaintMessage();
					return 0;

				case WM_NCPAINT:
					if (wParam != 1)
						handlePaintMessage();

					if (hasTitleBar())
						break;

					return 0;

				case WM_ERASEBKGND:
				case WM_NCCALCSIZE:
					if (hasTitleBar())
						break;

					return 1;

				case WM_MOUSEMOVE:
					doMouseMove (getPointFromLParam (lParam));
					return 0;

				case WM_MOUSELEAVE:
					doMouseExit();
					return 0;

				case WM_LBUTTONDOWN:
				case WM_MBUTTONDOWN:
				case WM_RBUTTONDOWN:
					doMouseDown (getPointFromLParam (lParam), wParam);
					return 0;

				case WM_LBUTTONUP:
				case WM_MBUTTONUP:
				case WM_RBUTTONUP:
					doMouseUp (getPointFromLParam (lParam), wParam);
					return 0;

				case WM_CAPTURECHANGED:
					doCaptureChanged();
					return 0;

				case WM_NCMOUSEMOVE:
					if (hasTitleBar())
						break;

					return 0;

				case 0x020A: /* WM_MOUSEWHEEL */
					doMouseWheel (getCurrentMousePos(), wParam, true);
					return 0;

				case 0x020E: /* WM_MOUSEHWHEEL */
					doMouseWheel (getCurrentMousePos(), wParam, false);
					return 0;

				case WM_WINDOWPOSCHANGING:
					if ((styleFlags & (windowHasTitleBar | windowIsResizable)) == (windowHasTitleBar | windowIsResizable))
					{
						WINDOWPOS* const wp = (WINDOWPOS*) lParam;

						if ((wp->flags & (SWP_NOMOVE | SWP_NOSIZE)) != (SWP_NOMOVE | SWP_NOSIZE))
						{
							if (constrainer != 0)
							{
								const Rectangle<int> current (component->getX() - windowBorder.getLeft(),
															  component->getY() - windowBorder.getTop(),
															  component->getWidth() + windowBorder.getLeftAndRight(),
															  component->getHeight() + windowBorder.getTopAndBottom());

								Rectangle<int> pos (wp->x, wp->y, wp->cx, wp->cy);

								constrainer->checkBounds (pos, current,
														  Desktop::getInstance().getAllMonitorDisplayAreas().getBounds(),
														  pos.getY() != current.getY() && pos.getBottom() == current.getBottom(),
														  pos.getX() != current.getX() && pos.getRight() == current.getRight(),
														  pos.getY() == current.getY() && pos.getBottom() != current.getBottom(),
														  pos.getX() == current.getX() && pos.getRight() != current.getRight());
								wp->x = pos.getX();
								wp->y = pos.getY();
								wp->cx = pos.getWidth();
								wp->cy = pos.getHeight();
							}
						}
					}

					return 0;

				case WM_WINDOWPOSCHANGED:
					handleMovedOrResized();

					if (dontRepaint)
						break;  // needed for non-accelerated openGL windows to draw themselves correctly..

					return 0;

				case WM_KEYDOWN:
				case WM_SYSKEYDOWN:
					if (doKeyDown (wParam))
						return 0;

					break;

				case WM_KEYUP:
				case WM_SYSKEYUP:
					if (doKeyUp (wParam))
						return 0;

					break;

				case WM_CHAR:
					if (doKeyChar ((int) wParam, lParam))
						return 0;

					break;

				case WM_APPCOMMAND:
					if (doAppCommand (lParam))
						return TRUE;

					break;

				case WM_SETFOCUS:
					updateKeyModifiers();
					handleFocusGain();
					break;

				case WM_KILLFOCUS:
					if (hasCreatedCaret)
					{
						hasCreatedCaret = false;
						DestroyCaret();
					}

					handleFocusLoss();
					break;

				case WM_ACTIVATEAPP:
					// Windows does weird things to process priority when you swap apps,
					// so this forces an update when the app is brought to the front
					if (wParam != FALSE)
						juce_repeatLastProcessPriority();
					else
						Desktop::getInstance().setKioskModeComponent (0); // turn kiosk mode off if we lose focus

					juce_CheckCurrentlyFocusedTopLevelWindow();
					modifiersAtLastCallback = -1;
					return 0;

				case WM_ACTIVATE:
					if (LOWORD (wParam) == WA_ACTIVE || LOWORD (wParam) == WA_CLICKACTIVE)
					{
						modifiersAtLastCallback = -1;
						updateKeyModifiers();

						if (isMinimised())
						{
							component->repaint();
							handleMovedOrResized();

							if (! ComponentPeer::isValidPeer (this))
								return 0;
						}

						if (LOWORD (wParam) == WA_CLICKACTIVE
							 && component->isCurrentlyBlockedByAnotherModalComponent())
						{
							const Point<int> mousePos (component->getMouseXYRelative());
							Component* const underMouse = component->getComponentAt (mousePos.getX(), mousePos.getY());

							if (underMouse != 0 && underMouse->isCurrentlyBlockedByAnotherModalComponent())
								Component::getCurrentlyModalComponent()->inputAttemptWhenModal();

							return 0;
						}

						handleBroughtToFront();

						if (component->isCurrentlyBlockedByAnotherModalComponent())
							Component::getCurrentlyModalComponent()->toFront (true);

						return 0;
					}

					break;

				case WM_NCACTIVATE:
					// while a temporary window is being shown, prevent Windows from deactivating the
					// title bars of our main windows.
					if (wParam == 0 && ! shouldDeactivateTitleBar)
						wParam = TRUE; // change this and let it get passed to the DefWindowProc.

					break;

				case WM_MOUSEACTIVATE:
					if (! component->getMouseClickGrabsKeyboardFocus())
						return MA_NOACTIVATE;

					break;

				case WM_SHOWWINDOW:
					if (wParam != 0)
						handleBroughtToFront();

					break;

				case WM_CLOSE:
					if (! component->isCurrentlyBlockedByAnotherModalComponent())
						handleUserClosingWindow();

					return 0;

				case WM_QUERYENDSESSION:
					if (JUCEApplication::getInstance() != 0)
					{
						JUCEApplication::getInstance()->systemRequestedQuit();
						return MessageManager::getInstance()->hasStopMessageBeenSent();
					}
					return TRUE;

				case WM_TRAYNOTIFY:
					if (component->isCurrentlyBlockedByAnotherModalComponent())
					{
						if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN
							 || lParam == WM_LBUTTONDBLCLK || lParam == WM_LBUTTONDBLCLK)
						{
							Component* const current = Component::getCurrentlyModalComponent();

							if (current != 0)
								current->inputAttemptWhenModal();
						}
					}
					else
					{
						ModifierKeys eventMods (ModifierKeys::getCurrentModifiersRealtime());

						if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK)
							eventMods = eventMods.withFlags (ModifierKeys::leftButtonModifier);
						else if (lParam == WM_RBUTTONDOWN || lParam == WM_RBUTTONDBLCLK)
							eventMods = eventMods.withFlags (ModifierKeys::rightButtonModifier);
						else if (lParam == WM_LBUTTONUP || lParam == WM_RBUTTONUP)
							eventMods = eventMods.withoutMouseButtons();

						const MouseEvent e (Desktop::getInstance().getMainMouseSource(),
											Point<int>(), eventMods, component, getMouseEventTime(),
											Point<int>(), getMouseEventTime(), 1, false);

						if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN)
						{
							SetFocus (hwnd);
							SetForegroundWindow (hwnd);
							component->mouseDown (e);
						}
						else if (lParam == WM_LBUTTONUP || lParam == WM_RBUTTONUP)
						{
							component->mouseUp (e);
						}
						else if (lParam == WM_LBUTTONDBLCLK || lParam == WM_LBUTTONDBLCLK)
						{
							component->mouseDoubleClick (e);
						}
						else if (lParam == WM_MOUSEMOVE)
						{
							component->mouseMove (e);
						}
					}

					break;

				case WM_SYNCPAINT:
					return 0;

				case WM_PALETTECHANGED:
					InvalidateRect (h, 0, 0);
					break;

				case WM_DISPLAYCHANGE:
					InvalidateRect (h, 0, 0);
					createPaletteIfNeeded = true;
					// intentional fall-through...
				case WM_SETTINGCHANGE:  // note the fall-through in the previous case!
					doSettingChange();
					break;

				case WM_INITMENU:
					if (! hasTitleBar())
					{
						if (isFullScreen())
						{
							EnableMenuItem ((HMENU) wParam, SC_RESTORE, MF_BYCOMMAND | MF_ENABLED);
							EnableMenuItem ((HMENU) wParam, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
						}
						else if (! isMinimised())
						{
							EnableMenuItem ((HMENU) wParam, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
						}
					}
					break;

				case WM_SYSCOMMAND:
					switch (wParam & 0xfff0)
					{
					case SC_CLOSE:
						if (sendInputAttemptWhenModalMessage())
							return 0;

						if (hasTitleBar())
						{
							PostMessage (h, WM_CLOSE, 0, 0);
							return 0;
						}
						break;

					case SC_KEYMENU:
						// (NB mustn't call sendInputAttemptWhenModalMessage() here because of very
						// obscure situations that can arise if a modal loop is started from an alt-key
						// keypress).

						if (hasTitleBar() && h == GetCapture())
							ReleaseCapture();

						break;

					case SC_MAXIMIZE:
						if (sendInputAttemptWhenModalMessage())
							return 0;

						setFullScreen (true);
						return 0;

					case SC_MINIMIZE:
						if (sendInputAttemptWhenModalMessage())
							return 0;

						if (! hasTitleBar())
						{
							setMinimised (true);
							return 0;
						}
						break;

					case SC_RESTORE:
						if (sendInputAttemptWhenModalMessage())
							return 0;

						if (hasTitleBar())
						{
							if (isFullScreen())
							{
								setFullScreen (false);
								return 0;
							}
						}
						else
						{
							if (isMinimised())
								setMinimised (false);
							else if (isFullScreen())
								setFullScreen (false);

							return 0;
						}

						break;
					}

					break;

				case WM_NCLBUTTONDOWN:
				case WM_NCRBUTTONDOWN:
				case WM_NCMBUTTONDOWN:
					sendInputAttemptWhenModalMessage();
					break;

				//case WM_IME_STARTCOMPOSITION;
				  //  return 0;

				case WM_GETDLGCODE:
					return DLGC_WANTALLKEYS;

				default:
					break;
			}
		}

		return DefWindowProcW (h, message, wParam, lParam);
	}

	bool sendInputAttemptWhenModalMessage()
	{
		if (component->isCurrentlyBlockedByAnotherModalComponent())
		{
			Component* const current = Component::getCurrentlyModalComponent();

			if (current != 0)
				current->inputAttemptWhenModal();

			return true;
		}

		return false;
	}

	Win32ComponentPeer (const Win32ComponentPeer&);
	Win32ComponentPeer& operator= (const Win32ComponentPeer&);
};

ModifierKeys Win32ComponentPeer::currentModifiers;
ModifierKeys Win32ComponentPeer::modifiersAtLastCallback;

ComponentPeer* Component::createNewPeer (int styleFlags, void* /*nativeWindowToAttachTo*/)
{
	return new Win32ComponentPeer (this, styleFlags);
}

juce_ImplementSingleton_SingleThreaded (Win32ComponentPeer::WindowClassHolder);

void ModifierKeys::updateCurrentModifiers() throw()
{
	currentModifiers = Win32ComponentPeer::currentModifiers;
}

const ModifierKeys ModifierKeys::getCurrentModifiersRealtime() throw()
{
	Win32ComponentPeer::updateKeyModifiers();

	int keyMods = 0;
	if ((GetKeyState (VK_LBUTTON) & 0x8000) != 0)   keyMods |= ModifierKeys::leftButtonModifier;
	if ((GetKeyState (VK_RBUTTON) & 0x8000) != 0)   keyMods |= ModifierKeys::rightButtonModifier;
	if ((GetKeyState (VK_MBUTTON) & 0x8000) != 0)   keyMods |= ModifierKeys::middleButtonModifier;

	Win32ComponentPeer::currentModifiers
		= Win32ComponentPeer::currentModifiers.withOnlyMouseButtons().withFlags (keyMods);

	return Win32ComponentPeer::currentModifiers;
}

void SystemTrayIconComponent::setIconImage (const Image& newImage)
{
	Win32ComponentPeer* const wp = dynamic_cast <Win32ComponentPeer*> (getPeer());

	if (wp != 0)
		wp->setTaskBarIcon (&newImage);
}

void SystemTrayIconComponent::setIconTooltip (const String& tooltip)
{
	Win32ComponentPeer* const wp = dynamic_cast <Win32ComponentPeer*> (getPeer());

	if (wp != 0)
		wp->setTaskBarIconToolTip (tooltip);
}

void juce_setWindowStyleBit (HWND h, const int styleType, const int feature, const bool bitIsSet) throw()
{
	DWORD val = GetWindowLong (h, styleType);

	if (bitIsSet)
		val |= feature;
	else
		val &= ~feature;

	SetWindowLongPtr (h, styleType, val);
	SetWindowPos (h, 0, 0, 0, 0, 0,
				  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER
				   | SWP_NOOWNERZORDER | SWP_FRAMECHANGED | SWP_NOSENDCHANGING);
}

bool Process::isForegroundProcess()
{
	HWND fg = GetForegroundWindow();

	if (fg == 0)
		return true;

	// when running as a plugin in IE8, the browser UI runs in a different process to the plugin, so
	// process ID isn't a reliable way to check if the foreground window belongs to us - instead, we
	// have to see if any of our windows are children of the foreground window
	fg = GetAncestor (fg, GA_ROOT);

	for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
	{
		Win32ComponentPeer* const wp = dynamic_cast <Win32ComponentPeer*> (ComponentPeer::getPeer (i));

		if (wp != 0 && wp->isInside (fg))
			return true;
	}

	return false;
}

bool AlertWindow::showNativeDialogBox (const String& title,
									   const String& bodyText,
									   bool isOkCancel)
{
	return MessageBox (0, bodyText, title,
					   MB_SETFOREGROUND | (isOkCancel ? MB_OKCANCEL
													  : MB_OK)) == IDOK;
}

void Desktop::createMouseInputSources()
{
	mouseSources.add (new MouseInputSource (0, true));
}

const Point<int> Desktop::getMousePosition()
{
	POINT mousePos;
	GetCursorPos (&mousePos);
	return Point<int> (mousePos.x, mousePos.y);
}

void Desktop::setMousePosition (const Point<int>& newPosition)
{
	SetCursorPos (newPosition.getX(), newPosition.getY());
}

Image* Image::createNativeImage (const PixelFormat format, const int imageWidth, const int imageHeight, const bool clearImage)
{
	return new Image (format, imageWidth, imageHeight, clearImage);
}

class ScreenSaverDefeater   : public Timer,
							  public DeletedAtShutdown
{
public:
	ScreenSaverDefeater() throw()
	{
		startTimer (10000);
		timerCallback();
	}

	~ScreenSaverDefeater() {}

	void timerCallback()
	{
		if (Process::isForegroundProcess())
		{
			// simulate a shift key getting pressed..
			INPUT input[2];
			input[0].type = INPUT_KEYBOARD;
			input[0].ki.wVk = VK_SHIFT;
			input[0].ki.dwFlags = 0;
			input[0].ki.dwExtraInfo = 0;

			input[1].type = INPUT_KEYBOARD;
			input[1].ki.wVk = VK_SHIFT;
			input[1].ki.dwFlags = KEYEVENTF_KEYUP;
			input[1].ki.dwExtraInfo = 0;

			SendInput (2, input, sizeof (INPUT));
		}
	}
};

static ScreenSaverDefeater* screenSaverDefeater = 0;

void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
{
	if (isEnabled)
	{
		deleteAndZero (screenSaverDefeater);
	}
	else if (screenSaverDefeater == 0)
	{
		screenSaverDefeater = new ScreenSaverDefeater();
	}
}

bool Desktop::isScreenSaverEnabled() throw()
{
	return screenSaverDefeater == 0;
}

void juce_setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool /*allowMenusAndBars*/)
{
	if (enableOrDisable)
		kioskModeComponent->setBounds (Desktop::getInstance().getMainMonitorArea (false));
}

static BOOL CALLBACK enumMonitorsProc (HMONITOR, HDC, LPRECT r, LPARAM userInfo)
{
	Array <Rectangle<int> >* const monitorCoords = (Array <Rectangle<int> >*) userInfo;

	monitorCoords->add (Rectangle<int> (r->left, r->top, r->right - r->left, r->bottom - r->top));

	return TRUE;
}

void juce_updateMultiMonitorInfo (Array <Rectangle<int> >& monitorCoords, const bool clipToWorkArea)
{
	EnumDisplayMonitors (0, 0, &enumMonitorsProc, (LPARAM) &monitorCoords);

	// make sure the first in the list is the main monitor
	for (int i = 1; i < monitorCoords.size(); ++i)
		if (monitorCoords[i].getX() == 0 && monitorCoords[i].getY() == 0)
			monitorCoords.swap (i, 0);

	if (monitorCoords.size() == 0)
	{
		RECT r;
		GetWindowRect (GetDesktopWindow(), &r);

		monitorCoords.add (Rectangle<int> (r.left, r.top, r.right - r.left, r.bottom - r.top));
	}

	if (clipToWorkArea)
	{
		// clip the main monitor to the active non-taskbar area
		RECT r;
		SystemParametersInfo (SPI_GETWORKAREA, 0, &r, 0);

		Rectangle<int>& screen = monitorCoords.getReference (0);

		screen.setPosition (jmax (screen.getX(), (int) r.left),
							jmax (screen.getY(), (int) r.top));

		screen.setSize (jmin (screen.getRight(), (int) r.right) - screen.getX(),
						jmin (screen.getBottom(), (int) r.bottom) - screen.getY());
	}
}

static Image* createImageFromHBITMAP (HBITMAP bitmap) throw()
{
	Image* im = 0;

	if (bitmap != 0)
	{
		BITMAP bm;

		if (GetObject (bitmap, sizeof (BITMAP), &bm)
			 && bm.bmWidth > 0 && bm.bmHeight > 0)
		{
			HDC tempDC = GetDC (0);
			HDC dc = CreateCompatibleDC (tempDC);
			ReleaseDC (0, tempDC);

			SelectObject (dc, bitmap);

			im = new Image (Image::ARGB, bm.bmWidth, bm.bmHeight, true);

			for (int y = bm.bmHeight; --y >= 0;)
			{
				for (int x = bm.bmWidth; --x >= 0;)
				{
					COLORREF col = GetPixel (dc, x, y);

					im->setPixelAt (x, y, Colour ((uint8) GetRValue (col),
												  (uint8) GetGValue (col),
												  (uint8) GetBValue (col)));
				}
			}

			DeleteDC (dc);
		}
	}

	return im;
}

static Image* createImageFromHICON (HICON icon) throw()
{
	ICONINFO info;

	if (GetIconInfo (icon, &info))
	{
		Image* const mask = createImageFromHBITMAP (info.hbmMask);

		if (mask == 0)
			return 0;

		Image* const image = createImageFromHBITMAP (info.hbmColor);

		if (image == 0)
			return mask;

		for (int y = image->getHeight(); --y >= 0;)
		{
			for (int x = image->getWidth(); --x >= 0;)
			{
				const float brightness = mask->getPixelAt (x, y).getBrightness();

				if (brightness > 0.0f)
					image->multiplyAlphaAt (x, y, 1.0f - brightness);
			}
		}

		delete mask;
		return image;
	}

	return 0;
}

static HICON createHICONFromImage (const Image& image, const BOOL isIcon, int hotspotX, int hotspotY) throw()
{
	HBITMAP mask = CreateBitmap (image.getWidth(), image.getHeight(), 1, 1, 0);

	ICONINFO info;
	info.fIcon = isIcon;
	info.xHotspot = hotspotX;
	info.yHotspot = hotspotY;
	info.hbmMask = mask;
	HICON hi = 0;

	if (SystemStats::getOperatingSystemType() >= SystemStats::WinXP)
	{
		WindowsBitmapImage bitmap (Image::ARGB, image.getWidth(), image.getHeight(), true);
		Graphics g (bitmap);
		g.drawImageAt (&image, 0, 0);

		info.hbmColor = bitmap.hBitmap;
		hi = CreateIconIndirect (&info);
	}
	else
	{
		HBITMAP colour = CreateCompatibleBitmap (GetDC (0), image.getWidth(), image.getHeight());

		HDC colDC = CreateCompatibleDC (GetDC (0));
		HDC alphaDC = CreateCompatibleDC (GetDC (0));
		SelectObject (colDC, colour);
		SelectObject (alphaDC, mask);

		for (int y = image.getHeight(); --y >= 0;)
		{
			for (int x = image.getWidth(); --x >= 0;)
			{
				const Colour c (image.getPixelAt (x, y));

				SetPixel (colDC, x, y, COLORREF (c.getRed() | (c.getGreen() << 8) | (c.getBlue() << 16)));
				SetPixel (alphaDC, x, y, COLORREF (0xffffff - (c.getAlpha() | (c.getAlpha() << 8) | (c.getAlpha() << 16))));
			}
		}

		DeleteDC (colDC);
		DeleteDC (alphaDC);

		info.hbmColor = colour;
		hi = CreateIconIndirect (&info);
		DeleteObject (colour);
	}

	DeleteObject (mask);
	return hi;
}

Image* juce_createIconForFile (const File& file)
{
	Image* image = 0;

	WCHAR filename [1024];
	file.getFullPathName().copyToUnicode (filename, 1023);
	WORD iconNum = 0;

	HICON icon = ExtractAssociatedIcon ((HINSTANCE) PlatformUtilities::getCurrentModuleInstanceHandle(),
										filename, &iconNum);

	if (icon != 0)
	{
		image = createImageFromHICON (icon);
		DestroyIcon (icon);
	}

	return image;
}

void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY)
{
	const int maxW = GetSystemMetrics (SM_CXCURSOR);
	const int maxH = GetSystemMetrics (SM_CYCURSOR);

	const Image* im = &image;
	Image* newIm = 0;

	if (image.getWidth() > maxW || image.getHeight() > maxH)
	{
		im = newIm = image.createCopy (maxW, maxH);

		hotspotX = (hotspotX * maxW) / image.getWidth();
		hotspotY = (hotspotY * maxH) / image.getHeight();
	}

	void* cursorH = 0;

	const SystemStats::OperatingSystemType os = SystemStats::getOperatingSystemType();

	if (os == SystemStats::WinXP)
	{
		cursorH = createHICONFromImage (*im, FALSE, hotspotX, hotspotY);
	}
	else
	{
		const int stride = (maxW + 7) >> 3;
		HeapBlock <uint8> andPlane, xorPlane;
		andPlane.calloc (stride * maxH);
		xorPlane.calloc (stride * maxH);
		int index = 0;

		for (int y = 0; y < maxH; ++y)
		{
			for (int x = 0; x < maxW; ++x)
			{
				const unsigned char bit = (unsigned char) (1 << (7 - (x & 7)));

				const Colour pixelColour (im->getPixelAt (x, y));

				if (pixelColour.getAlpha() < 127)
					andPlane [index + (x >> 3)] |= bit;
				else if (pixelColour.getBrightness() >= 0.5f)
					xorPlane [index + (x >> 3)] |= bit;
			}

			index += stride;
		}

		cursorH = CreateCursor (0, hotspotX, hotspotY, maxW, maxH, andPlane, xorPlane);
	}

	delete newIm;
	return cursorH;
}

void juce_deleteMouseCursor (void* const cursorHandle, const bool isStandard)
{
	if (cursorHandle != 0 && ! isStandard)
		DestroyCursor ((HCURSOR) cursorHandle);
}

void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type)
{
	LPCTSTR cursorName = IDC_ARROW;

	switch (type)
	{
	case MouseCursor::NormalCursor:
		cursorName = IDC_ARROW;
		break;

	case MouseCursor::NoCursor:
		return 0;

	case MouseCursor::DraggingHandCursor:
	{
		static void* dragHandCursor = 0;

		if (dragHandCursor == 0)
		{
			static const unsigned char dragHandData[] =
				{ 71,73,70,56,57,97,16,0,16,0,145,2,0,0,0,0,255,255,255,0,0,0,0,0,0,33,249,4,1,0,0,2,0,44,0,0,0,0,16,0,
				  16,0,0,2,52,148,47,0,200,185,16,130,90,12,74,139,107,84,123,39, 132,117,151,116,132,146,248,60,209,138,
				  98,22,203,114,34,236,37,52,77,217,247,154,191,119,110,240,193,128,193,95,163,56,60,234,98,135,2,0,59 };

			const ScopedPointer <Image> image (ImageFileFormat::loadFrom ((const char*) dragHandData, sizeof (dragHandData)));
			dragHandCursor = juce_createMouseCursorFromImage (*image, 8, 7);
		}

		return dragHandCursor;
	}

	case MouseCursor::WaitCursor:
		cursorName = IDC_WAIT;
		break;

	case MouseCursor::IBeamCursor:
		cursorName = IDC_IBEAM;
		break;

	case MouseCursor::PointingHandCursor:
		cursorName = MAKEINTRESOURCE(32649);
		break;

	case MouseCursor::LeftRightResizeCursor:
	case MouseCursor::LeftEdgeResizeCursor:
	case MouseCursor::RightEdgeResizeCursor:
		cursorName = IDC_SIZEWE;
		break;

	case MouseCursor::UpDownResizeCursor:
	case MouseCursor::TopEdgeResizeCursor:
	case MouseCursor::BottomEdgeResizeCursor:
		cursorName = IDC_SIZENS;
		break;

	case MouseCursor::TopLeftCornerResizeCursor:
	case MouseCursor::BottomRightCornerResizeCursor:
		cursorName = IDC_SIZENWSE;
		break;

	case MouseCursor::TopRightCornerResizeCursor:
	case MouseCursor::BottomLeftCornerResizeCursor:
		cursorName = IDC_SIZENESW;
		break;

	case MouseCursor::UpDownLeftRightResizeCursor:
		cursorName = IDC_SIZEALL;
		break;

	case MouseCursor::CrosshairCursor:
		cursorName = IDC_CROSS;
		break;

	case MouseCursor::CopyingCursor:
		// can't seem to find one of these in the win32 list..
		break;
	}

	HCURSOR cursorH = LoadCursor (0, cursorName);

	if (cursorH == 0)
		cursorH = LoadCursor (0, IDC_ARROW);

	return cursorH;
}

void MouseCursor::showInWindow (ComponentPeer*) const
{
	SetCursor ((HCURSOR) getHandle());
}

void MouseCursor::showInAllWindows() const
{
	showInWindow (0);
}

class JuceDropSource   : public IDropSource
{
	int refCount;

public:
	JuceDropSource()
		: refCount (1)
	{
	}

	virtual ~JuceDropSource()
	{
		jassert (refCount == 0);
	}

	HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
	{
		if (id == IID_IUnknown || id == IID_IDropSource)
		{
			AddRef();
			*result = this;
			return S_OK;
		}

		*result = 0;
		return E_NOINTERFACE;
	}

	ULONG __stdcall AddRef()	{ return ++refCount; }
	ULONG __stdcall Release()   { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }

	HRESULT __stdcall QueryContinueDrag (BOOL escapePressed, DWORD keys)
	{
		if (escapePressed)
			return DRAGDROP_S_CANCEL;

		if ((keys & (MK_LBUTTON | MK_RBUTTON)) == 0)
			return DRAGDROP_S_DROP;

		return S_OK;
	}

	HRESULT __stdcall GiveFeedback (DWORD)
	{
		return DRAGDROP_S_USEDEFAULTCURSORS;
	}
};

class JuceEnumFormatEtc   : public IEnumFORMATETC
{
public:
	JuceEnumFormatEtc (const FORMATETC* const format_)
		: refCount (1),
		  format (format_),
		  index (0)
	{
	}

	virtual ~JuceEnumFormatEtc()
	{
		jassert (refCount == 0);
	}

	HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
	{
		if (id == IID_IUnknown || id == IID_IEnumFORMATETC)
		{
			AddRef();
			*result = this;
			return S_OK;
		}

		*result = 0;
		return E_NOINTERFACE;
	}

	ULONG __stdcall AddRef()	{ return ++refCount; }
	ULONG __stdcall Release()   { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }

	HRESULT __stdcall Clone (IEnumFORMATETC** result)
	{
		if (result == 0)
			return E_POINTER;

		JuceEnumFormatEtc* const newOne = new JuceEnumFormatEtc (format);
		newOne->index = index;

		*result = newOne;
		return S_OK;
	}

	HRESULT __stdcall Next (ULONG celt, LPFORMATETC lpFormatEtc, ULONG* pceltFetched)
	{
		if (pceltFetched != 0)
			*pceltFetched = 0;
		else if (celt != 1)
			return S_FALSE;

		if (index == 0 && celt > 0 && lpFormatEtc != 0)
		{
			copyFormatEtc (lpFormatEtc [0], *format);
			++index;

			if (pceltFetched != 0)
				*pceltFetched = 1;

			return S_OK;
		}

		return S_FALSE;
	}

	HRESULT __stdcall Skip (ULONG celt)
	{
		if (index + (int) celt >= 1)
			return S_FALSE;

		index += celt;
		return S_OK;
	}

	HRESULT __stdcall Reset()
	{
		index = 0;
		return S_OK;
	}

private:
	int refCount;
	const FORMATETC* const format;
	int index;

	static void copyFormatEtc (FORMATETC& dest, const FORMATETC& source)
	{
		dest = source;

		if (source.ptd != 0)
		{
			dest.ptd = (DVTARGETDEVICE*) CoTaskMemAlloc (sizeof (DVTARGETDEVICE));
			*(dest.ptd) = *(source.ptd);
		}
	}

	JuceEnumFormatEtc (const JuceEnumFormatEtc&);
	JuceEnumFormatEtc& operator= (const JuceEnumFormatEtc&);
};

class JuceDataObject  : public IDataObject
{
	JuceDropSource* const dropSource;
	const FORMATETC* const format;
	const STGMEDIUM* const medium;
	int refCount;

	JuceDataObject (const JuceDataObject&);
	JuceDataObject& operator= (const JuceDataObject&);

public:
	JuceDataObject (JuceDropSource* const dropSource_,
					const FORMATETC* const format_,
					const STGMEDIUM* const medium_)
		: dropSource (dropSource_),
		  format (format_),
		  medium (medium_),
		  refCount (1)
	{
	}

	virtual ~JuceDataObject()
	{
		jassert (refCount == 0);
	}

	HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
	{
		if (id == IID_IUnknown || id == IID_IDataObject)
		{
			AddRef();
			*result = this;
			return S_OK;
		}

		*result = 0;
		return E_NOINTERFACE;
	}

	ULONG __stdcall AddRef()	{ return ++refCount; }
	ULONG __stdcall Release()   { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }

	HRESULT __stdcall GetData (FORMATETC __RPC_FAR* pFormatEtc, STGMEDIUM __RPC_FAR* pMedium)
	{
		if ((pFormatEtc->tymed & format->tymed) != 0
			 && pFormatEtc->cfFormat == format->cfFormat
			 && pFormatEtc->dwAspect == format->dwAspect)
		{
			pMedium->tymed = format->tymed;
			pMedium->pUnkForRelease = 0;

			if (format->tymed == TYMED_HGLOBAL)
			{
				const SIZE_T len = GlobalSize (medium->hGlobal);
				void* const src = GlobalLock (medium->hGlobal);
				void* const dst = GlobalAlloc (GMEM_FIXED, len);

				memcpy (dst, src, len);

				GlobalUnlock (medium->hGlobal);

				pMedium->hGlobal = dst;
				return S_OK;
			}
		}

		return DV_E_FORMATETC;
	}

	HRESULT __stdcall QueryGetData (FORMATETC __RPC_FAR* f)
	{
		if (f == 0)
			return E_INVALIDARG;

		if (f->tymed == format->tymed
			  && f->cfFormat == format->cfFormat
			  && f->dwAspect == format->dwAspect)
			return S_OK;

		return DV_E_FORMATETC;
	}

	HRESULT __stdcall GetCanonicalFormatEtc (FORMATETC __RPC_FAR*, FORMATETC __RPC_FAR* pFormatEtcOut)
	{
		pFormatEtcOut->ptd = 0;
		return E_NOTIMPL;
	}

	HRESULT __stdcall EnumFormatEtc (DWORD direction, IEnumFORMATETC __RPC_FAR *__RPC_FAR *result)
	{
		if (result == 0)
			return E_POINTER;

		if (direction == DATADIR_GET)
		{
			*result = new JuceEnumFormatEtc (format);
			return S_OK;
		}

		*result = 0;
		return E_NOTIMPL;
	}

	HRESULT __stdcall GetDataHere (FORMATETC __RPC_FAR*, STGMEDIUM __RPC_FAR*)			  { return DATA_E_FORMATETC; }
	HRESULT __stdcall SetData (FORMATETC __RPC_FAR*, STGMEDIUM __RPC_FAR*, BOOL)			{ return E_NOTIMPL; }
	HRESULT __stdcall DAdvise (FORMATETC __RPC_FAR*, DWORD, IAdviseSink __RPC_FAR*, DWORD __RPC_FAR*)   { return OLE_E_ADVISENOTSUPPORTED; }
	HRESULT __stdcall DUnadvise (DWORD)								 { return E_NOTIMPL; }
	HRESULT __stdcall EnumDAdvise (IEnumSTATDATA __RPC_FAR *__RPC_FAR *)				{ return OLE_E_ADVISENOTSUPPORTED; }
};

static HDROP createHDrop (const StringArray& fileNames) throw()
{
	int totalChars = 0;
	for (int i = fileNames.size(); --i >= 0;)
		totalChars += fileNames[i].length() + 1;

	HDROP hDrop = (HDROP) GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT,
									   sizeof (DROPFILES)
										 + sizeof (WCHAR) * (totalChars + 2));

	if (hDrop != 0)
	{
		LPDROPFILES pDropFiles = (LPDROPFILES) GlobalLock (hDrop);
		pDropFiles->pFiles = sizeof (DROPFILES);

		pDropFiles->fWide = true;

		WCHAR* fname = (WCHAR*) (((char*) pDropFiles) + sizeof (DROPFILES));

		for (int i = 0; i < fileNames.size(); ++i)
		{
			fileNames[i].copyToUnicode (fname, 2048);
			fname += fileNames[i].length() + 1;
		}

		*fname = 0;

		GlobalUnlock (hDrop);
	}

	return hDrop;
}

static bool performDragDrop (FORMATETC* const format, STGMEDIUM* const medium, const DWORD whatToDo) throw()
{
	JuceDropSource* const source = new JuceDropSource();
	JuceDataObject* const data = new JuceDataObject (source, format, medium);

	DWORD effect;
	const HRESULT res = DoDragDrop (data, source, whatToDo, &effect);

	data->Release();
	source->Release();

	return res == DRAGDROP_S_DROP;
}

bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMove)
{
	FORMATETC format = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
	STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };

	medium.hGlobal = createHDrop (files);

	return performDragDrop (&format, &medium, canMove ? (DROPEFFECT_COPY | DROPEFFECT_MOVE)
													  : DROPEFFECT_COPY);
}

bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
{
	FORMATETC format = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
	STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };

	const int numChars = text.length();

	medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, (numChars + 2) * sizeof (WCHAR));
	WCHAR* const data = static_cast <WCHAR*> (GlobalLock (medium.hGlobal));

	text.copyToUnicode (data, numChars + 1);
	format.cfFormat = CF_UNICODETEXT;

	GlobalUnlock (medium.hGlobal);

	return performDragDrop (&format, &medium, DROPEFFECT_COPY | DROPEFFECT_MOVE);
}

#endif
/*** End of inlined file: juce_win32_Windowing.cpp ***/


/*** Start of inlined file: juce_win32_Fonts.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

static int CALLBACK wfontEnum2 (ENUMLOGFONTEXW* lpelfe,
								NEWTEXTMETRICEXW*,
								int type,
								LPARAM lParam)
{
	if (lpelfe != 0 && (type & RASTER_FONTTYPE) == 0)
	{
		const String fontName (lpelfe->elfLogFont.lfFaceName);

		((StringArray*) lParam)->addIfNotAlreadyThere (fontName.removeCharacters ("@"));
	}

	return 1;
}

static int CALLBACK wfontEnum1 (ENUMLOGFONTEXW* lpelfe,
								NEWTEXTMETRICEXW*,
								int type,
								LPARAM lParam)
{
	if (lpelfe != 0 && (type & RASTER_FONTTYPE) == 0)
	{
		LOGFONTW lf;
		zerostruct (lf);

		lf.lfWeight = FW_DONTCARE;
		lf.lfOutPrecision = OUT_OUTLINE_PRECIS;
		lf.lfQuality = DEFAULT_QUALITY;
		lf.lfCharSet = DEFAULT_CHARSET;
		lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
		lf.lfPitchAndFamily = FF_DONTCARE;

		const String fontName (lpelfe->elfLogFont.lfFaceName);
		fontName.copyToUnicode (lf.lfFaceName, LF_FACESIZE - 1);

		HDC dc = CreateCompatibleDC (0);
		EnumFontFamiliesEx (dc, &lf,
							(FONTENUMPROCW) &wfontEnum2,
							lParam, 0);
		DeleteDC (dc);
	}

	return 1;
}

const StringArray Font::findAllTypefaceNames()
{
	StringArray results;
	HDC dc = CreateCompatibleDC (0);

	{
		LOGFONTW lf;
		zerostruct (lf);

		lf.lfWeight = FW_DONTCARE;
		lf.lfOutPrecision = OUT_OUTLINE_PRECIS;
		lf.lfQuality = DEFAULT_QUALITY;
		lf.lfCharSet = DEFAULT_CHARSET;
		lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
		lf.lfPitchAndFamily = FF_DONTCARE;
		lf.lfFaceName[0] = 0;

		EnumFontFamiliesEx (dc, &lf,
							(FONTENUMPROCW) &wfontEnum1,
							(LPARAM) &results, 0);
	}

	DeleteDC (dc);

	results.sort (true);
	return results;
}

extern bool juce_IsRunningInWine();

void Font::getPlatformDefaultFontNames (String& defaultSans, String& defaultSerif, String& defaultFixed)
{
	if (juce_IsRunningInWine())
	{
		// If we're running in Wine, then use fonts that might be available on Linux..
		defaultSans  = "Bitstream Vera Sans";
		defaultSerif = "Bitstream Vera Serif";
		defaultFixed = "Bitstream Vera Sans Mono";
	}
	else
	{
		defaultSans  = "Verdana";
		defaultSerif = "Times";
		defaultFixed = "Lucida Console";
	}
}

class FontDCHolder  : private DeletedAtShutdown
{
public:

	FontDCHolder() throw()
		: dc (0), numKPs (0), size (0),
		  bold (false), italic (false)
	{
	}

	~FontDCHolder() throw()
	{
		if (dc != 0)
		{
			DeleteDC (dc);
			DeleteObject (fontH);
		}

		clearSingletonInstance();
	}

	juce_DeclareSingleton_SingleThreaded_Minimal (FontDCHolder);

	HDC loadFont (const String& fontName_, const bool bold_, const bool italic_, const int size_) throw()
	{
		if (fontName != fontName_ || bold != bold_ || italic != italic_ || size != size_)
		{
			fontName = fontName_;
			bold = bold_;
			italic = italic_;
			size = size_;

			if (dc != 0)
			{
				DeleteDC (dc);
				DeleteObject (fontH);
				kps.free();
			}

			fontH = 0;

			dc = CreateCompatibleDC (0);
			SetMapperFlags (dc, 0);
			SetMapMode (dc, MM_TEXT);

			LOGFONTW lfw;
			zerostruct (lfw);

			lfw.lfCharSet = DEFAULT_CHARSET;
			lfw.lfClipPrecision = CLIP_DEFAULT_PRECIS;
			lfw.lfOutPrecision = OUT_OUTLINE_PRECIS;
			lfw.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
			lfw.lfQuality = PROOF_QUALITY;
			lfw.lfItalic = (BYTE) (italic ? TRUE : FALSE);
			lfw.lfWeight = bold ? FW_BOLD : FW_NORMAL;
			fontName.copyToUnicode (lfw.lfFaceName, LF_FACESIZE - 1);

			lfw.lfHeight = size > 0 ? size : -256;
			HFONT standardSizedFont = CreateFontIndirect (&lfw);

			if (standardSizedFont != 0)
			{
				if (SelectObject (dc, standardSizedFont) != 0)
				{
					fontH = standardSizedFont;

					if (size == 0)
					{
						OUTLINETEXTMETRIC otm;
						if (GetOutlineTextMetrics (dc, sizeof (otm), &otm) != 0)
						{
							lfw.lfHeight = -(int) otm.otmEMSquare;
							fontH = CreateFontIndirect (&lfw);

							SelectObject (dc, fontH);
							DeleteObject (standardSizedFont);
						}
					}
				}
				else
				{
					jassertfalse
				}
			}
			else
			{
				jassertfalse
			}
		}

		return dc;
	}

	KERNINGPAIR* getKerningPairs (int& numKPs_) throw()
	{
		if (kps == 0)
		{
			numKPs = GetKerningPairs (dc, 0, 0);
			kps.calloc (numKPs);
			GetKerningPairs (dc, numKPs, kps);
		}

		numKPs_ = numKPs;
		return kps;
	}

private:

	HFONT fontH;
	HDC dc;
	String fontName;
	HeapBlock <KERNINGPAIR> kps;
	int numKPs, size;
	bool bold, italic;

	FontDCHolder (const FontDCHolder&);
	FontDCHolder& operator= (const FontDCHolder&);
};

juce_ImplementSingleton_SingleThreaded (FontDCHolder);

class WindowsTypeface   : public CustomTypeface
{
public:
	WindowsTypeface (const Font& font)
	{
		HDC dc = FontDCHolder::getInstance()->loadFont (font.getTypefaceName(),
														font.isBold(), font.isItalic(), 0);

		TEXTMETRIC tm;
		tm.tmAscent = tm.tmHeight = 1;
		tm.tmDefaultChar = 0;
		GetTextMetrics (dc, &tm);

		setCharacteristics (font.getTypefaceName(),
							tm.tmAscent / (float) tm.tmHeight,
							font.isBold(), font.isItalic(),
							tm.tmDefaultChar);
	}

	bool loadGlyphIfPossible (juce_wchar character)
	{
		HDC dc = FontDCHolder::getInstance()->loadFont (name, isBold, isItalic, 0);

		GLYPHMETRICS gm;

		{
			const WCHAR charToTest[] = { (WCHAR) character, 0 };
			WORD index = 0;

			if (GetGlyphIndices (dc, charToTest, 1, &index, GGI_MARK_NONEXISTING_GLYPHS) != GDI_ERROR
				 && index == 0xffff)
			{
				return false;
			}
		}

		Path glyphPath;

		TEXTMETRIC tm;
		if (! GetTextMetrics (dc, &tm))
		{
			addGlyph (character, glyphPath, 0);
			return true;
		}

		const float height = (float) tm.tmHeight;
		static const MAT2 identityMatrix = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, 1 } };

		const int bufSize = GetGlyphOutline (dc, character, GGO_NATIVE,
											 &gm, 0, 0, &identityMatrix);

		if (bufSize > 0)
		{
			HeapBlock<char> data (bufSize);

			GetGlyphOutline (dc, character, GGO_NATIVE, &gm,
							 bufSize, data, &identityMatrix);

			const TTPOLYGONHEADER* pheader = reinterpret_cast<TTPOLYGONHEADER*> (data.getData());

			const float scaleX = 1.0f / height;
			const float scaleY = -1.0f / height;

			while ((char*) pheader < data + bufSize)
			{
				float x = scaleX * pheader->pfxStart.x.value;
				float y = scaleY * pheader->pfxStart.y.value;

				glyphPath.startNewSubPath (x, y);

				const TTPOLYCURVE* curve = (const TTPOLYCURVE*) ((const char*) pheader + sizeof (TTPOLYGONHEADER));
				const char* const curveEnd = ((const char*) pheader) + pheader->cb;

				while ((const char*) curve < curveEnd)
				{
					if (curve->wType == TT_PRIM_LINE)
					{
						for (int i = 0; i < curve->cpfx; ++i)
						{
							x = scaleX * curve->apfx[i].x.value;
							y = scaleY * curve->apfx[i].y.value;

							glyphPath.lineTo (x, y);
						}
					}
					else if (curve->wType == TT_PRIM_QSPLINE)
					{
						for (int i = 0; i < curve->cpfx - 1; ++i)
						{
							const float x2 = scaleX * curve->apfx[i].x.value;
							const float y2 = scaleY * curve->apfx[i].y.value;
							float x3, y3;

							if (i < curve->cpfx - 2)
							{
								x3 = 0.5f * (x2 + scaleX * curve->apfx[i + 1].x.value);
								y3 = 0.5f * (y2 + scaleY * curve->apfx[i + 1].y.value);
							}
							else
							{
								x3 = scaleX * curve->apfx[i + 1].x.value;
								y3 = scaleY * curve->apfx[i + 1].y.value;
							}

							glyphPath.quadraticTo (x2, y2, x3, y3);

							x = x3;
							y = y3;
						}
					}

					curve = (const TTPOLYCURVE*) &(curve->apfx [curve->cpfx]);
				}

				pheader = (const TTPOLYGONHEADER*) curve;

				glyphPath.closeSubPath();
			}
		}

		addGlyph (character, glyphPath, gm.gmCellIncX / height);

		int numKPs;
		const KERNINGPAIR* const kps = FontDCHolder::getInstance()->getKerningPairs (numKPs);

		for (int i = 0; i < numKPs; ++i)
		{
			if (kps[i].wFirst == character)
				addKerningPair (kps[i].wFirst, kps[i].wSecond,
								kps[i].iKernAmount / height);
		}

		return true;
	}
};

const Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font)
{
	return new WindowsTypeface (font);
}

#endif
/*** End of inlined file: juce_win32_Fonts.cpp ***/


/*** Start of inlined file: juce_win32_FileChooser.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

void juce_setWindowStyleBit (HWND h, const int styleType, const int feature, const bool bitIsSet) throw();

namespace FileChooserHelpers
{
	static const void* defaultDirPath = 0;
	static String returnedString; // need this to get non-existent pathnames from the directory chooser
	static Component* currentExtraFileWin = 0;

	static bool areThereAnyAlwaysOnTopWindows()
	{
		for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
		{
			Component* c = Desktop::getInstance().getComponent (i);

			if (c != 0 && c->isAlwaysOnTop() && c->isShowing())
				return true;
		}

		return false;
	}

	static int CALLBACK browseCallbackProc (HWND hWnd, UINT msg, LPARAM lParam, LPARAM /*lpData*/)
	{
		if (msg == BFFM_INITIALIZED)
			SendMessage (hWnd, BFFM_SETSELECTIONW, TRUE, (LPARAM) defaultDirPath);
		else if (msg == BFFM_VALIDATEFAILEDW)
			returnedString = (LPCWSTR) lParam;
		else if (msg == BFFM_VALIDATEFAILEDA)
			returnedString = (const char*) lParam;

		return 0;
	}

	static UINT_PTR CALLBACK openCallback (HWND hdlg, UINT uiMsg, WPARAM /*wParam*/, LPARAM lParam)
	{
		if (currentExtraFileWin != 0)
		{
			if (uiMsg == WM_INITDIALOG)
			{
				HWND dialogH = GetParent (hdlg);
				jassert (dialogH != 0);
				if (dialogH == 0)
					dialogH = hdlg;

				RECT r, cr;
				GetWindowRect (dialogH, &r);
				GetClientRect (dialogH, &cr);

				SetWindowPos (dialogH, 0,
							  r.left, r.top,
							  currentExtraFileWin->getWidth() + jmax (150, (int) (r.right - r.left)),
							  jmax (150, (int) (r.bottom - r.top)),
							  SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);

				currentExtraFileWin->setBounds (cr.right, cr.top, currentExtraFileWin->getWidth(), cr.bottom - cr.top);
				currentExtraFileWin->getChildComponent(0)->setBounds (0, 0, currentExtraFileWin->getWidth(), currentExtraFileWin->getHeight());

				SetParent ((HWND) currentExtraFileWin->getWindowHandle(), (HWND) dialogH);
				juce_setWindowStyleBit ((HWND)currentExtraFileWin->getWindowHandle(), GWL_STYLE, WS_CHILD, (dialogH != 0));
				juce_setWindowStyleBit ((HWND)currentExtraFileWin->getWindowHandle(), GWL_STYLE, WS_POPUP, (dialogH == 0));
			}
			else if (uiMsg == WM_NOTIFY)
			{
				LPOFNOTIFY ofn = (LPOFNOTIFY) lParam;

				if (ofn->hdr.code == CDN_SELCHANGE)
				{
					FilePreviewComponent* comp = (FilePreviewComponent*) currentExtraFileWin->getChildComponent(0);

					if (comp != 0)
					{
						TCHAR path [MAX_PATH * 2];
						path[0] = 0;
						CommDlg_OpenSave_GetFilePath (GetParent (hdlg), (LPARAM) &path, MAX_PATH);

						const String fn ((const WCHAR*) path);

						comp->selectedFileChanged (File (fn));
					}
				}
			}
		}

		return 0;
	}

	class FPComponentHolder  : public Component
	{
	public:
		FPComponentHolder()
		{
			setVisible (true);
			setOpaque (true);
		}

		~FPComponentHolder()
		{
		}

		void paint (Graphics& g)
		{
			g.fillAll (Colours::lightgrey);
		}

	private:
		FPComponentHolder (const FPComponentHolder&);
		FPComponentHolder& operator= (const FPComponentHolder&);
	};
}

void FileChooser::showPlatformDialog (Array<File>& results,
									  const String& title,
									  const File& currentFileOrDirectory,
									  const String& filter,
									  bool selectsDirectory,
									  bool /*selectsFiles*/,
									  bool isSaveDialogue,
									  bool warnAboutOverwritingExistingFiles,
									  bool selectMultipleFiles,
									  FilePreviewComponent* extraInfoComponent)
{
	using namespace FileChooserHelpers;
	const int numCharsAvailable = 32768;
	MemoryBlock filenameSpace ((numCharsAvailable + 1) * sizeof (WCHAR), true);
	WCHAR* const fname = (WCHAR*) filenameSpace.getData();
	int fnameIdx = 0;

	JUCE_TRY
	{
		// use a modal window as the parent for this dialog box
		// to block input from other app windows
		const Rectangle<int> mainMon (Desktop::getInstance().getMainMonitorArea());

		Component w (String::empty);
		w.setBounds (mainMon.getX() + mainMon.getWidth() / 4,
					 mainMon.getY() + mainMon.getHeight() / 4,
					 0, 0);
		w.setOpaque (true);
		w.setAlwaysOnTop (areThereAnyAlwaysOnTopWindows());
		w.addToDesktop (0);

		if (extraInfoComponent == 0)
			w.enterModalState();

		String initialDir;

		if (currentFileOrDirectory.isDirectory())
		{
			initialDir = currentFileOrDirectory.getFullPathName();
		}
		else
		{
			currentFileOrDirectory.getFileName().copyToUnicode (fname, numCharsAvailable);

			initialDir = currentFileOrDirectory.getParentDirectory().getFullPathName();
		}

		if (currentExtraFileWin->isValidComponent())
		{
			jassertfalse
			return;
		}

		if (selectsDirectory)
		{
			LPITEMIDLIST list = 0;
			filenameSpace.fillWith (0);

			{
				BROWSEINFO bi;
				zerostruct (bi);

				bi.hwndOwner = (HWND) w.getWindowHandle();
				bi.pszDisplayName = fname;
				bi.lpszTitle = title;
				bi.lpfn = browseCallbackProc;
#ifdef BIF_USENEWUI
				bi.ulFlags = BIF_USENEWUI | BIF_VALIDATE;
#else
				bi.ulFlags = 0x50;
#endif
				defaultDirPath = (const WCHAR*) initialDir;

				list = SHBrowseForFolder (&bi);

				if (! SHGetPathFromIDListW (list, fname))
				{
					fname[0] = 0;
					returnedString = String::empty;
				}
			}

			LPMALLOC al;
			if (list != 0 && SUCCEEDED (SHGetMalloc (&al)))
				al->Free (list);

			defaultDirPath = 0;

			if (returnedString.isNotEmpty())
			{
				const String stringFName (fname);

				results.add (File (stringFName).getSiblingFile (returnedString));
				returnedString = String::empty;

				return;
			}
		}
		else
		{
			DWORD flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;

			if (warnAboutOverwritingExistingFiles)
				flags |= OFN_OVERWRITEPROMPT;

			if (selectMultipleFiles)
				flags |= OFN_ALLOWMULTISELECT;

			if (extraInfoComponent != 0)
			{
				flags |= OFN_ENABLEHOOK;

				currentExtraFileWin = new FPComponentHolder();
				currentExtraFileWin->addAndMakeVisible (extraInfoComponent);
				currentExtraFileWin->setSize (jlimit (20, 800, extraInfoComponent->getWidth()),
											  extraInfoComponent->getHeight());
				currentExtraFileWin->addToDesktop (0);

				currentExtraFileWin->enterModalState();
			}

			{
				WCHAR filters [1024];
				zeromem (filters, sizeof (filters));
				filter.copyToUnicode (filters, 1024);
				filter.copyToUnicode (filters + filter.length() + 1,
									  1022 - filter.length());

				OPENFILENAMEW of;
				zerostruct (of);

#ifdef OPENFILENAME_SIZE_VERSION_400W
				of.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
#else
				of.lStructSize = sizeof (of);
#endif
				of.hwndOwner = (HWND) w.getWindowHandle();
				of.lpstrFilter = filters;
				of.nFilterIndex = 1;
				of.lpstrFile = fname;
				of.nMaxFile = numCharsAvailable;
				of.lpstrInitialDir = initialDir;
				of.lpstrTitle = title;
				of.Flags = flags;

				if (extraInfoComponent != 0)
					of.lpfnHook = &openCallback;

				if (isSaveDialogue)
				{
					if (! GetSaveFileName (&of))
						fname[0] = 0;
					else
						fnameIdx = of.nFileOffset;
				}
				else
				{
					if (! GetOpenFileName (&of))
						fname[0] = 0;
					else
						fnameIdx = of.nFileOffset;
				}
			}
		}
	}
#if JUCE_CATCH_UNHANDLED_EXCEPTIONS
	catch (...)
	{
		fname[0] = 0;
	}
#endif

	deleteAndZero (currentExtraFileWin);

	const WCHAR* const files = fname;

	if (selectMultipleFiles && fnameIdx > 0 && files [fnameIdx - 1] == 0)
	{
		const WCHAR* filename = files + fnameIdx;

		while (*filename != 0)
		{
			const String filepath (String (files) + "\\" + String (filename));
			results.add (File (filepath));
			filename += CharacterFunctions::length (filename) + 1;
		}
	}
	else if (files[0] != 0)
	{
		results.add (File (files));
	}
}

#endif
/*** End of inlined file: juce_win32_FileChooser.cpp ***/


/*** Start of inlined file: juce_win32_Misc.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

void SystemClipboard::copyTextToClipboard (const String& text)
{
	if (OpenClipboard (0) != 0)
	{
		if (EmptyClipboard() != 0)
		{
			const int len = text.length();

			if (len > 0)
			{
				HGLOBAL bufH = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE,
											(len + 1) * sizeof (wchar_t));

				if (bufH != 0)
				{
					WCHAR* const data = static_cast <WCHAR*> (GlobalLock (bufH));
					text.copyToUnicode (data, len);
					GlobalUnlock (bufH);

					SetClipboardData (CF_UNICODETEXT, bufH);
				}
			}
		}

		CloseClipboard();
	}
}

const String SystemClipboard::getTextFromClipboard()
{
	String result;

	if (OpenClipboard (0) != 0)
	{
		HANDLE bufH = GetClipboardData (CF_UNICODETEXT);

		if (bufH != 0)
		{
			const wchar_t* const data = (const wchar_t*) GlobalLock (bufH);

			if (data != 0)
			{
				result = String (data, (int) (GlobalSize (bufH) / sizeof (wchar_t)));

				GlobalUnlock (bufH);
			}
		}

		CloseClipboard();
	}

	return result;
}

#endif
/*** End of inlined file: juce_win32_Misc.cpp ***/


/*** Start of inlined file: juce_win32_ActiveXComponent.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

namespace ActiveXHelpers
{
	class JuceIStorage   : public IStorage
	{
		int refCount;

	public:
		JuceIStorage() : refCount (1) {}

		virtual ~JuceIStorage()
		{
			jassert (refCount == 0);
		}

		HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
		{
			if (id == IID_IUnknown || id == IID_IStorage)
			{
				AddRef();
				*result = this;
				return S_OK;
			}

			*result = 0;
			return E_NOINTERFACE;
		}

		ULONG __stdcall AddRef()	{ return ++refCount; }
		ULONG __stdcall Release()   { const int r = --refCount; if (r == 0) delete this; return r; }

		HRESULT __stdcall CreateStream (const WCHAR*, DWORD, DWORD, DWORD, IStream**)	   { return E_NOTIMPL; }
		HRESULT __stdcall OpenStream (const WCHAR*, void*, DWORD, DWORD, IStream**)		 { return E_NOTIMPL; }
		HRESULT __stdcall CreateStorage (const WCHAR*, DWORD, DWORD, DWORD, IStorage**)	 { return E_NOTIMPL; }
		HRESULT __stdcall OpenStorage (const WCHAR*, IStorage*, DWORD, SNB, DWORD, IStorage**)  { return E_NOTIMPL; }
		HRESULT __stdcall CopyTo (DWORD, IID const*, SNB, IStorage*)				{ return E_NOTIMPL; }
		HRESULT __stdcall MoveElementTo (const OLECHAR*,IStorage*, const OLECHAR*, DWORD)	   { return E_NOTIMPL; }
		HRESULT __stdcall Commit (DWORD)							{ return E_NOTIMPL; }
		HRESULT __stdcall Revert()								  { return E_NOTIMPL; }
		HRESULT __stdcall EnumElements (DWORD, void*, DWORD, IEnumSTATSTG**)			{ return E_NOTIMPL; }
		HRESULT __stdcall DestroyElement (const OLECHAR*)					   { return E_NOTIMPL; }
		HRESULT __stdcall RenameElement (const WCHAR*, const WCHAR*)				{ return E_NOTIMPL; }
		HRESULT __stdcall SetElementTimes (const WCHAR*, FILETIME const*, FILETIME const*, FILETIME const*)	{ return E_NOTIMPL; }
		HRESULT __stdcall SetClass (REFCLSID)						   { return S_OK; }
		HRESULT __stdcall SetStateBits (DWORD, DWORD)					   { return E_NOTIMPL; }
		HRESULT __stdcall Stat (STATSTG*, DWORD)						{ return E_NOTIMPL; }

		juce_UseDebuggingNewOperator
	};

	class JuceOleInPlaceFrame   : public IOleInPlaceFrame
	{
		int refCount;
		HWND window;

	public:
		JuceOleInPlaceFrame (HWND window_)
			: refCount (1),
			  window (window_)
		{
		}

		virtual ~JuceOleInPlaceFrame()
		{
			jassert (refCount == 0);
		}

		HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
		{
			if (id == IID_IUnknown || id == IID_IOleInPlaceFrame)
			{
				AddRef();
				*result = this;
				return S_OK;
			}

			*result = 0;
			return E_NOINTERFACE;
		}

		ULONG __stdcall AddRef()	{ return ++refCount; }
		ULONG __stdcall Release()   { const int r = --refCount; if (r == 0) delete this; return r; }

		HRESULT __stdcall GetWindow (HWND* lphwnd)			  { *lphwnd = window; return S_OK; }
		HRESULT __stdcall ContextSensitiveHelp (BOOL)		   { return E_NOTIMPL; }
		HRESULT __stdcall GetBorder (LPRECT)				{ return E_NOTIMPL; }
		HRESULT __stdcall RequestBorderSpace (LPCBORDERWIDTHS)	  { return E_NOTIMPL; }
		HRESULT __stdcall SetBorderSpace (LPCBORDERWIDTHS)		  { return E_NOTIMPL; }
		HRESULT __stdcall SetActiveObject (IOleInPlaceActiveObject*, LPCOLESTR)	 { return S_OK; }
		HRESULT __stdcall InsertMenus (HMENU, LPOLEMENUGROUPWIDTHS)	 { return E_NOTIMPL; }
		HRESULT __stdcall SetMenu (HMENU, HOLEMENU, HWND)		   { return S_OK; }
		HRESULT __stdcall RemoveMenus (HMENU)			   { return E_NOTIMPL; }
		HRESULT __stdcall SetStatusText (LPCOLESTR)			 { return S_OK; }
		HRESULT __stdcall EnableModeless (BOOL)			 { return S_OK; }
		HRESULT __stdcall TranslateAccelerator(LPMSG, WORD)		 { return E_NOTIMPL; }

		juce_UseDebuggingNewOperator
	};

	class JuceIOleInPlaceSite   : public IOleInPlaceSite
	{
		int refCount;
		HWND window;
		JuceOleInPlaceFrame* frame;

	public:
		JuceIOleInPlaceSite (HWND window_)
			: refCount (1),
			  window (window_)
		{
			frame = new JuceOleInPlaceFrame (window);
		}

		virtual ~JuceIOleInPlaceSite()
		{
			jassert (refCount == 0);
			frame->Release();
		}

		HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
		{
			if (id == IID_IUnknown || id == IID_IOleInPlaceSite)
			{
				AddRef();
				*result = this;
				return S_OK;
			}

			*result = 0;
			return E_NOINTERFACE;
		}

		ULONG __stdcall AddRef()	{ return ++refCount; }
		ULONG __stdcall Release()   { const int r = --refCount; if (r == 0) delete this; return r; }

		HRESULT __stdcall GetWindow (HWND* lphwnd)	  { *lphwnd = window; return S_OK; }
		HRESULT __stdcall ContextSensitiveHelp (BOOL)   { return E_NOTIMPL; }
		HRESULT __stdcall CanInPlaceActivate()	  { return S_OK; }
		HRESULT __stdcall OnInPlaceActivate()	   { return S_OK; }
		HRESULT __stdcall OnUIActivate()		{ return S_OK; }

		HRESULT __stdcall GetWindowContext (LPOLEINPLACEFRAME* lplpFrame, LPOLEINPLACEUIWINDOW* lplpDoc, LPRECT, LPRECT, LPOLEINPLACEFRAMEINFO lpFrameInfo)
		{
			*lplpFrame = frame;
			*lplpDoc = 0;
			lpFrameInfo->fMDIApp = FALSE;
			lpFrameInfo->hwndFrame = window;
			lpFrameInfo->haccel = 0;
			lpFrameInfo->cAccelEntries = 0;
			return S_OK;
		}

		HRESULT __stdcall Scroll (SIZE)		 { return E_NOTIMPL; }
		HRESULT __stdcall OnUIDeactivate (BOOL)	 { return S_OK; }
		HRESULT __stdcall OnInPlaceDeactivate()	 { return S_OK; }
		HRESULT __stdcall DiscardUndoState()		{ return E_NOTIMPL; }
		HRESULT __stdcall DeactivateAndUndo()	   { return E_NOTIMPL; }
		HRESULT __stdcall OnPosRectChange (LPCRECT)	 { return S_OK; }

		juce_UseDebuggingNewOperator
	};

	class JuceIOleClientSite  : public IOleClientSite
	{
		int refCount;
		JuceIOleInPlaceSite* inplaceSite;

	public:
		JuceIOleClientSite (HWND window)
			: refCount (1)
		{
			inplaceSite = new JuceIOleInPlaceSite (window);
		}

		virtual ~JuceIOleClientSite()
		{
			jassert (refCount == 0);
			inplaceSite->Release();
		}

		HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
		{
			if (id == IID_IUnknown || id == IID_IOleClientSite)
			{
				AddRef();
				*result = this;
				return S_OK;
			}
			else if (id == IID_IOleInPlaceSite)
			{
				inplaceSite->AddRef();
				*result = inplaceSite;
				return S_OK;
			}

			*result = 0;
			return E_NOINTERFACE;
		}

		ULONG __stdcall AddRef()	{ return ++refCount; }
		ULONG __stdcall Release()   { const int r = --refCount; if (r == 0) delete this; return r; }

		HRESULT __stdcall SaveObject()				  { return E_NOTIMPL; }
		HRESULT __stdcall GetMoniker (DWORD, DWORD, IMoniker**)	 { return E_NOTIMPL; }
		HRESULT __stdcall GetContainer (LPOLECONTAINER* ppContainer)	{ *ppContainer = 0; return E_NOINTERFACE; }
		HRESULT __stdcall ShowObject()				  { return S_OK; }
		HRESULT __stdcall OnShowWindow (BOOL)			   { return E_NOTIMPL; }
		HRESULT __stdcall RequestNewObjectLayout()			  { return E_NOTIMPL; }

		juce_UseDebuggingNewOperator
	};

	static VoidArray activeXComps;

	static HWND getHWND (const ActiveXControlComponent* const component)
	{
		HWND hwnd = 0;

		const IID iid = IID_IOleWindow;
		IOleWindow* const window = (IOleWindow*) component->queryInterface (&iid);

		if (window != 0)
		{
			window->GetWindow (&hwnd);
			window->Release();
		}

		return hwnd;
	}

	static void offerActiveXMouseEventToPeer (ComponentPeer* const peer, HWND hwnd, UINT message, LPARAM lParam)
	{
		RECT activeXRect, peerRect;
		GetWindowRect (hwnd, &activeXRect);
		GetWindowRect ((HWND) peer->getNativeHandle(), &peerRect);

		const Point<int> mousePos (GET_X_LPARAM (lParam) + activeXRect.left - peerRect.left,
								   GET_Y_LPARAM (lParam) + activeXRect.top - peerRect.top);
		const int64 mouseEventTime = Win32ComponentPeer::getMouseEventTime();

		ModifierKeys::getCurrentModifiersRealtime(); // to update the mouse button flags

		switch (message)
		{
		case WM_MOUSEMOVE:
		case WM_LBUTTONDOWN:
		case WM_MBUTTONDOWN:
		case WM_RBUTTONDOWN:
		case WM_LBUTTONUP:
		case WM_MBUTTONUP:
		case WM_RBUTTONUP:
			peer->handleMouseEvent (0, mousePos, Win32ComponentPeer::currentModifiers, mouseEventTime);
			break;

		default:
			break;
		}
	}
}

class ActiveXControlComponent::Pimpl  : public ComponentMovementWatcher
{
	ActiveXControlComponent* const owner;
	bool wasShowing;

public:
	HWND controlHWND;
	IStorage* storage;
	IOleClientSite* clientSite;
	IOleObject* control;

	Pimpl (HWND hwnd, ActiveXControlComponent* const owner_)
		: ComponentMovementWatcher (owner_),
		  owner (owner_),
		  wasShowing (owner_ != 0 && owner_->isShowing()),
		  controlHWND (0),
		  storage (new ActiveXHelpers::JuceIStorage()),
		  clientSite (new ActiveXHelpers::JuceIOleClientSite (hwnd)),
		  control (0)
	{
	}

	~Pimpl()
	{
		if (control != 0)
		{
			control->Close (OLECLOSE_NOSAVE);
			control->Release();
		}

		clientSite->Release();
		storage->Release();
	}

	void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
	{
		Component* const topComp = owner->getTopLevelComponent();

		if (topComp->getPeer() != 0)
		{
			const Point<int> pos (owner->relativePositionToOtherComponent (topComp, Point<int>()));

			owner->setControlBounds (Rectangle<int> (pos.getX(), pos.getY(), owner->getWidth(), owner->getHeight()));
		}
	}

	void componentPeerChanged()
	{
		const bool isShowingNow = owner->isShowing();

		if (wasShowing != isShowingNow)
		{
			wasShowing = isShowingNow;
			owner->setControlVisible (isShowingNow);
		}

		componentMovedOrResized (true, true);
	}

	void componentVisibilityChanged (Component&)
	{
		componentPeerChanged();
	}

	// intercepts events going to an activeX control, so we can sneakily use the mouse events
	static LRESULT CALLBACK activeXHookWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		for (int i = ActiveXHelpers::activeXComps.size(); --i >= 0;)
		{
			const ActiveXControlComponent* const ax
					= static_cast <const ActiveXControlComponent*> (ActiveXHelpers::activeXComps.getUnchecked(i));

			if (ax->control != 0 && ax->control->controlHWND == hwnd)
			{
				switch (message)
				{
				case WM_MOUSEMOVE:
				case WM_LBUTTONDOWN:
				case WM_MBUTTONDOWN:
				case WM_RBUTTONDOWN:
				case WM_LBUTTONUP:
				case WM_MBUTTONUP:
				case WM_RBUTTONUP:
				case WM_LBUTTONDBLCLK:
				case WM_MBUTTONDBLCLK:
				case WM_RBUTTONDBLCLK:
					if (ax->isShowing())
					{
						ComponentPeer* const peer = ax->getPeer();

						if (peer != 0)
						{
							ActiveXHelpers::offerActiveXMouseEventToPeer (peer, hwnd, message, lParam);

							if (! ax->areMouseEventsAllowed())
								return 0;
						}
					}
					break;

				default:
					break;
				}

				return CallWindowProc ((WNDPROC) ax->originalWndProc, hwnd, message, wParam, lParam);
			}
		}

		return DefWindowProc (hwnd, message, wParam, lParam);
	}
};

ActiveXControlComponent::ActiveXControlComponent()
	: originalWndProc (0),
	  mouseEventsAllowed (true)
{
	ActiveXHelpers::activeXComps.add (this);
}

ActiveXControlComponent::~ActiveXControlComponent()
{
	deleteControl();
	ActiveXHelpers::activeXComps.removeValue (this);
}

void ActiveXControlComponent::paint (Graphics& g)
{
	if (control == 0)
		g.fillAll (Colours::lightgrey);
}

bool ActiveXControlComponent::createControl (const void* controlIID)
{
	deleteControl();
	ComponentPeer* const peer = getPeer();

	// the component must have already been added to a real window when you call this!
	jassert (dynamic_cast <Win32ComponentPeer*> (peer) != 0);

	if (dynamic_cast <Win32ComponentPeer*> (peer) != 0)
	{
		const Point<int> pos (relativePositionToOtherComponent (getTopLevelComponent(), Point<int>()));
		HWND hwnd = (HWND) peer->getNativeHandle();

		ScopedPointer<Pimpl> newControl (new Pimpl (hwnd, this));

		HRESULT hr;
		if ((hr = OleCreate (*(const IID*) controlIID, IID_IOleObject, 1 /*OLERENDER_DRAW*/, 0,
							 newControl->clientSite, newControl->storage,
							 (void**) &(newControl->control))) == S_OK)
		{
			newControl->control->SetHostNames (L"Juce", 0);

			if (OleSetContainedObject (newControl->control, TRUE) == S_OK)
			{
				RECT rect;
				rect.left = pos.getX();
				rect.top = pos.getY();
				rect.right = pos.getX() + getWidth();
				rect.bottom = pos.getY() + getHeight();

				if (newControl->control->DoVerb (OLEIVERB_SHOW, 0, newControl->clientSite, 0, hwnd, &rect) == S_OK)
				{
					control = newControl;
					setControlBounds (Rectangle<int> (pos.getX(), pos.getY(), getWidth(), getHeight()));

					control->controlHWND = ActiveXHelpers::getHWND (this);

					if (control->controlHWND != 0)
					{
						originalWndProc = (void*) (pointer_sized_int) GetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC);
						SetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC, (LONG_PTR) Pimpl::activeXHookWndProc);
					}

					return true;
				}
			}
		}
	}

	return false;
}

void ActiveXControlComponent::deleteControl()
{
	control = 0;
	originalWndProc = 0;
}

void* ActiveXControlComponent::queryInterface (const void* iid) const
{
	void* result = 0;

	if (control != 0 && control->control != 0
		 && SUCCEEDED (control->control->QueryInterface (*(const IID*) iid, &result)))
		return result;

	return 0;
}

void ActiveXControlComponent::setControlBounds (const Rectangle<int>& newBounds) const
{
	if (control->controlHWND != 0)
		MoveWindow (control->controlHWND, newBounds.getX(), newBounds.getY(), newBounds.getWidth(), newBounds.getHeight(), TRUE);
}

void ActiveXControlComponent::setControlVisible (const bool shouldBeVisible) const
{
	if (control->controlHWND != 0)
		ShowWindow (control->controlHWND, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
}

void ActiveXControlComponent::setMouseEventsAllowed (const bool eventsCanReachControl)
{
	mouseEventsAllowed = eventsCanReachControl;
}

#endif
/*** End of inlined file: juce_win32_ActiveXComponent.cpp ***/


/*** Start of inlined file: juce_win32_QuickTimeMovieComponent.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_QUICKTIME

using namespace QTOLibrary;
using namespace QTOControlLib;

bool juce_OpenQuickTimeMovieFromStream (InputStream* input, Movie& movie, Handle& dataHandle);

static bool isQTAvailable = false;

class QuickTimeMovieComponent::Pimpl
{
public:
	Pimpl() : dataHandle (0)
	{
	}

	~Pimpl()
	{
		clearHandle();
	}

	void clearHandle()
	{
		if (dataHandle != 0)
		{
			DisposeHandle (dataHandle);
			dataHandle = 0;
		}
	}

	IQTControlPtr qtControl;
	IQTMoviePtr qtMovie;
	Handle dataHandle;
};

QuickTimeMovieComponent::QuickTimeMovieComponent()
	: movieLoaded (false),
	  controllerVisible (true)
{
	pimpl = new Pimpl();
	setMouseEventsAllowed (false);
}

QuickTimeMovieComponent::~QuickTimeMovieComponent()
{
	closeMovie();
	pimpl->qtControl = 0;
	deleteControl();
	pimpl = 0;
}

bool QuickTimeMovieComponent::isQuickTimeAvailable() throw()
{
	if (! isQTAvailable)
		isQTAvailable = (InitializeQTML (0) == noErr) && (EnterMovies() == noErr);

	return isQTAvailable;
}

void QuickTimeMovieComponent::createControlIfNeeded()
{
	if (isShowing() && ! isControlCreated())
	{
		const IID qtIID = __uuidof (QTControl);

		if (createControl (&qtIID))
		{
			const IID qtInterfaceIID = __uuidof (IQTControl);
			pimpl->qtControl = (IQTControl*) queryInterface (&qtInterfaceIID);

			if (pimpl->qtControl != 0)
			{
				pimpl->qtControl->Release(); // it has one ref too many at this point

				pimpl->qtControl->QuickTimeInitialize();
				pimpl->qtControl->PutSizing (qtMovieFitsControl);

				if (movieFile != File::nonexistent)
					loadMovie (movieFile, controllerVisible);
			}
		}
	}
}

bool QuickTimeMovieComponent::isControlCreated() const
{
	return isControlOpen();
}

bool QuickTimeMovieComponent::loadMovie (InputStream* movieStream,
										 const bool isControllerVisible)
{
	const ScopedPointer<InputStream> movieStreamDeleter (movieStream);

	movieFile = File::nonexistent;
	movieLoaded = false;
	pimpl->qtMovie = 0;
	controllerVisible = isControllerVisible;
	createControlIfNeeded();

	if (isControlCreated())
	{
		if (pimpl->qtControl != 0)
		{
			pimpl->qtControl->Put_MovieHandle (0);
			pimpl->clearHandle();

			Movie movie;
			if (juce_OpenQuickTimeMovieFromStream (movieStream, movie, pimpl->dataHandle))
			{
				pimpl->qtControl->Put_MovieHandle ((long) (pointer_sized_int) movie);

				pimpl->qtMovie = pimpl->qtControl->GetMovie();

				if (pimpl->qtMovie != 0)
					pimpl->qtMovie->PutMovieControllerType (isControllerVisible ? qtMovieControllerTypeStandard
																				: qtMovieControllerTypeNone);
			}

			if (movie == 0)
				pimpl->clearHandle();
		}

		movieLoaded = (pimpl->qtMovie != 0);
	}
	else
	{
		// You're trying to open a movie when the control hasn't yet been created, probably because
		// you've not yet added this component to a Window and made the whole component hierarchy visible.
		jassertfalse
	}

	return movieLoaded;
}

void QuickTimeMovieComponent::closeMovie()
{
	stop();
	movieFile = File::nonexistent;
	movieLoaded = false;
	pimpl->qtMovie = 0;

	if (pimpl->qtControl != 0)
		pimpl->qtControl->Put_MovieHandle (0);

	pimpl->clearHandle();
}

const File QuickTimeMovieComponent::getCurrentMovieFile() const
{
	return movieFile;
}

bool QuickTimeMovieComponent::isMovieOpen() const
{
	return movieLoaded;
}

double QuickTimeMovieComponent::getMovieDuration() const
{
	if (pimpl->qtMovie != 0)
		return pimpl->qtMovie->GetDuration() / (double) pimpl->qtMovie->GetTimeScale();

	return 0.0;
}

void QuickTimeMovieComponent::getMovieNormalSize (int& width, int& height) const
{
	if (pimpl->qtMovie != 0)
	{
		struct QTRECT r = pimpl->qtMovie->GetNaturalRect();

		width = r.right - r.left;
		height = r.bottom - r.top;
	}
	else
	{
		width = height = 0;
	}
}

void QuickTimeMovieComponent::play()
{
	if (pimpl->qtMovie != 0)
		pimpl->qtMovie->Play();
}

void QuickTimeMovieComponent::stop()
{
	if (pimpl->qtMovie != 0)
		pimpl->qtMovie->Stop();
}

bool QuickTimeMovieComponent::isPlaying() const
{
	return pimpl->qtMovie != 0 && pimpl->qtMovie->GetRate() != 0.0f;
}

void QuickTimeMovieComponent::setPosition (const double seconds)
{
	if (pimpl->qtMovie != 0)
		pimpl->qtMovie->PutTime ((long) (seconds * pimpl->qtMovie->GetTimeScale()));
}

double QuickTimeMovieComponent::getPosition() const
{
	if (pimpl->qtMovie != 0)
		return pimpl->qtMovie->GetTime() / (double) pimpl->qtMovie->GetTimeScale();

	return 0.0;
}

void QuickTimeMovieComponent::setSpeed (const float newSpeed)
{
	if (pimpl->qtMovie != 0)
		pimpl->qtMovie->PutRate (newSpeed);
}

void QuickTimeMovieComponent::setMovieVolume (const float newVolume)
{
	if (pimpl->qtMovie != 0)
	{
		pimpl->qtMovie->PutAudioVolume (newVolume);
		pimpl->qtMovie->PutAudioMute (newVolume <= 0);
	}
}

float QuickTimeMovieComponent::getMovieVolume() const
{
	if (pimpl->qtMovie != 0)
		return pimpl->qtMovie->GetAudioVolume();

	return 0.0f;
}

void QuickTimeMovieComponent::setLooping (const bool shouldLoop)
{
	if (pimpl->qtMovie != 0)
		pimpl->qtMovie->PutLoop (shouldLoop);
}

bool QuickTimeMovieComponent::isLooping() const
{
	return pimpl->qtMovie != 0 && pimpl->qtMovie->GetLoop();
}

bool QuickTimeMovieComponent::isControllerVisible() const
{
	return controllerVisible;
}

void QuickTimeMovieComponent::parentHierarchyChanged()
{
	createControlIfNeeded();
	QTCompBaseClass::parentHierarchyChanged();
}

void QuickTimeMovieComponent::visibilityChanged()
{
	createControlIfNeeded();
	QTCompBaseClass::visibilityChanged();
}

void QuickTimeMovieComponent::paint (Graphics& g)
{
	if (! isControlCreated())
		g.fillAll (Colours::black);
}

static Handle createHandleDataRef (Handle dataHandle, const char* fileName)
{
	Handle dataRef = 0;
	OSStatus err = PtrToHand (&dataHandle, &dataRef, sizeof (Handle));
	if (err == noErr)
	{
		Str255 suffix;

		CharacterFunctions::copy ((char*) suffix, fileName, 128);

		StringPtr name = suffix;
		err = PtrAndHand (name, dataRef, name[0] + 1);

		if (err == noErr)
		{
			long atoms[3];
			atoms[0] = EndianU32_NtoB (3 * sizeof (long));
			atoms[1] = EndianU32_NtoB (kDataRefExtensionMacOSFileType);
			atoms[2] = EndianU32_NtoB (MovieFileType);

			err = PtrAndHand (atoms, dataRef, 3 * sizeof (long));

			if (err == noErr)
				return dataRef;
		}

		DisposeHandle (dataRef);
	}

	return 0;
}

static CFStringRef juceStringToCFString (const String& s)
{
	const int len = s.length();
	const juce_wchar* const t = s;

	HeapBlock <UniChar> temp (len + 2);
	for (int i = 0; i <= len; ++i)
		temp[i] = t[i];

	return CFStringCreateWithCharacters (kCFAllocatorDefault, temp, len);
}

static bool openMovie (QTNewMoviePropertyElement* props, int prop, Movie& movie)
{
	Boolean trueBool = true;
	props[prop].propClass = kQTPropertyClass_MovieInstantiation;
	props[prop].propID = kQTMovieInstantiationPropertyID_DontResolveDataRefs;
	props[prop].propValueSize = sizeof (trueBool);
	props[prop].propValueAddress = &trueBool;
	++prop;

	props[prop].propClass = kQTPropertyClass_MovieInstantiation;
	props[prop].propID = kQTMovieInstantiationPropertyID_AsyncOK;
	props[prop].propValueSize = sizeof (trueBool);
	props[prop].propValueAddress = &trueBool;
	++prop;

	Boolean isActive = true;
	props[prop].propClass = kQTPropertyClass_NewMovieProperty;
	props[prop].propID = kQTNewMoviePropertyID_Active;
	props[prop].propValueSize = sizeof (isActive);
	props[prop].propValueAddress = &isActive;
	++prop;

	MacSetPort (0);

	jassert (prop <= 5);
	OSStatus err = NewMovieFromProperties (prop, props, 0, 0, &movie);

	return err == noErr;
}

bool juce_OpenQuickTimeMovieFromStream (InputStream* input, Movie& movie, Handle& dataHandle)
{
	if (input == 0)
		return false;

	dataHandle = 0;
	bool ok = false;

	QTNewMoviePropertyElement props[5];
	zeromem (props, sizeof (props));
	int prop = 0;

	DataReferenceRecord dr;
	props[prop].propClass = kQTPropertyClass_DataLocation;
	props[prop].propID = kQTDataLocationPropertyID_DataReference;
	props[prop].propValueSize = sizeof (dr);
	props[prop].propValueAddress = &dr;
	++prop;

	FileInputStream* const fin = dynamic_cast <FileInputStream*> (input);

	if (fin != 0)
	{
		CFStringRef filePath = juceStringToCFString (fin->getFile().getFullPathName());

		QTNewDataReferenceFromFullPathCFString (filePath, (QTPathStyle) kQTNativeDefaultPathStyle, 0,
												&dr.dataRef, &dr.dataRefType);

		ok = openMovie (props, prop, movie);

		DisposeHandle (dr.dataRef);
		CFRelease (filePath);
	}
	else
	{
		// sanity-check because this currently needs to load the whole stream into memory..
		jassert (input->getTotalLength() < 50 * 1024 * 1024);

		dataHandle = NewHandle ((Size) input->getTotalLength());
		HLock (dataHandle);
		// read the entire stream into memory - this is a pain, but can't get it to work
		// properly using a custom callback to supply the data.
		input->read (*dataHandle, (int) input->getTotalLength());
		HUnlock (dataHandle);

		// different types to get QT to try. (We should really be a bit smarter here by
		// working out in advance which one the stream contains, rather than just trying
		// each one)
		const char* const suffixesToTry[] = { "\04.mov", "\04.mp3",
											  "\04.avi", "\04.m4a" };

		for (int i = 0; i < numElementsInArray (suffixesToTry) && ! ok; ++i)
		{

			dr.dataRef = createHandleDataRef (dataHandle, suffixesToTry [i]);
			dr.dataRefType = HandleDataHandlerSubType;
			ok = openMovie (props, prop, movie);

			DisposeHandle (dr.dataRef);
		}
	}

	return ok;
}

bool QuickTimeMovieComponent::loadMovie (const File& movieFile_,
										 const bool isControllerVisible)
{
	const bool ok = loadMovie (static_cast <InputStream*> (movieFile_.createInputStream()), isControllerVisible);
	movieFile = movieFile_;
	return ok;
}

bool QuickTimeMovieComponent::loadMovie (const URL& movieURL,
										 const bool isControllerVisible)
{
	return loadMovie (static_cast <InputStream*> (movieURL.createInputStream (false)), isControllerVisible);
}

void QuickTimeMovieComponent::goToStart()
{
	setPosition (0.0);
}

void QuickTimeMovieComponent::setBoundsWithCorrectAspectRatio (const Rectangle<int>& spaceToFitWithin,
															   const RectanglePlacement& placement)
{
	int normalWidth, normalHeight;
	getMovieNormalSize (normalWidth, normalHeight);

	if (normalWidth > 0 && normalHeight > 0 && ! spaceToFitWithin.isEmpty())
	{
		double x = 0.0, y = 0.0, w = normalWidth, h = normalHeight;

		placement.applyTo (x, y, w, h,
						   spaceToFitWithin.getX(), spaceToFitWithin.getY(),
						   spaceToFitWithin.getWidth(), spaceToFitWithin.getHeight());

		if (w > 0 && h > 0)
		{
			setBounds (roundToInt (x), roundToInt (y),
					   roundToInt (w), roundToInt (h));
		}
	}
	else
	{
		setBounds (spaceToFitWithin);
	}
}

#endif
/*** End of inlined file: juce_win32_QuickTimeMovieComponent.cpp ***/


/*** Start of inlined file: juce_win32_WebBrowserComponent.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_WEB_BROWSER

class WebBrowserComponentInternal   : public ActiveXControlComponent
{
public:

	WebBrowserComponentInternal()
		: browser (0),
		  connectionPoint (0),
		  adviseCookie (0)
	{
	}

	~WebBrowserComponentInternal()
	{
		if (connectionPoint != 0)
			connectionPoint->Unadvise (adviseCookie);

		if (browser != 0)
			browser->Release();
	}

	void createBrowser()
	{
		createControl (&CLSID_WebBrowser);
		browser = (IWebBrowser2*) queryInterface (&IID_IWebBrowser2);

		IConnectionPointContainer* connectionPointContainer = (IConnectionPointContainer*) queryInterface (&IID_IConnectionPointContainer);

		if (connectionPointContainer != 0)
		{
			connectionPointContainer->FindConnectionPoint (DIID_DWebBrowserEvents2,
														   &connectionPoint);

			if (connectionPoint != 0)
			{
				WebBrowserComponent* const owner = dynamic_cast <WebBrowserComponent*> (getParentComponent());
				jassert (owner != 0);

				EventHandler* handler = new EventHandler (owner);
				connectionPoint->Advise (handler, &adviseCookie);
			}
		}
	}

	void goToURL (const String& url,
				  const StringArray* headers,
				  const MemoryBlock* postData)
	{
		if (browser != 0)
		{
			LPSAFEARRAY sa = 0;

			VARIANT flags, frame, postDataVar, headersVar;  // (_variant_t isn't available in all compilers)
			VariantInit (&flags);
			VariantInit (&frame);
			VariantInit (&postDataVar);
			VariantInit (&headersVar);

			if (headers != 0)
			{
				V_VT (&headersVar) = VT_BSTR;
				V_BSTR (&headersVar) = SysAllocString ((const OLECHAR*) headers->joinIntoString ("\r\n"));
			}

			if (postData != 0 && postData->getSize() > 0)
			{
				LPSAFEARRAY sa = SafeArrayCreateVector (VT_UI1, 0, postData->getSize());

				if (sa != 0)
				{
					void* data = 0;
					SafeArrayAccessData (sa, &data);
					jassert (data != 0);

					if (data != 0)
					{
						postData->copyTo (data, 0, postData->getSize());
						SafeArrayUnaccessData (sa);

						VARIANT postDataVar2;
						VariantInit (&postDataVar2);
						V_VT (&postDataVar2) = VT_ARRAY | VT_UI1;
						V_ARRAY (&postDataVar2) = sa;

						postDataVar = postDataVar2;
					}
				}
			}

			browser->Navigate ((BSTR) (const OLECHAR*) url,
							   &flags, &frame,
							   &postDataVar, &headersVar);

			if (sa != 0)
				SafeArrayDestroy (sa);

			VariantClear (&flags);
			VariantClear (&frame);
			VariantClear (&postDataVar);
			VariantClear (&headersVar);
		}
	}

	IWebBrowser2* browser;

	juce_UseDebuggingNewOperator

private:
	IConnectionPoint* connectionPoint;
	DWORD adviseCookie;

	class EventHandler  : public IDispatch,
						  public ComponentMovementWatcher
	{
	public:
		EventHandler (WebBrowserComponent* owner_)
			: ComponentMovementWatcher (owner_),
			  owner (owner_),
			  refCount (0)
		{
		}

		~EventHandler()
		{
		}

		HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
		{
			if (id == IID_IUnknown || id == IID_IDispatch || id == DIID_DWebBrowserEvents2)
			{
				AddRef();
				*result = this;
				return S_OK;
			}

			*result = 0;
			return E_NOINTERFACE;
		}

		ULONG __stdcall AddRef()	{ return ++refCount; }
		ULONG __stdcall Release()   { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }

		HRESULT __stdcall GetTypeInfoCount (UINT __RPC_FAR*)						{ return E_NOTIMPL; }
		HRESULT __stdcall GetTypeInfo (UINT, LCID, ITypeInfo __RPC_FAR *__RPC_FAR*)			 { return E_NOTIMPL; }
		HRESULT __stdcall GetIDsOfNames (REFIID, LPOLESTR __RPC_FAR*, UINT, LCID, DISPID __RPC_FAR*)	{ return E_NOTIMPL; }

		HRESULT __stdcall Invoke (DISPID dispIdMember, REFIID /*riid*/, LCID /*lcid*/,
								  WORD /*wFlags*/, DISPPARAMS __RPC_FAR* pDispParams,
								  VARIANT __RPC_FAR* /*pVarResult*/, EXCEPINFO __RPC_FAR* /*pExcepInfo*/,
								  UINT __RPC_FAR* /*puArgErr*/)
		{
			switch (dispIdMember)
			{
				case DISPID_BEFORENAVIGATE2:
				{
					VARIANT* const vurl = pDispParams->rgvarg[5].pvarVal;

					String url;

					if ((vurl->vt & VT_BYREF) != 0)
						url = *vurl->pbstrVal;
					else
						url = vurl->bstrVal;

					*pDispParams->rgvarg->pboolVal
						= owner->pageAboutToLoad (url) ? VARIANT_FALSE
													   : VARIANT_TRUE;

					return S_OK;
				}

				default:
					break;
			}

			return E_NOTIMPL;
		}

		void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) {}
		void componentPeerChanged() {}

		void componentVisibilityChanged (Component&)
		{
			owner->visibilityChanged();
		}

		juce_UseDebuggingNewOperator

	private:
		WebBrowserComponent* const owner;
		int refCount;

		EventHandler (const EventHandler&);
		EventHandler& operator= (const EventHandler&);
	};
};

WebBrowserComponent::WebBrowserComponent (const bool unloadPageWhenBrowserIsHidden_)
	: browser (0),
	  blankPageShown (false),
	  unloadPageWhenBrowserIsHidden (unloadPageWhenBrowserIsHidden_)
{
	setOpaque (true);
	addAndMakeVisible (browser = new WebBrowserComponentInternal());
}

WebBrowserComponent::~WebBrowserComponent()
{
	delete browser;
}

void WebBrowserComponent::goToURL (const String& url,
								   const StringArray* headers,
								   const MemoryBlock* postData)
{
	lastURL = url;

	lastHeaders.clear();
	if (headers != 0)
		lastHeaders = *headers;

	lastPostData.setSize (0);
	if (postData != 0)
		lastPostData = *postData;

	blankPageShown = false;

	browser->goToURL (url, headers, postData);
}

void WebBrowserComponent::stop()
{
	if (browser->browser != 0)
		browser->browser->Stop();
}

void WebBrowserComponent::goBack()
{
	lastURL = String::empty;
	blankPageShown = false;

	if (browser->browser != 0)
		browser->browser->GoBack();
}

void WebBrowserComponent::goForward()
{
	lastURL = String::empty;

	if (browser->browser != 0)
		browser->browser->GoForward();
}

void WebBrowserComponent::refresh()
{
	if (browser->browser != 0)
		browser->browser->Refresh();
}

void WebBrowserComponent::paint (Graphics& g)
{
	if (browser->browser == 0)
		g.fillAll (Colours::white);
}

void WebBrowserComponent::checkWindowAssociation()
{
	if (isShowing())
	{
		if (browser->browser == 0 && getPeer() != 0)
		{
			browser->createBrowser();
			reloadLastURL();
		}
		else
		{
			if (blankPageShown)
				goBack();
		}
	}
	else
	{
		if (browser != 0 && unloadPageWhenBrowserIsHidden && ! blankPageShown)
		{
			// when the component becomes invisible, some stuff like flash
			// carries on playing audio, so we need to force it onto a blank
			// page to avoid this..

			blankPageShown = true;
			browser->goToURL ("about:blank", 0, 0);
		}
	}
}

void WebBrowserComponent::reloadLastURL()
{
	if (lastURL.isNotEmpty())
	{
		goToURL (lastURL, &lastHeaders, &lastPostData);
		lastURL = String::empty;
	}
}

void WebBrowserComponent::parentHierarchyChanged()
{
	checkWindowAssociation();
}

void WebBrowserComponent::resized()
{
	browser->setSize (getWidth(), getHeight());
}

void WebBrowserComponent::visibilityChanged()
{
	checkWindowAssociation();
}

bool WebBrowserComponent::pageAboutToLoad (const String&)
{
	return true;
}

#endif
/*** End of inlined file: juce_win32_WebBrowserComponent.cpp ***/


/*** Start of inlined file: juce_win32_OpenGLComponent.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_OPENGL

#define WGL_EXT_FUNCTION_INIT(extType, extFunc) \
	((extFunc = (extType) wglGetProcAddress (#extFunc)) != 0)

typedef const char* (WINAPI* PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
typedef BOOL (WINAPI * PFNWGLGETPIXELFORMATATTRIBIVARBPROC) (HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues);
typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int* piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);

#define WGL_NUMBER_PIXEL_FORMATS_ARB	0x2000
#define WGL_DRAW_TO_WINDOW_ARB	  0x2001
#define WGL_ACCELERATION_ARB		0x2003
#define WGL_SWAP_METHOD_ARB		 0x2007
#define WGL_SUPPORT_OPENGL_ARB	  0x2010
#define WGL_PIXEL_TYPE_ARB		  0x2013
#define WGL_DOUBLE_BUFFER_ARB	   0x2011
#define WGL_COLOR_BITS_ARB		  0x2014
#define WGL_RED_BITS_ARB		0x2015
#define WGL_GREEN_BITS_ARB		  0x2017
#define WGL_BLUE_BITS_ARB		   0x2019
#define WGL_ALPHA_BITS_ARB		  0x201B
#define WGL_DEPTH_BITS_ARB		  0x2022
#define WGL_STENCIL_BITS_ARB		0x2023
#define WGL_FULL_ACCELERATION_ARB	   0x2027
#define WGL_ACCUM_RED_BITS_ARB	  0x201E
#define WGL_ACCUM_GREEN_BITS_ARB	0x201F
#define WGL_ACCUM_BLUE_BITS_ARB	 0x2020
#define WGL_ACCUM_ALPHA_BITS_ARB	0x2021
#define WGL_STEREO_ARB		  0x2012
#define WGL_SAMPLE_BUFFERS_ARB	  0x2041
#define WGL_SAMPLES_ARB		 0x2042
#define WGL_TYPE_RGBA_ARB		   0x202B

static void getWglExtensions (HDC dc, StringArray& result) throw()
{
	PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;

	if (WGL_EXT_FUNCTION_INIT (PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetExtensionsStringARB))
		result.addTokens (String (wglGetExtensionsStringARB (dc)), false);
	else
		jassertfalse // If this fails, it may be because you didn't activate the openGL context
}

class WindowedGLContext	 : public OpenGLContext
{
public:
	WindowedGLContext (Component* const component_,
					   HGLRC contextToShareWith,
					   const OpenGLPixelFormat& pixelFormat)
		: renderContext (0),
		  nativeWindow (0),
		  dc (0),
		  component (component_)
	{
		jassert (component != 0);

		createNativeWindow();

		// Use a default pixel format that should be supported everywhere
		PIXELFORMATDESCRIPTOR pfd;
		zerostruct (pfd);
		pfd.nSize = sizeof (pfd);
		pfd.nVersion = 1;
		pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
		pfd.iPixelType = PFD_TYPE_RGBA;
		pfd.cColorBits = 24;
		pfd.cDepthBits = 16;

		const int format = ChoosePixelFormat (dc, &pfd);

		if (format != 0)
			SetPixelFormat (dc, format, &pfd);

		renderContext = wglCreateContext (dc);
		makeActive();

		setPixelFormat (pixelFormat);

		if (contextToShareWith != 0 && renderContext != 0)
			wglShareLists (contextToShareWith, renderContext);
	}

	~WindowedGLContext()
	{
		makeInactive();

		wglDeleteContext (renderContext);

		ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
		delete nativeWindow;
	}

	bool makeActive() const throw()
	{
		jassert (renderContext != 0);
		return wglMakeCurrent (dc, renderContext) != 0;
	}

	bool makeInactive() const throw()
	{
		return (! isActive()) || (wglMakeCurrent (0, 0) != 0);
	}

	bool isActive() const throw()
	{
		return wglGetCurrentContext() == renderContext;
	}

	const OpenGLPixelFormat getPixelFormat() const
	{
		OpenGLPixelFormat pf;

		makeActive();
		StringArray availableExtensions;
		getWglExtensions (dc, availableExtensions);

		fillInPixelFormatDetails (GetPixelFormat (dc), pf, availableExtensions);
		return pf;
	}

	void* getRawContext() const throw()
	{
		return renderContext;
	}

	bool setPixelFormat (const OpenGLPixelFormat& pixelFormat)
	{
		makeActive();

		PIXELFORMATDESCRIPTOR pfd;
		zerostruct (pfd);
		pfd.nSize = sizeof (pfd);
		pfd.nVersion = 1;
		pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
		pfd.iPixelType = PFD_TYPE_RGBA;
		pfd.iLayerType = PFD_MAIN_PLANE;
		pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
		pfd.cRedBits = (BYTE) pixelFormat.redBits;
		pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
		pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
		pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
		pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
		pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
		pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
									+ pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
		pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
		pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
		pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
		pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;

		int format = 0;

		PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = 0;

		StringArray availableExtensions;
		getWglExtensions (dc, availableExtensions);

		if (availableExtensions.contains ("WGL_ARB_pixel_format")
			 && WGL_EXT_FUNCTION_INIT (PFNWGLCHOOSEPIXELFORMATARBPROC, wglChoosePixelFormatARB))
		{
			int attributes[64];
			int n = 0;

			attributes[n++] = WGL_DRAW_TO_WINDOW_ARB;
			attributes[n++] = GL_TRUE;
			attributes[n++] = WGL_SUPPORT_OPENGL_ARB;
			attributes[n++] = GL_TRUE;
			attributes[n++] = WGL_ACCELERATION_ARB;
			attributes[n++] = WGL_FULL_ACCELERATION_ARB;
			attributes[n++] = WGL_DOUBLE_BUFFER_ARB;
			attributes[n++] = GL_TRUE;
			attributes[n++] = WGL_PIXEL_TYPE_ARB;
			attributes[n++] = WGL_TYPE_RGBA_ARB;

			attributes[n++] = WGL_COLOR_BITS_ARB;
			attributes[n++] = pfd.cColorBits;
			attributes[n++] = WGL_RED_BITS_ARB;
			attributes[n++] = pixelFormat.redBits;
			attributes[n++] = WGL_GREEN_BITS_ARB;
			attributes[n++] = pixelFormat.greenBits;
			attributes[n++] = WGL_BLUE_BITS_ARB;
			attributes[n++] = pixelFormat.blueBits;
			attributes[n++] = WGL_ALPHA_BITS_ARB;
			attributes[n++] = pixelFormat.alphaBits;
			attributes[n++] = WGL_DEPTH_BITS_ARB;
			attributes[n++] = pixelFormat.depthBufferBits;

			if (pixelFormat.stencilBufferBits > 0)
			{
				attributes[n++] = WGL_STENCIL_BITS_ARB;
				attributes[n++] = pixelFormat.stencilBufferBits;
			}

			attributes[n++] = WGL_ACCUM_RED_BITS_ARB;
			attributes[n++] = pixelFormat.accumulationBufferRedBits;
			attributes[n++] = WGL_ACCUM_GREEN_BITS_ARB;
			attributes[n++] = pixelFormat.accumulationBufferGreenBits;
			attributes[n++] = WGL_ACCUM_BLUE_BITS_ARB;
			attributes[n++] = pixelFormat.accumulationBufferBlueBits;
			attributes[n++] = WGL_ACCUM_ALPHA_BITS_ARB;
			attributes[n++] = pixelFormat.accumulationBufferAlphaBits;

			if (availableExtensions.contains ("WGL_ARB_multisample")
				 && pixelFormat.fullSceneAntiAliasingNumSamples > 0)
			{
				attributes[n++] = WGL_SAMPLE_BUFFERS_ARB;
				attributes[n++] = 1;
				attributes[n++] = WGL_SAMPLES_ARB;
				attributes[n++] = pixelFormat.fullSceneAntiAliasingNumSamples;
			}

			attributes[n++] = 0;

			UINT formatsCount;
			const BOOL ok = wglChoosePixelFormatARB (dc, attributes, 0, 1, &format, &formatsCount);
			(void) ok;
			jassert (ok);
		}
		else
		{
			format = ChoosePixelFormat (dc, &pfd);
		}

		if (format != 0)
		{
			makeInactive();

			// win32 can't change the pixel format of a window, so need to delete the
			// old one and create a new one..
			jassert (nativeWindow != 0);
			ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
			delete nativeWindow;

			createNativeWindow();

			if (SetPixelFormat (dc, format, &pfd))
			{
				wglDeleteContext (renderContext);
				renderContext = wglCreateContext (dc);

				jassert (renderContext != 0);
				return renderContext != 0;
			}
		}

		return false;
	}

	void updateWindowPosition (int x, int y, int w, int h, int)
	{
		SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
					  x, y, w, h,
					  SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
	}

	void repaint()
	{
		const Rectangle<int> bounds (nativeWindow->getBounds());
		nativeWindow->repaint (0, 0, bounds.getWidth(), bounds.getHeight());
	}

	void swapBuffers()
	{
		SwapBuffers (dc);
	}

	bool setSwapInterval (int numFramesPerSwap)
	{
		makeActive();

		StringArray availableExtensions;
		getWglExtensions (dc, availableExtensions);

		PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = 0;

		return availableExtensions.contains ("WGL_EXT_swap_control")
				&& WGL_EXT_FUNCTION_INIT (PFNWGLSWAPINTERVALEXTPROC, wglSwapIntervalEXT)
				&& wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
	}

	int getSwapInterval() const
	{
		makeActive();

		StringArray availableExtensions;
		getWglExtensions (dc, availableExtensions);

		PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = 0;

		if (availableExtensions.contains ("WGL_EXT_swap_control")
			 && WGL_EXT_FUNCTION_INIT (PFNWGLGETSWAPINTERVALEXTPROC, wglGetSwapIntervalEXT))
			return wglGetSwapIntervalEXT();

		return 0;
	}

	void findAlternativeOpenGLPixelFormats (OwnedArray <OpenGLPixelFormat>& results)
	{
		jassert (isActive());

		StringArray availableExtensions;
		getWglExtensions (dc, availableExtensions);

		PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
		int numTypes = 0;

		if (availableExtensions.contains("WGL_ARB_pixel_format")
			 && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
		{
			int attributes = WGL_NUMBER_PIXEL_FORMATS_ARB;

			if (! wglGetPixelFormatAttribivARB (dc, 1, 0, 1, &attributes, &numTypes))
				jassertfalse
		}
		else
		{
			numTypes = DescribePixelFormat (dc, 0, 0, 0);
		}

		OpenGLPixelFormat pf;

		for (int i = 0; i < numTypes; ++i)
		{
			if (fillInPixelFormatDetails (i + 1, pf, availableExtensions))
			{
				bool alreadyListed = false;
				for (int j = results.size(); --j >= 0;)
					if (pf == *results.getUnchecked(j))
						alreadyListed = true;

				if (! alreadyListed)
					results.add (new OpenGLPixelFormat (pf));
			}
		}
	}

	void* getNativeWindowHandle() const
	{
		return nativeWindow != 0 ? nativeWindow->getNativeHandle() : 0;
	}

	juce_UseDebuggingNewOperator

	HGLRC renderContext;

private:
	Win32ComponentPeer* nativeWindow;
	Component* const component;
	HDC dc;

	void createNativeWindow()
	{
		nativeWindow = new Win32ComponentPeer (component, 0);
		nativeWindow->dontRepaint = true;
		nativeWindow->setVisible (true);

		HWND hwnd = (HWND) nativeWindow->getNativeHandle();

		Win32ComponentPeer* const peer = dynamic_cast <Win32ComponentPeer*> (component->getTopLevelComponent()->getPeer());

		if (peer != 0)
		{
			SetParent (hwnd, (HWND) peer->getNativeHandle());
			juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_CHILD, true);
			juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_POPUP, false);
		}

		dc = GetDC (hwnd);
	}

	bool fillInPixelFormatDetails (const int pixelFormatIndex,
								   OpenGLPixelFormat& result,
								   const StringArray& availableExtensions) const throw()
	{
		PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;

		if (availableExtensions.contains ("WGL_ARB_pixel_format")
			 && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
		{
			int attributes[32];
			int numAttributes = 0;

			attributes[numAttributes++] = WGL_DRAW_TO_WINDOW_ARB;
			attributes[numAttributes++] = WGL_SUPPORT_OPENGL_ARB;
			attributes[numAttributes++] = WGL_ACCELERATION_ARB;
			attributes[numAttributes++] = WGL_DOUBLE_BUFFER_ARB;
			attributes[numAttributes++] = WGL_PIXEL_TYPE_ARB;
			attributes[numAttributes++] = WGL_RED_BITS_ARB;
			attributes[numAttributes++] = WGL_GREEN_BITS_ARB;
			attributes[numAttributes++] = WGL_BLUE_BITS_ARB;
			attributes[numAttributes++] = WGL_ALPHA_BITS_ARB;
			attributes[numAttributes++] = WGL_DEPTH_BITS_ARB;
			attributes[numAttributes++] = WGL_STENCIL_BITS_ARB;
			attributes[numAttributes++] = WGL_ACCUM_RED_BITS_ARB;
			attributes[numAttributes++] = WGL_ACCUM_GREEN_BITS_ARB;
			attributes[numAttributes++] = WGL_ACCUM_BLUE_BITS_ARB;
			attributes[numAttributes++] = WGL_ACCUM_ALPHA_BITS_ARB;

			if (availableExtensions.contains ("WGL_ARB_multisample"))
				attributes[numAttributes++] = WGL_SAMPLES_ARB;

			int values[32];
			zeromem (values, sizeof (values));

			if (wglGetPixelFormatAttribivARB (dc, pixelFormatIndex, 0, numAttributes, attributes, values))
			{
				int n = 0;
				bool isValidFormat = (values[n++] == GL_TRUE);	  // WGL_DRAW_TO_WINDOW_ARB
				isValidFormat = (values[n++] == GL_TRUE) && isValidFormat;   // WGL_SUPPORT_OPENGL_ARB
				isValidFormat = (values[n++] == WGL_FULL_ACCELERATION_ARB) && isValidFormat; // WGL_ACCELERATION_ARB
				isValidFormat = (values[n++] == GL_TRUE) && isValidFormat;   // WGL_DOUBLE_BUFFER_ARB:
				isValidFormat = (values[n++] == WGL_TYPE_RGBA_ARB) && isValidFormat; // WGL_PIXEL_TYPE_ARB
				result.redBits = values[n++];	   // WGL_RED_BITS_ARB
				result.greenBits = values[n++];	 // WGL_GREEN_BITS_ARB
				result.blueBits = values[n++];	  // WGL_BLUE_BITS_ARB
				result.alphaBits = values[n++];	 // WGL_ALPHA_BITS_ARB
				result.depthBufferBits = values[n++];   // WGL_DEPTH_BITS_ARB
				result.stencilBufferBits = values[n++]; // WGL_STENCIL_BITS_ARB
				result.accumulationBufferRedBits = values[n++];	  // WGL_ACCUM_RED_BITS_ARB
				result.accumulationBufferGreenBits = values[n++];	// WGL_ACCUM_GREEN_BITS_ARB
				result.accumulationBufferBlueBits = values[n++];	 // WGL_ACCUM_BLUE_BITS_ARB
				result.accumulationBufferAlphaBits = values[n++];	// WGL_ACCUM_ALPHA_BITS_ARB
				result.fullSceneAntiAliasingNumSamples = (uint8) values[n++];	   // WGL_SAMPLES_ARB

				return isValidFormat;
			}
			else
			{
				jassertfalse
			}
		}
		else
		{
			PIXELFORMATDESCRIPTOR pfd;

			if (DescribePixelFormat (dc, pixelFormatIndex, sizeof (pfd), &pfd))
			{
				result.redBits = pfd.cRedBits;
				result.greenBits = pfd.cGreenBits;
				result.blueBits = pfd.cBlueBits;
				result.alphaBits = pfd.cAlphaBits;
				result.depthBufferBits = pfd.cDepthBits;
				result.stencilBufferBits = pfd.cStencilBits;
				result.accumulationBufferRedBits = pfd.cAccumRedBits;
				result.accumulationBufferGreenBits = pfd.cAccumGreenBits;
				result.accumulationBufferBlueBits = pfd.cAccumBlueBits;
				result.accumulationBufferAlphaBits = pfd.cAccumAlphaBits;
				result.fullSceneAntiAliasingNumSamples = 0;

				return true;
			}
			else
			{
				jassertfalse
			}
		}

		return false;
	}

	WindowedGLContext (const WindowedGLContext&);
	WindowedGLContext& operator= (const WindowedGLContext&);
};

OpenGLContext* OpenGLComponent::createContext()
{
	ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this,
															   contextToShareListsWith != 0 ? (HGLRC) contextToShareListsWith->getRawContext() : 0,
															   preferredPixelFormat));

	return (c->renderContext != 0) ? c.release() : 0;
}

void* OpenGLComponent::getNativeWindowHandle() const
{
	return context != 0 ? static_cast<WindowedGLContext*> (static_cast<OpenGLContext*> (context))->getNativeWindowHandle() : 0;
}

void juce_glViewport (const int w, const int h)
{
	glViewport (0, 0, w, h);
}

void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
												  OwnedArray <OpenGLPixelFormat>& results)
{
	Component tempComp;

	{
		WindowedGLContext wc (component, 0, OpenGLPixelFormat (8, 8, 16, 0));
		wc.makeActive();
		wc.findAlternativeOpenGLPixelFormats (results);
	}
}

#endif
/*** End of inlined file: juce_win32_OpenGLComponent.cpp ***/


/*** Start of inlined file: juce_win32_AudioCDReader.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if JUCE_USE_CDREADER

namespace CDReaderHelpers
{

//***************************************************************************
//	   %%% TARGET STATUS VALUES %%%
//***************************************************************************
#define STATUS_GOOD	 0x00	// Status Good
#define STATUS_CHKCOND	  0x02	// Check Condition
#define STATUS_CONDMET	  0x04	// Condition Met
#define STATUS_BUSY	 0x08	// Busy
#define STATUS_INTERM	   0x10	// Intermediate
#define STATUS_INTCDMET	 0x14	// Intermediate-condition met
#define STATUS_RESCONF	  0x18	// Reservation conflict
#define STATUS_COMTERM	  0x22	// Command Terminated
#define STATUS_QFULL	0x28	// Queue full

//***************************************************************************
//	  %%% SCSI MISCELLANEOUS EQUATES %%%
//***************************************************************************
#define MAXLUN		  7	   // Maximum Logical Unit Id
#define MAXTARG		 7	   // Maximum Target Id
#define MAX_SCSI_LUNS	   64	  // Maximum Number of SCSI LUNs
#define MAX_NUM_HA	  8	   // Maximum Number of SCSI HA's

//***************************************************************************
//	 %%% Commands for all Device Types %%%
//***************************************************************************
#define SCSI_CHANGE_DEF	 0x40	// Change Definition (Optional)
#define SCSI_COMPARE	0x39	// Compare (O)
#define SCSI_COPY	   0x18	// Copy (O)
#define SCSI_COP_VERIFY	 0x3A	// Copy and Verify (O)
#define SCSI_INQUIRY	0x12	// Inquiry (MANDATORY)
#define SCSI_LOG_SELECT	 0x4C	// Log Select (O)
#define SCSI_LOG_SENSE	  0x4D	// Log Sense (O)
#define SCSI_MODE_SEL6	  0x15	// Mode Select 6-byte (Device Specific)
#define SCSI_MODE_SEL10	 0x55	// Mode Select 10-byte (Device Specific)
#define SCSI_MODE_SEN6	  0x1A	// Mode Sense 6-byte (Device Specific)
#define SCSI_MODE_SEN10	 0x5A	// Mode Sense 10-byte (Device Specific)
#define SCSI_READ_BUFF	  0x3C	// Read Buffer (O)
#define SCSI_REQ_SENSE	  0x03	// Request Sense (MANDATORY)
#define SCSI_SEND_DIAG	  0x1D	// Send Diagnostic (O)
#define SCSI_TST_U_RDY	  0x00	// Test Unit Ready (MANDATORY)
#define SCSI_WRITE_BUFF	 0x3B	// Write Buffer (O)

//***************************************************************************
//	 %%% Commands Unique to Direct Access Devices %%%
//***************************************************************************
#define SCSI_COMPARE	0x39	// Compare (O)
#define SCSI_FORMAT	 0x04	// Format Unit (MANDATORY)
#define SCSI_LCK_UN_CAC	 0x36	// Lock Unlock Cache (O)
#define SCSI_PREFETCH	   0x34	// Prefetch (O)
#define SCSI_MED_REMOVL	 0x1E	// Prevent/Allow medium Removal (O)
#define SCSI_READ6	  0x08	// Read 6-byte (MANDATORY)
#define SCSI_READ10	 0x28	// Read 10-byte (MANDATORY)
#define SCSI_RD_CAPAC	   0x25	// Read Capacity (MANDATORY)
#define SCSI_RD_DEFECT	  0x37	// Read Defect Data (O)
#define SCSI_READ_LONG	  0x3E	// Read Long (O)
#define SCSI_REASS_BLK	  0x07	// Reassign Blocks (O)
#define SCSI_RCV_DIAG	   0x1C	// Receive Diagnostic Results (O)
#define SCSI_RELEASE	0x17	// Release Unit (MANDATORY)
#define SCSI_REZERO	 0x01	// Rezero Unit (O)
#define SCSI_SRCH_DAT_E	 0x31	// Search Data Equal (O)
#define SCSI_SRCH_DAT_H	 0x30	// Search Data High (O)
#define SCSI_SRCH_DAT_L	 0x32	// Search Data Low (O)
#define SCSI_SEEK6	  0x0B	// Seek 6-Byte (O)
#define SCSI_SEEK10	 0x2B	// Seek 10-Byte (O)
#define SCSI_SEND_DIAG	  0x1D	// Send Diagnostics (MANDATORY)
#define SCSI_SET_LIMIT	  0x33	// Set Limits (O)
#define SCSI_START_STP	  0x1B	// Start/Stop Unit (O)
#define SCSI_SYNC_CACHE	 0x35	// Synchronize Cache (O)
#define SCSI_VERIFY	 0x2F	// Verify (O)
#define SCSI_WRITE6	 0x0A	// Write 6-Byte (MANDATORY)
#define SCSI_WRITE10	0x2A	// Write 10-Byte (MANDATORY)
#define SCSI_WRT_VERIFY	 0x2E	// Write and Verify (O)
#define SCSI_WRITE_LONG	 0x3F	// Write Long (O)
#define SCSI_WRITE_SAME	 0x41	// Write Same (O)

//***************************************************************************
//   %%% Commands Unique to Sequential Access Devices %%%
//***************************************************************************
#define SCSI_ERASE	  0x19	// Erase (MANDATORY)
#define SCSI_LOAD_UN	0x1b	// Load/Unload (O)
#define SCSI_LOCATE	 0x2B	// Locate (O)
#define SCSI_RD_BLK_LIM	 0x05	// Read Block Limits (MANDATORY)
#define SCSI_READ_POS	   0x34	// Read Position (O)
#define SCSI_READ_REV	   0x0F	// Read Reverse (O)
#define SCSI_REC_BF_DAT	 0x14	// Recover Buffer Data (O)
#define SCSI_RESERVE	0x16	// Reserve Unit (MANDATORY)
#define SCSI_REWIND	 0x01	// Rewind (MANDATORY)
#define SCSI_SPACE	  0x11	// Space (MANDATORY)
#define SCSI_VERIFY_T	   0x13	// Verify (Tape) (O)
#define SCSI_WRT_FILE	   0x10	// Write Filemarks (MANDATORY)

//***************************************************************************
//	  %%% Commands Unique to Printer Devices %%%
//***************************************************************************
#define SCSI_PRINT	  0x0A	// Print (MANDATORY)
#define SCSI_SLEW_PNT	   0x0B	// Slew and Print (O)
#define SCSI_STOP_PNT	   0x1B	// Stop Print (O)
#define SCSI_SYNC_BUFF	  0x10	// Synchronize Buffer (O)

//***************************************************************************
//	 %%% Commands Unique to Processor Devices %%%
//***************************************************************************
#define SCSI_RECEIVE	0x08	// Receive (O)
#define SCSI_SEND	   0x0A	// Send (O)

//***************************************************************************
//	%%% Commands Unique to Write-Once Devices %%%
//***************************************************************************
#define SCSI_MEDIUM_SCN	 0x38	// Medium Scan (O)
#define SCSI_SRCHDATE10	 0x31	// Search Data Equal 10-Byte (O)
#define SCSI_SRCHDATE12	 0xB1	// Search Data Equal 12-Byte (O)
#define SCSI_SRCHDATH10	 0x30	// Search Data High 10-Byte (O)
#define SCSI_SRCHDATH12	 0xB0	// Search Data High 12-Byte (O)
#define SCSI_SRCHDATL10	 0x32	// Search Data Low 10-Byte (O)
#define SCSI_SRCHDATL12	 0xB2	// Search Data Low 12-Byte (O)
#define SCSI_SET_LIM_10	 0x33	// Set Limits 10-Byte (O)
#define SCSI_SET_LIM_12	 0xB3	// Set Limits 10-Byte (O)
#define SCSI_VERIFY10	   0x2F	// Verify 10-Byte (O)
#define SCSI_VERIFY12	   0xAF	// Verify 12-Byte (O)
#define SCSI_WRITE12	0xAA	// Write 12-Byte (O)
#define SCSI_WRT_VER10	  0x2E	// Write and Verify 10-Byte (O)
#define SCSI_WRT_VER12	  0xAE	// Write and Verify 12-Byte (O)

//***************************************************************************
//	  %%% Commands Unique to CD-ROM Devices %%%
//***************************************************************************
#define SCSI_PLAYAUD_10	 0x45	// Play Audio 10-Byte (O)
#define SCSI_PLAYAUD_12	 0xA5	// Play Audio 12-Byte 12-Byte (O)
#define SCSI_PLAYAUDMSF	 0x47	// Play Audio MSF (O)
#define SCSI_PLAYA_TKIN	 0x48	// Play Audio Track/Index (O)
#define SCSI_PLYTKREL10	 0x49	// Play Track Relative 10-Byte (O)
#define SCSI_PLYTKREL12	 0xA9	// Play Track Relative 12-Byte (O)
#define SCSI_READCDCAP	  0x25	// Read CD-ROM Capacity (MANDATORY)
#define SCSI_READHEADER	 0x44	// Read Header (O)
#define SCSI_SUBCHANNEL	 0x42	// Read Subchannel (O)
#define SCSI_READ_TOC	   0x43	// Read TOC (O)

//***************************************************************************
//	  %%% Commands Unique to Scanner Devices %%%
//***************************************************************************
#define SCSI_GETDBSTAT	  0x34	// Get Data Buffer Status (O)
#define SCSI_GETWINDOW	  0x25	// Get Window (O)
#define SCSI_OBJECTPOS	  0x31	// Object Postion (O)
#define SCSI_SCAN	   0x1B	// Scan (O)
#define SCSI_SETWINDOW	  0x24	// Set Window (MANDATORY)

//***************************************************************************
//	%%% Commands Unique to Optical Memory Devices %%%
//***************************************************************************
#define SCSI_UpdateBlk	  0x3D	// Update Block (O)

//***************************************************************************
//	%%% Commands Unique to Medium Changer Devices %%%
//***************************************************************************
#define SCSI_EXCHMEDIUM	 0xA6	// Exchange Medium (O)
#define SCSI_INITELSTAT	 0x07	// Initialize Element Status (O)
#define SCSI_POSTOELEM	  0x2B	// Position to Element (O)
#define SCSI_REQ_VE_ADD	 0xB5	// Request Volume Element Address (O)
#define SCSI_SENDVOLTAG	 0xB6	// Send Volume Tag (O)

//***************************************************************************
//	 %%% Commands Unique to Communication Devices %%%
//***************************************************************************
#define SCSI_GET_MSG_6	  0x08	// Get Message 6-Byte (MANDATORY)
#define SCSI_GET_MSG_10	 0x28	// Get Message 10-Byte (O)
#define SCSI_GET_MSG_12	 0xA8	// Get Message 12-Byte (O)
#define SCSI_SND_MSG_6	  0x0A	// Send Message 6-Byte (MANDATORY)
#define SCSI_SND_MSG_10	 0x2A	// Send Message 10-Byte (O)
#define SCSI_SND_MSG_12	 0xAA	// Send Message 12-Byte (O)

//***************************************************************************
//	  %%% Request Sense Data Format %%%
//***************************************************************************
typedef struct {
 BYTE  ErrorCode;	   // Error Code (70H or 71H)
 BYTE  SegmentNum;	  // Number of current segment descriptor
 BYTE  SenseKey;	// Sense Key(See bit definitions too)
 BYTE  InfoByte0;	   // Information MSB
 BYTE  InfoByte1;	   // Information MID
 BYTE  InfoByte2;	   // Information MID
 BYTE  InfoByte3;	   // Information LSB
 BYTE  AddSenLen;	   // Additional Sense Length
 BYTE  ComSpecInf0;	 // Command Specific Information MSB
 BYTE  ComSpecInf1;	 // Command Specific Information MID
 BYTE  ComSpecInf2;	 // Command Specific Information MID
 BYTE  ComSpecInf3;	 // Command Specific Information LSB
 BYTE  AddSenseCode;	// Additional Sense Code
 BYTE  AddSenQual;	  // Additional Sense Code Qualifier
 BYTE  FieldRepUCode;   // Field Replaceable Unit Code
 BYTE  SenKeySpec15;	// Sense Key Specific 15th byte
 BYTE  SenKeySpec16;	// Sense Key Specific 16th byte
 BYTE  SenKeySpec17;	// Sense Key Specific 17th byte
 BYTE  AddSenseBytes;   // Additional Sense Bytes
} SENSE_DATA_FMT;

//***************************************************************************
//	   %%% REQUEST SENSE ERROR CODE %%%
//***************************************************************************
#define SERROR_CURRENT	  0x70	// Current Errors
#define SERROR_DEFERED	  0x71	// Deferred Errors

//***************************************************************************
//	  %%% REQUEST SENSE BIT DEFINITIONS %%%
//***************************************************************************
#define SENSE_VALID	 0x80	// Byte 0 Bit 7
#define SENSE_FILEMRK	   0x80	// Byte 2 Bit 7
#define SENSE_EOM	   0x40	// Byte 2 Bit 6
#define SENSE_ILI	   0x20	// Byte 2 Bit 5

//***************************************************************************
//	 %%% REQUEST SENSE SENSE KEY DEFINITIONS %%%
//***************************************************************************
#define KEY_NOSENSE	 0x00	// No Sense
#define KEY_RECERROR	0x01	// Recovered Error
#define KEY_NOTREADY	0x02	// Not Ready
#define KEY_MEDIUMERR	   0x03	// Medium Error
#define KEY_HARDERROR	   0x04	// Hardware Error
#define KEY_ILLGLREQ	0x05	// Illegal Request
#define KEY_UNITATT	 0x06	// Unit Attention
#define KEY_DATAPROT	0x07	// Data Protect
#define KEY_BLANKCHK	0x08	// Blank Check
#define KEY_VENDSPEC	0x09	// Vendor Specific
#define KEY_COPYABORT	   0x0A	// Copy Abort
#define KEY_EQUAL	   0x0C	// Equal (Search)
#define KEY_VOLOVRFLW	   0x0D	// Volume Overflow
#define KEY_MISCOMP	 0x0E	// Miscompare (Search)
#define KEY_RESERVED	0x0F	// Reserved

//***************************************************************************
//	  %%% PERIPHERAL DEVICE TYPE DEFINITIONS %%%
//***************************************************************************
#define DTYPE_DASD	  0x00	// Disk Device
#define DTYPE_SEQD	  0x01	// Tape Device
#define DTYPE_PRNT	  0x02	// Printer
#define DTYPE_PROC	  0x03	// Processor
#define DTYPE_WORM	  0x04	// Write-once read-multiple
#define DTYPE_CROM	  0x05	// CD-ROM device
#define DTYPE_SCAN	  0x06	// Scanner device
#define DTYPE_OPTI	  0x07	// Optical memory device
#define DTYPE_JUKE	  0x08	// Medium Changer device
#define DTYPE_COMM	  0x09	// Communications device
#define DTYPE_RESL	  0x0A	// Reserved (low)
#define DTYPE_RESH	  0x1E	// Reserved (high)
#define DTYPE_UNKNOWN	   0x1F	// Unknown or no device type

//***************************************************************************
//	  %%% ANSI APPROVED VERSION DEFINITIONS %%%
//***************************************************************************
#define ANSI_MAYBE	  0x0	 // Device may or may not be ANSI approved stand
#define ANSI_SCSI1	  0x1	 // Device complies to ANSI X3.131-1986 (SCSI-1)
#define ANSI_SCSI2	  0x2	 // Device complies to SCSI-2
#define ANSI_RESLO	  0x3	 // Reserved (low)
#define ANSI_RESHI	  0x7	 // Reserved (high)

typedef struct
{
  USHORT Length;
  UCHAR  ScsiStatus;
  UCHAR  PathId;
  UCHAR  TargetId;
  UCHAR  Lun;
  UCHAR  CdbLength;
  UCHAR  SenseInfoLength;
  UCHAR  DataIn;
  ULONG  DataTransferLength;
  ULONG  TimeOutValue;
  ULONG  DataBufferOffset;
  ULONG  SenseInfoOffset;
  UCHAR  Cdb[16];
} SCSI_PASS_THROUGH, *PSCSI_PASS_THROUGH;

typedef struct
{
  USHORT Length;
  UCHAR  ScsiStatus;
  UCHAR  PathId;
  UCHAR  TargetId;
  UCHAR  Lun;
  UCHAR  CdbLength;
  UCHAR  SenseInfoLength;
  UCHAR  DataIn;
  ULONG  DataTransferLength;
  ULONG  TimeOutValue;
  PVOID  DataBuffer;
  ULONG  SenseInfoOffset;
  UCHAR  Cdb[16];
} SCSI_PASS_THROUGH_DIRECT, *PSCSI_PASS_THROUGH_DIRECT;

typedef struct
{
  SCSI_PASS_THROUGH_DIRECT spt;
  ULONG Filler;
  UCHAR ucSenseBuf[32];
} SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER, *PSCSI_PASS_THROUGH_DIRECT_WITH_BUFFER;

typedef struct
{
  ULONG Length;
  UCHAR PortNumber;
  UCHAR PathId;
  UCHAR TargetId;
  UCHAR Lun;
} SCSI_ADDRESS, *PSCSI_ADDRESS;

#define  METHOD_BUFFERED	 0
#define  METHOD_IN_DIRECT	1
#define  METHOD_OUT_DIRECT   2
#define  METHOD_NEITHER	  3

#define  FILE_ANY_ACCESS	  0
#ifndef FILE_READ_ACCESS
#define  FILE_READ_ACCESS	 (0x0001)
#endif
#ifndef FILE_WRITE_ACCESS
#define  FILE_WRITE_ACCESS	(0x0002)
#endif

#define IOCTL_SCSI_BASE	0x00000004

#define  SCSI_IOCTL_DATA_OUT	  0
#define  SCSI_IOCTL_DATA_IN	   1
#define  SCSI_IOCTL_DATA_UNSPECIFIED  2

#define CTL_CODE2( DevType, Function, Method, Access ) (		 \
	((DevType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
)

#define IOCTL_SCSI_PASS_THROUGH	 CTL_CODE2( IOCTL_SCSI_BASE, 0x0401, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS )
#define IOCTL_SCSI_GET_CAPABILITIES	 CTL_CODE2( IOCTL_SCSI_BASE, 0x0404, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_SCSI_PASS_THROUGH_DIRECT  CTL_CODE2( IOCTL_SCSI_BASE, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS )
#define IOCTL_SCSI_GET_ADDRESS	  CTL_CODE2( IOCTL_SCSI_BASE, 0x0406, METHOD_BUFFERED, FILE_ANY_ACCESS )

#define SENSE_LEN		 14
#define SRB_DIR_SCSI		  0x00
#define SRB_POSTING		   0x01
#define SRB_ENABLE_RESIDUAL_COUNT 0x04
#define SRB_DIR_IN		0x08
#define SRB_DIR_OUT		   0x10
#define SRB_EVENT_NOTIFY	  0x40
#define RESIDUAL_COUNT_SUPPORTED  0x02
#define MAX_SRB_TIMEOUT	   1080001u
#define DEFAULT_SRB_TIMEOUT   1080001u

#define SC_HA_INQUIRY		 0x00
#define SC_GET_DEV_TYPE	   0x01
#define SC_EXEC_SCSI_CMD	  0x02
#define SC_ABORT_SRB		  0x03
#define SC_RESET_DEV		  0x04
#define SC_SET_HA_PARMS	   0x05
#define SC_GET_DISK_INFO	  0x06
#define SC_RESCAN_SCSI_BUS	0x07
#define SC_GETSET_TIMEOUTS	0x08

#define SS_PENDING		0x00
#define SS_COMP		   0x01
#define SS_ABORTED		0x02
#define SS_ABORT_FAIL		 0x03
#define SS_ERR			0x04
#define SS_INVALID_CMD		0x80
#define SS_INVALID_HA		 0x81
#define SS_NO_DEVICE		  0x82
#define SS_INVALID_SRB		0xE0
#define SS_OLD_MANAGER		0xE1
#define SS_BUFFER_ALIGN	   0xE1
#define SS_ILLEGAL_MODE	   0xE2
#define SS_NO_ASPI		0xE3
#define SS_FAILED_INIT		0xE4
#define SS_ASPI_IS_BUSY	   0xE5
#define SS_BUFFER_TO_BIG	  0xE6
#define SS_BUFFER_TOO_BIG	 0xE6
#define SS_MISMATCHED_COMPONENTS  0xE7
#define SS_NO_ADAPTERS		0xE8
#define SS_INSUFFICIENT_RESOURCES 0xE9
#define SS_ASPI_IS_SHUTDOWN	   0xEA
#define SS_BAD_INSTALL		0xEB

#define HASTAT_OK		 0x00
#define HASTAT_SEL_TO		 0x11
#define HASTAT_DO_DU		  0x12
#define HASTAT_BUS_FREE	   0x13
#define HASTAT_PHASE_ERR	  0x14
#define HASTAT_TIMEOUT		0x09
#define HASTAT_COMMAND_TIMEOUT	0x0B
#define HASTAT_MESSAGE_REJECT	 0x0D
#define HASTAT_BUS_RESET	  0x0E
#define HASTAT_PARITY_ERROR	   0x0F
#define HASTAT_REQUEST_SENSE_FAILED 0x10

#define PACKED
#pragma pack(1)

typedef struct
{
  BYTE	 SRB_Cmd;
  BYTE	 SRB_Status;
  BYTE	 SRB_HaID;
  BYTE	 SRB_Flags;
  DWORD	SRB_Hdr_Rsvd;
  BYTE	 HA_Count;
  BYTE	 HA_SCSI_ID;
  BYTE	 HA_ManagerId[16];
  BYTE	 HA_Identifier[16];
  BYTE	 HA_Unique[16];
  WORD	 HA_Rsvd1;
  BYTE	 pad[20];
} PACKED SRB_HAInquiry, *PSRB_HAInquiry, FAR *LPSRB_HAInquiry;

typedef struct
{
  BYTE	 SRB_Cmd;
  BYTE	 SRB_Status;
  BYTE	 SRB_HaID;
  BYTE	 SRB_Flags;
  DWORD	SRB_Hdr_Rsvd;
  BYTE	 SRB_Target;
  BYTE	 SRB_Lun;
  BYTE	 SRB_DeviceType;
  BYTE	 SRB_Rsvd1;
  BYTE	 pad[68];
} PACKED SRB_GDEVBlock, *PSRB_GDEVBlock, FAR *LPSRB_GDEVBlock;

typedef struct
{
  BYTE	 SRB_Cmd;
  BYTE	 SRB_Status;
  BYTE	 SRB_HaID;
  BYTE	 SRB_Flags;
  DWORD	SRB_Hdr_Rsvd;
  BYTE	 SRB_Target;
  BYTE	 SRB_Lun;
  WORD	 SRB_Rsvd1;
  DWORD	SRB_BufLen;
  BYTE FAR *SRB_BufPointer;
  BYTE	 SRB_SenseLen;
  BYTE	 SRB_CDBLen;
  BYTE	 SRB_HaStat;
  BYTE	 SRB_TargStat;
  VOID FAR *SRB_PostProc;
  BYTE	 SRB_Rsvd2[20];
  BYTE	 CDBByte[16];
  BYTE SenseArea[SENSE_LEN+2];
} PACKED SRB_ExecSCSICmd, *PSRB_ExecSCSICmd, FAR *LPSRB_ExecSCSICmd;

typedef struct
{
  BYTE	  SRB_Cmd;
  BYTE	  SRB_Status;
  BYTE	  SRB_HaId;
  BYTE	  SRB_Flags;
  DWORD	 SRB_Hdr_Rsvd;
} PACKED SRB, *PSRB, FAR *LPSRB;

#pragma pack()

struct CDDeviceInfo
{
	char vendor[9];
	char productId[17];
	char rev[5];
	char vendorSpec[21];

	BYTE ha;
	BYTE tgt;
	BYTE lun;
	char scsiDriveLetter; // will be 0 if not using scsi
};

class CDReadBuffer
{
public:
	int startFrame;
	int numFrames;
	int dataStartOffset;
	int dataLength;
	BYTE* buffer;
	int bufferSize;
	int index;
	bool wantsIndex;

	CDReadBuffer (const int numberOfFrames)
		: startFrame (0),
		  numFrames (0),
		  dataStartOffset (0),
		  dataLength (0),
		  index (0),
		  wantsIndex (false)
	{
		bufferSize = 2352 * numberOfFrames;
		buffer = (BYTE*) juce_malloc (bufferSize);
	}

	~CDReadBuffer()
	{
		juce_free (buffer);
	}

	bool isZero() const
	{
		BYTE* p = buffer + dataStartOffset;

		for (int i = dataLength; --i >= 0;)
			if (*p++ != 0)
				return false;

		return true;
	}
};

class CDDeviceHandle;

class CDController
{
public:
	CDController();
	virtual ~CDController();

	virtual bool read (CDReadBuffer* t) = 0;
	virtual void shutDown();

	bool readAudio (CDReadBuffer* t, CDReadBuffer* overlapBuffer = 0);
	int getLastIndex();

public:
	bool initialised;

	CDDeviceHandle* deviceInfo;
	int framesToCheck, framesOverlap;

	void prepare (SRB_ExecSCSICmd& s);
	void perform (SRB_ExecSCSICmd& s);
	void setPaused (bool paused);
};

#pragma pack(1)

struct TOCTRACK
{
	BYTE rsvd;
	BYTE ADR;
	BYTE trackNumber;
	BYTE rsvd2;
	BYTE addr[4];
};

struct TOC
{
	WORD tocLen;
	BYTE firstTrack;
	BYTE lastTrack;
	TOCTRACK tracks[100];
};

#pragma pack()

enum
{
	READTYPE_ANY = 0,
	READTYPE_ATAPI1 = 1,
	READTYPE_ATAPI2 = 2,
	READTYPE_READ6 = 3,
	READTYPE_READ10 = 4,
	READTYPE_READ_D8 = 5,
	READTYPE_READ_D4 = 6,
	READTYPE_READ_D4_1 = 7,
	READTYPE_READ10_2 = 8
};

class CDDeviceHandle
{
public:
	CDDeviceHandle (const CDDeviceInfo* const device)
		: scsiHandle (0),
		  readType (READTYPE_ANY),
		  controller (0)
	{
		memcpy (&info, device, sizeof (info));
	}

	~CDDeviceHandle()
	{
		if (controller != 0)
		{
			controller->shutDown();
			controller = 0;
		}

		if (scsiHandle != 0)
			CloseHandle (scsiHandle);
	}

	bool readTOC (TOC* lpToc, bool useMSF);
	bool readAudio (CDReadBuffer* buffer, CDReadBuffer* overlapBuffer = 0);
	void openDrawer (bool shouldBeOpen);

	CDDeviceInfo info;
	HANDLE scsiHandle;
	BYTE readType;

private:
	ScopedPointer<CDController> controller;

	bool testController (const int readType,
						 CDController* const newController,
						 CDReadBuffer* const bufferToUse);
};

DWORD (*fGetASPI32SupportInfo)(void);
DWORD (*fSendASPI32Command)(LPSRB);

static HINSTANCE winAspiLib = 0;
static bool usingScsi = false;
static bool initialised = false;

static bool InitialiseCDRipper()
{
	if (! initialised)
	{
		initialised = true;

		OSVERSIONINFO info;
		info.dwOSVersionInfoSize = sizeof (info);
		GetVersionEx (&info);

		usingScsi = (info.dwPlatformId == VER_PLATFORM_WIN32_NT) && (info.dwMajorVersion > 4);

		if (! usingScsi)
		{
			fGetASPI32SupportInfo = 0;
			fSendASPI32Command = 0;
			winAspiLib = LoadLibrary (_T("WNASPI32.DLL"));

			if (winAspiLib != 0)
			{
				fGetASPI32SupportInfo = (DWORD(*)(void))  GetProcAddress (winAspiLib, "GetASPI32SupportInfo");
				fSendASPI32Command    = (DWORD(*)(LPSRB)) GetProcAddress (winAspiLib, "SendASPI32Command");

				if (fGetASPI32SupportInfo == 0 || fSendASPI32Command == 0)
					return false;
			}
			else
			{
				usingScsi = true;
			}
		}
	}

	return true;
}

static void DeinitialiseCDRipper()
{
	if (winAspiLib != 0)
	{
		fGetASPI32SupportInfo = 0;
		fSendASPI32Command = 0;
		FreeLibrary (winAspiLib);
		winAspiLib = 0;
	}

	initialised = false;
}

static HANDLE CreateSCSIDeviceHandle (char driveLetter)
{
	TCHAR devicePath[] = { '\\', '\\', '.', '\\', driveLetter, ':', 0, 0 };

	OSVERSIONINFO info;
	info.dwOSVersionInfoSize = sizeof (info);
	GetVersionEx (&info);

	DWORD flags = GENERIC_READ;

	if ((info.dwPlatformId == VER_PLATFORM_WIN32_NT) && (info.dwMajorVersion > 4))
		flags = GENERIC_READ | GENERIC_WRITE;

	HANDLE h = CreateFile (devicePath, flags, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

	if (h == INVALID_HANDLE_VALUE)
	{
		flags ^= GENERIC_WRITE;
		h = CreateFile (devicePath, flags, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
	}

	return h;
}

static DWORD performScsiPassThroughCommand (const LPSRB_ExecSCSICmd srb,
											const char driveLetter,
											HANDLE& deviceHandle,
											const bool retryOnFailure = true)
{
	SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER s;
	zerostruct (s);

	s.spt.Length = sizeof (SCSI_PASS_THROUGH);
	s.spt.CdbLength = srb->SRB_CDBLen;

	s.spt.DataIn = (BYTE) ((srb->SRB_Flags & SRB_DIR_IN)
							? SCSI_IOCTL_DATA_IN
							: ((srb->SRB_Flags & SRB_DIR_OUT)
								? SCSI_IOCTL_DATA_OUT
								: SCSI_IOCTL_DATA_UNSPECIFIED));

	s.spt.DataTransferLength = srb->SRB_BufLen;
	s.spt.TimeOutValue = 5;
	s.spt.DataBuffer = srb->SRB_BufPointer;
	s.spt.SenseInfoOffset = offsetof (SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER, ucSenseBuf);

	memcpy (s.spt.Cdb, srb->CDBByte, srb->SRB_CDBLen);

	srb->SRB_Status = SS_ERR;
	srb->SRB_TargStat = 0x0004;

	DWORD bytesReturned = 0;

	if (DeviceIoControl (deviceHandle, IOCTL_SCSI_PASS_THROUGH_DIRECT,
						 &s, sizeof (s),
						 &s, sizeof (s),
						 &bytesReturned, 0) != 0)
	{
		srb->SRB_Status = SS_COMP;
	}
	else if (retryOnFailure)
	{
		const DWORD error = GetLastError();

		if ((error == ERROR_MEDIA_CHANGED) || (error == ERROR_INVALID_HANDLE))
		{
			if (error != ERROR_INVALID_HANDLE)
				CloseHandle (deviceHandle);

			deviceHandle = CreateSCSIDeviceHandle (driveLetter);

			return performScsiPassThroughCommand (srb, driveLetter, deviceHandle, false);
		}
	}

	return srb->SRB_Status;
}

// Controller types..

class ControllerType1  : public CDController
{
public:
	ControllerType1() {}
	~ControllerType1() {}

	bool read (CDReadBuffer* rb)
	{
		if (rb->numFrames * 2352 > rb->bufferSize)
			return false;

		SRB_ExecSCSICmd s;
		prepare (s);
		s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
		s.SRB_BufLen = rb->bufferSize;
		s.SRB_BufPointer = rb->buffer;
		s.SRB_CDBLen = 12;
		s.CDBByte[0] = 0xBE;
		s.CDBByte[3] = (BYTE)((rb->startFrame >> 16) & 0xFF);
		s.CDBByte[4] = (BYTE)((rb->startFrame >> 8) & 0xFF);
		s.CDBByte[5] = (BYTE)(rb->startFrame & 0xFF);
		s.CDBByte[8] = (BYTE)(rb->numFrames & 0xFF);
		s.CDBByte[9] = (BYTE)((deviceInfo->readType == READTYPE_ATAPI1) ? 0x10 : 0xF0);
		perform (s);

		if (s.SRB_Status != SS_COMP)
			return false;

		rb->dataLength = rb->numFrames * 2352;
		rb->dataStartOffset = 0;
		return true;
	}
};

class ControllerType2  : public CDController
{
public:
	ControllerType2() {}
	~ControllerType2() {}

	void shutDown()
	{
		if (initialised)
		{
			BYTE bufPointer[] = { 0, 0, 0, 8, 83, 0, 0, 0, 0, 0, 8, 0 };

			SRB_ExecSCSICmd s;
			prepare (s);
			s.SRB_Flags = SRB_EVENT_NOTIFY | SRB_ENABLE_RESIDUAL_COUNT;
			s.SRB_BufLen = 0x0C;
			s.SRB_BufPointer = bufPointer;
			s.SRB_CDBLen = 6;
			s.CDBByte[0] = 0x15;
			s.CDBByte[4] = 0x0C;
			perform (s);
		}
	}

	bool init()
	{
		SRB_ExecSCSICmd s;
		s.SRB_Status = SS_ERR;

		if (deviceInfo->readType == READTYPE_READ10_2)
		{
			BYTE bufPointer1[] = { 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 9, 48, 35, 6, 0, 0, 0, 0, 0, 128 };
			BYTE bufPointer2[] = { 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 9, 48, 1, 6, 32, 7, 0, 0, 0, 0 };

			for (int i = 0; i < 2; ++i)
			{
				prepare (s);
				s.SRB_Flags = SRB_EVENT_NOTIFY;
				s.SRB_BufLen = 0x14;
				s.SRB_BufPointer = (i == 0) ? bufPointer1 : bufPointer2;
				s.SRB_CDBLen = 6;
				s.CDBByte[0] = 0x15;
				s.CDBByte[1] = 0x10;
				s.CDBByte[4] = 0x14;
				perform (s);

				if (s.SRB_Status != SS_COMP)
					return false;
			}
		}
		else
		{
			BYTE bufPointer[] = { 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 9, 48 };

			prepare (s);
			s.SRB_Flags = SRB_EVENT_NOTIFY;
			s.SRB_BufLen = 0x0C;
			s.SRB_BufPointer = bufPointer;
			s.SRB_CDBLen = 6;
			s.CDBByte[0] = 0x15;
			s.CDBByte[4] = 0x0C;
			perform (s);
		}

		return s.SRB_Status == SS_COMP;
	}

	bool read (CDReadBuffer* rb)
	{
		if (rb->numFrames * 2352 > rb->bufferSize)
			return false;

		if (!initialised)
		{
			initialised = init();

			if (!initialised)
				return false;
		}

		SRB_ExecSCSICmd s;
		prepare (s);
		s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
		s.SRB_BufLen = rb->bufferSize;
		s.SRB_BufPointer = rb->buffer;
		s.SRB_CDBLen = 10;
		s.CDBByte[0] = 0x28;
		s.CDBByte[1] = (BYTE)(deviceInfo->info.lun << 5);
		s.CDBByte[3] = (BYTE)((rb->startFrame >> 16) & 0xFF);
		s.CDBByte[4] = (BYTE)((rb->startFrame >> 8) & 0xFF);
		s.CDBByte[5] = (BYTE)(rb->startFrame & 0xFF);
		s.CDBByte[8] = (BYTE)(rb->numFrames & 0xFF);
		perform (s);

		if (s.SRB_Status != SS_COMP)
			return false;

		rb->dataLength = rb->numFrames * 2352;
		rb->dataStartOffset = 0;

		return true;
	}
};

class ControllerType3  : public CDController
{
public:
	ControllerType3() {}
	~ControllerType3() {}

	bool read (CDReadBuffer* rb)
	{
		if (rb->numFrames * 2352 > rb->bufferSize)
			return false;

		if (!initialised)
		{
			setPaused (false);
			initialised = true;
		}

		SRB_ExecSCSICmd s;
		prepare (s);
		s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
		s.SRB_BufLen = rb->numFrames * 2352;
		s.SRB_BufPointer = rb->buffer;
		s.SRB_CDBLen = 12;
		s.CDBByte[0] = 0xD8;
		s.CDBByte[3] = (BYTE)((rb->startFrame >> 16) & 0xFF);
		s.CDBByte[4] = (BYTE)((rb->startFrame >> 8) & 0xFF);
		s.CDBByte[5] = (BYTE)(rb->startFrame & 0xFF);
		s.CDBByte[9] = (BYTE)(rb->numFrames & 0xFF);
		perform (s);

		if (s.SRB_Status != SS_COMP)
			return false;

		rb->dataLength = rb->numFrames * 2352;
		rb->dataStartOffset = 0;

		return true;
	}
};

class ControllerType4  : public CDController
{
public:
	ControllerType4() {}
	~ControllerType4() {}

	bool selectD4Mode()
	{
		BYTE bufPointer[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 48 };

		SRB_ExecSCSICmd s;
		prepare (s);
		s.SRB_Flags = SRB_EVENT_NOTIFY;
		s.SRB_CDBLen = 6;
		s.SRB_BufLen = 12;
		s.SRB_BufPointer = bufPointer;
		s.CDBByte[0] = 0x15;
		s.CDBByte[1] = 0x10;
		s.CDBByte[4] = 0x08;
		perform (s);

		return s.SRB_Status == SS_COMP;
	}

	bool read (CDReadBuffer* rb)
	{
		if (rb->numFrames * 2352 > rb->bufferSize)
			return false;

		if (!initialised)
		{
			setPaused (true);

			if (deviceInfo->readType == READTYPE_READ_D4_1)
				selectD4Mode();

			initialised = true;
		}

		SRB_ExecSCSICmd s;
		prepare (s);
		s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
		s.SRB_BufLen = rb->bufferSize;
		s.SRB_BufPointer = rb->buffer;
		s.SRB_CDBLen = 10;
		s.CDBByte[0] = 0xD4;
		s.CDBByte[3] = (BYTE)((rb->startFrame >> 16) & 0xFF);
		s.CDBByte[4] = (BYTE)((rb->startFrame >> 8) & 0xFF);
		s.CDBByte[5] = (BYTE)(rb->startFrame & 0xFF);
		s.CDBByte[8] = (BYTE)(rb->numFrames & 0xFF);
		perform (s);

		if (s.SRB_Status != SS_COMP)
			return false;

		rb->dataLength = rb->numFrames * 2352;
		rb->dataStartOffset = 0;

		return true;
	}
};

CDController::CDController()  : initialised (false)
{
}

CDController::~CDController()
{
}

void CDController::prepare (SRB_ExecSCSICmd& s)
{
	zerostruct (s);

	s.SRB_Cmd = SC_EXEC_SCSI_CMD;
	s.SRB_HaID = deviceInfo->info.ha;
	s.SRB_Target = deviceInfo->info.tgt;
	s.SRB_Lun = deviceInfo->info.lun;
	s.SRB_SenseLen = SENSE_LEN;
}

void CDController::perform (SRB_ExecSCSICmd& s)
{
	HANDLE event = CreateEvent (0, TRUE, FALSE, 0);
	s.SRB_PostProc = event;

	ResetEvent (event);

	DWORD status = (usingScsi) ? performScsiPassThroughCommand ((LPSRB_ExecSCSICmd)&s,
																deviceInfo->info.scsiDriveLetter,
																deviceInfo->scsiHandle)
							   : fSendASPI32Command ((LPSRB)&s);

	if (status == SS_PENDING)
		WaitForSingleObject (event, 4000);

	CloseHandle (event);
}

void CDController::setPaused (bool paused)
{
	SRB_ExecSCSICmd s;
	prepare (s);
	s.SRB_Flags = SRB_EVENT_NOTIFY;
	s.SRB_CDBLen = 10;
	s.CDBByte[0] = 0x4B;
	s.CDBByte[8] = (BYTE) (paused ? 0 : 1);
	perform (s);
}

void CDController::shutDown()
{
}

bool CDController::readAudio (CDReadBuffer* rb, CDReadBuffer* overlapBuffer)
{
	if (overlapBuffer != 0)
	{
		const bool canDoJitter = (overlapBuffer->bufferSize >= 2352 * framesToCheck);
		const bool doJitter = canDoJitter && ! overlapBuffer->isZero();

		if (doJitter
			 && overlapBuffer->startFrame > 0
			 && overlapBuffer->numFrames > 0
			 && overlapBuffer->dataLength > 0)
		{
			const int numFrames = rb->numFrames;

			if (overlapBuffer->startFrame == (rb->startFrame - framesToCheck))
			{
				rb->startFrame -= framesOverlap;

				if (framesToCheck < framesOverlap
					 && numFrames + framesOverlap <= rb->bufferSize / 2352)
					rb->numFrames += framesOverlap;
			}
			else
			{
				overlapBuffer->dataLength = 0;
				overlapBuffer->startFrame = 0;
				overlapBuffer->numFrames = 0;
			}
		}

		if (! read (rb))
			return false;

		if (doJitter)
		{
			const int checkLen = framesToCheck * 2352;
			const int maxToCheck = rb->dataLength - checkLen;

			if (overlapBuffer->dataLength == 0 || overlapBuffer->isZero())
				return true;

			BYTE* const p = overlapBuffer->buffer + overlapBuffer->dataStartOffset;
			bool found = false;

			for (int i = 0; i < maxToCheck; ++i)
			{
				if (memcmp (p, rb->buffer + i, checkLen) == 0)
				{
					i += checkLen;
					rb->dataStartOffset = i;
					rb->dataLength -= i;
					rb->startFrame = overlapBuffer->startFrame + framesToCheck;
					found = true;
					break;
				}
			}

			rb->numFrames = rb->dataLength / 2352;
			rb->dataLength = 2352 * rb->numFrames;

			if (!found)
				return false;
		}

		if (canDoJitter)
		{
			memcpy (overlapBuffer->buffer,
					rb->buffer + rb->dataStartOffset + 2352 * (rb->numFrames - framesToCheck),
					2352 * framesToCheck);

			overlapBuffer->startFrame = rb->startFrame + rb->numFrames - framesToCheck;
			overlapBuffer->numFrames = framesToCheck;
			overlapBuffer->dataLength = 2352 * framesToCheck;
			overlapBuffer->dataStartOffset = 0;
		}
		else
		{
			overlapBuffer->startFrame = 0;
			overlapBuffer->numFrames = 0;
			overlapBuffer->dataLength = 0;
		}

		return true;
	}
	else
	{
		return read (rb);
	}
}

int CDController::getLastIndex()
{
	char qdata[100];

	SRB_ExecSCSICmd s;
	prepare (s);
	s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
	s.SRB_BufLen = sizeof (qdata);
	s.SRB_BufPointer = (BYTE*)qdata;
	s.SRB_CDBLen = 12;
	s.CDBByte[0] = 0x42;
	s.CDBByte[1] = (BYTE)(deviceInfo->info.lun << 5);
	s.CDBByte[2] = 64;
	s.CDBByte[3] = 1; // get current position
	s.CDBByte[7] = 0;
	s.CDBByte[8] = (BYTE)sizeof (qdata);
	perform (s);

	if (s.SRB_Status == SS_COMP)
		return qdata[7];

	return 0;
}

bool CDDeviceHandle::readTOC (TOC* lpToc, bool useMSF)
{
	HANDLE event = CreateEvent (0, TRUE, FALSE, 0);

	SRB_ExecSCSICmd s;
	zerostruct (s);

	s.SRB_Cmd = SC_EXEC_SCSI_CMD;
	s.SRB_HaID = info.ha;
	s.SRB_Target = info.tgt;
	s.SRB_Lun = info.lun;
	s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
	s.SRB_BufLen = 0x324;
	s.SRB_BufPointer = (BYTE*)lpToc;
	s.SRB_SenseLen = 0x0E;
	s.SRB_CDBLen = 0x0A;
	s.SRB_PostProc = event;
	s.CDBByte[0] = 0x43;
	s.CDBByte[1] = (BYTE)(useMSF ? 0x02 : 0x00);
	s.CDBByte[7] = 0x03;
	s.CDBByte[8] = 0x24;

	ResetEvent (event);
	DWORD status = (usingScsi) ? performScsiPassThroughCommand ((LPSRB_ExecSCSICmd)&s, info.scsiDriveLetter, scsiHandle)
							   : fSendASPI32Command ((LPSRB)&s);

	if (status == SS_PENDING)
		WaitForSingleObject (event, 4000);

	CloseHandle (event);
	return (s.SRB_Status == SS_COMP);
}

bool CDDeviceHandle::readAudio (CDReadBuffer* const buffer,
								CDReadBuffer* const overlapBuffer)
{
	if (controller == 0)
	{
		   testController (READTYPE_ATAPI2,	new ControllerType1(), buffer)
		|| testController (READTYPE_ATAPI1,	new ControllerType1(), buffer)
		|| testController (READTYPE_READ10_2,  new ControllerType2(), buffer)
		|| testController (READTYPE_READ10,	new ControllerType2(), buffer)
		|| testController (READTYPE_READ_D8,   new ControllerType3(), buffer)
		|| testController (READTYPE_READ_D4,   new ControllerType4(), buffer)
		|| testController (READTYPE_READ_D4_1, new ControllerType4(), buffer);
	}

	buffer->index = 0;

	if ((controller != 0)
		&& controller->readAudio (buffer, overlapBuffer))
	{
		if (buffer->wantsIndex)
			buffer->index = controller->getLastIndex();

		return true;
	}

	return false;
}

void CDDeviceHandle::openDrawer (bool shouldBeOpen)
{
	if (shouldBeOpen)
	{
		if (controller != 0)
		{
			controller->shutDown();
			controller = 0;
		}

		if (scsiHandle != 0)
		{
			CloseHandle (scsiHandle);
			scsiHandle = 0;
		}
	}

	SRB_ExecSCSICmd s;
	zerostruct (s);

	s.SRB_Cmd = SC_EXEC_SCSI_CMD;
	s.SRB_HaID = info.ha;
	s.SRB_Target = info.tgt;
	s.SRB_Lun = info.lun;
	s.SRB_SenseLen = SENSE_LEN;
	s.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
	s.SRB_BufLen = 0;
	s.SRB_BufPointer = 0;
	s.SRB_CDBLen = 12;
	s.CDBByte[0] = 0x1b;
	s.CDBByte[1] = (BYTE)(info.lun << 5);
	s.CDBByte[4] = (BYTE)((shouldBeOpen) ? 2 : 3);

	HANDLE event = CreateEvent (0, TRUE, FALSE, 0);
	s.SRB_PostProc = event;

	ResetEvent (event);

	DWORD status = (usingScsi) ? performScsiPassThroughCommand  ((LPSRB_ExecSCSICmd)&s, info.scsiDriveLetter, scsiHandle)
							   : fSendASPI32Command ((LPSRB)&s);

	if (status == SS_PENDING)
		WaitForSingleObject (event, 4000);

	CloseHandle (event);
}

bool CDDeviceHandle::testController (const int type,
									 CDController* const newController,
									 CDReadBuffer* const rb)
{
	controller = newController;
	readType = (BYTE)type;

	controller->deviceInfo = this;
	controller->framesToCheck = 1;
	controller->framesOverlap = 3;

	bool passed = false;

	memset (rb->buffer, 0xcd, rb->bufferSize);

	if (controller->read (rb))
	{
		passed = true;
		int* p = (int*) (rb->buffer + rb->dataStartOffset);
		int wrong = 0;

		for (int i = rb->dataLength / 4; --i >= 0;)
		{
			if (*p++ == (int) 0xcdcdcdcd)
			{
				if (++wrong == 4)
				{
					passed = false;
					break;
				}
			}
			else
			{
				wrong = 0;
			}
		}
	}

	if (! passed)
	{
		controller->shutDown();
		controller = 0;
	}

	return passed;
}

static void GetAspiDeviceInfo (CDDeviceInfo* dev, BYTE ha, BYTE tgt, BYTE lun)
{
	HANDLE event = CreateEvent (0, TRUE, FALSE, 0);

	const int bufSize = 128;
	BYTE buffer[bufSize];
	zeromem (buffer, bufSize);

	SRB_ExecSCSICmd s;
	zerostruct (s);

	s.SRB_Cmd	= SC_EXEC_SCSI_CMD;
	s.SRB_HaID	   = ha;
	s.SRB_Target	 = tgt;
	s.SRB_Lun	= lun;
	s.SRB_Flags	  = SRB_DIR_IN | SRB_EVENT_NOTIFY;
	s.SRB_BufLen	 = bufSize;
	s.SRB_BufPointer = buffer;
	s.SRB_SenseLen   = SENSE_LEN;
	s.SRB_CDBLen	 = 6;
	s.SRB_PostProc   = event;
	s.CDBByte[0]	 = SCSI_INQUIRY;
	s.CDBByte[4]	 = 100;

	ResetEvent (event);

	if (fSendASPI32Command ((LPSRB)&s) == SS_PENDING)
		WaitForSingleObject (event, 4000);

	CloseHandle (event);

	if (s.SRB_Status == SS_COMP)
	{
		memcpy (dev->vendor,	 &buffer[8], 8);
		memcpy (dev->productId,  &buffer[16], 16);
		memcpy (dev->rev,	&buffer[32], 4);
		memcpy (dev->vendorSpec, &buffer[36], 20);
	}
}

static int FindCDDevices (CDDeviceInfo* const list,
						  int maxItems)
{
	int count = 0;

	if (usingScsi)
	{
		for (char driveLetter = 'b'; driveLetter <= 'z'; ++driveLetter)
		{
			TCHAR drivePath[8];
			drivePath[0] = driveLetter;
			drivePath[1] = ':';
			drivePath[2] = '\\';
			drivePath[3] = 0;

			if (GetDriveType (drivePath) == DRIVE_CDROM)
			{
				HANDLE h = CreateSCSIDeviceHandle (driveLetter);

				if (h != INVALID_HANDLE_VALUE)
				{
					BYTE buffer[100], passThroughStruct[1024];
					zeromem (buffer, sizeof (buffer));
					zeromem (passThroughStruct, sizeof (passThroughStruct));

					PSCSI_PASS_THROUGH_DIRECT_WITH_BUFFER p = (PSCSI_PASS_THROUGH_DIRECT_WITH_BUFFER)passThroughStruct;

					p->spt.Length		 = sizeof (SCSI_PASS_THROUGH);
					p->spt.CdbLength	  = 6;
					p->spt.SenseInfoLength	= 24;
					p->spt.DataIn		 = SCSI_IOCTL_DATA_IN;
					p->spt.DataTransferLength = 100;
					p->spt.TimeOutValue	   = 2;
					p->spt.DataBuffer	 = buffer;
					p->spt.SenseInfoOffset	= offsetof (SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER, ucSenseBuf);
					p->spt.Cdb[0]		 = 0x12;
					p->spt.Cdb[4]		 = 100;

					DWORD bytesReturned = 0;

					if (DeviceIoControl (h, IOCTL_SCSI_PASS_THROUGH_DIRECT,
										 p, sizeof (SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER),
										 p, sizeof (SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER),
										 &bytesReturned, 0) != 0)
					{
						zeromem (&list[count], sizeof (CDDeviceInfo));

						list[count].scsiDriveLetter = driveLetter;

						memcpy (list[count].vendor,	 &buffer[8], 8);
						memcpy (list[count].productId,  &buffer[16], 16);
						memcpy (list[count].rev,	&buffer[32], 4);
						memcpy (list[count].vendorSpec, &buffer[36], 20);

						zeromem (passThroughStruct, sizeof (passThroughStruct));
						PSCSI_ADDRESS scsiAddr = (PSCSI_ADDRESS)passThroughStruct;

						scsiAddr->Length = sizeof (SCSI_ADDRESS);

						if (DeviceIoControl (h, IOCTL_SCSI_GET_ADDRESS,
											 0, 0, scsiAddr, sizeof (SCSI_ADDRESS),
											 &bytesReturned, 0) != 0)
						{
							list[count].ha = scsiAddr->PortNumber;
							list[count].tgt = scsiAddr->TargetId;
							list[count].lun = scsiAddr->Lun;
							++count;
						}
					}

					CloseHandle (h);
				}
			}
		}
	}
	else
	{
		const DWORD d = fGetASPI32SupportInfo();
		BYTE status = HIBYTE (LOWORD (d));

		if (status != SS_COMP || status == SS_NO_ADAPTERS)
			return 0;

		const int numAdapters = LOBYTE (LOWORD (d));

		for (BYTE ha = 0; ha < numAdapters; ++ha)
		{
			SRB_HAInquiry s;
			zerostruct (s);

			s.SRB_Cmd = SC_HA_INQUIRY;
			s.SRB_HaID = ha;
			fSendASPI32Command ((LPSRB)&s);

			if (s.SRB_Status == SS_COMP)
			{
				maxItems = (int)s.HA_Unique[3];

				if (maxItems == 0)
					maxItems = 8;

				for (BYTE tgt = 0; tgt < maxItems; ++tgt)
				{
					for (BYTE lun = 0; lun < 8; ++lun)
					{
						SRB_GDEVBlock sb;
						zerostruct (sb);

						sb.SRB_Cmd = SC_GET_DEV_TYPE;
						sb.SRB_HaID = ha;
						sb.SRB_Target = tgt;
						sb.SRB_Lun = lun;
						fSendASPI32Command ((LPSRB) &sb);

						if (sb.SRB_Status == SS_COMP
							 && sb.SRB_DeviceType == DTYPE_CROM)
						{
							zeromem (&list[count], sizeof (CDDeviceInfo));

							list[count].ha = ha;
							list[count].tgt = tgt;
							list[count].lun = lun;

							GetAspiDeviceInfo (&(list[count]), ha, tgt, lun);

							++count;
						}
					}
				}
			}
		}
	}

	return count;
}

static int ripperUsers = 0;
static bool initialisedOk = false;

class DeinitialiseTimer  : private Timer,
						   private DeletedAtShutdown
{
	DeinitialiseTimer (const DeinitialiseTimer&);
	DeinitialiseTimer& operator= (const DeinitialiseTimer&);

public:
	DeinitialiseTimer()
	{
		startTimer (4000);
	}

	~DeinitialiseTimer()
	{
		if (--ripperUsers == 0)
			DeinitialiseCDRipper();
	}

	void timerCallback()
	{
		delete this;
	}

	juce_UseDebuggingNewOperator
};

static void incUserCount()
{
	if (ripperUsers++ == 0)
		initialisedOk = InitialiseCDRipper();
}

static void decUserCount()
{
	new DeinitialiseTimer();
}

struct CDDeviceWrapper
{
	ScopedPointer<CDDeviceHandle> cdH;
	ScopedPointer<CDReadBuffer> overlapBuffer;
	bool jitter;
};

static int getAddressOf (const TOCTRACK* const t)
{
	return (((DWORD)t->addr[0]) << 24) + (((DWORD)t->addr[1]) << 16) +
		   (((DWORD)t->addr[2]) << 8) + ((DWORD)t->addr[3]);
}

static int getMSFAddressOf (const TOCTRACK* const t)
{
	return 60 * t->addr[1] + t->addr[2];
}

static const int samplesPerFrame = 44100 / 75;
static const int bytesPerFrame = samplesPerFrame * 4;

static CDDeviceHandle* openHandle (const CDDeviceInfo* const device)
{
	SRB_GDEVBlock s;
	zerostruct (s);

	s.SRB_Cmd = SC_GET_DEV_TYPE;
	s.SRB_HaID = device->ha;
	s.SRB_Target = device->tgt;
	s.SRB_Lun = device->lun;

	if (usingScsi)
	{
		HANDLE h = CreateSCSIDeviceHandle (device->scsiDriveLetter);

		if (h != INVALID_HANDLE_VALUE)
		{
			CDDeviceHandle* cdh = new CDDeviceHandle (device);
			cdh->scsiHandle = h;
			return cdh;
		}
	}
	else
	{
		if (fSendASPI32Command ((LPSRB)&s) == SS_COMP
			 && s.SRB_DeviceType == DTYPE_CROM)
		{
			return new CDDeviceHandle (device);
		}
	}

	return 0;
}

}

const StringArray AudioCDReader::getAvailableCDNames()
{
	using namespace CDReaderHelpers;
	StringArray results;
	incUserCount();

	if (initialisedOk)
	{
		CDDeviceInfo list[8];
		const int num = FindCDDevices (list, 8);

		decUserCount();

		for (int i = 0; i < num; ++i)
		{
			String s;

			if (list[i].scsiDriveLetter > 0)
				s << String::charToString (list[i].scsiDriveLetter).toUpperCase() << ": ";

			s << String (list[i].vendor).trim()
			  << ' ' << String (list[i].productId).trim()
			  << ' ' << String (list[i].rev).trim();

			results.add (s);
		}
	}

	return results;
}

AudioCDReader* AudioCDReader::createReaderForCD (const int deviceIndex)
{
	using namespace CDReaderHelpers;
	incUserCount();

	if (initialisedOk)
	{
		CDDeviceInfo list[8];
		const int num = FindCDDevices (list, 8);

		if (((unsigned int) deviceIndex) < (unsigned int) num)
		{
			CDDeviceHandle* const handle = openHandle (&(list[deviceIndex]));

			if (handle != 0)
			{
				CDDeviceWrapper* const d = new CDDeviceWrapper();
				d->cdH = handle;
				d->overlapBuffer = new CDReadBuffer(3);

				return new AudioCDReader (d);
			}
		}
	}

	decUserCount();
	return 0;
}

AudioCDReader::AudioCDReader (void* handle_)
	: AudioFormatReader (0, "CD Audio"),
	  handle (handle_),
	  indexingEnabled (false),
	  lastIndex (0),
	  firstFrameInBuffer (0),
	  samplesInBuffer (0)
{
	using namespace CDReaderHelpers;
	jassert (handle_ != 0);

	refreshTrackLengths();

	sampleRate = 44100.0;
	bitsPerSample = 16;
	lengthInSamples = getPositionOfTrackStart (numTracks);
	numChannels = 2;
	usesFloatingPointData = false;

	buffer.setSize (4 * bytesPerFrame, true);
}

AudioCDReader::~AudioCDReader()
{
	using namespace CDReaderHelpers;
	CDDeviceWrapper* const device = (CDDeviceWrapper*) handle;
	delete device;

	decUserCount();
}

bool AudioCDReader::readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
								 int64 startSampleInFile, int numSamples)
{
	using namespace CDReaderHelpers;
	CDDeviceWrapper* const device = (CDDeviceWrapper*) handle;

	bool ok = true;

	while (numSamples > 0)
	{
		const int bufferStartSample = firstFrameInBuffer * samplesPerFrame;
		const int bufferEndSample = bufferStartSample + samplesInBuffer;

		if (startSampleInFile >= bufferStartSample
			 && startSampleInFile < bufferEndSample)
		{
			const int toDo = (int) jmin ((int64) numSamples, bufferEndSample - startSampleInFile);

			int* const l = destSamples[0] + startOffsetInDestBuffer;
			int* const r = numDestChannels > 1 ? (destSamples[1] + startOffsetInDestBuffer) : 0;
			const short* src = (const short*) buffer.getData();
			src += 2 * (startSampleInFile - bufferStartSample);

			for (int i = 0; i < toDo; ++i)
			{
				l[i] = src [i << 1] << 16;

				if (r != 0)
					r[i] = src [(i << 1) + 1] << 16;
			}

			startOffsetInDestBuffer += toDo;
			startSampleInFile += toDo;
			numSamples -= toDo;
		}
		else
		{
			const int framesInBuffer = buffer.getSize() / bytesPerFrame;
			const int frameNeeded = (int) (startSampleInFile / samplesPerFrame);

			if (firstFrameInBuffer + framesInBuffer != frameNeeded)
			{
				device->overlapBuffer->dataLength = 0;
				device->overlapBuffer->startFrame = 0;
				device->overlapBuffer->numFrames = 0;
				device->jitter = false;
			}

			firstFrameInBuffer = frameNeeded;
			lastIndex = 0;

			CDReadBuffer readBuffer (framesInBuffer + 4);
			readBuffer.wantsIndex = indexingEnabled;

			int i;
			for (i = 5; --i >= 0;)
			{
				readBuffer.startFrame = frameNeeded;
				readBuffer.numFrames = framesInBuffer;

				if (device->cdH->readAudio (&readBuffer, (device->jitter) ? device->overlapBuffer : 0))
					break;
				else
					device->overlapBuffer->dataLength = 0;
			}

			if (i >= 0)
			{
				memcpy ((char*) buffer.getData(),
						readBuffer.buffer + readBuffer.dataStartOffset,
						readBuffer.dataLength);

				samplesInBuffer = readBuffer.dataLength >> 2;
				lastIndex = readBuffer.index;
			}
			else
			{
				int* l = destSamples[0] + startOffsetInDestBuffer;
				int* r = numDestChannels > 1 ? (destSamples[1] + startOffsetInDestBuffer) : 0;

				while (--numSamples >= 0)
				{
					*l++ = 0;

					if (r != 0)
						*r++ = 0;
				}

				// sometimes the read fails for just the very last couple of blocks, so
				// we'll ignore and errors in the last half-second of the disk..
				ok = startSampleInFile > (trackStarts [numTracks] - 20000);
				break;
			}
		}
	}

	return ok;
}

bool AudioCDReader::isCDStillPresent() const
{
	using namespace CDReaderHelpers;
	TOC toc;
	zerostruct (toc);

	return ((CDDeviceWrapper*) handle)->cdH->readTOC (&toc, false);
}

int AudioCDReader::getNumTracks() const
{
	return numTracks;
}

int AudioCDReader::getPositionOfTrackStart (int trackNum) const
{
	using namespace CDReaderHelpers;
	return (trackNum >= 0 && trackNum <= numTracks) ? trackStarts [trackNum] * samplesPerFrame
													: 0;
}

void AudioCDReader::refreshTrackLengths()
{
	using namespace CDReaderHelpers;
	zeromem (trackStarts, sizeof (trackStarts));
	zeromem (audioTracks, sizeof (audioTracks));

	TOC toc;
	zerostruct (toc);

	if (((CDDeviceWrapper*)handle)->cdH->readTOC (&toc, false))
	{
		numTracks = 1 + toc.lastTrack - toc.firstTrack;

		for (int i = 0; i <= numTracks; ++i)
		{
			trackStarts[i] = getAddressOf (&toc.tracks[i]);
			audioTracks[i] = ((toc.tracks[i].ADR & 4) == 0);
		}
	}
	else
	{
		numTracks = 0;
	}
}

bool AudioCDReader::isTrackAudio (int trackNum) const
{
	return (trackNum >= 0 && trackNum <= numTracks) ? audioTracks [trackNum]
													: false;
}

void AudioCDReader::enableIndexScanning (bool b)
{
	indexingEnabled = b;
}

int AudioCDReader::getLastIndex() const
{
	return lastIndex;
}

const int framesPerIndexRead = 4;

int AudioCDReader::getIndexAt (int samplePos)
{
	using namespace CDReaderHelpers;
	CDDeviceWrapper* const device = (CDDeviceWrapper*) handle;

	const int frameNeeded = samplePos / samplesPerFrame;

	device->overlapBuffer->dataLength = 0;
	device->overlapBuffer->startFrame = 0;
	device->overlapBuffer->numFrames = 0;
	device->jitter = false;

	firstFrameInBuffer = 0;
	lastIndex = 0;

	CDReadBuffer readBuffer (4 + framesPerIndexRead);
	readBuffer.wantsIndex = true;

	int i;
	for (i = 5; --i >= 0;)
	{
		readBuffer.startFrame = frameNeeded;
		readBuffer.numFrames = framesPerIndexRead;

		if (device->cdH->readAudio (&readBuffer, (false) ? device->overlapBuffer : 0))
			break;
	}

	if (i >= 0)
		return readBuffer.index;

	return -1;
}

const Array <int> AudioCDReader::findIndexesInTrack (const int trackNumber)
{
	using namespace CDReaderHelpers;
	Array <int> indexes;

	const int trackStart = getPositionOfTrackStart (trackNumber);
	const int trackEnd = getPositionOfTrackStart (trackNumber + 1);

	bool needToScan = true;

	if (trackEnd - trackStart > 20 * 44100)
	{
		// check the end of the track for indexes before scanning the whole thing
		needToScan = false;
		int pos = jmax (trackStart, trackEnd - 44100 * 5);
		bool seenAnIndex = false;

		while (pos <= trackEnd - samplesPerFrame)
		{
			const int index = getIndexAt (pos);

			if (index == 0)
			{
				// lead-out, so skip back a bit if we've not found any indexes yet..
				if (seenAnIndex)
					break;

				pos -= 44100 * 5;

				if (pos < trackStart)
					break;
			}
			else
			{
				if (index > 0)
					seenAnIndex = true;

				if (index > 1)
				{
					needToScan = true;
					break;
				}

				pos += samplesPerFrame * framesPerIndexRead;
			}
		}
	}

	if (needToScan)
	{
		CDDeviceWrapper* const device = (CDDeviceWrapper*) handle;

		int pos = trackStart;
		int last = -1;

		while (pos < trackEnd - samplesPerFrame * 10)
		{
			const int frameNeeded = pos / samplesPerFrame;

			device->overlapBuffer->dataLength = 0;
			device->overlapBuffer->startFrame = 0;
			device->overlapBuffer->numFrames = 0;
			device->jitter = false;

			firstFrameInBuffer = 0;

			CDReadBuffer readBuffer (4);
			readBuffer.wantsIndex = true;

			int i;
			for (i = 5; --i >= 0;)
			{
				readBuffer.startFrame = frameNeeded;
				readBuffer.numFrames = framesPerIndexRead;

				if (device->cdH->readAudio (&readBuffer, (false) ? device->overlapBuffer : 0))
					break;
			}

			if (i < 0)
				break;

			if (readBuffer.index > last && readBuffer.index > 1)
			{
				last = readBuffer.index;
				indexes.add (pos);
			}

			pos += samplesPerFrame * framesPerIndexRead;
		}

		indexes.removeValue (trackStart);
	}

	return indexes;
}

int AudioCDReader::getCDDBId()
{
	using namespace CDReaderHelpers;
	refreshTrackLengths();

	if (numTracks > 0)
	{
		TOC toc;
		zerostruct (toc);

		if (((CDDeviceWrapper*) handle)->cdH->readTOC (&toc, true))
		{
			int n = 0;

			for (int i = numTracks; --i >= 0;)
			{
				int j = getMSFAddressOf (&toc.tracks[i]);

				while (j > 0)
				{
					n += (j % 10);
					j /= 10;
				}
			}

			if (n != 0)
			{
				const int t = getMSFAddressOf (&toc.tracks[numTracks])
								- getMSFAddressOf (&toc.tracks[0]);

				return ((n % 0xff) << 24) | (t << 8) | numTracks;
			}
		}
	}

	return 0;
}

void AudioCDReader::ejectDisk()
{
	using namespace CDReaderHelpers;
	((CDDeviceWrapper*) handle)->cdH->openDrawer (true);
}

#endif

#if JUCE_USE_CDBURNER

static IDiscRecorder* enumCDBurners (StringArray* list, int indexToOpen, IDiscMaster** master)
{
	CoInitialize (0);

	IDiscMaster* dm;
	IDiscRecorder* result = 0;

	if (SUCCEEDED (CoCreateInstance (CLSID_MSDiscMasterObj, 0,
									 CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
									 IID_IDiscMaster,
									 (void**) &dm)))
	{
		if (SUCCEEDED (dm->Open()))
		{
			IEnumDiscRecorders* drEnum = 0;

			if (SUCCEEDED (dm->EnumDiscRecorders (&drEnum)))
			{
				IDiscRecorder* dr = 0;
				DWORD dummy;
				int index = 0;

				while (drEnum->Next (1, &dr, &dummy) == S_OK)
				{
					if (indexToOpen == index)
					{
						result = dr;
						break;
					}
					else if (list != 0)
					{
						BSTR path;

						if (SUCCEEDED (dr->GetPath (&path)))
							list->add ((const WCHAR*) path);
					}

					++index;
					dr->Release();
				}

				drEnum->Release();
			}

			if (master == 0)
				dm->Close();
		}

		if (master != 0)
			*master = dm;
		else
			dm->Release();
	}

	return result;
}

class AudioCDBurner::Pimpl  : public IDiscMasterProgressEvents,
							  public Timer
{
public:
	Pimpl (AudioCDBurner& owner_, IDiscMaster* discMaster_, IDiscRecorder* discRecorder_)
	  : owner (owner_), discMaster (discMaster_), discRecorder (discRecorder_), redbook (0),
		listener (0), progress (0), shouldCancel (false), refCount (1)
	{
		HRESULT hr = discMaster->SetActiveDiscMasterFormat (IID_IRedbookDiscMaster, (void**) &redbook);
		jassert (SUCCEEDED (hr));
		hr = discMaster->SetActiveDiscRecorder (discRecorder);
		//jassert (SUCCEEDED (hr));

		lastState = getDiskState();
		startTimer (2000);
	}

	~Pimpl()  {}

	void releaseObjects()
	{
		discRecorder->Close();
		if (redbook != 0)
			redbook->Release();
		discRecorder->Release();
		discMaster->Release();
		Release();
	}

	HRESULT __stdcall QueryInterface (REFIID id, void __RPC_FAR* __RPC_FAR* result)
	{
		if (result == 0)
			return E_POINTER;

		if (id == IID_IUnknown || id == IID_IDiscMasterProgressEvents)
		{
			AddRef();
			*result = this;
			return S_OK;
		}

		*result = 0;
		return E_NOINTERFACE;
	}

	ULONG __stdcall AddRef()	{ return ++refCount; }
	ULONG __stdcall Release()   { jassert (refCount > 0); const int r = --refCount; if (r == 0) delete this; return r; }

	HRESULT __stdcall QueryCancel (boolean* pbCancel)
	{
		if (listener != 0 && ! shouldCancel)
			shouldCancel = listener->audioCDBurnProgress (progress);

		*pbCancel = shouldCancel;

		return S_OK;
	}

	HRESULT __stdcall NotifyBlockProgress (long nCompleted, long nTotal)
	{
		progress = nCompleted / (float) nTotal;
		shouldCancel = listener != 0 && listener->audioCDBurnProgress (progress);

		return E_NOTIMPL;
	}

	HRESULT __stdcall NotifyPnPActivity (void)				  { return E_NOTIMPL; }
	HRESULT __stdcall NotifyAddProgress (long /*nCompletedSteps*/, long /*nTotalSteps*/)	{ return E_NOTIMPL; }
	HRESULT __stdcall NotifyTrackProgress (long /*nCurrentTrack*/, long /*nTotalTracks*/)   { return E_NOTIMPL; }
	HRESULT __stdcall NotifyPreparingBurn (long /*nEstimatedSeconds*/)	  { return E_NOTIMPL; }
	HRESULT __stdcall NotifyClosingDisc (long /*nEstimatedSeconds*/)	{ return E_NOTIMPL; }
	HRESULT __stdcall NotifyBurnComplete (HRESULT /*status*/)		   { return E_NOTIMPL; }
	HRESULT __stdcall NotifyEraseComplete (HRESULT /*status*/)		  { return E_NOTIMPL; }

	class ScopedDiscOpener
	{
	public:
		ScopedDiscOpener (Pimpl& p) : pimpl (p) { pimpl.discRecorder->OpenExclusive(); }
		~ScopedDiscOpener()			 { pimpl.discRecorder->Close(); }

	private:
		Pimpl& pimpl;

		ScopedDiscOpener (const ScopedDiscOpener&);
		ScopedDiscOpener& operator= (const ScopedDiscOpener&);
	};

	DiskState getDiskState()
	{
		const ScopedDiscOpener opener (*this);

		long type, flags;
		HRESULT hr = discRecorder->QueryMediaType (&type, &flags);

		if (FAILED (hr))
			return unknown;

		if (type != 0 && (flags & MEDIA_WRITABLE) != 0)
			return writableDiskPresent;

		if (type == 0)
			return noDisc;
		else
			return readOnlyDiskPresent;
	}

	int getIntProperty (const LPOLESTR name, const int defaultReturn) const
	{
		ComSmartPtr<IPropertyStorage> prop;
		if (FAILED (discRecorder->GetRecorderProperties (&prop)))
			return defaultReturn;

		PROPSPEC iPropSpec;
		iPropSpec.ulKind = PRSPEC_LPWSTR;
		iPropSpec.lpwstr = name;

		PROPVARIANT iPropVariant;
		return FAILED (prop->ReadMultiple (1, &iPropSpec, &iPropVariant))
				   ? defaultReturn : (int) iPropVariant.lVal;
	}

	bool setIntProperty (const LPOLESTR name, const int value) const
	{
		ComSmartPtr<IPropertyStorage> prop;
		if (FAILED (discRecorder->GetRecorderProperties (&prop)))
			return false;

		PROPSPEC iPropSpec;
		iPropSpec.ulKind = PRSPEC_LPWSTR;
		iPropSpec.lpwstr = name;

		PROPVARIANT iPropVariant;
		if (FAILED (prop->ReadMultiple (1, &iPropSpec, &iPropVariant)))
			return false;

		iPropVariant.lVal = (long) value;
		return SUCCEEDED (prop->WriteMultiple (1, &iPropSpec, &iPropVariant, iPropVariant.vt))
				&& SUCCEEDED (discRecorder->SetRecorderProperties (prop));
	}

	void timerCallback()
	{
		const DiskState state = getDiskState();

		if (state != lastState)
		{
			lastState = state;
			owner.sendChangeMessage (&owner);
		}
	}

	AudioCDBurner& owner;
	DiskState lastState;
	IDiscMaster* discMaster;
	IDiscRecorder* discRecorder;
	IRedbookDiscMaster* redbook;
	AudioCDBurner::BurnProgressListener* listener;
	float progress;
	bool shouldCancel;

private:
	int refCount;
};

AudioCDBurner::AudioCDBurner (const int deviceIndex)
{
	IDiscMaster* discMaster = 0;
	IDiscRecorder* discRecorder = enumCDBurners (0, deviceIndex, &discMaster);

	if (discRecorder != 0)
		pimpl = new Pimpl (*this, discMaster, discRecorder);
}

AudioCDBurner::~AudioCDBurner()
{
	if (pimpl != 0)
		pimpl.release()->releaseObjects();
}

const StringArray AudioCDBurner::findAvailableDevices()
{
	StringArray devs;
	enumCDBurners (&devs, -1, 0);
	return devs;
}

AudioCDBurner* AudioCDBurner::openDevice (const int deviceIndex)
{
	ScopedPointer<AudioCDBurner> b (new AudioCDBurner (deviceIndex));

	if (b->pimpl == 0)
		b = 0;

	return b.release();
}

AudioCDBurner::DiskState AudioCDBurner::getDiskState() const
{
	return pimpl->getDiskState();
}

bool AudioCDBurner::isDiskPresent() const
{
	return getDiskState() == writableDiskPresent;
}

bool AudioCDBurner::openTray()
{
	const Pimpl::ScopedDiscOpener opener (*pimpl);
	return SUCCEEDED (pimpl->discRecorder->Eject());
}

AudioCDBurner::DiskState AudioCDBurner::waitUntilStateChange (int timeOutMilliseconds)
{
	const int64 timeout = Time::currentTimeMillis() + timeOutMilliseconds;
	DiskState oldState = getDiskState();
	DiskState newState = oldState;

	while (newState == oldState && Time::currentTimeMillis() < timeout)
	{
		newState = getDiskState();
		Thread::sleep (jmin (250, (int) (timeout - Time::currentTimeMillis())));
	}

	return newState;
}

const Array<int> AudioCDBurner::getAvailableWriteSpeeds() const
{
	Array<int> results;
	const int maxSpeed = pimpl->getIntProperty (L"MaxWriteSpeed", 1);
	const int speeds[] = { 1, 2, 4, 8, 12, 16, 20, 24, 32, 40, 64, 80 };

	for (int i = 0; i < numElementsInArray (speeds); ++i)
		if (speeds[i] <= maxSpeed)
			results.add (speeds[i]);

	results.addIfNotAlreadyThere (maxSpeed);
	return results;
}

bool AudioCDBurner::setBufferUnderrunProtection (const bool shouldBeEnabled)
{
	if (pimpl->getIntProperty (L"BufferUnderrunFreeCapable", 0) == 0)
		return false;

	pimpl->setIntProperty (L"EnableBufferUnderrunFree", shouldBeEnabled ? -1 : 0);
	return pimpl->getIntProperty (L"EnableBufferUnderrunFree", 0) != 0;
}

int AudioCDBurner::getNumAvailableAudioBlocks() const
{
	long blocksFree = 0;
	pimpl->redbook->GetAvailableAudioTrackBlocks (&blocksFree);
	return blocksFree;
}

const String AudioCDBurner::burn (AudioCDBurner::BurnProgressListener* listener, bool ejectDiscAfterwards,
								  bool performFakeBurnForTesting, int writeSpeed)
{
	pimpl->setIntProperty (L"WriteSpeed", writeSpeed > 0 ? writeSpeed : -1);

	pimpl->listener = listener;
	pimpl->progress = 0;
	pimpl->shouldCancel = false;

	UINT_PTR cookie;
	HRESULT hr = pimpl->discMaster->ProgressAdvise ((AudioCDBurner::Pimpl*) pimpl, &cookie);

	hr = pimpl->discMaster->RecordDisc (performFakeBurnForTesting,
										ejectDiscAfterwards);

	String error;
	if (hr != S_OK)
	{
		const char* e = "Couldn't open or write to the CD device";

		if (hr == IMAPI_E_USERABORT)
			e = "User cancelled the write operation";
		else if (hr == IMAPI_E_MEDIUM_NOTPRESENT || hr == IMAPI_E_TRACKOPEN)
			e = "No Disk present";

		error = e;
	}

	pimpl->discMaster->ProgressUnadvise (cookie);
	pimpl->listener = 0;

	return error;
}

bool AudioCDBurner::addAudioTrack (AudioSource* audioSource, int numSamples)
{
	if (audioSource == 0)
		return false;

	ScopedPointer<AudioSource> source (audioSource);

	long bytesPerBlock;
	HRESULT hr = pimpl->redbook->GetAudioBlockSize (&bytesPerBlock);

	const int samplesPerBlock = bytesPerBlock / 4;
	bool ok = true;

	hr = pimpl->redbook->CreateAudioTrack ((long) numSamples / (bytesPerBlock * 4));

	HeapBlock <byte> buffer (bytesPerBlock);
	AudioSampleBuffer sourceBuffer (2, samplesPerBlock);
	int samplesDone = 0;

	source->prepareToPlay (samplesPerBlock, 44100.0);

	while (ok)
	{
		{
			AudioSourceChannelInfo info;
			info.buffer = &sourceBuffer;
			info.numSamples = samplesPerBlock;
			info.startSample = 0;
			sourceBuffer.clear();

			source->getNextAudioBlock (info);
		}

		zeromem (buffer, bytesPerBlock);

		AudioDataConverters::convertFloatToInt16LE (sourceBuffer.getSampleData (0, 0),
													buffer, samplesPerBlock, 4);

		AudioDataConverters::convertFloatToInt16LE (sourceBuffer.getSampleData (1, 0),
													buffer + 2, samplesPerBlock, 4);

		hr = pimpl->redbook->AddAudioTrackBlocks (buffer, bytesPerBlock);

		if (FAILED (hr))
			ok = false;

		samplesDone += samplesPerBlock;

		if (samplesDone >= numSamples)
			break;
	}

	hr = pimpl->redbook->CloseAudioTrack();
	return ok && hr == S_OK;
}

#endif
#endif
/*** End of inlined file: juce_win32_AudioCDReader.cpp ***/


/*** Start of inlined file: juce_win32_Midi.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

using ::free;

namespace MidiConstants
{
	static const int midiBufferSize = 1024 * 10;
	static const int numInHeaders = 32;
	static const int inBufferSize = 256;
}

class MidiInThread  : public Thread
{
public:

	MidiInThread (MidiInput* const input_,
				  MidiInputCallback* const callback_)
		: Thread ("Juce Midi"),
		  hIn (0),
		  input (input_),
		  callback (callback_),
		  isStarted (false),
		  startTime (0),
		  pendingLength(0)
	{
		for (int i = MidiConstants::numInHeaders; --i >= 0;)
		{
			zeromem (&hdr[i], sizeof (MIDIHDR));
			hdr[i].lpData = inData[i];
			hdr[i].dwBufferLength = MidiConstants::inBufferSize;
		}
	};

	~MidiInThread()
	{
		stop();

		if (hIn != 0)
		{
			int count = 5;
			while (--count >= 0)
			{
				if (midiInClose (hIn) == MMSYSERR_NOERROR)
					break;

				Sleep (20);
			}
		}
	}

	void handle (const uint32 message, const uint32 timeStamp) throw()
	{
		const int byte = message & 0xff;
		if (byte < 0x80)
			return;

		const int numBytes = MidiMessage::getMessageLengthFromFirstByte ((uint8) byte);
		const double time = timeStampToTime (timeStamp);

		{
			const ScopedLock sl (lock);

			if (pendingLength < MidiConstants::midiBufferSize - 12)
			{
				char* const p = pending + pendingLength;
				*(double*) p = time;
				*(uint32*) (p + 8) = numBytes;
				*(uint32*) (p + 12) = message;
				pendingLength += 12 + numBytes;
			}
			else
			{
				jassertfalse // midi buffer overflow! You might need to increase the size..
			}
		}

		notify();
	}

	void handleSysEx (MIDIHDR* const hdr, const uint32 timeStamp) throw()
	{
		const int num = hdr->dwBytesRecorded;

		if (num > 0)
		{
			const double time = timeStampToTime (timeStamp);

			{
				const ScopedLock sl (lock);

				if (pendingLength < MidiConstants::midiBufferSize - (8 + num))
				{
					char* const p = pending + pendingLength;
					*(double*) p = time;
					*(uint32*) (p + 8) = num;
					memcpy (p + 12, hdr->lpData, num);
					pendingLength += 12 + num;
				}
				else
				{
					jassertfalse // midi buffer overflow! You might need to increase the size..
				}
			}

			notify();
		}
	}

	void writeBlock (const int i) throw()
	{
		hdr[i].dwBytesRecorded = 0;
		MMRESULT res = midiInPrepareHeader (hIn, &hdr[i], sizeof (MIDIHDR));
		jassert (res == MMSYSERR_NOERROR);
		res = midiInAddBuffer (hIn, &hdr[i], sizeof (MIDIHDR));
		jassert (res == MMSYSERR_NOERROR);
	}

	void run()
	{
		MemoryBlock pendingCopy (64);

		while (! threadShouldExit())
		{
			for (int i = 0; i < MidiConstants::numInHeaders; ++i)
			{
				if ((hdr[i].dwFlags & WHDR_DONE) != 0)
				{
					MMRESULT res = midiInUnprepareHeader (hIn, &hdr[i], sizeof (MIDIHDR));
					(void) res;
					jassert (res == MMSYSERR_NOERROR);
					writeBlock (i);
				}
			}

			int len;

			{
				const ScopedLock sl (lock);

				len = pendingLength;

				if (len > 0)
				{
					pendingCopy.ensureSize (len);
					pendingCopy.copyFrom (pending, 0, len);
					pendingLength = 0;
				}
			}

			//xxx needs to figure out if blocks are broken up or not

			if (len == 0)
			{
				wait (500);
			}
			else
			{
				const char* p = (const char*) pendingCopy.getData();

				while (len > 0)
				{
					const double time = *(const double*) p;
					const int messageLen = *(const int*) (p + 8);

					const MidiMessage message ((const uint8*) (p + 12), messageLen, time);

					callback->handleIncomingMidiMessage (input, message);

					p += 12 + messageLen;
					len -= 12 + messageLen;
				}
			}
		}
	}

	void start() throw()
	{
		jassert (hIn != 0);
		if (hIn != 0 && ! isStarted)
		{
			stop();

			activeMidiThreads.addIfNotAlreadyThere (this);

			int i;
			for (i = 0; i < MidiConstants::numInHeaders; ++i)
				writeBlock (i);

			startTime = Time::getMillisecondCounter();
			MMRESULT res = midiInStart (hIn);

			jassert (res == MMSYSERR_NOERROR);

			if (res == MMSYSERR_NOERROR)
			{
				isStarted = true;
				pendingLength = 0;
				startThread (6);
			}
		}
	}

	void stop() throw()
	{
		if (isStarted)
		{
			stopThread (5000);

			midiInReset (hIn);
			midiInStop (hIn);

			activeMidiThreads.removeValue (this);

			{ const ScopedLock sl (lock); }

			for (int i = MidiConstants::numInHeaders; --i >= 0;)
			{
				if ((hdr[i].dwFlags & WHDR_DONE) != 0)
				{
					int c = 10;
					while (--c >= 0 && midiInUnprepareHeader (hIn, &hdr[i], sizeof (MIDIHDR)) == MIDIERR_STILLPLAYING)
						Sleep (20);

					jassert (c >= 0);
				}
			}

			isStarted = false;
			pendingLength = 0;
		}
	}

	static void CALLBACK midiInCallback (HMIDIIN, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR midiMessage, DWORD_PTR timeStamp)
	{
		MidiInThread* const thread = reinterpret_cast <MidiInThread*> (dwInstance);

		if (thread != 0 && activeMidiThreads.contains (thread))
		{
			if (uMsg == MIM_DATA)
				thread->handle ((uint32) midiMessage, (uint32) timeStamp);
			else if (uMsg == MIM_LONGDATA)
				thread->handleSysEx ((MIDIHDR*) midiMessage, (uint32) timeStamp);
		}
	}

	juce_UseDebuggingNewOperator

	HMIDIIN hIn;

private:
	static Array <void*, CriticalSection> activeMidiThreads;

	MidiInput* input;
	MidiInputCallback* callback;
	bool isStarted;
	uint32 startTime;
	CriticalSection lock;

	MIDIHDR hdr [MidiConstants::numInHeaders];
	char inData [MidiConstants::numInHeaders] [MidiConstants::inBufferSize];

	int pendingLength;
	char pending [MidiConstants::midiBufferSize];

	double timeStampToTime (uint32 timeStamp) throw()
	{
		timeStamp += startTime;

		const uint32 now = Time::getMillisecondCounter();
		if (timeStamp > now)
		{
			if (timeStamp > now + 2)
				--startTime;

			timeStamp = now;
		}

		return 0.001 * timeStamp;
	}

	MidiInThread (const MidiInThread&);
	MidiInThread& operator= (const MidiInThread&);
};

Array <void*, CriticalSection> MidiInThread::activeMidiThreads;

const StringArray MidiInput::getDevices()
{
	StringArray s;
	const int num = midiInGetNumDevs();

	for (int i = 0; i < num; ++i)
	{
		MIDIINCAPS mc;
		zerostruct (mc);

		if (midiInGetDevCaps (i, &mc, sizeof (mc)) == MMSYSERR_NOERROR)
			s.add (String (mc.szPname, sizeof (mc.szPname)));
	}

	return s;
}

int MidiInput::getDefaultDeviceIndex()
{
	return 0;
}

MidiInput* MidiInput::openDevice (const int index, MidiInputCallback* const callback)
{
	if (callback == 0)
		return 0;

	UINT deviceId = MIDI_MAPPER;
	int n = 0;
	String name;

	const int num = midiInGetNumDevs();

	for (int i = 0; i < num; ++i)
	{
		MIDIINCAPS mc;
		zerostruct (mc);

		if (midiInGetDevCaps (i, &mc, sizeof (mc)) == MMSYSERR_NOERROR)
		{
			if (index == n)
			{
				deviceId = i;
				name = String (mc.szPname, sizeof (mc.szPname));
				break;
			}

			++n;
		}
	}

	ScopedPointer <MidiInput> in (new MidiInput (name));
	ScopedPointer <MidiInThread> thread (new MidiInThread (in, callback));

	HMIDIIN h;
	HRESULT err = midiInOpen (&h, deviceId,
							  (DWORD_PTR) &MidiInThread::midiInCallback,
							  (DWORD_PTR) (MidiInThread*) thread,
							  CALLBACK_FUNCTION);

	if (err == MMSYSERR_NOERROR)
	{
		thread->hIn = h;
		in->internal = thread.release();
		return in.release();
	}

	return 0;
}

MidiInput::MidiInput (const String& name_)
	: name (name_),
	  internal (0)
{
}

MidiInput::~MidiInput()
{
	delete static_cast <MidiInThread*> (internal);
}

void MidiInput::start()
{
	static_cast <MidiInThread*> (internal)->start();
}

void MidiInput::stop()
{
	static_cast <MidiInThread*> (internal)->stop();
}

struct MidiOutHandle
{
	int refCount;
	UINT deviceId;
	HMIDIOUT handle;

	static Array<MidiOutHandle*> activeHandles;

	juce_UseDebuggingNewOperator
};

Array<MidiOutHandle*> MidiOutHandle::activeHandles;

const StringArray MidiOutput::getDevices()
{
	StringArray s;
	const int num = midiOutGetNumDevs();

	for (int i = 0; i < num; ++i)
	{
		MIDIOUTCAPS mc;
		zerostruct (mc);

		if (midiOutGetDevCaps (i, &mc, sizeof (mc)) == MMSYSERR_NOERROR)
			s.add (String (mc.szPname, sizeof (mc.szPname)));
	}

	return s;
}

int MidiOutput::getDefaultDeviceIndex()
{
	const int num = midiOutGetNumDevs();
	int n = 0;

	for (int i = 0; i < num; ++i)
	{
		MIDIOUTCAPS mc;
		zerostruct (mc);

		if (midiOutGetDevCaps (i, &mc, sizeof (mc)) == MMSYSERR_NOERROR)
		{
			if ((mc.wTechnology & MOD_MAPPER) != 0)
				return n;

			 ++n;
		}
	}

	return 0;
}

MidiOutput* MidiOutput::openDevice (int index)
{
	UINT deviceId = MIDI_MAPPER;
	const int num = midiOutGetNumDevs();
	int i, n = 0;

	for (i = 0; i < num; ++i)
	{
		MIDIOUTCAPS mc;
		zerostruct (mc);

		if (midiOutGetDevCaps (i, &mc, sizeof (mc)) == MMSYSERR_NOERROR)
		{
			// use the microsoft sw synth as a default - best not to allow deviceId
			// to be MIDI_MAPPER, or else device sharing breaks
			if (String (mc.szPname, sizeof (mc.szPname)).containsIgnoreCase ("microsoft"))
				deviceId = i;

			if (index == n)
			{
				deviceId = i;
				break;
			}

			++n;
		}
	}

	for (i = MidiOutHandle::activeHandles.size(); --i >= 0;)
	{
		MidiOutHandle* const han = MidiOutHandle::activeHandles.getUnchecked(i);

		if (han != 0 && han->deviceId == deviceId)
		{
			han->refCount++;

			MidiOutput* const out = new MidiOutput();
			out->internal = han;
			return out;
		}
	}

	for (i = 4; --i >= 0;)
	{
		HMIDIOUT h = 0;
		MMRESULT res = midiOutOpen (&h, deviceId, 0, 0, CALLBACK_NULL);

		if (res == MMSYSERR_NOERROR)
		{
			MidiOutHandle* const han = new MidiOutHandle();
			han->deviceId = deviceId;
			han->refCount = 1;
			han->handle = h;
			MidiOutHandle::activeHandles.add (han);

			MidiOutput* const out = new MidiOutput();
			out->internal = han;
			return out;
		}
		else if (res == MMSYSERR_ALLOCATED)
		{
			Sleep (100);
		}
		else
		{
			break;
		}
	}

	return 0;
}

MidiOutput::~MidiOutput()
{
	MidiOutHandle* const h = static_cast <MidiOutHandle*> (internal);

	if (MidiOutHandle::activeHandles.contains (h) && --(h->refCount) == 0)
	{
		midiOutClose (h->handle);
		MidiOutHandle::activeHandles.removeValue (h);
		delete h;
	}
}

void MidiOutput::reset()
{
	const MidiOutHandle* const h = static_cast <const MidiOutHandle*> (internal);
	midiOutReset (h->handle);
}

bool MidiOutput::getVolume (float& leftVol,
							float& rightVol)
{
	const MidiOutHandle* const handle = static_cast <const MidiOutHandle*> (internal);

	DWORD n;
	if (midiOutGetVolume (handle->handle, &n) == MMSYSERR_NOERROR)
	{
		const unsigned short* const nn = (const unsigned short*) &n;
		rightVol = nn[0] / (float) 0xffff;
		leftVol = nn[1] / (float) 0xffff;
		return true;
	}
	else
	{
		rightVol = leftVol = 1.0f;
		return false;
	}
}

void MidiOutput::setVolume (float leftVol,
							float rightVol)
{
	const MidiOutHandle* const handle = static_cast <MidiOutHandle*> (internal);

	DWORD n;
	unsigned short* const nn = (unsigned short*) &n;
	nn[0] = (unsigned short) jlimit (0, 0xffff, (int) (rightVol * 0xffff));
	nn[1] = (unsigned short) jlimit (0, 0xffff, (int) (leftVol * 0xffff));
	midiOutSetVolume (handle->handle, n);
}

void MidiOutput::sendMessageNow (const MidiMessage& message)
{
	const MidiOutHandle* const handle = static_cast <const MidiOutHandle*> (internal);

	if (message.getRawDataSize() > 3
		 || message.isSysEx())
	{
		MIDIHDR h;
		zerostruct (h);

		h.lpData = (char*) message.getRawData();
		h.dwBufferLength = message.getRawDataSize();
		h.dwBytesRecorded = message.getRawDataSize();

		if (midiOutPrepareHeader (handle->handle, &h, sizeof (MIDIHDR)) == MMSYSERR_NOERROR)
		{
			MMRESULT res = midiOutLongMsg (handle->handle, &h, sizeof (MIDIHDR));

			if (res == MMSYSERR_NOERROR)
			{
				while ((h.dwFlags & MHDR_DONE) == 0)
					Sleep (1);

				int count = 500; // 1 sec timeout

				while (--count >= 0)
				{
					res = midiOutUnprepareHeader (handle->handle, &h, sizeof (MIDIHDR));

					if (res == MIDIERR_STILLPLAYING)
						Sleep (2);
					else
						break;
				}
			}
		}
	}
	else
	{
		midiOutShortMsg (handle->handle,
						 *(unsigned int*) message.getRawData());
	}
}

#endif
/*** End of inlined file: juce_win32_Midi.cpp ***/


/*** Start of inlined file: juce_win32_ASIO.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_ASIO

#undef WINDOWS

// #define ASIO_DEBUGGING

#ifdef ASIO_DEBUGGING
  #define log(a) { Logger::writeToLog (a); DBG (a) }
#else
  #define log(a) {}
#endif

#ifdef ASIO_DEBUGGING
static void logError (const String& context, long error)
{
	String err ("unknown error");

	if (error == ASE_NotPresent)
		err = "Not Present";
	else if (error == ASE_HWMalfunction)
		err = "Hardware Malfunction";
	else if (error == ASE_InvalidParameter)
		err = "Invalid Parameter";
	else if (error == ASE_InvalidMode)
		err = "Invalid Mode";
	else if (error == ASE_SPNotAdvancing)
		err = "Sample position not advancing";
	else if (error == ASE_NoClock)
		err = "No Clock";
	else if (error == ASE_NoMemory)
		err = "Out of memory";

	log ("!!error: " + context + " - " + err);
}
#else
  #define logError(a, b) {}
#endif

class ASIOAudioIODevice;
static ASIOAudioIODevice* volatile currentASIODev[3] = { 0, 0, 0 };

static const int maxASIOChannels = 160;

class JUCE_API  ASIOAudioIODevice  : public AudioIODevice,
									 private Timer
{
public:
	Component ourWindow;

	ASIOAudioIODevice (const String& name_, const CLSID classId_, const int slotNumber,
					   const String& optionalDllForDirectLoading_)
	   : AudioIODevice (name_, "ASIO"),
		 asioObject (0),
		 classId (classId_),
		 optionalDllForDirectLoading (optionalDllForDirectLoading_),
		 currentBitDepth (16),
		 currentSampleRate (0),
		 isOpen_ (false),
		 isStarted (false),
		 postOutput (true),
		 insideControlPanelModalLoop (false),
		 shouldUsePreferredSize (false)
	{
		name = name_;

		ourWindow.addToDesktop (0);
		windowHandle = ourWindow.getWindowHandle();

		jassert (currentASIODev [slotNumber] == 0);
		currentASIODev [slotNumber] = this;

		openDevice();
	}

	~ASIOAudioIODevice()
	{
		for (int i = 0; i < numElementsInArray (currentASIODev); ++i)
			if (currentASIODev[i] == this)
				currentASIODev[i] = 0;

		close();
		log ("ASIO - exiting");
		removeCurrentDriver();
	}

	void updateSampleRates()
	{
		// find a list of sample rates..
		const double possibleSampleRates[] = { 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0 };
		sampleRates.clear();

		if (asioObject != 0)
		{
			for (int index = 0; index < numElementsInArray (possibleSampleRates); ++index)
			{
				const long err = asioObject->canSampleRate (possibleSampleRates[index]);

				if (err == 0)
				{
					sampleRates.add ((int) possibleSampleRates[index]);
					log ("rate: " + String ((int) possibleSampleRates[index]));
				}
				else if (err != ASE_NoClock)
				{
					logError ("CanSampleRate", err);
				}
			}

			if (sampleRates.size() == 0)
			{
				double cr = 0;
				const long err = asioObject->getSampleRate (&cr);
				log ("No sample rates supported - current rate: " + String ((int) cr));

				if (err == 0)
					sampleRates.add ((int) cr);
			}
		}
	}

	const StringArray getOutputChannelNames()
	{
		return outputChannelNames;
	}

	const StringArray getInputChannelNames()
	{
		return inputChannelNames;
	}

	int getNumSampleRates()
	{
		return sampleRates.size();
	}

	double getSampleRate (int index)
	{
		return sampleRates [index];
	}

	int getNumBufferSizesAvailable()
	{
		return bufferSizes.size();
	}

	int getBufferSizeSamples (int index)
	{
		return bufferSizes [index];
	}

	int getDefaultBufferSize()
	{
		return preferredSize;
	}

	const String open (const BigInteger& inputChannels,
					   const BigInteger& outputChannels,
					   double sr,
					   int bufferSizeSamples)
	{
		close();
		currentCallback = 0;

		if (bufferSizeSamples <= 0)
			shouldUsePreferredSize = true;

		if (asioObject == 0 || ! isASIOOpen)
		{
			log ("Warning: device not open");
			const String err (openDevice());

			if (asioObject == 0 || ! isASIOOpen)
				return err;
		}

		isStarted = false;
		bufferIndex = -1;
		long err = 0;

		long newPreferredSize = 0;

		// if the preferred size has just changed, assume it's a control panel thing and use it as the new value.
		minSize = 0;
		maxSize = 0;
		newPreferredSize = 0;
		granularity = 0;

		if (asioObject->getBufferSize (&minSize, &maxSize, &newPreferredSize, &granularity) == 0)
		{
			if (preferredSize != 0 && newPreferredSize != 0 && newPreferredSize != preferredSize)
				shouldUsePreferredSize = true;

			preferredSize = newPreferredSize;
		}

		// unfortunate workaround for certain manufacturers whose drivers crash horribly if you make
		// dynamic changes to the buffer size...
		shouldUsePreferredSize = shouldUsePreferredSize
								   || getName().containsIgnoreCase ("Digidesign");

		if (shouldUsePreferredSize)
		{
			log ("Using preferred size for buffer..");

			if ((err = asioObject->getBufferSize (&minSize, &maxSize, &preferredSize, &granularity)) == 0)
			{
				bufferSizeSamples = preferredSize;
			}
			else
			{
				bufferSizeSamples = 1024;
				logError ("GetBufferSize1", err);
			}

			shouldUsePreferredSize = false;
		}

		int sampleRate = roundDoubleToInt (sr);
		currentSampleRate = sampleRate;
		currentBlockSizeSamples = bufferSizeSamples;
		currentChansOut.clear();
		currentChansIn.clear();
		zeromem (inBuffers, sizeof (inBuffers));
		zeromem (outBuffers, sizeof (outBuffers));

		updateSampleRates();

		if (sampleRate == 0 || (sampleRates.size() > 0 && ! sampleRates.contains (sampleRate)))
			sampleRate = sampleRates[0];

		jassert (sampleRate != 0);
		if (sampleRate == 0)
			sampleRate = 44100;

		long numSources = 32;
		ASIOClockSource clocks[32];
		zeromem (clocks, sizeof (clocks));
		asioObject->getClockSources (clocks, &numSources);
		bool isSourceSet = false;

		// careful not to remove this loop because it does more than just logging!
		int i;
		for (i = 0; i < numSources; ++i)
		{
			String s ("clock: ");
			s += clocks[i].name;

			if (clocks[i].isCurrentSource)
			{
				isSourceSet = true;
				s << " (cur)";
			}

			log (s);
		}

		if (numSources > 1 && ! isSourceSet)
		{
			log ("setting clock source");
			asioObject->setClockSource (clocks[0].index);
			Thread::sleep (20);
		}
		else
		{
			if (numSources == 0)
			{
				log ("ASIO - no clock sources!");
			}
		}

		double cr = 0;
		err = asioObject->getSampleRate (&cr);
		if (err == 0)
		{
			currentSampleRate = cr;
		}
		else
		{
			logError ("GetSampleRate", err);
			currentSampleRate = 0;
		}

		error = String::empty;
		needToReset = false;
		isReSync = false;
		err = 0;
		bool buffersCreated = false;

		if (currentSampleRate != sampleRate)
		{
			log ("ASIO samplerate: " + String (currentSampleRate) + " to " + String (sampleRate));
			err = asioObject->setSampleRate (sampleRate);

			if (err == ASE_NoClock && numSources > 0)
			{
				log ("trying to set a clock source..");
				Thread::sleep (10);
				err = asioObject->setClockSource (clocks[0].index);
				if (err != 0)
				{
					logError ("SetClock", err);
				}

				Thread::sleep (10);
				err = asioObject->setSampleRate (sampleRate);
			}
		}

		if (err == 0)
		{
			currentSampleRate = sampleRate;

			if (needToReset)
			{
				if (isReSync)
				{
					log ("Resync request");
				}

				log ("! Resetting ASIO after sample rate change");
				removeCurrentDriver();

				loadDriver();
				const String error (initDriver());

				if (error.isNotEmpty())
				{
					log ("ASIOInit: " + error);
				}

				needToReset = false;
				isReSync = false;
			}

			numActiveInputChans = 0;
			numActiveOutputChans = 0;

			ASIOBufferInfo* info = bufferInfos;
			int i;
			for (i = 0; i < totalNumInputChans; ++i)
			{
				if (inputChannels[i])
				{
					currentChansIn.setBit (i);
					info->isInput = 1;
					info->channelNum = i;
					info->buffers[0] = info->buffers[1] = 0;
					++info;
					++numActiveInputChans;
				}
			}

			for (i = 0; i < totalNumOutputChans; ++i)
			{
				if (outputChannels[i])
				{
					currentChansOut.setBit (i);
					info->isInput = 0;
					info->channelNum = i;
					info->buffers[0] = info->buffers[1] = 0;
					++info;
					++numActiveOutputChans;
				}
			}

			const int totalBuffers = numActiveInputChans + numActiveOutputChans;

			callbacks.sampleRateDidChange = &sampleRateChangedCallback;

			if (currentASIODev[0] == this)
			{
				callbacks.bufferSwitch = &bufferSwitchCallback0;
				callbacks.asioMessage = &asioMessagesCallback0;
				callbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfoCallback0;
			}
			else if (currentASIODev[1] == this)
			{
				callbacks.bufferSwitch = &bufferSwitchCallback1;
				callbacks.asioMessage = &asioMessagesCallback1;
				callbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfoCallback1;
			}
			else if (currentASIODev[2] == this)
			{
				callbacks.bufferSwitch = &bufferSwitchCallback2;
				callbacks.asioMessage = &asioMessagesCallback2;
				callbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfoCallback2;
			}
			else
			{
				jassertfalse
			}

			log ("disposing buffers");
			err = asioObject->disposeBuffers();

			log ("creating buffers: " + String (totalBuffers) + ", " + String (currentBlockSizeSamples));
			err = asioObject->createBuffers (bufferInfos,
											 totalBuffers,
											 currentBlockSizeSamples,
											 &callbacks);

			if (err != 0)
			{
				currentBlockSizeSamples = preferredSize;
				logError ("create buffers 2", err);

				asioObject->disposeBuffers();
				err = asioObject->createBuffers (bufferInfos,
												 totalBuffers,
												 currentBlockSizeSamples,
												 &callbacks);
			}

			if (err == 0)
			{
				buffersCreated = true;

				tempBuffer.calloc (totalBuffers * currentBlockSizeSamples + 32);

				int n = 0;
				Array <int> types;
				currentBitDepth = 16;

				for (i = 0; i < jmin ((int) totalNumInputChans, maxASIOChannels); ++i)
				{
					if (inputChannels[i])
					{
						inBuffers[n] = tempBuffer + (currentBlockSizeSamples * n);

						ASIOChannelInfo channelInfo;
						zerostruct (channelInfo);

						channelInfo.channel = i;
						channelInfo.isInput = 1;
						asioObject->getChannelInfo (&channelInfo);

						types.addIfNotAlreadyThere (channelInfo.type);
						typeToFormatParameters (channelInfo.type,
												inputChannelBitDepths[n],
												inputChannelBytesPerSample[n],
												inputChannelIsFloat[n],
												inputChannelLittleEndian[n]);

						currentBitDepth = jmax (currentBitDepth, inputChannelBitDepths[n]);

						++n;
					}
				}

				jassert (numActiveInputChans == n);
				n = 0;

				for (i = 0; i < jmin ((int) totalNumOutputChans, maxASIOChannels); ++i)
				{
					if (outputChannels[i])
					{
						outBuffers[n] = tempBuffer + (currentBlockSizeSamples * (numActiveInputChans + n));

						ASIOChannelInfo channelInfo;
						zerostruct (channelInfo);

						channelInfo.channel = i;
						channelInfo.isInput = 0;
						asioObject->getChannelInfo (&channelInfo);

						types.addIfNotAlreadyThere (channelInfo.type);
						typeToFormatParameters (channelInfo.type,
												outputChannelBitDepths[n],
												outputChannelBytesPerSample[n],
												outputChannelIsFloat[n],
												outputChannelLittleEndian[n]);

						currentBitDepth = jmax (currentBitDepth, outputChannelBitDepths[n]);

						++n;
					}
				}

				jassert (numActiveOutputChans == n);

				for (i = types.size(); --i >= 0;)
				{
					log ("channel format: " + String (types[i]));
				}

				jassert (n <= totalBuffers);

				for (i = 0; i < numActiveOutputChans; ++i)
				{
					const int size = currentBlockSizeSamples * (outputChannelBitDepths[i] >> 3);

					if (bufferInfos [numActiveInputChans + i].buffers[0] == 0
						|| bufferInfos [numActiveInputChans + i].buffers[1] == 0)
					{
						log ("!! Null buffers");
					}
					else
					{
						zeromem (bufferInfos[numActiveInputChans + i].buffers[0], size);
						zeromem (bufferInfos[numActiveInputChans + i].buffers[1], size);
					}
				}

				inputLatency = outputLatency = 0;

				if (asioObject->getLatencies (&inputLatency, &outputLatency) != 0)
				{
					log ("ASIO - no latencies");
				}
				else
				{
					log ("ASIO latencies: " + String ((int) outputLatency) + ", " + String ((int) inputLatency));
				}

				isOpen_ = true;

				log ("starting ASIO");
				calledback = false;
				err = asioObject->start();

				if (err != 0)
				{
					isOpen_ = false;
					log ("ASIO - stop on failure");
					Thread::sleep (10);
					asioObject->stop();
					error = "Can't start device";
					Thread::sleep (10);
				}
				else
				{
					int count = 300;
					while (--count > 0 && ! calledback)
						Thread::sleep (10);

					isStarted = true;

					if (! calledback)
					{
						error = "Device didn't start correctly";
						log ("ASIO didn't callback - stopping..");
						asioObject->stop();
					}
				}
			}
			else
			{
				error = "Can't create i/o buffers";
			}
		}
		else
		{
			error = "Can't set sample rate: ";
			error << sampleRate;
		}

		if (error.isNotEmpty())
		{
			logError (error, err);

			if (asioObject != 0 && buffersCreated)
				asioObject->disposeBuffers();

			Thread::sleep (20);
			isStarted = false;
			isOpen_ = false;
			close();
		}

		needToReset = false;
		isReSync = false;

		return error;
	}

	void close()
	{
		error = String::empty;
		stopTimer();
		stop();

		if (isASIOOpen && isOpen_)
		{
			const ScopedLock sl (callbackLock);

			isOpen_ = false;
			isStarted = false;
			needToReset = false;
			isReSync = false;

			log ("ASIO - stopping");

			if (asioObject != 0)
			{
				Thread::sleep (20);
				asioObject->stop();
				Thread::sleep (10);
				asioObject->disposeBuffers();
			}

			Thread::sleep (10);
		}
	}

	bool isOpen()
	{
		return isOpen_ || insideControlPanelModalLoop;
	}

	int getCurrentBufferSizeSamples()
	{
		return currentBlockSizeSamples;
	}

	double getCurrentSampleRate()
	{
		return currentSampleRate;
	}

	const BigInteger getActiveOutputChannels() const
	{
		return currentChansOut;
	}

	const BigInteger getActiveInputChannels() const
	{
		return currentChansIn;
	}

	int getCurrentBitDepth()
	{
		return currentBitDepth;
	}

	int getOutputLatencyInSamples()
	{
		return outputLatency + currentBlockSizeSamples / 4;
	}

	int getInputLatencyInSamples()
	{
		return inputLatency + currentBlockSizeSamples / 4;
	}

	void start (AudioIODeviceCallback* callback)
	{
		if (callback != 0)
		{
			callback->audioDeviceAboutToStart (this);

			const ScopedLock sl (callbackLock);
			currentCallback = callback;
		}
	}

	void stop()
	{
		AudioIODeviceCallback* const lastCallback = currentCallback;

		{
			const ScopedLock sl (callbackLock);
			currentCallback = 0;
		}

		if (lastCallback != 0)
			lastCallback->audioDeviceStopped();
	}

	bool isPlaying()
	{
		return isASIOOpen && (currentCallback != 0);
	}

	const String getLastError()
	{
		return error;
	}

	bool hasControlPanel() const
	{
		return true;
	}

	bool showControlPanel()
	{
		log ("ASIO - showing control panel");

		Component modalWindow (String::empty);
		modalWindow.setOpaque (true);
		modalWindow.addToDesktop (0);
		modalWindow.enterModalState();
		bool done = false;

		JUCE_TRY
		{
			// are there are devices that need to be closed before showing their control panel?
			// close();
			insideControlPanelModalLoop = true;

			const uint32 started = Time::getMillisecondCounter();

			if (asioObject != 0)
			{
				asioObject->controlPanel();

				const int spent = (int) Time::getMillisecondCounter() - (int) started;

				log ("spent: " + String (spent));

				if (spent > 300)
				{
					shouldUsePreferredSize = true;
					done = true;
				}
			}
		}
		JUCE_CATCH_ALL

		insideControlPanelModalLoop = false;
		return done;
	}

	void resetRequest() throw()
	{
		needToReset = true;
	}

	void resyncRequest() throw()
	{
		needToReset = true;
		isReSync = true;
	}

	void timerCallback()
	{
		if (! insideControlPanelModalLoop)
		{
			stopTimer();

			// used to cause a reset
			log ("! ASIO restart request!");

			if (isOpen_)
			{
				AudioIODeviceCallback* const oldCallback = currentCallback;

				close();
				open (BigInteger (currentChansIn), BigInteger (currentChansOut),
					  currentSampleRate, currentBlockSizeSamples);

				if (oldCallback != 0)
					start (oldCallback);
			}
		}
		else
		{
			startTimer (100);
		}
	}

	juce_UseDebuggingNewOperator

private:

	IASIO* volatile asioObject;
	ASIOCallbacks callbacks;

	void* windowHandle;
	CLSID classId;
	const String optionalDllForDirectLoading;
	String error;

	long totalNumInputChans, totalNumOutputChans;
	StringArray inputChannelNames, outputChannelNames;

	Array<int> sampleRates, bufferSizes;
	long inputLatency, outputLatency;
	long minSize, maxSize, preferredSize, granularity;

	int volatile currentBlockSizeSamples;
	int volatile currentBitDepth;
	double volatile currentSampleRate;
	BigInteger currentChansOut, currentChansIn;
	AudioIODeviceCallback* volatile currentCallback;
	CriticalSection callbackLock;

	ASIOBufferInfo bufferInfos [maxASIOChannels];
	float* inBuffers [maxASIOChannels];
	float* outBuffers [maxASIOChannels];

	int inputChannelBitDepths [maxASIOChannels];
	int outputChannelBitDepths [maxASIOChannels];
	int inputChannelBytesPerSample [maxASIOChannels];
	int outputChannelBytesPerSample [maxASIOChannels];
	bool inputChannelIsFloat [maxASIOChannels];
	bool outputChannelIsFloat [maxASIOChannels];
	bool inputChannelLittleEndian [maxASIOChannels];
	bool outputChannelLittleEndian [maxASIOChannels];

	WaitableEvent event1;
	HeapBlock <float> tempBuffer;
	int volatile bufferIndex, numActiveInputChans, numActiveOutputChans;

	bool isOpen_, isStarted;
	bool volatile isASIOOpen;
	bool volatile calledback;
	bool volatile littleEndian, postOutput, needToReset, isReSync;
	bool volatile insideControlPanelModalLoop;
	bool volatile shouldUsePreferredSize;

	void removeCurrentDriver()
	{
		if (asioObject != 0)
		{
			asioObject->Release();
			asioObject = 0;
		}
	}

	bool loadDriver()
	{
		removeCurrentDriver();

		JUCE_TRY
		{
			if (CoCreateInstance (classId, 0, CLSCTX_INPROC_SERVER,
								  classId, (void**) &asioObject) == S_OK)
			{
				return true;
			}

			// If a class isn't registered but we have a path for it, we can fallback to
			// doing a direct load of the COM object (only available via the juce_createASIOAudioIODeviceForGUID function).
			if (optionalDllForDirectLoading.isNotEmpty())
			{
				HMODULE h = LoadLibrary (optionalDllForDirectLoading);

				if (h != 0)
				{
					typedef HRESULT (CALLBACK* DllGetClassObjectFunc) (REFCLSID clsid, REFIID iid, LPVOID* ppv);
					DllGetClassObjectFunc dllGetClassObject = (DllGetClassObjectFunc) GetProcAddress (h, "DllGetClassObject");

					if (dllGetClassObject != 0)
					{
						IClassFactory* classFactory = 0;
						HRESULT hr = dllGetClassObject (classId, IID_IClassFactory, (void**) &classFactory);

						if (classFactory != 0)
						{
							hr = classFactory->CreateInstance (0, classId, (void**) &asioObject);
							classFactory->Release();
						}

						return asioObject != 0;
					}
				}
			}
		}
		JUCE_CATCH_ALL

		asioObject = 0;
		return false;
	}

	const String initDriver()
	{
		if (asioObject != 0)
		{
			char buffer [256];
			zeromem (buffer, sizeof (buffer));

			if (! asioObject->init (windowHandle))
			{
				asioObject->getErrorMessage (buffer);
				return String (buffer, sizeof (buffer) - 1);
			}

			// just in case any daft drivers expect this to be called..
			asioObject->getDriverName (buffer);

			return String::empty;
		}

		return "No Driver";
	}

	const String openDevice()
	{
		// use this in case the driver starts opening dialog boxes..
		Component modalWindow (String::empty);
		modalWindow.setOpaque (true);
		modalWindow.addToDesktop (0);
		modalWindow.enterModalState();

		// open the device and get its info..
		log ("opening ASIO device: " + getName());

		needToReset = false;
		isReSync = false;
		outputChannelNames.clear();
		inputChannelNames.clear();
		bufferSizes.clear();
		sampleRates.clear();
		isASIOOpen = false;
		isOpen_ = false;
		totalNumInputChans = 0;
		totalNumOutputChans = 0;
		numActiveInputChans = 0;
		numActiveOutputChans = 0;
		currentCallback = 0;

		error = String::empty;

		if (getName().isEmpty())
			return error;

		long err = 0;

		if (loadDriver())
		{
			if ((error = initDriver()).isEmpty())
			{
				numActiveInputChans = 0;
				numActiveOutputChans = 0;
				totalNumInputChans = 0;
				totalNumOutputChans = 0;

				if (asioObject != 0
					 && (err = asioObject->getChannels (&totalNumInputChans, &totalNumOutputChans)) == 0)
				{
					log (String ((int) totalNumInputChans) + " in, " + String ((int) totalNumOutputChans) + " out");

					if ((err = asioObject->getBufferSize (&minSize, &maxSize, &preferredSize, &granularity)) == 0)
					{
						// find a list of buffer sizes..
						log (String ((int) minSize) + " " + String ((int) maxSize) + " " + String ((int) preferredSize) + " " + String ((int) granularity));

						if (granularity >= 0)
						{
							granularity = jmax (1, (int) granularity);

							for (int i = jmax ((int) minSize, (int) granularity); i < jmin (6400, (int) maxSize); i += granularity)
								bufferSizes.addIfNotAlreadyThere (granularity * (i / granularity));
						}
						else if (granularity < 0)
						{
							for (int i = 0; i < 18; ++i)
							{
								const int s = (1 << i);

								if (s >= minSize && s <= maxSize)
									bufferSizes.add (s);
							}
						}

						if (! bufferSizes.contains (preferredSize))
							bufferSizes.insert (0, preferredSize);

						double currentRate = 0;
						asioObject->getSampleRate (&currentRate);

						if (currentRate <= 0.0 || currentRate > 192001.0)
						{
							log ("setting sample rate");
							err = asioObject->setSampleRate (44100.0);
							if (err != 0)
							{
								logError ("setting sample rate", err);
							}

							asioObject->getSampleRate (&currentRate);
						}

						currentSampleRate = currentRate;

						postOutput = (asioObject->outputReady() == 0);
						if (postOutput)
						{
							log ("ASIO outputReady = ok");
						}

						updateSampleRates();

						// ..because cubase does it at this point
						inputLatency = outputLatency = 0;
						if (asioObject->getLatencies (&inputLatency, &outputLatency) != 0)
						{
							log ("ASIO - no latencies");
						}

						log ("latencies: " + String ((int) inputLatency) + ", " + String ((int) outputLatency));

						// create some dummy buffers now.. because cubase does..
						numActiveInputChans = 0;
						numActiveOutputChans = 0;

						ASIOBufferInfo* info = bufferInfos;
						int i, numChans = 0;
						for (i = 0; i < jmin (2, (int) totalNumInputChans); ++i)
						{
							info->isInput = 1;
							info->channelNum = i;
							info->buffers[0] = info->buffers[1] = 0;
							++info;
							++numChans;
						}

						const int outputBufferIndex = numChans;

						for (i = 0; i < jmin (2, (int) totalNumOutputChans); ++i)
						{
							info->isInput = 0;
							info->channelNum = i;
							info->buffers[0] = info->buffers[1] = 0;
							++info;
							++numChans;
						}

						callbacks.sampleRateDidChange = &sampleRateChangedCallback;

						if (currentASIODev[0] == this)
						{
							callbacks.bufferSwitch = &bufferSwitchCallback0;
							callbacks.asioMessage = &asioMessagesCallback0;
							callbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfoCallback0;
						}
						else if (currentASIODev[1] == this)
						{
							callbacks.bufferSwitch = &bufferSwitchCallback1;
							callbacks.asioMessage = &asioMessagesCallback1;
							callbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfoCallback1;
						}
						else if (currentASIODev[2] == this)
						{
							callbacks.bufferSwitch = &bufferSwitchCallback2;
							callbacks.asioMessage = &asioMessagesCallback2;
							callbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfoCallback2;
						}
						else
						{
							jassertfalse
						}

						log ("creating buffers (dummy): " + String (numChans) + ", " + String ((int) preferredSize));

						if (preferredSize > 0)
						{
							err = asioObject->createBuffers (bufferInfos, numChans, preferredSize, &callbacks);
							if (err != 0)
							{
								logError ("dummy buffers", err);
							}
						}

						long newInps = 0, newOuts = 0;
						asioObject->getChannels (&newInps, &newOuts);

						if (totalNumInputChans != newInps || totalNumOutputChans != newOuts)
						{
							totalNumInputChans = newInps;
							totalNumOutputChans = newOuts;

							log (String ((int) totalNumInputChans) + " in; " + String ((int) totalNumOutputChans) + " out");
						}

						updateSampleRates();

						ASIOChannelInfo channelInfo;
						channelInfo.type = 0;

						for (i = 0; i < totalNumInputChans; ++i)
						{
							zerostruct (channelInfo);
							channelInfo.channel = i;
							channelInfo.isInput = 1;
							asioObject->getChannelInfo (&channelInfo);

							inputChannelNames.add (String (channelInfo.name));
						}

						for (i = 0; i < totalNumOutputChans; ++i)
						{
							zerostruct (channelInfo);
							channelInfo.channel = i;
							channelInfo.isInput = 0;
							asioObject->getChannelInfo (&channelInfo);

							outputChannelNames.add (String (channelInfo.name));

							typeToFormatParameters (channelInfo.type,
													outputChannelBitDepths[i],
													outputChannelBytesPerSample[i],
													outputChannelIsFloat[i],
													outputChannelLittleEndian[i]);

							if (i < 2)
							{
								// clear the channels that are used with the dummy stuff
								const int bytesPerBuffer = preferredSize * (outputChannelBitDepths[i] >> 3);
								zeromem (bufferInfos [outputBufferIndex + i].buffers[0], bytesPerBuffer);
								zeromem (bufferInfos [outputBufferIndex + i].buffers[1], bytesPerBuffer);
							}
						}

						outputChannelNames.trim();
						inputChannelNames.trim();
						outputChannelNames.appendNumbersToDuplicates (false, true);
						inputChannelNames.appendNumbersToDuplicates (false, true);

						// start and stop because cubase does it..
						asioObject->getLatencies (&inputLatency, &outputLatency);

						if ((err = asioObject->start()) != 0)
						{
							// ignore an error here, as it might start later after setting other stuff up
							logError ("ASIO start", err);
						}

						Thread::sleep (100);
						asioObject->stop();
					}
					else
					{
						error = "Can't detect buffer sizes";
					}
				}
				else
				{
					error = "Can't detect asio channels";
				}
			}
		}
		else
		{
			error = "No such device";
		}

		if (error.isNotEmpty())
		{
			logError (error, err);

			if (asioObject != 0)
				asioObject->disposeBuffers();

			removeCurrentDriver();
			isASIOOpen = false;
		}
		else
		{
			isASIOOpen = true;
			log ("ASIO device open");
		}

		isOpen_ = false;
		needToReset = false;
		isReSync = false;

		return error;
	}

	void callback (const long index) throw()
	{
		if (isStarted)
		{
			bufferIndex = index;
			processBuffer();
		}
		else
		{
			if (postOutput && (asioObject != 0))
				asioObject->outputReady();
		}

		calledback = true;
	}

	void processBuffer() throw()
	{
		const ASIOBufferInfo* const infos = bufferInfos;
		const int bi = bufferIndex;

		const ScopedLock sl (callbackLock);

		if (needToReset)
		{
			needToReset = false;

			if (isReSync)
			{
				log ("! ASIO resync");
				isReSync = false;
			}
			else
			{
				startTimer (20);
			}
		}

		if (bi >= 0)
		{
			const int samps = currentBlockSizeSamples;

			if (currentCallback != 0)
			{
				int i;
				for (i = 0; i < numActiveInputChans; ++i)
				{
					float* const dst = inBuffers[i];

					jassert (dst != 0);

					const char* const src = (const char*) (infos[i].buffers[bi]);

					if (inputChannelIsFloat[i])
					{
						memcpy (dst, src, samps * sizeof (float));
					}
					else
					{
						jassert (dst == tempBuffer + (samps * i));

						switch (inputChannelBitDepths[i])
						{
						case 16:
							convertInt16ToFloat (src, dst, inputChannelBytesPerSample[i],
												 samps, inputChannelLittleEndian[i]);
							break;

						case 24:
							convertInt24ToFloat (src, dst, inputChannelBytesPerSample[i],
												 samps, inputChannelLittleEndian[i]);
							break;

						case 32:
							convertInt32ToFloat (src, dst, inputChannelBytesPerSample[i],
												 samps, inputChannelLittleEndian[i]);
							break;

						case 64:
							jassertfalse
							break;
						}
					}
				}

				currentCallback->audioDeviceIOCallback ((const float**) inBuffers,
														numActiveInputChans,
														outBuffers,
														numActiveOutputChans,
														samps);

				for (i = 0; i < numActiveOutputChans; ++i)
				{
					float* const src = outBuffers[i];

					jassert (src != 0);

					char* const dst = (char*) (infos [numActiveInputChans + i].buffers[bi]);

					if (outputChannelIsFloat[i])
					{
						memcpy (dst, src, samps * sizeof (float));
					}
					else
					{
						jassert (src == tempBuffer + (samps * (numActiveInputChans + i)));

						switch (outputChannelBitDepths[i])
						{
						case 16:
							convertFloatToInt16 (src, dst, outputChannelBytesPerSample[i],
												 samps, outputChannelLittleEndian[i]);
							break;

						case 24:
							convertFloatToInt24 (src, dst, outputChannelBytesPerSample[i],
												 samps, outputChannelLittleEndian[i]);
							break;

						case 32:
							convertFloatToInt32 (src, dst, outputChannelBytesPerSample[i],
												 samps, outputChannelLittleEndian[i]);
							break;

						case 64:
							jassertfalse
							break;
						}
					}
				}
			}
			else
			{
				for (int i = 0; i < numActiveOutputChans; ++i)
				{
					const int bytesPerBuffer = samps * (outputChannelBitDepths[i] >> 3);
					zeromem (infos[numActiveInputChans + i].buffers[bi], bytesPerBuffer);
				}
			}
		}

		if (postOutput)
			asioObject->outputReady();
	}

	static ASIOTime* bufferSwitchTimeInfoCallback0 (ASIOTime*, long index, long) throw()
	{
		if (currentASIODev[0] != 0)
			currentASIODev[0]->callback (index);

		return 0;
	}

	static ASIOTime* bufferSwitchTimeInfoCallback1 (ASIOTime*, long index, long) throw()
	{
		if (currentASIODev[1] != 0)
			currentASIODev[1]->callback (index);

		return 0;
	}

	static ASIOTime* bufferSwitchTimeInfoCallback2 (ASIOTime*, long index, long) throw()
	{
		if (currentASIODev[2] != 0)
			currentASIODev[2]->callback (index);

		return 0;
	}

	static void bufferSwitchCallback0 (long index, long) throw()
	{
		if (currentASIODev[0] != 0)
			currentASIODev[0]->callback (index);
	}

	static void bufferSwitchCallback1 (long index, long) throw()
	{
		if (currentASIODev[1] != 0)
			currentASIODev[1]->callback (index);
	}

	static void bufferSwitchCallback2 (long index, long) throw()
	{
		if (currentASIODev[2] != 0)
			currentASIODev[2]->callback (index);
	}

	static long asioMessagesCallback0 (long selector, long value, void*, double*) throw()
	{
		return asioMessagesCallback (selector, value, 0);
	}

	static long asioMessagesCallback1 (long selector, long value, void*, double*) throw()
	{
		return asioMessagesCallback (selector, value, 1);
	}

	static long asioMessagesCallback2 (long selector, long value, void*, double*) throw()
	{
		return asioMessagesCallback (selector, value, 2);
	}

	static long asioMessagesCallback (long selector, long value, const int deviceIndex) throw()
	{
		switch (selector)
		{
		case kAsioSelectorSupported:
			if (value == kAsioResetRequest
				|| value == kAsioEngineVersion
				|| value == kAsioResyncRequest
				|| value == kAsioLatenciesChanged
				|| value == kAsioSupportsInputMonitor)
				return 1;
			break;

		case kAsioBufferSizeChange:
			break;

		case kAsioResetRequest:
			if (currentASIODev[deviceIndex] != 0)
				currentASIODev[deviceIndex]->resetRequest();

			return 1;

		case kAsioResyncRequest:
			if (currentASIODev[deviceIndex] != 0)
				currentASIODev[deviceIndex]->resyncRequest();

			return 1;

		case kAsioLatenciesChanged:
			return 1;

		case kAsioEngineVersion:
			return 2;

		case kAsioSupportsTimeInfo:
		case kAsioSupportsTimeCode:
			return 0;
		}

		return 0;
	}

	static void sampleRateChangedCallback (ASIOSampleRate) throw()
	{
	}

	static void convertInt16ToFloat (const char* src,
									 float* dest,
									 const int srcStrideBytes,
									 int numSamples,
									 const bool littleEndian) throw()
	{
		const double g = 1.0 / 32768.0;

		if (littleEndian)
		{
			while (--numSamples >= 0)
			{
				*dest++ = (float) (g * (short) ByteOrder::littleEndianShort (src));
				src += srcStrideBytes;
			}
		}
		else
		{
			while (--numSamples >= 0)
			{
				*dest++ = (float) (g * (short) ByteOrder::bigEndianShort (src));
				src += srcStrideBytes;
			}
		}
	}

	static void convertFloatToInt16 (const float* src,
									 char* dest,
									 const int dstStrideBytes,
									 int numSamples,
									 const bool littleEndian) throw()
	{
		const double maxVal = (double) 0x7fff;

		if (littleEndian)
		{
			while (--numSamples >= 0)
			{
				*(uint16*) dest = ByteOrder::swapIfBigEndian ((uint16) (short) roundDoubleToInt (jlimit (-maxVal, maxVal, maxVal * *src++)));
				dest += dstStrideBytes;
			}
		}
		else
		{
			while (--numSamples >= 0)
			{
				*(uint16*) dest = ByteOrder::swapIfLittleEndian ((uint16) (short) roundDoubleToInt (jlimit (-maxVal, maxVal, maxVal * *src++)));
				dest += dstStrideBytes;
			}
		}
	}

	static void convertInt24ToFloat (const char* src,
									 float* dest,
									 const int srcStrideBytes,
									 int numSamples,
									 const bool littleEndian) throw()
	{
		const double g = 1.0 / 0x7fffff;

		if (littleEndian)
		{
			while (--numSamples >= 0)
			{
				*dest++ = (float) (g * ByteOrder::littleEndian24Bit (src));
				src += srcStrideBytes;
			}
		}
		else
		{
			while (--numSamples >= 0)
			{
				*dest++ = (float) (g * ByteOrder::bigEndian24Bit (src));
				src += srcStrideBytes;
			}
		}
	}

	static void convertFloatToInt24 (const float* src,
									 char* dest,
									 const int dstStrideBytes,
									 int numSamples,
									 const bool littleEndian) throw()
	{
		const double maxVal = (double) 0x7fffff;

		if (littleEndian)
		{
			while (--numSamples >= 0)
			{
				ByteOrder::littleEndian24BitToChars ((uint32) roundDoubleToInt (jlimit (-maxVal, maxVal, maxVal * *src++)), dest);
				dest += dstStrideBytes;
			}
		}
		else
		{
			while (--numSamples >= 0)
			{
				ByteOrder::bigEndian24BitToChars ((uint32) roundDoubleToInt (jlimit (-maxVal, maxVal, maxVal * *src++)), dest);
				dest += dstStrideBytes;
			}
		}
	}

	static void convertInt32ToFloat (const char* src,
									 float* dest,
									 const int srcStrideBytes,
									 int numSamples,
									 const bool littleEndian) throw()
	{
		const double g = 1.0 / 0x7fffffff;

		if (littleEndian)
		{
			while (--numSamples >= 0)
			{
				*dest++ = (float) (g * (int) ByteOrder::littleEndianInt (src));
				src += srcStrideBytes;
			}
		}
		else
		{
			while (--numSamples >= 0)
			{
				*dest++ = (float) (g * (int) ByteOrder::bigEndianInt (src));
				src += srcStrideBytes;
			}
		}
	}

	static void convertFloatToInt32 (const float* src,
									 char* dest,
									 const int dstStrideBytes,
									 int numSamples,
									 const bool littleEndian) throw()
	{
		const double maxVal = (double) 0x7fffffff;

		if (littleEndian)
		{
			while (--numSamples >= 0)
			{
				*(uint32*) dest = ByteOrder::swapIfBigEndian ((uint32) roundDoubleToInt (jlimit (-maxVal, maxVal, maxVal * *src++)));
				dest += dstStrideBytes;
			}
		}
		else
		{
			while (--numSamples >= 0)
			{
				*(uint32*) dest = ByteOrder::swapIfLittleEndian ((uint32) roundDoubleToInt (jlimit (-maxVal, maxVal, maxVal * *src++)));
				dest += dstStrideBytes;
			}
		}
	}

	static void typeToFormatParameters (const long type,
										int& bitDepth,
										int& byteStride,
										bool& formatIsFloat,
										bool& littleEndian) throw()
	{
		bitDepth = 0;
		littleEndian = false;
		formatIsFloat = false;

		switch (type)
		{
			case ASIOSTInt16MSB:
			case ASIOSTInt16LSB:
			case ASIOSTInt32MSB16:
			case ASIOSTInt32LSB16:
				bitDepth = 16; break;

			case ASIOSTFloat32MSB:
			case ASIOSTFloat32LSB:
				formatIsFloat = true;
				bitDepth = 32; break;

			case ASIOSTInt32MSB:
			case ASIOSTInt32LSB:
				bitDepth = 32; break;

			case ASIOSTInt24MSB:
			case ASIOSTInt24LSB:
			case ASIOSTInt32MSB24:
			case ASIOSTInt32LSB24:
			case ASIOSTInt32MSB18:
			case ASIOSTInt32MSB20:
			case ASIOSTInt32LSB18:
			case ASIOSTInt32LSB20:
				bitDepth = 24; break;

			case ASIOSTFloat64MSB:
			case ASIOSTFloat64LSB:
			default:
				bitDepth = 64;
				break;
		}

		switch (type)
		{
			case ASIOSTInt16MSB:
			case ASIOSTInt32MSB16:
			case ASIOSTFloat32MSB:
			case ASIOSTFloat64MSB:
			case ASIOSTInt32MSB:
			case ASIOSTInt32MSB18:
			case ASIOSTInt32MSB20:
			case ASIOSTInt32MSB24:
			case ASIOSTInt24MSB:
				littleEndian = false; break;

			case ASIOSTInt16LSB:
			case ASIOSTInt32LSB16:
			case ASIOSTFloat32LSB:
			case ASIOSTFloat64LSB:
			case ASIOSTInt32LSB:
			case ASIOSTInt32LSB18:
			case ASIOSTInt32LSB20:
			case ASIOSTInt32LSB24:
			case ASIOSTInt24LSB:
				littleEndian = true; break;

			default:
				break;
		}

		switch (type)
		{
			case ASIOSTInt16LSB:
			case ASIOSTInt16MSB:
				byteStride = 2; break;

			case ASIOSTInt24LSB:
			case ASIOSTInt24MSB:
				byteStride = 3; break;

			case ASIOSTInt32MSB16:
			case ASIOSTInt32LSB16:
			case ASIOSTInt32MSB:
			case ASIOSTInt32MSB18:
			case ASIOSTInt32MSB20:
			case ASIOSTInt32MSB24:
			case ASIOSTInt32LSB:
			case ASIOSTInt32LSB18:
			case ASIOSTInt32LSB20:
			case ASIOSTInt32LSB24:
			case ASIOSTFloat32LSB:
			case ASIOSTFloat32MSB:
				byteStride = 4; break;

			case ASIOSTFloat64MSB:
			case ASIOSTFloat64LSB:
				byteStride = 8; break;

			default:
				break;
		}
	}
};

class ASIOAudioIODeviceType  : public AudioIODeviceType
{
public:
	ASIOAudioIODeviceType()
		: AudioIODeviceType ("ASIO"),
		  hasScanned (false)
	{
		CoInitialize (0);
	}

	~ASIOAudioIODeviceType()
	{
	}

	void scanForDevices()
	{
		hasScanned = true;

		deviceNames.clear();
		classIds.clear();

		HKEY hk = 0;
		int index = 0;

		if (RegOpenKeyA (HKEY_LOCAL_MACHINE, "software\\asio", &hk) == ERROR_SUCCESS)
		{
			for (;;)
			{
				char name [256];

				if (RegEnumKeyA (hk, index++, name, 256) == ERROR_SUCCESS)
				{
					addDriverInfo (name, hk);
				}
				else
				{
					break;
				}
			}

			RegCloseKey (hk);
		}
	}

	const StringArray getDeviceNames (bool /*wantInputNames*/) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		return deviceNames;
	}

	int getDefaultDeviceIndex (bool) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		for (int i = deviceNames.size(); --i >= 0;)
			if (deviceNames[i].containsIgnoreCase ("asio4all"))
				return i; // asio4all is a safe choice for a default..

#if JUCE_DEBUG
		if (deviceNames.size() > 1 && deviceNames[0].containsIgnoreCase ("digidesign"))
			return 1; // (the digi m-box driver crashes the app when you run
					  // it in the debugger, which can be a bit annoying)
#endif

		return 0;
	}

	static int findFreeSlot()
	{
		for (int i = 0; i < numElementsInArray (currentASIODev); ++i)
			if (currentASIODev[i] == 0)
				return i;

		jassertfalse;  // unfortunately you can only have a finite number
					   // of ASIO devices open at the same time..
		return -1;
	}

	int getIndexOfDevice (AudioIODevice* d, bool /*asInput*/) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		return d == 0 ? -1 : deviceNames.indexOf (d->getName());
	}

	bool hasSeparateInputsAndOutputs() const	{ return false; }

	AudioIODevice* createDevice (const String& outputDeviceName,
								 const String& inputDeviceName)
	{
		// ASIO can't open two different devices for input and output - they must be the same one.
		jassert (inputDeviceName == outputDeviceName || outputDeviceName.isEmpty() || inputDeviceName.isEmpty());
		jassert (hasScanned); // need to call scanForDevices() before doing this

		const int index = deviceNames.indexOf (outputDeviceName.isNotEmpty() ? outputDeviceName
																			 : inputDeviceName);

		if (index >= 0)
		{
			const int freeSlot = findFreeSlot();

			if (freeSlot >= 0)
				return new ASIOAudioIODevice (outputDeviceName, *(classIds [index]), freeSlot, String::empty);
		}

		return 0;
	}

	juce_UseDebuggingNewOperator

private:
	StringArray deviceNames;
	OwnedArray <CLSID> classIds;

	bool hasScanned;

	static bool checkClassIsOk (const String& classId)
	{
		HKEY hk = 0;
		bool ok = false;

		if (RegOpenKey (HKEY_CLASSES_ROOT, _T("clsid"), &hk) == ERROR_SUCCESS)
		{
			int index = 0;

			for (;;)
			{
				WCHAR buf [512];

				if (RegEnumKey (hk, index++, buf, 512) == ERROR_SUCCESS)
				{
					if (classId.equalsIgnoreCase (buf))
					{
						HKEY subKey, pathKey;

						if (RegOpenKeyEx (hk, buf, 0, KEY_READ, &subKey) == ERROR_SUCCESS)
						{
							if (RegOpenKeyEx (subKey, _T("InprocServer32"), 0, KEY_READ, &pathKey) == ERROR_SUCCESS)
							{
								WCHAR pathName [1024];
								DWORD dtype = REG_SZ;
								DWORD dsize = sizeof (pathName);

								if (RegQueryValueEx (pathKey, 0, 0, &dtype, (LPBYTE) pathName, &dsize) == ERROR_SUCCESS)
									ok = File (pathName).exists();

								RegCloseKey (pathKey);
							}

							RegCloseKey (subKey);
						}

						break;
					}
				}
				else
				{
					break;
				}
			}

			RegCloseKey (hk);
		}

		return ok;
	}

	void addDriverInfo (const String& keyName, HKEY hk)
	{
		HKEY subKey;

		if (RegOpenKeyEx (hk, keyName, 0, KEY_READ, &subKey) == ERROR_SUCCESS)
		{
			WCHAR buf [256];
			zerostruct (buf);
			DWORD dtype = REG_SZ;
			DWORD dsize = sizeof (buf);

			if (RegQueryValueEx (subKey, _T("clsid"), 0, &dtype, (LPBYTE) buf, &dsize) == ERROR_SUCCESS)
			{
				if (dsize > 0 && checkClassIsOk (buf))
				{
					CLSID classId;
					if (CLSIDFromString ((LPOLESTR) buf, &classId) == S_OK)
					{
						dtype = REG_SZ;
						dsize = sizeof (buf);
						String deviceName;

						if (RegQueryValueEx (subKey, _T("description"), 0, &dtype, (LPBYTE) buf, &dsize) == ERROR_SUCCESS)
							deviceName = buf;
						else
							deviceName = keyName;

						log ("found " + deviceName);
						deviceNames.add (deviceName);
						classIds.add (new CLSID (classId));
					}
				}

				RegCloseKey (subKey);
			}
		}
	}

	ASIOAudioIODeviceType (const ASIOAudioIODeviceType&);
	ASIOAudioIODeviceType& operator= (const ASIOAudioIODeviceType&);
};

AudioIODeviceType* juce_createAudioIODeviceType_ASIO()
{
	return new ASIOAudioIODeviceType();
}

AudioIODevice* juce_createASIOAudioIODeviceForGUID (const String& name,
													void* guid,
													const String& optionalDllForDirectLoading)
{
	const int freeSlot = ASIOAudioIODeviceType::findFreeSlot();

	if (freeSlot < 0)
		return 0;

	return new ASIOAudioIODevice (name, *(CLSID*) guid, freeSlot, optionalDllForDirectLoading);
}

#undef log

#endif
/*** End of inlined file: juce_win32_ASIO.cpp ***/


/*** Start of inlined file: juce_win32_DirectSound.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_DIRECTSOUND

END_JUCE_NAMESPACE

extern "C"
{

// Declare just the minimum number of interfaces for the DSound objects that we need..
typedef struct typeDSBUFFERDESC
{
	DWORD dwSize;
	DWORD dwFlags;
	DWORD dwBufferBytes;
	DWORD dwReserved;
	LPWAVEFORMATEX lpwfxFormat;
	GUID guid3DAlgorithm;
} DSBUFFERDESC;

struct IDirectSoundBuffer;

#undef INTERFACE
#define INTERFACE IDirectSound
DECLARE_INTERFACE_(IDirectSound, IUnknown)
{
	STDMETHOD(QueryInterface)	   (THIS_ REFIID, LPVOID*) PURE;
	STDMETHOD_(ULONG,AddRef)	(THIS) PURE;
	STDMETHOD_(ULONG,Release)	   (THIS) PURE;
	STDMETHOD(CreateSoundBuffer)	(THIS_ DSBUFFERDESC*, IDirectSoundBuffer**, LPUNKNOWN) PURE;
	STDMETHOD(GetCaps)		  (THIS_ void*) PURE;
	STDMETHOD(DuplicateSoundBuffer) (THIS_ IDirectSoundBuffer*, IDirectSoundBuffer**) PURE;
	STDMETHOD(SetCooperativeLevel)  (THIS_ HWND, DWORD) PURE;
	STDMETHOD(Compact)		  (THIS) PURE;
	STDMETHOD(GetSpeakerConfig)	 (THIS_ LPDWORD) PURE;
	STDMETHOD(SetSpeakerConfig)	 (THIS_ DWORD) PURE;
	STDMETHOD(Initialize)	   (THIS_ const GUID*) PURE;
};

#undef INTERFACE
#define INTERFACE IDirectSoundBuffer
DECLARE_INTERFACE_(IDirectSoundBuffer, IUnknown)
{
	STDMETHOD(QueryInterface)	   (THIS_ REFIID, LPVOID*) PURE;
	STDMETHOD_(ULONG,AddRef)	(THIS) PURE;
	STDMETHOD_(ULONG,Release)	   (THIS) PURE;
	STDMETHOD(GetCaps)		  (THIS_ void*) PURE;
	STDMETHOD(GetCurrentPosition)   (THIS_ LPDWORD, LPDWORD) PURE;
	STDMETHOD(GetFormat)		(THIS_ LPWAVEFORMATEX, DWORD, LPDWORD) PURE;
	STDMETHOD(GetVolume)		(THIS_ LPLONG) PURE;
	STDMETHOD(GetPan)		   (THIS_ LPLONG) PURE;
	STDMETHOD(GetFrequency)	 (THIS_ LPDWORD) PURE;
	STDMETHOD(GetStatus)		(THIS_ LPDWORD) PURE;
	STDMETHOD(Initialize)	   (THIS_ IDirectSound*, DSBUFFERDESC*) PURE;
	STDMETHOD(Lock)		 (THIS_ DWORD, DWORD, LPVOID*, LPDWORD, LPVOID*, LPDWORD, DWORD) PURE;
	STDMETHOD(Play)		 (THIS_ DWORD, DWORD, DWORD) PURE;
	STDMETHOD(SetCurrentPosition)   (THIS_ DWORD) PURE;
	STDMETHOD(SetFormat)		(THIS_ const WAVEFORMATEX*) PURE;
	STDMETHOD(SetVolume)		(THIS_ LONG) PURE;
	STDMETHOD(SetPan)		   (THIS_ LONG) PURE;
	STDMETHOD(SetFrequency)	 (THIS_ DWORD) PURE;
	STDMETHOD(Stop)		 (THIS) PURE;
	STDMETHOD(Unlock)		   (THIS_ LPVOID, DWORD, LPVOID, DWORD) PURE;
	STDMETHOD(Restore)		  (THIS) PURE;
};

typedef struct typeDSCBUFFERDESC
{
	DWORD dwSize;
	DWORD dwFlags;
	DWORD dwBufferBytes;
	DWORD dwReserved;
	LPWAVEFORMATEX lpwfxFormat;
} DSCBUFFERDESC;

struct IDirectSoundCaptureBuffer;

#undef INTERFACE
#define INTERFACE IDirectSoundCapture
DECLARE_INTERFACE_(IDirectSoundCapture, IUnknown)
{
	STDMETHOD(QueryInterface)	   (THIS_ REFIID, LPVOID*) PURE;
	STDMETHOD_(ULONG,AddRef)	(THIS) PURE;
	STDMETHOD_(ULONG,Release)	   (THIS) PURE;
	STDMETHOD(CreateCaptureBuffer)  (THIS_ DSCBUFFERDESC*, IDirectSoundCaptureBuffer**, LPUNKNOWN) PURE;
	STDMETHOD(GetCaps)		  (THIS_ void*) PURE;
	STDMETHOD(Initialize)	   (THIS_ const GUID*) PURE;
};

#undef INTERFACE
#define INTERFACE IDirectSoundCaptureBuffer
DECLARE_INTERFACE_(IDirectSoundCaptureBuffer, IUnknown)
{
	STDMETHOD(QueryInterface)	   (THIS_ REFIID, LPVOID*) PURE;
	STDMETHOD_(ULONG,AddRef)	(THIS) PURE;
	STDMETHOD_(ULONG,Release)	   (THIS) PURE;
	STDMETHOD(GetCaps)		  (THIS_ void*) PURE;
	STDMETHOD(GetCurrentPosition)   (THIS_ LPDWORD, LPDWORD) PURE;
	STDMETHOD(GetFormat)		(THIS_ LPWAVEFORMATEX, DWORD, LPDWORD) PURE;
	STDMETHOD(GetStatus)		(THIS_ LPDWORD) PURE;
	STDMETHOD(Initialize)	   (THIS_ IDirectSoundCapture*, DSCBUFFERDESC*) PURE;
	STDMETHOD(Lock)		 (THIS_ DWORD, DWORD, LPVOID*, LPDWORD, LPVOID*, LPDWORD, DWORD) PURE;
	STDMETHOD(Start)		(THIS_ DWORD) PURE;
	STDMETHOD(Stop)		 (THIS) PURE;
	STDMETHOD(Unlock)		   (THIS_ LPVOID, DWORD, LPVOID, DWORD) PURE;
};

};

BEGIN_JUCE_NAMESPACE

static const String getDSErrorMessage (HRESULT hr)
{
	const char* result = 0;

	switch (hr)
	{
	case MAKE_HRESULT(1, 0x878, 10):
		result = "Device already allocated";
		break;
	case MAKE_HRESULT(1, 0x878, 30):
		result = "Control unavailable";
		break;
	case E_INVALIDARG:
		result = "Invalid parameter";
		break;
	case MAKE_HRESULT(1, 0x878, 50):
		result = "Invalid call";
		break;
	case E_FAIL:
		result = "Generic error";
		break;
	case MAKE_HRESULT(1, 0x878, 70):
		result = "Priority level error";
		break;
	case E_OUTOFMEMORY:
		result = "Out of memory";
		break;
	case MAKE_HRESULT(1, 0x878, 100):
		result = "Bad format";
		break;
	case E_NOTIMPL:
		result = "Unsupported function";
		break;
	case MAKE_HRESULT(1, 0x878, 120):
		result = "No driver";
		break;
	case MAKE_HRESULT(1, 0x878, 130):
		result = "Already initialised";
		break;
	case CLASS_E_NOAGGREGATION:
		result = "No aggregation";
		break;
	case MAKE_HRESULT(1, 0x878, 150):
		result = "Buffer lost";
		break;
	case MAKE_HRESULT(1, 0x878, 160):
		result = "Another app has priority";
		break;
	case MAKE_HRESULT(1, 0x878, 170):
		result = "Uninitialised";
		break;
	case E_NOINTERFACE:
		result = "No interface";
		break;
	case S_OK:
		result = "No error";
		break;

	default:
		return "Unknown error: " + String ((int) hr);
	}

	return result;
}

#define DS_DEBUGGING 1

#ifdef DS_DEBUGGING
	#define CATCH JUCE_CATCH_EXCEPTION
	#undef log
	#define log(a) Logger::writeToLog(a);
	#undef logError
	#define logError(a) logDSError(a, __LINE__);

	static void logDSError (HRESULT hr, int lineNum)
	{
		if (hr != S_OK)
		{
			String error ("DS error at line ");
			error << lineNum << " - " << getDSErrorMessage (hr);
			log (error);
		}
	}
#else
	#define CATCH JUCE_CATCH_ALL
	#define log(a)
	#define logError(a)
#endif

#define DSOUND_FUNCTION(functionName, params) \
	typedef HRESULT (WINAPI *type##functionName) params; \
	static type##functionName ds##functionName = 0;

#define DSOUND_FUNCTION_LOAD(functionName) \
	ds##functionName = (type##functionName) GetProcAddress (h, #functionName);  \
	jassert (ds##functionName != 0);

typedef BOOL (CALLBACK *LPDSENUMCALLBACKW) (LPGUID, LPCWSTR, LPCWSTR, LPVOID);
typedef BOOL (CALLBACK *LPDSENUMCALLBACKA) (LPGUID, LPCSTR, LPCSTR, LPVOID);

DSOUND_FUNCTION (DirectSoundCreate, (const GUID*, IDirectSound**, LPUNKNOWN))
DSOUND_FUNCTION (DirectSoundCaptureCreate, (const GUID*, IDirectSoundCapture**, LPUNKNOWN))
DSOUND_FUNCTION (DirectSoundEnumerateW, (LPDSENUMCALLBACKW, LPVOID))
DSOUND_FUNCTION (DirectSoundCaptureEnumerateW, (LPDSENUMCALLBACKW, LPVOID))

static void initialiseDSoundFunctions()
{
	if (dsDirectSoundCreate == 0)
	{
		HMODULE h = LoadLibraryA ("dsound.dll");

		DSOUND_FUNCTION_LOAD (DirectSoundCreate)
		DSOUND_FUNCTION_LOAD (DirectSoundCaptureCreate)
		DSOUND_FUNCTION_LOAD (DirectSoundEnumerateW)
		DSOUND_FUNCTION_LOAD (DirectSoundCaptureEnumerateW)
	}
}

class DSoundInternalOutChannel
{
	String name;
	LPGUID guid;
	int sampleRate, bufferSizeSamples;
	float* leftBuffer;
	float* rightBuffer;

	IDirectSound* pDirectSound;
	IDirectSoundBuffer* pOutputBuffer;
	DWORD writeOffset;
	int totalBytesPerBuffer;
	int bytesPerBuffer;
	unsigned int lastPlayCursor;

public:
	int bitDepth;
	bool doneFlag;

	DSoundInternalOutChannel (const String& name_,
							  LPGUID guid_,
							  int rate,
							  int bufferSize,
							  float* left,
							  float* right)
		: name (name_),
		  guid (guid_),
		  sampleRate (rate),
		  bufferSizeSamples (bufferSize),
		  leftBuffer (left),
		  rightBuffer (right),
		  pDirectSound (0),
		  pOutputBuffer (0),
		  bitDepth (16)
	{
	}

	~DSoundInternalOutChannel()
	{
		close();
	}

	void close()
	{
		HRESULT hr;

		if (pOutputBuffer != 0)
		{
			JUCE_TRY
			{
				log ("closing dsound out: " + name);
				hr = pOutputBuffer->Stop();
				logError (hr);
			}
			CATCH

			JUCE_TRY
			{
				hr = pOutputBuffer->Release();
				logError (hr);
			}
			CATCH

			pOutputBuffer = 0;
		}

		if (pDirectSound != 0)
		{
			JUCE_TRY
			{
				hr = pDirectSound->Release();
				logError (hr);
			}
			CATCH

			pDirectSound = 0;
		}
	}

	const String open()
	{
		log ("opening dsound out device: " + name + "  rate=" + String (sampleRate)
			  + " bits=" + String (bitDepth) + " buf=" + String (bufferSizeSamples));

		pDirectSound = 0;
		pOutputBuffer = 0;
		writeOffset = 0;

		String error;
		HRESULT hr = E_NOINTERFACE;

		if (dsDirectSoundCreate != 0)
			hr = dsDirectSoundCreate (guid, &pDirectSound, 0);

		if (hr == S_OK)
		{
			bytesPerBuffer = (bufferSizeSamples * (bitDepth >> 2)) & ~15;
			totalBytesPerBuffer = (3 * bytesPerBuffer) & ~15;
			const int numChannels = 2;

			hr = pDirectSound->SetCooperativeLevel (GetDesktopWindow(), 2 /* DSSCL_PRIORITY  */);
			logError (hr);

			if (hr == S_OK)
			{
				IDirectSoundBuffer* pPrimaryBuffer;

				DSBUFFERDESC primaryDesc;
				zerostruct (primaryDesc);

				primaryDesc.dwSize = sizeof (DSBUFFERDESC);
				primaryDesc.dwFlags = 1 /* DSBCAPS_PRIMARYBUFFER */;
				primaryDesc.dwBufferBytes = 0;
				primaryDesc.lpwfxFormat = 0;

				log ("opening dsound out step 2");
				hr = pDirectSound->CreateSoundBuffer (&primaryDesc, &pPrimaryBuffer, 0);
				logError (hr);

				if (hr == S_OK)
				{
					WAVEFORMATEX wfFormat;
					wfFormat.wFormatTag = WAVE_FORMAT_PCM;
					wfFormat.nChannels = (unsigned short) numChannels;
					wfFormat.nSamplesPerSec = sampleRate;
					wfFormat.wBitsPerSample = (unsigned short) bitDepth;
					wfFormat.nBlockAlign = (unsigned short) (wfFormat.nChannels * wfFormat.wBitsPerSample / 8);
					wfFormat.nAvgBytesPerSec = wfFormat.nSamplesPerSec * wfFormat.nBlockAlign;
					wfFormat.cbSize = 0;

					hr = pPrimaryBuffer->SetFormat (&wfFormat);
					logError (hr);

					if (hr == S_OK)
					{
						DSBUFFERDESC secondaryDesc;
						zerostruct (secondaryDesc);

						secondaryDesc.dwSize = sizeof (DSBUFFERDESC);
						secondaryDesc.dwFlags =  0x8000 /* DSBCAPS_GLOBALFOCUS */
												  | 0x10000 /* DSBCAPS_GETCURRENTPOSITION2 */;
						secondaryDesc.dwBufferBytes = totalBytesPerBuffer;
						secondaryDesc.lpwfxFormat = &wfFormat;

						hr = pDirectSound->CreateSoundBuffer (&secondaryDesc, &pOutputBuffer, 0);
						logError (hr);

						if (hr == S_OK)
						{
							log ("opening dsound out step 3");

							DWORD dwDataLen;
							unsigned char* pDSBuffData;

							hr = pOutputBuffer->Lock (0, totalBytesPerBuffer,
													  (LPVOID*) &pDSBuffData, &dwDataLen, 0, 0, 0);
							logError (hr);

							if (hr == S_OK)
							{
								zeromem (pDSBuffData, dwDataLen);

								hr = pOutputBuffer->Unlock (pDSBuffData, dwDataLen, 0, 0);

								if (hr == S_OK)
								{
									hr = pOutputBuffer->SetCurrentPosition (0);

									if (hr == S_OK)
									{
										hr = pOutputBuffer->Play (0, 0, 1 /* DSBPLAY_LOOPING */);

										if (hr == S_OK)
											return String::empty;
									}
								}
							}
						}
					}
				}
			}
		}

		error = getDSErrorMessage (hr);
		close();
		return error;
	}

	void synchronisePosition()
	{
		if (pOutputBuffer != 0)
		{
			DWORD playCursor;
			pOutputBuffer->GetCurrentPosition (&playCursor, &writeOffset);
		}
	}

	bool service()
	{
		if (pOutputBuffer == 0)
			return true;

		DWORD playCursor, writeCursor;

		for (;;)
		{
			HRESULT hr = pOutputBuffer->GetCurrentPosition (&playCursor, &writeCursor);

			if (hr == MAKE_HRESULT (1, 0x878, 150)) // DSERR_BUFFERLOST
			{
				pOutputBuffer->Restore();
				continue;
			}

			if (hr == S_OK)
				break;

			logError (hr);
			jassertfalse
			return true;
		}

		int playWriteGap = writeCursor - playCursor;
		if (playWriteGap < 0)
			playWriteGap += totalBytesPerBuffer;

		int bytesEmpty = playCursor - writeOffset;

		if (bytesEmpty < 0)
			bytesEmpty += totalBytesPerBuffer;

		if (bytesEmpty > (totalBytesPerBuffer - playWriteGap))
		{
			writeOffset = writeCursor;
			bytesEmpty = totalBytesPerBuffer - playWriteGap;
		}

		if (bytesEmpty >= bytesPerBuffer)
		{
			LPBYTE lpbuf1 = 0;
			LPBYTE lpbuf2 = 0;
			DWORD dwSize1 = 0;
			DWORD dwSize2 = 0;

			HRESULT hr = pOutputBuffer->Lock (writeOffset,
											  bytesPerBuffer,
											  (void**) &lpbuf1, &dwSize1,
											  (void**) &lpbuf2, &dwSize2, 0);

			if (hr == MAKE_HRESULT (1, 0x878, 150)) // DSERR_BUFFERLOST
			{
				pOutputBuffer->Restore();

				hr = pOutputBuffer->Lock (writeOffset,
										  bytesPerBuffer,
										  (void**) &lpbuf1, &dwSize1,
										  (void**) &lpbuf2, &dwSize2, 0);
			}

			if (hr == S_OK)
			{
				if (bitDepth == 16)
				{
					const float gainL = 32767.0f;
					const float gainR = 32767.0f;

					int* dest = (int*)lpbuf1;
					const float* left = leftBuffer;
					const float* right = rightBuffer;
					int samples1 = dwSize1 >> 2;
					int samples2 = dwSize2 >> 2;

					if (left == 0)
					{
						while (--samples1 >= 0)
						{
							int r = roundToInt (gainR * *right++);

							if (r < -32768)
								r = -32768;
							else if (r > 32767)
								r = 32767;

							*dest++ = (r << 16);
						}

						dest = (int*)lpbuf2;

						while (--samples2 >= 0)
						{
							int r = roundToInt (gainR * *right++);

							if (r < -32768)
								r = -32768;
							else if (r > 32767)
								r = 32767;

							*dest++ = (r << 16);
						}
					}
					else if (right == 0)
					{
						while (--samples1 >= 0)
						{
							int l = roundToInt (gainL * *left++);

							if (l < -32768)
								l = -32768;
							else if (l > 32767)
								l = 32767;

							l &= 0xffff;

							*dest++ = l;
						}

						dest = (int*)lpbuf2;

						while (--samples2 >= 0)
						{
							int l = roundToInt (gainL * *left++);

							if (l < -32768)
								l = -32768;
							else if (l > 32767)
								l = 32767;

							l &= 0xffff;

							*dest++ = l;
						}
					}
					else
					{
						while (--samples1 >= 0)
						{
							int l = roundToInt (gainL * *left++);

							if (l < -32768)
								l = -32768;
							else if (l > 32767)
								l = 32767;

							l &= 0xffff;

							int r = roundToInt (gainR * *right++);

							if (r < -32768)
								r = -32768;
							else if (r > 32767)
								r = 32767;

							*dest++ = (r << 16) | l;
						}

						dest = (int*)lpbuf2;

						while (--samples2 >= 0)
						{
							int l = roundToInt (gainL * *left++);

							if (l < -32768)
								l = -32768;
							else if (l > 32767)
								l = 32767;

							l &= 0xffff;

							int r = roundToInt (gainR * *right++);

							if (r < -32768)
								r = -32768;
							else if (r > 32767)
								r = 32767;

							*dest++ = (r << 16) | l;
						}
					}
				}
				else
				{
					jassertfalse
				}

				writeOffset = (writeOffset + dwSize1 + dwSize2) % totalBytesPerBuffer;

				pOutputBuffer->Unlock (lpbuf1, dwSize1, lpbuf2, dwSize2);
			}
			else
			{
				jassertfalse
				logError (hr);
			}

			bytesEmpty -= bytesPerBuffer;

			return true;
		}
		else
		{
			return false;
		}
	}
};

struct DSoundInternalInChannel
{
	String name;
	LPGUID guid;
	int sampleRate, bufferSizeSamples;
	float* leftBuffer;
	float* rightBuffer;

	IDirectSound* pDirectSound;
	IDirectSoundCapture* pDirectSoundCapture;
	IDirectSoundCaptureBuffer* pInputBuffer;

public:
	unsigned int readOffset;
	int bytesPerBuffer, totalBytesPerBuffer;
	int bitDepth;
	bool doneFlag;

	DSoundInternalInChannel (const String& name_,
							 LPGUID guid_,
							 int rate,
							 int bufferSize,
							 float* left,
							 float* right)
		: name (name_),
		  guid (guid_),
		  sampleRate (rate),
		  bufferSizeSamples (bufferSize),
		  leftBuffer (left),
		  rightBuffer (right),
		  pDirectSound (0),
		  pDirectSoundCapture (0),
		  pInputBuffer (0),
		  bitDepth (16)
	{
	}

	~DSoundInternalInChannel()
	{
		close();
	}

	void close()
	{
		HRESULT hr;

		if (pInputBuffer != 0)
		{
			JUCE_TRY
			{
				log ("closing dsound in: " + name);
				hr = pInputBuffer->Stop();
				logError (hr);
			}
			CATCH

			JUCE_TRY
			{
				hr = pInputBuffer->Release();
				logError (hr);
			}
			CATCH

			pInputBuffer = 0;
		}

		if (pDirectSoundCapture != 0)
		{
			JUCE_TRY
			{
				hr = pDirectSoundCapture->Release();
				logError (hr);
			}
			CATCH

			pDirectSoundCapture = 0;
		}

		if (pDirectSound != 0)
		{
			JUCE_TRY
			{
				hr = pDirectSound->Release();
				logError (hr);
			}
			CATCH

			pDirectSound = 0;
		}
	}

	const String open()
	{
		log ("opening dsound in device: " + name
			 + "  rate=" + String (sampleRate) + " bits=" + String (bitDepth) + " buf=" + String (bufferSizeSamples));

		pDirectSound = 0;
		pDirectSoundCapture = 0;
		pInputBuffer = 0;
		readOffset = 0;
		totalBytesPerBuffer = 0;

		String error;
		HRESULT hr = E_NOINTERFACE;

		if (dsDirectSoundCaptureCreate != 0)
			hr = dsDirectSoundCaptureCreate (guid, &pDirectSoundCapture, 0);

		logError (hr);

		if (hr == S_OK)
		{
			const int numChannels = 2;
			bytesPerBuffer = (bufferSizeSamples * (bitDepth >> 2)) & ~15;
			totalBytesPerBuffer = (3 * bytesPerBuffer) & ~15;

			WAVEFORMATEX wfFormat;
			wfFormat.wFormatTag = WAVE_FORMAT_PCM;
			wfFormat.nChannels = (unsigned short)numChannels;
			wfFormat.nSamplesPerSec = sampleRate;
			wfFormat.wBitsPerSample = (unsigned short)bitDepth;
			wfFormat.nBlockAlign = (unsigned short)(wfFormat.nChannels * (wfFormat.wBitsPerSample / 8));
			wfFormat.nAvgBytesPerSec = wfFormat.nSamplesPerSec * wfFormat.nBlockAlign;
			wfFormat.cbSize = 0;

			DSCBUFFERDESC captureDesc;
			zerostruct (captureDesc);

			captureDesc.dwSize = sizeof (DSCBUFFERDESC);
			captureDesc.dwFlags = 0;
			captureDesc.dwBufferBytes = totalBytesPerBuffer;
			captureDesc.lpwfxFormat = &wfFormat;

			log ("opening dsound in step 2");
			hr = pDirectSoundCapture->CreateCaptureBuffer (&captureDesc, &pInputBuffer, 0);

			logError (hr);

			if (hr == S_OK)
			{
				hr = pInputBuffer->Start (1 /* DSCBSTART_LOOPING */);
				logError (hr);

				if (hr == S_OK)
					return String::empty;
			}
		}

		error = getDSErrorMessage (hr);
		close();

		return error;
	}

	void synchronisePosition()
	{
		if (pInputBuffer != 0)
		{
			DWORD capturePos;
			pInputBuffer->GetCurrentPosition (&capturePos, (DWORD*)&readOffset);
		}
	}

	bool service()
	{
		if (pInputBuffer == 0)
			return true;

		DWORD capturePos, readPos;
		HRESULT hr = pInputBuffer->GetCurrentPosition (&capturePos, &readPos);
		logError (hr);

		if (hr != S_OK)
			return true;

		int bytesFilled = readPos - readOffset;
		if (bytesFilled < 0)
			bytesFilled += totalBytesPerBuffer;

		if (bytesFilled >= bytesPerBuffer)
		{
			LPBYTE lpbuf1 = 0;
			LPBYTE lpbuf2 = 0;
			DWORD dwsize1 = 0;
			DWORD dwsize2 = 0;

			HRESULT hr = pInputBuffer->Lock (readOffset,
											 bytesPerBuffer,
											 (void**) &lpbuf1, &dwsize1,
											 (void**) &lpbuf2, &dwsize2, 0);

			if (hr == S_OK)
			{
				if (bitDepth == 16)
				{
					const float g = 1.0f / 32768.0f;

					float* destL = leftBuffer;
					float* destR = rightBuffer;
					int samples1 = dwsize1 >> 2;
					int samples2 = dwsize2 >> 2;

					const short* src = (const short*)lpbuf1;

					if (destL == 0)
					{
						while (--samples1 >= 0)
						{
							++src;
							*destR++ = *src++ * g;
						}

						src = (const short*)lpbuf2;

						while (--samples2 >= 0)
						{
							++src;
							*destR++ = *src++ * g;
						}
					}
					else if (destR == 0)
					{
						while (--samples1 >= 0)
						{
							*destL++ = *src++ * g;
							++src;
						}

						src = (const short*)lpbuf2;

						while (--samples2 >= 0)
						{
							*destL++ = *src++ * g;
							++src;
						}
					}
					else
					{
						while (--samples1 >= 0)
						{
							*destL++ = *src++ * g;
							*destR++ = *src++ * g;
						}

						src = (const short*)lpbuf2;

						while (--samples2 >= 0)
						{
							*destL++ = *src++ * g;
							*destR++ = *src++ * g;
						}
					}
				}
				else
				{
					jassertfalse
				}

				readOffset = (readOffset + dwsize1 + dwsize2) % totalBytesPerBuffer;

				pInputBuffer->Unlock (lpbuf1, dwsize1, lpbuf2, dwsize2);
			}
			else
			{
				logError (hr);
				jassertfalse
			}

			bytesFilled -= bytesPerBuffer;

			return true;
		}
		else
		{
			return false;
		}
	}
};

class DSoundAudioIODevice  : public AudioIODevice,
							 public Thread
{
public:
	DSoundAudioIODevice (const String& deviceName,
						 const int outputDeviceIndex_,
						 const int inputDeviceIndex_)
		: AudioIODevice (deviceName, "DirectSound"),
		  Thread ("Juce DSound"),
		  isOpen_ (false),
		  isStarted (false),
		  outputDeviceIndex (outputDeviceIndex_),
		  inputDeviceIndex (inputDeviceIndex_),
		  numInputBuffers (0),
		  numOutputBuffers (0),
		  totalSamplesOut (0),
		  sampleRate (0.0),
		  inputBuffers (0),
		  outputBuffers (0),
		  callback (0),
		  bufferSizeSamples (0)
	{
		if (outputDeviceIndex_ >= 0)
		{
			outChannels.add (TRANS("Left"));
			outChannels.add (TRANS("Right"));
		}

		if (inputDeviceIndex_ >= 0)
		{
			inChannels.add (TRANS("Left"));
			inChannels.add (TRANS("Right"));
		}
	}

	~DSoundAudioIODevice()
	{
		close();
	}

	const StringArray getOutputChannelNames()
	{
		return outChannels;
	}

	const StringArray getInputChannelNames()
	{
		return inChannels;
	}

	int getNumSampleRates()
	{
		return 4;
	}

	double getSampleRate (int index)
	{
		const double samps[] = { 44100.0, 48000.0, 88200.0, 96000.0 };

		return samps [jlimit (0, 3, index)];
	}

	int getNumBufferSizesAvailable()
	{
		return 50;
	}

	int getBufferSizeSamples (int index)
	{
		int n = 64;
		for (int i = 0; i < index; ++i)
			n += (n < 512) ? 32
						   : ((n < 1024) ? 64
										 : ((n < 2048) ? 128 : 256));

		return n;
	}

	int getDefaultBufferSize()
	{
		return 2560;
	}

	const String open (const BigInteger& inputChannels,
					   const BigInteger& outputChannels,
					   double sampleRate,
					   int bufferSizeSamples)
	{
		lastError = openDevice (inputChannels, outputChannels, sampleRate, bufferSizeSamples);
		isOpen_ = lastError.isEmpty();

		return lastError;
	}

	void close()
	{
		stop();

		if (isOpen_)
		{
			closeDevice();
			isOpen_ = false;
		}
	}

	bool isOpen()
	{
		return isOpen_ && isThreadRunning();
	}

	int getCurrentBufferSizeSamples()
	{
		return bufferSizeSamples;
	}

	double getCurrentSampleRate()
	{
		return sampleRate;
	}

	int getCurrentBitDepth()
	{
		int i, bits = 256;

		for (i = inChans.size(); --i >= 0;)
			bits = jmin (bits, inChans[i]->bitDepth);

		for (i = outChans.size(); --i >= 0;)
			bits = jmin (bits, outChans[i]->bitDepth);

		if (bits > 32)
			bits = 16;

		return bits;
	}

	const BigInteger getActiveOutputChannels() const
	{
		return enabledOutputs;
	}

	const BigInteger getActiveInputChannels() const
	{
		return enabledInputs;
	}

	int getOutputLatencyInSamples()
	{
		return (int) (getCurrentBufferSizeSamples() * 1.5);
	}

	int getInputLatencyInSamples()
	{
		return getOutputLatencyInSamples();
	}

	void start (AudioIODeviceCallback* call)
	{
		if (isOpen_ && call != 0 && ! isStarted)
		{
			if (! isThreadRunning())
			{
				// something gone wrong and the thread's stopped..
				isOpen_ = false;
				return;
			}

			call->audioDeviceAboutToStart (this);

			const ScopedLock sl (startStopLock);
			callback = call;
			isStarted = true;
		}
	}

	void stop()
	{
		if (isStarted)
		{
			AudioIODeviceCallback* const callbackLocal = callback;

			{
				const ScopedLock sl (startStopLock);
				isStarted = false;
			}

			if (callbackLocal != 0)
				callbackLocal->audioDeviceStopped();
		}
	}

	bool isPlaying()
	{
		return isStarted && isOpen_ && isThreadRunning();
	}

	const String getLastError()
	{
		return lastError;
	}

	juce_UseDebuggingNewOperator

	StringArray inChannels, outChannels;
	int outputDeviceIndex, inputDeviceIndex;

private:
	bool isOpen_;
	bool isStarted;
	String lastError;

	OwnedArray <DSoundInternalInChannel> inChans;
	OwnedArray <DSoundInternalOutChannel> outChans;
	WaitableEvent startEvent;

	int numInputBuffers, numOutputBuffers, bufferSizeSamples;
	int volatile totalSamplesOut;
	int64 volatile lastBlockTime;
	double sampleRate;
	BigInteger enabledInputs, enabledOutputs;
	HeapBlock <float*> inputBuffers, outputBuffers;

	AudioIODeviceCallback* callback;
	CriticalSection startStopLock;

	DSoundAudioIODevice (const DSoundAudioIODevice&);
	DSoundAudioIODevice& operator= (const DSoundAudioIODevice&);

	const String openDevice (const BigInteger& inputChannels,
							 const BigInteger& outputChannels,
							 double sampleRate_,
							 int bufferSizeSamples_);

	void closeDevice()
	{
		isStarted = false;
		stopThread (5000);

		inChans.clear();
		outChans.clear();

		int i;
		for (i = 0; i < numInputBuffers; ++i)
			juce_free (inputBuffers[i]);

		inputBuffers.free();
		numInputBuffers = 0;

		for (i = 0; i < numOutputBuffers; ++i)
			juce_free (outputBuffers[i]);

		outputBuffers.free();
		numOutputBuffers = 0;
	}

	void resync()
	{
		if (! threadShouldExit())
		{
			sleep (5);

			int i;
			for (i = 0; i < outChans.size(); ++i)
				outChans.getUnchecked(i)->synchronisePosition();

			for (i = 0; i < inChans.size(); ++i)
				inChans.getUnchecked(i)->synchronisePosition();
		}
	}

public:
	void run()
	{
		while (! threadShouldExit())
		{
			if (wait (100))
				break;
		}

		const int latencyMs = (int) (bufferSizeSamples * 1000.0 / sampleRate);
		const int maxTimeMS = jmax (5, 3 * latencyMs);

		while (! threadShouldExit())
		{
			int numToDo = 0;
			uint32 startTime = Time::getMillisecondCounter();

			int i;
			for (i = inChans.size(); --i >= 0;)
			{
				inChans.getUnchecked(i)->doneFlag = false;
				++numToDo;
			}

			for (i = outChans.size(); --i >= 0;)
			{
				outChans.getUnchecked(i)->doneFlag = false;
				++numToDo;
			}

			if (numToDo > 0)
			{
				const int maxCount = 3;
				int count = maxCount;

				for (;;)
				{
					for (i = inChans.size(); --i >= 0;)
					{
						DSoundInternalInChannel* const in = inChans.getUnchecked(i);

						if ((! in->doneFlag) && in->service())
						{
							in->doneFlag = true;
							--numToDo;
						}
					}

					for (i = outChans.size(); --i >= 0;)
					{
						DSoundInternalOutChannel* const out = outChans.getUnchecked(i);

						if ((! out->doneFlag) && out->service())
						{
							out->doneFlag = true;
							--numToDo;
						}
					}

					if (numToDo <= 0)
						break;

					if (Time::getMillisecondCounter() > startTime + maxTimeMS)
					{
						resync();
						break;
					}

					if (--count <= 0)
					{
						Sleep (1);
						count = maxCount;
					}

					if (threadShouldExit())
						return;
				}
			}
			else
			{
				sleep (1);
			}

			const ScopedLock sl (startStopLock);

			if (isStarted)
			{
				JUCE_TRY
				{
					callback->audioDeviceIOCallback (const_cast <const float**> (inputBuffers.getData()),
													 numInputBuffers,
													 outputBuffers,
													 numOutputBuffers,
													 bufferSizeSamples);
				}
				JUCE_CATCH_EXCEPTION

				totalSamplesOut += bufferSizeSamples;
			}
			else
			{
				for (i = 0; i < numOutputBuffers; ++i)
					if (outputBuffers[i] != 0)
						zeromem (outputBuffers[i], bufferSizeSamples * sizeof (float));

				totalSamplesOut = 0;
				sleep (1);
			}
		}
	}
};

class DSoundAudioIODeviceType  : public AudioIODeviceType
{
public:
	DSoundAudioIODeviceType()
		: AudioIODeviceType ("DirectSound"),
		  hasScanned (false)
	{
		initialiseDSoundFunctions();
	}

	~DSoundAudioIODeviceType()
	{
	}

	void scanForDevices()
	{
		hasScanned = true;

		outputDeviceNames.clear();
		outputGuids.clear();
		inputDeviceNames.clear();
		inputGuids.clear();

		if (dsDirectSoundEnumerateW != 0)
		{
			dsDirectSoundEnumerateW (outputEnumProcW, this);
			dsDirectSoundCaptureEnumerateW (inputEnumProcW, this);
		}
	}

	const StringArray getDeviceNames (bool wantInputNames) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		return wantInputNames ? inputDeviceNames
							   : outputDeviceNames;
	}

	int getDefaultDeviceIndex (bool /*forInput*/) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this
		return 0;
	}

	int getIndexOfDevice (AudioIODevice* device, bool asInput) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		DSoundAudioIODevice* const d = dynamic_cast <DSoundAudioIODevice*> (device);
		if (d == 0)
			return -1;

		return asInput ? d->inputDeviceIndex
					   : d->outputDeviceIndex;
	}

	bool hasSeparateInputsAndOutputs() const	{ return true; }

	AudioIODevice* createDevice (const String& outputDeviceName,
								 const String& inputDeviceName)
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		const int outputIndex = outputDeviceNames.indexOf (outputDeviceName);
		const int inputIndex = inputDeviceNames.indexOf (inputDeviceName);

		if (outputIndex >= 0 || inputIndex >= 0)
			return new DSoundAudioIODevice (outputDeviceName.isNotEmpty() ? outputDeviceName
																		  : inputDeviceName,
											outputIndex, inputIndex);

		return 0;
	}

	juce_UseDebuggingNewOperator

	StringArray outputDeviceNames;
	OwnedArray <GUID> outputGuids;

	StringArray inputDeviceNames;
	OwnedArray <GUID> inputGuids;

private:
	bool hasScanned;

	BOOL outputEnumProc (LPGUID lpGUID, String desc)
	{
		desc = desc.trim();

		if (desc.isNotEmpty())
		{
			const String origDesc (desc);

			int n = 2;
			while (outputDeviceNames.contains (desc))
				desc = origDesc + " (" + String (n++) + ")";

			outputDeviceNames.add (desc);

			if (lpGUID != 0)
				outputGuids.add (new GUID (*lpGUID));
			else
				outputGuids.add (0);
		}

		return TRUE;
	}

	static BOOL CALLBACK outputEnumProcW (LPGUID lpGUID, LPCWSTR description, LPCWSTR, LPVOID object)
	{
		return ((DSoundAudioIODeviceType*) object)
					->outputEnumProc (lpGUID, String (description));
	}

	static BOOL CALLBACK outputEnumProcA (LPGUID lpGUID, LPCTSTR description, LPCTSTR, LPVOID object)
	{
		return ((DSoundAudioIODeviceType*) object)
					->outputEnumProc (lpGUID, String (description));
	}

	BOOL CALLBACK inputEnumProc (LPGUID lpGUID, String desc)
	{
		desc = desc.trim();

		if (desc.isNotEmpty())
		{
			const String origDesc (desc);

			int n = 2;
			while (inputDeviceNames.contains (desc))
				desc = origDesc + " (" + String (n++) + ")";

			inputDeviceNames.add (desc);

			if (lpGUID != 0)
				inputGuids.add (new GUID (*lpGUID));
			else
				inputGuids.add (0);
		}

		return TRUE;
	}

	static BOOL CALLBACK inputEnumProcW (LPGUID lpGUID, LPCWSTR description, LPCWSTR, LPVOID object)
	{
		return ((DSoundAudioIODeviceType*) object)
					->inputEnumProc (lpGUID, String (description));
	}

	static BOOL CALLBACK inputEnumProcA (LPGUID lpGUID, LPCTSTR description, LPCTSTR, LPVOID object)
	{
		return ((DSoundAudioIODeviceType*) object)
					->inputEnumProc (lpGUID, String (description));
	}

	DSoundAudioIODeviceType (const DSoundAudioIODeviceType&);
	DSoundAudioIODeviceType& operator= (const DSoundAudioIODeviceType&);
};

const String DSoundAudioIODevice::openDevice (const BigInteger& inputChannels,
											  const BigInteger& outputChannels,
											  double sampleRate_,
											  int bufferSizeSamples_)
{
	closeDevice();
	totalSamplesOut = 0;

	sampleRate = sampleRate_;

	if (bufferSizeSamples_ <= 0)
		bufferSizeSamples_ = 960; // use as a default size if none is set.

	bufferSizeSamples = bufferSizeSamples_ & ~7;

	DSoundAudioIODeviceType dlh;
	dlh.scanForDevices();

	enabledInputs = inputChannels;
	enabledInputs.setRange (inChannels.size(),
							enabledInputs.getHighestBit() + 1 - inChannels.size(),
							false);

	numInputBuffers = enabledInputs.countNumberOfSetBits();
	inputBuffers.calloc (numInputBuffers + 2);
	int i, numIns = 0;

	for (i = 0; i <= enabledInputs.getHighestBit(); i += 2)
	{
		float* left = 0;
		float* right = 0;

		if (enabledInputs[i])
			left = inputBuffers[numIns++] = (float*) juce_calloc ((bufferSizeSamples + 16) * sizeof (float));

		if (enabledInputs[i + 1])
			right = inputBuffers[numIns++] = (float*) juce_calloc ((bufferSizeSamples + 16) * sizeof (float));

		if (left != 0 || right != 0)
			inChans.add (new DSoundInternalInChannel (dlh.inputDeviceNames [inputDeviceIndex],
													  dlh.inputGuids [inputDeviceIndex],
													  (int) sampleRate, bufferSizeSamples,
													  left, right));
	}

	enabledOutputs = outputChannels;
	enabledOutputs.setRange (outChannels.size(),
							 enabledOutputs.getHighestBit() + 1 - outChannels.size(),
							 false);

	numOutputBuffers = enabledOutputs.countNumberOfSetBits();
	outputBuffers.calloc (numOutputBuffers + 2);
	int numOuts = 0;

	for (i = 0; i <= enabledOutputs.getHighestBit(); i += 2)
	{
		float* left = 0;
		float* right = 0;

		if (enabledOutputs[i])
			left = outputBuffers[numOuts++] = (float*) juce_calloc ((bufferSizeSamples + 16) * sizeof (float));

		if (enabledOutputs[i + 1])
			right = outputBuffers[numOuts++] = (float*) juce_calloc ((bufferSizeSamples + 16) * sizeof (float));

		if (left != 0 || right != 0)
			outChans.add (new DSoundInternalOutChannel (dlh.outputDeviceNames[outputDeviceIndex],
														dlh.outputGuids [outputDeviceIndex],
														(int) sampleRate, bufferSizeSamples,
														left, right));
	}

	String error;

	// boost our priority while opening the devices to try to get better sync between them
	const int oldThreadPri = GetThreadPriority (GetCurrentThread());
	const int oldProcPri = GetPriorityClass (GetCurrentProcess());
	SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
	SetPriorityClass (GetCurrentProcess(), REALTIME_PRIORITY_CLASS);

	for (i = 0; i < outChans.size(); ++i)
	{
		error = outChans[i]->open();

		if (error.isNotEmpty())
		{
			error = "Error opening " + dlh.outputDeviceNames[i] + ": \"" + error + "\"";
			break;
		}
	}

	if (error.isEmpty())
	{
		for (i = 0; i < inChans.size(); ++i)
		{
			error = inChans[i]->open();

			if (error.isNotEmpty())
			{
				error = "Error opening " + dlh.inputDeviceNames[i] + ": \"" + error + "\"";
				break;
			}
		}
	}

	if (error.isEmpty())
	{
		totalSamplesOut = 0;

		for (i = 0; i < outChans.size(); ++i)
			outChans.getUnchecked(i)->synchronisePosition();

		for (i = 0; i < inChans.size(); ++i)
			inChans.getUnchecked(i)->synchronisePosition();

		startThread (9);
		sleep (10);

		notify();
	}
	else
	{
		log (error);
	}

	SetThreadPriority (GetCurrentThread(), oldThreadPri);
	SetPriorityClass (GetCurrentProcess(), oldProcPri);

	return error;
}

AudioIODeviceType* juce_createAudioIODeviceType_DirectSound()
{
	return new DSoundAudioIODeviceType();
}

#undef log

#endif
/*** End of inlined file: juce_win32_DirectSound.cpp ***/


/*** Start of inlined file: juce_win32_WASAPI.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_WASAPI

#if 1

const String getAudioErrorDesc (HRESULT hr)
{
	const char* e = 0;

	switch (hr)
	{
		case E_POINTER:                                 e = "E_POINTER"; break;
		case E_INVALIDARG:                              e = "E_INVALIDARG"; break;
		case AUDCLNT_E_NOT_INITIALIZED:                 e = "AUDCLNT_E_NOT_INITIALIZED"; break;
		case AUDCLNT_E_ALREADY_INITIALIZED:             e = "AUDCLNT_E_ALREADY_INITIALIZED"; break;
		case AUDCLNT_E_WRONG_ENDPOINT_TYPE:             e = "AUDCLNT_E_WRONG_ENDPOINT_TYPE"; break;
		case AUDCLNT_E_DEVICE_INVALIDATED:              e = "AUDCLNT_E_DEVICE_INVALIDATED"; break;
		case AUDCLNT_E_NOT_STOPPED:                     e = "AUDCLNT_E_NOT_STOPPED"; break;
		case AUDCLNT_E_BUFFER_TOO_LARGE:                e = "AUDCLNT_E_BUFFER_TOO_LARGE"; break;
		case AUDCLNT_E_OUT_OF_ORDER:                    e = "AUDCLNT_E_OUT_OF_ORDER"; break;
		case AUDCLNT_E_UNSUPPORTED_FORMAT:              e = "AUDCLNT_E_UNSUPPORTED_FORMAT"; break;
		case AUDCLNT_E_INVALID_SIZE:                    e = "AUDCLNT_E_INVALID_SIZE"; break;
		case AUDCLNT_E_DEVICE_IN_USE:                   e = "AUDCLNT_E_DEVICE_IN_USE"; break;
		case AUDCLNT_E_BUFFER_OPERATION_PENDING:        e = "AUDCLNT_E_BUFFER_OPERATION_PENDING"; break;
		case AUDCLNT_E_THREAD_NOT_REGISTERED:           e = "AUDCLNT_E_THREAD_NOT_REGISTERED"; break;
		case AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED:      e = "AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED"; break;
		case AUDCLNT_E_ENDPOINT_CREATE_FAILED:          e = "AUDCLNT_E_ENDPOINT_CREATE_FAILED"; break;
		case AUDCLNT_E_SERVICE_NOT_RUNNING:             e = "AUDCLNT_E_SERVICE_NOT_RUNNING"; break;
		case AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED:        e = "AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED"; break;
		case AUDCLNT_E_EXCLUSIVE_MODE_ONLY:             e = "AUDCLNT_E_EXCLUSIVE_MODE_ONLY"; break;
		case AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL:    e = "AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL"; break;
		case AUDCLNT_E_EVENTHANDLE_NOT_SET:             e = "AUDCLNT_E_EVENTHANDLE_NOT_SET"; break;
		case AUDCLNT_E_INCORRECT_BUFFER_SIZE:           e = "AUDCLNT_E_INCORRECT_BUFFER_SIZE"; break;
		case AUDCLNT_E_BUFFER_SIZE_ERROR:               e = "AUDCLNT_E_BUFFER_SIZE_ERROR"; break;
		case AUDCLNT_S_BUFFER_EMPTY:                    e = "AUDCLNT_S_BUFFER_EMPTY"; break;
		case AUDCLNT_S_THREAD_ALREADY_REGISTERED:       e = "AUDCLNT_S_THREAD_ALREADY_REGISTERED"; break;
		default:					return String::toHexString ((int) hr);
	}

	return e;
}

#define logFailure(hr) { if (FAILED (hr)) { DBG ("WASAPI FAIL! " + getAudioErrorDesc (hr)); jassertfalse } }
#define OK(a) wasapi_checkResult(a)

static bool wasapi_checkResult (HRESULT hr)
{
	logFailure (hr);
	return SUCCEEDED (hr);
}

#else
 #define logFailure(hr) {}
 #define OK(a) SUCCEEDED(a)
#endif

static const String wasapi_getDeviceID (IMMDevice* const device)
{
	String s;
	WCHAR* deviceId = 0;

	if (OK (device->GetId (&deviceId)))
	{
		s = String (deviceId);
		CoTaskMemFree (deviceId);
	}

	return s;
}

static EDataFlow wasapi_getDataFlow (IMMDevice* const device)
{
	EDataFlow flow = eRender;
	ComSmartPtr <IMMEndpoint> endPoint;
	if (OK (device->QueryInterface (__uuidof (IMMEndpoint), (void**) &endPoint)))
		(void) OK (endPoint->GetDataFlow (&flow));

	return flow;
}

static int wasapi_refTimeToSamples (const REFERENCE_TIME& t, const double sampleRate) throw()
{
	return roundDoubleToInt (sampleRate * ((double) t) * 0.0000001);
}

static void wasapi_copyWavFormat (WAVEFORMATEXTENSIBLE& dest, const WAVEFORMATEX* const src) throw()
{
	memcpy (&dest, src, src->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? sizeof (WAVEFORMATEXTENSIBLE)
																  : sizeof (WAVEFORMATEX));
}

class WASAPIDeviceBase
{
public:
	WASAPIDeviceBase (const ComSmartPtr <IMMDevice>& device_, const bool useExclusiveMode_)
		: device (device_),
		  sampleRate (0),
		  numChannels (0),
		  actualNumChannels (0),
		  defaultSampleRate (0),
		  minBufferSize (0),
		  defaultBufferSize (0),
		  latencySamples (0),
		  useExclusiveMode (useExclusiveMode_)
	{
		clientEvent = CreateEvent (0, false, false, _T("JuceWASAPI"));

		ComSmartPtr <IAudioClient> tempClient (createClient());
		if (tempClient == 0)
			return;

		REFERENCE_TIME defaultPeriod, minPeriod;
		if (! OK (tempClient->GetDevicePeriod (&defaultPeriod, &minPeriod)))
			return;

		WAVEFORMATEX* mixFormat = 0;
		if (! OK (tempClient->GetMixFormat (&mixFormat)))
			return;

		WAVEFORMATEXTENSIBLE format;
		wasapi_copyWavFormat (format, mixFormat);
		CoTaskMemFree (mixFormat);

		actualNumChannels = numChannels = format.Format.nChannels;
		defaultSampleRate = format.Format.nSamplesPerSec;
		minBufferSize = wasapi_refTimeToSamples (minPeriod, defaultSampleRate);
		defaultBufferSize = wasapi_refTimeToSamples (defaultPeriod, defaultSampleRate);

		rates.addUsingDefaultSort (defaultSampleRate);

		static const double ratesToTest[] = { 44100.0, 48000.0, 88200.0, 96000.0 };

		for (int i = 0; i < numElementsInArray (ratesToTest); ++i)
		{
			if (ratesToTest[i] == defaultSampleRate)
				continue;

			format.Format.nSamplesPerSec = roundDoubleToInt (ratesToTest[i]);

			if (SUCCEEDED (tempClient->IsFormatSupported (useExclusiveMode ? AUDCLNT_SHAREMODE_EXCLUSIVE : AUDCLNT_SHAREMODE_SHARED,
														  (WAVEFORMATEX*) &format, 0)))
				if (! rates.contains (ratesToTest[i]))
					rates.addUsingDefaultSort (ratesToTest[i]);
		}
	}

	~WASAPIDeviceBase()
	{
		device = 0;
		CloseHandle (clientEvent);
	}

	bool isOk() const throw()	 { return defaultBufferSize > 0 && defaultSampleRate > 0; }

	bool openClient (const double newSampleRate, const BigInteger& newChannels)
	{
		sampleRate = newSampleRate;
		channels = newChannels;
		channels.setRange (actualNumChannels, channels.getHighestBit() + 1 - actualNumChannels, false);
		numChannels = channels.getHighestBit() + 1;

		if (numChannels == 0)
			return true;

		client = createClient();

		if (client != 0
			 && (tryInitialisingWithFormat (true, 4) || tryInitialisingWithFormat (false, 4)
				  || tryInitialisingWithFormat (false, 3) || tryInitialisingWithFormat (false, 2)))
		{
			channelMaps.clear();
			for (int i = 0; i <= channels.getHighestBit(); ++i)
				if (channels[i])
					channelMaps.add (i);

			REFERENCE_TIME latency;
			if (OK (client->GetStreamLatency (&latency)))
				latencySamples = wasapi_refTimeToSamples (latency, sampleRate);

			(void) OK (client->GetBufferSize (&actualBufferSize));

			return OK (client->SetEventHandle (clientEvent));
		}

		return false;
	}

	void closeClient()
	{
		if (client != 0)
			client->Stop();

		client = 0;
		ResetEvent (clientEvent);
	}

	ComSmartPtr <IMMDevice> device;
	ComSmartPtr <IAudioClient> client;
	double sampleRate, defaultSampleRate;
	int numChannels, actualNumChannels;
	int minBufferSize, defaultBufferSize, latencySamples;
	const bool useExclusiveMode;
	Array <double> rates;
	HANDLE clientEvent;
	BigInteger channels;
	AudioDataConverters::DataFormat dataFormat;
	Array <int> channelMaps;
	UINT32 actualBufferSize;
	int bytesPerSample;

private:
	const ComSmartPtr <IAudioClient> createClient()
	{
		ComSmartPtr <IAudioClient> client;

		if (device != 0)
		{
			HRESULT hr = device->Activate (__uuidof (IAudioClient), CLSCTX_INPROC_SERVER, 0, (void**) &client);
			logFailure (hr);
		}

		return client;
	}

	bool tryInitialisingWithFormat (const bool useFloat, const int bytesPerSampleToTry)
	{
		WAVEFORMATEXTENSIBLE format;
		zerostruct (format);

		if (numChannels <= 2 && bytesPerSampleToTry <= 2)
		{
			format.Format.wFormatTag = WAVE_FORMAT_PCM;
		}
		else
		{
			format.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
			format.Format.cbSize = sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX);
		}

		format.Format.nSamplesPerSec = roundDoubleToInt (sampleRate);
		format.Format.nChannels = (WORD) numChannels;
		format.Format.wBitsPerSample = (WORD) (8 * bytesPerSampleToTry);
		format.Format.nAvgBytesPerSec = (DWORD) (format.Format.nSamplesPerSec * numChannels * bytesPerSampleToTry);
		format.Format.nBlockAlign = (WORD) (numChannels * bytesPerSampleToTry);
		format.SubFormat = useFloat ? KSDATAFORMAT_SUBTYPE_IEEE_FLOAT : KSDATAFORMAT_SUBTYPE_PCM;
		format.Samples.wValidBitsPerSample = format.Format.wBitsPerSample;

		switch (numChannels)
		{
			case 1:	 format.dwChannelMask = SPEAKER_FRONT_CENTER; break;
			case 2:	 format.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT; break;
			case 4:	 format.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; break;
			case 6:	 format.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; break;
			case 8:	 format.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_FRONT_LEFT_OF_CENTER | SPEAKER_FRONT_RIGHT_OF_CENTER; break;
			default:	break;
		}

		WAVEFORMATEXTENSIBLE* nearestFormat = 0;

		HRESULT hr = client->IsFormatSupported (useExclusiveMode ? AUDCLNT_SHAREMODE_EXCLUSIVE : AUDCLNT_SHAREMODE_SHARED,
												(WAVEFORMATEX*) &format, useExclusiveMode ? 0 : (WAVEFORMATEX**) &nearestFormat);
		logFailure (hr);

		if (hr == S_FALSE && format.Format.nSamplesPerSec == nearestFormat->Format.nSamplesPerSec)
		{
			wasapi_copyWavFormat (format, (WAVEFORMATEX*) nearestFormat);
			hr = S_OK;
		}

		CoTaskMemFree (nearestFormat);

		REFERENCE_TIME defaultPeriod = 0, minPeriod = 0;
		if (useExclusiveMode)
			OK (client->GetDevicePeriod (&defaultPeriod, &minPeriod));

		GUID session;
		if (hr == S_OK
			 && OK (client->Initialize (useExclusiveMode ? AUDCLNT_SHAREMODE_EXCLUSIVE : AUDCLNT_SHAREMODE_SHARED,
										AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
										defaultPeriod, defaultPeriod, (WAVEFORMATEX*) &format, &session)))
		{
			actualNumChannels = format.Format.nChannels;
			const bool isFloat = format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE && format.SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
			bytesPerSample = format.Format.wBitsPerSample / 8;
			dataFormat = isFloat ? AudioDataConverters::float32LE
								 : (bytesPerSample == 4 ? AudioDataConverters::int32LE
														: ((bytesPerSample == 3 ? AudioDataConverters::int24LE
																				: AudioDataConverters::int16LE)));
			return true;
		}

		return false;
	}

	WASAPIDeviceBase (const WASAPIDeviceBase&);
	WASAPIDeviceBase& operator= (const WASAPIDeviceBase&);
};

class WASAPIInputDevice  : public WASAPIDeviceBase
{
public:
	WASAPIInputDevice (const ComSmartPtr <IMMDevice>& device_, const bool useExclusiveMode_)
		: WASAPIDeviceBase (device_, useExclusiveMode_),
		  reservoir (1, 1)
	{
	}

	~WASAPIInputDevice()
	{
		close();
	}

	bool open (const double newSampleRate, const BigInteger& newChannels)
	{
		reservoirSize = 0;
		reservoirCapacity = 16384;
		reservoir.setSize (actualNumChannels * reservoirCapacity * sizeof (float));
		return openClient (newSampleRate, newChannels)
				&& (numChannels == 0 || OK (client->GetService (__uuidof (IAudioCaptureClient), (void**) &captureClient)));
	}

	void close()
	{
		closeClient();
		captureClient = 0;
		reservoir.setSize (0);
	}

	void copyBuffers (float** destBuffers, int numDestBuffers, int bufferSize, Thread& thread)
	{
		if (numChannels <= 0)
			return;

		int offset = 0;

		while (bufferSize > 0)
		{
			if (reservoirSize > 0)  // There's stuff in the reservoir, so use that...
			{
				const int samplesToDo = jmin (bufferSize, (int) reservoirSize);

				for (int i = 0; i < numDestBuffers; ++i)
				{
					float* const dest = destBuffers[i] + offset;
					const int srcChan = channelMaps.getUnchecked(i);

					switch (dataFormat)
					{
					case AudioDataConverters::float32LE:
						AudioDataConverters::convertFloat32LEToFloat (((uint8*) reservoir.getData()) + 4 * srcChan, dest, samplesToDo, 4 * actualNumChannels);
						break;

					case AudioDataConverters::int32LE:
						AudioDataConverters::convertInt32LEToFloat (((uint8*) reservoir.getData()) + 4 * srcChan, dest, samplesToDo, 4 * actualNumChannels);
						break;

					case AudioDataConverters::int24LE:
						AudioDataConverters::convertInt24LEToFloat (((uint8*) reservoir.getData()) + 3 * srcChan, dest, samplesToDo, 3 * actualNumChannels);
						break;

					case AudioDataConverters::int16LE:
						AudioDataConverters::convertInt16LEToFloat (((uint8*) reservoir.getData()) + 2 * srcChan, dest, samplesToDo, 2 * actualNumChannels);
						break;

					default: jassertfalse; break;
					}
				}

				bufferSize -= samplesToDo;
				offset += samplesToDo;
				reservoirSize -= samplesToDo;
			}
			else
			{
				UINT32 packetLength = 0;
				if (! OK (captureClient->GetNextPacketSize (&packetLength)))
					break;

				if (packetLength == 0)
				{
					if (thread.threadShouldExit())
						break;

					Thread::sleep (1);
					continue;
				}

				uint8* inputData = 0;
				UINT32 numSamplesAvailable;
				DWORD flags;

				if (OK (captureClient->GetBuffer (&inputData, &numSamplesAvailable, &flags, 0, 0)))
				{
					const int samplesToDo = jmin (bufferSize, (int) numSamplesAvailable);

					for (int i = 0; i < numDestBuffers; ++i)
					{
						float* const dest = destBuffers[i] + offset;
						const int srcChan = channelMaps.getUnchecked(i);

						switch (dataFormat)
						{
						case AudioDataConverters::float32LE:
							AudioDataConverters::convertFloat32LEToFloat (inputData + 4 * srcChan, dest, samplesToDo, 4 * actualNumChannels);
							break;

						case AudioDataConverters::int32LE:
							AudioDataConverters::convertInt32LEToFloat (inputData + 4 * srcChan, dest, samplesToDo, 4 * actualNumChannels);
							break;

						case AudioDataConverters::int24LE:
							AudioDataConverters::convertInt24LEToFloat (inputData + 3 * srcChan, dest, samplesToDo, 3 * actualNumChannels);
							break;

						case AudioDataConverters::int16LE:
							AudioDataConverters::convertInt16LEToFloat (inputData + 2 * srcChan, dest, samplesToDo, 2 * actualNumChannels);
							break;

						default: jassertfalse; break;
						}
					}

					bufferSize -= samplesToDo;
					offset += samplesToDo;

					if (samplesToDo < (int) numSamplesAvailable)
					{
						reservoirSize = jmin ((int) (numSamplesAvailable - samplesToDo), reservoirCapacity);
						memcpy ((uint8*) reservoir.getData(), inputData + bytesPerSample * actualNumChannels * samplesToDo,
								bytesPerSample * actualNumChannels * reservoirSize);
					}

					captureClient->ReleaseBuffer (numSamplesAvailable);
				}
			}
		}
	}

	ComSmartPtr <IAudioCaptureClient> captureClient;
	MemoryBlock reservoir;
	int reservoirSize, reservoirCapacity;

private:
	WASAPIInputDevice (const WASAPIInputDevice&);
	WASAPIInputDevice& operator= (const WASAPIInputDevice&);
};

class WASAPIOutputDevice  : public WASAPIDeviceBase
{
public:
	WASAPIOutputDevice (const ComSmartPtr <IMMDevice>& device_, const bool useExclusiveMode_)
		: WASAPIDeviceBase (device_, useExclusiveMode_)
	{
	}

	~WASAPIOutputDevice()
	{
		close();
	}

	bool open (const double newSampleRate, const BigInteger& newChannels)
	{
		return openClient (newSampleRate, newChannels)
				&& (numChannels == 0 || OK (client->GetService (__uuidof (IAudioRenderClient), (void**) &renderClient)));
	}

	void close()
	{
		closeClient();
		renderClient = 0;
	}

	void copyBuffers (const float** const srcBuffers, const int numSrcBuffers, int bufferSize, Thread& thread)
	{
		if (numChannels <= 0)
			return;

		int offset = 0;

		while (bufferSize > 0)
		{
			UINT32 padding = 0;
			if (! OK (client->GetCurrentPadding (&padding)))
				return;

			int samplesToDo = useExclusiveMode ? bufferSize
											   : jmin ((int) (actualBufferSize - padding), bufferSize);

			if (samplesToDo <= 0)
			{
				if (thread.threadShouldExit())
					break;

				Thread::sleep (0);
				continue;
			}

			uint8* outputData = 0;
			if (OK (renderClient->GetBuffer (samplesToDo, &outputData)))
			{
				for (int i = 0; i < numSrcBuffers; ++i)
				{
					const float* const source = srcBuffers[i] + offset;
					const int destChan = channelMaps.getUnchecked(i);

					switch (dataFormat)
					{
					case AudioDataConverters::float32LE:
						AudioDataConverters::convertFloatToFloat32LE (source, outputData + 4 * destChan, samplesToDo, 4 * actualNumChannels);
						break;

					case AudioDataConverters::int32LE:
						AudioDataConverters::convertFloatToInt32LE (source, outputData + 4 * destChan, samplesToDo, 4 * actualNumChannels);
						break;

					case AudioDataConverters::int24LE:
						AudioDataConverters::convertFloatToInt24LE (source, outputData + 3 * destChan, samplesToDo, 3 * actualNumChannels);
						break;

					case AudioDataConverters::int16LE:
						AudioDataConverters::convertFloatToInt16LE (source, outputData + 2 * destChan, samplesToDo, 2 * actualNumChannels);
						break;

					default: jassertfalse; break;
					}
				}

				renderClient->ReleaseBuffer (samplesToDo, 0);

				offset += samplesToDo;
				bufferSize -= samplesToDo;
			}
		}
	}

	ComSmartPtr <IAudioRenderClient> renderClient;

private:
	WASAPIOutputDevice (const WASAPIOutputDevice&);
	WASAPIOutputDevice& operator= (const WASAPIOutputDevice&);
};

class WASAPIAudioIODevice  : public AudioIODevice,
							 public Thread
{
public:
	WASAPIAudioIODevice (const String& deviceName,
						 const String& outputDeviceId_,
						 const String& inputDeviceId_,
						 const bool useExclusiveMode_)
		: AudioIODevice (deviceName, "Windows Audio"),
		  Thread ("Juce WASAPI"),
		  isOpen_ (false),
		  isStarted (false),
		  outputDevice (0),
		  outputDeviceId (outputDeviceId_),
		  inputDevice (0),
		  inputDeviceId (inputDeviceId_),
		  useExclusiveMode (useExclusiveMode_),
		  currentBufferSizeSamples (0),
		  currentSampleRate (0),
		  callback (0)
	{
	}

	~WASAPIAudioIODevice()
	{
		close();

		deleteAndZero (inputDevice);
		deleteAndZero (outputDevice);
	}

	bool initialise()
	{
		double defaultSampleRateIn = 0, defaultSampleRateOut = 0;
		int minBufferSizeIn = 0, defaultBufferSizeIn = 0, minBufferSizeOut = 0, defaultBufferSizeOut = 0;
		latencyIn = latencyOut = 0;
		Array <double> ratesIn, ratesOut;

		if (createDevices())
		{
			jassert (inputDevice != 0 || outputDevice != 0);

			if (inputDevice != 0 && outputDevice != 0)
			{
				defaultSampleRate = jmin (inputDevice->defaultSampleRate, outputDevice->defaultSampleRate);
				minBufferSize = jmin (inputDevice->minBufferSize, outputDevice->minBufferSize);
				defaultBufferSize = jmax (inputDevice->defaultBufferSize, outputDevice->defaultBufferSize);
				sampleRates = inputDevice->rates;
				sampleRates.removeValuesNotIn (outputDevice->rates);
			}
			else
			{
				WASAPIDeviceBase* const d = inputDevice != 0 ? (WASAPIDeviceBase*) inputDevice : (WASAPIDeviceBase*) outputDevice;
				defaultSampleRate = d->defaultSampleRate;
				minBufferSize = d->minBufferSize;
				defaultBufferSize = d->defaultBufferSize;
				sampleRates = d->rates;
			}

			bufferSizes.addUsingDefaultSort (defaultBufferSize);
			if (minBufferSize != defaultBufferSize)
				bufferSizes.addUsingDefaultSort (minBufferSize);

			int n = 64;
			for (int i = 0; i < 40; ++i)
			{
				if (n >= minBufferSize && n <= 2048 && ! bufferSizes.contains (n))
					bufferSizes.addUsingDefaultSort (n);

				n += (n < 512) ? 32 : (n < 1024 ? 64 : 128);
			}

			return true;
		}

		return false;
	}

	const StringArray getOutputChannelNames()
	{
		StringArray outChannels;

		if (outputDevice != 0)
			for (int i = 1; i <= outputDevice->actualNumChannels; ++i)
				outChannels.add ("Output channel " + String (i));

		return outChannels;
	}

	const StringArray getInputChannelNames()
	{
		StringArray inChannels;

		if (inputDevice != 0)
			for (int i = 1; i <= inputDevice->actualNumChannels; ++i)
				inChannels.add ("Input channel " + String (i));

		return inChannels;
	}

	int getNumSampleRates()				 { return sampleRates.size(); }
	double getSampleRate (int index)			{ return sampleRates [index]; }
	int getNumBufferSizesAvailable()			{ return bufferSizes.size(); }
	int getBufferSizeSamples (int index)		{ return bufferSizes [index]; }
	int getDefaultBufferSize()			  { return defaultBufferSize; }

	int getCurrentBufferSizeSamples()		   { return currentBufferSizeSamples; }
	double getCurrentSampleRate()			   { return currentSampleRate; }
	int getCurrentBitDepth()				{ return 32; }
	int getOutputLatencyInSamples()			 { return latencyOut; }
	int getInputLatencyInSamples()			  { return latencyIn; }
	const BigInteger getActiveOutputChannels() const	{ return outputDevice != 0 ? outputDevice->channels : BigInteger(); }
	const BigInteger getActiveInputChannels() const	 { return inputDevice != 0 ? inputDevice->channels : BigInteger(); }
	const String getLastError() { return lastError; }

	const String open (const BigInteger& inputChannels, const BigInteger& outputChannels,
					   double sampleRate, int bufferSizeSamples)
	{
		close();
		lastError = String::empty;

		if (sampleRates.size() == 0 && inputDevice != 0 && outputDevice != 0)
		{
			lastError = "The input and output devices don't share a common sample rate!";
			return lastError;
		}

		currentBufferSizeSamples = bufferSizeSamples <= 0 ? defaultBufferSize : jmax (bufferSizeSamples, minBufferSize);
		currentSampleRate = sampleRate > 0 ? sampleRate : defaultSampleRate;

		if (inputDevice != 0 && ! inputDevice->open (currentSampleRate, inputChannels))
		{
			lastError = "Couldn't open the input device!";
			return lastError;
		}

		if (outputDevice != 0 && ! outputDevice->open (currentSampleRate, outputChannels))
		{
			close();
			lastError = "Couldn't open the output device!";
			return lastError;
		}

		if (inputDevice != 0)
			ResetEvent (inputDevice->clientEvent);
		if (outputDevice != 0)
			ResetEvent (outputDevice->clientEvent);

		startThread (8);
		Thread::sleep (5);

		if (inputDevice != 0 && inputDevice->client != 0)
		{
			latencyIn = inputDevice->latencySamples + inputDevice->actualBufferSize + inputDevice->minBufferSize;
			HRESULT hr = inputDevice->client->Start();
			logFailure (hr); //xxx handle this
		}

		if (outputDevice != 0 && outputDevice->client != 0)
		{
			latencyOut = outputDevice->latencySamples + outputDevice->actualBufferSize + outputDevice->minBufferSize;
			HRESULT hr = outputDevice->client->Start();
			logFailure (hr); //xxx handle this
		}

		isOpen_ = true;
		return lastError;
	}

	void close()
	{
		stop();

		if (inputDevice != 0)
			SetEvent (inputDevice->clientEvent);

		if (outputDevice != 0)
			SetEvent (outputDevice->clientEvent);

		stopThread (5000);

		if (inputDevice != 0)
			inputDevice->close();

		if (outputDevice != 0)
			outputDevice->close();

		isOpen_ = false;
	}

	bool isOpen()	   { return isOpen_ && isThreadRunning(); }
	bool isPlaying()	{ return isStarted && isOpen_ && isThreadRunning(); }

	void start (AudioIODeviceCallback* call)
	{
		if (isOpen_ && call != 0 && ! isStarted)
		{
			if (! isThreadRunning())
			{
				// something's gone wrong and the thread's stopped..
				isOpen_ = false;
				return;
			}

			call->audioDeviceAboutToStart (this);

			const ScopedLock sl (startStopLock);
			callback = call;
			isStarted = true;
		}
	}

	void stop()
	{
		if (isStarted)
		{
			AudioIODeviceCallback* const callbackLocal = callback;

			{
				const ScopedLock sl (startStopLock);
				isStarted = false;
			}

			if (callbackLocal != 0)
				callbackLocal->audioDeviceStopped();
		}
	}

	void setMMThreadPriority()
	{
		DynamicLibraryLoader dll ("avrt.dll");
		DynamicLibraryImport (AvSetMmThreadCharacteristics, avSetMmThreadCharacteristics, HANDLE, dll, (LPCTSTR, LPDWORD))
		DynamicLibraryImport (AvSetMmThreadPriority, avSetMmThreadPriority, HANDLE, dll, (HANDLE, AVRT_PRIORITY))

		if (avSetMmThreadCharacteristics != 0 && avSetMmThreadPriority != 0)
		{
			DWORD dummy = 0;
			HANDLE h = avSetMmThreadCharacteristics (_T("Pro Audio"), &dummy);

			if (h != 0)
				avSetMmThreadPriority (h, AVRT_PRIORITY_NORMAL);
		}
	}

	void run()
	{
		setMMThreadPriority();

		const int bufferSize = currentBufferSizeSamples;

		HANDLE events[2];
		int numEvents = 0;
		if (inputDevice != 0)
			events [numEvents++] = inputDevice->clientEvent;
		if (outputDevice != 0)
			events [numEvents++] = outputDevice->clientEvent;

		const int numInputBuffers = getActiveInputChannels().countNumberOfSetBits();
		const int numOutputBuffers = getActiveOutputChannels().countNumberOfSetBits();

		AudioSampleBuffer ins (jmax (1, numInputBuffers), bufferSize + 32);
		AudioSampleBuffer outs (jmax (1, numOutputBuffers), bufferSize + 32);
		float** const inputBuffers = ins.getArrayOfChannels();
		float** const outputBuffers = outs.getArrayOfChannels();
		ins.clear();

		while (! threadShouldExit())
		{
			const DWORD result = useExclusiveMode ? WaitForSingleObject (inputDevice->clientEvent, 1000)
												  : WaitForMultipleObjects (numEvents, events, true, 1000);
			if (result == WAIT_TIMEOUT)
				continue;

			if (threadShouldExit())
				break;

			if (inputDevice != 0)
				inputDevice->copyBuffers (inputBuffers, numInputBuffers, bufferSize, *this);

			// Make the callback..
			{
				const ScopedLock sl (startStopLock);

				if (isStarted)
				{
					JUCE_TRY
					{
						callback->audioDeviceIOCallback ((const float**) inputBuffers,
														 numInputBuffers,
														 outputBuffers,
														 numOutputBuffers,
														 bufferSize);
					}
					JUCE_CATCH_EXCEPTION
				}
				else
				{
					outs.clear();
				}
			}

			if (useExclusiveMode && WaitForSingleObject (outputDevice->clientEvent, 1000) == WAIT_TIMEOUT)
				continue;

			if (outputDevice != 0)
				outputDevice->copyBuffers ((const float**) outputBuffers, numOutputBuffers, bufferSize, *this);
		}
	}

	juce_UseDebuggingNewOperator

	String outputDeviceId, inputDeviceId;
	String lastError;

private:
	// Device stats...
	WASAPIInputDevice* inputDevice;
	WASAPIOutputDevice* outputDevice;
	const bool useExclusiveMode;
	double defaultSampleRate;
	int minBufferSize, defaultBufferSize;
	int latencyIn, latencyOut;
	Array <double> sampleRates;
	Array <int> bufferSizes;

	// Active state...
	bool isOpen_, isStarted;
	int currentBufferSizeSamples;
	double currentSampleRate;

	AudioIODeviceCallback* callback;
	CriticalSection startStopLock;

	bool createDevices()
	{
		ComSmartPtr <IMMDeviceEnumerator> enumerator;
		if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator), CLSCTX_INPROC_SERVER)))
			return false;

		ComSmartPtr <IMMDeviceCollection> deviceCollection;
		if (! OK (enumerator->EnumAudioEndpoints (eAll, DEVICE_STATE_ACTIVE, &deviceCollection)))
			return false;

		UINT32 numDevices = 0;
		if (! OK (deviceCollection->GetCount (&numDevices)))
			return false;

		for (UINT32 i = 0; i < numDevices; ++i)
		{
			ComSmartPtr <IMMDevice> device;
			if (! OK (deviceCollection->Item (i, &device)))
				continue;

			const String deviceId (wasapi_getDeviceID (device));
			if (deviceId.isEmpty())
				continue;

			const EDataFlow flow = wasapi_getDataFlow (device);

			if (deviceId == inputDeviceId && flow == eCapture)
				inputDevice = new WASAPIInputDevice (device, useExclusiveMode);
			else if (deviceId == outputDeviceId && flow == eRender)
				outputDevice = new WASAPIOutputDevice (device, useExclusiveMode);
		}

		return (outputDeviceId.isEmpty() || (outputDevice != 0 && outputDevice->isOk()))
			&& (inputDeviceId.isEmpty() || (inputDevice != 0 && inputDevice->isOk()));
	}

	WASAPIAudioIODevice (const WASAPIAudioIODevice&);
	WASAPIAudioIODevice& operator= (const WASAPIAudioIODevice&);
};

class WASAPIAudioIODeviceType  : public AudioIODeviceType
{
public:
	WASAPIAudioIODeviceType()
		: AudioIODeviceType ("Windows Audio"),
		  hasScanned (false)
	{
	}

	~WASAPIAudioIODeviceType()
	{
	}

	void scanForDevices()
	{
		hasScanned = true;

		outputDeviceNames.clear();
		inputDeviceNames.clear();
		outputDeviceIds.clear();
		inputDeviceIds.clear();

		ComSmartPtr <IMMDeviceEnumerator> enumerator;
		if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator), CLSCTX_INPROC_SERVER)))
			return;

		const String defaultRenderer = getDefaultEndpoint (enumerator, false);
		const String defaultCapture = getDefaultEndpoint (enumerator, true);

		ComSmartPtr <IMMDeviceCollection> deviceCollection;
		UINT32 numDevices = 0;

		if (! (OK (enumerator->EnumAudioEndpoints (eAll, DEVICE_STATE_ACTIVE, &deviceCollection))
				&& OK (deviceCollection->GetCount (&numDevices))))
			return;

		for (UINT32 i = 0; i < numDevices; ++i)
		{
			ComSmartPtr <IMMDevice> device;
			if (! OK (deviceCollection->Item (i, &device)))
				continue;

			const String deviceId (wasapi_getDeviceID (device));

			DWORD state = 0;
			if (! OK (device->GetState (&state)))
				continue;

			if (state != DEVICE_STATE_ACTIVE)
				continue;

			String name;

			{
				ComSmartPtr <IPropertyStore> properties;
				if (! OK (device->OpenPropertyStore (STGM_READ, &properties)))
					continue;

				PROPVARIANT value;
				PropVariantInit (&value);
				if (OK (properties->GetValue (PKEY_Device_FriendlyName, &value)))
					name = value.pwszVal;

				PropVariantClear (&value);
			}

			const EDataFlow flow = wasapi_getDataFlow (device);

			if (flow == eRender)
			{
				const int index = (deviceId == defaultRenderer) ? 0 : -1;
				outputDeviceIds.insert (index, deviceId);
				outputDeviceNames.insert (index, name);
			}
			else if (flow == eCapture)
			{
				const int index = (deviceId == defaultCapture) ? 0 : -1;
				inputDeviceIds.insert (index, deviceId);
				inputDeviceNames.insert (index, name);
			}
		}

		inputDeviceNames.appendNumbersToDuplicates (false, false);
		outputDeviceNames.appendNumbersToDuplicates (false, false);
	}

	const StringArray getDeviceNames (bool wantInputNames) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		return wantInputNames ? inputDeviceNames
							  : outputDeviceNames;
	}

	int getDefaultDeviceIndex (bool /*forInput*/) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this
		return 0;
	}

	int getIndexOfDevice (AudioIODevice* device, bool asInput) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this
		WASAPIAudioIODevice* const d = dynamic_cast <WASAPIAudioIODevice*> (device);
		return d == 0 ? -1 : (asInput ? inputDeviceIds.indexOf (d->inputDeviceId)
									  : outputDeviceIds.indexOf (d->outputDeviceId));
	}

	bool hasSeparateInputsAndOutputs() const	{ return true; }

	AudioIODevice* createDevice (const String& outputDeviceName,
								 const String& inputDeviceName)
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		const bool useExclusiveMode = false;
		ScopedPointer<WASAPIAudioIODevice> device;

		const int outputIndex = outputDeviceNames.indexOf (outputDeviceName);
		const int inputIndex = inputDeviceNames.indexOf (inputDeviceName);

		if (outputIndex >= 0 || inputIndex >= 0)
		{
			device = new WASAPIAudioIODevice (outputDeviceName.isNotEmpty() ? outputDeviceName
																			: inputDeviceName,
											  outputDeviceIds [outputIndex],
											  inputDeviceIds [inputIndex],
											  useExclusiveMode);

			if (! device->initialise())
				device = 0;
		}

		return device.release();
	}

	juce_UseDebuggingNewOperator

	StringArray outputDeviceNames, outputDeviceIds;
	StringArray inputDeviceNames, inputDeviceIds;

private:
	bool hasScanned;

	static const String getDefaultEndpoint (IMMDeviceEnumerator* const enumerator, const bool forCapture)
	{
		String s;
		IMMDevice* dev = 0;
		if (OK (enumerator->GetDefaultAudioEndpoint (forCapture ? eCapture : eRender,
													 eMultimedia, &dev)))
		{
			WCHAR* deviceId = 0;
			if (OK (dev->GetId (&deviceId)))
			{
				s = String (deviceId);
				CoTaskMemFree (deviceId);
			}

			dev->Release();
		}

		return s;
	}

	WASAPIAudioIODeviceType (const WASAPIAudioIODeviceType&);
	WASAPIAudioIODeviceType& operator= (const WASAPIAudioIODeviceType&);
};

AudioIODeviceType* juce_createAudioIODeviceType_WASAPI()
{
	return new WASAPIAudioIODeviceType();
}

#undef logFailure
#undef OK

#endif
/*** End of inlined file: juce_win32_WASAPI.cpp ***/


/*** Start of inlined file: juce_win32_CameraDevice.cpp ***/
// (This file gets included by juce_win32_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_USE_CAMERA

class DShowCameraDeviceInteral  : public ChangeBroadcaster
{
public:
	DShowCameraDeviceInteral (CameraDevice* const owner_,
							  const ComSmartPtr <ICaptureGraphBuilder2>& captureGraphBuilder_,
							  const ComSmartPtr <IBaseFilter>& filter_,
							  int minWidth, int minHeight,
							  int maxWidth, int maxHeight)
	  : owner (owner_),
		captureGraphBuilder (captureGraphBuilder_),
		filter (filter_),
		ok (false),
		imageNeedsFlipping (false),
		width (0),
		height (0),
		activeUsers (0),
		recordNextFrameTime (false),
		activeImage (0),
		loadingImage (0)
	{
		HRESULT hr = graphBuilder.CoCreateInstance (CLSID_FilterGraph, CLSCTX_INPROC);
		if (FAILED (hr))
			return;

		hr = captureGraphBuilder->SetFiltergraph (graphBuilder);
		if (FAILED (hr))
			return;

		hr = graphBuilder->QueryInterface (IID_IMediaControl, (void**) &mediaControl);
		if (FAILED (hr))
			return;

		{
			ComSmartPtr <IAMStreamConfig> streamConfig;

			hr = captureGraphBuilder->FindInterface (&PIN_CATEGORY_CAPTURE, 0, filter,
													 IID_IAMStreamConfig, (void**) &streamConfig);

			if (streamConfig != 0)
			{
				getVideoSizes (streamConfig);

				if (! selectVideoSize (streamConfig, minWidth, minHeight, maxWidth, maxHeight))
					return;
			}
		}

		hr = graphBuilder->AddFilter (filter, _T("Video Capture"));
		if (FAILED (hr))
			return;

		hr = smartTee.CoCreateInstance (CLSID_SmartTee, CLSCTX_INPROC_SERVER);
		if (FAILED (hr))
			return;

		hr = graphBuilder->AddFilter (smartTee, _T("Smart Tee"));
		if (FAILED (hr))
			return;

		if (! connectFilters (filter, smartTee))
			return;

		ComSmartPtr <IBaseFilter> sampleGrabberBase;
		hr = sampleGrabberBase.CoCreateInstance (CLSID_SampleGrabber, CLSCTX_INPROC_SERVER);
		if (FAILED (hr))
			return;

		hr = sampleGrabberBase->QueryInterface (IID_ISampleGrabber, (void**) &sampleGrabber);
		if (FAILED (hr))
			return;

		AM_MEDIA_TYPE mt;
		zerostruct (mt);
		mt.majortype = MEDIATYPE_Video;
		mt.subtype = MEDIASUBTYPE_RGB24;
		mt.formattype = FORMAT_VideoInfo;
		sampleGrabber->SetMediaType (&mt);

		callback = new GrabberCallback (*this);
		sampleGrabber->SetCallback (callback, 1);

		hr = graphBuilder->AddFilter (sampleGrabberBase, _T("Sample Grabber"));
		if (FAILED (hr))
			return;

		ComSmartPtr <IPin> grabberInputPin;
		if (! (getPin (smartTee, PINDIR_OUTPUT, &smartTeeCaptureOutputPin, "capture")
				&& getPin (smartTee, PINDIR_OUTPUT, &smartTeePreviewOutputPin, "preview")
				&& getPin (sampleGrabberBase, PINDIR_INPUT, &grabberInputPin)))
			return;

		hr = graphBuilder->Connect (smartTeePreviewOutputPin, grabberInputPin);
		if (FAILED (hr))
			return;

		zerostruct (mt);
		hr = sampleGrabber->GetConnectedMediaType (&mt);
		VIDEOINFOHEADER* pVih = (VIDEOINFOHEADER*) (mt.pbFormat);
		width = pVih->bmiHeader.biWidth;
		height = pVih->bmiHeader.biHeight;

		ComSmartPtr <IBaseFilter> nullFilter;
		hr = nullFilter.CoCreateInstance (CLSID_NullRenderer, CLSCTX_INPROC_SERVER);
		hr = graphBuilder->AddFilter (nullFilter, _T("Null Renderer"));

		if (connectFilters (sampleGrabberBase, nullFilter)
			  && addGraphToRot())
		{
			activeImage = new Image (Image::RGB, width, height, true);
			loadingImage = new Image (Image::RGB, width, height, true);

			ok = true;
		}
	}

	~DShowCameraDeviceInteral()
	{
		if (mediaControl != 0)
			mediaControl->Stop();

		removeGraphFromRot();

		for (int i = viewerComps.size(); --i >= 0;)
			((DShowCaptureViewerComp*) viewerComps.getUnchecked(i))->ownerDeleted();

		callback = 0;
		graphBuilder = 0;
		sampleGrabber = 0;
		mediaControl = 0;
		filter = 0;
		captureGraphBuilder = 0;
		smartTee = 0;
		smartTeePreviewOutputPin = 0;
		smartTeeCaptureOutputPin = 0;
		asfWriter = 0;

		delete activeImage;
		delete loadingImage;
	}

	void addUser()
	{
		if (ok && activeUsers++ == 0)
			mediaControl->Run();
	}

	void removeUser()
	{
		if (ok && --activeUsers == 0)
			mediaControl->Stop();
	}

	void handleFrame (double /*time*/, BYTE* buffer, long /*bufferSize*/)
	{
		if (recordNextFrameTime)
		{
			const double defaultCameraLatency = 0.1;

			firstRecordedTime = Time::getCurrentTime() - RelativeTime (defaultCameraLatency);
			recordNextFrameTime = false;

			ComSmartPtr <IPin> pin;
			if (getPin (filter, PINDIR_OUTPUT, &pin))
			{
				ComSmartPtr <IAMPushSource> pushSource;
				HRESULT hr = pin->QueryInterface (IID_IAMPushSource, (void**) &pushSource);

				if (pushSource != 0)
				{
					REFERENCE_TIME latency = 0;
					hr = pushSource->GetLatency (&latency);

					firstRecordedTime = firstRecordedTime - RelativeTime ((double) latency);
				}
			}
		}

		{
			const int lineStride = width * 3;
			const ScopedLock sl (imageSwapLock);

			{
				const Image::BitmapData destData (*loadingImage, 0, 0, width, height, true);

				for (int i = 0; i < height; ++i)
					memcpy (destData.getLinePointer ((height - 1) - i),
							buffer + lineStride * i,
							lineStride);
			}

			imageNeedsFlipping = true;
		}

		if (listeners.size() > 0)
			callListeners (*loadingImage);

		sendChangeMessage (this);
	}

	void drawCurrentImage (Graphics& g, int x, int y, int w, int h)
	{
		if (imageNeedsFlipping)
		{
			const ScopedLock sl (imageSwapLock);
			swapVariables (loadingImage, activeImage);
			imageNeedsFlipping = false;
		}

		RectanglePlacement rp (RectanglePlacement::centred);
		double dx = 0, dy = 0, dw = width, dh = height;
		rp.applyTo (dx, dy, dw, dh, x, y, w, h);
		const int rx = roundToInt (dx), ry = roundToInt (dy);
		const int rw = roundToInt (dw), rh = roundToInt (dh);

		g.saveState();
		g.excludeClipRegion (Rectangle<int> (rx, ry, rw, rh));
		g.fillAll (Colours::black);
		g.restoreState();

		g.drawImage (activeImage, rx, ry, rw, rh, 0, 0, width, height);
	}

	bool createFileCaptureFilter (const File& file)
	{
		removeFileCaptureFilter();
		file.deleteFile();
		mediaControl->Stop();
		firstRecordedTime = Time();
		recordNextFrameTime = true;

		HRESULT hr = asfWriter.CoCreateInstance (CLSID_WMAsfWriter, CLSCTX_INPROC_SERVER);

		if (SUCCEEDED (hr))
		{
			ComSmartPtr <IFileSinkFilter> fileSink;
			hr = asfWriter->QueryInterface (IID_IFileSinkFilter, (void**) &fileSink);

			if (SUCCEEDED (hr))
			{
				hr = fileSink->SetFileName (file.getFullPathName(), 0);

				if (SUCCEEDED (hr))
				{
					hr = graphBuilder->AddFilter (asfWriter, _T("AsfWriter"));

					if (SUCCEEDED (hr))
					{
						ComSmartPtr <IConfigAsfWriter> asfConfig;
						hr = asfWriter->QueryInterface (IID_IConfigAsfWriter, (void**) &asfConfig);
						asfConfig->SetIndexMode (true);
						ComSmartPtr <IWMProfileManager> profileManager;
						hr = WMCreateProfileManager (&profileManager);

						// This gibberish is the DirectShow profile for a video-only wmv file.
						String prof ("<profile version=\"589824\" storageformat=\"1\" name=\"Quality\" description=\"Quality type for output.\"><streamconfig "
							"majortype=\"{73646976-0000-0010-8000-00AA00389B71}\" streamnumber=\"1\" streamname=\"Video Stream\" inputname=\"Video409\" bitrate=\"894960\" "
							"bufferwindow=\"0\" reliabletransport=\"1\" decodercomplexity=\"AU\" rfc1766langid=\"en-us\"><videomediaprops maxkeyframespacing=\"50000000\" quality=\"90\"/>"
							"<wmmediatype subtype=\"{33564D57-0000-0010-8000-00AA00389B71}\" bfixedsizesamples=\"0\" btemporalcompression=\"1\" lsamplesize=\"0\"> <videoinfoheader "
							"dwbitrate=\"894960\" dwbiterrorrate=\"0\" avgtimeperframe=\"100000\"><rcsource left=\"0\" top=\"0\" right=\"$WIDTH\" bottom=\"$HEIGHT\"/> <rctarget "
							"left=\"0\" top=\"0\" right=\"$WIDTH\" bottom=\"$HEIGHT\"/> <bitmapinfoheader biwidth=\"$WIDTH\" biheight=\"$HEIGHT\" biplanes=\"1\" bibitcount=\"24\" "
							"bicompression=\"WMV3\" bisizeimage=\"0\" bixpelspermeter=\"0\" biypelspermeter=\"0\" biclrused=\"0\" biclrimportant=\"0\"/> "
							"</videoinfoheader></wmmediatype></streamconfig></profile>");

						prof = prof.replace ("$WIDTH", String (width))
								   .replace ("$HEIGHT", String (height));

						ComSmartPtr <IWMProfile> currentProfile;
						hr = profileManager->LoadProfileByData ((const WCHAR*) prof, &currentProfile);
						hr = asfConfig->ConfigureFilterUsingProfile (currentProfile);

						if (SUCCEEDED (hr))
						{
							ComSmartPtr <IPin> asfWriterInputPin;

							if (getPin (asfWriter, PINDIR_INPUT, &asfWriterInputPin, "Video Input 01"))
							{
								hr = graphBuilder->Connect (smartTeeCaptureOutputPin, asfWriterInputPin);

								if (SUCCEEDED (hr)
									 && ok && activeUsers > 0
									 && SUCCEEDED (mediaControl->Run()))
								{
									return true;
								}
							}
						}
					}
				}
			}
		}

		removeFileCaptureFilter();

		if (ok && activeUsers > 0)
			mediaControl->Run();

		return false;
	}

	void removeFileCaptureFilter()
	{
		mediaControl->Stop();

		if (asfWriter != 0)
		{
			graphBuilder->RemoveFilter (asfWriter);
			asfWriter = 0;
		}

		if (ok && activeUsers > 0)
			mediaControl->Run();
	}

	void addListener (CameraImageListener* listenerToAdd)
	{
		const ScopedLock sl (listenerLock);

		if (listeners.size() == 0)
			addUser();

		listeners.addIfNotAlreadyThere (listenerToAdd);
	}

	void removeListener (CameraImageListener* listenerToRemove)
	{
		const ScopedLock sl (listenerLock);
		listeners.removeValue (listenerToRemove);

		if (listeners.size() == 0)
			removeUser();
	}

	void callListeners (Image& image)
	{
		const ScopedLock sl (listenerLock);

		for (int i = listeners.size(); --i >= 0;)
		{
			CameraImageListener* l = (CameraImageListener*) listeners[i];

			if (l != 0)
				l->imageReceived (image);
		}
	}

	class DShowCaptureViewerComp   : public Component,
									 public ChangeListener
	{
	public:
		DShowCaptureViewerComp (DShowCameraDeviceInteral* const owner_)
			: owner (owner_)
		{
			setOpaque (true);
			owner->addChangeListener (this);
			owner->addUser();
			owner->viewerComps.add (this);
			setSize (owner_->width, owner_->height);
		}

		~DShowCaptureViewerComp()
		{
			if (owner != 0)
			{
				owner->viewerComps.removeValue (this);
				owner->removeUser();
				owner->removeChangeListener (this);
			}
		}

		void ownerDeleted()
		{
			owner = 0;
		}

		void paint (Graphics& g)
		{
			g.setColour (Colours::black);
			g.setImageResamplingQuality (Graphics::lowResamplingQuality);

			if (owner != 0)
				owner->drawCurrentImage (g, 0, 0, getWidth(), getHeight());
			else
				g.fillAll (Colours::black);
		}

		void changeListenerCallback (void*)
		{
			repaint();
		}

	private:
		DShowCameraDeviceInteral* owner;
	};

	bool ok;
	int width, height;
	Time firstRecordedTime;

	VoidArray viewerComps;

private:
	CameraDevice* const owner;
	ComSmartPtr <ICaptureGraphBuilder2> captureGraphBuilder;
	ComSmartPtr <IBaseFilter> filter;
	ComSmartPtr <IBaseFilter> smartTee;
	ComSmartPtr <IGraphBuilder> graphBuilder;
	ComSmartPtr <ISampleGrabber> sampleGrabber;
	ComSmartPtr <IMediaControl> mediaControl;
	ComSmartPtr <IPin> smartTeePreviewOutputPin;
	ComSmartPtr <IPin> smartTeeCaptureOutputPin;
	ComSmartPtr <IBaseFilter> asfWriter;
	int activeUsers;
	Array <int> widths, heights;
	DWORD graphRegistrationID;

	CriticalSection imageSwapLock;
	bool imageNeedsFlipping;
	Image* loadingImage;
	Image* activeImage;

	bool recordNextFrameTime;

	void getVideoSizes (IAMStreamConfig* const streamConfig)
	{
		widths.clear();
		heights.clear();

		int count = 0, size = 0;
		streamConfig->GetNumberOfCapabilities (&count, &size);

		if (size == sizeof (VIDEO_STREAM_CONFIG_CAPS))
		{
			for (int i = 0; i < count; ++i)
			{
				VIDEO_STREAM_CONFIG_CAPS scc;
				AM_MEDIA_TYPE* config;

				HRESULT hr = streamConfig->GetStreamCaps (i, &config, (BYTE*) &scc);

				if (SUCCEEDED (hr))
				{
					const int w = scc.InputSize.cx;
					const int h = scc.InputSize.cy;

					bool duplicate = false;

					for (int j = widths.size(); --j >= 0;)
					{
						if (w == widths.getUnchecked (j) && h == heights.getUnchecked (j))
						{
							duplicate = true;
							break;
						}
					}

					if (! duplicate)
					{
						DBG ("Camera capture size: " + String (w) + ", " + String (h));
						widths.add (w);
						heights.add (h);
					}

					deleteMediaType (config);
				}
			}
		}
	}

	bool selectVideoSize (IAMStreamConfig* const streamConfig,
						  const int minWidth, const int minHeight,
						  const int maxWidth, const int maxHeight)
	{
		int count = 0, size = 0, bestArea = 0, bestIndex = -1;
		streamConfig->GetNumberOfCapabilities (&count, &size);

		if (size == sizeof (VIDEO_STREAM_CONFIG_CAPS))
		{
			AM_MEDIA_TYPE* config;
			VIDEO_STREAM_CONFIG_CAPS scc;

			for (int i = 0; i < count; ++i)
			{
				HRESULT hr = streamConfig->GetStreamCaps (i, &config, (BYTE*) &scc);

				if (SUCCEEDED (hr))
				{
					if (scc.InputSize.cx >= minWidth
						 && scc.InputSize.cy >= minHeight
						 && scc.InputSize.cx <= maxWidth
						 && scc.InputSize.cy <= maxHeight)
					{
						int area = scc.InputSize.cx * scc.InputSize.cy;
						if (area > bestArea)
						{
							bestIndex = i;
							bestArea = area;
						}
					}

					deleteMediaType (config);
				}
			}

			if (bestIndex >= 0)
			{
				HRESULT hr = streamConfig->GetStreamCaps (bestIndex, &config, (BYTE*) &scc);

				hr = streamConfig->SetFormat (config);
				deleteMediaType (config);
				return SUCCEEDED (hr);
			}
		}

		return false;
	}

	static bool getPin (IBaseFilter* filter, const PIN_DIRECTION wantedDirection, IPin** result, const char* pinName = 0)
	{
		ComSmartPtr <IEnumPins> enumerator;
		ComSmartPtr <IPin> pin;

		filter->EnumPins (&enumerator);

		while (enumerator->Next (1, &pin, 0) == S_OK)
		{
			PIN_DIRECTION dir;
			pin->QueryDirection (&dir);

			if (wantedDirection == dir)
			{
				PIN_INFO info;
				zerostruct (info);
				pin->QueryPinInfo (&info);

				if (pinName == 0 || String (pinName).equalsIgnoreCase (String (info.achName)))
				{
					pin.p->AddRef();
					*result = pin;
					return true;
				}
			}
		}

		return false;
	}

	bool connectFilters (IBaseFilter* const first, IBaseFilter* const second) const
	{
		ComSmartPtr <IPin> in, out;

		return getPin (first, PINDIR_OUTPUT, &out)
				&& getPin (second, PINDIR_INPUT, &in)
				&& SUCCEEDED (graphBuilder->Connect (out, in));
	}

	bool addGraphToRot()
	{
		ComSmartPtr <IRunningObjectTable> rot;
		if (FAILED (GetRunningObjectTable (0, &rot)))
			return false;

		ComSmartPtr <IMoniker> moniker;
		WCHAR buffer[128];
		HRESULT hr = CreateItemMoniker (_T("!"), buffer, &moniker);
		if (FAILED (hr))
			return false;

		graphRegistrationID = 0;
		return SUCCEEDED (rot->Register (0, graphBuilder, moniker, &graphRegistrationID));
	}

	void removeGraphFromRot()
	{
		ComSmartPtr <IRunningObjectTable> rot;

		if (SUCCEEDED (GetRunningObjectTable (0, &rot)))
			rot->Revoke (graphRegistrationID);
	}

	static void deleteMediaType (AM_MEDIA_TYPE* const pmt)
	{
		if (pmt->cbFormat != 0)
			CoTaskMemFree ((PVOID) pmt->pbFormat);

		if (pmt->pUnk != 0)
			pmt->pUnk->Release();

		CoTaskMemFree (pmt);
	}

	class GrabberCallback   : public ISampleGrabberCB
	{
	public:
		GrabberCallback (DShowCameraDeviceInteral& owner_)
			: owner (owner_)
		{
		}

		HRESULT __stdcall QueryInterface (REFIID id, void** result)
		{
			if (id == IID_IUnknown)
				*result = dynamic_cast <IUnknown*> (this);
			else if (id == IID_ISampleGrabberCB)
				*result = dynamic_cast <ISampleGrabberCB*> (this);
			else
			{
				*result = 0;
				return E_NOINTERFACE;
			}

			AddRef();
			return S_OK;
		}

		ULONG __stdcall AddRef()	{ return ++refCount; }
		ULONG __stdcall Release()   { const int r = --refCount; if (r == 0) delete this; return r; }

		STDMETHODIMP SampleCB (double /*SampleTime*/, IMediaSample* /*pSample*/)
		{
			return E_FAIL;
		}

		STDMETHODIMP BufferCB (double time, BYTE* buffer, long bufferSize)
		{
			owner.handleFrame (time, buffer, bufferSize);
			return S_OK;
		}

	private:
		int refCount;
		DShowCameraDeviceInteral& owner;

		GrabberCallback (const GrabberCallback&);
		GrabberCallback& operator= (const GrabberCallback&);
	};

	ComSmartPtr <GrabberCallback> callback;
	VoidArray listeners;
	CriticalSection listenerLock;

	DShowCameraDeviceInteral (const DShowCameraDeviceInteral&);
	DShowCameraDeviceInteral& operator= (const DShowCameraDeviceInteral&);
};

CameraDevice::CameraDevice (const String& name_, int /*index*/)
	: name (name_)
{
	isRecording = false;
}

CameraDevice::~CameraDevice()
{
	stopRecording();
	delete (DShowCameraDeviceInteral*) internal;
	internal = 0;
}

Component* CameraDevice::createViewerComponent()
{
	return new DShowCameraDeviceInteral::DShowCaptureViewerComp ((DShowCameraDeviceInteral*) internal);
}

const String CameraDevice::getFileExtension()
{
	return ".wmv";
}

void CameraDevice::startRecordingToFile (const File& file, int quality)
{
	stopRecording();

	DShowCameraDeviceInteral* const d = (DShowCameraDeviceInteral*) internal;
	d->addUser();
	isRecording = d->createFileCaptureFilter (file);
}

const Time CameraDevice::getTimeOfFirstRecordedFrame() const
{
	DShowCameraDeviceInteral* const d = (DShowCameraDeviceInteral*) internal;
	return d->firstRecordedTime;
}

void CameraDevice::stopRecording()
{
	if (isRecording)
	{
		DShowCameraDeviceInteral* const d = (DShowCameraDeviceInteral*) internal;
		d->removeFileCaptureFilter();
		d->removeUser();
		isRecording = false;
	}
}

void CameraDevice::addListener (CameraImageListener* listenerToAdd)
{
	DShowCameraDeviceInteral* const d = (DShowCameraDeviceInteral*) internal;

	if (listenerToAdd != 0)
		d->addListener (listenerToAdd);
}

void CameraDevice::removeListener (CameraImageListener* listenerToRemove)
{
	DShowCameraDeviceInteral* const d = (DShowCameraDeviceInteral*) internal;

	if (listenerToRemove != 0)
		d->removeListener (listenerToRemove);
}

static ComSmartPtr <IBaseFilter> enumerateCameras (StringArray* const names,
												   const int deviceIndexToOpen,
												   String& name)
{
	int index = 0;
	ComSmartPtr <IBaseFilter> result;

	ComSmartPtr <ICreateDevEnum> pDevEnum;
	HRESULT hr = pDevEnum.CoCreateInstance (CLSID_SystemDeviceEnum, CLSCTX_INPROC);

	if (SUCCEEDED (hr))
	{
		ComSmartPtr <IEnumMoniker> enumerator;
		hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &enumerator, 0);

		if (SUCCEEDED (hr) && enumerator != 0)
		{
			ComSmartPtr <IBaseFilter> captureFilter;
			ComSmartPtr <IMoniker> moniker;
			ULONG fetched;

			while (enumerator->Next (1, &moniker, &fetched) == S_OK)
			{
				hr = moniker->BindToObject (0, 0, IID_IBaseFilter, (void**) &captureFilter);

				if (SUCCEEDED (hr))
				{
					ComSmartPtr <IPropertyBag> propertyBag;
					hr = moniker->BindToStorage (0, 0, IID_IPropertyBag, (void**) &propertyBag);

					if (SUCCEEDED (hr))
					{
						VARIANT var;
						var.vt = VT_BSTR;

						hr = propertyBag->Read (_T("FriendlyName"), &var, 0);
						propertyBag = 0;

						if (SUCCEEDED (hr))
						{
							if (names != 0)
								names->add (var.bstrVal);

							if (index == deviceIndexToOpen)
							{
								name = var.bstrVal;
								result = captureFilter;
								captureFilter = 0;
								break;
							}

							++index;
						}

						moniker = 0;
					}

					captureFilter = 0;
				}
			}
		}
	}

	return result;
}

const StringArray CameraDevice::getAvailableDevices()
{
	StringArray devs;
	String dummy;
	enumerateCameras (&devs, -1, dummy);
	return devs;
}

CameraDevice* CameraDevice::openDevice (int index,
										int minWidth, int minHeight,
										int maxWidth, int maxHeight)
{
	ComSmartPtr <ICaptureGraphBuilder2> captureGraphBuilder;
	HRESULT hr = captureGraphBuilder.CoCreateInstance (CLSID_CaptureGraphBuilder2, CLSCTX_INPROC);

	if (SUCCEEDED (hr))
	{
		String name;
		const ComSmartPtr <IBaseFilter> filter (enumerateCameras (0, index, name));

		if (filter != 0)
		{
			CameraDevice* const cam = new CameraDevice (name, index);
			DShowCameraDeviceInteral* const intern
				= new DShowCameraDeviceInteral (cam, captureGraphBuilder, filter,
												minWidth, minHeight, maxWidth, maxHeight);
			cam->internal = intern;

			if (intern->ok)
				return cam;
			else
				delete cam;
		}
	}

	return 0;
}

#endif
/*** End of inlined file: juce_win32_CameraDevice.cpp ***/

#endif

// Auto-link the other win32 libs that are needed by library calls..
#if (JUCE_AMALGAMATED_TEMPLATE || defined (JUCE_DLL_BUILD)) && JUCE_MSVC && ! DONT_AUTOLINK_TO_WIN32_LIBRARIES

/*** Start of inlined file: juce_win32_AutoLinkLibraries.h ***/
// Auto-links to various win32 libs that are needed by library calls..
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "vfw32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "comsupp.lib")
#pragma comment(lib, "version.lib")

#if JUCE_OPENGL
 #pragma comment(lib, "OpenGL32.Lib")
 #pragma comment(lib, "GlU32.Lib")
#endif

#if JUCE_QUICKTIME
 #pragma comment (lib, "QTMLClient.lib")
#endif

#if JUCE_USE_CAMERA
 #pragma comment (lib, "Strmiids.lib")
 #pragma comment (lib, "wmvcore.lib")
#endif
/*** End of inlined file: juce_win32_AutoLinkLibraries.h ***/


#endif

END_JUCE_NAMESPACE

#endif
/*** End of inlined file: juce_win32_NativeCode.cpp ***/


 #endif

 #if JUCE_LINUX

/*** Start of inlined file: juce_linux_NativeCode.cpp ***/
#if JUCE_LINUX

BEGIN_JUCE_NAMESPACE

#if defined (CPU_ISSET) && ! defined (SUPPORT_AFFINITIES)
  #define SUPPORT_AFFINITIES 1
#endif

#define JUCE_INCLUDED_FILE 1

// Now include the actual code files..


/*** Start of inlined file: juce_posix_SharedCode.h ***/
CriticalSection::CriticalSection() throw()
{
	pthread_mutexattr_t atts;
	pthread_mutexattr_init (&atts);
	pthread_mutexattr_settype (&atts, PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init (&internal, &atts);
}

CriticalSection::~CriticalSection() throw()
{
	pthread_mutex_destroy (&internal);
}

void CriticalSection::enter() const throw()
{
	pthread_mutex_lock (&internal);
}

bool CriticalSection::tryEnter() const throw()
{
	return pthread_mutex_trylock (&internal) == 0;
}

void CriticalSection::exit() const throw()
{
	pthread_mutex_unlock (&internal);
}

class WaitableEventImpl
{
public:
	WaitableEventImpl (const bool manualReset_)
		: triggered (false),
		  manualReset (manualReset_)
	{
		pthread_cond_init (&condition, 0);
		pthread_mutex_init (&mutex, 0);
	}

	~WaitableEventImpl()
	{
		pthread_cond_destroy (&condition);
		pthread_mutex_destroy (&mutex);
	}

	bool wait (const int timeOutMillisecs) throw()
	{
		pthread_mutex_lock (&mutex);

		if (! triggered)
		{
			if (timeOutMillisecs < 0)
			{
				do
				{
					pthread_cond_wait (&condition, &mutex);
				}
				while (! triggered);
			}
			else
			{
				struct timeval now;
				gettimeofday (&now, 0);

				struct timespec time;
				time.tv_sec  = now.tv_sec  + (timeOutMillisecs / 1000);
				time.tv_nsec = (now.tv_usec + ((timeOutMillisecs % 1000) * 1000)) * 1000;

				if (time.tv_nsec >= 1000000000)
				{
					time.tv_nsec -= 1000000000;
					time.tv_sec++;
				}

				do
				{
					if (pthread_cond_timedwait (&condition, &mutex, &time) == ETIMEDOUT)
					{
						pthread_mutex_unlock (&mutex);
						return false;
					}
				}
				while (! triggered);
			}
		}

		if (! manualReset)
			triggered = false;

		pthread_mutex_unlock (&mutex);
		return true;
	}

	void signal() throw()
	{
		pthread_mutex_lock (&mutex);
		triggered = true;
		pthread_cond_broadcast (&condition);
		pthread_mutex_unlock (&mutex);
	}

	void reset() throw()
	{
		pthread_mutex_lock (&mutex);
		triggered = false;
		pthread_mutex_unlock (&mutex);
	}

private:
	pthread_cond_t condition;
	pthread_mutex_t mutex;
	bool triggered;
	const bool manualReset;

	WaitableEventImpl (const WaitableEventImpl&);
	WaitableEventImpl& operator= (const WaitableEventImpl&);
};

WaitableEvent::WaitableEvent (const bool manualReset) throw()
	: internal (new WaitableEventImpl (manualReset))
{
}

WaitableEvent::~WaitableEvent() throw()
{
	delete static_cast <WaitableEventImpl*> (internal);
}

bool WaitableEvent::wait (const int timeOutMillisecs) const throw()
{
	return static_cast <WaitableEventImpl*> (internal)->wait (timeOutMillisecs);
}

void WaitableEvent::signal() const throw()
{
	static_cast <WaitableEventImpl*> (internal)->signal();
}

void WaitableEvent::reset() const throw()
{
	static_cast <WaitableEventImpl*> (internal)->reset();
}

void JUCE_CALLTYPE Thread::sleep (int millisecs)
{
	struct timespec time;
	time.tv_sec = millisecs / 1000;
	time.tv_nsec = (millisecs % 1000) * 1000000;
	nanosleep (&time, 0);
}

const juce_wchar File::separator = '/';
const juce_wchar* File::separatorString = L"/";

const File File::getCurrentWorkingDirectory()
{
	HeapBlock<char> heapBuffer;

	char localBuffer [1024];
	char* cwd = getcwd (localBuffer, sizeof (localBuffer) - 1);
	int bufferSize = 4096;

	while (cwd == 0 && errno == ERANGE)
	{
		heapBuffer.malloc (bufferSize);
		cwd = getcwd (heapBuffer, bufferSize - 1);
		bufferSize += 1024;
	}

	return File (String::fromUTF8 (cwd));
}

bool File::setAsCurrentWorkingDirectory() const
{
	return chdir (getFullPathName().toUTF8()) == 0;
}

bool juce_copyFile (const String& s, const String& d);

static bool juce_stat (const String& fileName, struct stat& info)
{
	return fileName.isNotEmpty()
			&& (stat (fileName.toUTF8(), &info) == 0);
}

bool juce_isDirectory (const String& fileName)
{
	struct stat info;

	return fileName.isEmpty()
			|| (juce_stat (fileName, info)
				  && ((info.st_mode & S_IFDIR) != 0));
}

bool juce_fileExists (const String& fileName, const bool dontCountDirectories)
{
	if (fileName.isEmpty())
		return false;

	const char* const fileNameUTF8 = fileName.toUTF8();
	bool exists = access (fileNameUTF8, F_OK) == 0;

	if (exists && dontCountDirectories)
	{
		struct stat info;
		const int res = stat (fileNameUTF8, &info);

		if (res == 0 && (info.st_mode & S_IFDIR) != 0)
			exists = false;
	}

	return exists;
}

int64 juce_getFileSize (const String& fileName)
{
	struct stat info;
	return juce_stat (fileName, info) ? info.st_size : 0;
}

bool juce_canWriteToFile (const String& fileName)
{
	return access (fileName.toUTF8(), W_OK) == 0;
}

bool juce_deleteFile (const String& fileName)
{
	if (juce_isDirectory (fileName))
		return rmdir (fileName.toUTF8()) == 0;
	else
		return remove (fileName.toUTF8()) == 0;
}

bool juce_moveFile (const String& source, const String& dest)
{
	if (rename (source.toUTF8(), dest.toUTF8()) == 0)
		return true;

	if (juce_canWriteToFile (source)
		 && juce_copyFile (source, dest))
	{
		if (juce_deleteFile (source))
			return true;

		juce_deleteFile (dest);
	}

	return false;
}

void juce_createDirectory (const String& fileName)
{
	mkdir (fileName.toUTF8(), 0777);
}

void* juce_fileOpen (const String& fileName, bool forWriting)
{
	int flags = O_RDONLY;

	if (forWriting)
	{
		if (juce_fileExists (fileName, false))
		{
			const int f = open (fileName.toUTF8(), O_RDWR, 00644);

			if (f != -1)
				lseek (f, 0, SEEK_END);

			return (void*) f;
		}
		else
		{
			flags = O_RDWR + O_CREAT;
		}
	}

	return (void*) open (fileName.toUTF8(), flags, 00644);
}

void juce_fileClose (void* handle)
{
	if (handle != 0)
		close ((int) (pointer_sized_int) handle);
}

int juce_fileRead (void* handle, void* buffer, int size)
{
	if (handle != 0)
		return jmax (0, (int) read ((int) (pointer_sized_int) handle, buffer, size));

	return 0;
}

int juce_fileWrite (void* handle, const void* buffer, int size)
{
	if (handle != 0)
		return (int) write ((int) (pointer_sized_int) handle, buffer, size);

	return 0;
}

int64 juce_fileSetPosition (void* handle, int64 pos)
{
	if (handle != 0 && lseek ((int) (pointer_sized_int) handle, pos, SEEK_SET) == pos)
		return pos;

	return -1;
}

int64 juce_fileGetPosition (void* handle)
{
	if (handle != 0)
		return lseek ((int) (pointer_sized_int) handle, 0, SEEK_CUR);

	return -1;
}

void juce_fileFlush (void* handle)
{
	if (handle != 0)
		fsync ((int) (pointer_sized_int) handle);
}

const File juce_getExecutableFile()
{
	Dl_info exeInfo;
	dladdr ((const void*) juce_getExecutableFile, &exeInfo);
	return File::getCurrentWorkingDirectory().getChildFile (String::fromUTF8 (exeInfo.dli_fname));
}

// if this file doesn't exist, find a parent of it that does..
static bool doStatFS (const File* file, struct statfs& result)
{
	File f (*file);

	for (int i = 5; --i >= 0;)
	{
		if (f.exists())
			break;

		f = f.getParentDirectory();
	}

	return statfs (f.getFullPathName().toUTF8(), &result) == 0;
}

int64 File::getBytesFreeOnVolume() const
{
	struct statfs buf;
	if (doStatFS (this, buf))
		return (int64) buf.f_bsize * (int64) buf.f_bavail; // Note: this returns space available to non-super user

	return 0;
}

int64 File::getVolumeTotalSize() const
{
	struct statfs buf;
	if (doStatFS (this, buf))
		return (int64) buf.f_bsize * (int64) buf.f_blocks;

	return 0;
}

const String juce_getVolumeLabel (const String& filenameOnVolume,
								  int& volumeSerialNumber)
{
	volumeSerialNumber = 0;

#if JUCE_MAC
	struct VolAttrBuf
	{
		u_int32_t	   length;
		attrreference_t mountPointRef;
		char		mountPointSpace [MAXPATHLEN];
	} attrBuf;

	struct attrlist attrList;
	zerostruct (attrList);
	attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
	attrList.volattr = ATTR_VOL_INFO | ATTR_VOL_NAME;

	File f (filenameOnVolume);

	for (;;)
	{
		if (getattrlist (f.getFullPathName().toUTF8(),
						 &attrList, &attrBuf, sizeof(attrBuf), 0) == 0)
		{
			return String::fromUTF8 (((const char*) &attrBuf.mountPointRef) + attrBuf.mountPointRef.attr_dataoffset,
									 (int) attrBuf.mountPointRef.attr_length);
		}

		const File parent (f.getParentDirectory());

		if (f == parent)
			break;

		f = parent;
	}
#endif

	return String::empty;
}

void juce_runSystemCommand (const String& command)
{
	int result = system (command.toUTF8());
	(void) result;
}

const String juce_getOutputFromCommand (const String& command)
{
	// slight bodge here, as we just pipe the output into a temp file and read it...
	const File tempFile (File::getSpecialLocation (File::tempDirectory)
						   .getNonexistentChildFile (String::toHexString (Random::getSystemRandom().nextInt()), ".tmp", false));

	juce_runSystemCommand (command + " > " + tempFile.getFullPathName());

	String result (tempFile.loadFileAsString());
	tempFile.deleteFile();
	return result;
}

class InterProcessLock::Pimpl
{
public:
	Pimpl (const String& name, const int timeOutMillisecs)
		: handle (0), refCount (1)
	{
#if JUCE_MAC
		// (don't use getSpecialLocation() to avoid the temp folder being different for each app)
		const File temp (File ("~/Library/Caches/Juce").getChildFile (name));
#else
		const File temp (File::getSpecialLocation (File::tempDirectory).getChildFile (name));
#endif
		temp.create();
		handle = open (temp.getFullPathName().toUTF8(), O_RDWR);

		if (handle != 0)
		{
			struct flock fl;
			zerostruct (fl);
			fl.l_whence = SEEK_SET;
			fl.l_type = F_WRLCK;

			const int64 endTime = Time::currentTimeMillis() + timeOutMillisecs;

			for (;;)
			{
				const int result = fcntl (handle, F_SETLK, &fl);

				if (result >= 0)
					return;

				if (errno != EINTR)
				{
					if (timeOutMillisecs == 0
						 || (timeOutMillisecs > 0 && Time::currentTimeMillis() >= endTime))
						break;

					Thread::sleep (10);
				}
			}
		}

		closeFile();
	}

	~Pimpl()
	{
		closeFile();
	}

	void closeFile()
	{
		if (handle != 0)
		{
			struct flock fl;
			zerostruct (fl);
			fl.l_whence = SEEK_SET;
			fl.l_type = F_UNLCK;

			while (! (fcntl (handle, F_SETLKW, &fl) >= 0 || errno != EINTR))
			{}

			close (handle);
			handle = 0;
		}
	}

	int handle, refCount;
};

InterProcessLock::InterProcessLock (const String& name_)
	: name (name_)
{
}

InterProcessLock::~InterProcessLock()
{
}

bool InterProcessLock::enter (const int timeOutMillisecs)
{
	const ScopedLock sl (lock);

	if (pimpl == 0)
	{
		pimpl = new Pimpl (name, timeOutMillisecs);

		if (pimpl->handle == 0)
			pimpl = 0;
	}
	else
	{
		pimpl->refCount++;
	}

	return pimpl != 0;
}

void InterProcessLock::exit()
{
	const ScopedLock sl (lock);

	// Trying to release the lock too many times!
	jassert (pimpl != 0);

	if (pimpl != 0 && --(pimpl->refCount) == 0)
		pimpl = 0;
}
/*** End of inlined file: juce_posix_SharedCode.h ***/


/*** Start of inlined file: juce_linux_Files.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

static const short U_ISOFS_SUPER_MAGIC = 0x9660;   // linux/iso_fs.h
static const short U_MSDOS_SUPER_MAGIC = 0x4d44;   // linux/msdos_fs.h
static const short U_NFS_SUPER_MAGIC = 0x6969;	 // linux/nfs_fs.h
static const short U_SMB_SUPER_MAGIC = 0x517B;	 // linux/smb_fs.h

void juce_getFileTimes (const String& fileName,
						int64& modificationTime,
						int64& accessTime,
						int64& creationTime)
{
	modificationTime = 0;
	accessTime = 0;
	creationTime = 0;

	struct stat info;
	const int res = stat (fileName.toUTF8(), &info);
	if (res == 0)
	{
		modificationTime = (int64) info.st_mtime * 1000;
		accessTime = (int64) info.st_atime * 1000;
		creationTime = (int64) info.st_ctime * 1000;
	}
}

bool juce_setFileTimes (const String& fileName,
						int64 modificationTime,
						int64 accessTime,
						int64 creationTime)
{
	struct utimbuf times;
	times.actime = (time_t) (accessTime / 1000);
	times.modtime = (time_t) (modificationTime / 1000);

	return utime (fileName.toUTF8(), &times) == 0;
}

bool juce_setFileReadOnly (const String& fileName, bool isReadOnly)
{
	struct stat info;
	const int res = stat (fileName.toUTF8(), &info);
	if (res != 0)
		return false;

	info.st_mode &= 0777;   // Just permissions

	if( isReadOnly )
		info.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
	else
		// Give everybody write permission?
		info.st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;

	return chmod (fileName.toUTF8(), info.st_mode) == 0;
}

bool juce_copyFile (const String& s, const String& d)
{
	const File source (s), dest (d);

	FileInputStream* in = source.createInputStream();
	bool ok = false;

	if (in != 0)
	{
		if (dest.deleteFile())
		{
			FileOutputStream* const out = dest.createOutputStream();

			if (out != 0)
			{
				const int bytesCopied = out->writeFromInputStream (*in, -1);
				delete out;

				ok = (bytesCopied == source.getSize());

				if (! ok)
					dest.deleteFile();
			}
		}

		delete in;
	}

	return ok;
}

const StringArray juce_getFileSystemRoots()
{
	StringArray s;
	s.add ("/");
	return s;
}

bool File::isOnCDRomDrive() const
{
	struct statfs buf;

	if (statfs (getFullPathName().toUTF8(), &buf) == 0)
		return (buf.f_type == U_ISOFS_SUPER_MAGIC);

	// Assume not if this fails for some reason
	return false;
}

bool File::isOnHardDisk() const
{
	struct statfs buf;

	if (statfs (getFullPathName().toUTF8(), &buf) == 0)
	{
		switch (buf.f_type)
		{
			case U_ISOFS_SUPER_MAGIC:   // CD-ROM
			case U_MSDOS_SUPER_MAGIC:   // Probably floppy (but could be mounted FAT filesystem)
			case U_NFS_SUPER_MAGIC:	 // Network NFS
			case U_SMB_SUPER_MAGIC:	 // Network Samba
				return false;

			default:
				// Assume anything else is a hard-disk (but note it could
				// be a RAM disk.  There isn't a good way of determining
				// this for sure)
				return true;
		}
	}

	// Assume so if this fails for some reason
	return true;
}

bool File::isOnRemovableDrive() const
{
	jassertfalse // xxx not implemented for linux!
	return false;
}

bool File::isHidden() const
{
	return getFileName().startsWithChar ('.');
}

const char* juce_Argv0 = 0;  // referenced from juce_Application.cpp

const File File::getSpecialLocation (const SpecialLocationType type)
{
	switch (type)
	{
	case userHomeDirectory:
	{
		const char* homeDir = getenv ("HOME");

		if (homeDir == 0)
		{
			struct passwd* const pw = getpwuid (getuid());
			if (pw != 0)
				homeDir = pw->pw_dir;
		}

		return File (String::fromUTF8 (homeDir));
	}

	case userDocumentsDirectory:
	case userMusicDirectory:
	case userMoviesDirectory:
	case userApplicationDataDirectory:
		return File ("~");

	case userDesktopDirectory:
		return File ("~/Desktop");

	case commonApplicationDataDirectory:
		return File ("/var");

	case globalApplicationsDirectory:
		return File ("/usr");

	case tempDirectory:
	{
		File tmp ("/var/tmp");

		if (! tmp.isDirectory())
		{
			tmp = "/tmp";

			if (! tmp.isDirectory())
				tmp = File::getCurrentWorkingDirectory();
		}

		return tmp;
	}

	case invokedExecutableFile:
		if (juce_Argv0 != 0)
			return File (String::fromUTF8 (juce_Argv0));
		// deliberate fall-through...

	case currentExecutableFile:
	case currentApplicationFile:
		return juce_getExecutableFile();

	default:
		jassertfalse // unknown type?
		break;
	}

	return File::nonexistent;
}

const String File::getVersion() const
{
	return String::empty; // xxx not yet implemented
}

const File File::getLinkedTarget() const
{
	char buffer [4096];
	size_t numChars = readlink (getFullPathName().toUTF8(),
								buffer, sizeof (buffer));

	if (numChars > 0 && numChars <= sizeof (buffer))
		return File (String::fromUTF8 (buffer, (int) numChars));

	return *this;
}

bool File::moveToTrash() const
{
	if (! exists())
		return true;

	File trashCan ("~/.Trash");

	if (! trashCan.isDirectory())
		trashCan = "~/.local/share/Trash/files";

	if (! trashCan.isDirectory())
		return false;

	return moveFileTo (trashCan.getNonexistentChildFile (getFileNameWithoutExtension(),
														 getFileExtension()));
}

struct FindFileStruct
{
	String parentDir, wildCard;
	DIR* dir;

	bool getNextMatch (String& result, bool* const isDir, bool* const isHidden, int64* const fileSize,
					   Time* const modTime, Time* const creationTime, bool* const isReadOnly)
	{
		const char* const wildcardUTF8 = wildCard.toUTF8();

		for (;;)
		{
			struct dirent* const de = readdir (dir);

			if (de == 0)
				break;

			if (fnmatch (wildcardUTF8, de->d_name, FNM_CASEFOLD) == 0)
			{
				result = String::fromUTF8 (de->d_name);

				const String path (parentDir + result);

				if (isDir != 0 || fileSize != 0)
				{
					struct stat info;
					const bool statOk = (stat (path.toUTF8(), &info) == 0);

					if (isDir != 0)
						*isDir = path.isEmpty() || (statOk && ((info.st_mode & S_IFDIR) != 0));

					if (isHidden != 0)
						*isHidden = (de->d_name[0] == '.');

					if (fileSize != 0)
						*fileSize = statOk ? info.st_size : 0;
				}

				if (modTime != 0 || creationTime != 0)
				{
					int64 m, a, c;
					juce_getFileTimes (path, m, a, c);

					if (modTime != 0)
						*modTime = m;

					if (creationTime != 0)
						*creationTime = c;
				}

				if (isReadOnly != 0)
					*isReadOnly = ! juce_canWriteToFile (path);

				return true;
			}
		}

		return false;
	}
};

// returns 0 on failure
void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
						  bool* isDir, bool* isHidden, int64* fileSize, Time* modTime,
						  Time* creationTime, bool* isReadOnly)
{
	DIR* d = opendir (directory.toUTF8());

	if (d != 0)
	{
		FindFileStruct* ff = new FindFileStruct();
		ff->parentDir = directory;

		if (!ff->parentDir.endsWithChar (File::separator))
			ff->parentDir += File::separator;

		ff->wildCard = wildCard;
		if (wildCard == "*.*")
			ff->wildCard = "*";

		ff->dir = d;

		if (ff->getNextMatch (firstResultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly))
		{
			return ff;
		}
		else
		{
			firstResultFile = String::empty;
			isDir = false;
			isHidden = false;
			closedir (d);
			delete ff;
		}
	}

	return 0;
}

bool juce_findFileNext (void* handle, String& resultFile,
						bool* isDir, bool* isHidden, int64* fileSize, Time* modTime, Time* creationTime, bool* isReadOnly)
{
	FindFileStruct* const ff = (FindFileStruct*) handle;

	if (ff != 0)
		return ff->getNextMatch (resultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly);

	return false;
}

void juce_findFileClose (void* handle)
{
	FindFileStruct* const ff = (FindFileStruct*) handle;

	if (ff != 0)
	{
		closedir (ff->dir);
		delete ff;
	}
}

bool juce_launchFile (const String& fileName,
					  const String& parameters)
{
	String cmdString (fileName.replace (" ", "\\ ",false));
	cmdString << " " << parameters;

	if (URL::isProbablyAWebsiteURL (fileName)
		 || cmdString.startsWithIgnoreCase ("file:")
		 || URL::isProbablyAnEmailAddress (fileName))
	{
		// create a command that tries to launch a bunch of likely browsers
		const char* const browserNames[] = { "xdg-open", "/etc/alternatives/x-www-browser", "firefox", "mozilla", "konqueror", "opera" };

		StringArray cmdLines;

		for (int i = 0; i < numElementsInArray (browserNames); ++i)
			cmdLines.add (String (browserNames[i]) + " " + cmdString.trim().quoted());

		cmdString = cmdLines.joinIntoString (" || ");
	}

	const char* const argv[4] = { "/bin/sh", "-c", cmdString.toUTF8(), 0 };

	const int cpid = fork();

	if (cpid == 0)
	{
		setsid();

		// Child process
		execve (argv[0], (char**) argv, environ);
		exit (0);
	}

	return cpid >= 0;
}

void File::revealToUser() const
{
	if (isDirectory())
		startAsProcess();
	else if (getParentDirectory().exists())
		getParentDirectory().startAsProcess();
}

#endif
/*** End of inlined file: juce_linux_Files.cpp ***/


/*** Start of inlined file: juce_posix_NamedPipe.cpp ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

struct NamedPipeInternal
{
	String pipeInName, pipeOutName;
	int pipeIn, pipeOut;

	bool volatile createdPipe, blocked, stopReadOperation;

	static void signalHandler (int) {}
};

void NamedPipe::cancelPendingReads()
{
	while (internal != 0 && ((NamedPipeInternal*) internal)->blocked)
	{
		NamedPipeInternal* const intern = (NamedPipeInternal*) internal;

		intern->stopReadOperation = true;

		char buffer [1] = { 0 };
		int bytesWritten = (int) ::write (intern->pipeIn, buffer, 1);
		(void) bytesWritten;

		int timeout = 2000;
		while (intern->blocked && --timeout >= 0)
			Thread::sleep (2);

		intern->stopReadOperation = false;
	}
}

void NamedPipe::close()
{
	NamedPipeInternal* const intern = (NamedPipeInternal*) internal;

	if (intern != 0)
	{
		internal = 0;

		if (intern->pipeIn != -1)
			::close (intern->pipeIn);

		if (intern->pipeOut != -1)
			::close (intern->pipeOut);

		if (intern->createdPipe)
		{
			unlink (intern->pipeInName.toUTF8());
			unlink (intern->pipeOutName.toUTF8());
		}

		delete intern;
	}
}

bool NamedPipe::openInternal (const String& pipeName, const bool createPipe)
{
	close();

	NamedPipeInternal* const intern = new NamedPipeInternal();
	internal = intern;
	intern->createdPipe = createPipe;
	intern->blocked = false;
	intern->stopReadOperation = false;

	signal (SIGPIPE, NamedPipeInternal::signalHandler);
	siginterrupt (SIGPIPE, 1);

	const String pipePath ("/tmp/" + File::createLegalFileName (pipeName));

	intern->pipeInName  = pipePath + "_in";
	intern->pipeOutName = pipePath + "_out";
	intern->pipeIn = -1;
	intern->pipeOut = -1;

	if (createPipe)
	{
		if ((mkfifo (intern->pipeInName.toUTF8(), 0666) && errno != EEXIST)
			|| (mkfifo (intern->pipeOutName.toUTF8(), 0666) && errno != EEXIST))
		{
			delete intern;
			internal = 0;

			return false;
		}
	}

	return true;
}

int NamedPipe::read (void* destBuffer, int maxBytesToRead, int /*timeOutMilliseconds*/)
{
	int bytesRead = -1;
	NamedPipeInternal* const intern = (NamedPipeInternal*) internal;

	if (intern != 0)
	{
		intern->blocked = true;

		if (intern->pipeIn == -1)
		{
			if (intern->createdPipe)
				intern->pipeIn = ::open (intern->pipeInName.toUTF8(), O_RDWR);
			else
				intern->pipeIn = ::open (intern->pipeOutName.toUTF8(), O_RDWR);

			if (intern->pipeIn == -1)
			{
				intern->blocked = false;
				return -1;
			}
		}

		bytesRead = 0;

		char* p = (char*) destBuffer;

		while (bytesRead < maxBytesToRead)
		{
			const int bytesThisTime = maxBytesToRead - bytesRead;
			const int numRead = (int) ::read (intern->pipeIn, p, bytesThisTime);

			if (numRead <= 0 || intern->stopReadOperation)
			{
				bytesRead = -1;
				break;
			}

			bytesRead += numRead;
			p += bytesRead;
		}

		intern->blocked = false;
	}

	return bytesRead;
}

int NamedPipe::write (const void* sourceBuffer, int numBytesToWrite, int timeOutMilliseconds)
{
	int bytesWritten = -1;
	NamedPipeInternal* const intern = (NamedPipeInternal*) internal;

	if (intern != 0)
	{
		if (intern->pipeOut == -1)
		{
			if (intern->createdPipe)
				intern->pipeOut = ::open (intern->pipeOutName.toUTF8(), O_WRONLY);
			else
				intern->pipeOut = ::open (intern->pipeInName.toUTF8(), O_WRONLY);

			if (intern->pipeOut == -1)
			{
				return -1;
			}
		}

		const char* p = (const char*) sourceBuffer;
		bytesWritten = 0;

		const uint32 timeOutTime = Time::getMillisecondCounter() + timeOutMilliseconds;

		while (bytesWritten < numBytesToWrite
			   && (timeOutMilliseconds < 0 || Time::getMillisecondCounter() < timeOutTime))
		{
			const int bytesThisTime = numBytesToWrite - bytesWritten;
			const int numWritten = (int) ::write (intern->pipeOut, p, bytesThisTime);

			if (numWritten <= 0)
			{
				bytesWritten = -1;
				break;
			}

			bytesWritten += numWritten;
			p += bytesWritten;
		}
	}

	return bytesWritten;
}

#endif
/*** End of inlined file: juce_posix_NamedPipe.cpp ***/


/*** Start of inlined file: juce_linux_Network.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian)
{
	int numResults = 0;

	const int s = socket (AF_INET, SOCK_DGRAM, 0);
	if (s != -1)
	{
		char buf [1024];
		struct ifconf ifc;
		ifc.ifc_len = sizeof (buf);
		ifc.ifc_buf = buf;
		ioctl (s, SIOCGIFCONF, &ifc);

		for (unsigned int i = 0; i < ifc.ifc_len / sizeof (struct ifreq); ++i)
		{
			struct ifreq ifr;
			strcpy (ifr.ifr_name, ifc.ifc_req[i].ifr_name);

			if (ioctl (s, SIOCGIFFLAGS, &ifr) == 0
				 && (ifr.ifr_flags & IFF_LOOPBACK) == 0
				 && ioctl (s, SIOCGIFHWADDR, &ifr) == 0
				 && numResults < maxNum)
			{
				int64 a = 0;
				for (int j = 6; --j >= 0;)
					a = (a << 8) | (uint8) ifr.ifr_hwaddr.sa_data [littleEndian ? j : (5 - j)];

				*addresses++ = a;
				++numResults;
			}
		}

		close (s);
	}

	return numResults;
}

bool PlatformUtilities::launchEmailWithAttachments (const String& targetEmailAddress,
													const String& emailSubject,
													const String& bodyText,
													const StringArray& filesToAttach)
{
	jassertfalse	// xxx todo

	return false;
}

class JUCE_HTTPSocketStream
{
public:

	JUCE_HTTPSocketStream()
		: readPosition (0),
		  socketHandle (-1),
		  levelsOfRedirection (0),
		  timeoutSeconds (15)
	{
	}

	~JUCE_HTTPSocketStream()
	{
		closeSocket();
	}

	bool open (const String& url,
			   const String& headers,
			   const MemoryBlock& postData,
			   const bool isPost,
			   URL::OpenStreamProgressCallback* callback,
			   void* callbackContext,
			   int timeOutMs)
	{
		closeSocket();

		uint32 timeOutTime = Time::getMillisecondCounter();

		if (timeOutMs == 0)
			timeOutTime += 60000;
		else if (timeOutMs < 0)
			timeOutTime = 0xffffffff;
		else
			timeOutTime += timeOutMs;

		String hostName, hostPath;
		int hostPort;

		if (! decomposeURL (url, hostName, hostPath, hostPort))
			return false;

		const struct hostent* host = 0;
		int port = 0;

		String proxyName, proxyPath;
		int proxyPort = 0;

		String proxyURL (getenv ("http_proxy"));
		if (proxyURL.startsWithIgnoreCase ("http://"))
		{
			if (! decomposeURL (proxyURL, proxyName, proxyPath, proxyPort))
				return false;

			host = gethostbyname (proxyName.toUTF8());
			port = proxyPort;
		}
		else
		{
			host = gethostbyname (hostName.toUTF8());
			port = hostPort;
		}

		if (host == 0)
			return false;

		struct sockaddr_in address;
		zerostruct (address);
		memcpy (&address.sin_addr, host->h_addr, host->h_length);
		address.sin_family = host->h_addrtype;
		address.sin_port = htons (port);

		socketHandle = socket (host->h_addrtype, SOCK_STREAM, 0);

		if (socketHandle == -1)
			return false;

		int receiveBufferSize = 16384;
		setsockopt (socketHandle, SOL_SOCKET, SO_RCVBUF, (char*) &receiveBufferSize, sizeof (receiveBufferSize));
		setsockopt (socketHandle, SOL_SOCKET, SO_KEEPALIVE, 0, 0);

#if JUCE_MAC
		setsockopt (socketHandle, SOL_SOCKET, SO_NOSIGPIPE, 0, 0);
#endif

		if (connect (socketHandle, (struct sockaddr*) &address, sizeof (address)) == -1)
		{
			closeSocket();
			return false;
		}

		const MemoryBlock requestHeader (createRequestHeader (hostName, hostPort,
															  proxyName, proxyPort,
															  hostPath, url,
															  headers, postData,
															  isPost));
		size_t totalHeaderSent = 0;

		while (totalHeaderSent < requestHeader.getSize())
		{
			if (Time::getMillisecondCounter() > timeOutTime)
			{
				closeSocket();
				return false;
			}

			const int numToSend = jmin (1024, (int) (requestHeader.getSize() - totalHeaderSent));

			if (send (socketHandle,
					  ((const char*) requestHeader.getData()) + totalHeaderSent,
					  numToSend, 0)
				!= numToSend)
			{
				closeSocket();
				return false;
			}

			totalHeaderSent += numToSend;

			if (callback != 0 && ! callback (callbackContext, totalHeaderSent, requestHeader.getSize()))
			{
				closeSocket();
				return false;
			}
		}

		const String responseHeader (readResponse (timeOutTime));

		if (responseHeader.isNotEmpty())
		{
			//DBG (responseHeader);

			StringArray lines;
			lines.addLines (responseHeader);

			const int statusCode = responseHeader.fromFirstOccurrenceOf (" ", false, false)
												 .substring (0, 3).getIntValue();

			//int contentLength = findHeaderItem (lines, "Content-Length:").getIntValue();
			//bool isChunked = findHeaderItem (lines, "Transfer-Encoding:").equalsIgnoreCase ("chunked");

			String location (findHeaderItem (lines, "Location:"));

			if (statusCode >= 300 && statusCode < 400
				&& location.isNotEmpty())
			{
				if (! location.startsWithIgnoreCase ("http://"))
					location = "http://" + location;

				if (levelsOfRedirection++ < 3)
					return open (location, headers, postData, isPost, callback, callbackContext, timeOutMs);
			}
			else
			{
				levelsOfRedirection = 0;
				return true;
			}
		}

		closeSocket();
		return false;
	}

	int read (void* buffer, int bytesToRead)
	{
		fd_set readbits;
		FD_ZERO (&readbits);
		FD_SET (socketHandle, &readbits);

		struct timeval tv;
		tv.tv_sec = timeoutSeconds;
		tv.tv_usec = 0;

		if (select (socketHandle + 1, &readbits, 0, 0, &tv) <= 0)
			return 0;   // (timeout)

		const int bytesRead = jmax (0, (int) recv (socketHandle, buffer, bytesToRead, MSG_WAITALL));
		readPosition += bytesRead;
		return bytesRead;
	}

	int readPosition;

	juce_UseDebuggingNewOperator

private:
	int socketHandle, levelsOfRedirection;
	const int timeoutSeconds;

	void closeSocket()
	{
		if (socketHandle >= 0)
			close (socketHandle);

		socketHandle = -1;
	}

	const MemoryBlock createRequestHeader (const String& hostName,
										   const int hostPort,
										   const String& proxyName,
										   const int proxyPort,
										   const String& hostPath,
										   const String& originalURL,
										   const String& headers,
										   const MemoryBlock& postData,
										   const bool isPost)
	{
		String header (isPost ? "POST " : "GET ");

		if (proxyName.isEmpty())
		{
			header << hostPath << " HTTP/1.0\r\nHost: "
			<< hostName << ':' << hostPort;
		}
		else
		{
			header << originalURL << " HTTP/1.0\r\nHost: "
			<< proxyName << ':' << proxyPort;
		}

		header << "\r\nUser-Agent: JUCE/"
		<< JUCE_MAJOR_VERSION << '.' << JUCE_MINOR_VERSION
		<< "\r\nConnection: Close\r\nContent-Length: "
		<< postData.getSize() << "\r\n"
		<< headers << "\r\n";

		MemoryBlock mb;
		mb.append (header.toUTF8(), (int) strlen (header.toUTF8()));
		mb.append (postData.getData(), postData.getSize());

		return mb;
	}

	const String readResponse (const uint32 timeOutTime)
	{
		int bytesRead = 0, numConsecutiveLFs  = 0;
		MemoryBlock buffer (1024, true);

		while (numConsecutiveLFs < 2 && bytesRead < 32768
			   && Time::getMillisecondCounter() <= timeOutTime)
		{
			fd_set readbits;
			FD_ZERO (&readbits);
			FD_SET (socketHandle, &readbits);

			struct timeval tv;
			tv.tv_sec = timeoutSeconds;
			tv.tv_usec = 0;

			if (select (socketHandle + 1, &readbits, 0, 0, &tv) <= 0)
				return String::empty;  // (timeout)

			buffer.ensureSize (bytesRead + 8, true);
			char* const dest = (char*) buffer.getData() + bytesRead;

			if (recv (socketHandle, dest, 1, 0) == -1)
				return String::empty;

			const char lastByte = *dest;
			++bytesRead;

			if (lastByte == '\n')
				++numConsecutiveLFs;
			else if (lastByte != '\r')
				numConsecutiveLFs = 0;
		}

		const String header (String::fromUTF8 ((const char*) buffer.getData()));

		if (header.startsWithIgnoreCase ("HTTP/"))
			return header.trimEnd();

		return String::empty;
	}

	static bool decomposeURL (const String& url,
							  String& host, String& path, int& port)
	{
		if (! url.startsWithIgnoreCase ("http://"))
			return false;

		const int nextSlash = url.indexOfChar (7, '/');
		int nextColon = url.indexOfChar (7, ':');
		if (nextColon > nextSlash && nextSlash > 0)
			nextColon = -1;

		if (nextColon >= 0)
		{
			host = url.substring (7, nextColon);

			if (nextSlash >= 0)
				port = url.substring (nextColon + 1, nextSlash).getIntValue();
			else
				port = url.substring (nextColon + 1).getIntValue();
		}
		else
		{
			port = 80;

			if (nextSlash >= 0)
				host = url.substring (7, nextSlash);
			else
				host = url.substring (7);
		}

		if (nextSlash >= 0)
			path = url.substring (nextSlash);
		else
			path = "/";

		return true;
	}

	static const String findHeaderItem (const StringArray& lines, const String& itemName)
	{
		for (int i = 0; i < lines.size(); ++i)
			if (lines[i].startsWithIgnoreCase (itemName))
				return lines[i].substring (itemName.length()).trim();

		return String::empty;
	}
};

bool juce_isOnLine()
{
	return true;
}

void* juce_openInternetFile (const String& url,
							 const String& headers,
							 const MemoryBlock& postData,
							 const bool isPost,
							 URL::OpenStreamProgressCallback* callback,
							 void* callbackContext,
							 int timeOutMs)
{
	JUCE_HTTPSocketStream* const s = new JUCE_HTTPSocketStream();

	if (s->open (url, headers, postData, isPost,
				 callback, callbackContext, timeOutMs))
		return s;

	delete s;
	return 0;
}

void juce_closeInternetFile (void* handle)
{
	JUCE_HTTPSocketStream* const s = (JUCE_HTTPSocketStream*) handle;

	if (s != 0)
		delete s;
}

int juce_readFromInternetFile (void* handle, void* buffer, int bytesToRead)
{
	JUCE_HTTPSocketStream* const s = (JUCE_HTTPSocketStream*) handle;

	if (s != 0)
		return s->read (buffer, bytesToRead);

	return 0;
}

int64 juce_getInternetFileContentLength (void* handle)
{
	JUCE_HTTPSocketStream* const s = (JUCE_HTTPSocketStream*) handle;

	if (s != 0)
	{
		//xxx todo
		jassertfalse
	}

	return -1;
}

int juce_seekInInternetFile (void* handle, int newPosition)
{
	JUCE_HTTPSocketStream* const s = (JUCE_HTTPSocketStream*) handle;

	if (s != 0)
		return s->readPosition;

	return 0;
}

#endif
/*** End of inlined file: juce_linux_Network.cpp ***/


/*** Start of inlined file: juce_linux_SystemStats.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

void Logger::outputDebugString (const String& text) throw()
{
	std::cerr << text << std::endl;
}

SystemStats::OperatingSystemType SystemStats::getOperatingSystemType() throw()
{
	return Linux;
}

const String SystemStats::getOperatingSystemName() throw()
{
	return "Linux";
}

bool SystemStats::isOperatingSystem64Bit() throw()
{
#if JUCE_64BIT
	return true;
#else
	//xxx not sure how to find this out?..
	return false;
#endif
}

static const String getCpuInfo (const char* key, bool lastOne = false) throw()
{
	String info;
	char buf [256];

	FILE* f = fopen ("/proc/cpuinfo", "r");

	while (f != 0 && fgets (buf, sizeof(buf), f))
	{
		if (strncmp (buf, key, strlen (key)) == 0)
		{
			char* p = buf;

			while (*p && *p != '\n')
				++p;

			if (*p != 0)
				*p = 0;

			p = buf;

			while (*p != 0 && *p != ':')
				++p;

			if (*p != 0 && *(p + 1) != 0)
				info = p + 2;

			if (! lastOne)
				break;
		}
	}

	fclose (f);
	return info;
}

bool SystemStats::hasMMX() throw()
{
	return getCpuInfo ("flags").contains ("mmx");
}

bool SystemStats::hasSSE() throw()
{
	return getCpuInfo ("flags").contains ("sse");
}

bool SystemStats::hasSSE2() throw()
{
	return getCpuInfo ("flags").contains ("sse2");
}

bool SystemStats::has3DNow() throw()
{
	return getCpuInfo ("flags").contains ("3dnow");
}

const String SystemStats::getCpuVendor() throw()
{
	return getCpuInfo ("vendor_id");
}

int SystemStats::getCpuSpeedInMegaherz() throw()
{
	const String speed (getCpuInfo ("cpu MHz"));

	return (int) (speed.getFloatValue() + 0.5f);
}

int SystemStats::getMemorySizeInMegabytes() throw()
{
	struct sysinfo sysi;

	if (sysinfo (&sysi) == 0)
		return (sysi.totalram * sysi.mem_unit / (1024 * 1024));

	return 0;
}

uint32 juce_millisecondsSinceStartup() throw()
{
	static unsigned int calibrate = 0;
	static bool calibrated = false;
	timeval t;
	unsigned int ret = 0;

	if (! gettimeofday (&t, 0))
	{
		if (! calibrated)
		{
			struct sysinfo sysi;

			if (sysinfo (&sysi) == 0)
				// Safe to assume system was not brought up earlier than 1970!
				calibrate = t.tv_sec - sysi.uptime;

			calibrated = true;
		}

		ret = 1000 * (t.tv_sec - calibrate) + (t.tv_usec / 1000);
	}

	return ret;
}

double Time::getMillisecondCounterHiRes() throw()
{
	return getHighResolutionTicks() * 0.001;
}

int64 Time::getHighResolutionTicks() throw()
{
	timeval t;
	if (gettimeofday (&t, 0))
		return 0;

	return ((int64) t.tv_sec * (int64) 1000000) + (int64) t.tv_usec;
}

int64 Time::getHighResolutionTicksPerSecond() throw()
{
	// Microseconds
	return 1000000;
}

bool Time::setSystemTimeToThisTime() const throw()
{
	timeval t;
	t.tv_sec = millisSinceEpoch % 1000000;
	t.tv_usec = millisSinceEpoch - t.tv_sec;

	return settimeofday (&t, NULL) ? false : true;
}

int SystemStats::getPageSize() throw()
{
	static int systemPageSize = 0;

	if (systemPageSize == 0)
		systemPageSize = sysconf (_SC_PAGESIZE);

	return systemPageSize;
}

int SystemStats::getNumCpus() throw()
{
	const int lastCpu = getCpuInfo ("processor", true).getIntValue();

	return lastCpu + 1;
}

const String SystemStats::getLogonName()
{
	const char* user = getenv ("USER");

	if (user == 0)
	{
		struct passwd* const pw = getpwuid (getuid());
		if (pw != 0)
			user = pw->pw_name;
	}

	return String::fromUTF8 (user);
}

const String SystemStats::getFullUserName()
{
	return getLogonName();
}

void SystemStats::initialiseStats() throw()
{
	// Process starts off as root when running suid
	Process::lowerPrivilege();

	String s (SystemStats::getJUCEVersion());
}

void PlatformUtilities::fpuReset()
{
}

#endif
/*** End of inlined file: juce_linux_SystemStats.cpp ***/


/*** Start of inlined file: juce_linux_Threads.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

void JUCE_API juce_threadEntryPoint (void*);

void* threadEntryProc (void* value)
{
	// New threads start off as root when running suid
	Process::lowerPrivilege();

	juce_threadEntryPoint (value);
	return 0;
}

void* juce_createThread (void* userData)
{
	pthread_t handle = 0;

	if (pthread_create (&handle, 0, threadEntryProc, userData) == 0)
	{
		pthread_detach (handle);
		return (void*) handle;
	}

	return 0;
}

void juce_killThread (void* handle)
{
	if (handle != 0)
		pthread_cancel ((pthread_t)handle);
}

void juce_setCurrentThreadName (const String& /*name*/)
{
}

Thread::ThreadID Thread::getCurrentThreadId()
{
	return (ThreadID) pthread_self();
}

// priority 1 to 10 where 5=normal, 1=low. If the handle is 0, sets the
// priority of the current thread
bool juce_setThreadPriority (void* handle, int priority)
{
	struct sched_param param;
	int policy, maxp, minp, pri;

	if (handle == 0)
		handle = (void*) pthread_self();

	if (pthread_getschedparam ((pthread_t) handle, &policy, &param) == 0
		 && policy != SCHED_OTHER)
	{
		minp = sched_get_priority_min(policy);
		maxp = sched_get_priority_max(policy);

		pri = ((maxp - minp) / 2) * (priority - 1) / 9;

		if (param.__sched_priority >= (minp + (maxp - minp) / 2))
			// Realtime process priority
			param.__sched_priority = minp + ((maxp - minp) / 2) + pri;
		else
			// High process priority
			param.__sched_priority = minp + pri;

		param.sched_priority = jlimit (1, 127, 1 + (priority * 126) / 11);

		return pthread_setschedparam ((pthread_t) handle, policy, &param) == 0;
	}

	return false;
}

void Thread::setCurrentThreadAffinityMask (const uint32 affinityMask)
{
#if SUPPORT_AFFINITIES
	cpu_set_t affinity;
	CPU_ZERO (&affinity);

	for (int i = 0; i < 32; ++i)
		if ((affinityMask & (1 << i)) != 0)
			CPU_SET (i, &affinity);

	sched_setaffinity (getpid(), sizeof (cpu_set_t), &affinity);
	sched_yield();

#else
	/* affinities aren't supported because either the appropriate header files weren't found,
	   or the SUPPORT_AFFINITIES macro was turned off in linuxheaders.h
	*/
	jassertfalse
#endif
}

void Thread::yield()
{
	sched_yield();
}

// sets the process to 0=low priority, 1=normal, 2=high, 3=realtime
void Process::setPriority (ProcessPriority prior)
{
	struct sched_param param;
	int policy, maxp, minp;

	const int p = (int) prior;

	if (p <= 1)
		policy = SCHED_OTHER;
	else
		policy = SCHED_RR;

	minp = sched_get_priority_min (policy);
	maxp = sched_get_priority_max (policy);

	if (p < 2)
		param.__sched_priority = 0;
	else if (p == 2 )
		// Set to middle of lower realtime priority range
		param.__sched_priority = minp + (maxp - minp) / 4;
	else
		// Set to middle of higher realtime priority range
		param.__sched_priority = minp + (3 * (maxp - minp) / 4);

	pthread_setschedparam (pthread_self(), policy, &param);
}

void Process::terminate()
{
	exit (0);
}

bool JUCE_PUBLIC_FUNCTION juce_isRunningUnderDebugger()
{
	static char testResult = 0;

	if (testResult == 0)
	{
		testResult = (char) ptrace (PT_TRACE_ME, 0, 0, 0);

		if (testResult >= 0)
		{
			ptrace (PT_DETACH, 0, (caddr_t) 1, 0);
			testResult = 1;
		}
	}

	return testResult < 0;
}

bool JUCE_CALLTYPE Process::isRunningUnderDebugger()
{
	return juce_isRunningUnderDebugger();
}

void Process::raisePrivilege()
{
	// If running suid root, change effective user
	// to root
	if (geteuid() != 0 && getuid() == 0)
	{
		setreuid (geteuid(), getuid());
		setregid (getegid(), getgid());
	}
}

void Process::lowerPrivilege()
{
	// If runing suid root, change effective user
	// back to real user
	if (geteuid() == 0 && getuid() != 0)
	{
		setreuid (geteuid(), getuid());
		setregid (getegid(), getgid());
	}
}

#if ! JUCE_ONLY_BUILD_CORE_LIBRARY

void* PlatformUtilities::loadDynamicLibrary (const String& name)
{
	return dlopen (name.toUTF8(), RTLD_LOCAL | RTLD_NOW);
}

void PlatformUtilities::freeDynamicLibrary (void* handle)
{
	dlclose(handle);
}

void* PlatformUtilities::getProcedureEntryPoint (void* libraryHandle, const String& procedureName)
{
	return dlsym (libraryHandle, procedureName.toCString());
}

#endif

#endif
/*** End of inlined file: juce_linux_Threads.cpp ***/

#if ! JUCE_ONLY_BUILD_CORE_LIBRARY

/*** Start of inlined file: juce_linux_Clipboard.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#ifdef JUCE_DEBUG
  #define JUCE_DEBUG_XERRORS 1
#endif

extern Display* display;
extern Window juce_messageWindowHandle;

static String localClipboardContent;
static Atom   atom_UTF8_STRING;
static Atom   atom_CLIPBOARD;
static Atom   atom_TARGETS;

static void initSelectionAtoms()
{
	static bool isInitialised = false;
	if (! isInitialised)
	{
		atom_UTF8_STRING = XInternAtom (display, "UTF8_STRING", False);
		atom_CLIPBOARD   = XInternAtom (display, "CLIPBOARD", False);
		atom_TARGETS     = XInternAtom (display, "TARGETS", False);
	}
}

// Read the content of a window property as either a locale-dependent string or an utf8 string
// works only for strings shorter than 1000000 bytes
static String juce_readWindowProperty (Window window, Atom prop,
									   Atom fmt, // XA_STRING or UTF8_STRING
									   bool deleteAfterReading)
{
	String returnData;
	char* clipData;
	Atom actualType;
	int  actualFormat;
	unsigned long numItems, bytesLeft;

	if (XGetWindowProperty (display, window, prop,
							0L /* offset */, 1000000 /* length (max) */, False,
							AnyPropertyType /* format */,
							&actualType, &actualFormat, &numItems, &bytesLeft,
							(unsigned char**) &clipData) == Success)
	{
		if (actualType == atom_UTF8_STRING && actualFormat == 8)
		{
			returnData = String::fromUTF8 (clipData, numItems);
		}
		else if (actualType == XA_STRING && actualFormat == 8)
		{
			returnData = String (clipData, numItems);
		}

		if (clipData != 0)
			XFree (clipData);

		jassert (bytesLeft == 0 || numItems == 1000000);
	}

	if (deleteAfterReading)
		XDeleteProperty (display, window, prop);

	return returnData;
}

// Send a SelectionRequest to the window owning the selection and waits for its answer (with a timeout) */
static bool juce_requestSelectionContent (String &selection_content, Atom selection, Atom requested_format)
{
	Atom property_name = XInternAtom (display, "JUCE_SEL", false);

	// The selection owner will be asked to set the JUCE_SEL property on the
	// juce_messageWindowHandle with the selection content
	XConvertSelection (display, selection, requested_format, property_name,
					   juce_messageWindowHandle, CurrentTime);

	int timeoutMs = 200; // will wait at most for 200 ms

	do
	{
		XEvent event;

		if (XCheckTypedWindowEvent (display, juce_messageWindowHandle, SelectionNotify, &event))
		{
			if (event.xselection.property == property_name)
			{
				jassert (event.xselection.requestor == juce_messageWindowHandle);

				selection_content = juce_readWindowProperty (event.xselection.requestor,
															 event.xselection.property,
															 requested_format, true);
				return true;
			}
			else
			{
				return false; // the format we asked for was denied.. (event.xselection.property == None)
			}
		}

		// not very elegant.. we could do a select() or something like that...
		// however clipboard content requesting is inherently slow on x11, it
		// often takes 50ms or more so...
		Thread::sleep (4);
		timeoutMs -= 4;
	}
	while (timeoutMs > 0);

	DBG("timeout for juce_requestSelectionContent");
	return false;
}

// Called from the event loop in juce_linux_Messaging in response to SelectionRequest events
void juce_handleSelectionRequest (XSelectionRequestEvent &evt)
{
	initSelectionAtoms();

	// the selection content is sent to the target window as a window property
	XSelectionEvent reply;
	reply.type = SelectionNotify;
	reply.display = evt.display;
	reply.requestor = evt.requestor;
	reply.selection = evt.selection;
	reply.target = evt.target;
	reply.property = None; // == "fail"
	reply.time = evt.time;

	HeapBlock <char> data;
	int propertyFormat = 0, numDataItems = 0;

	if (evt.selection == XA_PRIMARY || evt.selection == atom_CLIPBOARD)
	{
		if (evt.target == XA_STRING)
		{
			// format data according to system locale
			numDataItems = localClipboardContent.getNumBytesAsCString() + 1;
			data.calloc (numDataItems + 1);
			localClipboardContent.copyToCString (data, numDataItems);
			propertyFormat = 8; // bits/item
		}
		else if (evt.target == atom_UTF8_STRING)
		{
			// translate to utf8
			numDataItems = localClipboardContent.getNumBytesAsUTF8() + 1;
			data.calloc (numDataItems + 1);
			localClipboardContent.copyToUTF8 (data, numDataItems);
			propertyFormat = 8; // bits/item
		}
		else if (evt.target == atom_TARGETS)
		{
			// another application wants to know what we are able to send
			numDataItems = 2;
			propertyFormat = 32; // atoms are 32-bit
			data.calloc (numDataItems * 4);
			Atom* atoms = reinterpret_cast<Atom*> (data.getData());
			atoms[0] = atom_UTF8_STRING;
			atoms[1] = XA_STRING;
		}
	}
	else
	{
		DBG ("requested unsupported clipboard");
	}

	if (data != 0)
	{
		const int maxReasonableSelectionSize = 1000000;

		// for very big chunks of data, we should use the "INCR" protocol , which is a pain in the *ss
		if (evt.property != None && numDataItems < maxReasonableSelectionSize)
		{
			XChangeProperty (evt.display, evt.requestor,
							 evt.property, evt.target,
							 propertyFormat /* 8 or 32 */, PropModeReplace,
							 reinterpret_cast<const unsigned char*> (data.getData()), numDataItems);
			reply.property = evt.property; // " == success"
		}
	}

	XSendEvent (evt.display, evt.requestor, 0, NoEventMask, (XEvent*) &reply);
}

void SystemClipboard::copyTextToClipboard (const String& clipText)
{
	initSelectionAtoms();
	localClipboardContent = clipText;

	XSetSelectionOwner (display, XA_PRIMARY, juce_messageWindowHandle, CurrentTime);
	XSetSelectionOwner (display, atom_CLIPBOARD, juce_messageWindowHandle, CurrentTime);
}

const String SystemClipboard::getTextFromClipboard()
{
	initSelectionAtoms();

	String content;
	Atom selection = XA_PRIMARY;
	Window selectionOwner = None;

	if ((selectionOwner = XGetSelectionOwner (display, selection)) == None)
	{
		selection = atom_CLIPBOARD;
		selectionOwner = XGetSelectionOwner (display, selection);
	}

	if (selectionOwner != None)
	{
		if (selectionOwner == juce_messageWindowHandle)
		{
			content = localClipboardContent;
		}
		else
		{
			// first try: we want an utf8 string
			bool ok = juce_requestSelectionContent (content, selection, atom_UTF8_STRING);

			if (! ok)
			{
				// second chance, ask for a good old locale-dependent string ..
				ok = juce_requestSelectionContent (content, selection, XA_STRING);
			}
		}
	}

	return content;
}

#endif
/*** End of inlined file: juce_linux_Clipboard.cpp ***/



/*** Start of inlined file: juce_linux_Messaging.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#ifdef JUCE_DEBUG
 #define JUCE_DEBUG_XERRORS 1
#endif

Display* display = 0;	 // This is also referenced from WindowDriver.cpp
Window juce_messageWindowHandle = None;

XContext improbableNumber;   // This is referenced from Windowing.cpp

extern void juce_windowMessageReceive (XEvent* event);  // Defined in Windowing.cpp
extern void juce_handleSelectionRequest (XSelectionRequestEvent &evt);  // Defined in Clipboard.cpp

ScopedXLock::ScopedXLock()	   { XLockDisplay (display); }
ScopedXLock::~ScopedXLock()	  { XUnlockDisplay (display); }

class InternalMessageQueue
{
public:
	InternalMessageQueue()
		: bytesInSocket (0)
	{
		int ret = ::socketpair (AF_LOCAL, SOCK_STREAM, 0, fd);
		(void) ret; jassert (ret == 0);

		//setNonBlocking (fd[0]);
		//setNonBlocking (fd[1]);
	}

	~InternalMessageQueue()
	{
		close (fd[0]);
		close (fd[1]);
	}

	void postMessage (Message* msg)
	{
		const int maxBytesInSocketQueue = 128;

		ScopedLock sl (lock);
		queue.add (msg);

		if (bytesInSocket < maxBytesInSocketQueue)
		{
			++bytesInSocket;

			ScopedUnlock ul (lock);
			const unsigned char x = 0xff;
			size_t bytesWritten = write (fd[0], &x, 1);
			(void) bytesWritten;
		}
	}

	bool isEmpty() const
	{
		ScopedLock sl (lock);
		return queue.size() == 0;
	}

	Message* popNextMessage()
	{
		ScopedLock sl (lock);

		if (bytesInSocket > 0)
		{
			--bytesInSocket;

			ScopedUnlock ul (lock);
			unsigned char x;
			size_t numBytes = read (fd[1], &x, 1);
			(void) numBytes;
		}

		Message* m = queue[0];
		queue.remove (0, false /* deleteObject */);
		return m;
	}

	int getWaitHandle() const	 { return fd[1]; }

private:
	CriticalSection lock;
	OwnedArray <Message> queue;
	int fd[2];
	int bytesInSocket;

	static bool setNonBlocking (int handle)
	{
		int socketFlags = fcntl (handle, F_GETFL, 0);

		if (socketFlags == -1)
			return false;

		socketFlags |= O_NONBLOCK;

		return fcntl (handle, F_SETFL, socketFlags) == 0;
	}
};

struct MessageThreadFuncCall
{
	enum { uniqueID = 0x73774623 };

	MessageCallbackFunction* func;
	void* parameter;
	void* result;
	CriticalSection lock;
	WaitableEvent event;
};

static InternalMessageQueue* juce_internalMessageQueue = 0;

// error handling in X11
static bool errorOccurred = false;
static bool keyboardBreakOccurred = false;
static XErrorHandler oldErrorHandler = (XErrorHandler) 0;
static XIOErrorHandler oldIOErrorHandler = (XIOErrorHandler) 0;

// Usually happens when client-server connection is broken
static int ioErrorHandler (Display* display)
{
	DBG ("ERROR: connection to X server broken.. terminating.");

	errorOccurred = true;

	if (JUCEApplication::getInstance() != 0)
		Process::terminate();

	return 0;
}

// A protocol error has occurred
static int errorHandler (Display* display, XErrorEvent* event)
{
#ifdef JUCE_DEBUG_XERRORS
	char errorStr[64] = { 0 };
	char requestStr[64] = { 0 };

	XGetErrorText (display, event->error_code, errorStr, 64);

	XGetErrorDatabaseText (display, "XRequest", String (event->request_code).toCString(),
						   "Unknown", requestStr, 64);

	DBG ("ERROR: X returned " + String (errorStr) + " for operation " + String (requestStr));
#endif

	return 0;
}

// Breakin from keyboard
static void signalHandler (int sig)
{
	if (sig == SIGINT)
	{
		keyboardBreakOccurred = true;
		return;
	}

	static bool reentrant = false;

	if (! reentrant)
	{
		reentrant = true;

		// Illegal instruction
		fflush (stdout);
		Logger::outputDebugString ("ERROR: Program executed illegal instruction.. terminating");

		errorOccurred = true;

		if (JUCEApplication::getInstance() != 0)
			Process::terminate();
	}
	else
	{
		if (JUCEApplication::getInstance() != 0)
			exit(0);
	}
}

void MessageManager::doPlatformSpecificInitialisation()
{
	// Initialise xlib for multiple thread support
	static bool initThreadCalled = false;

	if (! initThreadCalled)
	{
		if (! XInitThreads())
		{
			// This is fatal!  Print error and closedown
			Logger::outputDebugString ("Failed to initialise xlib thread support.");

			if (JUCEApplication::getInstance() != 0)
				Process::terminate();

			return;
		}

		initThreadCalled = true;
	}

	// This is called if the client/server connection is broken
	oldIOErrorHandler = XSetIOErrorHandler (ioErrorHandler);

	// This is called if a protocol error occurs
	oldErrorHandler = XSetErrorHandler (errorHandler);

	// Install signal handler for break-in
	struct sigaction saction;
	sigset_t maskSet;
	sigemptyset (&maskSet);
	saction.sa_handler = signalHandler;
	saction.sa_mask = maskSet;
	saction.sa_flags = 0;
	sigaction (SIGINT, &saction, NULL);

#ifndef _DEBUG
	// Setup signal handlers for various fatal errors
	sigaction (SIGILL, &saction, NULL);
	sigaction (SIGBUS, &saction, NULL);
	sigaction (SIGFPE, &saction, NULL);
	sigaction (SIGSEGV, &saction, NULL);
	sigaction (SIGSYS, &saction, NULL);
#endif

	// Create the internal message queue
	juce_internalMessageQueue = new InternalMessageQueue();

	// Try to connect to a display
	String displayName (getenv ("DISPLAY"));
	if (displayName.isEmpty())
		displayName = ":0.0";

	display = XOpenDisplay (displayName.toCString());

	if (display == 0)
	{
		// This is not fatal! we can run headless.
		return;
	}

	// Get defaults for various properties
	int screen = DefaultScreen (display);
	Window root = RootWindow (display, screen);
	Visual* visual = DefaultVisual (display, screen);

	// Create a context to store user data associated with Windows we
	// create in WindowDriver
	improbableNumber = XUniqueContext();

	// We're only interested in client messages for this window
	// which are always sent
	XSetWindowAttributes swa;
	swa.event_mask = NoEventMask;

	// Create our message window (this will never be mapped)
	juce_messageWindowHandle = XCreateWindow (display, root,
											  0, 0, 1, 1, 0, 0, InputOnly,
											  visual, CWEventMask, &swa);
}

void MessageManager::doPlatformSpecificShutdown()
{
	deleteAndZero (juce_internalMessageQueue);

	if (display != 0 && ! errorOccurred)
	{
		XDestroyWindow (display, juce_messageWindowHandle);
		XCloseDisplay (display);

		// reset pointers
		juce_messageWindowHandle = 0;
		display = 0;

		// Restore original error handlers
		XSetIOErrorHandler (oldIOErrorHandler);
		oldIOErrorHandler = 0;
		XSetErrorHandler (oldErrorHandler);
		oldErrorHandler = 0;
	}
}

bool juce_postMessageToSystemQueue (void* message)
{
	if (errorOccurred)
		return false;

	juce_internalMessageQueue->postMessage ((Message*) message);
	return true;
}

void MessageManager::broadcastMessage (const String& value) throw()
{
}

void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* func,
												   void* parameter)
{
	if (errorOccurred)
		return 0;

	if (! isThisTheMessageThread())
	{
		MessageThreadFuncCall messageCallContext;
		messageCallContext.func = func;
		messageCallContext.parameter = parameter;

		juce_internalMessageQueue->postMessage (new Message (MessageThreadFuncCall::uniqueID,
															 0, 0, &messageCallContext));

		// Wait for it to complete before continuing
		messageCallContext.event.wait();

		return messageCallContext.result;
	}
	else
	{
		// Just call the function directly
		return func (parameter);
	}
}

// Wait for an event (either XEvent, or an internal Message)
static bool juce_sleepUntilEvent (const int timeoutMs)
{
	if (! juce_internalMessageQueue->isEmpty())
		return true;

	if (display != 0)
	{
		ScopedXLock xlock;
		if (XPending (display))
			return true;
	}

	struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = timeoutMs * 1000;
	int fd0 = juce_internalMessageQueue->getWaitHandle();
	int fdmax = fd0;

	fd_set readset;
	FD_ZERO (&readset);
	FD_SET (fd0, &readset);

	if (display != 0)
	{
		ScopedXLock xlock;
		int fd1 = XConnectionNumber (display);
		FD_SET (fd1, &readset);
		fdmax = jmax (fd0, fd1);
	}

	const int ret = select (fdmax + 1, &readset, 0, 0, &tv);
	return (ret > 0); // ret <= 0 if error or timeout
}

// Handle next XEvent (if any)
static bool juce_dispatchNextXEvent()
{
	if (display == 0)
		return false;

	XEvent evt;

	{
		ScopedXLock xlock;

		if (! XPending (display))
			return false;

		XNextEvent (display, &evt);
	}

	if (evt.type == SelectionRequest && evt.xany.window == juce_messageWindowHandle)
	{
		juce_handleSelectionRequest (evt.xselectionrequest);
	}
	else if (evt.xany.window != juce_messageWindowHandle)
	{
		juce_windowMessageReceive (&evt);
	}

	return true;
}

// Handle next internal Message (if any)
static bool juce_dispatchNextInternalMessage()
{
	ScopedPointer <Message> msg (juce_internalMessageQueue->popNextMessage());

	if (msg == 0)
		return false;

	if (msg->intParameter1 == MessageThreadFuncCall::uniqueID)
	{
		// Handle callback message
		MessageThreadFuncCall* const call = (MessageThreadFuncCall*) msg->pointerParameter;

		call->result = (*(call->func)) (call->parameter);
		call->event.signal();
	}
	else
	{
		// Handle "normal" messages
		MessageManager::getInstance()->deliverMessage (msg.release());
	}

	return true;
}

// this function expects that it will NEVER be called simultaneously for two concurrent threads
bool juce_dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
{
	for (;;)
	{
		if (errorOccurred)
			break;

		if (keyboardBreakOccurred)
		{
			errorOccurred = true;

			if (JUCEApplication::getInstance() != 0)
				Process::terminate();

			break;
		}

		static int totalEventCount = 0;
		++totalEventCount;

		// The purpose here is to give either priority to XEvents or
		// to internal messages This is necessary to keep a "good"
		// behaviour when the cpu is overloaded
		if (totalEventCount & 1)
		{
			if (juce_dispatchNextXEvent() || juce_dispatchNextInternalMessage())
				return true;
		}
		else
		{
			if (juce_dispatchNextInternalMessage() || juce_dispatchNextXEvent())
				return true;
		}

		if (returnIfNoPendingMessages) // early exit
			break;

		// the timeout is to be on the safe side, but it does not seem to be useful
		juce_sleepUntilEvent (2000);
	}

	return false;
}

#endif
/*** End of inlined file: juce_linux_Messaging.cpp ***/


/*** Start of inlined file: juce_linux_Fonts.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class FreeTypeFontFace
{
public:

	enum FontStyle
	{
		Plain = 0,
		Bold = 1,
		Italic = 2
	};

	struct FontNameIndex
	{
		String fileName;
		int faceIndex;
	};

	FreeTypeFontFace (const String& familyName)
	  : hasSerif (false),
		monospaced (false)
	{
		family = familyName;
	}

	void setFileName (const String& name, const int faceIndex, FontStyle style)
	{
		if (names [(int) style].fileName.isEmpty())
		{
			names [(int) style].fileName = name;
			names [(int) style].faceIndex = faceIndex;
		}
	}

	const String& getFamilyName() const throw()	 { return family; }

	const String& getFileName (const int style, int& faceIndex) const throw()
	{
		faceIndex = names[style].faceIndex;
		return names[style].fileName;
	}

	void setMonospaced (bool mono) throw()	  { monospaced = mono; }
	bool getMonospaced() const throw()	  { return monospaced; }

	void setSerif (const bool serif) throw()	{ hasSerif = serif; }
	bool getSerif() const throw()		   { return hasSerif; }

private:

	String family;
	FontNameIndex names[4];
	bool hasSerif, monospaced;
};

class FreeTypeInterface  : public DeletedAtShutdown
{
public:

	FreeTypeInterface()
		: ftLib (0),
		  lastFace (0),
		  lastBold (false),
		  lastItalic (false)
	{
		if (FT_Init_FreeType (&ftLib) != 0)
		{
			ftLib = 0;
			DBG ("Failed to initialize FreeType");
		}

		StringArray fontDirs;
		fontDirs.addTokens (String::fromUTF8 (getenv ("JUCE_FONT_PATH")), ";,", String::empty);
		fontDirs.removeEmptyStrings (true);

		if (fontDirs.size() == 0)
		{
			XmlDocument fontsConfig (File ("/etc/fonts/fonts.conf"));
			XmlElement* const fontsInfo = fontsConfig.getDocumentElement();

			if (fontsInfo != 0)
			{
				forEachXmlChildElementWithTagName (*fontsInfo, e, "dir")
				{
					fontDirs.add (e->getAllSubText().trim());
				}

				delete fontsInfo;
			}
		}

		if (fontDirs.size() == 0)
			fontDirs.add ("/usr/X11R6/lib/X11/fonts");

		for (int i = 0; i < fontDirs.size(); ++i)
			enumerateFaces (fontDirs[i]);
	}

	~FreeTypeInterface()
	{
		if (lastFace != 0)
			FT_Done_Face (lastFace);

		if (ftLib != 0)
			FT_Done_FreeType (ftLib);

		clearSingletonInstance();
	}

	FreeTypeFontFace* findOrCreate (const String& familyName, const bool create = false)
	{
		for (int i = 0; i < faces.size(); i++)
			if (faces[i]->getFamilyName() == familyName)
				return faces[i];

		if (! create)
			return NULL;

		FreeTypeFontFace* newFace = new FreeTypeFontFace (familyName);
		faces.add (newFace);

		return newFace;
	}

	// Enumerate all font faces available in a given directory
	void enumerateFaces (const String& path)
	{
		File dirPath (path);
		if (path.isEmpty() || ! dirPath.isDirectory())
			return;

		DirectoryIterator di (dirPath, true);

		while (di.next())
		{
			File possible (di.getFile());

			if (possible.hasFileExtension ("ttf")
				 || possible.hasFileExtension ("pfb")
				 || possible.hasFileExtension ("pcf"))
			{
				FT_Face face;
				int faceIndex = 0;
				int numFaces = 0;

				do
				{
					if (FT_New_Face (ftLib, possible.getFullPathName().toUTF8(),
									 faceIndex, &face) == 0)
					{
						if (faceIndex == 0)
							numFaces = face->num_faces;

						if ((face->face_flags & FT_FACE_FLAG_SCALABLE) != 0)
						{
							FreeTypeFontFace* const newFace = findOrCreate (face->family_name, true);
							int style = (int) FreeTypeFontFace::Plain;

							if ((face->style_flags & FT_STYLE_FLAG_BOLD) != 0)
								style |= (int) FreeTypeFontFace::Bold;

							if ((face->style_flags & FT_STYLE_FLAG_ITALIC) != 0)
								style |= (int) FreeTypeFontFace::Italic;

							newFace->setFileName (possible.getFullPathName(), faceIndex, (FreeTypeFontFace::FontStyle) style);
							newFace->setMonospaced ((face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) != 0);

							// Surely there must be a better way to do this?
							const String name (face->family_name);
							newFace->setSerif (! (name.containsIgnoreCase ("Sans")
												   || name.containsIgnoreCase ("Verdana")
												   || name.containsIgnoreCase ("Arial")));
						}

						FT_Done_Face (face);
					}

					++faceIndex;
				}
				while (faceIndex < numFaces);
			}
		}
	}

	// Create a FreeType face object for a given font
	FT_Face createFT_Face (const String& fontName, const bool bold, const bool italic)
	{
		FT_Face face = 0;

		if (fontName == lastFontName && bold == lastBold && italic == lastItalic)
		{
			face = lastFace;
		}
		else
		{
			if (lastFace != 0)
			{
				FT_Done_Face (lastFace);
				lastFace = 0;
			}

			lastFontName = fontName;
			lastBold = bold;
			lastItalic = italic;

			FreeTypeFontFace* const ftFace = findOrCreate (fontName);

			if (ftFace != 0)
			{
				int style = (int) FreeTypeFontFace::Plain;

				if (bold)
					style |= (int) FreeTypeFontFace::Bold;

				if (italic)
					style |= (int) FreeTypeFontFace::Italic;

				int faceIndex;
				String fileName (ftFace->getFileName (style, faceIndex));

				if (fileName.isEmpty())
				{
					style ^= (int) FreeTypeFontFace::Bold;

					fileName = ftFace->getFileName (style, faceIndex);

					if (fileName.isEmpty())
					{
						style ^= (int) FreeTypeFontFace::Bold;
						style ^= (int) FreeTypeFontFace::Italic;

						fileName = ftFace->getFileName (style, faceIndex);

						if (! fileName.length())
						{
							style ^= (int) FreeTypeFontFace::Bold;
							fileName = ftFace->getFileName (style, faceIndex);
						}
					}
				}

				if (! FT_New_Face (ftLib, fileName.toUTF8(), faceIndex, &lastFace))
				{
					face = lastFace;

					// If there isn't a unicode charmap then select the first one.
					if (FT_Select_Charmap (face, ft_encoding_unicode))
						FT_Set_Charmap (face, face->charmaps[0]);
				}
			}
		}

		return face;
	}

	bool addGlyph (FT_Face face, CustomTypeface& dest, uint32 character)
	{
		const unsigned int glyphIndex = FT_Get_Char_Index (face, character);
		const float height = (float) (face->ascender - face->descender);
		const float scaleX = 1.0f / height;
		const float scaleY = -1.0f / height;
		Path destShape;

		if (FT_Load_Glyph (face, glyphIndex, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM) != 0
			 || face->glyph->format != ft_glyph_format_outline)
		{
			return false;
		}

		const FT_Outline* const outline = &face->glyph->outline;
		const short* const contours = outline->contours;
		const char* const tags = outline->tags;
		FT_Vector* const points = outline->points;

		for (int c = 0; c < outline->n_contours; c++)
		{
			const int startPoint = (c == 0) ? 0 : contours [c - 1] + 1;
			const int endPoint = contours[c];

			for (int p = startPoint; p <= endPoint; p++)
			{
				const float x = scaleX * points[p].x;
				const float y = scaleY * points[p].y;

				if (p == startPoint)
				{
					if (FT_CURVE_TAG (tags[p]) == FT_Curve_Tag_Conic)
					{
						float x2 = scaleX * points [endPoint].x;
						float y2 = scaleY * points [endPoint].y;

						if (FT_CURVE_TAG (tags[endPoint]) != FT_Curve_Tag_On)
						{
							x2 = (x + x2) * 0.5f;
							y2 = (y + y2) * 0.5f;
						}

						destShape.startNewSubPath (x2, y2);
					}
					else
					{
						destShape.startNewSubPath (x, y);
					}
				}

				if (FT_CURVE_TAG (tags[p]) == FT_Curve_Tag_On)
				{
					if (p != startPoint)
						destShape.lineTo (x, y);
				}
				else if (FT_CURVE_TAG (tags[p]) == FT_Curve_Tag_Conic)
				{
					const int nextIndex = (p == endPoint) ? startPoint : p + 1;
					float x2 = scaleX * points [nextIndex].x;
					float y2 = scaleY * points [nextIndex].y;

					if (FT_CURVE_TAG (tags [nextIndex]) == FT_Curve_Tag_Conic)
					{
						x2 = (x + x2) * 0.5f;
						y2 = (y + y2) * 0.5f;
					}
					else
					{
						++p;
					}

					destShape.quadraticTo (x, y, x2, y2);
				}
				else if (FT_CURVE_TAG (tags[p]) == FT_Curve_Tag_Cubic)
				{
					if (p >= endPoint)
						return false;

					const int next1 = p + 1;
					const int next2 = (p == (endPoint - 1)) ? startPoint : p + 2;

					const float x2 = scaleX * points [next1].x;
					const float y2 = scaleY * points [next1].y;
					const float x3 = scaleX * points [next2].x;
					const float y3 = scaleY * points [next2].y;

					if (FT_CURVE_TAG (tags[next1]) != FT_Curve_Tag_Cubic
						 || FT_CURVE_TAG (tags[next2]) != FT_Curve_Tag_On)
						return false;

					destShape.cubicTo (x, y, x2, y2, x3, y3);
					p += 2;
				}
			}

			destShape.closeSubPath();
		}

		dest.addGlyph (character, destShape, face->glyph->metrics.horiAdvance / height);

		if ((face->face_flags & FT_FACE_FLAG_KERNING) != 0)
			addKerning (face, dest, character, glyphIndex);

		return true;
	}

	void addKerning (FT_Face face, CustomTypeface& dest, const uint32 character, const uint32 glyphIndex)
	{
		const float height = (float) (face->ascender - face->descender);

		uint32 rightGlyphIndex;
		uint32 rightCharCode = FT_Get_First_Char (face, &rightGlyphIndex);

		while (rightGlyphIndex != 0)
		{
			FT_Vector kerning;

			if (FT_Get_Kerning (face, glyphIndex, rightGlyphIndex, ft_kerning_unscaled, &kerning) == 0)
			{
				if (kerning.x != 0)
					dest.addKerningPair (character, rightCharCode, kerning.x / height);
			}

			rightCharCode = FT_Get_Next_Char (face, rightCharCode, &rightGlyphIndex);
		}
	}

	// Add a glyph to a font
	bool addGlyphToFont (const uint32 character, const String& fontName,
						 bool bold, bool italic, CustomTypeface& dest)
	{
		FT_Face face = createFT_Face (fontName, bold, italic);

		return face != 0 && addGlyph (face, dest, character);
	}

	void getFamilyNames (StringArray& familyNames) const
	{
		for (int i = 0; i < faces.size(); i++)
			familyNames.add (faces[i]->getFamilyName());
	}

	void getMonospacedNames (StringArray& monoSpaced) const
	{
		for (int i = 0; i < faces.size(); i++)
			if (faces[i]->getMonospaced())
				monoSpaced.add (faces[i]->getFamilyName());
	}

	void getSerifNames (StringArray& serif) const
	{
		for (int i = 0; i < faces.size(); i++)
			if (faces[i]->getSerif())
				serif.add (faces[i]->getFamilyName());
	}

	void getSansSerifNames (StringArray& sansSerif) const
	{
		for (int i = 0; i < faces.size(); i++)
			if (! faces[i]->getSerif())
				sansSerif.add (faces[i]->getFamilyName());
	}

	juce_DeclareSingleton_SingleThreaded_Minimal (FreeTypeInterface)

private:

	FT_Library ftLib;
	FT_Face lastFace;
	String lastFontName;
	bool lastBold, lastItalic;
	OwnedArray<FreeTypeFontFace> faces;
};

juce_ImplementSingleton_SingleThreaded (FreeTypeInterface)

class FreetypeTypeface   : public CustomTypeface
{
public:
	FreetypeTypeface (const Font& font)
	{
		FT_Face face = FreeTypeInterface::getInstance()
							->createFT_Face (font.getTypefaceName(), font.isBold(), font.isItalic());

		if (face == 0)
		{
#ifdef JUCE_DEBUG
			String msg ("Failed to create typeface: ");
			msg << font.getTypefaceName() << " " << (font.isBold() ? 'B' : ' ') << (font.isItalic() ? 'I' : ' ');
			DBG (msg);
#endif
		}
		else
		{
			setCharacteristics (font.getTypefaceName(),
								face->ascender / (float) (face->ascender - face->descender),
								font.isBold(), font.isItalic(),
								L' ');
		}
	}

	bool loadGlyphIfPossible (juce_wchar character)
	{
		return FreeTypeInterface::getInstance()
					->addGlyphToFont (character, name, isBold, isItalic, *this);
	}
};

const Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font)
{
	return new FreetypeTypeface (font);
}

const StringArray Font::findAllTypefaceNames()
{
	StringArray s;
	FreeTypeInterface::getInstance()->getFamilyNames (s);
	s.sort (true);
	return s;
}

static const String pickBestFont (const StringArray& names,
								  const char* const choicesString)
{
	StringArray choices;
	choices.addTokens (String (choicesString), ",", String::empty);
	choices.trim();
	choices.removeEmptyStrings();

	int i, j;
	for (j = 0; j < choices.size(); ++j)
		if (names.contains (choices[j], true))
			return choices[j];

	for (j = 0; j < choices.size(); ++j)
		for (i = 0; i < names.size(); i++)
			if (names[i].startsWithIgnoreCase (choices[j]))
				return names[i];

	for (j = 0; j < choices.size(); ++j)
		for (i = 0; i < names.size(); i++)
			if (names[i].containsIgnoreCase (choices[j]))
				return names[i];

	return names[0];
}

static const String linux_getDefaultSansSerifFontName()
{
	StringArray allFonts;
	FreeTypeInterface::getInstance()->getSansSerifNames (allFonts);

	return pickBestFont (allFonts, "Verdana, Bitstream Vera Sans, Luxi Sans, Sans");
}

static const String linux_getDefaultSerifFontName()
{
	StringArray allFonts;
	FreeTypeInterface::getInstance()->getSerifNames (allFonts);

	return pickBestFont (allFonts, "Bitstream Vera Serif, Times, Nimbus Roman, Serif");
}

static const String linux_getDefaultMonospacedFontName()
{
	StringArray allFonts;
	FreeTypeInterface::getInstance()->getMonospacedNames (allFonts);

	return pickBestFont (allFonts, "Bitstream Vera Sans Mono, Courier, Sans Mono, Mono");
}

void Font::getPlatformDefaultFontNames (String& defaultSans, String& defaultSerif, String& defaultFixed)
{
	defaultSans = linux_getDefaultSansSerifFontName();
	defaultSerif = linux_getDefaultSerifFontName();
	defaultFixed = linux_getDefaultMonospacedFontName();
}

#endif
/*** End of inlined file: juce_linux_Fonts.cpp ***/


/*** Start of inlined file: juce_linux_Windowing.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

// These are defined in juce_linux_Messaging.cpp
extern Display* display;
extern XContext improbableNumber;

namespace Atoms
{
	enum ProtocolItems
	{
		TAKE_FOCUS = 0,
		DELETE_WINDOW = 1,
		PING = 2
	};

	static Atom Protocols, ProtocolList[3], ChangeState, State,
				ActiveWin, Pid, WindowType, WindowState,
				XdndAware, XdndEnter, XdndLeave, XdndPosition, XdndStatus,
				XdndDrop, XdndFinished, XdndSelection, XdndTypeList, XdndActionList,
				XdndActionDescription, XdndActionCopy,
				allowedActions[5],
				allowedMimeTypes[2];

	const unsigned long DndVersion = 3;

	static void initialiseAtoms()
	{
		static bool atomsInitialised = false;

		if (! atomsInitialised)
		{
			atomsInitialised = true;

			Protocols                       = XInternAtom (display, "WM_PROTOCOLS", True);
			ProtocolList [TAKE_FOCUS]       = XInternAtom (display, "WM_TAKE_FOCUS", True);
			ProtocolList [DELETE_WINDOW]    = XInternAtom (display, "WM_DELETE_WINDOW", True);
			ProtocolList [PING]             = XInternAtom (display, "_NET_WM_PING", True);
			ChangeState                     = XInternAtom (display, "WM_CHANGE_STATE", True);
			State                           = XInternAtom (display, "WM_STATE", True);
			ActiveWin                       = XInternAtom (display, "_NET_ACTIVE_WINDOW", False);
			Pid                             = XInternAtom (display, "_NET_WM_PID", False);
			WindowType                      = XInternAtom (display, "_NET_WM_WINDOW_TYPE", True);
			WindowState                     = XInternAtom (display, "_NET_WM_STATE", True);

			XdndAware                       = XInternAtom (display, "XdndAware", False);
			XdndEnter                       = XInternAtom (display, "XdndEnter", False);
			XdndLeave                       = XInternAtom (display, "XdndLeave", False);
			XdndPosition                    = XInternAtom (display, "XdndPosition", False);
			XdndStatus                      = XInternAtom (display, "XdndStatus", False);
			XdndDrop                        = XInternAtom (display, "XdndDrop", False);
			XdndFinished                    = XInternAtom (display, "XdndFinished", False);
			XdndSelection                   = XInternAtom (display, "XdndSelection", False);

			XdndTypeList                    = XInternAtom (display, "XdndTypeList", False);
			XdndActionList                  = XInternAtom (display, "XdndActionList", False);
			XdndActionCopy                  = XInternAtom (display, "XdndActionCopy", False);
			XdndActionDescription           = XInternAtom (display, "XdndActionDescription", False);

			allowedMimeTypes[0]             = XInternAtom (display, "text/plain", False);
			allowedMimeTypes[1]             = XInternAtom (display, "text/uri-list", False);

			allowedActions[0]               = XInternAtom (display, "XdndActionMove", False);
			allowedActions[1]		   = XdndActionCopy;
			allowedActions[2]               = XInternAtom (display, "XdndActionLink", False);
			allowedActions[3]               = XInternAtom (display, "XdndActionAsk", False);
			allowedActions[4]               = XInternAtom (display, "XdndActionPrivate", False);
		}
	}
}

namespace Keys
{
	enum MouseButtons
	{
		NoButton = 0,
		LeftButton = 1,
		MiddleButton = 2,
		RightButton = 3,
		WheelUp = 4,
		WheelDown = 5
	};

	static int AltMask = 0;
	static int NumLockMask = 0;
	static bool numLock = false;
	static bool capsLock = false;
	static char keyStates [32];
	static const int extendedKeyModifier = 0x10000000;
}

bool KeyPress::isKeyCurrentlyDown (const int keyCode)
{
	int keysym;

	if (keyCode & Keys::extendedKeyModifier)
	{
		keysym = 0xff00 | (keyCode & 0xff);
	}
	else
	{
		keysym = keyCode;

		if (keysym == (XK_Tab & 0xff)
			|| keysym == (XK_Return & 0xff)
			|| keysym == (XK_Escape & 0xff)
			|| keysym == (XK_BackSpace & 0xff))
		{
			keysym |= 0xff00;
		}
	}

	ScopedXLock xlock;

	const int keycode = XKeysymToKeycode (display, keysym);

	const int keybyte = keycode >> 3;
	const int keybit = (1 << (keycode & 7));
	return (Keys::keyStates [keybyte] & keybit) != 0;
}

#if JUCE_USE_XSHM
namespace XSHMHelpers
{
	static int trappedErrorCode = 0;
	extern "C" int errorTrapHandler (Display*, XErrorEvent* err)
	{
		trappedErrorCode = err->error_code;
		return 0;
	}

	static bool isShmAvailable() throw()
	{
		static bool isChecked = false;
		static bool isAvailable = false;

		if (! isChecked)
		{
			isChecked = true;
			int major, minor;
			Bool pixmaps;

			ScopedXLock xlock;

			if (XShmQueryVersion (display, &major, &minor, &pixmaps))
			{
				trappedErrorCode = 0;
				XErrorHandler oldHandler = XSetErrorHandler (errorTrapHandler);

				XShmSegmentInfo segmentInfo;
				zerostruct (segmentInfo);
				XImage* xImage = XShmCreateImage (display, DefaultVisual (display, DefaultScreen (display)),
												  24, ZPixmap, 0, &segmentInfo, 50, 50);

				if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
												 xImage->bytes_per_line * xImage->height,
												 IPC_CREAT | 0777)) >= 0)
				{
					segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, 0, 0);

					if (segmentInfo.shmaddr != (void*) -1)
					{
						segmentInfo.readOnly = False;
						xImage->data = segmentInfo.shmaddr;
						XSync (display, False);

						if (XShmAttach (display, &segmentInfo) != 0)
						{
							XSync (display, False);
							XShmDetach (display, &segmentInfo);

							isAvailable = true;
						}
					}

					XFlush (display);
					XDestroyImage (xImage);

					shmdt (segmentInfo.shmaddr);
				}

				shmctl (segmentInfo.shmid, IPC_RMID, 0);

				XSetErrorHandler (oldHandler);
				if (trappedErrorCode != 0)
					isAvailable = false;
			}
		}

		return isAvailable;
	}
}
#endif

#if JUCE_USE_XRENDER
namespace XRender
{
	typedef Status (*tXRenderQueryVersion) (Display*, int*, int*);
	typedef XRenderPictFormat* (*tXrenderFindStandardFormat) (Display*, int);
	typedef XRenderPictFormat* (*tXRenderFindFormat) (Display*, unsigned long, XRenderPictFormat*, int);
	typedef XRenderPictFormat* (*tXRenderFindVisualFormat) (Display*, Visual*);

	static tXRenderQueryVersion xRenderQueryVersion = 0;
	static tXrenderFindStandardFormat xRenderFindStandardFormat = 0;
	static tXRenderFindFormat xRenderFindFormat = 0;
	static tXRenderFindVisualFormat xRenderFindVisualFormat = 0;

	static bool isAvailable()
	{
		static bool hasLoaded = false;

		if (! hasLoaded)
		{
			ScopedXLock xlock;
			hasLoaded = true;

			void* h = dlopen ("libXrender.so", RTLD_GLOBAL | RTLD_NOW);

			if (h != 0)
			{
				xRenderQueryVersion         = (tXRenderQueryVersion)        dlsym (h, "XRenderQueryVersion");
				xRenderFindStandardFormat   = (tXrenderFindStandardFormat)  dlsym (h, "XrenderFindStandardFormat");
				xRenderFindFormat           = (tXRenderFindFormat)          dlsym (h, "XRenderFindFormat");
				xRenderFindVisualFormat     = (tXRenderFindVisualFormat)    dlsym (h, "XRenderFindVisualFormat");
			}

			if (xRenderQueryVersion != 0
				 && xRenderFindStandardFormat != 0
				 && xRenderFindFormat != 0
				 && xRenderFindVisualFormat != 0)
			{
				int major, minor;
				if (xRenderQueryVersion (display, &major, &minor))
					return true;
			}

			xRenderQueryVersion = 0;
		}

		return xRenderQueryVersion != 0;
	}

	static XRenderPictFormat* findPictureFormat()
	{
		ScopedXLock xlock;

		XRenderPictFormat* pictFormat = 0;

		if (isAvailable())
		{
			pictFormat = xRenderFindStandardFormat (display, PictStandardARGB32);

			if (pictFormat == 0)
			{
				XRenderPictFormat desiredFormat;
				desiredFormat.type = PictTypeDirect;
				desiredFormat.depth = 32;

				desiredFormat.direct.alphaMask = 0xff;
				desiredFormat.direct.redMask = 0xff;
				desiredFormat.direct.greenMask = 0xff;
				desiredFormat.direct.blueMask = 0xff;

				desiredFormat.direct.alpha = 24;
				desiredFormat.direct.red = 16;
				desiredFormat.direct.green = 8;
				desiredFormat.direct.blue = 0;

				pictFormat = xRenderFindFormat (display,
												PictFormatType | PictFormatDepth
												 | PictFormatRedMask | PictFormatRed
												 | PictFormatGreenMask | PictFormatGreen
												 | PictFormatBlueMask | PictFormatBlue
												 | PictFormatAlphaMask | PictFormatAlpha,
												&desiredFormat,
												0);
			}
		}

		return pictFormat;
	}
}
#endif

namespace Visuals
{
	static Visual* findVisualWithDepth (const int desiredDepth) throw()
	{
		ScopedXLock xlock;

		Visual* visual = 0;
		int numVisuals = 0;
		long desiredMask = VisualNoMask;
		XVisualInfo desiredVisual;

		desiredVisual.screen = DefaultScreen (display);
		desiredVisual.depth = desiredDepth;

		desiredMask = VisualScreenMask | VisualDepthMask;

		if (desiredDepth == 32)
		{
			desiredVisual.c_class = TrueColor;
			desiredVisual.red_mask = 0x00FF0000;
			desiredVisual.green_mask = 0x0000FF00;
			desiredVisual.blue_mask = 0x000000FF;
			desiredVisual.bits_per_rgb = 8;

			desiredMask |= VisualClassMask;
			desiredMask |= VisualRedMaskMask;
			desiredMask |= VisualGreenMaskMask;
			desiredMask |= VisualBlueMaskMask;
			desiredMask |= VisualBitsPerRGBMask;
		}

		XVisualInfo* xvinfos = XGetVisualInfo (display,
											   desiredMask,
											   &desiredVisual,
											   &numVisuals);

		if (xvinfos != 0)
		{
			for (int i = 0; i < numVisuals; i++)
			{
				if (xvinfos[i].depth == desiredDepth)
				{
					visual = xvinfos[i].visual;
					break;
				}
			}

			XFree (xvinfos);
		}

		return visual;
	}

	static Visual* findVisualFormat (const int desiredDepth, int& matchedDepth) throw()
	{
		Visual* visual = 0;

		if (desiredDepth == 32)
		{
#if JUCE_USE_XSHM
			if (XSHMHelpers::isShmAvailable())
			{
#if JUCE_USE_XRENDER
				if (XRender::isAvailable())
				{
					XRenderPictFormat* pictFormat = XRender::findPictureFormat();

					if (pictFormat != 0)
					{
						int numVisuals = 0;
						XVisualInfo desiredVisual;
						desiredVisual.screen = DefaultScreen (display);
						desiredVisual.depth = 32;
						desiredVisual.bits_per_rgb = 8;

						XVisualInfo* xvinfos = XGetVisualInfo (display,
															   VisualScreenMask | VisualDepthMask | VisualBitsPerRGBMask,
															   &desiredVisual, &numVisuals);
						if (xvinfos != 0)
						{
							for (int i = 0; i < numVisuals; ++i)
							{
								XRenderPictFormat* pictVisualFormat = XRender::xRenderFindVisualFormat (display, xvinfos[i].visual);

								if (pictVisualFormat != 0
									 && pictVisualFormat->type == PictTypeDirect
									 && pictVisualFormat->direct.alphaMask)
								{
									visual = xvinfos[i].visual;
									matchedDepth = 32;
									break;
								}
							}

							XFree (xvinfos);
						}
					}
				}
#endif
				if (visual == 0)
				{
					visual = findVisualWithDepth (32);
					if (visual != 0)
						matchedDepth = 32;
				}
			}
#endif
		}

		if (visual == 0 && desiredDepth >= 24)
		{
			visual = findVisualWithDepth (24);
			if (visual != 0)
				matchedDepth = 24;
		}

		if (visual == 0 && desiredDepth >= 16)
		{
			visual = findVisualWithDepth (16);
			if (visual != 0)
				matchedDepth = 16;
		}

		return visual;
	}
}

class XBitmapImage  : public Image
{
public:

	XBitmapImage (const PixelFormat format_, const int w, const int h,
				  const bool clearImage, const int imageDepth_, Visual* visual)
		: Image (format_, w, h),
		  imageDepth (imageDepth_),
		  gc (None)
	{
		jassert (format_ == RGB || format_ == ARGB);

		pixelStride = (format_ == RGB) ? 3 : 4;
		lineStride = ((w * pixelStride + 3) & ~3);

		ScopedXLock xlock;

#if JUCE_USE_XSHM
		usingXShm = false;

		if ((imageDepth > 16) && XSHMHelpers::isShmAvailable())
		{
			zerostruct (segmentInfo);

			segmentInfo.shmid = -1;
			segmentInfo.shmaddr = (char *) -1;
			segmentInfo.readOnly = False;

			xImage = XShmCreateImage (display, visual, imageDepth, ZPixmap, 0, &segmentInfo, w, h);

			if (xImage != 0)
			{
				if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
												 xImage->bytes_per_line * xImage->height,
												 IPC_CREAT | 0777)) >= 0)
				{
					if (segmentInfo.shmid != -1)
					{
						segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, 0, 0);

						if (segmentInfo.shmaddr != (void*) -1)
						{
							segmentInfo.readOnly = False;

							xImage->data = segmentInfo.shmaddr;
							imageData = (uint8*) segmentInfo.shmaddr;

							if (XShmAttach (display, &segmentInfo) != 0)
							{
								usingXShm = true;
							}
							else
							{
								jassertfalse
							}
						}
						else
						{
							shmctl (segmentInfo.shmid, IPC_RMID, 0);
						}
					}
				}
			}
		}

		if (! usingXShm)
#endif
		{
			imageDataAllocated.malloc (lineStride * h);
			imageData = imageDataAllocated;

			if (format_ == ARGB && clearImage)
				zeromem (imageData, h * lineStride);

			xImage = (XImage*) juce_calloc (sizeof (XImage));

			xImage->width = w;
			xImage->height = h;
			xImage->xoffset = 0;
			xImage->format = ZPixmap;
			xImage->data = (char*) imageData;
			xImage->byte_order = ImageByteOrder (display);
			xImage->bitmap_unit = BitmapUnit (display);
			xImage->bitmap_bit_order = BitmapBitOrder (display);
			xImage->bitmap_pad = 32;
			xImage->depth = pixelStride * 8;
			xImage->bytes_per_line = lineStride;
			xImage->bits_per_pixel = pixelStride * 8;
			xImage->red_mask   = 0x00FF0000;
			xImage->green_mask = 0x0000FF00;
			xImage->blue_mask  = 0x000000FF;

			if (imageDepth == 16)
			{
				const int pixelStride = 2;
				const int lineStride = ((w * pixelStride + 3) & ~3);

				imageData16Bit.malloc (lineStride * h);
				xImage->data = imageData16Bit;
				xImage->bitmap_pad = 16;
				xImage->depth = pixelStride * 8;
				xImage->bytes_per_line = lineStride;
				xImage->bits_per_pixel = pixelStride * 8;
				xImage->red_mask   = visual->red_mask;
				xImage->green_mask = visual->green_mask;
				xImage->blue_mask  = visual->blue_mask;
			}

			if (! XInitImage (xImage))
			{
				jassertfalse
			}
		}
	}

	~XBitmapImage()
	{
		ScopedXLock xlock;

		if (gc != None)
			XFreeGC (display, gc);

#if JUCE_USE_XSHM
		if (usingXShm)
		{
			XShmDetach (display, &segmentInfo);

			XFlush (display);
			XDestroyImage (xImage);

			shmdt (segmentInfo.shmaddr);
			shmctl (segmentInfo.shmid, IPC_RMID, 0);
		}
		else
#endif
		{
			xImage->data = 0;
			XDestroyImage (xImage);
		}
	}

	void blitToWindow (Window window, int dx, int dy, int dw, int dh, int sx, int sy)
	{
		ScopedXLock xlock;

		if (gc == None)
		{
			XGCValues gcvalues;
			gcvalues.foreground = None;
			gcvalues.background = None;
			gcvalues.function = GXcopy;
			gcvalues.plane_mask = AllPlanes;
			gcvalues.clip_mask = None;
			gcvalues.graphics_exposures = False;

			gc = XCreateGC (display, window,
							GCBackground | GCForeground | GCFunction | GCPlaneMask | GCClipMask | GCGraphicsExposures,
							&gcvalues);
		}

		if (imageDepth == 16)
		{
			const uint32 rMask = xImage->red_mask;
			const uint32 rShiftL = jmax (0, getShiftNeeded (rMask));
			const uint32 rShiftR = jmax (0, -getShiftNeeded (rMask));
			const uint32 gMask = xImage->green_mask;
			const uint32 gShiftL = jmax (0, getShiftNeeded (gMask));
			const uint32 gShiftR = jmax (0, -getShiftNeeded (gMask));
			const uint32 bMask = xImage->blue_mask;
			const uint32 bShiftL = jmax (0, getShiftNeeded (bMask));
			const uint32 bShiftR = jmax (0, -getShiftNeeded (bMask));

			const Image::BitmapData srcData (*this, 0, 0, getWidth(), getHeight());

			for (int y = sy; y < sy + dh; ++y)
			{
				const uint8* p = srcData.getPixelPointer (sx, y);

				for (int x = sx; x < sx + dw; ++x)
				{
					const PixelRGB* const pixel = (const PixelRGB*) p;
					p += srcData.pixelStride;

					XPutPixel (xImage, x, y,
							   (((((uint32) pixel->getRed()) << rShiftL) >> rShiftR) & rMask)
								 | (((((uint32) pixel->getGreen()) << gShiftL) >> gShiftR) & gMask)
								 | (((((uint32) pixel->getBlue()) << bShiftL) >> bShiftR) & bMask));
				}
			}
		}

		// blit results to screen.
#if JUCE_USE_XSHM
		if (usingXShm)
			XShmPutImage (display, (::Drawable) window, gc, xImage, sx, sy, dx, dy, dw, dh, True);
		else
#endif
			XPutImage (display, (::Drawable) window, gc, xImage, sx, sy, dx, dy, dw, dh);
	}

	juce_UseDebuggingNewOperator

private:
	XImage* xImage;
	const int imageDepth;
	HeapBlock <char> imageData16Bit;

	GC gc;

#if JUCE_USE_XSHM
	XShmSegmentInfo segmentInfo;
	bool usingXShm;
#endif

	static int getShiftNeeded (const uint32 mask) throw()
	{
		for (int i = 32; --i >= 0;)
			if (((mask >> i) & 1) != 0)
				return i - 7;

		jassertfalse
		return 0;
	}
};

class LinuxComponentPeer  : public ComponentPeer
{
public:

	LinuxComponentPeer (Component* const component, const int windowStyleFlags)
		: ComponentPeer (component, windowStyleFlags),
		  windowH (0),
		  parentWindow (0),
		  wx (0),
		  wy (0),
		  ww (0),
		  wh (0),
		  fullScreen (false),
		  mapped (false),
		  visual (0),
		  depth (0)
	{
		// it's dangerous to create a window on a thread other than the message thread..
		jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());

		repainter = new LinuxRepaintManager (this);

		createWindow();

		setTitle (component->getName());
	}

	~LinuxComponentPeer()
	{
		// it's dangerous to delete a window on a thread other than the message thread..
		jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());

		deleteIconPixmaps();

		destroyWindow();

		windowH = 0;
	}

	void* getNativeHandle() const
	{
		return (void*) windowH;
	}

	static LinuxComponentPeer* getPeerFor (Window windowHandle) throw()
	{
		XPointer peer = 0;

		ScopedXLock xlock;
		if (! XFindContext (display, (XID) windowHandle, improbableNumber, &peer))
		{
			if (peer != 0 && ! ComponentPeer::isValidPeer ((LinuxComponentPeer*) peer))
				peer = 0;
		}

		return (LinuxComponentPeer*) peer;
	}

	void setVisible (bool shouldBeVisible)
	{
		ScopedXLock xlock;
		if (shouldBeVisible)
			XMapWindow (display, windowH);
		else
			XUnmapWindow (display, windowH);
	}

	void setTitle (const String& title)
	{
		setWindowTitle (windowH, title);
	}

	void setPosition (int x, int y)
	{
		setBounds (x, y, ww, wh, false);
	}

	void setSize (int w, int h)
	{
		setBounds (wx, wy, w, h, false);
	}

	void setBounds (int x, int y, int w, int h, bool isNowFullScreen)
	{
		fullScreen = isNowFullScreen;

		if (windowH != 0)
		{
			Component::SafePointer<Component> deletionChecker (component);

			wx = x;
			wy = y;
			ww = jmax (1, w);
			wh = jmax (1, h);

			ScopedXLock xlock;

			// Make sure the Window manager does what we want
			XSizeHints* hints = XAllocSizeHints();
			hints->flags = USSize | USPosition;
			hints->width = ww;
			hints->height = wh;
			hints->x = wx;
			hints->y = wy;

			if ((getStyleFlags() & (windowHasTitleBar | windowIsResizable)) == windowHasTitleBar)
			{
				hints->min_width  = hints->max_width  = hints->width;
				hints->min_height = hints->max_height = hints->height;
				hints->flags |= PMinSize | PMaxSize;
			}

			XSetWMNormalHints (display, windowH, hints);
			XFree (hints);

			XMoveResizeWindow (display, windowH,
							   wx - windowBorder.getLeft(),
							   wy - windowBorder.getTop(), ww, wh);

			if (deletionChecker != 0)
			{
				updateBorderSize();
				handleMovedOrResized();
			}
		}
	}

	const Rectangle<int> getBounds() const	  { return Rectangle<int> (wx, wy, ww, wh); }
	const Point<int> getScreenPosition() const  { return Point<int> (wx, wy); }

	const Point<int> relativePositionToGlobal (const Point<int>& relativePosition)
	{
		return relativePosition + getScreenPosition();
	}

	const Point<int> globalPositionToRelative (const Point<int>& screenPosition)
	{
		return screenPosition - getScreenPosition();
	}

	void setMinimised (bool shouldBeMinimised)
	{
		if (shouldBeMinimised)
		{
			Window root = RootWindow (display, DefaultScreen (display));

			XClientMessageEvent clientMsg;
			clientMsg.display = display;
			clientMsg.window = windowH;
			clientMsg.type = ClientMessage;
			clientMsg.format = 32;
			clientMsg.message_type = Atoms::ChangeState;
			clientMsg.data.l[0] = IconicState;

			ScopedXLock xlock;
			XSendEvent (display, root, false,
						SubstructureRedirectMask | SubstructureNotifyMask,
						(XEvent*) &clientMsg);
		}
		else
		{
			setVisible (true);
		}
	}

	bool isMinimised() const
	{
		bool minimised = false;

		unsigned char* stateProp;
		unsigned long nitems, bytesLeft;
		Atom actualType;
		int actualFormat;

		ScopedXLock xlock;
		if (XGetWindowProperty (display, windowH, Atoms::State, 0, 64, False,
								Atoms::State, &actualType, &actualFormat, &nitems, &bytesLeft,
								&stateProp) == Success
			&& actualType == Atoms::State
			&& actualFormat == 32
			&& nitems > 0)
		{
			if (((unsigned long*) stateProp)[0] == IconicState)
				minimised = true;

			XFree (stateProp);
		}

		return minimised;
	}

	void setFullScreen (const bool shouldBeFullScreen)
	{
		Rectangle<int> r (lastNonFullscreenBounds); // (get a copy of this before de-minimising)

		setMinimised (false);

		if (fullScreen != shouldBeFullScreen)
		{
			if (shouldBeFullScreen)
				r = Desktop::getInstance().getMainMonitorArea();

			if (! r.isEmpty())
				setBounds (r.getX(), r.getY(), r.getWidth(), r.getHeight(), shouldBeFullScreen);

			getComponent()->repaint();
		}
	}

	bool isFullScreen() const
	{
		return fullScreen;
	}

	bool isChildWindowOf (Window possibleParent) const
	{
		Window* windowList = 0;
		uint32 windowListSize = 0;
		Window parent, root;

		ScopedXLock xlock;
		if (XQueryTree (display, windowH, &root, &parent, &windowList, &windowListSize) != 0)
		{
			if (windowList != 0)
				XFree (windowList);

			return parent == possibleParent;
		}

		return false;
	}

	bool isFrontWindow() const
	{
		Window* windowList = 0;
		uint32 windowListSize = 0;
		bool result = false;

		ScopedXLock xlock;
		Window parent, root = RootWindow (display, DefaultScreen (display));

		if (XQueryTree (display, root, &root, &parent, &windowList, &windowListSize) != 0)
		{
			for (int i = windowListSize; --i >= 0;)
			{
				LinuxComponentPeer* const peer = LinuxComponentPeer::getPeerFor (windowList[i]);

				if (peer != 0)
				{
					result = (peer == this);
					break;
				}
			}
		}

		if (windowList != 0)
			XFree (windowList);

		return result;
	}

	bool contains (const Point<int>& position, bool trueIfInAChildWindow) const
	{
		int x = position.getX();
		int y = position.getY();

		if (((unsigned int) x) >= (unsigned int) ww
			 || ((unsigned int) y) >= (unsigned int) wh)
			return false;

		bool inFront = false;

		for (int i = 0; i < Desktop::getInstance().getNumComponents(); ++i)
		{
			Component* const c = Desktop::getInstance().getComponent (i);

			if (inFront)
			{
				if (c->contains (x + wx - c->getScreenX(),
								 y + wy - c->getScreenY()))
				{
					return false;
				}
			}
			else if (c == getComponent())
			{
				inFront = true;
			}
		}

		if (trueIfInAChildWindow)
			return true;

		::Window root, child;
		unsigned int bw, depth;
		int wx, wy, w, h;

		ScopedXLock xlock;
		if (! XGetGeometry (display, (::Drawable) windowH, &root,
							&wx, &wy, (unsigned int*) &w, (unsigned int*) &h,
							&bw, &depth))
		{
			return false;
		}

		if (! XTranslateCoordinates (display, windowH, windowH, x, y, &wx, &wy, &child))
			return false;

		return child == None;
	}

	const BorderSize getFrameSize() const
	{
		return BorderSize();
	}

	bool setAlwaysOnTop (bool alwaysOnTop)
	{
		if (windowH != 0)
		{
			const bool wasVisible = component->isVisible();

			if (wasVisible)
				setVisible (false);  // doesn't always seem to work if the window is visible when this is done..

			{
				ScopedXLock xlock;
				XSetWindowAttributes swa;
				swa.override_redirect = alwaysOnTop ? True : False;

				XChangeWindowAttributes (display, windowH, CWOverrideRedirect, &swa);
			}

			if (wasVisible)
				setVisible (true);
		}

		return true;
	}

	void toFront (bool makeActive)
	{
		if (makeActive)
		{
			setVisible (true);
			grabFocus();
		}

		XEvent ev;
		ev.xclient.type = ClientMessage;
		ev.xclient.serial = 0;
		ev.xclient.send_event = True;
		ev.xclient.message_type = Atoms::ActiveWin;
		ev.xclient.window = windowH;
		ev.xclient.format = 32;
		ev.xclient.data.l[0] = 2;
		ev.xclient.data.l[1] = CurrentTime;
		ev.xclient.data.l[2] = 0;
		ev.xclient.data.l[3] = 0;
		ev.xclient.data.l[4] = 0;

		{
			ScopedXLock xlock;
			XSendEvent (display, RootWindow (display, DefaultScreen (display)),
						False,
						SubstructureRedirectMask | SubstructureNotifyMask,
						&ev);

			XWindowAttributes attr;
			XGetWindowAttributes (display, windowH, &attr);

			if (attr.override_redirect)
				XRaiseWindow (display, windowH);

			XSync (display, False);
		}

		handleBroughtToFront();
	}

	void toBehind (ComponentPeer* other)
	{
		LinuxComponentPeer* const otherPeer = dynamic_cast <LinuxComponentPeer*> (other);
		jassert (otherPeer != 0); // wrong type of window?

		if (otherPeer != 0)
		{
			setMinimised (false);

			Window newStack[] = { otherPeer->windowH, windowH };

			ScopedXLock xlock;
			XRestackWindows (display, newStack, 2);
		}
	}

	bool isFocused() const
	{
		int revert;
		Window focusedWindow = 0;
		ScopedXLock xlock;
		XGetInputFocus (display, &focusedWindow, &revert);

		return focusedWindow == windowH;
	}

	void grabFocus()
	{
		XWindowAttributes atts;
		ScopedXLock xlock;

		if (windowH != 0
			&& XGetWindowAttributes (display, windowH, &atts)
			&& atts.map_state == IsViewable
			&& ! isFocused())
		{
			XSetInputFocus (display, windowH, RevertToParent, CurrentTime);
			isActiveApplication = true;
		}
	}

	void textInputRequired (const Point<int>&)
	{
	}

	void repaint (int x, int y, int w, int h)
	{
		if (Rectangle<int>::intersectRectangles (x, y, w, h,
												 0, 0,
												 getComponent()->getWidth(),
												 getComponent()->getHeight()))
		{
			repainter->repaint (x, y, w, h);
		}
	}

	void performAnyPendingRepaintsNow()
	{
		repainter->performAnyPendingRepaintsNow();
	}

	static Pixmap juce_createColourPixmapFromImage (Display* display, const Image& image)
	{
		ScopedXLock xlock;

		const int width = image.getWidth();
		const int height = image.getHeight();
		HeapBlock <char> colour (width * height);
		int index = 0;

		for (int y = 0; y < height; ++y)
			for (int x = 0; x < width; ++x)
				colour[index++] = static_cast<char> (image.getPixelAt (x, y).getARGB());

		XImage* ximage = XCreateImage (display, CopyFromParent, 24, ZPixmap,
									   0, colour.getData(),
									   width, height, 32, 0);

		Pixmap pixmap = XCreatePixmap (display, DefaultRootWindow (display),
									   width, height, 24);

		GC gc = XCreateGC (display, pixmap, 0, 0);
		XPutImage (display, pixmap, gc, ximage, 0, 0, 0, 0, width, height);
		XFreeGC (display, gc);

		return pixmap;
	}

	static Pixmap juce_createMaskPixmapFromImage (Display* display, const Image& image)
	{
		ScopedXLock xlock;

		const int width = image.getWidth();
		const int height = image.getHeight();
		const int stride = (width + 7) >> 3;
		HeapBlock <char> mask;
		mask.calloc (stride * height);
		const bool msbfirst = (BitmapBitOrder (display) == MSBFirst);

		for (int y = 0; y < height; ++y)
		{
			for (int x = 0; x < width; ++x)
			{
				const char bit = (char) (1 << (msbfirst ? (7 - (x & 7)) : (x & 7)));
				const int offset = y * stride + (x >> 3);

				if (image.getPixelAt (x, y).getAlpha() >= 128)
					mask[offset] |= bit;
			}
		}

		return XCreatePixmapFromBitmapData (display, DefaultRootWindow (display),
											mask.getData(), width, height, 1, 0, 1);
	}

	void setIcon (const Image& newIcon)
	{
		const int dataSize = newIcon.getWidth() * newIcon.getHeight() + 2;
		HeapBlock <unsigned long> data (dataSize);

		int index = 0;
		data[index++] = newIcon.getWidth();
		data[index++] = newIcon.getHeight();

		for (int y = 0; y < newIcon.getHeight(); ++y)
			for (int x = 0; x < newIcon.getWidth(); ++x)
				data[index++] = newIcon.getPixelAt (x, y).getARGB();

		ScopedXLock xlock;
		XChangeProperty (display, windowH,
						 XInternAtom (display, "_NET_WM_ICON", False),
						 XA_CARDINAL, 32, PropModeReplace,
						 reinterpret_cast<unsigned char*> (data.getData()), dataSize);

		deleteIconPixmaps();

		XWMHints* wmHints = XGetWMHints (display, windowH);

		if (wmHints == 0)
			wmHints = XAllocWMHints();

		wmHints->flags |= IconPixmapHint | IconMaskHint;
		wmHints->icon_pixmap = juce_createColourPixmapFromImage (display, newIcon);
		wmHints->icon_mask = juce_createMaskPixmapFromImage (display, newIcon);

		XSetWMHints (display, windowH, wmHints);
		XFree (wmHints);

		XSync (display, False);
	}

	void deleteIconPixmaps()
	{
		ScopedXLock xlock;
		XWMHints* wmHints = XGetWMHints (display, windowH);

		if (wmHints != 0)
		{
			if ((wmHints->flags & IconPixmapHint) != 0)
			{
				wmHints->flags &= ~IconPixmapHint;
				XFreePixmap (display, wmHints->icon_pixmap);
			}

			if ((wmHints->flags & IconMaskHint) != 0)
			{
				wmHints->flags &= ~IconMaskHint;
				XFreePixmap (display, wmHints->icon_mask);
			}

			XSetWMHints (display, windowH, wmHints);
			XFree (wmHints);
		}
	}

	void handleWindowMessage (XEvent* event)
	{
		switch (event->xany.type)
		{
			case 2: // 'KeyPress'
			{
				ScopedXLock xlock;
				XKeyEvent* const keyEvent = (XKeyEvent*) &event->xkey;
				updateKeyStates (keyEvent->keycode, true);

				char utf8 [64];
				zeromem (utf8, sizeof (utf8));
				KeySym sym;

				{
					const char* oldLocale = ::setlocale (LC_ALL, 0);
					::setlocale (LC_ALL, "");
					XLookupString (keyEvent, utf8, sizeof (utf8), &sym, 0);
					::setlocale (LC_ALL, oldLocale);
				}

				const juce_wchar unicodeChar = String::fromUTF8 (utf8, sizeof (utf8) - 1) [0];
				int keyCode = (int) unicodeChar;

				if (keyCode < 0x20)
					keyCode = XKeycodeToKeysym (display, keyEvent->keycode, currentModifiers.isShiftDown() ? 1 : 0);

				const ModifierKeys oldMods (currentModifiers);
				bool keyPressed = false;

				const bool keyDownChange = (sym != NoSymbol) && ! updateKeyModifiersFromSym (sym, true);

				if ((sym & 0xff00) == 0xff00)
				{
					// Translate keypad
					if (sym == XK_KP_Divide)
						keyCode = XK_slash;
					else if (sym == XK_KP_Multiply)
						keyCode = XK_asterisk;
					else if (sym == XK_KP_Subtract)
						keyCode = XK_hyphen;
					else if (sym == XK_KP_Add)
						keyCode = XK_plus;
					else if (sym == XK_KP_Enter)
						keyCode = XK_Return;
					else if (sym == XK_KP_Decimal)
						keyCode = Keys::numLock ? XK_period : XK_Delete;
					else if (sym == XK_KP_0)
						keyCode = Keys::numLock ? XK_0 : XK_Insert;
					else if (sym == XK_KP_1)
						keyCode = Keys::numLock ? XK_1 : XK_End;
					else if (sym == XK_KP_2)
						keyCode = Keys::numLock ? XK_2 : XK_Down;
					else if (sym == XK_KP_3)
						keyCode = Keys::numLock ? XK_3 : XK_Page_Down;
					else if (sym == XK_KP_4)
						keyCode = Keys::numLock ? XK_4 : XK_Left;
					else if (sym == XK_KP_5)
						keyCode = XK_5;
					else if (sym == XK_KP_6)
						keyCode = Keys::numLock ? XK_6 : XK_Right;
					else if (sym == XK_KP_7)
						keyCode = Keys::numLock ? XK_7 : XK_Home;
					else if (sym == XK_KP_8)
						keyCode = Keys::numLock ? XK_8 : XK_Up;
					else if (sym == XK_KP_9)
						keyCode = Keys::numLock ? XK_9 : XK_Page_Up;

					switch (sym)
					{
						case XK_Left:
						case XK_Right:
						case XK_Up:
						case XK_Down:
						case XK_Page_Up:
						case XK_Page_Down:
						case XK_End:
						case XK_Home:
						case XK_Delete:
						case XK_Insert:
							keyPressed = true;
							keyCode = (sym & 0xff) | Keys::extendedKeyModifier;
							break;
						case XK_Tab:
						case XK_Return:
						case XK_Escape:
						case XK_BackSpace:
							keyPressed = true;
							keyCode &= 0xff;
							break;
						default:
						{
							if (sym >= XK_F1 && sym <= XK_F16)
							{
								keyPressed = true;
								keyCode = (sym & 0xff) | Keys::extendedKeyModifier;
							}
							break;
						}
					}
				}

				if (utf8[0] != 0 || ((sym & 0xff00) == 0 && sym >= 8))
					keyPressed = true;

				if (oldMods != currentModifiers)
					handleModifierKeysChange();

				if (keyDownChange)
					handleKeyUpOrDown (true);

				if (keyPressed)
					handleKeyPress (keyCode, unicodeChar);

				break;
			}

			case KeyRelease:
			{
				const XKeyEvent* const keyEvent = (const XKeyEvent*) &event->xkey;
				updateKeyStates (keyEvent->keycode, false);

				ScopedXLock xlock;
				KeySym sym = XKeycodeToKeysym (display, keyEvent->keycode, 0);

				const ModifierKeys oldMods (currentModifiers);
				const bool keyDownChange = (sym != NoSymbol) && ! updateKeyModifiersFromSym (sym, false);

				if (oldMods != currentModifiers)
					handleModifierKeysChange();

				if (keyDownChange)
					handleKeyUpOrDown (false);

				break;
			}

			case ButtonPress:
			{
				const XButtonPressedEvent* const buttonPressEvent = (const XButtonPressedEvent*) &event->xbutton;
				updateKeyModifiers (buttonPressEvent->state);

				bool buttonMsg = false;
				const int map = pointerMap [buttonPressEvent->button - Button1];

				if (map == Keys::WheelUp || map == Keys::WheelDown)
				{
					handleMouseWheel (0, Point<int> (buttonPressEvent->x, buttonPressEvent->y),
									  getEventTime (buttonPressEvent->time), 0, map == Keys::WheelDown ? -84.0f : 84.0f);
				}
				if (map == Keys::LeftButton)
				{
					currentModifiers = currentModifiers.withFlags (ModifierKeys::leftButtonModifier);
					buttonMsg = true;
				}
				else if (map == Keys::RightButton)
				{
					currentModifiers = currentModifiers.withFlags (ModifierKeys::rightButtonModifier);
					buttonMsg = true;
				}
				else if (map == Keys::MiddleButton)
				{
					currentModifiers = currentModifiers.withFlags (ModifierKeys::middleButtonModifier);
					buttonMsg = true;
				}

				if (buttonMsg)
				{
					toFront (true);

					handleMouseEvent (0, Point<int> (buttonPressEvent->x, buttonPressEvent->y), currentModifiers,
									  getEventTime (buttonPressEvent->time));
				}

				clearLastMousePos();
				break;
			}

			case ButtonRelease:
			{
				const XButtonReleasedEvent* const buttonRelEvent = (const XButtonReleasedEvent*) &event->xbutton;
				updateKeyModifiers (buttonRelEvent->state);

				const int map = pointerMap [buttonRelEvent->button - Button1];

				if (map == Keys::LeftButton)
					currentModifiers = currentModifiers.withoutFlags (ModifierKeys::leftButtonModifier);
				else if (map == Keys::RightButton)
					currentModifiers = currentModifiers.withoutFlags (ModifierKeys::rightButtonModifier);
				else if (map == Keys::MiddleButton)
					currentModifiers = currentModifiers.withoutFlags (ModifierKeys::middleButtonModifier);

				handleMouseEvent (0, Point<int> (buttonRelEvent->x, buttonRelEvent->y), currentModifiers,
								  getEventTime (buttonRelEvent->time));

				clearLastMousePos();
				break;
			}

			case MotionNotify:
			{
				const XPointerMovedEvent* const movedEvent = (const XPointerMovedEvent*) &event->xmotion;
				updateKeyModifiers (movedEvent->state);

				const Point<int> mousePos (Desktop::getMousePosition());

				if (lastMousePos != mousePos)
				{
					lastMousePos = mousePos;

					if (parentWindow != 0 && (styleFlags & windowHasTitleBar) == 0)
					{
						Window wRoot = 0, wParent = 0;

						{
							ScopedXLock xlock;
							unsigned int numChildren;
							Window* wChild = 0;
							XQueryTree (display, windowH, &wRoot, &wParent, &wChild, &numChildren);
						}

						if (wParent != 0
							 && wParent != windowH
							 && wParent != wRoot)
						{
							parentWindow = wParent;
							updateBounds();
						}
						else
						{
							parentWindow = 0;
						}
					}

					handleMouseEvent (0, mousePos - getScreenPosition(), currentModifiers, getEventTime (movedEvent->time));
				}

				break;
			}

			case EnterNotify:
			{
				clearLastMousePos();
				const XEnterWindowEvent* const enterEvent = (const XEnterWindowEvent*) &event->xcrossing;

				if (! currentModifiers.isAnyMouseButtonDown())
				{
					updateKeyModifiers (enterEvent->state);
					handleMouseEvent (0, Point<int> (enterEvent->x, enterEvent->y), currentModifiers, getEventTime (enterEvent->time));
				}

				break;
			}

			case LeaveNotify:
			{
				const XLeaveWindowEvent* const leaveEvent = (const XLeaveWindowEvent*) &event->xcrossing;

				// Suppress the normal leave if we've got a pointer grab, or if
				// it's a bogus one caused by clicking a mouse button when running
				// in a Window manager
				if (((! currentModifiers.isAnyMouseButtonDown()) && leaveEvent->mode == NotifyNormal)
					 || leaveEvent->mode == NotifyUngrab)
				{
					updateKeyModifiers (leaveEvent->state);
					handleMouseEvent (0, Point<int> (leaveEvent->x, leaveEvent->y), currentModifiers, getEventTime (leaveEvent->time));
				}

				break;
			}

			case FocusIn:
			{
				isActiveApplication = true;
				if (isFocused())
					handleFocusGain();

				break;
			}

			case FocusOut:
			{
				isActiveApplication = false;
				if (! isFocused())
					handleFocusLoss();

				break;
			}

			case Expose:
			{
				// Batch together all pending expose events
				XExposeEvent* exposeEvent = (XExposeEvent*) &event->xexpose;
				XEvent nextEvent;
				ScopedXLock xlock;

				if (exposeEvent->window != windowH)
				{
					Window child;
					XTranslateCoordinates (display, exposeEvent->window, windowH,
										   exposeEvent->x, exposeEvent->y, &exposeEvent->x, &exposeEvent->y,
										   &child);
				}

				repaint (exposeEvent->x, exposeEvent->y,
						 exposeEvent->width, exposeEvent->height);

				while (XEventsQueued (display, QueuedAfterFlush) > 0)
				{
					XPeekEvent (display, (XEvent*) &nextEvent);
					if (nextEvent.type != Expose || nextEvent.xany.window != event->xany.window)
						break;

					XNextEvent (display, (XEvent*) &nextEvent);
					XExposeEvent* nextExposeEvent = (XExposeEvent*) &nextEvent.xexpose;
					repaint (nextExposeEvent->x, nextExposeEvent->y,
							 nextExposeEvent->width, nextExposeEvent->height);
				}

				break;
			}

			case CirculateNotify:
			case CreateNotify:
			case DestroyNotify:
				// Think we can ignore these
				break;

			case ConfigureNotify:
			{
				updateBounds();
				updateBorderSize();
				handleMovedOrResized();

				// if the native title bar is dragged, need to tell any active menus, etc.
				if ((styleFlags & windowHasTitleBar) != 0
					  && component->isCurrentlyBlockedByAnotherModalComponent())
				{
					Component* const currentModalComp = Component::getCurrentlyModalComponent();

					if (currentModalComp != 0)
						currentModalComp->inputAttemptWhenModal();
				}

				XConfigureEvent* const confEvent = (XConfigureEvent*) &event->xconfigure;

				if (confEvent->window == windowH
					 && confEvent->above != 0
					 && isFrontWindow())
				{
					handleBroughtToFront();
				}

				break;
			}

			case ReparentNotify:
			{
				parentWindow = 0;
				Window wRoot = 0;
				Window* wChild = 0;
				unsigned int numChildren;

				{
					ScopedXLock xlock;
					XQueryTree (display, windowH, &wRoot, &parentWindow, &wChild, &numChildren);
				}

				if (parentWindow == windowH || parentWindow == wRoot)
					parentWindow = 0;

				updateBounds();
				updateBorderSize();
				handleMovedOrResized();
				break;
			}

			case GravityNotify:
			{
				updateBounds();
				updateBorderSize();
				handleMovedOrResized();
				break;
			}

			case MapNotify:
				mapped = true;
				handleBroughtToFront();
				break;

			case UnmapNotify:
				mapped = false;
				break;

			case MappingNotify:
			{
				XMappingEvent* mappingEvent = (XMappingEvent*) &event->xmapping;

				if (mappingEvent->request != MappingPointer)
				{
					// Deal with modifier/keyboard mapping
					ScopedXLock xlock;
					XRefreshKeyboardMapping (mappingEvent);
					updateModifierMappings();
				}

				break;
			}

			case ClientMessage:
			{
				const XClientMessageEvent* const clientMsg = (const XClientMessageEvent*) &event->xclient;

				if (clientMsg->message_type == Atoms::Protocols && clientMsg->format == 32)
				{
					const Atom atom = (Atom) clientMsg->data.l[0];

					if (atom == Atoms::ProtocolList [Atoms::PING])
					{
						Window root = RootWindow (display, DefaultScreen (display));

						event->xclient.window = root;

						XSendEvent (display, root, False, NoEventMask, event);
						XFlush (display);
					}
					else if (atom == Atoms::ProtocolList [Atoms::TAKE_FOCUS])
					{
						XWindowAttributes atts;

						ScopedXLock xlock;
						if (clientMsg->window != 0
							 && XGetWindowAttributes (display, clientMsg->window, &atts))
						{
							if (atts.map_state == IsViewable)
								XSetInputFocus (display, clientMsg->window, RevertToParent, clientMsg->data.l[1]);
						}
					}
					else if (atom == Atoms::ProtocolList [Atoms::DELETE_WINDOW])
					{
						handleUserClosingWindow();
					}
				}
				else if (clientMsg->message_type == Atoms::XdndEnter)
				{
					handleDragAndDropEnter (clientMsg);
				}
				else if (clientMsg->message_type == Atoms::XdndLeave)
				{
					resetDragAndDrop();
				}
				else if (clientMsg->message_type == Atoms::XdndPosition)
				{
					handleDragAndDropPosition (clientMsg);
				}
				else if (clientMsg->message_type == Atoms::XdndDrop)
				{
					handleDragAndDropDrop (clientMsg);
				}
				else if (clientMsg->message_type == Atoms::XdndStatus)
				{
					handleDragAndDropStatus (clientMsg);
				}
				else if (clientMsg->message_type == Atoms::XdndFinished)
				{
					resetDragAndDrop();
				}

				break;
			}

			case SelectionNotify:
				handleDragAndDropSelection (event);
				break;

			case SelectionClear:
			case SelectionRequest:
				break;

			default:
#if JUCE_USE_XSHM
			{
				ScopedXLock xlock;
				if (event->xany.type == XShmGetEventBase (display))
					repainter->notifyPaintCompleted();
			}
#endif
				break;
		}
	}

	void showMouseCursor (Cursor cursor) throw()
	{
		ScopedXLock xlock;
		XDefineCursor (display, windowH, cursor);
	}

	void setTaskBarIcon (const Image& image)
	{
		ScopedXLock xlock;
		taskbarImage = image.createCopy();

		Screen* const screen = XDefaultScreenOfDisplay (display);
		const int screenNumber = XScreenNumberOfScreen (screen);

		String screenAtom ("_NET_SYSTEM_TRAY_S");
		screenAtom << screenNumber;
		Atom selectionAtom = XInternAtom (display, screenAtom.toUTF8(), false);

		XGrabServer (display);
		Window managerWin = XGetSelectionOwner (display, selectionAtom);

		if (managerWin != None)
			XSelectInput (display, managerWin, StructureNotifyMask);

		XUngrabServer (display);
		XFlush (display);

		if (managerWin != None)
		{
			XEvent ev;
			zerostruct (ev);
			ev.xclient.type = ClientMessage;
			ev.xclient.window = managerWin;
			ev.xclient.message_type = XInternAtom (display, "_NET_SYSTEM_TRAY_OPCODE", False);
			ev.xclient.format = 32;
			ev.xclient.data.l[0] = CurrentTime;
			ev.xclient.data.l[1] = 0 /*SYSTEM_TRAY_REQUEST_DOCK*/;
			ev.xclient.data.l[2] = windowH;
			ev.xclient.data.l[3] = 0;
			ev.xclient.data.l[4] = 0;

			XSendEvent (display, managerWin, False, NoEventMask, &ev);
			XSync (display, False);
		}

		// For older KDE's ...
		long atomData = 1;
		Atom trayAtom = XInternAtom (display, "KWM_DOCKWINDOW", false);
		XChangeProperty (display, windowH, trayAtom, trayAtom, 32, PropModeReplace, (unsigned char*) &atomData, 1);

		// For more recent KDE's...
		trayAtom = XInternAtom (display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", false);
		XChangeProperty (display, windowH, trayAtom, XA_WINDOW, 32, PropModeReplace, (unsigned char*) &windowH, 1);

		// a minimum size must be specified for GNOME and Xfce, otherwise the icon is displayed with a width of 1
		XSizeHints* hints = XAllocSizeHints();
		hints->flags = PMinSize;
		hints->min_width = 22;
		hints->min_height = 22;
		XSetWMNormalHints (display, windowH, hints);
		XFree (hints);
	}

	const Image* getTaskbarIcon() const throw()	   { return taskbarImage; }

	juce_UseDebuggingNewOperator

	bool dontRepaint;

	static ModifierKeys currentModifiers;
	static bool isActiveApplication;

private:

	class LinuxRepaintManager : public Timer
	{
	public:
		LinuxRepaintManager (LinuxComponentPeer* const peer_)
			: peer (peer_),
			  lastTimeImageUsed (0)
		{
#if JUCE_USE_XSHM
			shmCompletedDrawing = true;

			useARGBImagesForRendering = XSHMHelpers::isShmAvailable();

			if (useARGBImagesForRendering)
			{
				ScopedXLock xlock;
				XShmSegmentInfo segmentinfo;

				XImage* const testImage
					= XShmCreateImage (display, DefaultVisual (display, DefaultScreen (display)),
									   24, ZPixmap, 0, &segmentinfo, 64, 64);

				useARGBImagesForRendering = (testImage->bits_per_pixel == 32);
				XDestroyImage (testImage);
			}
#endif
		}

		~LinuxRepaintManager()
		{
		}

		void timerCallback()
		{
#if JUCE_USE_XSHM
			if (! shmCompletedDrawing)
				return;
#endif
			if (! regionsNeedingRepaint.isEmpty())
			{
				stopTimer();
				performAnyPendingRepaintsNow();
			}
			else if (Time::getApproximateMillisecondCounter() > lastTimeImageUsed + 3000)
			{
				stopTimer();
				image = 0;
			}
		}

		void repaint (int x, int y, int w, int h)
		{
			if (! isTimerRunning())
				startTimer (repaintTimerPeriod);

			regionsNeedingRepaint.add (x, y, w, h);
		}

		void performAnyPendingRepaintsNow()
		{
#if JUCE_USE_XSHM
			if (! shmCompletedDrawing)
			{
				startTimer (repaintTimerPeriod);
				return;
			}
#endif

			peer->clearMaskedRegion();

			RectangleList originalRepaintRegion (regionsNeedingRepaint);
			regionsNeedingRepaint.clear();
			const Rectangle<int> totalArea (originalRepaintRegion.getBounds());

			if (! totalArea.isEmpty())
			{
				if (image == 0 || image->getWidth() < totalArea.getWidth()
					 || image->getHeight() < totalArea.getHeight())
				{
#if JUCE_USE_XSHM
					image = new XBitmapImage (useARGBImagesForRendering ? Image::ARGB
																		: Image::RGB,
#else
					image = new XBitmapImage (Image::RGB,
#endif
											  (totalArea.getWidth() + 31) & ~31,
											  (totalArea.getHeight() + 31) & ~31,
											  false,
											  peer->depth,
											  peer->visual);
				}

				startTimer (repaintTimerPeriod);

				LowLevelGraphicsSoftwareRenderer context (*image);

				context.setOrigin (-totalArea.getX(), -totalArea.getY());

				if (context.clipToRectangleList (originalRepaintRegion))
				{
					if (peer->depth == 32)
					{
						RectangleList::Iterator i (originalRepaintRegion);

						while (i.next())
						{
							const Rectangle<int>& r = *i.getRectangle();
							image->clear (r.getX() - totalArea.getX(), r.getY() - totalArea.getY(), r.getWidth(), r.getHeight());
						}
					}

					peer->handlePaint (context);
				}

				if (! peer->maskedRegion.isEmpty())
					originalRepaintRegion.subtract (peer->maskedRegion);

				for (RectangleList::Iterator i (originalRepaintRegion); i.next();)
				{
#if JUCE_USE_XSHM
					shmCompletedDrawing = false;
#endif
					const Rectangle<int>& r = *i.getRectangle();

					image->blitToWindow (peer->windowH,
										 r.getX(), r.getY(), r.getWidth(), r.getHeight(),
										 r.getX() - totalArea.getX(), r.getY() - totalArea.getY());
				}
			}

			lastTimeImageUsed = Time::getApproximateMillisecondCounter();
			startTimer (repaintTimerPeriod);
		}

#if JUCE_USE_XSHM
		void notifyPaintCompleted()		 { shmCompletedDrawing = true; }
#endif

	private:
		enum { repaintTimerPeriod = 1000 / 100 };

		LinuxComponentPeer* const peer;
		ScopedPointer <XBitmapImage> image;
		uint32 lastTimeImageUsed;
		RectangleList regionsNeedingRepaint;

#if JUCE_USE_XSHM
		bool useARGBImagesForRendering, shmCompletedDrawing;
#endif
		LinuxRepaintManager (const LinuxRepaintManager&);
		LinuxRepaintManager& operator= (const LinuxRepaintManager&);
	};

	ScopedPointer <LinuxRepaintManager> repainter;

	friend class LinuxRepaintManager;
	Window windowH, parentWindow;
	int wx, wy, ww, wh;
	ScopedPointer<Image> taskbarImage;
	bool fullScreen, mapped;
	Visual* visual;
	int depth;
	BorderSize windowBorder;

	struct MotifWmHints
	{
		unsigned long flags;
		unsigned long functions;
		unsigned long decorations;
		long input_mode;
		unsigned long status;
	};

	static void updateKeyStates (const int keycode, const bool press) throw()
	{
		const int keybyte = keycode >> 3;
		const int keybit = (1 << (keycode & 7));

		if (press)
			Keys::keyStates [keybyte] |= keybit;
		else
			Keys::keyStates [keybyte] &= ~keybit;
	}

	static void updateKeyModifiers (const int status) throw()
	{
		int keyMods = 0;

		if (status & ShiftMask)	 keyMods |= ModifierKeys::shiftModifier;
		if (status & ControlMask)   keyMods |= ModifierKeys::ctrlModifier;
		if (status & Keys::AltMask)	   keyMods |= ModifierKeys::altModifier;

		currentModifiers = currentModifiers.withOnlyMouseButtons().withFlags (keyMods);

		Keys::numLock  = ((status & Keys::NumLockMask) != 0);
		Keys::capsLock = ((status & LockMask) != 0);
	}

	static bool updateKeyModifiersFromSym (KeySym sym, const bool press) throw()
	{
		int modifier = 0;
		bool isModifier = true;

		switch (sym)
		{
			case XK_Shift_L:
			case XK_Shift_R:
				modifier = ModifierKeys::shiftModifier;
				break;

			case XK_Control_L:
			case XK_Control_R:
				modifier = ModifierKeys::ctrlModifier;
				break;

			case XK_Alt_L:
			case XK_Alt_R:
				modifier = ModifierKeys::altModifier;
				break;

			case XK_Num_Lock:
				if (press)
					Keys::numLock = ! Keys::numLock;

				break;

			case XK_Caps_Lock:
				if (press)
					Keys::capsLock = ! Keys::capsLock;

				break;

			case XK_Scroll_Lock:
				break;

			default:
				isModifier = false;
				break;
		}

		if (modifier != 0)
		{
			if (press)
				currentModifiers = currentModifiers.withFlags (modifier);
			else
				currentModifiers = currentModifiers.withoutFlags (modifier);
		}

		return isModifier;
	}

	// Alt and Num lock are not defined by standard X
	// modifier constants: check what they're mapped to
	static void updateModifierMappings() throw()
	{
		ScopedXLock xlock;
		const int altLeftCode = XKeysymToKeycode (display, XK_Alt_L);
		const int numLockCode = XKeysymToKeycode (display, XK_Num_Lock);

		Keys::AltMask = 0;
		Keys::NumLockMask = 0;

		XModifierKeymap* mapping = XGetModifierMapping (display);

		if (mapping)
		{
			for (int i = 0; i < 8; i++)
			{
				if (mapping->modifiermap [i << 1] == altLeftCode)
					Keys::AltMask = 1 << i;
				else if (mapping->modifiermap [i << 1] == numLockCode)
					Keys::NumLockMask = 1 << i;
			}

			XFreeModifiermap (mapping);
		}
	}

	void removeWindowDecorations (Window wndH)
	{
		Atom hints = XInternAtom (display, "_MOTIF_WM_HINTS", True);

		if (hints != None)
		{
			MotifWmHints motifHints;
			zerostruct (motifHints);
			motifHints.flags = 2; /* MWM_HINTS_DECORATIONS */
			motifHints.decorations = 0;

			ScopedXLock xlock;
			XChangeProperty (display, wndH, hints, hints, 32, PropModeReplace,
							 (unsigned char*) &motifHints, 4);
		}

		hints = XInternAtom (display, "_WIN_HINTS", True);

		if (hints != None)
		{
			long gnomeHints = 0;

			ScopedXLock xlock;
			XChangeProperty (display, wndH, hints, hints, 32, PropModeReplace,
							 (unsigned char*) &gnomeHints, 1);
		}

		hints = XInternAtom (display, "KWM_WIN_DECORATION", True);

		if (hints != None)
		{
			long kwmHints = 2; /*KDE_tinyDecoration*/

			ScopedXLock xlock;
			XChangeProperty (display, wndH, hints, hints, 32, PropModeReplace,
							 (unsigned char*) &kwmHints, 1);
		}
	}

	void addWindowButtons (Window wndH)
	{
		ScopedXLock xlock;
		Atom hints = XInternAtom (display, "_MOTIF_WM_HINTS", True);

		if (hints != None)
		{
			MotifWmHints motifHints;
			zerostruct (motifHints);

			motifHints.flags = 1 | 2; /* MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS */
			motifHints.decorations = 2 /* MWM_DECOR_BORDER */ | 8 /* MWM_DECOR_TITLE */ | 16; /* MWM_DECOR_MENU */

			motifHints.functions = 4 /* MWM_FUNC_MOVE */;

			if ((styleFlags & windowHasCloseButton) != 0)
				motifHints.functions |= 32; /* MWM_FUNC_CLOSE */

			if ((styleFlags & windowHasMinimiseButton) != 0)
			{
				motifHints.functions |= 8; /* MWM_FUNC_MINIMIZE */
				motifHints.decorations |= 0x20; /* MWM_DECOR_MINIMIZE */
			}

			if ((styleFlags & windowHasMaximiseButton) != 0)
			{
				motifHints.functions |= 0x10; /* MWM_FUNC_MAXIMIZE */
				motifHints.decorations |= 0x40; /* MWM_DECOR_MAXIMIZE */
			}

			if ((styleFlags & windowIsResizable) != 0)
			{
				motifHints.functions |= 2; /* MWM_FUNC_RESIZE */
				motifHints.decorations |= 0x4; /* MWM_DECOR_RESIZEH */
			}

			XChangeProperty (display, wndH, hints, hints, 32, 0, (unsigned char*) &motifHints, 5);
		}

		hints = XInternAtom (display, "_NET_WM_ALLOWED_ACTIONS", True);

		if (hints != None)
		{
			int netHints [6];
			int num = 0;

			netHints [num++] = XInternAtom (display, "_NET_WM_ACTION_RESIZE", (styleFlags & windowIsResizable) ? True : False);
			netHints [num++] = XInternAtom (display, "_NET_WM_ACTION_FULLSCREEN", (styleFlags & windowHasMaximiseButton) ? True : False);
			netHints [num++] = XInternAtom (display, "_NET_WM_ACTION_MINIMIZE", (styleFlags & windowHasMinimiseButton) ? True : False);
			netHints [num++] = XInternAtom (display, "_NET_WM_ACTION_CLOSE", (styleFlags & windowHasCloseButton) ? True : False);

			XChangeProperty (display, wndH, hints, XA_ATOM, 32, PropModeReplace,
							 (unsigned char*) &netHints, num);
		}
	}

	void setWindowType (Window wndH)
	{
		int netHints [2];
		int numHints = 0;

		if ((styleFlags & windowIsTemporary) != 0
			 || ((styleFlags & windowHasDropShadow) == 0 && Desktop::canUseSemiTransparentWindows()))
		{
			netHints [numHints] = XInternAtom (display, "_NET_WM_WINDOW_TYPE_COMBO", True);
		}
		else
		{
			netHints [numHints] = XInternAtom (display, "_NET_WM_WINDOW_TYPE_NORMAL", True);
		}

		if (netHints [numHints] != 0)
			++numHints;

		netHints[numHints] = XInternAtom (display, "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE", True);

		if (netHints [numHints] != 0)
			++numHints;

		XChangeProperty (display, wndH, Atoms::WindowType, XA_ATOM, 32, PropModeReplace,
						 (unsigned char*) &netHints, numHints);

		if ((styleFlags & windowAppearsOnTaskbar) == 0)
		{
			Atom skipTaskbar = XInternAtom (display, "_NET_WM_STATE_SKIP_TASKBAR", False);

			XChangeProperty (display, wndH, Atoms::WindowState, XA_ATOM, 32, PropModeReplace,
							 (unsigned char*) &skipTaskbar, 1);
		}
	}

	void createWindow()
	{
		ScopedXLock xlock;
		Atoms::initialiseAtoms();
		resetDragAndDrop();

		// Get defaults for various properties
		const int screen = DefaultScreen (display);
		Window root = RootWindow (display, screen);

		// Try to obtain a 32-bit visual or fallback to 24 or 16
		visual = Visuals::findVisualFormat ((styleFlags & windowIsSemiTransparent) ? 32 : 24, depth);

		if (visual == 0)
		{
			Logger::outputDebugString ("ERROR: System doesn't support 32, 24 or 16 bit RGB display.\n");
			Process::terminate();
		}

		// Create and install a colormap suitable fr our visual
		Colormap colormap = XCreateColormap (display, root, visual, AllocNone);
		XInstallColormap (display, colormap);

		// Set up the window attributes
		XSetWindowAttributes swa;
		swa.border_pixel = 0;
		swa.background_pixmap = None;
		swa.colormap = colormap;
		swa.override_redirect = getComponent()->isAlwaysOnTop() ? True : False;
		swa.event_mask = getAllEventsMask();

		Window wndH = XCreateWindow (display, root,
									 0, 0, 1, 1,
									 0, depth, InputOutput, visual,
									 CWBorderPixel | CWColormap | CWBackPixmap | CWEventMask | CWOverrideRedirect,
									 &swa);

		XGrabButton (display, AnyButton, AnyModifier, wndH, False,
					 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
					 GrabModeAsync, GrabModeAsync, None, None);

		// Set the window context to identify the window handle object
		if (XSaveContext (display, (XID) wndH, improbableNumber, (XPointer) this))
		{
			// Failed
			jassertfalse
			Logger::outputDebugString ("Failed to create context information for window.\n");
			XDestroyWindow (display, wndH);
			wndH = 0;
		}

		// Set window manager hints
		XWMHints* wmHints = XAllocWMHints();
		wmHints->flags = InputHint | StateHint;
		wmHints->input = True;	  // Locally active input model
		wmHints->initial_state = NormalState;
		XSetWMHints (display, wndH, wmHints);
		XFree (wmHints);

		// Set the window type
		setWindowType (wndH);

		// Define decoration
		if ((styleFlags & windowHasTitleBar) == 0)
			removeWindowDecorations (wndH);
		else
			addWindowButtons (wndH);

		// Set window name
		setWindowTitle (wndH, getComponent()->getName());

		// Associate the PID, allowing to be shut down when something goes wrong
		unsigned long pid = getpid();
		XChangeProperty (display, wndH, Atoms::Pid, XA_CARDINAL, 32, PropModeReplace,
						 (unsigned char*) &pid, 1);

		// Set window manager protocols
		XChangeProperty (display, wndH, Atoms::Protocols, XA_ATOM, 32, PropModeReplace,
						 (unsigned char*) Atoms::ProtocolList, 2);

		// Set drag and drop flags
		XChangeProperty (display, wndH, Atoms::XdndTypeList, XA_ATOM, 32, PropModeReplace,
						 (const unsigned char*) Atoms::allowedMimeTypes, numElementsInArray (Atoms::allowedMimeTypes));

		XChangeProperty (display, wndH, Atoms::XdndActionList, XA_ATOM, 32, PropModeReplace,
						 (const unsigned char*) Atoms::allowedActions, numElementsInArray (Atoms::allowedActions));

		XChangeProperty (display, wndH, Atoms::XdndActionDescription, XA_STRING, 8, PropModeReplace,
						 (const unsigned char*) "", 0);

		unsigned long dndVersion = Atoms::DndVersion;
		XChangeProperty (display, wndH, Atoms::XdndAware, XA_ATOM, 32, PropModeReplace,
						 (const unsigned char*) &dndVersion, 1);

		// Initialise the pointer and keyboard mapping
		// This is not the same as the logical pointer mapping the X server uses:
		// we don't mess with this.
		static bool mappingInitialised = false;

		if (! mappingInitialised)
		{
			mappingInitialised = true;

			const int numButtons = XGetPointerMapping (display, 0, 0);

			if (numButtons == 2)
			{
				pointerMap[0] = Keys::LeftButton;
				pointerMap[1] = Keys::RightButton;
				pointerMap[2] = pointerMap[3] = pointerMap[4] = Keys::NoButton;
			}
			else if (numButtons >= 3)
			{
				pointerMap[0] = Keys::LeftButton;
				pointerMap[1] = Keys::MiddleButton;
				pointerMap[2] = Keys::RightButton;

				if (numButtons >= 5)
				{
					pointerMap[3] = Keys::WheelUp;
					pointerMap[4] = Keys::WheelDown;
				}
			}

			updateModifierMappings();
		}

		windowH = wndH;
	}

	void destroyWindow()
	{
		ScopedXLock xlock;

		XPointer handlePointer;
		if (! XFindContext (display, (XID) windowH, improbableNumber, &handlePointer))
			XDeleteContext (display, (XID) windowH, improbableNumber);

		XDestroyWindow (display, windowH);

		// Wait for it to complete and then remove any events for this
		// window from the event queue.
		XSync (display, false);

		XEvent event;
		while (XCheckWindowEvent (display, windowH, getAllEventsMask(), &event) == True)
		{}
	}

	static int getAllEventsMask() throw()
	{
		return NoEventMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask
				 | EnterWindowMask | LeaveWindowMask | PointerMotionMask | KeymapStateMask
				 | ExposureMask | StructureNotifyMask | FocusChangeMask;
	}

	static int64 getEventTime (::Time t)
	{
		static int64 eventTimeOffset = 0x12345678;
		const int64 thisMessageTime = t;

		if (eventTimeOffset == 0x12345678)
			eventTimeOffset = Time::currentTimeMillis() - thisMessageTime;

		return eventTimeOffset + thisMessageTime;
	}

	static void setWindowTitle (Window xwin, const String& title)
	{
		XTextProperty nameProperty;
		char* strings[] = { const_cast <char*> (title.toUTF8()) };
		ScopedXLock xlock;

		if (XStringListToTextProperty (strings, 1, &nameProperty))
		{
			XSetWMName (display, xwin, &nameProperty);
			XSetWMIconName (display, xwin, &nameProperty);

			XFree (nameProperty.value);
		}
	}

	void updateBorderSize()
	{
		if ((styleFlags & windowHasTitleBar) == 0)
		{
			windowBorder = BorderSize (0);
		}
		else if (windowBorder.getTopAndBottom() == 0 && windowBorder.getLeftAndRight() == 0)
		{
			ScopedXLock xlock;
			Atom hints = XInternAtom (display, "_NET_FRAME_EXTENTS", True);

			if (hints != None)
			{
				unsigned char* data = 0;
				unsigned long nitems, bytesLeft;
				Atom actualType;
				int actualFormat;

				if (XGetWindowProperty (display, windowH, hints, 0, 4, False,
										XA_CARDINAL, &actualType, &actualFormat, &nitems, &bytesLeft,
										&data) == Success)
				{
					const unsigned long* const sizes = (const unsigned long*) data;

					if (actualFormat == 32)
						windowBorder = BorderSize ((int) sizes[2], (int) sizes[0],
												   (int) sizes[3], (int) sizes[1]);

					XFree (data);
				}
			}
		}
	}

	void updateBounds()
	{
		jassert (windowH != 0);
		if (windowH != 0)
		{
			Window root, child;
			unsigned int bw, depth;
			ScopedXLock xlock;

			if (! XGetGeometry (display, (::Drawable) windowH, &root,
								&wx, &wy, (unsigned int*) &ww, (unsigned int*) &wh,
								&bw, &depth))
			{
				wx = wy = ww = wh = 0;
			}
			else if (! XTranslateCoordinates (display, windowH, root, 0, 0, &wx, &wy, &child))
			{
				wx = wy = 0;
			}
		}
	}

	void resetDragAndDrop()
	{
		dragAndDropFiles.clear();
		lastDropPos = Point<int> (-1, -1);
		dragAndDropCurrentMimeType = 0;
		dragAndDropSourceWindow = 0;
		srcMimeTypeAtomList.clear();
	}

	void sendDragAndDropMessage (XClientMessageEvent& msg)
	{
		msg.type = ClientMessage;
		msg.display = display;
		msg.window = dragAndDropSourceWindow;
		msg.format = 32;
		msg.data.l[0] = windowH;

		ScopedXLock xlock;
		XSendEvent (display, dragAndDropSourceWindow, False, 0, (XEvent*) &msg);
	}

	void sendDragAndDropStatus (const bool acceptDrop, Atom dropAction)
	{
		XClientMessageEvent msg;
		zerostruct (msg);
		msg.message_type = Atoms::XdndStatus;
		msg.data.l[1] = (acceptDrop ? 1 : 0) | 2; // 2 indicates that we want to receive position messages
		msg.data.l[4] = dropAction;

		sendDragAndDropMessage (msg);
	}

	void sendDragAndDropLeave()
	{
		XClientMessageEvent msg;
		zerostruct (msg);
		msg.message_type = Atoms::XdndLeave;
		sendDragAndDropMessage (msg);
	}

	void sendDragAndDropFinish()
	{
		XClientMessageEvent msg;
		zerostruct (msg);
		msg.message_type = Atoms::XdndFinished;
		sendDragAndDropMessage (msg);
	}

	void handleDragAndDropStatus (const XClientMessageEvent* const clientMsg)
	{
		if ((clientMsg->data.l[1] & 1) == 0)
		{
			sendDragAndDropLeave();

			if (dragAndDropFiles.size() > 0)
				handleFileDragExit (dragAndDropFiles);

			dragAndDropFiles.clear();
		}
	}

	void handleDragAndDropPosition (const XClientMessageEvent* const clientMsg)
	{
		if (dragAndDropSourceWindow == 0)
			return;

		dragAndDropSourceWindow = clientMsg->data.l[0];

		Point<int> dropPos ((int) clientMsg->data.l[2] >> 16,
							(int) clientMsg->data.l[2] & 0xffff);
		dropPos -= getScreenPosition();

		if (lastDropPos != dropPos)
		{
			lastDropPos = dropPos;
			dragAndDropTimestamp = clientMsg->data.l[3];

			Atom targetAction = Atoms::XdndActionCopy;

			for (int i = numElementsInArray (Atoms::allowedActions); --i >= 0;)
			{
				if ((Atom) clientMsg->data.l[4] == Atoms::allowedActions[i])
				{
					targetAction = Atoms::allowedActions[i];
					break;
				}
			}

			sendDragAndDropStatus (true, targetAction);

			if (dragAndDropFiles.size() == 0)
				updateDraggedFileList (clientMsg);

			if (dragAndDropFiles.size() > 0)
				handleFileDragMove (dragAndDropFiles, dropPos);
		}
	}

	void handleDragAndDropDrop (const XClientMessageEvent* const clientMsg)
	{
		if (dragAndDropFiles.size() == 0)
			updateDraggedFileList (clientMsg);

		const StringArray files (dragAndDropFiles);
		const Point<int> lastPos (lastDropPos);

		sendDragAndDropFinish();
		resetDragAndDrop();

		if (files.size() > 0)
			handleFileDragDrop (files, lastPos);
	}

	void handleDragAndDropEnter (const XClientMessageEvent* const clientMsg)
	{
		dragAndDropFiles.clear();
		srcMimeTypeAtomList.clear();

		dragAndDropCurrentMimeType = 0;
		const unsigned long dndCurrentVersion = static_cast <unsigned long> (clientMsg->data.l[1] & 0xff000000) >> 24;

		if (dndCurrentVersion < 3 || dndCurrentVersion > Atoms::DndVersion)
		{
			dragAndDropSourceWindow = 0;
			return;
		}

		dragAndDropSourceWindow = clientMsg->data.l[0];

		if ((clientMsg->data.l[1] & 1) != 0)
		{
			Atom actual;
			int format;
			unsigned long count = 0, remaining = 0;
			unsigned char* data = 0;

			ScopedXLock xlock;
			XGetWindowProperty (display, dragAndDropSourceWindow, Atoms::XdndTypeList,
								0, 0x8000000L, False, XA_ATOM, &actual, &format,
								&count, &remaining, &data);

			if (data != 0)
			{
				if (actual == XA_ATOM && format == 32 && count != 0)
				{
					const unsigned long* const types = (const unsigned long*) data;

					for (unsigned int i = 0; i < count; ++i)
						if (types[i] != None)
							srcMimeTypeAtomList.add (types[i]);
				}

				XFree (data);
			}
		}

		if (srcMimeTypeAtomList.size() == 0)
		{
			for (int i = 2; i < 5; ++i)
				if (clientMsg->data.l[i] != None)
					srcMimeTypeAtomList.add (clientMsg->data.l[i]);

			if (srcMimeTypeAtomList.size() == 0)
			{
				dragAndDropSourceWindow = 0;
				return;
			}
		}

		for (int i = 0; i < srcMimeTypeAtomList.size() && dragAndDropCurrentMimeType == 0; ++i)
			for (int j = 0; j < numElementsInArray (Atoms::allowedMimeTypes); ++j)
				if (srcMimeTypeAtomList[i] == Atoms::allowedMimeTypes[j])
					dragAndDropCurrentMimeType = Atoms::allowedMimeTypes[j];

		handleDragAndDropPosition (clientMsg);
	}

	void handleDragAndDropSelection (const XEvent* const evt)
	{
		dragAndDropFiles.clear();

		if (evt->xselection.property != 0)
		{
			StringArray lines;

			{
				MemoryBlock dropData;

				for (;;)
				{
					Atom actual;
					uint8* data = 0;
					unsigned long count = 0, remaining = 0;
					int format = 0;
					ScopedXLock xlock;

					if (XGetWindowProperty (display, evt->xany.window, evt->xselection.property,
											dropData.getSize() / 4, 65536, 1, AnyPropertyType, &actual,
											&format, &count, &remaining, &data) == Success)
					{
						dropData.append (data, count * format / 8);
						XFree (data);

						if (remaining == 0)
							break;
					}
					else
					{
						XFree (data);
						break;
					}
				}

				lines.addLines (dropData.toString());
			}

			for (int i = 0; i < lines.size(); ++i)
				dragAndDropFiles.add (URL::removeEscapeChars (lines[i].fromFirstOccurrenceOf ("file://", false, true)));

			dragAndDropFiles.trim();
			dragAndDropFiles.removeEmptyStrings();
		}
	}

	void updateDraggedFileList (const XClientMessageEvent* const clientMsg)
	{
		dragAndDropFiles.clear();

		if (dragAndDropSourceWindow != None
			 && dragAndDropCurrentMimeType != 0)
		{
			dragAndDropTimestamp = clientMsg->data.l[2];

			ScopedXLock xlock;
			XConvertSelection (display,
							   Atoms::XdndSelection,
							   dragAndDropCurrentMimeType,
							   XInternAtom (display, "JXSelectionWindowProperty", 0),
							   windowH,
							   dragAndDropTimestamp);
		}
	}

	StringArray dragAndDropFiles;
	int dragAndDropTimestamp;
	Point<int> lastDropPos;

	Atom dragAndDropCurrentMimeType;
	Window dragAndDropSourceWindow;

	Array <Atom> srcMimeTypeAtomList;

	static int pointerMap[5];
	static Point<int> lastMousePos;

	static void clearLastMousePos() throw()
	{
		lastMousePos = Point<int> (0x100000, 0x100000);
	}
};

ModifierKeys LinuxComponentPeer::currentModifiers;
bool LinuxComponentPeer::isActiveApplication = false;
int LinuxComponentPeer::pointerMap[5];
Point<int> LinuxComponentPeer::lastMousePos;

bool Process::isForegroundProcess()
{
	return LinuxComponentPeer::isActiveApplication;
}

void ModifierKeys::updateCurrentModifiers() throw()
{
	currentModifiers = LinuxComponentPeer::currentModifiers;
}

const ModifierKeys ModifierKeys::getCurrentModifiersRealtime() throw()
{
	Window root, child;
	int x, y, winx, winy;
	unsigned int mask;
	int mouseMods = 0;

	ScopedXLock xlock;

	if (XQueryPointer (display, RootWindow (display, DefaultScreen (display)),
					   &root, &child, &x, &y, &winx, &winy, &mask) != False)
	{
		if ((mask & Button1Mask) != 0)  mouseMods |= ModifierKeys::leftButtonModifier;
		if ((mask & Button2Mask) != 0)  mouseMods |= ModifierKeys::middleButtonModifier;
		if ((mask & Button3Mask) != 0)  mouseMods |= ModifierKeys::rightButtonModifier;
	}

	LinuxComponentPeer::currentModifiers = LinuxComponentPeer::currentModifiers.withoutMouseButtons().withFlags (mouseMods);
	return LinuxComponentPeer::currentModifiers;
}

void juce_setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool allowMenusAndBars)
{
	if (enableOrDisable)
		kioskModeComponent->setBounds (Desktop::getInstance().getMainMonitorArea (false));
}

ComponentPeer* Component::createNewPeer (int styleFlags, void* /*nativeWindowToAttachTo*/)
{
	return new LinuxComponentPeer (this, styleFlags);
}

// (this callback is hooked up in the messaging code)
void juce_windowMessageReceive (XEvent* event)
{
	if (event->xany.window != None)
	{
		LinuxComponentPeer* const peer = LinuxComponentPeer::getPeerFor (event->xany.window);

		if (ComponentPeer::isValidPeer (peer))
			peer->handleWindowMessage (event);
	}
	else
	{
		switch (event->xany.type)
		{
			case KeymapNotify:
			{
				const XKeymapEvent* const keymapEvent = (const XKeymapEvent*) &event->xkeymap;
				memcpy (Keys::keyStates, keymapEvent->key_vector, 32);
				break;
			}

			default:
				break;
		}
	}
}

void juce_updateMultiMonitorInfo (Array <Rectangle<int> >& monitorCoords, const bool /*clipToWorkArea*/)
{
	if (display == 0)
		return;

#if JUCE_USE_XINERAMA
	int major_opcode, first_event, first_error;

	ScopedXLock xlock;
	if (XQueryExtension (display, "XINERAMA", &major_opcode, &first_event, &first_error))
	{
		typedef Bool (*tXineramaIsActive) (Display*);
		typedef XineramaScreenInfo* (*tXineramaQueryScreens) (Display*, int*);

		static tXineramaIsActive xXineramaIsActive = 0;
		static tXineramaQueryScreens xXineramaQueryScreens = 0;

		if (xXineramaIsActive == 0 || xXineramaQueryScreens == 0)
		{
			void* h = dlopen ("libXinerama.so", RTLD_GLOBAL | RTLD_NOW);

			if (h != 0)
			{
				xXineramaIsActive = (tXineramaIsActive) dlsym (h, "XineramaIsActive");
				xXineramaQueryScreens = (tXineramaQueryScreens) dlsym (h, "XineramaQueryScreens");
			}
		}

		if (xXineramaIsActive != 0
			&& xXineramaQueryScreens != 0
			&& xXineramaIsActive (display))
		{
			int numMonitors = 0;
			XineramaScreenInfo* const screens = xXineramaQueryScreens (display, &numMonitors);

			if (screens != 0)
			{
				for (int i = numMonitors; --i >= 0;)
				{
					int index = screens[i].screen_number;

					if (index >= 0)
					{
						while (monitorCoords.size() < index)
							monitorCoords.add (Rectangle<int>());

						monitorCoords.set (index, Rectangle<int> (screens[i].x_org,
																  screens[i].y_org,
																  screens[i].width,
																  screens[i].height));
					}
				}

				XFree (screens);
			}
		}
	}

	if (monitorCoords.size() == 0)
#endif
	{
		Atom hints = XInternAtom (display, "_NET_WORKAREA", True);

		if (hints != None)
		{
			const int numMonitors = ScreenCount (display);

			for (int i = 0; i < numMonitors; ++i)
			{
				Window root = RootWindow (display, i);

				unsigned long nitems, bytesLeft;
				Atom actualType;
				int actualFormat;
				unsigned char* data = 0;

				if (XGetWindowProperty (display, root, hints, 0, 4, False,
										XA_CARDINAL, &actualType, &actualFormat, &nitems, &bytesLeft,
										&data) == Success)
				{
					const long* const position = (const long*) data;

					if (actualType == XA_CARDINAL && actualFormat == 32 && nitems == 4)
						monitorCoords.add (Rectangle<int> (position[0], position[1],
														   position[2], position[3]));

					XFree (data);
				}
			}
		}

		if (monitorCoords.size() == 0)
		{
			monitorCoords.add (Rectangle<int> (0, 0,
											   DisplayWidth (display, DefaultScreen (display)),
											   DisplayHeight (display, DefaultScreen (display))));
		}
	}
}

void Desktop::createMouseInputSources()
{
	mouseSources.add (new MouseInputSource (0, true));
}

bool Desktop::canUseSemiTransparentWindows() throw()
{
	int matchedDepth = 0;
	const int desiredDepth = 32;

	return Visuals::findVisualFormat (desiredDepth, matchedDepth) != 0
			 && (matchedDepth == desiredDepth);
}

const Point<int> Desktop::getMousePosition()
{
	Window root, child;
	int x, y, winx, winy;
	unsigned int mask;

	ScopedXLock xlock;

	if (XQueryPointer (display,
					   RootWindow (display, DefaultScreen (display)),
					   &root, &child,
					   &x, &y, &winx, &winy, &mask) == False)
	{
		// Pointer not on the default screen
		x = y = -1;
	}

	return Point<int> (x, y);
}

void Desktop::setMousePosition (const Point<int>& newPosition)
{
	ScopedXLock xlock;
	Window root = RootWindow (display, DefaultScreen (display));
	XWarpPointer (display, None, root, 0, 0, 0, 0, newPosition.getX(), newPosition.getY());
}

static bool screenSaverAllowed = true;

void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
{
	if (screenSaverAllowed != isEnabled)
	{
		screenSaverAllowed = isEnabled;

		typedef void (*tXScreenSaverSuspend) (Display*, Bool);
		static tXScreenSaverSuspend xScreenSaverSuspend = 0;

		if (xScreenSaverSuspend == 0)
		{
			void* h = dlopen ("libXss.so", RTLD_GLOBAL | RTLD_NOW);

			if (h != 0)
				xScreenSaverSuspend = (tXScreenSaverSuspend) dlsym (h, "XScreenSaverSuspend");
		}

		ScopedXLock xlock;
		if (xScreenSaverSuspend != 0)
			xScreenSaverSuspend (display, ! isEnabled);
	}
}

bool Desktop::isScreenSaverEnabled() throw()
{
	return screenSaverAllowed;
}

void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY)
{
	ScopedXLock xlock;
	const unsigned int imageW = image.getWidth();
	const unsigned int imageH = image.getHeight();

#if JUCE_USE_XCURSOR
	{
		typedef XcursorBool (*tXcursorSupportsARGB) (Display*);
		typedef XcursorImage* (*tXcursorImageCreate) (int, int);
		typedef void (*tXcursorImageDestroy) (XcursorImage*);
		typedef Cursor (*tXcursorImageLoadCursor) (Display*, const XcursorImage*);

		static tXcursorSupportsARGB xXcursorSupportsARGB = 0;
		static tXcursorImageCreate xXcursorImageCreate = 0;
		static tXcursorImageDestroy xXcursorImageDestroy = 0;
		static tXcursorImageLoadCursor xXcursorImageLoadCursor = 0;
		static bool hasBeenLoaded = false;

		if (! hasBeenLoaded)
		{
			hasBeenLoaded = true;
			void* h = dlopen ("libXcursor.so", RTLD_GLOBAL | RTLD_NOW);

			if (h != 0)
			{
				xXcursorSupportsARGB    = (tXcursorSupportsARGB)    dlsym (h, "XcursorSupportsARGB");
				xXcursorImageCreate     = (tXcursorImageCreate)     dlsym (h, "XcursorImageCreate");
				xXcursorImageLoadCursor = (tXcursorImageLoadCursor) dlsym (h, "XcursorImageLoadCursor");
				xXcursorImageDestroy    = (tXcursorImageDestroy)    dlsym (h, "XcursorImageDestroy");

				if (xXcursorSupportsARGB == 0 || xXcursorImageCreate == 0
					  || xXcursorImageLoadCursor == 0 || xXcursorImageDestroy == 0
					  || ! xXcursorSupportsARGB (display))
					xXcursorSupportsARGB = 0;
			}
		}

		if (xXcursorSupportsARGB != 0)
		{
			XcursorImage* xcImage = xXcursorImageCreate (imageW, imageH);

			if (xcImage != 0)
			{
				xcImage->xhot = hotspotX;
				xcImage->yhot = hotspotY;
				XcursorPixel* dest = xcImage->pixels;

				for (int y = 0; y < (int) imageH; ++y)
					for (int x = 0; x < (int) imageW; ++x)
						*dest++ = image.getPixelAt (x, y).getARGB();

				void* result = (void*) xXcursorImageLoadCursor (display, xcImage);
				xXcursorImageDestroy (xcImage);

				if (result != 0)
					return result;
			}
		}
	}
#endif

	Window root = RootWindow (display, DefaultScreen (display));
	unsigned int cursorW, cursorH;
	if (! XQueryBestCursor (display, root, imageW, imageH, &cursorW, &cursorH))
		return 0;

	Image im (Image::ARGB, cursorW, cursorH, true);
	{
		Graphics g (im);

		if (imageW > cursorW || imageH > cursorH)
		{
			hotspotX = (hotspotX * cursorW) / imageW;
			hotspotY = (hotspotY * cursorH) / imageH;

			g.drawImageWithin (&image, 0, 0, imageW, imageH,
							   RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize,
							   false);
		}
		else
		{
			g.drawImageAt (&image, 0, 0);
		}
	}

	const int stride = (cursorW + 7) >> 3;
	HeapBlock <char> maskPlane, sourcePlane;
	maskPlane.calloc (stride * cursorH);
	sourcePlane.calloc (stride * cursorH);

	const bool msbfirst = (BitmapBitOrder (display) == MSBFirst);

	for (int y = cursorH; --y >= 0;)
	{
		for (int x = cursorW; --x >= 0;)
		{
			const char mask = (char) (1 << (msbfirst ? (7 - (x & 7)) : (x & 7)));
			const int offset = y * stride + (x >> 3);

			const Colour c (im.getPixelAt (x, y));

			if (c.getAlpha() >= 128)
				maskPlane[offset] |= mask;

			if (c.getBrightness() >= 0.5f)
				sourcePlane[offset] |= mask;
		}
	}

	Pixmap sourcePixmap = XCreatePixmapFromBitmapData (display, root, sourcePlane.getData(), cursorW, cursorH, 0xffff, 0, 1);
	Pixmap maskPixmap = XCreatePixmapFromBitmapData (display, root, maskPlane.getData(), cursorW, cursorH, 0xffff, 0, 1);

	XColor white, black;
	black.red = black.green = black.blue = 0;
	white.red = white.green = white.blue = 0xffff;

	void* result = (void*) XCreatePixmapCursor (display, sourcePixmap, maskPixmap, &white, &black, hotspotX, hotspotY);

	XFreePixmap (display, sourcePixmap);
	XFreePixmap (display, maskPixmap);

	return result;
}

void juce_deleteMouseCursor (void* const cursorHandle, const bool)
{
	ScopedXLock xlock;
	if (cursorHandle != 0)
		XFreeCursor (display, (Cursor) cursorHandle);
}

void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type)
{
	unsigned int shape;

	switch (type)
	{
		case MouseCursor::NoCursor:
		{
			const Image im (Image::ARGB, 16, 16, true);
			return juce_createMouseCursorFromImage (im, 0, 0);
		}

		case MouseCursor::NormalCursor:
			return (void*) None; // Use parent cursor

		case MouseCursor::DraggingHandCursor:
		{
			static unsigned char dragHandData[] = {71,73,70,56,57,97,16,0,16,0,145,2,0,0,0,0,255,255,255,0,
			  0,0,0,0,0,33,249,4,1,0,0,2,0,44,0,0,0,0,16,0,
			  16,0,0,2,52,148,47,0,200,185,16,130,90,12,74,139,107,84,123,39,
			  132,117,151,116,132,146,248,60,209,138,98,22,203,114,34,236,37,52,77,217,
			  247,154,191,119,110,240,193,128,193,95,163,56,60,234,98,135,2,0,59 };
			const int dragHandDataSize = 99;

			const ScopedPointer <Image> im (ImageFileFormat::loadFrom (dragHandData, dragHandDataSize));
			return juce_createMouseCursorFromImage (*im, 8, 7);
		}

		case MouseCursor::CopyingCursor:
		{
			static unsigned char copyCursorData[] = {71,73,70,56,57,97,21,0,21,0,145,0,0,0,0,0,255,255,255,0,
			  128,128,255,255,255,33,249,4,1,0,0,3,0,44,0,0,0,0,21,0,
			  21,0,0,2,72,4,134,169,171,16,199,98,11,79,90,71,161,93,56,111,
			  78,133,218,215,137,31,82,154,100,200,86,91,202,142,12,108,212,87,235,174,
			  15,54,214,126,237,226,37,96,59,141,16,37,18,201,142,157,230,204,51,112,
			  252,114,147,74,83,5,50,68,147,208,217,16,71,149,252,124,5,0,59,0,0 };
			const int copyCursorSize = 119;

			const ScopedPointer <Image> im (ImageFileFormat::loadFrom (copyCursorData, copyCursorSize));
			return juce_createMouseCursorFromImage (*im, 1, 3);
		}

		case MouseCursor::WaitCursor:
			shape = XC_watch;
			break;

		case MouseCursor::IBeamCursor:
			shape = XC_xterm;
			break;

		case MouseCursor::PointingHandCursor:
			shape = XC_hand2;
			break;

		case MouseCursor::LeftRightResizeCursor:
			shape = XC_sb_h_double_arrow;
			break;

		case MouseCursor::UpDownResizeCursor:
			shape = XC_sb_v_double_arrow;
			break;

		case MouseCursor::UpDownLeftRightResizeCursor:
			shape = XC_fleur;
			break;

		case MouseCursor::TopEdgeResizeCursor:
			shape = XC_top_side;
			break;

		case MouseCursor::BottomEdgeResizeCursor:
			shape = XC_bottom_side;
			break;

		case MouseCursor::LeftEdgeResizeCursor:
			shape = XC_left_side;
			break;

		case MouseCursor::RightEdgeResizeCursor:
			shape = XC_right_side;
			break;

		case MouseCursor::TopLeftCornerResizeCursor:
			shape = XC_top_left_corner;
			break;

		case MouseCursor::TopRightCornerResizeCursor:
			shape = XC_top_right_corner;
			break;

		case MouseCursor::BottomLeftCornerResizeCursor:
			shape = XC_bottom_left_corner;
			break;

		case MouseCursor::BottomRightCornerResizeCursor:
			shape = XC_bottom_right_corner;
			break;

		case MouseCursor::CrosshairCursor:
			shape = XC_crosshair;
			break;

		default:
			return (void*) None; // Use parent cursor
	}

	ScopedXLock xlock;
	return (void*) XCreateFontCursor (display, shape);
}

void MouseCursor::showInWindow (ComponentPeer* peer) const
{
	LinuxComponentPeer* const lp = dynamic_cast <LinuxComponentPeer*> (peer);

	if (lp != 0)
		lp->showMouseCursor ((Cursor) getHandle());
}

void MouseCursor::showInAllWindows() const
{
	for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
		showInWindow (ComponentPeer::getPeer (i));
}

Image* juce_createIconForFile (const File& file)
{
	return 0;
}

Image* Image::createNativeImage (const PixelFormat format, const int imageWidth, const int imageHeight, const bool clearImage)
{
	return new Image (format, imageWidth, imageHeight, clearImage);
}

#if JUCE_OPENGL

class WindowedGLContext	 : public OpenGLContext
{
public:
	WindowedGLContext (Component* const component,
					   const OpenGLPixelFormat& pixelFormat_,
					   GLXContext sharedContext)
		: renderContext (0),
		  embeddedWindow (0),
		  pixelFormat (pixelFormat_)
	{
		jassert (component != 0);
		LinuxComponentPeer* const peer = dynamic_cast <LinuxComponentPeer*> (component->getTopLevelComponent()->getPeer());
		if (peer == 0)
			return;

		ScopedXLock xlock;
		XSync (display, False);

		GLint attribs [64];
		int n = 0;
		attribs[n++] = GLX_RGBA;
		attribs[n++] = GLX_DOUBLEBUFFER;
		attribs[n++] = GLX_RED_SIZE;
		attribs[n++] = pixelFormat.redBits;
		attribs[n++] = GLX_GREEN_SIZE;
		attribs[n++] = pixelFormat.greenBits;
		attribs[n++] = GLX_BLUE_SIZE;
		attribs[n++] = pixelFormat.blueBits;
		attribs[n++] = GLX_ALPHA_SIZE;
		attribs[n++] = pixelFormat.alphaBits;
		attribs[n++] = GLX_DEPTH_SIZE;
		attribs[n++] = pixelFormat.depthBufferBits;
		attribs[n++] = GLX_STENCIL_SIZE;
		attribs[n++] = pixelFormat.stencilBufferBits;
		attribs[n++] = GLX_ACCUM_RED_SIZE;
		attribs[n++] = pixelFormat.accumulationBufferRedBits;
		attribs[n++] = GLX_ACCUM_GREEN_SIZE;
		attribs[n++] = pixelFormat.accumulationBufferGreenBits;
		attribs[n++] = GLX_ACCUM_BLUE_SIZE;
		attribs[n++] = pixelFormat.accumulationBufferBlueBits;
		attribs[n++] = GLX_ACCUM_ALPHA_SIZE;
		attribs[n++] = pixelFormat.accumulationBufferAlphaBits;

		// xxx not sure how to do fullSceneAntiAliasingNumSamples on linux..

		attribs[n++] = None;

		XVisualInfo* const bestVisual = glXChooseVisual (display, DefaultScreen (display), attribs);

		if (bestVisual == 0)
			return;

		renderContext = glXCreateContext (display, bestVisual, sharedContext, GL_TRUE);

		Window windowH = (Window) peer->getNativeHandle();

		Colormap colourMap = XCreateColormap (display, windowH, bestVisual->visual, AllocNone);
		XSetWindowAttributes swa;
		swa.colormap = colourMap;
		swa.border_pixel = 0;
		swa.event_mask = ExposureMask | StructureNotifyMask;

		embeddedWindow = XCreateWindow (display, windowH,
										0, 0, 1, 1, 0,
										bestVisual->depth,
										InputOutput,
										bestVisual->visual,
										CWBorderPixel | CWColormap | CWEventMask,
										&swa);

		XSaveContext (display, (XID) embeddedWindow, improbableNumber, (XPointer) peer);

		XMapWindow (display, embeddedWindow);
		XFreeColormap (display, colourMap);

		XFree (bestVisual);
		XSync (display, False);
	}

	~WindowedGLContext()
	{
		makeInactive();

		ScopedXLock xlock;
		glXDestroyContext (display, renderContext);

		XUnmapWindow (display, embeddedWindow);
		XDestroyWindow (display, embeddedWindow);
	}

	bool makeActive() const throw()
	{
		jassert (renderContext != 0);

		ScopedXLock xlock;
		return glXMakeCurrent (display, embeddedWindow, renderContext)
				&& XSync (display, False);
	}

	bool makeInactive() const throw()
	{
		ScopedXLock xlock;
		return (! isActive()) || glXMakeCurrent (display, None, 0);
	}

	bool isActive() const throw()
	{
		ScopedXLock xlock;
		return glXGetCurrentContext() == renderContext;
	}

	const OpenGLPixelFormat getPixelFormat() const
	{
		return pixelFormat;
	}

	void* getRawContext() const throw()
	{
		return renderContext;
	}

	void updateWindowPosition (int x, int y, int w, int h, int)
	{
		ScopedXLock xlock;
		XMoveResizeWindow (display, embeddedWindow,
						   x, y, jmax (1, w), jmax (1, h));
	}

	void swapBuffers()
	{
		ScopedXLock xlock;
		glXSwapBuffers (display, embeddedWindow);
	}

	bool setSwapInterval (const int numFramesPerSwap)
	{
		// xxx needs doing..
		return false;
	}

	int getSwapInterval() const
	{
		// xxx needs doing..
		return 0;
	}

	void repaint()
	{
	}

	juce_UseDebuggingNewOperator

	GLXContext renderContext;

private:
	Window embeddedWindow;
	OpenGLPixelFormat pixelFormat;

	WindowedGLContext (const WindowedGLContext&);
	WindowedGLContext& operator= (const WindowedGLContext&);
};

OpenGLContext* OpenGLComponent::createContext()
{
	ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
															   contextToShareListsWith != 0 ? (GLXContext) contextToShareListsWith->getRawContext() : 0));

	return (c->renderContext != 0) ? c.release() : 0;
}

void juce_glViewport (const int w, const int h)
{
	glViewport (0, 0, w, h);
}

void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
												  OwnedArray <OpenGLPixelFormat>& results)
{
	results.add (new OpenGLPixelFormat()); // xxx
}

#endif

bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMoveFiles)
{
	jassertfalse	// not implemented!
	return false;
}

bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
{
	jassertfalse	// not implemented!
	return false;
}

void SystemTrayIconComponent::setIconImage (const Image& newImage)
{
	if (! isOnDesktop ())
		addToDesktop (0);

	LinuxComponentPeer* const wp = dynamic_cast <LinuxComponentPeer*> (getPeer());

	if (wp != 0)
	{
		wp->setTaskBarIcon (newImage);

		setVisible (true);
		toFront (false);
		repaint();
	}
}

void SystemTrayIconComponent::paint (Graphics& g)
{
	LinuxComponentPeer* const wp = dynamic_cast <LinuxComponentPeer*> (getPeer());

	if (wp != 0)
	{
		const Image* const image = wp->getTaskbarIcon();

		if (image != 0)
		{
			g.drawImageWithin (image, 0, 0, getWidth(), getHeight(),
							   RectanglePlacement::xLeft | RectanglePlacement::yTop | RectanglePlacement::onlyReduceInSize,
							   false);
		}
	}
}

void SystemTrayIconComponent::setIconTooltip (const String& tooltip)
{
	// xxx not yet implemented!
}

void PlatformUtilities::beep()
{
	std::cout << "\a" << std::flush;
}

bool AlertWindow::showNativeDialogBox (const String& title,
									   const String& bodyText,
									   bool isOkCancel)
{
	// use a non-native one for the time being..
	if (isOkCancel)
		return AlertWindow::showOkCancelBox (AlertWindow::NoIcon, title, bodyText);
	else
		AlertWindow::showMessageBox (AlertWindow::NoIcon, title, bodyText);

	return true;
}

const int KeyPress::spaceKey		= XK_space & 0xff;
const int KeyPress::returnKey		   = XK_Return & 0xff;
const int KeyPress::escapeKey		   = XK_Escape & 0xff;
const int KeyPress::backspaceKey		= XK_BackSpace & 0xff;
const int KeyPress::leftKey		 = (XK_Left & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::rightKey		= (XK_Right & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::upKey		   = (XK_Up & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::downKey		 = (XK_Down & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::pageUpKey		   = (XK_Page_Up & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::pageDownKey		 = (XK_Page_Down & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::endKey		  = (XK_End & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::homeKey		 = (XK_Home & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::insertKey		   = (XK_Insert & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::deleteKey		   = (XK_Delete & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::tabKey		  = XK_Tab & 0xff;
const int KeyPress::F1Key		   = (XK_F1 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F2Key		   = (XK_F2 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F3Key		   = (XK_F3 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F4Key		   = (XK_F4 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F5Key		   = (XK_F5 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F6Key		   = (XK_F6 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F7Key		   = (XK_F7 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F8Key		   = (XK_F8 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F9Key		   = (XK_F9 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F10Key		  = (XK_F10 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F11Key		  = (XK_F11 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F12Key		  = (XK_F12 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F13Key		  = (XK_F13 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F14Key		  = (XK_F14 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F15Key		  = (XK_F15 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::F16Key		  = (XK_F16 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::numberPad0		  = (XK_KP_0 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::numberPad1		  = (XK_KP_1 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::numberPad2		  = (XK_KP_2 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::numberPad3		  = (XK_KP_3 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::numberPad4		  = (XK_KP_4 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::numberPad5		  = (XK_KP_5 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::numberPad6		  = (XK_KP_6 & 0xff) | Keys::extendedKeyModifier;
const int KeyPress::numberPad7		  = (XK_KP_7 & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPad8		  = (XK_KP_8 & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPad9		  = (XK_KP_9 & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPadAdd		= (XK_KP_Add & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPadSubtract	   = (XK_KP_Subtract & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPadMultiply	   = (XK_KP_Multiply & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPadDivide	 = (XK_KP_Divide & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPadSeparator	  = (XK_KP_Separator & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPadDecimalPoint   = (XK_KP_Decimal & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPadEquals	 = (XK_KP_Equal & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::numberPadDelete	 = (XK_KP_Delete & 0xff)| Keys::extendedKeyModifier;
const int KeyPress::playKey		 = (0xffeeff00) | Keys::extendedKeyModifier;
const int KeyPress::stopKey		 = (0xffeeff01) | Keys::extendedKeyModifier;
const int KeyPress::fastForwardKey	  = (0xffeeff02) | Keys::extendedKeyModifier;
const int KeyPress::rewindKey		   = (0xffeeff03) | Keys::extendedKeyModifier;

#endif
/*** End of inlined file: juce_linux_Windowing.cpp ***/


/*** Start of inlined file: juce_linux_Audio.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_ALSA

static const int maxNumChans = 64;

static void getDeviceSampleRates (snd_pcm_t* handle, Array <int>& rates)
{
	const int ratesToTry[] = { 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 0 };

	snd_pcm_hw_params_t* hwParams;
	snd_pcm_hw_params_alloca (&hwParams);

	for (int i = 0; ratesToTry[i] != 0; ++i)
	{
		if (snd_pcm_hw_params_any (handle, hwParams) >= 0
			 && snd_pcm_hw_params_test_rate (handle, hwParams, ratesToTry[i], 0) == 0)
		{
			rates.addIfNotAlreadyThere (ratesToTry[i]);
		}
	}
}

static void getDeviceNumChannels (snd_pcm_t* handle, unsigned int* minChans, unsigned int* maxChans)
{
	snd_pcm_hw_params_t *params;
	snd_pcm_hw_params_alloca (&params);

	if (snd_pcm_hw_params_any (handle, params) >= 0)
	{
		snd_pcm_hw_params_get_channels_min (params, minChans);
		snd_pcm_hw_params_get_channels_max (params, maxChans);
	}
}

static void getDeviceProperties (const String& deviceID,
								 unsigned int& minChansOut,
								 unsigned int& maxChansOut,
								 unsigned int& minChansIn,
								 unsigned int& maxChansIn,
								 Array <int>& rates)
{
	if (deviceID.isEmpty())
		return;

	snd_ctl_t* handle;

	if (snd_ctl_open (&handle, deviceID.upToLastOccurrenceOf (",", false, false).toUTF8(), SND_CTL_NONBLOCK) >= 0)
	{
		snd_pcm_info_t* info;
		snd_pcm_info_alloca (&info);

		snd_pcm_info_set_stream (info, SND_PCM_STREAM_PLAYBACK);
		snd_pcm_info_set_device (info, deviceID.fromLastOccurrenceOf (",", false, false).getIntValue());
		snd_pcm_info_set_subdevice (info, 0);

		if (snd_ctl_pcm_info (handle, info) >= 0)
		{
			snd_pcm_t* pcmHandle;
			if (snd_pcm_open (&pcmHandle, deviceID.toUTF8(), SND_PCM_STREAM_PLAYBACK, SND_PCM_ASYNC | SND_PCM_NONBLOCK ) >= 0)
			{
				getDeviceNumChannels (pcmHandle, &minChansOut, &maxChansOut);
				getDeviceSampleRates (pcmHandle, rates);

				snd_pcm_close (pcmHandle);
			}
		}

		snd_pcm_info_set_stream (info, SND_PCM_STREAM_CAPTURE);

		if (snd_ctl_pcm_info (handle, info) >= 0)
		{
			snd_pcm_t* pcmHandle;
			if (snd_pcm_open (&pcmHandle, deviceID.toUTF8(), SND_PCM_STREAM_CAPTURE, SND_PCM_ASYNC | SND_PCM_NONBLOCK ) >= 0)
			{
				getDeviceNumChannels (pcmHandle, &minChansIn, &maxChansIn);

				if (rates.size() == 0)
					getDeviceSampleRates (pcmHandle, rates);

				snd_pcm_close (pcmHandle);
			}
		}

		snd_ctl_close (handle);
	}
}

class ALSADevice
{
public:
	ALSADevice (const String& deviceID,
				const bool forInput)
		: handle (0),
		  bitDepth (16),
		  numChannelsRunning (0),
		  isInput (forInput),
		  sampleFormat (AudioDataConverters::int16LE)
	{
		failed (snd_pcm_open (&handle, deviceID.toUTF8(),
							  forInput ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
							  SND_PCM_ASYNC));
	}

	~ALSADevice()
	{
		if (handle != 0)
			snd_pcm_close (handle);
	}

	bool setParameters (unsigned int sampleRate, int numChannels, int bufferSize)
	{
		if (handle == 0)
			return false;

		snd_pcm_hw_params_t* hwParams;
		snd_pcm_hw_params_alloca (&hwParams);

		if (failed (snd_pcm_hw_params_any (handle, hwParams)))
			return false;

		if (snd_pcm_hw_params_set_access (handle, hwParams, SND_PCM_ACCESS_RW_NONINTERLEAVED) >= 0)
			isInterleaved = false;
		else if (snd_pcm_hw_params_set_access (handle, hwParams, SND_PCM_ACCESS_RW_INTERLEAVED) >= 0)
			isInterleaved = true;
		else
		{
			jassertfalse
			return false;
		}

		const int formatsToTry[] = { SND_PCM_FORMAT_FLOAT_LE, 32, AudioDataConverters::float32LE,
									 SND_PCM_FORMAT_FLOAT_BE, 32, AudioDataConverters::float32BE,
									 SND_PCM_FORMAT_S32_LE, 32, AudioDataConverters::int32LE,
									 SND_PCM_FORMAT_S32_BE, 32, AudioDataConverters::int32BE,
									 SND_PCM_FORMAT_S24_3LE, 24, AudioDataConverters::int24LE,
									 SND_PCM_FORMAT_S24_3BE, 24, AudioDataConverters::int24BE,
									 SND_PCM_FORMAT_S16_LE, 16, AudioDataConverters::int16LE,
									 SND_PCM_FORMAT_S16_BE, 16, AudioDataConverters::int16BE };
		bitDepth = 0;

		for (int i = 0; i < numElementsInArray (formatsToTry); i += 3)
		{
			if (snd_pcm_hw_params_set_format (handle, hwParams, (_snd_pcm_format) formatsToTry [i]) >= 0)
			{
				bitDepth = formatsToTry [i + 1];
				sampleFormat = (AudioDataConverters::DataFormat) formatsToTry [i + 2];
				break;
			}
		}

		if (bitDepth == 0)
		{
			error = "device doesn't support a compatible PCM format";
			DBG ("ALSA error: " + error + "\n");
			return false;
		}

		int dir = 0;
		unsigned int periods = 4;
		snd_pcm_uframes_t samplesPerPeriod = bufferSize;

		if (failed (snd_pcm_hw_params_set_rate_near (handle, hwParams, &sampleRate, 0))
			|| failed (snd_pcm_hw_params_set_channels (handle, hwParams, numChannels))
			|| failed (snd_pcm_hw_params_set_periods_near (handle, hwParams, &periods, &dir))
			|| failed (snd_pcm_hw_params_set_period_size_near (handle, hwParams, &samplesPerPeriod, &dir))
			|| failed (snd_pcm_hw_params (handle, hwParams)))
		{
			return false;
		}

		snd_pcm_sw_params_t* swParams;
		snd_pcm_sw_params_alloca (&swParams);
		snd_pcm_uframes_t boundary;

		if (failed (snd_pcm_sw_params_current (handle, swParams))
			|| failed (snd_pcm_sw_params_get_boundary (swParams, &boundary))
			|| failed (snd_pcm_sw_params_set_silence_threshold (handle, swParams, 0))
			|| failed (snd_pcm_sw_params_set_silence_size (handle, swParams, boundary))
			|| failed (snd_pcm_sw_params_set_start_threshold (handle, swParams, samplesPerPeriod))
			|| failed (snd_pcm_sw_params_set_stop_threshold (handle, swParams, boundary))
			|| failed (snd_pcm_sw_params (handle, swParams)))
		{
			return false;
		}

		numChannelsRunning = numChannels;

		return true;
	}

	bool write (float** const data, const int numSamples)
	{
		if (isInterleaved)
		{
			scratch.ensureSize (sizeof (float) * numSamples * numChannelsRunning, false);
			float* interleaved = static_cast <float*> (scratch.getData());

			AudioDataConverters::interleaveSamples ((const float**) data, interleaved, numSamples, numChannelsRunning);
			AudioDataConverters::convertFloatToFormat (sampleFormat, interleaved, interleaved, numSamples * numChannelsRunning);

			snd_pcm_sframes_t num = snd_pcm_writei (handle, interleaved, numSamples);

			if (failed (num) && num != -EPIPE && num != -ESTRPIPE)
				return false;
		}
		else
		{
			for (int i = 0; i < numChannelsRunning; ++i)
				if (data[i] != 0)
					AudioDataConverters::convertFloatToFormat (sampleFormat, data[i], data[i], numSamples);

			snd_pcm_sframes_t num = snd_pcm_writen (handle, (void**) data, numSamples);

			if (failed (num))
			{
				if (num == -EPIPE)
				{
					if (failed (snd_pcm_prepare (handle)))
						return false;
				}
				else if (num != -ESTRPIPE)
					return false;
			}
		}

		return true;
	}

	bool read (float** const data, const int numSamples)
	{
		if (isInterleaved)
		{
			scratch.ensureSize (sizeof (float) * numSamples * numChannelsRunning, false);
			float* interleaved = static_cast <float*> (scratch.getData());

			snd_pcm_sframes_t num = snd_pcm_readi (handle, interleaved, numSamples);

			if (failed (num))
			{
				if (num == -EPIPE)
				{
					if (failed (snd_pcm_prepare (handle)))
						return false;
				}
				else if (num != -ESTRPIPE)
					return false;
			}

			AudioDataConverters::convertFormatToFloat (sampleFormat, interleaved, interleaved, numSamples * numChannelsRunning);
			AudioDataConverters::deinterleaveSamples (interleaved, data, numSamples, numChannelsRunning);
		}
		else
		{
			snd_pcm_sframes_t num = snd_pcm_readn (handle, (void**) data, numSamples);

			if (failed (num) && num != -EPIPE && num != -ESTRPIPE)
				return false;

			for (int i = 0; i < numChannelsRunning; ++i)
				if (data[i] != 0)
					AudioDataConverters::convertFormatToFloat (sampleFormat, data[i], data[i], numSamples);
		}

		return true;
	}

	juce_UseDebuggingNewOperator

	snd_pcm_t* handle;
	String error;
	int bitDepth, numChannelsRunning;

private:
	const bool isInput;
	bool isInterleaved;
	MemoryBlock scratch;
	AudioDataConverters::DataFormat sampleFormat;

	bool failed (const int errorNum)
	{
		if (errorNum >= 0)
			return false;

		error = snd_strerror (errorNum);
		DBG ("ALSA error: " + error + "\n");
		return true;
	}
};

class ALSAThread  : public Thread
{
public:
	ALSAThread (const String& inputId_,
				const String& outputId_)
		: Thread ("Juce ALSA"),
		  sampleRate (0),
		  bufferSize (0),
		  callback (0),
		  inputId (inputId_),
		  outputId (outputId_),
		  outputDevice (0),
		  inputDevice (0),
		  numCallbacks (0),
		  totalNumInputChannels (0),
		  totalNumOutputChannels (0)
	{
		zeromem (outputChannelData, sizeof (outputChannelData));
		zeromem (outputChannelDataForCallback, sizeof (outputChannelDataForCallback));
		zeromem (inputChannelData, sizeof (inputChannelData));
		zeromem (inputChannelDataForCallback, sizeof (inputChannelDataForCallback));

		initialiseRatesAndChannels();
	}

	~ALSAThread()
	{
		close();
	}

	void open (BigInteger inputChannels,
			   BigInteger outputChannels,
			   const double sampleRate_,
			   const int bufferSize_)
	{
		close();

		error = String::empty;
		sampleRate = sampleRate_;
		bufferSize = bufferSize_;
		currentInputChans.clear();
		currentOutputChans.clear();

		if (inputChannels.getHighestBit() >= 0)
		{
			for (int i = 0; i <= jmax (inputChannels.getHighestBit(), (int) minChansIn); ++i)
			{
				inputChannelData [i] = (float*) juce_calloc (sizeof (float) * bufferSize);

				if (inputChannels[i])
				{
					inputChannelDataForCallback [totalNumInputChannels++] = inputChannelData [i];
					currentInputChans.setBit (i);
				}
			}
		}

		if (outputChannels.getHighestBit() >= 0)
		{
			for (int i = 0; i <= jmax (outputChannels.getHighestBit(), (int) minChansOut); ++i)
			{
				outputChannelData [i] = (float*) juce_calloc (sizeof (float) * bufferSize);

				if (outputChannels[i])
				{
					outputChannelDataForCallback [totalNumOutputChannels++] = outputChannelData [i];
					currentOutputChans.setBit (i);
				}
			}
		}

		if (totalNumOutputChannels > 0 && outputId.isNotEmpty())
		{
			outputDevice = new ALSADevice (outputId, false);

			if (outputDevice->error.isNotEmpty())
			{
				error = outputDevice->error;
				deleteAndZero (outputDevice);
				return;
			}

			currentOutputChans.setRange (0, minChansOut, true);

			if (! outputDevice->setParameters ((unsigned int) sampleRate,
											   jlimit ((int) minChansOut, (int) maxChansOut, currentOutputChans.getHighestBit() + 1),
											   bufferSize))
			{
				error = outputDevice->error;
				deleteAndZero (outputDevice);
				return;
			}
		}

		if (totalNumInputChannels > 0 && inputId.isNotEmpty())
		{
			inputDevice = new ALSADevice (inputId, true);

			if (inputDevice->error.isNotEmpty())
			{
				error = inputDevice->error;
				deleteAndZero (inputDevice);
				return;
			}

			currentInputChans.setRange (0, minChansIn, true);

			if (! inputDevice->setParameters ((unsigned int) sampleRate,
											  jlimit ((int) minChansIn, (int) maxChansIn, currentInputChans.getHighestBit() + 1),
											  bufferSize))
			{
				error = inputDevice->error;
				deleteAndZero (inputDevice);
				return;
			}
		}

		if (outputDevice == 0 && inputDevice == 0)
		{
			error = "no channels";
			return;
		}

		if (outputDevice != 0 && inputDevice != 0)
		{
			snd_pcm_link (outputDevice->handle, inputDevice->handle);
		}

		if (inputDevice != 0 && failed (snd_pcm_prepare (inputDevice->handle)))
			return;

		if (outputDevice != 0 && failed (snd_pcm_prepare (outputDevice->handle)))
			return;

		startThread (9);

		int count = 1000;

		while (numCallbacks == 0)
		{
			sleep (5);

			if (--count < 0 || ! isThreadRunning())
			{
				error = "device didn't start";
				break;
			}
		}
	}

	void close()
	{
		stopThread (6000);

		deleteAndZero (inputDevice);
		deleteAndZero (outputDevice);

		for (int i = 0; i < maxNumChans; ++i)
		{
			juce_free (inputChannelData [i]);
			juce_free (outputChannelData [i]);
		}

		zeromem (outputChannelData, sizeof (outputChannelData));
		zeromem (outputChannelDataForCallback, sizeof (outputChannelDataForCallback));
		zeromem (inputChannelData, sizeof (inputChannelData));
		zeromem (inputChannelDataForCallback, sizeof (inputChannelDataForCallback));
		totalNumOutputChannels = 0;
		totalNumInputChannels = 0;

		numCallbacks = 0;
	}

	void setCallback (AudioIODeviceCallback* const newCallback) throw()
	{
		const ScopedLock sl (callbackLock);
		callback = newCallback;
	}

	void run()
	{
		while (! threadShouldExit())
		{
			if (inputDevice != 0)
			{
				if (! inputDevice->read (inputChannelData, bufferSize))
				{
					DBG ("ALSA: read failure");
					break;
				}
			}

			if (threadShouldExit())
				break;

			{
				const ScopedLock sl (callbackLock);
				++numCallbacks;

				if (callback != 0)
				{
					callback->audioDeviceIOCallback ((const float**) inputChannelDataForCallback,
													 totalNumInputChannels,
													 outputChannelDataForCallback,
													 totalNumOutputChannels,
													 bufferSize);
				}
				else
				{
					for (int i = 0; i < totalNumOutputChannels; ++i)
						zeromem (outputChannelDataForCallback[i], sizeof (float) * bufferSize);
				}
			}

			if (outputDevice != 0)
			{
				failed (snd_pcm_wait (outputDevice->handle, 2000));

				if (threadShouldExit())
					break;

				failed (snd_pcm_avail_update (outputDevice->handle));

				if (! outputDevice->write (outputChannelData, bufferSize))
				{
					DBG ("ALSA: write failure");
					break;
				}
			}
		}
	}

	int getBitDepth() const throw()
	{
		if (outputDevice != 0)
			return outputDevice->bitDepth;

		if (inputDevice != 0)
			return inputDevice->bitDepth;

		return 16;
	}

	juce_UseDebuggingNewOperator

	String error;
	double sampleRate;
	int bufferSize;
	BigInteger currentInputChans, currentOutputChans;

	Array <int> sampleRates;
	StringArray channelNamesOut, channelNamesIn;
	AudioIODeviceCallback* callback;

private:

	const String inputId, outputId;
	ALSADevice* outputDevice;
	ALSADevice* inputDevice;
	int numCallbacks;

	CriticalSection callbackLock;

	float* outputChannelData [maxNumChans];
	float* outputChannelDataForCallback [maxNumChans];
	int totalNumInputChannels;
	float* inputChannelData [maxNumChans];
	float* inputChannelDataForCallback [maxNumChans];
	int totalNumOutputChannels;

	unsigned int minChansOut, maxChansOut;
	unsigned int minChansIn, maxChansIn;

	bool failed (const int errorNum) throw()
	{
		if (errorNum >= 0)
			return false;

		error = snd_strerror (errorNum);
		DBG ("ALSA error: " + error + "\n");
		return true;
	}

	void initialiseRatesAndChannels() throw()
	{
		sampleRates.clear();
		channelNamesOut.clear();
		channelNamesIn.clear();
		minChansOut = 0;
		maxChansOut = 0;
		minChansIn = 0;
		maxChansIn = 0;
		unsigned int dummy = 0;

		getDeviceProperties (inputId, dummy, dummy, minChansIn, maxChansIn, sampleRates);
		getDeviceProperties (outputId, minChansOut, maxChansOut, dummy, dummy, sampleRates);

		unsigned int i;
		for (i = 0; i < maxChansOut; ++i)
			channelNamesOut.add ("channel " + String ((int) i + 1));

		for (i = 0; i < maxChansIn; ++i)
			channelNamesIn.add ("channel " + String ((int) i + 1));
	}
};

class ALSAAudioIODevice   : public AudioIODevice
{
public:
	ALSAAudioIODevice (const String& deviceName,
					   const String& inputId_,
					   const String& outputId_)
		: AudioIODevice (deviceName, "ALSA"),
		  inputId (inputId_),
		  outputId (outputId_),
		  isOpen_ (false),
		  isStarted (false),
		  internal (0)
	{
		internal = new ALSAThread (inputId, outputId);
	}

	~ALSAAudioIODevice()
	{
		delete internal;
	}

	const StringArray getOutputChannelNames()
	{
		return internal->channelNamesOut;
	}

	const StringArray getInputChannelNames()
	{
		return internal->channelNamesIn;
	}

	int getNumSampleRates()
	{
		return internal->sampleRates.size();
	}

	double getSampleRate (int index)
	{
		return internal->sampleRates [index];
	}

	int getNumBufferSizesAvailable()
	{
		return 50;
	}

	int getBufferSizeSamples (int index)
	{
		int n = 16;
		for (int i = 0; i < index; ++i)
			n += n < 64 ? 16
						: (n < 512 ? 32
								   : (n < 1024 ? 64
											   : (n < 2048 ? 128 : 256)));

		return n;
	}

	int getDefaultBufferSize()
	{
		return 512;
	}

	const String open (const BigInteger& inputChannels,
					   const BigInteger& outputChannels,
					   double sampleRate,
					   int bufferSizeSamples)
	{
		close();

		if (bufferSizeSamples <= 0)
			bufferSizeSamples = getDefaultBufferSize();

		if (sampleRate <= 0)
		{
			for (int i = 0; i < getNumSampleRates(); ++i)
			{
				if (getSampleRate (i) >= 44100)
				{
					sampleRate = getSampleRate (i);
					break;
				}
			}
		}

		internal->open (inputChannels, outputChannels,
						sampleRate, bufferSizeSamples);

		isOpen_ = internal->error.isEmpty();
		return internal->error;
	}

	void close()
	{
		stop();
		internal->close();
		isOpen_ = false;
	}

	bool isOpen()
	{
		return isOpen_;
	}

	int getCurrentBufferSizeSamples()
	{
		return internal->bufferSize;
	}

	double getCurrentSampleRate()
	{
		return internal->sampleRate;
	}

	int getCurrentBitDepth()
	{
		return internal->getBitDepth();
	}

	const BigInteger getActiveOutputChannels() const
	{
		return internal->currentOutputChans;
	}

	const BigInteger getActiveInputChannels() const
	{
		return internal->currentInputChans;
	}

	int getOutputLatencyInSamples()
	{
		return 0;
	}

	int getInputLatencyInSamples()
	{
		return 0;
	}

	void start (AudioIODeviceCallback* callback)
	{
		if (! isOpen_)
			callback = 0;

		internal->setCallback (callback);

		if (callback != 0)
			callback->audioDeviceAboutToStart (this);

		isStarted = (callback != 0);
	}

	void stop()
	{
		AudioIODeviceCallback* const oldCallback = internal->callback;

		start (0);

		if (oldCallback != 0)
			oldCallback->audioDeviceStopped();
	}

	bool isPlaying()
	{
		return isStarted && internal->error.isEmpty();
	}

	const String getLastError()
	{
		return internal->error;
	}

	String inputId, outputId;

private:
	bool isOpen_, isStarted;
	ALSAThread* internal;
};

class ALSAAudioIODeviceType  : public AudioIODeviceType
{
public:

	ALSAAudioIODeviceType()
		: AudioIODeviceType ("ALSA"),
		  hasScanned (false)
	{
	}

	~ALSAAudioIODeviceType()
	{
	}

	void scanForDevices()
	{
		if (hasScanned)
			return;

		hasScanned = true;
		inputNames.clear();
		inputIds.clear();
		outputNames.clear();
		outputIds.clear();

		snd_ctl_t* handle;
		snd_ctl_card_info_t* info;
		snd_ctl_card_info_alloca (&info);

		int cardNum = -1;

		while (outputIds.size() + inputIds.size() <= 32)
		{
			snd_card_next (&cardNum);

			if (cardNum < 0)
				break;

			if (snd_ctl_open (&handle, ("hw:" + String (cardNum)).toUTF8(), SND_CTL_NONBLOCK) >= 0)
			{
				if (snd_ctl_card_info (handle, info) >= 0)
				{
					String cardId (snd_ctl_card_info_get_id (info));

					if (cardId.removeCharacters ("0123456789").isEmpty())
						cardId = String (cardNum);

					int device = -1;

					for (;;)
					{
						if (snd_ctl_pcm_next_device (handle, &device) < 0 || device < 0)
							break;

						String id, name;
						id << "hw:" << cardId << ',' << device;

						bool isInput, isOutput;

						if (testDevice (id, isInput, isOutput))
						{
							name << snd_ctl_card_info_get_name (info);

							if (name.isEmpty())
								name = id;

							if (isInput)
							{
								inputNames.add (name);
								inputIds.add (id);
							}

							if (isOutput)
							{
								outputNames.add (name);
								outputIds.add (id);
							}
						}
					}
				}

				snd_ctl_close (handle);
			}
		}

		inputNames.appendNumbersToDuplicates (false, true);
		outputNames.appendNumbersToDuplicates (false, true);
	}

	const StringArray getDeviceNames (bool wantInputNames) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		return wantInputNames ? inputNames : outputNames;
	}

	int getDefaultDeviceIndex (bool forInput) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this
		return 0;
	}

	bool hasSeparateInputsAndOutputs() const	{ return true; }

	int getIndexOfDevice (AudioIODevice* device, bool asInput) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		ALSAAudioIODevice* d = dynamic_cast <ALSAAudioIODevice*> (device);
		if (d == 0)
			return -1;

		return asInput ? inputIds.indexOf (d->inputId)
					   : outputIds.indexOf (d->outputId);
	}

	AudioIODevice* createDevice (const String& outputDeviceName,
								 const String& inputDeviceName)
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		const int inputIndex = inputNames.indexOf (inputDeviceName);
		const int outputIndex = outputNames.indexOf (outputDeviceName);

		String deviceName (outputIndex >= 0 ? outputDeviceName
											: inputDeviceName);

		if (inputIndex >= 0 || outputIndex >= 0)
			return new ALSAAudioIODevice (deviceName,
										  inputIds [inputIndex],
										  outputIds [outputIndex]);

		return 0;
	}

	juce_UseDebuggingNewOperator

private:
	StringArray inputNames, outputNames, inputIds, outputIds;
	bool hasScanned;

	static bool testDevice (const String& id, bool& isInput, bool& isOutput)
	{
		unsigned int minChansOut = 0, maxChansOut = 0;
		unsigned int minChansIn = 0, maxChansIn = 0;
		Array <int> rates;

		getDeviceProperties (id, minChansOut, maxChansOut, minChansIn, maxChansIn, rates);

		DBG ("ALSA device: " + id
			  + " outs=" + String ((int) minChansOut) + "-" + String ((int) maxChansOut)
			  + " ins=" + String ((int) minChansIn) + "-" + String ((int) maxChansIn)
			  + " rates=" + String (rates.size()));

		isInput = maxChansIn > 0;
		isOutput = maxChansOut > 0;

		return (isInput || isOutput) && rates.size() > 0;
	}

	ALSAAudioIODeviceType (const ALSAAudioIODeviceType&);
	ALSAAudioIODeviceType& operator= (const ALSAAudioIODeviceType&);
};

AudioIODeviceType* juce_createAudioIODeviceType_ALSA()
{
	return new ALSAAudioIODeviceType();
}

#endif
/*** End of inlined file: juce_linux_Audio.cpp ***/


/*** Start of inlined file: juce_linux_JackAudio.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#ifdef JUCE_INCLUDED_FILE

#if JUCE_JACK

static void* juce_libjack_handle = 0;

void* juce_load_jack_function (const char* const name)
{
	if (juce_libjack_handle == 0)
		return 0;

	return dlsym (juce_libjack_handle, name);
}

#define JUCE_DECL_JACK_FUNCTION(return_type, fn_name, argument_types, arguments)  \
  typedef return_type (*fn_name##_ptr_t)argument_types;			   \
  return_type fn_name argument_types {					\
	static fn_name##_ptr_t fn = 0;						\
	if (fn == 0) { fn = (fn_name##_ptr_t)juce_load_jack_function(#fn_name); } \
	if (fn) return (*fn)arguments;						\
	else return 0;								\
  }

#define JUCE_DECL_VOID_JACK_FUNCTION(fn_name, argument_types, arguments)	  \
  typedef void (*fn_name##_ptr_t)argument_types;				  \
  void fn_name argument_types {						   \
	static fn_name##_ptr_t fn = 0;						\
	if (fn == 0) { fn = (fn_name##_ptr_t)juce_load_jack_function(#fn_name); } \
	if (fn) (*fn)arguments;						   \
  }

JUCE_DECL_JACK_FUNCTION (jack_client_t*, jack_client_open, (const char* client_name, jack_options_t options, jack_status_t* status), (client_name, options, status));
JUCE_DECL_JACK_FUNCTION (int, jack_client_close, (jack_client_t *client), (client));
JUCE_DECL_JACK_FUNCTION (int, jack_activate, (jack_client_t* client), (client));
JUCE_DECL_JACK_FUNCTION (int, jack_deactivate, (jack_client_t* client), (client));
JUCE_DECL_JACK_FUNCTION (jack_nframes_t, jack_get_buffer_size, (jack_client_t* client), (client));
JUCE_DECL_JACK_FUNCTION (jack_nframes_t, jack_get_sample_rate, (jack_client_t* client), (client));
JUCE_DECL_VOID_JACK_FUNCTION (jack_on_shutdown, (jack_client_t* client, void (*function)(void* arg), void* arg), (client, function, arg));
JUCE_DECL_JACK_FUNCTION (void* , jack_port_get_buffer, (jack_port_t* port, jack_nframes_t nframes), (port, nframes));
JUCE_DECL_JACK_FUNCTION (jack_nframes_t, jack_port_get_total_latency, (jack_client_t* client, jack_port_t* port), (client, port));
JUCE_DECL_JACK_FUNCTION (jack_port_t* , jack_port_register, (jack_client_t* client, const char* port_name, const char* port_type, unsigned long flags, unsigned long buffer_size), (client, port_name, port_type, flags, buffer_size));
JUCE_DECL_VOID_JACK_FUNCTION (jack_set_error_function, (void (*func)(const char*)), (func));
JUCE_DECL_JACK_FUNCTION (int, jack_set_process_callback, (jack_client_t* client, JackProcessCallback process_callback, void* arg), (client, process_callback, arg));
JUCE_DECL_JACK_FUNCTION (const char**, jack_get_ports, (jack_client_t* client, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags), (client, port_name_pattern, type_name_pattern, flags));
JUCE_DECL_JACK_FUNCTION (int, jack_connect, (jack_client_t* client, const char* source_port, const char* destination_port), (client, source_port, destination_port));
JUCE_DECL_JACK_FUNCTION (const char*, jack_port_name, (const jack_port_t* port), (port));
JUCE_DECL_JACK_FUNCTION (int, jack_set_port_connect_callback, (jack_client_t* client, JackPortConnectCallback connect_callback, void* arg), (client, connect_callback, arg));
JUCE_DECL_JACK_FUNCTION (jack_port_t* , jack_port_by_id, (jack_client_t* client, jack_port_id_t port_id), (client, port_id));
JUCE_DECL_JACK_FUNCTION (int, jack_port_connected, (const jack_port_t* port), (port));
JUCE_DECL_JACK_FUNCTION (int, jack_port_connected_to, (const jack_port_t* port, const char* port_name), (port, port_name));

#if JUCE_DEBUG
  #define JACK_LOGGING_ENABLED 1
#endif

#if JACK_LOGGING_ENABLED
static void jack_Log (const String& s)
{
	std::cerr << s << std::endl;
}

static void dumpJackErrorMessage (const jack_status_t status) throw()
{
	if (status & JackServerFailed || status & JackServerError)
		jack_Log ("Unable to connect to JACK server");
	if (status & JackVersionError)
		jack_Log ("Client's protocol version does not match");
	if (status & JackInvalidOption)
		jack_Log ("The operation contained an invalid or unsupported option");
	if (status & JackNameNotUnique)
		jack_Log ("The desired client name was not unique");
	if (status & JackNoSuchClient)
		jack_Log ("Requested client does not exist");
	if (status & JackInitFailure)
		jack_Log ("Unable to initialize client");
}
#else
  #define dumpJackErrorMessage(a) {}
  #define jack_Log(...) {}
#endif

#ifndef JUCE_JACK_CLIENT_NAME
  #define JUCE_JACK_CLIENT_NAME "JuceJack"
#endif

class JackAudioIODevice   : public AudioIODevice
{
public:
	JackAudioIODevice (const String& deviceName,
					   const String& inputId_,
					   const String& outputId_)
		: AudioIODevice (deviceName, "JACK"),
		  inputId (inputId_),
		  outputId (outputId_),
		  isOpen_ (false),
		  callback (0),
		  totalNumberOfInputChannels (0),
		  totalNumberOfOutputChannels (0)
	{
		jassert (deviceName.isNotEmpty());

		jack_status_t status;
		client = JUCE_NAMESPACE::jack_client_open (JUCE_JACK_CLIENT_NAME, JackNoStartServer, &status);

		if (client == 0)
		{
			dumpJackErrorMessage (status);
		}
		else
		{
			JUCE_NAMESPACE::jack_set_error_function (errorCallback);

			// open input ports
			const StringArray inputChannels (getInputChannelNames());
			for (int i = 0; i < inputChannels.size(); i++)
			{
				String inputName;
				inputName << "in_" << ++totalNumberOfInputChannels;

				inputPorts.add (JUCE_NAMESPACE::jack_port_register (client, inputName.toUTF8(),
																	JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0));
			}

			// open output ports
			const StringArray outputChannels (getOutputChannelNames());
			for (int i = 0; i < outputChannels.size (); i++)
			{
				String outputName;
				outputName << "out_" << ++totalNumberOfOutputChannels;

				outputPorts.add (JUCE_NAMESPACE::jack_port_register (client, outputName.toUTF8(),
																	 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0));
			}

			inChans.calloc (totalNumberOfInputChannels + 2);
			outChans.calloc (totalNumberOfOutputChannels + 2);
		}
	}

	~JackAudioIODevice()
	{
		close();
		if (client != 0)
		{
			JUCE_NAMESPACE::jack_client_close (client);
			client = 0;
		}
	}

	const StringArray getChannelNames (bool forInput) const
	{
		StringArray names;
		const char** const ports = JUCE_NAMESPACE::jack_get_ports (client, 0, 0, /* JackPortIsPhysical | */
																   forInput ? JackPortIsInput : JackPortIsOutput);

		if (ports != 0)
		{
			int j = 0;
			while (ports[j] != 0)
			{
				const String portName (ports [j++]);

				if (portName.upToFirstOccurrenceOf (":", false, false) == getName())
					names.add (portName.fromFirstOccurrenceOf (":", false, false));
			}

			free (ports);
		}

		return names;
	}

	const StringArray getOutputChannelNames()   { return getChannelNames (false); }
	const StringArray getInputChannelNames()	{ return getChannelNames (true); }
	int getNumSampleRates()			 { return client != 0 ? 1 : 0; }
	double getSampleRate (int index)		{ return client != 0 ? JUCE_NAMESPACE::jack_get_sample_rate (client) : 0; }
	int getNumBufferSizesAvailable()		{ return client != 0 ? 1 : 0; }
	int getBufferSizeSamples (int index)	{ return getDefaultBufferSize(); }
	int getDefaultBufferSize()		  { return client != 0 ? JUCE_NAMESPACE::jack_get_buffer_size (client) : 0; }

	const String open (const BigInteger& inputChannels, const BigInteger& outputChannels,
					   double sampleRate, int bufferSizeSamples)
	{
		if (client == 0)
		{
			lastError = "No JACK client running";
			return lastError;
		}

		lastError = String::empty;
		close();

		JUCE_NAMESPACE::jack_set_process_callback (client, processCallback, this);
		JUCE_NAMESPACE::jack_on_shutdown (client, shutdownCallback, this);
		JUCE_NAMESPACE::jack_activate (client);
		isOpen_ = true;

		if (! inputChannels.isZero())
		{
			const char** const ports = JUCE_NAMESPACE::jack_get_ports (client, 0, 0, /* JackPortIsPhysical | */ JackPortIsOutput);

			if (ports != 0)
			{
				const int numInputChannels = inputChannels.getHighestBit() + 1;

				for (int i = 0; i < numInputChannels; ++i)
				{
					const String portName (ports[i]);

					if (inputChannels[i] && portName.upToFirstOccurrenceOf (":", false, false) == getName())
					{
						int error = JUCE_NAMESPACE::jack_connect (client, ports[i], JUCE_NAMESPACE::jack_port_name ((jack_port_t*) inputPorts[i]));
						if (error != 0)
							jack_Log ("Cannot connect input port " + String (i) + " (" + String (ports[i]) + "), error " + String (error));
					}
				}

				free (ports);
			}
		}

		if (! outputChannels.isZero())
		{
			const char** const ports = JUCE_NAMESPACE::jack_get_ports (client, 0, 0, /* JackPortIsPhysical | */ JackPortIsInput);

			if (ports != 0)
			{
				const int numOutputChannels = outputChannels.getHighestBit() + 1;

				for (int i = 0; i < numOutputChannels; ++i)
				{
					const String portName (ports[i]);

					if (outputChannels[i] && portName.upToFirstOccurrenceOf (":", false, false) == getName())
					{
						int error = JUCE_NAMESPACE::jack_connect (client, JUCE_NAMESPACE::jack_port_name ((jack_port_t*) outputPorts[i]), ports[i]);
						if (error != 0)
							jack_Log ("Cannot connect output port " + String (i) + " (" + String (ports[i]) + "), error " + String (error));
					}
				}

				free (ports);
			}
		}

		return lastError;
	}

	void close()
	{
		stop();

		if (client != 0)
		{
			JUCE_NAMESPACE::jack_deactivate (client);
			JUCE_NAMESPACE::jack_set_process_callback (client, processCallback, 0);
			JUCE_NAMESPACE::jack_on_shutdown (client, shutdownCallback, 0);
		}

		isOpen_ = false;
	}

	void start (AudioIODeviceCallback* newCallback)
	{
		if (isOpen_ && newCallback != callback)
		{
			if (newCallback != 0)
				newCallback->audioDeviceAboutToStart (this);

			AudioIODeviceCallback* const oldCallback = callback;

			{
				const ScopedLock sl (callbackLock);
				callback = newCallback;
			}

			if (oldCallback != 0)
				oldCallback->audioDeviceStopped();
		}
	}

	void stop()
	{
		start (0);
	}

	bool isOpen()			   { return isOpen_; }
	bool isPlaying()			{ return callback != 0; }
	int getCurrentBufferSizeSamples()	   { return getBufferSizeSamples (0); }
	double getCurrentSampleRate()	   { return getSampleRate (0); }
	int getCurrentBitDepth()		{ return 32; }
	const String getLastError()		 { return lastError; }

	const BigInteger getActiveOutputChannels() const
	{
		BigInteger outputBits;

		for (int i = 0; i < outputPorts.size(); i++)
			if (JUCE_NAMESPACE::jack_port_connected ((jack_port_t*) outputPorts [i]))
				outputBits.setBit (i);

		return outputBits;
	}

	const BigInteger getActiveInputChannels() const
	{
		BigInteger inputBits;

		for (int i = 0; i < inputPorts.size(); i++)
			if (JUCE_NAMESPACE::jack_port_connected ((jack_port_t*) inputPorts [i]))
				inputBits.setBit (i);

		return inputBits;
	}

	int getOutputLatencyInSamples()
	{
		int latency = 0;

		for (int i = 0; i < outputPorts.size(); i++)
			latency = jmax (latency, (int) JUCE_NAMESPACE::jack_port_get_total_latency (client, (jack_port_t*) outputPorts [i]));

		return latency;
	}

	int getInputLatencyInSamples()
	{
		int latency = 0;

		for (int i = 0; i < inputPorts.size(); i++)
			latency = jmax (latency, (int) JUCE_NAMESPACE::jack_port_get_total_latency (client, (jack_port_t*) inputPorts [i]));

		return latency;
	}

	String inputId, outputId;

private:
	void process (const int numSamples)
	{
		int i, numActiveInChans = 0, numActiveOutChans = 0;

		for (i = 0; i < totalNumberOfInputChannels; ++i)
		{
			jack_default_audio_sample_t* in
				= (jack_default_audio_sample_t*) JUCE_NAMESPACE::jack_port_get_buffer ((jack_port_t*) inputPorts.getUnchecked(i), numSamples);

			if (in != 0)
				inChans [numActiveInChans++] = (float*) in;
		}

		for (i = 0; i < totalNumberOfOutputChannels; ++i)
		{
			jack_default_audio_sample_t* out
				= (jack_default_audio_sample_t*) JUCE_NAMESPACE::jack_port_get_buffer ((jack_port_t*) outputPorts.getUnchecked(i), numSamples);

			if (out != 0)
				outChans [numActiveOutChans++] = (float*) out;
		}

		const ScopedLock sl (callbackLock);

		if (callback != 0)
		{
			callback->audioDeviceIOCallback (const_cast<const float**> (inChans.getData()), numActiveInChans,
											 outChans, numActiveOutChans, numSamples);
		}
		else
		{
			for (i = 0; i < numActiveOutChans; ++i)
				zeromem (outChans[i], sizeof (float) * numSamples);
		}
	}

	static int processCallback (jack_nframes_t nframes, void* callbackArgument)
	{
		if (callbackArgument != 0)
			((JackAudioIODevice*) callbackArgument)->process (nframes);

		return 0;
	}

	static void threadInitCallback (void* callbackArgument)
	{
		jack_Log ("JackAudioIODevice::initialise");
	}

	static void shutdownCallback (void* callbackArgument)
	{
		jack_Log ("JackAudioIODevice::shutdown");

		JackAudioIODevice* device = (JackAudioIODevice*) callbackArgument;

		if (device != 0)
		{
			device->client = 0;
			device->close();
		}
	}

	static void errorCallback (const char* msg)
	{
		jack_Log ("JackAudioIODevice::errorCallback " + String (msg));
	}

	bool isOpen_;
	jack_client_t* client;
	String lastError;
	AudioIODeviceCallback* callback;
	CriticalSection callbackLock;

	HeapBlock <float*> inChans, outChans;
	int totalNumberOfInputChannels;
	int totalNumberOfOutputChannels;
	VoidArray inputPorts, outputPorts;
};

class JackAudioIODeviceType  : public AudioIODeviceType
{
public:

	JackAudioIODeviceType()
		: AudioIODeviceType ("JACK"),
		  hasScanned (false)
	{
	}

	~JackAudioIODeviceType()
	{
	}

	void scanForDevices()
	{
		hasScanned = true;
		inputNames.clear();
		inputIds.clear();
		outputNames.clear();
		outputIds.clear();

		if (juce_libjack_handle == 0)
		{
			juce_libjack_handle = dlopen ("libjack.so", RTLD_LAZY);

			if (juce_libjack_handle == 0)
				return;
		}

		// open a dummy client
		jack_status_t status;
		jack_client_t* client = JUCE_NAMESPACE::jack_client_open ("JuceJackDummy", JackNoStartServer, &status);

		if (client == 0)
		{
			dumpJackErrorMessage (status);
		}
		else
		{
			// scan for output devices
			const char** ports = JUCE_NAMESPACE::jack_get_ports (client, 0, 0, /* JackPortIsPhysical | */ JackPortIsOutput);

			if (ports != 0)
			{
				int j = 0;
				while (ports[j] != 0)
				{
					String clientName (ports[j]);
					clientName = clientName.upToFirstOccurrenceOf (":", false, false);

					if (clientName != String (JUCE_JACK_CLIENT_NAME)
						 && ! inputNames.contains (clientName))
					{
						inputNames.add (clientName);
						inputIds.add (ports [j]);
					}

					++j;
				}

				free (ports);
			}

			// scan for input devices
			ports = JUCE_NAMESPACE::jack_get_ports (client, 0, 0, /* JackPortIsPhysical | */ JackPortIsInput);

			if (ports != 0)
			{
				int j = 0;
				while (ports[j] != 0)
				{
					String clientName (ports[j]);
					clientName = clientName.upToFirstOccurrenceOf (":", false, false);

					if (clientName != String (JUCE_JACK_CLIENT_NAME)
						 && ! outputNames.contains (clientName))
					{
						outputNames.add (clientName);
						outputIds.add (ports [j]);
					}

					++j;
				}

				free (ports);
			}

			JUCE_NAMESPACE::jack_client_close (client);
		}
	}

	const StringArray getDeviceNames (bool wantInputNames) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this
		return wantInputNames ? inputNames : outputNames;
	}

	int getDefaultDeviceIndex (bool forInput) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this
		return 0;
	}

	bool hasSeparateInputsAndOutputs() const	{ return true; }

	int getIndexOfDevice (AudioIODevice* device, bool asInput) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		JackAudioIODevice* d = dynamic_cast <JackAudioIODevice*> (device);
		if (d == 0)
			return -1;

		return asInput ? inputIds.indexOf (d->inputId)
					   : outputIds.indexOf (d->outputId);
	}

	AudioIODevice* createDevice (const String& outputDeviceName,
								 const String& inputDeviceName)
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		const int inputIndex = inputNames.indexOf (inputDeviceName);
		const int outputIndex = outputNames.indexOf (outputDeviceName);

		if (inputIndex >= 0 || outputIndex >= 0)
			return new JackAudioIODevice (outputIndex >= 0 ? outputDeviceName
														   : inputDeviceName,
										  inputIds [inputIndex],
										  outputIds [outputIndex]);

		return 0;
	}

	juce_UseDebuggingNewOperator

private:
	StringArray inputNames, outputNames, inputIds, outputIds;
	bool hasScanned;

	JackAudioIODeviceType (const JackAudioIODeviceType&);
	JackAudioIODeviceType& operator= (const JackAudioIODeviceType&);
};

AudioIODeviceType* juce_createAudioIODeviceType_JACK()
{
	return new JackAudioIODeviceType();
}

#else  // if JACK is turned off..

AudioIODeviceType* juce_createAudioIODeviceType_JACK()	{ return 0; }

#endif
#endif
/*** End of inlined file: juce_linux_JackAudio.cpp ***/


/*** Start of inlined file: juce_linux_Midi.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE
#if JUCE_ALSA

static snd_seq_t* iterateDevices (const bool forInput,
								  StringArray& deviceNamesFound,
								  const int deviceIndexToOpen)
{
	snd_seq_t* returnedHandle = 0;
	snd_seq_t* seqHandle;

	if (snd_seq_open (&seqHandle, "default", forInput ? SND_SEQ_OPEN_INPUT
													  : SND_SEQ_OPEN_OUTPUT, 0) == 0)
	{
		snd_seq_system_info_t* systemInfo;
		snd_seq_client_info_t* clientInfo;

		if (snd_seq_system_info_malloc (&systemInfo) == 0)
		{
			if (snd_seq_system_info (seqHandle, systemInfo) == 0
				 && snd_seq_client_info_malloc (&clientInfo) == 0)
			{
				int numClients = snd_seq_system_info_get_cur_clients (systemInfo);

				while (--numClients >= 0 && returnedHandle == 0)
				{
					if (snd_seq_query_next_client (seqHandle, clientInfo) == 0)
					{
						snd_seq_port_info_t* portInfo;
						if (snd_seq_port_info_malloc (&portInfo) == 0)
						{
							int numPorts = snd_seq_client_info_get_num_ports (clientInfo);
							const int client = snd_seq_client_info_get_client (clientInfo);

							snd_seq_port_info_set_client (portInfo, client);
							snd_seq_port_info_set_port (portInfo, -1);

							while (--numPorts >= 0)
							{
								if (snd_seq_query_next_port (seqHandle, portInfo) == 0
									 && (snd_seq_port_info_get_capability (portInfo)
										   & (forInput ? SND_SEQ_PORT_CAP_READ
													   : SND_SEQ_PORT_CAP_WRITE)) != 0)
								{
									deviceNamesFound.add (snd_seq_client_info_get_name (clientInfo));

									if (deviceNamesFound.size() == deviceIndexToOpen + 1)
									{
										const int sourcePort = snd_seq_port_info_get_port (portInfo);
										const int sourceClient = snd_seq_client_info_get_client (clientInfo);

										if (sourcePort != -1)
										{
											snd_seq_set_client_name (seqHandle,
																	 forInput ? "Juce Midi Input"
																			  : "Juce Midi Output");

											const int portId
												= snd_seq_create_simple_port (seqHandle,
																			  forInput ? "Juce Midi In Port"
																					   : "Juce Midi Out Port",
																			  forInput ? (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE)
																					   : (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ),
																			  SND_SEQ_PORT_TYPE_MIDI_GENERIC);

											snd_seq_connect_from (seqHandle, portId, sourceClient, sourcePort);

											returnedHandle = seqHandle;
										}
									}
								}
							}

							snd_seq_port_info_free (portInfo);
						}
					}
				}

				snd_seq_client_info_free (clientInfo);
			}

			snd_seq_system_info_free (systemInfo);
		}

		if (returnedHandle == 0)
			snd_seq_close (seqHandle);
	}

	deviceNamesFound.appendNumbersToDuplicates (true, true);

	return returnedHandle;
}

static snd_seq_t* createDevice (const bool forInput,
								const String& deviceNameToOpen)
{
	snd_seq_t* seqHandle = 0;

	if (snd_seq_open (&seqHandle, "default", forInput ? SND_SEQ_OPEN_INPUT
													  : SND_SEQ_OPEN_OUTPUT, 0) == 0)
	{
		snd_seq_set_client_name (seqHandle,
								 (deviceNameToOpen + (forInput ? " Input" : " Output")).toCString());

		const int portId
			= snd_seq_create_simple_port (seqHandle,
										  forInput ? "in"
												   : "out",
										  forInput ? (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE)
												   : (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ),
										  forInput ? SND_SEQ_PORT_TYPE_APPLICATION
												   : SND_SEQ_PORT_TYPE_MIDI_GENERIC);

		if (portId < 0)
		{
			snd_seq_close (seqHandle);
			seqHandle = 0;
		}
	}

	return seqHandle;
}

class MidiOutputDevice
{
public:
	MidiOutputDevice (MidiOutput* const midiOutput_,
					  snd_seq_t* const seqHandle_)
		:
		  midiOutput (midiOutput_),
		  seqHandle (seqHandle_),
		  maxEventSize (16 * 1024)
	{
		jassert (seqHandle != 0 && midiOutput != 0);
		snd_midi_event_new (maxEventSize, &midiParser);
	}

	~MidiOutputDevice()
	{
		snd_midi_event_free (midiParser);
		snd_seq_close (seqHandle);
	}

	void sendMessageNow (const MidiMessage& message)
	{
		if (message.getRawDataSize() > maxEventSize)
		{
			maxEventSize = message.getRawDataSize();
			snd_midi_event_free (midiParser);
			snd_midi_event_new (maxEventSize, &midiParser);
		}

		snd_seq_event_t event;
		snd_seq_ev_clear (&event);

		snd_midi_event_encode (midiParser,
							   message.getRawData(),
							   message.getRawDataSize(),
							   &event);

		snd_midi_event_reset_encode (midiParser);

		snd_seq_ev_set_source (&event, 0);
		snd_seq_ev_set_subs (&event);
		snd_seq_ev_set_direct (&event);

		snd_seq_event_output (seqHandle, &event);
		snd_seq_drain_output (seqHandle);
	}

	juce_UseDebuggingNewOperator

private:
	MidiOutput* const midiOutput;
	snd_seq_t* const seqHandle;
	snd_midi_event_t* midiParser;
	int maxEventSize;
};

const StringArray MidiOutput::getDevices()
{
	StringArray devices;
	iterateDevices (false, devices, -1);
	return devices;
}

int MidiOutput::getDefaultDeviceIndex()
{
	return 0;
}

MidiOutput* MidiOutput::openDevice (int deviceIndex)
{
	MidiOutput* newDevice = 0;

	StringArray devices;
	snd_seq_t* const handle = iterateDevices (false, devices, deviceIndex);

	if (handle != 0)
	{
		newDevice = new MidiOutput();
		newDevice->internal = new MidiOutputDevice (newDevice, handle);
	}

	return newDevice;
}

MidiOutput* MidiOutput::createNewDevice (const String& deviceName)
{
	MidiOutput* newDevice = 0;

	snd_seq_t* const handle = createDevice (false, deviceName);

	if (handle != 0)
	{
		newDevice = new MidiOutput();
		newDevice->internal = new MidiOutputDevice (newDevice, handle);
	}

	return newDevice;
}

MidiOutput::~MidiOutput()
{
	MidiOutputDevice* const device = (MidiOutputDevice*) internal;
	delete device;
}

void MidiOutput::reset()
{
}

bool MidiOutput::getVolume (float& leftVol, float& rightVol)
{
	return false;
}

void MidiOutput::setVolume (float leftVol, float rightVol)
{
}

void MidiOutput::sendMessageNow (const MidiMessage& message)
{
	((MidiOutputDevice*) internal)->sendMessageNow (message);
}

class MidiInputThread   : public Thread
{
public:
	MidiInputThread (MidiInput* const midiInput_,
					 snd_seq_t* const seqHandle_,
					 MidiInputCallback* const callback_)
		: Thread ("Juce MIDI Input"),
		  midiInput (midiInput_),
		  seqHandle (seqHandle_),
		  callback (callback_)
	{
		jassert (seqHandle != 0 && callback != 0 && midiInput != 0);
	}

	~MidiInputThread()
	{
		snd_seq_close (seqHandle);
	}

	void run()
	{
		const int maxEventSize = 16 * 1024;
		snd_midi_event_t* midiParser;

		if (snd_midi_event_new (maxEventSize, &midiParser) >= 0)
		{
			HeapBlock <uint8> buffer (maxEventSize);

			const int numPfds = snd_seq_poll_descriptors_count (seqHandle, POLLIN);
			struct pollfd* const pfd = (struct pollfd*) alloca (numPfds * sizeof (struct pollfd));

			snd_seq_poll_descriptors (seqHandle, pfd, numPfds, POLLIN);

			while (! threadShouldExit())
			{
				if (poll (pfd, numPfds, 500) > 0)
				{
					snd_seq_event_t* inputEvent = 0;

					snd_seq_nonblock (seqHandle, 1);

					do
					{
						if (snd_seq_event_input (seqHandle, &inputEvent) >= 0)
						{
							// xxx what about SYSEXes that are too big for the buffer?
							const int numBytes = snd_midi_event_decode (midiParser, buffer, maxEventSize, inputEvent);

							snd_midi_event_reset_decode (midiParser);

							if (numBytes > 0)
							{
								const MidiMessage message ((const uint8*) buffer,
														   numBytes,
														   Time::getMillisecondCounter() * 0.001);

								callback->handleIncomingMidiMessage (midiInput, message);
							}

							snd_seq_free_event (inputEvent);
						}
					}
					while (snd_seq_event_input_pending (seqHandle, 0) > 0);

					snd_seq_free_event (inputEvent);
				}
			}

			snd_midi_event_free (midiParser);
		}
	};

	juce_UseDebuggingNewOperator

private:
	MidiInput* const midiInput;
	snd_seq_t* const seqHandle;
	MidiInputCallback* const callback;
};

MidiInput::MidiInput (const String& name_)
	: name (name_),
	  internal (0)
{
}

MidiInput::~MidiInput()
{
	stop();
	MidiInputThread* const thread = (MidiInputThread*) internal;
	delete thread;
}

void MidiInput::start()
{
	((MidiInputThread*) internal)->startThread();
}

void MidiInput::stop()
{
	((MidiInputThread*) internal)->stopThread (3000);
}

int MidiInput::getDefaultDeviceIndex()
{
	return 0;
}

const StringArray MidiInput::getDevices()
{
	StringArray devices;
	iterateDevices (true, devices, -1);
	return devices;
}

MidiInput* MidiInput::openDevice (int deviceIndex, MidiInputCallback* callback)
{
	MidiInput* newDevice = 0;

	StringArray devices;
	snd_seq_t* const handle = iterateDevices (true, devices, deviceIndex);

	if (handle != 0)
	{
		newDevice = new MidiInput (devices [deviceIndex]);
		newDevice->internal = new MidiInputThread (newDevice, handle, callback);
	}

	return newDevice;
}

MidiInput* MidiInput::createNewDevice (const String& deviceName, MidiInputCallback* callback)
{
	MidiInput* newDevice = 0;

	snd_seq_t* const handle = createDevice (true, deviceName);

	if (handle != 0)
	{
		newDevice = new MidiInput (deviceName);
		newDevice->internal = new MidiInputThread (newDevice, handle, callback);
	}

	return newDevice;
}

#else

// (These are just stub functions if ALSA is unavailable...)

const StringArray MidiOutput::getDevices()			  { return StringArray(); }
int MidiOutput::getDefaultDeviceIndex()				 { return 0; }
MidiOutput* MidiOutput::openDevice (int)				{ return 0; }
MidiOutput* MidiOutput::createNewDevice (const String&)		 { return 0; }
MidiOutput::~MidiOutput()   {}
void MidiOutput::reset()	{}
bool MidiOutput::getVolume (float&, float&)	 { return false; }
void MidiOutput::setVolume (float, float)	   {}
void MidiOutput::sendMessageNow (const MidiMessage&)	{}

MidiInput::MidiInput (const String& name_) : name (name_), internal (0)  {}
MidiInput::~MidiInput() {}
void MidiInput::start() {}
void MidiInput::stop()  {}
int MidiInput::getDefaultDeviceIndex()	  { return 0; }
const StringArray MidiInput::getDevices()   { return StringArray(); }
MidiInput* MidiInput::openDevice (int, MidiInputCallback*)		  { return 0; }
MidiInput* MidiInput::createNewDevice (const String&, MidiInputCallback*)   { return 0; }

#endif
#endif
/*** End of inlined file: juce_linux_Midi.cpp ***/


/*** Start of inlined file: juce_linux_AudioCDReader.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_USE_CDREADER

AudioCDReader::AudioCDReader()
	: AudioFormatReader (0, "CD Audio")
{
}

const StringArray AudioCDReader::getAvailableCDNames()
{
	StringArray names;
	return names;
}

AudioCDReader* AudioCDReader::createReaderForCD (const int index)
{
	return 0;
}

AudioCDReader::~AudioCDReader()
{
}

void AudioCDReader::refreshTrackLengths()
{
}

bool AudioCDReader::readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
								 int64 startSampleInFile, int numSamples)
{
	return false;
}

bool AudioCDReader::isCDStillPresent() const
{
	return false;
}

int AudioCDReader::getNumTracks() const
{
	return 0;
}

int AudioCDReader::getPositionOfTrackStart (int trackNum) const
{
	return 0;
}

bool AudioCDReader::isTrackAudio (int trackNum) const
{
	return false;
}

void AudioCDReader::enableIndexScanning (bool b)
{
}

int AudioCDReader::getLastIndex() const
{
	return 0;
}

const Array<int> AudioCDReader::findIndexesInTrack (const int trackNumber)
{
	return Array<int>();
}

int AudioCDReader::getCDDBId()
{
	return 0;
}

#endif
/*** End of inlined file: juce_linux_AudioCDReader.cpp ***/


/*** Start of inlined file: juce_linux_FileChooser.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

void FileChooser::showPlatformDialog (Array<File>& results,
									  const String& title,
									  const File& file,
									  const String& filters,
									  bool isDirectory,
									  bool selectsFiles,
									  bool isSave,
									  bool warnAboutOverwritingExistingFiles,
									  bool selectMultipleFiles,
									  FilePreviewComponent* previewComponent)
{
	const String separator (":");
	String command ("zenity --file-selection");

	if (title.isNotEmpty())
		command << " --title=\"" << title << "\"";

	if (file != File::nonexistent)
		command << " --filename=\"" << file.getFullPathName () << "\"";

	if (isDirectory)
		command << " --directory";

	if (isSave)
		command << " --save";

	if (selectMultipleFiles)
		command << " --multiple --separator=\"" << separator << "\"";

	command << " 2>&1";

	MemoryOutputStream result;
	int status = -1;
	FILE* stream = popen (command.toUTF8(), "r");

	if (stream != 0)
	{
		for (;;)
		{
			char buffer [1024];
			const int bytesRead = fread (buffer, 1, sizeof (buffer), stream);

			if (bytesRead <= 0)
				break;

			result.write (buffer, bytesRead);
		}

		status = pclose (stream);
	}

	if (status == 0)
	{
		StringArray tokens;

		if (selectMultipleFiles)
			tokens.addTokens (result.toUTF8(), separator, String::empty);
		else
			tokens.add (result.toUTF8());

		for (int i = 0; i < tokens.size(); i++)
			results.add (File (tokens[i]));

		return;
	}

	//xxx ain't got one!
	jassertfalse
}

#endif
/*** End of inlined file: juce_linux_FileChooser.cpp ***/


/*** Start of inlined file: juce_linux_WebBrowserComponent.cpp ***/
// (This file gets included by juce_linux_NativeCode.cpp, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_WEB_BROWSER

WebBrowserComponent::WebBrowserComponent (const bool unloadPageWhenBrowserIsHidden_)
	: browser (0),
	  blankPageShown (false),
	  unloadPageWhenBrowserIsHidden (unloadPageWhenBrowserIsHidden_)
{
	setOpaque (true);
}

WebBrowserComponent::~WebBrowserComponent()
{
}

void WebBrowserComponent::goToURL (const String& url,
								   const StringArray* headers,
								   const MemoryBlock* postData)
{
	lastURL = url;

	lastHeaders.clear();
	if (headers != 0)
		lastHeaders = *headers;

	lastPostData.setSize (0);
	if (postData != 0)
		lastPostData = *postData;

	blankPageShown = false;

}

void WebBrowserComponent::stop()
{
}

void WebBrowserComponent::goBack()
{
	lastURL = String::empty;
	blankPageShown = false;

}

void WebBrowserComponent::goForward()
{
	lastURL = String::empty;

}

void WebBrowserComponent::refresh()
{
}

void WebBrowserComponent::paint (Graphics& g)
{
	g.fillAll (Colours::white);
}

void WebBrowserComponent::checkWindowAssociation()
{
}

void WebBrowserComponent::reloadLastURL()
{
	if (lastURL.isNotEmpty())
	{
		goToURL (lastURL, &lastHeaders, &lastPostData);
		lastURL = String::empty;
	}
}

void WebBrowserComponent::parentHierarchyChanged()
{
	checkWindowAssociation();
}

void WebBrowserComponent::resized()
{
}

void WebBrowserComponent::visibilityChanged()
{
	checkWindowAssociation();
}

bool WebBrowserComponent::pageAboutToLoad (const String& url)
{
	return true;
}

#endif
/*** End of inlined file: juce_linux_WebBrowserComponent.cpp ***/

#endif

END_JUCE_NAMESPACE

#endif
/*** End of inlined file: juce_linux_NativeCode.cpp ***/


 #endif

 #if JUCE_MAC || JUCE_IPHONE

/*** Start of inlined file: juce_mac_NativeCode.mm ***/
#if JUCE_MAC || JUCE_IPHONE

BEGIN_JUCE_NAMESPACE

#undef Point

#define JUCE_INCLUDED_FILE 1

// Now include the actual code files..


/*** Start of inlined file: juce_mac_ObjCSuffix.h ***/
#ifndef JUCE_ObjCExtraSuffix
 #define JUCE_ObjCExtraSuffix 3
#endif

#define appendMacro1(a, b, c, d, e) a ## _ ## b ## _ ## c ## _ ## d ## _ ## e
#define appendMacro2(a, b, c, d, e) appendMacro1(a, b, c, d, e)
#define MakeObjCClassName(rootName)  appendMacro2 (rootName, JUCE_MAJOR_VERSION, JUCE_MINOR_VERSION, JUCE_BUILDNUMBER, JUCE_ObjCExtraSuffix)
/*** End of inlined file: juce_mac_ObjCSuffix.h ***/


/*** Start of inlined file: juce_mac_Strings.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

static const String nsStringToJuce (NSString* s)
{
	return String::fromUTF8 ([s UTF8String]);
}

static NSString* juceStringToNS (const String& s)
{
	return [NSString stringWithUTF8String: s.toUTF8()];
}

static const String convertUTF16ToString (const UniChar* utf16)
{
	String s;

	while (*utf16 != 0)
		s += (juce_wchar) *utf16++;

	return s;
}

const String PlatformUtilities::cfStringToJuceString (CFStringRef cfString)
{
	String result;

	if (cfString != 0)
	{
		CFRange range = { 0, CFStringGetLength (cfString) };
		HeapBlock <UniChar> u (range.length + 1);
		CFStringGetCharacters (cfString, range, u);
		u[range.length] = 0;
		result = convertUTF16ToString (u);
	}

	return result;
}

CFStringRef PlatformUtilities::juceStringToCFString (const String& s)
{
	const int len = s.length();
	HeapBlock <UniChar> temp (len + 2);

	for (int i = 0; i <= len; ++i)
		temp[i] = s[i];

	return CFStringCreateWithCharacters (kCFAllocatorDefault, temp, len);
}

const String PlatformUtilities::convertToPrecomposedUnicode (const String& s)
{
#if JUCE_IPHONE
	const ScopedAutoReleasePool pool;
	return nsStringToJuce ([juceStringToNS (s) precomposedStringWithCanonicalMapping]);
#else
	UnicodeMapping map;

	map.unicodeEncoding = CreateTextEncoding (kTextEncodingUnicodeDefault,
											  kUnicodeNoSubset,
											  kTextEncodingDefaultFormat);

	map.otherEncoding = CreateTextEncoding (kTextEncodingUnicodeDefault,
											kUnicodeCanonicalCompVariant,
											kTextEncodingDefaultFormat);

	map.mappingVersion = kUnicodeUseLatestMapping;

	UnicodeToTextInfo conversionInfo = 0;
	String result;

	if (CreateUnicodeToTextInfo (&map, &conversionInfo) == noErr)
	{
		const int len = s.length();

		HeapBlock <UniChar> tempIn, tempOut;
		tempIn.calloc (len + 2);
		tempOut.calloc (len + 2);

		for (int i = 0; i <= len; ++i)
			tempIn[i] = s[i];

		ByteCount bytesRead = 0;
		ByteCount outputBufferSize = 0;

		if (ConvertFromUnicodeToText (conversionInfo,
									  len * sizeof (UniChar), tempIn,
									  kUnicodeDefaultDirectionMask,
									  0, 0, 0, 0,
									  len * sizeof (UniChar), &bytesRead,
									  &outputBufferSize, tempOut) == noErr)
		{
			result.preallocateStorage (bytesRead / sizeof (UniChar) + 2);

			juce_wchar* t = result;

			unsigned int i;
			for (i = 0; i < bytesRead / sizeof (UniChar); ++i)
				t[i] = (juce_wchar) tempOut[i];

			t[i] = 0;
		}

		DisposeUnicodeToTextInfo (&conversionInfo);
	}

	return result;
#endif
}

#if ! JUCE_ONLY_BUILD_CORE_LIBRARY

void SystemClipboard::copyTextToClipboard (const String& text)
{
#if JUCE_IPHONE
	[[UIPasteboard generalPasteboard] setValue: juceStringToNS (text)
							 forPasteboardType: @"public.text"];
#else
	[[NSPasteboard generalPasteboard] declareTypes: [NSArray arrayWithObject: NSStringPboardType]
											 owner: nil];

	[[NSPasteboard generalPasteboard] setString: juceStringToNS (text)
										forType: NSStringPboardType];
#endif
}

const String SystemClipboard::getTextFromClipboard()
{
#if JUCE_IPHONE
	NSString* text = [[UIPasteboard generalPasteboard] valueForPasteboardType: @"public.text"];
#else
	NSString* text = [[NSPasteboard generalPasteboard] stringForType: NSStringPboardType];
#endif

	return text == 0 ? String::empty
					 : nsStringToJuce (text);
}

#endif

#endif
/*** End of inlined file: juce_mac_Strings.mm ***/


/*** Start of inlined file: juce_mac_SystemStats.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

namespace SystemStatsHelpers
{
	static int64 highResTimerFrequency = 0;
	static double highResTimerToMillisecRatio = 0;

	#if JUCE_INTEL

	static void juce_getCpuVendor (char* const v) throw()
	{
		int vendor[4];
		zerostruct (vendor);
		int dummy = 0;

		asm ("mov %%ebx, %%esi \n\t"
			 "cpuid \n\t"
			 "xchg %%esi, %%ebx"
			   : "=a" (dummy), "=S" (vendor[0]), "=c" (vendor[2]), "=d" (vendor[1]) : "a" (0));

		memcpy (v, vendor, 16);
	}

	static unsigned int getCPUIDWord (unsigned int& familyModel, unsigned int& extFeatures)
	{
		unsigned int cpu = 0;
		unsigned int ext = 0;
		unsigned int family = 0;
		unsigned int dummy = 0;

		asm ("mov %%ebx, %%esi \n\t"
			 "cpuid \n\t"
			 "xchg %%esi, %%ebx"
			   : "=a" (family), "=S" (ext), "=c" (dummy), "=d" (cpu) : "a" (1));

		familyModel = family;
		extFeatures = ext;
		return cpu;
	}

	struct CPUFlags
	{
		bool hasMMX : 1;
		bool hasSSE : 1;
		bool hasSSE2 : 1;
		bool has3DNow : 1;
	};

	static CPUFlags cpuFlags;

	#endif
}

void SystemStats::initialiseStats() throw()
{
	using namespace SystemStatsHelpers;
	static bool initialised = false;

	if (! initialised)
	{
		initialised = true;

#if JUCE_MAC
		// extremely annoying: adding this line stops the apple menu items from working. Of
		// course, not adding it means that carbon windows (e.g. in plugins) won't get
		// any events.
		//NSApplicationLoad();
		[NSApplication sharedApplication];
#endif

#if JUCE_INTEL
		{
			unsigned int familyModel, extFeatures;
			const unsigned int features = getCPUIDWord (familyModel, extFeatures);

			cpuFlags.hasMMX = ((features & (1 << 23)) != 0);
			cpuFlags.hasSSE = ((features & (1 << 25)) != 0);
			cpuFlags.hasSSE2 = ((features & (1 << 26)) != 0);
			cpuFlags.has3DNow = ((extFeatures & (1 << 31)) != 0);
		}
#endif

		mach_timebase_info_data_t timebase;
		(void) mach_timebase_info (&timebase);
		highResTimerFrequency = (int64) (1.0e9 * timebase.denom / timebase.numer);
		highResTimerToMillisecRatio = timebase.numer / (1.0e6 * timebase.denom);

		String s (SystemStats::getJUCEVersion());

		rlimit lim;
		getrlimit (RLIMIT_NOFILE, &lim);
		lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
		setrlimit (RLIMIT_NOFILE, &lim);
	}
}

SystemStats::OperatingSystemType SystemStats::getOperatingSystemType() throw()
{
	return MacOSX;
}

const String SystemStats::getOperatingSystemName() throw()
{
	return "Mac OS X";
}

bool SystemStats::isOperatingSystem64Bit() throw()
{
#if JUCE_64BIT
	return true;
#else
	//xxx not sure how to find this out?..
	return false;
#endif
}

int SystemStats::getMemorySizeInMegabytes() throw()
{
	uint64 mem = 0;
	size_t memSize = sizeof (mem);
	int mib[] = { CTL_HW, HW_MEMSIZE };
	sysctl (mib, 2, &mem, &memSize, 0, 0);
	return (int) (mem / (1024 * 1024));
}

bool SystemStats::hasMMX() throw()
{
#if JUCE_INTEL
	return SystemStatsHelpers::cpuFlags.hasMMX;
#else
	return false;
#endif
}

bool SystemStats::hasSSE() throw()
{
#if JUCE_INTEL
	return SystemStatsHelpers::cpuFlags.hasSSE;
#else
	return false;
#endif
}

bool SystemStats::hasSSE2() throw()
{
#if JUCE_INTEL
	return SystemStatsHelpers::cpuFlags.hasSSE2;
#else
	return false;
#endif
}

bool SystemStats::has3DNow() throw()
{
#if JUCE_INTEL
	return SystemStatsHelpers::cpuFlags.has3DNow;
#else
	return false;
#endif
}

const String SystemStats::getCpuVendor() throw()
{
#if JUCE_INTEL
	char v [16];
	SystemStatsHelpers::juce_getCpuVendor (v);
	return String (v, 16);
#else
	return String::empty;
#endif
}

int SystemStats::getCpuSpeedInMegaherz() throw()
{
	uint64 speedHz = 0;
	size_t speedSize = sizeof (speedHz);
	int mib[] = { CTL_HW, HW_CPU_FREQ };
	sysctl (mib, 2, &speedHz, &speedSize, 0, 0);

#if JUCE_BIG_ENDIAN
	if (speedSize == 4)
		speedHz >>= 32;
#endif
	return (int) (speedHz / 1000000);
}

int SystemStats::getNumCpus() throw()
{
#if JUCE_IPHONE || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
	return (int) [[NSProcessInfo processInfo] activeProcessorCount];
#else
	return MPProcessors();
#endif
}

const String SystemStats::getLogonName()
{
	return nsStringToJuce (NSUserName());
}

const String SystemStats::getFullUserName()
{
	return nsStringToJuce (NSFullUserName());
}

uint32 juce_millisecondsSinceStartup() throw()
{
	return (uint32) (mach_absolute_time() * SystemStatsHelpers::highResTimerToMillisecRatio);
}

double Time::getMillisecondCounterHiRes() throw()
{
	return mach_absolute_time() * SystemStatsHelpers::highResTimerToMillisecRatio;
}

int64 Time::getHighResolutionTicks() throw()
{
	return (int64) mach_absolute_time();
}

int64 Time::getHighResolutionTicksPerSecond() throw()
{
	return SystemStatsHelpers::highResTimerFrequency;
}

int64 SystemStats::getClockCycleCounter() throw()
{
	return (int64) mach_absolute_time();
}

bool Time::setSystemTimeToThisTime() const throw()
{
	jassertfalse
	return false;
}

int SystemStats::getPageSize() throw()
{
	return (int) NSPageSize();
}

void PlatformUtilities::fpuReset()
{
}

#endif
/*** End of inlined file: juce_mac_SystemStats.mm ***/


/*** Start of inlined file: juce_mac_Network.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

int SystemStats::getMACAddresses (int64* addresses, int maxNum, const bool littleEndian)
{
	#ifndef IFT_ETHER
	 #define IFT_ETHER 6
	#endif

	ifaddrs* addrs = 0;
	int numResults = 0;

	if (getifaddrs (&addrs) == 0)
	{
		const ifaddrs* cursor = addrs;

		while (cursor != 0 && numResults < maxNum)
		{
			sockaddr_storage* sto = (sockaddr_storage*) cursor->ifa_addr;
			if (sto->ss_family == AF_LINK)
			{
				const sockaddr_dl* const sadd = (const sockaddr_dl*) cursor->ifa_addr;

				if (sadd->sdl_type == IFT_ETHER)
				{
					const uint8* const addr = ((const uint8*) sadd->sdl_data) + sadd->sdl_nlen;

					uint64 a = 0;
					for (int i = 6; --i >= 0;)
						a = (a << 8) | addr [littleEndian ? i : (5 - i)];

					*addresses++ = (int64) a;
					++numResults;
				}
			}

			cursor = cursor->ifa_next;
		}

		freeifaddrs (addrs);
	}

	return numResults;
}

bool PlatformUtilities::launchEmailWithAttachments (const String& targetEmailAddress,
													const String& emailSubject,
													const String& bodyText,
													const StringArray& filesToAttach)
{
#if JUCE_IPHONE
	//xxx probably need to use MFMailComposeViewController
	jassertfalse
	return false;
#else
	const ScopedAutoReleasePool pool;

	String script;
	script << "tell application \"Mail\"\r\n"
			  "set newMessage to make new outgoing message with properties {subject:\""
		   << emailSubject.replace ("\"", "\\\"")
		   << "\", content:\""
		   << bodyText.replace ("\"", "\\\"")
		   << "\" & return & return}\r\n"
			  "tell newMessage\r\n"
			  "set visible to true\r\n"
			  "set sender to \"sdfsdfsdfewf\"\r\n"
			  "make new to recipient at end of to recipients with properties {address:\""
		   << targetEmailAddress
		   << "\"}\r\n";

	for (int i = 0; i < filesToAttach.size(); ++i)
	{
		script << "tell content\r\n"
				  "make new attachment with properties {file name:\""
			   << filesToAttach[i].replace ("\"", "\\\"")
			   << "\"} at after the last paragraph\r\n"
				  "end tell\r\n";
	}

	script << "end tell\r\n"
			  "end tell\r\n";

	NSAppleScript* s = [[NSAppleScript alloc]
							initWithSource: juceStringToNS (script)];
	NSDictionary* error = 0;
	const bool ok = [s executeAndReturnError: &error] != nil;
	[s release];

	return ok;
#endif
}

END_JUCE_NAMESPACE

using namespace JUCE_NAMESPACE;

#define JuceURLConnection MakeObjCClassName(JuceURLConnection)

@interface JuceURLConnection  : NSObject
{
@public
	NSURLRequest* request;
	NSURLConnection* connection;
	NSMutableData* data;
	Thread* runLoopThread;
	bool initialised, hasFailed, hasFinished;
	int position;
	int64 contentLength;
	NSLock* dataLock;
}

- (JuceURLConnection*) initWithRequest: (NSURLRequest*) req withCallback: (URL::OpenStreamProgressCallback*) callback withContext: (void*) context;
- (void) dealloc;
- (void) connection: (NSURLConnection*) connection didReceiveResponse: (NSURLResponse*) response;
- (void) connection: (NSURLConnection*) connection didFailWithError: (NSError*) error;
- (void) connection: (NSURLConnection*) connection didReceiveData: (NSData*) data;
- (void) connectionDidFinishLoading: (NSURLConnection*) connection;

- (BOOL) isOpen;
- (int) read: (char*) dest numBytes: (int) num;
- (int) readPosition;
- (void) stop;
- (void) createConnection;

@end

class JuceURLConnectionMessageThread  : public Thread
{
	JuceURLConnection* owner;

public:
	JuceURLConnectionMessageThread (JuceURLConnection* owner_)
		: Thread ("http connection"),
		  owner (owner_)
	{
	}

	~JuceURLConnectionMessageThread()
	{
		stopThread (10000);
	}

	void run()
	{
		[owner createConnection];

		while (! threadShouldExit())
		{
			const ScopedAutoReleasePool pool;
			[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
		}
	}
};

@implementation JuceURLConnection

- (JuceURLConnection*) initWithRequest: (NSURLRequest*) req
						  withCallback: (URL::OpenStreamProgressCallback*) callback
						   withContext: (void*) context;
{
	[super init];
	request = req;
	[request retain];
	data = [[NSMutableData data] retain];
	dataLock = [[NSLock alloc] init];
	connection = 0;
	initialised = false;
	hasFailed = false;
	hasFinished = false;
	contentLength = -1;

	runLoopThread = new JuceURLConnectionMessageThread (self);
	runLoopThread->startThread();

	while (runLoopThread->isThreadRunning() && ! initialised)
	{
		if (callback != 0)
			callback (context, -1, (int) [[request HTTPBody] length]);

		Thread::sleep (1);
	}

	return self;
}

- (void) dealloc
{
	[self stop];

	deleteAndZero (runLoopThread);
	[connection release];
	[data release];
	[dataLock release];
	[request release];
	[super dealloc];
}

- (void) createConnection
{
	connection = [[NSURLConnection alloc] initWithRequest: request
												 delegate: [self retain]];

	if (connection == nil)
		runLoopThread->signalThreadShouldExit();
}

- (void) connection: (NSURLConnection*) connection didReceiveResponse: (NSURLResponse*) response
{
	[dataLock lock];
	[data setLength: 0];
	[dataLock unlock];
	initialised = true;
	contentLength = [response expectedContentLength];
}

- (void) connection: (NSURLConnection*) connection didFailWithError: (NSError*) error
{
	DBG (nsStringToJuce ([error description]));
	hasFailed = true;
	initialised = true;

	if (runLoopThread != 0)
		runLoopThread->signalThreadShouldExit();
}

- (void) connection: (NSURLConnection*) connection didReceiveData: (NSData*) newData
{
	[dataLock lock];
	[data appendData: newData];
	[dataLock unlock];
	initialised = true;
}

- (void) connectionDidFinishLoading: (NSURLConnection*) connection
{
	hasFinished = true;
	initialised = true;

	if (runLoopThread != 0)
		runLoopThread->signalThreadShouldExit();
}

- (BOOL) isOpen
{
	return connection != 0 && ! hasFailed;
}

- (int) readPosition
{
	return position;
}

- (int) read: (char*) dest numBytes: (int) numNeeded
{
	int numDone = 0;

	while (numNeeded > 0)
	{
		int available = jmin (numNeeded, (int) [data length]);

		if (available > 0)
		{
			[dataLock lock];
			[data getBytes: dest length: available];
			[data replaceBytesInRange: NSMakeRange (0, available) withBytes: nil length: 0];
			[dataLock unlock];

			numDone += available;
			numNeeded -= available;
			dest += available;
		}
		else
		{
			if (hasFailed || hasFinished)
				break;

			Thread::sleep (1);
		}
	}

	position += numDone;
	return numDone;
}

- (void) stop
{
	[connection cancel];

	if (runLoopThread != 0)
		runLoopThread->stopThread (10000);
}

@end
BEGIN_JUCE_NAMESPACE

bool juce_isOnLine()
{
	return true;
}

void* juce_openInternetFile (const String& url,
							 const String& headers,
							 const MemoryBlock& postData,
							 const bool isPost,
							 URL::OpenStreamProgressCallback* callback,
							 void* callbackContext,
							 int timeOutMs)
{
	const ScopedAutoReleasePool pool;

	NSMutableURLRequest* req = [NSMutableURLRequest
			requestWithURL: [NSURL URLWithString: juceStringToNS (url)]
			   cachePolicy: NSURLRequestUseProtocolCachePolicy
		   timeoutInterval: timeOutMs <= 0 ? 60.0 : (timeOutMs / 1000.0)];

	if (req == nil)
		return 0;

	[req setHTTPMethod: isPost ? @"POST" : @"GET"];
	//[req setCachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData];

	StringArray headerLines;
	headerLines.addLines (headers);
	headerLines.removeEmptyStrings (true);

	for (int i = 0; i < headerLines.size(); ++i)
	{
		const String key (headerLines[i].upToFirstOccurrenceOf (":", false, false).trim());
		const String value (headerLines[i].fromFirstOccurrenceOf (":", false, false).trim());

		if (key.isNotEmpty() && value.isNotEmpty())
			[req addValue: juceStringToNS (value) forHTTPHeaderField: juceStringToNS (key)];
	}

	if (isPost && postData.getSize() > 0)
	{
		[req setHTTPBody: [NSData dataWithBytes: postData.getData()
										 length: postData.getSize()]];
	}

	JuceURLConnection* const s = [[JuceURLConnection alloc] initWithRequest: req
															   withCallback: callback
																withContext: callbackContext];

	if ([s isOpen])
		return s;

	[s release];
	return 0;
}

void juce_closeInternetFile (void* handle)
{
	JuceURLConnection* const s = (JuceURLConnection*) handle;

	if (s != 0)
	{
		const ScopedAutoReleasePool pool;
		[s stop];
		[s release];
	}
}

int juce_readFromInternetFile (void* handle, void* buffer, int bytesToRead)
{
	JuceURLConnection* const s = (JuceURLConnection*) handle;

	if (s != 0)
	{
		const ScopedAutoReleasePool pool;
		return [s read: (char*) buffer numBytes: bytesToRead];
	}

	return 0;
}

int64 juce_getInternetFileContentLength (void* handle)
{
	JuceURLConnection* const s = (JuceURLConnection*) handle;

	if (s != 0)
		return s->contentLength;

	return -1;
}

int juce_seekInInternetFile (void* handle, int newPosition)
{
	JuceURLConnection* const s = (JuceURLConnection*) handle;

	if (s != 0)
		return [s readPosition];

	return 0;
}

#endif
/*** End of inlined file: juce_mac_Network.mm ***/


/*** Start of inlined file: juce_posix_NamedPipe.cpp ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

struct NamedPipeInternal
{
	String pipeInName, pipeOutName;
	int pipeIn, pipeOut;

	bool volatile createdPipe, blocked, stopReadOperation;

	static void signalHandler (int) {}
};

void NamedPipe::cancelPendingReads()
{
	while (internal != 0 && ((NamedPipeInternal*) internal)->blocked)
	{
		NamedPipeInternal* const intern = (NamedPipeInternal*) internal;

		intern->stopReadOperation = true;

		char buffer [1] = { 0 };
		int bytesWritten = (int) ::write (intern->pipeIn, buffer, 1);
		(void) bytesWritten;

		int timeout = 2000;
		while (intern->blocked && --timeout >= 0)
			Thread::sleep (2);

		intern->stopReadOperation = false;
	}
}

void NamedPipe::close()
{
	NamedPipeInternal* const intern = (NamedPipeInternal*) internal;

	if (intern != 0)
	{
		internal = 0;

		if (intern->pipeIn != -1)
			::close (intern->pipeIn);

		if (intern->pipeOut != -1)
			::close (intern->pipeOut);

		if (intern->createdPipe)
		{
			unlink (intern->pipeInName.toUTF8());
			unlink (intern->pipeOutName.toUTF8());
		}

		delete intern;
	}
}

bool NamedPipe::openInternal (const String& pipeName, const bool createPipe)
{
	close();

	NamedPipeInternal* const intern = new NamedPipeInternal();
	internal = intern;
	intern->createdPipe = createPipe;
	intern->blocked = false;
	intern->stopReadOperation = false;

	signal (SIGPIPE, NamedPipeInternal::signalHandler);
	siginterrupt (SIGPIPE, 1);

	const String pipePath ("/tmp/" + File::createLegalFileName (pipeName));

	intern->pipeInName  = pipePath + "_in";
	intern->pipeOutName = pipePath + "_out";
	intern->pipeIn = -1;
	intern->pipeOut = -1;

	if (createPipe)
	{
		if ((mkfifo (intern->pipeInName.toUTF8(), 0666) && errno != EEXIST)
			|| (mkfifo (intern->pipeOutName.toUTF8(), 0666) && errno != EEXIST))
		{
			delete intern;
			internal = 0;

			return false;
		}
	}

	return true;
}

int NamedPipe::read (void* destBuffer, int maxBytesToRead, int /*timeOutMilliseconds*/)
{
	int bytesRead = -1;
	NamedPipeInternal* const intern = (NamedPipeInternal*) internal;

	if (intern != 0)
	{
		intern->blocked = true;

		if (intern->pipeIn == -1)
		{
			if (intern->createdPipe)
				intern->pipeIn = ::open (intern->pipeInName.toUTF8(), O_RDWR);
			else
				intern->pipeIn = ::open (intern->pipeOutName.toUTF8(), O_RDWR);

			if (intern->pipeIn == -1)
			{
				intern->blocked = false;
				return -1;
			}
		}

		bytesRead = 0;

		char* p = (char*) destBuffer;

		while (bytesRead < maxBytesToRead)
		{
			const int bytesThisTime = maxBytesToRead - bytesRead;
			const int numRead = (int) ::read (intern->pipeIn, p, bytesThisTime);

			if (numRead <= 0 || intern->stopReadOperation)
			{
				bytesRead = -1;
				break;
			}

			bytesRead += numRead;
			p += bytesRead;
		}

		intern->blocked = false;
	}

	return bytesRead;
}

int NamedPipe::write (const void* sourceBuffer, int numBytesToWrite, int timeOutMilliseconds)
{
	int bytesWritten = -1;
	NamedPipeInternal* const intern = (NamedPipeInternal*) internal;

	if (intern != 0)
	{
		if (intern->pipeOut == -1)
		{
			if (intern->createdPipe)
				intern->pipeOut = ::open (intern->pipeOutName.toUTF8(), O_WRONLY);
			else
				intern->pipeOut = ::open (intern->pipeInName.toUTF8(), O_WRONLY);

			if (intern->pipeOut == -1)
			{
				return -1;
			}
		}

		const char* p = (const char*) sourceBuffer;
		bytesWritten = 0;

		const uint32 timeOutTime = Time::getMillisecondCounter() + timeOutMilliseconds;

		while (bytesWritten < numBytesToWrite
			   && (timeOutMilliseconds < 0 || Time::getMillisecondCounter() < timeOutTime))
		{
			const int bytesThisTime = numBytesToWrite - bytesWritten;
			const int numWritten = (int) ::write (intern->pipeOut, p, bytesThisTime);

			if (numWritten <= 0)
			{
				bytesWritten = -1;
				break;
			}

			bytesWritten += numWritten;
			p += bytesWritten;
		}
	}

	return bytesWritten;
}

#endif
/*** End of inlined file: juce_posix_NamedPipe.cpp ***/


/*** Start of inlined file: juce_mac_Threads.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

void JUCE_API juce_threadEntryPoint (void*);

void* threadEntryProc (void* userData)
{
	const ScopedAutoReleasePool pool;
	juce_threadEntryPoint (userData);
	return 0;
}

void* juce_createThread (void* userData)
{
	pthread_t handle = 0;

	if (pthread_create (&handle, 0, threadEntryProc, userData) == 0)
	{
		pthread_detach (handle);
		return (void*) handle;
	}

	return 0;
}

void juce_killThread (void* handle)
{
	if (handle != 0)
		pthread_cancel ((pthread_t) handle);
}

void juce_setCurrentThreadName (const String& /*name*/)
{
}

bool juce_setThreadPriority (void* handle, int priority)
{
	if (handle == 0)
		handle = (void*) pthread_self();

	struct sched_param param;
	int policy;
	pthread_getschedparam ((pthread_t) handle, &policy, &param);
	param.sched_priority = jlimit (1, 127, 1 + (priority * 126) / 11);
	return pthread_setschedparam ((pthread_t) handle, policy, &param) == 0;
}

Thread::ThreadID Thread::getCurrentThreadId()
{
	return static_cast <ThreadID> (pthread_self());
}

void Thread::yield()
{
	sched_yield();
}

void Thread::setCurrentThreadAffinityMask (const uint32 affinityMask)
{
	// xxx
	jassertfalse
}

bool Process::isForegroundProcess()
{
#if JUCE_MAC
	return [NSApp isActive];
#else
	return true; // xxx change this if more than one app is ever possible on the iPhone!
#endif
}

void Process::raisePrivilege()
{
	jassertfalse
}

void Process::lowerPrivilege()
{
	jassertfalse
}

void Process::terminate()
{
	exit (0);
}

void Process::setPriority (ProcessPriority p)
{
	// xxx
}

#endif
/*** End of inlined file: juce_mac_Threads.mm ***/


/*** Start of inlined file: juce_posix_SharedCode.h ***/
CriticalSection::CriticalSection() throw()
{
	pthread_mutexattr_t atts;
	pthread_mutexattr_init (&atts);
	pthread_mutexattr_settype (&atts, PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init (&internal, &atts);
}

CriticalSection::~CriticalSection() throw()
{
	pthread_mutex_destroy (&internal);
}

void CriticalSection::enter() const throw()
{
	pthread_mutex_lock (&internal);
}

bool CriticalSection::tryEnter() const throw()
{
	return pthread_mutex_trylock (&internal) == 0;
}

void CriticalSection::exit() const throw()
{
	pthread_mutex_unlock (&internal);
}

class WaitableEventImpl
{
public:
	WaitableEventImpl (const bool manualReset_)
		: triggered (false),
		  manualReset (manualReset_)
	{
		pthread_cond_init (&condition, 0);
		pthread_mutex_init (&mutex, 0);
	}

	~WaitableEventImpl()
	{
		pthread_cond_destroy (&condition);
		pthread_mutex_destroy (&mutex);
	}

	bool wait (const int timeOutMillisecs) throw()
	{
		pthread_mutex_lock (&mutex);

		if (! triggered)
		{
			if (timeOutMillisecs < 0)
			{
				do
				{
					pthread_cond_wait (&condition, &mutex);
				}
				while (! triggered);
			}
			else
			{
				struct timeval now;
				gettimeofday (&now, 0);

				struct timespec time;
				time.tv_sec  = now.tv_sec  + (timeOutMillisecs / 1000);
				time.tv_nsec = (now.tv_usec + ((timeOutMillisecs % 1000) * 1000)) * 1000;

				if (time.tv_nsec >= 1000000000)
				{
					time.tv_nsec -= 1000000000;
					time.tv_sec++;
				}

				do
				{
					if (pthread_cond_timedwait (&condition, &mutex, &time) == ETIMEDOUT)
					{
						pthread_mutex_unlock (&mutex);
						return false;
					}
				}
				while (! triggered);
			}
		}

		if (! manualReset)
			triggered = false;

		pthread_mutex_unlock (&mutex);
		return true;
	}

	void signal() throw()
	{
		pthread_mutex_lock (&mutex);
		triggered = true;
		pthread_cond_broadcast (&condition);
		pthread_mutex_unlock (&mutex);
	}

	void reset() throw()
	{
		pthread_mutex_lock (&mutex);
		triggered = false;
		pthread_mutex_unlock (&mutex);
	}

private:
	pthread_cond_t condition;
	pthread_mutex_t mutex;
	bool triggered;
	const bool manualReset;

	WaitableEventImpl (const WaitableEventImpl&);
	WaitableEventImpl& operator= (const WaitableEventImpl&);
};

WaitableEvent::WaitableEvent (const bool manualReset) throw()
	: internal (new WaitableEventImpl (manualReset))
{
}

WaitableEvent::~WaitableEvent() throw()
{
	delete static_cast <WaitableEventImpl*> (internal);
}

bool WaitableEvent::wait (const int timeOutMillisecs) const throw()
{
	return static_cast <WaitableEventImpl*> (internal)->wait (timeOutMillisecs);
}

void WaitableEvent::signal() const throw()
{
	static_cast <WaitableEventImpl*> (internal)->signal();
}

void WaitableEvent::reset() const throw()
{
	static_cast <WaitableEventImpl*> (internal)->reset();
}

void JUCE_CALLTYPE Thread::sleep (int millisecs)
{
	struct timespec time;
	time.tv_sec = millisecs / 1000;
	time.tv_nsec = (millisecs % 1000) * 1000000;
	nanosleep (&time, 0);
}

const juce_wchar File::separator = '/';
const juce_wchar* File::separatorString = L"/";

const File File::getCurrentWorkingDirectory()
{
	HeapBlock<char> heapBuffer;

	char localBuffer [1024];
	char* cwd = getcwd (localBuffer, sizeof (localBuffer) - 1);
	int bufferSize = 4096;

	while (cwd == 0 && errno == ERANGE)
	{
		heapBuffer.malloc (bufferSize);
		cwd = getcwd (heapBuffer, bufferSize - 1);
		bufferSize += 1024;
	}

	return File (String::fromUTF8 (cwd));
}

bool File::setAsCurrentWorkingDirectory() const
{
	return chdir (getFullPathName().toUTF8()) == 0;
}

bool juce_copyFile (const String& s, const String& d);

static bool juce_stat (const String& fileName, struct stat& info)
{
	return fileName.isNotEmpty()
			&& (stat (fileName.toUTF8(), &info) == 0);
}

bool juce_isDirectory (const String& fileName)
{
	struct stat info;

	return fileName.isEmpty()
			|| (juce_stat (fileName, info)
				  && ((info.st_mode & S_IFDIR) != 0));
}

bool juce_fileExists (const String& fileName, const bool dontCountDirectories)
{
	if (fileName.isEmpty())
		return false;

	const char* const fileNameUTF8 = fileName.toUTF8();
	bool exists = access (fileNameUTF8, F_OK) == 0;

	if (exists && dontCountDirectories)
	{
		struct stat info;
		const int res = stat (fileNameUTF8, &info);

		if (res == 0 && (info.st_mode & S_IFDIR) != 0)
			exists = false;
	}

	return exists;
}

int64 juce_getFileSize (const String& fileName)
{
	struct stat info;
	return juce_stat (fileName, info) ? info.st_size : 0;
}

bool juce_canWriteToFile (const String& fileName)
{
	return access (fileName.toUTF8(), W_OK) == 0;
}

bool juce_deleteFile (const String& fileName)
{
	if (juce_isDirectory (fileName))
		return rmdir (fileName.toUTF8()) == 0;
	else
		return remove (fileName.toUTF8()) == 0;
}

bool juce_moveFile (const String& source, const String& dest)
{
	if (rename (source.toUTF8(), dest.toUTF8()) == 0)
		return true;

	if (juce_canWriteToFile (source)
		 && juce_copyFile (source, dest))
	{
		if (juce_deleteFile (source))
			return true;

		juce_deleteFile (dest);
	}

	return false;
}

void juce_createDirectory (const String& fileName)
{
	mkdir (fileName.toUTF8(), 0777);
}

void* juce_fileOpen (const String& fileName, bool forWriting)
{
	int flags = O_RDONLY;

	if (forWriting)
	{
		if (juce_fileExists (fileName, false))
		{
			const int f = open (fileName.toUTF8(), O_RDWR, 00644);

			if (f != -1)
				lseek (f, 0, SEEK_END);

			return (void*) f;
		}
		else
		{
			flags = O_RDWR + O_CREAT;
		}
	}

	return (void*) open (fileName.toUTF8(), flags, 00644);
}

void juce_fileClose (void* handle)
{
	if (handle != 0)
		close ((int) (pointer_sized_int) handle);
}

int juce_fileRead (void* handle, void* buffer, int size)
{
	if (handle != 0)
		return jmax (0, (int) read ((int) (pointer_sized_int) handle, buffer, size));

	return 0;
}

int juce_fileWrite (void* handle, const void* buffer, int size)
{
	if (handle != 0)
		return (int) write ((int) (pointer_sized_int) handle, buffer, size);

	return 0;
}

int64 juce_fileSetPosition (void* handle, int64 pos)
{
	if (handle != 0 && lseek ((int) (pointer_sized_int) handle, pos, SEEK_SET) == pos)
		return pos;

	return -1;
}

int64 juce_fileGetPosition (void* handle)
{
	if (handle != 0)
		return lseek ((int) (pointer_sized_int) handle, 0, SEEK_CUR);

	return -1;
}

void juce_fileFlush (void* handle)
{
	if (handle != 0)
		fsync ((int) (pointer_sized_int) handle);
}

const File juce_getExecutableFile()
{
	Dl_info exeInfo;
	dladdr ((const void*) juce_getExecutableFile, &exeInfo);
	return File::getCurrentWorkingDirectory().getChildFile (String::fromUTF8 (exeInfo.dli_fname));
}

// if this file doesn't exist, find a parent of it that does..
static bool doStatFS (const File* file, struct statfs& result)
{
	File f (*file);

	for (int i = 5; --i >= 0;)
	{
		if (f.exists())
			break;

		f = f.getParentDirectory();
	}

	return statfs (f.getFullPathName().toUTF8(), &result) == 0;
}

int64 File::getBytesFreeOnVolume() const
{
	struct statfs buf;
	if (doStatFS (this, buf))
		return (int64) buf.f_bsize * (int64) buf.f_bavail; // Note: this returns space available to non-super user

	return 0;
}

int64 File::getVolumeTotalSize() const
{
	struct statfs buf;
	if (doStatFS (this, buf))
		return (int64) buf.f_bsize * (int64) buf.f_blocks;

	return 0;
}

const String juce_getVolumeLabel (const String& filenameOnVolume,
								  int& volumeSerialNumber)
{
	volumeSerialNumber = 0;

#if JUCE_MAC
	struct VolAttrBuf
	{
		u_int32_t	   length;
		attrreference_t mountPointRef;
		char		mountPointSpace [MAXPATHLEN];
	} attrBuf;

	struct attrlist attrList;
	zerostruct (attrList);
	attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
	attrList.volattr = ATTR_VOL_INFO | ATTR_VOL_NAME;

	File f (filenameOnVolume);

	for (;;)
	{
		if (getattrlist (f.getFullPathName().toUTF8(),
						 &attrList, &attrBuf, sizeof(attrBuf), 0) == 0)
		{
			return String::fromUTF8 (((const char*) &attrBuf.mountPointRef) + attrBuf.mountPointRef.attr_dataoffset,
									 (int) attrBuf.mountPointRef.attr_length);
		}

		const File parent (f.getParentDirectory());

		if (f == parent)
			break;

		f = parent;
	}
#endif

	return String::empty;
}

void juce_runSystemCommand (const String& command)
{
	int result = system (command.toUTF8());
	(void) result;
}

const String juce_getOutputFromCommand (const String& command)
{
	// slight bodge here, as we just pipe the output into a temp file and read it...
	const File tempFile (File::getSpecialLocation (File::tempDirectory)
						   .getNonexistentChildFile (String::toHexString (Random::getSystemRandom().nextInt()), ".tmp", false));

	juce_runSystemCommand (command + " > " + tempFile.getFullPathName());

	String result (tempFile.loadFileAsString());
	tempFile.deleteFile();
	return result;
}

class InterProcessLock::Pimpl
{
public:
	Pimpl (const String& name, const int timeOutMillisecs)
		: handle (0), refCount (1)
	{
#if JUCE_MAC
		// (don't use getSpecialLocation() to avoid the temp folder being different for each app)
		const File temp (File ("~/Library/Caches/Juce").getChildFile (name));
#else
		const File temp (File::getSpecialLocation (File::tempDirectory).getChildFile (name));
#endif
		temp.create();
		handle = open (temp.getFullPathName().toUTF8(), O_RDWR);

		if (handle != 0)
		{
			struct flock fl;
			zerostruct (fl);
			fl.l_whence = SEEK_SET;
			fl.l_type = F_WRLCK;

			const int64 endTime = Time::currentTimeMillis() + timeOutMillisecs;

			for (;;)
			{
				const int result = fcntl (handle, F_SETLK, &fl);

				if (result >= 0)
					return;

				if (errno != EINTR)
				{
					if (timeOutMillisecs == 0
						 || (timeOutMillisecs > 0 && Time::currentTimeMillis() >= endTime))
						break;

					Thread::sleep (10);
				}
			}
		}

		closeFile();
	}

	~Pimpl()
	{
		closeFile();
	}

	void closeFile()
	{
		if (handle != 0)
		{
			struct flock fl;
			zerostruct (fl);
			fl.l_whence = SEEK_SET;
			fl.l_type = F_UNLCK;

			while (! (fcntl (handle, F_SETLKW, &fl) >= 0 || errno != EINTR))
			{}

			close (handle);
			handle = 0;
		}
	}

	int handle, refCount;
};

InterProcessLock::InterProcessLock (const String& name_)
	: name (name_)
{
}

InterProcessLock::~InterProcessLock()
{
}

bool InterProcessLock::enter (const int timeOutMillisecs)
{
	const ScopedLock sl (lock);

	if (pimpl == 0)
	{
		pimpl = new Pimpl (name, timeOutMillisecs);

		if (pimpl->handle == 0)
			pimpl = 0;
	}
	else
	{
		pimpl->refCount++;
	}

	return pimpl != 0;
}

void InterProcessLock::exit()
{
	const ScopedLock sl (lock);

	// Trying to release the lock too many times!
	jassert (pimpl != 0);

	if (pimpl != 0 && --(pimpl->refCount) == 0)
		pimpl = 0;
}
/*** End of inlined file: juce_posix_SharedCode.h ***/


/*** Start of inlined file: juce_mac_Files.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

void juce_getFileTimes (const String& fileName,
						int64& modificationTime,
						int64& accessTime,
						int64& creationTime)
{
	modificationTime = 0;
	accessTime = 0;
	creationTime = 0;

	struct stat info;
	const int res = stat (fileName.toUTF8(), &info);
	if (res == 0)
	{
		modificationTime = (int64) info.st_mtime * 1000;
		accessTime = (int64) info.st_atime * 1000;
		creationTime = (int64) info.st_ctime * 1000;
	}
}

bool juce_setFileTimes (const String& fileName,
						int64 modificationTime,
						int64 accessTime,
						int64 creationTime)
{
	struct utimbuf times;
	times.actime = (time_t) (accessTime / 1000);
	times.modtime = (time_t) (modificationTime / 1000);

	return utime (fileName.toUTF8(), &times) == 0;
}

bool juce_setFileReadOnly (const String& fileName, bool isReadOnly)
{
	struct stat info;
	const int res = stat (fileName.toUTF8(), &info);
	if (res != 0)
		return false;

	info.st_mode &= 0777;   // Just permissions

	if (isReadOnly)
		info.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
	else
		// Give everybody write permission?
		info.st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;

	return chmod (fileName.toUTF8(), info.st_mode) == 0;
}

bool juce_copyFile (const String& src, const String& dst)
{
	const ScopedAutoReleasePool pool;
	NSFileManager* fm = [NSFileManager defaultManager];

	return [fm fileExistsAtPath: juceStringToNS (src)]
#if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
			&& [fm copyItemAtPath: juceStringToNS (src)
						   toPath: juceStringToNS (dst)
							error: nil];
#else
			&& [fm copyPath: juceStringToNS (src)
					 toPath: juceStringToNS (dst)
					handler: nil];
#endif
}

const StringArray juce_getFileSystemRoots()
{
	StringArray s;
	s.add ("/");
	return s;
}

static bool isFileOnDriveType (const File* const f, const char** types)
{
	struct statfs buf;

	if (doStatFS (f, buf))
	{
		const String type (buf.f_fstypename);

		while (*types != 0)
			if (type.equalsIgnoreCase (*types++))
				return true;
	}

	return false;
}

bool File::isOnCDRomDrive() const
{
	static const char* const cdTypes[] = { "cd9660", "cdfs", "cddafs", "udf", 0 };

	return isFileOnDriveType (this, (const char**) cdTypes);
}

bool File::isOnHardDisk() const
{
	static const char* const nonHDTypes[] = { "nfs", "smbfs", "ramfs", 0 };

	return ! (isOnCDRomDrive() || isFileOnDriveType (this, (const char**) nonHDTypes));
}

bool File::isOnRemovableDrive() const
{
#if JUCE_IPHONE
	return false; // xxx is this possible?
#else
	const ScopedAutoReleasePool pool;
	BOOL removable = false;

	[[NSWorkspace sharedWorkspace]
		   getFileSystemInfoForPath: juceStringToNS (getFullPathName())
						isRemovable: &removable
						 isWritable: nil
					  isUnmountable: nil
						description: nil
							   type: nil];

	return removable;
#endif
}

static bool juce_isHiddenFile (const String& path)
{
#if JUCE_IPHONE
	return File (path).getFileName().startsWithChar ('.');
#else
	FSRef ref;
	if (! PlatformUtilities::makeFSRefFromPath (&ref, path))
		return false;

	FSCatalogInfo info;
	FSGetCatalogInfo (&ref, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo, &info, 0, 0, 0);

	if ((info.nodeFlags & kFSNodeIsDirectoryBit) != 0)
		return (((FolderInfo*) &info.finderInfo)->finderFlags & kIsInvisible) != 0;

	return (((FileInfo*) &info.finderInfo)->finderFlags & kIsInvisible) != 0;
#endif
}

bool File::isHidden() const
{
	return juce_isHiddenFile (getFullPathName());
}

const char* juce_Argv0 = 0;  // referenced from juce_Application.cpp

const File File::getSpecialLocation (const SpecialLocationType type)
{
	const ScopedAutoReleasePool pool;

	String resultPath;

	switch (type)
	{
	case userHomeDirectory:
		resultPath = nsStringToJuce (NSHomeDirectory());
		break;

	case userDocumentsDirectory:
		resultPath = "~/Documents";
		break;

	case userDesktopDirectory:
		resultPath = "~/Desktop";
		break;

	case userApplicationDataDirectory:
		resultPath = "~/Library";
		break;

	case commonApplicationDataDirectory:
		resultPath = "/Library";
		break;

	case globalApplicationsDirectory:
		resultPath = "/Applications";
		break;

	case userMusicDirectory:
		resultPath = "~/Music";
		break;

	case userMoviesDirectory:
		resultPath = "~/Movies";
		break;

	case tempDirectory:
	{
		File tmp ("~/Library/Caches/" + juce_getExecutableFile().getFileNameWithoutExtension());

		tmp.createDirectory();
		return tmp.getFullPathName();
	}

	case invokedExecutableFile:
		if (juce_Argv0 != 0)
			return File (String::fromUTF8 (juce_Argv0));
		// deliberate fall-through...

	case currentExecutableFile:
		return juce_getExecutableFile();

	case currentApplicationFile:
	{
		const File exe (juce_getExecutableFile());
		const File parent (exe.getParentDirectory());

		return parent.getFullPathName().endsWithIgnoreCase ("Contents/MacOS")
				? parent.getParentDirectory().getParentDirectory()
				: exe;
	}

	default:
		jassertfalse // unknown type?
		break;
	}

	if (resultPath.isNotEmpty())
		return File (PlatformUtilities::convertToPrecomposedUnicode (resultPath));

	return File::nonexistent;
}

const String File::getVersion() const
{
	const ScopedAutoReleasePool pool;
	String result;

	NSBundle* bundle = [NSBundle bundleWithPath: juceStringToNS (getFullPathName())];

	if (bundle != 0)
	{
		NSDictionary* info = [bundle infoDictionary];

		if (info != 0)
		{
			NSString* name = [info valueForKey: @"CFBundleShortVersionString"];

			if (name != nil)
				result = nsStringToJuce (name);
		}
	}

	return result;
}

const File File::getLinkedTarget() const
{
#if JUCE_IPHONE || (defined (MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
	NSString* dest = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath: juceStringToNS (getFullPathName()) error: nil];

#else
	NSString* dest = [[NSFileManager defaultManager] pathContentOfSymbolicLinkAtPath: juceStringToNS (getFullPathName())];
#endif

	if (dest != nil)
		return File (nsStringToJuce (dest));

	return *this;
}

bool File::moveToTrash() const
{
	if (! exists())
		return true;

#if JUCE_IPHONE
	return deleteFile(); //xxx is there a trashcan on the iPhone?
#else
	const ScopedAutoReleasePool pool;

	NSString* p = juceStringToNS (getFullPathName());

	return [[NSWorkspace sharedWorkspace]
				performFileOperation: NSWorkspaceRecycleOperation
							  source: [p stringByDeletingLastPathComponent]
						 destination: @""
							   files: [NSArray arrayWithObject: [p lastPathComponent]]
								 tag: nil ];
#endif
}

struct FindFileStruct
{
	NSDirectoryEnumerator* enumerator;
	String parentDir, wildCard;
};

bool juce_findFileNext (void* handle, String& resultFile,
						bool* isDir,  bool* isHidden, int64* fileSize, Time* modTime, Time* creationTime, bool* isReadOnly)
{
	ScopedAutoReleasePool pool;
	FindFileStruct* ff = (FindFileStruct*) handle;
	NSString* file;
	const char* const wildcardUTF8 = ff->wildCard.toUTF8();

	for (;;)
	{
		if (ff == 0 || (file = [ff->enumerator nextObject]) == 0)
			return false;

		[ff->enumerator skipDescendents];
		resultFile = nsStringToJuce (file);

		if (fnmatch (wildcardUTF8, resultFile.toUTF8(), FNM_CASEFOLD) != 0)
			continue;

		const String path (ff->parentDir + resultFile);

		if (isDir != 0 || fileSize != 0)
		{
			struct stat info;
			const bool statOk = juce_stat (path, info);

			if (isDir != 0)
				*isDir = statOk && ((info.st_mode & S_IFDIR) != 0);

			if (isHidden != 0)
				*isHidden = juce_isHiddenFile (path);

			if (fileSize != 0)
				*fileSize = statOk ? info.st_size : 0;
		}

		if (modTime != 0 || creationTime != 0)
		{
			int64 m, a, c;
			juce_getFileTimes (path, m, a, c);

			if (modTime != 0)
				*modTime = m;

			if (creationTime != 0)
				*creationTime = c;
		}

		if (isReadOnly != 0)
			*isReadOnly = ! juce_canWriteToFile (path);

		return true;
	}
}

void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
						  bool* isDir, bool* isHidden, int64* fileSize, Time* modTime,
						  Time* creationTime, bool* isReadOnly)
{
	ScopedAutoReleasePool pool;
	NSDirectoryEnumerator* e = [[NSFileManager defaultManager] enumeratorAtPath: juceStringToNS (directory)];

	if (e != 0)
	{
		ScopedPointer <FindFileStruct> ff (new FindFileStruct());
		ff->enumerator = [e retain];
		ff->parentDir = directory;
		ff->wildCard = wildCard;

		if (! ff->parentDir.endsWithChar (File::separator))
			ff->parentDir += File::separator;

		if (juce_findFileNext (ff, firstResultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly))
			return ff.release();

		[e release];
	}

	return 0;
}

void juce_findFileClose (void* handle)
{
	ScopedAutoReleasePool pool;
	ScopedPointer <FindFileStruct> ff ((FindFileStruct*) handle);
	[ff->enumerator release];
}

bool juce_launchExecutable (const String& pathAndArguments)
{
	const char* const argv[4] = { "/bin/sh", "-c", pathAndArguments.toUTF8(), 0 };

	const int cpid = fork();

	if (cpid == 0)
	{
		// Child process
		if (execve (argv[0], (char**) argv, 0) < 0)
			exit (0);
	}
	else
	{
		if (cpid < 0)
			return false;
	}

	return true;
}

bool juce_launchFile (const String& fileName, const String& parameters)
{
#if JUCE_IPHONE
	return [[UIApplication sharedApplication] openURL: [NSURL fileURLWithPath: juceStringToNS (fileName)]];
#else
	const ScopedAutoReleasePool pool;

	if (parameters.isEmpty())
	{
		return [[NSWorkspace sharedWorkspace] openFile: juceStringToNS (fileName)]
			|| [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: juceStringToNS (fileName)]];
	}

	bool ok = false;

	FSRef ref;
	if (PlatformUtilities::makeFSRefFromPath (&ref, fileName))
	{
		if (PlatformUtilities::isBundle (fileName))
		{
			NSMutableArray* urls = [NSMutableArray array];

			StringArray docs;
			docs.addTokens (parameters, true);
			for (int i = 0; i < docs.size(); ++i)
				[urls addObject: juceStringToNS (docs[i])];

			ok = [[NSWorkspace sharedWorkspace] openURLs: urls
								 withAppBundleIdentifier: [[NSBundle bundleWithPath: juceStringToNS (fileName)] bundleIdentifier]
												 options: 0
						  additionalEventParamDescriptor: nil
									   launchIdentifiers: nil];
		}
		else
		{
			ok = juce_launchExecutable ("\"" + fileName + "\" " + parameters);
		}
	}

	return ok;
#endif
}

void File::revealToUser() const
{
#if ! JUCE_IPHONE
	if (exists())
		[[NSWorkspace sharedWorkspace] selectFile: juceStringToNS (getFullPathName()) inFileViewerRootedAtPath: @""];
	else if (getParentDirectory().exists())
		getParentDirectory().revealToUser();
#endif
}

#if ! JUCE_IPHONE
bool PlatformUtilities::makeFSRefFromPath (FSRef* destFSRef, const String& path)
{
	return FSPathMakeRef ((const UInt8*) path.toUTF8(), destFSRef, 0) == noErr;
}

const String PlatformUtilities::makePathFromFSRef (FSRef* file)
{
	char path [2048];
	zerostruct (path);

	if (FSRefMakePath (file, (UInt8*) path, sizeof (path) - 1) == noErr)
		return PlatformUtilities::convertToPrecomposedUnicode (String::fromUTF8 (path));

	return String::empty;
}
#endif

OSType PlatformUtilities::getTypeOfFile (const String& filename)
{
	const ScopedAutoReleasePool pool;

#if JUCE_IPHONE || (defined (MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
	NSDictionary* fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath: juceStringToNS (filename) error: nil];
#else
	NSDictionary* fileDict = [[NSFileManager defaultManager] fileAttributesAtPath: juceStringToNS (filename) traverseLink: NO];
#endif
	//return (OSType) [fileDict objectForKey: NSFileHFSTypeCode];
	return [fileDict fileHFSTypeCode];
}

bool PlatformUtilities::isBundle (const String& filename)
{
#if JUCE_IPHONE
	return false; // xxx can't find a sensible way to do this without trying to open the bundle..
#else
	const ScopedAutoReleasePool pool;
	return [[NSWorkspace sharedWorkspace] isFilePackageAtPath: juceStringToNS (filename)];
#endif
}

#endif
/*** End of inlined file: juce_mac_Files.mm ***/

#if JUCE_IPHONE

/*** Start of inlined file: juce_iphone_MiscUtilities.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

static JUCEApplication* juce_intialisingApp;

END_JUCE_NAMESPACE

@interface JuceAppStartupDelegate : NSObject <UIApplicationDelegate>
{
}

- (void) applicationDidFinishLaunching: (UIApplication*) application;
- (void) applicationWillResignActive: (UIApplication*) application;

@end

@implementation JuceAppStartupDelegate

- (void) applicationDidFinishLaunching: (UIApplication*) application
{
	String dummy;

	if (! juce_intialisingApp->initialiseApp (dummy))
		exit (0);
}

- (void) applicationWillResignActive: (UIApplication*) application
{
	JUCEApplication::shutdownAppAndClearUp();
}

@end

BEGIN_JUCE_NAMESPACE

int juce_IPhoneMain (int argc, const char* argv[], JUCEApplication* app)
{
	juce_intialisingApp = app;
	return UIApplicationMain (argc, const_cast<char**> (argv), nil, @"JuceAppStartupDelegate");
}

ScopedAutoReleasePool::ScopedAutoReleasePool()
{
	pool = [[NSAutoreleasePool alloc] init];
}

ScopedAutoReleasePool::~ScopedAutoReleasePool()
{
	[((NSAutoreleasePool*) pool) release];
}

void PlatformUtilities::beep()
{
	//xxx
	//AudioServicesPlaySystemSound ();
}

void PlatformUtilities::addItemToDock (const File& file)
{
}

#if ! JUCE_ONLY_BUILD_CORE_LIBRARY

END_JUCE_NAMESPACE

@interface JuceAlertBoxDelegate  : NSObject
{
@public
	bool clickedOk;
}

- (void) alertView: (UIAlertView*) alertView clickedButtonAtIndex: (NSInteger) buttonIndex;

@end

@implementation JuceAlertBoxDelegate

- (void) alertView: (UIAlertView*) alertView clickedButtonAtIndex: (NSInteger) buttonIndex
{
	clickedOk = (buttonIndex == 0);
	alertView.hidden = true;
}

@end

BEGIN_JUCE_NAMESPACE

// (This function is used directly by other bits of code)
bool juce_iPhoneShowModalAlert (const String& title,
								const String& bodyText,
								NSString* okButtonText,
								NSString* cancelButtonText)
{
	const ScopedAutoReleasePool pool;

	JuceAlertBoxDelegate* callback = [[JuceAlertBoxDelegate alloc] init];

	UIAlertView* alert = [[UIAlertView alloc] initWithTitle: juceStringToNS (title)
													message: juceStringToNS (bodyText)
												   delegate: callback
										  cancelButtonTitle: okButtonText
										  otherButtonTitles: cancelButtonText, nil];
	[alert retain];
	[alert show];

	while (! alert.hidden && alert.superview != nil)
		[[NSRunLoop mainRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];

	const bool result = callback->clickedOk;
	[alert release];
	[callback release];

	return result;
}

bool AlertWindow::showNativeDialogBox (const String& title,
									   const String& bodyText,
									   bool isOkCancel)
{
	return juce_iPhoneShowModalAlert (title, bodyText,
									  @"OK",
									  isOkCancel ? @"Cancel" : nil);
}

bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMoveFiles)
{
	jassertfalse	// no such thing on the iphone!
	return false;
}

bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
{
	jassertfalse	// no such thing on the iphone!
	return false;
}

void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
{
	[[UIApplication sharedApplication] setIdleTimerDisabled: ! isEnabled];
}

bool Desktop::isScreenSaverEnabled() throw()
{
	return ! [[UIApplication sharedApplication] isIdleTimerDisabled];
}

void juce_updateMultiMonitorInfo (Array <Rectangle <int> >& monitorCoords, const bool clipToWorkArea)
{
	const ScopedAutoReleasePool pool;
	monitorCoords.clear();

	CGRect r = clipToWorkArea ? [[UIScreen mainScreen] applicationFrame]
							  : [[UIScreen mainScreen] bounds];

	monitorCoords.add (Rectangle<int> ((int) r.origin.x,
									   (int) r.origin.y,
									   (int) r.size.width,
									   (int) r.size.height));
}

#endif

#endif
/*** End of inlined file: juce_iphone_MiscUtilities.mm ***/


#else

/*** Start of inlined file: juce_mac_MiscUtilities.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

ScopedAutoReleasePool::ScopedAutoReleasePool()
{
	pool = [[NSAutoreleasePool alloc] init];
}

ScopedAutoReleasePool::~ScopedAutoReleasePool()
{
	[((NSAutoreleasePool*) pool) release];
}

void PlatformUtilities::beep()
{
	NSBeep();
}

void PlatformUtilities::addItemToDock (const File& file)
{
	// check that it's not already there...
	if (! juce_getOutputFromCommand ("defaults read com.apple.dock persistent-apps")
			.containsIgnoreCase (file.getFullPathName()))
	{
		juce_runSystemCommand ("defaults write com.apple.dock persistent-apps -array-add \"<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>"
								 + file.getFullPathName() + "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>\"");

		juce_runSystemCommand ("osascript -e \"tell application \\\"Dock\\\" to quit\"");
	}
}

int PlatformUtilities::getOSXMinorVersionNumber()
{
	SInt32 versionMinor = 0;
	OSErr err = Gestalt (gestaltSystemVersionMinor, &versionMinor);
	(void) err;
	jassert (err == noErr);
	return (int) versionMinor;
}

#if ! JUCE_ONLY_BUILD_CORE_LIBRARY

bool AlertWindow::showNativeDialogBox (const String& title,
									   const String& bodyText,
									   bool isOkCancel)
{
	const ScopedAutoReleasePool pool;
	return NSRunAlertPanel (juceStringToNS (title),
							juceStringToNS (bodyText),
							@"Ok",
							isOkCancel ? @"Cancel" : nil,
							nil) == NSAlertDefaultReturn;
}

bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMoveFiles)
{
	if (files.size() == 0)
		return false;

	MouseInputSource* draggingSource = Desktop::getInstance().getDraggingMouseSource(0);

	if (draggingSource == 0)
	{
		jassertfalse  // This method must be called in response to a component's mouseDown or mouseDrag event!
		return false;
	}

	Component* sourceComp = draggingSource->getComponentUnderMouse();

	if (sourceComp == 0)
	{
		jassertfalse  // This method must be called in response to a component's mouseDown or mouseDrag event!
		return false;
	}

	const ScopedAutoReleasePool pool;

	NSView* view = (NSView*) sourceComp->getWindowHandle();

	if (view == 0)
		return false;

	NSPasteboard* pboard = [NSPasteboard pasteboardWithName: NSDragPboard];
	[pboard declareTypes: [NSArray arrayWithObject: NSFilenamesPboardType]
				   owner: nil];

	NSMutableArray* filesArray = [NSMutableArray arrayWithCapacity: 4];
	for (int i = 0; i < files.size(); ++i)
		[filesArray addObject: juceStringToNS (files[i])];

	[pboard setPropertyList: filesArray
					forType: NSFilenamesPboardType];

	NSPoint dragPosition = [view convertPoint: [[[view window] currentEvent] locationInWindow]
									 fromView: nil];
	dragPosition.x -= 16;
	dragPosition.y -= 16;

	[view dragImage: [[NSWorkspace sharedWorkspace] iconForFile: juceStringToNS (files[0])]
				 at: dragPosition
			 offset: NSMakeSize (0, 0)
			  event: [[view window] currentEvent]
		 pasteboard: pboard
			 source: view
		  slideBack: YES];

	return true;
}

bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
{
	jassertfalse	// not implemented!
	return false;
}

bool Desktop::canUseSemiTransparentWindows() throw()
{
	return true;
}

const Point<int> Desktop::getMousePosition()
{
	const ScopedAutoReleasePool pool;
	const NSPoint p ([NSEvent mouseLocation]);
	return Point<int> (roundToInt (p.x), roundToInt ([[[NSScreen screens] objectAtIndex: 0] frame].size.height - p.y));
}

void Desktop::setMousePosition (const Point<int>& newPosition)
{
	// this rubbish needs to be done around the warp call, to avoid causing a
	// bizarre glitch..
	CGAssociateMouseAndMouseCursorPosition (false);
	CGWarpMouseCursorPosition (CGPointMake (newPosition.getX(), newPosition.getY()));
	CGAssociateMouseAndMouseCursorPosition (true);
}

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
class ScreenSaverDefeater   : public Timer,
							  public DeletedAtShutdown
{
public:
	ScreenSaverDefeater() throw()
	{
		startTimer (10000);
		timerCallback();
	}

	~ScreenSaverDefeater() {}

	void timerCallback()
	{
		if (Process::isForegroundProcess())
			UpdateSystemActivity (UsrActivity);
	}
};

static ScreenSaverDefeater* screenSaverDefeater = 0;

void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
{
	if (isEnabled)
	{
		deleteAndZero (screenSaverDefeater);
	}
	else if (screenSaverDefeater == 0)
	{
		screenSaverDefeater = new ScreenSaverDefeater();
	}
}

bool Desktop::isScreenSaverEnabled() throw()
{
	return screenSaverDefeater == 0;
}

#else

static IOPMAssertionID screenSaverDisablerID = 0;

void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
{
	if (isEnabled)
	{
		if (screenSaverDisablerID != 0)
		{
			IOPMAssertionRelease (screenSaverDisablerID);
			screenSaverDisablerID = 0;
		}
	}
	else
	{
		if (screenSaverDisablerID == 0)
		{
#if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
			IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn,
										 CFSTR ("Juce"), &screenSaverDisablerID);
#else
			IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn,
								 &screenSaverDisablerID);
#endif
		}
	}
}

bool Desktop::isScreenSaverEnabled() throw()
{
	return screenSaverDisablerID == 0;
}

#endif

void juce_updateMultiMonitorInfo (Array <Rectangle<int> >& monitorCoords, const bool clipToWorkArea)
{
	const ScopedAutoReleasePool pool;
	monitorCoords.clear();
	NSArray* screens = [NSScreen screens];
	const CGFloat mainScreenBottom = [[[NSScreen screens] objectAtIndex: 0] frame].size.height;

	for (unsigned int i = 0; i < [screens count]; ++i)
	{
		NSScreen* s = (NSScreen*) [screens objectAtIndex: i];

		NSRect r = clipToWorkArea ? [s visibleFrame]
								  : [s frame];

		monitorCoords.add (Rectangle<int> ((int) r.origin.x,
										   (int) (mainScreenBottom - (r.origin.y + r.size.height)),
										   (int) r.size.width,
										   (int) r.size.height));
	}

	jassert (monitorCoords.size() > 0);
}

#endif

#endif
/*** End of inlined file: juce_mac_MiscUtilities.mm ***/


#endif


/*** Start of inlined file: juce_mac_Debugging.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

void Logger::outputDebugString (const String& text) throw()
{
	std::cerr << text << std::endl;
}

bool JUCE_PUBLIC_FUNCTION juce_isRunningUnderDebugger()
{
	static char testResult = 0;

	if (testResult == 0)
	{
		struct kinfo_proc info;
		int m[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
		size_t sz = sizeof (info);
		sysctl (m, 4, &info, &sz, 0, 0);
		testResult = ((info.kp_proc.p_flag & P_TRACED) != 0) ? 1 : -1;
	}

	return testResult > 0;
}

bool JUCE_CALLTYPE Process::isRunningUnderDebugger()
{
	return juce_isRunningUnderDebugger();
}

#endif
/*** End of inlined file: juce_mac_Debugging.mm ***/

#if ! JUCE_ONLY_BUILD_CORE_LIBRARY
 #if JUCE_IPHONE

/*** Start of inlined file: juce_mac_Fonts.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  #define SUPPORT_10_4_FONTS 1
  #define NEW_CGFONT_FUNCTIONS_UNAVAILABLE (CGFontCreateWithFontName == 0)

  #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
	#define SUPPORT_ONLY_10_4_FONTS 1
  #endif

  END_JUCE_NAMESPACE
  @interface NSFont (PrivateHack)
	- (NSGlyph) _defaultGlyphForChar: (unichar) theChar;
  @end
  BEGIN_JUCE_NAMESPACE
#endif

class MacTypeface  : public Typeface
{
public:

	MacTypeface (const Font& font)
		: Typeface (font.getTypefaceName())
	{
		const ScopedAutoReleasePool pool;
		renderingTransform = CGAffineTransformIdentity;

		bool needsItalicTransform = false;

#if JUCE_IPHONE
		NSString* fontName = juceStringToNS (font.getTypefaceName());

		if (font.isItalic() || font.isBold())
		{
			NSArray* familyFonts = [UIFont fontNamesForFamilyName: juceStringToNS (font.getTypefaceName())];

			for (NSString* i in familyFonts)
			{
				const String fn (nsStringToJuce (i));
				const String afterDash (fn.fromFirstOccurrenceOf ("-", false, false));

				const bool probablyBold = afterDash.containsIgnoreCase ("bold") || fn.endsWithIgnoreCase ("bold");
				const bool probablyItalic = afterDash.containsIgnoreCase ("oblique")
											 || afterDash.containsIgnoreCase ("italic")
											 || fn.endsWithIgnoreCase ("oblique")
											 || fn.endsWithIgnoreCase ("italic");

				if (probablyBold == font.isBold()
					 && probablyItalic == font.isItalic())
				{
					fontName = i;
					needsItalicTransform = false;
					break;
				}
				else if (probablyBold && (! probablyItalic) && probablyBold == font.isBold())
				{
					fontName = i;
					needsItalicTransform = true; // not ideal, so carry on in case we find a better one
				}
			}

			if (needsItalicTransform)
				renderingTransform.c = 0.15f;
		}

		fontRef = CGFontCreateWithFontName ((CFStringRef) fontName);

		const int ascender = abs (CGFontGetAscent (fontRef));
		const float totalHeight = ascender + abs (CGFontGetDescent (fontRef));
		ascent = ascender / totalHeight;
		unitsToHeightScaleFactor = 1.0f / totalHeight;
		fontHeightToCGSizeFactor = CGFontGetUnitsPerEm (fontRef) / totalHeight;
#else
		nsFont = [NSFont fontWithName: juceStringToNS (font.getTypefaceName()) size: 1024];

		if (font.isItalic())
		{
			NSFont* newFont = [[NSFontManager sharedFontManager] convertFont: nsFont
																 toHaveTrait: NSItalicFontMask];

			if (newFont == nsFont)
				needsItalicTransform = true; // couldn't find a proper italic version, so fake it with a transform..

			nsFont = newFont;
		}

		if (font.isBold())
			nsFont = [[NSFontManager sharedFontManager] convertFont: nsFont toHaveTrait: NSBoldFontMask];

		[nsFont retain];

		ascent = fabsf ((float) [nsFont ascender]);
		float totalSize = ascent + fabsf ((float) [nsFont descender]);
		ascent /= totalSize;

		pathTransform = AffineTransform::identity.scale (1.0f / totalSize, 1.0f / totalSize);

		if (needsItalicTransform)
		{
			pathTransform = pathTransform.sheared (-0.15f, 0.0f);
			renderingTransform.c = 0.15f;
		}

#if SUPPORT_ONLY_10_4_FONTS
		ATSFontRef atsFont = ATSFontFindFromName ((CFStringRef) [nsFont fontName], kATSOptionFlagsDefault);

		if (atsFont == 0)
			atsFont = ATSFontFindFromPostScriptName ((CFStringRef) [nsFont fontName], kATSOptionFlagsDefault);

		fontRef = CGFontCreateWithPlatformFont (&atsFont);

		const float totalHeight = fabsf ([nsFont ascender]) + fabsf([nsFont descender]);
		unitsToHeightScaleFactor = 1.0f / totalHeight;
		fontHeightToCGSizeFactor = 1024.0f / totalHeight;
#else
  #if SUPPORT_10_4_FONTS
		if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
		{
			ATSFontRef atsFont = ATSFontFindFromName ((CFStringRef) [nsFont fontName], kATSOptionFlagsDefault);

			if (atsFont == 0)
				atsFont = ATSFontFindFromPostScriptName ((CFStringRef) [nsFont fontName], kATSOptionFlagsDefault);

			fontRef = CGFontCreateWithPlatformFont (&atsFont);

			const float totalHeight = fabsf ([nsFont ascender]) + fabsf([nsFont descender]);
			unitsToHeightScaleFactor = 1.0f / totalHeight;
			fontHeightToCGSizeFactor = 1024.0f / totalHeight;
		}
		else
  #endif
		{
			fontRef = CGFontCreateWithFontName ((CFStringRef) [nsFont fontName]);

			const int totalHeight = abs (CGFontGetAscent (fontRef)) + abs (CGFontGetDescent (fontRef));
			unitsToHeightScaleFactor = 1.0f / totalHeight;
			fontHeightToCGSizeFactor = CGFontGetUnitsPerEm (fontRef) / (float) totalHeight;
		}
#endif

#endif
	}

	~MacTypeface()
	{
#if ! JUCE_IPHONE
		[nsFont release];
#endif
		if (fontRef != 0)
			CGFontRelease (fontRef);
	}

	float getAscent() const
	{
		return ascent;
	}

	float getDescent() const
	{
		return 1.0f - ascent;
	}

	float getStringWidth (const String& text)
	{
		if (fontRef == 0 || text.isEmpty())
			return 0;

		const int length = text.length();
		HeapBlock <CGGlyph> glyphs;
		createGlyphsForString (text, length, glyphs);

		float x = 0;

#if SUPPORT_ONLY_10_4_FONTS
		HeapBlock <NSSize> advances (length);
		[nsFont getAdvancements: advances forGlyphs: reinterpret_cast <NSGlyph*> (glyphs.getData()) count: length];

		for (int i = 0; i < length; ++i)
			x += advances[i].width;
#else
  #if SUPPORT_10_4_FONTS
		if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
		{
			HeapBlock <NSSize> advances (length);
			[nsFont getAdvancements: advances forGlyphs: reinterpret_cast<NSGlyph*> (glyphs.getData()) count: length];

			for (int i = 0; i < length; ++i)
				x += advances[i].width;
		}
		else
  #endif
		{
			HeapBlock <int> advances (length);

			if (CGFontGetGlyphAdvances (fontRef, glyphs, length, advances))
				for (int i = 0; i < length; ++i)
					x += advances[i];
		}
#endif

		return x * unitsToHeightScaleFactor;
	}

	void getGlyphPositions (const String& text, Array <int>& resultGlyphs, Array <float>& xOffsets)
	{
		xOffsets.add (0);

		if (fontRef == 0 || text.isEmpty())
			return;

		const int length = text.length();
		HeapBlock <CGGlyph> glyphs;
		createGlyphsForString (text, length, glyphs);

#if SUPPORT_ONLY_10_4_FONTS
		HeapBlock <NSSize> advances (length);
		[nsFont getAdvancements: advances forGlyphs: reinterpret_cast <NSGlyph*> (glyphs.getData()) count: length];

		int x = 0;
		for (int i = 0; i < length; ++i)
		{
			x += advances[i].width;
			xOffsets.add (x * unitsToHeightScaleFactor);
			resultGlyphs.add (reinterpret_cast <NSGlyph*> (glyphs.getData())[i]);
		}

#else
  #if SUPPORT_10_4_FONTS
		if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
		{
			HeapBlock <NSSize> advances (length);
			NSGlyph* const nsGlyphs = reinterpret_cast<NSGlyph*> (glyphs.getData());
			[nsFont getAdvancements: advances forGlyphs: nsGlyphs count: length];

			float x = 0;
			for (int i = 0; i < length; ++i)
			{
				x += advances[i].width;
				xOffsets.add (x * unitsToHeightScaleFactor);
				resultGlyphs.add (nsGlyphs[i]);
			}
		}
		else
  #endif
		{
			HeapBlock <int> advances (length);

			if (CGFontGetGlyphAdvances (fontRef, glyphs, length, advances))
			{
				int x = 0;
				for (int i = 0; i < length; ++i)
				{
					x += advances [i];
					xOffsets.add (x * unitsToHeightScaleFactor);
					resultGlyphs.add (glyphs[i]);
				}
			}
		}
#endif
	}

	bool getOutlineForGlyph (int glyphNumber, Path& path)
	{
#if JUCE_IPHONE
		return false;
#else
		if (nsFont == 0)
			return false;

		// we might need to apply a transform to the path, so it mustn't have anything else in it
		jassert (path.isEmpty());

		const ScopedAutoReleasePool pool;

		NSBezierPath* bez = [NSBezierPath bezierPath];
		[bez moveToPoint: NSMakePoint (0, 0)];
		[bez appendBezierPathWithGlyph: (NSGlyph) glyphNumber
								inFont: nsFont];

		for (int i = 0; i < [bez elementCount]; ++i)
		{
			NSPoint p[3];
			switch ([bez elementAtIndex: i associatedPoints: p])
			{
			case NSMoveToBezierPathElement:
				path.startNewSubPath ((float) p[0].x, (float) -p[0].y);
				break;
			case NSLineToBezierPathElement:
				path.lineTo ((float) p[0].x, (float) -p[0].y);
				break;
			case NSCurveToBezierPathElement:
				path.cubicTo ((float) p[0].x, (float) -p[0].y, (float) p[1].x, (float) -p[1].y, (float) p[2].x, (float) -p[2].y);
				break;
			case NSClosePathBezierPathElement:
				path.closeSubPath();
				break;
			default:
				jassertfalse
				break;
			}
		}

		path.applyTransform (pathTransform);
		return true;
#endif
	}

	juce_UseDebuggingNewOperator

	CGFontRef fontRef;
	float fontHeightToCGSizeFactor;
	CGAffineTransform renderingTransform;

private:
	float ascent, unitsToHeightScaleFactor;

#if JUCE_IPHONE

#else
	NSFont* nsFont;
	AffineTransform pathTransform;
#endif

	void createGlyphsForString (const juce_wchar* const text, const int length, HeapBlock <CGGlyph>& glyphs)
	{
#if SUPPORT_10_4_FONTS
  #if ! SUPPORT_ONLY_10_4_FONTS
		if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
  #endif
		{
			glyphs.malloc (sizeof (NSGlyph) * length, 1);
			NSGlyph* const nsGlyphs = reinterpret_cast<NSGlyph*> (glyphs.getData());

			for (int i = 0; i < length; ++i)
				nsGlyphs[i] = (NSGlyph) [nsFont _defaultGlyphForChar: text[i]];

			return;
		}
#endif

#if ! SUPPORT_ONLY_10_4_FONTS
		if (charToGlyphMapper == 0)
			charToGlyphMapper = new CharToGlyphMapper (fontRef);

		glyphs.malloc (length);

		for (int i = 0; i < length; ++i)
			glyphs[i] = (CGGlyph) charToGlyphMapper->getGlyphForCharacter (text[i]);
#endif
	}

#if ! SUPPORT_ONLY_10_4_FONTS
	// Reads a CGFontRef's character map table to convert unicode into glyph numbers
	class CharToGlyphMapper
	{
	public:
		CharToGlyphMapper (CGFontRef fontRef)
			: segCount (0), endCode (0), startCode (0), idDelta (0),
			  idRangeOffset (0), glyphIndexes (0)
		{
			CFDataRef cmapTable = CGFontCopyTableForTag (fontRef, 'cmap');

			if (cmapTable != 0)
			{
				const int numSubtables = getValue16 (cmapTable, 2);

				for (int i = 0; i < numSubtables; ++i)
				{
					if (getValue16 (cmapTable, i * 8 + 4) == 0) // check for platform ID of 0
					{
						const int offset = getValue32 (cmapTable, i * 8 + 8);

						if (getValue16 (cmapTable, offset) == 4) // check that it's format 4..
						{
							const int length = getValue16 (cmapTable, offset + 2);
							const int segCountX2 =  getValue16 (cmapTable, offset + 6);
							segCount = segCountX2 / 2;
							const int endCodeOffset = offset + 14;
							const int startCodeOffset = endCodeOffset + 2 + segCountX2;
							const int idDeltaOffset = startCodeOffset + segCountX2;
							const int idRangeOffsetOffset = idDeltaOffset + segCountX2;
							const int glyphIndexesOffset = idRangeOffsetOffset + segCountX2;

							endCode = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + endCodeOffset, segCountX2);
							startCode = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + startCodeOffset, segCountX2);
							idDelta = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + idDeltaOffset, segCountX2);
							idRangeOffset = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + idRangeOffsetOffset, segCountX2);
							glyphIndexes = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + glyphIndexesOffset, offset + length - glyphIndexesOffset);
						}

						break;
					}
				}

				CFRelease (cmapTable);
			}
		}

		~CharToGlyphMapper()
		{
			if (endCode != 0)
			{
				CFRelease (endCode);
				CFRelease (startCode);
				CFRelease (idDelta);
				CFRelease (idRangeOffset);
				CFRelease (glyphIndexes);
			}
		}

		int getGlyphForCharacter (const juce_wchar c) const
		{
			for (int i = 0; i < segCount; ++i)
			{
				if (getValue16 (endCode, i * 2) >= c)
				{
					const int start = getValue16 (startCode, i * 2);
					if (start > c)
						break;

					const int delta = getValue16 (idDelta, i * 2);
					const int rangeOffset = getValue16 (idRangeOffset, i * 2);

					if (rangeOffset == 0)
						return delta + c;
					else
						return getValue16 (glyphIndexes, 2 * ((rangeOffset / 2) + (c - start) - (segCount - i)));
				}
			}

			// If we failed to find it "properly", this dodgy fall-back seems to do the trick for most fonts!
			return jmax (-1, c - 29);
		}

	private:
		int segCount;
		CFDataRef endCode, startCode, idDelta, idRangeOffset, glyphIndexes;

		static uint16 getValue16 (CFDataRef data, const int index)
		{
			return CFSwapInt16BigToHost (*(UInt16*) (CFDataGetBytePtr (data) + index));
		}

		static uint32 getValue32 (CFDataRef data, const int index)
		{
			return CFSwapInt32BigToHost (*(UInt32*) (CFDataGetBytePtr (data) + index));
		}
	};

	ScopedPointer <CharToGlyphMapper> charToGlyphMapper;
#endif

	MacTypeface (const MacTypeface&);
	MacTypeface& operator= (const MacTypeface&);
};

const Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font)
{
	return new MacTypeface (font);
}

const StringArray Font::findAllTypefaceNames()
{
	StringArray names;

	const ScopedAutoReleasePool pool;

#if JUCE_IPHONE
	NSArray* fonts = [UIFont familyNames];
#else
	NSArray* fonts = [[NSFontManager sharedFontManager] availableFontFamilies];
#endif

	for (unsigned int i = 0; i < [fonts count]; ++i)
		names.add (nsStringToJuce ((NSString*) [fonts objectAtIndex: i]));

	names.sort (true);
	return names;
}

void Font::getPlatformDefaultFontNames (String& defaultSans, String& defaultSerif, String& defaultFixed)
{
#if JUCE_IPHONE
	defaultSans  = "Helvetica";
	defaultSerif = "Times New Roman";
	defaultFixed = "Courier New";
#else
	defaultSans  = "Lucida Grande";
	defaultSerif = "Times New Roman";
	defaultFixed = "Monaco";
#endif
}

#endif
/*** End of inlined file: juce_mac_Fonts.mm ***/



/*** Start of inlined file: juce_mac_CoreGraphicsContext.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class CoreGraphicsImage : public Image
{
public:

	CoreGraphicsImage (const PixelFormat format_,
					   const int imageWidth_,
					   const int imageHeight_,
					   const bool clearImage)
		: Image (format_, imageWidth_, imageHeight_, clearImage)
	{
		CGColorSpaceRef colourSpace = (format == Image::SingleChannel) ? CGColorSpaceCreateDeviceGray()
																	   : CGColorSpaceCreateDeviceRGB();

		context = CGBitmapContextCreate (imageData, imageWidth, imageHeight, 8, lineStride,
										 colourSpace, getCGImageFlags (*this));

		CGColorSpaceRelease (colourSpace);
	}

	~CoreGraphicsImage()
	{
		CGContextRelease (context);
	}

	LowLevelGraphicsContext* createLowLevelContext();

	static CGImageRef createImage (const Image& juceImage, const bool forAlpha, CGColorSpaceRef colourSpace)
	{
		const CoreGraphicsImage* nativeImage = dynamic_cast <const CoreGraphicsImage*> (&juceImage);

		if (nativeImage != 0 && (juceImage.getFormat() == Image::SingleChannel || ! forAlpha))
		{
			return CGBitmapContextCreateImage (nativeImage->context);
		}
		else
		{
			const Image::BitmapData srcData (juceImage, 0, 0, juceImage.getWidth(), juceImage.getHeight());

			CGDataProviderRef provider = CGDataProviderCreateWithData (0, srcData.data, srcData.lineStride * srcData.height, 0);

			CGImageRef imageRef = CGImageCreate (srcData.width, srcData.height,
												 8, srcData.pixelStride * 8, srcData.lineStride,
												 colourSpace, getCGImageFlags (juceImage), provider,
												 0, true, kCGRenderingIntentDefault);

			CGDataProviderRelease (provider);
			return imageRef;
		}
	}

#if JUCE_MAC
	static NSImage* createNSImage (const Image& image)
	{
		const ScopedAutoReleasePool pool;

		NSImage* im = [[NSImage alloc] init];
		[im setSize: NSMakeSize (image.getWidth(), image.getHeight())];
		[im lockFocus];

		CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
		CGImageRef imageRef = createImage (image, false, colourSpace);
		CGColorSpaceRelease (colourSpace);

		CGContextRef cg = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
		CGContextDrawImage (cg, CGRectMake (0, 0, image.getWidth(), image.getHeight()), imageRef);

		CGImageRelease (imageRef);
		[im unlockFocus];

		return im;
	}
#endif

	CGContextRef context;

private:
	static CGBitmapInfo getCGImageFlags (const Image& image)
	{
#if JUCE_BIG_ENDIAN
		return image.getFormat() == Image::ARGB ? (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big) : kCGBitmapByteOrderDefault;
#else
		return image.getFormat() == Image::ARGB ? (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little) : kCGBitmapByteOrderDefault;
#endif
	}
};

Image* Image::createNativeImage (const PixelFormat format, const int imageWidth, const int imageHeight, const bool clearImage)
{
#if USE_COREGRAPHICS_RENDERING
	return new CoreGraphicsImage (format == RGB ? ARGB : format, imageWidth, imageHeight, clearImage);
#else
	return new Image (format, imageWidth, imageHeight, clearImage);
#endif
}

class CoreGraphicsContext   : public LowLevelGraphicsContext
{
public:
	CoreGraphicsContext (CGContextRef context_, const float flipHeight_)
		: context (context_),
		  flipHeight (flipHeight_),
		  state (new SavedState()),
		  numGradientLookupEntries (0)
	{
		CGContextRetain (context);
		CGContextSaveGState(context);
		CGContextSetShouldSmoothFonts (context, true);
		CGContextSetShouldAntialias (context, true);
		CGContextSetBlendMode (context, kCGBlendModeNormal);
		rgbColourSpace = CGColorSpaceCreateDeviceRGB();
		greyColourSpace = CGColorSpaceCreateDeviceGray();
		gradientCallbacks.version = 0;
		gradientCallbacks.evaluate = gradientCallback;
		gradientCallbacks.releaseInfo = 0;
		setFont (Font());
	}

	~CoreGraphicsContext()
	{
		CGContextRestoreGState (context);
		CGContextRelease (context);
		CGColorSpaceRelease (rgbColourSpace);
		CGColorSpaceRelease (greyColourSpace);
	}

	bool isVectorDevice() const	 { return false; }

	void setOrigin (int x, int y)
	{
		CGContextTranslateCTM (context, x, -y);
	}

	bool clipToRectangle (const Rectangle<int>& r)
	{
		CGContextClipToRect (context, CGRectMake (r.getX(), flipHeight - r.getBottom(), r.getWidth(), r.getHeight()));
		return ! isClipEmpty();
	}

	bool clipToRectangleList (const RectangleList& clipRegion)
	{
		if (clipRegion.isEmpty())
		{
			CGContextClipToRect (context, CGRectMake (0, 0, 0, 0));
			return false;
		}
		else
		{
			const int numRects = clipRegion.getNumRectangles();

			HeapBlock <CGRect> rects (numRects);
			for (int i = 0; i < numRects; ++i)
			{
				const Rectangle<int>& r = clipRegion.getRectangle(i);
				rects[i] = CGRectMake (r.getX(), flipHeight - r.getBottom(), r.getWidth(), r.getHeight());
			}

			CGContextClipToRects (context, rects, numRects);
			return ! isClipEmpty();
		}
	}

	void excludeClipRectangle (const Rectangle<int>& r)
	{
		RectangleList remaining (getClipBounds());
		remaining.subtract (r);
		clipToRectangleList (remaining);
	}

	void clipToPath (const Path& path, const AffineTransform& transform)
	{
		createPath (path, transform);
		CGContextClip (context);
	}

	void clipToImageAlpha (const Image& sourceImage, const Rectangle<int>& srcClip, const AffineTransform& transform)
	{
		if (! transform.isSingularity())
		{
			Image* singleChannelImage = createAlphaChannelImage (sourceImage);
			CGImageRef image = CoreGraphicsImage::createImage (*singleChannelImage, true, greyColourSpace);

			flip();
			AffineTransform t (AffineTransform::scale (1.0f, -1.0f).translated (0, sourceImage.getHeight()).followedBy (transform));
			applyTransform (t);

			CGRect r = CGRectMake (0, 0, sourceImage.getWidth(), sourceImage.getHeight());
			CGContextClipToMask (context, r, image);

			applyTransform (t.inverted());
			flip();

			CGImageRelease (image);
			deleteAlphaChannelImage (sourceImage, singleChannelImage);
		}
	}

	bool clipRegionIntersects (const Rectangle<int>& r)
	{
		return getClipBounds().intersects (r);
	}

	const Rectangle<int> getClipBounds() const
	{
		CGRect bounds = CGRectIntegral (CGContextGetClipBoundingBox (context));

		return Rectangle<int> (roundToInt (bounds.origin.x),
							   roundToInt (flipHeight - (bounds.origin.y + bounds.size.height)),
							   roundToInt (bounds.size.width),
							   roundToInt (bounds.size.height));
	}

	bool isClipEmpty() const
	{
		return CGRectIsEmpty (CGContextGetClipBoundingBox (context));
	}

	void saveState()
	{
		CGContextSaveGState (context);
		stateStack.add (new SavedState (*state));
	}

	void restoreState()
	{
		CGContextRestoreGState (context);

		SavedState* const top = stateStack.getLast();

		if (top != 0)
		{
			state = top;
			stateStack.removeLast (1, false);
		}
		else
		{
			jassertfalse // trying to pop with an empty stack!
		}
	}

	void setFill (const FillType& fillType)
	{
		state->fillType = fillType;

		if (fillType.isColour())
		{
			CGContextSetRGBFillColor (context, fillType.colour.getFloatRed(), fillType.colour.getFloatGreen(),
									  fillType.colour.getFloatBlue(), fillType.colour.getFloatAlpha());
			CGContextSetAlpha (context, 1.0f);
		}
	}

	void setOpacity (float newOpacity)
	{
		state->fillType.setOpacity (newOpacity);
		setFill (state->fillType);
	}

	void setInterpolationQuality (Graphics::ResamplingQuality quality)
	{
		CGContextSetInterpolationQuality (context, quality == Graphics::lowResamplingQuality
													? kCGInterpolationLow
													: kCGInterpolationHigh);
	}

	void fillRect (const Rectangle<int>& r, const bool replaceExistingContents)
	{
		CGRect cgRect = CGRectMake (r.getX(), flipHeight - r.getBottom(), r.getWidth(), r.getHeight());

		if (replaceExistingContents)
		{
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
			CGContextClearRect (context, cgRect);
#else
  #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
			if (CGContextDrawLinearGradient == 0) // (just a way of checking whether we're running in 10.5 or later)
				CGContextClearRect (context, cgRect);
			else
  #endif
				CGContextSetBlendMode (context, kCGBlendModeCopy);
#endif

			fillRect (r, false);
			CGContextSetBlendMode (context, kCGBlendModeNormal);
		}
		else
		{
			if (state->fillType.isColour())
			{
				CGContextFillRect (context, cgRect);
			}
			else if (state->fillType.isGradient())
			{
				CGContextSaveGState (context);
				CGContextClipToRect (context, cgRect);
				drawGradient();
				CGContextRestoreGState (context);
			}
			else
			{
				CGContextSaveGState (context);
				CGContextClipToRect (context, cgRect);
				drawImage (*(state->fillType.image), state->fillType.image->getBounds(), state->fillType.transform, true);
				CGContextRestoreGState (context);
			}
		}
	}

	void fillPath (const Path& path, const AffineTransform& transform)
	{
		CGContextSaveGState (context);

		if (state->fillType.isColour())
		{
			flip();
			applyTransform (transform);
			createPath (path);

			if (path.isUsingNonZeroWinding())
				CGContextFillPath (context);
			else
				CGContextEOFillPath (context);
		}
		else
		{
			createPath (path, transform);

			if (path.isUsingNonZeroWinding())
				CGContextClip (context);
			else
				CGContextEOClip (context);

			if (state->fillType.isGradient())
				drawGradient();
			else
				drawImage (*(state->fillType.image), state->fillType.image->getBounds(), state->fillType.transform, true);
		}

		CGContextRestoreGState (context);
	}

	void drawImage (const Image& sourceImage, const Rectangle<int>& srcClip,
					const AffineTransform& transform, const bool fillEntireClipAsTiles)
	{
		jassert (sourceImage.getBounds().contains (srcClip));

		CGImageRef fullImage = CoreGraphicsImage::createImage (sourceImage, false, rgbColourSpace);
		CGImageRef image = fullImage;

		if (srcClip != sourceImage.getBounds())
		{
			image = CGImageCreateWithImageInRect (fullImage, CGRectMake (srcClip.getX(), srcClip.getY(),
																		 srcClip.getWidth(), srcClip.getHeight()));
			CGImageRelease (fullImage);
		}

		CGContextSaveGState (context);
		CGContextSetAlpha (context, state->fillType.getOpacity());

		flip();
		applyTransform (AffineTransform::scale (1.0f, -1.0f).translated (0, srcClip.getHeight()).followedBy (transform));
		CGRect imageRect = CGRectMake (0, 0, srcClip.getWidth(), srcClip.getHeight());

		if (fillEntireClipAsTiles)
		{
#if JUCE_IPHONE
			CGContextDrawTiledImage (context, imageRect, image);
#else
  #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
			// There's a bug in CGContextDrawTiledImage that makes it incredibly slow
			// if it's doing a transformation - it's quicker to just draw lots of images manually
			if (CGContextDrawTiledImage != 0 && transform.isOnlyTranslation())
				CGContextDrawTiledImage (context, imageRect, image);
			else
  #endif
			{
				// Fallback to manually doing a tiled fill on 10.4
				CGRect clip = CGRectIntegral (CGContextGetClipBoundingBox (context));
				const int iw = srcClip.getWidth();
				const int ih = srcClip.getHeight();

				int x = 0, y = 0;
				while (x > clip.origin.x)   x -= iw;
				while (y > clip.origin.y)   y -= ih;

				const int right = (int) (clip.origin.x + clip.size.width);
				const int bottom = (int) (clip.origin.y + clip.size.height);

				while (y < bottom)
				{
					for (int x2 = x; x2 < right; x2 += iw)
						CGContextDrawImage (context, CGRectMake (x2, y, iw, ih), image);

					y += ih;
				}
			}
#endif
		}
		else
		{
			CGContextDrawImage (context, imageRect, image);
		}

		CGImageRelease (image); // (This causes a memory bug in iPhone sim 3.0 - try upgrading to a later version if you hit this)
		CGContextRestoreGState (context);
	}

	void drawLine (double x1, double y1, double x2, double y2)
	{
		CGContextSetLineCap (context, kCGLineCapSquare);
		CGContextSetLineWidth (context, 1.0f);
		CGContextSetRGBStrokeColor (context,
									state->fillType.colour.getFloatRed(), state->fillType.colour.getFloatGreen(),
									state->fillType.colour.getFloatBlue(), state->fillType.colour.getFloatAlpha());

		CGPoint line[] = { { (CGFloat) x1, flipHeight - (CGFloat) y1 },
						   { (CGFloat) x2, flipHeight - (CGFloat) y2 } };

		CGContextStrokeLineSegments (context, line, 1);
	}

	void drawVerticalLine (const int x, double top, double bottom)
	{
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5
		CGContextFillRect (context, CGRectMake (x, flipHeight - (float) bottom, 1.0f, (float) (bottom - top)));
#else
		// On Leopard, unless both co-ordinates are non-integer, it disables anti-aliasing, so nudge
		// the x co-ord slightly to trick it..
		CGContextFillRect (context, CGRectMake (x + 1.0f / 256.0f, flipHeight - (float) bottom, 1.0f + 1.0f / 256.0f, (float) (bottom - top)));
#endif
	}

	void drawHorizontalLine (const int y, double left, double right)
	{
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5
		CGContextFillRect (context, CGRectMake ((float) left, flipHeight - (y + 1.0f), (float) (right - left), 1.0f));
#else
		// On Leopard, unless both co-ordinates are non-integer, it disables anti-aliasing, so nudge
		// the x co-ord slightly to trick it..
		CGContextFillRect (context, CGRectMake ((float) left, flipHeight - (y + (1.0f + 1.0f / 256.0f)), (float) (right - left), 1.0f + 1.0f / 256.0f));
#endif
	}

	void setFont (const Font& newFont)
	{
		if (state->font != newFont)
		{
			state->fontRef = 0;
			state->font = newFont;

			MacTypeface* mf = dynamic_cast <MacTypeface*> (state->font.getTypeface());

			if (mf != 0)
			{
				state->fontRef = mf->fontRef;
				CGContextSetFont (context, state->fontRef);
				CGContextSetFontSize (context, state->font.getHeight() * mf->fontHeightToCGSizeFactor);

				state->fontTransform = mf->renderingTransform;
				state->fontTransform.a *= state->font.getHorizontalScale();
				CGContextSetTextMatrix (context, state->fontTransform);
			}
		}
	}

	const Font getFont()
	{
		return state->font;
	}

	void drawGlyph (int glyphNumber, const AffineTransform& transform)
	{
		if (state->fontRef != 0 && state->fillType.isColour())
		{
			if (transform.isOnlyTranslation())
			{
				CGGlyph g = glyphNumber;
				CGContextShowGlyphsAtPoint (context, transform.getTranslationX(),
											flipHeight - roundToInt (transform.getTranslationY()), &g, 1);
			}
			else
			{
				CGContextSaveGState (context);
				flip();
				applyTransform (transform);

				CGAffineTransform t = state->fontTransform;
				t.d = -t.d;
				CGContextSetTextMatrix (context, t);

				CGGlyph g = glyphNumber;
				CGContextShowGlyphsAtPoint (context, 0, 0, &g, 1);

				CGContextSetTextMatrix (context, state->fontTransform);
				CGContextRestoreGState (context);
			}
		}
		else
		{
			Path p;
			Font& f = state->font;
			f.getTypeface()->getOutlineForGlyph (glyphNumber, p);

			fillPath (p, AffineTransform::scale (f.getHeight() * f.getHorizontalScale(), f.getHeight())
										 .followedBy (transform));
		}
	}

private:
	CGContextRef context;
	const CGFloat flipHeight;
	CGColorSpaceRef rgbColourSpace, greyColourSpace;
	CGFunctionCallbacks gradientCallbacks;

	struct SavedState
	{
		SavedState()
			: font (1.0f), fontRef (0), fontTransform (CGAffineTransformIdentity)
		{
		}

		SavedState (const SavedState& other)
			: fillType (other.fillType), font (other.font), fontRef (other.fontRef),
			  fontTransform (other.fontTransform)
		{
		}

		~SavedState()
		{
		}

		FillType fillType;
		Font font;
		CGFontRef fontRef;
		CGAffineTransform fontTransform;
	};

	ScopedPointer <SavedState> state;
	OwnedArray <SavedState> stateStack;
	HeapBlock <PixelARGB> gradientLookupTable;
	int numGradientLookupEntries;

	static void gradientCallback (void* info, const CGFloat* inData, CGFloat* outData)
	{
		const CoreGraphicsContext* const g = static_cast <const CoreGraphicsContext*> (info);

		const int index = roundToInt (g->numGradientLookupEntries * inData[0]);
		PixelARGB colour (g->gradientLookupTable [jlimit (0, g->numGradientLookupEntries, index)]);
		colour.unpremultiply();

		outData[0] = colour.getRed() / 255.0f;
		outData[1] = colour.getGreen() / 255.0f;
		outData[2] = colour.getBlue() / 255.0f;
		outData[3] = colour.getAlpha() / 255.0f;
	}

	CGShadingRef createGradient (const AffineTransform& transform, ColourGradient gradient)
	{
		numGradientLookupEntries = gradient.createLookupTable (transform, gradientLookupTable);
		--numGradientLookupEntries;

		CGShadingRef result = 0;
		CGFunctionRef function = CGFunctionCreate (this, 1, 0, 4, 0, &gradientCallbacks);
		CGPoint p1 (CGPointMake (gradient.x1, gradient.y1));

		if (gradient.isRadial)
		{
			result = CGShadingCreateRadial (rgbColourSpace, p1, 0,
											p1, hypotf (gradient.x1 - gradient.x2, gradient.y1 - gradient.y2),
											function, true, true);
		}
		else
		{
			result = CGShadingCreateAxial (rgbColourSpace, p1,
										   CGPointMake (gradient.x2, gradient.y2),
										   function, true, true);
		}

		CGFunctionRelease (function);
		return result;
	}

	void drawGradient()
	{
		flip();
		applyTransform (state->fillType.transform);

		CGContextSetInterpolationQuality (context, kCGInterpolationDefault); // (This is required for 10.4, where there's a crash if
																			 // you draw a gradient with high quality interp enabled).
		CGShadingRef shading = createGradient (state->fillType.transform, *(state->fillType.gradient));
		CGContextSetAlpha (context, state->fillType.getOpacity());
		CGContextDrawShading (context, shading);
		CGShadingRelease (shading);
	}

	void createPath (const Path& path) const
	{
		CGContextBeginPath (context);
		Path::Iterator i (path);

		while (i.next())
		{
			switch (i.elementType)
			{
			case Path::Iterator::startNewSubPath:
				CGContextMoveToPoint (context, i.x1, i.y1);
				break;
			case Path::Iterator::lineTo:
				CGContextAddLineToPoint (context, i.x1, i.y1);
				break;
			case Path::Iterator::quadraticTo:
				CGContextAddQuadCurveToPoint (context, i.x1, i.y1, i.x2, i.y2);
				break;
			case Path::Iterator::cubicTo:
				CGContextAddCurveToPoint (context, i.x1, i.y1, i.x2, i.y2, i.x3, i.y3);
				break;
			case Path::Iterator::closePath:
				CGContextClosePath (context); break;
			default:
				jassertfalse
				break;
			}
		}
	}

	void createPath (const Path& path, const AffineTransform& transform) const
	{
		CGContextBeginPath (context);
		Path::Iterator i (path);

		while (i.next())
		{
			switch (i.elementType)
			{
			case Path::Iterator::startNewSubPath:
				transform.transformPoint (i.x1, i.y1);
				CGContextMoveToPoint (context, i.x1, flipHeight - i.y1);
				break;
			case Path::Iterator::lineTo:
				transform.transformPoint (i.x1, i.y1);
				CGContextAddLineToPoint (context, i.x1, flipHeight - i.y1);
				break;
			case Path::Iterator::quadraticTo:
				transform.transformPoint (i.x1, i.y1);
				transform.transformPoint (i.x2, i.y2);
				CGContextAddQuadCurveToPoint (context, i.x1, flipHeight - i.y1, i.x2, flipHeight - i.y2);
				break;
			case Path::Iterator::cubicTo:
				transform.transformPoint (i.x1, i.y1);
				transform.transformPoint (i.x2, i.y2);
				transform.transformPoint (i.x3, i.y3);
				CGContextAddCurveToPoint (context, i.x1, flipHeight - i.y1, i.x2, flipHeight - i.y2, i.x3, flipHeight - i.y3);
				break;
			case Path::Iterator::closePath:
				CGContextClosePath (context); break;
			default:
				jassertfalse
				break;
			}
		}
	}

	static Image* createAlphaChannelImage (const Image& im)
	{
		if (im.getFormat() == Image::SingleChannel)
			return const_cast <Image*> (&im);

		return im.createCopyOfAlphaChannel();
	}

	static void deleteAlphaChannelImage (const Image& im, Image* const alphaIm)
	{
		if (im.getFormat() != Image::SingleChannel)
			delete alphaIm;
	}

	void flip() const
	{
		CGContextConcatCTM (context, CGAffineTransformMake (1, 0, 0, -1, 0, flipHeight));
	}

	void applyTransform (const AffineTransform& transform) const
	{
		CGAffineTransform t;
		t.a = transform.mat00;
		t.b = transform.mat10;
		t.c = transform.mat01;
		t.d = transform.mat11;
		t.tx = transform.mat02;
		t.ty = transform.mat12;
		CGContextConcatCTM (context, t);
	}

	CoreGraphicsContext (const CoreGraphicsContext&);
	CoreGraphicsContext& operator= (const CoreGraphicsContext&);
};

LowLevelGraphicsContext* CoreGraphicsImage::createLowLevelContext()
{
	return new CoreGraphicsContext (context, imageHeight);
}

#endif
/*** End of inlined file: juce_mac_CoreGraphicsContext.mm ***/


/*** Start of inlined file: juce_iphone_UIViewComponentPeer.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class UIViewComponentPeer;

END_JUCE_NAMESPACE

#define JuceUIView MakeObjCClassName(JuceUIView)

@interface JuceUIView : UIView <UITextFieldDelegate>
{
@public
	UIViewComponentPeer* owner;
	UITextField *hiddenTextField;
}

- (JuceUIView*) initWithOwner: (UIViewComponentPeer*) owner withFrame: (CGRect) frame;
- (void) dealloc;

- (void) drawRect: (CGRect) r;

- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event;
- (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event;
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event;
- (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event;

- (BOOL) becomeFirstResponder;
- (BOOL) resignFirstResponder;
- (BOOL) canBecomeFirstResponder;

- (void) asyncRepaint: (id) rect;

- (BOOL) textField: (UITextField*) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString*) string;
- (BOOL) textFieldShouldClear: (UITextField*) textField;
- (BOOL) textFieldShouldReturn: (UITextField*) textField;
@end

#define JuceUIWindow MakeObjCClassName(JuceUIWindow)

@interface JuceUIWindow : UIWindow
{
@private
	UIViewComponentPeer* owner;
	bool isZooming;
}

- (void) setOwner: (UIViewComponentPeer*) owner;
- (void) becomeKeyWindow;
@end

BEGIN_JUCE_NAMESPACE

class UIViewComponentPeer  : public ComponentPeer,
							 public FocusChangeListener
{
public:
	UIViewComponentPeer (Component* const component,
						 const int windowStyleFlags,
						 UIView* viewToAttachTo);

	~UIViewComponentPeer();

	void* getNativeHandle() const;
	void setVisible (bool shouldBeVisible);
	void setTitle (const String& title);
	void setPosition (int x, int y);
	void setSize (int w, int h);
	void setBounds (int x, int y, int w, int h, bool isNowFullScreen);

	const Rectangle<int> getBounds() const;
	const Rectangle<int> getBounds (const bool global) const;
	const Point<int> getScreenPosition() const;
	const Point<int> relativePositionToGlobal (const Point<int>& relativePosition);
	const Point<int> globalPositionToRelative (const Point<int>& screenPosition);
	void setMinimised (bool shouldBeMinimised);
	bool isMinimised() const;
	void setFullScreen (bool shouldBeFullScreen);
	bool isFullScreen() const;
	bool contains (const Point<int>& position, bool trueIfInAChildWindow) const;
	const BorderSize getFrameSize() const;
	bool setAlwaysOnTop (bool alwaysOnTop);
	void toFront (bool makeActiveWindow);
	void toBehind (ComponentPeer* other);
	void setIcon (const Image& newIcon);

	virtual void drawRect (CGRect r);

	virtual bool canBecomeKeyWindow();
	virtual bool windowShouldClose();

	virtual void redirectMovedOrResized();
	virtual CGRect constrainRect (CGRect r);

	virtual void viewFocusGain();
	virtual void viewFocusLoss();
	bool isFocused() const;
	void grabFocus();
	void textInputRequired (const Point<int>& position);

	virtual BOOL textFieldReplaceCharacters (const Range<int>& range, const String& text);
	virtual BOOL textFieldShouldClear();
	virtual BOOL textFieldShouldReturn();
	void updateHiddenTextContent (TextInputTarget* target);
	void globalFocusChanged (Component*);

	void handleTouches (UIEvent* e, bool isDown, bool isUp, bool isCancel);

	void repaint (int x, int y, int w, int h);
	void performAnyPendingRepaintsNow();

	juce_UseDebuggingNewOperator

	UIWindow* window;
	JuceUIView* view;
	bool isSharedWindow, fullScreen, insideDrawRect;
	static ModifierKeys currentModifiers;

	static int64 getMouseTime (UIEvent* e)
	{
		return (Time::currentTimeMillis() - Time::getMillisecondCounter())
				+ (int64) ([e timestamp] * 1000.0);
	}

	Array <UITouch*> currentTouches;
};

END_JUCE_NAMESPACE

@implementation JuceUIView

- (JuceUIView*) initWithOwner: (UIViewComponentPeer*) owner_
					withFrame: (CGRect) frame
{
	[super initWithFrame: frame];
	owner = owner_;

	hiddenTextField = [[UITextField alloc] initWithFrame: CGRectMake (0, 0, 0, 0)];
	[self addSubview: hiddenTextField];
	hiddenTextField.delegate = self;

	return self;
}

- (void) dealloc
{
	[hiddenTextField removeFromSuperview];
	[hiddenTextField release];

	[super dealloc];
}

- (void) drawRect: (CGRect) r
{
	if (owner != 0)
		owner->drawRect (r);
}

bool KeyPress::isKeyCurrentlyDown (const int keyCode)
{
	return false;
}

ModifierKeys UIViewComponentPeer::currentModifiers;

const ModifierKeys ModifierKeys::getCurrentModifiersRealtime() throw()
{
	return UIViewComponentPeer::currentModifiers;
}

void ModifierKeys::updateCurrentModifiers() throw()
{
	currentModifiers = UIViewComponentPeer::currentModifiers;
}

JUCE_NAMESPACE::Point<int> juce_lastMousePos;

- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
{
	if (owner != 0)
		owner->handleTouches (event, true, false, false);
}

- (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event
{
	if (owner != 0)
		owner->handleTouches (event, false, false, false);
}

- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
{
	if (owner != 0)
		owner->handleTouches (event, false, true, false);
}

- (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event
{
	if (owner != 0)
		owner->handleTouches (event, false, true, true);

	[self touchesEnded: touches withEvent: event];
}

- (BOOL) becomeFirstResponder
{
	if (owner != 0)
		owner->viewFocusGain();

	return true;
}

- (BOOL) resignFirstResponder
{
	if (owner != 0)
		owner->viewFocusLoss();

	return true;
}

- (BOOL) canBecomeFirstResponder
{
	return owner != 0 && owner->canBecomeKeyWindow();
}

- (void) asyncRepaint: (id) rect
{
	CGRect* r = (CGRect*) [((NSData*) rect) bytes];
	[self setNeedsDisplayInRect: *r];
}

- (BOOL) textField: (UITextField*) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString*) text
{
	return owner->textFieldReplaceCharacters (Range<int> (range.location, range.location + range.length),
											  nsStringToJuce (text));
}

- (BOOL) textFieldShouldClear: (UITextField*) textField
{
	return owner->textFieldShouldClear();
}

- (BOOL) textFieldShouldReturn: (UITextField*) textField
{
	return owner->textFieldShouldReturn();
}

@end

@implementation JuceUIWindow

- (void) setOwner: (UIViewComponentPeer*) owner_
{
	owner = owner_;
	isZooming = false;
}

- (void) becomeKeyWindow
{
	[super becomeKeyWindow];

	if (owner != 0)
		owner->grabFocus();
}

@end

BEGIN_JUCE_NAMESPACE

UIViewComponentPeer::UIViewComponentPeer (Component* const component,
										  const int windowStyleFlags,
										  UIView* viewToAttachTo)
	: ComponentPeer (component, windowStyleFlags),
	  window (0),
	  view (0),
	  isSharedWindow (viewToAttachTo != 0),
	  fullScreen (false),
	  insideDrawRect (false)
{
	CGRect r = CGRectMake (0, 0, (float) component->getWidth(), (float) component->getHeight());

	view = [[JuceUIView alloc] initWithOwner: this withFrame: r];

	if (isSharedWindow)
	{
		window = [viewToAttachTo window];
		[viewToAttachTo addSubview: view];

		setVisible (component->isVisible());
	}
	else
	{
		r.origin.x = (float) component->getX();
		r.origin.y = (float) component->getY();
		r.origin.y = [[UIScreen mainScreen] bounds].size.height - (r.origin.y + r.size.height);

		window = [[JuceUIWindow alloc] init];
		window.frame = r;

		window.opaque = component->isOpaque();
		view.opaque = component->isOpaque();
		window.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
		view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];

		[((JuceUIWindow*) window) setOwner: this];

		if (component->isAlwaysOnTop())
			window.windowLevel = UIWindowLevelAlert;

		[window addSubview: view];
		view.frame = CGRectMake (0, 0, r.size.width, r.size.height);

		view.hidden = ! component->isVisible();
		window.hidden = ! component->isVisible();

		view.multipleTouchEnabled = YES;
	}

	setTitle (component->getName());

	Desktop::getInstance().addFocusChangeListener (this);
}

UIViewComponentPeer::~UIViewComponentPeer()
{
	Desktop::getInstance().removeFocusChangeListener (this);

	view->owner = 0;
	[view removeFromSuperview];
	[view release];

	if (! isSharedWindow)
	{
		[((JuceUIWindow*) window) setOwner: 0];
		[window release];
	}
}

void* UIViewComponentPeer::getNativeHandle() const
{
	return view;
}

void UIViewComponentPeer::setVisible (bool shouldBeVisible)
{
	view.hidden = ! shouldBeVisible;

	if (! isSharedWindow)
		window.hidden = ! shouldBeVisible;
}

void UIViewComponentPeer::setTitle (const String& title)
{
	// xxx is this possible?
}

void UIViewComponentPeer::setPosition (int x, int y)
{
	setBounds (x, y, component->getWidth(), component->getHeight(), false);
}

void UIViewComponentPeer::setSize (int w, int h)
{
	setBounds (component->getX(), component->getY(), w, h, false);
}

void UIViewComponentPeer::setBounds (int x, int y, int w, int h, const bool isNowFullScreen)
{
	fullScreen = isNowFullScreen;
	w = jmax (0, w);
	h = jmax (0, h);

	CGRect r;
	r.origin.x = (float) x;
	r.origin.y = (float) y;
	r.size.width = (float) w;
	r.size.height = (float) h;

	if (isSharedWindow)
	{
		//r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);

		if ([view frame].size.width != r.size.width
			 || [view frame].size.height != r.size.height)
			[view setNeedsDisplay];

		view.frame = r;
	}
	else
	{
		window.frame = r;
		view.frame = CGRectMake (0, 0, r.size.width, r.size.height);
	}
}

const Rectangle<int> UIViewComponentPeer::getBounds (const bool global) const
{
	CGRect r = [view frame];

	if (global && [view window] != 0)
	{
		r = [view convertRect: r toView: nil];
		CGRect wr = [[view window] frame];
		r.origin.x += wr.origin.x;
		r.origin.y += wr.origin.y;
	}

	return Rectangle<int> ((int) r.origin.x, (int) r.origin.y,
						   (int) r.size.width, (int) r.size.height);
}

const Rectangle<int> UIViewComponentPeer::getBounds() const
{
	return getBounds (! isSharedWindow);
}

const Point<int> UIViewComponentPeer::getScreenPosition() const
{
	return getBounds (true).getPosition();
}

const Point<int> UIViewComponentPeer::relativePositionToGlobal (const Point<int>& relativePosition)
{
	return relativePosition + getScreenPosition();
}

const Point<int> UIViewComponentPeer::globalPositionToRelative (const Point<int>& screenPosition)
{
	return screenPosition - getScreenPosition();
}

CGRect UIViewComponentPeer::constrainRect (CGRect r)
{
	if (constrainer != 0)
	{
		CGRect current = [window frame];
		current.origin.y = [[UIScreen mainScreen] bounds].size.height - current.origin.y - current.size.height;

		r.origin.y = [[UIScreen mainScreen] bounds].size.height - r.origin.y - r.size.height;

		Rectangle<int> pos ((int) r.origin.x, (int) r.origin.y,
							(int) r.size.width, (int) r.size.height);

		Rectangle<int> original ((int) current.origin.x, (int) current.origin.y,
								 (int) current.size.width, (int) current.size.height);

		constrainer->checkBounds (pos, original,
								  Desktop::getInstance().getAllMonitorDisplayAreas().getBounds(),
								  pos.getY() != original.getY() && pos.getBottom() == original.getBottom(),
								  pos.getX() != original.getX() && pos.getRight() == original.getRight(),
								  pos.getY() == original.getY() && pos.getBottom() != original.getBottom(),
								  pos.getX() == original.getX() && pos.getRight() != original.getRight());

		r.origin.x = pos.getX();
		r.origin.y = [[UIScreen mainScreen] bounds].size.height - r.size.height - pos.getY();
		r.size.width = pos.getWidth();
		r.size.height = pos.getHeight();
	}

	return r;
}

void UIViewComponentPeer::setMinimised (bool shouldBeMinimised)
{
	// xxx
}

bool UIViewComponentPeer::isMinimised() const
{
	return false;
}

void UIViewComponentPeer::setFullScreen (bool shouldBeFullScreen)
{
	if (! isSharedWindow)
	{
		Rectangle<int> r (lastNonFullscreenBounds);

		setMinimised (false);

		if (fullScreen != shouldBeFullScreen)
		{
			if (shouldBeFullScreen)
				r = Desktop::getInstance().getMainMonitorArea();

			// (can't call the component's setBounds method because that'll reset our fullscreen flag)
			if (r != getComponent()->getBounds() && ! r.isEmpty())
				setBounds (r.getX(), r.getY(), r.getWidth(), r.getHeight(), shouldBeFullScreen);
		}
	}
}

bool UIViewComponentPeer::isFullScreen() const
{
	return fullScreen;
}

bool UIViewComponentPeer::contains (const Point<int>& position, bool trueIfInAChildWindow) const
{
	if (((unsigned int) position.getX()) >= (unsigned int) component->getWidth()
		|| ((unsigned int) position.getY()) >= (unsigned int) component->getHeight())
		return false;

	CGPoint p;
	p.x = (float) position.getX();
	p.y = (float) position.getY();

	UIView* v = [view hitTest: p withEvent: nil];

	if (trueIfInAChildWindow)
		return v != nil;

	return v == view;
}

const BorderSize UIViewComponentPeer::getFrameSize() const
{
	BorderSize b;

	if (! isSharedWindow)
	{
		CGRect v = [view convertRect: [view frame] toView: nil];
		CGRect w = [window frame];

		b.setTop ((int) (w.size.height - (v.origin.y + v.size.height)));
		b.setBottom ((int) v.origin.y);
		b.setLeft ((int) v.origin.x);
		b.setRight ((int) (w.size.width - (v.origin.x + v.size.width)));
	}

	return b;
}

bool UIViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
{
	if (! isSharedWindow)
		window.windowLevel = alwaysOnTop ? UIWindowLevelAlert : UIWindowLevelNormal;

	return true;
}

void UIViewComponentPeer::toFront (bool makeActiveWindow)
{
	if (isSharedWindow)
		[[view superview] bringSubviewToFront: view];

	if (window != 0 && component->isVisible())
		[window makeKeyAndVisible];
}

void UIViewComponentPeer::toBehind (ComponentPeer* other)
{
	UIViewComponentPeer* o = (UIViewComponentPeer*) other;

	if (isSharedWindow)
	{
		[[view superview] insertSubview: view belowSubview: o->view];
	}
	else
	{
		jassertfalse // don't know how to do this
	}
}

void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
{
	// to do..
}

void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
{
	NSArray* touches = [[event touchesForView: view] allObjects];

	for (unsigned int i = 0; i < [touches count]; ++i)
	{
		UITouch* touch = [touches objectAtIndex: i];

		CGPoint p = [touch locationInView: view];
		const Point<int> pos ((int) p.x, (int) p.y);
		juce_lastMousePos = pos + getScreenPosition();

		const int64 time = getMouseTime (event);

		int touchIndex = currentTouches.indexOf (touch);

		if (touchIndex < 0)
		{
			touchIndex = currentTouches.size();
			currentTouches.add (touch);
		}

		if (isDown)
		{
			currentModifiers = currentModifiers.withoutMouseButtons();
			handleMouseEvent (touchIndex, pos, currentModifiers, time);
			currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
		}
		else if (isUp)
		{
			currentModifiers = currentModifiers.withoutMouseButtons();
			currentTouches.remove (touchIndex);
		}

		if (isCancel)
			currentTouches.clear();

		handleMouseEvent (touchIndex, pos, currentModifiers, time);
	}
}

static UIViewComponentPeer* currentlyFocusedPeer = 0;

void UIViewComponentPeer::viewFocusGain()
{
	if (currentlyFocusedPeer != this)
	{
		if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
			currentlyFocusedPeer->handleFocusLoss();

		currentlyFocusedPeer = this;

		handleFocusGain();
	}
}

void UIViewComponentPeer::viewFocusLoss()
{
	if (currentlyFocusedPeer == this)
	{
		currentlyFocusedPeer = 0;
		handleFocusLoss();
	}
}

void juce_HandleProcessFocusChange()
{
	if (UIViewComponentPeer::isValidPeer (currentlyFocusedPeer))
	{
		if (Process::isForegroundProcess())
		{
			currentlyFocusedPeer->handleFocusGain();

			ComponentPeer::bringModalComponentToFront();
		}
		else
		{
			currentlyFocusedPeer->handleFocusLoss();

			// turn kiosk mode off if we lose focus..
			Desktop::getInstance().setKioskModeComponent (0);
		}
	}
}

bool UIViewComponentPeer::isFocused() const
{
	return isSharedWindow ? this == currentlyFocusedPeer
						  : (window != 0 && [window isKeyWindow]);
}

void UIViewComponentPeer::grabFocus()
{
	if (window != 0)
	{
		[window makeKeyWindow];
		viewFocusGain();
	}
}

void UIViewComponentPeer::textInputRequired (const Point<int>&)
{
}

void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
{
	view->hiddenTextField.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
}

BOOL UIViewComponentPeer::textFieldReplaceCharacters (const Range<int>& range, const String& text)
{
	TextInputTarget* const target = findCurrentTextInputTarget();

	if (target != 0)
	{
		const Range<int> currentSelection (target->getHighlightedRegion());

		if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
			if (currentSelection.isEmpty())
				target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));

		target->insertTextAtCaret (text);
		updateHiddenTextContent (target);
	}

	return NO;
}

BOOL UIViewComponentPeer::textFieldShouldClear()
{
	TextInputTarget* const target = findCurrentTextInputTarget();

	if (target != 0)
	{
		target->setHighlightedRegion (Range<int> (0, std::numeric_limits<int>::max()));
		target->insertTextAtCaret (String::empty);
		updateHiddenTextContent (target);
	}

	return YES;
}

BOOL UIViewComponentPeer::textFieldShouldReturn()
{
	TextInputTarget* const target = findCurrentTextInputTarget();

	if (target != 0)
	{
		target->insertTextAtCaret ("\n");
		updateHiddenTextContent (target);
	}

	return YES;
}

void UIViewComponentPeer::globalFocusChanged (Component*)
{
	TextInputTarget* const target = findCurrentTextInputTarget();

	if (target != 0)
	{
		Component* comp = dynamic_cast<Component*> (target);

		Point<int> pos (comp->relativePositionToOtherComponent (component, Point<int>()));
		view->hiddenTextField.frame = CGRectMake (pos.getX(), pos.getY(), 0, 0);

		updateHiddenTextContent (target);
		[view->hiddenTextField becomeFirstResponder];
	}
	else
	{
		[view->hiddenTextField resignFirstResponder];
	}
}

void UIViewComponentPeer::drawRect (CGRect r)
{
	if (r.size.width < 1.0f || r.size.height < 1.0f)
		return;

	CGContextRef cg = UIGraphicsGetCurrentContext();

	if (! component->isOpaque())
		CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));

	CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, view.bounds.size.height));
	CoreGraphicsContext g (cg, view.bounds.size.height);

	insideDrawRect = true;
	handlePaint (g);
	insideDrawRect = false;
}

bool UIViewComponentPeer::canBecomeKeyWindow()
{
	return (getStyleFlags() & JUCE_NAMESPACE::ComponentPeer::windowIgnoresKeyPresses) == 0;
}

bool UIViewComponentPeer::windowShouldClose()
{
	if (! isValidPeer (this))
		return YES;

	handleUserClosingWindow();
	return NO;
}

void UIViewComponentPeer::redirectMovedOrResized()
{
	handleMovedOrResized();
}

void juce_setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool allowMenusAndBars)
{
}

class AsyncRepaintMessage  : public CallbackMessage
{
public:
	UIViewComponentPeer* const peer;
	const Rectangle<int> rect;

	AsyncRepaintMessage (UIViewComponentPeer* const peer_, const Rectangle<int>& rect_)
		: peer (peer_), rect (rect_)
	{
	}

	void messageCallback()
	{
		if (ComponentPeer::isValidPeer (peer))
			peer->repaint (rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
	}
};

void UIViewComponentPeer::repaint (int x, int y, int w, int h)
{
	if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
	{
		(new AsyncRepaintMessage (this, Rectangle<int> (x, y, w, h)))->post();
	}
	else
	{
		[view setNeedsDisplayInRect: CGRectMake ((float) x, (float) y, (float) w, (float) h)];
	}
}

void UIViewComponentPeer::performAnyPendingRepaintsNow()
{
}

ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
{
	return new UIViewComponentPeer (this, styleFlags, (UIView*) windowToAttachTo);
}

Image* juce_createIconForFile (const File& file)
{
	return 0;
}

void Desktop::createMouseInputSources()
{
	for (int i = 0; i < 10; ++i)
		mouseSources.add (new MouseInputSource (i, false));
}

bool Desktop::canUseSemiTransparentWindows() throw()
{
	return true;
}

const Point<int> Desktop::getMousePosition()
{
	return juce_lastMousePos;
}

void Desktop::setMousePosition (const Point<int>&)
{
}

const int KeyPress::spaceKey	= ' ';
const int KeyPress::returnKey	   = 0x0d;
const int KeyPress::escapeKey	   = 0x1b;
const int KeyPress::backspaceKey	= 0x7f;
const int KeyPress::leftKey	 = 0x1000;
const int KeyPress::rightKey	= 0x1001;
const int KeyPress::upKey	   = 0x1002;
const int KeyPress::downKey	 = 0x1003;
const int KeyPress::pageUpKey	   = 0x1004;
const int KeyPress::pageDownKey	 = 0x1005;
const int KeyPress::endKey	  = 0x1006;
const int KeyPress::homeKey	 = 0x1007;
const int KeyPress::deleteKey	   = 0x1008;
const int KeyPress::insertKey	   = -1;
const int KeyPress::tabKey	  = 9;
const int KeyPress::F1Key	   = 0x2001;
const int KeyPress::F2Key	   = 0x2002;
const int KeyPress::F3Key	   = 0x2003;
const int KeyPress::F4Key	   = 0x2004;
const int KeyPress::F5Key	   = 0x2005;
const int KeyPress::F6Key	   = 0x2006;
const int KeyPress::F7Key	   = 0x2007;
const int KeyPress::F8Key	   = 0x2008;
const int KeyPress::F9Key	   = 0x2009;
const int KeyPress::F10Key	  = 0x200a;
const int KeyPress::F11Key	  = 0x200b;
const int KeyPress::F12Key	  = 0x200c;
const int KeyPress::F13Key	  = 0x200d;
const int KeyPress::F14Key	  = 0x200e;
const int KeyPress::F15Key	  = 0x200f;
const int KeyPress::F16Key	  = 0x2010;
const int KeyPress::numberPad0	  = 0x30020;
const int KeyPress::numberPad1	  = 0x30021;
const int KeyPress::numberPad2	  = 0x30022;
const int KeyPress::numberPad3	  = 0x30023;
const int KeyPress::numberPad4	  = 0x30024;
const int KeyPress::numberPad5	  = 0x30025;
const int KeyPress::numberPad6	  = 0x30026;
const int KeyPress::numberPad7	  = 0x30027;
const int KeyPress::numberPad8	  = 0x30028;
const int KeyPress::numberPad9	  = 0x30029;
const int KeyPress::numberPadAdd		= 0x3002a;
const int KeyPress::numberPadSubtract	   = 0x3002b;
const int KeyPress::numberPadMultiply	   = 0x3002c;
const int KeyPress::numberPadDivide	 = 0x3002d;
const int KeyPress::numberPadSeparator	  = 0x3002e;
const int KeyPress::numberPadDecimalPoint   = 0x3002f;
const int KeyPress::numberPadEquals	 = 0x30030;
const int KeyPress::numberPadDelete	 = 0x30031;
const int KeyPress::playKey	 = 0x30000;
const int KeyPress::stopKey	 = 0x30001;
const int KeyPress::fastForwardKey  = 0x30002;
const int KeyPress::rewindKey	   = 0x30003;

#endif
/*** End of inlined file: juce_iphone_UIViewComponentPeer.mm ***/


/*** Start of inlined file: juce_iphone_MessageManager.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

struct CallbackMessagePayload
{
	MessageCallbackFunction* function;
	void* parameter;
	void* volatile result;
	bool volatile hasBeenExecuted;
};

END_JUCE_NAMESPACE
using namespace JUCE_NAMESPACE;

@interface JuceAppDelegate   : NSObject <UIApplicationDelegate>
{
}

- (JuceAppDelegate*) init;
- (void) dealloc;
- (BOOL) application: (UIApplication*) application handleOpenURL: (NSURL*) url;
- (void) applicationDidBecomeActive: (NSNotification*) aNotification;
- (void) applicationDidResignActive: (NSNotification*) aNotification;
- (void) applicationWillUnhide: (NSNotification*) aNotification;
- (void) customEvent: (id) data;
- (void) performCallback: (id) info;
@end

@implementation JuceAppDelegate

- (JuceAppDelegate*) init
{
	[super init];

	[[UIApplication sharedApplication] setDelegate: self];

	return self;
}

- (void) dealloc
{
	[[UIApplication sharedApplication] setDelegate: nil];
	[super dealloc];
}

- (BOOL) application: (UIApplication*) application handleOpenURL: (NSURL*) url
{
	if (JUCEApplication::getInstance() != 0)
	{
		JUCEApplication::getInstance()->anotherInstanceStarted (nsStringToJuce ([url absoluteString]));
		return YES;
	}

	return NO;
}

- (void) applicationDidBecomeActive: (NSNotification*) aNotification
{
	juce_HandleProcessFocusChange();
}

- (void) applicationDidResignActive: (NSNotification*) aNotification
{
	juce_HandleProcessFocusChange();
}

- (void) applicationWillUnhide: (NSNotification*) aNotification
{
	juce_HandleProcessFocusChange();
}

- (void) customEvent: (id) n
{
	NSData* data = (NSData*) n;
	void* message = 0;
	[data getBytes: &message length: sizeof (message)];
	[data release];

	if (message != 0)
		MessageManager::getInstance()->deliverMessage (message);
}

- (void) performCallback: (id) info
{
	if ([info isKindOfClass: [NSData class]])
	{
		CallbackMessagePayload* pl = (CallbackMessagePayload*) [((NSData*) info) bytes];

		if (pl != 0)
		{
			pl->result = (*pl->function) (pl->parameter);
			pl->hasBeenExecuted = true;
		}
	}
	else
	{
		jassertfalse // should never get here!
	}
}

@end

BEGIN_JUCE_NAMESPACE

static JuceAppDelegate* juceAppDelegate = 0;

void MessageManager::runDispatchLoop()
{
	jassert (isThisTheMessageThread()); // must only be called by the message thread
	runDispatchLoopUntil (-1);
}

void MessageManager::stopDispatchLoop()
{
	exit (0); // iPhone apps get no mercy..
}

bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
{
	const ScopedAutoReleasePool pool;
	jassert (isThisTheMessageThread()); // must only be called by the message thread

	uint32 endTime = Time::getMillisecondCounter() + millisecondsToRunFor;
	NSDate* endDate = [NSDate dateWithTimeIntervalSinceNow: millisecondsToRunFor * 0.001];

	while (! quitMessagePosted)
	{
		const ScopedAutoReleasePool pool;

		[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
								 beforeDate: endDate];

		if (millisecondsToRunFor >= 0 && Time::getMillisecondCounter() >= endTime)
			break;
	}

	return ! quitMessagePosted;
}

static CFRunLoopRef runLoop = 0;
static CFRunLoopSourceRef runLoopSource = 0;
static Array <void*, CriticalSection>* pendingMessages = 0;

static void runLoopSourceCallback (void*)
{
	if (pendingMessages != 0)
	{
		int numDispatched = 0;

		do
		{
			void* const nextMessage = pendingMessages->remove (0);

			if (nextMessage == 0)
				return;

			const ScopedAutoReleasePool pool;
			MessageManager::getInstance()->deliverMessage (nextMessage);

		} while (++numDispatched <= 4);

		CFRunLoopSourceSignal (runLoopSource);
		CFRunLoopWakeUp (runLoop);
	}
}

void MessageManager::doPlatformSpecificInitialisation()
{
	pendingMessages = new Array <void*, CriticalSection>();

	runLoop = CFRunLoopGetCurrent();
	CFRunLoopSourceContext sourceContext;
	zerostruct (sourceContext);
	sourceContext.perform = runLoopSourceCallback;
	runLoopSource = CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext);
	CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopCommonModes);

	if (juceAppDelegate == 0)
		juceAppDelegate = [[JuceAppDelegate alloc] init];
}

void MessageManager::doPlatformSpecificShutdown()
{
	CFRunLoopSourceInvalidate (runLoopSource);
	CFRelease (runLoopSource);
	runLoopSource = 0;

	if (pendingMessages != 0)
	{
		while (pendingMessages->size() > 0)
			delete ((Message*) pendingMessages->remove(0));

		deleteAndZero (pendingMessages);
	}

	if (juceAppDelegate != 0)
	{
		[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: juceAppDelegate];
		[juceAppDelegate release];
		juceAppDelegate = 0;
	}
}

bool juce_postMessageToSystemQueue (void* message)
{
	if (pendingMessages != 0)
	{
		pendingMessages->add (message);
		CFRunLoopSourceSignal (runLoopSource);
		CFRunLoopWakeUp (runLoop);
	}

	return true;
}

void MessageManager::broadcastMessage (const String& value) throw()
{
}

void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
												   void* data)
{
	if (isThisTheMessageThread())
	{
		return (*callback) (data);
	}
	else
	{
		// If a thread has a MessageManagerLock and then tries to call this method, it'll
		// deadlock because the message manager is blocked from running, so can never
		// call your function..
		jassert (! MessageManager::getInstance()->currentThreadHasLockedMessageManager());

		const ScopedAutoReleasePool pool;

		CallbackMessagePayload cmp;
		cmp.function = callback;
		cmp.parameter = data;
		cmp.result = 0;
		cmp.hasBeenExecuted = false;

		[juceAppDelegate performSelectorOnMainThread: @selector (performCallback:)
										  withObject: [NSData dataWithBytesNoCopy: &cmp
																		   length: sizeof (cmp)
																	 freeWhenDone: NO]
									   waitUntilDone: YES];

		return cmp.result;
	}
}

#endif
/*** End of inlined file: juce_iphone_MessageManager.mm ***/


/*** Start of inlined file: juce_mac_FileChooser.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if JUCE_MAC

END_JUCE_NAMESPACE
using namespace JUCE_NAMESPACE;

#define JuceFileChooserDelegate MakeObjCClassName(JuceFileChooserDelegate)

#if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
@interface JuceFileChooserDelegate   : NSObject <NSOpenSavePanelDelegate>
#else
@interface JuceFileChooserDelegate   : NSObject
#endif
{
	StringArray* filters;
}

- (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_;
- (void) dealloc;
- (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename;

@end

@implementation JuceFileChooserDelegate
- (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_
{
	[super init];
	filters = filters_;
	return self;
}

- (void) dealloc
{
	delete filters;
	[super dealloc];
}

- (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename
{
	const File f (nsStringToJuce (filename));

	for (int i = filters->size(); --i >= 0;)
		if (f.getFileName().matchesWildcard ((*filters)[i], true))
			return true;

	return f.isDirectory();
}
@end

BEGIN_JUCE_NAMESPACE

void FileChooser::showPlatformDialog (Array<File>& results,
									  const String& title,
									  const File& currentFileOrDirectory,
									  const String& filter,
									  bool selectsDirectory,
									  bool selectsFiles,
									  bool isSaveDialogue,
									  bool warnAboutOverwritingExistingFiles,
									  bool selectMultipleFiles,
									  FilePreviewComponent* extraInfoComponent)
{
	const ScopedAutoReleasePool pool;

	StringArray* filters = new StringArray();
	filters->addTokens (filter.replaceCharacters (",:", ";;"), ";", String::empty);
	filters->trim();
	filters->removeEmptyStrings();

	JuceFileChooserDelegate* delegate = [[JuceFileChooserDelegate alloc] initWithFilters: filters];
	[delegate autorelease];

	NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
										: [NSOpenPanel openPanel];

	[panel setTitle: juceStringToNS (title)];

	if (! isSaveDialogue)
	{
		NSOpenPanel* openPanel = (NSOpenPanel*) panel;
		[openPanel setCanChooseDirectories: selectsDirectory];
		[openPanel setCanChooseFiles: selectsFiles];
		[openPanel setAllowsMultipleSelection: selectMultipleFiles];
	}

	[panel setDelegate: delegate];

	if (isSaveDialogue || selectsDirectory)
		[panel setCanCreateDirectories: YES];

	String directory, filename;

	if (currentFileOrDirectory.isDirectory())
	{
		directory = currentFileOrDirectory.getFullPathName();
	}
	else
	{
		directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
		filename = currentFileOrDirectory.getFileName();
	}

	if ([panel runModalForDirectory: juceStringToNS (directory)
							   file: juceStringToNS (filename)]
		   == NSOKButton)
	{
		if (isSaveDialogue)
		{
			results.add (File (nsStringToJuce ([panel filename])));
		}
		else
		{
			NSOpenPanel* openPanel = (NSOpenPanel*) panel;
			NSArray* urls = [openPanel filenames];
			for (unsigned int i = 0; i < [urls count]; ++i)
			{
				NSString* f = [urls objectAtIndex: i];
				results.add (File (nsStringToJuce (f)));
			}
		}
	}

	[panel setDelegate: nil];
}

#else

void FileChooser::showPlatformDialog (Array<File>& results,
									  const String& title,
									  const File& currentFileOrDirectory,
									  const String& filter,
									  bool selectsDirectory,
									  bool selectsFiles,
									  bool isSaveDialogue,
									  bool warnAboutOverwritingExistingFiles,
									  bool selectMultipleFiles,
									  FilePreviewComponent* extraInfoComponent)
{
	const ScopedAutoReleasePool pool;

	jassertfalse //xxx to do
}

#endif

#endif
/*** End of inlined file: juce_mac_FileChooser.mm ***/


/*** Start of inlined file: juce_mac_OpenGLComponent.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_OPENGL

#if JUCE_MAC

END_JUCE_NAMESPACE

#define ThreadSafeNSOpenGLView MakeObjCClassName(ThreadSafeNSOpenGLView)

@interface ThreadSafeNSOpenGLView  : NSOpenGLView
{
	CriticalSection* contextLock;
	bool needsUpdate;
}

- (id) initWithFrame: (NSRect) frameRect pixelFormat: (NSOpenGLPixelFormat*) format;
- (bool) makeActive;
- (void) makeInactive;
- (void) reshape;
@end

@implementation ThreadSafeNSOpenGLView

- (id) initWithFrame: (NSRect) frameRect
		 pixelFormat: (NSOpenGLPixelFormat*) format
{
	contextLock = new CriticalSection();
	self = [super initWithFrame: frameRect pixelFormat: format];

	if (self != nil)
		[[NSNotificationCenter defaultCenter] addObserver: self
												 selector: @selector (_surfaceNeedsUpdate:)
													 name: NSViewGlobalFrameDidChangeNotification
												   object: self];
	return self;
}

- (void) dealloc
{
	[[NSNotificationCenter defaultCenter] removeObserver: self];
	delete contextLock;
	[super dealloc];
}

- (bool) makeActive
{
	const ScopedLock sl (*contextLock);

	if ([self openGLContext] == 0)
		return false;

	[[self openGLContext] makeCurrentContext];

	if (needsUpdate)
	{
		[super update];
		needsUpdate = false;
	}

	return true;
}

- (void) makeInactive
{
	const ScopedLock sl (*contextLock);
	[NSOpenGLContext clearCurrentContext];
}

- (void) _surfaceNeedsUpdate: (NSNotification*) notification
{
	const ScopedLock sl (*contextLock);
	needsUpdate = true;
}

- (void) update
{
	const ScopedLock sl (*contextLock);
	needsUpdate = true;
}

- (void) reshape
{
	const ScopedLock sl (*contextLock);
	needsUpdate = true;
}

@end
BEGIN_JUCE_NAMESPACE

class WindowedGLContext	 : public OpenGLContext
{
public:
	WindowedGLContext (Component* const component,
					   const OpenGLPixelFormat& pixelFormat_,
					   NSOpenGLContext* sharedContext)
		: renderContext (0),
		  pixelFormat (pixelFormat_)
	{
		jassert (component != 0);

		NSOpenGLPixelFormatAttribute attribs [64];
		int n = 0;
		attribs[n++] = NSOpenGLPFADoubleBuffer;
		attribs[n++] = NSOpenGLPFAAccelerated;
		attribs[n++] = NSOpenGLPFAMPSafe; // NSOpenGLPFAAccelerated, NSOpenGLPFAMultiScreen, NSOpenGLPFASingleRenderer
		attribs[n++] = NSOpenGLPFAColorSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) jmax (pixelFormat.redBits,
															pixelFormat.greenBits,
															pixelFormat.blueBits);
		attribs[n++] = NSOpenGLPFAAlphaSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.alphaBits;
		attribs[n++] = NSOpenGLPFADepthSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.depthBufferBits;
		attribs[n++] = NSOpenGLPFAStencilSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.stencilBufferBits;
		attribs[n++] = NSOpenGLPFAAccumSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) jmax (pixelFormat.accumulationBufferRedBits,
															pixelFormat.accumulationBufferGreenBits,
															pixelFormat.accumulationBufferBlueBits,
															pixelFormat.accumulationBufferAlphaBits);

		// xxx not sure how to do fullSceneAntiAliasingNumSamples..
		attribs[n++] = NSOpenGLPFASampleBuffers;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) 1;
		attribs[n++] = NSOpenGLPFAClosestPolicy;
		attribs[n++] = NSOpenGLPFANoRecovery;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) 0;

		NSOpenGLPixelFormat* format
			= [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];

		view = [[ThreadSafeNSOpenGLView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
												 pixelFormat: format];

		renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
													shareContext: sharedContext] autorelease];

		const GLint swapInterval = 1;
		[renderContext setValues: &swapInterval forParameter: NSOpenGLCPSwapInterval];

		[view setOpenGLContext: renderContext];
		[renderContext setView: view];

		[format release];

		viewHolder = new NSViewComponentInternal (view, component);
	}

	~WindowedGLContext()
	{
		makeInactive();
		[renderContext clearDrawable];
		viewHolder = 0;
	}

	bool makeActive() const throw()
	{
		jassert (renderContext != 0);
		[view makeActive];
		return isActive();
	}

	bool makeInactive() const throw()
	{
		[view makeInactive];
		return true;
	}

	bool isActive() const throw()
	{
		return [NSOpenGLContext currentContext] == renderContext;
	}

	const OpenGLPixelFormat getPixelFormat() const  { return pixelFormat; }
	void* getRawContext() const throw()		 { return renderContext; }

	void updateWindowPosition (int x, int y, int w, int h, int outerWindowHeight)
	{
	}

	void swapBuffers()
	{
		[renderContext flushBuffer];
	}

	bool setSwapInterval (const int numFramesPerSwap)
	{
		[renderContext setValues: (const GLint*) &numFramesPerSwap
					forParameter: NSOpenGLCPSwapInterval];
		return true;
	}

	int getSwapInterval() const
	{
		GLint numFrames = 0;
		[renderContext getValues: &numFrames
					forParameter: NSOpenGLCPSwapInterval];
		return numFrames;
	}

	void repaint()
	{
		// we need to invalidate the juce view that holds this gl view, to make it
		// cause a repaint callback
		NSView* v = (NSView*) viewHolder->view;
		NSRect r = [v frame];

		// bit of a bodge here.. if we only invalidate the area of the gl component,
		// it's completely covered by the NSOpenGLView, so the OS throws away the
		// repaint message, thus never causing our paint() callback, and never repainting
		// the comp. So invalidating just a little bit around the edge helps..
		[[v superview] setNeedsDisplayInRect: NSInsetRect (r, -2.0f, -2.0f)];
	}

	void* getNativeWindowHandle() const	 { return viewHolder->view; }

	juce_UseDebuggingNewOperator

	NSOpenGLContext* renderContext;
	ThreadSafeNSOpenGLView* view;

private:
	OpenGLPixelFormat pixelFormat;
	ScopedPointer <NSViewComponentInternal> viewHolder;

	WindowedGLContext (const WindowedGLContext&);
	WindowedGLContext& operator= (const WindowedGLContext&);
};

OpenGLContext* OpenGLComponent::createContext()
{
	ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
															   contextToShareListsWith != 0 ? (NSOpenGLContext*) contextToShareListsWith->getRawContext() : 0));

	return (c->renderContext != 0) ? c.release() : 0;
}

void* OpenGLComponent::getNativeWindowHandle() const
{
	return context != 0 ? static_cast<WindowedGLContext*> (static_cast<OpenGLContext*> (context))->getNativeWindowHandle()
						: 0;
}

void juce_glViewport (const int w, const int h)
{
	glViewport (0, 0, w, h);
}

void OpenGLPixelFormat::getAvailablePixelFormats (Component* /*component*/,
												  OwnedArray <OpenGLPixelFormat>& results)
{
/*	GLint attribs [64];
	int n = 0;
	attribs[n++] = AGL_RGBA;
	attribs[n++] = AGL_DOUBLEBUFFER;
	attribs[n++] = AGL_ACCELERATED;
	attribs[n++] = AGL_NO_RECOVERY;
	attribs[n++] = AGL_NONE;

	AGLPixelFormat p = aglChoosePixelFormat (0, 0, attribs);

	while (p != 0)
	{
		OpenGLPixelFormat* const pf = new OpenGLPixelFormat();
		pf->redBits = getAGLAttribute (p, AGL_RED_SIZE);
		pf->greenBits = getAGLAttribute (p, AGL_GREEN_SIZE);
		pf->blueBits = getAGLAttribute (p, AGL_BLUE_SIZE);
		pf->alphaBits = getAGLAttribute (p, AGL_ALPHA_SIZE);
		pf->depthBufferBits = getAGLAttribute (p, AGL_DEPTH_SIZE);
		pf->stencilBufferBits = getAGLAttribute (p, AGL_STENCIL_SIZE);
		pf->accumulationBufferRedBits = getAGLAttribute (p, AGL_ACCUM_RED_SIZE);
		pf->accumulationBufferGreenBits = getAGLAttribute (p, AGL_ACCUM_GREEN_SIZE);
		pf->accumulationBufferBlueBits = getAGLAttribute (p, AGL_ACCUM_BLUE_SIZE);
		pf->accumulationBufferAlphaBits = getAGLAttribute (p, AGL_ACCUM_ALPHA_SIZE);

		results.add (pf);

		p = aglNextPixelFormat (p);
	}*/

	//jassertfalse  //xxx can't see how you do this in cocoa!
}

#else

END_JUCE_NAMESPACE

@interface JuceGLView   : UIView
{
}
+ (Class) layerClass;
@end

@implementation JuceGLView
+ (Class) layerClass
{
	return [CAEAGLLayer class];
}
@end

BEGIN_JUCE_NAMESPACE

class GLESContext   : public OpenGLContext
{
public:
	GLESContext (UIViewComponentPeer* peer,
				 Component* const component_,
				 const OpenGLPixelFormat& pixelFormat_,
				 const GLESContext* const sharedContext,
				 NSUInteger apiType)
		: component (component_), pixelFormat (pixelFormat_), glLayer (0), context (0),
		  useDepthBuffer (pixelFormat_.depthBufferBits > 0), frameBufferHandle (0), colorBufferHandle (0),
		  depthBufferHandle (0), lastWidth (0), lastHeight (0)
	{
		view = [[JuceGLView alloc] initWithFrame: CGRectMake (0, 0, 64, 64)];
		view.opaque = YES;
		view.hidden = NO;
		view.backgroundColor = [UIColor blackColor];
		view.userInteractionEnabled = NO;

		glLayer = (CAEAGLLayer*) [view layer];
		[peer->view addSubview: view];

		if (sharedContext != 0)
			context = [[EAGLContext alloc] initWithAPI: apiType
											sharegroup: [sharedContext->context sharegroup]];
		else
			context = [[EAGLContext alloc] initWithAPI: apiType];

		createGLBuffers();
	}

	~GLESContext()
	{
		makeInactive();
		[context release];
		[view removeFromSuperview];
		[view release];
		freeGLBuffers();
	}

	bool makeActive() const throw()
	{
		jassert (context != 0);

		[EAGLContext setCurrentContext: context];
		glBindFramebufferOES (GL_FRAMEBUFFER_OES, frameBufferHandle);
		return true;
	}

	void swapBuffers()
	{
		glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);
		[context presentRenderbuffer: GL_RENDERBUFFER_OES];
	}

	bool makeInactive() const throw()
	{
		return [EAGLContext setCurrentContext: nil];
	}

	bool isActive() const throw()
	{
		return [EAGLContext currentContext] == context;
	}

	const OpenGLPixelFormat getPixelFormat() const  { return pixelFormat; }
	void* getRawContext() const throw()		 { return glLayer; }

	void updateWindowPosition (int x, int y, int w, int h, int outerWindowHeight)
	{
		view.frame = CGRectMake ((CGFloat) x, (CGFloat) y, (CGFloat) w, (CGFloat) h);

		if (lastWidth != w || lastHeight != h)
		{
			lastWidth = w;
			lastHeight = h;
			freeGLBuffers();
			createGLBuffers();
		}
	}

	bool setSwapInterval (const int numFramesPerSwap)
	{
		numFrames = numFramesPerSwap;
		return true;
	}

	int getSwapInterval() const
	{
		return numFrames;
	}

	void repaint()
	{
	}

	void createGLBuffers()
	{
		makeActive();

		glGenFramebuffersOES (1, &frameBufferHandle);
		glGenRenderbuffersOES (1, &colorBufferHandle);
		glGenRenderbuffersOES (1, &depthBufferHandle);

		glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);
		[context renderbufferStorage: GL_RENDERBUFFER_OES fromDrawable: glLayer];

		GLint width, height;
		glGetRenderbufferParameterivOES (GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &width);
		glGetRenderbufferParameterivOES (GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &height);

		if (useDepthBuffer)
		{
			glBindRenderbufferOES (GL_RENDERBUFFER_OES, depthBufferHandle);
			glRenderbufferStorageOES (GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, width, height);
		}

		glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);

		glBindFramebufferOES (GL_FRAMEBUFFER_OES, frameBufferHandle);
		glFramebufferRenderbufferOES (GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorBufferHandle);

		if (useDepthBuffer)
			glFramebufferRenderbufferOES (GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBufferHandle);

		jassert (glCheckFramebufferStatusOES (GL_FRAMEBUFFER_OES) == GL_FRAMEBUFFER_COMPLETE_OES);
	}

	void freeGLBuffers()
	{
		if (frameBufferHandle != 0)
		{
			glDeleteFramebuffersOES (1, &frameBufferHandle);
			frameBufferHandle = 0;
		}

		if (colorBufferHandle != 0)
		{
			glDeleteRenderbuffersOES (1, &colorBufferHandle);
			colorBufferHandle = 0;
		}

		if (depthBufferHandle != 0)
		{
			glDeleteRenderbuffersOES (1, &depthBufferHandle);
			depthBufferHandle = 0;
		}
	}

	juce_UseDebuggingNewOperator

private:
	Component::SafePointer<Component> component;
	OpenGLPixelFormat pixelFormat;
	JuceGLView* view;
	CAEAGLLayer* glLayer;
	EAGLContext* context;
	bool useDepthBuffer;
	GLuint frameBufferHandle, colorBufferHandle, depthBufferHandle;
	int numFrames;
	int lastWidth, lastHeight;

	GLESContext (const GLESContext&);
	GLESContext& operator= (const GLESContext&);
};

OpenGLContext* OpenGLComponent::createContext()
{
	ScopedAutoReleasePool pool;
	UIViewComponentPeer* peer = dynamic_cast <UIViewComponentPeer*> (getPeer());

	if (peer != 0)
		return new GLESContext (peer, this, preferredPixelFormat,
								dynamic_cast <const GLESContext*> (contextToShareListsWith),
								type == openGLES2 ? kEAGLRenderingAPIOpenGLES2 : kEAGLRenderingAPIOpenGLES1);

	return 0;
}

void OpenGLPixelFormat::getAvailablePixelFormats (Component* /*component*/,
												  OwnedArray <OpenGLPixelFormat>& /*results*/)
{
}

void juce_glViewport (const int w, const int h)
{
	glViewport (0, 0, w, h);
}

#endif

#endif
/*** End of inlined file: juce_mac_OpenGLComponent.mm ***/


/*** Start of inlined file: juce_mac_MouseCursor.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if JUCE_MAC

Image* juce_loadPNGImageFromStream (InputStream& inputStream);

void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY)
{
	NSImage* im = CoreGraphicsImage::createNSImage (image);
	NSCursor* c = [[NSCursor alloc] initWithImage: im
										  hotSpot: NSMakePoint (hotspotX, hotspotY)];
	[im release];

	return c;
}

static void* juce_cursorFromData (const MemoryBlock& data, const float hx, const float hy)
{
	MemoryInputStream stream (data, false);
	ScopedPointer <Image> im (juce_loadPNGImageFromStream (stream));
	jassert (im != 0);

	if (im == 0)
		return 0;

	return juce_createMouseCursorFromImage (*im,
											(int) (hx * im->getWidth()),
											(int) (hy * im->getHeight()));
}

static void* juce_cursorFromWebKitFile (const char* filename, float hx, float hy)
{
	const File f ("/System/Library/Frameworks/WebKit.framework/Frameworks/WebCore.framework/Resources");

	MemoryBlock mb;
	if (f.getChildFile (filename).loadFileAsData (mb))
		return juce_cursorFromData (mb, hx, hy);

	return 0;
}

void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type)
{
	const ScopedAutoReleasePool pool;
	NSCursor* c = 0;

	switch (type)
	{
	case MouseCursor::NormalCursor:
		c = [NSCursor arrowCursor];
		break;

	case MouseCursor::NoCursor:
		return juce_createMouseCursorFromImage (Image (Image::ARGB, 8, 8, true), 0, 0);

	case MouseCursor::DraggingHandCursor:
		c = [NSCursor openHandCursor];
		break;

	case MouseCursor::CopyingCursor:
		return juce_cursorFromWebKitFile ("copyCursor.png", 0, 0);

	case MouseCursor::WaitCursor:
		c = [NSCursor arrowCursor]; // avoid this on the mac, let the OS provide the beachball
		break;
		//return juce_cursorFromWebKitFile ("waitCursor.png", 0.5f, 0.5f);

	case MouseCursor::IBeamCursor:
		c = [NSCursor IBeamCursor];
		break;

	case MouseCursor::PointingHandCursor:
		c = [NSCursor pointingHandCursor];
		break;

	case MouseCursor::LeftRightResizeCursor:
		c = [NSCursor resizeLeftRightCursor];
		break;

	case MouseCursor::LeftEdgeResizeCursor:
		c = [NSCursor resizeLeftCursor];
		break;

	case MouseCursor::RightEdgeResizeCursor:
		c = [NSCursor resizeRightCursor];
		break;

	case MouseCursor::UpDownResizeCursor:
	case MouseCursor::TopEdgeResizeCursor:
	case MouseCursor::BottomEdgeResizeCursor:
		return juce_cursorFromWebKitFile ("northSouthResizeCursor.png", 0.5f, 0.5f);

	case MouseCursor::TopLeftCornerResizeCursor:
	case MouseCursor::BottomRightCornerResizeCursor:
		return juce_cursorFromWebKitFile ("northWestSouthEastResizeCursor.png", 0.5f, 0.5f);

	case MouseCursor::TopRightCornerResizeCursor:
	case MouseCursor::BottomLeftCornerResizeCursor:
		return juce_cursorFromWebKitFile ("northEastSouthWestResizeCursor.png", 0.5f, 0.5f);

	case MouseCursor::UpDownLeftRightResizeCursor:
		return juce_cursorFromWebKitFile ("moveCursor.png", 0.5f, 0.5f);

	case MouseCursor::CrosshairCursor:
		c = [NSCursor crosshairCursor];
		break;
	}

	[c retain];
	return c;
}

void juce_deleteMouseCursor (void* const cursorHandle, const bool isStandard)
{
	NSCursor* c = (NSCursor*) cursorHandle;
	[c release];
}

void MouseCursor::showInAllWindows() const
{
	showInWindow (0);
}

void MouseCursor::showInWindow (ComponentPeer*) const
{
	NSCursor* const c = (NSCursor*) getHandle();
	[c set];
}

#else

void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY)	  { return 0; }
void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type)			 { return 0; }
void juce_deleteMouseCursor (void* const cursorHandle, const bool isStandard)		   {}
void MouseCursor::showInAllWindows() const							  {}
void MouseCursor::showInWindow (ComponentPeer*) const					   {}

#endif

#endif
/*** End of inlined file: juce_mac_MouseCursor.mm ***/


/*** Start of inlined file: juce_mac_WebBrowserComponent.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_WEB_BROWSER

#if JUCE_MAC

END_JUCE_NAMESPACE

#define DownloadClickDetector MakeObjCClassName(DownloadClickDetector)

@interface DownloadClickDetector   : NSObject
{
	JUCE_NAMESPACE::WebBrowserComponent* ownerComponent;
}

- (DownloadClickDetector*) initWithWebBrowserOwner: (JUCE_NAMESPACE::WebBrowserComponent*) ownerComponent;

- (void) webView: (WebView*) webView decidePolicyForNavigationAction: (NSDictionary*) actionInformation
															 request: (NSURLRequest*) request
															   frame: (WebFrame*) frame
													decisionListener: (id<WebPolicyDecisionListener>) listener;
@end

@implementation DownloadClickDetector

- (DownloadClickDetector*) initWithWebBrowserOwner: (JUCE_NAMESPACE::WebBrowserComponent*) ownerComponent_
{
	[super init];
	ownerComponent = ownerComponent_;
	return self;
}

- (void) webView: (WebView*) sender decidePolicyForNavigationAction: (NSDictionary*) actionInformation
															request: (NSURLRequest*) request
															  frame: (WebFrame*) frame
												   decisionListener: (id <WebPolicyDecisionListener>) listener
{
	NSURL* url = [actionInformation valueForKey: @"WebActionOriginalURLKey"];

	if (ownerComponent->pageAboutToLoad (nsStringToJuce ([url absoluteString])))
		[listener use];
	else
		[listener ignore];
}

@end

BEGIN_JUCE_NAMESPACE

class WebBrowserComponentInternal  : public NSViewComponent
{
public:
	WebBrowserComponentInternal (WebBrowserComponent* owner)
	{
		webView = [[WebView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
									   frameName: @""
									   groupName: @""];
		setView (webView);

		clickListener = [[DownloadClickDetector alloc] initWithWebBrowserOwner: owner];
		[webView setPolicyDelegate: clickListener];
	}

	~WebBrowserComponentInternal()
	{
		[webView setPolicyDelegate: nil];
		[clickListener release];
		setView (0);
	}

	void goToURL (const String& url,
				  const StringArray* headers,
				  const MemoryBlock* postData)
	{
		NSMutableURLRequest* r
			= [NSMutableURLRequest requestWithURL: [NSURL URLWithString: juceStringToNS (url)]
									  cachePolicy: NSURLRequestUseProtocolCachePolicy
								  timeoutInterval: 30.0];

		if (postData != 0 && postData->getSize() > 0)
		{
			[r setHTTPMethod: @"POST"];
			[r setHTTPBody: [NSData dataWithBytes: postData->getData()
										   length: postData->getSize()]];
		}

		if (headers != 0)
		{
			for (int i = 0; i < headers->size(); ++i)
			{
				const String headerName ((*headers)[i].upToFirstOccurrenceOf (":", false, false).trim());
				const String headerValue ((*headers)[i].fromFirstOccurrenceOf (":", false, false).trim());

				[r setValue: juceStringToNS (headerValue)
				   forHTTPHeaderField: juceStringToNS (headerName)];
			}
		}

		stop();
		[[webView mainFrame] loadRequest: r];
	}

	void goBack()
	{
		[webView goBack];
	}

	void goForward()
	{
		[webView goForward];
	}

	void stop()
	{
		[webView stopLoading: nil];
	}

	void refresh()
	{
		[webView reload: nil];
	}

private:
	WebView* webView;
	DownloadClickDetector* clickListener;
};

WebBrowserComponent::WebBrowserComponent (const bool unloadPageWhenBrowserIsHidden_)
	: browser (0),
	  blankPageShown (false),
	  unloadPageWhenBrowserIsHidden (unloadPageWhenBrowserIsHidden_)
{
	setOpaque (true);

	addAndMakeVisible (browser = new WebBrowserComponentInternal (this));
}

WebBrowserComponent::~WebBrowserComponent()
{
	deleteAndZero (browser);
}

void WebBrowserComponent::goToURL (const String& url,
								   const StringArray* headers,
								   const MemoryBlock* postData)
{
	lastURL = url;

	lastHeaders.clear();
	if (headers != 0)
		lastHeaders = *headers;

	lastPostData.setSize (0);
	if (postData != 0)
		lastPostData = *postData;

	blankPageShown = false;

	browser->goToURL (url, headers, postData);
}

void WebBrowserComponent::stop()
{
	browser->stop();
}

void WebBrowserComponent::goBack()
{
	lastURL = String::empty;
	blankPageShown = false;
	browser->goBack();
}

void WebBrowserComponent::goForward()
{
	lastURL = String::empty;
	browser->goForward();
}

void WebBrowserComponent::refresh()
{
	browser->refresh();
}

void WebBrowserComponent::paint (Graphics& g)
{
}

void WebBrowserComponent::checkWindowAssociation()
{
	if (isShowing())
	{
		if (blankPageShown)
			goBack();
	}
	else
	{
		if (unloadPageWhenBrowserIsHidden && ! blankPageShown)
		{
			// when the component becomes invisible, some stuff like flash
			// carries on playing audio, so we need to force it onto a blank
			// page to avoid this, (and send it back when it's made visible again).

			blankPageShown = true;
			browser->goToURL ("about:blank", 0, 0);
		}
	}
}

void WebBrowserComponent::reloadLastURL()
{
	if (lastURL.isNotEmpty())
	{
		goToURL (lastURL, &lastHeaders, &lastPostData);
		lastURL = String::empty;
	}
}

void WebBrowserComponent::parentHierarchyChanged()
{
	checkWindowAssociation();
}

void WebBrowserComponent::resized()
{
	browser->setSize (getWidth(), getHeight());
}

void WebBrowserComponent::visibilityChanged()
{
	checkWindowAssociation();
}

bool WebBrowserComponent::pageAboutToLoad (const String& url)
{
	return true;
}

#else

WebBrowserComponent::WebBrowserComponent (const bool unloadPageWhenBrowserIsHidden_)
{
}

WebBrowserComponent::~WebBrowserComponent()
{
}

void WebBrowserComponent::goToURL (const String& url,
								   const StringArray* headers,
								   const MemoryBlock* postData)
{
}

void WebBrowserComponent::stop()
{
}

void WebBrowserComponent::goBack()
{
}

void WebBrowserComponent::goForward()
{
}

void WebBrowserComponent::refresh()
{
}

void WebBrowserComponent::paint (Graphics& g)
{
}

void WebBrowserComponent::checkWindowAssociation()
{
}

void WebBrowserComponent::reloadLastURL()
{
}

void WebBrowserComponent::parentHierarchyChanged()
{
}

void WebBrowserComponent::resized()
{
}

void WebBrowserComponent::visibilityChanged()
{
}

bool WebBrowserComponent::pageAboutToLoad (const String& url)
{
	return true;
}

#endif

#endif
/*** End of inlined file: juce_mac_WebBrowserComponent.mm ***/


/*** Start of inlined file: juce_iphone_Audio.cpp ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class IPhoneAudioIODevice  : public AudioIODevice
{
public:

	IPhoneAudioIODevice (const String& deviceName)
		: AudioIODevice (deviceName, "Audio"),
		  audioUnit (0),
		  isRunning (false),
		  callback (0),
		  actualBufferSize (0),
		  floatData (1, 2)
	{
		numInputChannels = 2;
		numOutputChannels = 2;
		preferredBufferSize = 0;

		AudioSessionInitialize (0, 0, interruptionListenerStatic, this);
		updateDeviceInfo();
	}

	~IPhoneAudioIODevice()
	{
		close();
	}

	const StringArray getOutputChannelNames()
	{
		StringArray s;
		s.add ("Left");
		s.add ("Right");
		return s;
	}

	const StringArray getInputChannelNames()
	{
		StringArray s;
		if (audioInputIsAvailable)
		{
			s.add ("Left");
			s.add ("Right");
		}
		return s;
	}

	int getNumSampleRates()
	{
		return 1;
	}

	double getSampleRate (int index)
	{
		return sampleRate;
	}

	int getNumBufferSizesAvailable()
	{
		return 1;
	}

	int getBufferSizeSamples (int index)
	{
		return getDefaultBufferSize();
	}

	int getDefaultBufferSize()
	{
		return 1024;
	}

	const String open (const BigInteger& inputChannels,
					   const BigInteger& outputChannels,
					   double sampleRate,
					   int bufferSize)
	{
		close();

		lastError = String::empty;
		preferredBufferSize = (bufferSize <= 0) ? getDefaultBufferSize() : bufferSize;

		//  xxx set up channel mapping

		activeOutputChans = outputChannels;
		activeOutputChans.setRange (2, activeOutputChans.getHighestBit(), false);
		numOutputChannels = activeOutputChans.countNumberOfSetBits();
		monoOutputChannelNumber = activeOutputChans.findNextSetBit (0);

		activeInputChans = inputChannels;
		activeInputChans.setRange (2, activeInputChans.getHighestBit(), false);
		numInputChannels = activeInputChans.countNumberOfSetBits();
		monoInputChannelNumber = activeInputChans.findNextSetBit (0);

		AudioSessionSetActive (true);

		UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
		AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (audioCategory), &audioCategory);
		AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, propertyChangedStatic, this);

		fixAudioRouteIfSetToReceiver();
		updateDeviceInfo();

		Float32 bufferDuration = preferredBufferSize / sampleRate;
		AudioSessionSetProperty (kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof (bufferDuration), &bufferDuration);
		actualBufferSize = preferredBufferSize;

		prepareFloatBuffers();

		isRunning = true;
		propertyChanged (0, 0, 0);  // creates and starts the AU

		lastError = audioUnit != 0 ? "" : "Couldn't open the device";
		return lastError;
	}

	void close()
	{
		if (isRunning)
		{
			isRunning = false;
			AudioSessionSetActive (false);

			if (audioUnit != 0)
			{
				AudioComponentInstanceDispose (audioUnit);
				audioUnit = 0;
			}
		}
	}

	bool isOpen()
	{
		return isRunning;
	}

	int getCurrentBufferSizeSamples()
	{
		return actualBufferSize;
	}

	double getCurrentSampleRate()
	{
		return sampleRate;
	}

	int getCurrentBitDepth()
	{
		return 16;
	}

	const BigInteger getActiveOutputChannels() const
	{
		return activeOutputChans;
	}

	const BigInteger getActiveInputChannels() const
	{
		return activeInputChans;
	}

	int getOutputLatencyInSamples()
	{
		return 0; //xxx
	}

	int getInputLatencyInSamples()
	{
		return 0; //xxx
	}

	void start (AudioIODeviceCallback* callback_)
	{
		if (isRunning && callback != callback_)
		{
			if (callback_ != 0)
				callback_->audioDeviceAboutToStart (this);

			const ScopedLock sl (callbackLock);
			callback = callback_;
		}
	}

	void stop()
	{
		if (isRunning)
		{
			AudioIODeviceCallback* lastCallback;

			{
				const ScopedLock sl (callbackLock);
				lastCallback = callback;
				callback = 0;
			}

			if (lastCallback != 0)
				lastCallback->audioDeviceStopped();
		}
	}

	bool isPlaying()
	{
		return isRunning && callback != 0;
	}

	const String getLastError()
	{
		return lastError;
	}

private:

	CriticalSection callbackLock;
	Float64 sampleRate;
	int numInputChannels, numOutputChannels;
	int preferredBufferSize;
	int actualBufferSize;
	bool isRunning;
	String lastError;

	AudioStreamBasicDescription format;
	AudioUnit audioUnit;
	UInt32 audioInputIsAvailable;
	AudioIODeviceCallback* callback;
	BigInteger activeOutputChans, activeInputChans;

	AudioSampleBuffer floatData;
	float* inputChannels[3];
	float* outputChannels[3];
	bool monoInputChannelNumber, monoOutputChannelNumber;

	void prepareFloatBuffers()
	{
		floatData.setSize (numInputChannels + numOutputChannels, actualBufferSize);
		zerostruct (inputChannels);
		zerostruct (outputChannels);

		for (int i = 0; i < numInputChannels; ++i)
			inputChannels[i] = floatData.getSampleData (i);

		for (int i = 0; i < numOutputChannels; ++i)
			outputChannels[i] = floatData.getSampleData (i + numInputChannels);
	}

	OSStatus process (AudioUnitRenderActionFlags* ioActionFlags, const AudioTimeStamp* inTimeStamp,
					  UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList* ioData)
	{
		OSStatus err = noErr;

		if (audioInputIsAvailable)
			err = AudioUnitRender (audioUnit, ioActionFlags, inTimeStamp, 1, inNumberFrames, ioData);

		const ScopedLock sl (callbackLock);

		if (callback != 0)
		{
			if (audioInputIsAvailable && numInputChannels > 0)
			{
				short* shortData = (short*) ioData->mBuffers[0].mData;

				if (numInputChannels >= 2)
				{
					for (UInt32 i = 0; i < inNumberFrames; ++i)
					{
						inputChannels[0][i] = *shortData++ * (1.0f / 32768.0f);
						inputChannels[1][i] = *shortData++ * (1.0f / 32768.0f);
					}
				}
				else
				{
					if (monoInputChannelNumber > 0)
						++shortData;

					for (UInt32 i = 0; i < inNumberFrames; ++i)
					{
						inputChannels[0][i] = *shortData++ * (1.0f / 32768.0f);
						++shortData;
					}
				}
			}
			else
			{
				for (int i = numInputChannels; --i >= 0;)
					zeromem (inputChannels[i], sizeof (float) * inNumberFrames);
			}

			callback->audioDeviceIOCallback ((const float**) inputChannels, numInputChannels,
											 outputChannels, numOutputChannels,
											 (int) inNumberFrames);

			short* shortData = (short*) ioData->mBuffers[0].mData;
			int n = 0;

			if (numOutputChannels >= 2)
			{
				for (UInt32 i = 0; i < inNumberFrames; ++i)
				{
					shortData [n++] = (short) (outputChannels[0][i] * 32767.0f);
					shortData [n++] = (short) (outputChannels[1][i] * 32767.0f);
				}
			}
			else if (numOutputChannels == 1)
			{
				for (UInt32 i = 0; i < inNumberFrames; ++i)
				{
					const short s = (short) (outputChannels[monoOutputChannelNumber][i] * 32767.0f);
					shortData [n++] = s;
					shortData [n++] = s;
				}
			}
			else
			{
				zeromem (ioData->mBuffers[0].mData, 2 * sizeof (short) * inNumberFrames);
			}
		}
		else
		{
			zeromem (ioData->mBuffers[0].mData, 2 * sizeof (short) * inNumberFrames);
		}

		return err;
	}

	void updateDeviceInfo()
	{
		UInt32 size = sizeof (sampleRate);
		AudioSessionGetProperty (kAudioSessionProperty_CurrentHardwareSampleRate, &size, &sampleRate);

		size = sizeof (audioInputIsAvailable);
		AudioSessionGetProperty (kAudioSessionProperty_AudioInputAvailable, &size, &audioInputIsAvailable);
	}

	void propertyChanged (AudioSessionPropertyID inID, UInt32 inDataSize, const void* inPropertyValue)
	{
		if (! isRunning)
			return;

		if (inPropertyValue != 0)
		{
			CFDictionaryRef routeChangeDictionary = (CFDictionaryRef) inPropertyValue;
			CFNumberRef routeChangeReasonRef = (CFNumberRef) CFDictionaryGetValue (routeChangeDictionary,
																				   CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

			SInt32 routeChangeReason;
			CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

			if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable)
				fixAudioRouteIfSetToReceiver();
		}

		updateDeviceInfo();
		createAudioUnit();

		AudioSessionSetActive (true);

		if (audioUnit != 0)
		{
			UInt32 formatSize = sizeof (format);
			AudioUnitGetProperty (audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &format, &formatSize);

			Float32 bufferDuration = preferredBufferSize / sampleRate;
			UInt32 bufferDurationSize = sizeof (bufferDuration);
			AudioSessionGetProperty (kAudioSessionProperty_CurrentHardwareIOBufferDuration, &bufferDurationSize, &bufferDurationSize);
			actualBufferSize = (int) (sampleRate * bufferDuration + 0.5);

			AudioOutputUnitStart (audioUnit);
		}
	}

	void interruptionListener (UInt32 inInterruption)
	{

		if (inInterruption == kAudioSessionEndInterruption)
		{
			isRunning = true;
			AudioSessionSetActive (true);
			AudioOutputUnitStart (audioUnit);
		}
	}

	static OSStatus processStatic (void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags, const AudioTimeStamp* inTimeStamp,
								   UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList* ioData)
	{
		return ((IPhoneAudioIODevice*) inRefCon)->process (ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData);
	}

	static void propertyChangedStatic (void* inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void* inPropertyValue)
	{
		((IPhoneAudioIODevice*) inClientData)->propertyChanged (inID, inDataSize, inPropertyValue);
	}

	static void interruptionListenerStatic (void* inClientData, UInt32 inInterruption)
	{
		((IPhoneAudioIODevice*) inClientData)->interruptionListener (inInterruption);
	}

	void resetFormat (const int numChannels)
	{
		memset (&format, 0, sizeof (format));
		format.mFormatID = kAudioFormatLinearPCM;
		format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
		format.mBitsPerChannel = 8 * sizeof (short);
		format.mChannelsPerFrame = 2;
		format.mFramesPerPacket = 1;
		format.mBytesPerFrame = format.mBytesPerPacket = 2 * sizeof (short);
	}

	bool createAudioUnit()
	{
		if (audioUnit != 0)
		{
			AudioComponentInstanceDispose (audioUnit);
			audioUnit = 0;
		}

		resetFormat (2);

		AudioComponentDescription desc;
		desc.componentType = kAudioUnitType_Output;
		desc.componentSubType = kAudioUnitSubType_RemoteIO;
		desc.componentManufacturer = kAudioUnitManufacturer_Apple;
		desc.componentFlags = 0;
		desc.componentFlagsMask = 0;

		AudioComponent comp = AudioComponentFindNext (0, &desc);
		AudioComponentInstanceNew (comp, &audioUnit);

		if (audioUnit == 0)
			return false;

		const UInt32 one = 1;
		AudioUnitSetProperty (audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &one, sizeof (one));

		AudioChannelLayout layout;
		layout.mChannelBitmap = 0;
		layout.mNumberChannelDescriptions = 0;
		layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
		AudioUnitSetProperty (audioUnit, kAudioUnitProperty_AudioChannelLayout, kAudioUnitScope_Input, 0, &layout, sizeof (layout));
		AudioUnitSetProperty (audioUnit, kAudioUnitProperty_AudioChannelLayout, kAudioUnitScope_Output, 0, &layout, sizeof (layout));

		AURenderCallbackStruct inputProc;
		inputProc.inputProc = processStatic;
		inputProc.inputProcRefCon = this;
		AudioUnitSetProperty (audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &inputProc, sizeof (inputProc));

		AudioUnitSetProperty (audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, sizeof (format));
		AudioUnitSetProperty (audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &format, sizeof (format));

		AudioUnitInitialize (audioUnit);
		return true;
	}

	// If the routing is set to go through the receiver (i.e. the speaker, but quiet), this re-routes it
	// to make it loud. Needed because by default when using an input + output, the output is kept quiet.
	static void fixAudioRouteIfSetToReceiver()
	{
		CFStringRef audioRoute = 0;
		UInt32 propertySize = sizeof (audioRoute);
		if (AudioSessionGetProperty (kAudioSessionProperty_AudioRoute, &propertySize, &audioRoute) == noErr)
		{
			NSString* route = (NSString*) audioRoute;

			//DBG ("audio route: " + nsStringToJuce (route));

			if ([route hasPrefix: @"Receiver"])
			{
				UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
				AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride);
			}

			CFRelease (audioRoute);
		}
	}

	IPhoneAudioIODevice (const IPhoneAudioIODevice&);
	IPhoneAudioIODevice& operator= (const IPhoneAudioIODevice&);
};

class IPhoneAudioIODeviceType  : public AudioIODeviceType
{
public:

	IPhoneAudioIODeviceType()
		: AudioIODeviceType ("iPhone Audio")
	{
	}

	~IPhoneAudioIODeviceType()
	{
	}

	void scanForDevices()
	{
	}

	const StringArray getDeviceNames (bool wantInputNames) const
	{
		StringArray s;
		s.add ("iPhone Audio");
		return s;
	}

	int getDefaultDeviceIndex (bool forInput) const
	{
		return 0;
	}

	int getIndexOfDevice (AudioIODevice* device, bool asInput) const
	{
		return device != 0 ? 0 : -1;
	}

	bool hasSeparateInputsAndOutputs() const	{ return false; }

	AudioIODevice* createDevice (const String& outputDeviceName,
								 const String& inputDeviceName)
	{
		if (outputDeviceName.isNotEmpty() || inputDeviceName.isNotEmpty())
		{
			return new IPhoneAudioIODevice (outputDeviceName.isNotEmpty() ? outputDeviceName
																		  : inputDeviceName);
		}

		return 0;
	}

	juce_UseDebuggingNewOperator

private:
	IPhoneAudioIODeviceType (const IPhoneAudioIODeviceType&);
	IPhoneAudioIODeviceType& operator= (const IPhoneAudioIODeviceType&);
};

AudioIODeviceType* juce_createAudioIODeviceType_iPhoneAudio()
{
	return new IPhoneAudioIODeviceType();
}

#endif
/*** End of inlined file: juce_iphone_Audio.cpp ***/


/*** Start of inlined file: juce_mac_CoreMidi.cpp ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if JUCE_MAC

#undef log
#define log(a) Logger::writeToLog(a)

static bool logAnyErrorsMidi (const OSStatus err, const int lineNum)
{
	if (err == noErr)
		return true;

	log ("CoreMidi error: " + String (lineNum) + " - " + String::toHexString ((int) err));
	jassertfalse
	return false;
}

#undef OK
#define OK(a) logAnyErrorsMidi(a, __LINE__)

static const String getEndpointName (MIDIEndpointRef endpoint, bool isExternal)
{
	String result;
	CFStringRef str = 0;

	MIDIObjectGetStringProperty (endpoint, kMIDIPropertyName, &str);

	if (str != 0)
	{
		result = PlatformUtilities::cfStringToJuceString (str);
		CFRelease (str);
		str = 0;
	}

	MIDIEntityRef entity = 0;
	MIDIEndpointGetEntity (endpoint, &entity);

	if (entity == 0)
		return result; // probably virtual

	if (result.isEmpty())
	{
		// endpoint name has zero length - try the entity
		MIDIObjectGetStringProperty (entity, kMIDIPropertyName, &str);

		if (str != 0)
		{
			result += PlatformUtilities::cfStringToJuceString (str);
			CFRelease (str);
			str = 0;
		}
	}

	// now consider the device's name
	MIDIDeviceRef device = 0;
	MIDIEntityGetDevice (entity, &device);
	if (device == 0)
		return result;

	MIDIObjectGetStringProperty (device, kMIDIPropertyName, &str);

	if (str != 0)
	{
		const String s (PlatformUtilities::cfStringToJuceString (str));
		CFRelease (str);

		// if an external device has only one entity, throw away
		// the endpoint name and just use the device name
		if (isExternal && MIDIDeviceGetNumberOfEntities (device) < 2)
		{
			result = s;
		}
		else if (! result.startsWithIgnoreCase (s))
		{
			// prepend the device name to the entity name
			result = (s + " " + result).trimEnd();
		}
	}

	return result;
}

static const String getConnectedEndpointName (MIDIEndpointRef endpoint)
{
	String result;

	// Does the endpoint have connections?
	CFDataRef connections = 0;
	int numConnections = 0;

	MIDIObjectGetDataProperty (endpoint, kMIDIPropertyConnectionUniqueID, &connections);

	if (connections != 0)
	{
		numConnections = (int) (CFDataGetLength (connections) / sizeof (MIDIUniqueID));

		if (numConnections > 0)
		{
			const SInt32* pid = reinterpret_cast <const SInt32*> (CFDataGetBytePtr (connections));

			for (int i = 0; i < numConnections; ++i, ++pid)
			{
				MIDIUniqueID uid = EndianS32_BtoN (*pid);
				MIDIObjectRef connObject;
				MIDIObjectType connObjectType;
				OSStatus err = MIDIObjectFindByUniqueID (uid, &connObject, &connObjectType);

				if (err == noErr)
				{
					String s;

					if (connObjectType == kMIDIObjectType_ExternalSource
						 || connObjectType == kMIDIObjectType_ExternalDestination)
					{
						// Connected to an external device's endpoint (10.3 and later).
						s = getEndpointName (static_cast <MIDIEndpointRef> (connObject), true);
					}
					else
					{
						// Connected to an external device (10.2) (or something else, catch-all)
						CFStringRef str = 0;
						MIDIObjectGetStringProperty (connObject, kMIDIPropertyName, &str);

						if (str != 0)
						{
							s = PlatformUtilities::cfStringToJuceString (str);
							CFRelease (str);
						}
					}

					if (s.isNotEmpty())
					{
						if (result.isNotEmpty())
							result += ", ";

						result += s;
					}
				}
			}
		}

		CFRelease (connections);
	}

	if (result.isNotEmpty())
		return result;

	// Here, either the endpoint had no connections, or we failed to obtain names for any of them.
	return getEndpointName (endpoint, false);
}

const StringArray MidiOutput::getDevices()
{
	StringArray s;

	const ItemCount num = MIDIGetNumberOfDestinations();
	for (ItemCount i = 0; i < num; ++i)
	{
		MIDIEndpointRef dest = MIDIGetDestination (i);

		if (dest != 0)
		{
			String name (getConnectedEndpointName (dest));

			if (name.isEmpty())
				name = "<error>";

			s.add (name);
		}
		else
		{
			s.add ("<error>");
		}
	}

	return s;
}

int MidiOutput::getDefaultDeviceIndex()
{
	return 0;
}

static MIDIClientRef globalMidiClient;
static bool hasGlobalClientBeenCreated = false;

static bool makeSureClientExists()
{
	if (! hasGlobalClientBeenCreated)
	{
		String name ("JUCE");

		if (JUCEApplication::getInstance() != 0)
			name = JUCEApplication::getInstance()->getApplicationName();

		CFStringRef appName = PlatformUtilities::juceStringToCFString (name);

		hasGlobalClientBeenCreated = OK (MIDIClientCreate (appName, 0, 0, &globalMidiClient));
		CFRelease (appName);
	}

	return hasGlobalClientBeenCreated;
}

class MidiPortAndEndpoint
{
public:
	MidiPortAndEndpoint (MIDIPortRef port_, MIDIEndpointRef endPoint_)
		: port (port_), endPoint (endPoint_)
	{
	}

	~MidiPortAndEndpoint()
	{
		if (port != 0)
			MIDIPortDispose (port);

		if (port == 0 && endPoint != 0) // if port == 0, it means we created the endpoint, so it's safe to delete it
			MIDIEndpointDispose (endPoint);
	}

	MIDIPortRef port;
	MIDIEndpointRef endPoint;
};

MidiOutput* MidiOutput::openDevice (int index)
{
	MidiOutput* mo = 0;

	if (((unsigned int) index) < (unsigned int) MIDIGetNumberOfDestinations())
	{
		MIDIEndpointRef endPoint = MIDIGetDestination (index);

		CFStringRef pname;
		if (OK (MIDIObjectGetStringProperty (endPoint, kMIDIPropertyName, &pname)))
		{
			log ("CoreMidi - opening out: " + PlatformUtilities::cfStringToJuceString (pname));

			if (makeSureClientExists())
			{
				MIDIPortRef port;

				if (OK (MIDIOutputPortCreate (globalMidiClient, pname, &port)))
				{
					mo = new MidiOutput();
					mo->internal = new MidiPortAndEndpoint (port, endPoint);
				}
			}

			CFRelease (pname);
		}
	}

	return mo;
}

MidiOutput* MidiOutput::createNewDevice (const String& deviceName)
{
	MidiOutput* mo = 0;

	if (makeSureClientExists())
	{
		MIDIEndpointRef endPoint;
		CFStringRef name = PlatformUtilities::juceStringToCFString (deviceName);

		if (OK (MIDISourceCreate (globalMidiClient, name, &endPoint)))
		{
			mo = new MidiOutput();
			mo->internal = new MidiPortAndEndpoint (0, endPoint);
		}

		CFRelease (name);
	}

	return mo;
}

MidiOutput::~MidiOutput()
{
	delete (MidiPortAndEndpoint*) internal;
}

void MidiOutput::reset()
{
}

bool MidiOutput::getVolume (float& leftVol, float& rightVol)
{
	return false;
}

void MidiOutput::setVolume (float leftVol, float rightVol)
{
}

void MidiOutput::sendMessageNow (const MidiMessage& message)
{
	MidiPortAndEndpoint* const mpe = (MidiPortAndEndpoint*) internal;

	if (message.isSysEx())
	{
		const int maxPacketSize = 256;
		int pos = 0, bytesLeft = message.getRawDataSize();
		const int numPackets = (bytesLeft + maxPacketSize - 1) / maxPacketSize;
		HeapBlock <MIDIPacketList> packets;
		packets.malloc (32 * numPackets + message.getRawDataSize(), 1);
		packets->numPackets = numPackets;

		MIDIPacket* p = packets->packet;

		for (int i = 0; i < numPackets; ++i)
		{
			p->timeStamp = 0;
			p->length = jmin (maxPacketSize, bytesLeft);
			memcpy (p->data, message.getRawData() + pos, p->length);
			pos += p->length;
			bytesLeft -= p->length;
			p = MIDIPacketNext (p);
		}

		if (mpe->port != 0)
			MIDISend (mpe->port, mpe->endPoint, packets);
		else
			MIDIReceived (mpe->endPoint, packets);
	}
	else
	{
		MIDIPacketList packets;
		packets.numPackets = 1;
		packets.packet[0].timeStamp = 0;
		packets.packet[0].length = message.getRawDataSize();
		*(int*) (packets.packet[0].data) = *(const int*) message.getRawData();

		if (mpe->port != 0)
			MIDISend (mpe->port, mpe->endPoint, &packets);
		else
			MIDIReceived (mpe->endPoint, &packets);
	}
}

const StringArray MidiInput::getDevices()
{
	StringArray s;

	const ItemCount num = MIDIGetNumberOfSources();
	for (ItemCount i = 0; i < num; ++i)
	{
		MIDIEndpointRef source = MIDIGetSource (i);

		if (source != 0)
		{
			String name (getConnectedEndpointName (source));

			if (name.isEmpty())
				name = "<error>";

			s.add (name);
		}
		else
		{
			s.add ("<error>");
		}
	}

	return s;
}

int MidiInput::getDefaultDeviceIndex()
{
	return 0;
}

struct MidiPortAndCallback
{
	MidiInput* input;
	MidiPortAndEndpoint* portAndEndpoint;
	MidiInputCallback* callback;
	MemoryBlock pendingData;
	int pendingBytes;
	double pendingDataTime;
	bool active;

	void processSysex (const uint8*& d, int& size, const double time)
	{
		if (*d == 0xf0)
		{
			pendingBytes = 0;
			pendingDataTime = time;
		}

		pendingData.ensureSize (pendingBytes + size, false);
		uint8* totalMessage = (uint8*) pendingData.getData();

		uint8* dest = totalMessage + pendingBytes;

		while (size > 0)
		{
			if (pendingBytes > 0 && *d >= 0x80)
			{
				if (*d >= 0xfa || *d == 0xf8)
				{
					callback->handleIncomingMidiMessage (input, MidiMessage (*d, time));
					++d;
					--size;
				}
				else
				{
					if (*d == 0xf7)
					{
						*dest++ = *d++;
						pendingBytes++;
						--size;
					}

					break;
				}
			}
			else
			{
				*dest++ = *d++;
				pendingBytes++;
				--size;
			}
		}

		if (totalMessage [pendingBytes - 1] == 0xf7)
		{
			callback->handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingBytes, pendingDataTime));
			pendingBytes = 0;
		}
		else
		{
			callback->handlePartialSysexMessage (input, totalMessage, pendingBytes, pendingDataTime);
		}
	}
};

namespace CoreMidiCallbacks
{
	static CriticalSection callbackLock;
	static VoidArray activeCallbacks;
}

static void midiInputProc (const MIDIPacketList* pktlist,
						   void* readProcRefCon,
						   void* srcConnRefCon)
{
	double time = Time::getMillisecondCounterHiRes() * 0.001;
	const double originalTime = time;

	MidiPortAndCallback* const mpc = (MidiPortAndCallback*) readProcRefCon;
	const ScopedLock sl (CoreMidiCallbacks::callbackLock);

	if (CoreMidiCallbacks::activeCallbacks.contains (mpc) && mpc->active)
	{
		const MIDIPacket* packet = &pktlist->packet[0];

		for (unsigned int i = 0; i < pktlist->numPackets; ++i)
		{
			const uint8* d = (const uint8*) (packet->data);
			int size = packet->length;

			while (size > 0)
			{
				time = originalTime;

				if (mpc->pendingBytes > 0 || d[0] == 0xf0)
				{
					mpc->processSysex (d, size, time);
				}
				else
				{
					int used = 0;
					const MidiMessage m (d, size, used, 0, time);

					if (used <= 0)
					{
						jassertfalse // malformed midi message
						break;
					}
					else
					{
						mpc->callback->handleIncomingMidiMessage (mpc->input, m);
					}

					size -= used;
					d += used;
				}
			}

			packet = MIDIPacketNext (packet);
		}
	}
}

MidiInput* MidiInput::openDevice (int index, MidiInputCallback* callback)
{
	MidiInput* mi = 0;

	if (((unsigned int) index) < (unsigned int) MIDIGetNumberOfSources())
	{
		MIDIEndpointRef endPoint = MIDIGetSource (index);

		if (endPoint != 0)
		{
			CFStringRef pname;

			if (OK (MIDIObjectGetStringProperty (endPoint, kMIDIPropertyName, &pname)))
			{
				log ("CoreMidi - opening inp: " + PlatformUtilities::cfStringToJuceString (pname));

				if (makeSureClientExists())
				{
					MIDIPortRef port;

					ScopedPointer <MidiPortAndCallback> mpc (new MidiPortAndCallback());
					mpc->active = false;

					if (OK (MIDIInputPortCreate (globalMidiClient, pname, midiInputProc, mpc, &port)))
					{
						if (OK (MIDIPortConnectSource (port, endPoint, 0)))
						{
							mpc->portAndEndpoint = new MidiPortAndEndpoint (port, endPoint);
							mpc->callback = callback;
							mpc->pendingBytes = 0;
							mpc->pendingData.ensureSize (128);

							mi = new MidiInput (getDevices() [index]);
							mpc->input = mi;
							mi->internal = mpc;

							const ScopedLock sl (CoreMidiCallbacks::callbackLock);
							CoreMidiCallbacks::activeCallbacks.add (mpc.release());
						}
						else
						{
							OK (MIDIPortDispose (port));
						}
					}
				}
			}

			CFRelease (pname);
		}
	}

	return mi;
}

MidiInput* MidiInput::createNewDevice (const String& deviceName, MidiInputCallback* callback)
{
	MidiInput* mi = 0;

	if (makeSureClientExists())
	{
		ScopedPointer <MidiPortAndCallback> mpc (new MidiPortAndCallback());
		mpc->active = false;

		MIDIEndpointRef endPoint;
		CFStringRef name = PlatformUtilities::juceStringToCFString(deviceName);
		if (OK (MIDIDestinationCreate (globalMidiClient, name, midiInputProc, mpc, &endPoint)))
		{
			mpc->portAndEndpoint = new MidiPortAndEndpoint (0, endPoint);
			mpc->callback = callback;
			mpc->pendingBytes = 0;
			mpc->pendingData.ensureSize (128);

			mi = new MidiInput (deviceName);
			mpc->input = mi;
			mi->internal = mpc;

			const ScopedLock sl (CoreMidiCallbacks::callbackLock);
			CoreMidiCallbacks::activeCallbacks.add (mpc.release());
		}

		CFRelease (name);
	}

	return mi;
}

MidiInput::MidiInput (const String& name_)
	: name (name_)
{
}

MidiInput::~MidiInput()
{
	MidiPortAndCallback* const mpc = (MidiPortAndCallback*) internal;
	mpc->active = false;

	{
		const ScopedLock sl (CoreMidiCallbacks::callbackLock);
		CoreMidiCallbacks::activeCallbacks.removeValue (mpc);
	}

	if (mpc->portAndEndpoint->port != 0)
		OK (MIDIPortDisconnectSource (mpc->portAndEndpoint->port, mpc->portAndEndpoint->endPoint));

	delete mpc->portAndEndpoint;
	delete mpc;
}

void MidiInput::start()
{
	const ScopedLock sl (CoreMidiCallbacks::callbackLock);
	((MidiPortAndCallback*) internal)->active = true;
}

void MidiInput::stop()
{
	const ScopedLock sl (CoreMidiCallbacks::callbackLock);
	((MidiPortAndCallback*) internal)->active = false;
}

#undef log

#else

MidiOutput::~MidiOutput()
{
}

void MidiOutput::reset()
{
}

bool MidiOutput::getVolume (float& leftVol, float& rightVol)
{
	return false;
}

void MidiOutput::setVolume (float leftVol, float rightVol)
{
}

void MidiOutput::sendMessageNow (const MidiMessage& message)
{
}

const StringArray MidiOutput::getDevices()
{
	return StringArray();
}

MidiOutput* MidiOutput::openDevice (int index)
{
	return 0;
}

const StringArray MidiInput::getDevices()
{
	return StringArray();
}

MidiInput* MidiInput::openDevice (int index, MidiInputCallback* callback)
{
	return 0;
}

#endif

#endif
/*** End of inlined file: juce_mac_CoreMidi.cpp ***/

 #else

/*** Start of inlined file: juce_mac_Fonts.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  #define SUPPORT_10_4_FONTS 1
  #define NEW_CGFONT_FUNCTIONS_UNAVAILABLE (CGFontCreateWithFontName == 0)

  #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
	#define SUPPORT_ONLY_10_4_FONTS 1
  #endif

  END_JUCE_NAMESPACE
  @interface NSFont (PrivateHack)
	- (NSGlyph) _defaultGlyphForChar: (unichar) theChar;
  @end
  BEGIN_JUCE_NAMESPACE
#endif

class MacTypeface  : public Typeface
{
public:

	MacTypeface (const Font& font)
		: Typeface (font.getTypefaceName())
	{
		const ScopedAutoReleasePool pool;
		renderingTransform = CGAffineTransformIdentity;

		bool needsItalicTransform = false;

#if JUCE_IPHONE
		NSString* fontName = juceStringToNS (font.getTypefaceName());

		if (font.isItalic() || font.isBold())
		{
			NSArray* familyFonts = [UIFont fontNamesForFamilyName: juceStringToNS (font.getTypefaceName())];

			for (NSString* i in familyFonts)
			{
				const String fn (nsStringToJuce (i));
				const String afterDash (fn.fromFirstOccurrenceOf ("-", false, false));

				const bool probablyBold = afterDash.containsIgnoreCase ("bold") || fn.endsWithIgnoreCase ("bold");
				const bool probablyItalic = afterDash.containsIgnoreCase ("oblique")
											 || afterDash.containsIgnoreCase ("italic")
											 || fn.endsWithIgnoreCase ("oblique")
											 || fn.endsWithIgnoreCase ("italic");

				if (probablyBold == font.isBold()
					 && probablyItalic == font.isItalic())
				{
					fontName = i;
					needsItalicTransform = false;
					break;
				}
				else if (probablyBold && (! probablyItalic) && probablyBold == font.isBold())
				{
					fontName = i;
					needsItalicTransform = true; // not ideal, so carry on in case we find a better one
				}
			}

			if (needsItalicTransform)
				renderingTransform.c = 0.15f;
		}

		fontRef = CGFontCreateWithFontName ((CFStringRef) fontName);

		const int ascender = abs (CGFontGetAscent (fontRef));
		const float totalHeight = ascender + abs (CGFontGetDescent (fontRef));
		ascent = ascender / totalHeight;
		unitsToHeightScaleFactor = 1.0f / totalHeight;
		fontHeightToCGSizeFactor = CGFontGetUnitsPerEm (fontRef) / totalHeight;
#else
		nsFont = [NSFont fontWithName: juceStringToNS (font.getTypefaceName()) size: 1024];

		if (font.isItalic())
		{
			NSFont* newFont = [[NSFontManager sharedFontManager] convertFont: nsFont
																 toHaveTrait: NSItalicFontMask];

			if (newFont == nsFont)
				needsItalicTransform = true; // couldn't find a proper italic version, so fake it with a transform..

			nsFont = newFont;
		}

		if (font.isBold())
			nsFont = [[NSFontManager sharedFontManager] convertFont: nsFont toHaveTrait: NSBoldFontMask];

		[nsFont retain];

		ascent = fabsf ((float) [nsFont ascender]);
		float totalSize = ascent + fabsf ((float) [nsFont descender]);
		ascent /= totalSize;

		pathTransform = AffineTransform::identity.scale (1.0f / totalSize, 1.0f / totalSize);

		if (needsItalicTransform)
		{
			pathTransform = pathTransform.sheared (-0.15f, 0.0f);
			renderingTransform.c = 0.15f;
		}

#if SUPPORT_ONLY_10_4_FONTS
		ATSFontRef atsFont = ATSFontFindFromName ((CFStringRef) [nsFont fontName], kATSOptionFlagsDefault);

		if (atsFont == 0)
			atsFont = ATSFontFindFromPostScriptName ((CFStringRef) [nsFont fontName], kATSOptionFlagsDefault);

		fontRef = CGFontCreateWithPlatformFont (&atsFont);

		const float totalHeight = fabsf ([nsFont ascender]) + fabsf([nsFont descender]);
		unitsToHeightScaleFactor = 1.0f / totalHeight;
		fontHeightToCGSizeFactor = 1024.0f / totalHeight;
#else
  #if SUPPORT_10_4_FONTS
		if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
		{
			ATSFontRef atsFont = ATSFontFindFromName ((CFStringRef) [nsFont fontName], kATSOptionFlagsDefault);

			if (atsFont == 0)
				atsFont = ATSFontFindFromPostScriptName ((CFStringRef) [nsFont fontName], kATSOptionFlagsDefault);

			fontRef = CGFontCreateWithPlatformFont (&atsFont);

			const float totalHeight = fabsf ([nsFont ascender]) + fabsf([nsFont descender]);
			unitsToHeightScaleFactor = 1.0f / totalHeight;
			fontHeightToCGSizeFactor = 1024.0f / totalHeight;
		}
		else
  #endif
		{
			fontRef = CGFontCreateWithFontName ((CFStringRef) [nsFont fontName]);

			const int totalHeight = abs (CGFontGetAscent (fontRef)) + abs (CGFontGetDescent (fontRef));
			unitsToHeightScaleFactor = 1.0f / totalHeight;
			fontHeightToCGSizeFactor = CGFontGetUnitsPerEm (fontRef) / (float) totalHeight;
		}
#endif

#endif
	}

	~MacTypeface()
	{
#if ! JUCE_IPHONE
		[nsFont release];
#endif
		if (fontRef != 0)
			CGFontRelease (fontRef);
	}

	float getAscent() const
	{
		return ascent;
	}

	float getDescent() const
	{
		return 1.0f - ascent;
	}

	float getStringWidth (const String& text)
	{
		if (fontRef == 0 || text.isEmpty())
			return 0;

		const int length = text.length();
		HeapBlock <CGGlyph> glyphs;
		createGlyphsForString (text, length, glyphs);

		float x = 0;

#if SUPPORT_ONLY_10_4_FONTS
		HeapBlock <NSSize> advances (length);
		[nsFont getAdvancements: advances forGlyphs: reinterpret_cast <NSGlyph*> (glyphs.getData()) count: length];

		for (int i = 0; i < length; ++i)
			x += advances[i].width;
#else
  #if SUPPORT_10_4_FONTS
		if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
		{
			HeapBlock <NSSize> advances (length);
			[nsFont getAdvancements: advances forGlyphs: reinterpret_cast<NSGlyph*> (glyphs.getData()) count: length];

			for (int i = 0; i < length; ++i)
				x += advances[i].width;
		}
		else
  #endif
		{
			HeapBlock <int> advances (length);

			if (CGFontGetGlyphAdvances (fontRef, glyphs, length, advances))
				for (int i = 0; i < length; ++i)
					x += advances[i];
		}
#endif

		return x * unitsToHeightScaleFactor;
	}

	void getGlyphPositions (const String& text, Array <int>& resultGlyphs, Array <float>& xOffsets)
	{
		xOffsets.add (0);

		if (fontRef == 0 || text.isEmpty())
			return;

		const int length = text.length();
		HeapBlock <CGGlyph> glyphs;
		createGlyphsForString (text, length, glyphs);

#if SUPPORT_ONLY_10_4_FONTS
		HeapBlock <NSSize> advances (length);
		[nsFont getAdvancements: advances forGlyphs: reinterpret_cast <NSGlyph*> (glyphs.getData()) count: length];

		int x = 0;
		for (int i = 0; i < length; ++i)
		{
			x += advances[i].width;
			xOffsets.add (x * unitsToHeightScaleFactor);
			resultGlyphs.add (reinterpret_cast <NSGlyph*> (glyphs.getData())[i]);
		}

#else
  #if SUPPORT_10_4_FONTS
		if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
		{
			HeapBlock <NSSize> advances (length);
			NSGlyph* const nsGlyphs = reinterpret_cast<NSGlyph*> (glyphs.getData());
			[nsFont getAdvancements: advances forGlyphs: nsGlyphs count: length];

			float x = 0;
			for (int i = 0; i < length; ++i)
			{
				x += advances[i].width;
				xOffsets.add (x * unitsToHeightScaleFactor);
				resultGlyphs.add (nsGlyphs[i]);
			}
		}
		else
  #endif
		{
			HeapBlock <int> advances (length);

			if (CGFontGetGlyphAdvances (fontRef, glyphs, length, advances))
			{
				int x = 0;
				for (int i = 0; i < length; ++i)
				{
					x += advances [i];
					xOffsets.add (x * unitsToHeightScaleFactor);
					resultGlyphs.add (glyphs[i]);
				}
			}
		}
#endif
	}

	bool getOutlineForGlyph (int glyphNumber, Path& path)
	{
#if JUCE_IPHONE
		return false;
#else
		if (nsFont == 0)
			return false;

		// we might need to apply a transform to the path, so it mustn't have anything else in it
		jassert (path.isEmpty());

		const ScopedAutoReleasePool pool;

		NSBezierPath* bez = [NSBezierPath bezierPath];
		[bez moveToPoint: NSMakePoint (0, 0)];
		[bez appendBezierPathWithGlyph: (NSGlyph) glyphNumber
								inFont: nsFont];

		for (int i = 0; i < [bez elementCount]; ++i)
		{
			NSPoint p[3];
			switch ([bez elementAtIndex: i associatedPoints: p])
			{
			case NSMoveToBezierPathElement:
				path.startNewSubPath ((float) p[0].x, (float) -p[0].y);
				break;
			case NSLineToBezierPathElement:
				path.lineTo ((float) p[0].x, (float) -p[0].y);
				break;
			case NSCurveToBezierPathElement:
				path.cubicTo ((float) p[0].x, (float) -p[0].y, (float) p[1].x, (float) -p[1].y, (float) p[2].x, (float) -p[2].y);
				break;
			case NSClosePathBezierPathElement:
				path.closeSubPath();
				break;
			default:
				jassertfalse
				break;
			}
		}

		path.applyTransform (pathTransform);
		return true;
#endif
	}

	juce_UseDebuggingNewOperator

	CGFontRef fontRef;
	float fontHeightToCGSizeFactor;
	CGAffineTransform renderingTransform;

private:
	float ascent, unitsToHeightScaleFactor;

#if JUCE_IPHONE

#else
	NSFont* nsFont;
	AffineTransform pathTransform;
#endif

	void createGlyphsForString (const juce_wchar* const text, const int length, HeapBlock <CGGlyph>& glyphs)
	{
#if SUPPORT_10_4_FONTS
  #if ! SUPPORT_ONLY_10_4_FONTS
		if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
  #endif
		{
			glyphs.malloc (sizeof (NSGlyph) * length, 1);
			NSGlyph* const nsGlyphs = reinterpret_cast<NSGlyph*> (glyphs.getData());

			for (int i = 0; i < length; ++i)
				nsGlyphs[i] = (NSGlyph) [nsFont _defaultGlyphForChar: text[i]];

			return;
		}
#endif

#if ! SUPPORT_ONLY_10_4_FONTS
		if (charToGlyphMapper == 0)
			charToGlyphMapper = new CharToGlyphMapper (fontRef);

		glyphs.malloc (length);

		for (int i = 0; i < length; ++i)
			glyphs[i] = (CGGlyph) charToGlyphMapper->getGlyphForCharacter (text[i]);
#endif
	}

#if ! SUPPORT_ONLY_10_4_FONTS
	// Reads a CGFontRef's character map table to convert unicode into glyph numbers
	class CharToGlyphMapper
	{
	public:
		CharToGlyphMapper (CGFontRef fontRef)
			: segCount (0), endCode (0), startCode (0), idDelta (0),
			  idRangeOffset (0), glyphIndexes (0)
		{
			CFDataRef cmapTable = CGFontCopyTableForTag (fontRef, 'cmap');

			if (cmapTable != 0)
			{
				const int numSubtables = getValue16 (cmapTable, 2);

				for (int i = 0; i < numSubtables; ++i)
				{
					if (getValue16 (cmapTable, i * 8 + 4) == 0) // check for platform ID of 0
					{
						const int offset = getValue32 (cmapTable, i * 8 + 8);

						if (getValue16 (cmapTable, offset) == 4) // check that it's format 4..
						{
							const int length = getValue16 (cmapTable, offset + 2);
							const int segCountX2 =  getValue16 (cmapTable, offset + 6);
							segCount = segCountX2 / 2;
							const int endCodeOffset = offset + 14;
							const int startCodeOffset = endCodeOffset + 2 + segCountX2;
							const int idDeltaOffset = startCodeOffset + segCountX2;
							const int idRangeOffsetOffset = idDeltaOffset + segCountX2;
							const int glyphIndexesOffset = idRangeOffsetOffset + segCountX2;

							endCode = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + endCodeOffset, segCountX2);
							startCode = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + startCodeOffset, segCountX2);
							idDelta = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + idDeltaOffset, segCountX2);
							idRangeOffset = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + idRangeOffsetOffset, segCountX2);
							glyphIndexes = CFDataCreate (kCFAllocatorDefault, CFDataGetBytePtr (cmapTable) + glyphIndexesOffset, offset + length - glyphIndexesOffset);
						}

						break;
					}
				}

				CFRelease (cmapTable);
			}
		}

		~CharToGlyphMapper()
		{
			if (endCode != 0)
			{
				CFRelease (endCode);
				CFRelease (startCode);
				CFRelease (idDelta);
				CFRelease (idRangeOffset);
				CFRelease (glyphIndexes);
			}
		}

		int getGlyphForCharacter (const juce_wchar c) const
		{
			for (int i = 0; i < segCount; ++i)
			{
				if (getValue16 (endCode, i * 2) >= c)
				{
					const int start = getValue16 (startCode, i * 2);
					if (start > c)
						break;

					const int delta = getValue16 (idDelta, i * 2);
					const int rangeOffset = getValue16 (idRangeOffset, i * 2);

					if (rangeOffset == 0)
						return delta + c;
					else
						return getValue16 (glyphIndexes, 2 * ((rangeOffset / 2) + (c - start) - (segCount - i)));
				}
			}

			// If we failed to find it "properly", this dodgy fall-back seems to do the trick for most fonts!
			return jmax (-1, c - 29);
		}

	private:
		int segCount;
		CFDataRef endCode, startCode, idDelta, idRangeOffset, glyphIndexes;

		static uint16 getValue16 (CFDataRef data, const int index)
		{
			return CFSwapInt16BigToHost (*(UInt16*) (CFDataGetBytePtr (data) + index));
		}

		static uint32 getValue32 (CFDataRef data, const int index)
		{
			return CFSwapInt32BigToHost (*(UInt32*) (CFDataGetBytePtr (data) + index));
		}
	};

	ScopedPointer <CharToGlyphMapper> charToGlyphMapper;
#endif

	MacTypeface (const MacTypeface&);
	MacTypeface& operator= (const MacTypeface&);
};

const Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font)
{
	return new MacTypeface (font);
}

const StringArray Font::findAllTypefaceNames()
{
	StringArray names;

	const ScopedAutoReleasePool pool;

#if JUCE_IPHONE
	NSArray* fonts = [UIFont familyNames];
#else
	NSArray* fonts = [[NSFontManager sharedFontManager] availableFontFamilies];
#endif

	for (unsigned int i = 0; i < [fonts count]; ++i)
		names.add (nsStringToJuce ((NSString*) [fonts objectAtIndex: i]));

	names.sort (true);
	return names;
}

void Font::getPlatformDefaultFontNames (String& defaultSans, String& defaultSerif, String& defaultFixed)
{
#if JUCE_IPHONE
	defaultSans  = "Helvetica";
	defaultSerif = "Times New Roman";
	defaultFixed = "Courier New";
#else
	defaultSans  = "Lucida Grande";
	defaultSerif = "Times New Roman";
	defaultFixed = "Monaco";
#endif
}

#endif
/*** End of inlined file: juce_mac_Fonts.mm ***/

 // (must go before juce_mac_CoreGraphicsContext.mm)

/*** Start of inlined file: juce_mac_CoreGraphicsContext.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class CoreGraphicsImage : public Image
{
public:

	CoreGraphicsImage (const PixelFormat format_,
					   const int imageWidth_,
					   const int imageHeight_,
					   const bool clearImage)
		: Image (format_, imageWidth_, imageHeight_, clearImage)
	{
		CGColorSpaceRef colourSpace = (format == Image::SingleChannel) ? CGColorSpaceCreateDeviceGray()
																	   : CGColorSpaceCreateDeviceRGB();

		context = CGBitmapContextCreate (imageData, imageWidth, imageHeight, 8, lineStride,
										 colourSpace, getCGImageFlags (*this));

		CGColorSpaceRelease (colourSpace);
	}

	~CoreGraphicsImage()
	{
		CGContextRelease (context);
	}

	LowLevelGraphicsContext* createLowLevelContext();

	static CGImageRef createImage (const Image& juceImage, const bool forAlpha, CGColorSpaceRef colourSpace)
	{
		const CoreGraphicsImage* nativeImage = dynamic_cast <const CoreGraphicsImage*> (&juceImage);

		if (nativeImage != 0 && (juceImage.getFormat() == Image::SingleChannel || ! forAlpha))
		{
			return CGBitmapContextCreateImage (nativeImage->context);
		}
		else
		{
			const Image::BitmapData srcData (juceImage, 0, 0, juceImage.getWidth(), juceImage.getHeight());

			CGDataProviderRef provider = CGDataProviderCreateWithData (0, srcData.data, srcData.lineStride * srcData.height, 0);

			CGImageRef imageRef = CGImageCreate (srcData.width, srcData.height,
												 8, srcData.pixelStride * 8, srcData.lineStride,
												 colourSpace, getCGImageFlags (juceImage), provider,
												 0, true, kCGRenderingIntentDefault);

			CGDataProviderRelease (provider);
			return imageRef;
		}
	}

#if JUCE_MAC
	static NSImage* createNSImage (const Image& image)
	{
		const ScopedAutoReleasePool pool;

		NSImage* im = [[NSImage alloc] init];
		[im setSize: NSMakeSize (image.getWidth(), image.getHeight())];
		[im lockFocus];

		CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
		CGImageRef imageRef = createImage (image, false, colourSpace);
		CGColorSpaceRelease (colourSpace);

		CGContextRef cg = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
		CGContextDrawImage (cg, CGRectMake (0, 0, image.getWidth(), image.getHeight()), imageRef);

		CGImageRelease (imageRef);
		[im unlockFocus];

		return im;
	}
#endif

	CGContextRef context;

private:
	static CGBitmapInfo getCGImageFlags (const Image& image)
	{
#if JUCE_BIG_ENDIAN
		return image.getFormat() == Image::ARGB ? (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big) : kCGBitmapByteOrderDefault;
#else
		return image.getFormat() == Image::ARGB ? (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little) : kCGBitmapByteOrderDefault;
#endif
	}
};

Image* Image::createNativeImage (const PixelFormat format, const int imageWidth, const int imageHeight, const bool clearImage)
{
#if USE_COREGRAPHICS_RENDERING
	return new CoreGraphicsImage (format == RGB ? ARGB : format, imageWidth, imageHeight, clearImage);
#else
	return new Image (format, imageWidth, imageHeight, clearImage);
#endif
}

class CoreGraphicsContext   : public LowLevelGraphicsContext
{
public:
	CoreGraphicsContext (CGContextRef context_, const float flipHeight_)
		: context (context_),
		  flipHeight (flipHeight_),
		  state (new SavedState()),
		  numGradientLookupEntries (0)
	{
		CGContextRetain (context);
		CGContextSaveGState(context);
		CGContextSetShouldSmoothFonts (context, true);
		CGContextSetShouldAntialias (context, true);
		CGContextSetBlendMode (context, kCGBlendModeNormal);
		rgbColourSpace = CGColorSpaceCreateDeviceRGB();
		greyColourSpace = CGColorSpaceCreateDeviceGray();
		gradientCallbacks.version = 0;
		gradientCallbacks.evaluate = gradientCallback;
		gradientCallbacks.releaseInfo = 0;
		setFont (Font());
	}

	~CoreGraphicsContext()
	{
		CGContextRestoreGState (context);
		CGContextRelease (context);
		CGColorSpaceRelease (rgbColourSpace);
		CGColorSpaceRelease (greyColourSpace);
	}

	bool isVectorDevice() const	 { return false; }

	void setOrigin (int x, int y)
	{
		CGContextTranslateCTM (context, x, -y);
	}

	bool clipToRectangle (const Rectangle<int>& r)
	{
		CGContextClipToRect (context, CGRectMake (r.getX(), flipHeight - r.getBottom(), r.getWidth(), r.getHeight()));
		return ! isClipEmpty();
	}

	bool clipToRectangleList (const RectangleList& clipRegion)
	{
		if (clipRegion.isEmpty())
		{
			CGContextClipToRect (context, CGRectMake (0, 0, 0, 0));
			return false;
		}
		else
		{
			const int numRects = clipRegion.getNumRectangles();

			HeapBlock <CGRect> rects (numRects);
			for (int i = 0; i < numRects; ++i)
			{
				const Rectangle<int>& r = clipRegion.getRectangle(i);
				rects[i] = CGRectMake (r.getX(), flipHeight - r.getBottom(), r.getWidth(), r.getHeight());
			}

			CGContextClipToRects (context, rects, numRects);
			return ! isClipEmpty();
		}
	}

	void excludeClipRectangle (const Rectangle<int>& r)
	{
		RectangleList remaining (getClipBounds());
		remaining.subtract (r);
		clipToRectangleList (remaining);
	}

	void clipToPath (const Path& path, const AffineTransform& transform)
	{
		createPath (path, transform);
		CGContextClip (context);
	}

	void clipToImageAlpha (const Image& sourceImage, const Rectangle<int>& srcClip, const AffineTransform& transform)
	{
		if (! transform.isSingularity())
		{
			Image* singleChannelImage = createAlphaChannelImage (sourceImage);
			CGImageRef image = CoreGraphicsImage::createImage (*singleChannelImage, true, greyColourSpace);

			flip();
			AffineTransform t (AffineTransform::scale (1.0f, -1.0f).translated (0, sourceImage.getHeight()).followedBy (transform));
			applyTransform (t);

			CGRect r = CGRectMake (0, 0, sourceImage.getWidth(), sourceImage.getHeight());
			CGContextClipToMask (context, r, image);

			applyTransform (t.inverted());
			flip();

			CGImageRelease (image);
			deleteAlphaChannelImage (sourceImage, singleChannelImage);
		}
	}

	bool clipRegionIntersects (const Rectangle<int>& r)
	{
		return getClipBounds().intersects (r);
	}

	const Rectangle<int> getClipBounds() const
	{
		CGRect bounds = CGRectIntegral (CGContextGetClipBoundingBox (context));

		return Rectangle<int> (roundToInt (bounds.origin.x),
							   roundToInt (flipHeight - (bounds.origin.y + bounds.size.height)),
							   roundToInt (bounds.size.width),
							   roundToInt (bounds.size.height));
	}

	bool isClipEmpty() const
	{
		return CGRectIsEmpty (CGContextGetClipBoundingBox (context));
	}

	void saveState()
	{
		CGContextSaveGState (context);
		stateStack.add (new SavedState (*state));
	}

	void restoreState()
	{
		CGContextRestoreGState (context);

		SavedState* const top = stateStack.getLast();

		if (top != 0)
		{
			state = top;
			stateStack.removeLast (1, false);
		}
		else
		{
			jassertfalse // trying to pop with an empty stack!
		}
	}

	void setFill (const FillType& fillType)
	{
		state->fillType = fillType;

		if (fillType.isColour())
		{
			CGContextSetRGBFillColor (context, fillType.colour.getFloatRed(), fillType.colour.getFloatGreen(),
									  fillType.colour.getFloatBlue(), fillType.colour.getFloatAlpha());
			CGContextSetAlpha (context, 1.0f);
		}
	}

	void setOpacity (float newOpacity)
	{
		state->fillType.setOpacity (newOpacity);
		setFill (state->fillType);
	}

	void setInterpolationQuality (Graphics::ResamplingQuality quality)
	{
		CGContextSetInterpolationQuality (context, quality == Graphics::lowResamplingQuality
													? kCGInterpolationLow
													: kCGInterpolationHigh);
	}

	void fillRect (const Rectangle<int>& r, const bool replaceExistingContents)
	{
		CGRect cgRect = CGRectMake (r.getX(), flipHeight - r.getBottom(), r.getWidth(), r.getHeight());

		if (replaceExistingContents)
		{
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
			CGContextClearRect (context, cgRect);
#else
  #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
			if (CGContextDrawLinearGradient == 0) // (just a way of checking whether we're running in 10.5 or later)
				CGContextClearRect (context, cgRect);
			else
  #endif
				CGContextSetBlendMode (context, kCGBlendModeCopy);
#endif

			fillRect (r, false);
			CGContextSetBlendMode (context, kCGBlendModeNormal);
		}
		else
		{
			if (state->fillType.isColour())
			{
				CGContextFillRect (context, cgRect);
			}
			else if (state->fillType.isGradient())
			{
				CGContextSaveGState (context);
				CGContextClipToRect (context, cgRect);
				drawGradient();
				CGContextRestoreGState (context);
			}
			else
			{
				CGContextSaveGState (context);
				CGContextClipToRect (context, cgRect);
				drawImage (*(state->fillType.image), state->fillType.image->getBounds(), state->fillType.transform, true);
				CGContextRestoreGState (context);
			}
		}
	}

	void fillPath (const Path& path, const AffineTransform& transform)
	{
		CGContextSaveGState (context);

		if (state->fillType.isColour())
		{
			flip();
			applyTransform (transform);
			createPath (path);

			if (path.isUsingNonZeroWinding())
				CGContextFillPath (context);
			else
				CGContextEOFillPath (context);
		}
		else
		{
			createPath (path, transform);

			if (path.isUsingNonZeroWinding())
				CGContextClip (context);
			else
				CGContextEOClip (context);

			if (state->fillType.isGradient())
				drawGradient();
			else
				drawImage (*(state->fillType.image), state->fillType.image->getBounds(), state->fillType.transform, true);
		}

		CGContextRestoreGState (context);
	}

	void drawImage (const Image& sourceImage, const Rectangle<int>& srcClip,
					const AffineTransform& transform, const bool fillEntireClipAsTiles)
	{
		jassert (sourceImage.getBounds().contains (srcClip));

		CGImageRef fullImage = CoreGraphicsImage::createImage (sourceImage, false, rgbColourSpace);
		CGImageRef image = fullImage;

		if (srcClip != sourceImage.getBounds())
		{
			image = CGImageCreateWithImageInRect (fullImage, CGRectMake (srcClip.getX(), srcClip.getY(),
																		 srcClip.getWidth(), srcClip.getHeight()));
			CGImageRelease (fullImage);
		}

		CGContextSaveGState (context);
		CGContextSetAlpha (context, state->fillType.getOpacity());

		flip();
		applyTransform (AffineTransform::scale (1.0f, -1.0f).translated (0, srcClip.getHeight()).followedBy (transform));
		CGRect imageRect = CGRectMake (0, 0, srcClip.getWidth(), srcClip.getHeight());

		if (fillEntireClipAsTiles)
		{
#if JUCE_IPHONE
			CGContextDrawTiledImage (context, imageRect, image);
#else
  #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
			// There's a bug in CGContextDrawTiledImage that makes it incredibly slow
			// if it's doing a transformation - it's quicker to just draw lots of images manually
			if (CGContextDrawTiledImage != 0 && transform.isOnlyTranslation())
				CGContextDrawTiledImage (context, imageRect, image);
			else
  #endif
			{
				// Fallback to manually doing a tiled fill on 10.4
				CGRect clip = CGRectIntegral (CGContextGetClipBoundingBox (context));
				const int iw = srcClip.getWidth();
				const int ih = srcClip.getHeight();

				int x = 0, y = 0;
				while (x > clip.origin.x)   x -= iw;
				while (y > clip.origin.y)   y -= ih;

				const int right = (int) (clip.origin.x + clip.size.width);
				const int bottom = (int) (clip.origin.y + clip.size.height);

				while (y < bottom)
				{
					for (int x2 = x; x2 < right; x2 += iw)
						CGContextDrawImage (context, CGRectMake (x2, y, iw, ih), image);

					y += ih;
				}
			}
#endif
		}
		else
		{
			CGContextDrawImage (context, imageRect, image);
		}

		CGImageRelease (image); // (This causes a memory bug in iPhone sim 3.0 - try upgrading to a later version if you hit this)
		CGContextRestoreGState (context);
	}

	void drawLine (double x1, double y1, double x2, double y2)
	{
		CGContextSetLineCap (context, kCGLineCapSquare);
		CGContextSetLineWidth (context, 1.0f);
		CGContextSetRGBStrokeColor (context,
									state->fillType.colour.getFloatRed(), state->fillType.colour.getFloatGreen(),
									state->fillType.colour.getFloatBlue(), state->fillType.colour.getFloatAlpha());

		CGPoint line[] = { { (CGFloat) x1, flipHeight - (CGFloat) y1 },
						   { (CGFloat) x2, flipHeight - (CGFloat) y2 } };

		CGContextStrokeLineSegments (context, line, 1);
	}

	void drawVerticalLine (const int x, double top, double bottom)
	{
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5
		CGContextFillRect (context, CGRectMake (x, flipHeight - (float) bottom, 1.0f, (float) (bottom - top)));
#else
		// On Leopard, unless both co-ordinates are non-integer, it disables anti-aliasing, so nudge
		// the x co-ord slightly to trick it..
		CGContextFillRect (context, CGRectMake (x + 1.0f / 256.0f, flipHeight - (float) bottom, 1.0f + 1.0f / 256.0f, (float) (bottom - top)));
#endif
	}

	void drawHorizontalLine (const int y, double left, double right)
	{
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5
		CGContextFillRect (context, CGRectMake ((float) left, flipHeight - (y + 1.0f), (float) (right - left), 1.0f));
#else
		// On Leopard, unless both co-ordinates are non-integer, it disables anti-aliasing, so nudge
		// the x co-ord slightly to trick it..
		CGContextFillRect (context, CGRectMake ((float) left, flipHeight - (y + (1.0f + 1.0f / 256.0f)), (float) (right - left), 1.0f + 1.0f / 256.0f));
#endif
	}

	void setFont (const Font& newFont)
	{
		if (state->font != newFont)
		{
			state->fontRef = 0;
			state->font = newFont;

			MacTypeface* mf = dynamic_cast <MacTypeface*> (state->font.getTypeface());

			if (mf != 0)
			{
				state->fontRef = mf->fontRef;
				CGContextSetFont (context, state->fontRef);
				CGContextSetFontSize (context, state->font.getHeight() * mf->fontHeightToCGSizeFactor);

				state->fontTransform = mf->renderingTransform;
				state->fontTransform.a *= state->font.getHorizontalScale();
				CGContextSetTextMatrix (context, state->fontTransform);
			}
		}
	}

	const Font getFont()
	{
		return state->font;
	}

	void drawGlyph (int glyphNumber, const AffineTransform& transform)
	{
		if (state->fontRef != 0 && state->fillType.isColour())
		{
			if (transform.isOnlyTranslation())
			{
				CGGlyph g = glyphNumber;
				CGContextShowGlyphsAtPoint (context, transform.getTranslationX(),
											flipHeight - roundToInt (transform.getTranslationY()), &g, 1);
			}
			else
			{
				CGContextSaveGState (context);
				flip();
				applyTransform (transform);

				CGAffineTransform t = state->fontTransform;
				t.d = -t.d;
				CGContextSetTextMatrix (context, t);

				CGGlyph g = glyphNumber;
				CGContextShowGlyphsAtPoint (context, 0, 0, &g, 1);

				CGContextSetTextMatrix (context, state->fontTransform);
				CGContextRestoreGState (context);
			}
		}
		else
		{
			Path p;
			Font& f = state->font;
			f.getTypeface()->getOutlineForGlyph (glyphNumber, p);

			fillPath (p, AffineTransform::scale (f.getHeight() * f.getHorizontalScale(), f.getHeight())
										 .followedBy (transform));
		}
	}

private:
	CGContextRef context;
	const CGFloat flipHeight;
	CGColorSpaceRef rgbColourSpace, greyColourSpace;
	CGFunctionCallbacks gradientCallbacks;

	struct SavedState
	{
		SavedState()
			: font (1.0f), fontRef (0), fontTransform (CGAffineTransformIdentity)
		{
		}

		SavedState (const SavedState& other)
			: fillType (other.fillType), font (other.font), fontRef (other.fontRef),
			  fontTransform (other.fontTransform)
		{
		}

		~SavedState()
		{
		}

		FillType fillType;
		Font font;
		CGFontRef fontRef;
		CGAffineTransform fontTransform;
	};

	ScopedPointer <SavedState> state;
	OwnedArray <SavedState> stateStack;
	HeapBlock <PixelARGB> gradientLookupTable;
	int numGradientLookupEntries;

	static void gradientCallback (void* info, const CGFloat* inData, CGFloat* outData)
	{
		const CoreGraphicsContext* const g = static_cast <const CoreGraphicsContext*> (info);

		const int index = roundToInt (g->numGradientLookupEntries * inData[0]);
		PixelARGB colour (g->gradientLookupTable [jlimit (0, g->numGradientLookupEntries, index)]);
		colour.unpremultiply();

		outData[0] = colour.getRed() / 255.0f;
		outData[1] = colour.getGreen() / 255.0f;
		outData[2] = colour.getBlue() / 255.0f;
		outData[3] = colour.getAlpha() / 255.0f;
	}

	CGShadingRef createGradient (const AffineTransform& transform, ColourGradient gradient)
	{
		numGradientLookupEntries = gradient.createLookupTable (transform, gradientLookupTable);
		--numGradientLookupEntries;

		CGShadingRef result = 0;
		CGFunctionRef function = CGFunctionCreate (this, 1, 0, 4, 0, &gradientCallbacks);
		CGPoint p1 (CGPointMake (gradient.x1, gradient.y1));

		if (gradient.isRadial)
		{
			result = CGShadingCreateRadial (rgbColourSpace, p1, 0,
											p1, hypotf (gradient.x1 - gradient.x2, gradient.y1 - gradient.y2),
											function, true, true);
		}
		else
		{
			result = CGShadingCreateAxial (rgbColourSpace, p1,
										   CGPointMake (gradient.x2, gradient.y2),
										   function, true, true);
		}

		CGFunctionRelease (function);
		return result;
	}

	void drawGradient()
	{
		flip();
		applyTransform (state->fillType.transform);

		CGContextSetInterpolationQuality (context, kCGInterpolationDefault); // (This is required for 10.4, where there's a crash if
																			 // you draw a gradient with high quality interp enabled).
		CGShadingRef shading = createGradient (state->fillType.transform, *(state->fillType.gradient));
		CGContextSetAlpha (context, state->fillType.getOpacity());
		CGContextDrawShading (context, shading);
		CGShadingRelease (shading);
	}

	void createPath (const Path& path) const
	{
		CGContextBeginPath (context);
		Path::Iterator i (path);

		while (i.next())
		{
			switch (i.elementType)
			{
			case Path::Iterator::startNewSubPath:
				CGContextMoveToPoint (context, i.x1, i.y1);
				break;
			case Path::Iterator::lineTo:
				CGContextAddLineToPoint (context, i.x1, i.y1);
				break;
			case Path::Iterator::quadraticTo:
				CGContextAddQuadCurveToPoint (context, i.x1, i.y1, i.x2, i.y2);
				break;
			case Path::Iterator::cubicTo:
				CGContextAddCurveToPoint (context, i.x1, i.y1, i.x2, i.y2, i.x3, i.y3);
				break;
			case Path::Iterator::closePath:
				CGContextClosePath (context); break;
			default:
				jassertfalse
				break;
			}
		}
	}

	void createPath (const Path& path, const AffineTransform& transform) const
	{
		CGContextBeginPath (context);
		Path::Iterator i (path);

		while (i.next())
		{
			switch (i.elementType)
			{
			case Path::Iterator::startNewSubPath:
				transform.transformPoint (i.x1, i.y1);
				CGContextMoveToPoint (context, i.x1, flipHeight - i.y1);
				break;
			case Path::Iterator::lineTo:
				transform.transformPoint (i.x1, i.y1);
				CGContextAddLineToPoint (context, i.x1, flipHeight - i.y1);
				break;
			case Path::Iterator::quadraticTo:
				transform.transformPoint (i.x1, i.y1);
				transform.transformPoint (i.x2, i.y2);
				CGContextAddQuadCurveToPoint (context, i.x1, flipHeight - i.y1, i.x2, flipHeight - i.y2);
				break;
			case Path::Iterator::cubicTo:
				transform.transformPoint (i.x1, i.y1);
				transform.transformPoint (i.x2, i.y2);
				transform.transformPoint (i.x3, i.y3);
				CGContextAddCurveToPoint (context, i.x1, flipHeight - i.y1, i.x2, flipHeight - i.y2, i.x3, flipHeight - i.y3);
				break;
			case Path::Iterator::closePath:
				CGContextClosePath (context); break;
			default:
				jassertfalse
				break;
			}
		}
	}

	static Image* createAlphaChannelImage (const Image& im)
	{
		if (im.getFormat() == Image::SingleChannel)
			return const_cast <Image*> (&im);

		return im.createCopyOfAlphaChannel();
	}

	static void deleteAlphaChannelImage (const Image& im, Image* const alphaIm)
	{
		if (im.getFormat() != Image::SingleChannel)
			delete alphaIm;
	}

	void flip() const
	{
		CGContextConcatCTM (context, CGAffineTransformMake (1, 0, 0, -1, 0, flipHeight));
	}

	void applyTransform (const AffineTransform& transform) const
	{
		CGAffineTransform t;
		t.a = transform.mat00;
		t.b = transform.mat10;
		t.c = transform.mat01;
		t.d = transform.mat11;
		t.tx = transform.mat02;
		t.ty = transform.mat12;
		CGContextConcatCTM (context, t);
	}

	CoreGraphicsContext (const CoreGraphicsContext&);
	CoreGraphicsContext& operator= (const CoreGraphicsContext&);
};

LowLevelGraphicsContext* CoreGraphicsImage::createLowLevelContext()
{
	return new CoreGraphicsContext (context, imageHeight);
}

#endif
/*** End of inlined file: juce_mac_CoreGraphicsContext.mm ***/



/*** Start of inlined file: juce_mac_NSViewComponentPeer.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class NSViewComponentPeer;

END_JUCE_NAMESPACE

#define JuceNSView MakeObjCClassName(JuceNSView)

@interface JuceNSView : NSView<NSTextInput>
{
@public
	NSViewComponentPeer* owner;
	NSNotificationCenter* notificationCenter;
	String* stringBeingComposed;
	bool textWasInserted;
}

- (JuceNSView*) initWithOwner: (NSViewComponentPeer*) owner withFrame: (NSRect) frame;
- (void) dealloc;

- (BOOL) isOpaque;
- (void) drawRect: (NSRect) r;

- (void) mouseDown: (NSEvent*) ev;
- (void) asyncMouseDown: (NSEvent*) ev;
- (void) mouseUp: (NSEvent*) ev;
- (void) asyncMouseUp: (NSEvent*) ev;
- (void) mouseDragged: (NSEvent*) ev;
- (void) mouseMoved: (NSEvent*) ev;
- (void) mouseEntered: (NSEvent*) ev;
- (void) mouseExited: (NSEvent*) ev;
- (void) rightMouseDown: (NSEvent*) ev;
- (void) rightMouseDragged: (NSEvent*) ev;
- (void) rightMouseUp: (NSEvent*) ev;
- (void) otherMouseDown: (NSEvent*) ev;
- (void) otherMouseDragged: (NSEvent*) ev;
- (void) otherMouseUp: (NSEvent*) ev;
- (void) scrollWheel: (NSEvent*) ev;
- (BOOL) acceptsFirstMouse: (NSEvent*) ev;
- (void) frameChanged: (NSNotification*) n;
- (void) viewDidMoveToWindow;

- (void) keyDown: (NSEvent*) ev;
- (void) keyUp: (NSEvent*) ev;

// NSTextInput Methods
- (void) insertText: (id) aString;
- (void) doCommandBySelector: (SEL) aSelector;
- (void) setMarkedText: (id) aString selectedRange: (NSRange) selRange;
- (void) unmarkText;
- (BOOL) hasMarkedText;
- (long) conversationIdentifier;
- (NSAttributedString*) attributedSubstringFromRange: (NSRange) theRange;
- (NSRange) markedRange;
- (NSRange) selectedRange;
- (NSRect) firstRectForCharacterRange: (NSRange) theRange;
- (unsigned int) characterIndexForPoint: (NSPoint) thePoint;
- (NSArray*) validAttributesForMarkedText;

- (void) flagsChanged: (NSEvent*) ev;
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
- (BOOL) performKeyEquivalent: (NSEvent*) ev;
#endif

- (BOOL) becomeFirstResponder;
- (BOOL) resignFirstResponder;
- (BOOL) acceptsFirstResponder;

- (void) asyncRepaint: (id) rect;

- (NSArray*) getSupportedDragTypes;
- (BOOL) sendDragCallback: (int) type sender: (id <NSDraggingInfo>) sender;
- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) sender;
- (NSDragOperation) draggingUpdated: (id <NSDraggingInfo>) sender;
- (void) draggingEnded: (id <NSDraggingInfo>) sender;
- (void) draggingExited: (id <NSDraggingInfo>) sender;
- (BOOL) prepareForDragOperation: (id <NSDraggingInfo>) sender;
- (BOOL) performDragOperation: (id <NSDraggingInfo>) sender;
- (void) concludeDragOperation: (id <NSDraggingInfo>) sender;

@end

#define JuceNSWindow MakeObjCClassName(JuceNSWindow)

#if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
@interface JuceNSWindow : NSWindow <NSWindowDelegate>
#else
@interface JuceNSWindow : NSWindow
#endif
{
@private
	NSViewComponentPeer* owner;
	bool isZooming;
}

- (void) setOwner: (NSViewComponentPeer*) owner;
- (BOOL) canBecomeKeyWindow;
- (void) becomeKeyWindow;
- (BOOL) windowShouldClose: (id) window;
- (NSRect) constrainFrameRect: (NSRect) frameRect toScreen: (NSScreen*) screen;
- (NSSize) windowWillResize: (NSWindow*) window toSize: (NSSize) proposedFrameSize;
- (void) zoom: (id) sender;
@end

BEGIN_JUCE_NAMESPACE

class NSViewComponentPeer  : public ComponentPeer
{
public:
	NSViewComponentPeer (Component* const component,
						 const int windowStyleFlags,
						 NSView* viewToAttachTo);

	~NSViewComponentPeer();

	void* getNativeHandle() const;
	void setVisible (bool shouldBeVisible);
	void setTitle (const String& title);
	void setPosition (int x, int y);
	void setSize (int w, int h);
	void setBounds (int x, int y, int w, int h, const bool isNowFullScreen);
	const Rectangle<int> getBounds (const bool global) const;
	const Rectangle<int> getBounds() const;
	const Point<int> getScreenPosition() const;
	const Point<int> relativePositionToGlobal (const Point<int>& relativePosition);
	const Point<int> globalPositionToRelative (const Point<int>& screenPosition);
	void setMinimised (bool shouldBeMinimised);
	bool isMinimised() const;
	void setFullScreen (bool shouldBeFullScreen);
	bool isFullScreen() const;
	bool contains (const Point<int>& position, bool trueIfInAChildWindow) const;
	const BorderSize getFrameSize() const;
	bool setAlwaysOnTop (bool alwaysOnTop);
	void toFront (bool makeActiveWindow);
	void toBehind (ComponentPeer* other);
	void setIcon (const Image& newIcon);
	const StringArray getAvailableRenderingEngines() throw();
	int getCurrentRenderingEngine() throw();
	void setCurrentRenderingEngine (int index) throw();

	virtual void redirectMouseDown (NSEvent* ev);
	virtual void redirectMouseUp (NSEvent* ev);
	virtual void redirectMouseDrag (NSEvent* ev);
	virtual void redirectMouseMove (NSEvent* ev);
	virtual void redirectMouseEnter (NSEvent* ev);
	virtual void redirectMouseExit (NSEvent* ev);
	virtual void redirectMouseWheel (NSEvent* ev);
	void sendMouseEvent (NSEvent* ev);

	bool handleKeyEvent (NSEvent* ev, bool isKeyDown);
	virtual bool redirectKeyDown (NSEvent* ev);
	virtual bool redirectKeyUp (NSEvent* ev);
	virtual void redirectModKeyChange (NSEvent* ev);
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
	virtual bool redirectPerformKeyEquivalent (NSEvent* ev);
#endif

	virtual BOOL sendDragCallback (int type, id <NSDraggingInfo> sender);

	virtual bool isOpaque();
	virtual void drawRect (NSRect r);

	virtual bool canBecomeKeyWindow();
	virtual bool windowShouldClose();

	virtual void redirectMovedOrResized();
	virtual void viewMovedToWindow();

	virtual NSRect constrainRect (NSRect r);

	static void showArrowCursorIfNeeded();
	static void updateModifiers (NSEvent* e);
	static void updateKeysDown (NSEvent* ev, bool isKeyDown);

	static int getKeyCodeFromEvent (NSEvent* ev)
	{
		const String unmodified (nsStringToJuce ([ev charactersIgnoringModifiers]));
		int keyCode = unmodified[0];

		if (keyCode == 0x19) // (backwards-tab)
			keyCode = '\t';
		else if (keyCode == 0x03) // (enter)
			keyCode = '\r';

		return keyCode;
	}

	static int64 getMouseTime (NSEvent* e)
	{
		return (Time::currentTimeMillis() - Time::getMillisecondCounter())
				+ (int64) ([e timestamp] * 1000.0);
	}

	static const Point<int> getMousePos (NSEvent* e, NSView* view)
	{
		NSPoint p = [view convertPoint: [e locationInWindow] fromView: nil];
		return Point<int> (roundToInt (p.x), roundToInt ([view frame].size.height - p.y));
	}

	static int getModifierForButtonNumber (const NSInteger num)
	{
		return num == 0 ? ModifierKeys::leftButtonModifier
					: (num == 1 ? ModifierKeys::rightButtonModifier
								: (num == 2 ? ModifierKeys::middleButtonModifier : 0));
	}

	virtual void viewFocusGain();
	virtual void viewFocusLoss();
	bool isFocused() const;
	void grabFocus();
	void textInputRequired (const Point<int>& position);

	void repaint (int x, int y, int w, int h);
	void performAnyPendingRepaintsNow();

	juce_UseDebuggingNewOperator

	NSWindow* window;
	JuceNSView* view;
	bool isSharedWindow, fullScreen, insideDrawRect, usingCoreGraphics, recursiveToFrontCall;

	static ModifierKeys currentModifiers;
	static ComponentPeer* currentlyFocusedPeer;
	static Array<int> keysCurrentlyDown;
};

END_JUCE_NAMESPACE

@implementation JuceNSView

- (JuceNSView*) initWithOwner: (NSViewComponentPeer*) owner_
					withFrame: (NSRect) frame
{
	[super initWithFrame: frame];
	owner = owner_;
	stringBeingComposed = 0;
	textWasInserted = false;

	notificationCenter = [NSNotificationCenter defaultCenter];

	[notificationCenter  addObserver: self
							selector: @selector (frameChanged:)
								name: NSViewFrameDidChangeNotification
							  object: self];

	if (! owner_->isSharedWindow)
	{
		[notificationCenter  addObserver: self
								selector: @selector (frameChanged:)
									name: NSWindowDidMoveNotification
								  object: owner_->window];
	}

	[self registerForDraggedTypes: [self getSupportedDragTypes]];

	return self;
}

- (void) dealloc
{
	[notificationCenter removeObserver: self];
	delete stringBeingComposed;
	[super dealloc];
}

- (void) drawRect: (NSRect) r
{
	if (owner != 0)
		owner->drawRect (r);
}

- (BOOL) isOpaque
{
	return owner == 0 || owner->isOpaque();
}

- (void) mouseDown: (NSEvent*) ev
{
	// In some host situations, the host will stop modal loops from working
	// correctly if they're called from a mouse event, so we'll trigger
	// the event asynchronously..
	if (JUCEApplication::getInstance() == 0)
		[self performSelectorOnMainThread: @selector (asyncMouseDown:)
							   withObject: ev
							waitUntilDone: NO];
	else
		[self asyncMouseDown: ev];
}

- (void) asyncMouseDown: (NSEvent*) ev
{
	if (owner != 0)
		owner->redirectMouseDown (ev);
}

- (void) mouseUp: (NSEvent*) ev
{
	// In some host situations, the host will stop modal loops from working
	// correctly if they're called from a mouse event, so we'll trigger
	// the event asynchronously..
	if (JUCEApplication::getInstance() == 0)
		[self performSelectorOnMainThread: @selector (asyncMouseUp:)
							   withObject: ev
							waitUntilDone: NO];
	else
		[self asyncMouseUp: ev];
}

- (void) asyncMouseUp: (NSEvent*) ev
{
	if (owner != 0)
		owner->redirectMouseUp (ev);
}

- (void) mouseDragged: (NSEvent*) ev
{
	if (owner != 0)
		owner->redirectMouseDrag (ev);
}

- (void) mouseMoved: (NSEvent*) ev
{
	if (owner != 0)
		owner->redirectMouseMove (ev);
}

- (void) mouseEntered: (NSEvent*) ev
{
	if (owner != 0)
		owner->redirectMouseEnter (ev);
}

- (void) mouseExited: (NSEvent*) ev
{
	if (owner != 0)
		owner->redirectMouseExit (ev);
}

- (void) rightMouseDown: (NSEvent*) ev
{
	[self mouseDown: ev];
}

- (void) rightMouseDragged: (NSEvent*) ev
{
	[self mouseDragged: ev];
}

- (void) rightMouseUp: (NSEvent*) ev
{
	[self mouseUp: ev];
}

- (void) otherMouseDown: (NSEvent*) ev
{
	[self mouseDown: ev];
}

- (void) otherMouseDragged: (NSEvent*) ev
{
	[self mouseDragged: ev];
}

- (void) otherMouseUp: (NSEvent*) ev
{
	[self mouseUp: ev];
}

- (void) scrollWheel: (NSEvent*) ev
{
	if (owner != 0)
		owner->redirectMouseWheel (ev);
}

- (BOOL) acceptsFirstMouse: (NSEvent*) ev
{
	return YES;
}

- (void) frameChanged: (NSNotification*) n
{
	if (owner != 0)
		owner->redirectMovedOrResized();
}

- (void) viewDidMoveToWindow
{
   if (owner != 0)
	   owner->viewMovedToWindow();
}

- (void) asyncRepaint: (id) rect
{
	NSRect* r = (NSRect*) [((NSData*) rect) bytes];
	[self setNeedsDisplayInRect: *r];
}

- (void) keyDown: (NSEvent*) ev
{
	TextInputTarget* const target = owner->findCurrentTextInputTarget();
	textWasInserted = false;

	if (target != 0)
		[self interpretKeyEvents: [NSArray arrayWithObject: ev]];
	else
		deleteAndZero (stringBeingComposed);

	if ((! textWasInserted) && (owner == 0 || ! owner->redirectKeyDown (ev)))
		[super keyDown: ev];
}

- (void) keyUp: (NSEvent*) ev
{
	if (owner == 0 || ! owner->redirectKeyUp (ev))
		[super keyUp: ev];
}

- (void) insertText: (id) aString
{
	// This commits multi-byte text when return is pressed, or after every keypress for western keyboards
	if ([aString length] > 0)
	{
		TextInputTarget* const target = owner->findCurrentTextInputTarget();

		if (target != 0)
		{
			target->insertTextAtCaret (nsStringToJuce ([aString isKindOfClass: [NSAttributedString class]] ? [aString string] : aString));
			textWasInserted = true;
		}
	}

	deleteAndZero (stringBeingComposed);
}

- (void) doCommandBySelector: (SEL) aSelector
{
}

- (void) setMarkedText: (id) aString selectedRange: (NSRange) selectionRange
{
	if (stringBeingComposed == 0)
		stringBeingComposed = new String();

	*stringBeingComposed = nsStringToJuce ([aString isKindOfClass:[NSAttributedString class]] ? [aString string] : aString);

	TextInputTarget* const target = owner->findCurrentTextInputTarget();

	if (target != 0)
	{
		const Range<int> currentHighlight (target->getHighlightedRegion());
		target->insertTextAtCaret (*stringBeingComposed);
		target->setHighlightedRegion (currentHighlight.withLength (stringBeingComposed->length()));
		textWasInserted = true;
	}
}

- (void) unmarkText
{
	if (stringBeingComposed != 0)
	{
		TextInputTarget* const target = owner->findCurrentTextInputTarget();

		if (target != 0)
		{
			target->insertTextAtCaret (*stringBeingComposed);
			textWasInserted = true;
		}
	}

	deleteAndZero (stringBeingComposed);
}

- (BOOL) hasMarkedText
{
	return stringBeingComposed != 0;
}

- (long) conversationIdentifier
{
	return (long) (pointer_sized_int) self;
}

- (NSAttributedString*) attributedSubstringFromRange: (NSRange) theRange
{
	TextInputTarget* const target = owner->findCurrentTextInputTarget();

	if (target != 0)
	{
		const Range<int> r ((int) theRange.location,
							(int) (theRange.location + theRange.length));

		return [[[NSAttributedString alloc] initWithString: juceStringToNS (target->getTextInRange (r))] autorelease];
	}

	return nil;
}

- (NSRange) markedRange
{
	return stringBeingComposed != 0 ? NSMakeRange (0, stringBeingComposed->length())
									: NSMakeRange (NSNotFound, 0);
}

- (NSRange) selectedRange
{
	TextInputTarget* const target = owner->findCurrentTextInputTarget();

	if (target != 0)
	{
		const Range<int> highlight (target->getHighlightedRegion());

		if (! highlight.isEmpty())
			return NSMakeRange (highlight.getStart(), highlight.getLength());
	}

	return NSMakeRange (NSNotFound, 0);
}

- (NSRect) firstRectForCharacterRange: (NSRange) theRange
{
	JUCE_NAMESPACE::Component* const comp = dynamic_cast <JUCE_NAMESPACE::Component*> (owner->findCurrentTextInputTarget());

	if (comp == 0)
		return NSMakeRect (0, 0, 0, 0);

	const Rectangle<int> bounds (comp->getScreenBounds());

	return NSMakeRect (bounds.getX(),
					   [[[NSScreen screens] objectAtIndex: 0] frame].size.height - bounds.getY(),
					   bounds.getWidth(),
					   bounds.getHeight());
}

- (unsigned int) characterIndexForPoint: (NSPoint) thePoint
{
	return NSNotFound;
}

- (NSArray*) validAttributesForMarkedText
{
	return [NSArray array];
}

- (void) flagsChanged: (NSEvent*) ev
{
	if (owner != 0)
		owner->redirectModKeyChange (ev);
}

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
- (BOOL) performKeyEquivalent: (NSEvent*) ev
{
	if (owner != 0 && owner->redirectPerformKeyEquivalent (ev))
		return true;

	return [super performKeyEquivalent: ev];
}
#endif

- (BOOL) becomeFirstResponder
{
	if (owner != 0)
		owner->viewFocusGain();

	return true;
}

- (BOOL) resignFirstResponder
{
	if (owner != 0)
		owner->viewFocusLoss();

	return true;
}

- (BOOL) acceptsFirstResponder
{
	return owner != 0 && owner->canBecomeKeyWindow();
}

- (NSArray*) getSupportedDragTypes
{
	return [NSArray arrayWithObjects: NSFilenamesPboardType, /*NSFilesPromisePboardType, NSStringPboardType,*/ nil];
}

- (BOOL) sendDragCallback: (int) type sender: (id <NSDraggingInfo>) sender
{
	return owner != 0 && owner->sendDragCallback (type, sender);
}

- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) sender
{
	if ([self sendDragCallback: 0 sender: sender])
		return NSDragOperationCopy | NSDragOperationMove | NSDragOperationGeneric;
	else
		return NSDragOperationNone;
}

- (NSDragOperation) draggingUpdated: (id <NSDraggingInfo>) sender
{
	if ([self sendDragCallback: 0 sender: sender])
		return NSDragOperationCopy | NSDragOperationMove | NSDragOperationGeneric;
	else
		return NSDragOperationNone;
}

- (void) draggingEnded: (id <NSDraggingInfo>) sender
{
	[self sendDragCallback: 1 sender: sender];
}

- (void) draggingExited: (id <NSDraggingInfo>) sender
{
	[self sendDragCallback: 1 sender: sender];
}

- (BOOL) prepareForDragOperation: (id <NSDraggingInfo>) sender
{
	return YES;
}

- (BOOL) performDragOperation: (id <NSDraggingInfo>) sender
{
	return [self sendDragCallback: 2 sender: sender];
}

- (void) concludeDragOperation: (id <NSDraggingInfo>) sender
{
}

@end

@implementation JuceNSWindow

- (void) setOwner: (NSViewComponentPeer*) owner_
{
	owner = owner_;
	isZooming = false;
}

- (BOOL) canBecomeKeyWindow
{
	return owner != 0 && owner->canBecomeKeyWindow();
}

- (void) becomeKeyWindow
{
	[super becomeKeyWindow];

	if (owner != 0)
		owner->grabFocus();
}

- (BOOL) windowShouldClose: (id) window
{
	return owner == 0 || owner->windowShouldClose();
}

- (NSRect) constrainFrameRect: (NSRect) frameRect toScreen: (NSScreen*) screen
{
	if (owner != 0)
		frameRect = owner->constrainRect (frameRect);

	return frameRect;
}

- (NSSize) windowWillResize: (NSWindow*) window toSize: (NSSize) proposedFrameSize
{
	if (isZooming)
		return proposedFrameSize;

	NSRect frameRect = [self frame];
	frameRect.origin.y -= proposedFrameSize.height - frameRect.size.height;
	frameRect.size = proposedFrameSize;

	if (owner != 0)
		frameRect = owner->constrainRect (frameRect);

	return frameRect.size;
}

- (void) zoom: (id) sender
{
	isZooming = true;
	[super zoom: sender];
	isZooming = false;
}

- (void) windowWillMove: (NSNotification*) notification
{
	if (JUCE_NAMESPACE::Component::getCurrentlyModalComponent() != 0
		  && owner->getComponent()->isCurrentlyBlockedByAnotherModalComponent()
		  && (owner->getStyleFlags() & JUCE_NAMESPACE::ComponentPeer::windowHasTitleBar) != 0)
		JUCE_NAMESPACE::Component::getCurrentlyModalComponent()->inputAttemptWhenModal();
}

@end

BEGIN_JUCE_NAMESPACE

ModifierKeys NSViewComponentPeer::currentModifiers;
ComponentPeer* NSViewComponentPeer::currentlyFocusedPeer = 0;
Array<int> NSViewComponentPeer::keysCurrentlyDown;

bool KeyPress::isKeyCurrentlyDown (const int keyCode)
{
	if (NSViewComponentPeer::keysCurrentlyDown.contains (keyCode))
		return true;

	if (keyCode >= 'A' && keyCode <= 'Z'
		&& NSViewComponentPeer::keysCurrentlyDown.contains ((int) CharacterFunctions::toLowerCase ((juce_wchar) keyCode)))
		return true;

	if (keyCode >= 'a' && keyCode <= 'z'
		&& NSViewComponentPeer::keysCurrentlyDown.contains ((int) CharacterFunctions::toUpperCase ((juce_wchar) keyCode)))
		return true;

	return false;
}

void NSViewComponentPeer::updateModifiers (NSEvent* e)
{
	int m = 0;

	if (([e modifierFlags] & NSShiftKeyMask) != 0)	  m |= ModifierKeys::shiftModifier;
	if (([e modifierFlags] & NSControlKeyMask) != 0)	m |= ModifierKeys::ctrlModifier;
	if (([e modifierFlags] & NSAlternateKeyMask) != 0)	  m |= ModifierKeys::altModifier;
	if (([e modifierFlags] & NSCommandKeyMask) != 0)	m |= ModifierKeys::commandModifier;

	currentModifiers = currentModifiers.withOnlyMouseButtons().withFlags (m);
}

void NSViewComponentPeer::updateKeysDown (NSEvent* ev, bool isKeyDown)
{
	updateModifiers (ev);
	int keyCode = getKeyCodeFromEvent (ev);

	if (keyCode != 0)
	{
		if (isKeyDown)
			keysCurrentlyDown.addIfNotAlreadyThere (keyCode);
		else
			keysCurrentlyDown.removeValue (keyCode);
	}
}

const ModifierKeys ModifierKeys::getCurrentModifiersRealtime() throw()
{
	return NSViewComponentPeer::currentModifiers;
}

void ModifierKeys::updateCurrentModifiers() throw()
{
	currentModifiers = NSViewComponentPeer::currentModifiers;
}

NSViewComponentPeer::NSViewComponentPeer (Component* const component_,
										  const int windowStyleFlags,
										  NSView* viewToAttachTo)
	: ComponentPeer (component_, windowStyleFlags),
	  window (0),
	  view (0),
	  isSharedWindow (viewToAttachTo != 0),
	  fullScreen (false),
	  insideDrawRect (false),
#if USE_COREGRAPHICS_RENDERING
	  usingCoreGraphics (true),
#else
	  usingCoreGraphics (false),
#endif
	  recursiveToFrontCall (false)
{
	NSRect r;
	r.origin.x = 0;
	r.origin.y = 0;
	r.size.width = (float) component->getWidth();
	r.size.height = (float) component->getHeight();

	view = [[JuceNSView alloc] initWithOwner: this withFrame: r];
	[view setPostsFrameChangedNotifications: YES];

	if (isSharedWindow)
	{
		window = [viewToAttachTo window];
		[viewToAttachTo addSubview: view];

		setVisible (component->isVisible());
	}
	else
	{
		r.origin.x = (float) component->getX();
		r.origin.y = (float) component->getY();
		r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - (r.origin.y + r.size.height);

		unsigned int style = 0;
		if ((windowStyleFlags & windowHasTitleBar) == 0)
			style = NSBorderlessWindowMask;
		else
			style = NSTitledWindowMask;

		if ((windowStyleFlags & windowHasMinimiseButton) != 0)
			style |= NSMiniaturizableWindowMask;

		if ((windowStyleFlags & windowHasCloseButton) != 0)
			style |= NSClosableWindowMask;

		if ((windowStyleFlags & windowIsResizable) != 0)
			style |= NSResizableWindowMask;

		window = [[JuceNSWindow alloc] initWithContentRect: r
												 styleMask: style
												   backing: NSBackingStoreBuffered
													 defer: YES];

		[((JuceNSWindow*) window) setOwner: this];
		[window orderOut: nil];
		[window setDelegate: (JuceNSWindow*) window];
		[window setOpaque: component->isOpaque()];
		[window setHasShadow: ((windowStyleFlags & windowHasDropShadow) != 0)];

		if (component->isAlwaysOnTop())
			[window setLevel: NSFloatingWindowLevel];

		[window setContentView: view];
		[window setAutodisplay: YES];
		[window setAcceptsMouseMovedEvents: YES];

		// We'll both retain and also release this on closing because plugin hosts can unexpectedly
		// close the window for us, and also tend to get cause trouble if setReleasedWhenClosed is NO.
		[window setReleasedWhenClosed: YES];
		[window retain];

		[window setExcludedFromWindowsMenu: (windowStyleFlags & windowIsTemporary) != 0];
		[window setIgnoresMouseEvents: (windowStyleFlags & windowIgnoresMouseClicks) != 0];
	}

	setTitle (component->getName());
}

NSViewComponentPeer::~NSViewComponentPeer()
{
	view->owner = 0;
	[view removeFromSuperview];
	[view release];

	if (! isSharedWindow)
	{
		[((JuceNSWindow*) window) setOwner: 0];
		[window close];
		[window release];
	}
}

void* NSViewComponentPeer::getNativeHandle() const
{
	return view;
}

void NSViewComponentPeer::setVisible (bool shouldBeVisible)
{
	if (isSharedWindow)
	{
		[view setHidden: ! shouldBeVisible];
	}
	else
	{
		if (shouldBeVisible)
		{
			[window orderFront: nil];
			handleBroughtToFront();
		}
		else
		{
			[window orderOut: nil];
		}
	}
}

void NSViewComponentPeer::setTitle (const String& title)
{
	const ScopedAutoReleasePool pool;

	if (! isSharedWindow)
		[window setTitle: juceStringToNS (title)];
}

void NSViewComponentPeer::setPosition (int x, int y)
{
	setBounds (x, y, component->getWidth(), component->getHeight(), false);
}

void NSViewComponentPeer::setSize (int w, int h)
{
	setBounds (component->getX(), component->getY(), w, h, false);
}

void NSViewComponentPeer::setBounds (int x, int y, int w, int h, bool isNowFullScreen)
{
	fullScreen = isNowFullScreen;
	w = jmax (0, w);
	h = jmax (0, h);

	NSRect r;
	r.origin.x = (float) x;
	r.origin.y = (float) y;
	r.size.width = (float) w;
	r.size.height = (float) h;

	if (isSharedWindow)
	{
		r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);

		if ([view frame].size.width != r.size.width
			 || [view frame].size.height != r.size.height)
			[view setNeedsDisplay: true];

		[view setFrame: r];
	}
	else
	{
		r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - (r.origin.y + r.size.height);

		[window setFrame: [window frameRectForContentRect: r]
				 display: true];
	}
}

const Rectangle<int> NSViewComponentPeer::getBounds (const bool global) const
{
	NSRect r = [view frame];

	if (global && [view window] != 0)
	{
		r = [view convertRect: r toView: nil];
		NSRect wr = [[view window] frame];
		r.origin.x += wr.origin.x;
		r.origin.y += wr.origin.y;
		r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - r.origin.y - r.size.height;
	}
	else
	{
		r.origin.y = [[view superview] frame].size.height - r.origin.y - r.size.height;
	}

	return Rectangle<int> ((int) r.origin.x, (int) r.origin.y, (int) r.size.width, (int) r.size.height);
}

const Rectangle<int> NSViewComponentPeer::getBounds() const
{
	return getBounds (! isSharedWindow);
}

const Point<int> NSViewComponentPeer::getScreenPosition() const
{
	return getBounds (true).getPosition();
}

const Point<int> NSViewComponentPeer::relativePositionToGlobal (const Point<int>& relativePosition)
{
	return relativePosition + getScreenPosition();
}

const Point<int> NSViewComponentPeer::globalPositionToRelative (const Point<int>& screenPosition)
{
	return screenPosition - getScreenPosition();
}

NSRect NSViewComponentPeer::constrainRect (NSRect r)
{
	if (constrainer != 0)
	{
		NSRect current = [window frame];
		current.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - current.origin.y - current.size.height;

		r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - r.origin.y - r.size.height;

		Rectangle<int> pos ((int) r.origin.x, (int) r.origin.y,
							(int) r.size.width, (int) r.size.height);

		Rectangle<int> original ((int) current.origin.x, (int) current.origin.y,
								 (int) current.size.width, (int) current.size.height);

		constrainer->checkBounds (pos, original,
								  Desktop::getInstance().getAllMonitorDisplayAreas().getBounds(),
								  pos.getY() != original.getY() && pos.getBottom() == original.getBottom(),
								  pos.getX() != original.getX() && pos.getRight() == original.getRight(),
								  pos.getY() == original.getY() && pos.getBottom() != original.getBottom(),
								  pos.getX() == original.getX() && pos.getRight() != original.getRight());

		r.origin.x = pos.getX();
		r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - r.size.height - pos.getY();
		r.size.width = pos.getWidth();
		r.size.height = pos.getHeight();
	}

	return r;
}

void NSViewComponentPeer::setMinimised (bool shouldBeMinimised)
{
	if (! isSharedWindow)
	{
		if (shouldBeMinimised)
			[window miniaturize: nil];
		else
			[window deminiaturize: nil];
	}
}

bool NSViewComponentPeer::isMinimised() const
{
	return window != 0 && [window isMiniaturized];
}

void NSViewComponentPeer::setFullScreen (bool shouldBeFullScreen)
{
	if (! isSharedWindow)
	{
		Rectangle<int> r (lastNonFullscreenBounds);

		setMinimised (false);

		if (fullScreen != shouldBeFullScreen)
		{
			if (shouldBeFullScreen && (getStyleFlags() & windowHasTitleBar) != 0)
			{
				fullScreen = true;
				[window performZoom: nil];
			}
			else
			{
				if (shouldBeFullScreen)
					r = Desktop::getInstance().getMainMonitorArea();

				// (can't call the component's setBounds method because that'll reset our fullscreen flag)
				if (r != getComponent()->getBounds() && ! r.isEmpty())
					setBounds (r.getX(), r.getY(), r.getWidth(), r.getHeight(), shouldBeFullScreen);
			}
		}
	}
}

bool NSViewComponentPeer::isFullScreen() const
{
	return fullScreen;
}

bool NSViewComponentPeer::contains (const Point<int>& position, bool trueIfInAChildWindow) const
{
	if (((unsigned int) position.getX()) >= (unsigned int) component->getWidth()
		|| ((unsigned int) position.getY()) >= (unsigned int) component->getHeight())
		return false;

	NSPoint p;
	p.x = (float) position.getX();
	p.y = (float) position.getY();

	NSView* v = [view hitTest: p];

	if (trueIfInAChildWindow)
		return v != nil;

	return v == view;
}

const BorderSize NSViewComponentPeer::getFrameSize() const
{
	BorderSize b;

	if (! isSharedWindow)
	{
		NSRect v = [view convertRect: [view frame] toView: nil];
		NSRect w = [window frame];

		b.setTop ((int) (w.size.height - (v.origin.y + v.size.height)));
		b.setBottom ((int) v.origin.y);
		b.setLeft ((int) v.origin.x);
		b.setRight ((int) (w.size.width - (v.origin.x + v.size.width)));
	}

	return b;
}

bool NSViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
{
	if (! isSharedWindow)
	{
		[window setLevel: alwaysOnTop ? NSFloatingWindowLevel
									  : NSNormalWindowLevel];
	}

	return true;
}

void NSViewComponentPeer::toFront (bool makeActiveWindow)
{
	if (isSharedWindow)
	{
		[[view superview] addSubview: view
						  positioned: NSWindowAbove
						  relativeTo: nil];
	}

	if (window != 0 && component->isVisible())
	{
		if (makeActiveWindow)
			[window makeKeyAndOrderFront: nil];
		else
			[window orderFront: nil];

		if (! recursiveToFrontCall)
		{
			recursiveToFrontCall = true;
			Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
			handleBroughtToFront();
			recursiveToFrontCall = false;
		}
	}
}

void NSViewComponentPeer::toBehind (ComponentPeer* other)
{
	NSViewComponentPeer* o = (NSViewComponentPeer*) other;

	if (isSharedWindow)
	{
		[[view superview] addSubview: view
						  positioned: NSWindowBelow
						  relativeTo: o->view];
	}
	else
	{
		[window orderWindow: NSWindowBelow
				 relativeTo: o->window != 0 ? [o->window windowNumber]
											: nil ];
	}
}

void NSViewComponentPeer::setIcon (const Image& /*newIcon*/)
{
	// to do..
}

void NSViewComponentPeer::viewFocusGain()
{
	if (currentlyFocusedPeer != this)
	{
		if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
			currentlyFocusedPeer->handleFocusLoss();

		currentlyFocusedPeer = this;
		handleFocusGain();
	}
}

void NSViewComponentPeer::viewFocusLoss()
{
	if (currentlyFocusedPeer == this)
	{
		currentlyFocusedPeer = 0;
		handleFocusLoss();
	}
}

void juce_HandleProcessFocusChange()
{
	NSViewComponentPeer::keysCurrentlyDown.clear();

	if (NSViewComponentPeer::isValidPeer (NSViewComponentPeer::currentlyFocusedPeer))
	{
		if (Process::isForegroundProcess())
		{
			NSViewComponentPeer::currentlyFocusedPeer->handleFocusGain();

			ComponentPeer::bringModalComponentToFront();
		}
		else
		{
			NSViewComponentPeer::currentlyFocusedPeer->handleFocusLoss();

			// turn kiosk mode off if we lose focus..
			Desktop::getInstance().setKioskModeComponent (0);
		}
	}
}

bool NSViewComponentPeer::isFocused() const
{
	return isSharedWindow ? this == currentlyFocusedPeer
						  : (window != 0 && [window isKeyWindow]);
}

void NSViewComponentPeer::grabFocus()
{
	if (window != 0)
	{
		[window makeKeyWindow];
		[window makeFirstResponder: view];

		viewFocusGain();
	}
}

void NSViewComponentPeer::textInputRequired (const Point<int>&)
{
}

bool NSViewComponentPeer::handleKeyEvent (NSEvent* ev, bool isKeyDown)
{
	String unicode (nsStringToJuce ([ev characters]));
	String unmodified (nsStringToJuce ([ev charactersIgnoringModifiers]));
	int keyCode = getKeyCodeFromEvent (ev);

	//DBG ("unicode: " + unicode + " " + String::toHexString ((int) unicode[0]));
	//DBG ("unmodified: " + unmodified + " " + String::toHexString ((int) unmodified[0]));

	if (unicode.isNotEmpty() || keyCode != 0)
	{
		if (isKeyDown)
		{
			bool used = false;

			while (unicode.length() > 0)
			{
				juce_wchar textCharacter = unicode[0];
				unicode = unicode.substring (1);

				if (([ev modifierFlags] & NSCommandKeyMask) != 0)
					textCharacter = 0;

				used = handleKeyUpOrDown (true) || used;
				used = handleKeyPress (keyCode, textCharacter) || used;
			}

			return used;
		}
		else
		{
			if (handleKeyUpOrDown (false))
				return true;
		}
	}

	return false;
}

bool NSViewComponentPeer::redirectKeyDown (NSEvent* ev)
{
	updateKeysDown (ev, true);
	bool used = handleKeyEvent (ev, true);

	if (([ev modifierFlags] & NSCommandKeyMask) != 0)
	{
		// for command keys, the key-up event is thrown away, so simulate one..
		updateKeysDown (ev, false);
		used = (isValidPeer (this) && handleKeyEvent (ev, false)) || used;
	}

	// (If we're running modally, don't allow unused keystrokes to be passed
	// along to other blocked views..)
	if (Component::getCurrentlyModalComponent() != 0)
		used = true;

	return used;
}

bool NSViewComponentPeer::redirectKeyUp (NSEvent* ev)
{
	updateKeysDown (ev, false);
	return handleKeyEvent (ev, false)
			|| Component::getCurrentlyModalComponent() != 0;
}

void NSViewComponentPeer::redirectModKeyChange (NSEvent* ev)
{
	keysCurrentlyDown.clear();
	handleKeyUpOrDown (true);

	updateModifiers (ev);
	handleModifierKeysChange();
}

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
bool NSViewComponentPeer::redirectPerformKeyEquivalent (NSEvent* ev)
{
	if ([ev type] == NSKeyDown)
		return redirectKeyDown (ev);
	else if ([ev type] == NSKeyUp)
		return redirectKeyUp (ev);

	return false;
}
#endif

void NSViewComponentPeer::sendMouseEvent (NSEvent* ev)
{
	updateModifiers (ev);
	handleMouseEvent (0, getMousePos (ev, view), currentModifiers, getMouseTime (ev));
}

void NSViewComponentPeer::redirectMouseDown (NSEvent* ev)
{
	currentModifiers = currentModifiers.withFlags (getModifierForButtonNumber ([ev buttonNumber]));
	sendMouseEvent (ev);
}

void NSViewComponentPeer::redirectMouseUp (NSEvent* ev)
{
	currentModifiers = currentModifiers.withoutFlags (getModifierForButtonNumber ([ev buttonNumber]));
	sendMouseEvent (ev);
	showArrowCursorIfNeeded();
}

void NSViewComponentPeer::redirectMouseDrag (NSEvent* ev)
{
	currentModifiers = currentModifiers.withFlags (getModifierForButtonNumber ([ev buttonNumber]));
	sendMouseEvent (ev);
}

void NSViewComponentPeer::redirectMouseMove (NSEvent* ev)
{
	currentModifiers = currentModifiers.withoutMouseButtons();
	sendMouseEvent (ev);
	showArrowCursorIfNeeded();
}

void NSViewComponentPeer::redirectMouseEnter (NSEvent* ev)
{
	Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
	currentModifiers = currentModifiers.withoutMouseButtons();
	sendMouseEvent (ev);
}

void NSViewComponentPeer::redirectMouseExit (NSEvent* ev)
{
	currentModifiers = currentModifiers.withoutMouseButtons();
	sendMouseEvent (ev);
}

void NSViewComponentPeer::redirectMouseWheel (NSEvent* ev)
{
	updateModifiers (ev);

	handleMouseWheel (0, getMousePos (ev, view), getMouseTime (ev),
					  [ev deltaX] * 10.0f, [ev deltaY] * 10.0f);
}

void NSViewComponentPeer::showArrowCursorIfNeeded()
{
	MouseInputSource& mouse = Desktop::getInstance().getMainMouseSource();

	if (mouse.getComponentUnderMouse() == 0
		 && Desktop::getInstance().findComponentAt (mouse.getScreenPosition()) == 0)
	{
		[[NSCursor arrowCursor] set];
	}
}

BOOL NSViewComponentPeer::sendDragCallback (int type, id <NSDraggingInfo> sender)
{
	NSString* bestType
		= [[sender draggingPasteboard] availableTypeFromArray: [view getSupportedDragTypes]];

	if (bestType == nil)
		return false;

	NSPoint p = [view convertPoint: [sender draggingLocation] fromView: nil];
	const Point<int> pos ((int) p.x, (int) ([view frame].size.height - p.y));

	StringArray files;

	id list = [[sender draggingPasteboard] propertyListForType: bestType];
	if (list == nil)
		return false;

	if ([list isKindOfClass: [NSArray class]])
	{
		NSArray* items = (NSArray*) list;

		for (unsigned int i = 0; i < [items count]; ++i)
			files.add (nsStringToJuce ((NSString*) [items objectAtIndex: i]));
	}

	if (files.size() == 0)
		return false;

	if (type == 0)
		handleFileDragMove (files, pos);
	else if (type == 1)
		handleFileDragExit (files);
	else if (type == 2)
		handleFileDragDrop (files, pos);

	return true;
}

bool NSViewComponentPeer::isOpaque()
{
	return component == 0 || component->isOpaque();
}

void NSViewComponentPeer::drawRect (NSRect r)
{
	if (r.size.width < 1.0f || r.size.height < 1.0f)
		return;

	CGContextRef cg = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];

	if (! component->isOpaque())
		CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));

#if USE_COREGRAPHICS_RENDERING
	if (usingCoreGraphics)
	{
		CoreGraphicsContext context (cg, (float) [view frame].size.height);

		insideDrawRect = true;
		handlePaint (context);
		insideDrawRect = false;
	}
	else
#endif
	{
		Image temp (getComponent()->isOpaque() ? Image::RGB : Image::ARGB,
					(int) (r.size.width + 0.5f),
					(int) (r.size.height + 0.5f),
					! getComponent()->isOpaque());

		LowLevelGraphicsSoftwareRenderer context (temp);
		context.setOrigin (-roundToInt (r.origin.x),
						   -roundToInt ([view frame].size.height - (r.origin.y + r.size.height)));

		const NSRect* rects = 0;
		NSInteger numRects = 0;
		[view getRectsBeingDrawn: &rects count: &numRects];

		RectangleList clip;
		for (int i = 0; i < numRects; ++i)
		{
			clip.addWithoutMerging (Rectangle<int> (roundToInt (rects[i].origin.x),
													roundToInt ([view frame].size.height - (rects[i].origin.y + rects[i].size.height)),
													roundToInt (rects[i].size.width),
													roundToInt (rects[i].size.height)));
		}

		if (context.clipToRectangleList (clip))
		{
			insideDrawRect = true;
			handlePaint (context);
			insideDrawRect = false;

			CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
			CGImageRef image = CoreGraphicsImage::createImage (temp, false, colourSpace);
			CGColorSpaceRelease (colourSpace);
			CGContextDrawImage (cg, CGRectMake (r.origin.x, r.origin.y, temp.getWidth(), temp.getHeight()), image);
			CGImageRelease (image);
		}
	}
}

const StringArray NSViewComponentPeer::getAvailableRenderingEngines() throw()
{
	StringArray s;
	s.add ("Software Renderer");

#if USE_COREGRAPHICS_RENDERING
	s.add ("CoreGraphics Renderer");
#endif

	return s;
}

int NSViewComponentPeer::getCurrentRenderingEngine() throw()
{
	return usingCoreGraphics ? 1 : 0;
}

void NSViewComponentPeer::setCurrentRenderingEngine (int index) throw()
{
#if USE_COREGRAPHICS_RENDERING
	if (usingCoreGraphics != (index > 0))
	{
		usingCoreGraphics = index > 0;
		[view setNeedsDisplay: true];
	}
#endif
}

bool NSViewComponentPeer::canBecomeKeyWindow()
{
	return (getStyleFlags() & JUCE_NAMESPACE::ComponentPeer::windowIgnoresKeyPresses) == 0;
}

bool NSViewComponentPeer::windowShouldClose()
{
	if (! isValidPeer (this))
		return YES;

	handleUserClosingWindow();
	return NO;
}

void NSViewComponentPeer::redirectMovedOrResized()
{
	handleMovedOrResized();
}

void NSViewComponentPeer::viewMovedToWindow()
{
	if (isSharedWindow)
		window = [view window];
}

void Desktop::createMouseInputSources()
{
	mouseSources.add (new MouseInputSource (0, true));
}

void juce_setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool allowMenusAndBars)
{
	// Very annoyingly, this function has to use the old SetSystemUIMode function,
	// which is in Carbon.framework. But, because there's no Cocoa equivalent, it
	// is apparently still available in 64-bit apps..
	if (enableOrDisable)
	{
		SetSystemUIMode (kUIModeAllSuppressed, allowMenusAndBars ? kUIOptionAutoShowMenuBar : 0);
		kioskModeComponent->setBounds (Desktop::getInstance().getMainMonitorArea (false));
	}
	else
	{
		SetSystemUIMode (kUIModeNormal, 0);
	}
}

class AsyncRepaintMessage  : public CallbackMessage
{
public:
	NSViewComponentPeer* const peer;
	const Rectangle<int> rect;

	AsyncRepaintMessage (NSViewComponentPeer* const peer_, const Rectangle<int>& rect_)
		: peer (peer_), rect (rect_)
	{
	}

	void messageCallback()
	{
		if (ComponentPeer::isValidPeer (peer))
			peer->repaint (rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
	}
};

void NSViewComponentPeer::repaint (int x, int y, int w, int h)
{
	if (insideDrawRect)
	{
		(new AsyncRepaintMessage (this, Rectangle<int> (x, y, w, h)))->post();
	}
	else
	{
		[view setNeedsDisplayInRect: NSMakeRect ((float) x, (float) ([view frame].size.height - (y + h)),
												 (float) w, (float) h)];
	}
}

void NSViewComponentPeer::performAnyPendingRepaintsNow()
{
	[view displayIfNeeded];
}

ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
{
	return new NSViewComponentPeer (this, styleFlags, (NSView*) windowToAttachTo);
}

Image* juce_createIconForFile (const File& file)
{
	const ScopedAutoReleasePool pool;

	NSImage* image = [[NSWorkspace sharedWorkspace] iconForFile: juceStringToNS (file.getFullPathName())];

	CoreGraphicsImage* result = new CoreGraphicsImage (Image::ARGB, (int) [image size].width, (int) [image size].height, true);

	[NSGraphicsContext saveGraphicsState];
	[NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort: result->context flipped: false]];

	[image drawAtPoint: NSMakePoint (0, 0)
			  fromRect: NSMakeRect (0, 0, [image size].width, [image size].height)
			 operation: NSCompositeSourceOver fraction: 1.0f];

	[[NSGraphicsContext currentContext] flushGraphics];
	[NSGraphicsContext restoreGraphicsState];

	return result;
}

const int KeyPress::spaceKey	= ' ';
const int KeyPress::returnKey	   = 0x0d;
const int KeyPress::escapeKey	   = 0x1b;
const int KeyPress::backspaceKey	= 0x7f;
const int KeyPress::leftKey	 = NSLeftArrowFunctionKey;
const int KeyPress::rightKey	= NSRightArrowFunctionKey;
const int KeyPress::upKey	   = NSUpArrowFunctionKey;
const int KeyPress::downKey	 = NSDownArrowFunctionKey;
const int KeyPress::pageUpKey	   = NSPageUpFunctionKey;
const int KeyPress::pageDownKey	 = NSPageDownFunctionKey;
const int KeyPress::endKey	  = NSEndFunctionKey;
const int KeyPress::homeKey	 = NSHomeFunctionKey;
const int KeyPress::deleteKey	   = NSDeleteFunctionKey;
const int KeyPress::insertKey	   = -1;
const int KeyPress::tabKey	  = 9;
const int KeyPress::F1Key	   = NSF1FunctionKey;
const int KeyPress::F2Key	   = NSF2FunctionKey;
const int KeyPress::F3Key	   = NSF3FunctionKey;
const int KeyPress::F4Key	   = NSF4FunctionKey;
const int KeyPress::F5Key	   = NSF5FunctionKey;
const int KeyPress::F6Key	   = NSF6FunctionKey;
const int KeyPress::F7Key	   = NSF7FunctionKey;
const int KeyPress::F8Key	   = NSF8FunctionKey;
const int KeyPress::F9Key	   = NSF9FunctionKey;
const int KeyPress::F10Key	  = NSF10FunctionKey;
const int KeyPress::F11Key	  = NSF1FunctionKey;
const int KeyPress::F12Key	  = NSF12FunctionKey;
const int KeyPress::F13Key	  = NSF13FunctionKey;
const int KeyPress::F14Key	  = NSF14FunctionKey;
const int KeyPress::F15Key	  = NSF15FunctionKey;
const int KeyPress::F16Key	  = NSF16FunctionKey;
const int KeyPress::numberPad0	  = 0x30020;
const int KeyPress::numberPad1	  = 0x30021;
const int KeyPress::numberPad2	  = 0x30022;
const int KeyPress::numberPad3	  = 0x30023;
const int KeyPress::numberPad4	  = 0x30024;
const int KeyPress::numberPad5	  = 0x30025;
const int KeyPress::numberPad6	  = 0x30026;
const int KeyPress::numberPad7	  = 0x30027;
const int KeyPress::numberPad8	  = 0x30028;
const int KeyPress::numberPad9	  = 0x30029;
const int KeyPress::numberPadAdd		= 0x3002a;
const int KeyPress::numberPadSubtract	   = 0x3002b;
const int KeyPress::numberPadMultiply	   = 0x3002c;
const int KeyPress::numberPadDivide	 = 0x3002d;
const int KeyPress::numberPadSeparator	  = 0x3002e;
const int KeyPress::numberPadDecimalPoint   = 0x3002f;
const int KeyPress::numberPadEquals	 = 0x30030;
const int KeyPress::numberPadDelete	 = 0x30031;
const int KeyPress::playKey	 = 0x30000;
const int KeyPress::stopKey	 = 0x30001;
const int KeyPress::fastForwardKey  = 0x30002;
const int KeyPress::rewindKey	   = 0x30003;

#endif
/*** End of inlined file: juce_mac_NSViewComponentPeer.mm ***/


/*** Start of inlined file: juce_mac_MouseCursor.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if JUCE_MAC

Image* juce_loadPNGImageFromStream (InputStream& inputStream);

void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY)
{
	NSImage* im = CoreGraphicsImage::createNSImage (image);
	NSCursor* c = [[NSCursor alloc] initWithImage: im
										  hotSpot: NSMakePoint (hotspotX, hotspotY)];
	[im release];

	return c;
}

static void* juce_cursorFromData (const MemoryBlock& data, const float hx, const float hy)
{
	MemoryInputStream stream (data, false);
	ScopedPointer <Image> im (juce_loadPNGImageFromStream (stream));
	jassert (im != 0);

	if (im == 0)
		return 0;

	return juce_createMouseCursorFromImage (*im,
											(int) (hx * im->getWidth()),
											(int) (hy * im->getHeight()));
}

static void* juce_cursorFromWebKitFile (const char* filename, float hx, float hy)
{
	const File f ("/System/Library/Frameworks/WebKit.framework/Frameworks/WebCore.framework/Resources");

	MemoryBlock mb;
	if (f.getChildFile (filename).loadFileAsData (mb))
		return juce_cursorFromData (mb, hx, hy);

	return 0;
}

void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type)
{
	const ScopedAutoReleasePool pool;
	NSCursor* c = 0;

	switch (type)
	{
	case MouseCursor::NormalCursor:
		c = [NSCursor arrowCursor];
		break;

	case MouseCursor::NoCursor:
		return juce_createMouseCursorFromImage (Image (Image::ARGB, 8, 8, true), 0, 0);

	case MouseCursor::DraggingHandCursor:
		c = [NSCursor openHandCursor];
		break;

	case MouseCursor::CopyingCursor:
		return juce_cursorFromWebKitFile ("copyCursor.png", 0, 0);

	case MouseCursor::WaitCursor:
		c = [NSCursor arrowCursor]; // avoid this on the mac, let the OS provide the beachball
		break;
		//return juce_cursorFromWebKitFile ("waitCursor.png", 0.5f, 0.5f);

	case MouseCursor::IBeamCursor:
		c = [NSCursor IBeamCursor];
		break;

	case MouseCursor::PointingHandCursor:
		c = [NSCursor pointingHandCursor];
		break;

	case MouseCursor::LeftRightResizeCursor:
		c = [NSCursor resizeLeftRightCursor];
		break;

	case MouseCursor::LeftEdgeResizeCursor:
		c = [NSCursor resizeLeftCursor];
		break;

	case MouseCursor::RightEdgeResizeCursor:
		c = [NSCursor resizeRightCursor];
		break;

	case MouseCursor::UpDownResizeCursor:
	case MouseCursor::TopEdgeResizeCursor:
	case MouseCursor::BottomEdgeResizeCursor:
		return juce_cursorFromWebKitFile ("northSouthResizeCursor.png", 0.5f, 0.5f);

	case MouseCursor::TopLeftCornerResizeCursor:
	case MouseCursor::BottomRightCornerResizeCursor:
		return juce_cursorFromWebKitFile ("northWestSouthEastResizeCursor.png", 0.5f, 0.5f);

	case MouseCursor::TopRightCornerResizeCursor:
	case MouseCursor::BottomLeftCornerResizeCursor:
		return juce_cursorFromWebKitFile ("northEastSouthWestResizeCursor.png", 0.5f, 0.5f);

	case MouseCursor::UpDownLeftRightResizeCursor:
		return juce_cursorFromWebKitFile ("moveCursor.png", 0.5f, 0.5f);

	case MouseCursor::CrosshairCursor:
		c = [NSCursor crosshairCursor];
		break;
	}

	[c retain];
	return c;
}

void juce_deleteMouseCursor (void* const cursorHandle, const bool isStandard)
{
	NSCursor* c = (NSCursor*) cursorHandle;
	[c release];
}

void MouseCursor::showInAllWindows() const
{
	showInWindow (0);
}

void MouseCursor::showInWindow (ComponentPeer*) const
{
	NSCursor* const c = (NSCursor*) getHandle();
	[c set];
}

#else

void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY)	  { return 0; }
void* juce_createStandardMouseCursor (MouseCursor::StandardCursorType type)			 { return 0; }
void juce_deleteMouseCursor (void* const cursorHandle, const bool isStandard)		   {}
void MouseCursor::showInAllWindows() const							  {}
void MouseCursor::showInWindow (ComponentPeer*) const					   {}

#endif

#endif
/*** End of inlined file: juce_mac_MouseCursor.mm ***/


/*** Start of inlined file: juce_mac_NSViewComponent.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class NSViewComponentInternal  : public ComponentMovementWatcher
{
	Component* const owner;
	NSViewComponentPeer* currentPeer;
	bool wasShowing;

public:
	NSView* const view;

	NSViewComponentInternal (NSView* const view_, Component* const owner_)
		: ComponentMovementWatcher (owner_),
		  owner (owner_),
		  currentPeer (0),
		  wasShowing (false),
		  view (view_)
	{
		[view_ retain];

		if (owner_->isShowing())
			componentPeerChanged();
	}

	~NSViewComponentInternal()
	{
		[view removeFromSuperview];
		[view release];
	}

	void componentMovedOrResized (Component& comp, bool wasMoved, bool wasResized)
	{
		ComponentMovementWatcher::componentMovedOrResized (comp, wasMoved, wasResized);

		// The ComponentMovementWatcher version of this method avoids calling
		// us when the top-level comp is resized, but for an NSView we need to know this
		// because with inverted co-ords, we need to update the position even if the
		// top-left pos hasn't changed
		if (comp.isOnDesktop() && wasResized)
			componentMovedOrResized (wasMoved, wasResized);
	}

	void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
	{
		Component* const topComp = owner->getTopLevelComponent();

		if (topComp->getPeer() != 0)
		{
			const Point<int> pos (owner->relativePositionToOtherComponent (topComp, Point<int>()));

			NSRect r;
			r.origin.x = (float) pos.getX();
			r.origin.y = (float) pos.getY();
			r.size.width = (float) owner->getWidth();
			r.size.height = (float) owner->getHeight();
			r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);

			[view setFrame: r];
		}
	}

	void componentPeerChanged()
	{
		NSViewComponentPeer* const peer = dynamic_cast <NSViewComponentPeer*> (owner->getPeer());

		if (currentPeer != peer)
		{
			[view removeFromSuperview];
			currentPeer = peer;

			if (peer != 0)
			{
				[peer->view addSubview: view];
				componentMovedOrResized (false, false);
			}
		}

		[view setHidden: ! owner->isShowing()];
	}

	void componentVisibilityChanged (Component&)
	{
		componentPeerChanged();
	}

	juce_UseDebuggingNewOperator

private:
	NSViewComponentInternal (const NSViewComponentInternal&);
	NSViewComponentInternal& operator= (const NSViewComponentInternal&);
};

NSViewComponent::NSViewComponent()
{
}

NSViewComponent::~NSViewComponent()
{
}

void NSViewComponent::setView (void* view)
{
	if (view != getView())
	{
		if (view != 0)
			info = new NSViewComponentInternal ((NSView*) view, this);
		else
			info = 0;
	}
}

void* NSViewComponent::getView() const
{
	return info == 0 ? 0 : info->view;
}

void NSViewComponent::paint (Graphics& g)
{
}

#endif
/*** End of inlined file: juce_mac_NSViewComponent.mm ***/


/*** Start of inlined file: juce_mac_AppleRemote.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

AppleRemoteDevice::AppleRemoteDevice()
	: device (0),
	  queue (0),
	  remoteId (0)
{
}

AppleRemoteDevice::~AppleRemoteDevice()
{
	stop();
}

static io_object_t getAppleRemoteDevice()
{
	CFMutableDictionaryRef dict = IOServiceMatching ("AppleIRController");

	io_iterator_t iter = 0;
	io_object_t iod = 0;

	if (IOServiceGetMatchingServices (kIOMasterPortDefault, dict, &iter) == kIOReturnSuccess
		 && iter != 0)
	{
		iod = IOIteratorNext (iter);
	}

	IOObjectRelease (iter);
	return iod;
}

static bool createAppleRemoteInterface (io_object_t iod, void** device)
{
	jassert (*device == 0);
	io_name_t classname;

	if (IOObjectGetClass (iod, classname) == kIOReturnSuccess)
	{
		IOCFPlugInInterface** cfPlugInInterface = 0;
		SInt32 score = 0;

		if (IOCreatePlugInInterfaceForService (iod,
											   kIOHIDDeviceUserClientTypeID,
											   kIOCFPlugInInterfaceID,
											   &cfPlugInInterface,
											   &score) == kIOReturnSuccess)
		{
			HRESULT hr = (*cfPlugInInterface)->QueryInterface (cfPlugInInterface,
															   CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID),
															   device);

			(void) hr;

			(*cfPlugInInterface)->Release (cfPlugInInterface);
		}
	}

	return *device != 0;
}

bool AppleRemoteDevice::start (const bool inExclusiveMode)
{
	if (queue != 0)
		return true;

	stop();

	bool result = false;
	io_object_t iod = getAppleRemoteDevice();

	if (iod != 0)
	{
		if (createAppleRemoteInterface (iod, &device) && open (inExclusiveMode))
			result = true;
		else
			stop();

		IOObjectRelease (iod);
	}

	return result;
}

void AppleRemoteDevice::stop()
{
	if (queue != 0)
	{
		(*(IOHIDQueueInterface**) queue)->stop ((IOHIDQueueInterface**) queue);
		(*(IOHIDQueueInterface**) queue)->dispose ((IOHIDQueueInterface**) queue);
		(*(IOHIDQueueInterface**) queue)->Release ((IOHIDQueueInterface**) queue);
		queue = 0;
	}

	if (device != 0)
	{
		(*(IOHIDDeviceInterface**) device)->close ((IOHIDDeviceInterface**) device);
		(*(IOHIDDeviceInterface**) device)->Release ((IOHIDDeviceInterface**) device);
		device = 0;
	}
}

bool AppleRemoteDevice::isActive() const
{
	return queue != 0;
}

static void appleRemoteQueueCallback (void* const target, const IOReturn result, void*, void*)
{
	if (result == kIOReturnSuccess)
		((AppleRemoteDevice*) target)->handleCallbackInternal();
}

bool AppleRemoteDevice::open (const bool openInExclusiveMode)
{
	Array <int> cookies;

	CFArrayRef elements;
	IOHIDDeviceInterface122** const device122 = (IOHIDDeviceInterface122**) device;

	if ((*device122)->copyMatchingElements (device122, 0, &elements) != kIOReturnSuccess)
		return false;

	for (int i = 0; i < CFArrayGetCount (elements); ++i)
	{
		CFDictionaryRef element = (CFDictionaryRef) CFArrayGetValueAtIndex (elements, i);

		// get the cookie
		CFTypeRef object = CFDictionaryGetValue (element, CFSTR (kIOHIDElementCookieKey));

		if (object == 0 || CFGetTypeID (object) != CFNumberGetTypeID())
			continue;

		long number;
		if (! CFNumberGetValue ((CFNumberRef) object, kCFNumberLongType, &number))
			continue;

		cookies.add ((int) number);
	}

	CFRelease (elements);

	if ((*(IOHIDDeviceInterface**) device)
			->open ((IOHIDDeviceInterface**) device,
					openInExclusiveMode ? kIOHIDOptionsTypeSeizeDevice
										: kIOHIDOptionsTypeNone) == KERN_SUCCESS)
	{
		queue = (*(IOHIDDeviceInterface**) device)->allocQueue ((IOHIDDeviceInterface**) device);

		if (queue != 0)
		{
			(*(IOHIDQueueInterface**) queue)->create ((IOHIDQueueInterface**) queue, 0, 12);

			for (int i = 0; i < cookies.size(); ++i)
			{
				IOHIDElementCookie cookie = (IOHIDElementCookie) cookies.getUnchecked(i);
				(*(IOHIDQueueInterface**) queue)->addElement ((IOHIDQueueInterface**) queue, cookie, 0);
			}

			CFRunLoopSourceRef eventSource;

			if ((*(IOHIDQueueInterface**) queue)
					->createAsyncEventSource ((IOHIDQueueInterface**) queue, &eventSource) == KERN_SUCCESS)
			{
				if ((*(IOHIDQueueInterface**) queue)->setEventCallout ((IOHIDQueueInterface**) queue,
																	   appleRemoteQueueCallback, this, 0) == KERN_SUCCESS)
				{
					CFRunLoopAddSource (CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);

					(*(IOHIDQueueInterface**) queue)->start ((IOHIDQueueInterface**) queue);

					return true;
				}
			}
		}
	}

	return false;
}

void AppleRemoteDevice::handleCallbackInternal()
{
	int totalValues = 0;
	AbsoluteTime nullTime = { 0, 0 };
	char cookies [12];
	int numCookies = 0;

	while (numCookies < numElementsInArray (cookies))
	{
		IOHIDEventStruct e;

		if ((*(IOHIDQueueInterface**) queue)->getNextEvent ((IOHIDQueueInterface**) queue, &e, nullTime, 0) != kIOReturnSuccess)
			break;

		if ((int) e.elementCookie == 19)
		{
			remoteId = e.value;
			buttonPressed (switched, false);
		}
		else
		{
			totalValues += e.value;
			cookies [numCookies++] = (char) (pointer_sized_int) e.elementCookie;
		}
	}

	cookies [numCookies++] = 0;
	//DBG (String::toHexString ((uint8*) cookies, numCookies, 1) + " "  + String (totalValues));

	static const char buttonPatterns[] =
	{
		0x1f, 0x14, 0x12, 0x1f, 0x14, 0x12, 0,
		0x1f, 0x15, 0x12, 0x1f, 0x15, 0x12, 0,
		0x1f, 0x1d, 0x1c, 0x12, 0,
		0x1f, 0x1e, 0x1c, 0x12, 0,
		0x1f, 0x16, 0x12, 0x1f, 0x16, 0x12, 0,
		0x1f, 0x17, 0x12, 0x1f, 0x17, 0x12, 0,
		0x1f, 0x12, 0x04, 0x02, 0,
		0x1f, 0x12, 0x03, 0x02, 0,
		0x1f, 0x12, 0x1f, 0x12, 0,
		0x23, 0x1f, 0x12, 0x23, 0x1f, 0x12, 0,
		19, 0
	};

	int buttonNum = (int) menuButton;
	int i = 0;

	while (i < numElementsInArray (buttonPatterns))
	{
		if (strcmp (cookies, buttonPatterns + i) == 0)
		{
			buttonPressed ((ButtonType) buttonNum, totalValues > 0);
			break;
		}

		i += (int) strlen (buttonPatterns + i) + 1;
		++buttonNum;
	}
}

#endif
/*** End of inlined file: juce_mac_AppleRemote.mm ***/


/*** Start of inlined file: juce_mac_OpenGLComponent.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_OPENGL

#if JUCE_MAC

END_JUCE_NAMESPACE

#define ThreadSafeNSOpenGLView MakeObjCClassName(ThreadSafeNSOpenGLView)

@interface ThreadSafeNSOpenGLView  : NSOpenGLView
{
	CriticalSection* contextLock;
	bool needsUpdate;
}

- (id) initWithFrame: (NSRect) frameRect pixelFormat: (NSOpenGLPixelFormat*) format;
- (bool) makeActive;
- (void) makeInactive;
- (void) reshape;
@end

@implementation ThreadSafeNSOpenGLView

- (id) initWithFrame: (NSRect) frameRect
		 pixelFormat: (NSOpenGLPixelFormat*) format
{
	contextLock = new CriticalSection();
	self = [super initWithFrame: frameRect pixelFormat: format];

	if (self != nil)
		[[NSNotificationCenter defaultCenter] addObserver: self
												 selector: @selector (_surfaceNeedsUpdate:)
													 name: NSViewGlobalFrameDidChangeNotification
												   object: self];
	return self;
}

- (void) dealloc
{
	[[NSNotificationCenter defaultCenter] removeObserver: self];
	delete contextLock;
	[super dealloc];
}

- (bool) makeActive
{
	const ScopedLock sl (*contextLock);

	if ([self openGLContext] == 0)
		return false;

	[[self openGLContext] makeCurrentContext];

	if (needsUpdate)
	{
		[super update];
		needsUpdate = false;
	}

	return true;
}

- (void) makeInactive
{
	const ScopedLock sl (*contextLock);
	[NSOpenGLContext clearCurrentContext];
}

- (void) _surfaceNeedsUpdate: (NSNotification*) notification
{
	const ScopedLock sl (*contextLock);
	needsUpdate = true;
}

- (void) update
{
	const ScopedLock sl (*contextLock);
	needsUpdate = true;
}

- (void) reshape
{
	const ScopedLock sl (*contextLock);
	needsUpdate = true;
}

@end
BEGIN_JUCE_NAMESPACE

class WindowedGLContext	 : public OpenGLContext
{
public:
	WindowedGLContext (Component* const component,
					   const OpenGLPixelFormat& pixelFormat_,
					   NSOpenGLContext* sharedContext)
		: renderContext (0),
		  pixelFormat (pixelFormat_)
	{
		jassert (component != 0);

		NSOpenGLPixelFormatAttribute attribs [64];
		int n = 0;
		attribs[n++] = NSOpenGLPFADoubleBuffer;
		attribs[n++] = NSOpenGLPFAAccelerated;
		attribs[n++] = NSOpenGLPFAMPSafe; // NSOpenGLPFAAccelerated, NSOpenGLPFAMultiScreen, NSOpenGLPFASingleRenderer
		attribs[n++] = NSOpenGLPFAColorSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) jmax (pixelFormat.redBits,
															pixelFormat.greenBits,
															pixelFormat.blueBits);
		attribs[n++] = NSOpenGLPFAAlphaSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.alphaBits;
		attribs[n++] = NSOpenGLPFADepthSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.depthBufferBits;
		attribs[n++] = NSOpenGLPFAStencilSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.stencilBufferBits;
		attribs[n++] = NSOpenGLPFAAccumSize;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) jmax (pixelFormat.accumulationBufferRedBits,
															pixelFormat.accumulationBufferGreenBits,
															pixelFormat.accumulationBufferBlueBits,
															pixelFormat.accumulationBufferAlphaBits);

		// xxx not sure how to do fullSceneAntiAliasingNumSamples..
		attribs[n++] = NSOpenGLPFASampleBuffers;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) 1;
		attribs[n++] = NSOpenGLPFAClosestPolicy;
		attribs[n++] = NSOpenGLPFANoRecovery;
		attribs[n++] = (NSOpenGLPixelFormatAttribute) 0;

		NSOpenGLPixelFormat* format
			= [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];

		view = [[ThreadSafeNSOpenGLView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
												 pixelFormat: format];

		renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
													shareContext: sharedContext] autorelease];

		const GLint swapInterval = 1;
		[renderContext setValues: &swapInterval forParameter: NSOpenGLCPSwapInterval];

		[view setOpenGLContext: renderContext];
		[renderContext setView: view];

		[format release];

		viewHolder = new NSViewComponentInternal (view, component);
	}

	~WindowedGLContext()
	{
		makeInactive();
		[renderContext clearDrawable];
		viewHolder = 0;
	}

	bool makeActive() const throw()
	{
		jassert (renderContext != 0);
		[view makeActive];
		return isActive();
	}

	bool makeInactive() const throw()
	{
		[view makeInactive];
		return true;
	}

	bool isActive() const throw()
	{
		return [NSOpenGLContext currentContext] == renderContext;
	}

	const OpenGLPixelFormat getPixelFormat() const  { return pixelFormat; }
	void* getRawContext() const throw()		 { return renderContext; }

	void updateWindowPosition (int x, int y, int w, int h, int outerWindowHeight)
	{
	}

	void swapBuffers()
	{
		[renderContext flushBuffer];
	}

	bool setSwapInterval (const int numFramesPerSwap)
	{
		[renderContext setValues: (const GLint*) &numFramesPerSwap
					forParameter: NSOpenGLCPSwapInterval];
		return true;
	}

	int getSwapInterval() const
	{
		GLint numFrames = 0;
		[renderContext getValues: &numFrames
					forParameter: NSOpenGLCPSwapInterval];
		return numFrames;
	}

	void repaint()
	{
		// we need to invalidate the juce view that holds this gl view, to make it
		// cause a repaint callback
		NSView* v = (NSView*) viewHolder->view;
		NSRect r = [v frame];

		// bit of a bodge here.. if we only invalidate the area of the gl component,
		// it's completely covered by the NSOpenGLView, so the OS throws away the
		// repaint message, thus never causing our paint() callback, and never repainting
		// the comp. So invalidating just a little bit around the edge helps..
		[[v superview] setNeedsDisplayInRect: NSInsetRect (r, -2.0f, -2.0f)];
	}

	void* getNativeWindowHandle() const	 { return viewHolder->view; }

	juce_UseDebuggingNewOperator

	NSOpenGLContext* renderContext;
	ThreadSafeNSOpenGLView* view;

private:
	OpenGLPixelFormat pixelFormat;
	ScopedPointer <NSViewComponentInternal> viewHolder;

	WindowedGLContext (const WindowedGLContext&);
	WindowedGLContext& operator= (const WindowedGLContext&);
};

OpenGLContext* OpenGLComponent::createContext()
{
	ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
															   contextToShareListsWith != 0 ? (NSOpenGLContext*) contextToShareListsWith->getRawContext() : 0));

	return (c->renderContext != 0) ? c.release() : 0;
}

void* OpenGLComponent::getNativeWindowHandle() const
{
	return context != 0 ? static_cast<WindowedGLContext*> (static_cast<OpenGLContext*> (context))->getNativeWindowHandle()
						: 0;
}

void juce_glViewport (const int w, const int h)
{
	glViewport (0, 0, w, h);
}

void OpenGLPixelFormat::getAvailablePixelFormats (Component* /*component*/,
												  OwnedArray <OpenGLPixelFormat>& results)
{
/*	GLint attribs [64];
	int n = 0;
	attribs[n++] = AGL_RGBA;
	attribs[n++] = AGL_DOUBLEBUFFER;
	attribs[n++] = AGL_ACCELERATED;
	attribs[n++] = AGL_NO_RECOVERY;
	attribs[n++] = AGL_NONE;

	AGLPixelFormat p = aglChoosePixelFormat (0, 0, attribs);

	while (p != 0)
	{
		OpenGLPixelFormat* const pf = new OpenGLPixelFormat();
		pf->redBits = getAGLAttribute (p, AGL_RED_SIZE);
		pf->greenBits = getAGLAttribute (p, AGL_GREEN_SIZE);
		pf->blueBits = getAGLAttribute (p, AGL_BLUE_SIZE);
		pf->alphaBits = getAGLAttribute (p, AGL_ALPHA_SIZE);
		pf->depthBufferBits = getAGLAttribute (p, AGL_DEPTH_SIZE);
		pf->stencilBufferBits = getAGLAttribute (p, AGL_STENCIL_SIZE);
		pf->accumulationBufferRedBits = getAGLAttribute (p, AGL_ACCUM_RED_SIZE);
		pf->accumulationBufferGreenBits = getAGLAttribute (p, AGL_ACCUM_GREEN_SIZE);
		pf->accumulationBufferBlueBits = getAGLAttribute (p, AGL_ACCUM_BLUE_SIZE);
		pf->accumulationBufferAlphaBits = getAGLAttribute (p, AGL_ACCUM_ALPHA_SIZE);

		results.add (pf);

		p = aglNextPixelFormat (p);
	}*/

	//jassertfalse  //xxx can't see how you do this in cocoa!
}

#else

END_JUCE_NAMESPACE

@interface JuceGLView   : UIView
{
}
+ (Class) layerClass;
@end

@implementation JuceGLView
+ (Class) layerClass
{
	return [CAEAGLLayer class];
}
@end

BEGIN_JUCE_NAMESPACE

class GLESContext   : public OpenGLContext
{
public:
	GLESContext (UIViewComponentPeer* peer,
				 Component* const component_,
				 const OpenGLPixelFormat& pixelFormat_,
				 const GLESContext* const sharedContext,
				 NSUInteger apiType)
		: component (component_), pixelFormat (pixelFormat_), glLayer (0), context (0),
		  useDepthBuffer (pixelFormat_.depthBufferBits > 0), frameBufferHandle (0), colorBufferHandle (0),
		  depthBufferHandle (0), lastWidth (0), lastHeight (0)
	{
		view = [[JuceGLView alloc] initWithFrame: CGRectMake (0, 0, 64, 64)];
		view.opaque = YES;
		view.hidden = NO;
		view.backgroundColor = [UIColor blackColor];
		view.userInteractionEnabled = NO;

		glLayer = (CAEAGLLayer*) [view layer];
		[peer->view addSubview: view];

		if (sharedContext != 0)
			context = [[EAGLContext alloc] initWithAPI: apiType
											sharegroup: [sharedContext->context sharegroup]];
		else
			context = [[EAGLContext alloc] initWithAPI: apiType];

		createGLBuffers();
	}

	~GLESContext()
	{
		makeInactive();
		[context release];
		[view removeFromSuperview];
		[view release];
		freeGLBuffers();
	}

	bool makeActive() const throw()
	{
		jassert (context != 0);

		[EAGLContext setCurrentContext: context];
		glBindFramebufferOES (GL_FRAMEBUFFER_OES, frameBufferHandle);
		return true;
	}

	void swapBuffers()
	{
		glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);
		[context presentRenderbuffer: GL_RENDERBUFFER_OES];
	}

	bool makeInactive() const throw()
	{
		return [EAGLContext setCurrentContext: nil];
	}

	bool isActive() const throw()
	{
		return [EAGLContext currentContext] == context;
	}

	const OpenGLPixelFormat getPixelFormat() const  { return pixelFormat; }
	void* getRawContext() const throw()		 { return glLayer; }

	void updateWindowPosition (int x, int y, int w, int h, int outerWindowHeight)
	{
		view.frame = CGRectMake ((CGFloat) x, (CGFloat) y, (CGFloat) w, (CGFloat) h);

		if (lastWidth != w || lastHeight != h)
		{
			lastWidth = w;
			lastHeight = h;
			freeGLBuffers();
			createGLBuffers();
		}
	}

	bool setSwapInterval (const int numFramesPerSwap)
	{
		numFrames = numFramesPerSwap;
		return true;
	}

	int getSwapInterval() const
	{
		return numFrames;
	}

	void repaint()
	{
	}

	void createGLBuffers()
	{
		makeActive();

		glGenFramebuffersOES (1, &frameBufferHandle);
		glGenRenderbuffersOES (1, &colorBufferHandle);
		glGenRenderbuffersOES (1, &depthBufferHandle);

		glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);
		[context renderbufferStorage: GL_RENDERBUFFER_OES fromDrawable: glLayer];

		GLint width, height;
		glGetRenderbufferParameterivOES (GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &width);
		glGetRenderbufferParameterivOES (GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &height);

		if (useDepthBuffer)
		{
			glBindRenderbufferOES (GL_RENDERBUFFER_OES, depthBufferHandle);
			glRenderbufferStorageOES (GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, width, height);
		}

		glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);

		glBindFramebufferOES (GL_FRAMEBUFFER_OES, frameBufferHandle);
		glFramebufferRenderbufferOES (GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorBufferHandle);

		if (useDepthBuffer)
			glFramebufferRenderbufferOES (GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBufferHandle);

		jassert (glCheckFramebufferStatusOES (GL_FRAMEBUFFER_OES) == GL_FRAMEBUFFER_COMPLETE_OES);
	}

	void freeGLBuffers()
	{
		if (frameBufferHandle != 0)
		{
			glDeleteFramebuffersOES (1, &frameBufferHandle);
			frameBufferHandle = 0;
		}

		if (colorBufferHandle != 0)
		{
			glDeleteRenderbuffersOES (1, &colorBufferHandle);
			colorBufferHandle = 0;
		}

		if (depthBufferHandle != 0)
		{
			glDeleteRenderbuffersOES (1, &depthBufferHandle);
			depthBufferHandle = 0;
		}
	}

	juce_UseDebuggingNewOperator

private:
	Component::SafePointer<Component> component;
	OpenGLPixelFormat pixelFormat;
	JuceGLView* view;
	CAEAGLLayer* glLayer;
	EAGLContext* context;
	bool useDepthBuffer;
	GLuint frameBufferHandle, colorBufferHandle, depthBufferHandle;
	int numFrames;
	int lastWidth, lastHeight;

	GLESContext (const GLESContext&);
	GLESContext& operator= (const GLESContext&);
};

OpenGLContext* OpenGLComponent::createContext()
{
	ScopedAutoReleasePool pool;
	UIViewComponentPeer* peer = dynamic_cast <UIViewComponentPeer*> (getPeer());

	if (peer != 0)
		return new GLESContext (peer, this, preferredPixelFormat,
								dynamic_cast <const GLESContext*> (contextToShareListsWith),
								type == openGLES2 ? kEAGLRenderingAPIOpenGLES2 : kEAGLRenderingAPIOpenGLES1);

	return 0;
}

void OpenGLPixelFormat::getAvailablePixelFormats (Component* /*component*/,
												  OwnedArray <OpenGLPixelFormat>& /*results*/)
{
}

void juce_glViewport (const int w, const int h)
{
	glViewport (0, 0, w, h);
}

#endif

#endif
/*** End of inlined file: juce_mac_OpenGLComponent.mm ***/


/*** Start of inlined file: juce_mac_MainMenu.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class JuceMainMenuHandler;

END_JUCE_NAMESPACE
using namespace JUCE_NAMESPACE;

#define JuceMenuCallback MakeObjCClassName(JuceMenuCallback)

#if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
@interface JuceMenuCallback  : NSObject <NSMenuDelegate>
#else
@interface JuceMenuCallback  : NSObject
#endif
{
	JuceMainMenuHandler* owner;
}

- (JuceMenuCallback*) initWithOwner: (JuceMainMenuHandler*) owner_;
- (void) dealloc;
- (void) menuItemInvoked: (id) menu;
- (void) menuNeedsUpdate: (NSMenu*) menu;
@end
BEGIN_JUCE_NAMESPACE

class JuceMainMenuHandler   : private MenuBarModelListener,
							  private DeletedAtShutdown
{
public:
	static JuceMainMenuHandler* instance;

	JuceMainMenuHandler()
		: currentModel (0),
		  lastUpdateTime (0)
	{
		callback = [[JuceMenuCallback alloc] initWithOwner: this];
	}

	~JuceMainMenuHandler()
	{
		setMenu (0);

		jassert (instance == this);
		instance = 0;

		[callback release];
	}

	void setMenu (MenuBarModel* const newMenuBarModel)
	{
		if (currentModel != newMenuBarModel)
		{
			if (currentModel != 0)
				currentModel->removeListener (this);

			currentModel = newMenuBarModel;

			if (currentModel != 0)
				currentModel->addListener (this);

			menuBarItemsChanged (0);
		}
	}

	void addSubMenu (NSMenu* parent, const PopupMenu& child,
					 const String& name, const int menuId, const int tag)
	{
		NSMenuItem* item = [parent addItemWithTitle: juceStringToNS (name)
											 action: nil
									  keyEquivalent: @""];
		[item setTag: tag];

		NSMenu* sub = createMenu (child, name, menuId, tag);

		[parent setSubmenu: sub forItem: item];
		[sub setAutoenablesItems: false];
		[sub release];
	}

	void updateSubMenu (NSMenuItem* parentItem, const PopupMenu& menuToCopy,
						const String& name, const int menuId, const int tag)
	{
		[parentItem setTag: tag];
		NSMenu* menu = [parentItem submenu];

		[menu setTitle: juceStringToNS (name)];

		while ([menu numberOfItems] > 0)
			[menu removeItemAtIndex: 0];

		PopupMenu::MenuItemIterator iter (menuToCopy);

		while (iter.next())
			addMenuItem (iter, menu, menuId, tag);

		[menu setAutoenablesItems: false];
		[menu update];
	}

	void menuBarItemsChanged (MenuBarModel*)
	{
		lastUpdateTime = Time::getMillisecondCounter();

		StringArray menuNames;
		if (currentModel != 0)
			menuNames = currentModel->getMenuBarNames();

		NSMenu* menuBar = [NSApp mainMenu];
		while ([menuBar numberOfItems] > 1 + menuNames.size())
			[menuBar removeItemAtIndex: [menuBar numberOfItems] - 1];

		int menuId = 1;

		for (int i = 0; i < menuNames.size(); ++i)
		{
			const PopupMenu menu (currentModel->getMenuForIndex (i, menuNames [i]));

			if (i >= [menuBar numberOfItems] - 1)
				addSubMenu (menuBar, menu, menuNames[i], menuId, i);
			else
				updateSubMenu ([menuBar itemAtIndex: 1 + i], menu, menuNames[i], menuId, i);
		}
	}

	static void flashMenuBar (NSMenu* menu)
	{
		if ([[menu title] isEqualToString: @"Apple"])
			return;

		[menu retain];

		const unichar f35Key = NSF35FunctionKey;
		NSString* f35String = [NSString stringWithCharacters: &f35Key length: 1];

		NSMenuItem* item = [[NSMenuItem alloc] initWithTitle: @"x"
													  action: nil
											   keyEquivalent: f35String];
		[item setTarget: nil];
		[menu insertItem: item atIndex: [menu numberOfItems]];
		[item release];

		if ([menu indexOfItem: item] >= 0)
		{
			NSEvent* f35Event = [NSEvent keyEventWithType: NSKeyDown
												 location: NSZeroPoint
											modifierFlags: NSCommandKeyMask
												timestamp: 0
											 windowNumber: 0
												  context: [NSGraphicsContext currentContext]
											   characters: f35String
							  charactersIgnoringModifiers: f35String
												isARepeat: NO
												  keyCode: 0];

			[menu performKeyEquivalent: f35Event];

			if ([menu indexOfItem: item] >= 0)
				[menu removeItem: item]; // (this throws if the item isn't actually in the menu)
		}

		[menu release];
	}

	static NSMenuItem* findMenuItem (NSMenu* const menu, const ApplicationCommandTarget::InvocationInfo& info)
	{
		for (NSInteger i = [menu numberOfItems]; --i >= 0;)
		{
			NSMenuItem* m = [menu itemAtIndex: i];
			if ([m tag] == info.commandID)
				return m;

			if ([m submenu] != 0)
			{
				NSMenuItem* found = findMenuItem ([m submenu], info);
				if (found != 0)
					return found;
			}
		}

		return 0;
	}

	void menuCommandInvoked (MenuBarModel*, const ApplicationCommandTarget::InvocationInfo& info)
	{
		NSMenuItem* item = findMenuItem ([NSApp mainMenu], info);

		if (item != 0)
			flashMenuBar ([item menu]);
	}

	void updateMenus()
	{
		if (Time::getMillisecondCounter() > lastUpdateTime + 500)
			menuBarItemsChanged (0);
	}

	void invoke (const int commandId, ApplicationCommandManager* const commandManager, const int topLevelIndex) const
	{
		if (currentModel != 0)
		{
			if (commandManager != 0)
			{
				ApplicationCommandTarget::InvocationInfo info (commandId);
				info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromMenu;

				commandManager->invoke (info, true);
			}

			currentModel->menuItemSelected (commandId, topLevelIndex);
		}
	}

	MenuBarModel* currentModel;
	uint32 lastUpdateTime;

	void addMenuItem (PopupMenu::MenuItemIterator& iter, NSMenu* menuToAddTo,
					  const int topLevelMenuId, const int topLevelIndex)
	{
		NSString* text = juceStringToNS (iter.itemName.upToFirstOccurrenceOf ("<end>", false, true));

		if (text == 0)
			text = @"";

		if (iter.isSeparator)
		{
			[menuToAddTo addItem: [NSMenuItem separatorItem]];
		}
		else if (iter.isSectionHeader)
		{
			NSMenuItem* item = [menuToAddTo addItemWithTitle: text
													  action: nil
											   keyEquivalent: @""];

			[item setEnabled: false];
		}
		else if (iter.subMenu != 0)
		{
			NSMenuItem* item = [menuToAddTo addItemWithTitle: text
													  action: nil
											   keyEquivalent: @""];

			[item setTag: iter.itemId];
			[item setEnabled: iter.isEnabled];

			NSMenu* sub = createMenu (*iter.subMenu, iter.itemName, topLevelMenuId, topLevelIndex);
			[sub setDelegate: nil];
			[menuToAddTo setSubmenu: sub forItem: item];
			[sub release];
		}
		else
		{
			NSMenuItem* item = [menuToAddTo addItemWithTitle: text
													  action: @selector (menuItemInvoked:)
											   keyEquivalent: @""];

			[item setTag: iter.itemId];
			[item setEnabled: iter.isEnabled];
			[item setState: iter.isTicked ? NSOnState : NSOffState];
			[item setTarget: (id) callback];

			NSMutableArray* info = [NSMutableArray arrayWithObject: [NSNumber numberWithUnsignedLongLong: (pointer_sized_int) (void*) iter.commandManager]];
			[info addObject: [NSNumber numberWithInt: topLevelIndex]];
			[item setRepresentedObject: info];

			if (iter.commandManager != 0)
			{
				const Array <KeyPress> keyPresses (iter.commandManager->getKeyMappings()
												   ->getKeyPressesAssignedToCommand (iter.itemId));

				if (keyPresses.size() > 0)
				{
					const KeyPress& kp = keyPresses.getReference(0);

					juce_wchar key = kp.getTextCharacter();

					if (kp.getKeyCode() == KeyPress::backspaceKey)
						key = NSBackspaceCharacter;
					else if (kp.getKeyCode() == KeyPress::deleteKey)
						key = NSDeleteCharacter;
					else if (key == 0)
						key = (juce_wchar) kp.getKeyCode();

					unsigned int mods = 0;
					if (kp.getModifiers().isShiftDown())
						mods |= NSShiftKeyMask;
					if (kp.getModifiers().isCtrlDown())
						mods |= NSControlKeyMask;
					if (kp.getModifiers().isAltDown())
						mods |= NSAlternateKeyMask;
					if (kp.getModifiers().isCommandDown())
						mods |= NSCommandKeyMask;

					[item setKeyEquivalent: juceStringToNS (String::charToString (key))];
					[item setKeyEquivalentModifierMask: mods];
				}
			}
		}
	}

	JuceMenuCallback* callback;
private:

	NSMenu* createMenu (const PopupMenu menu,
						const String& menuName,
						const int topLevelMenuId,
						const int topLevelIndex)
	{
		NSMenu* m = [[NSMenu alloc] initWithTitle: juceStringToNS (menuName)];

		[m setAutoenablesItems: false];
		[m setDelegate: callback];

		PopupMenu::MenuItemIterator iter (menu);

		while (iter.next())
			addMenuItem (iter, m, topLevelMenuId, topLevelIndex);

		[m update];
		return m;
	}
};

JuceMainMenuHandler* JuceMainMenuHandler::instance = 0;

END_JUCE_NAMESPACE
@implementation JuceMenuCallback

- (JuceMenuCallback*) initWithOwner: (JuceMainMenuHandler*) owner_
{
	[super init];
	owner = owner_;
	return self;
}

- (void) dealloc
{
	[super dealloc];
}

- (void) menuItemInvoked: (id) menu
{
	NSMenuItem* item = (NSMenuItem*) menu;

	if ([[item representedObject] isKindOfClass: [NSArray class]])
	{
		// If the menu is being triggered by a keypress, the OS will have picked it up before we had a chance to offer it to
		// our own components, which may have wanted to intercept it. So, rather than dispatching directly, we'll feed it back
		// into the focused component and let it trigger the menu item indirectly.
		NSEvent* e = [NSApp currentEvent];
		if ([e type] == NSKeyDown || [e type] == NSKeyUp)
		{
			if (JUCE_NAMESPACE::Component::getCurrentlyFocusedComponent() != 0)
			{
				JUCE_NAMESPACE::NSViewComponentPeer* peer = dynamic_cast <JUCE_NAMESPACE::NSViewComponentPeer*> (JUCE_NAMESPACE::Component::getCurrentlyFocusedComponent()->getPeer());

				if (peer != 0)
				{
					if ([e type] == NSKeyDown)
						peer->redirectKeyDown (e);
					else
						peer->redirectKeyUp (e);

					return;
				}
			}
		}

		NSArray* info = (NSArray*) [item representedObject];

		owner->invoke ((int) [item tag],
					   (ApplicationCommandManager*) (pointer_sized_int)
							[((NSNumber*) [info objectAtIndex: 0]) unsignedLongLongValue],
					   (int) [((NSNumber*) [info objectAtIndex: 1]) intValue]);
	}
}

- (void) menuNeedsUpdate: (NSMenu*) menu;
{
	if (JuceMainMenuHandler::instance != 0)
		JuceMainMenuHandler::instance->updateMenus();
}

@end
BEGIN_JUCE_NAMESPACE

static NSMenu* createStandardAppMenu (NSMenu* menu, const String& appName,
									  const PopupMenu* extraItems)
{
	if (extraItems != 0 && JuceMainMenuHandler::instance != 0 && extraItems->getNumItems() > 0)
	{
		PopupMenu::MenuItemIterator iter (*extraItems);

		while (iter.next())
			JuceMainMenuHandler::instance->addMenuItem (iter, menu, 0, -1);

		[menu addItem: [NSMenuItem separatorItem]];
	}

	NSMenuItem* item;

	// Services...
	item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (@"Services", nil)
									  action: nil  keyEquivalent: @""];
	[menu addItem: item];
	[item release];
	NSMenu* servicesMenu = [[NSMenu alloc] initWithTitle: @"Services"];
	[menu setSubmenu: servicesMenu forItem: item];
	[NSApp setServicesMenu: servicesMenu];
	[servicesMenu release];
	[menu addItem: [NSMenuItem separatorItem]];

	// Hide + Show stuff...
	item = [[NSMenuItem alloc] initWithTitle: juceStringToNS ("Hide " + appName)
									  action: @selector (hide:)  keyEquivalent: @"h"];
	[item setTarget: NSApp];
	[menu addItem: item];
	[item release];

	item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (@"Hide Others", nil)
						action: @selector (hideOtherApplications:)  keyEquivalent: @"h"];
	[item setKeyEquivalentModifierMask: NSCommandKeyMask | NSAlternateKeyMask];
	[item setTarget: NSApp];
	[menu addItem: item];
	[item release];

	item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (@"Show All", nil)
							action: @selector (unhideAllApplications:)  keyEquivalent: @""];
	[item setTarget: NSApp];
	[menu addItem: item];
	[item release];

	[menu addItem: [NSMenuItem separatorItem]];

	// Quit item....
	item = [[NSMenuItem alloc] initWithTitle: juceStringToNS ("Quit " + appName)
									  action: @selector (terminate:)  keyEquivalent: @"q"];

	[item setTarget: NSApp];
	[menu addItem: item];
	[item release];

	return menu;
}

// Since our app has no NIB, this initialises a standard app menu...
static void rebuildMainMenu (const PopupMenu* extraItems)
{
	// this can't be used in a plugin!
	jassert (JUCEApplication::getInstance() != 0);

	if (JUCEApplication::getInstance() != 0)
	{
		const ScopedAutoReleasePool pool;

		NSMenu* mainMenu = [[NSMenu alloc] initWithTitle: @"MainMenu"];
		NSMenuItem* item = [mainMenu addItemWithTitle: @"Apple" action: nil keyEquivalent: @""];

		NSMenu* appMenu = [[NSMenu alloc] initWithTitle: @"Apple"];

		[NSApp performSelector: @selector (setAppleMenu:) withObject: appMenu];
		[mainMenu setSubmenu: appMenu forItem: item];

		[NSApp setMainMenu: mainMenu];
		createStandardAppMenu (appMenu, JUCEApplication::getInstance()->getApplicationName(), extraItems);

		[appMenu release];
		[mainMenu release];
	}
}

void MenuBarModel::setMacMainMenu (MenuBarModel* newMenuBarModel,
								   const PopupMenu* extraAppleMenuItems)
{
	if (getMacMainMenu() != newMenuBarModel)
	{
		const ScopedAutoReleasePool pool;

		if (newMenuBarModel == 0)
		{
			delete JuceMainMenuHandler::instance;
			jassert (JuceMainMenuHandler::instance == 0); // should be zeroed in the destructor
			jassert (extraAppleMenuItems == 0); // you can't specify some extra items without also supplying a model

			extraAppleMenuItems = 0;
		}
		else
		{
			if (JuceMainMenuHandler::instance == 0)
				JuceMainMenuHandler::instance = new JuceMainMenuHandler();

			JuceMainMenuHandler::instance->setMenu (newMenuBarModel);
		}
	}

	rebuildMainMenu (extraAppleMenuItems);

	if (newMenuBarModel != 0)
		newMenuBarModel->menuItemsChanged();
}

MenuBarModel* MenuBarModel::getMacMainMenu()
{
	return JuceMainMenuHandler::instance != 0
			? JuceMainMenuHandler::instance->currentModel : 0;
}

void initialiseMainMenu()
{
	if (JUCEApplication::getInstance() != 0) // only needed in an app
		rebuildMainMenu (0);
}

#endif
/*** End of inlined file: juce_mac_MainMenu.mm ***/


/*** Start of inlined file: juce_mac_FileChooser.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if JUCE_MAC

END_JUCE_NAMESPACE
using namespace JUCE_NAMESPACE;

#define JuceFileChooserDelegate MakeObjCClassName(JuceFileChooserDelegate)

#if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
@interface JuceFileChooserDelegate   : NSObject <NSOpenSavePanelDelegate>
#else
@interface JuceFileChooserDelegate   : NSObject
#endif
{
	StringArray* filters;
}

- (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_;
- (void) dealloc;
- (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename;

@end

@implementation JuceFileChooserDelegate
- (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_
{
	[super init];
	filters = filters_;
	return self;
}

- (void) dealloc
{
	delete filters;
	[super dealloc];
}

- (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename
{
	const File f (nsStringToJuce (filename));

	for (int i = filters->size(); --i >= 0;)
		if (f.getFileName().matchesWildcard ((*filters)[i], true))
			return true;

	return f.isDirectory();
}
@end

BEGIN_JUCE_NAMESPACE

void FileChooser::showPlatformDialog (Array<File>& results,
									  const String& title,
									  const File& currentFileOrDirectory,
									  const String& filter,
									  bool selectsDirectory,
									  bool selectsFiles,
									  bool isSaveDialogue,
									  bool warnAboutOverwritingExistingFiles,
									  bool selectMultipleFiles,
									  FilePreviewComponent* extraInfoComponent)
{
	const ScopedAutoReleasePool pool;

	StringArray* filters = new StringArray();
	filters->addTokens (filter.replaceCharacters (",:", ";;"), ";", String::empty);
	filters->trim();
	filters->removeEmptyStrings();

	JuceFileChooserDelegate* delegate = [[JuceFileChooserDelegate alloc] initWithFilters: filters];
	[delegate autorelease];

	NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
										: [NSOpenPanel openPanel];

	[panel setTitle: juceStringToNS (title)];

	if (! isSaveDialogue)
	{
		NSOpenPanel* openPanel = (NSOpenPanel*) panel;
		[openPanel setCanChooseDirectories: selectsDirectory];
		[openPanel setCanChooseFiles: selectsFiles];
		[openPanel setAllowsMultipleSelection: selectMultipleFiles];
	}

	[panel setDelegate: delegate];

	if (isSaveDialogue || selectsDirectory)
		[panel setCanCreateDirectories: YES];

	String directory, filename;

	if (currentFileOrDirectory.isDirectory())
	{
		directory = currentFileOrDirectory.getFullPathName();
	}
	else
	{
		directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
		filename = currentFileOrDirectory.getFileName();
	}

	if ([panel runModalForDirectory: juceStringToNS (directory)
							   file: juceStringToNS (filename)]
		   == NSOKButton)
	{
		if (isSaveDialogue)
		{
			results.add (File (nsStringToJuce ([panel filename])));
		}
		else
		{
			NSOpenPanel* openPanel = (NSOpenPanel*) panel;
			NSArray* urls = [openPanel filenames];
			for (unsigned int i = 0; i < [urls count]; ++i)
			{
				NSString* f = [urls objectAtIndex: i];
				results.add (File (nsStringToJuce (f)));
			}
		}
	}

	[panel setDelegate: nil];
}

#else

void FileChooser::showPlatformDialog (Array<File>& results,
									  const String& title,
									  const File& currentFileOrDirectory,
									  const String& filter,
									  bool selectsDirectory,
									  bool selectsFiles,
									  bool isSaveDialogue,
									  bool warnAboutOverwritingExistingFiles,
									  bool selectMultipleFiles,
									  FilePreviewComponent* extraInfoComponent)
{
	const ScopedAutoReleasePool pool;

	jassertfalse //xxx to do
}

#endif

#endif
/*** End of inlined file: juce_mac_FileChooser.mm ***/


/*** Start of inlined file: juce_mac_QuickTimeMovieComponent.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_QUICKTIME

END_JUCE_NAMESPACE

#define NonInterceptingQTMovieView MakeObjCClassName(NonInterceptingQTMovieView)

@interface NonInterceptingQTMovieView   : QTMovieView
{
}

- (id) initWithFrame: (NSRect) frame;
- (BOOL) acceptsFirstMouse: (NSEvent*) theEvent;
- (NSView*) hitTest: (NSPoint) p;

@end

@implementation NonInterceptingQTMovieView

- (id) initWithFrame: (NSRect) frame
{
	self = [super initWithFrame: frame];
	[self setNextResponder: [self superview]];
	return self;
}

- (void) dealloc
{
	[super dealloc];
}

- (NSView*) hitTest: (NSPoint) point
{
	return [self isControllerVisible] ? [super hitTest: point] : nil;
}

- (BOOL) acceptsFirstMouse: (NSEvent*) theEvent
{
	return YES;
}

@end

BEGIN_JUCE_NAMESPACE

#define theMovie (static_cast <QTMovie*> (movie))

QuickTimeMovieComponent::QuickTimeMovieComponent()
	: movie (0)
{
	setOpaque (true);
	setVisible (true);

	QTMovieView* view = [[NonInterceptingQTMovieView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)];
	setView (view);
	[view release];
}

QuickTimeMovieComponent::~QuickTimeMovieComponent()
{
	closeMovie();
	setView (0);
}

bool QuickTimeMovieComponent::isQuickTimeAvailable() throw()
{
	return true;
}

static QTMovie* openMovieFromStream (InputStream* movieStream, File& movieFile)
{
	// unfortunately, QTMovie objects can only be created on the main thread..
	jassert (MessageManager::getInstance()->isThisTheMessageThread());

	QTMovie* movie = 0;

	FileInputStream* const fin = dynamic_cast <FileInputStream*> (movieStream);

	if (fin != 0)
	{
		movieFile = fin->getFile();
		movie = [QTMovie movieWithFile: juceStringToNS (movieFile.getFullPathName())
								 error: nil];
	}
	else
	{
		MemoryBlock temp;
		movieStream->readIntoMemoryBlock (temp);

		const char* const suffixesToTry[] = { ".mov", ".mp3", ".avi", ".m4a" };

		for (int i = 0; i < numElementsInArray (suffixesToTry); ++i)
		{
			movie = [QTMovie movieWithDataReference: [QTDataReference dataReferenceWithReferenceToData: [NSData dataWithBytes: temp.getData()
																													   length: temp.getSize()]
																								  name: [NSString stringWithUTF8String: suffixesToTry[i]]
																							  MIMEType: @""]
											  error: nil];

			if (movie != 0)
				break;
		}
	}

	return movie;
}

bool QuickTimeMovieComponent::loadMovie (const File& movieFile_,
										 const bool isControllerVisible_)
{
	return loadMovie ((InputStream*) movieFile_.createInputStream(), isControllerVisible_);
}

bool QuickTimeMovieComponent::loadMovie (InputStream* movieStream,
										 const bool controllerVisible_)
{
	closeMovie();

	if (getPeer() == 0)
	{
		// To open a movie, this component must be visible inside a functioning window, so that
		// the QT control can be assigned to the window.
		jassertfalse
		return false;
	}

	movie = openMovieFromStream (movieStream, movieFile);

	[theMovie retain];
	QTMovieView* view = (QTMovieView*) getView();
	[view setMovie: theMovie];
	[view setControllerVisible: controllerVisible_];
	setLooping (looping);

	return movie != nil;
}

bool QuickTimeMovieComponent::loadMovie (const URL& movieURL,
										 const bool isControllerVisible_)
{
	// unfortunately, QTMovie objects can only be created on the main thread..
	jassert (MessageManager::getInstance()->isThisTheMessageThread());

	closeMovie();

	if (getPeer() == 0)
	{
		// To open a movie, this component must be visible inside a functioning window, so that
		// the QT control can be assigned to the window.
		jassertfalse
		return false;
	}

	NSURL* url = [NSURL URLWithString: juceStringToNS (movieURL.toString (true))];
	NSError* err;
	if ([QTMovie canInitWithURL: url])
		movie = [QTMovie movieWithURL: url error: &err];

	[theMovie retain];
	QTMovieView* view = (QTMovieView*) getView();
	[view setMovie: theMovie];
	[view setControllerVisible: controllerVisible];
	setLooping (looping);

	return movie != nil;
}

void QuickTimeMovieComponent::closeMovie()
{
	stop();
	QTMovieView* view = (QTMovieView*) getView();
	[view setMovie: nil];
	[theMovie release];
	movie = 0;
	movieFile = File::nonexistent;
}

bool QuickTimeMovieComponent::isMovieOpen() const
{
	return movie != nil;
}

const File QuickTimeMovieComponent::getCurrentMovieFile() const
{
	return movieFile;
}

void QuickTimeMovieComponent::play()
{
	[theMovie play];
}

void QuickTimeMovieComponent::stop()
{
	[theMovie stop];
}

bool QuickTimeMovieComponent::isPlaying() const
{
	return movie != 0  && [theMovie rate] != 0;
}

void QuickTimeMovieComponent::setPosition (const double seconds)
{
	if (movie != 0)
	{
		QTTime t;
		t.timeValue = (uint64) (100000.0 * seconds);
		t.timeScale = 100000;
		t.flags = 0;

		[theMovie setCurrentTime: t];
	}
}

double QuickTimeMovieComponent::getPosition() const
{
	if (movie == 0)
		return 0.0;

	QTTime t = [theMovie currentTime];
	return t.timeValue / (double) t.timeScale;
}

void QuickTimeMovieComponent::setSpeed (const float newSpeed)
{
	[theMovie setRate: newSpeed];
}

double QuickTimeMovieComponent::getMovieDuration() const
{
	if (movie == 0)
		return 0.0;

	QTTime t = [theMovie duration];
	return t.timeValue / (double) t.timeScale;
}

void QuickTimeMovieComponent::setLooping (const bool shouldLoop)
{
	looping = shouldLoop;

	[theMovie setAttribute: [NSNumber numberWithBool: shouldLoop]
					forKey: QTMovieLoopsAttribute];
}

bool QuickTimeMovieComponent::isLooping() const
{
	return looping;
}

void QuickTimeMovieComponent::setMovieVolume (const float newVolume)
{
	[theMovie setVolume: newVolume];
}

float QuickTimeMovieComponent::getMovieVolume() const
{
	return movie != 0 ? [theMovie volume] : 0.0f;
}

void QuickTimeMovieComponent::getMovieNormalSize (int& width, int& height) const
{
	width = 0;
	height = 0;

	if (movie != 0)
	{
		NSSize s = [[theMovie attributeForKey: QTMovieNaturalSizeAttribute] sizeValue];
		width = (int) s.width;
		height = (int) s.height;
	}
}

void QuickTimeMovieComponent::paint (Graphics& g)
{
	if (movie == 0)
		g.fillAll (Colours::black);
}

bool QuickTimeMovieComponent::isControllerVisible() const
{
	return controllerVisible;
}

void QuickTimeMovieComponent::goToStart()
{
	setPosition (0.0);
}

void QuickTimeMovieComponent::setBoundsWithCorrectAspectRatio (const Rectangle<int>& spaceToFitWithin,
															   const RectanglePlacement& placement)
{
	int normalWidth, normalHeight;
	getMovieNormalSize (normalWidth, normalHeight);

	if (normalWidth > 0 && normalHeight > 0 && ! spaceToFitWithin.isEmpty())
	{
		double x = 0.0, y = 0.0, w = normalWidth, h = normalHeight;

		placement.applyTo (x, y, w, h,
						   spaceToFitWithin.getX(), spaceToFitWithin.getY(),
						   spaceToFitWithin.getWidth(), spaceToFitWithin.getHeight());

		if (w > 0 && h > 0)
		{
			setBounds (roundToInt (x), roundToInt (y),
					   roundToInt (w), roundToInt (h));
		}
	}
	else
	{
		setBounds (spaceToFitWithin);
	}
}

#if ! (JUCE_MAC && JUCE_64BIT)

bool juce_OpenQuickTimeMovieFromStream (InputStream* movieStream, Movie& result, Handle& dataHandle)
{
	if (movieStream == 0)
		return false;

	File file;
	QTMovie* movie = openMovieFromStream (movieStream, file);

	if (movie != nil)
		result = [movie quickTimeMovie];

	return movie != nil;
}

#endif

#endif
/*** End of inlined file: juce_mac_QuickTimeMovieComponent.mm ***/


/*** Start of inlined file: juce_mac_AudioCDBurner.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_USE_CDBURNER

const int kilobytesPerSecond1x = 176;

END_JUCE_NAMESPACE

#define OpenDiskDevice MakeObjCClassName(OpenDiskDevice)

@interface OpenDiskDevice   : NSObject
{
@public
	DRDevice* device;
	NSMutableArray* tracks;
	bool underrunProtection;
}

- (OpenDiskDevice*) initWithDRDevice: (DRDevice*) device;
- (void) dealloc;
- (void) addSourceTrack: (JUCE_NAMESPACE::AudioSource*) source numSamples: (int) numSamples_;
- (void) burn: (JUCE_NAMESPACE::AudioCDBurner::BurnProgressListener*) listener  errorString: (JUCE_NAMESPACE::String*) error
		 ejectAfterwards: (bool) shouldEject isFake: (bool) peformFakeBurnForTesting speed: (int) burnSpeed;
@end

#define AudioTrackProducer MakeObjCClassName(AudioTrackProducer)

@interface AudioTrackProducer   : NSObject
{
	JUCE_NAMESPACE::AudioSource* source;
	int readPosition, lengthInFrames;
}

- (AudioTrackProducer*) init: (int) lengthInFrames;
- (AudioTrackProducer*) initWithAudioSource: (JUCE_NAMESPACE::AudioSource*) source numSamples: (int) lengthInSamples;
- (void) dealloc;
- (void) setupTrackProperties: (DRTrack*) track;

- (void) cleanupTrackAfterBurn: (DRTrack*) track;
- (BOOL) cleanupTrackAfterVerification:(DRTrack*)track;
- (uint64_t) estimateLengthOfTrack:(DRTrack*)track;
- (BOOL) prepareTrack:(DRTrack*)track forBurn:(DRBurn*)burn
		 toMedia:(NSDictionary*)mediaInfo;
- (BOOL) prepareTrackForVerification:(DRTrack*)track;
- (uint32_t) produceDataForTrack:(DRTrack*)track intoBuffer:(char*)buffer
		length:(uint32_t)bufferLength atAddress:(uint64_t)address
		blockSize:(uint32_t)blockSize ioFlags:(uint32_t*)flags;
- (uint32_t) producePreGapForTrack:(DRTrack*)track
			 intoBuffer:(char*)buffer length:(uint32_t)bufferLength
			 atAddress:(uint64_t)address blockSize:(uint32_t)blockSize
			 ioFlags:(uint32_t*)flags;
- (BOOL) verifyDataForTrack:(DRTrack*)track inBuffer:(const char*)buffer
		 length:(uint32_t)bufferLength atAddress:(uint64_t)address
		 blockSize:(uint32_t)blockSize ioFlags:(uint32_t*)flags;
- (uint32_t) producePreGapForTrack:(DRTrack*)track
		intoBuffer:(char*)buffer length:(uint32_t)bufferLength
		atAddress:(uint64_t)address blockSize:(uint32_t)blockSize
		ioFlags:(uint32_t*)flags;
@end

@implementation OpenDiskDevice

- (OpenDiskDevice*) initWithDRDevice: (DRDevice*) device_
{
	[super init];

	device = device_;
	tracks = [[NSMutableArray alloc] init];
	underrunProtection = true;
	return self;
}

- (void) dealloc
{
	[tracks release];
	[super dealloc];
}

- (void) addSourceTrack: (JUCE_NAMESPACE::AudioSource*) source_ numSamples: (int) numSamples_
{
	AudioTrackProducer* p = [[AudioTrackProducer alloc] initWithAudioSource: source_ numSamples: numSamples_];
	DRTrack* t = [[DRTrack alloc] initWithProducer: p];
	[p setupTrackProperties: t];

	[tracks addObject: t];

	[t release];
	[p release];
}

- (void) burn: (JUCE_NAMESPACE::AudioCDBurner::BurnProgressListener*) listener errorString: (JUCE_NAMESPACE::String*) error
		 ejectAfterwards: (bool) shouldEject isFake: (bool) peformFakeBurnForTesting speed: (int) burnSpeed
{
	DRBurn* burn = [DRBurn burnForDevice: device];

	if (! [device acquireExclusiveAccess])
	{
		*error = "Couldn't open or write to the CD device";
		return;
	}

	[device acquireMediaReservation];

	NSMutableDictionary* d = [[burn properties] mutableCopy];
	[d autorelease];
	[d setObject: [NSNumber numberWithBool: peformFakeBurnForTesting] forKey: DRBurnTestingKey];
	[d setObject: [NSNumber numberWithBool: false] forKey: DRBurnVerifyDiscKey];
	[d setObject: (shouldEject ? DRBurnCompletionActionEject : DRBurnCompletionActionMount) forKey: DRBurnCompletionActionKey];

	if (burnSpeed > 0)
		[d setObject: [NSNumber numberWithFloat: burnSpeed * JUCE_NAMESPACE::kilobytesPerSecond1x] forKey: DRBurnRequestedSpeedKey];

	if (! underrunProtection)
		[d setObject: [NSNumber numberWithBool: false] forKey: DRBurnUnderrunProtectionKey];

	[burn setProperties: d];

	[burn writeLayout: tracks];

	for (;;)
	{
		JUCE_NAMESPACE::Thread::sleep (300);
		float progress = [[[burn status] objectForKey: DRStatusPercentCompleteKey] floatValue];

		if (listener != 0 && listener->audioCDBurnProgress (progress))
		{
			[burn abort];
			*error = "User cancelled the write operation";
			break;
		}

		if ([[[burn status] objectForKey: DRStatusStateKey] isEqualTo: DRStatusStateFailed])
		{
			*error = "Write operation failed";
			break;
		}
		else if ([[[burn status] objectForKey: DRStatusStateKey] isEqualTo: DRStatusStateDone])
		{
			break;
		}

		NSString* err = (NSString*) [[[burn status] objectForKey: DRErrorStatusKey]
													objectForKey: DRErrorStatusErrorStringKey];

		if ([err length] > 0)
		{
			*error = JUCE_NAMESPACE::String::fromUTF8 ([err UTF8String]);
			break;
		}
	}

	[device releaseMediaReservation];
	[device releaseExclusiveAccess];
}
@end

@implementation AudioTrackProducer

- (AudioTrackProducer*) init: (int) lengthInFrames_
{
	lengthInFrames = lengthInFrames_;
	readPosition = 0;
	return self;
}

- (void) setupTrackProperties: (DRTrack*) track
{
	NSMutableDictionary*  p = [[track properties] mutableCopy];
	[p setObject:[DRMSF msfWithFrames: lengthInFrames] forKey: DRTrackLengthKey];
	[p setObject:[NSNumber numberWithUnsignedShort:2352] forKey: DRBlockSizeKey];
	[p setObject:[NSNumber numberWithInt:0] forKey: DRDataFormKey];
	[p setObject:[NSNumber numberWithInt:0] forKey: DRBlockTypeKey];
	[p setObject:[NSNumber numberWithInt:0] forKey: DRTrackModeKey];
	[p setObject:[NSNumber numberWithInt:0] forKey: DRSessionFormatKey];

	[track setProperties: p];
	[p release];
}

- (AudioTrackProducer*) initWithAudioSource: (JUCE_NAMESPACE::AudioSource*) source_ numSamples: (int) lengthInSamples
{
	AudioTrackProducer* s = [self init: (lengthInSamples + 587) / 588];

	if (s != nil)
		s->source = source_;

	return s;
}

- (void) dealloc
{
	if (source != 0)
	{
		source->releaseResources();
		delete source;
	}

	[super dealloc];
}

- (void) cleanupTrackAfterBurn: (DRTrack*) track
{
}

- (BOOL) cleanupTrackAfterVerification: (DRTrack*) track
{
	return true;
}

- (uint64_t) estimateLengthOfTrack: (DRTrack*) track
{
	return lengthInFrames;
}

- (BOOL) prepareTrack: (DRTrack*) track forBurn: (DRBurn*) burn
		 toMedia: (NSDictionary*) mediaInfo
{
	if (source != 0)
		source->prepareToPlay (44100 / 75, 44100);

	readPosition = 0;
	return true;
}

- (BOOL) prepareTrackForVerification: (DRTrack*) track
{
	if (source != 0)
		source->prepareToPlay (44100 / 75, 44100);

	return true;
}

- (uint32_t) produceDataForTrack: (DRTrack*) track intoBuffer: (char*) buffer
		length: (uint32_t) bufferLength atAddress: (uint64_t) address
		blockSize: (uint32_t) blockSize ioFlags: (uint32_t*) flags
{
	if (source != 0)
	{
		const int numSamples = JUCE_NAMESPACE::jmin ((int) bufferLength / 4, (lengthInFrames * (44100 / 75)) - readPosition);

		if (numSamples > 0)
		{
			JUCE_NAMESPACE::AudioSampleBuffer tempBuffer (2, numSamples);

			JUCE_NAMESPACE::AudioSourceChannelInfo info;
			info.buffer = &tempBuffer;
			info.startSample = 0;
			info.numSamples = numSamples;

			source->getNextAudioBlock (info);

			JUCE_NAMESPACE::AudioDataConverters::convertFloatToInt16LE (tempBuffer.getSampleData (0),
																		buffer, numSamples, 4);
			JUCE_NAMESPACE::AudioDataConverters::convertFloatToInt16LE (tempBuffer.getSampleData (1),
																		buffer + 2, numSamples, 4);

			readPosition += numSamples;
		}

		return numSamples * 4;
	}

	return 0;
}

- (uint32_t) producePreGapForTrack: (DRTrack*) track
		intoBuffer: (char*) buffer length: (uint32_t) bufferLength
		atAddress: (uint64_t) address blockSize: (uint32_t) blockSize
		ioFlags: (uint32_t*) flags
{
	zeromem (buffer, bufferLength);
	return bufferLength;
}

- (BOOL) verifyDataForTrack: (DRTrack*) track inBuffer: (const char*) buffer
		 length: (uint32_t) bufferLength atAddress: (uint64_t) address
		 blockSize: (uint32_t) blockSize ioFlags: (uint32_t*) flags
{
	return true;
}

@end

BEGIN_JUCE_NAMESPACE

class AudioCDBurner::Pimpl  : public Timer
{
public:
	Pimpl (AudioCDBurner& owner_, const int deviceIndex)
		: device (0), owner (owner_)
	{
		DRDevice* dev = [[DRDevice devices] objectAtIndex: deviceIndex];
		if (dev != 0)
		{
			device = [[OpenDiskDevice alloc] initWithDRDevice: dev];
			lastState = getDiskState();
			startTimer (1000);
		}
	}

	~Pimpl()
	{
		stopTimer();
		[device release];
	}

	void timerCallback()
	{
		const DiskState state = getDiskState();

		if (state != lastState)
		{
			lastState = state;
			owner.sendChangeMessage (&owner);
		}
	}

	DiskState getDiskState() const
	{
		if ([device->device isValid])
		{
			NSDictionary* status = [device->device status];

			NSString* state = [status objectForKey: DRDeviceMediaStateKey];

			if ([state isEqualTo: DRDeviceMediaStateNone])
			{
				if ([[status objectForKey: DRDeviceIsTrayOpenKey] boolValue])
					return trayOpen;

				return noDisc;
			}

			if ([state isEqualTo: DRDeviceMediaStateMediaPresent])
			{
				if ([[[status objectForKey: DRDeviceMediaInfoKey] objectForKey: DRDeviceMediaBlocksFreeKey] intValue] > 0)
					return writableDiskPresent;
				else
					return readOnlyDiskPresent;
			}
		}

		return unknown;
	}

	bool openTray() { return [device->device isValid] && [device->device ejectMedia]; }

	const Array<int> getAvailableWriteSpeeds() const
	{
		Array<int> results;

		if ([device->device isValid])
		{
			NSArray* speeds = [[[device->device status] objectForKey: DRDeviceMediaInfoKey] objectForKey: DRDeviceBurnSpeedsKey];
			for (unsigned int i = 0; i < [speeds count]; ++i)
			{
				const int kbPerSec = [[speeds objectAtIndex: i] intValue];
				results.add (kbPerSec / kilobytesPerSecond1x);
			}
		}

		return results;
	}

	bool setBufferUnderrunProtection (const bool shouldBeEnabled)
	{
		if ([device->device isValid])
		{
			device->underrunProtection = shouldBeEnabled;
			return shouldBeEnabled && [[[device->device status] objectForKey: DRDeviceCanUnderrunProtectCDKey] boolValue];
		}

		return false;
	}

	int getNumAvailableAudioBlocks() const
	{
		return [[[[device->device status] objectForKey: DRDeviceMediaInfoKey]
										  objectForKey: DRDeviceMediaBlocksFreeKey] intValue];
	}

	OpenDiskDevice* device;

private:
	DiskState lastState;
	AudioCDBurner& owner;
};

AudioCDBurner::AudioCDBurner (const int deviceIndex)
{
	pimpl = new Pimpl (*this, deviceIndex);
}

AudioCDBurner::~AudioCDBurner()
{
}

AudioCDBurner* AudioCDBurner::openDevice (const int deviceIndex)
{
	ScopedPointer <AudioCDBurner> b (new AudioCDBurner (deviceIndex));

	if (b->pimpl->device == 0)
		b = 0;

	return b.release();
}

static NSArray* findDiskBurnerDevices()
{
	NSMutableArray* results = [NSMutableArray array];
	NSArray* devs = [DRDevice devices];

	if (devs != 0)
	{
		int num = [devs count];
		int i;
		for (i = 0; i < num; ++i)
		{
			NSDictionary* dic = [[devs objectAtIndex: i] info];
			NSString* name = [dic valueForKey: DRDeviceProductNameKey];
			if (name != nil)
				[results addObject: name];
		}
	}

	return results;
}

const StringArray AudioCDBurner::findAvailableDevices()
{
	NSArray* names = findDiskBurnerDevices();
	StringArray s;

	for (unsigned int i = 0; i < [names count]; ++i)
		s.add (String::fromUTF8 ([[names objectAtIndex: i] UTF8String]));

	return s;
}

AudioCDBurner::DiskState AudioCDBurner::getDiskState() const
{
	return pimpl->getDiskState();
}

bool AudioCDBurner::isDiskPresent() const
{
	return getDiskState() == writableDiskPresent;
}

bool AudioCDBurner::openTray()
{
	return pimpl->openTray();
}

AudioCDBurner::DiskState AudioCDBurner::waitUntilStateChange (int timeOutMilliseconds)
{
	const int64 timeout = Time::currentTimeMillis() + timeOutMilliseconds;
	DiskState oldState = getDiskState();
	DiskState newState = oldState;

	while (newState == oldState && Time::currentTimeMillis() < timeout)
	{
		newState = getDiskState();
		Thread::sleep (100);
	}

	return newState;
}

const Array<int> AudioCDBurner::getAvailableWriteSpeeds() const
{
	return pimpl->getAvailableWriteSpeeds();
}

bool AudioCDBurner::setBufferUnderrunProtection (const bool shouldBeEnabled)
{
	return pimpl->setBufferUnderrunProtection (shouldBeEnabled);
}

int AudioCDBurner::getNumAvailableAudioBlocks() const
{
	return pimpl->getNumAvailableAudioBlocks();
}

bool AudioCDBurner::addAudioTrack (AudioSource* source, int numSamps)
{
	if ([pimpl->device->device isValid])
	{
		[pimpl->device addSourceTrack: source numSamples: numSamps];
		return true;
	}

	return false;
}

const String AudioCDBurner::burn (JUCE_NAMESPACE::AudioCDBurner::BurnProgressListener* listener,
								  bool ejectDiscAfterwards,
								  bool performFakeBurnForTesting,
								  int writeSpeed)
{
	String error ("Couldn't open or write to the CD device");

	if ([pimpl->device->device isValid])
	{
		error = String::empty;

		[pimpl->device  burn: listener
				 errorString: &error
			 ejectAfterwards: ejectDiscAfterwards
					  isFake: performFakeBurnForTesting
					   speed: writeSpeed];
	}

	return error;
}

#endif

#if JUCE_INCLUDED_FILE && JUCE_USE_CDREADER

void AudioCDReader::ejectDisk()
{
	const ScopedAutoReleasePool p;
	[[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtPath: juceStringToNS (volumeDir.getFullPathName())];
}

#endif
/*** End of inlined file: juce_mac_AudioCDBurner.mm ***/


/*** Start of inlined file: juce_mac_MessageManager.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

class AppDelegateRedirector
{
public:
	AppDelegateRedirector()
	{
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4
		runLoop = CFRunLoopGetMain();
#else
		runLoop = CFRunLoopGetCurrent();
#endif
		CFRunLoopSourceContext sourceContext;
		zerostruct (sourceContext);
		sourceContext.info = this;
		sourceContext.perform = runLoopSourceCallback;
		runLoopSource = CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext);
		CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
	}

	virtual ~AppDelegateRedirector()
	{
		CFRunLoopRemoveSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
		CFRunLoopSourceInvalidate (runLoopSource);
		CFRelease (runLoopSource);

		while (messages.size() > 0)
			delete static_cast <Message*> (messages.remove(0));
	}

	virtual NSApplicationTerminateReply shouldTerminate()
	{
		if (JUCEApplication::getInstance() != 0)
		{
			JUCEApplication::getInstance()->systemRequestedQuit();
			return NSTerminateCancel;
		}

		return NSTerminateNow;
	}

	virtual BOOL openFile (const NSString* filename)
	{
		if (JUCEApplication::getInstance() != 0)
		{
			JUCEApplication::getInstance()->anotherInstanceStarted (nsStringToJuce (filename));
			return YES;
		}

		return NO;
	}

	virtual void openFiles (NSArray* filenames)
	{
		StringArray files;
		for (unsigned int i = 0; i < [filenames count]; ++i)
		{
			String filename (nsStringToJuce ((NSString*) [filenames objectAtIndex: i]));
			if (filename.containsChar (' '))
				filename = filename.quoted('"');

			files.add (filename);
		}

		if (files.size() > 0 && JUCEApplication::getInstance() != 0)
		{
			JUCEApplication::getInstance()->anotherInstanceStarted (files.joinIntoString (" "));
		}
	}

	virtual void focusChanged()
	{
		juce_HandleProcessFocusChange();
	}

	struct CallbackMessagePayload
	{
		MessageCallbackFunction* function;
		void* parameter;
		void* volatile result;
		bool volatile hasBeenExecuted;
	};

	virtual void performCallback (CallbackMessagePayload* pl)
	{
		pl->result = (*pl->function) (pl->parameter);
		pl->hasBeenExecuted = true;
	}

	virtual void deleteSelf()
	{
		delete this;
	}

	void postMessage (void* m)
	{
		messages.add (m);
		CFRunLoopSourceSignal (runLoopSource);
		CFRunLoopWakeUp (runLoop);
	}

private:
	CFRunLoopRef runLoop;
	CFRunLoopSourceRef runLoopSource;
	Array <void*, CriticalSection> messages;

	void runLoopCallback()
	{
		int numDispatched = 0;

		do
		{
			void* const nextMessage = messages.remove (0);

			if (nextMessage == 0)
				return;

			const ScopedAutoReleasePool pool;
			MessageManager::getInstance()->deliverMessage (nextMessage);

		} while (++numDispatched <= 4);

		CFRunLoopSourceSignal (runLoopSource);
		CFRunLoopWakeUp (runLoop);
	}

	static void runLoopSourceCallback (void* info)
	{
		static_cast <AppDelegateRedirector*> (info)->runLoopCallback();
	}
};

END_JUCE_NAMESPACE
using namespace JUCE_NAMESPACE;

#define JuceAppDelegate MakeObjCClassName(JuceAppDelegate)

@interface JuceAppDelegate   : NSObject
{
@private
	id oldDelegate;

@public
	AppDelegateRedirector* redirector;
}

- (JuceAppDelegate*) init;
- (void) dealloc;
- (BOOL) application: (NSApplication*) theApplication openFile: (NSString*) filename;
- (void) application: (NSApplication*) sender openFiles: (NSArray*) filenames;
- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication*) app;
- (void) applicationDidBecomeActive: (NSNotification*) aNotification;
- (void) applicationDidResignActive: (NSNotification*) aNotification;
- (void) applicationWillUnhide: (NSNotification*) aNotification;
- (void) performCallback: (id) info;
- (void) dummyMethod;
@end

@implementation JuceAppDelegate

- (JuceAppDelegate*) init
{
	[super init];

	redirector = new AppDelegateRedirector();

	NSNotificationCenter* center = [NSNotificationCenter defaultCenter];

	if (JUCEApplication::getInstance() != 0)
	{
		oldDelegate = [NSApp delegate];
		[NSApp setDelegate: self];
	}
	else
	{
		oldDelegate = 0;
		[center addObserver: self selector: @selector (applicationDidResignActive:)
					   name: NSApplicationDidResignActiveNotification object: NSApp];

		[center addObserver: self selector: @selector (applicationDidBecomeActive:)
					   name: NSApplicationDidBecomeActiveNotification object: NSApp];

		[center addObserver: self selector: @selector (applicationWillUnhide:)
					   name: NSApplicationWillUnhideNotification object: NSApp];
	}

	return self;
}

- (void) dealloc
{
	if (oldDelegate != 0)
		[NSApp setDelegate: oldDelegate];

	redirector->deleteSelf();
	[super dealloc];
}

- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication*) app
{
	return redirector->shouldTerminate();
}

- (BOOL) application: (NSApplication*) app openFile: (NSString*) filename
{
	return redirector->openFile (filename);
}

- (void) application: (NSApplication*) sender openFiles: (NSArray*) filenames
{
	return redirector->openFiles (filenames);
}

- (void) applicationDidBecomeActive: (NSNotification*) aNotification
{
	redirector->focusChanged();
}

- (void) applicationDidResignActive: (NSNotification*) aNotification
{
	redirector->focusChanged();
}

- (void) applicationWillUnhide: (NSNotification*) aNotification
{
	redirector->focusChanged();
}

- (void) performCallback: (id) info
{
	if ([info isKindOfClass: [NSData class]])
	{
		AppDelegateRedirector::CallbackMessagePayload* pl
			= (AppDelegateRedirector::CallbackMessagePayload*) [((NSData*) info) bytes];

		if (pl != 0)
			redirector->performCallback (pl);
	}
	else
	{
		jassertfalse // should never get here!
	}
}

- (void) dummyMethod  {}   // (used as a way of running a dummy thread)

@end

BEGIN_JUCE_NAMESPACE

static JuceAppDelegate* juceAppDelegate = 0;

void MessageManager::runDispatchLoop()
{
	if (! quitMessagePosted) // check that the quit message wasn't already posted..
	{
		const ScopedAutoReleasePool pool;

		// must only be called by the message thread!
		jassert (isThisTheMessageThread());

#if JUCE_CATCH_UNHANDLED_EXCEPTIONS
		@try
		{
			[NSApp run];
		}
		@catch (NSException* e)
		{
			// An AppKit exception will kill the app, but at least this provides a chance to log it.,
			std::runtime_error ex (std::string ("NSException: ") + [[e name] UTF8String] + ", Reason:" + [[e reason] UTF8String]);
			JUCEApplication::sendUnhandledException (&ex, __FILE__, __LINE__);
		}
		@finally
		{
		}
#else
		[NSApp run];
#endif
	}
}

void MessageManager::stopDispatchLoop()
{
	quitMessagePosted = true;
	[NSApp stop: nil];
	[NSApp activateIgnoringOtherApps: YES]; // (if the app is inactive, it sits there and ignores the quit request until the next time it gets activated)
	[NSEvent startPeriodicEventsAfterDelay: 0 withPeriod: 0.1];
}

static bool isEventBlockedByModalComps (NSEvent* e)
{
	if (Component::getNumCurrentlyModalComponents() == 0)
		return false;

	NSWindow* const w = [e window];
	if (w == 0 || [w worksWhenModal])
		return false;

	bool isKey = false, isInputAttempt = false;

	switch ([e type])
	{
		case NSKeyDown:
		case NSKeyUp:
			isKey = isInputAttempt = true;
			break;

		case NSLeftMouseDown:
		case NSRightMouseDown:
		case NSOtherMouseDown:
			isInputAttempt = true;
			break;

		case NSLeftMouseDragged:
		case NSRightMouseDragged:
		case NSLeftMouseUp:
		case NSRightMouseUp:
		case NSOtherMouseUp:
		case NSOtherMouseDragged:
			if (Desktop::getInstance().getDraggingMouseSource(0) != 0)
				return false;
			break;

		case NSMouseMoved:
		case NSMouseEntered:
		case NSMouseExited:
		case NSCursorUpdate:
		case NSScrollWheel:
		case NSTabletPoint:
		case NSTabletProximity:
			break;

		default:
			return false;
	}

	for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
	{
		ComponentPeer* const peer = ComponentPeer::getPeer (i);
		NSView* const compView = (NSView*) peer->getNativeHandle();

		if ([compView window] == w)
		{
			if (isKey)
			{
				if (compView == [w firstResponder])
					return false;
			}
			else
			{
				if (NSPointInRect ([compView convertPoint: [e locationInWindow] fromView: nil],
								   [compView bounds]))
					return false;
			}
		}
	}

	if (isInputAttempt)
	{
		if (! [NSApp isActive])
			[NSApp activateIgnoringOtherApps: YES];

		Component* const modal = Component::getCurrentlyModalComponent (0);
		if (modal != 0)
			modal->inputAttemptWhenModal();
	}

	return true;
}

bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
{
	const ScopedAutoReleasePool pool;
	jassert (isThisTheMessageThread()); // must only be called by the message thread

	uint32 endTime = Time::getMillisecondCounter() + millisecondsToRunFor;

	while (! quitMessagePosted)
	{
		const ScopedAutoReleasePool pool2;

		CFRunLoopRunInMode (kCFRunLoopDefaultMode, 0.001, true);

		NSEvent* e = [NSApp nextEventMatchingMask: NSAnyEventMask
										untilDate: [NSDate dateWithTimeIntervalSinceNow: 0.001]
										   inMode: NSDefaultRunLoopMode
										  dequeue: YES];

		if (e != 0 && ! isEventBlockedByModalComps (e))
			[NSApp sendEvent: e];

		if (Time::getMillisecondCounter() >= endTime)
			break;
	}

	return ! quitMessagePosted;
}

void MessageManager::doPlatformSpecificInitialisation()
{
	if (juceAppDelegate == 0)
		juceAppDelegate = [[JuceAppDelegate alloc] init];

	// This launches a dummy thread, which forces Cocoa to initialise NSThreads
	// correctly (needed prior to 10.5)
	if (! [NSThread isMultiThreaded])
		[NSThread detachNewThreadSelector: @selector (dummyMethod)
								 toTarget: juceAppDelegate
							   withObject: nil];

	initialiseMainMenu();
}

void MessageManager::doPlatformSpecificShutdown()
{
	if (juceAppDelegate != 0)
	{
		[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: juceAppDelegate];
		[[NSNotificationCenter defaultCenter] removeObserver: juceAppDelegate];
		[juceAppDelegate release];
		juceAppDelegate = 0;
	}
}

bool juce_postMessageToSystemQueue (void* message)
{
	juceAppDelegate->redirector->postMessage (message);
	return true;
}

void MessageManager::broadcastMessage (const String& value) throw()
{
}

void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
												   void* data)
{
	if (isThisTheMessageThread())
	{
		return (*callback) (data);
	}
	else
	{
		// If a thread has a MessageManagerLock and then tries to call this method, it'll
		// deadlock because the message manager is blocked from running, so can never
		// call your function..
		jassert (! MessageManager::getInstance()->currentThreadHasLockedMessageManager());

		const ScopedAutoReleasePool pool;

		AppDelegateRedirector::CallbackMessagePayload cmp;
		cmp.function = callback;
		cmp.parameter = data;
		cmp.result = 0;
		cmp.hasBeenExecuted = false;

		[juceAppDelegate performSelectorOnMainThread: @selector (performCallback:)
										  withObject: [NSData dataWithBytesNoCopy: &cmp
																		   length: sizeof (cmp)
																	 freeWhenDone: NO]
									   waitUntilDone: YES];

		return cmp.result;
	}
}

#endif
/*** End of inlined file: juce_mac_MessageManager.mm ***/


/*** Start of inlined file: juce_mac_WebBrowserComponent.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_WEB_BROWSER

#if JUCE_MAC

END_JUCE_NAMESPACE

#define DownloadClickDetector MakeObjCClassName(DownloadClickDetector)

@interface DownloadClickDetector   : NSObject
{
	JUCE_NAMESPACE::WebBrowserComponent* ownerComponent;
}

- (DownloadClickDetector*) initWithWebBrowserOwner: (JUCE_NAMESPACE::WebBrowserComponent*) ownerComponent;

- (void) webView: (WebView*) webView decidePolicyForNavigationAction: (NSDictionary*) actionInformation
															 request: (NSURLRequest*) request
															   frame: (WebFrame*) frame
													decisionListener: (id<WebPolicyDecisionListener>) listener;
@end

@implementation DownloadClickDetector

- (DownloadClickDetector*) initWithWebBrowserOwner: (JUCE_NAMESPACE::WebBrowserComponent*) ownerComponent_
{
	[super init];
	ownerComponent = ownerComponent_;
	return self;
}

- (void) webView: (WebView*) sender decidePolicyForNavigationAction: (NSDictionary*) actionInformation
															request: (NSURLRequest*) request
															  frame: (WebFrame*) frame
												   decisionListener: (id <WebPolicyDecisionListener>) listener
{
	NSURL* url = [actionInformation valueForKey: @"WebActionOriginalURLKey"];

	if (ownerComponent->pageAboutToLoad (nsStringToJuce ([url absoluteString])))
		[listener use];
	else
		[listener ignore];
}

@end

BEGIN_JUCE_NAMESPACE

class WebBrowserComponentInternal  : public NSViewComponent
{
public:
	WebBrowserComponentInternal (WebBrowserComponent* owner)
	{
		webView = [[WebView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
									   frameName: @""
									   groupName: @""];
		setView (webView);

		clickListener = [[DownloadClickDetector alloc] initWithWebBrowserOwner: owner];
		[webView setPolicyDelegate: clickListener];
	}

	~WebBrowserComponentInternal()
	{
		[webView setPolicyDelegate: nil];
		[clickListener release];
		setView (0);
	}

	void goToURL (const String& url,
				  const StringArray* headers,
				  const MemoryBlock* postData)
	{
		NSMutableURLRequest* r
			= [NSMutableURLRequest requestWithURL: [NSURL URLWithString: juceStringToNS (url)]
									  cachePolicy: NSURLRequestUseProtocolCachePolicy
								  timeoutInterval: 30.0];

		if (postData != 0 && postData->getSize() > 0)
		{
			[r setHTTPMethod: @"POST"];
			[r setHTTPBody: [NSData dataWithBytes: postData->getData()
										   length: postData->getSize()]];
		}

		if (headers != 0)
		{
			for (int i = 0; i < headers->size(); ++i)
			{
				const String headerName ((*headers)[i].upToFirstOccurrenceOf (":", false, false).trim());
				const String headerValue ((*headers)[i].fromFirstOccurrenceOf (":", false, false).trim());

				[r setValue: juceStringToNS (headerValue)
				   forHTTPHeaderField: juceStringToNS (headerName)];
			}
		}

		stop();
		[[webView mainFrame] loadRequest: r];
	}

	void goBack()
	{
		[webView goBack];
	}

	void goForward()
	{
		[webView goForward];
	}

	void stop()
	{
		[webView stopLoading: nil];
	}

	void refresh()
	{
		[webView reload: nil];
	}

private:
	WebView* webView;
	DownloadClickDetector* clickListener;
};

WebBrowserComponent::WebBrowserComponent (const bool unloadPageWhenBrowserIsHidden_)
	: browser (0),
	  blankPageShown (false),
	  unloadPageWhenBrowserIsHidden (unloadPageWhenBrowserIsHidden_)
{
	setOpaque (true);

	addAndMakeVisible (browser = new WebBrowserComponentInternal (this));
}

WebBrowserComponent::~WebBrowserComponent()
{
	deleteAndZero (browser);
}

void WebBrowserComponent::goToURL (const String& url,
								   const StringArray* headers,
								   const MemoryBlock* postData)
{
	lastURL = url;

	lastHeaders.clear();
	if (headers != 0)
		lastHeaders = *headers;

	lastPostData.setSize (0);
	if (postData != 0)
		lastPostData = *postData;

	blankPageShown = false;

	browser->goToURL (url, headers, postData);
}

void WebBrowserComponent::stop()
{
	browser->stop();
}

void WebBrowserComponent::goBack()
{
	lastURL = String::empty;
	blankPageShown = false;
	browser->goBack();
}

void WebBrowserComponent::goForward()
{
	lastURL = String::empty;
	browser->goForward();
}

void WebBrowserComponent::refresh()
{
	browser->refresh();
}

void WebBrowserComponent::paint (Graphics& g)
{
}

void WebBrowserComponent::checkWindowAssociation()
{
	if (isShowing())
	{
		if (blankPageShown)
			goBack();
	}
	else
	{
		if (unloadPageWhenBrowserIsHidden && ! blankPageShown)
		{
			// when the component becomes invisible, some stuff like flash
			// carries on playing audio, so we need to force it onto a blank
			// page to avoid this, (and send it back when it's made visible again).

			blankPageShown = true;
			browser->goToURL ("about:blank", 0, 0);
		}
	}
}

void WebBrowserComponent::reloadLastURL()
{
	if (lastURL.isNotEmpty())
	{
		goToURL (lastURL, &lastHeaders, &lastPostData);
		lastURL = String::empty;
	}
}

void WebBrowserComponent::parentHierarchyChanged()
{
	checkWindowAssociation();
}

void WebBrowserComponent::resized()
{
	browser->setSize (getWidth(), getHeight());
}

void WebBrowserComponent::visibilityChanged()
{
	checkWindowAssociation();
}

bool WebBrowserComponent::pageAboutToLoad (const String& url)
{
	return true;
}

#else

WebBrowserComponent::WebBrowserComponent (const bool unloadPageWhenBrowserIsHidden_)
{
}

WebBrowserComponent::~WebBrowserComponent()
{
}

void WebBrowserComponent::goToURL (const String& url,
								   const StringArray* headers,
								   const MemoryBlock* postData)
{
}

void WebBrowserComponent::stop()
{
}

void WebBrowserComponent::goBack()
{
}

void WebBrowserComponent::goForward()
{
}

void WebBrowserComponent::refresh()
{
}

void WebBrowserComponent::paint (Graphics& g)
{
}

void WebBrowserComponent::checkWindowAssociation()
{
}

void WebBrowserComponent::reloadLastURL()
{
}

void WebBrowserComponent::parentHierarchyChanged()
{
}

void WebBrowserComponent::resized()
{
}

void WebBrowserComponent::visibilityChanged()
{
}

bool WebBrowserComponent::pageAboutToLoad (const String& url)
{
	return true;
}

#endif

#endif
/*** End of inlined file: juce_mac_WebBrowserComponent.mm ***/


/*** Start of inlined file: juce_mac_CoreAudio.cpp ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#ifndef JUCE_COREAUDIO_ERROR_LOGGING_ENABLED
  #define JUCE_COREAUDIO_ERROR_LOGGING_ENABLED 1
#endif

#undef log
#if JUCE_COREAUDIO_LOGGING_ENABLED
  #define log(a) Logger::writeToLog (a)
#else
  #define log(a)
#endif

#undef OK
#if JUCE_COREAUDIO_ERROR_LOGGING_ENABLED
  static bool logAnyErrors_CoreAudio (const OSStatus err, const int lineNum)
  {
	  if (err == noErr)
		  return true;

	  Logger::writeToLog ("CoreAudio error: " + String (lineNum) + " - " + String::toHexString ((int) err));
	  jassertfalse
	  return false;
  }

  #define OK(a) logAnyErrors_CoreAudio (a, __LINE__)
#else
  #define OK(a) (a == noErr)
#endif

class CoreAudioInternal  : public Timer
{
public:

	CoreAudioInternal (AudioDeviceID id)
	   : inputLatency (0),
		 outputLatency (0),
		 callback (0),
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
		 audioProcID (0),
#endif
		 inputDevice (0),
		 isSlaveDevice (false),
		 deviceID (id),
		 started (false),
		 sampleRate (0),
		 bufferSize (512),
		 numInputChans (0),
		 numOutputChans (0),
		 callbacksAllowed (true),
		 numInputChannelInfos (0),
		 numOutputChannelInfos (0)
	{
		jassert (deviceID != 0);

		updateDetailsFromDevice();

		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioObjectPropertySelectorWildcard;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementWildcard;

		AudioObjectAddPropertyListener (deviceID, &pa, deviceListenerProc, this);
	}

	~CoreAudioInternal()
	{
		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioObjectPropertySelectorWildcard;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementWildcard;

		AudioObjectRemovePropertyListener (deviceID, &pa, deviceListenerProc, this);

		stop (false);
		delete inputDevice;
	}

	void allocateTempBuffers()
	{
		const int tempBufSize = bufferSize + 4;
		audioBuffer.calloc ((numInputChans + numOutputChans) * tempBufSize);

		tempInputBuffers.calloc (numInputChans + 2);
		tempOutputBuffers.calloc (numOutputChans + 2);

		int i, count = 0;
		for (i = 0; i < numInputChans; ++i)
			tempInputBuffers[i] = audioBuffer + count++ * tempBufSize;

		for (i = 0; i < numOutputChans; ++i)
			tempOutputBuffers[i] = audioBuffer + count++ * tempBufSize;
	}

	// returns the number of actual available channels
	void fillInChannelInfo (const bool input)
	{
		int chanNum = 0;
		UInt32 size;

		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioDevicePropertyStreamConfiguration;
		pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
		pa.mElement = kAudioObjectPropertyElementMaster;

		if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size)))
		{
			HeapBlock <AudioBufferList> bufList;
			bufList.calloc (size, 1);

			if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, bufList)))
			{
				const int numStreams = bufList->mNumberBuffers;

				for (int i = 0; i < numStreams; ++i)
				{
					const AudioBuffer& b = bufList->mBuffers[i];

					for (unsigned int j = 0; j < b.mNumberChannels; ++j)
					{
						String name;

						{
							char channelName [256];
							zerostruct (channelName);
							UInt32 nameSize = sizeof (channelName);
							UInt32 channelNum = chanNum + 1;
							pa.mSelector = kAudioDevicePropertyChannelName;

							if (AudioObjectGetPropertyData (deviceID, &pa, sizeof (channelNum), &channelNum, &nameSize, channelName) == noErr)
								name = String::fromUTF8 (channelName, nameSize);
						}

						if (input)
						{
							if (activeInputChans[chanNum])
							{
								inputChannelInfo [numInputChannelInfos].streamNum = i;
								inputChannelInfo [numInputChannelInfos].dataOffsetSamples = j;
								inputChannelInfo [numInputChannelInfos].dataStrideSamples = b.mNumberChannels;
								++numInputChannelInfos;
							}

							if (name.isEmpty())
								name << "Input " << (chanNum + 1);

							inChanNames.add (name);
						}
						else
						{
							if (activeOutputChans[chanNum])
							{
								outputChannelInfo [numOutputChannelInfos].streamNum = i;
								outputChannelInfo [numOutputChannelInfos].dataOffsetSamples = j;
								outputChannelInfo [numOutputChannelInfos].dataStrideSamples = b.mNumberChannels;
								++numOutputChannelInfos;
							}

							if (name.isEmpty())
								name << "Output " << (chanNum + 1);

							outChanNames.add (name);
						}

						++chanNum;
					}
				}
			}
		}
	}

	void updateDetailsFromDevice()
	{
		stopTimer();

		if (deviceID == 0)
			return;

		const ScopedLock sl (callbackLock);

		Float64 sr;
		UInt32 size = sizeof (Float64);

		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioDevicePropertyNominalSampleRate;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementMaster;

		if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &sr)))
			sampleRate = sr;

		UInt32 framesPerBuf;
		size = sizeof (framesPerBuf);

		pa.mSelector = kAudioDevicePropertyBufferFrameSize;
		if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &framesPerBuf)))
		{
			bufferSize = framesPerBuf;
			allocateTempBuffers();
		}

		bufferSizes.clear();

		pa.mSelector = kAudioDevicePropertyBufferFrameSizeRange;

		if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size)))
		{
			HeapBlock <AudioValueRange> ranges;
			ranges.calloc (size, 1);

			if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, ranges)))
			{
				bufferSizes.add ((int) ranges[0].mMinimum);

				for (int i = 32; i < 2048; i += 32)
				{
					for (int j = size / (int) sizeof (AudioValueRange); --j >= 0;)
					{
						if (i >= ranges[j].mMinimum && i <= ranges[j].mMaximum)
						{
							bufferSizes.addIfNotAlreadyThere (i);
							break;
						}
					}
				}

				if (bufferSize > 0)
					bufferSizes.addIfNotAlreadyThere (bufferSize);
			}
		}

		if (bufferSizes.size() == 0 && bufferSize > 0)
			bufferSizes.add (bufferSize);

		sampleRates.clear();
		const double possibleRates[] = { 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0 };
		String rates;

		pa.mSelector = kAudioDevicePropertyAvailableNominalSampleRates;

		if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size)))
		{
			HeapBlock <AudioValueRange> ranges;
			ranges.calloc (size, 1);

			if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, ranges)))
			{
				for (int i = 0; i < numElementsInArray (possibleRates); ++i)
				{
					bool ok = false;

					for (int j = size / (int) sizeof (AudioValueRange); --j >= 0;)
						if (possibleRates[i] >= ranges[j].mMinimum - 2 && possibleRates[i] <= ranges[j].mMaximum + 2)
							ok = true;

					if (ok)
					{
						sampleRates.add (possibleRates[i]);
						rates << possibleRates[i] << ' ';
					}
				}
			}
		}

		if (sampleRates.size() == 0 && sampleRate > 0)
		{
			sampleRates.add (sampleRate);
			rates << sampleRate;
		}

		log ("sr: " + rates);

		inputLatency = 0;
		outputLatency = 0;
		UInt32 lat;
		size = sizeof (lat);
		pa.mSelector = kAudioDevicePropertyLatency;
		pa.mScope = kAudioDevicePropertyScopeInput;
		//if (AudioDeviceGetProperty (deviceID, 0, true, kAudioDevicePropertyLatency, &size, &lat) == noErr)
		if (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &lat) == noErr)
			inputLatency = (int) lat;

		pa.mScope = kAudioDevicePropertyScopeOutput;
		size = sizeof (lat);

		if (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &lat) == noErr)
			outputLatency = (int) lat;

		log ("lat: " + String (inputLatency) + " " + String (outputLatency));

		inChanNames.clear();
		outChanNames.clear();

		inputChannelInfo.calloc (numInputChans + 2);
		numInputChannelInfos = 0;

		outputChannelInfo.calloc (numOutputChans + 2);
		numOutputChannelInfos = 0;

		fillInChannelInfo (true);
		fillInChannelInfo (false);
	}

	const StringArray getSources (bool input)
	{
		StringArray s;
		HeapBlock <OSType> types;
		const int num = getAllDataSourcesForDevice (deviceID, input, types);

		for (int i = 0; i < num; ++i)
		{
			AudioValueTranslation avt;
			char buffer[256];

			avt.mInputData = &(types[i]);
			avt.mInputDataSize = sizeof (UInt32);
			avt.mOutputData = buffer;
			avt.mOutputDataSize = 256;

			UInt32 transSize = sizeof (avt);

			AudioObjectPropertyAddress pa;
			pa.mSelector = kAudioDevicePropertyDataSourceNameForID;
			pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
			pa.mElement = kAudioObjectPropertyElementMaster;

			if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &transSize, &avt)))
			{
				DBG (buffer);
				s.add (buffer);
			}
		}

		return s;
	}

	int getCurrentSourceIndex (bool input) const
	{
		OSType currentSourceID = 0;
		UInt32 size = sizeof (currentSourceID);
		int result = -1;

		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioDevicePropertyDataSource;
		pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
		pa.mElement = kAudioObjectPropertyElementMaster;

		if (deviceID != 0)
		{
			if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &currentSourceID)))
			{
				HeapBlock <OSType> types;
				const int num = getAllDataSourcesForDevice (deviceID, input, types);

				for (int i = 0; i < num; ++i)
				{
					if (types[num] == currentSourceID)
					{
						result = i;
						break;
					}
				}
			}
		}

		return result;
	}

	void setCurrentSourceIndex (int index, bool input)
	{
		if (deviceID != 0)
		{
			HeapBlock <OSType> types;
			const int num = getAllDataSourcesForDevice (deviceID, input, types);

			if (((unsigned int) index) < (unsigned int) num)
			{
				AudioObjectPropertyAddress pa;
				pa.mSelector = kAudioDevicePropertyDataSource;
				pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
				pa.mElement = kAudioObjectPropertyElementMaster;

				OSType typeId = types[index];

				OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (typeId), &typeId));
			}
		}
	}

	const String reopen (const BigInteger& inputChannels,
						 const BigInteger& outputChannels,
						 double newSampleRate,
						 int bufferSizeSamples)
	{
		String error;
		log ("CoreAudio reopen");
		callbacksAllowed = false;
		stopTimer();

		stop (false);

		activeInputChans = inputChannels;
		activeInputChans.setRange (inChanNames.size(),
								   activeInputChans.getHighestBit() + 1 - inChanNames.size(),
								   false);

		activeOutputChans = outputChannels;
		activeOutputChans.setRange (outChanNames.size(),
									activeOutputChans.getHighestBit() + 1 - outChanNames.size(),
									false);

		numInputChans = activeInputChans.countNumberOfSetBits();
		numOutputChans = activeOutputChans.countNumberOfSetBits();

		// set sample rate
		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioDevicePropertyNominalSampleRate;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementMaster;
		Float64 sr = newSampleRate;

		if (! OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (sr), &sr)))
		{
			error = "Couldn't change sample rate";
		}
		else
		{
			// change buffer size
			UInt32 framesPerBuf = bufferSizeSamples;
			pa.mSelector = kAudioDevicePropertyBufferFrameSize;

			if (! OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (framesPerBuf), &framesPerBuf)))
			{
				error = "Couldn't change buffer size";
			}
			else
			{
				// Annoyingly, after changing the rate and buffer size, some devices fail to
				// correctly report their new settings until some random time in the future, so
				// after calling updateDetailsFromDevice, we need to manually bodge these values
				// to make sure we're using the correct numbers..
				updateDetailsFromDevice();
				sampleRate = newSampleRate;
				bufferSize = bufferSizeSamples;

				if (sampleRates.size() == 0)
					error = "Device has no available sample-rates";
				else if (bufferSizes.size() == 0)
					error = "Device has no available buffer-sizes";
				else if (inputDevice != 0)
					error = inputDevice->reopen (inputChannels,
												 outputChannels,
												 newSampleRate,
												 bufferSizeSamples);
			}
		}

		callbacksAllowed = true;
		return error;
	}

	bool start (AudioIODeviceCallback* cb)
	{
		if (! started)
		{
			callback = 0;

			if (deviceID != 0)
			{
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
				if (OK (AudioDeviceAddIOProc (deviceID, audioIOProc, this)))
#else
				if (OK (AudioDeviceCreateIOProcID (deviceID, audioIOProc, this, &audioProcID)))
#endif
				{
					if (OK (AudioDeviceStart (deviceID, audioIOProc)))
					{
						started = true;
					}
					else
					{
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
						OK (AudioDeviceRemoveIOProc (deviceID, audioIOProc));
#else
						OK (AudioDeviceDestroyIOProcID (deviceID, audioProcID));
						audioProcID = 0;
#endif
					}
				}
			}
		}

		if (started)
		{
			const ScopedLock sl (callbackLock);
			callback = cb;
		}

		if (inputDevice != 0)
			return started && inputDevice->start (cb);
		else
			return started;
	}

	void stop (bool leaveInterruptRunning)
	{
		{
			const ScopedLock sl (callbackLock);
			callback = 0;
		}

		if (started
			 && (deviceID != 0)
			 && ! leaveInterruptRunning)
		{
			OK (AudioDeviceStop (deviceID, audioIOProc));

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
			OK (AudioDeviceRemoveIOProc (deviceID, audioIOProc));
#else
			OK (AudioDeviceDestroyIOProcID (deviceID, audioProcID));
			audioProcID = 0;
#endif
			started = false;

			{ const ScopedLock sl (callbackLock); }

			// wait until it's definately stopped calling back..
			for (int i = 40; --i >= 0;)
			{
				Thread::sleep (50);

				UInt32 running = 0;
				UInt32 size = sizeof (running);

				AudioObjectPropertyAddress pa;
				pa.mSelector = kAudioDevicePropertyDeviceIsRunning;
				pa.mScope = kAudioObjectPropertyScopeWildcard;
				pa.mElement = kAudioObjectPropertyElementMaster;

				OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &running));

				if (running == 0)
					break;
			}

			const ScopedLock sl (callbackLock);
		}

		if (inputDevice != 0)
			inputDevice->stop (leaveInterruptRunning);
	}

	double getSampleRate() const
	{
		return sampleRate;
	}

	int getBufferSize() const
	{
		return bufferSize;
	}

	void audioCallback (const AudioBufferList* inInputData,
						AudioBufferList* outOutputData)
	{
		int i;
		const ScopedLock sl (callbackLock);

		if (callback != 0)
		{
			if (inputDevice == 0)
			{
				for (i = numInputChans; --i >= 0;)
				{
					const CallbackDetailsForChannel& info = inputChannelInfo[i];
					float* dest = tempInputBuffers [i];
					const float* src = ((const float*) inInputData->mBuffers[info.streamNum].mData)
										+ info.dataOffsetSamples;
					const int stride = info.dataStrideSamples;

					if (stride != 0) // if this is zero, info is invalid
					{
						for (int j = bufferSize; --j >= 0;)
						{
							*dest++ = *src;
							src += stride;
						}
					}
				}
			}

			if (! isSlaveDevice)
			{
				if (inputDevice == 0)
				{
					callback->audioDeviceIOCallback (const_cast<const float**> (tempInputBuffers.getData()),
													 numInputChans,
													 tempOutputBuffers,
													 numOutputChans,
													 bufferSize);
				}
				else
				{
					jassert (inputDevice->bufferSize == bufferSize);

					// Sometimes the two linked devices seem to get their callbacks in
					// parallel, so we need to lock both devices to stop the input data being
					// changed while inside our callback..
					const ScopedLock sl2 (inputDevice->callbackLock);

					callback->audioDeviceIOCallback (const_cast<const float**> (inputDevice->tempInputBuffers.getData()),
													 inputDevice->numInputChans,
													 tempOutputBuffers,
													 numOutputChans,
													 bufferSize);
				}

				for (i = numOutputChans; --i >= 0;)
				{
					const CallbackDetailsForChannel& info = outputChannelInfo[i];
					const float* src = tempOutputBuffers [i];
					float* dest = ((float*) outOutputData->mBuffers[info.streamNum].mData)
									+ info.dataOffsetSamples;
					const int stride = info.dataStrideSamples;

					if (stride != 0) // if this is zero, info is invalid
					{
						for (int j = bufferSize; --j >= 0;)
						{
							*dest = *src++;
							dest += stride;
						}
					}
				}
			}
		}
		else
		{
			for (i = jmin (numOutputChans, numOutputChannelInfos); --i >= 0;)
			{
				const CallbackDetailsForChannel& info = outputChannelInfo[i];
				float* dest = ((float*) outOutputData->mBuffers[info.streamNum].mData)
								+ info.dataOffsetSamples;
				const int stride = info.dataStrideSamples;

				if (stride != 0) // if this is zero, info is invalid
				{
					for (int j = bufferSize; --j >= 0;)
					{
						*dest = 0.0f;
						dest += stride;
					}
				}
			}
		}
	}

	// called by callbacks
	void deviceDetailsChanged()
	{
		if (callbacksAllowed)
			startTimer (100);
	}

	void timerCallback()
	{
		stopTimer();
		log ("CoreAudio device changed callback");

		const double oldSampleRate = sampleRate;
		const int oldBufferSize = bufferSize;
		updateDetailsFromDevice();

		if (oldBufferSize != bufferSize || oldSampleRate != sampleRate)
		{
			callbacksAllowed = false;
			stop (false);
			updateDetailsFromDevice();
			callbacksAllowed = true;
		}
	}

	CoreAudioInternal* getRelatedDevice() const
	{
		UInt32 size = 0;
		ScopedPointer <CoreAudioInternal> result;

		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioDevicePropertyRelatedDevices;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementMaster;

		if (deviceID != 0
			 && AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size) == noErr
			 && size > 0)
		{
			HeapBlock <AudioDeviceID> devs;
			devs.calloc (size, 1);

			if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, devs)))
			{
				for (unsigned int i = 0; i < size / sizeof (AudioDeviceID); ++i)
				{
					if (devs[i] != deviceID && devs[i] != 0)
					{
						result = new CoreAudioInternal (devs[i]);

						const bool thisIsInput = inChanNames.size() > 0 && outChanNames.size() == 0;
						const bool otherIsInput = result->inChanNames.size() > 0 && result->outChanNames.size() == 0;

						if (thisIsInput != otherIsInput
							 || (inChanNames.size() + outChanNames.size() == 0)
							 || (result->inChanNames.size() + result->outChanNames.size()) == 0)
							break;

						result = 0;
					}
				}
			}
		}

		return result.release();
	}

	juce_UseDebuggingNewOperator

	int inputLatency, outputLatency;
	BigInteger activeInputChans, activeOutputChans;
	StringArray inChanNames, outChanNames;
	Array <double> sampleRates;
	Array <int> bufferSizes;
	AudioIODeviceCallback* callback;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
	AudioDeviceIOProcID audioProcID;
#endif

	CoreAudioInternal* inputDevice;
	bool isSlaveDevice;

private:
	CriticalSection callbackLock;
	AudioDeviceID deviceID;
	bool started;
	double sampleRate;
	int bufferSize;
	HeapBlock <float> audioBuffer;
	int numInputChans, numOutputChans;
	bool callbacksAllowed;

	struct CallbackDetailsForChannel
	{
		int streamNum;
		int dataOffsetSamples;
		int dataStrideSamples;
	};

	int numInputChannelInfos, numOutputChannelInfos;
	HeapBlock <CallbackDetailsForChannel> inputChannelInfo, outputChannelInfo;
	HeapBlock <float*> tempInputBuffers, tempOutputBuffers;

	CoreAudioInternal (const CoreAudioInternal&);
	CoreAudioInternal& operator= (const CoreAudioInternal&);

	static OSStatus audioIOProc (AudioDeviceID inDevice,
								 const AudioTimeStamp* inNow,
								 const AudioBufferList* inInputData,
								 const AudioTimeStamp* inInputTime,
								 AudioBufferList* outOutputData,
								 const AudioTimeStamp* inOutputTime,
								 void* device)
	{
		static_cast <CoreAudioInternal*> (device)->audioCallback (inInputData, outOutputData);
		return noErr;
	}

	static OSStatus deviceListenerProc (AudioDeviceID /*inDevice*/, UInt32 /*inLine*/, const AudioObjectPropertyAddress* pa, void* inClientData)
	{
		CoreAudioInternal* const intern = static_cast <CoreAudioInternal*> (inClientData);

		switch (pa->mSelector)
		{
			case kAudioDevicePropertyBufferSize:
			case kAudioDevicePropertyBufferFrameSize:
			case kAudioDevicePropertyNominalSampleRate:
			case kAudioDevicePropertyStreamFormat:
			case kAudioDevicePropertyDeviceIsAlive:
				intern->deviceDetailsChanged();
				break;

			case kAudioDevicePropertyBufferSizeRange:
			case kAudioDevicePropertyVolumeScalar:
			case kAudioDevicePropertyMute:
			case kAudioDevicePropertyPlayThru:
			case kAudioDevicePropertyDataSource:
			case kAudioDevicePropertyDeviceIsRunning:
				break;
		}

		return noErr;
	}

	static int getAllDataSourcesForDevice (AudioDeviceID deviceID, const bool input, HeapBlock <OSType>& types)
	{
		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioDevicePropertyDataSources;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementMaster;
		UInt32 size = 0;

		if (deviceID != 0
			 && OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size)))
		{
			types.calloc (size, 1);

			if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, types)))
				return size / (int) sizeof (OSType);
		}

		return 0;
	}
};

class CoreAudioIODevice   : public AudioIODevice
{
public:
	CoreAudioIODevice (const String& deviceName,
					   AudioDeviceID inputDeviceId,
					   const int inputIndex_,
					   AudioDeviceID outputDeviceId,
					   const int outputIndex_)
		: AudioIODevice (deviceName, "CoreAudio"),
		  inputIndex (inputIndex_),
		  outputIndex (outputIndex_),
		  isOpen_ (false),
		  isStarted (false)
	{
		internal = 0;
		CoreAudioInternal* device = 0;

		if (outputDeviceId == 0 || outputDeviceId == inputDeviceId)
		{
			jassert (inputDeviceId != 0);

			device = new CoreAudioInternal (inputDeviceId);
		}
		else
		{
			device = new CoreAudioInternal (outputDeviceId);

			if (inputDeviceId != 0)
			{
				CoreAudioInternal* secondDevice = new CoreAudioInternal (inputDeviceId);

				device->inputDevice = secondDevice;
				secondDevice->isSlaveDevice = true;
			}
		}

		internal = device;

		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioObjectPropertySelectorWildcard;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementWildcard;

		AudioObjectAddPropertyListener (kAudioObjectSystemObject, &pa, hardwareListenerProc, internal);
	}

	~CoreAudioIODevice()
	{
		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioObjectPropertySelectorWildcard;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementWildcard;

		AudioObjectRemovePropertyListener (kAudioObjectSystemObject, &pa, hardwareListenerProc, internal);

		delete internal;
	}

	const StringArray getOutputChannelNames()
	{
		return internal->outChanNames;
	}

	const StringArray getInputChannelNames()
	{
		if (internal->inputDevice != 0)
			return internal->inputDevice->inChanNames;
		else
			return internal->inChanNames;
	}

	int getNumSampleRates()
	{
		return internal->sampleRates.size();
	}

	double getSampleRate (int index)
	{
		return internal->sampleRates [index];
	}

	int getNumBufferSizesAvailable()
	{
		return internal->bufferSizes.size();
	}

	int getBufferSizeSamples (int index)
	{
		return internal->bufferSizes [index];
	}

	int getDefaultBufferSize()
	{
		for (int i = 0; i < getNumBufferSizesAvailable(); ++i)
			if (getBufferSizeSamples(i) >= 512)
				return getBufferSizeSamples(i);

		return 512;
	}

	const String open (const BigInteger& inputChannels,
					   const BigInteger& outputChannels,
					   double sampleRate,
					   int bufferSizeSamples)
	{
		isOpen_ = true;

		if (bufferSizeSamples <= 0)
			bufferSizeSamples = getDefaultBufferSize();

		lastError = internal->reopen (inputChannels, outputChannels, sampleRate, bufferSizeSamples);
		isOpen_ = lastError.isEmpty();
		return lastError;
	}

	void close()
	{
		isOpen_ = false;
		internal->stop (false);
	}

	bool isOpen()
	{
		return isOpen_;
	}

	int getCurrentBufferSizeSamples()
	{
		return internal != 0 ? internal->getBufferSize() : 512;
	}

	double getCurrentSampleRate()
	{
		return internal != 0 ? internal->getSampleRate() : 0;
	}

	int getCurrentBitDepth()
	{
		return 32;  // no way to find out, so just assume it's high..
	}

	const BigInteger getActiveOutputChannels() const
	{
		return internal != 0 ? internal->activeOutputChans : BigInteger();
	}

	const BigInteger getActiveInputChannels() const
	{
		BigInteger chans;

		if (internal != 0)
		{
			chans = internal->activeInputChans;

			if (internal->inputDevice != 0)
				chans |= internal->inputDevice->activeInputChans;
		}

		return chans;
	}

	int getOutputLatencyInSamples()
	{
		if (internal == 0)
			return 0;

		// this seems like a good guess at getting the latency right - comparing
		// this with a round-trip measurement, it gets it to within a few millisecs
		// for the built-in mac soundcard
		return internal->outputLatency + internal->getBufferSize() * 2;
	}

	int getInputLatencyInSamples()
	{
		if (internal == 0)
			return 0;

		return internal->inputLatency + internal->getBufferSize() * 2;
	}

	void start (AudioIODeviceCallback* callback)
	{
		if (internal != 0 && ! isStarted)
		{
			if (callback != 0)
				callback->audioDeviceAboutToStart (this);

			isStarted = true;
			internal->start (callback);
		}
	}

	void stop()
	{
		if (isStarted && internal != 0)
		{
			AudioIODeviceCallback* const lastCallback = internal->callback;

			isStarted = false;
			internal->stop (true);

			if (lastCallback != 0)
				lastCallback->audioDeviceStopped();
		}
	}

	bool isPlaying()
	{
		if (internal->callback == 0)
			isStarted = false;

		return isStarted;
	}

	const String getLastError()
	{
		return lastError;
	}

	int inputIndex, outputIndex;

	juce_UseDebuggingNewOperator

private:
	CoreAudioInternal* internal;
	bool isOpen_, isStarted;
	String lastError;

	static OSStatus hardwareListenerProc (AudioDeviceID /*inDevice*/, UInt32 /*inLine*/, const AudioObjectPropertyAddress* pa, void* inClientData)
	{
		CoreAudioInternal* const intern = static_cast <CoreAudioInternal*> (inClientData);

		switch (pa->mSelector)
		{
			case kAudioHardwarePropertyDevices:
				intern->deviceDetailsChanged();
				break;

			case kAudioHardwarePropertyDefaultOutputDevice:
			case kAudioHardwarePropertyDefaultInputDevice:
			case kAudioHardwarePropertyDefaultSystemOutputDevice:
				break;
		}

		return noErr;
	}

	CoreAudioIODevice (const CoreAudioIODevice&);
	CoreAudioIODevice& operator= (const CoreAudioIODevice&);
};

class CoreAudioIODeviceType  : public AudioIODeviceType
{
public:

	CoreAudioIODeviceType()
		: AudioIODeviceType ("CoreAudio"),
		  hasScanned (false)
	{
	}

	~CoreAudioIODeviceType()
	{
	}

	void scanForDevices()
	{
		hasScanned = true;

		inputDeviceNames.clear();
		outputDeviceNames.clear();
		inputIds.clear();
		outputIds.clear();

		UInt32 size;

		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioHardwarePropertyDevices;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementMaster;

		if (OK (AudioObjectGetPropertyDataSize (kAudioObjectSystemObject, &pa, 0, 0, &size)))
		{
			HeapBlock <AudioDeviceID> devs;
			devs.calloc (size, 1);

			if (OK (AudioObjectGetPropertyData (kAudioObjectSystemObject, &pa, 0, 0, &size, devs)))
			{
				static bool alreadyLogged = false;
				const int num = size / (int) sizeof (AudioDeviceID);
				for (int i = 0; i < num; ++i)
				{
					char name [1024];
					size = sizeof (name);
					pa.mSelector = kAudioDevicePropertyDeviceName;

					if (OK (AudioObjectGetPropertyData (devs[i], &pa, 0, 0, &size, name)))
					{
						const String nameString (String::fromUTF8 (name, (int) strlen (name)));

						if (! alreadyLogged)
							log ("CoreAudio device: " + nameString);

						const int numIns = getNumChannels (devs[i], true);
						const int numOuts = getNumChannels (devs[i], false);

						if (numIns > 0)
						{
							inputDeviceNames.add (nameString);
							inputIds.add (devs[i]);
						}

						if (numOuts > 0)
						{
							outputDeviceNames.add (nameString);
							outputIds.add (devs[i]);
						}
					}
				}

				alreadyLogged = true;
			}
		}

		inputDeviceNames.appendNumbersToDuplicates (false, true);
		outputDeviceNames.appendNumbersToDuplicates (false, true);
	}

	const StringArray getDeviceNames (bool wantInputNames) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		if (wantInputNames)
			return inputDeviceNames;
		else
			return outputDeviceNames;
	}

	int getDefaultDeviceIndex (bool forInput) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		AudioDeviceID deviceID;
		UInt32 size = sizeof (deviceID);

		// if they're asking for any input channels at all, use the default input, so we
		// get the built-in mic rather than the built-in output with no inputs..

		AudioObjectPropertyAddress pa;
		pa.mSelector = forInput ? kAudioHardwarePropertyDefaultInputDevice : kAudioHardwarePropertyDefaultOutputDevice;
		pa.mScope = kAudioObjectPropertyScopeWildcard;
		pa.mElement = kAudioObjectPropertyElementMaster;

		if (AudioObjectGetPropertyData (kAudioObjectSystemObject, &pa, 0, 0, &size, &deviceID) == noErr)
		{
			if (forInput)
			{
				for (int i = inputIds.size(); --i >= 0;)
					if (inputIds[i] == deviceID)
						return i;
			}
			else
			{
				for (int i = outputIds.size(); --i >= 0;)
					if (outputIds[i] == deviceID)
						return i;
			}
		}

		return 0;
	}

	int getIndexOfDevice (AudioIODevice* device, bool asInput) const
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		CoreAudioIODevice* const d = dynamic_cast <CoreAudioIODevice*> (device);
		if (d == 0)
			return -1;

		return asInput ? d->inputIndex
					   : d->outputIndex;
	}

	bool hasSeparateInputsAndOutputs() const	{ return true; }

	AudioIODevice* createDevice (const String& outputDeviceName,
								 const String& inputDeviceName)
	{
		jassert (hasScanned); // need to call scanForDevices() before doing this

		const int inputIndex = inputDeviceNames.indexOf (inputDeviceName);
		const int outputIndex = outputDeviceNames.indexOf (outputDeviceName);

		String deviceName (outputDeviceName);
		if (deviceName.isEmpty())
			deviceName = inputDeviceName;

		if (index >= 0)
			return new CoreAudioIODevice (deviceName,
										  inputIds [inputIndex],
										  inputIndex,
										  outputIds [outputIndex],
										  outputIndex);

		return 0;
	}

	juce_UseDebuggingNewOperator

private:
	StringArray inputDeviceNames, outputDeviceNames;
	Array <AudioDeviceID> inputIds, outputIds;

	bool hasScanned;

	static int getNumChannels (AudioDeviceID deviceID, bool input)
	{
		int total = 0;
		UInt32 size;

		AudioObjectPropertyAddress pa;
		pa.mSelector = kAudioDevicePropertyStreamConfiguration;
		pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
		pa.mElement = kAudioObjectPropertyElementMaster;

		if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size)))
		{
			HeapBlock <AudioBufferList> bufList;
			bufList.calloc (size, 1);

			if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, bufList)))
			{
				const int numStreams = bufList->mNumberBuffers;

				for (int i = 0; i < numStreams; ++i)
				{
					const AudioBuffer& b = bufList->mBuffers[i];
					total += b.mNumberChannels;
				}
			}
		}

		return total;
	}

	CoreAudioIODeviceType (const CoreAudioIODeviceType&);
	CoreAudioIODeviceType& operator= (const CoreAudioIODeviceType&);
};

AudioIODeviceType* juce_createAudioIODeviceType_CoreAudio()
{
	return new CoreAudioIODeviceType();
}

#undef log

#endif
/*** End of inlined file: juce_mac_CoreAudio.cpp ***/


/*** Start of inlined file: juce_mac_CoreMidi.cpp ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE

#if JUCE_MAC

#undef log
#define log(a) Logger::writeToLog(a)

static bool logAnyErrorsMidi (const OSStatus err, const int lineNum)
{
	if (err == noErr)
		return true;

	log ("CoreMidi error: " + String (lineNum) + " - " + String::toHexString ((int) err));
	jassertfalse
	return false;
}

#undef OK
#define OK(a) logAnyErrorsMidi(a, __LINE__)

static const String getEndpointName (MIDIEndpointRef endpoint, bool isExternal)
{
	String result;
	CFStringRef str = 0;

	MIDIObjectGetStringProperty (endpoint, kMIDIPropertyName, &str);

	if (str != 0)
	{
		result = PlatformUtilities::cfStringToJuceString (str);
		CFRelease (str);
		str = 0;
	}

	MIDIEntityRef entity = 0;
	MIDIEndpointGetEntity (endpoint, &entity);

	if (entity == 0)
		return result; // probably virtual

	if (result.isEmpty())
	{
		// endpoint name has zero length - try the entity
		MIDIObjectGetStringProperty (entity, kMIDIPropertyName, &str);

		if (str != 0)
		{
			result += PlatformUtilities::cfStringToJuceString (str);
			CFRelease (str);
			str = 0;
		}
	}

	// now consider the device's name
	MIDIDeviceRef device = 0;
	MIDIEntityGetDevice (entity, &device);
	if (device == 0)
		return result;

	MIDIObjectGetStringProperty (device, kMIDIPropertyName, &str);

	if (str != 0)
	{
		const String s (PlatformUtilities::cfStringToJuceString (str));
		CFRelease (str);

		// if an external device has only one entity, throw away
		// the endpoint name and just use the device name
		if (isExternal && MIDIDeviceGetNumberOfEntities (device) < 2)
		{
			result = s;
		}
		else if (! result.startsWithIgnoreCase (s))
		{
			// prepend the device name to the entity name
			result = (s + " " + result).trimEnd();
		}
	}

	return result;
}

static const String getConnectedEndpointName (MIDIEndpointRef endpoint)
{
	String result;

	// Does the endpoint have connections?
	CFDataRef connections = 0;
	int numConnections = 0;

	MIDIObjectGetDataProperty (endpoint, kMIDIPropertyConnectionUniqueID, &connections);

	if (connections != 0)
	{
		numConnections = (int) (CFDataGetLength (connections) / sizeof (MIDIUniqueID));

		if (numConnections > 0)
		{
			const SInt32* pid = reinterpret_cast <const SInt32*> (CFDataGetBytePtr (connections));

			for (int i = 0; i < numConnections; ++i, ++pid)
			{
				MIDIUniqueID uid = EndianS32_BtoN (*pid);
				MIDIObjectRef connObject;
				MIDIObjectType connObjectType;
				OSStatus err = MIDIObjectFindByUniqueID (uid, &connObject, &connObjectType);

				if (err == noErr)
				{
					String s;

					if (connObjectType == kMIDIObjectType_ExternalSource
						 || connObjectType == kMIDIObjectType_ExternalDestination)
					{
						// Connected to an external device's endpoint (10.3 and later).
						s = getEndpointName (static_cast <MIDIEndpointRef> (connObject), true);
					}
					else
					{
						// Connected to an external device (10.2) (or something else, catch-all)
						CFStringRef str = 0;
						MIDIObjectGetStringProperty (connObject, kMIDIPropertyName, &str);

						if (str != 0)
						{
							s = PlatformUtilities::cfStringToJuceString (str);
							CFRelease (str);
						}
					}

					if (s.isNotEmpty())
					{
						if (result.isNotEmpty())
							result += ", ";

						result += s;
					}
				}
			}
		}

		CFRelease (connections);
	}

	if (result.isNotEmpty())
		return result;

	// Here, either the endpoint had no connections, or we failed to obtain names for any of them.
	return getEndpointName (endpoint, false);
}

const StringArray MidiOutput::getDevices()
{
	StringArray s;

	const ItemCount num = MIDIGetNumberOfDestinations();
	for (ItemCount i = 0; i < num; ++i)
	{
		MIDIEndpointRef dest = MIDIGetDestination (i);

		if (dest != 0)
		{
			String name (getConnectedEndpointName (dest));

			if (name.isEmpty())
				name = "<error>";

			s.add (name);
		}
		else
		{
			s.add ("<error>");
		}
	}

	return s;
}

int MidiOutput::getDefaultDeviceIndex()
{
	return 0;
}

static MIDIClientRef globalMidiClient;
static bool hasGlobalClientBeenCreated = false;

static bool makeSureClientExists()
{
	if (! hasGlobalClientBeenCreated)
	{
		String name ("JUCE");

		if (JUCEApplication::getInstance() != 0)
			name = JUCEApplication::getInstance()->getApplicationName();

		CFStringRef appName = PlatformUtilities::juceStringToCFString (name);

		hasGlobalClientBeenCreated = OK (MIDIClientCreate (appName, 0, 0, &globalMidiClient));
		CFRelease (appName);
	}

	return hasGlobalClientBeenCreated;
}

class MidiPortAndEndpoint
{
public:
	MidiPortAndEndpoint (MIDIPortRef port_, MIDIEndpointRef endPoint_)
		: port (port_), endPoint (endPoint_)
	{
	}

	~MidiPortAndEndpoint()
	{
		if (port != 0)
			MIDIPortDispose (port);

		if (port == 0 && endPoint != 0) // if port == 0, it means we created the endpoint, so it's safe to delete it
			MIDIEndpointDispose (endPoint);
	}

	MIDIPortRef port;
	MIDIEndpointRef endPoint;
};

MidiOutput* MidiOutput::openDevice (int index)
{
	MidiOutput* mo = 0;

	if (((unsigned int) index) < (unsigned int) MIDIGetNumberOfDestinations())
	{
		MIDIEndpointRef endPoint = MIDIGetDestination (index);

		CFStringRef pname;
		if (OK (MIDIObjectGetStringProperty (endPoint, kMIDIPropertyName, &pname)))
		{
			log ("CoreMidi - opening out: " + PlatformUtilities::cfStringToJuceString (pname));

			if (makeSureClientExists())
			{
				MIDIPortRef port;

				if (OK (MIDIOutputPortCreate (globalMidiClient, pname, &port)))
				{
					mo = new MidiOutput();
					mo->internal = new MidiPortAndEndpoint (port, endPoint);
				}
			}

			CFRelease (pname);
		}
	}

	return mo;
}

MidiOutput* MidiOutput::createNewDevice (const String& deviceName)
{
	MidiOutput* mo = 0;

	if (makeSureClientExists())
	{
		MIDIEndpointRef endPoint;
		CFStringRef name = PlatformUtilities::juceStringToCFString (deviceName);

		if (OK (MIDISourceCreate (globalMidiClient, name, &endPoint)))
		{
			mo = new MidiOutput();
			mo->internal = new MidiPortAndEndpoint (0, endPoint);
		}

		CFRelease (name);
	}

	return mo;
}

MidiOutput::~MidiOutput()
{
	delete (MidiPortAndEndpoint*) internal;
}

void MidiOutput::reset()
{
}

bool MidiOutput::getVolume (float& leftVol, float& rightVol)
{
	return false;
}

void MidiOutput::setVolume (float leftVol, float rightVol)
{
}

void MidiOutput::sendMessageNow (const MidiMessage& message)
{
	MidiPortAndEndpoint* const mpe = (MidiPortAndEndpoint*) internal;

	if (message.isSysEx())
	{
		const int maxPacketSize = 256;
		int pos = 0, bytesLeft = message.getRawDataSize();
		const int numPackets = (bytesLeft + maxPacketSize - 1) / maxPacketSize;
		HeapBlock <MIDIPacketList> packets;
		packets.malloc (32 * numPackets + message.getRawDataSize(), 1);
		packets->numPackets = numPackets;

		MIDIPacket* p = packets->packet;

		for (int i = 0; i < numPackets; ++i)
		{
			p->timeStamp = 0;
			p->length = jmin (maxPacketSize, bytesLeft);
			memcpy (p->data, message.getRawData() + pos, p->length);
			pos += p->length;
			bytesLeft -= p->length;
			p = MIDIPacketNext (p);
		}

		if (mpe->port != 0)
			MIDISend (mpe->port, mpe->endPoint, packets);
		else
			MIDIReceived (mpe->endPoint, packets);
	}
	else
	{
		MIDIPacketList packets;
		packets.numPackets = 1;
		packets.packet[0].timeStamp = 0;
		packets.packet[0].length = message.getRawDataSize();
		*(int*) (packets.packet[0].data) = *(const int*) message.getRawData();

		if (mpe->port != 0)
			MIDISend (mpe->port, mpe->endPoint, &packets);
		else
			MIDIReceived (mpe->endPoint, &packets);
	}
}

const StringArray MidiInput::getDevices()
{
	StringArray s;

	const ItemCount num = MIDIGetNumberOfSources();
	for (ItemCount i = 0; i < num; ++i)
	{
		MIDIEndpointRef source = MIDIGetSource (i);

		if (source != 0)
		{
			String name (getConnectedEndpointName (source));

			if (name.isEmpty())
				name = "<error>";

			s.add (name);
		}
		else
		{
			s.add ("<error>");
		}
	}

	return s;
}

int MidiInput::getDefaultDeviceIndex()
{
	return 0;
}

struct MidiPortAndCallback
{
	MidiInput* input;
	MidiPortAndEndpoint* portAndEndpoint;
	MidiInputCallback* callback;
	MemoryBlock pendingData;
	int pendingBytes;
	double pendingDataTime;
	bool active;

	void processSysex (const uint8*& d, int& size, const double time)
	{
		if (*d == 0xf0)
		{
			pendingBytes = 0;
			pendingDataTime = time;
		}

		pendingData.ensureSize (pendingBytes + size, false);
		uint8* totalMessage = (uint8*) pendingData.getData();

		uint8* dest = totalMessage + pendingBytes;

		while (size > 0)
		{
			if (pendingBytes > 0 && *d >= 0x80)
			{
				if (*d >= 0xfa || *d == 0xf8)
				{
					callback->handleIncomingMidiMessage (input, MidiMessage (*d, time));
					++d;
					--size;
				}
				else
				{
					if (*d == 0xf7)
					{
						*dest++ = *d++;
						pendingBytes++;
						--size;
					}

					break;
				}
			}
			else
			{
				*dest++ = *d++;
				pendingBytes++;
				--size;
			}
		}

		if (totalMessage [pendingBytes - 1] == 0xf7)
		{
			callback->handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingBytes, pendingDataTime));
			pendingBytes = 0;
		}
		else
		{
			callback->handlePartialSysexMessage (input, totalMessage, pendingBytes, pendingDataTime);
		}
	}
};

namespace CoreMidiCallbacks
{
	static CriticalSection callbackLock;
	static VoidArray activeCallbacks;
}

static void midiInputProc (const MIDIPacketList* pktlist,
						   void* readProcRefCon,
						   void* srcConnRefCon)
{
	double time = Time::getMillisecondCounterHiRes() * 0.001;
	const double originalTime = time;

	MidiPortAndCallback* const mpc = (MidiPortAndCallback*) readProcRefCon;
	const ScopedLock sl (CoreMidiCallbacks::callbackLock);

	if (CoreMidiCallbacks::activeCallbacks.contains (mpc) && mpc->active)
	{
		const MIDIPacket* packet = &pktlist->packet[0];

		for (unsigned int i = 0; i < pktlist->numPackets; ++i)
		{
			const uint8* d = (const uint8*) (packet->data);
			int size = packet->length;

			while (size > 0)
			{
				time = originalTime;

				if (mpc->pendingBytes > 0 || d[0] == 0xf0)
				{
					mpc->processSysex (d, size, time);
				}
				else
				{
					int used = 0;
					const MidiMessage m (d, size, used, 0, time);

					if (used <= 0)
					{
						jassertfalse // malformed midi message
						break;
					}
					else
					{
						mpc->callback->handleIncomingMidiMessage (mpc->input, m);
					}

					size -= used;
					d += used;
				}
			}

			packet = MIDIPacketNext (packet);
		}
	}
}

MidiInput* MidiInput::openDevice (int index, MidiInputCallback* callback)
{
	MidiInput* mi = 0;

	if (((unsigned int) index) < (unsigned int) MIDIGetNumberOfSources())
	{
		MIDIEndpointRef endPoint = MIDIGetSource (index);

		if (endPoint != 0)
		{
			CFStringRef pname;

			if (OK (MIDIObjectGetStringProperty (endPoint, kMIDIPropertyName, &pname)))
			{
				log ("CoreMidi - opening inp: " + PlatformUtilities::cfStringToJuceString (pname));

				if (makeSureClientExists())
				{
					MIDIPortRef port;

					ScopedPointer <MidiPortAndCallback> mpc (new MidiPortAndCallback());
					mpc->active = false;

					if (OK (MIDIInputPortCreate (globalMidiClient, pname, midiInputProc, mpc, &port)))
					{
						if (OK (MIDIPortConnectSource (port, endPoint, 0)))
						{
							mpc->portAndEndpoint = new MidiPortAndEndpoint (port, endPoint);
							mpc->callback = callback;
							mpc->pendingBytes = 0;
							mpc->pendingData.ensureSize (128);

							mi = new MidiInput (getDevices() [index]);
							mpc->input = mi;
							mi->internal = mpc;

							const ScopedLock sl (CoreMidiCallbacks::callbackLock);
							CoreMidiCallbacks::activeCallbacks.add (mpc.release());
						}
						else
						{
							OK (MIDIPortDispose (port));
						}
					}
				}
			}

			CFRelease (pname);
		}
	}

	return mi;
}

MidiInput* MidiInput::createNewDevice (const String& deviceName, MidiInputCallback* callback)
{
	MidiInput* mi = 0;

	if (makeSureClientExists())
	{
		ScopedPointer <MidiPortAndCallback> mpc (new MidiPortAndCallback());
		mpc->active = false;

		MIDIEndpointRef endPoint;
		CFStringRef name = PlatformUtilities::juceStringToCFString(deviceName);
		if (OK (MIDIDestinationCreate (globalMidiClient, name, midiInputProc, mpc, &endPoint)))
		{
			mpc->portAndEndpoint = new MidiPortAndEndpoint (0, endPoint);
			mpc->callback = callback;
			mpc->pendingBytes = 0;
			mpc->pendingData.ensureSize (128);

			mi = new MidiInput (deviceName);
			mpc->input = mi;
			mi->internal = mpc;

			const ScopedLock sl (CoreMidiCallbacks::callbackLock);
			CoreMidiCallbacks::activeCallbacks.add (mpc.release());
		}

		CFRelease (name);
	}

	return mi;
}

MidiInput::MidiInput (const String& name_)
	: name (name_)
{
}

MidiInput::~MidiInput()
{
	MidiPortAndCallback* const mpc = (MidiPortAndCallback*) internal;
	mpc->active = false;

	{
		const ScopedLock sl (CoreMidiCallbacks::callbackLock);
		CoreMidiCallbacks::activeCallbacks.removeValue (mpc);
	}

	if (mpc->portAndEndpoint->port != 0)
		OK (MIDIPortDisconnectSource (mpc->portAndEndpoint->port, mpc->portAndEndpoint->endPoint));

	delete mpc->portAndEndpoint;
	delete mpc;
}

void MidiInput::start()
{
	const ScopedLock sl (CoreMidiCallbacks::callbackLock);
	((MidiPortAndCallback*) internal)->active = true;
}

void MidiInput::stop()
{
	const ScopedLock sl (CoreMidiCallbacks::callbackLock);
	((MidiPortAndCallback*) internal)->active = false;
}

#undef log

#else

MidiOutput::~MidiOutput()
{
}

void MidiOutput::reset()
{
}

bool MidiOutput::getVolume (float& leftVol, float& rightVol)
{
	return false;
}

void MidiOutput::setVolume (float leftVol, float rightVol)
{
}

void MidiOutput::sendMessageNow (const MidiMessage& message)
{
}

const StringArray MidiOutput::getDevices()
{
	return StringArray();
}

MidiOutput* MidiOutput::openDevice (int index)
{
	return 0;
}

const StringArray MidiInput::getDevices()
{
	return StringArray();
}

MidiInput* MidiInput::openDevice (int index, MidiInputCallback* callback)
{
	return 0;
}

#endif

#endif
/*** End of inlined file: juce_mac_CoreMidi.cpp ***/


/*** Start of inlined file: juce_mac_CameraDevice.mm ***/
// (This file gets included by juce_mac_NativeCode.mm, rather than being
// compiled on its own).
#if JUCE_INCLUDED_FILE && JUCE_USE_CAMERA

#if ! JUCE_QUICKTIME
 #error "On the Mac, cameras use Quicktime, so if you turn on JUCE_USE_CAMERA, you also need to enable JUCE_QUICKTIME"
#endif

#define QTCaptureCallbackDelegate MakeObjCClassName(QTCaptureCallbackDelegate)

class QTCameraDeviceInteral;

END_JUCE_NAMESPACE

@interface QTCaptureCallbackDelegate	: NSObject
{
@public
	CameraDevice* owner;
	QTCameraDeviceInteral* internal;
	int64 firstPresentationTime;
	int64 averageTimeOffset;
}

- (QTCaptureCallbackDelegate*) initWithOwner: (CameraDevice*) owner internalDev: (QTCameraDeviceInteral*) d;
- (void) dealloc;

- (void) captureOutput: (QTCaptureOutput*) captureOutput
		 didOutputVideoFrame: (CVImageBufferRef) videoFrame
		 withSampleBuffer: (QTSampleBuffer*) sampleBuffer
		 fromConnection: (QTCaptureConnection*) connection;

- (void) captureOutput: (QTCaptureFileOutput*) captureOutput
		 didOutputSampleBuffer: (QTSampleBuffer*) sampleBuffer
		 fromConnection: (QTCaptureConnection*) connection;
@end

BEGIN_JUCE_NAMESPACE

class QTCameraDeviceInteral
{
public:
	QTCameraDeviceInteral (CameraDevice* owner, int index)
	{
		const ScopedAutoReleasePool pool;

		session = [[QTCaptureSession alloc] init];

		NSArray* devs = [QTCaptureDevice inputDevicesWithMediaType: QTMediaTypeVideo];
		device = (QTCaptureDevice*) [devs objectAtIndex: index];
		input = 0;
		audioInput = 0;
		audioDevice = 0;
		fileOutput = 0;
		imageOutput = 0;
		callbackDelegate = [[QTCaptureCallbackDelegate alloc] initWithOwner: owner
																internalDev: this];

		NSError* err = 0;
		[device retain];
		[device open: &err];

		if (err == 0)
		{
			input = [[QTCaptureDeviceInput alloc] initWithDevice: device];
			audioInput = [[QTCaptureDeviceInput alloc] initWithDevice: device];

			[session addInput: input error: &err];

			if (err == 0)
			{
				resetFile();

				imageOutput = [[QTCaptureDecompressedVideoOutput alloc] init];
				[imageOutput setDelegate: callbackDelegate];

				if (err == 0)
				{
					[session startRunning];
					return;
				}
			}
		}

		openingError = nsStringToJuce ([err description]);
		DBG (openingError);
	}

	~QTCameraDeviceInteral()
	{
		[session stopRunning];
		[session removeOutput: imageOutput];

		[session release];
		[input release];
		[device release];
		[audioDevice release];
		[audioInput release];
		[fileOutput release];
		[imageOutput release];
		[callbackDelegate release];
	}

	void resetFile()
	{
		[fileOutput recordToOutputFileURL: nil];
		[session removeOutput: fileOutput];
		[fileOutput release];
		fileOutput = [[QTCaptureMovieFileOutput alloc] init];

		[session removeInput: audioInput];
		[audioInput release];
		audioInput = 0;
		[audioDevice release];
		audioDevice = 0;

		[fileOutput setDelegate: callbackDelegate];
	}

	void addDefaultAudioInput()
	{
		NSError* err = nil;
		audioDevice = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeSound];

		if ([audioDevice open: &err])
			[audioDevice retain];
		else
			audioDevice = nil;

		if (audioDevice != 0)
		{
			audioInput = [[QTCaptureDeviceInput alloc] initWithDevice: audioDevice];
			[session addInput: audioInput error: &err];
		}
	}

	void addListener (CameraImageListener* listenerToAdd)
	{
		const ScopedLock sl (listenerLock);

		if (listeners.size() == 0)
			[session addOutput: imageOutput error: nil];

		listeners.addIfNotAlreadyThere (listenerToAdd);
	}

	void removeListener (CameraImageListener* listenerToRemove)
	{
		const ScopedLock sl (listenerLock);
		listeners.removeValue (listenerToRemove);

		if (listeners.size() == 0)
			[session removeOutput: imageOutput];
	}

	void callListeners (CIImage* frame, int w, int h)
	{
		CoreGraphicsImage image (Image::ARGB, w, h, false);
		CIContext* cic = [CIContext contextWithCGContext: image.context options: nil];
		[cic drawImage: frame inRect: CGRectMake (0, 0, w, h) fromRect: CGRectMake (0, 0, w, h)];
		CGContextFlush (image.context);

		const ScopedLock sl (listenerLock);

		for (int i = listeners.size(); --i >= 0;)
		{
			CameraImageListener* l = (CameraImageListener*) listeners[i];

			if (l != 0)
				l->imageReceived (image);
		}
	}

	QTCaptureDevice* device;
	QTCaptureDeviceInput* input;
	QTCaptureDevice* audioDevice;
	QTCaptureDeviceInput* audioInput;
	QTCaptureSession* session;
	QTCaptureMovieFileOutput* fileOutput;
	QTCaptureDecompressedVideoOutput* imageOutput;
	QTCaptureCallbackDelegate* callbackDelegate;
	String openingError;

	VoidArray listeners;
	CriticalSection listenerLock;
};

END_JUCE_NAMESPACE
@implementation QTCaptureCallbackDelegate

- (QTCaptureCallbackDelegate*) initWithOwner: (CameraDevice*) owner_
								 internalDev: (QTCameraDeviceInteral*) d
{
	[super init];
	owner = owner_;
	internal = d;
	firstPresentationTime = 0;
	averageTimeOffset = 0;
	return self;
}

- (void) dealloc
{
	[super dealloc];
}

- (void) captureOutput: (QTCaptureOutput*) captureOutput
		 didOutputVideoFrame: (CVImageBufferRef) videoFrame
		 withSampleBuffer: (QTSampleBuffer*) sampleBuffer
		 fromConnection: (QTCaptureConnection*) connection
{
	if (internal->listeners.size() > 0)
	{
		const ScopedAutoReleasePool pool;

		internal->callListeners ([CIImage imageWithCVImageBuffer: videoFrame],
								 CVPixelBufferGetWidth (videoFrame),
								 CVPixelBufferGetHeight (videoFrame));
	}
}

- (void) captureOutput: (QTCaptureFileOutput*) captureOutput
		 didOutputSampleBuffer: (QTSampleBuffer*) sampleBuffer
		 fromConnection: (QTCaptureConnection*) connection
{
	const Time now (Time::getCurrentTime());

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
	NSNumber* hosttime = (NSNumber*) [sampleBuffer attributeForKey: QTSampleBufferHostTimeAttribute];
#else
	NSNumber* hosttime = (NSNumber*) [sampleBuffer attributeForKey: @"hostTime"];
#endif

	int64 presentationTime = (hosttime != nil)
			? ((int64) AudioConvertHostTimeToNanos ([hosttime unsignedLongLongValue]) / 1000000 + 40)
			: (([sampleBuffer presentationTime].timeValue * 1000) / [sampleBuffer presentationTime].timeScale + 50);

	const int64 timeDiff = now.toMilliseconds() - presentationTime;

	if (firstPresentationTime == 0)
	{
		firstPresentationTime = presentationTime;
		averageTimeOffset = timeDiff;
	}
	else
	{
		averageTimeOffset = (averageTimeOffset * 120 + timeDiff * 8) / 128;
	}
}

@end
BEGIN_JUCE_NAMESPACE

class QTCaptureViewerComp : public NSViewComponent
{
public:
	QTCaptureViewerComp (CameraDevice* const cameraDevice, QTCameraDeviceInteral* const internal)
	{
		const ScopedAutoReleasePool pool;
		captureView = [[QTCaptureView alloc] init];
		[captureView setCaptureSession: internal->session];

		setSize (640, 480); //  xxx need to somehow get the movie size - how?
		setView (captureView);
	}

	~QTCaptureViewerComp()
	{
		setView (0);
		[captureView setCaptureSession: nil];
		[captureView release];
	}

	QTCaptureView* captureView;
};

CameraDevice::CameraDevice (const String& name_, int index)
	: name (name_)
{
	isRecording = false;
	internal = new QTCameraDeviceInteral (this, index);
}

CameraDevice::~CameraDevice()
{
	stopRecording();
	delete static_cast <QTCameraDeviceInteral*> (internal);
	internal = 0;
}

Component* CameraDevice::createViewerComponent()
{
	return new QTCaptureViewerComp (this, static_cast <QTCameraDeviceInteral*> (internal));
}

const String CameraDevice::getFileExtension()
{
	return ".mov";
}

void CameraDevice::startRecordingToFile (const File& file, int quality)
{
	stopRecording();

	QTCameraDeviceInteral* const d = static_cast <QTCameraDeviceInteral*> (internal);
	d->callbackDelegate->firstPresentationTime = 0;
	file.deleteFile();

	// In some versions of QT (e.g. on 10.5), if you record video without audio, the speed comes
	// out wrong, so we'll put some audio in there too..,
	d->addDefaultAudioInput();

	[d->session addOutput: d->fileOutput error: nil];

	NSEnumerator* connectionEnumerator = [[d->fileOutput connections] objectEnumerator];

	for (;;)
	{
		QTCaptureConnection* connection = [connectionEnumerator nextObject];
		if (connection == 0)
			break;

		QTCompressionOptions* options = 0;
		NSString* mediaType = [connection mediaType];

		if ([mediaType isEqualToString: QTMediaTypeVideo])
			options = [QTCompressionOptions compressionOptionsWithIdentifier:
							quality >= 1 ? @"QTCompressionOptionsSD480SizeH264Video"
										 : @"QTCompressionOptions240SizeH264Video"];
		else if ([mediaType isEqualToString: QTMediaTypeSound])
			options = [QTCompressionOptions compressionOptionsWithIdentifier: @"QTCompressionOptionsHighQualityAACAudio"];

		[d->fileOutput setCompressionOptions: options forConnection: connection];
	}

	[d->fileOutput recordToOutputFileURL: [NSURL fileURLWithPath: juceStringToNS (file.getFullPathName())]];
	isRecording = true;
}

const Time CameraDevice::getTimeOfFirstRecordedFrame() const
{
	QTCameraDeviceInteral* const d = static_cast <QTCameraDeviceInteral*> (internal);
	if (d->callbackDelegate->firstPresentationTime != 0)
		return Time (d->callbackDelegate->firstPresentationTime + d->callbackDelegate->averageTimeOffset);

	return Time();
}

void CameraDevice::stopRecording()
{
	if (isRecording)
	{
		static_cast <QTCameraDeviceInteral*> (internal)->resetFile();
		isRecording = false;
	}
}

void CameraDevice::addListener (CameraImageListener* listenerToAdd)
{
	if (listenerToAdd != 0)
		static_cast <QTCameraDeviceInteral*> (internal)->addListener (listenerToAdd);
}

void CameraDevice::removeListener (CameraImageListener* listenerToRemove)
{
	if (listenerToRemove != 0)
		static_cast <QTCameraDeviceInteral*> (internal)->removeListener (listenerToRemove);
}

const StringArray CameraDevice::getAvailableDevices()
{
	const ScopedAutoReleasePool pool;

	StringArray results;
	NSArray* devs = [QTCaptureDevice inputDevicesWithMediaType: QTMediaTypeVideo];

	for (int i = 0; i < (int) [devs count]; ++i)
	{
		QTCaptureDevice* dev = (QTCaptureDevice*) [devs objectAtIndex: i];
		results.add (nsStringToJuce ([dev localizedDisplayName]));
	}

	return results;
}

CameraDevice* CameraDevice::openDevice (int index,
										int minWidth, int minHeight,
										int maxWidth, int maxHeight)
{
	ScopedPointer <CameraDevice> d (new CameraDevice (getAvailableDevices() [index], index));

	if (static_cast <QTCameraDeviceInteral*> (d->internal)->openingError.isEmpty())
		return d.release();

	return 0;
}

#endif
/*** End of inlined file: juce_mac_CameraDevice.mm ***/

 #endif
#endif

END_JUCE_NAMESPACE

#endif
/*** End of inlined file: juce_mac_NativeCode.mm ***/


 #endif
#endif
