Indexing.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 convert full color RGB
    // to 4 BPP and 8 BPP indexed images.
    public class Indexing
    {
        public GcBitmap GenerateImage(int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            var bmp = new GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY);
            using (var origBmp = new GcBitmap())
            {
                // Load a sample photo:
                var imagePath = Path.Combine("Resources", "Images", "maple.jpg");
                using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
                    origBmp.Load(stm);

                origBmp.SetAlphaTo255();
                origBmp.Opaque = opaque;

                // 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))
                using (var ib4bpp = sizedBmp.ToIndexed4bppBitmap(DitheringMethod.JarvisJudiceNinke))
                using (var ib8bpp = sizedBmp.ToIndexed8bppBitmap(DitheringMethod.JarvisJudiceNinke))
                using (var b4bpp = ib4bpp.ToGcBitmap())
                using (var b8bpp = ib8bpp.ToGcBitmap())
                {
                    // Copy the original and indexed images into 4 quadrants of the resulting bitmap:
                    bmp.BitBlt(sizedBmp, 0, 0);
                    b4bpp.Opaque = opaque;
                    bmp.BitBlt(b4bpp, w, 0);
                    b8bpp.Opaque = opaque;
                    bmp.BitBlt(b8bpp, 0, h);
                    bmp.BitBlt(sizedBmp, 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(foreColor, lineh * 2));
                    g.DrawLine(0, h, w * 2, h, new Pen(foreColor, lineh * 2));
                    var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
                    g.DrawString(" Original image ", tf, new PointF(0, 0));
                    g.DrawString(" Indexed 4 bits per pixel ", tf, new PointF(w + lineh, 0));
                    g.DrawString(" Indexed 8 bits per pixel ", tf, new PointF(0, h + lineh));
                    g.DrawString(" Original image ", tf, new PointF(w + lineh, h + lineh));
                }
            }
            return bmp;
        }
    }
}