Attribute VB_Name = "Helpers"
Rem
Rem
Rem
Rem               INTEL CORPORATION PROPRIETARY INFORMATION
Rem  This software is supplied under the terms of a license agreement or
Rem  nondisclosure agreement with Intel Corporation and may not be copied
Rem  or disclosed except in accordance with the terms of that agreement.
Rem      Copyright (c) 1998 Intel Corporation. All Rights Reserved.
Rem
Rem
Rem  File:
Rem    helpers.bas
Rem
Rem  Purpose:
Rem    Helper functions
Rem


Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpvDest As Long, ByVal lpvSource As Long, ByVal cbCopy As Long)


Public Function ShowErrorMsg(ByVal context As String, ByVal code As IJLERR)
  Dim message As String
  
  message = "IJL ERROR: [" & code & "]" & " - " & context
  
  Call MsgBox(message, vbExclamation, "Intel(R) JPEG Library")

End Function


Public Function SaveJPG(ByRef cDib As cDIBSection, ByVal sFile As String, ByVal cal As String) As Boolean
  Dim jerr As IJLERR
  Dim jcprops As JPEG_CORE_PROPERTIES
  Dim aFile As String
  Dim lPtr As Long
   
 jerr = ijlInit(jcprops)
 If jerr = IJL_OK Then
   ' Set up the DIB information:
   
   ' DIB width
   jcprops.DIBWidth = cDib.dib_width
   ' DIB height
   jcprops.DIBHeight = -cDib.dib_height
   ' DIB number of channels
   jcprops.DIBChannels = cDib.dib_channels
   ' DIB color space
   If jcprops.DIBChannels = 3 Then
     jcprops.DIBColor = IJL_BGR
     jcprops.JPGColor = IJL_YCBCR
     jcprops.JPGChannels = 3
     jcprops.JPGSubsampling = IJL_411
   Else
     jcprops.DIBColor = IJL_RGBA_FPX
     jcprops.JPGColor = IJL_YCBCRA_FPX
     jcprops.JPGChannels = 4
     jcprops.JPGSubsampling = IJL_4114
   End If
   ' DIBBytes (pointer to uncompressed RGB data):
   jcprops.DIBBytes = cDib.DIBSectionBitsPtr
   ' DIBPadBytes
   jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth, jcprops.DIBChannels)

   ' Set up the JPEG information:
      
    aFile = StrConv(sFile, vbFromUnicode)
      
   ' JPEG filename
    jcprops.JPGFile = StrPtr(aFile)
      
   ' JPG width
   jcprops.JPGWidth = cDib.dib_width
   ' JPG height
   jcprops.JPGHeight = cDib.dib_height
   ' JPEG quality
   jcprops.jquality = cal

   ' Encode the image into file
   jerr = ijlWrite(jcprops, IJL_JFILE_WRITEWHOLEIMAGE)
   If jerr = IJL_OK Then
     SaveJPG = True
   Else
     ' Throw error
     Call ShowErrorMsg("Failed to save to JPG", jerr)
   End If
      
   ' Ensure we have freed memory:
   Call ijlFree(jcprops)
 
 Else
   ' Throw error:
   Call ShowErrorMsg("Failed to initialise the IJL library", jerr)
 
 End If
   
End Function

