public static FreeImageBitmap LoadImage(byte[] bytes) { var qntHeader = GetQntHeader(bytes); if (qntHeader == null || !qntHeader.Validate()) { return(null); } var pixels = GetQntPixels(bytes, qntHeader); byte[] alphaPixels = null; if (qntHeader.alphaSize != 0) { alphaPixels = GetQntAlpha(bytes, qntHeader); } FreeImageBitmap image = new FreeImageBitmap(qntHeader.width, qntHeader.height, qntHeader.width * 3, 24, FREE_IMAGE_TYPE.FIT_BITMAP, pixels); using (var blue = image.GetChannel(FREE_IMAGE_COLOR_CHANNEL.FICC_RED)) using (var red = image.GetChannel(FREE_IMAGE_COLOR_CHANNEL.FICC_BLUE)) { if (alphaPixels != null) { image.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_32_BPP); } image.SetChannel(blue, FREE_IMAGE_COLOR_CHANNEL.FICC_BLUE); image.SetChannel(red, FREE_IMAGE_COLOR_CHANNEL.FICC_RED); } FreeImageBitmap alpha = null; try { if (alphaPixels != null) { alpha = new FreeImageBitmap(qntHeader.width, qntHeader.height, qntHeader.width, 8, FREE_IMAGE_TYPE.FIT_BITMAP, alphaPixels); } if (alpha != null) { image.SetChannel(alpha, FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA); } } finally { if (alpha != null) { alpha.Dispose(); } } image.Comment = qntHeader.GetComment(); image.Tag = qntHeader; return(image); }
public static FreeImageBitmap LoadImage(Stream ajpStream) { MemoryStream jpegFile; MemoryStream pmsFile; AjpHeader ajpHeader; LoadImage(ajpStream, out jpegFile, out pmsFile, out ajpHeader); if (jpegFile == null) { return(null); } jpegFile.Position = 0; FreeImageBitmap jpegImage = new FreeImageBitmap(jpegFile, FREE_IMAGE_FORMAT.FIF_JPEG); jpegImage.Tag = ajpHeader; jpegImage.Comment = ajpHeader.GetComment(); if (pmsFile != null && pmsFile.Length > 0) { pmsFile.Position = 0; using (var pmsImage = Pms.LoadImage(pmsFile.ToArray())) { if (pmsImage == null) { return(jpegImage); } jpegImage.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_32_BPP); jpegImage.SetChannel(pmsImage, FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA); } } return(jpegImage); }
/// <summary> /// Write to the given graphics, if the widget texture is not large enough it will be /// resized temporarily and then sized back. So be careful with large destrects. /// </summary> /// <param name="g"></param> /// <param name="destRect"></param> public void writeToGraphics(FreeImageBitmap g, Rectangle destRect) { bool changedSize = false; IntSize2 originalSize = new IntSize2(imageBox.Width, imageBox.Height); float cropRatio = (float)imageBox.Width / destRect.Width; Rectangle srcRect = new Rectangle(0, 0, imageBox.Width, (int)(destRect.Height * cropRatio)); //Make sure the source image is large enough, if not resize. if (originalSize.Width < srcRect.Width || originalSize.Height < srcRect.Height) { imageBox.setSize(srcRect.Width, srcRect.Height); changedSize = true; resized(); renderTexture.update(); } else if (renderOneFrame) { renderTexture.update(); } using (FreeImageBitmap fullBitmap = new FreeImageBitmap(currentTextureWidth, currentTextureHeight, BitmapFormat)) { fullBitmap.copyFromRenderTarget(renderTexture, ogreTextureFormat); //Remove alpha //BitmapDataExtensions.SetAlpha(bmpData, 255); if (srcRect.Height > fullBitmap.Height) { srcRect.Height = fullBitmap.Height; } using (FreeImageBitmap cropped = fullBitmap.Copy(srcRect)) { if (cropped != null) { cropped.Rescale(destRect.Width, destRect.Height, FREE_IMAGE_FILTER.FILTER_BILINEAR); if (ogreTextureFormat == OgrePlugin.PixelFormat.PF_X8R8G8B8) { //Use the api to set the alpha channel to 255, this makes sure we dont carry over the x8 channel from ogre using (var alpha = cropped.GetChannel(FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA)) { alpha.FillBackground(new RGBQUAD(new FreeImageAPI.Color() { R = 255, G = 255, B = 255, A = 255 })); cropped.SetChannel(alpha, FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA); } } g.Paste(cropped, destRect.X, destRect.Y, int.MaxValue); } } } if (changedSize) { imageBox.setSize(originalSize.Width, originalSize.Height); resized(); renderTexture.update(); } }