'' This code is part of GrapeCity Documents for Imaging samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging

'' This sample takes a color JPEG, converts it to different image types
'' (such as b/w and grayscale) and creates a multi-frame TIFF from those
'' images.
Public Class ImageTypesInTiff
    Function GenerateImageStream(
                ByVal targetEncoding As ImageEncoding,
                ByVal pixelSize As Size,
                ByVal dpi As Single,
                ByVal opaque As Boolean,
                Optional sampleParams As String() = Nothing) As Stream

        If Not targetEncoding = ImageEncoding.Tiff Then
            Throw New Exception("This sample only supports TIFF output format.")
        End If

        Dim pth = Path.Combine("Resources", "Images", "minerva.jpg")

        Dim ms = New MemoryStream()
        Using tw = New GcTiffWriter(ms),
            bmp = New GcBitmap(pth)
            '' Using the green channel produces a marginally better result for this photo.
            '' For a much better bilevel image produced by dithering, see DitheringInTiff.
            Using f = bmp.ToBilevelBitmap(ColorChannel.Green)
            End Using
            Using f = bmp.ToGrayscaleBitmap()
            End Using
            '' 4bpp allows from 8 to 16 colors, we use max (16)
            Using f = bmp.ToIndexed4bppBitmap(16)
            End Using
            '' 8bpp allows from 8 to 256 colors, we use max (256)
            Using f = bmp.ToIndexed8bppBitmap(256)
            End Using
        End Using
        ms.Seek(0, SeekOrigin.Begin)
        Return ms
    End Function

    Public ReadOnly Property DefaultEncoding() As ImageEncoding
            Return ImageEncoding.Tiff
        End Get
    End Property
End Class