Leadtools.Codecs Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.17
StartCompress Method
See Also  Example
Leadtools.Codecs Namespace > RasterCodecs Class : StartCompress Method




Initializes the buffered compression engine.

Overload List

Example

Visual BasicCopy Code
RasterCodecs.Compress
      Public Sub CompressExample()
         RasterCodecs.Startup()
         Dim codecs As RasterCodecs = New RasterCodecs()

         Dim srcFileName As String = "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image1.cmp"
         Dim destFileName As String = "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image1_CompressData.cmp"

         ' Load the image to at 24-bits per pixel
         Dim image As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)
         If image.ViewPerspective <> RasterViewPerspective.TopLeft Then
            image.ChangeViewPerspective(RasterViewPerspective.TopLeft)
         End If

         ' Create the output file
         _compressStream = File.Create(destFileName)

         ' Calculate the bytes per line in the input buffer, without padding
         Dim lineBytes As Integer = image.Width * 3

         ' Allocate a buffer for the incoming uncompressed data. Note that we
         ' are compressing 16 lines at a time. You should always use multiples of 16
         Dim inBuffer As Byte() = New Byte(16 * lineBytes - 1) {}

         ' Allocate an output buffer. This is where the compressed data will
         ' go. Note that this allocates 1024-byte packets.
         Dim outBuffer As Byte() = New Byte(1023) {}

         ' Lock down the image
         image.Access()

         ' Initialize the compression engine
         codecs.Options.Jpeg.Save.CmpQualityFactorPredefined = CodecsCmpQualityFactorPredefined.QualityAndSize
         codecs.StartCompress(image.Width, image.Height, image.BitsPerPixel, image.Order, image.ViewPerspective, 16 * lineBytes, outBuffer, 0, outBuffer.Length, CodecsCompression.Cmp, AddressOf MyCodecsCompressDataCallback)

         ' Compress the data
         Dim i As Integer = 0
         Do While i < image.Height ' i is incremented at the end
            ' Compression of the 16-line chunk starts here
            Dim j As Integer = 0
            Dim inBufferIndex As Integer = 0
            Do While (i + j) < image.Height AndAlso j < 16
               ' Get one line at time
               image.GetRow(i + j, inBuffer, inBufferIndex, lineBytes)

               ' Move to next line
               inBufferIndex += lineBytes

               j += 1
            Loop

            ' This is the main function that will do the actual Compression
            codecs.Compress(inBuffer, 0)
            i += 16
         Loop

         ' Reset the compression engine
         codecs.StopCompress()

         ' Release the image and close the file
         image.Release()
         _compressStream.Close()

         ' Clean up
         codecs.Dispose()
         RasterCodecs.Shutdown()
      End Sub

      Private _compressStream As FileStream
      Private _compressBuffer As Byte()

      Private Function MyCodecsCompressDataCallback(ByVal width As Integer, ByVal height As Integer, ByVal bitsPerPixel As Integer, ByVal order As RasterByteOrder, ByVal viewPerspective As RasterViewPerspective, ByVal data As IntPtr, ByVal dataLength As Integer) As Boolean
         ' Write data to the file
         If _compressBuffer Is Nothing OrElse _compressBuffer.Length < dataLength Then
            _compressBuffer = New Byte(dataLength - 1) {}
         End If

         Marshal.Copy(data, _compressBuffer, 0, dataLength)

         _compressStream.Write(_compressBuffer, 0, dataLength)

         Return True
      End Function
