public void Operators() { FreeImageBitmap fib1 = null, fib2 = null; Assert.IsTrue(fib1 == fib2); Assert.IsFalse(fib1 != fib2); Assert.IsTrue(fib1 == null); Assert.IsFalse(fib1 != null); fib1 = new FreeImageBitmap(10, 10, PixelFormat.Format24bppRgb); Assert.IsFalse(fib1 == fib2); Assert.IsTrue(fib1 != fib2); fib2 = fib1; fib1 = null; Assert.IsFalse(fib1 == fib2); Assert.IsTrue(fib1 != fib2); fib1 = new FreeImageBitmap(10, 9, PixelFormat.Format24bppRgb); Assert.IsFalse(fib1 == fib2); Assert.IsTrue(fib1 != fib2); fib2.Dispose(); fib2 = fib1; Assert.IsTrue(fib1 == fib2); Assert.IsFalse(fib1 != fib2); fib2 = fib1.Clone() as FreeImageBitmap; Assert.IsTrue(fib1 == fib2); Assert.IsFalse(fib1 != fib2); fib1.Dispose(); fib2.Dispose(); }
public void SaveAdd() { string filename = @"saveadd.tif"; FreeImageBitmap fib = new FreeImageBitmap(100, 100, PixelFormat.Format24bppRgb); try { fib.SaveAdd(); Assert.Fail(); } catch { } Assert.IsFalse(File.Exists(filename)); fib.Save(filename); fib.AdjustBrightness(0.3d); fib.SaveAdd(); FreeImageBitmap other = new FreeImageBitmap(100, 100, PixelFormat.Format24bppRgb); foreach (Scanline <RGBTRIPLE> scanline in other) { for (int i = 0; i < scanline.Length; i++) { scanline[i] = new RGBTRIPLE(Color.FromArgb(0x339955)); } } fib.SaveAdd(other); other.SaveAdd(filename); other.Dispose(); fib.Dispose(); fib = new FreeImageBitmap(filename); Assert.AreEqual(4, fib.FrameCount); fib.Dispose(); File.Delete(filename); Assert.IsFalse(File.Exists(filename)); }
public void Dispose() { if (thumbnail != null) { thumbnail.Dispose(); } }
private void ReplaceBitmap(FreeImageBitmap newBitmap) { // Checks whether the bitmap is usable if (newBitmap == null || newBitmap.IsDisposed) { MessageBox.Show("Unexpected error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Check whether the image type of the new bitmap is 'FIT_BITMAP'. // If not convert to 'FIT_BITMAP'. if (newBitmap.ImageType != FREE_IMAGE_TYPE.FIT_BITMAP) { if (!newBitmap.ConvertType(FREE_IMAGE_TYPE.FIT_BITMAP, true)) { MessageBox.Show("Error converting bitmap to standard type.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if ((bitmap != null) && !object.ReferenceEquals(bitmap, newBitmap)) { bitmap.Dispose(); } if (resultbmp != null) { resultbmp.Dispose(); } bitmap = newBitmap; resultbmp = (Bitmap)bitmap; UpdateBitmapInformations(); }
private void openImageBGThread(String filename, bool applyUpdate) { try { using (Stream imageStream = resourceProvider.openFile(filename)) { var image = new FreeImageBitmap(imageStream); int left = 0; int top = 0; int width = 8; float aspect = (float)image.Height / image.Width; int height = (int)((float)imagePanel.Width * aspect); if (height < imagePanel.Height) { width = imagePanel.Width; top = (imagePanel.Height - height) / 2; } else { aspect = (float)image.Width / image.Height; height = imagePanel.Height; width = (int)((float)imagePanel.Height * aspect); left = (imagePanel.Width - width) / 2; } ThreadManager.invoke(() => { try { if (NotDisposed) { imagePreview.setPosition(left, top); imagePreview.setSize(width, height); imageAtlas.ImageSize = new IntSize2(width, height); String imageKey = imageAtlas.addImage(Key, image); imagePreview.setItemResource(imageKey); loadingLabel.Visible = false; if (applyUpdate) { this.fireChangesMade(); this.fireApplyChanges(); } } } finally { image.Dispose(); } this.fireChangesMade(); }); } } catch (Exception ex) { Logging.Log.Error("Could not load image '{0}'. Reason: {1}", filename, ex.Message); } }
public void Dispose() { renderTexture.Dispose(); renderTexture = null; pixelBuffer.Dispose(); TextureManager.getInstance().remove(texture); texture.Dispose(); fullBitmapBox.Dispose(); fullBitmap.Dispose(); }
public void Resize(int width) { FreeImageBitmap bmp = new FreeImageBitmap(ImageFromData(Data)); int ratio = (int)((double)bmp.Height / bmp.Width * width); bmp.Rescale(width, ratio, FREE_IMAGE_FILTER.FILTER_BOX); Data = ImageToByte((Image)(bmp.Clone() as FreeImageBitmap)); bmp.Dispose(); }
public void loadThumbnail(String file, int width, int height, Action <String, IntSize2> loadedCallback) { String id = imageAtlas.getImageId(file); if (id != null) { loadedCallback(id, sizes[file]); } else { workQueue.enqueue(() => { String thumbPath = file; try { if (resourceProvider.exists(thumbPath)) { using (Stream stream = resourceProvider.openFile(thumbPath)) { var thumb = new FreeImageBitmap(stream); ThreadManager.invoke(new Action(() => { try { if (!disposed) { if (!imageAtlas.containsImage(file)) { imageAtlas.ImageSize = new IntSize2(width, height); IntSize2 size; String imageKey = imageAtlas.addImage(file, thumb, out size); sizes.Add(imageKey, size); loadedCallback(imageKey, size); } } } finally { thumb.Dispose(); } })); } } } catch (Exception ex) { Logging.Log.Error("Could not load thumbnail because of {0} exception.\nReason: {1}", ex.GetType(), ex.Message); } }); } }
public void Clone() { FreeImageBitmap fib = new FreeImageBitmap(iManager.GetBitmapPath(ImageType.Even, ImageColorType.Type_24)); object obj = new object(); fib.Tag = obj; FreeImageBitmap clone = fib.Clone() as FreeImageBitmap; Assert.IsNotNull(clone); Assert.AreEqual(fib.Width, clone.Width); Assert.AreEqual(fib.Height, clone.Height); Assert.AreEqual(fib.ColorDepth, clone.ColorDepth); Assert.AreSame(fib.Tag, clone.Tag); Assert.AreEqual(fib.ImageFormat, clone.ImageFormat); clone.Dispose(); fib.Dispose(); }
public void GetPropertyItem() { FreeImageBitmap fib = new FreeImageBitmap(iManager.GetBitmapPath(ImageType.Metadata, ImageColorType.Type_01_Dither)); int[] list = fib.PropertyIdList; Assert.IsNotNull(list); Assert.Greater(list.Length, 0); for (int i = 0; i < list.Length; i++) { PropertyItem item = fib.GetPropertyItem(list[i]); Assert.IsNotNull(item); } fib.Dispose(); }
void closeCurrentImage() { if (currentImage != null) { currentImage.Dispose(); currentImage = null; toggleRequireImagesWidgets(); } if (imageAtlas != null) { previewImage.setItemResource(""); imageAtlas.Dispose(); imageAtlas = null; } }
public unsafe void RotateFlip() { FreeImageBitmap fib = new FreeImageBitmap(2, 2, PixelFormat.Format32bppArgb); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.RotateNoneFlipX); Assert.AreEqual(0x00000002, ((int *)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000001, ((int *)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000004, ((int *)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000003, ((int *)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.RotateNoneFlipY); Assert.AreEqual(0x00000003, ((int *)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000004, ((int *)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000001, ((int *)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000002, ((int *)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.RotateNoneFlipXY); Assert.AreEqual(0x00000004, ((int *)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000003, ((int *)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000002, ((int *)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000001, ((int *)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.Rotate90FlipNone); Assert.AreEqual(0x00000003, ((int *)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000001, ((int *)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000004, ((int *)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000002, ((int *)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.Rotate90FlipX); Assert.AreEqual(0x00000001, ((int *)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000003, ((int *)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000002, ((int *)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000004, ((int *)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.Rotate90FlipY); Assert.AreEqual(0x00000004, ((int *)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000002, ((int *)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000003, ((int *)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000001, ((int *)fib.GetScanlinePointer(1))[1]); fib.Dispose(); }
/// <summary> /// Create a Picture out of the given filename /// </summary> /// <param name="fileName"></param> public Picture(string fileName) { try { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { FreeImageBitmap img = new FreeImageBitmap(fs); fs.Close(); Data = ImageToByte((Image)(img.Clone() as FreeImageBitmap)); img.Dispose(); } } catch (Exception ex) { ServiceScope.Get <ILogger>().GetLogger.Error("Error creating picture from file: {0}. Error: {1}", fileName, ex.Message); } }
// Replaces the current bitmap with the given one. private void ReplaceBitmap(FreeImageBitmap newBitmap) { // Checks whether the bitmap is usable if (newBitmap == null || newBitmap.IsDisposed) { MessageBox.Show( "Unexpected error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Check whether the image type of the new bitmap is 'FIT_BITMAP'. // If not convert to 'FIT_BITMAP'. if (newBitmap.ImageType != FREE_IMAGE_TYPE.FIT_BITMAP) { if (!newBitmap.ConvertType(FREE_IMAGE_TYPE.FIT_BITMAP, true)) { MessageBox.Show( "Error converting bitmap to standard type.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // Dispose the old bitmap only in case it exists and // the old instance is another than the new one. if ((bitmap != null) && !object.ReferenceEquals(bitmap, newBitmap)) { bitmap.Dispose(); } // Dispose the picturebox's bitmap in case it exists. if (pictureBox.Image != null) { pictureBox.Image.Dispose(); } // Set the new bitmap. pictureBox.Image = (Bitmap)(bitmap = newBitmap); // Update gui. UpdateBitmapInformations(); UpdateFrameSelection(); }
/// <summary> /// Create a Picture out of the given filename /// </summary> /// <param name="fileName"></param> public Picture(string fileName) { try { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { FreeImageBitmap img = new FreeImageBitmap(fs); fs.Close(); Data = ImageToByte((Image)(img.Clone() as FreeImageBitmap)); img.Dispose(); } } catch (Exception) { // ignored } }
public void Clone() { FreeImageBitmap fib = new FreeImageBitmap(iManager.GetBitmapPath(ImageType.Even, ImageColorType.Type_24)); object obj = new object(); fib.Tag = obj; FreeImageBitmap clone = fib.Clone() as FreeImageBitmap; Assert.IsNotNull(clone); Assert.AreEqual(fib.Width, clone.Width); Assert.AreEqual(fib.Height, clone.Height); Assert.AreEqual(fib.ColorDepth, clone.ColorDepth); Assert.AreSame(fib.Tag, clone.Tag); Assert.AreEqual(fib.ImageFormat, clone.ImageFormat); clone.Dispose(); fib.Dispose(); }
/// <summary> /// Create a Picture out of the given filename /// </summary> /// <param name="fileName"></param> public Picture(string fileName) { try { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { FreeImageBitmap img = new FreeImageBitmap(fs); fs.Close(); Data = (Image)(img.Clone() as FreeImageBitmap); img.Dispose(); } } catch (Exception ex) { ServiceScope.Get<ILogger>().GetLogger.Error("Error creating picture from file: {0}. Error: {1}", fileName, ex.Message); } }
/// <summary> /// This will save all outstanding thumbnails to the disk. /// </summary> public void saveThumbnails() { foreach (Slide slide in unsavedThumbs.Keys) { FreeImageBitmap thumb = unsavedThumbs[slide]; saveThumbnail(Path.Combine(slide.UniqueName, Slideshow.SlideThumbName), thumb); thumb.Dispose(); } unsavedThumbs.Clear(); foreach (Slide slide in unsavedSceneThumbs.Keys) { var thumb = unsavedSceneThumbs[slide]; saveSceneThumbInfo(slide, thumb); thumb.Dispose(); } unsavedSceneThumbs.Clear(); }
public void RemovePropertyItem() { FreeImageBitmap fib = new FreeImageBitmap(iManager.GetBitmapPath(ImageType.Metadata, ImageColorType.Type_01_Dither)); Random rand = new Random(); int[] list = fib.PropertyIdList; int length = list.Length; Assert.Greater(list.Length, 0); int id = list[rand.Next(0, list.Length - 1)]; Assert.IsNotNull(fib.GetPropertyItem(id)); fib.RemovePropertyItem(id); list = fib.PropertyIdList; Assert.That((list.Length + 1) == length); fib.Dispose(); }
public void GetBounds() { Random rand = new Random(); int height = rand.Next(1, 100), width = rand.Next(1, 100); FreeImageBitmap fib = new FreeImageBitmap(width, height, PixelFormat.Format24bppRgb); Assert.AreEqual(fib.VerticalResolution, fib.HorizontalResolution); GraphicsUnit unit; RectangleF rect; unit = GraphicsUnit.Display; rect = fib.GetBounds(ref unit); Assert.AreEqual(GraphicsUnit.Pixel, unit); Assert.AreEqual(width, (int)rect.Width); Assert.AreEqual(height, (int)rect.Height); fib.Dispose(); }
public void loadThumbnail(Slide slide, Action <Slide, String> loadedCallback) { String id = imageAtlas.getImageId(slide.UniqueName); if (id != null) { loadedCallback(slide, id); } else { workQueue.enqueue(() => { String thumbPath = Path.Combine(slide.UniqueName, Slideshow.SlideThumbName); try { if (slideEditController.ResourceProvider.exists(thumbPath)) { using (Stream stream = slideEditController.ResourceProvider.openFile(thumbPath)) { var thumb = new FreeImageBitmap(stream); ThreadManager.invoke(new Action(() => { try { if (!imageAtlas.containsImage(slide.UniqueName)) { loadedCallback(slide, imageAtlas.addImage(slide.UniqueName, thumb)); } } finally { thumb.Dispose(); } })); } } } catch (Exception ex) { Logging.Log.Error("Could not load thumbnail because of {0} exception.\nReason: {1}", ex.GetType(), ex.Message); } }); } }
static void Main(string[] args) { // string path = (@"E:\C\work\2019-07-00\贴图流程化测试\eeee.tga"); string path = args[0]; FreeImageBitmap ne = new FreeImageBitmap(path, FREE_IMAGE_FORMAT.FIF_TARGA); Size b = ne.Size; int image_h = b.Height; int image_w = b.Width; /// 这里抛出移除是因为会当做方块来循环, 应该是小 // FreeImageBitmap new_tem = new FreeImageBitmap(image_w, image_h); Bitmap new_tem = new Bitmap(image_w, image_h); Color tt = ne.GetPixel(0, 0); /// 优先获得高度 在高度上去处理 宽度 Console.WriteLine(image_w); Bitmap ne_type = (Bitmap)ne.ToBitmap(); ne.Dispose(); for (int i = 0; i < image_h; i++) { for (int j = 0; j < image_w; j++) { //Color t = ne.GetPixel(i, j); /// 优先获得高度 在高度上去处理 宽度 Color t = ne_type.GetPixel(j, i); Program pro = new Program(); Color srgbcolor = pro.linetosrgb(t); new_tem.SetPixel(j, i, srgbcolor); } } ///对图形处理完成 /// FreeImageBitmap yy = new FreeImageBitmap(new_tem); yy.Save(path); }
public void GetScanline() { FreeImageBitmap fib; fib = new FreeImageBitmap(10, 10, PixelFormat.Format1bppIndexed); Scanline <FI1BIT> scanline1 = (Scanline <FI1BIT>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format4bppIndexed); Scanline <FI4BIT> scanline2 = (Scanline <FI4BIT>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format8bppIndexed); Scanline <Byte> scanline3 = (Scanline <Byte>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format16bppRgb555); Scanline <FI16RGB555> scanline4 = (Scanline <FI16RGB555>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format16bppRgb565); Scanline <FI16RGB565> scanline5 = (Scanline <FI16RGB565>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format24bppRgb); Scanline <RGBTRIPLE> scanline6 = (Scanline <RGBTRIPLE>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format32bppArgb); Scanline <RGBQUAD> scanline7 = (Scanline <RGBQUAD>)fib.GetScanline(0); fib.Dispose(); }
public override bool Close() { if (m_file != null) { m_file.Close(); } if (m_picbxImage.Image != null) { m_picbxImage.Image.Dispose(); m_picbxImage.Image = null; } if (m_binary != null) { m_binary.Close(); m_binary.Dispose(); } m_picbxImage.Dispose(); if (m_image != null) { m_image.Dispose(); } return(true); }
public void Resize(int width) { FreeImageBitmap bmp = new FreeImageBitmap(Data); int ratio = (int)((double)bmp.Height / bmp.Width * width); bmp.Rescale(width, ratio, FREE_IMAGE_FILTER.FILTER_BOX); Data = (Image) (bmp.Clone() as FreeImageBitmap); bmp.Dispose(); }
/// <summary> /// 根据传入图片判读图片格式是什么格式 格式只是识别 rgb argb //// 返回 0 文件路径不对 或者格式不对 //// 返回 24 32 说明通道企管科 /// </summary> /// <returns></returns> public int is3224() { if (!File.Exists(this.imagepath)) { ///路径不对也是按户籍0 return(0); } FreeImageBitmap fb = new FreeImageBitmap(this.imagepath); Bitmap bim = fb.ToBitmap(); fb.Dispose(); ///这里只是处理美术用的2中格式 rgb argb 其他返回0 /// PixelFormat imformat = bim.PixelFormat; if (imformat == PixelFormat.Format24bppRgb) { bim.Dispose(); return(24); } else if (imformat == PixelFormat.Format32bppArgb) { //// 这里还要判断是不是真的32位, 对于有一个a通道但是全是纯白的,这种情况就是假的a通道, 会当作24处理 /// 最后返回的还是 24 /// int wdith = bim.Width; int height = bim.Height; for (int i = 0; i < height; i++) { for (int fo = 0; fo < wdith; fo++) { /// 防止加入变大 从0开始 if (fo > wdith - 1) { fo = wdith - 1; } ; if (i > height - 1) { i = height - 1; } ; Color cl = bim.GetPixel(fo, i); if (cl.A != this.bai) { /// 本来这里就是告诉你这里使用的是32位图,有一个不是255 就告诉你 就是32 不用在处理的 /// 直接返回你就是32位 return(32); } fo += this.ina; } i += this.ina; } /// 处理完成之后每10 像素采样完成 都是白色 直接返回 24 return(24); } else { bim.Dispose(); //// 对于其他格式一律不处理 返回0 表示该图有问题 return(0); } }
public void Dispose() { image.Dispose(); }
public void GetScanlines() { FreeImageBitmap fib; fib = new FreeImageBitmap(10, 10, PixelFormat.Format1bppIndexed); IList <Scanline <FI1BIT> > scanline01 = (IList <Scanline <FI1BIT> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format4bppIndexed); IList <Scanline <FI4BIT> > scanline02 = (IList <Scanline <FI4BIT> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format8bppIndexed); IList <Scanline <Byte> > scanline03 = (IList <Scanline <Byte> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format16bppRgb555); IList <Scanline <FI16RGB555> > scanline04 = (IList <Scanline <FI16RGB555> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format16bppRgb565); IList <Scanline <FI16RGB565> > scanline05 = (IList <Scanline <FI16RGB565> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format24bppRgb); IList <Scanline <RGBTRIPLE> > scanline06 = (IList <Scanline <RGBTRIPLE> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format32bppArgb); IList <Scanline <RGBQUAD> > scanline07 = (IList <Scanline <RGBQUAD> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_COMPLEX); IList <Scanline <FICOMPLEX> > scanline08 = (IList <Scanline <FICOMPLEX> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_DOUBLE); IList <Scanline <Double> > scanline09 = (IList <Scanline <Double> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_FLOAT); IList <Scanline <Single> > scanline10 = (IList <Scanline <Single> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_INT16); IList <Scanline <Int16> > scanline11 = (IList <Scanline <Int16> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_INT32); IList <Scanline <Int32> > scanline12 = (IList <Scanline <Int32> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_RGB16); IList <Scanline <FIRGB16> > scanline13 = (IList <Scanline <FIRGB16> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_RGBA16); IList <Scanline <FIRGBA16> > scanline14 = (IList <Scanline <FIRGBA16> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_RGBAF); IList <Scanline <FIRGBAF> > scanline15 = (IList <Scanline <FIRGBAF> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_RGBF); IList <Scanline <FIRGBF> > scanline16 = (IList <Scanline <FIRGBF> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_UINT16); IList <Scanline <UInt16> > scanline17 = (IList <Scanline <UInt16> >)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_UINT32); IList <Scanline <UInt32> > scanline18 = (IList <Scanline <UInt32> >)fib.GetScanlines(); fib.Dispose(); }
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(); }
public void RemovePropertyItem() { FreeImageBitmap fib = new FreeImageBitmap(iManager.GetBitmapPath(ImageType.Metadata, ImageColorType.Type_01_Dither)); Random rand = new Random(); int[] list = fib.PropertyIdList; int length = list.Length; Assert.Greater(list.Length, 0); int id = list[rand.Next(0, list.Length - 1)]; Assert.IsNotNull(fib.GetPropertyItem(id)); fib.RemovePropertyItem(id); list = fib.PropertyIdList; Assert.That((list.Length + 1) == length); fib.Dispose(); }
/// <summary> /// Create new sprite file from image files. /// </summary> /// <param name="outputPath">Output filename *.SPR</param> /// <param name="files">Input image paths.</param> /// <param name="spriteType">SprType</param> /// <param name="textFormat">SprTextFormat</param> /// <param name="palIndex">Which palette use from files</param> public static void CreateSpriteFile(string outputPath, string[] files, SprType spriteType, SprTextFormat textFormat, int palIndex, Color alphaReplacementColor) { List <FreeImageBitmap> images = files.Select(file => new FreeImageBitmap(file)).ToList(); //Retrieve maximum width, height int prevSize = 0; int maxW = 0, maxH = 0; for (int i = 0; i < images.Count; i++) { FreeImageBitmap image = images[i]; if ((image.Height + image.Width) > prevSize) { prevSize = image.Height + image.Width; maxW = image.Width; maxH = image.Height; } if (image.IsTransparent) { image.SwapColors(new RGBQUAD(Color.Transparent), new RGBQUAD(alphaReplacementColor), false); } } //Calc. bounding box float f = (float)Math.Sqrt((maxW >> 1) * (maxW >> 1) + (maxH >> 1) * (maxH >> 1)); using (BinaryWriter bw = new BinaryWriter(new FileStream(outputPath, FileMode.Create))) { //Write header first bw.Write(SpriteHeaderId.ToCharArray()); bw.Write(2); bw.Write((uint)spriteType); bw.Write((uint)textFormat); bw.Write(f); bw.Write(maxW); bw.Write(maxH); bw.Write(images.Count); bw.Write(0.0f); //Always 0 ? bw.Write(1); //Synch. type //Color palette bw.Write((ushort)MaxPaletteColors); //Always 256 ? if ((palIndex > (images.Count - 1)) || palIndex < images.Count) { palIndex = 0; } if (!images[palIndex].HasPalette || images[palIndex].Palette.Length != 256) { images[palIndex].ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP); } Palette pal = images[palIndex].Palette; for (int i = 0; i < 256; i++) { bw.Write(pal[i].rgbRed); bw.Write(pal[i].rgbGreen); bw.Write(pal[i].rgbBlue); } //Write images for (int i = 0; i < images.Count; i++) { bw.Write(0); //group bw.Write(-(images[i].Width / 2)); //origin x bw.Write(images[i].Height / 2); //origin y bw.Write(images[i].Width); //w bw.Write(images[i].Height); //h byte[] arr = new byte[images[i].Width * images[i].Height]; images[i].RotateFlip(RotateFlipType.RotateNoneFlipX); System.Runtime.InteropServices.Marshal.Copy(images[i].GetScanlinePointer(0), arr, 0, arr.Length); Array.Reverse(arr); bw.Write(arr); } } //Free resources images.ForEach(image => image.Dispose()); }
public void SaveAdd() { string filename = @"saveadd.tif"; FreeImageBitmap fib = new FreeImageBitmap(100, 100, PixelFormat.Format24bppRgb); try { fib.SaveAdd(); Assert.Fail(); } catch { } Assert.IsFalse(File.Exists(filename)); fib.Save(filename); fib.AdjustBrightness(0.3d); fib.SaveAdd(); FreeImageBitmap other = new FreeImageBitmap(100, 100, PixelFormat.Format24bppRgb); foreach (Scanline<RGBTRIPLE> scanline in other) { for (int i = 0; i < scanline.Length; i++) { scanline[i] = new RGBTRIPLE(Color.FromArgb(0x339955)); } } fib.SaveAdd(other); other.SaveAdd(filename); other.Dispose(); fib.Dispose(); fib = new FreeImageBitmap(filename); Assert.AreEqual(4, fib.FrameCount); fib.Dispose(); File.Delete(filename); Assert.IsFalse(File.Exists(filename)); }
public void Operators() { FreeImageBitmap fib1 = null, fib2 = null; Assert.IsTrue(fib1 == fib2); Assert.IsFalse(fib1 != fib2); Assert.IsTrue(fib1 == null); Assert.IsFalse(fib1 != null); fib1 = new FreeImageBitmap(10, 10, PixelFormat.Format24bppRgb); Assert.IsFalse(fib1 == fib2); Assert.IsTrue(fib1 != fib2); fib2 = fib1; fib1 = null; Assert.IsFalse(fib1 == fib2); Assert.IsTrue(fib1 != fib2); fib1 = new FreeImageBitmap(10, 9, PixelFormat.Format24bppRgb); Assert.IsFalse(fib1 == fib2); Assert.IsTrue(fib1 != fib2); fib2.Dispose(); fib2 = fib1; Assert.IsTrue(fib1 == fib2); Assert.IsFalse(fib1 != fib2); fib2 = fib1.Clone() as FreeImageBitmap; Assert.IsTrue(fib1 == fib2); Assert.IsFalse(fib1 != fib2); fib1.Dispose(); fib2.Dispose(); }
public void GetScanline() { FreeImageBitmap fib; fib = new FreeImageBitmap(10, 10, PixelFormat.Format1bppIndexed); Scanline<FI1BIT> scanline1 = (Scanline<FI1BIT>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format4bppIndexed); Scanline<FI4BIT> scanline2 = (Scanline<FI4BIT>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format8bppIndexed); Scanline<Byte> scanline3 = (Scanline<Byte>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format16bppRgb555); Scanline<FI16RGB555> scanline4 = (Scanline<FI16RGB555>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format16bppRgb565); Scanline<FI16RGB565> scanline5 = (Scanline<FI16RGB565>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format24bppRgb); Scanline<RGBTRIPLE> scanline6 = (Scanline<RGBTRIPLE>)fib.GetScanline(0); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format32bppArgb); Scanline<RGBQUAD> scanline7 = (Scanline<RGBQUAD>)fib.GetScanline(0); fib.Dispose(); }
public unsafe void GetSetPixel() { Random rand = new Random(); FreeImageBitmap fib = new FreeImageBitmap(2, 1, PixelFormat.Format1bppIndexed); Palette palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(16, 1, PixelFormat.Format4bppIndexed); palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(256, 1, PixelFormat.Format8bppIndexed); palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format16bppRgb555); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.That(Math.Abs(orgColor.B - newColor.B) <= 8); Assert.That(Math.Abs(orgColor.G - newColor.G) <= 8); Assert.That(Math.Abs(orgColor.R - newColor.R) <= 8); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format24bppRgb); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.AreEqual(orgColor.B, newColor.B); Assert.AreEqual(orgColor.G, newColor.G); Assert.AreEqual(orgColor.R, newColor.R); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format32bppArgb); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.AreEqual(orgColor.B, newColor.B); Assert.AreEqual(orgColor.G, newColor.G); Assert.AreEqual(orgColor.R, newColor.R); Assert.AreEqual(orgColor.A, newColor.A); } fib.Dispose(); }
public unsafe void RotateFlip() { FreeImageBitmap fib = new FreeImageBitmap(2, 2, PixelFormat.Format32bppArgb); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.RotateNoneFlipX); Assert.AreEqual(0x00000002, ((int*)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000001, ((int*)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000004, ((int*)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000003, ((int*)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.RotateNoneFlipY); Assert.AreEqual(0x00000003, ((int*)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000004, ((int*)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000001, ((int*)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000002, ((int*)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.RotateNoneFlipXY); Assert.AreEqual(0x00000004, ((int*)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000003, ((int*)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000002, ((int*)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000001, ((int*)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.Rotate90FlipNone); Assert.AreEqual(0x00000003, ((int*)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000001, ((int*)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000004, ((int*)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000002, ((int*)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.Rotate90FlipX); Assert.AreEqual(0x00000001, ((int*)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000003, ((int*)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000002, ((int*)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000004, ((int*)fib.GetScanlinePointer(1))[1]); ResetRotateBitmap(fib); fib.RotateFlip(RotateFlipType.Rotate90FlipY); Assert.AreEqual(0x00000004, ((int*)fib.GetScanlinePointer(0))[0]); Assert.AreEqual(0x00000002, ((int*)fib.GetScanlinePointer(0))[1]); Assert.AreEqual(0x00000003, ((int*)fib.GetScanlinePointer(1))[0]); Assert.AreEqual(0x00000001, ((int*)fib.GetScanlinePointer(1))[1]); fib.Dispose(); }
public void GetBounds() { Random rand = new Random(); int height = rand.Next(1, 100), width = rand.Next(1, 100); FreeImageBitmap fib = new FreeImageBitmap(width, height, PixelFormat.Format24bppRgb); Assert.AreEqual(fib.VerticalResolution, fib.HorizontalResolution); GraphicsUnit unit; RectangleF rect; unit = GraphicsUnit.Display; rect = fib.GetBounds(ref unit); Assert.AreEqual(GraphicsUnit.Pixel, unit); Assert.AreEqual(width, (int)rect.Width); Assert.AreEqual(height, (int)rect.Height); fib.Dispose(); }
private bool MergeImages(string fileNameFormat, string outputDir, string prefix, int frameNum, string ext) { FreeImageBitmap combined = null; string fname = ""; bool supported = false; bool mergeSuccessful = true; FREE_IMAGE_TYPE type = FREE_IMAGE_TYPE.FIT_UNKNOWN; FreeImageBitmap source = null; string mergedFile = string.Format(fileNameFormat, outputDir, prefix, frameNum, ext); string tempFile = string.Format(fileNameFormat, outputDir, prefix, frameNum, "_tmp" + ext); // Allocate a bitmap to store the final image fname = string.Format(fileNameFormat, outputDir, "slice_0_" + prefix, frameNum, ext); try { source = new FreeImageBitmap(fname); if (source != null) { type = source.ImageType; switch (type) { case FREE_IMAGE_TYPE.FIT_BITMAP: if (source.ColorDepth == 32 || source.ColorDepth == 24) supported = true; break; case FREE_IMAGE_TYPE.FIT_RGB16: case FREE_IMAGE_TYPE.FIT_RGBA16: case FREE_IMAGE_TYPE.FIT_RGBAF: case FREE_IMAGE_TYPE.FIT_RGBF: supported = true; break; } } } catch (Exception ex) { logger.Error(ex, "Error opening slice file"); } if (supported == false) { Console.WriteLine("Image format not supported"); return false; } try { // Create a new image of the input file type and the correct size FIBITMAP newImage = FreeImage.AllocateT(type, Width, Height, source.ColorDepth, source.RedMask, source.BlueMask, source.GreenMask); FreeImage.SaveEx(newImage, tempFile); FreeImage.UnloadEx(ref newImage); source.Dispose(); source = null; GC.Collect(); GC.WaitForPendingFinalizers(); combined = new FreeImageBitmap(tempFile); } catch (Exception ex) { logger.Error(ex, "Error creating output file"); mergeSuccessful = false; } for (int i = 0; i < SlicesAcross * SlicesDown; i++) { // Load the image slice fname = string.Format(fileNameFormat, outputDir, "slice_" + i + "_" + prefix, frameNum, ext); FreeImageBitmap slice = new FreeImageBitmap(fname); int posX; int posY; if (SlicesDown > 1 && SlicesAcross > 1) { posX = i % SlicesAcross; posY = i / SlicesAcross; } else if (SlicesDown == 1) { posX = i; posY = 0; } else { posX = 0; posY = i; } // Calculate the image slice sizes and the row/column position double sizeV = (1.0 / SlicesDown) * Height; double sizeH = (1.0 / SlicesAcross) * Width; double overlapV = sizeV * (Overlap / 100.0); double overlapH = sizeH * (Overlap / 100.0); double realLeft = sizeH * posX; double left = realLeft - overlapH; double realTop = sizeV * posY; double top = realTop - overlapV; // Check the sizes are within limits and adjust left = Math.Max(0.0, left); top = Math.Max(0.0, top); try { switch (type) { case FREE_IMAGE_TYPE.FIT_BITMAP: if (slice.ColorDepth == 24) { for (int y = 0; y < slice.Height; y++) { int srcY = (slice.Height - 1) - y; int destY = (combined.Height - 1) - (srcY + (int)top); int topY = y + (int)top; Scanline<RGBTRIPLE> srcLine = (Scanline<RGBTRIPLE>)slice.GetScanline(y); Scanline<RGBTRIPLE> destLine = (Scanline<RGBTRIPLE>)combined.GetScanline(destY); for (int x = 0; x < slice.Width; x++) { int destX = x + (int)left; // Make sure it's not out of bounds if (destY >= Height || destY >= Width) continue; // Is the pixel in an overlapping Area if (topY < realTop || destX < realLeft) { MergePixel srcPixel = new MergePixel(srcLine[x].rgbtRed, srcLine[x].rgbtGreen, srcLine[x].rgbtBlue, 0); MergePixel destPixel = new MergePixel(destLine[destX].rgbtRed, destLine[destX].rgbtGreen, destLine[destX].rgbtBlue, 0); destPixel = CalculatePixelWeight(overlapV, overlapH, realLeft, left, realTop, top, y, topY, x, srcPixel, destPixel); RGBTRIPLE dest; dest.rgbtRed = destPixel.red > 255.0 ? (byte)255 : (byte)destPixel.red; dest.rgbtGreen = destPixel.green > 255.0 ? (byte)255 : (byte)destPixel.green; dest.rgbtBlue = destPixel.blue > 255.0 ? (byte)255 : (byte)destPixel.blue; destLine[destX] = dest; } else destLine[destX] = srcLine[x]; } } } else { for (int y = 0; y < slice.Height; y++) { int srcY = (slice.Height - 1) - y; int destY = (combined.Height - 1) - (srcY + (int)top); int topY = y + (int)top; Scanline<RGBQUAD> srcLine = (Scanline<RGBQUAD>)slice.GetScanline(y); Scanline<RGBQUAD> destLine = (Scanline<RGBQUAD>)combined.GetScanline(destY); for (int x = 0; x < slice.Width; x++) { int destX = x + (int)left; // Make sure it's not out of bounds if (destY >= Height || destY >= Width) continue; // Is the pixel in an overlapping Area if (topY < realTop || destX < realLeft) { MergePixel srcPixel = new MergePixel(srcLine[x].rgbRed, srcLine[x].rgbGreen, srcLine[x].rgbBlue, destLine[destX].rgbReserved); MergePixel destPixel = new MergePixel(destLine[destX].rgbRed, destLine[destX].rgbGreen, destLine[destX].rgbBlue, destLine[destX].rgbReserved); destPixel = CalculatePixelWeight(overlapV, overlapH, realLeft, left, realTop, top, y, topY, x, srcPixel, destPixel); RGBQUAD dest = new RGBQUAD(); dest.rgbRed = destPixel.red > 255.0 ? (byte)255 : (byte)destPixel.red; dest.rgbGreen = destPixel.green > 255.0 ? (byte)255 : (byte)destPixel.green; dest.rgbBlue = destPixel.blue > 255.0 ? (byte)255 : (byte)destPixel.blue; dest.rgbReserved = destPixel.alpha > 255.0 ? (byte)255 : (byte)destPixel.alpha; destLine[destX] = dest; } else destLine[destX] = srcLine[x]; } } } break; case FREE_IMAGE_TYPE.FIT_RGB16: for (int y = 0; y < slice.Height; y++) { int srcY = (slice.Height - 1) - y; int destY = (combined.Height - 1) - (srcY + (int)top); int topY = y + (int)top; Scanline<FIRGB16> srcLine = (Scanline<FIRGB16>)slice.GetScanline(y); Scanline<FIRGB16> destLine = (Scanline<FIRGB16>)combined.GetScanline(destY); for (int x = 0; x < slice.Width; x++) { int destX = x + (int)left; // Make sure it's not out of bounds if (destY >= Height || destY >= Width) continue; // Is the pixel in an overlapping Area if (topY < realTop || destX < realLeft) { MergePixel srcPixel = new MergePixel(srcLine[x].red, srcLine[x].green, srcLine[x].blue, 0); MergePixel destPixel = new MergePixel(destLine[destX].red, destLine[destX].green, destLine[destX].blue, 0); destPixel = CalculatePixelWeight(overlapV, overlapH, realLeft, left, realTop, top, y, topY, x, srcPixel, destPixel); FIRGB16 dest = new FIRGB16(); dest.red = (ushort)destPixel.red; dest.green = (ushort)destPixel.green; dest.blue = (ushort)destPixel.blue; destLine[destX] = dest; } else destLine[destX] = srcLine[x]; } } break; case FREE_IMAGE_TYPE.FIT_RGBA16: for (int y = 0; y < slice.Height; y++) { int srcY = (slice.Height - 1) - y; int destY = (combined.Height - 1) - (srcY + (int)top); int topY = y + (int)top; Scanline<FIRGBA16> srcLine = (Scanline<FIRGBA16>)slice.GetScanline(y); Scanline<FIRGBA16> destLine = (Scanline<FIRGBA16>)combined.GetScanline(destY); for (int x = 0; x < slice.Width; x++) { int destX = x + (int)left; // Make sure it's not out of bounds if (destY >= Height || destY >= Width) continue; // Is the pixel in an overlapping Area if (topY < realTop || destX < realLeft) { MergePixel srcPixel = new MergePixel(srcLine[x].red, srcLine[x].green, srcLine[x].blue, srcLine[x].alpha); MergePixel destPixel = new MergePixel(destLine[destX].red, destLine[destX].green, destLine[destX].blue, destLine[destX].alpha); destPixel = CalculatePixelWeight(overlapV, overlapH, realLeft, left, realTop, top, y, topY, x, srcPixel, destPixel); FIRGBA16 dest = new FIRGBA16(); dest.red = (ushort)destPixel.red; dest.green = (ushort)destPixel.green; dest.blue = (ushort)destPixel.blue; dest.alpha = (ushort)destPixel.alpha; destLine[destX] = dest; } else destLine[destX] = srcLine[x]; } } break; case FREE_IMAGE_TYPE.FIT_RGBAF: for (int y = 0; y < slice.Height; y++) { int srcY = (slice.Height - 1) - y; int destY = (combined.Height - 1) - (srcY + (int)top); int topY = y + (int)top; Scanline<FIRGBAF> srcLine = (Scanline<FIRGBAF>)slice.GetScanline(y); Scanline<FIRGBAF> destLine = (Scanline<FIRGBAF>)combined.GetScanline(destY); for (int x = 0; x < slice.Width; x++) { int destX = x + (int)left; // Make sure it's not out of bounds if (destY >= Height || destY >= Width) continue; // Is the pixel in an overlapping Area if (topY < realTop || destX < realLeft) { MergePixel srcPixel = new MergePixel(srcLine[x].red, srcLine[x].green, srcLine[x].blue, destLine[destX].alpha); MergePixel destPixel = new MergePixel(destLine[destX].red, destLine[destX].green, destLine[destX].blue, destLine[destX].alpha); destPixel = CalculatePixelWeight(overlapV, overlapH, realLeft, left, realTop, top, y, topY, x, srcPixel, destPixel); FIRGBAF dest = new FIRGBAF(); dest.red = (float)destPixel.red; dest.green = (float)destPixel.green; dest.blue = (float)destPixel.blue; dest.alpha = (float)destPixel.alpha; destLine[destX] = dest; } else destLine[destX] = srcLine[x]; } } break; case FREE_IMAGE_TYPE.FIT_RGBF: for (int y = 0; y < slice.Height; y++) { int srcY = (slice.Height - 1) - y; int destY = (combined.Height - 1) - (srcY + (int)top); int topY = y + (int)top; Scanline<FIRGBF> srcLine = (Scanline<FIRGBF>)slice.GetScanline(y); Scanline<FIRGBF> destLine = (Scanline<FIRGBF>)combined.GetScanline(destY); for (int x = 0; x < slice.Width; x++) { int destX = x + (int)left; // Make sure it's not out of bounds if (destY >= Height || destY >= Width) continue; // Is the pixel in an overlapping Area if (topY < realTop || destX < realLeft) { MergePixel srcPixel = new MergePixel(srcLine[x].red, srcLine[x].green, srcLine[x].blue, 0); MergePixel destPixel = new MergePixel(destLine[destX].red, destLine[destX].green, destLine[destX].blue, 0); destPixel = CalculatePixelWeight(overlapV, overlapH, realLeft, left, realTop, top, y, topY, x, srcPixel, destPixel); FIRGBF dest = new FIRGBF(); dest.red = (float)destPixel.red; dest.green = (float)destPixel.green; dest.blue = (float)destPixel.blue; destLine[destX] = dest; } else destLine[destX] = srcLine[x]; } } break; } slice.Dispose(); } catch (Exception ex) { logger.Error(ex, "Error merging image files"); mergeSuccessful = false; } } try { if (mergeSuccessful) { combined.Save(mergedFile); combined.Dispose(); combined = null; GC.Collect(); GC.WaitForPendingFinalizers(); File.Delete(tempFile); } else { Log += DateTime.Now.ToLongTimeString() + " Merging frame " + frameNum + " failed.\n"; } } catch (Exception ex) { logger.Error(ex, "Error writing combined file"); mergeSuccessful = false; } return mergeSuccessful; }
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 void GetPropertyItem() { FreeImageBitmap fib = new FreeImageBitmap(iManager.GetBitmapPath(ImageType.Metadata, ImageColorType.Type_01_Dither)); int[] list = fib.PropertyIdList; Assert.IsNotNull(list); Assert.Greater(list.Length, 0); for (int i = 0; i < list.Length; i++) { PropertyItem item = fib.GetPropertyItem(list[i]); Assert.IsNotNull(item); } fib.Dispose(); }
public void GetScanlines() { FreeImageBitmap fib; fib = new FreeImageBitmap(10, 10, PixelFormat.Format1bppIndexed); IList<Scanline<FI1BIT>> scanline01 = (IList<Scanline<FI1BIT>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format4bppIndexed); IList<Scanline<FI4BIT>> scanline02 = (IList<Scanline<FI4BIT>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format8bppIndexed); IList<Scanline<Byte>> scanline03 = (IList<Scanline<Byte>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format16bppRgb555); IList<Scanline<FI16RGB555>> scanline04 = (IList<Scanline<FI16RGB555>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format16bppRgb565); IList<Scanline<FI16RGB565>> scanline05 = (IList<Scanline<FI16RGB565>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format24bppRgb); IList<Scanline<RGBTRIPLE>> scanline06 = (IList<Scanline<RGBTRIPLE>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, PixelFormat.Format32bppArgb); IList<Scanline<RGBQUAD>> scanline07 = (IList<Scanline<RGBQUAD>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_COMPLEX); IList<Scanline<FICOMPLEX>> scanline08 = (IList<Scanline<FICOMPLEX>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_DOUBLE); IList<Scanline<Double>> scanline09 = (IList<Scanline<Double>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_FLOAT); IList<Scanline<Single>> scanline10 = (IList<Scanline<Single>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_INT16); IList<Scanline<Int16>> scanline11 = (IList<Scanline<Int16>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_INT32); IList<Scanline<Int32>> scanline12 = (IList<Scanline<Int32>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_RGB16); IList<Scanline<FIRGB16>> scanline13 = (IList<Scanline<FIRGB16>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_RGBA16); IList<Scanline<FIRGBA16>> scanline14 = (IList<Scanline<FIRGBA16>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_RGBAF); IList<Scanline<FIRGBAF>> scanline15 = (IList<Scanline<FIRGBAF>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_RGBF); IList<Scanline<FIRGBF>> scanline16 = (IList<Scanline<FIRGBF>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_UINT16); IList<Scanline<UInt16>> scanline17 = (IList<Scanline<UInt16>>)fib.GetScanlines(); fib.Dispose(); fib = new FreeImageBitmap(10, 10, FREE_IMAGE_TYPE.FIT_UINT32); IList<Scanline<UInt32>> scanline18 = (IList<Scanline<UInt32>>)fib.GetScanlines(); fib.Dispose(); }
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 GetSetPixel() { Random rand = new Random(); FreeImageBitmap fib = new FreeImageBitmap(2, 1, PixelFormat.Format1bppIndexed); Palette palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(16, 1, PixelFormat.Format4bppIndexed); palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(256, 1, PixelFormat.Format8bppIndexed); palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format16bppRgb555); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.That(Math.Abs(orgColor.B - newColor.B) <= 8); Assert.That(Math.Abs(orgColor.G - newColor.G) <= 8); Assert.That(Math.Abs(orgColor.R - newColor.R) <= 8); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format24bppRgb); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.AreEqual(orgColor.B, newColor.B); Assert.AreEqual(orgColor.G, newColor.G); Assert.AreEqual(orgColor.R, newColor.R); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format32bppArgb); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.AreEqual(orgColor.B, newColor.B); Assert.AreEqual(orgColor.G, newColor.G); Assert.AreEqual(orgColor.R, newColor.R); Assert.AreEqual(orgColor.A, newColor.A); } fib.Dispose(); }
public static Response GetPageImage(Guid id, int page, int width, int height, IResponseFormatter response) { // Restrict access to the FreeImage library to one thread at a time. lock (lockThis) { int max_width = 0; int max_height = 0; bool thumbnail = !(width == -1 && height == -1); bool processed = false; string filename = string.Format("{0}-p{1}-w{2}-h{3}.jpg", id, page, width, height); if (thumbnail) { MemoryStream cachestream = ImageCache.Instance.LoadFromCache(filename, true); // Cached thumbnails are assumed to be in the correct format and adhere to the size/format restrictions of the ipad. if (cachestream != null) { return(response.FromStream(cachestream, MimeTypes.GetMimeType(".jpg"))); } } else { // Check if a processed (rescaled and/or progressive) image is cached. string processed_filename = string.Format("{0}-p{1}-processed.jpg", id, page); MemoryStream cachestream = ImageCache.Instance.LoadFromCache(processed_filename, false); if (cachestream != null) { return(response.FromStream(cachestream, MimeTypes.GetMimeType(".jpg"))); } } MemoryStream stream = null; // Check if original image is in the cache. string org_filename = string.Format("{0}-p{1}.jpg", id, page); stream = ImageCache.Instance.LoadFromCache(org_filename, false); if (stream == null) { // Image is not in the cache, get it via ComicRack. var bytes = GetPageImageBytes(id, page); if (bytes == null) { return(HttpStatusCode.NotFound); } stream = new MemoryStream(bytes); // Always save the original page to the cache ImageCache.Instance.SaveToCache(org_filename, stream, false); } stream.Seek(0, SeekOrigin.Begin); #if USE_GDI Bitmap bitmap = new Bitmap(stream, false); int bitmap_width = (int)bitmap.Width; int bitmap_height = (int)bitmap.Height; #elif USE_DIB FIBITMAP dib = FreeImage.LoadFromStream(stream); if (dib == null) { Console.WriteLine("Loading bitmap failed. Aborting."); // Check whether there was an error message. return(HttpStatusCode.InternalServerError); } int bitmap_width = (int)FreeImage.GetWidth(dib); int bitmap_height = (int)FreeImage.GetHeight(dib); #elif USE_FIB FreeImageBitmap fib = FreeImageBitmap.FromStream(stream, false); if (fib == null) { Console.WriteLine("Loading bitmap failed. Aborting."); // Check whether there was an error message. return(HttpStatusCode.InternalServerError); } int bitmap_width = (int)fib.Width; int bitmap_height = (int)fib.Height; #endif if (ImageCache.Instance.use_max_dimension) { int mw, mh; if (bitmap_width >= bitmap_height) { mw = ImageCache.Instance.max_dimension_long; mh = ImageCache.Instance.max_dimension_short; } else { mw = ImageCache.Instance.max_dimension_short; mh = ImageCache.Instance.max_dimension_long; } if (bitmap_width > mw || bitmap_height > mh) { double scaleW = (double)mw / (double)bitmap_width; double scaleH = (double)mh / (double)bitmap_height; double scale = Math.Min(scaleW, scaleH); max_width = (int)Math.Floor(scale * bitmap_width); max_height = (int)Math.Floor(scale * bitmap_height); } else { max_width = bitmap_width; max_height = bitmap_height; } } else // Check if the image dimensions exceeds the maximum image dimensions if ((bitmap_width * bitmap_height) > ImageCache.Instance.maximum_imagesize) { max_width = (int)Math.Floor(Math.Sqrt((double)bitmap_width / (double)bitmap_height * (double)ImageCache.Instance.maximum_imagesize)); max_height = (int)Math.Floor((double)max_width * (double)bitmap_height / (double)bitmap_width); } else { max_width = bitmap_width; max_height = bitmap_height; } // Calculate the dimensions of the returned image. int result_width = width; int result_height = height; if (result_width == -1 && result_height == -1) { result_width = max_width; result_height = max_height; } else { if (result_width == -1) { result_height = Math.Min(max_height, result_height); double ratio = (double)result_height / (double)max_height; result_width = (int)Math.Floor(((double)max_width * ratio)); } else if (result_height == -1) { result_width = Math.Min(max_width, result_width); double ratio = (double)result_width / (double)max_width; result_height = (int)Math.Floor(((double)max_height * ratio)); } } // TODO: do this per requesting target device instead of using one global setting. // Resize ? if (result_width != bitmap_width || result_height != bitmap_height) { processed = true; #if USE_DIB || USE_FIB //FREE_IMAGE_FILTER resizer = FREE_IMAGE_FILTER.FILTER_BICUBIC; FREE_IMAGE_FILTER resizer = FREE_IMAGE_FILTER.FILTER_LANCZOS3; #if USE_FIB fib.Rescale(result_width, result_height, resizer); #else FIBITMAP newdib = FreeImage.Rescale(dib, result_width, result_height, resizer); if (!newdib.IsNull) { FreeImage.Unload(dib); dib.SetNull(); dib = newdib; } #endif #elif USE_GDI Bitmap resizedBitmap = Resize(bitmap, result_width, result_height); bitmap.Dispose(); bitmap = resizedBitmap; resizedBitmap = null; #endif } // Check if the image must be converted to progressive jpeg if (ImageCache.Instance.use_progressive_jpeg && (result_width * result_height) >= ImageCache.Instance.progressive_jpeg_size_threshold) { processed = true; // Convert image to progressive jpeg // FreeImage source code reveals that lower 7 bits of the FREE_IMAGE_SAVE_FLAGS enum are used for low-level quality control. FREE_IMAGE_SAVE_FLAGS quality = (FREE_IMAGE_SAVE_FLAGS)ImageCache.Instance.progressive_jpeg_quality; FREE_IMAGE_SAVE_FLAGS flags = FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_444 | FREE_IMAGE_SAVE_FLAGS.JPEG_PROGRESSIVE | quality; #if USE_DIB || USE_FIB stream.Dispose(); stream = new MemoryStream(); #if USE_FIB fib.Save(stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags); fib.Dispose(); #else FreeImage.SaveToStream(dib, stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags); FreeImage.Unload(dib); dib.SetNull(); #endif #else FIBITMAP dib = FreeImage.CreateFromBitmap(bitmap); bitmap.Dispose(); bitmap = null; stream.Dispose(); stream = new MemoryStream(); FreeImage.SaveToStream(dib, stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags); FreeImage.Unload(dib); dib.SetNull(); #endif } else if (processed) { // image was rescaled, make new stream with rescaled bitmap #if USE_DIB || USE_FIB FREE_IMAGE_SAVE_FLAGS flags = FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_444 | FREE_IMAGE_SAVE_FLAGS.JPEG_OPTIMIZE | FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL; stream.Dispose(); stream = new MemoryStream(); #if USE_FIB fib.Save(stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags); fib.Dispose(); #else FreeImage.SaveToStream(dib, stream, FREE_IMAGE_FORMAT.FIF_JPEG, flags); FreeImage.Unload(dib); dib.SetNull(); #endif #else stream = GetBytesFromImage(bitmap); #endif // For now, images that were resized because they exceeded the maximum dimensions are not saved to the cache. } #if USE_DIB FreeImage.Unload(dib); dib.SetNull(); #elif USE_FIB fib.Dispose(); #elif USE_GDI if (bitmap != null) { bitmap.Dispose(); bitmap = null; } #endif // Always save thumbnails to the cache if (thumbnail) { ImageCache.Instance.SaveToCache(filename, stream, true); } else if (processed) { // Store rescaled and/or progressive jpegs in the cache for now. string processed_filename = string.Format("{0}-p{1}-processed.jpg", id, page); ImageCache.Instance.SaveToCache(processed_filename, stream, false); } stream.Seek(0, SeekOrigin.Begin); return(response.FromStream(stream, MimeTypes.GetMimeType(".jpg"))); } }
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(); }