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

namespace GcImagingWeb.Samples
{
    // 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
    {
        public GcBitmap GenerateImage(int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            opaque = true;

            var bmp = new GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY);
            using (var origBmp = new GcBitmap())
            {
                // Load a sample photo:
                var imagePath = Path.Combine("Resources", "ImagesBis", "clivia.jpg");
                using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
                    origBmp.Load(stm);

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

                    // Simple dithering:
                    using (var ind = sizedBmp.ToIndexed4bppBitmap(DitheringMethod.Stucki))
                    using (var bm = ind.ToGcBitmap())
                        bmp.BitBlt(bm, w, 0);

                    // Octree quantizer algorithm without dithering:
                    using (var ind = sizedBmp.ToIndexed4bppBitmap(16))
                    using (var bm = ind.ToGcBitmap())
                        bmp.BitBlt(bm, 0, h);

                    // Octree quantizer + dithering:
                    var pal = sizedBmp.GenerateOctreePalette(16);
                    using (var ind = sizedBmp.ToIndexed4bppBitmap(pal, DitheringMethod.Stucki))
                    using (var bm = ind.ToGcBitmap())
                        bmp.BitBlt(bm, w, h);
                }

                // Add borders between the quadrants, and captions for each:
                var lineh = 2;
                using (var g = bmp.CreateGraphics(null))
                {
                    var foreColor = Color.Yellow;
                    var backColor = Color.Blue;
                    var font = 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));
                    var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
                    var 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));
                }
            }
            return bmp;
        }
    }
}