Initializes the buffered compression engine.
Overload List
Overload | Description |
StartCompress(Int32,Int32,Int32,RasterByteOrder,RasterViewPerspective,Int32,Byte[],Int32,Int32,CodecsCompression,CodecsCompressDataCallback) |
Initializes the buffered compression engine.
|
StartCompress(Int32,Int32,Int32,RasterByteOrder,RasterViewPerspective,Int32,IntPtr,Int32,CodecsCompression,CodecsCompressDataCallback) |
Initializes the unmanaged memory buffered compression engine.
|
Example
Visual Basic | Copy 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"
Dim image As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)
If image.ViewPerspective <> RasterViewPerspective.TopLeft Then
image.ChangeViewPerspective(RasterViewPerspective.TopLeft)
End If
_compressStream = File.Create(destFileName)
Dim lineBytes As Integer = image.Width * 3
Dim inBuffer As Byte() = New Byte(16 * lineBytes - 1) {}
Dim outBuffer As Byte() = New Byte(1023) {}
image.Access()
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)
Dim i As Integer = 0
Do While i < image.Height
Dim j As Integer = 0
Dim inBufferIndex As Integer = 0
Do While (i + j) < image.Height AndAlso j < 16
image.GetRow(i + j, inBuffer, inBufferIndex, lineBytes)
inBufferIndex += lineBytes
j += 1
Loop
codecs.Compress(inBuffer, 0)
i += 16
Loop
codecs.StopCompress()
image.Release()
_compressStream.Close()
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
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
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