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) {}
78 public Bitmap (Stream stream, bool useIcm)
79 :this (stream, useIcm, null) {}
81 public Bitmap (string filename, bool useIcm)
82 :this (filename, useIcm, null) {}
84 internal Bitmap (Stream stream, bool useIcm, ImageFormat format) {
86 io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
87 Initialize (new stream.MemoryCacheImageInputStream (jis), format);
90 internal Bitmap (string filename, bool useIcm, ImageFormat format) {
91 using(FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
93 io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
94 Initialize (new stream.MemoryCacheImageInputStream (jis), format);
98 public Bitmap (Type type, string resource) {
99 using (Stream s = type.Assembly.GetManifestResourceStream (resource)) {
101 throw new ArgumentException("Resource '" + resource + "' could not be found in class '" + type.ToString() + "'");
103 io.InputStream jis = vmw.common.IOUtils.ToInputStream (s);
104 Initialize (new stream.MemoryCacheImageInputStream (jis), null);
109 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
111 throw new NotImplementedException();
116 #region Internal Initialization
118 private void Initialize (stream.ImageInputStream input, ImageFormat format) {
119 ImageCodec ic = null;
122 ic = ImageCodec.CreateReader(input);
124 ic = ImageCodec.CreateReader(format);
127 ic.NativeStream = input;
128 PlainImage pi = ic.ReadPlainImage();
129 base.Initialize( pi, false );
131 pi = ic.ReadNextPlainImage();
132 while ( pi != null) {
133 base.Initialize( pi, true );
134 pi = ic.ReadNextPlainImage();
137 _flags |= (int)(ImageFlags.ImageFlagsReadOnly | ImageFlags.ImageFlagsHasRealPixelSize);
139 catch (IOException ex) {
143 throw new OutOfMemoryException ("Out of memory");
153 protected override void InternalSave (stream.ImageOutputStream output, Guid clsid) {
155 ImageCodec ic = ImageCodec.CreateWriter( clsid );
158 PlainImage plainImage = CurrentImage;
159 plainImage.NativeImage.flush();
161 if ( ImageCodec.ClsidToImageFormat( clsid ).Equals( ImageFormat.Jpeg ) ) {
162 image.ColorModel cm = ((image.BufferedImage)CurrentImage.NativeImage).getColorModel();
164 if (cm is image.DirectColorModel) {
165 image.Raster raster = ((image.BufferedImage)CurrentImage.NativeImage).getRaster();
166 image.DataBuffer db = raster.getDataBuffer();
167 image.DirectColorModel dcm = (image.DirectColorModel)cm;
168 image.SinglePixelPackedSampleModel jpegSampleModel = new image.SinglePixelPackedSampleModel(
169 db.getDataType(), Width, Height,
170 new int[] {dcm.getRedMask(), dcm.getGreenMask(), dcm.getBlueMask()} );
172 image.BufferedImage tb = new image.BufferedImage(
174 image.Raster.createWritableRaster( jpegSampleModel, db, null ),
177 plainImage = new PlainImage( tb, plainImage.Thumbnails, ImageFormat.Jpeg, plainImage.HorizontalResolution, plainImage.VerticalResolution, plainImage.Dimension );
178 plainImage.NativeMetadata = plainImage.NativeMetadata;
183 ic.NativeStream = output;
184 ic.WritePlainImage( plainImage );
190 #region private statics: ToBufferedImageFormat, CreateScaledImage
192 private static int ToBufferedImageFormat (PixelFormat format) {
194 case PixelFormat.Format16bppGrayScale:
195 return BufferedImage.TYPE_USHORT_GRAY;
196 case PixelFormat.Format1bppIndexed:
197 return BufferedImage.TYPE_BYTE_GRAY;
198 case PixelFormat.Format32bppArgb:
199 return BufferedImage.TYPE_INT_ARGB;
200 case PixelFormat.Format32bppRgb:
201 return BufferedImage.TYPE_INT_RGB;
202 case PixelFormat.Format32bppPArgb:
203 return BufferedImage.TYPE_INT_ARGB_PRE;
204 case PixelFormat.Format16bppRgb555:
205 return BufferedImage.TYPE_USHORT_555_RGB;
206 case PixelFormat.Format16bppRgb565:
207 return BufferedImage.TYPE_USHORT_565_RGB;
208 case PixelFormat.Indexed:
209 return BufferedImage.TYPE_BYTE_INDEXED;
211 return BufferedImage.TYPE_INT_ARGB;
215 private static java.awt.Image CreateScaledImage(Image original, int width, int height) {
216 JavaImage oldscaled = original.CurrentImage.NativeImage.getScaledInstance(width, height,
217 JavaImage.SCALE_DEFAULT);
218 BufferedImage newimage = new BufferedImage(oldscaled.getWidth(null),
219 oldscaled.getHeight(null),
220 BufferedImage.TYPE_INT_ARGB);
221 java.awt.Graphics2D graphics2d = newimage.createGraphics();
222 graphics2d.drawImage(oldscaled, 0, 0, null);
223 graphics2d.dispose();
229 public Color GetPixel (int x, int y)
232 int argb = NativeObject.getRGB(x,y);
233 return Color.FromArgb(argb);
236 public void SetPixel (int x, int y, Color color)
238 int rgb = color.ToArgb();
239 NativeObject.setRGB(x,y,rgb);
244 public override object Clone () {
245 return new Bitmap ( (PlainImage)CurrentImage.Clone() );
248 public Bitmap Clone (Rectangle rect, PixelFormat pixFormat)
250 return Clone(new RectangleF( rect.X, rect.Y, rect.Width, rect.Height ), pixFormat);
253 public Bitmap Clone (RectangleF rect, PixelFormat pixFormat)
255 PlainImage plainImage = CurrentImage.Clone(false);
256 BufferedImage clone = new BufferedImage( (int)rect.Width, (int)rect.Height, ToBufferedImageFormat( pixFormat ) );
257 awt.Graphics2D g = clone.createGraphics();
259 g.drawImage( NativeObject, -(int)rect.X, -(int)rect.Y, null );
265 plainImage.NativeImage = clone;
266 return new Bitmap(plainImage);
272 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format) {
273 throw new NotImplementedException();
277 #region MakeTransparent
278 public void MakeTransparent ()
280 Color clr = Color.FromArgb(0,0,0);
281 MakeTransparent (clr);
284 public void MakeTransparent (Color transparentColor)
286 image.WritableRaster raster = NativeObject.getRaster();
287 int numBands = raster.getNumBands();
291 int maxWidth = raster.getWidth() + raster.getMinX();
292 int maxHeight = raster.getHeight() + raster.getMinY();
293 int[] srcPix = new int[numBands];
295 for (int y = raster.getMinY(); y < maxHeight; y++) {
296 for (int x = raster.getMinX(); x < maxWidth; x++) {
297 /*srcPix =*/ raster.getPixel(x, y, srcPix);
298 if (srcPix[0] == transparentColor.R &&
299 srcPix[1] == transparentColor.G &&
300 srcPix[2] == transparentColor.B) {
302 raster.setPixel(x, y, srcPix);
309 #region SetResolution
310 public void SetResolution (float xDpi, float yDpi)
312 CurrentImage.HorizontalResolution = xDpi;
313 CurrentImage.VerticalResolution = yDpi;
319 public void UnlockBits (BitmapData bitmap_data)
321 throw new NotImplementedException();
326 internal new BufferedImage NativeObject {
328 return (BufferedImage)base.NativeObject.CurrentImage.NativeImage;
332 protected override java.awt.Image[] CloneNativeObjects(java.awt.Image[] src) {
336 awt.Image[] dst = new awt.Image[src.Length];
337 for (int i = 0; i < dst.Length; i++) {
338 BufferedImage image = src[i] as BufferedImage;
340 throw new ArgumentException(String.Format("Unsupported image type '{0}'", src[i].ToString()), "src");
342 dst[i] = new BufferedImage(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
350 #region InternalPixelFormat
351 protected override PixelFormat InternalPixelFormat {
353 int t = NativeObject.getType();
355 case 11://JavaImage.TYPE_USHORT_GRAY:
356 return PixelFormat.Format16bppGrayScale;
357 case 10://JavaImage.TYPE_BYTE_GRAY:
358 return PixelFormat.Format1bppIndexed;
359 case 1: //JavaImage.TYPE_INT_RGB
360 return PixelFormat.Format32bppRgb;
361 case 2: //JavaImage.TYPE_INT_ARGB:
362 return PixelFormat.Format32bppArgb;
363 case 3://JavaImage.TYPE_INT_ARGB_PRE:
364 return PixelFormat.Format32bppPArgb;
365 case 9://JavaImage.TYPE_USHORT_555_RGB:
366 return PixelFormat.Format16bppRgb555;
367 case 8://JavaImage.TYPE_USHORT_565_RGB:
368 return PixelFormat.Format16bppRgb565;
369 case 13://JavaImage.TYPE_BYTE_INDEXED:
370 return PixelFormat.Indexed;
372 case 12://JavaImage.TYPE_BYTE_BINARY:
373 case 0://JavaImage.TYPE_CUSTOM:
374 case 4://JavaImage.TYPE_INT_BGR:
375 case 5://JavaImage.TYPE_3BYTE_BGR:
376 case 6://JavaImage.TYPE_4BYTE_ABGR:
377 case 7://JavaImage.TYPE_4BYTE_ABGR_PRE:
379 return PixelFormat.Undefined;
387 public static Bitmap FromHicon (IntPtr hicon)
389 throw new NotImplementedException();
393 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TBD: Untested
395 throw new NotImplementedException();
399 public IntPtr GetHbitmap ()
401 throw new NotImplementedException();
405 public IntPtr GetHbitmap (Color background)
407 throw new NotImplementedException();
411 public IntPtr GetHicon ()
413 throw new NotImplementedException();