Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
StandardImageHelper.Windows.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
4 using System;
5 using System.Drawing;
6 using System.Drawing.Imaging;
7 using System.IO;
8 using System.Runtime.InteropServices;
9 using SiliconStudio.Core;
10 
11 namespace SiliconStudio.Paradox.Graphics
12 {
13  /// <summary>
14  /// This class is responsible to provide image loader for png, gif, bmp.
15  /// TODO: Replace using System.Drawing, as it is not available on all platforms (not on Windows 8/WP8).
16  /// </summary>
17  partial class StandardImageHelper
18  {
19  public unsafe static Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle? handle)
20  {
21  using (var memoryStream = new UnmanagedMemoryStream((byte*)pSource, size))
22  using (var bitmap = (Bitmap)System.Drawing.Image.FromStream(memoryStream))
23  {
24  var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
25  // Lock System.Drawing.Bitmap
26 
27  var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
28  var image = Image.New2D(bitmap.Width, bitmap.Height, 1, PixelFormat.B8G8R8A8_UNorm, 1, bitmapData.Stride);
29  // var dataRect = new DataRectangle(bitmapData.Stride, bitmapData.Scan0);
30 
31  try
32  {
33 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES && SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
34  // Directly load image as RGBA instead of BGRA, because OpenGL ES devices don't support it out of the box (extension).
35  image.Description.Format = PixelFormat.R8G8B8A8_UNorm;
36  CopyMemoryBGRA(image.PixelBuffer[0].DataPointer, bitmapData.Scan0, image.PixelBuffer[0].BufferStride);
37 #else
38  Utilities.CopyMemory(image.PixelBuffer[0].DataPointer, bitmapData.Scan0, image.PixelBuffer[0].BufferStride);
39 #endif
40  }
41  finally
42  {
43  bitmap.UnlockBits(bitmapData);
44 
45  if (handle != null)
46  handle.Value.Free();
47  else if (!makeACopy)
48  Utilities.FreeMemory(pSource);
49  }
50 
51  return image;
52  }
53 
54  }
55 
56  public static void SaveGifFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
57  {
58  SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Gif);
59  }
60 
61  public static void SaveTiffFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
62  {
63  SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Tiff);
64  }
65 
66  public static void SaveBmpFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
67  {
68  SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Bmp);
69  }
70 
71  public static void SaveJpgFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
72  {
73  SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Jpeg);
74  }
75 
76  public static void SavePngFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
77  {
78  SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Png);
79  }
80 
81  public static void SaveWmpFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
82  {
83  throw new NotImplementedException();
84  }
85 
86  private static void SaveFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream, ImageFormat imageFormat)
87  {
88  using (var bitmap = new Bitmap(description.Width, description.Height))
89  {
90  var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
91 
92  // Lock System.Drawing.Bitmap
93  var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
94 
95  try
96  {
97  // Copy memory
98  if (description.Format == PixelFormat.R8G8B8A8_UNorm)
99  CopyMemoryBGRA(bitmapData.Scan0, pixelBuffers[0].DataPointer, pixelBuffers[0].BufferStride);
100  else if (description.Format == PixelFormat.B8G8R8A8_UNorm)
101  Utilities.CopyMemory(bitmapData.Scan0, pixelBuffers[0].DataPointer, pixelBuffers[0].BufferStride);
102  else
103  throw new NotSupportedException();
104  }
105  finally
106  {
107  bitmap.UnlockBits(bitmapData);
108  }
109 
110  // Save
111  bitmap.Save(imageStream, imageFormat);
112  }
113  }
114  }
115 }
116 #endif
_In_ size_t count
Definition: DirectXTexP.h:174
System.Windows.Shapes.Rectangle Rectangle
Definition: ColorPicker.cs:16
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32