Encoder for generating a image out of a png stream.
At the moment the following features are supported:

Filters: all filters are supported.

Pixel formats: RGB (Truecolor) with alpha (8 bit). RGB (Truecolor) without alpha (8 bit). Greyscale with alpha (8 bit). Greyscale without alpha (8 bit). Palette Index with alpha (8 bit). Palette Index without alpha (8 bit).

Inheritance: IImageDecoder
Ejemplo n.º 1
1
		public override void LoadFileDialog(FolderLocations folderLocation, int maxWidth, int maxHeight, int x, int y, int width, int height, string[] fileTypes, StreamLoadedCallbackMethod streamLoadedCallback)
		{
			if (streamLoadedCallback == null) return;
			string filename = EditorUtility.OpenFilePanel("Load file", "", generateFilterValue(fileTypes));
			if (!string.IsNullOrEmpty(filename))
			{
				if (maxWidth == 0 || maxHeight == 0 || folderLocation != FolderLocations.Pictures)
				{
					streamLoadedCallback(new FileStream(filename, FileMode.Open, FileAccess.Read), true);
				}
				else
				{
					var newStream = new MemoryStream();
					try
					{
						using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
						{
							IImageDecoder decoder = null;
							switch (Path.GetExtension(filename).ToLower())
							{
								case ".jpg": decoder = new JpegDecoder(); break;
								case ".jpeg": decoder = new JpegDecoder(); break;
								case ".png": decoder = new PngDecoder(); break;
								default:
									Debug.LogError("Unsuported file ext type: " + Path.GetExtension(filename));
									streamLoadedCallback(null, false);
									return;
							}
							var image = new ExtendedImage();
							decoder.Decode(image, stream);
							var newSize = MathUtilities.FitInViewIfLarger(image.PixelWidth, image.PixelHeight, maxWidth, maxHeight);
							var newImage = ExtendedImage.Resize(image, (int)newSize.x, (int)newSize.y, new NearestNeighborResizer());

							var encoder = new PngEncoder();
							encoder.Encode(newImage, newStream);
							newStream.Position = 0;
						}
					}
					catch (Exception e)
					{
						newStream.Dispose();
						newStream = null;
						Debug.LogError(e.Message);
					}
					finally
					{
						streamLoadedCallback(newStream, true);
					}
				}
			}
			else
			{
				streamLoadedCallback(null, false);
			}
		}
Ejemplo n.º 2
0
 private void btnOpen_Click(object sender, RoutedEventArgs e)
 {
     OpenFileDialog ofd = new OpenFileDialog();
     ofd.Filter = "JPEG,PNG,BMPファイル (*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";
     ofd.Multiselect = false;
     if (ofd.ShowDialog() != true) return;
     string fn = ofd.File.Name.ToLower();
     IImageDecoder dec;
     if (fn.EndsWith(".jpg"))
     {
         dec = new JpegDecoder();
     }
     else if (fn.EndsWith(".png"))
     {
         dec = new PngDecoder();
     }
     else if (fn.EndsWith(".bmp"))
     {
         dec = new BmpDecoder();
     }
     else
     {
         MessageBox.Show("エラー");
         return;
     }
     try
     {
         FileStream fs = ofd.File.OpenRead();
         ImageTools.Image img = new ImageTools.Image();
         dec.Decode(img, fs);
         if (img.Width < 50 || img.Height < 50)
         {
             MessageBox.Show("画像サイズが小さすぎます");
             return;
         }
         WriteableBitmap wb = new WriteableBitmap(img.Width, img.Height);
         byte[] data = img.GetPixels();
         for (int y = 0; y < img.Height; y++)
         {
             for (int x = 0; x < img.Width; x++)
             {
                 int idx = (x + y * img.Width) * 4;
                 wb.SetPixel(x, y, Color.FromArgb(data[idx + 3], data[idx], data[idx + 1], data[idx + 2]));
             }
         }
         SetImage(wb);
         fs.Close();
     }
     catch
     {
         MessageBox.Show("エラー");
     }
 }
