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 {
22 Bitmap (PlainImage orig) {
23 base.Initialize( orig, false );
26 private Bitmap (SerializationInfo info, StreamingContext context) {
27 throw new NotImplementedException ();
30 public Bitmap (int width, int height, Graphics g)
31 :this (width, height, PixelFormat.Format32bppArgb) {
32 CurrentImage.HorizontalResolution = g.DpiX;
33 CurrentImage.VerticalResolution = g.DpiY;
36 public Bitmap (Image original)
37 :this (original, original.Size) {}
39 public Bitmap (Image orig, Size newSize)
40 :this (orig, newSize.Width, newSize.Height) {}
42 public Bitmap (Image orig, int width, int height)
43 :base (CreateScaledImage (orig, width, height), ImageFormat.MemoryBmp) {}
45 internal Bitmap (java.awt.Image nativeObject, ImageFormat format)
46 :base (nativeObject, format) {}
48 private Bitmap (java.awt.Image nativeObject, ImageFormat format, PixelFormat pixFormat)
49 :this (nativeObject, format) {
50 if (pixFormat != this.PixelFormat)
51 throw new NotImplementedException ("Converting PixelFormat is not implemented yet.");
54 public Bitmap (int width, int height)
55 :this (width, height, PixelFormat.Format32bppArgb) {}
57 public Bitmap (int width, int height, PixelFormat format)
59 new java.awt.image.BufferedImage (width, height,
60 ToBufferedImageFormat (format)),
64 public Bitmap (Stream stream)
65 :this (stream, false) {}
67 public Bitmap (string filename)
68 :this (filename, false) {}
70 public Bitmap (Stream stream, bool useIcm)
71 :this (stream, useIcm, null) {}
73 public Bitmap (string filename, bool useIcm)
74 :this (filename, useIcm, null) {}
76 internal Bitmap (Stream stream, bool useIcm, ImageFormat format) {
78 io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
79 Initialize (new stream.MemoryCacheImageInputStream (jis), format);
82 internal Bitmap (string filename, bool useIcm, ImageFormat format) {
84 java.io.File file = vmw.common.IOUtils.getJavaFile (filename);
86 throw new System.IO.FileNotFoundException (filename);
87 Initialize (new stream.FileImageInputStream (file), format);
90 public Bitmap (Type type, string resource) {
91 using (Stream s = type.Assembly.GetManifestResourceStream (resource)) {
93 throw new ArgumentException("Resource '" + resource + "' could not be found in class '" + type.ToString() + "'");
95 io.InputStream jis = vmw.common.IOUtils.ToInputStream (s);
96 Initialize (new stream.MemoryCacheImageInputStream (jis), null);
100 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
102 throw new NotImplementedException();
107 #region Internal Initialization
109 private void Initialize (stream.ImageInputStream input, ImageFormat format) {
110 ImageCodec ic = null;
113 ic = ImageCodec.CreateReader(input);
115 ic = ImageCodec.CreateReader(format);
118 ic.NativeStream = input;
119 PlainImage pi = ic.ReadPlainImage();
120 base.Initialize( pi, false );
122 pi = ic.ReadNextPlainImage();
123 while ( pi != null) {
124 base.Initialize( pi, true );
125 pi = ic.ReadNextPlainImage();
128 _flags |= (int)(ImageFlags.ImageFlagsReadOnly | ImageFlags.ImageFlagsHasRealPixelSize);
130 catch (IOException ex) {
134 throw new OutOfMemoryException ("Out of memory");
141 protected override void InternalSave (stream.ImageOutputStream output, Guid clsid) {
143 ImageCodec ic = ImageCodec.CreateWriter( clsid );
145 // .net saves in png if cannot find requested encoder. act id 316563
147 ic = ImageCodec.CreateWriter( ImageFormat.Png );
150 ic.NativeStream = output;
151 ic.WritePlainImage( CurrentImage );
156 catch (java.io.IOException ex) {
157 throw new System.IO.IOException(ex.Message, ex);
161 throw new NotSupportedException("The requested format encoder is not supported");
167 #region private statics: ToBufferedImageFormat, CreateScaledImage
169 private static int ToBufferedImageFormat (PixelFormat format) {
171 case PixelFormat.Format16bppGrayScale:
172 return BufferedImage.TYPE_USHORT_GRAY;
173 case PixelFormat.Format1bppIndexed:
174 return BufferedImage.TYPE_BYTE_GRAY;
175 case PixelFormat.Format32bppArgb:
176 return BufferedImage.TYPE_INT_ARGB;
177 case PixelFormat.Format32bppRgb:
178 return BufferedImage.TYPE_INT_RGB;
179 case PixelFormat.Format32bppPArgb:
180 return BufferedImage.TYPE_INT_ARGB_PRE;
181 case PixelFormat.Format16bppRgb555:
182 return BufferedImage.TYPE_USHORT_555_RGB;
183 case PixelFormat.Format16bppRgb565:
184 return BufferedImage.TYPE_USHORT_565_RGB;
185 case PixelFormat.Indexed:
186 return BufferedImage.TYPE_BYTE_INDEXED;
188 return BufferedImage.TYPE_INT_ARGB;
192 private static java.awt.Image CreateScaledImage(Image original, int width, int height) {
193 JavaImage oldscaled = original.CurrentImage.NativeImage.getScaledInstance(width, height,
194 JavaImage.SCALE_DEFAULT);
195 BufferedImage newimage = new BufferedImage(oldscaled.getWidth(null),
196 oldscaled.getHeight(null),
197 BufferedImage.TYPE_INT_ARGB);
198 java.awt.Graphics2D graphics2d = newimage.createGraphics();
199 graphics2d.drawImage(oldscaled, 0, 0, null);
200 graphics2d.dispose();
206 public Color GetPixel (int x, int y)
209 int argb = NativeObject.getRGB(x,y);
210 return Color.FromArgb(argb);
213 public void SetPixel (int x, int y, Color color)
215 int rgb = color.ToArgb();
216 NativeObject.setRGB(x,y,rgb);
221 public override object Clone () {
222 return new Bitmap ( (PlainImage)CurrentImage.Clone() );
225 public Bitmap Clone (Rectangle rect, PixelFormat pixFormat)
227 return Clone(new RectangleF( rect.X, rect.Y, rect.Width, rect.Height ), pixFormat);
230 public Bitmap Clone (RectangleF rect, PixelFormat pixFormat)
232 PlainImage plainImage = CurrentImage.Clone(false);
\r
233 BufferedImage clone = new BufferedImage( (int)rect.Width, (int)rect.Height, ToBufferedImageFormat( pixFormat ) );
\r
234 clone.getGraphics().drawImage( NativeObject, -(int)rect.X, -(int)rect.Y, null );
\r
236 plainImage.NativeImage = clone;
\r
237 return new Bitmap(plainImage);
\r
242 // TBD: implement this
243 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format) {
244 throw new NotImplementedException();
248 #region MakeTransparent
249 public void MakeTransparent ()
251 Color clr = GetPixel(0,0);
252 MakeTransparent (clr);
255 public void MakeTransparent (Color transparentColor)
257 byte A = transparentColor.A;
258 image.WritableRaster raster = NativeObject.getRaster();
259 int numBands = raster.getNumBands();
260 int maxWidth = raster.getWidth() + raster.getMinX();
261 int maxHeight = raster.getHeight() + raster.getMinY();
262 int[] srcPix = new int[numBands];
264 for (int y = raster.getMinY(); y < maxHeight; y++) {
265 for (int x = raster.getMinX(); x < maxWidth; x++) {
266 /*srcPix =*/ raster.getPixel(x, y, srcPix);
267 for (int z = 0; z < numBands; z++) {
268 int argb = srcPix[z];
269 if ((uint)argb >> 24 == A) {
279 #region SetResolution
280 public void SetResolution (float xDpi, float yDpi)
282 CurrentImage.HorizontalResolution = xDpi;
283 CurrentImage.VerticalResolution = yDpi;
288 // TBD: implement this
289 public void UnlockBits (BitmapData bitmap_data)
291 throw new NotImplementedException();
296 internal new BufferedImage NativeObject {
298 return (BufferedImage)base.NativeObject.CurrentImage.NativeImage;
302 protected override java.awt.Image[] CloneNativeObjects(java.awt.Image[] src) {
306 awt.Image[] dst = new awt.Image[src.Length];
307 for (int i = 0; i < dst.Length; i++) {
308 BufferedImage image = src[i] as BufferedImage;
310 throw new ArgumentException(String.Format("Unsupported image type '{0}'", src[i].ToString()), "src");
312 dst[i] = new BufferedImage(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
320 #region InternalPixelFormat
321 protected override PixelFormat InternalPixelFormat {
323 int t = NativeObject.getType();
325 case 11://JavaImage.TYPE_USHORT_GRAY:
326 return PixelFormat.Format16bppGrayScale;
327 case 10://JavaImage.TYPE_BYTE_GRAY:
328 return PixelFormat.Format1bppIndexed;
329 case 1: //JavaImage.TYPE_INT_RGB
330 return PixelFormat.Format32bppRgb;
331 case 2: //JavaImage.TYPE_INT_ARGB:
332 return PixelFormat.Format32bppArgb;
333 case 3://JavaImage.TYPE_INT_ARGB_PRE:
334 return PixelFormat.Format32bppPArgb;
335 case 9://JavaImage.TYPE_USHORT_555_RGB:
336 return PixelFormat.Format16bppRgb555;
337 case 8://JavaImage.TYPE_USHORT_565_RGB:
338 return PixelFormat.Format16bppRgb565;
339 case 13://JavaImage.TYPE_BYTE_INDEXED:
340 return PixelFormat.Indexed;
342 case 12://JavaImage.TYPE_BYTE_BINARY:
343 case 0://JavaImage.TYPE_CUSTOM:
344 case 4://JavaImage.TYPE_INT_BGR:
345 case 5://JavaImage.TYPE_3BYTE_BGR:
346 case 6://JavaImage.TYPE_4BYTE_ABGR:
347 case 7://JavaImage.TYPE_4BYTE_ABGR_PRE:
349 return PixelFormat.Undefined;
356 public static Bitmap FromHicon (IntPtr hicon)
358 throw new NotImplementedException();
361 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TBD: Untested
363 throw new NotImplementedException();
366 public IntPtr GetHbitmap ()
368 throw new NotImplementedException();
371 public IntPtr GetHbitmap (Color background)
373 throw new NotImplementedException();
376 public IntPtr GetHicon ()
378 throw new NotImplementedException();