C#Copy Code
RasterCodecs.Compress 
      public void CompressExample() 
      { 
         RasterCodecs.Startup(); 
         RasterCodecs codecs = new RasterCodecs(); 
 
         string srcFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image1.cmp"; 
         string destFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image1_CompressData.cmp"; 
 
         // Load the image to at 24-bits per pixel 
         RasterImage image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1); 
         if(image.ViewPerspective != RasterViewPerspective.TopLeft) 
            image.ChangeViewPerspective(RasterViewPerspective.TopLeft); 
 
         // Create the output file 
         _compressStream = File.Create(destFileName); 
 
         // Calculate the bytes per line in the input buffer, without padding  
         int lineBytes = image.Width * 3; 
 
         // Allocate a buffer for the incoming uncompressed data. Note that we 
         //  are compressing 16 lines at a time. You should always use multiples of 16 
         byte[] inBuffer = new byte[16 * lineBytes]; 
 
         // Allocate an output buffer. This is where the compressed data will 
         // go.  Note that this allocates 1024-byte packets.  
         byte[] outBuffer = new byte[1024]; 
 
         // Lock down the image 
         image.Access(); 
 
         // Initialize the compression engine 
         codecs.Options.Jpeg.Save.CmpQualityFactorPredefined = CodecsCmpQualityFactorPredefined.QualityAndSize; 
         codecs.StartCompress( 
            image.Width, 
            image.Height, 
            image.BitsPerPixel, 
            image.Order, 
            image.ViewPerspective, 
            16 * lineBytes, 
            outBuffer, 
            0, 
            outBuffer.Length, 
            CodecsCompression.Cmp, 
            MyCodecsCompressDataCallback); 
 
         // Compress the data 
         int i = 0; 
         while(i < image.Height) // i is incremented at the end 
         { 
            // Compression of the 16-line chunk starts here 
            int j = 0; 
            int inBufferIndex = 0; 
            while((i + j) < image.Height && j < 16) 
            { 
               // Get one line at time 
               image.GetRow(i + j, inBuffer, inBufferIndex, lineBytes); 
 
               // Move to next line 
               inBufferIndex += lineBytes; 
 
               j++; 
            } 
 
            // This is the main function that will do the actual Compression 
            codecs.Compress(inBuffer, 0); 
            i += 16; 
         } 
 
         // Reset the compression engine  
         codecs.StopCompress(); 
 
         // Release the image and close the file 
         image.Release(); 
         _compressStream.Close(); 
 
         // Clean up 
         codecs.Dispose(); 
         RasterCodecs.Shutdown(); 
      } 
 
      FileStream _compressStream; 
      byte[] _compressBuffer; 
 
      bool MyCodecsCompressDataCallback(int width, int height, int bitsPerPixel, RasterByteOrder order, RasterViewPerspective viewPerspective, IntPtr data, int dataLength) 
      { 
         // Write data to the file 
         if(_compressBuffer == null || _compressBuffer.Length < dataLength) 
            _compressBuffer = new byte[dataLength]; 
 
         Marshal.Copy(data, _compressBuffer, 0, dataLength); 
 
         _compressStream.Write(_compressBuffer, 0, dataLength); 
 
         return true; 
      }

Remarks

This method initializes the buffered compression engine. The compression is then carried out using the Compress method. It is ended by the StopCompress method.

If order is set to to RasterByteOrder.Bgr and viewPerspective is RasterViewPerspective.TopLeft then the data that you put into the input buffer must be RasterByteOrder.Bgr and loaded from top left.

The compression process starts after the first call to Compress. The callback is called when the output buffer is filled with compressed data or after completing the compression process. callback is responsible for emptying the output buffer - storing it, sending it, or doing other processing.

The following is a flow chart that shows the relationship of these methods:

Call StopCompress to end the compression process started by a call to StartCompress.

The quality factor of the compressed data is obtained as follows:

FormatQuality factor used
CodecsCompression.CmpCodecsJpegSaveOptions.QualityFactor and CodecsJpegSaveOptions.CmpQualityFactorPredefined
CodecsCompression.Jpeg444CodecsJpegSaveOptions.QualityFactor
CodecsCompression.Jpeg422CodecsJpegSaveOptions.QualityFactor
CodecsCompression.Jpeg411CodecsJpegSaveOptions.QualityFactor
CodecsCompression.TifJpeg444CodecsJpegSaveOptions.QualityFactor
CodecsCompression.TifJpeg422CodecsJpegSaveOptions.QualityFactor
CodecsCompression.TifJpeg411CodecsJpegSaveOptions.QualityFactor
CodecsCompression.Lead0Not used
CodecsCompression.Lead1Not used
CodecsCompression.TiffCcittNot used
CodecsCompression.TiffCcittG3Fax1DimNot used
CodecsCompression.TiffCcittG3Fax2DimNot used
CodecsCompression.TiffCcittG4FaxNot used

This method does not support signed data images.

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also