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 );
154 throw new NotSupportedException("The requested format encoder is not supported");
160 #region private statics: ToBufferedImageFormat, CreateScaledImage
162 private static int ToBufferedImageFormat (PixelFormat format) {
164 case PixelFormat.Format16bppGrayScale:
165 return BufferedImage.TYPE_USHORT_GRAY;
166 case PixelFormat.Format1bppIndexed:
167 return BufferedImage.TYPE_BYTE_GRAY;
168 case PixelFormat.Format32bppArgb:
169 return BufferedImage.TYPE_INT_ARGB;
170 case PixelFormat.Format32bppRgb:
171 return BufferedImage.TYPE_INT_RGB;
172 case PixelFormat.Format32bppPArgb:
173 return BufferedImage.TYPE_INT_ARGB_PRE;
174 case PixelFormat.Format16bppRgb555:
175 return BufferedImage.TYPE_USHORT_555_RGB;
176 case PixelFormat.Format16bppRgb565:
177 return BufferedImage.TYPE_USHORT_565_RGB;
178 case PixelFormat.Indexed:
179 return BufferedImage.TYPE_BYTE_INDEXED;
185 private static java.awt.Image CreateScaledImage(Image original, int width, int height) {
186 JavaImage oldscaled = original.CurrentImage.NativeImage.getScaledInstance(width, height,
187 JavaImage.SCALE_DEFAULT);
188 BufferedImage newimage = new BufferedImage(oldscaled.getWidth(null),
189 oldscaled.getHeight(null),
190 BufferedImage.TYPE_INT_ARGB);
191 java.awt.Graphics2D graphics2d = newimage.createGraphics();
192 graphics2d.drawImage(oldscaled, 0, 0, null);
193 graphics2d.dispose();
199 public Color GetPixel (int x, int y)
202 int argb = NativeObject.getRGB(x,y);
203 return Color.FromArgb(argb);
206 public void SetPixel (int x, int y, Color color)
208 int rgb = color.ToArgb();
209 NativeObject.setRGB(x,y,rgb);
214 public override object Clone () {
215 return new Bitmap ( (PlainImage)CurrentImage.Clone() );
218 public Bitmap Clone (Rectangle rect, PixelFormat pixFormat)
220 return Clone(new RectangleF( rect.X, rect.Y, rect.Width, rect.Height ), pixFormat);
223 public Bitmap Clone (RectangleF rect, PixelFormat pixFormat)
225 PlainImage plainImage = (PlainImage)CurrentImage.Clone();
226 plainImage.NativeImage = ((BufferedImage)plainImage.NativeImage).getSubimage((int)rect.X,(int)rect.Y,(int)rect.Width,(int)rect.Height);
228 if (pixFormat != this.PixelFormat)
229 throw new NotImplementedException ("Converting PixelFormat is not implemented yet.");
231 return new Bitmap(plainImage);
236 // TBD: implement this
237 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format) {
238 throw new NotImplementedException();
242 #region MakeTransparent
243 public void MakeTransparent ()
245 Color clr = GetPixel(0,0);
246 MakeTransparent (clr);
249 public void MakeTransparent (Color transparentColor)
251 byte A = transparentColor.A;
252 image.WritableRaster raster = NativeObject.getRaster();
253 int numBands = raster.getNumBands();
254 int maxWidth = raster.getWidth() + raster.getMinX();
255 int maxHeight = raster.getHeight() + raster.getMinY();
256 int[] srcPix = new int[numBands];
258 for (int y = raster.getMinY(); y < maxHeight; y++) {
259 for (int x = raster.getMinX(); x < maxWidth; x++) {
260 /*srcPix =*/ raster.getPixel(x, y, srcPix);
261 for (int z = 0; z < numBands; z++) {
262 int argb = srcPix[z];
263 if ((uint)argb >> 24 == A) {
273 #region SetResolution
274 public void SetResolution (float xDpi, float yDpi)
276 CurrentImage.HorizontalResolution = xDpi;
277 CurrentImage.VerticalResolution = yDpi;
282 // TBD: implement this
283 public void UnlockBits (BitmapData bitmap_data)
285 throw new NotImplementedException();
290 internal new BufferedImage NativeObject {
292 return (BufferedImage)base.NativeObject.CurrentImage.NativeImage;
296 protected override java.awt.Image[] CloneNativeObjects(java.awt.Image[] src) {
300 awt.Image[] dst = new awt.Image[src.Length];
301 for (int i = 0; i < dst.Length; i++) {
302 BufferedImage image = src[i] as BufferedImage;
304 throw new ArgumentException(String.Format("Unsupported image type '{0}'", src[i].ToString()), "src");
306 dst[i] = new BufferedImage(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
314 #region InternalPixelFormat
315 protected override PixelFormat InternalPixelFormat {
317 int t = NativeObject.getType();
319 case 11://JavaImage.TYPE_USHORT_GRAY:
320 return PixelFormat.Format16bppGrayScale;
321 case 10://JavaImage.TYPE_BYTE_GRAY:
322 return PixelFormat.Format1bppIndexed;
323 case 1: //JavaImage.TYPE_INT_RGB
324 return PixelFormat.Format32bppRgb;
325 case 2: //JavaImage.TYPE_INT_ARGB:
326 return PixelFormat.Format32bppArgb;
327 case 3://JavaImage.TYPE_INT_ARGB_PRE:
328 return PixelFormat.Format32bppPArgb;
329 case 9://JavaImage.TYPE_USHORT_555_RGB:
330 return PixelFormat.Format16bppRgb555;
331 case 8://JavaImage.TYPE_USHORT_565_RGB:
332 return PixelFormat.Format16bppRgb565;
333 case 13://JavaImage.TYPE_BYTE_INDEXED:
334 return PixelFormat.Indexed;
336 case 12://JavaImage.TYPE_BYTE_BINARY:
337 case 0://JavaImage.TYPE_CUSTOM:
338 case 4://JavaImage.TYPE_INT_BGR:
339 case 5://JavaImage.TYPE_3BYTE_BGR:
340 case 6://JavaImage.TYPE_4BYTE_ABGR:
341 case 7://JavaImage.TYPE_4BYTE_ABGR_PRE:
343 return PixelFormat.Undefined;
350 public static Bitmap FromHicon (IntPtr hicon)
352 throw new NotImplementedException();
355 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TBD: Untested
357 throw new NotImplementedException();
360 public IntPtr GetHbitmap ()
362 throw new NotImplementedException();
365 public IntPtr GetHbitmap (Color background)
367 throw new NotImplementedException();
370 public IntPtr GetHicon ()
372 throw new NotImplementedException();