LimitedPalette.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Numerics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This sample demonstrates how quantization and dithering
  20. // algorithms can be used to convert a 24 bits per pixel image
  21. // to a reasonably looking image with a limited palette
  22. // (16 colors, 4 bits per pixel).
  23. public class LimitedPalette
  24. {
  25. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  26. {
  27. opaque = true;
  28.  
  29. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  30. using (var origBmp = new GcBitmap())
  31. {
  32. // Load a sample photo:
  33. var imagePath = Path.Combine("Resources", "ImagesBis", "clivia.jpg");
  34. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  35. origBmp.Load(stm);
  36.  
  37. // Resize the original photo so we can place 4 samples of it
  38. // on the resulting bitmap:
  39. int w = pixelSize.Width / 2;
  40. int h = pixelSize.Height / 2;
  41. using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
  42. {
  43. // Resized original image:
  44. bmp.BitBlt(sizedBmp, 0, 0);
  45.  
  46. // Simple dithering:
  47. using (var ind = sizedBmp.ToIndexed4bppBitmap(DitheringMethod.Stucki))
  48. using (var bm = ind.ToGcBitmap())
  49. bmp.BitBlt(bm, w, 0);
  50.  
  51. // Octree quantizer algorithm without dithering:
  52. using (var ind = sizedBmp.ToIndexed4bppBitmap(16))
  53. using (var bm = ind.ToGcBitmap())
  54. bmp.BitBlt(bm, 0, h);
  55.  
  56. // Octree quantizer + dithering:
  57. var pal = sizedBmp.GenerateOctreePalette(16);
  58. using (var ind = sizedBmp.ToIndexed4bppBitmap(pal, DitheringMethod.Stucki))
  59. using (var bm = ind.ToGcBitmap())
  60. bmp.BitBlt(bm, w, h);
  61. }
  62.  
  63. // Add borders between the quadrants, and captions for each:
  64. var lineh = 2;
  65. using (var g = bmp.CreateGraphics(null))
  66. {
  67. var foreColor = Color.Yellow;
  68. var backColor = Color.Blue;
  69. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  70. g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(Color.Gray, lineh * 2));
  71. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(Color.Gray, lineh * 2));
  72. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  73. var th = g.MeasureString("QWERTY", tf).Height;
  74. g.DrawString(" Original image (24bpp) ", tf, new PointF(0, h - th + lineh));
  75. g.DrawString(" Simple dithering (4bpp) ", tf, new PointF(w + lineh, h - th + lineh));
  76. g.DrawString(" Octree quantizer (4bpp) ", tf, new PointF(0, h * 2 + lineh - th + lineh));
  77. g.DrawString(" Octree + dithering (4bpp) ", tf, new PointF(w + lineh, h * 2 + lineh - th + lineh));
  78. }
  79. }
  80. return bmp;
  81. }
  82. }
  83. }
  84.