private void m_import_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                dlg.Filter = "Mask (*.png; *.dds; *.bmp; *.tga; *.tif)|*.png;*.dds;*.bmp;*.tga;*.tif";
                dlg.CheckFileExists = true;
                dlg.InitialDirectory = ResourceRoot;
                if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    string filename = dlg.FileName;
                    string error = string.Empty;
                    m_importedMask = new ImageData();
                    m_importedMask.LoadFromFile(new Uri(filename));
                    m_importedMask.Convert(ImageDataFORMAT.R8_UNORM);
                    if (m_importedMask.IsValid)
                    {
                        m_widthTxt.Text = m_importedMask.Width.ToString();
                        m_heightTxt.Text = m_importedMask.Height.ToString();                        
                    }
                    else
                    {
                        m_importedMask.Dispose();
                        m_importedMask = null;
                        error = "unsupported file type ";
                    }

                    if (!string.IsNullOrEmpty(error))
                    {
                        MessageBox.Show(this, error, "Error importing heightmap", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }                    
                }
            }
        }
        Image IThumbnailResolver.Resolve(Uri resourceUri)
        {
            Image img = null;
            string path = resourceUri.LocalPath;
            if (path == null || !File.Exists(path))
                return null;

            string extension = Path.GetExtension(path).ToLower();
            var res = m_gameEngine.Info.ResourceInfos.GetByType(ResourceTypes.Texture);

            if(res.IsSupported(extension))
            {
                ImageData imgdata = new ImageData();
                imgdata.LoadFromFile(resourceUri);                
                imgdata.Convert(ImageDataFORMAT.B8G8R8A8_UNORM);                
                if (imgdata.IsValid)
                {
                    Bitmap tempImage = new Bitmap((int)imgdata.Width, (int)imgdata.Height, PixelFormat.Format32bppArgb);
                    BitmapData destData = null;

                    try
                    {
                        destData = tempImage.LockBits(
                            new Rectangle(0, 0, tempImage.Width, tempImage.Height),
                            ImageLockMode.WriteOnly,
                            tempImage.PixelFormat);

                        unsafe
                        {
                            byte* srcScan0 = (byte*)imgdata.Data;
                            int srcStride = imgdata.RowPitch;

                            byte* destScan0 = (byte*)destData.Scan0;
                            int destStride = destData.Stride;

                            for (int y = 0; y < tempImage.Height; y++)
                            {
                                uint* srcRow = (uint*)(srcScan0 + y * srcStride);
                                uint* destRow = (uint*)(destScan0 + y * destStride);
                                for (int x = 0; x < tempImage.Width; x++)
                                {
                                    *destRow++ = *srcRow++;
                                    
                                }
                            }

                        }
                    }
                    finally
                    {
                        tempImage.UnlockBits(destData);
                    }

                    int width, height;
                    float aspect = (float)tempImage.Width / (float)tempImage.Height;
                    if (aspect > 1.0f)
                    {
                        width = (int)ThumbnailSize;
                        height = (int)Math.Round(ThumbnailSize / aspect);
                    }
                    else
                    {
                        height = (int)ThumbnailSize;
                        width = (int)Math.Round(ThumbnailSize * aspect);
                    }
                    img = GdiUtil.ResizeImage(tempImage, width, height);
                    tempImage.Dispose();
                    imgdata.Dispose();

                }

            }           
            return img;
        }
Example #3
0
        Image IThumbnailResolver.Resolve(Uri resourceUri)
        {
            Image  img  = null;
            string path = resourceUri.LocalPath;

            if (path == null || !File.Exists(path))
            {
                return(null);
            }

            string extension = Path.GetExtension(path).ToLower();
            var    res       = m_gameEngine.Info.ResourceInfos.GetByType(ResourceTypes.Texture);

            if (res.IsSupported(extension))
            {
                ImageData imgdata = new ImageData();
                imgdata.LoadFromFile(resourceUri);
                imgdata.Convert(ImageDataFORMAT.B8G8R8A8_UNORM);
                if (imgdata.IsValid)
                {
                    Bitmap     tempImage = new Bitmap((int)imgdata.Width, (int)imgdata.Height, PixelFormat.Format32bppArgb);
                    BitmapData destData  = null;

                    try
                    {
                        destData = tempImage.LockBits(
                            new Rectangle(0, 0, tempImage.Width, tempImage.Height),
                            ImageLockMode.WriteOnly,
                            tempImage.PixelFormat);

                        unsafe
                        {
                            byte *srcScan0  = (byte *)imgdata.Data;
                            int   srcStride = imgdata.RowPitch;

                            byte *destScan0  = (byte *)destData.Scan0;
                            int   destStride = destData.Stride;

                            for (int y = 0; y < tempImage.Height; y++)
                            {
                                uint *srcRow  = (uint *)(srcScan0 + y * srcStride);
                                uint *destRow = (uint *)(destScan0 + y * destStride);
                                for (int x = 0; x < tempImage.Width; x++)
                                {
                                    *destRow++ = *srcRow++;
                                }
                            }
                        }
                    }
                    finally
                    {
                        tempImage.UnlockBits(destData);
                    }

                    int   width, height;
                    float aspect = (float)tempImage.Width / (float)tempImage.Height;
                    if (aspect > 1.0f)
                    {
                        width  = (int)ThumbnailSize;
                        height = (int)Math.Round(ThumbnailSize / aspect);
                    }
                    else
                    {
                        height = (int)ThumbnailSize;
                        width  = (int)Math.Round(ThumbnailSize * aspect);
                    }
                    img = GdiUtil.ResizeImage(tempImage, width, height);
                    tempImage.Dispose();
                    imgdata.Dispose();
                }
            }
            return(img);
        }
Example #4
0
        private void m_import_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                dlg.Filter = "HeightMap (*.png; *.dds; *.bmp; *.tga; *.tif)|*.png;*.dds;*.bmp;*.tga;*.tif";
                dlg.CheckFileExists = true;
                dlg.InitialDirectory = ResourceRoot;
                if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    string filename = dlg.FileName;
                    string error = string.Empty;

                    if(m_importedHeightMap != null) 
                        m_importedHeightMap.Dispose();

                    m_importedHeightMap = new ImageData();
                    m_importedHeightMap.LoadFromFile(new Uri(filename));
                    m_importedHeightMap.Convert(ImageDataFORMAT.R32_FLOAT);
                    if (m_importedHeightMap.IsValid)
                    {
                       // set rows and cols.
                        foreach(int c in m_hmapColsCmbox.Items)
                        {
                            if (c >= m_importedHeightMap.Width)
                            {
                                m_hmapColsCmbox.SelectedItem = c;
                                break;
                            }
                        }

                        foreach(int r in m_hmapRowsCmbox.Items)
                        {
                            if (r >= m_importedHeightMap.Height)
                            {
                                m_hmapRowsCmbox.SelectedItem = r;
                                break;
                            }
                        }                        
                    }
                    else
                    {
                        m_importedHeightMap.Dispose();
                        m_importedHeightMap = null;
                        error = "unsupported file type ";
                    }
                        
                    if (!string.IsNullOrEmpty(error))
                    {
                        MessageBox.Show(this, error, "Error importing heightmap", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }                    
                }
            }
        }