Decode() public method

Decodes the image from the specified stream and sets the data to image.
/// is null (Nothing in Visual Basic). /// - or - /// is null (Nothing in Visual Basic). ///
public Decode ( ExtendedImage image, Stream stream ) : void
image ExtendedImage The image, where the data should be set to. /// Cannot be null (Nothing in Visual Basic).
stream Stream The stream, where the image should be /// decoded from. Cannot be null (Nothing in Visual Basic).
return void
Exemplo 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);
			}
		}
Exemplo n.º 2
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);
            }
        }
Exemplo 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);
			}
		}