LimitedPalette.vb
''
'' 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 demonstrates how quantization and dithering
'' algorithms can be used to convert a 24 bits per pixel image
'' to a reasonably looking image with a limited palette
'' (16 colors, 4 bits per pixel).
Public Class LimitedPalette
    Function GenerateImage(
            Optional pixelWidth As Integer = 1024,
            Optional pixelHeight As Integer = 1024,
            Optional opaque As Boolean = True,
            Optional dpiX As Single = 96,
            Optional dpiY As Single = 96) As GcBitmap

        opaque = True

        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY)
        Using origBmp = New GcBitmap()
            '' Load a sample photo:
            Dim imagePath = Path.Combine("Resources", "ImagesBis", "clivia.jpg")
            Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
                origBmp.Load(stm)
            End Using

            '' Resize the original photo so we can place 4 samples of it
            '' on the resulting bitmap:
            Dim w = pixelWidth / 2
            Dim h = pixelHeight / 2
            Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic)
                '' Resized original image:
                bmp.BitBlt(sizedBmp, 0, 0)

                '' Simple dithering
                Using ind = sizedBmp.ToIndexed4bppBitmap(DitheringMethod.Stucki), bm = ind.ToGcBitmap()
                    bmp.BitBlt(bm, w, 0)
                End Using

                '' Octree quantizer algorithm without dithering
                Using ind = sizedBmp.ToIndexed4bppBitmap(16), bm = ind.ToGcBitmap()
                    bmp.BitBlt(bm, 0, h)
                End Using

                '' Octree quantizer + dithering:
                Dim pal = sizedBmp.GenerateOctreePalette(16)
                Using ind = sizedBmp.ToIndexed4bppBitmap(pal, DitheringMethod.Stucki), bm = ind.ToGcBitmap()
                    bmp.BitBlt(bm, w, h)
                End Using
            End Using

            '' Add borders between the quadrants, And captions for each:
            Dim lineh = 2
            Using g = bmp.CreateGraphics(Nothing)
                Dim foreColor = Color.Yellow
                Dim backColor = Color.Blue
                Dim fnt = Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
                g.DrawLine(w, 0, w, h * 2, New Pen(Color.Gray, lineh * 2))
                g.DrawLine(0, h, w * 2, h, New Pen(Color.Gray, lineh * 2))
                Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
                Dim th = g.MeasureString("QWERTY", tf).Height
                g.DrawString(" Original image (24bpp) ", tf, New PointF(0, h - th + lineh))
                g.DrawString(" Simple dithering (4bpp) ", tf, New PointF(w + lineh, h - th + lineh))
                g.DrawString(" Octree quantizer (4bpp) ", tf, New PointF(0, h * 2 + lineh - th + lineh))
                g.DrawString(" Octree + dithering (4bpp) ", tf, New PointF(w + lineh, h * 2 + lineh - th + lineh))
            End Using
        End Using
        Return bmp
    End Function
End Class