123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- #region MIT License
- #endregion
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- using SharpFont.Internal;
- namespace SharpFont
- {
-
-
-
-
-
-
-
-
- public sealed class FTBitmap : IDisposable
- {
- #region Fields
- private IntPtr reference;
- private BitmapRec rec;
- private Library library;
- private bool disposed;
-
- private bool user;
- #endregion
- #region Constructors
-
-
-
-
- public FTBitmap(Library library)
- {
- IntPtr bitmapRef;
- FT.FT_Bitmap_New(out bitmapRef);
- Reference = bitmapRef;
- this.library = library;
- this.user = true;
- }
- internal FTBitmap(IntPtr reference, Library library)
- {
- Reference = reference;
- this.library = library;
- user = true;
- }
- internal FTBitmap(BitmapRec bmpInt, Library library)
- {
- this.rec = bmpInt;
- this.library = library;
- }
- internal FTBitmap(IntPtr reference)
- : this(reference, null)
- {
- user = true;
- }
- internal FTBitmap(BitmapRec bmpInt)
- : this(bmpInt, null)
- {
- }
-
-
-
- ~FTBitmap()
- {
- Dispose(false);
- }
- #endregion
- #region Properties
-
-
-
-
- public bool IsDisposed
- {
- get
- {
- return disposed;
- }
- }
-
-
-
- public int Rows
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return rec.rows;
- }
- }
-
-
-
- public int Width
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return rec.width;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int Pitch
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return rec.pitch;
- }
- }
-
-
-
-
- public IntPtr Buffer
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return rec.buffer;
- }
- }
-
-
-
-
- public short GrayLevels
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return rec.num_grays;
- }
- }
-
-
-
- public PixelMode PixelMode
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return rec.pixel_mode;
- }
- }
-
-
-
- [Obsolete("Not used currently.")]
- public byte PaletteMode
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return rec.palette_mode;
- }
- }
-
-
-
- [Obsolete("Not used currently.")]
- public IntPtr Palette
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return rec.palette;
- }
- }
-
-
-
- public byte[] BufferData
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- byte[] data = new byte[rec.rows * rec.width];
- Marshal.Copy(rec.buffer, data, 0, data.Length);
- return data;
- }
- }
- internal IntPtr Reference
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- return reference;
- }
- set
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- reference = value;
- rec = PInvokeHelper.PtrToStructure<BitmapRec>(reference);
- }
- }
- #endregion
- #region Methods
-
-
-
-
-
- public FTBitmap Copy(Library library)
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- if (library == null)
- throw new ArgumentNullException("library");
- IntPtr bitmapRef;
- Error err = FT.FT_Bitmap_Copy(library.Reference, Reference, out bitmapRef);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- return new FTBitmap(bitmapRef);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void Embolden(Library library, int xStrength, int yStrength)
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- if (library == null)
- throw new ArgumentNullException("library");
- Error err = FT.FT_Bitmap_Embolden(library.Reference, Reference, xStrength, yStrength);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public FTBitmap Convert(Library library, int alignment)
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- if (library == null)
- throw new ArgumentNullException("library");
- IntPtr bitmapRef;
- Error err = FT.FT_Bitmap_Convert(library.Reference, Reference, out bitmapRef, alignment);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- return new FTBitmap(bitmapRef);
- }
-
-
-
-
- public Bitmap ToGdipBitmap()
- {
- if (disposed)
- throw new ObjectDisposedException("FTBitmap", "Cannot access a disposed object.");
- switch (rec.pixel_mode)
- {
- case PixelMode.Mono:
- {
- Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format1bppIndexed);
- var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
- Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows);
- bmp.UnlockBits(locked);
- bmp.Palette.Entries[0] = Color.FromArgb(0, 0, 0, 0);
- bmp.Palette.Entries[1] = Color.FromArgb(1, 0, 0, 0);
- return bmp;
- }
- case PixelMode.Gray4:
- {
- Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format4bppIndexed);
- var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format4bppIndexed);
- Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows);
- bmp.UnlockBits(locked);
- for (int i = 0; i < 16; i++)
- {
- bmp.Palette.Entries[i] = Color.FromArgb((int)((i / 15) * 255), 0, 0, 0);
- }
- return bmp;
- }
- case PixelMode.Gray:
- {
- Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format8bppIndexed);
- var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
- Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows);
- bmp.UnlockBits(locked);
- for (int i = 0; i < 256; i++)
- {
- bmp.Palette.Entries[i] = Color.FromArgb(i, 0, 0, 0);
- }
- return bmp;
- }
- case PixelMode.Lcd:
- case PixelMode.VerticalLcd:
- {
-
- Bitmap bmp = new Bitmap(rec.width, rec.rows, PixelFormat.Format24bppRgb);
- var locked = bmp.LockBits(new Rectangle(0, 0, rec.width, rec.rows), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
- Marshal.Copy(BufferData, 0, locked.Scan0, rec.width * rec.rows);
- bmp.UnlockBits(locked);
- for (int i = 0; i < 256; i++)
- {
- bmp.Palette.Entries[i] = Color.FromArgb(i, 0, 0, 0);
- }
- return bmp;
- }
- default:
- throw new InvalidOperationException("System.Drawing.Bitmap does not support this pixel mode.");
- }
- }
- #region IDisposable
-
-
-
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- private void Dispose(bool disposing)
- {
- if (!disposed)
- {
- disposed = true;
- if (user)
- {
- Error err = FT.FT_Bitmap_Done(library.Reference, reference);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- }
- reference = IntPtr.Zero;
- library = null;
- }
- }
- #endregion
- #endregion
- }
- }
|