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 );
32 private Bitmap (SerializationInfo info, StreamingContext context) {
33 throw new NotImplementedException ();
36 public Bitmap (int width, int height, Graphics g)
37 :this (width, height, PixelFormat.Format32bppArgb) {
38 CurrentImage.HorizontalResolution = g.DpiX;
39 CurrentImage.VerticalResolution = g.DpiY;
42 public Bitmap (Image original)
43 :this (original, original.Size) {}
45 public Bitmap (Image orig, Size newSize)
46 :this (orig, newSize.Width, newSize.Height) {}
48 public Bitmap (Image orig, int width, int height)
49 :base (CreateScaledImage (orig, width, height), ImageFormat.MemoryBmp) {}
51 internal Bitmap (java.awt.Image nativeObject, ImageFormat format)
52 :base (nativeObject, format) {}
54 private Bitmap (java.awt.Image nativeObject, ImageFormat format, PixelFormat pixFormat)
55 :this (nativeObject, format) {
56 if (pixFormat != this.PixelFormat)
57 throw new NotImplementedException ("Converting PixelFormat is not implemented yet.");
60 public Bitmap (int width, int height)
61 :this (width, height, PixelFormat.Format32bppArgb) {}
63 public Bitmap (int width, int height, PixelFormat format)
65 new java.awt.image.BufferedImage (width, height,
66 ToBufferedImageFormat (format)),
70 public Bitmap (Stream stream)
71 :this (stream, false) {}
73 public Bitmap (string filename)
74 :this (filename, false) {}
76 public Bitmap (Stream stream, bool useIcm)
77 :this (stream, useIcm, null) {}
79 public Bitmap (string filename, bool useIcm)
80 :this (filename, useIcm, null) {}
82 internal Bitmap (Stream stream, bool useIcm, ImageFormat format) {
84 io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
85 Initialize (new stream.MemoryCacheImageInputStream (jis), format);
88 internal Bitmap (string filename, bool useIcm, ImageFormat format) {
89 using(FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
91 io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
92 Initialize (new stream.MemoryCacheImageInputStream (jis), format);
96 public Bitmap (Type type, string resource) {
97 using (Stream s = type.Assembly.GetManifestResourceStream (resource)) {
99 throw new ArgumentException("Resource '" + resource + "' could not be found in class '" + type.ToString() + "'");
101 io.InputStream jis = vmw.common.IOUtils.ToInputStream (s);
102 Initialize (new stream.MemoryCacheImageInputStream (jis), null);
106 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
108 throw new NotImplementedException();
113 #region Internal Initialization
115 private void Initialize (stream.ImageInputStream input, ImageFormat format) {
116 ImageCodec ic = null;
119 ic = ImageCodec.CreateReader(input);
121 ic = ImageCodec.CreateReader(format);
124 ic.NativeStream = input;
125 PlainImage pi = ic.ReadPlainImage();
126 base.Initialize( pi, false );
128 pi = ic.ReadNextPlainImage();
129 while ( pi != null) {
130 base.Initialize( pi, true );
131 pi = ic.ReadNextPlainImage();
134 _flags |= (int)(ImageFlags.ImageFlagsReadOnly | ImageFlags.ImageFlagsHasRealPixelSize);
136 catch (IOException ex) {
140 throw new OutOfMemoryException ("Out of memory");
150 protected override void InternalSave (stream.ImageOutputStream output, Guid clsid) {
152 ImageCodec ic = ImageCodec.CreateWriter( clsid );
155 PlainImage plainImage = CurrentImage;
156 plainImage.NativeImage.flush();
158 if ( ImageCodec.ClsidToImageFormat( clsid ).Equals( ImageFormat.Jpeg ) ) {
159 image.ColorModel cm = ((image.BufferedImage)CurrentImage.NativeImage).getColorModel();
161 if (cm is image.DirectColorModel) {
162 image.Raster raster = ((image.BufferedImage)CurrentImage.NativeImage).getRaster();
163 image.DataBuffer db = raster.getDataBuffer();
164 image.DirectColorModel dcm = (image.DirectColorModel)cm;
165 image.SinglePixelPackedSampleModel jpegSampleModel = new image.SinglePixelPackedSampleModel(
166 db.getDataType(), Width, Height,
167 new int[] {dcm.getRedMask(), dcm.getGreenMask(), dcm.getBlueMask()} );
169 image.BufferedImage tb = new image.BufferedImage(
171 image.Raster.createWritableRaster( jpegSampleModel, db, null ),
174 plainImage = new PlainImage( tb, plainImage.Thumbnails, ImageFormat.Jpeg, plainImage.HorizontalResolution, plainImage.VerticalResolution, plainImage.Dimension );
175 plainImage.NativeMetadata = plainImage.NativeMetadata;
180 ic.NativeStream = output;
181 ic.WritePlainImage( plainImage );
187 #region private statics: ToBufferedImageFormat, CreateScaledImage
189 private static int ToBufferedImageFormat (PixelFormat format) {
191 case PixelFormat.Format16bppGrayScale:
192 return BufferedImage.TYPE_USHORT_GRAY;
193 case PixelFormat.Format1bppIndexed:
194 return BufferedImage.TYPE_BYTE_GRAY;
195 case PixelFormat.Format32bppArgb:
196 return BufferedImage.TYPE_INT_ARGB;
197 case PixelFormat.Format32bppRgb:
198 return BufferedImage.TYPE_INT_RGB;
199 case PixelFormat.Format32bppPArgb:
200 return BufferedImage.TYPE_INT_ARGB_PRE;
201 case PixelFormat.Format16bppRgb555:
202 return BufferedImage.TYPE_USHORT_555_RGB;
203 case PixelFormat.Format16bppRgb565:
204 return BufferedImage.TYPE_USHORT_565_RGB;
205 case PixelFormat.Indexed:
206 return BufferedImage.TYPE_BYTE_INDEXED;
208 return BufferedImage.TYPE_INT_ARGB;
212 private static java.awt.Image CreateScaledImage(Image original, int width, int height) {
213 JavaImage oldscaled = original.CurrentImage.NativeImage.getScaledInstance(width, height,
214 JavaImage.SCALE_DEFAULT);
215 BufferedImage newimage = new BufferedImage(oldscaled.getWidth(null),
216 oldscaled.getHeight(null),
217 BufferedImage.TYPE_INT_ARGB);
218 java.awt.Graphics2D graphics2d = newimage.createGraphics();
219 graphics2d.drawImage(oldscaled, 0, 0, null);
220 graphics2d.dispose();
226 public Color GetPixel (int x, int y)
229 int argb = NativeObject.getRGB(x,y);
230 return Color.FromArgb(argb);
233 public void SetPixel (int x, int y, Color color)
235 int rgb = color.ToArgb();
236 NativeObject.setRGB(x,y,rgb);
241 public override object Clone () {
242 return new Bitmap ( (PlainImage)CurrentImage.Clone() );
245 public Bitmap Clone (Rectangle rect, PixelFormat pixFormat)
247 return Clone(new RectangleF( rect.X, rect.Y, rect.Width, rect.Height ), pixFormat);
250 public Bitmap Clone (RectangleF rect, PixelFormat pixFormat)
252 PlainImage plainImage = CurrentImage.Clone(false);
253 BufferedImage clone = new BufferedImage( (int)rect.Width, (int)rect.Height, ToBufferedImageFormat( pixFormat ) );
254 awt.Graphics2D g = clone.createGraphics();
256 g.drawImage( NativeObject, -(int)rect.X, -(int)rect.Y, null );
262 plainImage.NativeImage = clone;
263 return new Bitmap(plainImage);
268 // TBD: implement this
269 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format) {
270 throw new NotImplementedException();
274 #region MakeTransparent
275 public void MakeTransparent ()
277 Color clr = Color.FromArgb(0,0,0);
278 MakeTransparent (clr);
281 public void MakeTransparent (Color transparentColor)
283 image.WritableRaster raster = NativeObject.getRaster();
284 int numBands = raster.getNumBands();
288 int maxWidth = raster.getWidth() + raster.getMinX();
289 int maxHeight = raster.getHeight() + raster.getMinY();
290 int[] srcPix = new int[numBands];
292 for (int y = raster.getMinY(); y < maxHeight; y++) {
293 for (int x = raster.getMinX(); x < maxWidth; x++) {
294 /*srcPix =*/ raster.getPixel(x, y, srcPix);
295 if (srcPix[0] == transparentColor.R &&
296 srcPix[1] == transparentColor.G &&
297 srcPix[2] == transparentColor.B) {
299 raster.setPixel(x, y, srcPix);
306 #region SetResolution
307 public void SetResolution (float xDpi, float yDpi)
309 CurrentImage.HorizontalResolution = xDpi;
310 CurrentImage.VerticalResolution = yDpi;
315 // TBD: implement this
316 public void UnlockBits (BitmapData bitmap_data)
318 throw new NotImplementedException();
323 internal new BufferedImage NativeObject {
325 return (BufferedImage)base.NativeObject.CurrentImage.NativeImage;
329 protected override java.awt.Image[] CloneNativeObjects(java.awt.Image[] src) {
333 awt.Image[] dst = new awt.Image[src.Length];
334 for (int i = 0; i < dst.Length; i++) {
335 BufferedImage image = src[i] as BufferedImage;
337 throw new ArgumentException(String.Format("Unsupported image type '{0}'", src[i].ToString()), "src");
339 dst[i] = new BufferedImage(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
347 #region InternalPixelFormat
348 protected override PixelFormat InternalPixelFormat {
350 int t = NativeObject.getType();
352 case 11://JavaImage.TYPE_USHORT_GRAY:
353 return PixelFormat.Format16bppGrayScale;
354 case 10://JavaImage.TYPE_BYTE_GRAY:
355 return PixelFormat.Format1bppIndexed;
356 case 1: //JavaImage.TYPE_INT_RGB
357 return PixelFormat.Format32bppRgb;
358 case 2: //JavaImage.TYPE_INT_ARGB:
359 return PixelFormat.Format32bppArgb;
360 case 3://JavaImage.TYPE_INT_ARGB_PRE:
361 return PixelFormat.Format32bppPArgb;
362 case 9://JavaImage.TYPE_USHORT_555_RGB:
363 return PixelFormat.Format16bppRgb555;
364 case 8://JavaImage.TYPE_USHORT_565_RGB:
365 return PixelFormat.Format16bppRgb565;
366 case 13://JavaImage.TYPE_BYTE_INDEXED:
367 return PixelFormat.Indexed;
369 case 12://JavaImage.TYPE_BYTE_BINARY:
370 case 0://JavaImage.TYPE_CUSTOM:
371 case 4://JavaImage.TYPE_INT_BGR:
372 case 5://JavaImage.TYPE_3BYTE_BGR:
373 case 6://JavaImage.TYPE_4BYTE_ABGR:
374 case 7://JavaImage.TYPE_4BYTE_ABGR_PRE:
376 return PixelFormat.Undefined;
383 public static Bitmap FromHicon (IntPtr hicon)
385 throw new NotImplementedException();
388 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TBD: Untested
390 throw new NotImplementedException();
393 public IntPtr GetHbitmap ()
395 throw new NotImplementedException();
398 public IntPtr GetHbitmap (Color background)
400 throw new NotImplementedException();
403 public IntPtr GetHicon ()
405 throw new NotImplementedException();