Exemple #1
0
        public static void SaveImage(Stream stream, FreeImageBitmap bitmap)
        {
            AjpHeader ajpHeaderFromBitmap  = bitmap.Tag as AjpHeader;
            AjpHeader ajpHeaderFromComment = null;

            if (!string.IsNullOrEmpty(bitmap.Comment))
            {
                ajpHeaderFromComment = new AjpHeader();
                if (!ajpHeaderFromComment.ParseComment(bitmap.Comment))
                {
                    ajpHeaderFromComment = null;
                }
            }

            var ms  = new MemoryStream();
            var ms2 = new MemoryStream();

            bitmap.Save(ms, FREE_IMAGE_FORMAT.FIF_JPEG, FREE_IMAGE_SAVE_FLAGS.JPEG_PROGRESSIVE | FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYGOOD);
            using (var alpha = bitmap.GetChannel(FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA))
            {
                if (alpha != null)
                {
                    alpha.Comment = "signature = 19792, version = 2, headerSize = 64, colorDepth = 8";
                    Pms.SaveImage(ms2, alpha);
                }
            }

            AjpHeader ajpHeader = ajpHeaderFromBitmap;

            if (ajpHeader == null)
            {
                ajpHeader = ajpHeaderFromComment;
            }
            SaveImage(stream, ms.ToArray(), ms2.ToArray(), ajpHeader);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        public static FreeImageBitmap LoadImage(byte[] bytes)
        {
            var vspHeader = GetHeader(bytes);

            if (vspHeader.width > 0 && ((vspHeader.is8Bit == 0) ? vspHeader.width : ((vspHeader.width + 7) / 8)) <= 80 && vspHeader.height > 0 && vspHeader.height <= 480)
            {
            }
            else
            {
                return(null);
            }
            if (vspHeader.is8Bit == 1)
            {
                var             pmsHeader = vspHeader.ToPmsHeader();
                var             imageData = Pms.GetImageData8Bit(pmsHeader, bytes);
                FreeImageBitmap bitmap    = new FreeImageBitmap(pmsHeader.width, pmsHeader.height, pmsHeader.width, 8, FREE_IMAGE_TYPE.FIT_BITMAP, imageData);
                Pms.GetPalette(bitmap.Palette, pmsHeader, bytes);
                bitmap.Comment = vspHeader.GetComment();
                bitmap.Tag     = vspHeader;
                return(bitmap);
            }
            else
            {
                var             imageData = GetImage(vspHeader, bytes);
                FreeImageBitmap bitmap    = new FreeImageBitmap(vspHeader.width * 8, vspHeader.height, vspHeader.width * 8, 8, FREE_IMAGE_TYPE.FIT_BITMAP, imageData);
                GetPalette(bitmap.Palette, bytes, vspHeader);
                bitmap.Comment = vspHeader.GetComment();
                bitmap.Tag     = vspHeader;
                return(bitmap);
            }
        }
Exemple #4
0
        public static void SaveImage(Stream stream, FreeImageBitmap bitmap)
        {
            string comment   = bitmap.Comment;
            var    vspHeader = new VspHeader();

            if (!String.IsNullOrEmpty(comment))
            {
                vspHeader.ParseComment(comment);
            }
            if (bitmap.ColorDepth != 4 && bitmap.ColorDepth != 8)
            {
                if (bitmap.ColorDepth > 8)
                {
                    if (bitmap.ColorDepth == 32)
                    {
                        bitmap.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                    }
                    if (vspHeader.is8Bit == 0)
                    {
                        bitmap.Quantize(FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, 16);
                    }
                    else
                    {
                        bitmap.Quantize(FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, 256);
                    }
                }
                //throw new ArgumentException("image must be 4-bit or 8-bit");
            }
            if ((bitmap.Width & 7) != 0)
            {
                int slackPixels = (bitmap.Width & 7);
                int pixelsToAdd = 8 - slackPixels;
                bitmap.EnlargeCanvas <RGBQUAD>(0, 0, pixelsToAdd, 0, new RGBQUAD(Color.Black));
            }

            if (vspHeader.is8Bit == 0)
            {
                vspHeader.height = bitmap.Height;
                vspHeader.width  = bitmap.Width / 8;
                SaveHeader(vspHeader, stream);
                SavePalette(bitmap.Palette, stream);
                SaveImageData(stream, bitmap);
            }
            else
            {
                vspHeader.height = bitmap.Height;
                if (vspHeader.width != bitmap.Width - 1)
                {
                }
                vspHeader.width = bitmap.Width - 1;
                SaveHeader(vspHeader, stream);
                var pmsHeader = vspHeader.ToPmsHeader();
                Pms.SavePalette(stream, bitmap.Palette);
                Pms.SaveImageData8Bit(stream, bitmap);
            }
        }
Exemple #5
0
        public static void SaveImage(Stream stream, byte[] jpegFile, FreeImageBitmap alpha, AjpHeader ajpHeader)
        {
            byte[] pmsFile = null;
            if (alpha != null)
            {
                using (var ms = new MemoryStream()) {
                    alpha.Comment = "signature = 19792, version = 2, headerSize = 64";
                    Pms.SaveImage(ms, alpha);
                    pmsFile = ms.ToArray();
                }
            }

            SaveImage(stream, jpegFile, pmsFile, ajpHeader);
        }
Exemple #6
0
        public static void LoadImage(Stream ajpStream, out byte[] jpegBytes, out FreeImageBitmap alpha, out AjpHeader ajpHeader)
        {
            MemoryStream jpegFile;
            MemoryStream pmsFile;

            LoadImage(ajpStream, out jpegFile, out pmsFile, out ajpHeader);
            jpegBytes = jpegFile.ToArray();
            jpegFile.Dispose();

            alpha = null;
            if (pmsFile != null)
            {
                FreeImageBitmap pmsImage = Pms.LoadImage(pmsFile.ToArray());
                pmsFile.Dispose();
                alpha = pmsImage;
            }
        }