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