Sub CreateWebPreview(ImageFile, imgRootPath)
	Dim Doc, PScale, xSize, ySize, dStrm, sMin, sMax

' Open the image
	Set Doc = CreateObject("MaxIm.Document")
	Doc.OpenFile ImageFile

/// Next is Russ' change, to get a large document down to a more manageable size
/// It's only a preview image anyway, so some detail can be sacrificed, if it's even noticeable
/// Also remove the hot pixels before going into any further scaling and processing
///

' Bin 2x2 as long as image is >=4x the preview size (binning doesn't blur hot pixels, and is far faster than the Resize function)
    Do While Doc.XSize >= 4096
        Doc.Bin(2)
    Loop

' Filter hot pixels prior to resize
	Doc.KernelFilter 5, 20

/// Now scale the image to the larger size first. 
/// Create the thumbnail later
///
/// This is your (Bob) original scaling algorithm. There's no need to fancy this up in any way
/// because the calculation of the resize sizes is only microseconds. I tried a different approach
/// but the preview image blew up . 
///

' Resize image to max 1024 pixels wide
	PScale = Fix(Doc.XSize / 1024) + 1
	If Doc.XSize >= 1024 And CInt(Doc.XSize / 1024) = Doc.XSize / 1024 Then PScale = PScale - 1 ' Catch edge condition
	If PScale < 1 Then PScale = 1
	xSize = Fix(Doc.XSize / PScale) ' For the dims file
	ySize = Fix(Doc.YSize / PScale)
	
	Doc.Resize xSize, ySize

/// Russ' good idea was to DDP the image AFTER doing the resizing
/// and then not needing a second DDP, which could be quite processor intensive for very large images
/// There are three suggested beautification algorithms
///  1. Yours (Bob), which I've left intact here
///
///  2. Russ', which relied on MaxIm's stretch mode values, in which he preferred to stretch between the 
///     minimum values and the maximum values in the image. He then applied these limits in DDP
///  3. Mine, which also relies on MaxIm's stretch mode, but for me the "Median" is preferable.
///
///  I've put them all in here so you could see them, but I would do the "Plato" thing and use your DDP
///  if I were you. Russ said he would replace your DDP with his preference, and I will probably do 
///  the same with mine. Other users haven't complained about "beauty" so your DDP is good to go.
///

' Beautify image for preview 
	Doc.DDP 0, true, true, 0, 0, 100 ' (0=FFT-lowpass, AutoBgd, AutoMidLevel, Bgd, MidLevel, Cutoff)  ' Denny
	
''''	Doc.StretchMode = 1     	' Set stretch to "Medium" in Screen Stretch Window  ' Berg
''''	Doc.Stretch 0, 1.0, 0, 1    ' (0=linear, 1.0=gamma; 0=256 grays; 1=min/max from Screen Stretch Window)

''''	Doc.StretchMode = 0 		' Croman
''''	sMin = Doc.StretchMin
''''	Doc.StretchMode = 2 
''''	sMax = Doc.StretchMax
''''	sMax = sMax - (sMax - sMin)/2
''''	Doc.DDP 0, false, false, sMin, sMax, 100 

	Doc.RemoveGradient

/// The rest is basically your code (Bob) reordered 
///

	Doc.SaveFile imgRootPath + LSTPNG_FILE, 7, True, 0 ' Scaled, 8-bits/pix, PNG
	On Error Resume Next
	Util.WaitForMilliseconds 500	' [BUG/WORKAROUND] See 25-Oct-2004 edit track

' Save dimensions of preview
	Set dStrm = FSO.CreateTextFile(imgRootPath + LSTDIM_FILE, True) ' Make new preview dimensions file
	dStrm.WriteLine xSize			' Dims one per line
	dStrm.WriteLine ySize
	dStrm.Close


' Create thumbnail at 128 pixel wide
    PScale = Fix(Doc.XSize / 128) + 1
    If Doc.XSize >= 128 And CInt(Doc.XSize / 128) = Doc.XSize / 128 Then PScale = PScale - 1   ' Catch edge condition
    If PScale < 1 Then PScale = 1
    Doc.Resize (Doc.XSize / PScale), (Doc.YSize / PScale)

	Doc.SaveFile imgRootPath + THUMB_FILE, 7, True, 0 ' Scaled, 8-bits/pix, PNG
	On Error Resume Next
	Doc.Close						' Close for MaxIm V4 only
	On Error Goto 0
	Util.WaitForMilliseconds 500	' [BUG/WORKAROUND] See 25-Oct-2004 edit track
End Sub