Ejemplo n.º 3
0
        public override void LoadFileDialog(FolderLocations folderLocation, int maxWidth, int maxHeight, int x, int y, int width, int height, string[] fileTypes, StreamLoadedCallbackMethod streamLoadedCallback)
        {
            if (streamLoadedCallback == null)
            {
                return;
            }

            // open native dlg
            var file = new OPENFILENAME();

            file.lStructSize = (uint)Marshal.SizeOf(typeof(OPENFILENAME));
            file.hwndOwner   = IntPtr.Zero;
            file.lpstrDefExt = IntPtr.Zero;
            file.lpstrFile   = Marshal.AllocHGlobal((int)MAX_PATH);
            unsafe { ((byte *)file.lpstrFile.ToPointer())[0] = 0; }
            file.nMaxFile        = MAX_PATH;
            file.lpstrFilter     = generateFilterValue(fileTypes);
            file.nFilterIndex    = 0;
            file.lpstrInitialDir = Marshal.StringToHGlobalUni(Application.dataPath);
            file.lpstrTitle      = Marshal.StringToHGlobalUni("Load file");
            file.Flags           = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
            GetOpenFileName(ref file);

            // get native dlg result
            string filename = null;

            if (file.lpstrFile != IntPtr.Zero)
            {
                filename = Marshal.PtrToStringUni(file.lpstrFile);
                Debug.Log("Loading file: " + filename);
            }

            Marshal.FreeHGlobal(file.lpstrFile);
            Marshal.FreeHGlobal(file.lpstrInitialDir);
            Marshal.FreeHGlobal(file.lpstrTitle);
            Marshal.FreeHGlobal(file.lpstrFilter);

            // open file
            if (!string.IsNullOrEmpty(filename))
            {
                if (maxWidth == 0 || maxHeight == 0 || folderLocation != FolderLocations.Pictures)
                {
                    streamLoadedCallback(new FileStream(filename, FileMode.Open, FileAccess.Read), true);
                }
                else
                {
                    var newStream = new MemoryStream();
                    try
                    {
                        using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
                        {
                            ImageTools.IO.IImageDecoder decoder = null;
                            switch (Path.GetExtension(filename).ToLower())
                            {
                            case ".jpg": decoder = new ImageTools.IO.Jpeg.JpegDecoder(); break;

                            case ".jpeg": decoder = new ImageTools.IO.Jpeg.JpegDecoder(); break;

                            case ".png": decoder = new ImageTools.IO.Png.PngDecoder(); break;

                            default:
                                Debug.LogError("Unsuported file ext type: " + Path.GetExtension(filename));
                                streamLoadedCallback(null, false);
                                return;
                            }
                            var image = new ExtendedImage();
                            decoder.Decode(image, stream);
                            var newSize  = Reign.MathUtilities.FitInViewIfLarger(image.PixelWidth, image.PixelHeight, maxWidth, maxHeight);
                            var newImage = ExtendedImage.Resize(image, (int)newSize.x, (int)newSize.y, new NearestNeighborResizer());

                            var encoder = new PngEncoder();
                            encoder.Encode(newImage, newStream);
                            newStream.Position = 0;
                        }
                    }
                    catch (Exception e)
                    {
                        newStream.Dispose();
                        newStream = null;
                        Debug.LogError(e.Message);
                    }
                    finally
                    {
                        streamLoadedCallback(newStream, true);
                    }
                }
            }
            else
            {
                streamLoadedCallback(null, false);
            }
        }
Ejemplo n.º 4
0
		public override void LoadFileDialog(FolderLocations folderLocation, int maxWidth, int maxHeight, int x, int y, int width, int height, string[] fileTypes, StreamLoadedCallbackMethod streamLoadedCallback)
		{
			if (streamLoadedCallback == null) return;

			// open native dlg
			var file = new OPENFILENAME();
			file.lStructSize = (uint)Marshal.SizeOf(typeof(OPENFILENAME));
			file.hwndOwner = IntPtr.Zero;
			file.lpstrDefExt = IntPtr.Zero;
			file.lpstrFile = Marshal.AllocHGlobal((int)MAX_PATH);
			unsafe {((byte*)file.lpstrFile.ToPointer())[0] = 0;}
			file.nMaxFile = MAX_PATH;
			file.lpstrFilter = generateFilterValue(fileTypes);
			file.nFilterIndex = 0;
			file.lpstrInitialDir = Marshal.StringToHGlobalUni(Application.dataPath);
			file.lpstrTitle = Marshal.StringToHGlobalUni("Load file");
			file.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
			GetOpenFileName(ref file);

			// get native dlg result
			string filename = null;
			if (file.lpstrFile != IntPtr.Zero)
			{
				filename = Marshal.PtrToStringUni(file.lpstrFile);
				Debug.Log("Loading file: " + filename);
			}

			Marshal.FreeHGlobal(file.lpstrFile);
			Marshal.FreeHGlobal(file.lpstrInitialDir);
			Marshal.FreeHGlobal(file.lpstrTitle);
			Marshal.FreeHGlobal(file.lpstrFilter);

			// open file
			if (!string.IsNullOrEmpty(filename))
			{
				if (maxWidth == 0 || maxHeight == 0 || folderLocation != FolderLocations.Pictures)
				{
					streamLoadedCallback(new FileStream(filename, FileMode.Open, FileAccess.Read), true);
				}
				else
				{
					var newStream = new MemoryStream();
					try
					{
						using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
						{
							ImageTools.IO.IImageDecoder decoder = null;
							switch (Path.GetExtension(filename).ToLower())
							{
								case ".jpg": decoder = new ImageTools.IO.Jpeg.JpegDecoder(); break;
								case ".jpeg": decoder = new ImageTools.IO.Jpeg.JpegDecoder(); break;
								case ".png": decoder = new ImageTools.IO.Png.PngDecoder(); break;
								default:
									Debug.LogError("Unsuported file ext type: " + Path.GetExtension(filename));
									streamLoadedCallback(null, false);
									return;
							}
							var image = new ExtendedImage();
							decoder.Decode(image, stream);
							var newSize = Reign.MathUtilities.FitInViewIfLarger(image.PixelWidth, image.PixelHeight, maxWidth, maxHeight);
							var newImage = ExtendedImage.Resize(image, (int)newSize.x, (int)newSize.y, new NearestNeighborResizer());

							var encoder = new PngEncoder();
							encoder.Encode(newImage, newStream);
							newStream.Position = 0;
						}
					}
					catch (Exception e)
					{
						newStream.Dispose();
						newStream = null;
						Debug.LogError(e.Message);
					}
					finally
					{
						streamLoadedCallback(newStream, true);
					}
				}
			}
			else
			{
				streamLoadedCallback(null, false);
			}
		}