<%@ Page debug="true" %>
<%@ import namespace="System" %>
<%@ import namespace="System.Net" %>
<%@ import namespace="viclib" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Selectprocess, a Victor Library demo using asp.net</title>

<script runat="server">

sub Page_Load
        Dim srcimg As vicwin.imgdes
        Dim rcode As Integer
        Dim outbuff As Integer
        Dim ipfunction() As String
        Dim func As String
        Dim imgbytearray() As Byte
        
        ReDim imgbytearray(16)
        If (Request.Params("func") Is Nothing) Then
        Else                            	 ' Otherwise parse the function parameter and process the image
            '  .... Make sure we are still in a valid session ....
            If (Session("sp_sessionflag") = 1) Then
                srcimg = Session("sp_imgdes")
                ipfunction = Request.QueryString.GetValues("func")
                func = ipfunction(0)
            Else
                func = "start"
            End If
      
            rcode = selectprocess(func, srcimg)
            ''''''''''''''''''''''''''''''''''        
            outbuff = 7

            If (rcode = vicwin.NO_ERROR) Then   ' Set the global variables
                Session("sp_imgdes") = srcimg       ' Image descriptor
                Session("sp_sessionflag") = 1       ' Flag to indicate session is active
                rcode = vicwin.savejpgtobuffer(outbuff, srcimg, 75)   ' Save image to a jpg file in memory, quality = 75
                
                If rcode = vicwin.NO_ERROR Then
                    imgbytearray = buffertobytearray(outbuff)
                    vicwin.freebuffer(outbuff)

                    Response.ContentType = "image/jpeg"
                    Response.Expires = 0
                    Response.Buffer = True
                    Response.Clear()
                    Response.BinaryWrite(imgbytearray)  ' Send the image to the browser
                    Response.End()
                End If
            End If
        End If
    End Sub

    Function selectprocess(ByVal ipfunction As String, ByRef srcimg As vicwin.imgdes)
        Dim rcode As Integer

        Select Case ipfunction
            Case "flip"
                rcode = vicwin.flipimage(srcimg, srcimg)
                Exit Select
                  
            Case "mirror"
                rcode = vicwin.mirrorimage(srcimg, srcimg)
                Exit Select
              
            Case "sharpen"
                rcode = vicwin.sharpen(srcimg, srcimg)
                Exit Select

            Case "togray"
                Dim tempimg As vicwin.imgdes
                
                rcode = vicwin.allocimage(tempimg, 160, 120, 8) ' Allocate new 8-bit image to hold grayscale
                If (rcode = vicwin.NO_ERROR) Then
                    rcode = vicwin.colortogray(srcimg, tempimg) ' Create 8-bit grayscale version of image
                    If (rcode = vicwin.NO_ERROR) Then
                        rcode = vicwin.convertpaltorgb(tempimg, srcimg)  ' Convert grayscale back to 24-bit image
                    End If
                    vicwin.freeimage(tempimg)  ' Free the temporary 8-bit image
                End If
                Exit Select
              
            Case "negative"
                rcode = vicwin.negative(srcimg, srcimg)
                Exit Select
              
            Case "kodalith"
                rcode = vicwin.kodalith(128, srcimg, srcimg)
                Exit Select

            Case "reload"
                Dim tempimg As vicwin.imgdes
               
                rcode = loadimagefromurl("http://www.catenarysystems.com/demos/simple/hibiscus.png", tempimg)
                If (rcode = vicwin.NO_ERROR) Then
                    rcode = vicwin.copyimage(tempimg, srcimg)
                End If
                vicwin.freeimage(tempimg)
                Exit Select

            Case "start"
                rcode = loadimagefromurl("http://www.catenarysystems.com/demos/simple/hibiscus.png", srcimg)
                Exit Select

        End Select
      
        selectprocess = rcode
    End Function
    
    
    Public Function loadimagefrombytearray(ByVal bytearray As Byte(), ByVal uploaded_ftype As String, ByRef srcimg As vicwin.imgdes) As Integer
        Dim rcode As Integer
        Dim ver As Short

        ver = vicwin.Victorversion()
        rcode = vicwin.BAD_FAC
      
        Select Case uploaded_ftype
            Case "bmp"
                Dim binfo As vicwin.BITMAPINFOHEADER
                rcode = vicwin.bmpinfofrombuffer(bytearray(0), binfo)
                If (rcode = vicwin.NO_ERROR) Then
                    rcode = vicwin.allocimage(srcimg, binfo.biWidth, binfo.biHeight, binfo.biBitCount)
                    If (rcode = vicwin.NO_ERROR) Then
                        rcode = vicwin.loadbmpfrombuffer(bytearray(0), srcimg)
                    End If
                End If
      	
            Case "gif"
                Dim ginfo As vicwin.GifData
                rcode = vicwin.gifinfofrombuffer(bytearray(0), ginfo)
                If (rcode = vicwin.NO_ERROR) Then
                    rcode = vicwin.allocimage(srcimg, ginfo.width, ginfo.length, ginfo.vbitcount)
                    If (rcode = vicwin.NO_ERROR) Then
                        rcode = vicwin.loadgiffrombuffer(bytearray(0), srcimg)
                    End If
                End If
            Case "jpg"
                Dim jinfo As vicwin.JpegData
                rcode = vicwin.jpeginfofrombuffer(bytearray(0), jinfo)
                If (rcode = vicwin.NO_ERROR) Then
                    rcode = vicwin.allocimage(srcimg, jinfo.width, jinfo.length, jinfo.vbitcount)
                    If (rcode = vicwin.NO_ERROR) Then
                        rcode = vicwin.loadjpgfrombuffer(bytearray(0), srcimg)
                    End If
                End If
            Case "png"
                Dim pinfo As vicwin.PngData
                rcode = vicwin.pnginfofrombuffer(bytearray(0), pinfo)
                If (rcode = vicwin.NO_ERROR) Then
                    rcode = vicwin.allocimage(srcimg, pinfo.width, pinfo.length, pinfo.vbitcount)
                    If (rcode = vicwin.NO_ERROR) Then
                        rcode = vicwin.loadpngfrombuffer(bytearray(0), srcimg)
                    End If
                End If
            Case "tif"
                Dim tinfo As vicwin.TiffData
                rcode = vicwin.tiffinfofrombuffer(bytearray(0), tinfo)
                If (rcode = vicwin.NO_ERROR) Then
                    rcode = vicwin.allocimage(srcimg, tinfo.width, tinfo.length, tinfo.vbitcount)
                    If (rcode = vicwin.NO_ERROR) Then
                        rcode = vicwin.loadtiffrombuffer(bytearray(0), srcimg)
					   
                        ' 16-bit images have to be converted to 8-bit for viewing
                        If (rcode = vicwin.NO_ERROR And tinfo.vbitcount = 16) Then
                            Dim tempimg As vicwin.imgdes
                            ' Allocate a temp image buffer
                            rcode = vicwin.allocimage(tempimg, tinfo.width, tinfo.length, 8)
                            If (rcode = vicwin.NO_ERROR) Then
                                Dim mm As vicwin.MINMAX
                                ' Convert the 16-bit image to 8-bit and replace the original 16-bit with the 8-bit
                                rcode = vicwin.convertgray16to8(srcimg, tempimg)
                                vicwin.freeimage(srcimg)
                                vicwin.copyimgdes(tempimg, srcimg)
                        
                                ' If it was only a 10-bit image, it may be too dark to view, brighten
                                vicwin.calcminmax(srcimg, mm, mm, mm)
                                If (mm.max < 160) Then
                                    vicwin.expandcontrast(0, mm.max, srcimg, srcimg)
                                End If
                            End If
                        End If
                    End If
                End If
        End Select

        If (rcode <> vicwin.NO_ERROR) Then
            vicwin.freeimage(srcimg)      ' Release image buffer
        End If

        loadimagefrombytearray = rcode

    End Function

    
    
    Public Function loadimagefromurl(ByVal fname As String, ByRef srcimg As vicwin.imgdes)
        Dim flen As Integer
        Dim uploaded_data() As Byte     ' Data received
        Dim rcode As Integer
        Dim uploaded_ftype As String
        Dim ver As Short
        ReDim uploaded_data(4)

        ver = vicwin.Victorversion()

        flen = Len(fname)
        uploaded_ftype = LCase(Mid(fname, flen - 2, 3))

        uploaded_data = loadimagedata(fname)
        rcode = loadimagefrombytearray(uploaded_data, uploaded_ftype, srcimg)
        loadimagefromurl = rcode
    End Function
    
    Function loadimagedata(ByVal imageurl As String)
        Dim wc As WebClient
        wc = New WebClient
        Dim count As Integer
        Dim imagedata As Byte()
   
        ReDim imagedata(0)
        count = 0
        While (count < 5 And imagedata.length < 2)
            imagedata = wc.DownloadData(imageurl)
            count = count + 1
        End While
   
        If (imagedata.length > 1) Then

        Else
            ReDim imagedata(0)
        End If

        loadimagedata = imagedata
            
    End Function
    
    Declare Sub RtlMoveMemory Lib "kernel32" (ByVal des As Integer, ByVal src As Integer, ByVal cnt As Integer)
    Declare Sub Copybmh Lib "kernel32" Alias "RtlMoveMemory" (ByRef des As vicwin.BITMAPINFOHEADER, ByVal src As Integer, ByVal cnt As Integer)
    Declare Sub CopyDatatbufftoBytearray Lib "kernel32" Alias "RtlMoveMemory" (ByRef des As Byte, ByVal src As Integer, ByVal cnt As Integer)
    Declare Sub CopyBytearraytoDatabuff Lib "kernel32" Alias "RtlMoveMemory" (ByVal des As Integer, ByRef src As Byte, ByVal cnt As Integer)
    Declare Function GlobalAlloc Lib "kernel32" (ByVal flags As Integer, ByVal dwbytes As Integer) As Integer
    Declare Function GlobalFree Lib "kernel32" (ByVal hmem As Integer) As Integer
    Declare Function GlobalHandle Lib "kernel32" (ByVal Addr As Integer) As Integer
    Declare Function GlobalSize Lib "kernel32" (ByVal hmem As Integer) As Integer

    Public Function buffertobytearray(ByVal buffaddr As Integer)
        Dim barray As Byte()
        Dim buffhandle As Integer
        Dim buffsize As Integer
   
        buffhandle = GlobalHandle(buffaddr)
        buffsize = GlobalSize(buffhandle)
        ReDim barray(buffsize)
        CopyDatatbufftoBytearray(barray(0), buffaddr, buffsize)
        GlobalFree(buffhandle)
        buffertobytearray = barray
    End Function
    
    
    
</script>
</head>
<body>
</body>
</html>