public void Example() { // Allocating a new bitmap with 99x99 pixels, 16-bit color depth and an allocation of 5 bits for each color. dib = FreeImage.Allocate(99, 99, 16, FreeImage.FI16_555_RED_MASK, FreeImage.FI16_555_GREEN_MASK, FreeImage.FI16_555_BLUE_MASK); // Saving bitmap. if (!FreeImage.SaveEx(ref dib, "example01.bmp", true)) { Console.WriteLine("Saving 'example.bmp' failed."); FreeImage.UnloadEx(ref dib); } // Allocation a new bitmap with 71x33 pixels, 4-bit color depth. Bitmaps below 16-bit have paletts. // Each pixel references an index within the palette wich contains the true color. // Therefor no bit-masks are needed and can be set to 0. dib = FreeImage.Allocate(71, 33, 4, 0, 0, 0); // Saving bitmap. if (!FreeImage.SaveEx(ref dib, "example02.tif", true)) { Console.WriteLine("Saving 'example02.tif' failed."); FreeImage.UnloadEx(ref dib); } // Allocation a new bitmap. This time 'AllocateT' is used because 'Allocate' can only create standard bitmaps. // In this case a RGBF bitmap is created. Red, green and blue are represented by a float-value so no bit-masks are needed. dib = FreeImage.AllocateT(FREE_IMAGE_TYPE.FIT_RGBF, 50, 75, 9, 0, 0, 0); // Saving bitmap. if (!FreeImage.SaveEx(ref dib, "example03.hdr", true)) { Console.WriteLine("Saving 'example03.hdr' failed."); FreeImage.UnloadEx(ref dib); } }
public BitmapContent(int width, int height) { Width = width; Height = height; _bitmap = FreeImage.Allocate(width, height, 24); // default to RGB image ImageType = ImageType.RGB; }
private PNG formatPng(ImageModel imgMd) { try { FIBITMAP dib = FreeImage.Allocate(imgMd.width, imgMd.height, 32, 8, 8, 8); IntPtr pFibData = FreeImage.GetBits(dib); Marshal.Copy(imgMd.data, 0, pFibData, imgMd.data.Length); //FIMEMORY fm = new FIMEMORY(); FIMEMORY fm = FreeImage.OpenMemory(IntPtr.Zero, 0); FreeImage.SaveToMemory(FREE_IMAGE_FORMAT.FIF_PNG, dib, fm, FREE_IMAGE_SAVE_FLAGS.DEFAULT); FreeImage.SeekMemory(fm, 0, SeekOrigin.End); int bufferSize = FreeImage.TellMemory(fm); //Debug.WriteLine("aaa:" + bufferSize); FreeImage.SeekMemory(fm, 0, SeekOrigin.Begin); byte[] buffer = new byte[bufferSize]; FreeImage.ReadMemory(buffer, (uint)bufferSize, (uint)bufferSize, fm); FreeImage.CloseMemory(fm); FreeImage.Unload(dib); //using(FileStream fs = new FileStream("aaa_" + i + ".png", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) { // fs.Write(buffer, 0, bufferSize); //} //using(FileStream fs = new FileStream("aaa.png", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) { // fs.Write(buffer, 0, bufferSize); //} PNG pngFrame = new PNG(); using (MemoryStream ms = new MemoryStream(buffer)) { pngFrame.Load(ms); } return(pngFrame); } catch (Exception) { } return(null); }
public void ImageMetadata() { ImageMetadata metadata; List <MetadataModel> modelList; MetadataTag tag = new MetadataTag(FREE_IMAGE_MDMODEL.FIMD_COMMENTS); tag.Key = "KEY"; tag.ID = 11; tag.Value = new double[] { 0d, 41d, -523d, -0.41d }; dib = FreeImage.Allocate(1, 1, 1, 1, 0, 0); Assert.IsFalse(dib.IsNull); metadata = new ImageMetadata(dib, true); Assert.AreEqual(0, metadata.Count); Assert.IsTrue(metadata.HideEmptyModels); Assert.IsEmpty(metadata.List); metadata = new ImageMetadata(dib, false); Assert.AreEqual(FreeImage.FREE_IMAGE_MDMODELS.Length, metadata.Count); Assert.IsFalse(metadata.HideEmptyModels); Assert.IsNotEmpty(metadata.List); metadata.HideEmptyModels = true; metadata.AddTag(tag); Assert.AreEqual(1, metadata.Count); Assert.IsNotEmpty(metadata.List); modelList = metadata.List; Assert.AreEqual(FREE_IMAGE_MDMODEL.FIMD_COMMENTS, modelList[0].Model); System.Collections.IEnumerator enumerator = metadata.GetEnumerator(); Assert.IsTrue(enumerator.MoveNext()); Assert.IsNotNull((MetadataModel)enumerator.Current); Assert.IsFalse(enumerator.MoveNext()); FreeImage.UnloadEx(ref dib); }
public void MetadataModel() { MetadataTag tag; dib = FreeImage.Allocate(1, 1, 1, 0, 0, 0); Assert.IsFalse(dib.IsNull); MetadataModel model = new MDM_GEOTIFF(dib); Assert.AreEqual(0, model.Count); Assert.IsFalse(model.Exists); Assert.IsEmpty(model.List); Assert.AreEqual(model.Model, FREE_IMAGE_MDMODEL.FIMD_GEOTIFF); Assert.IsTrue(model.DestoryModel()); foreach (MetadataTag m in model) { Assert.Fail(); } tag = new MetadataTag(FREE_IMAGE_MDMODEL.FIMD_GEOTIFF); tag.Key = "KEY"; tag.Value = 54321f; Assert.IsTrue(model.AddTag(tag)); Assert.AreEqual(1, model.Count); Assert.IsTrue(model.Exists); Assert.IsNotEmpty(model.List); Assert.AreEqual(model.Model, FREE_IMAGE_MDMODEL.FIMD_GEOTIFF); Assert.IsTrue(model.DestoryModel()); Assert.AreEqual(0, model.Count); Assert.IsFalse(model.Exists); Assert.IsEmpty(model.List); Assert.AreEqual(model.Model, FREE_IMAGE_MDMODEL.FIMD_GEOTIFF); FreeImage.UnloadEx(ref dib); }
static void Main(string[] args) { var colors = new List <RGBQUAD>(); foreach (int i in Enumerable.Range(0, 255)) { colors.Add(new RGBQUAD(Color.FromArgb(i, i, i))); } var resolution = 1024; try { if (args[0] != null) { resolution = int.Parse(args[0]); } } catch { Console.WriteLine("Couldn't parse resolution number from input."); } var bitmap = FreeImage.Allocate(resolution, resolution, 24); var rand = new Random(); foreach (uint y in Enumerable.Range(0, resolution)) { foreach (uint x in Enumerable.Range(0, resolution)) { var randomValue = rand.Next(0, 255); var color = colors[randomValue]; FreeImage.SetPixelColor(bitmap, x, y, ref color); } } FreeImage.Save(FREE_IMAGE_FORMAT.FIF_PNG, bitmap, "output.png", 0); }
public void saveAsPng(string savePath, int frameIdx) { ImageModel md = getImageModelByFrame(frameIdx, true); if (md == null) { return; } FIBITMAP dib = FreeImage.Allocate(md.width, md.height, 32, 8, 8, 8); IntPtr pFibData = FreeImage.GetBits(dib); Marshal.Copy(md.data, 0, pFibData, md.data.Length); FreeImage.Save(FREE_IMAGE_FORMAT.FIF_PNG, dib, savePath, FREE_IMAGE_SAVE_FLAGS.DEFAULT); //FreeImage.SaveToMemory(FREE_IMAGE_FORMAT.FIF_PNG, dib, ms, FREE_IMAGE_SAVE_FLAGS.DEFAULT); //int size = FreeImage.TellMemory(ms); //byte[] buffer = new byte[size]; //FreeImage.SeekMemory(ms, 0, SeekOrigin.Begin); //FreeImage.ReadMemory(buffer, (uint)size, (uint)size, ms); FreeImage.Unload(dib); }
static void Main(string[] args) { if (args.Length < 2) { PrintHelp(); return; } try { string inputFile = args[0]; string outputFile = args[1]; Console.WriteLine(String.Format("Loading file: {0}", inputFile)); FIBITMAP image = FreeImage.LoadEx(inputFile); // Make sure the image is one of the supported types FREE_IMAGE_TYPE imgType = FreeImage.GetImageType(image); switch (imgType) { case FREE_IMAGE_TYPE.FIT_INT16: Console.WriteLine("Detected 16-bit short"); break; case FREE_IMAGE_TYPE.FIT_INT32: Console.WriteLine("Detected 32-bit int"); break; case FREE_IMAGE_TYPE.FIT_UINT16: Console.WriteLine("Detected 16-bit ushort"); break; case FREE_IMAGE_TYPE.FIT_UINT32: Console.WriteLine("Detected 32-bit uint"); break; default: Console.WriteLine(String.Format("Unsupported file type: {0}", imgType.ToString())); return; } uint width = FreeImage.GetWidth(image); uint height = FreeImage.GetHeight(image); uint fileBPP = FreeImage.GetBPP(image); // Allocate new RGB Image FIBITMAP newMap = FreeImage.Allocate((int)width, (int)height, 8 /*BitsPerPixel*/ * 3); RGBQUAD outQuad = new RGBQUAD(); // Multiplier for the byte offset into the scaline int iterations = 0; int lateralMultipler = fileBPP == 16 ? 2 : 4; for (uint x = 0; x < width; ++x, ++iterations) { float progress = ((float)(iterations)) / ((float)(width * height)); Console.Write(String.Format("\rProgress {0:000.0}% ", progress * 100.0f)); for (uint y = 0; y < height; ++y, ++iterations) { IntPtr line = FreeImage.GetScanLine(image, (int)y); if (fileBPP >= 16) { line = new IntPtr(line.ToInt64() + lateralMultipler * x); if (imgType == FREE_IMAGE_TYPE.FIT_UINT16) { ushort value = (ushort)System.Runtime.InteropServices.Marshal.ReadInt16(line); outQuad.rgbRed = (byte)(value / 256); outQuad.rgbGreen = (byte)(value % 256); } else if (imgType == FREE_IMAGE_TYPE.FIT_UINT32) { uint value = (uint)System.Runtime.InteropServices.Marshal.ReadInt32(line); outQuad.rgbRed = (byte)(value / 256); outQuad.rgbGreen = (byte)(value % 256); } else if (imgType == FREE_IMAGE_TYPE.FIT_INT16) { short value = (short)System.Runtime.InteropServices.Marshal.ReadInt16(line); outQuad.rgbRed = (byte)(value / 256); outQuad.rgbGreen = (byte)(value % 256); } else if (imgType == FREE_IMAGE_TYPE.FIT_INT32) { int value = (int)System.Runtime.InteropServices.Marshal.ReadInt32(line); outQuad.rgbRed = (byte)(value / 256); outQuad.rgbGreen = (byte)(value % 256); } FreeImage.SetPixelColor(newMap, x, y, ref outQuad); } else { } } } Console.WriteLine(" "); //empty space Console.WriteLine(String.Format("Writing file: {0}", outputFile)); if (FreeImage.SaveEx(newMap, outputFile)) { Console.WriteLine("Finished"); } else { Console.WriteLine("ERROR: Failed to write file"); } } catch (Exception ex) { Console.WriteLine("ERROR: "); Console.Write(ex.Message); } }
public void FreeImageBitmapConstructors() { Image bitmap; FreeImageBitmap fib, fib2; Stream stream; Graphics g; string filename = iManager.GetBitmapPath(ImageType.Odd, ImageColorType.Type_24); Assert.IsNotNull(filename); Assert.IsTrue(File.Exists(filename)); bitmap = new Bitmap(filename); Assert.IsNotNull(bitmap); fib = new FreeImageBitmap(bitmap); Assert.AreEqual(bitmap.Width, fib.Width); Assert.AreEqual(bitmap.Height, fib.Height); fib.Dispose(); fib.Dispose(); fib = new FreeImageBitmap(bitmap, new Size(100, 100)); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); fib.Dispose(); bitmap.Dispose(); fib = new FreeImageBitmap(filename); fib.Dispose(); fib = new FreeImageBitmap(filename, FREE_IMAGE_LOAD_FLAGS.DEFAULT); fib.Dispose(); fib = new FreeImageBitmap(filename, FREE_IMAGE_FORMAT.FIF_UNKNOWN); fib.Dispose(); fib = new FreeImageBitmap(filename, FREE_IMAGE_FORMAT.FIF_UNKNOWN, FREE_IMAGE_LOAD_FLAGS.DEFAULT); fib.Dispose(); stream = new FileStream(filename, FileMode.Open); Assert.IsNotNull(stream); fib = new FreeImageBitmap(stream); fib.Dispose(); stream.Seek(0, SeekOrigin.Begin); fib = new FreeImageBitmap(stream, FREE_IMAGE_FORMAT.FIF_UNKNOWN); fib.Dispose(); stream.Seek(0, SeekOrigin.Begin); fib = new FreeImageBitmap(stream, FREE_IMAGE_LOAD_FLAGS.DEFAULT); fib.Dispose(); stream.Seek(0, SeekOrigin.Begin); fib = new FreeImageBitmap(stream, FREE_IMAGE_FORMAT.FIF_UNKNOWN, FREE_IMAGE_LOAD_FLAGS.DEFAULT); fib.Dispose(); stream.Dispose(); fib = new FreeImageBitmap(100, 100); Assert.AreEqual(24, fib.ColorDepth); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); fib.Dispose(); using (bitmap = new Bitmap(filename)) { Assert.IsNotNull(bitmap); using (g = Graphics.FromImage(bitmap)) { Assert.IsNotNull(g); fib = new FreeImageBitmap(100, 100, g); } } fib.Dispose(); fib = new FreeImageBitmap(100, 100, PixelFormat.Format1bppIndexed); Assert.AreEqual(PixelFormat.Format1bppIndexed, fib.PixelFormat); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); fib.Dispose(); fib = new FreeImageBitmap(100, 100, PixelFormat.Format4bppIndexed); Assert.AreEqual(PixelFormat.Format4bppIndexed, fib.PixelFormat); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); fib.Dispose(); fib = new FreeImageBitmap(100, 100, PixelFormat.Format8bppIndexed); Assert.AreEqual(PixelFormat.Format8bppIndexed, fib.PixelFormat); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); fib.Dispose(); fib = new FreeImageBitmap(100, 100, PixelFormat.Format16bppRgb555); Assert.AreEqual(PixelFormat.Format16bppRgb555, fib.PixelFormat); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); fib.Dispose(); fib = new FreeImageBitmap(100, 100, PixelFormat.Format16bppRgb565); Assert.AreEqual(PixelFormat.Format16bppRgb565, fib.PixelFormat); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); fib.Dispose(); fib = new FreeImageBitmap(100, 100, PixelFormat.Format24bppRgb); Assert.AreEqual(PixelFormat.Format24bppRgb, fib.PixelFormat); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); fib.Dispose(); fib = new FreeImageBitmap(100, 100, PixelFormat.Format32bppArgb); Assert.AreEqual(PixelFormat.Format32bppArgb, fib.PixelFormat); Assert.AreEqual(100, fib.Width); Assert.AreEqual(100, fib.Height); stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, fib); Assert.Greater(stream.Length, 0); stream.Position = 0; fib2 = formatter.Deserialize(stream) as FreeImageBitmap; stream.Dispose(); fib.Dispose(); fib2.Dispose(); fib = new FreeImageBitmap(filename); fib2 = new FreeImageBitmap(fib); fib2.Dispose(); fib2 = new FreeImageBitmap(fib, new Size(31, 22)); Assert.AreEqual(31, fib2.Width); Assert.AreEqual(22, fib2.Height); fib2.Dispose(); fib.Dispose(); dib = FreeImage.Allocate(1000, 800, 24, 0xFF0000, 0xFF00, 0xFF); Assert.IsFalse(dib.IsNull); fib = new FreeImageBitmap(1000, 800, -(int)FreeImage.GetPitch(dib), FreeImage.GetPixelFormat(dib), FreeImage.GetScanLine(dib, 0)); fib.Dispose(); FreeImage.UnloadEx(ref dib); }
public unsafe void Properties() { string filename = iManager.GetBitmapPath(ImageType.Even, ImageColorType.Type_32); Assert.IsNotNull(filename); Assert.IsTrue(File.Exists(filename)); FreeImageBitmap fib = new FreeImageBitmap(filename); Assert.IsFalse(fib.HasPalette); try { Palette palette = fib.Palette; Assert.Fail(); } catch { } Assert.IsFalse(fib.HasBackgroundColor); fib.BackgroundColor = Color.LightSeaGreen; Assert.IsTrue(fib.HasBackgroundColor); Assert.That( Color.LightSeaGreen.B == fib.BackgroundColor.Value.B && Color.LightSeaGreen.G == fib.BackgroundColor.Value.G && Color.LightSeaGreen.R == fib.BackgroundColor.Value.R); fib.BackgroundColor = null; Assert.IsFalse(fib.HasBackgroundColor); fib.Dispose(); fib = new FreeImageBitmap(100, 100, PixelFormat.Format1bppIndexed); ImageFlags flags = (ImageFlags)fib.Flags; Assert.That((flags & ImageFlags.ColorSpaceRgb) == ImageFlags.ColorSpaceRgb); Assert.That((flags & ImageFlags.HasAlpha) != ImageFlags.HasAlpha); Assert.That((flags & ImageFlags.HasRealDpi) != ImageFlags.HasRealDpi); Assert.That((flags & ImageFlags.HasTranslucent) != ImageFlags.HasTranslucent); fib.Dispose(); dib = FreeImage.Allocate(100, 100, 32, 0xFF0000, 0xFF00, 0xFF); FIICCPROFILE *prof = (FIICCPROFILE *)FreeImage.CreateICCProfile(dib, new byte[] { 0, 1, 2, 3 }, 4); fib = new FreeImageBitmap(dib); Scanline <RGBQUAD> sc = (Scanline <RGBQUAD>)fib.GetScanline(0); RGBQUAD rgbq = sc[0]; rgbq.rgbReserved = 127; sc[0] = rgbq; flags = (ImageFlags)fib.Flags; Assert.That((flags & ImageFlags.HasAlpha) == ImageFlags.HasAlpha); Assert.That((flags & ImageFlags.HasRealDpi) != ImageFlags.HasRealDpi); Assert.That((flags & ImageFlags.HasTranslucent) == ImageFlags.HasTranslucent); fib.Dispose(); fib = null; GC.Collect(2, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); fib = new FreeImageBitmap(iManager.GetBitmapPath(ImageType.Metadata, ImageColorType.Type_01_Dither)); int[] propList = fib.PropertyIdList; Assert.IsNotNull(propList); Assert.Greater(propList.Length, 0); PropertyItem[] propItemList = fib.PropertyItems; Assert.IsNotNull(propItemList); Assert.Greater(propItemList.Length, 0); Assert.IsNotNull(fib.RawFormat); fib.Dispose(); fib = new FreeImageBitmap(iManager.GetBitmapPath(ImageType.Multipaged, ImageColorType.Type_01_Dither)); Assert.Greater(fib.FrameCount, 1); fib.Dispose(); }
private static FIBITMAP NewFiBitMap(PixImage <byte> pi) { FreeImageCheck(pi.Volume.Info); var sx = pi.Size.X; var sy = pi.Size.Y; var bpp = pi.ChannelCount == 1 && pi.Format == Col.Format.BW ? 1 : pi.ChannelCount * 8; var dib = FreeImage.Allocate(sx, sy, bpp); var delta = (int)FreeImage.GetPitch(dib); var data = pi.Volume.Data; long i = pi.Volume.FirstIndex; long j = pi.Volume.JY; var bits = FreeImage.GetBits(dib) + sy * delta; switch (bpp) { case 1: { var palette = FreeImage.GetPaletteEx(dib); if (palette != null) // should alway be != null { palette[0] = new RGBQUAD { rgbRed = 0, rgbGreen = 0, rgbBlue = 0 }; palette[1] = new RGBQUAD { rgbRed = 255, rgbGreen = 255, rgbBlue = 255 }; } for (var y = 0; y < sy; y++) { bits = bits - delta; byte bit = 0x80; int bi = 0; unsafe { byte *pixel = (byte *)bits; for (var x = 0; x < sx; x++) { if ((data[i++] & 0x80) != 0) { pixel[bi] |= bit; } bit >>= 1; if (bit == 0) { bit = 0x80; bi++; } } } i += j; } } break; case 8: { for (var y = 0; y < sy; y++) { bits = bits - delta; unsafe { byte *pixel = (byte *)bits; for (var x = 0; x < sx; x++) { pixel[x] = data[i++]; } } i += j; } } break; case 24: { for (var y = 0; y < sy; y++) { bits = bits - delta; unsafe { Byte *pixel = (Byte *)bits; for (var x = 0; x < sx; x++) { pixel[FreeImage.FI_RGBA_BLUE] = data[i++]; pixel[FreeImage.FI_RGBA_GREEN] = data[i++]; pixel[FreeImage.FI_RGBA_RED] = data[i++]; pixel += 3; } } i += j; } } break; case 32: { for (var y = 0; y < sy; y++) { bits = bits - delta; unsafe { Byte *pixel = (Byte *)bits; for (var x = 0; x < sx; x++) { pixel[FreeImage.FI_RGBA_BLUE] = data[i++]; pixel[FreeImage.FI_RGBA_GREEN] = data[i++]; pixel[FreeImage.FI_RGBA_RED] = data[i++]; pixel[FreeImage.FI_RGBA_ALPHA] = data[i++]; pixel += 4; } } i += j; } } break; } return(dib); }
public override TImport Import(string filename, ContentImporterContext context) { FIBITMAP image = FreeImage.Allocate(100, 100, 24); //If you comment out this line and rebulid everything will work "fine". return(new FreeImageEditedFile(null)); }