' Opens JPEG2000 file and gets its first tile's image by parts.
Public Shared Function GetJpeg2000TileByParts(jpeg2000Filename As String) As Vintasoft.Imaging.VintasoftImage
' open an existing JPEG2000 file
Using file As New Vintasoft.Imaging.Codecs.ImageFiles.Jpeg2000.Jpeg2000File(jpeg2000Filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)
' get JPEG2000 page
Dim page As Vintasoft.Imaging.Codecs.ImageFiles.Jpeg2000.Jpeg2000Page = file.Page
' get page dimensions and pixel format
Dim imageWidth As Integer = page.Width
Dim imageHeight As Integer = page.Height
Dim palette As Vintasoft.Imaging.Palette = If(page.Palette IsNot Nothing, page.Palette, New Vintasoft.Imaging.Palette())
Dim imageInfo As New Vintasoft.Imaging.Codecs.Decoders.ImageInfo(imageWidth, imageHeight, page.BitsPerPixel, palette, page.Resolution)
Dim pixelFormat As Vintasoft.Imaging.PixelFormat = imageInfo.PixelFormat
' get first tile rectangle
Dim firstTileRectangle As System.Drawing.Rectangle = page.GetTileGrid()(0)
' get tile dimensions
Dim tileWidth As Integer = firstTileRectangle.Width
Dim tileHeight As Integer = firstTileRectangle.Height
' if tile is too small, then get tile at once
If tileWidth = 1 OrElse tileHeight = 1 Then
Return page.GetTileRectImage(0, New System.Drawing.Rectangle(0, 0, tileWidth, tileHeight), Nothing)
End If
' divide tile rectangle into 4 rectangles
Dim grid As System.Drawing.Rectangle() = New System.Drawing.Rectangle(3) {}
grid(0) = New System.Drawing.Rectangle(0, 0, tileWidth \ 2, tileHeight \ 2)
grid(1) = New System.Drawing.Rectangle(tileWidth \ 2, 0, (tileWidth + 1) \ 2, tileHeight \ 2)
grid(2) = New System.Drawing.Rectangle(0, tileHeight \ 2, tileWidth \ 2, (tileHeight + 1) \ 2)
grid(3) = New System.Drawing.Rectangle(tileWidth \ 2, tileHeight \ 2, (tileWidth + 1) \ 2, (tileHeight + 1) \ 2)
' get number of wavelet levels
Dim waveletLevel As Integer = page.WaveletLevels
' create a VintasoftImage object
Dim resultImage As New Vintasoft.Imaging.VintasoftImage(tileWidth, tileHeight, pixelFormat)
' for each part of tile
For i As Integer = 0 To 3
' get current rectangle
Dim rectangle As System.Drawing.Rectangle = grid(i)
' get current rectangle location
Dim position As System.Drawing.Point = rectangle.Location
' get image of current rectangle
Using rectImage As Vintasoft.Imaging.VintasoftImage = page.GetTileRectImage(0, rectangle, waveletLevel, Nothing)
' overlay rectangle image on result image
Dim overlayCommand As New Vintasoft.Imaging.ImageProcessing.OverlayCommand(rectImage, position)
overlayCommand.ExecuteInPlace(resultImage)
End Using
Next
Return resultImage
End Using
End Function