3 using System
.Drawing
.Imaging
;
4 using System
.Runtime
.Serialization
;
5 using Mainsoft
.Drawing
.Imaging
;
8 using imageio
= javax
.imageio
;
9 using stream
= javax
.imageio
.stream
;
10 using spi
= javax
.imageio
.spi
;
11 using BufferedImage
= java
.awt
.image
.BufferedImage
;
12 using JavaImage
= java
.awt
.Image
;
14 using image
= java
.awt
.image
;
16 namespace System
.Drawing
18 public sealed class Bitmap
: Image
{
20 # region Static fields
22 static readonly image
.ColorModel _jpegColorModel
= new image
.DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
28 Bitmap (PlainImage orig
) {
29 base.Initialize( orig
, false );
33 private Bitmap (SerializationInfo info
, StreamingContext context
) {
34 throw new NotImplementedException ();
37 public Bitmap (int width
, int height
, Graphics g
)
38 :this (width
, height
, PixelFormat
.Format32bppArgb
) {
39 CurrentImage
.HorizontalResolution
= g
.DpiX
;
40 CurrentImage
.VerticalResolution
= g
.DpiY
;
43 public Bitmap (Image original
)
44 :this (original
, original
.Size
) {}
46 public Bitmap (Image orig
, Size newSize
)
47 :this (orig
, newSize
.Width
, newSize
.Height
) {}
49 public Bitmap (Image orig
, int width
, int height
)
50 :base (CreateScaledImage (orig
, width
, height
), ImageFormat
.MemoryBmp
) {}
52 internal Bitmap (java
.awt
.Image nativeObject
, ImageFormat format
)
53 :base (nativeObject
, format
) {}
56 private Bitmap (java
.awt
.Image nativeObject
, ImageFormat format
, PixelFormat pixFormat
)
57 :this (nativeObject
, format
) {
58 if (pixFormat
!= this.PixelFormat
)
59 throw new NotImplementedException ("Converting PixelFormat is not implemented yet.");
62 public Bitmap (int width
, int height
)
63 :this (width
, height
, PixelFormat
.Format32bppArgb
) {}
65 public Bitmap (int width
, int height
, PixelFormat format
)
67 new java
.awt
.image
.BufferedImage (width
, height
,
68 ToBufferedImageFormat (format
)),
72 public Bitmap (Stream stream
)
73 :this (stream
, false) {}
75 public Bitmap (string filename
)
76 :this (filename
, false) {}
79 public Bitmap (Stream stream
, bool useIcm
)
80 :this (stream
, useIcm
, null) {}
83 public Bitmap (string filename
, bool useIcm
)
84 :this (filename
, useIcm
, null) {}
86 internal Bitmap (Stream stream
, bool useIcm
, ImageFormat format
) {
88 io
.InputStream jis
= vmw
.common
.IOUtils
.ToInputStream (stream
);
89 Initialize (new stream
.MemoryCacheImageInputStream (jis
), format
);
92 internal Bitmap (string filename
, bool useIcm
, ImageFormat format
) {
93 using(FileStream stream
= new FileStream(filename
, FileMode
.Open
, FileAccess
.Read
)) {
95 io
.InputStream jis
= vmw
.common
.IOUtils
.ToInputStream (stream
);
96 Initialize (new stream
.MemoryCacheImageInputStream (jis
), format
);
100 public Bitmap (Type type
, string resource
) {
101 using (Stream s
= type
.Assembly
.GetManifestResourceStream (resource
)) {
103 throw new ArgumentException("Resource '" + resource
+ "' could not be found in class '" + type
.ToString() + "'");
105 io
.InputStream jis
= vmw
.common
.IOUtils
.ToInputStream (s
);
106 Initialize (new stream
.MemoryCacheImageInputStream (jis
), null);
111 public Bitmap (int width
, int height
, int stride
, PixelFormat format
, IntPtr scan0
)
113 throw new NotImplementedException();
118 #region Internal Initialization
120 private void Initialize (stream
.ImageInputStream input
, ImageFormat format
) {
121 ImageCodec ic
= null;
124 ic
= ImageCodec
.CreateReader(input
);
126 ic
= ImageCodec
.CreateReader(format
);
129 throw new ArgumentException ("Parameter is not valid.");
132 ic
.NativeStream
= input
;
133 PlainImage pi
= ic
.ReadPlainImage();
134 base.Initialize( pi
, false );
136 pi
= ic
.ReadNextPlainImage();
137 while ( pi
!= null) {
138 base.Initialize( pi
, true );
139 pi
= ic
.ReadNextPlainImage();
142 _flags
|= (int)(ImageFlags
.ImageFlagsReadOnly
| ImageFlags
.ImageFlagsHasRealPixelSize
);
144 catch (IOException ex
) {
155 protected override void InternalSave (stream
.ImageOutputStream output
, Guid clsid
) {
157 ImageCodec ic
= ImageCodec
.CreateWriter( clsid
);
160 PlainImage plainImage
= CurrentImage
;
161 plainImage
.NativeImage
.flush();
163 if ( ImageCodec
.ClsidToImageFormat( clsid
).Equals( ImageFormat
.Jpeg
) ) {
164 image
.ColorModel cm
= ((image
.BufferedImage
)CurrentImage
.NativeImage
).getColorModel();
166 if (cm
is image
.DirectColorModel
) {
167 image
.Raster raster
= ((image
.BufferedImage
)CurrentImage
.NativeImage
).getRaster();
168 image
.DataBuffer db
= raster
.getDataBuffer();
169 image
.DirectColorModel dcm
= (image
.DirectColorModel
)cm
;
170 image
.SinglePixelPackedSampleModel jpegSampleModel
= new image
.SinglePixelPackedSampleModel(
171 db
.getDataType(), Width
, Height
,
172 new int[] {dcm.getRedMask(), dcm.getGreenMask(), dcm.getBlueMask()}
);
174 image
.BufferedImage tb
= new image
.BufferedImage(
176 image
.Raster
.createWritableRaster( jpegSampleModel
, db
, null ),
179 plainImage
= new PlainImage( tb
, plainImage
.Thumbnails
, ImageFormat
.Jpeg
, plainImage
.HorizontalResolution
, plainImage
.VerticalResolution
, plainImage
.Dimension
);
180 plainImage
.NativeMetadata
= plainImage
.NativeMetadata
;
185 ic
.NativeStream
= output
;
186 ic
.WritePlainImage( plainImage
);
192 #region private statics: ToBufferedImageFormat, CreateScaledImage
194 private static int ToBufferedImageFormat (PixelFormat format
) {
196 case PixelFormat
.Format16bppGrayScale
:
197 return BufferedImage
.TYPE_USHORT_GRAY
;
198 case PixelFormat
.Format1bppIndexed
:
199 return BufferedImage
.TYPE_BYTE_GRAY
;
200 case PixelFormat
.Format32bppArgb
:
201 return BufferedImage
.TYPE_INT_ARGB
;
202 case PixelFormat
.Format32bppRgb
:
203 return BufferedImage
.TYPE_INT_RGB
;
204 case PixelFormat
.Format32bppPArgb
:
205 return BufferedImage
.TYPE_INT_ARGB_PRE
;
206 case PixelFormat
.Format16bppRgb555
:
207 return BufferedImage
.TYPE_USHORT_555_RGB
;
208 case PixelFormat
.Format16bppRgb565
:
209 return BufferedImage
.TYPE_USHORT_565_RGB
;
210 case PixelFormat
.Indexed
:
211 return BufferedImage
.TYPE_BYTE_INDEXED
;
213 return BufferedImage
.TYPE_INT_ARGB
;
217 private static java
.awt
.Image
CreateScaledImage(Image original
, int width
, int height
) {
218 JavaImage oldscaled
= original
.CurrentImage
.NativeImage
.getScaledInstance(width
, height
,
219 JavaImage
.SCALE_DEFAULT
);
220 BufferedImage newimage
= new BufferedImage(oldscaled
.getWidth(null),
221 oldscaled
.getHeight(null),
222 BufferedImage
.TYPE_INT_ARGB
);
223 java
.awt
.Graphics2D graphics2d
= newimage
.createGraphics();
224 graphics2d
.drawImage(oldscaled
, 0, 0, null);
225 graphics2d
.dispose();
231 public Color
GetPixel (int x
, int y
)
234 int argb
= NativeObject
.getRGB(x
,y
);
235 return Color
.FromArgb(argb
);
238 public void SetPixel (int x
, int y
, Color color
)
240 int rgb
= color
.ToArgb();
241 NativeObject
.setRGB(x
,y
,rgb
);
246 public override object Clone () {
247 return new Bitmap ( (PlainImage
)CurrentImage
.Clone() );
250 public Bitmap
Clone (Rectangle rect
, PixelFormat pixFormat
)
252 return Clone(new RectangleF( rect
.X
, rect
.Y
, rect
.Width
, rect
.Height
), pixFormat
);
255 public Bitmap
Clone (RectangleF rect
, PixelFormat pixFormat
)
257 PlainImage plainImage
= CurrentImage
.Clone(false);
258 BufferedImage clone
= new BufferedImage( (int)rect
.Width
, (int)rect
.Height
, ToBufferedImageFormat( pixFormat
) );
259 awt
.Graphics2D g
= clone
.createGraphics();
261 g
.drawImage( NativeObject
, -(int)rect
.X
, -(int)rect
.Y
, null );
267 plainImage
.NativeImage
= clone
;
268 return new Bitmap(plainImage
);
274 public BitmapData
LockBits (Rectangle rect
, ImageLockMode flags
, PixelFormat format
) {
275 throw new NotImplementedException();
281 BitmapData
LockBits (Rectangle rect
, ImageLockMode flags
, PixelFormat format
, BitmapData bitmapData
) {
282 throw new NotImplementedException();
286 #region MakeTransparent
287 public void MakeTransparent ()
289 Color clr
= Color
.FromArgb(0,0,0);
290 MakeTransparent (clr
);
293 public void MakeTransparent (Color transparentColor
)
295 image
.WritableRaster raster
= NativeObject
.getRaster();
296 int numBands
= raster
.getNumBands();
300 int maxWidth
= raster
.getWidth() + raster
.getMinX();
301 int maxHeight
= raster
.getHeight() + raster
.getMinY();
302 int[] srcPix
= new int[numBands
];
304 for (int y
= raster
.getMinY(); y
< maxHeight
; y
++) {
305 for (int x
= raster
.getMinX(); x
< maxWidth
; x
++) {
306 /*srcPix =*/ raster
.getPixel(x
, y
, srcPix
);
307 if (srcPix
[0] == transparentColor
.R
&&
308 srcPix
[1] == transparentColor
.G
&&
309 srcPix
[2] == transparentColor
.B
) {
311 raster
.setPixel(x
, y
, srcPix
);
318 #region SetResolution
319 public void SetResolution (float xDpi
, float yDpi
)
321 CurrentImage
.HorizontalResolution
= xDpi
;
322 CurrentImage
.VerticalResolution
= yDpi
;
328 public void UnlockBits (BitmapData bitmap_data
)
330 throw new NotImplementedException();
335 internal new BufferedImage NativeObject
{
337 return (BufferedImage
)base.NativeObject
.CurrentImage
.NativeImage
;
341 protected override java
.awt
.Image
[] CloneNativeObjects(java
.awt
.Image
[] src
) {
345 awt
.Image
[] dst
= new awt
.Image
[src
.Length
];
346 for (int i
= 0; i
< dst
.Length
; i
++) {
347 BufferedImage image
= src
[i
] as BufferedImage
;
349 throw new ArgumentException(String
.Format("Unsupported image type '{0}'", src
[i
].ToString()), "src");
351 dst
[i
] = new BufferedImage(image
.getColorModel(), image
.copyData(null), image
.isAlphaPremultiplied(), null);
359 #region InternalPixelFormat
360 protected override PixelFormat InternalPixelFormat
{
362 int t
= NativeObject
.getType();
364 case 11://JavaImage.TYPE_USHORT_GRAY:
365 return PixelFormat
.Format16bppGrayScale
;
366 case 10://JavaImage.TYPE_BYTE_GRAY:
367 return PixelFormat
.Format8bppIndexed
;
368 case 1: //JavaImage.TYPE_INT_RGB
369 return PixelFormat
.Format32bppRgb
;
370 case 2: //JavaImage.TYPE_INT_ARGB:
371 return PixelFormat
.Format32bppArgb
;
372 case 3://JavaImage.TYPE_INT_ARGB_PRE:
373 return PixelFormat
.Format32bppPArgb
;
374 case 9://JavaImage.TYPE_USHORT_555_RGB:
375 return PixelFormat
.Format16bppRgb555
;
376 case 8://JavaImage.TYPE_USHORT_565_RGB:
377 return PixelFormat
.Format16bppRgb565
;
378 case 13://JavaImage.TYPE_BYTE_INDEXED:
379 return PixelFormat
.Indexed
;
381 case 12://JavaImage.TYPE_BYTE_BINARY:
382 case 0://JavaImage.TYPE_CUSTOM:
383 case 4://JavaImage.TYPE_INT_BGR:
384 case 5://JavaImage.TYPE_3BYTE_BGR:
385 case 6://JavaImage.TYPE_4BYTE_ABGR:
386 case 7://JavaImage.TYPE_4BYTE_ABGR_PRE:
388 return PixelFormat
.Undefined
;
396 public static Bitmap
FromHicon (IntPtr hicon
)
398 throw new NotImplementedException();
402 public static Bitmap
FromResource (IntPtr hinstance
, string bitmapName
) //TBD: Untested
404 throw new NotImplementedException();
408 public IntPtr
GetHbitmap ()
410 throw new NotImplementedException();
414 public IntPtr
GetHbitmap (Color background
)
416 throw new NotImplementedException();
420 public IntPtr
GetHicon ()
422 throw new NotImplementedException();