Advanced functionality for image decoding
In This Topic
SDK includes the following interfaces for optimal decoding of image data:
- IRasterGridDecoder - the interface can be implemented in raster image decoder, which can use the image grid with scaling for decoding of image.
- ILineInterlacedDecoder - the interface can be implemented in raster image decoder, which can effectively skip one or more image lines while decoding a part of image.
- ISimpleLineInterlacedDecoder - the interface can be implemented in raster image decoder, which can effectively skip one or more image lines while decoding the whole image.
- IVectorDecoder - the interface can be implemented in vector image decoder.
ImageRenderer class decodes a part or a scaled part of image by the most optimal way and without loading the whole image into memory if decoder implements one or several of the following interfaces:
IRasterGridDecoder,
ILineInterlacedDecoder,
ISimpleLineInterlacedDecoder,
IVectorDecoder.
Important! It is recommended to use the
ImageRenderer class for solving the following tasks:
- get any part of image in specified scale synchronously and asynchronously
- render image progressively
- render image or a part of image in several threads
ImageViewer and
WpfImageViewer classes can load only necessary part of image when image is displayed in image viewer if decoder implements one or several of the following interfaces:
IRasterGridDecoder,
ILineInterlacedDecoder,
ISimpleLineInterlacedDecoder,
IVectorDecoder. This functionality can be enabled/disabled using the
ImageViewer.RenderingRequirements and
WpfImageViewerBase.RenderingRequirements properties.
ImageViewer and
WpfImageViewer classes can load image in image viewer progressively if decoder implements the
IVectorDecoder interface. This functionality can be enabled/disabled using the
ImageViewer.IntermediateImagePreviewInterval and
WpfImageViewer.IntermediateImagePreviewInterval properties.
ThumbnailViewer and
WpfThumbnailViewer classes can render image thumbnail without loading the whole image into memory if decoder implements one or several of the following interfaces:
IRasterGridDecoder,
ILineInterlacedDecoder,
ISimpleLineInterlacedDecoder,
IVectorDecoder.
Here is C#/VB.NET code that demonstrates how to render the specified image region in specified scale using
ImageRenderer class:
/// <summary>
/// Returns the scaled image region.
/// </summary>
/// <param name="image">The source image.</param>
/// <param name="rect">The image region on source image, which should be rendered.</param>
/// <param name="scale">The scale factor, which should be applied to the image region.</param>
/// <returns>The scaled image region.</returns>
public static Vintasoft.Imaging.VintasoftImage RenderScaledImageRegion(
Vintasoft.Imaging.VintasoftImage image,
System.Drawing.Rectangle rect,
float scale)
{
// create the image renderer
using (Vintasoft.Imaging.ImageRendering.ImageRenderer renderer =
new Vintasoft.Imaging.ImageRendering.ImageRenderer(image))
{
// create the rendering task
Vintasoft.Imaging.ImageRendering.ImageRenderingTask task =
Vintasoft.Imaging.ImageRendering.ImageRenderingTask.CreateImageRegion(rect, scale);
// execute the rendering
return renderer.ExecuteRendering(task);
}
}
''' <summary>
''' Returns the scaled image region.
''' </summary>
''' <param name="image">The source image.</param>
''' <param name="rect">The image region on source image, which should be rendered.</param>
''' <param name="scale">The scale factor, which should be applied to the image region.</param>
''' <returns>The scaled image region.</returns>
Public Shared Function RenderScaledImageRegion(image As Vintasoft.Imaging.VintasoftImage, rect As System.Drawing.Rectangle, scale As Single) As Vintasoft.Imaging.VintasoftImage
' create the image renderer
Using renderer As New Vintasoft.Imaging.ImageRendering.ImageRenderer(image)
' create the rendering task
Dim task As Vintasoft.Imaging.ImageRendering.ImageRenderingTask = Vintasoft.Imaging.ImageRendering.ImageRenderingTask.CreateImageRegion(rect, scale)
' execute the rendering
Return renderer.ExecuteRendering(task)
End Using
End Function
1. Decode part or scaled part of raster image
The following interfaces are responsible for decoding of raster image:
IRasterGridDecoder interface can be implemented in raster image decoder, which can use the image grid with scaling for decoding of image. The interface provides the wide abilities to get a part or whole image in necessary scale and without the need to decode the whole image. The class, which implements this interface, should support the following methods:
ILineInterlacedDecoder interface can be implemented in raster image decoder, which can effectively skip one or more image lines while decoding a part of image. The class, which implements this interface, must support
ILineInterlacedDecoder.GetImage method, which performs the rendering of image with specified interlace decoding settings (
InterlaceSettings).
InterlaceSettings class defines the following interlace decoding settings:
- the rectangle which will be decoded
- the step, used for interlaced decoding of image lines.
ISimpleLineInterlacedDecoder interface can be implemented in raster image decoder, which can effectively skip one or more image lines while decoding the whole image. The decoder, which implements this interface, must support the
ISimpleLineInterlacedDecoder.GetImage method, which performs rendering of image with specified interlace step.
Here is C#/VB.NET code that demonstrates how to render a part of JPEG2000 image reduced 4 times:
/// <summary>
/// Returns a 4 times downscaled image of left-top rectangle of JPEG2000 image.
/// </summary>
public static Vintasoft.Imaging.VintasoftImage GetJpeg2000RectangleDownscaled4(string filename)
{
using (System.IO.Stream stream =
new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
using (Vintasoft.Imaging.Codecs.Decoders.Jpeg2000Decoder decoder =
new Vintasoft.Imaging.Codecs.Decoders.Jpeg2000Decoder(stream))
{
// check available scales
int[] scales = decoder.GetImageRectScales(0, decoder.GetDefaultDecodingSettings(0));
for (int i = 0; i < scales.Length; i++)
{
if (scales[i] == 4)
{
// get top-left rectangle of raster grid with scale 4
return decoder.GetImageRect(0, 0, 4, null, null, null);
}
}
throw new System.Exception("Scale 4 is not available for specified JPEG2000 file.");
}
}
}
''' <summary>
''' Returns a 4 times downscaled image of left-top rectangle of JPEG2000 image.
''' </summary>
Public Shared Function GetJpeg2000RectangleDownscaled4(filename As String) As Vintasoft.Imaging.VintasoftImage
Using stream As System.IO.Stream = New System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Using decoder As New Vintasoft.Imaging.Codecs.Decoders.Jpeg2000Decoder(stream)
' check available scales
Dim scales As Integer() = decoder.GetImageRectScales(0, decoder.GetDefaultDecodingSettings(0))
For i As Integer = 0 To scales.Length - 1
If scales(i) = 4 Then
' get top-left rectangle of raster grid with scale 4
Return decoder.GetImageRect(0, 0, 4, Nothing, Nothing, Nothing)
End If
Next
Throw New System.Exception("Scale 4 is not available for specified JPEG2000 file.")
End Using
End Using
End Function
2. Render a region of vector image
IVectorDecoder interface is responsible for rendering some part of vector image.
IVectorDecoder interface can be implemented in vector image decoder and is implemented by
PdfDecoder,
DocxDecoder and
XlsxDecoder classes.
IVectorDecoder interface defines the single
IVectorDecoder.GetImage method, which has the following parameters:
- pageIndex - index of page
- rect - the desired rectangle in source image coordinates
- scale - the desired scale
- decodingSettings - decoding settings
- renderingSettings - rendering settings
- imageRenderingProgress - delegate of rendering progress
- intermediateImageRequest - delegate using which can be organized the rendering of intermediate states of image.
Here is C#/VB.NET code that demonstrates how to obtain a part of PDF document page:
/// <summary>
/// Returns an enlarged image of the central region of PDF page.
/// </summary>
public static Vintasoft.Imaging.VintasoftImage GetEnlargedImageOfPdfPageRegion(string filename, int pageIndex)
{
using (System.IO.Stream stream =
new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
using (Vintasoft.Imaging.Codecs.Decoders.PdfDecoder decoder =
new Vintasoft.Imaging.Codecs.Decoders.PdfDecoder(stream))
{
// settings for page rendering
Vintasoft.Imaging.Codecs.Decoders.RenderingSettings renderingSettings =
Vintasoft.Imaging.Codecs.Decoders.RenderingSettings.Empty;
// get page info
Vintasoft.Imaging.Codecs.Decoders.ImageInfo pageInfo = decoder.GetImageInfo(pageIndex, renderingSettings, null);
// determine region on page
float x = 3f * pageInfo.Width / 8f;
float y = 3f * pageInfo.Height / 8f;
float width = pageInfo.Width / 4f;
float height = pageInfo.Height / 4f;
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(x, y, width, height);
// get the central region with size of 1/4 page and scale 4
return decoder.GetImage(pageIndex, rect, 4f, null, renderingSettings, null, null);
}
}
}
''' <summary>
''' Returns an enlarged image of the central region of PDF page.
''' </summary>
Public Shared Function GetEnlargedImageOfPdfPageRegion(filename As String, pageIndex As Integer) As Vintasoft.Imaging.VintasoftImage
Using stream As System.IO.Stream = New System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Using decoder As New Vintasoft.Imaging.Codecs.Decoders.PdfDecoder(stream)
' settings for page rendering
Dim renderingSettings As Vintasoft.Imaging.Codecs.Decoders.RenderingSettings = Vintasoft.Imaging.Codecs.Decoders.RenderingSettings.Empty
' get page info
Dim pageInfo As Vintasoft.Imaging.Codecs.Decoders.ImageInfo = decoder.GetImageInfo(pageIndex, renderingSettings, Nothing)
' determine region on page
Dim x As Single = 3F * pageInfo.Width / 8F
Dim y As Single = 3F * pageInfo.Height / 8F
Dim width As Single = pageInfo.Width / 4F
Dim height As Single = pageInfo.Height / 4F
Dim rect As New System.Drawing.RectangleF(x, y, width, height)
' get the central region with size of 1/4 page and scale 4
Return decoder.GetImage(pageIndex, rect, 4F, Nothing, renderingSettings, Nothing, _
Nothing)
End Using
End Using
End Function
2.1. Obtain intermediate images during image rendering
The
ImageViewer,
WpfImageViewer and
ImageRenderingTask classes allow to obtain intermediate images during rendering of vector image. This feature makes the rendering process of complex vector images visually faster. The interval for displaying the intermediate images can be set using the following properties:
For correct work of the above mechanism the vector decoder must block image, which being rendered, with non-exclusive write rights.